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 int route_choosen; 45951ebd318SNicolas Dichtel 460b673d6ccSJakub Sitnicki /* We might have already computed the hash for ICMPv6 errors. In such 461b673d6ccSJakub Sitnicki * case it will always be non-zero. Otherwise now is the time to do it. 462b673d6ccSJakub Sitnicki */ 463b673d6ccSJakub Sitnicki if (!fl6->mp_hash) 464b673d6ccSJakub Sitnicki fl6->mp_hash = rt6_multipath_hash(fl6, NULL); 465b673d6ccSJakub Sitnicki 466b673d6ccSJakub Sitnicki route_choosen = fl6->mp_hash % (match->rt6i_nsiblings + 1); 46751ebd318SNicolas Dichtel /* Don't change the route, if route_choosen == 0 46851ebd318SNicolas Dichtel * (siblings does not include ourself) 46951ebd318SNicolas Dichtel */ 47051ebd318SNicolas Dichtel if (route_choosen) 47151ebd318SNicolas Dichtel list_for_each_entry_safe(sibling, next_sibling, 47251ebd318SNicolas Dichtel &match->rt6i_siblings, rt6i_siblings) { 47351ebd318SNicolas Dichtel route_choosen--; 47451ebd318SNicolas Dichtel if (route_choosen == 0) { 475bbfcd776SIdo Schimmel struct inet6_dev *idev = sibling->rt6i_idev; 476bbfcd776SIdo Schimmel 477bbfcd776SIdo Schimmel if (!netif_carrier_ok(sibling->dst.dev) && 478bbfcd776SIdo Schimmel idev->cnf.ignore_routes_with_linkdown) 479bbfcd776SIdo Schimmel break; 48052bd4c0cSNicolas Dichtel if (rt6_score_route(sibling, oif, strict) < 0) 48152bd4c0cSNicolas Dichtel break; 48251ebd318SNicolas Dichtel match = sibling; 48351ebd318SNicolas Dichtel break; 48451ebd318SNicolas Dichtel } 48551ebd318SNicolas Dichtel } 48651ebd318SNicolas Dichtel return match; 48751ebd318SNicolas Dichtel } 48851ebd318SNicolas Dichtel 4891da177e4SLinus Torvalds /* 49066f5d6ceSWei Wang * Route lookup. rcu_read_lock() should be held. 4911da177e4SLinus Torvalds */ 4921da177e4SLinus Torvalds 4938ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net, 4948ed67789SDaniel Lezcano struct rt6_info *rt, 495b71d1d42SEric Dumazet const struct in6_addr *saddr, 4961da177e4SLinus Torvalds int oif, 497d420895eSYOSHIFUJI Hideaki int flags) 4981da177e4SLinus Torvalds { 4991da177e4SLinus Torvalds struct rt6_info *local = NULL; 5001da177e4SLinus Torvalds struct rt6_info *sprt; 5011da177e4SLinus Torvalds 502dd3abc4eSYOSHIFUJI Hideaki if (!oif && ipv6_addr_any(saddr)) 503dd3abc4eSYOSHIFUJI Hideaki goto out; 504dd3abc4eSYOSHIFUJI Hideaki 505071fb37eSDavid Miller for (sprt = rt; sprt; sprt = rcu_dereference(sprt->rt6_next)) { 506d1918542SDavid S. Miller struct net_device *dev = sprt->dst.dev; 507dd3abc4eSYOSHIFUJI Hideaki 508dd3abc4eSYOSHIFUJI Hideaki if (oif) { 5091da177e4SLinus Torvalds if (dev->ifindex == oif) 5101da177e4SLinus Torvalds return sprt; 5111da177e4SLinus Torvalds if (dev->flags & IFF_LOOPBACK) { 51238308473SDavid S. Miller if (!sprt->rt6i_idev || 5131da177e4SLinus Torvalds sprt->rt6i_idev->dev->ifindex != oif) { 51417fb0b2bSDavid Ahern if (flags & RT6_LOOKUP_F_IFACE) 5151da177e4SLinus Torvalds continue; 51617fb0b2bSDavid Ahern if (local && 51717fb0b2bSDavid Ahern local->rt6i_idev->dev->ifindex == oif) 5181da177e4SLinus Torvalds continue; 5191da177e4SLinus Torvalds } 5201da177e4SLinus Torvalds local = sprt; 5211da177e4SLinus Torvalds } 522dd3abc4eSYOSHIFUJI Hideaki } else { 523dd3abc4eSYOSHIFUJI Hideaki if (ipv6_chk_addr(net, saddr, dev, 524dd3abc4eSYOSHIFUJI Hideaki flags & RT6_LOOKUP_F_IFACE)) 525dd3abc4eSYOSHIFUJI Hideaki return sprt; 526dd3abc4eSYOSHIFUJI Hideaki } 5271da177e4SLinus Torvalds } 5281da177e4SLinus Torvalds 529dd3abc4eSYOSHIFUJI Hideaki if (oif) { 5301da177e4SLinus Torvalds if (local) 5311da177e4SLinus Torvalds return local; 5321da177e4SLinus Torvalds 533d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE) 5348ed67789SDaniel Lezcano return net->ipv6.ip6_null_entry; 5351da177e4SLinus Torvalds } 536dd3abc4eSYOSHIFUJI Hideaki out: 5371da177e4SLinus Torvalds return rt; 5381da177e4SLinus Torvalds } 5391da177e4SLinus Torvalds 54027097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 541c2f17e82SHannes Frederic Sowa struct __rt6_probe_work { 542c2f17e82SHannes Frederic Sowa struct work_struct work; 543c2f17e82SHannes Frederic Sowa struct in6_addr target; 544c2f17e82SHannes Frederic Sowa struct net_device *dev; 545c2f17e82SHannes Frederic Sowa }; 546c2f17e82SHannes Frederic Sowa 547c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w) 548c2f17e82SHannes Frederic Sowa { 549c2f17e82SHannes Frederic Sowa struct in6_addr mcaddr; 550c2f17e82SHannes Frederic Sowa struct __rt6_probe_work *work = 551c2f17e82SHannes Frederic Sowa container_of(w, struct __rt6_probe_work, work); 552c2f17e82SHannes Frederic Sowa 553c2f17e82SHannes Frederic Sowa addrconf_addr_solict_mult(&work->target, &mcaddr); 554adc176c5SErik Nordmark ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0); 555c2f17e82SHannes Frederic Sowa dev_put(work->dev); 556662f5533SMichael Büsch kfree(work); 557c2f17e82SHannes Frederic Sowa } 558c2f17e82SHannes Frederic Sowa 55927097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt) 56027097255SYOSHIFUJI Hideaki { 561990edb42SMartin KaFai Lau struct __rt6_probe_work *work; 562f2c31e32SEric Dumazet struct neighbour *neigh; 56327097255SYOSHIFUJI Hideaki /* 56427097255SYOSHIFUJI Hideaki * Okay, this does not seem to be appropriate 56527097255SYOSHIFUJI Hideaki * for now, however, we need to check if it 56627097255SYOSHIFUJI Hideaki * is really so; aka Router Reachability Probing. 56727097255SYOSHIFUJI Hideaki * 56827097255SYOSHIFUJI Hideaki * Router Reachability Probe MUST be rate-limited 56927097255SYOSHIFUJI Hideaki * to no more than one per minute. 57027097255SYOSHIFUJI Hideaki */ 5712152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (!rt || !(rt->rt6i_flags & RTF_GATEWAY)) 572fdd6681dSAmerigo Wang return; 5732152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 5742152caeaSYOSHIFUJI Hideaki / 吉藤英明 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 5752152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 5768d6c31bfSMartin KaFai Lau if (neigh->nud_state & NUD_VALID) 5778d6c31bfSMartin KaFai Lau goto out; 5788d6c31bfSMartin KaFai Lau 579990edb42SMartin KaFai Lau work = NULL; 5802152caeaSYOSHIFUJI Hideaki / 吉藤英明 write_lock(&neigh->lock); 581990edb42SMartin KaFai Lau if (!(neigh->nud_state & NUD_VALID) && 582990edb42SMartin KaFai Lau time_after(jiffies, 583990edb42SMartin KaFai Lau neigh->updated + 584990edb42SMartin KaFai Lau rt->rt6i_idev->cnf.rtr_probe_interval)) { 585c2f17e82SHannes Frederic Sowa work = kmalloc(sizeof(*work), GFP_ATOMIC); 586990edb42SMartin KaFai Lau if (work) 5877e980569SJiri Benc __neigh_set_probe_once(neigh); 588990edb42SMartin KaFai Lau } 589c2f17e82SHannes Frederic Sowa write_unlock(&neigh->lock); 590990edb42SMartin KaFai Lau } else { 591990edb42SMartin KaFai Lau work = kmalloc(sizeof(*work), GFP_ATOMIC); 592990edb42SMartin KaFai Lau } 593c2f17e82SHannes Frederic Sowa 594c2f17e82SHannes Frederic Sowa if (work) { 595c2f17e82SHannes Frederic Sowa INIT_WORK(&work->work, rt6_probe_deferred); 596c2f17e82SHannes Frederic Sowa work->target = rt->rt6i_gateway; 597c2f17e82SHannes Frederic Sowa dev_hold(rt->dst.dev); 598c2f17e82SHannes Frederic Sowa work->dev = rt->dst.dev; 599c2f17e82SHannes Frederic Sowa schedule_work(&work->work); 600c2f17e82SHannes Frederic Sowa } 601990edb42SMartin KaFai Lau 6028d6c31bfSMartin KaFai Lau out: 6032152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 604f2c31e32SEric Dumazet } 60527097255SYOSHIFUJI Hideaki #else 60627097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt) 60727097255SYOSHIFUJI Hideaki { 60827097255SYOSHIFUJI Hideaki } 60927097255SYOSHIFUJI Hideaki #endif 61027097255SYOSHIFUJI Hideaki 6111da177e4SLinus Torvalds /* 612554cfb7eSYOSHIFUJI Hideaki * Default Router Selection (RFC 2461 6.3.6) 6131da177e4SLinus Torvalds */ 614b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif) 6151da177e4SLinus Torvalds { 616d1918542SDavid S. Miller struct net_device *dev = rt->dst.dev; 617161980f4SDavid S. Miller if (!oif || dev->ifindex == oif) 618554cfb7eSYOSHIFUJI Hideaki return 2; 619161980f4SDavid S. Miller if ((dev->flags & IFF_LOOPBACK) && 620161980f4SDavid S. Miller rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif) 621161980f4SDavid S. Miller return 1; 622554cfb7eSYOSHIFUJI Hideaki return 0; 6231da177e4SLinus Torvalds } 6241da177e4SLinus Torvalds 625afc154e9SHannes Frederic Sowa static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt) 6261da177e4SLinus Torvalds { 627f2c31e32SEric Dumazet struct neighbour *neigh; 628afc154e9SHannes Frederic Sowa enum rt6_nud_state ret = RT6_NUD_FAIL_HARD; 629f2c31e32SEric Dumazet 6304d0c5911SYOSHIFUJI Hideaki if (rt->rt6i_flags & RTF_NONEXTHOP || 6314d0c5911SYOSHIFUJI Hideaki !(rt->rt6i_flags & RTF_GATEWAY)) 632afc154e9SHannes Frederic Sowa return RT6_NUD_SUCCEED; 633145a3621SYOSHIFUJI Hideaki / 吉藤英明 634145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 635145a3621SYOSHIFUJI Hideaki / 吉藤英明 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 636145a3621SYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 637145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_lock(&neigh->lock); 638554cfb7eSYOSHIFUJI Hideaki if (neigh->nud_state & NUD_VALID) 639afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 640398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 641a5a81f0bSPaul Marks else if (!(neigh->nud_state & NUD_FAILED)) 642afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 6437e980569SJiri Benc else 6447e980569SJiri Benc ret = RT6_NUD_FAIL_PROBE; 645398bcbebSYOSHIFUJI Hideaki #endif 646145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_unlock(&neigh->lock); 647afc154e9SHannes Frederic Sowa } else { 648afc154e9SHannes Frederic Sowa ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ? 6497e980569SJiri Benc RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR; 650a5a81f0bSPaul Marks } 651145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 652145a3621SYOSHIFUJI Hideaki / 吉藤英明 653a5a81f0bSPaul Marks return ret; 6541da177e4SLinus Torvalds } 6551da177e4SLinus Torvalds 656554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif, 657554cfb7eSYOSHIFUJI Hideaki int strict) 658554cfb7eSYOSHIFUJI Hideaki { 659a5a81f0bSPaul Marks int m; 6604d0c5911SYOSHIFUJI Hideaki 6614d0c5911SYOSHIFUJI Hideaki m = rt6_check_dev(rt, oif); 66277d16f45SYOSHIFUJI Hideaki if (!m && (strict & RT6_LOOKUP_F_IFACE)) 663afc154e9SHannes Frederic Sowa return RT6_NUD_FAIL_HARD; 664ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 665ebacaaa0SYOSHIFUJI Hideaki m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2; 666ebacaaa0SYOSHIFUJI Hideaki #endif 667afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) { 668afc154e9SHannes Frederic Sowa int n = rt6_check_neigh(rt); 669afc154e9SHannes Frederic Sowa if (n < 0) 670afc154e9SHannes Frederic Sowa return n; 671afc154e9SHannes Frederic Sowa } 672554cfb7eSYOSHIFUJI Hideaki return m; 673554cfb7eSYOSHIFUJI Hideaki } 674554cfb7eSYOSHIFUJI Hideaki 675f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict, 676afc154e9SHannes Frederic Sowa int *mpri, struct rt6_info *match, 677afc154e9SHannes Frederic Sowa bool *do_rr) 678554cfb7eSYOSHIFUJI Hideaki { 679554cfb7eSYOSHIFUJI Hideaki int m; 680afc154e9SHannes Frederic Sowa bool match_do_rr = false; 68135103d11SAndy Gospodarek struct inet6_dev *idev = rt->rt6i_idev; 68235103d11SAndy Gospodarek struct net_device *dev = rt->dst.dev; 68335103d11SAndy Gospodarek 68435103d11SAndy Gospodarek if (dev && !netif_carrier_ok(dev) && 685d5d32e4bSDavid Ahern idev->cnf.ignore_routes_with_linkdown && 686d5d32e4bSDavid Ahern !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE)) 68735103d11SAndy Gospodarek goto out; 688554cfb7eSYOSHIFUJI Hideaki 689554cfb7eSYOSHIFUJI Hideaki if (rt6_check_expired(rt)) 690f11e6659SDavid S. Miller goto out; 691554cfb7eSYOSHIFUJI Hideaki 692554cfb7eSYOSHIFUJI Hideaki m = rt6_score_route(rt, oif, strict); 6937e980569SJiri Benc if (m == RT6_NUD_FAIL_DO_RR) { 694afc154e9SHannes Frederic Sowa match_do_rr = true; 695afc154e9SHannes Frederic Sowa m = 0; /* lowest valid score */ 6967e980569SJiri Benc } else if (m == RT6_NUD_FAIL_HARD) { 697f11e6659SDavid S. Miller goto out; 6981da177e4SLinus Torvalds } 699f11e6659SDavid S. Miller 700afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) 701afc154e9SHannes Frederic Sowa rt6_probe(rt); 702afc154e9SHannes Frederic Sowa 7037e980569SJiri Benc /* note that m can be RT6_NUD_FAIL_PROBE at this point */ 704afc154e9SHannes Frederic Sowa if (m > *mpri) { 705afc154e9SHannes Frederic Sowa *do_rr = match_do_rr; 706afc154e9SHannes Frederic Sowa *mpri = m; 707afc154e9SHannes Frederic Sowa match = rt; 708afc154e9SHannes Frederic Sowa } 709f11e6659SDavid S. Miller out: 710f11e6659SDavid S. Miller return match; 7111da177e4SLinus Torvalds } 7121da177e4SLinus Torvalds 713f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn, 7148d1040e8SWei Wang struct rt6_info *leaf, 715f11e6659SDavid S. Miller struct rt6_info *rr_head, 716afc154e9SHannes Frederic Sowa u32 metric, int oif, int strict, 717afc154e9SHannes Frederic Sowa bool *do_rr) 718f11e6659SDavid S. Miller { 7199fbdcfafSSteffen Klassert struct rt6_info *rt, *match, *cont; 720f11e6659SDavid S. Miller int mpri = -1; 721f11e6659SDavid S. Miller 722f11e6659SDavid S. Miller match = NULL; 7239fbdcfafSSteffen Klassert cont = NULL; 724071fb37eSDavid Miller for (rt = rr_head; rt; rt = rcu_dereference(rt->rt6_next)) { 7259fbdcfafSSteffen Klassert if (rt->rt6i_metric != metric) { 7269fbdcfafSSteffen Klassert cont = rt; 7279fbdcfafSSteffen Klassert break; 7289fbdcfafSSteffen Klassert } 7299fbdcfafSSteffen Klassert 730afc154e9SHannes Frederic Sowa match = find_match(rt, oif, strict, &mpri, match, do_rr); 7319fbdcfafSSteffen Klassert } 7329fbdcfafSSteffen Klassert 73366f5d6ceSWei Wang for (rt = leaf; rt && rt != rr_head; 734071fb37eSDavid Miller rt = rcu_dereference(rt->rt6_next)) { 7359fbdcfafSSteffen Klassert if (rt->rt6i_metric != metric) { 7369fbdcfafSSteffen Klassert cont = rt; 7379fbdcfafSSteffen Klassert break; 7389fbdcfafSSteffen Klassert } 7399fbdcfafSSteffen Klassert 7409fbdcfafSSteffen Klassert match = find_match(rt, oif, strict, &mpri, match, do_rr); 7419fbdcfafSSteffen Klassert } 7429fbdcfafSSteffen Klassert 7439fbdcfafSSteffen Klassert if (match || !cont) 7449fbdcfafSSteffen Klassert return match; 7459fbdcfafSSteffen Klassert 746071fb37eSDavid Miller for (rt = cont; rt; rt = rcu_dereference(rt->rt6_next)) 747afc154e9SHannes Frederic Sowa match = find_match(rt, oif, strict, &mpri, match, do_rr); 748f11e6659SDavid S. Miller 749f11e6659SDavid S. Miller return match; 750f11e6659SDavid S. Miller } 751f11e6659SDavid S. Miller 7528d1040e8SWei Wang static struct rt6_info *rt6_select(struct net *net, struct fib6_node *fn, 7538d1040e8SWei Wang int oif, int strict) 754f11e6659SDavid S. Miller { 75566f5d6ceSWei Wang struct rt6_info *leaf = rcu_dereference(fn->leaf); 756f11e6659SDavid S. Miller struct rt6_info *match, *rt0; 757afc154e9SHannes Frederic Sowa bool do_rr = false; 75817ecf590SWei Wang int key_plen; 759f11e6659SDavid S. Miller 76087b1af8dSWei Wang if (!leaf || leaf == net->ipv6.ip6_null_entry) 7618d1040e8SWei Wang return net->ipv6.ip6_null_entry; 7628d1040e8SWei Wang 76366f5d6ceSWei Wang rt0 = rcu_dereference(fn->rr_ptr); 764f11e6659SDavid S. Miller if (!rt0) 76566f5d6ceSWei Wang rt0 = leaf; 766f11e6659SDavid S. Miller 76717ecf590SWei Wang /* Double check to make sure fn is not an intermediate node 76817ecf590SWei Wang * and fn->leaf does not points to its child's leaf 76917ecf590SWei Wang * (This might happen if all routes under fn are deleted from 77017ecf590SWei Wang * the tree and fib6_repair_tree() is called on the node.) 77117ecf590SWei Wang */ 77217ecf590SWei Wang key_plen = rt0->rt6i_dst.plen; 77317ecf590SWei Wang #ifdef CONFIG_IPV6_SUBTREES 77417ecf590SWei Wang if (rt0->rt6i_src.plen) 77517ecf590SWei Wang key_plen = rt0->rt6i_src.plen; 77617ecf590SWei Wang #endif 77717ecf590SWei Wang if (fn->fn_bit != key_plen) 77817ecf590SWei Wang return net->ipv6.ip6_null_entry; 77917ecf590SWei Wang 7808d1040e8SWei Wang match = find_rr_leaf(fn, leaf, rt0, rt0->rt6i_metric, oif, strict, 781afc154e9SHannes Frederic Sowa &do_rr); 782f11e6659SDavid S. Miller 783afc154e9SHannes Frederic Sowa if (do_rr) { 784071fb37eSDavid Miller struct rt6_info *next = rcu_dereference(rt0->rt6_next); 785f11e6659SDavid S. Miller 786554cfb7eSYOSHIFUJI Hideaki /* no entries matched; do round-robin */ 787f11e6659SDavid S. Miller if (!next || next->rt6i_metric != rt0->rt6i_metric) 7888d1040e8SWei Wang next = leaf; 789f11e6659SDavid S. Miller 79066f5d6ceSWei Wang if (next != rt0) { 79166f5d6ceSWei Wang spin_lock_bh(&leaf->rt6i_table->tb6_lock); 79266f5d6ceSWei Wang /* make sure next is not being deleted from the tree */ 79366f5d6ceSWei Wang if (next->rt6i_node) 79466f5d6ceSWei Wang rcu_assign_pointer(fn->rr_ptr, next); 79566f5d6ceSWei Wang spin_unlock_bh(&leaf->rt6i_table->tb6_lock); 79666f5d6ceSWei Wang } 797554cfb7eSYOSHIFUJI Hideaki } 798554cfb7eSYOSHIFUJI Hideaki 799a02cec21SEric Dumazet return match ? match : net->ipv6.ip6_null_entry; 8001da177e4SLinus Torvalds } 8011da177e4SLinus Torvalds 8028b9df265SMartin KaFai Lau static bool rt6_is_gw_or_nonexthop(const struct rt6_info *rt) 8038b9df265SMartin KaFai Lau { 8048b9df265SMartin KaFai Lau return (rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY)); 8058b9df265SMartin KaFai Lau } 8068b9df265SMartin KaFai Lau 80770ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 80870ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 809b71d1d42SEric Dumazet const struct in6_addr *gwaddr) 81070ceb4f5SYOSHIFUJI Hideaki { 811c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 81270ceb4f5SYOSHIFUJI Hideaki struct route_info *rinfo = (struct route_info *) opt; 81370ceb4f5SYOSHIFUJI Hideaki struct in6_addr prefix_buf, *prefix; 81470ceb4f5SYOSHIFUJI Hideaki unsigned int pref; 8154bed72e4SYOSHIFUJI Hideaki unsigned long lifetime; 81670ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt; 81770ceb4f5SYOSHIFUJI Hideaki 81870ceb4f5SYOSHIFUJI Hideaki if (len < sizeof(struct route_info)) { 81970ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 82070ceb4f5SYOSHIFUJI Hideaki } 82170ceb4f5SYOSHIFUJI Hideaki 82270ceb4f5SYOSHIFUJI Hideaki /* Sanity check for prefix_len and length */ 82370ceb4f5SYOSHIFUJI Hideaki if (rinfo->length > 3) { 82470ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 82570ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 128) { 82670ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 82770ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 64) { 82870ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 2) { 82970ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 83070ceb4f5SYOSHIFUJI Hideaki } 83170ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 0) { 83270ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 1) { 83370ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 83470ceb4f5SYOSHIFUJI Hideaki } 83570ceb4f5SYOSHIFUJI Hideaki } 83670ceb4f5SYOSHIFUJI Hideaki 83770ceb4f5SYOSHIFUJI Hideaki pref = rinfo->route_pref; 83870ceb4f5SYOSHIFUJI Hideaki if (pref == ICMPV6_ROUTER_PREF_INVALID) 8393933fc95SJens Rosenboom return -EINVAL; 84070ceb4f5SYOSHIFUJI Hideaki 8414bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); 84270ceb4f5SYOSHIFUJI Hideaki 84370ceb4f5SYOSHIFUJI Hideaki if (rinfo->length == 3) 84470ceb4f5SYOSHIFUJI Hideaki prefix = (struct in6_addr *)rinfo->prefix; 84570ceb4f5SYOSHIFUJI Hideaki else { 84670ceb4f5SYOSHIFUJI Hideaki /* this function is safe */ 84770ceb4f5SYOSHIFUJI Hideaki ipv6_addr_prefix(&prefix_buf, 84870ceb4f5SYOSHIFUJI Hideaki (struct in6_addr *)rinfo->prefix, 84970ceb4f5SYOSHIFUJI Hideaki rinfo->prefix_len); 85070ceb4f5SYOSHIFUJI Hideaki prefix = &prefix_buf; 85170ceb4f5SYOSHIFUJI Hideaki } 85270ceb4f5SYOSHIFUJI Hideaki 853f104a567SDuan Jiong if (rinfo->prefix_len == 0) 854f104a567SDuan Jiong rt = rt6_get_dflt_router(gwaddr, dev); 855f104a567SDuan Jiong else 856f104a567SDuan Jiong rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, 857830218c1SDavid Ahern gwaddr, dev); 85870ceb4f5SYOSHIFUJI Hideaki 85970ceb4f5SYOSHIFUJI Hideaki if (rt && !lifetime) { 860e0a1ad73SThomas Graf ip6_del_rt(rt); 86170ceb4f5SYOSHIFUJI Hideaki rt = NULL; 86270ceb4f5SYOSHIFUJI Hideaki } 86370ceb4f5SYOSHIFUJI Hideaki 86470ceb4f5SYOSHIFUJI Hideaki if (!rt && lifetime) 865830218c1SDavid Ahern rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, 866830218c1SDavid Ahern dev, pref); 86770ceb4f5SYOSHIFUJI Hideaki else if (rt) 86870ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags = RTF_ROUTEINFO | 86970ceb4f5SYOSHIFUJI Hideaki (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 87070ceb4f5SYOSHIFUJI Hideaki 87170ceb4f5SYOSHIFUJI Hideaki if (rt) { 8721716a961SGao feng if (!addrconf_finite_timeout(lifetime)) 8731716a961SGao feng rt6_clean_expires(rt); 8741716a961SGao feng else 8751716a961SGao feng rt6_set_expires(rt, jiffies + HZ * lifetime); 8761716a961SGao feng 87794e187c0SAmerigo Wang ip6_rt_put(rt); 87870ceb4f5SYOSHIFUJI Hideaki } 87970ceb4f5SYOSHIFUJI Hideaki return 0; 88070ceb4f5SYOSHIFUJI Hideaki } 88170ceb4f5SYOSHIFUJI Hideaki #endif 88270ceb4f5SYOSHIFUJI Hideaki 883a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn, 884a3c00e46SMartin KaFai Lau struct in6_addr *saddr) 885a3c00e46SMartin KaFai Lau { 88666f5d6ceSWei Wang struct fib6_node *pn, *sn; 887a3c00e46SMartin KaFai Lau while (1) { 888a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_TL_ROOT) 889a3c00e46SMartin KaFai Lau return NULL; 89066f5d6ceSWei Wang pn = rcu_dereference(fn->parent); 89166f5d6ceSWei Wang sn = FIB6_SUBTREE(pn); 89266f5d6ceSWei Wang if (sn && sn != fn) 89366f5d6ceSWei Wang fn = fib6_lookup(sn, NULL, saddr); 894a3c00e46SMartin KaFai Lau else 895a3c00e46SMartin KaFai Lau fn = pn; 896a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_RTINFO) 897a3c00e46SMartin KaFai Lau return fn; 898a3c00e46SMartin KaFai Lau } 899a3c00e46SMartin KaFai Lau } 900c71099acSThomas Graf 901d3843fe5SWei Wang static bool ip6_hold_safe(struct net *net, struct rt6_info **prt, 902d3843fe5SWei Wang bool null_fallback) 903d3843fe5SWei Wang { 904d3843fe5SWei Wang struct rt6_info *rt = *prt; 905d3843fe5SWei Wang 906d3843fe5SWei Wang if (dst_hold_safe(&rt->dst)) 907d3843fe5SWei Wang return true; 908d3843fe5SWei Wang if (null_fallback) { 909d3843fe5SWei Wang rt = net->ipv6.ip6_null_entry; 910d3843fe5SWei Wang dst_hold(&rt->dst); 911d3843fe5SWei Wang } else { 912d3843fe5SWei Wang rt = NULL; 913d3843fe5SWei Wang } 914d3843fe5SWei Wang *prt = rt; 915d3843fe5SWei Wang return false; 916d3843fe5SWei Wang } 917d3843fe5SWei Wang 9188ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net, 9198ed67789SDaniel Lezcano struct fib6_table *table, 9204c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 9211da177e4SLinus Torvalds { 9222b760fcfSWei Wang struct rt6_info *rt, *rt_cache; 9231da177e4SLinus Torvalds struct fib6_node *fn; 9241da177e4SLinus Torvalds 92566f5d6ceSWei Wang rcu_read_lock(); 9264c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 927c71099acSThomas Graf restart: 92866f5d6ceSWei Wang rt = rcu_dereference(fn->leaf); 92966f5d6ceSWei Wang if (!rt) { 93066f5d6ceSWei Wang rt = net->ipv6.ip6_null_entry; 93166f5d6ceSWei Wang } else { 93266f5d6ceSWei Wang rt = rt6_device_match(net, rt, &fl6->saddr, 93366f5d6ceSWei Wang fl6->flowi6_oif, flags); 93451ebd318SNicolas Dichtel if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0) 93566f5d6ceSWei Wang rt = rt6_multipath_select(rt, fl6, 93666f5d6ceSWei Wang fl6->flowi6_oif, flags); 93766f5d6ceSWei Wang } 938a3c00e46SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 939a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 940a3c00e46SMartin KaFai Lau if (fn) 941a3c00e46SMartin KaFai Lau goto restart; 942a3c00e46SMartin KaFai Lau } 9432b760fcfSWei Wang /* Search through exception table */ 9442b760fcfSWei Wang rt_cache = rt6_find_cached_rt(rt, &fl6->daddr, &fl6->saddr); 9452b760fcfSWei Wang if (rt_cache) 9462b760fcfSWei Wang rt = rt_cache; 9472b760fcfSWei Wang 948d3843fe5SWei Wang if (ip6_hold_safe(net, &rt, true)) 949d3843fe5SWei Wang dst_use_noref(&rt->dst, jiffies); 950d3843fe5SWei Wang 95166f5d6ceSWei Wang rcu_read_unlock(); 952b811580dSDavid Ahern 953b65f164dSPaolo Abeni trace_fib6_table_lookup(net, rt, table, fl6); 954b811580dSDavid Ahern 9551da177e4SLinus Torvalds return rt; 956c71099acSThomas Graf 957c71099acSThomas Graf } 958c71099acSThomas Graf 959ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6, 960ea6e574eSFlorian Westphal int flags) 961ea6e574eSFlorian Westphal { 962ea6e574eSFlorian Westphal return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup); 963ea6e574eSFlorian Westphal } 964ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup); 965ea6e574eSFlorian Westphal 9669acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, 9679acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *saddr, int oif, int strict) 968c71099acSThomas Graf { 9694c9483b2SDavid S. Miller struct flowi6 fl6 = { 9704c9483b2SDavid S. Miller .flowi6_oif = oif, 9714c9483b2SDavid S. Miller .daddr = *daddr, 972c71099acSThomas Graf }; 973c71099acSThomas Graf struct dst_entry *dst; 97477d16f45SYOSHIFUJI Hideaki int flags = strict ? RT6_LOOKUP_F_IFACE : 0; 975c71099acSThomas Graf 976adaa70bbSThomas Graf if (saddr) { 9774c9483b2SDavid S. Miller memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 978adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 979adaa70bbSThomas Graf } 980adaa70bbSThomas Graf 9814c9483b2SDavid S. Miller dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup); 982c71099acSThomas Graf if (dst->error == 0) 983c71099acSThomas Graf return (struct rt6_info *) dst; 984c71099acSThomas Graf 985c71099acSThomas Graf dst_release(dst); 986c71099acSThomas Graf 9871da177e4SLinus Torvalds return NULL; 9881da177e4SLinus Torvalds } 9897159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup); 9907159039aSYOSHIFUJI Hideaki 991c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock. 9921cfb71eeSWei Wang * It takes new route entry, the addition fails by any reason the 9931cfb71eeSWei Wang * route is released. 9941cfb71eeSWei Wang * Caller must hold dst before calling it. 9951da177e4SLinus Torvalds */ 9961da177e4SLinus Torvalds 997e5fd387aSMichal Kubeček static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info, 998333c4301SDavid Ahern struct mx6_config *mxc, 999333c4301SDavid Ahern struct netlink_ext_ack *extack) 10001da177e4SLinus Torvalds { 10011da177e4SLinus Torvalds int err; 1002c71099acSThomas Graf struct fib6_table *table; 10031da177e4SLinus Torvalds 1004c71099acSThomas Graf table = rt->rt6i_table; 100566f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 1006333c4301SDavid Ahern err = fib6_add(&table->tb6_root, rt, info, mxc, extack); 100766f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 10081da177e4SLinus Torvalds 10091da177e4SLinus Torvalds return err; 10101da177e4SLinus Torvalds } 10111da177e4SLinus Torvalds 101240e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt) 101340e22e8fSThomas Graf { 1014e715b6d3SFlorian Westphal struct nl_info info = { .nl_net = dev_net(rt->dst.dev), }; 1015e715b6d3SFlorian Westphal struct mx6_config mxc = { .mx = NULL, }; 1016e715b6d3SFlorian Westphal 10171cfb71eeSWei Wang /* Hold dst to account for the reference from the fib6 tree */ 10181cfb71eeSWei Wang dst_hold(&rt->dst); 1019333c4301SDavid Ahern return __ip6_ins_rt(rt, &info, &mxc, NULL); 102040e22e8fSThomas Graf } 102140e22e8fSThomas Graf 10224832c30dSDavid Ahern /* called with rcu_lock held */ 10234832c30dSDavid Ahern static struct net_device *ip6_rt_get_dev_rcu(struct rt6_info *rt) 10244832c30dSDavid Ahern { 10254832c30dSDavid Ahern struct net_device *dev = rt->dst.dev; 10264832c30dSDavid Ahern 102798d11291SDavid Ahern if (rt->rt6i_flags & (RTF_LOCAL | RTF_ANYCAST)) { 10284832c30dSDavid Ahern /* for copies of local routes, dst->dev needs to be the 10294832c30dSDavid Ahern * device if it is a master device, the master device if 10304832c30dSDavid Ahern * device is enslaved, and the loopback as the default 10314832c30dSDavid Ahern */ 10324832c30dSDavid Ahern if (netif_is_l3_slave(dev) && 10334832c30dSDavid Ahern !rt6_need_strict(&rt->rt6i_dst.addr)) 10344832c30dSDavid Ahern dev = l3mdev_master_dev_rcu(dev); 10354832c30dSDavid Ahern else if (!netif_is_l3_master(dev)) 10364832c30dSDavid Ahern dev = dev_net(dev)->loopback_dev; 10374832c30dSDavid Ahern /* last case is netif_is_l3_master(dev) is true in which 10384832c30dSDavid Ahern * case we want dev returned to be dev 10394832c30dSDavid Ahern */ 10404832c30dSDavid Ahern } 10414832c30dSDavid Ahern 10424832c30dSDavid Ahern return dev; 10434832c30dSDavid Ahern } 10444832c30dSDavid Ahern 10458b9df265SMartin KaFai Lau static struct rt6_info *ip6_rt_cache_alloc(struct rt6_info *ort, 104621efcfa0SEric Dumazet const struct in6_addr *daddr, 1047b71d1d42SEric Dumazet const struct in6_addr *saddr) 10481da177e4SLinus Torvalds { 10494832c30dSDavid Ahern struct net_device *dev; 10501da177e4SLinus Torvalds struct rt6_info *rt; 10511da177e4SLinus Torvalds 10521da177e4SLinus Torvalds /* 10531da177e4SLinus Torvalds * Clone the route. 10541da177e4SLinus Torvalds */ 10551da177e4SLinus Torvalds 1056d52d3997SMartin KaFai Lau if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)) 10573a2232e9SDavid Miller ort = ort->from; 10581da177e4SLinus Torvalds 10594832c30dSDavid Ahern rcu_read_lock(); 10604832c30dSDavid Ahern dev = ip6_rt_get_dev_rcu(ort); 10614832c30dSDavid Ahern rt = __ip6_dst_alloc(dev_net(dev), dev, 0); 10624832c30dSDavid Ahern rcu_read_unlock(); 106383a09abdSMartin KaFai Lau if (!rt) 106483a09abdSMartin KaFai Lau return NULL; 106583a09abdSMartin KaFai Lau 106683a09abdSMartin KaFai Lau ip6_rt_copy_init(rt, ort); 10678b9df265SMartin KaFai Lau rt->rt6i_flags |= RTF_CACHE; 106883a09abdSMartin KaFai Lau rt->rt6i_metric = 0; 106983a09abdSMartin KaFai Lau rt->dst.flags |= DST_HOST; 107083a09abdSMartin KaFai Lau rt->rt6i_dst.addr = *daddr; 107183a09abdSMartin KaFai Lau rt->rt6i_dst.plen = 128; 10728b9df265SMartin KaFai Lau 10738b9df265SMartin KaFai Lau if (!rt6_is_gw_or_nonexthop(ort)) { 1074bb3c3686SDavid S. Miller if (ort->rt6i_dst.plen != 128 && 107521efcfa0SEric Dumazet ipv6_addr_equal(&ort->rt6i_dst.addr, daddr)) 107658c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 10771da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 10781da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 10794e3fd7a0SAlexey Dobriyan rt->rt6i_src.addr = *saddr; 10801da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 10811da177e4SLinus Torvalds } 10821da177e4SLinus Torvalds #endif 108395a9a5baSYOSHIFUJI Hideaki } 108495a9a5baSYOSHIFUJI Hideaki 1085299d9939SYOSHIFUJI Hideaki return rt; 1086299d9939SYOSHIFUJI Hideaki } 1087299d9939SYOSHIFUJI Hideaki 1088d52d3997SMartin KaFai Lau static struct rt6_info *ip6_rt_pcpu_alloc(struct rt6_info *rt) 1089d52d3997SMartin KaFai Lau { 10904832c30dSDavid Ahern struct net_device *dev; 1091d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1092d52d3997SMartin KaFai Lau 10934832c30dSDavid Ahern rcu_read_lock(); 10944832c30dSDavid Ahern dev = ip6_rt_get_dev_rcu(rt); 10954832c30dSDavid Ahern pcpu_rt = __ip6_dst_alloc(dev_net(dev), dev, rt->dst.flags); 10964832c30dSDavid Ahern rcu_read_unlock(); 1097d52d3997SMartin KaFai Lau if (!pcpu_rt) 1098d52d3997SMartin KaFai Lau return NULL; 1099d52d3997SMartin KaFai Lau ip6_rt_copy_init(pcpu_rt, rt); 1100d52d3997SMartin KaFai Lau pcpu_rt->rt6i_protocol = rt->rt6i_protocol; 1101d52d3997SMartin KaFai Lau pcpu_rt->rt6i_flags |= RTF_PCPU; 1102d52d3997SMartin KaFai Lau return pcpu_rt; 1103d52d3997SMartin KaFai Lau } 1104d52d3997SMartin KaFai Lau 110566f5d6ceSWei Wang /* It should be called with rcu_read_lock() acquired */ 1106d52d3997SMartin KaFai Lau static struct rt6_info *rt6_get_pcpu_route(struct rt6_info *rt) 1107d52d3997SMartin KaFai Lau { 1108a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, **p; 1109d52d3997SMartin KaFai Lau 1110d52d3997SMartin KaFai Lau p = this_cpu_ptr(rt->rt6i_pcpu); 1111d52d3997SMartin KaFai Lau pcpu_rt = *p; 1112d52d3997SMartin KaFai Lau 1113d3843fe5SWei Wang if (pcpu_rt && ip6_hold_safe(NULL, &pcpu_rt, false)) 1114a73e4195SMartin KaFai Lau rt6_dst_from_metrics_check(pcpu_rt); 1115d3843fe5SWei Wang 1116a73e4195SMartin KaFai Lau return pcpu_rt; 1117a73e4195SMartin KaFai Lau } 1118a73e4195SMartin KaFai Lau 1119a73e4195SMartin KaFai Lau static struct rt6_info *rt6_make_pcpu_route(struct rt6_info *rt) 1120a73e4195SMartin KaFai Lau { 1121a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, *prev, **p; 1122d52d3997SMartin KaFai Lau 1123d52d3997SMartin KaFai Lau pcpu_rt = ip6_rt_pcpu_alloc(rt); 1124d52d3997SMartin KaFai Lau if (!pcpu_rt) { 1125d52d3997SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 1126d52d3997SMartin KaFai Lau 11279c7370a1SMartin KaFai Lau dst_hold(&net->ipv6.ip6_null_entry->dst); 11289c7370a1SMartin KaFai Lau return net->ipv6.ip6_null_entry; 1129d52d3997SMartin KaFai Lau } 1130d52d3997SMartin KaFai Lau 1131a94b9367SWei Wang dst_hold(&pcpu_rt->dst); 1132a73e4195SMartin KaFai Lau p = this_cpu_ptr(rt->rt6i_pcpu); 1133d52d3997SMartin KaFai Lau prev = cmpxchg(p, NULL, pcpu_rt); 1134951f788aSEric Dumazet BUG_ON(prev); 1135a94b9367SWei Wang 1136d52d3997SMartin KaFai Lau rt6_dst_from_metrics_check(pcpu_rt); 1137d52d3997SMartin KaFai Lau return pcpu_rt; 1138d52d3997SMartin KaFai Lau } 1139d52d3997SMartin KaFai Lau 114035732d01SWei Wang /* exception hash table implementation 114135732d01SWei Wang */ 114235732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock); 114335732d01SWei Wang 114435732d01SWei Wang /* Remove rt6_ex from hash table and free the memory 114535732d01SWei Wang * Caller must hold rt6_exception_lock 114635732d01SWei Wang */ 114735732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket, 114835732d01SWei Wang struct rt6_exception *rt6_ex) 114935732d01SWei Wang { 1150b2427e67SColin Ian King struct net *net; 115181eb8447SWei Wang 115235732d01SWei Wang if (!bucket || !rt6_ex) 115335732d01SWei Wang return; 1154b2427e67SColin Ian King 1155b2427e67SColin Ian King net = dev_net(rt6_ex->rt6i->dst.dev); 115635732d01SWei Wang rt6_ex->rt6i->rt6i_node = NULL; 115735732d01SWei Wang hlist_del_rcu(&rt6_ex->hlist); 115835732d01SWei Wang rt6_release(rt6_ex->rt6i); 115935732d01SWei Wang kfree_rcu(rt6_ex, rcu); 116035732d01SWei Wang WARN_ON_ONCE(!bucket->depth); 116135732d01SWei Wang bucket->depth--; 116281eb8447SWei Wang net->ipv6.rt6_stats->fib_rt_cache--; 116335732d01SWei Wang } 116435732d01SWei Wang 116535732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory 116635732d01SWei Wang * Caller must hold rt6_exception_lock 116735732d01SWei Wang */ 116835732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket) 116935732d01SWei Wang { 117035732d01SWei Wang struct rt6_exception *rt6_ex, *oldest = NULL; 117135732d01SWei Wang 117235732d01SWei Wang if (!bucket) 117335732d01SWei Wang return; 117435732d01SWei Wang 117535732d01SWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 117635732d01SWei Wang if (!oldest || time_before(rt6_ex->stamp, oldest->stamp)) 117735732d01SWei Wang oldest = rt6_ex; 117835732d01SWei Wang } 117935732d01SWei Wang rt6_remove_exception(bucket, oldest); 118035732d01SWei Wang } 118135732d01SWei Wang 118235732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst, 118335732d01SWei Wang const struct in6_addr *src) 118435732d01SWei Wang { 118535732d01SWei Wang static u32 seed __read_mostly; 118635732d01SWei Wang u32 val; 118735732d01SWei Wang 118835732d01SWei Wang net_get_random_once(&seed, sizeof(seed)); 118935732d01SWei Wang val = jhash(dst, sizeof(*dst), seed); 119035732d01SWei Wang 119135732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 119235732d01SWei Wang if (src) 119335732d01SWei Wang val = jhash(src, sizeof(*src), val); 119435732d01SWei Wang #endif 119535732d01SWei Wang return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT); 119635732d01SWei Wang } 119735732d01SWei Wang 119835732d01SWei Wang /* Helper function to find the cached rt in the hash table 119935732d01SWei Wang * and update bucket pointer to point to the bucket for this 120035732d01SWei Wang * (daddr, saddr) pair 120135732d01SWei Wang * Caller must hold rt6_exception_lock 120235732d01SWei Wang */ 120335732d01SWei Wang static struct rt6_exception * 120435732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket, 120535732d01SWei Wang const struct in6_addr *daddr, 120635732d01SWei Wang const struct in6_addr *saddr) 120735732d01SWei Wang { 120835732d01SWei Wang struct rt6_exception *rt6_ex; 120935732d01SWei Wang u32 hval; 121035732d01SWei Wang 121135732d01SWei Wang if (!(*bucket) || !daddr) 121235732d01SWei Wang return NULL; 121335732d01SWei Wang 121435732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 121535732d01SWei Wang *bucket += hval; 121635732d01SWei Wang 121735732d01SWei Wang hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) { 121835732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 121935732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 122035732d01SWei Wang 122135732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 122235732d01SWei Wang if (matched && saddr) 122335732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 122435732d01SWei Wang #endif 122535732d01SWei Wang if (matched) 122635732d01SWei Wang return rt6_ex; 122735732d01SWei Wang } 122835732d01SWei Wang return NULL; 122935732d01SWei Wang } 123035732d01SWei Wang 123135732d01SWei Wang /* Helper function to find the cached rt in the hash table 123235732d01SWei Wang * and update bucket pointer to point to the bucket for this 123335732d01SWei Wang * (daddr, saddr) pair 123435732d01SWei Wang * Caller must hold rcu_read_lock() 123535732d01SWei Wang */ 123635732d01SWei Wang static struct rt6_exception * 123735732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket, 123835732d01SWei Wang const struct in6_addr *daddr, 123935732d01SWei Wang const struct in6_addr *saddr) 124035732d01SWei Wang { 124135732d01SWei Wang struct rt6_exception *rt6_ex; 124235732d01SWei Wang u32 hval; 124335732d01SWei Wang 124435732d01SWei Wang WARN_ON_ONCE(!rcu_read_lock_held()); 124535732d01SWei Wang 124635732d01SWei Wang if (!(*bucket) || !daddr) 124735732d01SWei Wang return NULL; 124835732d01SWei Wang 124935732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 125035732d01SWei Wang *bucket += hval; 125135732d01SWei Wang 125235732d01SWei Wang hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) { 125335732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 125435732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 125535732d01SWei Wang 125635732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 125735732d01SWei Wang if (matched && saddr) 125835732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 125935732d01SWei Wang #endif 126035732d01SWei Wang if (matched) 126135732d01SWei Wang return rt6_ex; 126235732d01SWei Wang } 126335732d01SWei Wang return NULL; 126435732d01SWei Wang } 126535732d01SWei Wang 126635732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt, 126735732d01SWei Wang struct rt6_info *ort) 126835732d01SWei Wang { 126981eb8447SWei Wang struct net *net = dev_net(ort->dst.dev); 127035732d01SWei Wang struct rt6_exception_bucket *bucket; 127135732d01SWei Wang struct in6_addr *src_key = NULL; 127235732d01SWei Wang struct rt6_exception *rt6_ex; 127335732d01SWei Wang int err = 0; 127435732d01SWei Wang 127535732d01SWei Wang /* ort can't be a cache or pcpu route */ 127635732d01SWei Wang if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)) 12773a2232e9SDavid Miller ort = ort->from; 127835732d01SWei Wang WARN_ON_ONCE(ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)); 127935732d01SWei Wang 128035732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 128135732d01SWei Wang 128235732d01SWei Wang if (ort->exception_bucket_flushed) { 128335732d01SWei Wang err = -EINVAL; 128435732d01SWei Wang goto out; 128535732d01SWei Wang } 128635732d01SWei Wang 128735732d01SWei Wang bucket = rcu_dereference_protected(ort->rt6i_exception_bucket, 128835732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 128935732d01SWei Wang if (!bucket) { 129035732d01SWei Wang bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket), 129135732d01SWei Wang GFP_ATOMIC); 129235732d01SWei Wang if (!bucket) { 129335732d01SWei Wang err = -ENOMEM; 129435732d01SWei Wang goto out; 129535732d01SWei Wang } 129635732d01SWei Wang rcu_assign_pointer(ort->rt6i_exception_bucket, bucket); 129735732d01SWei Wang } 129835732d01SWei Wang 129935732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 130035732d01SWei Wang /* rt6i_src.plen != 0 indicates ort is in subtree 130135732d01SWei Wang * and exception table is indexed by a hash of 130235732d01SWei Wang * both rt6i_dst and rt6i_src. 130335732d01SWei Wang * Otherwise, the exception table is indexed by 130435732d01SWei Wang * a hash of only rt6i_dst. 130535732d01SWei Wang */ 130635732d01SWei Wang if (ort->rt6i_src.plen) 130735732d01SWei Wang src_key = &nrt->rt6i_src.addr; 130835732d01SWei Wang #endif 130960006a48SWei Wang 131060006a48SWei Wang /* Update rt6i_prefsrc as it could be changed 131160006a48SWei Wang * in rt6_remove_prefsrc() 131260006a48SWei Wang */ 131360006a48SWei Wang nrt->rt6i_prefsrc = ort->rt6i_prefsrc; 1314f5bbe7eeSWei Wang /* rt6_mtu_change() might lower mtu on ort. 1315f5bbe7eeSWei Wang * Only insert this exception route if its mtu 1316f5bbe7eeSWei Wang * is less than ort's mtu value. 1317f5bbe7eeSWei Wang */ 1318f5bbe7eeSWei Wang if (nrt->rt6i_pmtu >= dst_mtu(&ort->dst)) { 1319f5bbe7eeSWei Wang err = -EINVAL; 1320f5bbe7eeSWei Wang goto out; 1321f5bbe7eeSWei Wang } 132260006a48SWei Wang 132335732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr, 132435732d01SWei Wang src_key); 132535732d01SWei Wang if (rt6_ex) 132635732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 132735732d01SWei Wang 132835732d01SWei Wang rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC); 132935732d01SWei Wang if (!rt6_ex) { 133035732d01SWei Wang err = -ENOMEM; 133135732d01SWei Wang goto out; 133235732d01SWei Wang } 133335732d01SWei Wang rt6_ex->rt6i = nrt; 133435732d01SWei Wang rt6_ex->stamp = jiffies; 133535732d01SWei Wang atomic_inc(&nrt->rt6i_ref); 133635732d01SWei Wang nrt->rt6i_node = ort->rt6i_node; 133735732d01SWei Wang hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain); 133835732d01SWei Wang bucket->depth++; 133981eb8447SWei Wang net->ipv6.rt6_stats->fib_rt_cache++; 134035732d01SWei Wang 134135732d01SWei Wang if (bucket->depth > FIB6_MAX_DEPTH) 134235732d01SWei Wang rt6_exception_remove_oldest(bucket); 134335732d01SWei Wang 134435732d01SWei Wang out: 134535732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 134635732d01SWei Wang 134735732d01SWei Wang /* Update fn->fn_sernum to invalidate all cached dst */ 1348b886d5f2SPaolo Abeni if (!err) { 134935732d01SWei Wang fib6_update_sernum(ort); 1350b886d5f2SPaolo Abeni fib6_force_start_gc(net); 1351b886d5f2SPaolo Abeni } 135235732d01SWei Wang 135335732d01SWei Wang return err; 135435732d01SWei Wang } 135535732d01SWei Wang 135635732d01SWei Wang void rt6_flush_exceptions(struct rt6_info *rt) 135735732d01SWei Wang { 135835732d01SWei Wang struct rt6_exception_bucket *bucket; 135935732d01SWei Wang struct rt6_exception *rt6_ex; 136035732d01SWei Wang struct hlist_node *tmp; 136135732d01SWei Wang int i; 136235732d01SWei Wang 136335732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 136435732d01SWei Wang /* Prevent rt6_insert_exception() to recreate the bucket list */ 136535732d01SWei Wang rt->exception_bucket_flushed = 1; 136635732d01SWei Wang 136735732d01SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 136835732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 136935732d01SWei Wang if (!bucket) 137035732d01SWei Wang goto out; 137135732d01SWei Wang 137235732d01SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 137335732d01SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) 137435732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 137535732d01SWei Wang WARN_ON_ONCE(bucket->depth); 137635732d01SWei Wang bucket++; 137735732d01SWei Wang } 137835732d01SWei Wang 137935732d01SWei Wang out: 138035732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 138135732d01SWei Wang } 138235732d01SWei Wang 138335732d01SWei Wang /* Find cached rt in the hash table inside passed in rt 138435732d01SWei Wang * Caller has to hold rcu_read_lock() 138535732d01SWei Wang */ 138635732d01SWei Wang static struct rt6_info *rt6_find_cached_rt(struct rt6_info *rt, 138735732d01SWei Wang struct in6_addr *daddr, 138835732d01SWei Wang struct in6_addr *saddr) 138935732d01SWei Wang { 139035732d01SWei Wang struct rt6_exception_bucket *bucket; 139135732d01SWei Wang struct in6_addr *src_key = NULL; 139235732d01SWei Wang struct rt6_exception *rt6_ex; 139335732d01SWei Wang struct rt6_info *res = NULL; 139435732d01SWei Wang 139535732d01SWei Wang bucket = rcu_dereference(rt->rt6i_exception_bucket); 139635732d01SWei Wang 139735732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 139835732d01SWei Wang /* rt6i_src.plen != 0 indicates rt is in subtree 139935732d01SWei Wang * and exception table is indexed by a hash of 140035732d01SWei Wang * both rt6i_dst and rt6i_src. 140135732d01SWei Wang * Otherwise, the exception table is indexed by 140235732d01SWei Wang * a hash of only rt6i_dst. 140335732d01SWei Wang */ 140435732d01SWei Wang if (rt->rt6i_src.plen) 140535732d01SWei Wang src_key = saddr; 140635732d01SWei Wang #endif 140735732d01SWei Wang rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key); 140835732d01SWei Wang 140935732d01SWei Wang if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i)) 141035732d01SWei Wang res = rt6_ex->rt6i; 141135732d01SWei Wang 141235732d01SWei Wang return res; 141335732d01SWei Wang } 141435732d01SWei Wang 141535732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */ 141635732d01SWei Wang int rt6_remove_exception_rt(struct rt6_info *rt) 141735732d01SWei Wang { 141835732d01SWei Wang struct rt6_exception_bucket *bucket; 14193a2232e9SDavid Miller struct rt6_info *from = rt->from; 142035732d01SWei Wang struct in6_addr *src_key = NULL; 142135732d01SWei Wang struct rt6_exception *rt6_ex; 142235732d01SWei Wang int err; 142335732d01SWei Wang 142435732d01SWei Wang if (!from || 1425442d713bSColin Ian King !(rt->rt6i_flags & RTF_CACHE)) 142635732d01SWei Wang return -EINVAL; 142735732d01SWei Wang 142835732d01SWei Wang if (!rcu_access_pointer(from->rt6i_exception_bucket)) 142935732d01SWei Wang return -ENOENT; 143035732d01SWei Wang 143135732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 143235732d01SWei Wang bucket = rcu_dereference_protected(from->rt6i_exception_bucket, 143335732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 143435732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 143535732d01SWei Wang /* rt6i_src.plen != 0 indicates 'from' is in subtree 143635732d01SWei Wang * and exception table is indexed by a hash of 143735732d01SWei Wang * both rt6i_dst and rt6i_src. 143835732d01SWei Wang * Otherwise, the exception table is indexed by 143935732d01SWei Wang * a hash of only rt6i_dst. 144035732d01SWei Wang */ 144135732d01SWei Wang if (from->rt6i_src.plen) 144235732d01SWei Wang src_key = &rt->rt6i_src.addr; 144335732d01SWei Wang #endif 144435732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, 144535732d01SWei Wang &rt->rt6i_dst.addr, 144635732d01SWei Wang src_key); 144735732d01SWei Wang if (rt6_ex) { 144835732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 144935732d01SWei Wang err = 0; 145035732d01SWei Wang } else { 145135732d01SWei Wang err = -ENOENT; 145235732d01SWei Wang } 145335732d01SWei Wang 145435732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 145535732d01SWei Wang return err; 145635732d01SWei Wang } 145735732d01SWei Wang 145835732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and 145935732d01SWei Wang * refresh its stamp 146035732d01SWei Wang */ 146135732d01SWei Wang static void rt6_update_exception_stamp_rt(struct rt6_info *rt) 146235732d01SWei Wang { 146335732d01SWei Wang struct rt6_exception_bucket *bucket; 14643a2232e9SDavid Miller struct rt6_info *from = rt->from; 146535732d01SWei Wang struct in6_addr *src_key = NULL; 146635732d01SWei Wang struct rt6_exception *rt6_ex; 146735732d01SWei Wang 146835732d01SWei Wang if (!from || 1469442d713bSColin Ian King !(rt->rt6i_flags & RTF_CACHE)) 147035732d01SWei Wang return; 147135732d01SWei Wang 147235732d01SWei Wang rcu_read_lock(); 147335732d01SWei Wang bucket = rcu_dereference(from->rt6i_exception_bucket); 147435732d01SWei Wang 147535732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 147635732d01SWei Wang /* rt6i_src.plen != 0 indicates 'from' is in subtree 147735732d01SWei Wang * and exception table is indexed by a hash of 147835732d01SWei Wang * both rt6i_dst and rt6i_src. 147935732d01SWei Wang * Otherwise, the exception table is indexed by 148035732d01SWei Wang * a hash of only rt6i_dst. 148135732d01SWei Wang */ 148235732d01SWei Wang if (from->rt6i_src.plen) 148335732d01SWei Wang src_key = &rt->rt6i_src.addr; 148435732d01SWei Wang #endif 148535732d01SWei Wang rt6_ex = __rt6_find_exception_rcu(&bucket, 148635732d01SWei Wang &rt->rt6i_dst.addr, 148735732d01SWei Wang src_key); 148835732d01SWei Wang if (rt6_ex) 148935732d01SWei Wang rt6_ex->stamp = jiffies; 149035732d01SWei Wang 149135732d01SWei Wang rcu_read_unlock(); 149235732d01SWei Wang } 149335732d01SWei Wang 149460006a48SWei Wang static void rt6_exceptions_remove_prefsrc(struct rt6_info *rt) 149560006a48SWei Wang { 149660006a48SWei Wang struct rt6_exception_bucket *bucket; 149760006a48SWei Wang struct rt6_exception *rt6_ex; 149860006a48SWei Wang int i; 149960006a48SWei Wang 150060006a48SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 150160006a48SWei Wang lockdep_is_held(&rt6_exception_lock)); 150260006a48SWei Wang 150360006a48SWei Wang if (bucket) { 150460006a48SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 150560006a48SWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 150660006a48SWei Wang rt6_ex->rt6i->rt6i_prefsrc.plen = 0; 150760006a48SWei Wang } 150860006a48SWei Wang bucket++; 150960006a48SWei Wang } 151060006a48SWei Wang } 151160006a48SWei Wang } 151260006a48SWei Wang 1513f5bbe7eeSWei Wang static void rt6_exceptions_update_pmtu(struct rt6_info *rt, int mtu) 1514f5bbe7eeSWei Wang { 1515f5bbe7eeSWei Wang struct rt6_exception_bucket *bucket; 1516f5bbe7eeSWei Wang struct rt6_exception *rt6_ex; 1517f5bbe7eeSWei Wang int i; 1518f5bbe7eeSWei Wang 1519f5bbe7eeSWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1520f5bbe7eeSWei Wang lockdep_is_held(&rt6_exception_lock)); 1521f5bbe7eeSWei Wang 1522f5bbe7eeSWei Wang if (bucket) { 1523f5bbe7eeSWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1524f5bbe7eeSWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 1525f5bbe7eeSWei Wang struct rt6_info *entry = rt6_ex->rt6i; 1526f5bbe7eeSWei Wang /* For RTF_CACHE with rt6i_pmtu == 0 1527f5bbe7eeSWei Wang * (i.e. a redirected route), 1528f5bbe7eeSWei Wang * the metrics of its rt->dst.from has already 1529f5bbe7eeSWei Wang * been updated. 1530f5bbe7eeSWei Wang */ 1531f5bbe7eeSWei Wang if (entry->rt6i_pmtu && entry->rt6i_pmtu > mtu) 1532f5bbe7eeSWei Wang entry->rt6i_pmtu = mtu; 1533f5bbe7eeSWei Wang } 1534f5bbe7eeSWei Wang bucket++; 1535f5bbe7eeSWei Wang } 1536f5bbe7eeSWei Wang } 1537f5bbe7eeSWei Wang } 1538f5bbe7eeSWei Wang 1539b16cb459SWei Wang #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) 1540b16cb459SWei Wang 1541b16cb459SWei Wang static void rt6_exceptions_clean_tohost(struct rt6_info *rt, 1542b16cb459SWei Wang struct in6_addr *gateway) 1543b16cb459SWei Wang { 1544b16cb459SWei Wang struct rt6_exception_bucket *bucket; 1545b16cb459SWei Wang struct rt6_exception *rt6_ex; 1546b16cb459SWei Wang struct hlist_node *tmp; 1547b16cb459SWei Wang int i; 1548b16cb459SWei Wang 1549b16cb459SWei Wang if (!rcu_access_pointer(rt->rt6i_exception_bucket)) 1550b16cb459SWei Wang return; 1551b16cb459SWei Wang 1552b16cb459SWei Wang spin_lock_bh(&rt6_exception_lock); 1553b16cb459SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1554b16cb459SWei Wang lockdep_is_held(&rt6_exception_lock)); 1555b16cb459SWei Wang 1556b16cb459SWei Wang if (bucket) { 1557b16cb459SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1558b16cb459SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, 1559b16cb459SWei Wang &bucket->chain, hlist) { 1560b16cb459SWei Wang struct rt6_info *entry = rt6_ex->rt6i; 1561b16cb459SWei Wang 1562b16cb459SWei Wang if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) == 1563b16cb459SWei Wang RTF_CACHE_GATEWAY && 1564b16cb459SWei Wang ipv6_addr_equal(gateway, 1565b16cb459SWei Wang &entry->rt6i_gateway)) { 1566b16cb459SWei Wang rt6_remove_exception(bucket, rt6_ex); 1567b16cb459SWei Wang } 1568b16cb459SWei Wang } 1569b16cb459SWei Wang bucket++; 1570b16cb459SWei Wang } 1571b16cb459SWei Wang } 1572b16cb459SWei Wang 1573b16cb459SWei Wang spin_unlock_bh(&rt6_exception_lock); 1574b16cb459SWei Wang } 1575b16cb459SWei Wang 1576c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket, 1577c757faa8SWei Wang struct rt6_exception *rt6_ex, 1578c757faa8SWei Wang struct fib6_gc_args *gc_args, 1579c757faa8SWei Wang unsigned long now) 1580c757faa8SWei Wang { 1581c757faa8SWei Wang struct rt6_info *rt = rt6_ex->rt6i; 1582c757faa8SWei Wang 15831859bac0SPaolo Abeni /* we are pruning and obsoleting aged-out and non gateway exceptions 15841859bac0SPaolo Abeni * even if others have still references to them, so that on next 15851859bac0SPaolo Abeni * dst_check() such references can be dropped. 15861859bac0SPaolo Abeni * EXPIRES exceptions - e.g. pmtu-generated ones are pruned when 15871859bac0SPaolo Abeni * expired, independently from their aging, as per RFC 8201 section 4 15881859bac0SPaolo Abeni */ 15891859bac0SPaolo Abeni if (!(rt->rt6i_flags & RTF_EXPIRES) && 1590c757faa8SWei Wang time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) { 1591c757faa8SWei Wang RT6_TRACE("aging clone %p\n", rt); 1592c757faa8SWei Wang rt6_remove_exception(bucket, rt6_ex); 1593c757faa8SWei Wang return; 1594c757faa8SWei Wang } else if (rt->rt6i_flags & RTF_GATEWAY) { 1595c757faa8SWei Wang struct neighbour *neigh; 1596c757faa8SWei Wang __u8 neigh_flags = 0; 1597c757faa8SWei Wang 1598c757faa8SWei Wang neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway); 1599c757faa8SWei Wang if (neigh) { 1600c757faa8SWei Wang neigh_flags = neigh->flags; 1601c757faa8SWei Wang neigh_release(neigh); 1602c757faa8SWei Wang } 1603c757faa8SWei Wang if (!(neigh_flags & NTF_ROUTER)) { 1604c757faa8SWei Wang RT6_TRACE("purging route %p via non-router but gateway\n", 1605c757faa8SWei Wang rt); 1606c757faa8SWei Wang rt6_remove_exception(bucket, rt6_ex); 1607c757faa8SWei Wang return; 1608c757faa8SWei Wang } 16091859bac0SPaolo Abeni } else if (__rt6_check_expired(rt)) { 16101859bac0SPaolo Abeni RT6_TRACE("purging expired route %p\n", rt); 16111859bac0SPaolo Abeni rt6_remove_exception(bucket, rt6_ex); 16121859bac0SPaolo Abeni return; 1613c757faa8SWei Wang } 1614c757faa8SWei Wang gc_args->more++; 1615c757faa8SWei Wang } 1616c757faa8SWei Wang 1617c757faa8SWei Wang void rt6_age_exceptions(struct rt6_info *rt, 1618c757faa8SWei Wang struct fib6_gc_args *gc_args, 1619c757faa8SWei Wang unsigned long now) 1620c757faa8SWei Wang { 1621c757faa8SWei Wang struct rt6_exception_bucket *bucket; 1622c757faa8SWei Wang struct rt6_exception *rt6_ex; 1623c757faa8SWei Wang struct hlist_node *tmp; 1624c757faa8SWei Wang int i; 1625c757faa8SWei Wang 1626c757faa8SWei Wang if (!rcu_access_pointer(rt->rt6i_exception_bucket)) 1627c757faa8SWei Wang return; 1628c757faa8SWei Wang 1629c757faa8SWei Wang spin_lock_bh(&rt6_exception_lock); 1630c757faa8SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1631c757faa8SWei Wang lockdep_is_held(&rt6_exception_lock)); 1632c757faa8SWei Wang 1633c757faa8SWei Wang if (bucket) { 1634c757faa8SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1635c757faa8SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, 1636c757faa8SWei Wang &bucket->chain, hlist) { 1637c757faa8SWei Wang rt6_age_examine_exception(bucket, rt6_ex, 1638c757faa8SWei Wang gc_args, now); 1639c757faa8SWei Wang } 1640c757faa8SWei Wang bucket++; 1641c757faa8SWei Wang } 1642c757faa8SWei Wang } 1643c757faa8SWei Wang spin_unlock_bh(&rt6_exception_lock); 1644c757faa8SWei Wang } 1645c757faa8SWei Wang 16469ff74384SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, 16479ff74384SDavid Ahern int oif, struct flowi6 *fl6, int flags) 16481da177e4SLinus Torvalds { 1649367efcb9SMartin KaFai Lau struct fib6_node *fn, *saved_fn; 16502b760fcfSWei Wang struct rt6_info *rt, *rt_cache; 1651c71099acSThomas Graf int strict = 0; 16521da177e4SLinus Torvalds 165377d16f45SYOSHIFUJI Hideaki strict |= flags & RT6_LOOKUP_F_IFACE; 1654d5d32e4bSDavid Ahern strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE; 1655367efcb9SMartin KaFai Lau if (net->ipv6.devconf_all->forwarding == 0) 1656367efcb9SMartin KaFai Lau strict |= RT6_LOOKUP_F_REACHABLE; 16571da177e4SLinus Torvalds 165866f5d6ceSWei Wang rcu_read_lock(); 16591da177e4SLinus Torvalds 16604c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1661367efcb9SMartin KaFai Lau saved_fn = fn; 16621da177e4SLinus Torvalds 1663ca254490SDavid Ahern if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) 1664ca254490SDavid Ahern oif = 0; 1665ca254490SDavid Ahern 1666a3c00e46SMartin KaFai Lau redo_rt6_select: 16678d1040e8SWei Wang rt = rt6_select(net, fn, oif, strict); 166852bd4c0cSNicolas Dichtel if (rt->rt6i_nsiblings) 1669367efcb9SMartin KaFai Lau rt = rt6_multipath_select(rt, fl6, oif, strict); 1670a3c00e46SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 1671a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1672a3c00e46SMartin KaFai Lau if (fn) 1673a3c00e46SMartin KaFai Lau goto redo_rt6_select; 1674367efcb9SMartin KaFai Lau else if (strict & RT6_LOOKUP_F_REACHABLE) { 1675367efcb9SMartin KaFai Lau /* also consider unreachable route */ 1676367efcb9SMartin KaFai Lau strict &= ~RT6_LOOKUP_F_REACHABLE; 1677367efcb9SMartin KaFai Lau fn = saved_fn; 1678367efcb9SMartin KaFai Lau goto redo_rt6_select; 1679367efcb9SMartin KaFai Lau } 1680a3c00e46SMartin KaFai Lau } 1681a3c00e46SMartin KaFai Lau 16822b760fcfSWei Wang /*Search through exception table */ 16832b760fcfSWei Wang rt_cache = rt6_find_cached_rt(rt, &fl6->daddr, &fl6->saddr); 16842b760fcfSWei Wang if (rt_cache) 16852b760fcfSWei Wang rt = rt_cache; 1686d52d3997SMartin KaFai Lau 1687d3843fe5SWei Wang if (rt == net->ipv6.ip6_null_entry) { 168866f5d6ceSWei Wang rcu_read_unlock(); 1689d3843fe5SWei Wang dst_hold(&rt->dst); 1690b65f164dSPaolo Abeni trace_fib6_table_lookup(net, rt, table, fl6); 1691d3843fe5SWei Wang return rt; 1692d3843fe5SWei Wang } else if (rt->rt6i_flags & RTF_CACHE) { 1693d3843fe5SWei Wang if (ip6_hold_safe(net, &rt, true)) { 1694d3843fe5SWei Wang dst_use_noref(&rt->dst, jiffies); 1695d52d3997SMartin KaFai Lau rt6_dst_from_metrics_check(rt); 1696d3843fe5SWei Wang } 169766f5d6ceSWei Wang rcu_read_unlock(); 1698b65f164dSPaolo Abeni trace_fib6_table_lookup(net, rt, table, fl6); 1699d52d3997SMartin KaFai Lau return rt; 17003da59bd9SMartin KaFai Lau } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) && 17013da59bd9SMartin KaFai Lau !(rt->rt6i_flags & RTF_GATEWAY))) { 17023da59bd9SMartin KaFai Lau /* Create a RTF_CACHE clone which will not be 17033da59bd9SMartin KaFai Lau * owned by the fib6 tree. It is for the special case where 17043da59bd9SMartin KaFai Lau * the daddr in the skb during the neighbor look-up is different 17053da59bd9SMartin KaFai Lau * from the fl6->daddr used to look-up route here. 17063da59bd9SMartin KaFai Lau */ 1707c71099acSThomas Graf 17083da59bd9SMartin KaFai Lau struct rt6_info *uncached_rt; 17093da59bd9SMartin KaFai Lau 1710d3843fe5SWei Wang if (ip6_hold_safe(net, &rt, true)) { 1711d3843fe5SWei Wang dst_use_noref(&rt->dst, jiffies); 1712d3843fe5SWei Wang } else { 171366f5d6ceSWei Wang rcu_read_unlock(); 1714d3843fe5SWei Wang uncached_rt = rt; 1715d3843fe5SWei Wang goto uncached_rt_out; 1716d3843fe5SWei Wang } 171766f5d6ceSWei Wang rcu_read_unlock(); 1718d52d3997SMartin KaFai Lau 17193da59bd9SMartin KaFai Lau uncached_rt = ip6_rt_cache_alloc(rt, &fl6->daddr, NULL); 17203da59bd9SMartin KaFai Lau dst_release(&rt->dst); 17213da59bd9SMartin KaFai Lau 17221cfb71eeSWei Wang if (uncached_rt) { 17231cfb71eeSWei Wang /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc() 17241cfb71eeSWei Wang * No need for another dst_hold() 17251cfb71eeSWei Wang */ 17268d0b94afSMartin KaFai Lau rt6_uncached_list_add(uncached_rt); 172781eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); 17281cfb71eeSWei Wang } else { 17293da59bd9SMartin KaFai Lau uncached_rt = net->ipv6.ip6_null_entry; 17303da59bd9SMartin KaFai Lau dst_hold(&uncached_rt->dst); 17311cfb71eeSWei Wang } 1732b811580dSDavid Ahern 1733d3843fe5SWei Wang uncached_rt_out: 1734b65f164dSPaolo Abeni trace_fib6_table_lookup(net, uncached_rt, table, fl6); 17353da59bd9SMartin KaFai Lau return uncached_rt; 17363da59bd9SMartin KaFai Lau 1737d52d3997SMartin KaFai Lau } else { 1738d52d3997SMartin KaFai Lau /* Get a percpu copy */ 1739d52d3997SMartin KaFai Lau 1740d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1741d52d3997SMartin KaFai Lau 1742d3843fe5SWei Wang dst_use_noref(&rt->dst, jiffies); 1743951f788aSEric Dumazet local_bh_disable(); 1744d52d3997SMartin KaFai Lau pcpu_rt = rt6_get_pcpu_route(rt); 1745d52d3997SMartin KaFai Lau 1746951f788aSEric Dumazet if (!pcpu_rt) { 1747a94b9367SWei Wang /* atomic_inc_not_zero() is needed when using rcu */ 1748a94b9367SWei Wang if (atomic_inc_not_zero(&rt->rt6i_ref)) { 1749951f788aSEric Dumazet /* No dst_hold() on rt is needed because grabbing 1750a94b9367SWei Wang * rt->rt6i_ref makes sure rt can't be released. 17519c7370a1SMartin KaFai Lau */ 17529c7370a1SMartin KaFai Lau pcpu_rt = rt6_make_pcpu_route(rt); 1753a94b9367SWei Wang rt6_release(rt); 1754a94b9367SWei Wang } else { 1755a94b9367SWei Wang /* rt is already removed from tree */ 1756a94b9367SWei Wang pcpu_rt = net->ipv6.ip6_null_entry; 1757a94b9367SWei Wang dst_hold(&pcpu_rt->dst); 1758a94b9367SWei Wang } 17599c7370a1SMartin KaFai Lau } 1760951f788aSEric Dumazet local_bh_enable(); 1761951f788aSEric Dumazet rcu_read_unlock(); 1762b65f164dSPaolo Abeni trace_fib6_table_lookup(net, pcpu_rt, table, fl6); 1763d52d3997SMartin KaFai Lau return pcpu_rt; 1764d52d3997SMartin KaFai Lau } 1765c71099acSThomas Graf } 17669ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route); 1767c71099acSThomas Graf 17688ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table, 17694c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 17704acad72dSPavel Emelyanov { 17714c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags); 17724acad72dSPavel Emelyanov } 17734acad72dSPavel Emelyanov 1774d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net, 177572331bc0SShmulik Ladkani struct net_device *dev, 177672331bc0SShmulik Ladkani struct flowi6 *fl6, int flags) 177772331bc0SShmulik Ladkani { 177872331bc0SShmulik Ladkani if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) 177972331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_IFACE; 178072331bc0SShmulik Ladkani 178172331bc0SShmulik Ladkani return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input); 178272331bc0SShmulik Ladkani } 1783d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup); 178472331bc0SShmulik Ladkani 178523aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb, 178623aebdacSJakub Sitnicki struct flow_keys *keys) 178723aebdacSJakub Sitnicki { 178823aebdacSJakub Sitnicki const struct ipv6hdr *outer_iph = ipv6_hdr(skb); 178923aebdacSJakub Sitnicki const struct ipv6hdr *key_iph = outer_iph; 179023aebdacSJakub Sitnicki const struct ipv6hdr *inner_iph; 179123aebdacSJakub Sitnicki const struct icmp6hdr *icmph; 179223aebdacSJakub Sitnicki struct ipv6hdr _inner_iph; 179323aebdacSJakub Sitnicki 179423aebdacSJakub Sitnicki if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6)) 179523aebdacSJakub Sitnicki goto out; 179623aebdacSJakub Sitnicki 179723aebdacSJakub Sitnicki icmph = icmp6_hdr(skb); 179823aebdacSJakub Sitnicki if (icmph->icmp6_type != ICMPV6_DEST_UNREACH && 179923aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_PKT_TOOBIG && 180023aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_TIME_EXCEED && 180123aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_PARAMPROB) 180223aebdacSJakub Sitnicki goto out; 180323aebdacSJakub Sitnicki 180423aebdacSJakub Sitnicki inner_iph = skb_header_pointer(skb, 180523aebdacSJakub Sitnicki skb_transport_offset(skb) + sizeof(*icmph), 180623aebdacSJakub Sitnicki sizeof(_inner_iph), &_inner_iph); 180723aebdacSJakub Sitnicki if (!inner_iph) 180823aebdacSJakub Sitnicki goto out; 180923aebdacSJakub Sitnicki 181023aebdacSJakub Sitnicki key_iph = inner_iph; 181123aebdacSJakub Sitnicki out: 181223aebdacSJakub Sitnicki memset(keys, 0, sizeof(*keys)); 181323aebdacSJakub Sitnicki keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 181423aebdacSJakub Sitnicki keys->addrs.v6addrs.src = key_iph->saddr; 181523aebdacSJakub Sitnicki keys->addrs.v6addrs.dst = key_iph->daddr; 181623aebdacSJakub Sitnicki keys->tags.flow_label = ip6_flowinfo(key_iph); 181723aebdacSJakub Sitnicki keys->basic.ip_proto = key_iph->nexthdr; 181823aebdacSJakub Sitnicki } 181923aebdacSJakub Sitnicki 182023aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */ 182123aebdacSJakub Sitnicki u32 rt6_multipath_hash(const struct flowi6 *fl6, const struct sk_buff *skb) 182223aebdacSJakub Sitnicki { 182323aebdacSJakub Sitnicki struct flow_keys hash_keys; 182423aebdacSJakub Sitnicki 182523aebdacSJakub Sitnicki if (skb) { 182623aebdacSJakub Sitnicki ip6_multipath_l3_keys(skb, &hash_keys); 182723aebdacSJakub Sitnicki return flow_hash_from_keys(&hash_keys); 182823aebdacSJakub Sitnicki } 182923aebdacSJakub Sitnicki 183023aebdacSJakub Sitnicki return get_hash_from_flowi6(fl6); 183123aebdacSJakub Sitnicki } 183223aebdacSJakub Sitnicki 1833c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 1834c71099acSThomas Graf { 1835b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 1836c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 1837adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 1838904af04dSJiri Benc struct ip_tunnel_info *tun_info; 18394c9483b2SDavid S. Miller struct flowi6 fl6 = { 1840e0d56fddSDavid Ahern .flowi6_iif = skb->dev->ifindex, 18414c9483b2SDavid S. Miller .daddr = iph->daddr, 18424c9483b2SDavid S. Miller .saddr = iph->saddr, 18436502ca52SYOSHIFUJI Hideaki / 吉藤英明 .flowlabel = ip6_flowinfo(iph), 18444c9483b2SDavid S. Miller .flowi6_mark = skb->mark, 18454c9483b2SDavid S. Miller .flowi6_proto = iph->nexthdr, 1846c71099acSThomas Graf }; 1847adaa70bbSThomas Graf 1848904af04dSJiri Benc tun_info = skb_tunnel_info(skb); 184946fa062aSJiri Benc if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) 1850904af04dSJiri Benc fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id; 185123aebdacSJakub Sitnicki if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6)) 185223aebdacSJakub Sitnicki fl6.mp_hash = rt6_multipath_hash(&fl6, skb); 185306e9d040SJiri Benc skb_dst_drop(skb); 185472331bc0SShmulik Ladkani skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags)); 1855c71099acSThomas Graf } 1856c71099acSThomas Graf 18578ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table, 18584c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 1859c71099acSThomas Graf { 18604c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags); 1861c71099acSThomas Graf } 1862c71099acSThomas Graf 18636f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk, 18646f21c96aSPaolo Abeni struct flowi6 *fl6, int flags) 1865c71099acSThomas Graf { 1866d46a9d67SDavid Ahern bool any_src; 1867c71099acSThomas Graf 18684c1feac5SDavid Ahern if (rt6_need_strict(&fl6->daddr)) { 18694c1feac5SDavid Ahern struct dst_entry *dst; 18704c1feac5SDavid Ahern 18714c1feac5SDavid Ahern dst = l3mdev_link_scope_lookup(net, fl6); 1872ca254490SDavid Ahern if (dst) 1873ca254490SDavid Ahern return dst; 18744c1feac5SDavid Ahern } 1875ca254490SDavid Ahern 18761fb9489bSPavel Emelyanov fl6->flowi6_iif = LOOPBACK_IFINDEX; 18774dc27d1cSDavid McCullough 1878d46a9d67SDavid Ahern any_src = ipv6_addr_any(&fl6->saddr); 1879741a11d9SDavid Ahern if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) || 1880d46a9d67SDavid Ahern (fl6->flowi6_oif && any_src)) 188177d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 1882c71099acSThomas Graf 1883d46a9d67SDavid Ahern if (!any_src) 1884adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 18850c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 18860c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 1887adaa70bbSThomas Graf 18884c9483b2SDavid S. Miller return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output); 18891da177e4SLinus Torvalds } 18906f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags); 18911da177e4SLinus Torvalds 18922774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 189314e50e57SDavid S. Miller { 18945c1e6aa3SDavid S. Miller struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; 18951dbe3252SWei Wang struct net_device *loopback_dev = net->loopback_dev; 189614e50e57SDavid S. Miller struct dst_entry *new = NULL; 189714e50e57SDavid S. Miller 18981dbe3252SWei Wang rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1, 189962cf27e5SSteffen Klassert DST_OBSOLETE_DEAD, 0); 190014e50e57SDavid S. Miller if (rt) { 19010a1f5962SMartin KaFai Lau rt6_info_init(rt); 190281eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_alloc); 19030a1f5962SMartin KaFai Lau 1904d8d1f30bSChangli Gao new = &rt->dst; 190514e50e57SDavid S. Miller new->__use = 1; 1906352e512cSHerbert Xu new->input = dst_discard; 1907ede2059dSEric W. Biederman new->output = dst_discard_out; 190814e50e57SDavid S. Miller 1909defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 191014e50e57SDavid S. Miller 19111dbe3252SWei Wang rt->rt6i_idev = in6_dev_get(loopback_dev); 19124e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 19130a1f5962SMartin KaFai Lau rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU; 191414e50e57SDavid S. Miller rt->rt6i_metric = 0; 191514e50e57SDavid S. Miller 191614e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 191714e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 191814e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 191914e50e57SDavid S. Miller #endif 192014e50e57SDavid S. Miller } 192114e50e57SDavid S. Miller 192269ead7afSDavid S. Miller dst_release(dst_orig); 192369ead7afSDavid S. Miller return new ? new : ERR_PTR(-ENOMEM); 192414e50e57SDavid S. Miller } 192514e50e57SDavid S. Miller 19261da177e4SLinus Torvalds /* 19271da177e4SLinus Torvalds * Destination cache support functions 19281da177e4SLinus Torvalds */ 19291da177e4SLinus Torvalds 19304b32b5adSMartin KaFai Lau static void rt6_dst_from_metrics_check(struct rt6_info *rt) 19314b32b5adSMartin KaFai Lau { 19323a2232e9SDavid Miller if (rt->from && 19333a2232e9SDavid Miller dst_metrics_ptr(&rt->dst) != dst_metrics_ptr(&rt->from->dst)) 19343a2232e9SDavid Miller dst_init_metrics(&rt->dst, dst_metrics_ptr(&rt->from->dst), true); 19354b32b5adSMartin KaFai Lau } 19364b32b5adSMartin KaFai Lau 19373da59bd9SMartin KaFai Lau static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie) 19383da59bd9SMartin KaFai Lau { 193936143645SSteffen Klassert u32 rt_cookie = 0; 1940c5cff856SWei Wang 1941c5cff856SWei Wang if (!rt6_get_cookie_safe(rt, &rt_cookie) || rt_cookie != cookie) 19423da59bd9SMartin KaFai Lau return NULL; 19433da59bd9SMartin KaFai Lau 19443da59bd9SMartin KaFai Lau if (rt6_check_expired(rt)) 19453da59bd9SMartin KaFai Lau return NULL; 19463da59bd9SMartin KaFai Lau 19473da59bd9SMartin KaFai Lau return &rt->dst; 19483da59bd9SMartin KaFai Lau } 19493da59bd9SMartin KaFai Lau 19503da59bd9SMartin KaFai Lau static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, u32 cookie) 19513da59bd9SMartin KaFai Lau { 19525973fb1eSMartin KaFai Lau if (!__rt6_check_expired(rt) && 19535973fb1eSMartin KaFai Lau rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && 19543a2232e9SDavid Miller rt6_check(rt->from, cookie)) 19553da59bd9SMartin KaFai Lau return &rt->dst; 19563da59bd9SMartin KaFai Lau else 19573da59bd9SMartin KaFai Lau return NULL; 19583da59bd9SMartin KaFai Lau } 19593da59bd9SMartin KaFai Lau 19601da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 19611da177e4SLinus Torvalds { 19621da177e4SLinus Torvalds struct rt6_info *rt; 19631da177e4SLinus Torvalds 19641da177e4SLinus Torvalds rt = (struct rt6_info *) dst; 19651da177e4SLinus Torvalds 19666f3118b5SNicolas Dichtel /* All IPV6 dsts are created with ->obsolete set to the value 19676f3118b5SNicolas Dichtel * DST_OBSOLETE_FORCE_CHK which forces validation calls down 19686f3118b5SNicolas Dichtel * into this function always. 19696f3118b5SNicolas Dichtel */ 1970e3bc10bdSHannes Frederic Sowa 19714b32b5adSMartin KaFai Lau rt6_dst_from_metrics_check(rt); 19724b32b5adSMartin KaFai Lau 197302bcf4e0SMartin KaFai Lau if (rt->rt6i_flags & RTF_PCPU || 19743a2232e9SDavid Miller (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->from)) 19753da59bd9SMartin KaFai Lau return rt6_dst_from_check(rt, cookie); 19763da59bd9SMartin KaFai Lau else 19773da59bd9SMartin KaFai Lau return rt6_check(rt, cookie); 19781da177e4SLinus Torvalds } 19791da177e4SLinus Torvalds 19801da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 19811da177e4SLinus Torvalds { 19821da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 19831da177e4SLinus Torvalds 19841da177e4SLinus Torvalds if (rt) { 198554c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 198654c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 1987e0a1ad73SThomas Graf ip6_del_rt(rt); 198854c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 19891da177e4SLinus Torvalds } 199054c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 199154c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 199254c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 199354c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 199454c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 199554c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 19961da177e4SLinus Torvalds } 19971da177e4SLinus Torvalds 19981da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 19991da177e4SLinus Torvalds { 20001da177e4SLinus Torvalds struct rt6_info *rt; 20011da177e4SLinus Torvalds 20023ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 20031da177e4SLinus Torvalds 2004adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 20051da177e4SLinus Torvalds if (rt) { 20061eb4f758SHannes Frederic Sowa if (rt->rt6i_flags & RTF_CACHE) { 2007ad65a2f0SWei Wang if (dst_hold_safe(&rt->dst)) 20088e3d5be7SMartin KaFai Lau ip6_del_rt(rt); 2009c5cff856SWei Wang } else { 2010c5cff856SWei Wang struct fib6_node *fn; 2011c5cff856SWei Wang 2012c5cff856SWei Wang rcu_read_lock(); 2013c5cff856SWei Wang fn = rcu_dereference(rt->rt6i_node); 2014c5cff856SWei Wang if (fn && (rt->rt6i_flags & RTF_DEFAULT)) 2015c5cff856SWei Wang fn->fn_sernum = -1; 2016c5cff856SWei Wang rcu_read_unlock(); 20171da177e4SLinus Torvalds } 20181da177e4SLinus Torvalds } 20191eb4f758SHannes Frederic Sowa } 20201da177e4SLinus Torvalds 202145e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu) 202245e4fd26SMartin KaFai Lau { 202345e4fd26SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 202445e4fd26SMartin KaFai Lau 202545e4fd26SMartin KaFai Lau rt->rt6i_flags |= RTF_MODIFIED; 202645e4fd26SMartin KaFai Lau rt->rt6i_pmtu = mtu; 202745e4fd26SMartin KaFai Lau rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires); 202845e4fd26SMartin KaFai Lau } 202945e4fd26SMartin KaFai Lau 20300d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt) 20310d3f6d29SMartin KaFai Lau { 20320d3f6d29SMartin KaFai Lau return !(rt->rt6i_flags & RTF_CACHE) && 20334e587ea7SWei Wang (rt->rt6i_flags & RTF_PCPU || 20344e587ea7SWei Wang rcu_access_pointer(rt->rt6i_node)); 20350d3f6d29SMartin KaFai Lau } 20360d3f6d29SMartin KaFai Lau 203745e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk, 203845e4fd26SMartin KaFai Lau const struct ipv6hdr *iph, u32 mtu) 20391da177e4SLinus Torvalds { 20400dec879fSJulian Anastasov const struct in6_addr *daddr, *saddr; 20411da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info *)dst; 20421da177e4SLinus Torvalds 204345e4fd26SMartin KaFai Lau if (rt6->rt6i_flags & RTF_LOCAL) 204445e4fd26SMartin KaFai Lau return; 204545e4fd26SMartin KaFai Lau 204619bda36cSXin Long if (dst_metric_locked(dst, RTAX_MTU)) 204719bda36cSXin Long return; 204819bda36cSXin Long 204945e4fd26SMartin KaFai Lau if (iph) { 205045e4fd26SMartin KaFai Lau daddr = &iph->daddr; 205145e4fd26SMartin KaFai Lau saddr = &iph->saddr; 205245e4fd26SMartin KaFai Lau } else if (sk) { 205345e4fd26SMartin KaFai Lau daddr = &sk->sk_v6_daddr; 205445e4fd26SMartin KaFai Lau saddr = &inet6_sk(sk)->saddr; 205545e4fd26SMartin KaFai Lau } else { 20560dec879fSJulian Anastasov daddr = NULL; 20570dec879fSJulian Anastasov saddr = NULL; 20581da177e4SLinus Torvalds } 20590dec879fSJulian Anastasov dst_confirm_neigh(dst, daddr); 20600dec879fSJulian Anastasov mtu = max_t(u32, mtu, IPV6_MIN_MTU); 20610dec879fSJulian Anastasov if (mtu >= dst_mtu(dst)) 20620dec879fSJulian Anastasov return; 20630dec879fSJulian Anastasov 20640dec879fSJulian Anastasov if (!rt6_cache_allowed_for_pmtu(rt6)) { 20650dec879fSJulian Anastasov rt6_do_update_pmtu(rt6, mtu); 20662b760fcfSWei Wang /* update rt6_ex->stamp for cache */ 20672b760fcfSWei Wang if (rt6->rt6i_flags & RTF_CACHE) 20682b760fcfSWei Wang rt6_update_exception_stamp_rt(rt6); 20690dec879fSJulian Anastasov } else if (daddr) { 20700dec879fSJulian Anastasov struct rt6_info *nrt6; 20710dec879fSJulian Anastasov 207245e4fd26SMartin KaFai Lau nrt6 = ip6_rt_cache_alloc(rt6, daddr, saddr); 207345e4fd26SMartin KaFai Lau if (nrt6) { 207445e4fd26SMartin KaFai Lau rt6_do_update_pmtu(nrt6, mtu); 20752b760fcfSWei Wang if (rt6_insert_exception(nrt6, rt6)) 20762b760fcfSWei Wang dst_release_immediate(&nrt6->dst); 207745e4fd26SMartin KaFai Lau } 207845e4fd26SMartin KaFai Lau } 207945e4fd26SMartin KaFai Lau } 208045e4fd26SMartin KaFai Lau 208145e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 208245e4fd26SMartin KaFai Lau struct sk_buff *skb, u32 mtu) 208345e4fd26SMartin KaFai Lau { 208445e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu); 20851da177e4SLinus Torvalds } 20861da177e4SLinus Torvalds 208742ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, 2088e2d118a1SLorenzo Colitti int oif, u32 mark, kuid_t uid) 208981aded24SDavid S. Miller { 209081aded24SDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 209181aded24SDavid S. Miller struct dst_entry *dst; 209281aded24SDavid S. Miller struct flowi6 fl6; 209381aded24SDavid S. Miller 209481aded24SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 209581aded24SDavid S. Miller fl6.flowi6_oif = oif; 20961b3c61dcSLorenzo Colitti fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark); 209781aded24SDavid S. Miller fl6.daddr = iph->daddr; 209881aded24SDavid S. Miller fl6.saddr = iph->saddr; 20996502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 2100e2d118a1SLorenzo Colitti fl6.flowi6_uid = uid; 210181aded24SDavid S. Miller 210281aded24SDavid S. Miller dst = ip6_route_output(net, NULL, &fl6); 210381aded24SDavid S. Miller if (!dst->error) 210445e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu)); 210581aded24SDavid S. Miller dst_release(dst); 210681aded24SDavid S. Miller } 210781aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu); 210881aded24SDavid S. Miller 210981aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) 211081aded24SDavid S. Miller { 211133c162a9SMartin KaFai Lau struct dst_entry *dst; 211233c162a9SMartin KaFai Lau 211381aded24SDavid S. Miller ip6_update_pmtu(skb, sock_net(sk), mtu, 2114e2d118a1SLorenzo Colitti sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid); 211533c162a9SMartin KaFai Lau 211633c162a9SMartin KaFai Lau dst = __sk_dst_get(sk); 211733c162a9SMartin KaFai Lau if (!dst || !dst->obsolete || 211833c162a9SMartin KaFai Lau dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) 211933c162a9SMartin KaFai Lau return; 212033c162a9SMartin KaFai Lau 212133c162a9SMartin KaFai Lau bh_lock_sock(sk); 212233c162a9SMartin KaFai Lau if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) 212333c162a9SMartin KaFai Lau ip6_datagram_dst_update(sk, false); 212433c162a9SMartin KaFai Lau bh_unlock_sock(sk); 212581aded24SDavid S. Miller } 212681aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); 212781aded24SDavid S. Miller 2128b55b76b2SDuan Jiong /* Handle redirects */ 2129b55b76b2SDuan Jiong struct ip6rd_flowi { 2130b55b76b2SDuan Jiong struct flowi6 fl6; 2131b55b76b2SDuan Jiong struct in6_addr gateway; 2132b55b76b2SDuan Jiong }; 2133b55b76b2SDuan Jiong 2134b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net, 2135b55b76b2SDuan Jiong struct fib6_table *table, 2136b55b76b2SDuan Jiong struct flowi6 *fl6, 2137b55b76b2SDuan Jiong int flags) 2138b55b76b2SDuan Jiong { 2139b55b76b2SDuan Jiong struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; 21402b760fcfSWei Wang struct rt6_info *rt, *rt_cache; 2141b55b76b2SDuan Jiong struct fib6_node *fn; 2142b55b76b2SDuan Jiong 2143b55b76b2SDuan Jiong /* Get the "current" route for this destination and 214467c408cfSAlexander Alemayhu * check if the redirect has come from appropriate router. 2145b55b76b2SDuan Jiong * 2146b55b76b2SDuan Jiong * RFC 4861 specifies that redirects should only be 2147b55b76b2SDuan Jiong * accepted if they come from the nexthop to the target. 2148b55b76b2SDuan Jiong * Due to the way the routes are chosen, this notion 2149b55b76b2SDuan Jiong * is a bit fuzzy and one might need to check all possible 2150b55b76b2SDuan Jiong * routes. 2151b55b76b2SDuan Jiong */ 2152b55b76b2SDuan Jiong 215366f5d6ceSWei Wang rcu_read_lock(); 2154b55b76b2SDuan Jiong fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 2155b55b76b2SDuan Jiong restart: 215666f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 2157b55b76b2SDuan Jiong if (rt6_check_expired(rt)) 2158b55b76b2SDuan Jiong continue; 2159b55b76b2SDuan Jiong if (rt->dst.error) 2160b55b76b2SDuan Jiong break; 2161b55b76b2SDuan Jiong if (!(rt->rt6i_flags & RTF_GATEWAY)) 2162b55b76b2SDuan Jiong continue; 2163b55b76b2SDuan Jiong if (fl6->flowi6_oif != rt->dst.dev->ifindex) 2164b55b76b2SDuan Jiong continue; 21652b760fcfSWei Wang /* rt_cache's gateway might be different from its 'parent' 21662b760fcfSWei Wang * in the case of an ip redirect. 21672b760fcfSWei Wang * So we keep searching in the exception table if the gateway 21682b760fcfSWei Wang * is different. 21692b760fcfSWei Wang */ 21702b760fcfSWei Wang if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway)) { 21712b760fcfSWei Wang rt_cache = rt6_find_cached_rt(rt, 21722b760fcfSWei Wang &fl6->daddr, 21732b760fcfSWei Wang &fl6->saddr); 21742b760fcfSWei Wang if (rt_cache && 21752b760fcfSWei Wang ipv6_addr_equal(&rdfl->gateway, 21762b760fcfSWei Wang &rt_cache->rt6i_gateway)) { 21772b760fcfSWei Wang rt = rt_cache; 21782b760fcfSWei Wang break; 21792b760fcfSWei Wang } 2180b55b76b2SDuan Jiong continue; 21812b760fcfSWei Wang } 2182b55b76b2SDuan Jiong break; 2183b55b76b2SDuan Jiong } 2184b55b76b2SDuan Jiong 2185b55b76b2SDuan Jiong if (!rt) 2186b55b76b2SDuan Jiong rt = net->ipv6.ip6_null_entry; 2187b55b76b2SDuan Jiong else if (rt->dst.error) { 2188b55b76b2SDuan Jiong rt = net->ipv6.ip6_null_entry; 2189b0a1ba59SMartin KaFai Lau goto out; 2190b0a1ba59SMartin KaFai Lau } 2191b0a1ba59SMartin KaFai Lau 2192b0a1ba59SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 2193a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 2194a3c00e46SMartin KaFai Lau if (fn) 2195a3c00e46SMartin KaFai Lau goto restart; 2196b55b76b2SDuan Jiong } 2197a3c00e46SMartin KaFai Lau 2198b0a1ba59SMartin KaFai Lau out: 2199d3843fe5SWei Wang ip6_hold_safe(net, &rt, true); 2200b55b76b2SDuan Jiong 220166f5d6ceSWei Wang rcu_read_unlock(); 2202b55b76b2SDuan Jiong 2203b65f164dSPaolo Abeni trace_fib6_table_lookup(net, rt, table, fl6); 2204b55b76b2SDuan Jiong return rt; 2205b55b76b2SDuan Jiong }; 2206b55b76b2SDuan Jiong 2207b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net, 2208b55b76b2SDuan Jiong const struct flowi6 *fl6, 2209b55b76b2SDuan Jiong const struct in6_addr *gateway) 2210b55b76b2SDuan Jiong { 2211b55b76b2SDuan Jiong int flags = RT6_LOOKUP_F_HAS_SADDR; 2212b55b76b2SDuan Jiong struct ip6rd_flowi rdfl; 2213b55b76b2SDuan Jiong 2214b55b76b2SDuan Jiong rdfl.fl6 = *fl6; 2215b55b76b2SDuan Jiong rdfl.gateway = *gateway; 2216b55b76b2SDuan Jiong 2217b55b76b2SDuan Jiong return fib6_rule_lookup(net, &rdfl.fl6, 2218b55b76b2SDuan Jiong flags, __ip6_route_redirect); 2219b55b76b2SDuan Jiong } 2220b55b76b2SDuan Jiong 2221e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, 2222e2d118a1SLorenzo Colitti kuid_t uid) 22233a5ad2eeSDavid S. Miller { 22243a5ad2eeSDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 22253a5ad2eeSDavid S. Miller struct dst_entry *dst; 22263a5ad2eeSDavid S. Miller struct flowi6 fl6; 22273a5ad2eeSDavid S. Miller 22283a5ad2eeSDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 2229e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 22303a5ad2eeSDavid S. Miller fl6.flowi6_oif = oif; 22313a5ad2eeSDavid S. Miller fl6.flowi6_mark = mark; 22323a5ad2eeSDavid S. Miller fl6.daddr = iph->daddr; 22333a5ad2eeSDavid S. Miller fl6.saddr = iph->saddr; 22346502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 2235e2d118a1SLorenzo Colitti fl6.flowi6_uid = uid; 22363a5ad2eeSDavid S. Miller 2237b55b76b2SDuan Jiong dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr); 22386700c270SDavid S. Miller rt6_do_redirect(dst, NULL, skb); 22393a5ad2eeSDavid S. Miller dst_release(dst); 22403a5ad2eeSDavid S. Miller } 22413a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect); 22423a5ad2eeSDavid S. Miller 2243c92a59ecSDuan Jiong void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif, 2244c92a59ecSDuan Jiong u32 mark) 2245c92a59ecSDuan Jiong { 2246c92a59ecSDuan Jiong const struct ipv6hdr *iph = ipv6_hdr(skb); 2247c92a59ecSDuan Jiong const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); 2248c92a59ecSDuan Jiong struct dst_entry *dst; 2249c92a59ecSDuan Jiong struct flowi6 fl6; 2250c92a59ecSDuan Jiong 2251c92a59ecSDuan Jiong memset(&fl6, 0, sizeof(fl6)); 2252e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 2253c92a59ecSDuan Jiong fl6.flowi6_oif = oif; 2254c92a59ecSDuan Jiong fl6.flowi6_mark = mark; 2255c92a59ecSDuan Jiong fl6.daddr = msg->dest; 2256c92a59ecSDuan Jiong fl6.saddr = iph->daddr; 2257e2d118a1SLorenzo Colitti fl6.flowi6_uid = sock_net_uid(net, NULL); 2258c92a59ecSDuan Jiong 2259b55b76b2SDuan Jiong dst = ip6_route_redirect(net, &fl6, &iph->saddr); 2260c92a59ecSDuan Jiong rt6_do_redirect(dst, NULL, skb); 2261c92a59ecSDuan Jiong dst_release(dst); 2262c92a59ecSDuan Jiong } 2263c92a59ecSDuan Jiong 22643a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) 22653a5ad2eeSDavid S. Miller { 2266e2d118a1SLorenzo Colitti ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark, 2267e2d118a1SLorenzo Colitti sk->sk_uid); 22683a5ad2eeSDavid S. Miller } 22693a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect); 22703a5ad2eeSDavid S. Miller 22710dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 22721da177e4SLinus Torvalds { 22730dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 22740dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 22750dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 22760dbaee3bSDavid S. Miller 22771da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 22781da177e4SLinus Torvalds 22795578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 22805578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 22811da177e4SLinus Torvalds 22821da177e4SLinus Torvalds /* 22831da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 22841da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 22851da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 22861da177e4SLinus Torvalds * rely only on pmtu discovery" 22871da177e4SLinus Torvalds */ 22881da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 22891da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 22901da177e4SLinus Torvalds return mtu; 22911da177e4SLinus Torvalds } 22921da177e4SLinus Torvalds 2293ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst) 2294d33e4553SDavid S. Miller { 22954b32b5adSMartin KaFai Lau const struct rt6_info *rt = (const struct rt6_info *)dst; 22964b32b5adSMartin KaFai Lau unsigned int mtu = rt->rt6i_pmtu; 2297d33e4553SDavid S. Miller struct inet6_dev *idev; 2298618f9bc7SSteffen Klassert 2299618f9bc7SSteffen Klassert if (mtu) 230030f78d8eSEric Dumazet goto out; 2301618f9bc7SSteffen Klassert 23024b32b5adSMartin KaFai Lau mtu = dst_metric_raw(dst, RTAX_MTU); 23034b32b5adSMartin KaFai Lau if (mtu) 23044b32b5adSMartin KaFai Lau goto out; 23054b32b5adSMartin KaFai Lau 2306618f9bc7SSteffen Klassert mtu = IPV6_MIN_MTU; 2307d33e4553SDavid S. Miller 2308d33e4553SDavid S. Miller rcu_read_lock(); 2309d33e4553SDavid S. Miller idev = __in6_dev_get(dst->dev); 2310d33e4553SDavid S. Miller if (idev) 2311d33e4553SDavid S. Miller mtu = idev->cnf.mtu6; 2312d33e4553SDavid S. Miller rcu_read_unlock(); 2313d33e4553SDavid S. Miller 231430f78d8eSEric Dumazet out: 231514972cbdSRoopa Prabhu mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 231614972cbdSRoopa Prabhu 231714972cbdSRoopa Prabhu return mtu - lwtunnel_headroom(dst->lwtstate, mtu); 2318d33e4553SDavid S. Miller } 2319d33e4553SDavid S. Miller 23203b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 232187a11578SDavid S. Miller struct flowi6 *fl6) 23221da177e4SLinus Torvalds { 232387a11578SDavid S. Miller struct dst_entry *dst; 23241da177e4SLinus Torvalds struct rt6_info *rt; 23251da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 2326c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 23271da177e4SLinus Torvalds 232838308473SDavid S. Miller if (unlikely(!idev)) 2329122bdf67SEric Dumazet return ERR_PTR(-ENODEV); 23301da177e4SLinus Torvalds 2331ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, dev, 0); 233238308473SDavid S. Miller if (unlikely(!rt)) { 23331da177e4SLinus Torvalds in6_dev_put(idev); 233487a11578SDavid S. Miller dst = ERR_PTR(-ENOMEM); 23351da177e4SLinus Torvalds goto out; 23361da177e4SLinus Torvalds } 23371da177e4SLinus Torvalds 23388e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 2339588753f1SBrendan McGrath rt->dst.input = ip6_input; 23408e2ec639SYan, Zheng rt->dst.output = ip6_output; 2341550bab42SJulian Anastasov rt->rt6i_gateway = fl6->daddr; 234287a11578SDavid S. Miller rt->rt6i_dst.addr = fl6->daddr; 23438e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 23448e2ec639SYan, Zheng rt->rt6i_idev = idev; 234514edd87dSLi RongQing dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); 23461da177e4SLinus Torvalds 2347587fea74SWei Wang /* Add this dst into uncached_list so that rt6_ifdown() can 2348587fea74SWei Wang * do proper release of the net_device 2349587fea74SWei Wang */ 2350587fea74SWei Wang rt6_uncached_list_add(rt); 235181eb8447SWei Wang atomic_inc(&net->ipv6.rt6_stats->fib_rt_uncache); 23521da177e4SLinus Torvalds 235387a11578SDavid S. Miller dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); 235487a11578SDavid S. Miller 23551da177e4SLinus Torvalds out: 235687a11578SDavid S. Miller return dst; 23571da177e4SLinus Torvalds } 23581da177e4SLinus Torvalds 2359569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops) 23601da177e4SLinus Torvalds { 236186393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 23627019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 23637019b78eSDaniel Lezcano int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; 23647019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 23657019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 23667019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 2367fc66f95cSEric Dumazet int entries; 23681da177e4SLinus Torvalds 2369fc66f95cSEric Dumazet entries = dst_entries_get_fast(ops); 237049a18d86SMichal Kubeček if (time_after(rt_last_gc + rt_min_interval, jiffies) && 2371fc66f95cSEric Dumazet entries <= rt_max_size) 23721da177e4SLinus Torvalds goto out; 23731da177e4SLinus Torvalds 23746891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire++; 237514956643SLi RongQing fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true); 2376fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 2377fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 23787019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; 23791da177e4SLinus Torvalds out: 23807019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; 2381fc66f95cSEric Dumazet return entries > rt_max_size; 23821da177e4SLinus Torvalds } 23831da177e4SLinus Torvalds 2384e715b6d3SFlorian Westphal static int ip6_convert_metrics(struct mx6_config *mxc, 2385e715b6d3SFlorian Westphal const struct fib6_config *cfg) 2386e715b6d3SFlorian Westphal { 23876670e152SStephen Hemminger struct net *net = cfg->fc_nlinfo.nl_net; 2388c3a8d947SDaniel Borkmann bool ecn_ca = false; 2389e715b6d3SFlorian Westphal struct nlattr *nla; 2390e715b6d3SFlorian Westphal int remaining; 2391e715b6d3SFlorian Westphal u32 *mp; 2392e715b6d3SFlorian Westphal 239363159f29SIan Morris if (!cfg->fc_mx) 2394e715b6d3SFlorian Westphal return 0; 2395e715b6d3SFlorian Westphal 2396e715b6d3SFlorian Westphal mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL); 2397e715b6d3SFlorian Westphal if (unlikely(!mp)) 2398e715b6d3SFlorian Westphal return -ENOMEM; 2399e715b6d3SFlorian Westphal 2400e715b6d3SFlorian Westphal nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { 2401e715b6d3SFlorian Westphal int type = nla_type(nla); 2402ea697639SDaniel Borkmann u32 val; 2403ea697639SDaniel Borkmann 24041bb14807SDaniel Borkmann if (!type) 24051bb14807SDaniel Borkmann continue; 2406e715b6d3SFlorian Westphal if (unlikely(type > RTAX_MAX)) 2407e715b6d3SFlorian Westphal goto err; 24081bb14807SDaniel Borkmann 2409ea697639SDaniel Borkmann if (type == RTAX_CC_ALGO) { 2410ea697639SDaniel Borkmann char tmp[TCP_CA_NAME_MAX]; 2411e715b6d3SFlorian Westphal 2412ea697639SDaniel Borkmann nla_strlcpy(tmp, nla, sizeof(tmp)); 24136670e152SStephen Hemminger val = tcp_ca_get_key_by_name(net, tmp, &ecn_ca); 2414ea697639SDaniel Borkmann if (val == TCP_CA_UNSPEC) 2415ea697639SDaniel Borkmann goto err; 2416ea697639SDaniel Borkmann } else { 2417ea697639SDaniel Borkmann val = nla_get_u32(nla); 2418ea697639SDaniel Borkmann } 2419626abd59SPaolo Abeni if (type == RTAX_HOPLIMIT && val > 255) 2420626abd59SPaolo Abeni val = 255; 2421b8d3e416SDaniel Borkmann if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK)) 2422b8d3e416SDaniel Borkmann goto err; 2423ea697639SDaniel Borkmann 2424ea697639SDaniel Borkmann mp[type - 1] = val; 2425e715b6d3SFlorian Westphal __set_bit(type - 1, mxc->mx_valid); 2426e715b6d3SFlorian Westphal } 2427e715b6d3SFlorian Westphal 2428c3a8d947SDaniel Borkmann if (ecn_ca) { 2429c3a8d947SDaniel Borkmann __set_bit(RTAX_FEATURES - 1, mxc->mx_valid); 2430c3a8d947SDaniel Borkmann mp[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA; 2431c3a8d947SDaniel Borkmann } 2432e715b6d3SFlorian Westphal 2433c3a8d947SDaniel Borkmann mxc->mx = mp; 2434e715b6d3SFlorian Westphal return 0; 2435e715b6d3SFlorian Westphal err: 2436e715b6d3SFlorian Westphal kfree(mp); 2437e715b6d3SFlorian Westphal return -EINVAL; 2438e715b6d3SFlorian Westphal } 24391da177e4SLinus Torvalds 24408c14586fSDavid Ahern static struct rt6_info *ip6_nh_lookup_table(struct net *net, 24418c14586fSDavid Ahern struct fib6_config *cfg, 24428c14586fSDavid Ahern const struct in6_addr *gw_addr) 24438c14586fSDavid Ahern { 24448c14586fSDavid Ahern struct flowi6 fl6 = { 24458c14586fSDavid Ahern .flowi6_oif = cfg->fc_ifindex, 24468c14586fSDavid Ahern .daddr = *gw_addr, 24478c14586fSDavid Ahern .saddr = cfg->fc_prefsrc, 24488c14586fSDavid Ahern }; 24498c14586fSDavid Ahern struct fib6_table *table; 24508c14586fSDavid Ahern struct rt6_info *rt; 2451d5d32e4bSDavid Ahern int flags = RT6_LOOKUP_F_IFACE | RT6_LOOKUP_F_IGNORE_LINKSTATE; 24528c14586fSDavid Ahern 24538c14586fSDavid Ahern table = fib6_get_table(net, cfg->fc_table); 24548c14586fSDavid Ahern if (!table) 24558c14586fSDavid Ahern return NULL; 24568c14586fSDavid Ahern 24578c14586fSDavid Ahern if (!ipv6_addr_any(&cfg->fc_prefsrc)) 24588c14586fSDavid Ahern flags |= RT6_LOOKUP_F_HAS_SADDR; 24598c14586fSDavid Ahern 24608c14586fSDavid Ahern rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, flags); 24618c14586fSDavid Ahern 24628c14586fSDavid Ahern /* if table lookup failed, fall back to full lookup */ 24638c14586fSDavid Ahern if (rt == net->ipv6.ip6_null_entry) { 24648c14586fSDavid Ahern ip6_rt_put(rt); 24658c14586fSDavid Ahern rt = NULL; 24668c14586fSDavid Ahern } 24678c14586fSDavid Ahern 24688c14586fSDavid Ahern return rt; 24698c14586fSDavid Ahern } 24708c14586fSDavid Ahern 2471333c4301SDavid Ahern static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg, 2472333c4301SDavid Ahern struct netlink_ext_ack *extack) 24731da177e4SLinus Torvalds { 24745578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 24751da177e4SLinus Torvalds struct rt6_info *rt = NULL; 24761da177e4SLinus Torvalds struct net_device *dev = NULL; 24771da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 2478c71099acSThomas Graf struct fib6_table *table; 24791da177e4SLinus Torvalds int addr_type; 24808c5b83f0SRoopa Prabhu int err = -EINVAL; 24811da177e4SLinus Torvalds 2482557c44beSDavid Ahern /* RTF_PCPU is an internal flag; can not be set by userspace */ 2483d5d531cbSDavid Ahern if (cfg->fc_flags & RTF_PCPU) { 2484d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU"); 2485557c44beSDavid Ahern goto out; 2486d5d531cbSDavid Ahern } 2487557c44beSDavid Ahern 24882ea2352eSWei Wang /* RTF_CACHE is an internal flag; can not be set by userspace */ 24892ea2352eSWei Wang if (cfg->fc_flags & RTF_CACHE) { 24902ea2352eSWei Wang NL_SET_ERR_MSG(extack, "Userspace can not set RTF_CACHE"); 24912ea2352eSWei Wang goto out; 24922ea2352eSWei Wang } 24932ea2352eSWei Wang 2494d5d531cbSDavid Ahern if (cfg->fc_dst_len > 128) { 2495d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid prefix length"); 24968c5b83f0SRoopa Prabhu goto out; 2497d5d531cbSDavid Ahern } 2498d5d531cbSDavid Ahern if (cfg->fc_src_len > 128) { 2499d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address length"); 2500d5d531cbSDavid Ahern goto out; 2501d5d531cbSDavid Ahern } 25021da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 2503d5d531cbSDavid Ahern if (cfg->fc_src_len) { 2504d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2505d5d531cbSDavid Ahern "Specifying source address requires IPV6_SUBTREES to be enabled"); 25068c5b83f0SRoopa Prabhu goto out; 2507d5d531cbSDavid Ahern } 25081da177e4SLinus Torvalds #endif 250986872cb5SThomas Graf if (cfg->fc_ifindex) { 25101da177e4SLinus Torvalds err = -ENODEV; 25115578689aSDaniel Lezcano dev = dev_get_by_index(net, cfg->fc_ifindex); 25121da177e4SLinus Torvalds if (!dev) 25131da177e4SLinus Torvalds goto out; 25141da177e4SLinus Torvalds idev = in6_dev_get(dev); 25151da177e4SLinus Torvalds if (!idev) 25161da177e4SLinus Torvalds goto out; 25171da177e4SLinus Torvalds } 25181da177e4SLinus Torvalds 251986872cb5SThomas Graf if (cfg->fc_metric == 0) 252086872cb5SThomas Graf cfg->fc_metric = IP6_RT_PRIO_USER; 25211da177e4SLinus Torvalds 2522c71099acSThomas Graf err = -ENOBUFS; 252338308473SDavid S. Miller if (cfg->fc_nlinfo.nlh && 2524d71314b4SMatti Vaittinen !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { 2525d71314b4SMatti Vaittinen table = fib6_get_table(net, cfg->fc_table); 252638308473SDavid S. Miller if (!table) { 2527f3213831SJoe Perches pr_warn("NLM_F_CREATE should be specified when creating new route\n"); 2528d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 2529d71314b4SMatti Vaittinen } 2530d71314b4SMatti Vaittinen } else { 2531d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 2532d71314b4SMatti Vaittinen } 253338308473SDavid S. Miller 253438308473SDavid S. Miller if (!table) 2535c71099acSThomas Graf goto out; 2536c71099acSThomas Graf 2537ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, NULL, 2538ad706862SMartin KaFai Lau (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT); 25391da177e4SLinus Torvalds 254038308473SDavid S. Miller if (!rt) { 25411da177e4SLinus Torvalds err = -ENOMEM; 25421da177e4SLinus Torvalds goto out; 25431da177e4SLinus Torvalds } 25441da177e4SLinus Torvalds 25451716a961SGao feng if (cfg->fc_flags & RTF_EXPIRES) 25461716a961SGao feng rt6_set_expires(rt, jiffies + 25471716a961SGao feng clock_t_to_jiffies(cfg->fc_expires)); 25481716a961SGao feng else 25491716a961SGao feng rt6_clean_expires(rt); 25501da177e4SLinus Torvalds 255186872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 255286872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 255386872cb5SThomas Graf rt->rt6i_protocol = cfg->fc_protocol; 255486872cb5SThomas Graf 255586872cb5SThomas Graf addr_type = ipv6_addr_type(&cfg->fc_dst); 25561da177e4SLinus Torvalds 25571da177e4SLinus Torvalds if (addr_type & IPV6_ADDR_MULTICAST) 2558d8d1f30bSChangli Gao rt->dst.input = ip6_mc_input; 2559ab79ad14SMaciej Żenczykowski else if (cfg->fc_flags & RTF_LOCAL) 2560ab79ad14SMaciej Żenczykowski rt->dst.input = ip6_input; 25611da177e4SLinus Torvalds else 2562d8d1f30bSChangli Gao rt->dst.input = ip6_forward; 25631da177e4SLinus Torvalds 2564d8d1f30bSChangli Gao rt->dst.output = ip6_output; 25651da177e4SLinus Torvalds 256619e42e45SRoopa Prabhu if (cfg->fc_encap) { 256719e42e45SRoopa Prabhu struct lwtunnel_state *lwtstate; 256819e42e45SRoopa Prabhu 256930357d7dSDavid Ahern err = lwtunnel_build_state(cfg->fc_encap_type, 2570127eb7cdSTom Herbert cfg->fc_encap, AF_INET6, cfg, 25719ae28727SDavid Ahern &lwtstate, extack); 257219e42e45SRoopa Prabhu if (err) 257319e42e45SRoopa Prabhu goto out; 257461adedf3SJiri Benc rt->dst.lwtstate = lwtstate_get(lwtstate); 257561adedf3SJiri Benc if (lwtunnel_output_redirect(rt->dst.lwtstate)) { 257661adedf3SJiri Benc rt->dst.lwtstate->orig_output = rt->dst.output; 257761adedf3SJiri Benc rt->dst.output = lwtunnel_output; 257819e42e45SRoopa Prabhu } 257961adedf3SJiri Benc if (lwtunnel_input_redirect(rt->dst.lwtstate)) { 258061adedf3SJiri Benc rt->dst.lwtstate->orig_input = rt->dst.input; 258161adedf3SJiri Benc rt->dst.input = lwtunnel_input; 258225368623STom Herbert } 258325368623STom Herbert } 258419e42e45SRoopa Prabhu 258586872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 258686872cb5SThomas Graf rt->rt6i_dst.plen = cfg->fc_dst_len; 2587afc4eef8SMartin KaFai Lau if (rt->rt6i_dst.plen == 128) 258811d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 25891da177e4SLinus Torvalds 25901da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 259186872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len); 259286872cb5SThomas Graf rt->rt6i_src.plen = cfg->fc_src_len; 25931da177e4SLinus Torvalds #endif 25941da177e4SLinus Torvalds 259586872cb5SThomas Graf rt->rt6i_metric = cfg->fc_metric; 25961da177e4SLinus Torvalds 25971da177e4SLinus Torvalds /* We cannot add true routes via loopback here, 25981da177e4SLinus Torvalds they would result in kernel looping; promote them to reject routes 25991da177e4SLinus Torvalds */ 260086872cb5SThomas Graf if ((cfg->fc_flags & RTF_REJECT) || 260138308473SDavid S. Miller (dev && (dev->flags & IFF_LOOPBACK) && 260238308473SDavid S. Miller !(addr_type & IPV6_ADDR_LOOPBACK) && 260338308473SDavid S. Miller !(cfg->fc_flags & RTF_LOCAL))) { 26041da177e4SLinus Torvalds /* hold loopback dev/idev if we haven't done so. */ 26055578689aSDaniel Lezcano if (dev != net->loopback_dev) { 26061da177e4SLinus Torvalds if (dev) { 26071da177e4SLinus Torvalds dev_put(dev); 26081da177e4SLinus Torvalds in6_dev_put(idev); 26091da177e4SLinus Torvalds } 26105578689aSDaniel Lezcano dev = net->loopback_dev; 26111da177e4SLinus Torvalds dev_hold(dev); 26121da177e4SLinus Torvalds idev = in6_dev_get(dev); 26131da177e4SLinus Torvalds if (!idev) { 26141da177e4SLinus Torvalds err = -ENODEV; 26151da177e4SLinus Torvalds goto out; 26161da177e4SLinus Torvalds } 26171da177e4SLinus Torvalds } 26181da177e4SLinus Torvalds rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP; 2619ef2c7d7bSNicolas Dichtel switch (cfg->fc_type) { 2620ef2c7d7bSNicolas Dichtel case RTN_BLACKHOLE: 2621ef2c7d7bSNicolas Dichtel rt->dst.error = -EINVAL; 2622ede2059dSEric W. Biederman rt->dst.output = dst_discard_out; 26237150aedeSKamala R rt->dst.input = dst_discard; 2624ef2c7d7bSNicolas Dichtel break; 2625ef2c7d7bSNicolas Dichtel case RTN_PROHIBIT: 2626ef2c7d7bSNicolas Dichtel rt->dst.error = -EACCES; 26277150aedeSKamala R rt->dst.output = ip6_pkt_prohibit_out; 26287150aedeSKamala R rt->dst.input = ip6_pkt_prohibit; 2629ef2c7d7bSNicolas Dichtel break; 2630b4949ab2SNicolas Dichtel case RTN_THROW: 26310315e382SNikola Forró case RTN_UNREACHABLE: 2632ef2c7d7bSNicolas Dichtel default: 26337150aedeSKamala R rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN 26340315e382SNikola Forró : (cfg->fc_type == RTN_UNREACHABLE) 26350315e382SNikola Forró ? -EHOSTUNREACH : -ENETUNREACH; 26367150aedeSKamala R rt->dst.output = ip6_pkt_discard_out; 26377150aedeSKamala R rt->dst.input = ip6_pkt_discard; 2638ef2c7d7bSNicolas Dichtel break; 2639ef2c7d7bSNicolas Dichtel } 26401da177e4SLinus Torvalds goto install_route; 26411da177e4SLinus Torvalds } 26421da177e4SLinus Torvalds 264386872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY) { 2644b71d1d42SEric Dumazet const struct in6_addr *gw_addr; 26451da177e4SLinus Torvalds int gwa_type; 26461da177e4SLinus Torvalds 264786872cb5SThomas Graf gw_addr = &cfg->fc_gateway; 2648330567b7SFlorian Westphal gwa_type = ipv6_addr_type(gw_addr); 264948ed7b26SFlorian Westphal 265048ed7b26SFlorian Westphal /* if gw_addr is local we will fail to detect this in case 265148ed7b26SFlorian Westphal * address is still TENTATIVE (DAD in progress). rt6_lookup() 265248ed7b26SFlorian Westphal * will return already-added prefix route via interface that 265348ed7b26SFlorian Westphal * prefix route was assigned to, which might be non-loopback. 265448ed7b26SFlorian Westphal */ 265548ed7b26SFlorian Westphal err = -EINVAL; 2656330567b7SFlorian Westphal if (ipv6_chk_addr_and_flags(net, gw_addr, 2657330567b7SFlorian Westphal gwa_type & IPV6_ADDR_LINKLOCAL ? 2658d5d531cbSDavid Ahern dev : NULL, 0, 0)) { 2659d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid gateway address"); 266048ed7b26SFlorian Westphal goto out; 2661d5d531cbSDavid Ahern } 26624e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = *gw_addr; 26631da177e4SLinus Torvalds 26641da177e4SLinus Torvalds if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) { 26658c14586fSDavid Ahern struct rt6_info *grt = NULL; 26661da177e4SLinus Torvalds 26671da177e4SLinus Torvalds /* IPv6 strictly inhibits using not link-local 26681da177e4SLinus Torvalds addresses as nexthop address. 26691da177e4SLinus Torvalds Otherwise, router will not able to send redirects. 26701da177e4SLinus Torvalds It is very good, but in some (rare!) circumstances 26711da177e4SLinus Torvalds (SIT, PtP, NBMA NOARP links) it is handy to allow 26721da177e4SLinus Torvalds some exceptions. --ANK 267396d5822cSErik Nordmark We allow IPv4-mapped nexthops to support RFC4798-type 267496d5822cSErik Nordmark addressing 26751da177e4SLinus Torvalds */ 267696d5822cSErik Nordmark if (!(gwa_type & (IPV6_ADDR_UNICAST | 2677d5d531cbSDavid Ahern IPV6_ADDR_MAPPED))) { 2678d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2679d5d531cbSDavid Ahern "Invalid gateway address"); 26801da177e4SLinus Torvalds goto out; 2681d5d531cbSDavid Ahern } 26821da177e4SLinus Torvalds 2683a435a07fSVincent Bernat if (cfg->fc_table) { 26848c14586fSDavid Ahern grt = ip6_nh_lookup_table(net, cfg, gw_addr); 26858c14586fSDavid Ahern 2686a435a07fSVincent Bernat if (grt) { 2687a435a07fSVincent Bernat if (grt->rt6i_flags & RTF_GATEWAY || 2688a435a07fSVincent Bernat (dev && dev != grt->dst.dev)) { 2689a435a07fSVincent Bernat ip6_rt_put(grt); 2690a435a07fSVincent Bernat grt = NULL; 2691a435a07fSVincent Bernat } 2692a435a07fSVincent Bernat } 2693a435a07fSVincent Bernat } 2694a435a07fSVincent Bernat 26958c14586fSDavid Ahern if (!grt) 26968c14586fSDavid Ahern grt = rt6_lookup(net, gw_addr, NULL, 26978c14586fSDavid Ahern cfg->fc_ifindex, 1); 26981da177e4SLinus Torvalds 26991da177e4SLinus Torvalds err = -EHOSTUNREACH; 270038308473SDavid S. Miller if (!grt) 27011da177e4SLinus Torvalds goto out; 27021da177e4SLinus Torvalds if (dev) { 2703d1918542SDavid S. Miller if (dev != grt->dst.dev) { 270494e187c0SAmerigo Wang ip6_rt_put(grt); 27051da177e4SLinus Torvalds goto out; 27061da177e4SLinus Torvalds } 27071da177e4SLinus Torvalds } else { 2708d1918542SDavid S. Miller dev = grt->dst.dev; 27091da177e4SLinus Torvalds idev = grt->rt6i_idev; 27101da177e4SLinus Torvalds dev_hold(dev); 27111da177e4SLinus Torvalds in6_dev_hold(grt->rt6i_idev); 27121da177e4SLinus Torvalds } 27131da177e4SLinus Torvalds if (!(grt->rt6i_flags & RTF_GATEWAY)) 27141da177e4SLinus Torvalds err = 0; 271594e187c0SAmerigo Wang ip6_rt_put(grt); 27161da177e4SLinus Torvalds 27171da177e4SLinus Torvalds if (err) 27181da177e4SLinus Torvalds goto out; 27191da177e4SLinus Torvalds } 27201da177e4SLinus Torvalds err = -EINVAL; 2721d5d531cbSDavid Ahern if (!dev) { 2722d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Egress device not specified"); 27231da177e4SLinus Torvalds goto out; 2724d5d531cbSDavid Ahern } else if (dev->flags & IFF_LOOPBACK) { 2725d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2726d5d531cbSDavid Ahern "Egress device can not be loopback device for this route"); 2727d5d531cbSDavid Ahern goto out; 2728d5d531cbSDavid Ahern } 27291da177e4SLinus Torvalds } 27301da177e4SLinus Torvalds 27311da177e4SLinus Torvalds err = -ENODEV; 273238308473SDavid S. Miller if (!dev) 27331da177e4SLinus Torvalds goto out; 27341da177e4SLinus Torvalds 2735c3968a85SDaniel Walter if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 2736c3968a85SDaniel Walter if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 2737d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address"); 2738c3968a85SDaniel Walter err = -EINVAL; 2739c3968a85SDaniel Walter goto out; 2740c3968a85SDaniel Walter } 27414e3fd7a0SAlexey Dobriyan rt->rt6i_prefsrc.addr = cfg->fc_prefsrc; 2742c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 128; 2743c3968a85SDaniel Walter } else 2744c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 2745c3968a85SDaniel Walter 274686872cb5SThomas Graf rt->rt6i_flags = cfg->fc_flags; 27471da177e4SLinus Torvalds 27481da177e4SLinus Torvalds install_route: 2749d8d1f30bSChangli Gao rt->dst.dev = dev; 27501da177e4SLinus Torvalds rt->rt6i_idev = idev; 2751c71099acSThomas Graf rt->rt6i_table = table; 275263152fc0SDaniel Lezcano 2753c346dca1SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = dev_net(dev); 275463152fc0SDaniel Lezcano 27558c5b83f0SRoopa Prabhu return rt; 27561da177e4SLinus Torvalds out: 27571da177e4SLinus Torvalds if (dev) 27581da177e4SLinus Torvalds dev_put(dev); 27591da177e4SLinus Torvalds if (idev) 27601da177e4SLinus Torvalds in6_dev_put(idev); 2761587fea74SWei Wang if (rt) 2762587fea74SWei Wang dst_release_immediate(&rt->dst); 27636b9ea5a6SRoopa Prabhu 27648c5b83f0SRoopa Prabhu return ERR_PTR(err); 27656b9ea5a6SRoopa Prabhu } 27666b9ea5a6SRoopa Prabhu 2767333c4301SDavid Ahern int ip6_route_add(struct fib6_config *cfg, 2768333c4301SDavid Ahern struct netlink_ext_ack *extack) 27696b9ea5a6SRoopa Prabhu { 27706b9ea5a6SRoopa Prabhu struct mx6_config mxc = { .mx = NULL, }; 27718c5b83f0SRoopa Prabhu struct rt6_info *rt; 27726b9ea5a6SRoopa Prabhu int err; 27736b9ea5a6SRoopa Prabhu 2774333c4301SDavid Ahern rt = ip6_route_info_create(cfg, extack); 27758c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 27768c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 27778c5b83f0SRoopa Prabhu rt = NULL; 27786b9ea5a6SRoopa Prabhu goto out; 27798c5b83f0SRoopa Prabhu } 27806b9ea5a6SRoopa Prabhu 27816b9ea5a6SRoopa Prabhu err = ip6_convert_metrics(&mxc, cfg); 27826b9ea5a6SRoopa Prabhu if (err) 27836b9ea5a6SRoopa Prabhu goto out; 27846b9ea5a6SRoopa Prabhu 2785333c4301SDavid Ahern err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, &mxc, extack); 27866b9ea5a6SRoopa Prabhu 27876b9ea5a6SRoopa Prabhu kfree(mxc.mx); 27886b9ea5a6SRoopa Prabhu 27896b9ea5a6SRoopa Prabhu return err; 27906b9ea5a6SRoopa Prabhu out: 2791587fea74SWei Wang if (rt) 2792587fea74SWei Wang dst_release_immediate(&rt->dst); 27936b9ea5a6SRoopa Prabhu 27941da177e4SLinus Torvalds return err; 27951da177e4SLinus Torvalds } 27961da177e4SLinus Torvalds 279786872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info) 27981da177e4SLinus Torvalds { 27991da177e4SLinus Torvalds int err; 2800c71099acSThomas Graf struct fib6_table *table; 2801d1918542SDavid S. Miller struct net *net = dev_net(rt->dst.dev); 28021da177e4SLinus Torvalds 2803a4c2fd7fSWei Wang if (rt == net->ipv6.ip6_null_entry) { 28046825a26cSGao feng err = -ENOENT; 28056825a26cSGao feng goto out; 28066825a26cSGao feng } 28076c813a72SPatrick McHardy 2808c71099acSThomas Graf table = rt->rt6i_table; 280966f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 281086872cb5SThomas Graf err = fib6_del(rt, info); 281166f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 28121da177e4SLinus Torvalds 28136825a26cSGao feng out: 281494e187c0SAmerigo Wang ip6_rt_put(rt); 28151da177e4SLinus Torvalds return err; 28161da177e4SLinus Torvalds } 28171da177e4SLinus Torvalds 2818e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt) 2819e0a1ad73SThomas Graf { 28204d1169c1SDenis V. Lunev struct nl_info info = { 2821d1918542SDavid S. Miller .nl_net = dev_net(rt->dst.dev), 28224d1169c1SDenis V. Lunev }; 2823528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 2824e0a1ad73SThomas Graf } 2825e0a1ad73SThomas Graf 28260ae81335SDavid Ahern static int __ip6_del_rt_siblings(struct rt6_info *rt, struct fib6_config *cfg) 28270ae81335SDavid Ahern { 28280ae81335SDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 2829e3330039SWANG Cong struct net *net = info->nl_net; 283016a16cd3SDavid Ahern struct sk_buff *skb = NULL; 28310ae81335SDavid Ahern struct fib6_table *table; 2832e3330039SWANG Cong int err = -ENOENT; 28330ae81335SDavid Ahern 2834e3330039SWANG Cong if (rt == net->ipv6.ip6_null_entry) 2835e3330039SWANG Cong goto out_put; 28360ae81335SDavid Ahern table = rt->rt6i_table; 283766f5d6ceSWei Wang spin_lock_bh(&table->tb6_lock); 28380ae81335SDavid Ahern 28390ae81335SDavid Ahern if (rt->rt6i_nsiblings && cfg->fc_delete_all_nh) { 28400ae81335SDavid Ahern struct rt6_info *sibling, *next_sibling; 28410ae81335SDavid Ahern 284216a16cd3SDavid Ahern /* prefer to send a single notification with all hops */ 284316a16cd3SDavid Ahern skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 284416a16cd3SDavid Ahern if (skb) { 284516a16cd3SDavid Ahern u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 284616a16cd3SDavid Ahern 2847e3330039SWANG Cong if (rt6_fill_node(net, skb, rt, 284816a16cd3SDavid Ahern NULL, NULL, 0, RTM_DELROUTE, 284916a16cd3SDavid Ahern info->portid, seq, 0) < 0) { 285016a16cd3SDavid Ahern kfree_skb(skb); 285116a16cd3SDavid Ahern skb = NULL; 285216a16cd3SDavid Ahern } else 285316a16cd3SDavid Ahern info->skip_notify = 1; 285416a16cd3SDavid Ahern } 285516a16cd3SDavid Ahern 28560ae81335SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 28570ae81335SDavid Ahern &rt->rt6i_siblings, 28580ae81335SDavid Ahern rt6i_siblings) { 28590ae81335SDavid Ahern err = fib6_del(sibling, info); 28600ae81335SDavid Ahern if (err) 2861e3330039SWANG Cong goto out_unlock; 28620ae81335SDavid Ahern } 28630ae81335SDavid Ahern } 28640ae81335SDavid Ahern 28650ae81335SDavid Ahern err = fib6_del(rt, info); 2866e3330039SWANG Cong out_unlock: 286766f5d6ceSWei Wang spin_unlock_bh(&table->tb6_lock); 2868e3330039SWANG Cong out_put: 28690ae81335SDavid Ahern ip6_rt_put(rt); 287016a16cd3SDavid Ahern 287116a16cd3SDavid Ahern if (skb) { 2872e3330039SWANG Cong rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 287316a16cd3SDavid Ahern info->nlh, gfp_any()); 287416a16cd3SDavid Ahern } 28750ae81335SDavid Ahern return err; 28760ae81335SDavid Ahern } 28770ae81335SDavid Ahern 2878333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg, 2879333c4301SDavid Ahern struct netlink_ext_ack *extack) 28801da177e4SLinus Torvalds { 28812b760fcfSWei Wang struct rt6_info *rt, *rt_cache; 2882c71099acSThomas Graf struct fib6_table *table; 28831da177e4SLinus Torvalds struct fib6_node *fn; 28841da177e4SLinus Torvalds int err = -ESRCH; 28851da177e4SLinus Torvalds 28865578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 2887d5d531cbSDavid Ahern if (!table) { 2888d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "FIB table does not exist"); 2889c71099acSThomas Graf return err; 2890d5d531cbSDavid Ahern } 28911da177e4SLinus Torvalds 289266f5d6ceSWei Wang rcu_read_lock(); 2893c71099acSThomas Graf 2894c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 289586872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 289638fbeeeeSWei Wang &cfg->fc_src, cfg->fc_src_len, 28972b760fcfSWei Wang !(cfg->fc_flags & RTF_CACHE)); 28981da177e4SLinus Torvalds 28991da177e4SLinus Torvalds if (fn) { 290066f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 29012b760fcfSWei Wang if (cfg->fc_flags & RTF_CACHE) { 29022b760fcfSWei Wang rt_cache = rt6_find_cached_rt(rt, &cfg->fc_dst, 29032b760fcfSWei Wang &cfg->fc_src); 29042b760fcfSWei Wang if (!rt_cache) 29051f56a01fSMartin KaFai Lau continue; 29062b760fcfSWei Wang rt = rt_cache; 29072b760fcfSWei Wang } 290886872cb5SThomas Graf if (cfg->fc_ifindex && 2909d1918542SDavid S. Miller (!rt->dst.dev || 2910d1918542SDavid S. Miller rt->dst.dev->ifindex != cfg->fc_ifindex)) 29111da177e4SLinus Torvalds continue; 291286872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 291386872cb5SThomas Graf !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 29141da177e4SLinus Torvalds continue; 291586872cb5SThomas Graf if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric) 29161da177e4SLinus Torvalds continue; 2917c2ed1880SMantas M if (cfg->fc_protocol && cfg->fc_protocol != rt->rt6i_protocol) 2918c2ed1880SMantas M continue; 2919d3843fe5SWei Wang if (!dst_hold_safe(&rt->dst)) 2920d3843fe5SWei Wang break; 292166f5d6ceSWei Wang rcu_read_unlock(); 29221da177e4SLinus Torvalds 29230ae81335SDavid Ahern /* if gateway was specified only delete the one hop */ 29240ae81335SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY) 292586872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 29260ae81335SDavid Ahern 29270ae81335SDavid Ahern return __ip6_del_rt_siblings(rt, cfg); 29281da177e4SLinus Torvalds } 29291da177e4SLinus Torvalds } 293066f5d6ceSWei Wang rcu_read_unlock(); 29311da177e4SLinus Torvalds 29321da177e4SLinus Torvalds return err; 29331da177e4SLinus Torvalds } 29341da177e4SLinus Torvalds 29356700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 2936a6279458SYOSHIFUJI Hideaki { 2937a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 2938e8599ff4SDavid S. Miller struct rt6_info *rt, *nrt = NULL; 2939e8599ff4SDavid S. Miller struct ndisc_options ndopts; 2940e8599ff4SDavid S. Miller struct inet6_dev *in6_dev; 2941e8599ff4SDavid S. Miller struct neighbour *neigh; 294271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 struct rd_msg *msg; 29436e157b6aSDavid S. Miller int optlen, on_link; 29446e157b6aSDavid S. Miller u8 *lladdr; 2945e8599ff4SDavid S. Miller 294629a3cad5SSimon Horman optlen = skb_tail_pointer(skb) - skb_transport_header(skb); 294771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 optlen -= sizeof(*msg); 2948e8599ff4SDavid S. Miller 2949e8599ff4SDavid S. Miller if (optlen < 0) { 29506e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); 2951e8599ff4SDavid S. Miller return; 2952e8599ff4SDavid S. Miller } 2953e8599ff4SDavid S. Miller 295471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 msg = (struct rd_msg *)icmp6_hdr(skb); 2955e8599ff4SDavid S. Miller 295671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_is_multicast(&msg->dest)) { 29576e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); 2958e8599ff4SDavid S. Miller return; 2959e8599ff4SDavid S. Miller } 2960e8599ff4SDavid S. Miller 29616e157b6aSDavid S. Miller on_link = 0; 296271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_equal(&msg->dest, &msg->target)) { 2963e8599ff4SDavid S. Miller on_link = 1; 296471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 } else if (ipv6_addr_type(&msg->target) != 2965e8599ff4SDavid S. Miller (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { 29666e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); 2967e8599ff4SDavid S. Miller return; 2968e8599ff4SDavid S. Miller } 2969e8599ff4SDavid S. Miller 2970e8599ff4SDavid S. Miller in6_dev = __in6_dev_get(skb->dev); 2971e8599ff4SDavid S. Miller if (!in6_dev) 2972e8599ff4SDavid S. Miller return; 2973e8599ff4SDavid S. Miller if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) 2974e8599ff4SDavid S. Miller return; 2975e8599ff4SDavid S. Miller 2976e8599ff4SDavid S. Miller /* RFC2461 8.1: 2977e8599ff4SDavid S. Miller * The IP source address of the Redirect MUST be the same as the current 2978e8599ff4SDavid S. Miller * first-hop router for the specified ICMP Destination Address. 2979e8599ff4SDavid S. Miller */ 2980e8599ff4SDavid S. Miller 2981f997c55cSAlexander Aring if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { 2982e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); 2983e8599ff4SDavid S. Miller return; 2984e8599ff4SDavid S. Miller } 29856e157b6aSDavid S. Miller 29866e157b6aSDavid S. Miller lladdr = NULL; 2987e8599ff4SDavid S. Miller if (ndopts.nd_opts_tgt_lladdr) { 2988e8599ff4SDavid S. Miller lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, 2989e8599ff4SDavid S. Miller skb->dev); 2990e8599ff4SDavid S. Miller if (!lladdr) { 2991e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); 2992e8599ff4SDavid S. Miller return; 2993e8599ff4SDavid S. Miller } 2994e8599ff4SDavid S. Miller } 2995e8599ff4SDavid S. Miller 29966e157b6aSDavid S. Miller rt = (struct rt6_info *) dst; 2997ec13ad1dSMatthias Schiffer if (rt->rt6i_flags & RTF_REJECT) { 29986e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); 29996e157b6aSDavid S. Miller return; 30006e157b6aSDavid S. Miller } 30016e157b6aSDavid S. Miller 30026e157b6aSDavid S. Miller /* Redirect received -> path was valid. 30036e157b6aSDavid S. Miller * Look, redirects are sent only in response to data packets, 30046e157b6aSDavid S. Miller * so that this nexthop apparently is reachable. --ANK 30056e157b6aSDavid S. Miller */ 30060dec879fSJulian Anastasov dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); 30076e157b6aSDavid S. Miller 300871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); 3009e8599ff4SDavid S. Miller if (!neigh) 3010e8599ff4SDavid S. Miller return; 3011e8599ff4SDavid S. Miller 30121da177e4SLinus Torvalds /* 30131da177e4SLinus Torvalds * We have finally decided to accept it. 30141da177e4SLinus Torvalds */ 30151da177e4SLinus Torvalds 3016f997c55cSAlexander Aring ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, 30171da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 30181da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 30191da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 3020f997c55cSAlexander Aring NEIGH_UPDATE_F_ISROUTER)), 3021f997c55cSAlexander Aring NDISC_REDIRECT, &ndopts); 30221da177e4SLinus Torvalds 302383a09abdSMartin KaFai Lau nrt = ip6_rt_cache_alloc(rt, &msg->dest, NULL); 302438308473SDavid S. Miller if (!nrt) 30251da177e4SLinus Torvalds goto out; 30261da177e4SLinus Torvalds 30271da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 30281da177e4SLinus Torvalds if (on_link) 30291da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 30301da177e4SLinus Torvalds 3031b91d5329SXin Long nrt->rt6i_protocol = RTPROT_REDIRECT; 30324e3fd7a0SAlexey Dobriyan nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 30331da177e4SLinus Torvalds 30342b760fcfSWei Wang /* No need to remove rt from the exception table if rt is 30352b760fcfSWei Wang * a cached route because rt6_insert_exception() will 30362b760fcfSWei Wang * takes care of it 30372b760fcfSWei Wang */ 30382b760fcfSWei Wang if (rt6_insert_exception(nrt, rt)) { 30392b760fcfSWei Wang dst_release_immediate(&nrt->dst); 30402b760fcfSWei Wang goto out; 30412b760fcfSWei Wang } 30421da177e4SLinus Torvalds 3043d8d1f30bSChangli Gao netevent.old = &rt->dst; 3044d8d1f30bSChangli Gao netevent.new = &nrt->dst; 304571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 netevent.daddr = &msg->dest; 304660592833SYOSHIFUJI Hideaki / 吉藤英明 netevent.neigh = neigh; 30478d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 30488d71740cSTom Tucker 30491da177e4SLinus Torvalds out: 3050e8599ff4SDavid S. Miller neigh_release(neigh); 30516e157b6aSDavid S. Miller } 30526e157b6aSDavid S. Miller 30531da177e4SLinus Torvalds /* 30541da177e4SLinus Torvalds * Misc support functions 30551da177e4SLinus Torvalds */ 30561da177e4SLinus Torvalds 30574b32b5adSMartin KaFai Lau static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from) 30584b32b5adSMartin KaFai Lau { 30593a2232e9SDavid Miller BUG_ON(from->from); 30604b32b5adSMartin KaFai Lau 30614b32b5adSMartin KaFai Lau rt->rt6i_flags &= ~RTF_EXPIRES; 30624b32b5adSMartin KaFai Lau dst_hold(&from->dst); 30633a2232e9SDavid Miller rt->from = from; 30644b32b5adSMartin KaFai Lau dst_init_metrics(&rt->dst, dst_metrics_ptr(&from->dst), true); 30654b32b5adSMartin KaFai Lau } 30664b32b5adSMartin KaFai Lau 306783a09abdSMartin KaFai Lau static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort) 30681da177e4SLinus Torvalds { 3069d8d1f30bSChangli Gao rt->dst.input = ort->dst.input; 3070d8d1f30bSChangli Gao rt->dst.output = ort->dst.output; 307183a09abdSMartin KaFai Lau rt->rt6i_dst = ort->rt6i_dst; 3072d8d1f30bSChangli Gao rt->dst.error = ort->dst.error; 30731da177e4SLinus Torvalds rt->rt6i_idev = ort->rt6i_idev; 30741da177e4SLinus Torvalds if (rt->rt6i_idev) 30751da177e4SLinus Torvalds in6_dev_hold(rt->rt6i_idev); 3076d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 30774e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 30781716a961SGao feng rt->rt6i_flags = ort->rt6i_flags; 30791716a961SGao feng rt6_set_from(rt, ort); 308083a09abdSMartin KaFai Lau rt->rt6i_metric = ort->rt6i_metric; 30811da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 308283a09abdSMartin KaFai Lau rt->rt6i_src = ort->rt6i_src; 30831da177e4SLinus Torvalds #endif 308483a09abdSMartin KaFai Lau rt->rt6i_prefsrc = ort->rt6i_prefsrc; 3085c71099acSThomas Graf rt->rt6i_table = ort->rt6i_table; 308661adedf3SJiri Benc rt->dst.lwtstate = lwtstate_get(ort->dst.lwtstate); 30871da177e4SLinus Torvalds } 30881da177e4SLinus Torvalds 308970ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 3090efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 3091b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 3092830218c1SDavid Ahern const struct in6_addr *gwaddr, 3093830218c1SDavid Ahern struct net_device *dev) 309470ceb4f5SYOSHIFUJI Hideaki { 3095830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; 3096830218c1SDavid Ahern int ifindex = dev->ifindex; 309770ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 309870ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt = NULL; 3099c71099acSThomas Graf struct fib6_table *table; 310070ceb4f5SYOSHIFUJI Hideaki 3101830218c1SDavid Ahern table = fib6_get_table(net, tb_id); 310238308473SDavid S. Miller if (!table) 3103c71099acSThomas Graf return NULL; 3104c71099acSThomas Graf 310566f5d6ceSWei Wang rcu_read_lock(); 310638fbeeeeSWei Wang fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true); 310770ceb4f5SYOSHIFUJI Hideaki if (!fn) 310870ceb4f5SYOSHIFUJI Hideaki goto out; 310970ceb4f5SYOSHIFUJI Hideaki 311066f5d6ceSWei Wang for_each_fib6_node_rt_rcu(fn) { 3111d1918542SDavid S. Miller if (rt->dst.dev->ifindex != ifindex) 311270ceb4f5SYOSHIFUJI Hideaki continue; 311370ceb4f5SYOSHIFUJI Hideaki if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) 311470ceb4f5SYOSHIFUJI Hideaki continue; 311570ceb4f5SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr)) 311670ceb4f5SYOSHIFUJI Hideaki continue; 3117d3843fe5SWei Wang ip6_hold_safe(NULL, &rt, false); 311870ceb4f5SYOSHIFUJI Hideaki break; 311970ceb4f5SYOSHIFUJI Hideaki } 312070ceb4f5SYOSHIFUJI Hideaki out: 312166f5d6ceSWei Wang rcu_read_unlock(); 312270ceb4f5SYOSHIFUJI Hideaki return rt; 312370ceb4f5SYOSHIFUJI Hideaki } 312470ceb4f5SYOSHIFUJI Hideaki 3125efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 3126b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 3127830218c1SDavid Ahern const struct in6_addr *gwaddr, 3128830218c1SDavid Ahern struct net_device *dev, 312995c96174SEric Dumazet unsigned int pref) 313070ceb4f5SYOSHIFUJI Hideaki { 313186872cb5SThomas Graf struct fib6_config cfg = { 3132238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 3133830218c1SDavid Ahern .fc_ifindex = dev->ifindex, 313486872cb5SThomas Graf .fc_dst_len = prefixlen, 313586872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 313686872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 3137b91d5329SXin Long .fc_protocol = RTPROT_RA, 313815e47304SEric W. Biederman .fc_nlinfo.portid = 0, 3139efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 3140efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 314186872cb5SThomas Graf }; 314270ceb4f5SYOSHIFUJI Hideaki 3143830218c1SDavid Ahern cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO, 31444e3fd7a0SAlexey Dobriyan cfg.fc_dst = *prefix; 31454e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 314686872cb5SThomas Graf 3147e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 3148e317da96SYOSHIFUJI Hideaki if (!prefixlen) 314986872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 315070ceb4f5SYOSHIFUJI Hideaki 3151333c4301SDavid Ahern ip6_route_add(&cfg, NULL); 315270ceb4f5SYOSHIFUJI Hideaki 3153830218c1SDavid Ahern return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); 315470ceb4f5SYOSHIFUJI Hideaki } 315570ceb4f5SYOSHIFUJI Hideaki #endif 315670ceb4f5SYOSHIFUJI Hideaki 3157b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev) 31581da177e4SLinus Torvalds { 3159830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; 31601da177e4SLinus Torvalds struct rt6_info *rt; 3161c71099acSThomas Graf struct fib6_table *table; 31621da177e4SLinus Torvalds 3163830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), tb_id); 316438308473SDavid S. Miller if (!table) 3165c71099acSThomas Graf return NULL; 31661da177e4SLinus Torvalds 316766f5d6ceSWei Wang rcu_read_lock(); 316866f5d6ceSWei Wang for_each_fib6_node_rt_rcu(&table->tb6_root) { 3169d1918542SDavid S. Miller if (dev == rt->dst.dev && 3170045927ffSYOSHIFUJI Hideaki ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 31711da177e4SLinus Torvalds ipv6_addr_equal(&rt->rt6i_gateway, addr)) 31721da177e4SLinus Torvalds break; 31731da177e4SLinus Torvalds } 31741da177e4SLinus Torvalds if (rt) 3175d3843fe5SWei Wang ip6_hold_safe(NULL, &rt, false); 317666f5d6ceSWei Wang rcu_read_unlock(); 31771da177e4SLinus Torvalds return rt; 31781da177e4SLinus Torvalds } 31791da177e4SLinus Torvalds 3180b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr, 3181ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 3182ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 31831da177e4SLinus Torvalds { 318486872cb5SThomas Graf struct fib6_config cfg = { 3185ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, 3186238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 318786872cb5SThomas Graf .fc_ifindex = dev->ifindex, 318886872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 318986872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 3190b91d5329SXin Long .fc_protocol = RTPROT_RA, 319115e47304SEric W. Biederman .fc_nlinfo.portid = 0, 31925578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 3193c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 319486872cb5SThomas Graf }; 31951da177e4SLinus Torvalds 31964e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 31971da177e4SLinus Torvalds 3198333c4301SDavid Ahern if (!ip6_route_add(&cfg, NULL)) { 3199830218c1SDavid Ahern struct fib6_table *table; 3200830218c1SDavid Ahern 3201830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), cfg.fc_table); 3202830218c1SDavid Ahern if (table) 3203830218c1SDavid Ahern table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; 3204830218c1SDavid Ahern } 32051da177e4SLinus Torvalds 32061da177e4SLinus Torvalds return rt6_get_dflt_router(gwaddr, dev); 32071da177e4SLinus Torvalds } 32081da177e4SLinus Torvalds 3209830218c1SDavid Ahern static void __rt6_purge_dflt_routers(struct fib6_table *table) 32101da177e4SLinus Torvalds { 32111da177e4SLinus Torvalds struct rt6_info *rt; 32121da177e4SLinus Torvalds 32131da177e4SLinus Torvalds restart: 321466f5d6ceSWei Wang rcu_read_lock(); 321566f5d6ceSWei Wang for_each_fib6_node_rt_rcu(&table->tb6_root) { 32163e8b0ac3SLorenzo Colitti if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) && 32173e8b0ac3SLorenzo Colitti (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) { 3218d3843fe5SWei Wang if (dst_hold_safe(&rt->dst)) { 321966f5d6ceSWei Wang rcu_read_unlock(); 3220e0a1ad73SThomas Graf ip6_del_rt(rt); 3221d3843fe5SWei Wang } else { 322266f5d6ceSWei Wang rcu_read_unlock(); 3223d3843fe5SWei Wang } 32241da177e4SLinus Torvalds goto restart; 32251da177e4SLinus Torvalds } 32261da177e4SLinus Torvalds } 322766f5d6ceSWei Wang rcu_read_unlock(); 3228830218c1SDavid Ahern 3229830218c1SDavid Ahern table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; 3230830218c1SDavid Ahern } 3231830218c1SDavid Ahern 3232830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net) 3233830218c1SDavid Ahern { 3234830218c1SDavid Ahern struct fib6_table *table; 3235830218c1SDavid Ahern struct hlist_head *head; 3236830218c1SDavid Ahern unsigned int h; 3237830218c1SDavid Ahern 3238830218c1SDavid Ahern rcu_read_lock(); 3239830218c1SDavid Ahern 3240830218c1SDavid Ahern for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { 3241830218c1SDavid Ahern head = &net->ipv6.fib_table_hash[h]; 3242830218c1SDavid Ahern hlist_for_each_entry_rcu(table, head, tb6_hlist) { 3243830218c1SDavid Ahern if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) 3244830218c1SDavid Ahern __rt6_purge_dflt_routers(table); 3245830218c1SDavid Ahern } 3246830218c1SDavid Ahern } 3247830218c1SDavid Ahern 3248830218c1SDavid Ahern rcu_read_unlock(); 32491da177e4SLinus Torvalds } 32501da177e4SLinus Torvalds 32515578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 32525578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 325386872cb5SThomas Graf struct fib6_config *cfg) 325486872cb5SThomas Graf { 325586872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 325686872cb5SThomas Graf 3257ca254490SDavid Ahern cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? 3258ca254490SDavid Ahern : RT6_TABLE_MAIN; 325986872cb5SThomas Graf cfg->fc_ifindex = rtmsg->rtmsg_ifindex; 326086872cb5SThomas Graf cfg->fc_metric = rtmsg->rtmsg_metric; 326186872cb5SThomas Graf cfg->fc_expires = rtmsg->rtmsg_info; 326286872cb5SThomas Graf cfg->fc_dst_len = rtmsg->rtmsg_dst_len; 326386872cb5SThomas Graf cfg->fc_src_len = rtmsg->rtmsg_src_len; 326486872cb5SThomas Graf cfg->fc_flags = rtmsg->rtmsg_flags; 326586872cb5SThomas Graf 32665578689aSDaniel Lezcano cfg->fc_nlinfo.nl_net = net; 3267f1243c2dSBenjamin Thery 32684e3fd7a0SAlexey Dobriyan cfg->fc_dst = rtmsg->rtmsg_dst; 32694e3fd7a0SAlexey Dobriyan cfg->fc_src = rtmsg->rtmsg_src; 32704e3fd7a0SAlexey Dobriyan cfg->fc_gateway = rtmsg->rtmsg_gateway; 327186872cb5SThomas Graf } 327286872cb5SThomas Graf 32735578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) 32741da177e4SLinus Torvalds { 327586872cb5SThomas Graf struct fib6_config cfg; 32761da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 32771da177e4SLinus Torvalds int err; 32781da177e4SLinus Torvalds 32791da177e4SLinus Torvalds switch (cmd) { 32801da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 32811da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 3282af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 32831da177e4SLinus Torvalds return -EPERM; 32841da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 32851da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 32861da177e4SLinus Torvalds if (err) 32871da177e4SLinus Torvalds return -EFAULT; 32881da177e4SLinus Torvalds 32895578689aSDaniel Lezcano rtmsg_to_fib6_config(net, &rtmsg, &cfg); 329086872cb5SThomas Graf 32911da177e4SLinus Torvalds rtnl_lock(); 32921da177e4SLinus Torvalds switch (cmd) { 32931da177e4SLinus Torvalds case SIOCADDRT: 3294333c4301SDavid Ahern err = ip6_route_add(&cfg, NULL); 32951da177e4SLinus Torvalds break; 32961da177e4SLinus Torvalds case SIOCDELRT: 3297333c4301SDavid Ahern err = ip6_route_del(&cfg, NULL); 32981da177e4SLinus Torvalds break; 32991da177e4SLinus Torvalds default: 33001da177e4SLinus Torvalds err = -EINVAL; 33011da177e4SLinus Torvalds } 33021da177e4SLinus Torvalds rtnl_unlock(); 33031da177e4SLinus Torvalds 33041da177e4SLinus Torvalds return err; 33053ff50b79SStephen Hemminger } 33061da177e4SLinus Torvalds 33071da177e4SLinus Torvalds return -EINVAL; 33081da177e4SLinus Torvalds } 33091da177e4SLinus Torvalds 33101da177e4SLinus Torvalds /* 33111da177e4SLinus Torvalds * Drop the packet on the floor 33121da177e4SLinus Torvalds */ 33131da177e4SLinus Torvalds 3314d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 33151da177e4SLinus Torvalds { 3316612f09e8SYOSHIFUJI Hideaki int type; 3317adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 3318612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 3319612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 33200660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 332145bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 33223bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 33233bd653c8SDenis V. Lunev IPSTATS_MIB_INADDRERRORS); 3324612f09e8SYOSHIFUJI Hideaki break; 3325612f09e8SYOSHIFUJI Hideaki } 3326612f09e8SYOSHIFUJI Hideaki /* FALLTHROUGH */ 3327612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 33283bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 33293bd653c8SDenis V. Lunev ipstats_mib_noroutes); 3330612f09e8SYOSHIFUJI Hideaki break; 3331612f09e8SYOSHIFUJI Hideaki } 33323ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 33331da177e4SLinus Torvalds kfree_skb(skb); 33341da177e4SLinus Torvalds return 0; 33351da177e4SLinus Torvalds } 33361da177e4SLinus Torvalds 33379ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 33389ce8ade0SThomas Graf { 3339612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 33409ce8ade0SThomas Graf } 33419ce8ade0SThomas Graf 3342ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb) 33431da177e4SLinus Torvalds { 3344adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 3345612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 33461da177e4SLinus Torvalds } 33471da177e4SLinus Torvalds 33489ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 33499ce8ade0SThomas Graf { 3350612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 33519ce8ade0SThomas Graf } 33529ce8ade0SThomas Graf 3353ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb) 33549ce8ade0SThomas Graf { 3355adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 3356612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 33579ce8ade0SThomas Graf } 33589ce8ade0SThomas Graf 33591da177e4SLinus Torvalds /* 33601da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 33611da177e4SLinus Torvalds */ 33621da177e4SLinus Torvalds 33631da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, 33641da177e4SLinus Torvalds const struct in6_addr *addr, 33658f031519SDavid S. Miller bool anycast) 33661da177e4SLinus Torvalds { 3367ca254490SDavid Ahern u32 tb_id; 3368c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 33694832c30dSDavid Ahern struct net_device *dev = idev->dev; 33705f02ce24SDavid Ahern struct rt6_info *rt; 33715f02ce24SDavid Ahern 33725f02ce24SDavid Ahern rt = ip6_dst_alloc(net, dev, DST_NOCOUNT); 3373a3300ef4SHannes Frederic Sowa if (!rt) 33741da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 33751da177e4SLinus Torvalds 33761da177e4SLinus Torvalds in6_dev_hold(idev); 33771da177e4SLinus Torvalds 337811d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 3379d8d1f30bSChangli Gao rt->dst.input = ip6_input; 3380d8d1f30bSChangli Gao rt->dst.output = ip6_output; 33811da177e4SLinus Torvalds rt->rt6i_idev = idev; 33821da177e4SLinus Torvalds 338394b5e0f9SDavid Ahern rt->rt6i_protocol = RTPROT_KERNEL; 33841da177e4SLinus Torvalds rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP; 338558c4fb86SYOSHIFUJI Hideaki if (anycast) 338658c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 338758c4fb86SYOSHIFUJI Hideaki else 33881da177e4SLinus Torvalds rt->rt6i_flags |= RTF_LOCAL; 33891da177e4SLinus Torvalds 3390550bab42SJulian Anastasov rt->rt6i_gateway = *addr; 33914e3fd7a0SAlexey Dobriyan rt->rt6i_dst.addr = *addr; 33921da177e4SLinus Torvalds rt->rt6i_dst.plen = 128; 3393ca254490SDavid Ahern tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL; 3394ca254490SDavid Ahern rt->rt6i_table = fib6_get_table(net, tb_id); 33951da177e4SLinus Torvalds 33961da177e4SLinus Torvalds return rt; 33971da177e4SLinus Torvalds } 33981da177e4SLinus Torvalds 3399c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 3400c3968a85SDaniel Walter struct arg_dev_net_ip { 3401c3968a85SDaniel Walter struct net_device *dev; 3402c3968a85SDaniel Walter struct net *net; 3403c3968a85SDaniel Walter struct in6_addr *addr; 3404c3968a85SDaniel Walter }; 3405c3968a85SDaniel Walter 3406c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg) 3407c3968a85SDaniel Walter { 3408c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 3409c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 3410c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 3411c3968a85SDaniel Walter 3412d1918542SDavid S. Miller if (((void *)rt->dst.dev == dev || !dev) && 3413c3968a85SDaniel Walter rt != net->ipv6.ip6_null_entry && 3414c3968a85SDaniel Walter ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) { 341560006a48SWei Wang spin_lock_bh(&rt6_exception_lock); 3416c3968a85SDaniel Walter /* remove prefsrc entry */ 3417c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 341860006a48SWei Wang /* need to update cache as well */ 341960006a48SWei Wang rt6_exceptions_remove_prefsrc(rt); 342060006a48SWei Wang spin_unlock_bh(&rt6_exception_lock); 3421c3968a85SDaniel Walter } 3422c3968a85SDaniel Walter return 0; 3423c3968a85SDaniel Walter } 3424c3968a85SDaniel Walter 3425c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 3426c3968a85SDaniel Walter { 3427c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 3428c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 3429c3968a85SDaniel Walter .dev = ifp->idev->dev, 3430c3968a85SDaniel Walter .net = net, 3431c3968a85SDaniel Walter .addr = &ifp->addr, 3432c3968a85SDaniel Walter }; 34330c3584d5SLi RongQing fib6_clean_all(net, fib6_remove_prefsrc, &adni); 3434c3968a85SDaniel Walter } 3435c3968a85SDaniel Walter 3436be7a010dSDuan Jiong #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY) 3437be7a010dSDuan Jiong 3438be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */ 3439be7a010dSDuan Jiong static int fib6_clean_tohost(struct rt6_info *rt, void *arg) 3440be7a010dSDuan Jiong { 3441be7a010dSDuan Jiong struct in6_addr *gateway = (struct in6_addr *)arg; 3442be7a010dSDuan Jiong 34432b760fcfSWei Wang if (((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) && 3444be7a010dSDuan Jiong ipv6_addr_equal(gateway, &rt->rt6i_gateway)) { 3445be7a010dSDuan Jiong return -1; 3446be7a010dSDuan Jiong } 3447b16cb459SWei Wang 3448b16cb459SWei Wang /* Further clean up cached routes in exception table. 3449b16cb459SWei Wang * This is needed because cached route may have a different 3450b16cb459SWei Wang * gateway than its 'parent' in the case of an ip redirect. 3451b16cb459SWei Wang */ 3452b16cb459SWei Wang rt6_exceptions_clean_tohost(rt, gateway); 3453b16cb459SWei Wang 3454be7a010dSDuan Jiong return 0; 3455be7a010dSDuan Jiong } 3456be7a010dSDuan Jiong 3457be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) 3458be7a010dSDuan Jiong { 3459be7a010dSDuan Jiong fib6_clean_all(net, fib6_clean_tohost, gateway); 3460be7a010dSDuan Jiong } 3461be7a010dSDuan Jiong 34628ed67789SDaniel Lezcano struct arg_dev_net { 34638ed67789SDaniel Lezcano struct net_device *dev; 34648ed67789SDaniel Lezcano struct net *net; 34658ed67789SDaniel Lezcano }; 34668ed67789SDaniel Lezcano 3467a1a22c12SDavid Ahern /* called with write lock held for table with rt */ 34681da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg) 34691da177e4SLinus Torvalds { 3470bc3ef660Sstephen hemminger const struct arg_dev_net *adn = arg; 3471bc3ef660Sstephen hemminger const struct net_device *dev = adn->dev; 34728ed67789SDaniel Lezcano 3473d1918542SDavid S. Miller if ((rt->dst.dev == dev || !dev) && 3474a1a22c12SDavid Ahern rt != adn->net->ipv6.ip6_null_entry && 3475a1a22c12SDavid Ahern (rt->rt6i_nsiblings == 0 || 34768397ed36SDavid Ahern (dev && netdev_unregistering(dev)) || 3477a1a22c12SDavid Ahern !rt->rt6i_idev->cnf.ignore_routes_with_linkdown)) 34781da177e4SLinus Torvalds return -1; 3479c159d30cSDavid S. Miller 34801da177e4SLinus Torvalds return 0; 34811da177e4SLinus Torvalds } 34821da177e4SLinus Torvalds 3483f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev) 34841da177e4SLinus Torvalds { 34858ed67789SDaniel Lezcano struct arg_dev_net adn = { 34868ed67789SDaniel Lezcano .dev = dev, 34878ed67789SDaniel Lezcano .net = net, 34888ed67789SDaniel Lezcano }; 34898ed67789SDaniel Lezcano 34900c3584d5SLi RongQing fib6_clean_all(net, fib6_ifdown, &adn); 3491e332bc67SEric W. Biederman if (dev) 34928d0b94afSMartin KaFai Lau rt6_uncached_list_flush_dev(net, dev); 34931da177e4SLinus Torvalds } 34941da177e4SLinus Torvalds 349595c96174SEric Dumazet struct rt6_mtu_change_arg { 34961da177e4SLinus Torvalds struct net_device *dev; 349795c96174SEric Dumazet unsigned int mtu; 34981da177e4SLinus Torvalds }; 34991da177e4SLinus Torvalds 35001da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg) 35011da177e4SLinus Torvalds { 35021da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 35031da177e4SLinus Torvalds struct inet6_dev *idev; 35041da177e4SLinus Torvalds 35051da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 35061da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 35071da177e4SLinus Torvalds We still use this lock to block changes 35081da177e4SLinus Torvalds caused by addrconf/ndisc. 35091da177e4SLinus Torvalds */ 35101da177e4SLinus Torvalds 35111da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 351238308473SDavid S. Miller if (!idev) 35131da177e4SLinus Torvalds return 0; 35141da177e4SLinus Torvalds 35151da177e4SLinus Torvalds /* For administrative MTU increase, there is no way to discover 35161da177e4SLinus Torvalds IPv6 PMTU increase, so PMTU increase should be updated here. 35171da177e4SLinus Torvalds Since RFC 1981 doesn't include administrative MTU increase 35181da177e4SLinus Torvalds update PMTU increase is a MUST. (i.e. jumbo frame) 35191da177e4SLinus Torvalds */ 35201da177e4SLinus Torvalds /* 35211da177e4SLinus Torvalds If new MTU is less than route PMTU, this new MTU will be the 35221da177e4SLinus Torvalds lowest MTU in the path, update the route PMTU to reflect PMTU 35231da177e4SLinus Torvalds decreases; if new MTU is greater than route PMTU, and the 35241da177e4SLinus Torvalds old MTU is the lowest MTU in the path, update the route PMTU 35251da177e4SLinus Torvalds to reflect the increase. In this case if the other nodes' MTU 35261da177e4SLinus Torvalds also have the lowest MTU, TOO BIG MESSAGE will be lead to 352767c408cfSAlexander Alemayhu PMTU discovery. 35281da177e4SLinus Torvalds */ 3529d1918542SDavid S. Miller if (rt->dst.dev == arg->dev && 3530fb56be83SMaciej Żenczykowski dst_metric_raw(&rt->dst, RTAX_MTU) && 35314b32b5adSMartin KaFai Lau !dst_metric_locked(&rt->dst, RTAX_MTU)) { 3532f5bbe7eeSWei Wang spin_lock_bh(&rt6_exception_lock); 35332b760fcfSWei Wang if (dst_mtu(&rt->dst) >= arg->mtu || 3534d8d1f30bSChangli Gao (dst_mtu(&rt->dst) < arg->mtu && 35354b32b5adSMartin KaFai Lau dst_mtu(&rt->dst) == idev->cnf.mtu6)) { 3536defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu); 3537566cfd8fSSimon Arlott } 3538f5bbe7eeSWei Wang rt6_exceptions_update_pmtu(rt, arg->mtu); 3539f5bbe7eeSWei Wang spin_unlock_bh(&rt6_exception_lock); 35404b32b5adSMartin KaFai Lau } 35411da177e4SLinus Torvalds return 0; 35421da177e4SLinus Torvalds } 35431da177e4SLinus Torvalds 354495c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu) 35451da177e4SLinus Torvalds { 3546c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 3547c71099acSThomas Graf .dev = dev, 3548c71099acSThomas Graf .mtu = mtu, 3549c71099acSThomas Graf }; 35501da177e4SLinus Torvalds 35510c3584d5SLi RongQing fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); 35521da177e4SLinus Torvalds } 35531da177e4SLinus Torvalds 3554ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 35555176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 355686872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 3557ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 355886872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 355986872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 356051ebd318SNicolas Dichtel [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 3561c78ba6d6SLubomir Rintel [RTA_PREF] = { .type = NLA_U8 }, 356219e42e45SRoopa Prabhu [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, 356319e42e45SRoopa Prabhu [RTA_ENCAP] = { .type = NLA_NESTED }, 356432bc201eSXin Long [RTA_EXPIRES] = { .type = NLA_U32 }, 3565622ec2c9SLorenzo Colitti [RTA_UID] = { .type = NLA_U32 }, 35663b45a410SLiping Zhang [RTA_MARK] = { .type = NLA_U32 }, 356786872cb5SThomas Graf }; 356886872cb5SThomas Graf 356986872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 3570333c4301SDavid Ahern struct fib6_config *cfg, 3571333c4301SDavid Ahern struct netlink_ext_ack *extack) 35721da177e4SLinus Torvalds { 357386872cb5SThomas Graf struct rtmsg *rtm; 357486872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 3575c78ba6d6SLubomir Rintel unsigned int pref; 357686872cb5SThomas Graf int err; 35771da177e4SLinus Torvalds 3578fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 3579fceb6435SJohannes Berg NULL); 358086872cb5SThomas Graf if (err < 0) 358186872cb5SThomas Graf goto errout; 35821da177e4SLinus Torvalds 358386872cb5SThomas Graf err = -EINVAL; 358486872cb5SThomas Graf rtm = nlmsg_data(nlh); 358586872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 358686872cb5SThomas Graf 358786872cb5SThomas Graf cfg->fc_table = rtm->rtm_table; 358886872cb5SThomas Graf cfg->fc_dst_len = rtm->rtm_dst_len; 358986872cb5SThomas Graf cfg->fc_src_len = rtm->rtm_src_len; 359086872cb5SThomas Graf cfg->fc_flags = RTF_UP; 359186872cb5SThomas Graf cfg->fc_protocol = rtm->rtm_protocol; 3592ef2c7d7bSNicolas Dichtel cfg->fc_type = rtm->rtm_type; 359386872cb5SThomas Graf 3594ef2c7d7bSNicolas Dichtel if (rtm->rtm_type == RTN_UNREACHABLE || 3595ef2c7d7bSNicolas Dichtel rtm->rtm_type == RTN_BLACKHOLE || 3596b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_PROHIBIT || 3597b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_THROW) 359886872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 359986872cb5SThomas Graf 3600ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 3601ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 3602ab79ad14SMaciej Żenczykowski 36031f56a01fSMartin KaFai Lau if (rtm->rtm_flags & RTM_F_CLONED) 36041f56a01fSMartin KaFai Lau cfg->fc_flags |= RTF_CACHE; 36051f56a01fSMartin KaFai Lau 360615e47304SEric W. Biederman cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid; 360786872cb5SThomas Graf cfg->fc_nlinfo.nlh = nlh; 36083b1e0a65SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = sock_net(skb->sk); 360986872cb5SThomas Graf 361086872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 361167b61f6cSJiri Benc cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); 361286872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 36131da177e4SLinus Torvalds } 361486872cb5SThomas Graf 361586872cb5SThomas Graf if (tb[RTA_DST]) { 361686872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 361786872cb5SThomas Graf 361886872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 361986872cb5SThomas Graf goto errout; 362086872cb5SThomas Graf 362186872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 36221da177e4SLinus Torvalds } 362386872cb5SThomas Graf 362486872cb5SThomas Graf if (tb[RTA_SRC]) { 362586872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 362686872cb5SThomas Graf 362786872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 362886872cb5SThomas Graf goto errout; 362986872cb5SThomas Graf 363086872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 36311da177e4SLinus Torvalds } 363286872cb5SThomas Graf 3633c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 363467b61f6cSJiri Benc cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); 3635c3968a85SDaniel Walter 363686872cb5SThomas Graf if (tb[RTA_OIF]) 363786872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 363886872cb5SThomas Graf 363986872cb5SThomas Graf if (tb[RTA_PRIORITY]) 364086872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 364186872cb5SThomas Graf 364286872cb5SThomas Graf if (tb[RTA_METRICS]) { 364386872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 364486872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 36451da177e4SLinus Torvalds } 364686872cb5SThomas Graf 364786872cb5SThomas Graf if (tb[RTA_TABLE]) 364886872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 364986872cb5SThomas Graf 365051ebd318SNicolas Dichtel if (tb[RTA_MULTIPATH]) { 365151ebd318SNicolas Dichtel cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); 365251ebd318SNicolas Dichtel cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); 36539ed59592SDavid Ahern 36549ed59592SDavid Ahern err = lwtunnel_valid_encap_type_attr(cfg->fc_mp, 3655c255bd68SDavid Ahern cfg->fc_mp_len, extack); 36569ed59592SDavid Ahern if (err < 0) 36579ed59592SDavid Ahern goto errout; 365851ebd318SNicolas Dichtel } 365951ebd318SNicolas Dichtel 3660c78ba6d6SLubomir Rintel if (tb[RTA_PREF]) { 3661c78ba6d6SLubomir Rintel pref = nla_get_u8(tb[RTA_PREF]); 3662c78ba6d6SLubomir Rintel if (pref != ICMPV6_ROUTER_PREF_LOW && 3663c78ba6d6SLubomir Rintel pref != ICMPV6_ROUTER_PREF_HIGH) 3664c78ba6d6SLubomir Rintel pref = ICMPV6_ROUTER_PREF_MEDIUM; 3665c78ba6d6SLubomir Rintel cfg->fc_flags |= RTF_PREF(pref); 3666c78ba6d6SLubomir Rintel } 3667c78ba6d6SLubomir Rintel 366819e42e45SRoopa Prabhu if (tb[RTA_ENCAP]) 366919e42e45SRoopa Prabhu cfg->fc_encap = tb[RTA_ENCAP]; 367019e42e45SRoopa Prabhu 36719ed59592SDavid Ahern if (tb[RTA_ENCAP_TYPE]) { 367219e42e45SRoopa Prabhu cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); 367319e42e45SRoopa Prabhu 3674c255bd68SDavid Ahern err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); 36759ed59592SDavid Ahern if (err < 0) 36769ed59592SDavid Ahern goto errout; 36779ed59592SDavid Ahern } 36789ed59592SDavid Ahern 367932bc201eSXin Long if (tb[RTA_EXPIRES]) { 368032bc201eSXin Long unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); 368132bc201eSXin Long 368232bc201eSXin Long if (addrconf_finite_timeout(timeout)) { 368332bc201eSXin Long cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); 368432bc201eSXin Long cfg->fc_flags |= RTF_EXPIRES; 368532bc201eSXin Long } 368632bc201eSXin Long } 368732bc201eSXin Long 368886872cb5SThomas Graf err = 0; 368986872cb5SThomas Graf errout: 369086872cb5SThomas Graf return err; 36911da177e4SLinus Torvalds } 36921da177e4SLinus Torvalds 36936b9ea5a6SRoopa Prabhu struct rt6_nh { 36946b9ea5a6SRoopa Prabhu struct rt6_info *rt6_info; 36956b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 36966b9ea5a6SRoopa Prabhu struct mx6_config mxc; 36976b9ea5a6SRoopa Prabhu struct list_head next; 36986b9ea5a6SRoopa Prabhu }; 36996b9ea5a6SRoopa Prabhu 37006b9ea5a6SRoopa Prabhu static void ip6_print_replace_route_err(struct list_head *rt6_nh_list) 37016b9ea5a6SRoopa Prabhu { 37026b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 37036b9ea5a6SRoopa Prabhu 37046b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 37057d4d5065SDavid Ahern pr_warn("IPV6: multipath route replace failed (check consistency of installed routes): %pI6c nexthop %pI6c ifi %d\n", 37066b9ea5a6SRoopa Prabhu &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway, 37076b9ea5a6SRoopa Prabhu nh->r_cfg.fc_ifindex); 37086b9ea5a6SRoopa Prabhu } 37096b9ea5a6SRoopa Prabhu } 37106b9ea5a6SRoopa Prabhu 37116b9ea5a6SRoopa Prabhu static int ip6_route_info_append(struct list_head *rt6_nh_list, 37126b9ea5a6SRoopa Prabhu struct rt6_info *rt, struct fib6_config *r_cfg) 37136b9ea5a6SRoopa Prabhu { 37146b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 37156b9ea5a6SRoopa Prabhu int err = -EEXIST; 37166b9ea5a6SRoopa Prabhu 37176b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 37186b9ea5a6SRoopa Prabhu /* check if rt6_info already exists */ 3719f06b7549SDavid Ahern if (rt6_duplicate_nexthop(nh->rt6_info, rt)) 37206b9ea5a6SRoopa Prabhu return err; 37216b9ea5a6SRoopa Prabhu } 37226b9ea5a6SRoopa Prabhu 37236b9ea5a6SRoopa Prabhu nh = kzalloc(sizeof(*nh), GFP_KERNEL); 37246b9ea5a6SRoopa Prabhu if (!nh) 37256b9ea5a6SRoopa Prabhu return -ENOMEM; 37266b9ea5a6SRoopa Prabhu nh->rt6_info = rt; 37276b9ea5a6SRoopa Prabhu err = ip6_convert_metrics(&nh->mxc, r_cfg); 37286b9ea5a6SRoopa Prabhu if (err) { 37296b9ea5a6SRoopa Prabhu kfree(nh); 37306b9ea5a6SRoopa Prabhu return err; 37316b9ea5a6SRoopa Prabhu } 37326b9ea5a6SRoopa Prabhu memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); 37336b9ea5a6SRoopa Prabhu list_add_tail(&nh->next, rt6_nh_list); 37346b9ea5a6SRoopa Prabhu 37356b9ea5a6SRoopa Prabhu return 0; 37366b9ea5a6SRoopa Prabhu } 37376b9ea5a6SRoopa Prabhu 37383b1137feSDavid Ahern static void ip6_route_mpath_notify(struct rt6_info *rt, 37393b1137feSDavid Ahern struct rt6_info *rt_last, 37403b1137feSDavid Ahern struct nl_info *info, 37413b1137feSDavid Ahern __u16 nlflags) 37423b1137feSDavid Ahern { 37433b1137feSDavid Ahern /* if this is an APPEND route, then rt points to the first route 37443b1137feSDavid Ahern * inserted and rt_last points to last route inserted. Userspace 37453b1137feSDavid Ahern * wants a consistent dump of the route which starts at the first 37463b1137feSDavid Ahern * nexthop. Since sibling routes are always added at the end of 37473b1137feSDavid Ahern * the list, find the first sibling of the last route appended 37483b1137feSDavid Ahern */ 37493b1137feSDavid Ahern if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->rt6i_nsiblings) { 37503b1137feSDavid Ahern rt = list_first_entry(&rt_last->rt6i_siblings, 37513b1137feSDavid Ahern struct rt6_info, 37523b1137feSDavid Ahern rt6i_siblings); 37533b1137feSDavid Ahern } 37543b1137feSDavid Ahern 37553b1137feSDavid Ahern if (rt) 37563b1137feSDavid Ahern inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); 37573b1137feSDavid Ahern } 37583b1137feSDavid Ahern 3759333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg, 3760333c4301SDavid Ahern struct netlink_ext_ack *extack) 376151ebd318SNicolas Dichtel { 37623b1137feSDavid Ahern struct rt6_info *rt_notif = NULL, *rt_last = NULL; 37633b1137feSDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 376451ebd318SNicolas Dichtel struct fib6_config r_cfg; 376551ebd318SNicolas Dichtel struct rtnexthop *rtnh; 37666b9ea5a6SRoopa Prabhu struct rt6_info *rt; 37676b9ea5a6SRoopa Prabhu struct rt6_nh *err_nh; 37686b9ea5a6SRoopa Prabhu struct rt6_nh *nh, *nh_safe; 37693b1137feSDavid Ahern __u16 nlflags; 377051ebd318SNicolas Dichtel int remaining; 377151ebd318SNicolas Dichtel int attrlen; 37726b9ea5a6SRoopa Prabhu int err = 1; 37736b9ea5a6SRoopa Prabhu int nhn = 0; 37746b9ea5a6SRoopa Prabhu int replace = (cfg->fc_nlinfo.nlh && 37756b9ea5a6SRoopa Prabhu (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); 37766b9ea5a6SRoopa Prabhu LIST_HEAD(rt6_nh_list); 377751ebd318SNicolas Dichtel 37783b1137feSDavid Ahern nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; 37793b1137feSDavid Ahern if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) 37803b1137feSDavid Ahern nlflags |= NLM_F_APPEND; 37813b1137feSDavid Ahern 378235f1b4e9SMichal Kubeček remaining = cfg->fc_mp_len; 378351ebd318SNicolas Dichtel rtnh = (struct rtnexthop *)cfg->fc_mp; 378451ebd318SNicolas Dichtel 37856b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry and build a list (rt6_nh_list) of 37866b9ea5a6SRoopa Prabhu * rt6_info structs per nexthop 37876b9ea5a6SRoopa Prabhu */ 378851ebd318SNicolas Dichtel while (rtnh_ok(rtnh, remaining)) { 378951ebd318SNicolas Dichtel memcpy(&r_cfg, cfg, sizeof(*cfg)); 379051ebd318SNicolas Dichtel if (rtnh->rtnh_ifindex) 379151ebd318SNicolas Dichtel r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 379251ebd318SNicolas Dichtel 379351ebd318SNicolas Dichtel attrlen = rtnh_attrlen(rtnh); 379451ebd318SNicolas Dichtel if (attrlen > 0) { 379551ebd318SNicolas Dichtel struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 379651ebd318SNicolas Dichtel 379751ebd318SNicolas Dichtel nla = nla_find(attrs, attrlen, RTA_GATEWAY); 379851ebd318SNicolas Dichtel if (nla) { 379967b61f6cSJiri Benc r_cfg.fc_gateway = nla_get_in6_addr(nla); 380051ebd318SNicolas Dichtel r_cfg.fc_flags |= RTF_GATEWAY; 380151ebd318SNicolas Dichtel } 380219e42e45SRoopa Prabhu r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); 380319e42e45SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); 380419e42e45SRoopa Prabhu if (nla) 380519e42e45SRoopa Prabhu r_cfg.fc_encap_type = nla_get_u16(nla); 380651ebd318SNicolas Dichtel } 38076b9ea5a6SRoopa Prabhu 3808333c4301SDavid Ahern rt = ip6_route_info_create(&r_cfg, extack); 38098c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 38108c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 38118c5b83f0SRoopa Prabhu rt = NULL; 38126b9ea5a6SRoopa Prabhu goto cleanup; 38138c5b83f0SRoopa Prabhu } 38146b9ea5a6SRoopa Prabhu 38156b9ea5a6SRoopa Prabhu err = ip6_route_info_append(&rt6_nh_list, rt, &r_cfg); 381651ebd318SNicolas Dichtel if (err) { 3817587fea74SWei Wang dst_release_immediate(&rt->dst); 38186b9ea5a6SRoopa Prabhu goto cleanup; 381951ebd318SNicolas Dichtel } 38206b9ea5a6SRoopa Prabhu 38216b9ea5a6SRoopa Prabhu rtnh = rtnh_next(rtnh, &remaining); 382251ebd318SNicolas Dichtel } 38236b9ea5a6SRoopa Prabhu 38243b1137feSDavid Ahern /* for add and replace send one notification with all nexthops. 38253b1137feSDavid Ahern * Skip the notification in fib6_add_rt2node and send one with 38263b1137feSDavid Ahern * the full route when done 38273b1137feSDavid Ahern */ 38283b1137feSDavid Ahern info->skip_notify = 1; 38293b1137feSDavid Ahern 38306b9ea5a6SRoopa Prabhu err_nh = NULL; 38316b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 38323b1137feSDavid Ahern rt_last = nh->rt6_info; 3833333c4301SDavid Ahern err = __ip6_ins_rt(nh->rt6_info, info, &nh->mxc, extack); 38343b1137feSDavid Ahern /* save reference to first route for notification */ 38353b1137feSDavid Ahern if (!rt_notif && !err) 38363b1137feSDavid Ahern rt_notif = nh->rt6_info; 38373b1137feSDavid Ahern 38386b9ea5a6SRoopa Prabhu /* nh->rt6_info is used or freed at this point, reset to NULL*/ 38396b9ea5a6SRoopa Prabhu nh->rt6_info = NULL; 38406b9ea5a6SRoopa Prabhu if (err) { 38416b9ea5a6SRoopa Prabhu if (replace && nhn) 38426b9ea5a6SRoopa Prabhu ip6_print_replace_route_err(&rt6_nh_list); 38436b9ea5a6SRoopa Prabhu err_nh = nh; 38446b9ea5a6SRoopa Prabhu goto add_errout; 38456b9ea5a6SRoopa Prabhu } 38466b9ea5a6SRoopa Prabhu 38471a72418bSNicolas Dichtel /* Because each route is added like a single route we remove 384827596472SMichal Kubeček * these flags after the first nexthop: if there is a collision, 384927596472SMichal Kubeček * we have already failed to add the first nexthop: 385027596472SMichal Kubeček * fib6_add_rt2node() has rejected it; when replacing, old 385127596472SMichal Kubeček * nexthops have been replaced by first new, the rest should 385227596472SMichal Kubeček * be added to it. 38531a72418bSNicolas Dichtel */ 385427596472SMichal Kubeček cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | 385527596472SMichal Kubeček NLM_F_REPLACE); 38566b9ea5a6SRoopa Prabhu nhn++; 38576b9ea5a6SRoopa Prabhu } 38586b9ea5a6SRoopa Prabhu 38593b1137feSDavid Ahern /* success ... tell user about new route */ 38603b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 38616b9ea5a6SRoopa Prabhu goto cleanup; 38626b9ea5a6SRoopa Prabhu 38636b9ea5a6SRoopa Prabhu add_errout: 38643b1137feSDavid Ahern /* send notification for routes that were added so that 38653b1137feSDavid Ahern * the delete notifications sent by ip6_route_del are 38663b1137feSDavid Ahern * coherent 38673b1137feSDavid Ahern */ 38683b1137feSDavid Ahern if (rt_notif) 38693b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 38703b1137feSDavid Ahern 38716b9ea5a6SRoopa Prabhu /* Delete routes that were already added */ 38726b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 38736b9ea5a6SRoopa Prabhu if (err_nh == nh) 38746b9ea5a6SRoopa Prabhu break; 3875333c4301SDavid Ahern ip6_route_del(&nh->r_cfg, extack); 38766b9ea5a6SRoopa Prabhu } 38776b9ea5a6SRoopa Prabhu 38786b9ea5a6SRoopa Prabhu cleanup: 38796b9ea5a6SRoopa Prabhu list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) { 3880587fea74SWei Wang if (nh->rt6_info) 3881587fea74SWei Wang dst_release_immediate(&nh->rt6_info->dst); 38826b9ea5a6SRoopa Prabhu kfree(nh->mxc.mx); 38836b9ea5a6SRoopa Prabhu list_del(&nh->next); 38846b9ea5a6SRoopa Prabhu kfree(nh); 38856b9ea5a6SRoopa Prabhu } 38866b9ea5a6SRoopa Prabhu 38876b9ea5a6SRoopa Prabhu return err; 38886b9ea5a6SRoopa Prabhu } 38896b9ea5a6SRoopa Prabhu 3890333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg, 3891333c4301SDavid Ahern struct netlink_ext_ack *extack) 38926b9ea5a6SRoopa Prabhu { 38936b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 38946b9ea5a6SRoopa Prabhu struct rtnexthop *rtnh; 38956b9ea5a6SRoopa Prabhu int remaining; 38966b9ea5a6SRoopa Prabhu int attrlen; 38976b9ea5a6SRoopa Prabhu int err = 1, last_err = 0; 38986b9ea5a6SRoopa Prabhu 38996b9ea5a6SRoopa Prabhu remaining = cfg->fc_mp_len; 39006b9ea5a6SRoopa Prabhu rtnh = (struct rtnexthop *)cfg->fc_mp; 39016b9ea5a6SRoopa Prabhu 39026b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry */ 39036b9ea5a6SRoopa Prabhu while (rtnh_ok(rtnh, remaining)) { 39046b9ea5a6SRoopa Prabhu memcpy(&r_cfg, cfg, sizeof(*cfg)); 39056b9ea5a6SRoopa Prabhu if (rtnh->rtnh_ifindex) 39066b9ea5a6SRoopa Prabhu r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 39076b9ea5a6SRoopa Prabhu 39086b9ea5a6SRoopa Prabhu attrlen = rtnh_attrlen(rtnh); 39096b9ea5a6SRoopa Prabhu if (attrlen > 0) { 39106b9ea5a6SRoopa Prabhu struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 39116b9ea5a6SRoopa Prabhu 39126b9ea5a6SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_GATEWAY); 39136b9ea5a6SRoopa Prabhu if (nla) { 39146b9ea5a6SRoopa Prabhu nla_memcpy(&r_cfg.fc_gateway, nla, 16); 39156b9ea5a6SRoopa Prabhu r_cfg.fc_flags |= RTF_GATEWAY; 39166b9ea5a6SRoopa Prabhu } 39176b9ea5a6SRoopa Prabhu } 3918333c4301SDavid Ahern err = ip6_route_del(&r_cfg, extack); 39196b9ea5a6SRoopa Prabhu if (err) 39206b9ea5a6SRoopa Prabhu last_err = err; 39216b9ea5a6SRoopa Prabhu 392251ebd318SNicolas Dichtel rtnh = rtnh_next(rtnh, &remaining); 392351ebd318SNicolas Dichtel } 392451ebd318SNicolas Dichtel 392551ebd318SNicolas Dichtel return last_err; 392651ebd318SNicolas Dichtel } 392751ebd318SNicolas Dichtel 3928c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, 3929c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 39301da177e4SLinus Torvalds { 393186872cb5SThomas Graf struct fib6_config cfg; 393286872cb5SThomas Graf int err; 39331da177e4SLinus Torvalds 3934333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 393586872cb5SThomas Graf if (err < 0) 393686872cb5SThomas Graf return err; 393786872cb5SThomas Graf 393851ebd318SNicolas Dichtel if (cfg.fc_mp) 3939333c4301SDavid Ahern return ip6_route_multipath_del(&cfg, extack); 39400ae81335SDavid Ahern else { 39410ae81335SDavid Ahern cfg.fc_delete_all_nh = 1; 3942333c4301SDavid Ahern return ip6_route_del(&cfg, extack); 39431da177e4SLinus Torvalds } 39440ae81335SDavid Ahern } 39451da177e4SLinus Torvalds 3946c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, 3947c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 39481da177e4SLinus Torvalds { 394986872cb5SThomas Graf struct fib6_config cfg; 395086872cb5SThomas Graf int err; 39511da177e4SLinus Torvalds 3952333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 395386872cb5SThomas Graf if (err < 0) 395486872cb5SThomas Graf return err; 395586872cb5SThomas Graf 395651ebd318SNicolas Dichtel if (cfg.fc_mp) 3957333c4301SDavid Ahern return ip6_route_multipath_add(&cfg, extack); 395851ebd318SNicolas Dichtel else 3959333c4301SDavid Ahern return ip6_route_add(&cfg, extack); 39601da177e4SLinus Torvalds } 39611da177e4SLinus Torvalds 3962beb1afacSDavid Ahern static size_t rt6_nlmsg_size(struct rt6_info *rt) 3963339bf98fSThomas Graf { 3964beb1afacSDavid Ahern int nexthop_len = 0; 3965beb1afacSDavid Ahern 3966beb1afacSDavid Ahern if (rt->rt6i_nsiblings) { 3967beb1afacSDavid Ahern nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */ 3968beb1afacSDavid Ahern + NLA_ALIGN(sizeof(struct rtnexthop)) 3969beb1afacSDavid Ahern + nla_total_size(16) /* RTA_GATEWAY */ 3970beb1afacSDavid Ahern + lwtunnel_get_encap_size(rt->dst.lwtstate); 3971beb1afacSDavid Ahern 3972beb1afacSDavid Ahern nexthop_len *= rt->rt6i_nsiblings; 3973beb1afacSDavid Ahern } 3974beb1afacSDavid Ahern 3975339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 3976339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 3977339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 3978339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 3979339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 3980339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 3981339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 3982339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 3983339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 39846a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 3985ea697639SDaniel Borkmann + nla_total_size(sizeof(struct rta_cacheinfo)) 3986c78ba6d6SLubomir Rintel + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ 398719e42e45SRoopa Prabhu + nla_total_size(1) /* RTA_PREF */ 3988beb1afacSDavid Ahern + lwtunnel_get_encap_size(rt->dst.lwtstate) 3989beb1afacSDavid Ahern + nexthop_len; 3990beb1afacSDavid Ahern } 3991beb1afacSDavid Ahern 3992beb1afacSDavid Ahern static int rt6_nexthop_info(struct sk_buff *skb, struct rt6_info *rt, 39935be083ceSDavid Ahern unsigned int *flags, bool skip_oif) 3994beb1afacSDavid Ahern { 3995beb1afacSDavid Ahern if (!netif_running(rt->dst.dev) || !netif_carrier_ok(rt->dst.dev)) { 3996beb1afacSDavid Ahern *flags |= RTNH_F_LINKDOWN; 3997beb1afacSDavid Ahern if (rt->rt6i_idev->cnf.ignore_routes_with_linkdown) 3998beb1afacSDavid Ahern *flags |= RTNH_F_DEAD; 3999beb1afacSDavid Ahern } 4000beb1afacSDavid Ahern 4001beb1afacSDavid Ahern if (rt->rt6i_flags & RTF_GATEWAY) { 4002beb1afacSDavid Ahern if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0) 4003beb1afacSDavid Ahern goto nla_put_failure; 4004beb1afacSDavid Ahern } 4005beb1afacSDavid Ahern 4006fe400799SIdo Schimmel if (rt->rt6i_nh_flags & RTNH_F_OFFLOAD) 400761e4d01eSIdo Schimmel *flags |= RTNH_F_OFFLOAD; 400861e4d01eSIdo Schimmel 40095be083ceSDavid Ahern /* not needed for multipath encoding b/c it has a rtnexthop struct */ 40105be083ceSDavid Ahern if (!skip_oif && rt->dst.dev && 4011beb1afacSDavid Ahern nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex)) 4012beb1afacSDavid Ahern goto nla_put_failure; 4013beb1afacSDavid Ahern 4014beb1afacSDavid Ahern if (rt->dst.lwtstate && 4015beb1afacSDavid Ahern lwtunnel_fill_encap(skb, rt->dst.lwtstate) < 0) 4016beb1afacSDavid Ahern goto nla_put_failure; 4017beb1afacSDavid Ahern 4018beb1afacSDavid Ahern return 0; 4019beb1afacSDavid Ahern 4020beb1afacSDavid Ahern nla_put_failure: 4021beb1afacSDavid Ahern return -EMSGSIZE; 4022beb1afacSDavid Ahern } 4023beb1afacSDavid Ahern 40245be083ceSDavid Ahern /* add multipath next hop */ 4025beb1afacSDavid Ahern static int rt6_add_nexthop(struct sk_buff *skb, struct rt6_info *rt) 4026beb1afacSDavid Ahern { 4027beb1afacSDavid Ahern struct rtnexthop *rtnh; 4028beb1afacSDavid Ahern unsigned int flags = 0; 4029beb1afacSDavid Ahern 4030beb1afacSDavid Ahern rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh)); 4031beb1afacSDavid Ahern if (!rtnh) 4032beb1afacSDavid Ahern goto nla_put_failure; 4033beb1afacSDavid Ahern 4034beb1afacSDavid Ahern rtnh->rtnh_hops = 0; 4035beb1afacSDavid Ahern rtnh->rtnh_ifindex = rt->dst.dev ? rt->dst.dev->ifindex : 0; 4036beb1afacSDavid Ahern 40375be083ceSDavid Ahern if (rt6_nexthop_info(skb, rt, &flags, true) < 0) 4038beb1afacSDavid Ahern goto nla_put_failure; 4039beb1afacSDavid Ahern 4040beb1afacSDavid Ahern rtnh->rtnh_flags = flags; 4041beb1afacSDavid Ahern 4042beb1afacSDavid Ahern /* length of rtnetlink header + attributes */ 4043beb1afacSDavid Ahern rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh; 4044beb1afacSDavid Ahern 4045beb1afacSDavid Ahern return 0; 4046beb1afacSDavid Ahern 4047beb1afacSDavid Ahern nla_put_failure: 4048beb1afacSDavid Ahern return -EMSGSIZE; 4049339bf98fSThomas Graf } 4050339bf98fSThomas Graf 4051191cd582SBrian Haley static int rt6_fill_node(struct net *net, 4052191cd582SBrian Haley struct sk_buff *skb, struct rt6_info *rt, 40530d51aa80SJamal Hadi Salim struct in6_addr *dst, struct in6_addr *src, 405415e47304SEric W. Biederman int iif, int type, u32 portid, u32 seq, 4055f8cfe2ceSDavid Ahern unsigned int flags) 40561da177e4SLinus Torvalds { 40574b32b5adSMartin KaFai Lau u32 metrics[RTAX_MAX]; 40581da177e4SLinus Torvalds struct rtmsg *rtm; 40591da177e4SLinus Torvalds struct nlmsghdr *nlh; 4060e3703b3dSThomas Graf long expires; 40619e762a4aSPatrick McHardy u32 table; 40621da177e4SLinus Torvalds 406315e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); 406438308473SDavid S. Miller if (!nlh) 406526932566SPatrick McHardy return -EMSGSIZE; 40662d7202bfSThomas Graf 40672d7202bfSThomas Graf rtm = nlmsg_data(nlh); 40681da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 40691da177e4SLinus Torvalds rtm->rtm_dst_len = rt->rt6i_dst.plen; 40701da177e4SLinus Torvalds rtm->rtm_src_len = rt->rt6i_src.plen; 40711da177e4SLinus Torvalds rtm->rtm_tos = 0; 4072c71099acSThomas Graf if (rt->rt6i_table) 40739e762a4aSPatrick McHardy table = rt->rt6i_table->tb6_id; 4074c71099acSThomas Graf else 40759e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 40769e762a4aSPatrick McHardy rtm->rtm_table = table; 4077c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, table)) 4078c78679e8SDavid S. Miller goto nla_put_failure; 4079ef2c7d7bSNicolas Dichtel if (rt->rt6i_flags & RTF_REJECT) { 4080ef2c7d7bSNicolas Dichtel switch (rt->dst.error) { 4081ef2c7d7bSNicolas Dichtel case -EINVAL: 4082ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_BLACKHOLE; 4083ef2c7d7bSNicolas Dichtel break; 4084ef2c7d7bSNicolas Dichtel case -EACCES: 4085ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_PROHIBIT; 4086ef2c7d7bSNicolas Dichtel break; 4087b4949ab2SNicolas Dichtel case -EAGAIN: 4088b4949ab2SNicolas Dichtel rtm->rtm_type = RTN_THROW; 4089b4949ab2SNicolas Dichtel break; 4090ef2c7d7bSNicolas Dichtel default: 40911da177e4SLinus Torvalds rtm->rtm_type = RTN_UNREACHABLE; 4092ef2c7d7bSNicolas Dichtel break; 4093ef2c7d7bSNicolas Dichtel } 4094ef2c7d7bSNicolas Dichtel } 4095ab79ad14SMaciej Żenczykowski else if (rt->rt6i_flags & RTF_LOCAL) 4096ab79ad14SMaciej Żenczykowski rtm->rtm_type = RTN_LOCAL; 40974ee39733SDavid Ahern else if (rt->rt6i_flags & RTF_ANYCAST) 40984ee39733SDavid Ahern rtm->rtm_type = RTN_ANYCAST; 4099d1918542SDavid S. Miller else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK)) 41001da177e4SLinus Torvalds rtm->rtm_type = RTN_LOCAL; 41011da177e4SLinus Torvalds else 41021da177e4SLinus Torvalds rtm->rtm_type = RTN_UNICAST; 41031da177e4SLinus Torvalds rtm->rtm_flags = 0; 41041da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 41051da177e4SLinus Torvalds rtm->rtm_protocol = rt->rt6i_protocol; 41061da177e4SLinus Torvalds 41071da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) 41081da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 41091da177e4SLinus Torvalds 41101da177e4SLinus Torvalds if (dst) { 4111930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_DST, dst)) 4112c78679e8SDavid S. Miller goto nla_put_failure; 41131da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 41141da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 4115930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr)) 4116c78679e8SDavid S. Miller goto nla_put_failure; 41171da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 41181da177e4SLinus Torvalds if (src) { 4119930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_SRC, src)) 4120c78679e8SDavid S. Miller goto nla_put_failure; 41211da177e4SLinus Torvalds rtm->rtm_src_len = 128; 4122c78679e8SDavid S. Miller } else if (rtm->rtm_src_len && 4123930345eaSJiri Benc nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr)) 4124c78679e8SDavid S. Miller goto nla_put_failure; 41251da177e4SLinus Torvalds #endif 41267bc570c8SYOSHIFUJI Hideaki if (iif) { 41277bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 41287bc570c8SYOSHIFUJI Hideaki if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) { 4129fd61c6baSDavid Ahern int err = ip6mr_get_route(net, skb, rtm, portid); 41302cf75070SNikolay Aleksandrov 41317bc570c8SYOSHIFUJI Hideaki if (err == 0) 41327bc570c8SYOSHIFUJI Hideaki return 0; 4133fd61c6baSDavid Ahern if (err < 0) 41347bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 41357bc570c8SYOSHIFUJI Hideaki } else 41367bc570c8SYOSHIFUJI Hideaki #endif 4137c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_IIF, iif)) 4138c78679e8SDavid S. Miller goto nla_put_failure; 41397bc570c8SYOSHIFUJI Hideaki } else if (dst) { 41401da177e4SLinus Torvalds struct in6_addr saddr_buf; 4141c78679e8SDavid S. Miller if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 && 4142930345eaSJiri Benc nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 4143c78679e8SDavid S. Miller goto nla_put_failure; 4144c3968a85SDaniel Walter } 4145c3968a85SDaniel Walter 4146c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) { 4147c3968a85SDaniel Walter struct in6_addr saddr_buf; 41484e3fd7a0SAlexey Dobriyan saddr_buf = rt->rt6i_prefsrc.addr; 4149930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 4150c78679e8SDavid S. Miller goto nla_put_failure; 41511da177e4SLinus Torvalds } 41522d7202bfSThomas Graf 41534b32b5adSMartin KaFai Lau memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics)); 41544b32b5adSMartin KaFai Lau if (rt->rt6i_pmtu) 41554b32b5adSMartin KaFai Lau metrics[RTAX_MTU - 1] = rt->rt6i_pmtu; 41564b32b5adSMartin KaFai Lau if (rtnetlink_put_metrics(skb, metrics) < 0) 41572d7202bfSThomas Graf goto nla_put_failure; 41582d7202bfSThomas Graf 4159beb1afacSDavid Ahern if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric)) 4160beb1afacSDavid Ahern goto nla_put_failure; 4161beb1afacSDavid Ahern 4162beb1afacSDavid Ahern /* For multipath routes, walk the siblings list and add 4163beb1afacSDavid Ahern * each as a nexthop within RTA_MULTIPATH. 4164beb1afacSDavid Ahern */ 4165beb1afacSDavid Ahern if (rt->rt6i_nsiblings) { 4166beb1afacSDavid Ahern struct rt6_info *sibling, *next_sibling; 4167beb1afacSDavid Ahern struct nlattr *mp; 4168beb1afacSDavid Ahern 4169beb1afacSDavid Ahern mp = nla_nest_start(skb, RTA_MULTIPATH); 4170beb1afacSDavid Ahern if (!mp) 4171beb1afacSDavid Ahern goto nla_put_failure; 4172beb1afacSDavid Ahern 4173beb1afacSDavid Ahern if (rt6_add_nexthop(skb, rt) < 0) 4174beb1afacSDavid Ahern goto nla_put_failure; 4175beb1afacSDavid Ahern 4176beb1afacSDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 4177beb1afacSDavid Ahern &rt->rt6i_siblings, rt6i_siblings) { 4178beb1afacSDavid Ahern if (rt6_add_nexthop(skb, sibling) < 0) 417994f826b8SEric Dumazet goto nla_put_failure; 418094f826b8SEric Dumazet } 41812d7202bfSThomas Graf 4182beb1afacSDavid Ahern nla_nest_end(skb, mp); 4183beb1afacSDavid Ahern } else { 41845be083ceSDavid Ahern if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0) 4185c78679e8SDavid S. Miller goto nla_put_failure; 4186beb1afacSDavid Ahern } 41878253947eSLi Wei 41888253947eSLi Wei expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0; 418969cdf8f9SYOSHIFUJI Hideaki 419087a50699SDavid S. Miller if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0) 4191e3703b3dSThomas Graf goto nla_put_failure; 41921da177e4SLinus Torvalds 4193c78ba6d6SLubomir Rintel if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags))) 4194c78ba6d6SLubomir Rintel goto nla_put_failure; 4195c78ba6d6SLubomir Rintel 419619e42e45SRoopa Prabhu 4197053c095aSJohannes Berg nlmsg_end(skb, nlh); 4198053c095aSJohannes Berg return 0; 41992d7202bfSThomas Graf 42002d7202bfSThomas Graf nla_put_failure: 420126932566SPatrick McHardy nlmsg_cancel(skb, nlh); 420226932566SPatrick McHardy return -EMSGSIZE; 42031da177e4SLinus Torvalds } 42041da177e4SLinus Torvalds 42051b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg) 42061da177e4SLinus Torvalds { 42071da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 42081f17e2f2SDavid Ahern struct net *net = arg->net; 42091f17e2f2SDavid Ahern 42101f17e2f2SDavid Ahern if (rt == net->ipv6.ip6_null_entry) 42111f17e2f2SDavid Ahern return 0; 42121da177e4SLinus Torvalds 42132d7202bfSThomas Graf if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { 42142d7202bfSThomas Graf struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); 4215f8cfe2ceSDavid Ahern 4216f8cfe2ceSDavid Ahern /* user wants prefix routes only */ 4217f8cfe2ceSDavid Ahern if (rtm->rtm_flags & RTM_F_PREFIX && 4218f8cfe2ceSDavid Ahern !(rt->rt6i_flags & RTF_PREFIX_RT)) { 4219f8cfe2ceSDavid Ahern /* success since this is not a prefix route */ 4220f8cfe2ceSDavid Ahern return 1; 4221f8cfe2ceSDavid Ahern } 4222f8cfe2ceSDavid Ahern } 42231da177e4SLinus Torvalds 42241f17e2f2SDavid Ahern return rt6_fill_node(net, 4225191cd582SBrian Haley arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE, 422615e47304SEric W. Biederman NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq, 4227f8cfe2ceSDavid Ahern NLM_F_MULTI); 42281da177e4SLinus Torvalds } 42291da177e4SLinus Torvalds 4230c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, 4231c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 42321da177e4SLinus Torvalds { 42333b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 4234ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 423518c3a61cSRoopa Prabhu int err, iif = 0, oif = 0; 423618c3a61cSRoopa Prabhu struct dst_entry *dst; 42371da177e4SLinus Torvalds struct rt6_info *rt; 4238ab364a6fSThomas Graf struct sk_buff *skb; 4239ab364a6fSThomas Graf struct rtmsg *rtm; 42404c9483b2SDavid S. Miller struct flowi6 fl6; 424118c3a61cSRoopa Prabhu bool fibmatch; 4242ab364a6fSThomas Graf 4243fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 4244c21ef3e3SDavid Ahern extack); 4245ab364a6fSThomas Graf if (err < 0) 4246ab364a6fSThomas Graf goto errout; 4247ab364a6fSThomas Graf 4248ab364a6fSThomas Graf err = -EINVAL; 42494c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 425038b7097bSHannes Frederic Sowa rtm = nlmsg_data(nlh); 425138b7097bSHannes Frederic Sowa fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0); 425218c3a61cSRoopa Prabhu fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); 4253ab364a6fSThomas Graf 4254ab364a6fSThomas Graf if (tb[RTA_SRC]) { 4255ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 4256ab364a6fSThomas Graf goto errout; 4257ab364a6fSThomas Graf 42584e3fd7a0SAlexey Dobriyan fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 4259ab364a6fSThomas Graf } 4260ab364a6fSThomas Graf 4261ab364a6fSThomas Graf if (tb[RTA_DST]) { 4262ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 4263ab364a6fSThomas Graf goto errout; 4264ab364a6fSThomas Graf 42654e3fd7a0SAlexey Dobriyan fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 4266ab364a6fSThomas Graf } 4267ab364a6fSThomas Graf 4268ab364a6fSThomas Graf if (tb[RTA_IIF]) 4269ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 4270ab364a6fSThomas Graf 4271ab364a6fSThomas Graf if (tb[RTA_OIF]) 427272331bc0SShmulik Ladkani oif = nla_get_u32(tb[RTA_OIF]); 4273ab364a6fSThomas Graf 42742e47b291SLorenzo Colitti if (tb[RTA_MARK]) 42752e47b291SLorenzo Colitti fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); 42762e47b291SLorenzo Colitti 4277622ec2c9SLorenzo Colitti if (tb[RTA_UID]) 4278622ec2c9SLorenzo Colitti fl6.flowi6_uid = make_kuid(current_user_ns(), 4279622ec2c9SLorenzo Colitti nla_get_u32(tb[RTA_UID])); 4280622ec2c9SLorenzo Colitti else 4281622ec2c9SLorenzo Colitti fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); 4282622ec2c9SLorenzo Colitti 4283ab364a6fSThomas Graf if (iif) { 4284ab364a6fSThomas Graf struct net_device *dev; 428572331bc0SShmulik Ladkani int flags = 0; 428672331bc0SShmulik Ladkani 4287121622dbSFlorian Westphal rcu_read_lock(); 4288121622dbSFlorian Westphal 4289121622dbSFlorian Westphal dev = dev_get_by_index_rcu(net, iif); 4290ab364a6fSThomas Graf if (!dev) { 4291121622dbSFlorian Westphal rcu_read_unlock(); 4292ab364a6fSThomas Graf err = -ENODEV; 4293ab364a6fSThomas Graf goto errout; 4294ab364a6fSThomas Graf } 429572331bc0SShmulik Ladkani 429672331bc0SShmulik Ladkani fl6.flowi6_iif = iif; 429772331bc0SShmulik Ladkani 429872331bc0SShmulik Ladkani if (!ipv6_addr_any(&fl6.saddr)) 429972331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_HAS_SADDR; 430072331bc0SShmulik Ladkani 430118c3a61cSRoopa Prabhu dst = ip6_route_input_lookup(net, dev, &fl6, flags); 4302121622dbSFlorian Westphal 4303121622dbSFlorian Westphal rcu_read_unlock(); 430472331bc0SShmulik Ladkani } else { 430572331bc0SShmulik Ladkani fl6.flowi6_oif = oif; 430672331bc0SShmulik Ladkani 430718c3a61cSRoopa Prabhu dst = ip6_route_output(net, NULL, &fl6); 430818c3a61cSRoopa Prabhu } 430918c3a61cSRoopa Prabhu 431018c3a61cSRoopa Prabhu 431118c3a61cSRoopa Prabhu rt = container_of(dst, struct rt6_info, dst); 431218c3a61cSRoopa Prabhu if (rt->dst.error) { 431318c3a61cSRoopa Prabhu err = rt->dst.error; 431418c3a61cSRoopa Prabhu ip6_rt_put(rt); 431518c3a61cSRoopa Prabhu goto errout; 4316ab364a6fSThomas Graf } 43171da177e4SLinus Torvalds 43189d6acb3bSWANG Cong if (rt == net->ipv6.ip6_null_entry) { 43199d6acb3bSWANG Cong err = rt->dst.error; 43209d6acb3bSWANG Cong ip6_rt_put(rt); 43219d6acb3bSWANG Cong goto errout; 43229d6acb3bSWANG Cong } 43239d6acb3bSWANG Cong 4324*fba961abSDavid S. Miller if (fibmatch && rt->from) { 4325*fba961abSDavid S. Miller struct rt6_info *ort = rt->from; 432658acfd71SIdo Schimmel 432758acfd71SIdo Schimmel dst_hold(&ort->dst); 432858acfd71SIdo Schimmel ip6_rt_put(rt); 432958acfd71SIdo Schimmel rt = ort; 433058acfd71SIdo Schimmel } 433158acfd71SIdo Schimmel 43321da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 433338308473SDavid S. Miller if (!skb) { 433494e187c0SAmerigo Wang ip6_rt_put(rt); 4335ab364a6fSThomas Graf err = -ENOBUFS; 4336ab364a6fSThomas Graf goto errout; 4337ab364a6fSThomas Graf } 43381da177e4SLinus Torvalds 4339d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 434018c3a61cSRoopa Prabhu if (fibmatch) 434118c3a61cSRoopa Prabhu err = rt6_fill_node(net, skb, rt, NULL, NULL, iif, 434218c3a61cSRoopa Prabhu RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 434318c3a61cSRoopa Prabhu nlh->nlmsg_seq, 0); 434418c3a61cSRoopa Prabhu else 43454c9483b2SDavid S. Miller err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif, 434615e47304SEric W. Biederman RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 4347f8cfe2ceSDavid Ahern nlh->nlmsg_seq, 0); 43481da177e4SLinus Torvalds if (err < 0) { 4349ab364a6fSThomas Graf kfree_skb(skb); 4350ab364a6fSThomas Graf goto errout; 43511da177e4SLinus Torvalds } 43521da177e4SLinus Torvalds 435315e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 4354ab364a6fSThomas Graf errout: 43551da177e4SLinus Torvalds return err; 43561da177e4SLinus Torvalds } 43571da177e4SLinus Torvalds 435837a1d361SRoopa Prabhu void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info, 435937a1d361SRoopa Prabhu unsigned int nlm_flags) 43601da177e4SLinus Torvalds { 43611da177e4SLinus Torvalds struct sk_buff *skb; 43625578689aSDaniel Lezcano struct net *net = info->nl_net; 4363528c4cebSDenis V. Lunev u32 seq; 4364528c4cebSDenis V. Lunev int err; 43650d51aa80SJamal Hadi Salim 4366528c4cebSDenis V. Lunev err = -ENOBUFS; 436738308473SDavid S. Miller seq = info->nlh ? info->nlh->nlmsg_seq : 0; 436886872cb5SThomas Graf 436919e42e45SRoopa Prabhu skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 437038308473SDavid S. Miller if (!skb) 437121713ebcSThomas Graf goto errout; 43721da177e4SLinus Torvalds 4373191cd582SBrian Haley err = rt6_fill_node(net, skb, rt, NULL, NULL, 0, 4374f8cfe2ceSDavid Ahern event, info->portid, seq, nlm_flags); 437526932566SPatrick McHardy if (err < 0) { 437626932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 437726932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 437826932566SPatrick McHardy kfree_skb(skb); 437926932566SPatrick McHardy goto errout; 438026932566SPatrick McHardy } 438115e47304SEric W. Biederman rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 43825578689aSDaniel Lezcano info->nlh, gfp_any()); 43831ce85fe4SPablo Neira Ayuso return; 438421713ebcSThomas Graf errout: 438521713ebcSThomas Graf if (err < 0) 43865578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 43871da177e4SLinus Torvalds } 43881da177e4SLinus Torvalds 43898ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 4390351638e7SJiri Pirko unsigned long event, void *ptr) 43918ed67789SDaniel Lezcano { 4392351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 4393c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 43948ed67789SDaniel Lezcano 4395242d3a49SWANG Cong if (!(dev->flags & IFF_LOOPBACK)) 4396242d3a49SWANG Cong return NOTIFY_OK; 4397242d3a49SWANG Cong 4398242d3a49SWANG Cong if (event == NETDEV_REGISTER) { 4399d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 44008ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 44018ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 4402d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 44038ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 4404d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 44058ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 44068ed67789SDaniel Lezcano #endif 440776da0704SWANG Cong } else if (event == NETDEV_UNREGISTER && 440876da0704SWANG Cong dev->reg_state != NETREG_UNREGISTERED) { 440976da0704SWANG Cong /* NETDEV_UNREGISTER could be fired for multiple times by 441076da0704SWANG Cong * netdev_wait_allrefs(). Make sure we only call this once. 441176da0704SWANG Cong */ 441212d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev); 4413242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 441412d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev); 441512d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev); 4416242d3a49SWANG Cong #endif 44178ed67789SDaniel Lezcano } 44188ed67789SDaniel Lezcano 44198ed67789SDaniel Lezcano return NOTIFY_OK; 44208ed67789SDaniel Lezcano } 44218ed67789SDaniel Lezcano 44221da177e4SLinus Torvalds /* 44231da177e4SLinus Torvalds * /proc 44241da177e4SLinus Torvalds */ 44251da177e4SLinus Torvalds 44261da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 44271da177e4SLinus Torvalds 442833120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = { 442933120b30SAlexey Dobriyan .owner = THIS_MODULE, 443033120b30SAlexey Dobriyan .open = ipv6_route_open, 443133120b30SAlexey Dobriyan .read = seq_read, 443233120b30SAlexey Dobriyan .llseek = seq_lseek, 44338d2ca1d7SHannes Frederic Sowa .release = seq_release_net, 443433120b30SAlexey Dobriyan }; 443533120b30SAlexey Dobriyan 44361da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 44371da177e4SLinus Torvalds { 443869ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 44391da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 444069ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 444169ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 444281eb8447SWei Wang atomic_read(&net->ipv6.rt6_stats->fib_rt_alloc), 444369ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 444469ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 4445fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 444669ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 44471da177e4SLinus Torvalds 44481da177e4SLinus Torvalds return 0; 44491da177e4SLinus Torvalds } 44501da177e4SLinus Torvalds 44511da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file) 44521da177e4SLinus Torvalds { 4453de05c557SPavel Emelyanov return single_open_net(inode, file, rt6_stats_seq_show); 445469ddb805SDaniel Lezcano } 445569ddb805SDaniel Lezcano 44569a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = { 44571da177e4SLinus Torvalds .owner = THIS_MODULE, 44581da177e4SLinus Torvalds .open = rt6_stats_seq_open, 44591da177e4SLinus Torvalds .read = seq_read, 44601da177e4SLinus Torvalds .llseek = seq_lseek, 4461b6fcbdb4SPavel Emelyanov .release = single_release_net, 44621da177e4SLinus Torvalds }; 44631da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 44641da177e4SLinus Torvalds 44651da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 44661da177e4SLinus Torvalds 44671da177e4SLinus Torvalds static 4468fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, 44691da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 44701da177e4SLinus Torvalds { 4471c486da34SLucian Adrian Grijincu struct net *net; 4472c486da34SLucian Adrian Grijincu int delay; 4473c486da34SLucian Adrian Grijincu if (!write) 4474c486da34SLucian Adrian Grijincu return -EINVAL; 4475c486da34SLucian Adrian Grijincu 4476c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 4477c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 44788d65af78SAlexey Dobriyan proc_dointvec(ctl, write, buffer, lenp, ppos); 44792ac3ac8fSMichal Kubeček fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0); 44801da177e4SLinus Torvalds return 0; 44811da177e4SLinus Torvalds } 44821da177e4SLinus Torvalds 4483fe2c6338SJoe Perches struct ctl_table ipv6_route_table_template[] = { 44841da177e4SLinus Torvalds { 44851da177e4SLinus Torvalds .procname = "flush", 44864990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 44871da177e4SLinus Torvalds .maxlen = sizeof(int), 448889c8b3a1SDave Jones .mode = 0200, 44896d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 44901da177e4SLinus Torvalds }, 44911da177e4SLinus Torvalds { 44921da177e4SLinus Torvalds .procname = "gc_thresh", 44939a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 44941da177e4SLinus Torvalds .maxlen = sizeof(int), 44951da177e4SLinus Torvalds .mode = 0644, 44966d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 44971da177e4SLinus Torvalds }, 44981da177e4SLinus Torvalds { 44991da177e4SLinus Torvalds .procname = "max_size", 45004990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 45011da177e4SLinus Torvalds .maxlen = sizeof(int), 45021da177e4SLinus Torvalds .mode = 0644, 45036d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 45041da177e4SLinus Torvalds }, 45051da177e4SLinus Torvalds { 45061da177e4SLinus Torvalds .procname = "gc_min_interval", 45074990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 45081da177e4SLinus Torvalds .maxlen = sizeof(int), 45091da177e4SLinus Torvalds .mode = 0644, 45106d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 45111da177e4SLinus Torvalds }, 45121da177e4SLinus Torvalds { 45131da177e4SLinus Torvalds .procname = "gc_timeout", 45144990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 45151da177e4SLinus Torvalds .maxlen = sizeof(int), 45161da177e4SLinus Torvalds .mode = 0644, 45176d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 45181da177e4SLinus Torvalds }, 45191da177e4SLinus Torvalds { 45201da177e4SLinus Torvalds .procname = "gc_interval", 45214990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 45221da177e4SLinus Torvalds .maxlen = sizeof(int), 45231da177e4SLinus Torvalds .mode = 0644, 45246d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 45251da177e4SLinus Torvalds }, 45261da177e4SLinus Torvalds { 45271da177e4SLinus Torvalds .procname = "gc_elasticity", 45284990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 45291da177e4SLinus Torvalds .maxlen = sizeof(int), 45301da177e4SLinus Torvalds .mode = 0644, 4531f3d3f616SMin Zhang .proc_handler = proc_dointvec, 45321da177e4SLinus Torvalds }, 45331da177e4SLinus Torvalds { 45341da177e4SLinus Torvalds .procname = "mtu_expires", 45354990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 45361da177e4SLinus Torvalds .maxlen = sizeof(int), 45371da177e4SLinus Torvalds .mode = 0644, 45386d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 45391da177e4SLinus Torvalds }, 45401da177e4SLinus Torvalds { 45411da177e4SLinus Torvalds .procname = "min_adv_mss", 45424990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 45431da177e4SLinus Torvalds .maxlen = sizeof(int), 45441da177e4SLinus Torvalds .mode = 0644, 4545f3d3f616SMin Zhang .proc_handler = proc_dointvec, 45461da177e4SLinus Torvalds }, 45471da177e4SLinus Torvalds { 45481da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 45494990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 45501da177e4SLinus Torvalds .maxlen = sizeof(int), 45511da177e4SLinus Torvalds .mode = 0644, 45526d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 45531da177e4SLinus Torvalds }, 4554f8572d8fSEric W. Biederman { } 45551da177e4SLinus Torvalds }; 45561da177e4SLinus Torvalds 45572c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 4558760f2d01SDaniel Lezcano { 4559760f2d01SDaniel Lezcano struct ctl_table *table; 4560760f2d01SDaniel Lezcano 4561760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 4562760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 4563760f2d01SDaniel Lezcano GFP_KERNEL); 45645ee09105SYOSHIFUJI Hideaki 45655ee09105SYOSHIFUJI Hideaki if (table) { 45665ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 4567c486da34SLucian Adrian Grijincu table[0].extra1 = net; 456886393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 45695ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 45705ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 45715ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 45725ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 45735ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 45745ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 45755ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 45769c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 4577464dc801SEric W. Biederman 4578464dc801SEric W. Biederman /* Don't export sysctls to unprivileged users */ 4579464dc801SEric W. Biederman if (net->user_ns != &init_user_ns) 4580464dc801SEric W. Biederman table[0].procname = NULL; 45815ee09105SYOSHIFUJI Hideaki } 45825ee09105SYOSHIFUJI Hideaki 4583760f2d01SDaniel Lezcano return table; 4584760f2d01SDaniel Lezcano } 45851da177e4SLinus Torvalds #endif 45861da177e4SLinus Torvalds 45872c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 4588cdb18761SDaniel Lezcano { 4589633d424bSPavel Emelyanov int ret = -ENOMEM; 45908ed67789SDaniel Lezcano 459186393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 459286393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 4593f2fc6a54SBenjamin Thery 4594fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 4595fc66f95cSEric Dumazet goto out_ip6_dst_ops; 4596fc66f95cSEric Dumazet 45978ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 45988ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 45998ed67789SDaniel Lezcano GFP_KERNEL); 46008ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 4601fc66f95cSEric Dumazet goto out_ip6_dst_entries; 4602d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 460362fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 460462fa8a84SDavid S. Miller ip6_template_metrics, true); 46058ed67789SDaniel Lezcano 46068ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 4607feca7d8cSVincent Bernat net->ipv6.fib6_has_custom_rules = false; 46088ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 46098ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 46108ed67789SDaniel Lezcano GFP_KERNEL); 461168fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 461268fffc67SPeter Zijlstra goto out_ip6_null_entry; 4613d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 461462fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 461562fa8a84SDavid S. Miller ip6_template_metrics, true); 46168ed67789SDaniel Lezcano 46178ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 46188ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 46198ed67789SDaniel Lezcano GFP_KERNEL); 462068fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 462168fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 4622d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 462362fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 462462fa8a84SDavid S. Miller ip6_template_metrics, true); 46258ed67789SDaniel Lezcano #endif 46268ed67789SDaniel Lezcano 4627b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 4628b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 4629b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 4630b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 4631b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 4632b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 4633b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 4634b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 4635b339a47cSPeter Zijlstra 46366891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 46376891a346SBenjamin Thery 46388ed67789SDaniel Lezcano ret = 0; 46398ed67789SDaniel Lezcano out: 46408ed67789SDaniel Lezcano return ret; 4641f2fc6a54SBenjamin Thery 464268fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 464368fffc67SPeter Zijlstra out_ip6_prohibit_entry: 464468fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 464568fffc67SPeter Zijlstra out_ip6_null_entry: 464668fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 464768fffc67SPeter Zijlstra #endif 4648fc66f95cSEric Dumazet out_ip6_dst_entries: 4649fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 4650f2fc6a54SBenjamin Thery out_ip6_dst_ops: 4651f2fc6a54SBenjamin Thery goto out; 4652cdb18761SDaniel Lezcano } 4653cdb18761SDaniel Lezcano 46542c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 4655cdb18761SDaniel Lezcano { 46568ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 46578ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 46588ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 46598ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 46608ed67789SDaniel Lezcano #endif 466141bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 4662cdb18761SDaniel Lezcano } 4663cdb18761SDaniel Lezcano 4664d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net) 4665d189634eSThomas Graf { 4666d189634eSThomas Graf #ifdef CONFIG_PROC_FS 4667d4beaa66SGao feng proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops); 4668d4beaa66SGao feng proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops); 4669d189634eSThomas Graf #endif 4670d189634eSThomas Graf return 0; 4671d189634eSThomas Graf } 4672d189634eSThomas Graf 4673d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net) 4674d189634eSThomas Graf { 4675d189634eSThomas Graf #ifdef CONFIG_PROC_FS 4676ece31ffdSGao feng remove_proc_entry("ipv6_route", net->proc_net); 4677ece31ffdSGao feng remove_proc_entry("rt6_stats", net->proc_net); 4678d189634eSThomas Graf #endif 4679d189634eSThomas Graf } 4680d189634eSThomas Graf 4681cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 4682cdb18761SDaniel Lezcano .init = ip6_route_net_init, 4683cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 4684cdb18761SDaniel Lezcano }; 4685cdb18761SDaniel Lezcano 4686c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net) 4687c3426b47SDavid S. Miller { 4688c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 4689c3426b47SDavid S. Miller 4690c3426b47SDavid S. Miller if (!bp) 4691c3426b47SDavid S. Miller return -ENOMEM; 4692c3426b47SDavid S. Miller inet_peer_base_init(bp); 4693c3426b47SDavid S. Miller net->ipv6.peers = bp; 4694c3426b47SDavid S. Miller return 0; 4695c3426b47SDavid S. Miller } 4696c3426b47SDavid S. Miller 4697c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net) 4698c3426b47SDavid S. Miller { 4699c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv6.peers; 4700c3426b47SDavid S. Miller 4701c3426b47SDavid S. Miller net->ipv6.peers = NULL; 470256a6b248SDavid S. Miller inetpeer_invalidate_tree(bp); 4703c3426b47SDavid S. Miller kfree(bp); 4704c3426b47SDavid S. Miller } 4705c3426b47SDavid S. Miller 47062b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = { 4707c3426b47SDavid S. Miller .init = ipv6_inetpeer_init, 4708c3426b47SDavid S. Miller .exit = ipv6_inetpeer_exit, 4709c3426b47SDavid S. Miller }; 4710c3426b47SDavid S. Miller 4711d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = { 4712d189634eSThomas Graf .init = ip6_route_net_init_late, 4713d189634eSThomas Graf .exit = ip6_route_net_exit_late, 4714d189634eSThomas Graf }; 4715d189634eSThomas Graf 47168ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 47178ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 4718242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY - 10, 47198ed67789SDaniel Lezcano }; 47208ed67789SDaniel Lezcano 47212f460933SWANG Cong void __init ip6_route_init_special_entries(void) 47222f460933SWANG Cong { 47232f460933SWANG Cong /* Registering of the loopback is done before this portion of code, 47242f460933SWANG Cong * the loopback reference in rt6_info will not be taken, do it 47252f460933SWANG Cong * manually for init_net */ 47262f460933SWANG Cong init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 47272f460933SWANG Cong init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 47282f460933SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 47292f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 47302f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 47312f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 47322f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 47332f460933SWANG Cong #endif 47342f460933SWANG Cong } 47352f460933SWANG Cong 4736433d49c3SDaniel Lezcano int __init ip6_route_init(void) 47371da177e4SLinus Torvalds { 4738433d49c3SDaniel Lezcano int ret; 47398d0b94afSMartin KaFai Lau int cpu; 4740433d49c3SDaniel Lezcano 47419a7ec3a9SDaniel Lezcano ret = -ENOMEM; 47429a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 47439a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 47449a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 47459a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 4746c19a28e1SFernando Carrijo goto out; 474714e50e57SDavid S. Miller 4748fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 47498ed67789SDaniel Lezcano if (ret) 4750bdb3289fSDaniel Lezcano goto out_kmem_cache; 4751bdb3289fSDaniel Lezcano 4752c3426b47SDavid S. Miller ret = register_pernet_subsys(&ipv6_inetpeer_ops); 4753c3426b47SDavid S. Miller if (ret) 4754e8803b6cSDavid S. Miller goto out_dst_entries; 47552a0c451aSThomas Graf 47567e52b33bSDavid S. Miller ret = register_pernet_subsys(&ip6_route_net_ops); 47577e52b33bSDavid S. Miller if (ret) 47587e52b33bSDavid S. Miller goto out_register_inetpeer; 4759c3426b47SDavid S. Miller 47605dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 47615dc121e9SArnaud Ebalard 4762e8803b6cSDavid S. Miller ret = fib6_init(); 4763433d49c3SDaniel Lezcano if (ret) 47648ed67789SDaniel Lezcano goto out_register_subsys; 4765433d49c3SDaniel Lezcano 4766433d49c3SDaniel Lezcano ret = xfrm6_init(); 4767433d49c3SDaniel Lezcano if (ret) 4768e8803b6cSDavid S. Miller goto out_fib6_init; 4769c35b7e72SDaniel Lezcano 4770433d49c3SDaniel Lezcano ret = fib6_rules_init(); 4771433d49c3SDaniel Lezcano if (ret) 4772433d49c3SDaniel Lezcano goto xfrm6_init; 47737e5449c2SDaniel Lezcano 4774d189634eSThomas Graf ret = register_pernet_subsys(&ip6_route_net_late_ops); 4775d189634eSThomas Graf if (ret) 4776d189634eSThomas Graf goto fib6_rules_init; 4777d189634eSThomas Graf 477816feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWROUTE, 477916feebcfSFlorian Westphal inet6_rtm_newroute, NULL, 0); 478016feebcfSFlorian Westphal if (ret < 0) 478116feebcfSFlorian Westphal goto out_register_late_subsys; 478216feebcfSFlorian Westphal 478316feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELROUTE, 478416feebcfSFlorian Westphal inet6_rtm_delroute, NULL, 0); 478516feebcfSFlorian Westphal if (ret < 0) 478616feebcfSFlorian Westphal goto out_register_late_subsys; 478716feebcfSFlorian Westphal 478816feebcfSFlorian Westphal ret = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETROUTE, 478916feebcfSFlorian Westphal inet6_rtm_getroute, NULL, 479016feebcfSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED); 479116feebcfSFlorian Westphal if (ret < 0) 4792d189634eSThomas Graf goto out_register_late_subsys; 4793433d49c3SDaniel Lezcano 47948ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 4795cdb18761SDaniel Lezcano if (ret) 4796d189634eSThomas Graf goto out_register_late_subsys; 47978ed67789SDaniel Lezcano 47988d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 47998d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 48008d0b94afSMartin KaFai Lau 48018d0b94afSMartin KaFai Lau INIT_LIST_HEAD(&ul->head); 48028d0b94afSMartin KaFai Lau spin_lock_init(&ul->lock); 48038d0b94afSMartin KaFai Lau } 48048d0b94afSMartin KaFai Lau 4805433d49c3SDaniel Lezcano out: 4806433d49c3SDaniel Lezcano return ret; 4807433d49c3SDaniel Lezcano 4808d189634eSThomas Graf out_register_late_subsys: 480916feebcfSFlorian Westphal rtnl_unregister_all(PF_INET6); 4810d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 4811433d49c3SDaniel Lezcano fib6_rules_init: 4812433d49c3SDaniel Lezcano fib6_rules_cleanup(); 4813433d49c3SDaniel Lezcano xfrm6_init: 4814433d49c3SDaniel Lezcano xfrm6_fini(); 48152a0c451aSThomas Graf out_fib6_init: 48162a0c451aSThomas Graf fib6_gc_cleanup(); 48178ed67789SDaniel Lezcano out_register_subsys: 48188ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 48197e52b33bSDavid S. Miller out_register_inetpeer: 48207e52b33bSDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 4821fc66f95cSEric Dumazet out_dst_entries: 4822fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 4823433d49c3SDaniel Lezcano out_kmem_cache: 4824f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 4825433d49c3SDaniel Lezcano goto out; 48261da177e4SLinus Torvalds } 48271da177e4SLinus Torvalds 48281da177e4SLinus Torvalds void ip6_route_cleanup(void) 48291da177e4SLinus Torvalds { 48308ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 4831d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 4832101367c2SThomas Graf fib6_rules_cleanup(); 48331da177e4SLinus Torvalds xfrm6_fini(); 48341da177e4SLinus Torvalds fib6_gc_cleanup(); 4835c3426b47SDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 48368ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 483741bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 4838f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 48391da177e4SLinus Torvalds } 4840