11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Linux INET6 implementation 31da177e4SLinus Torvalds * FIB front-end. 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Authors: 61da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 91da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 101da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 111da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 121da177e4SLinus Torvalds */ 131da177e4SLinus Torvalds 141da177e4SLinus Torvalds /* Changes: 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI 171da177e4SLinus Torvalds * reworked default router selection. 181da177e4SLinus Torvalds * - respect outgoing interface 191da177e4SLinus Torvalds * - select from (probably) reachable routers (i.e. 201da177e4SLinus Torvalds * routers in REACHABLE, STALE, DELAY or PROBE states). 211da177e4SLinus Torvalds * - always select the same router if it is (probably) 221da177e4SLinus Torvalds * reachable. otherwise, round-robin the list. 23c0bece9fSYOSHIFUJI Hideaki * Ville Nuorvala 24c0bece9fSYOSHIFUJI Hideaki * Fixed routing subtrees. 251da177e4SLinus Torvalds */ 261da177e4SLinus Torvalds 27f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt 28f3213831SJoe Perches 294fc268d2SRandy Dunlap #include <linux/capability.h> 301da177e4SLinus Torvalds #include <linux/errno.h> 31bc3b2d7fSPaul Gortmaker #include <linux/export.h> 321da177e4SLinus Torvalds #include <linux/types.h> 331da177e4SLinus Torvalds #include <linux/times.h> 341da177e4SLinus Torvalds #include <linux/socket.h> 351da177e4SLinus Torvalds #include <linux/sockios.h> 361da177e4SLinus Torvalds #include <linux/net.h> 371da177e4SLinus Torvalds #include <linux/route.h> 381da177e4SLinus Torvalds #include <linux/netdevice.h> 391da177e4SLinus Torvalds #include <linux/in6.h> 407bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h> 411da177e4SLinus Torvalds #include <linux/init.h> 421da177e4SLinus Torvalds #include <linux/if_arp.h> 431da177e4SLinus Torvalds #include <linux/proc_fs.h> 441da177e4SLinus Torvalds #include <linux/seq_file.h> 455b7c931dSDaniel Lezcano #include <linux/nsproxy.h> 465a0e3ad6STejun Heo #include <linux/slab.h> 4735732d01SWei Wang #include <linux/jhash.h> 48457c4cbcSEric W. Biederman #include <net/net_namespace.h> 491da177e4SLinus Torvalds #include <net/snmp.h> 501da177e4SLinus Torvalds #include <net/ipv6.h> 511da177e4SLinus Torvalds #include <net/ip6_fib.h> 521da177e4SLinus Torvalds #include <net/ip6_route.h> 531da177e4SLinus Torvalds #include <net/ndisc.h> 541da177e4SLinus Torvalds #include <net/addrconf.h> 551da177e4SLinus Torvalds #include <net/tcp.h> 561da177e4SLinus Torvalds #include <linux/rtnetlink.h> 571da177e4SLinus Torvalds #include <net/dst.h> 58904af04dSJiri Benc #include <net/dst_metadata.h> 591da177e4SLinus Torvalds #include <net/xfrm.h> 608d71740cSTom Tucker #include <net/netevent.h> 6121713ebcSThomas Graf #include <net/netlink.h> 6251ebd318SNicolas Dichtel #include <net/nexthop.h> 6319e42e45SRoopa Prabhu #include <net/lwtunnel.h> 64904af04dSJiri Benc #include <net/ip_tunnels.h> 65ca254490SDavid Ahern #include <net/l3mdev.h> 66b811580dSDavid Ahern #include <trace/events/fib6.h> 671da177e4SLinus Torvalds 687c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 711da177e4SLinus Torvalds #include <linux/sysctl.h> 721da177e4SLinus Torvalds #endif 731da177e4SLinus Torvalds 74afc154e9SHannes Frederic Sowa enum rt6_nud_state { 757e980569SJiri Benc RT6_NUD_FAIL_HARD = -3, 767e980569SJiri Benc RT6_NUD_FAIL_PROBE = -2, 777e980569SJiri Benc RT6_NUD_FAIL_DO_RR = -1, 78afc154e9SHannes Frederic Sowa RT6_NUD_SUCCEED = 1 79afc154e9SHannes Frederic Sowa }; 80afc154e9SHannes Frederic Sowa 8183a09abdSMartin KaFai Lau static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort); 821da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie); 830dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst); 84ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst); 851da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *); 861da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *); 871da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *, 881da177e4SLinus Torvalds struct net_device *dev, int how); 89569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops); 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds static int ip6_pkt_discard(struct sk_buff *skb); 92ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb); 937150aedeSKamala R static int ip6_pkt_prohibit(struct sk_buff *skb); 94ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb); 951da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb); 966700c270SDavid S. Miller static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 976700c270SDavid S. Miller struct sk_buff *skb, u32 mtu); 986700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, 996700c270SDavid S. Miller struct sk_buff *skb); 1004b32b5adSMartin KaFai Lau static void rt6_dst_from_metrics_check(struct rt6_info *rt); 10152bd4c0cSNicolas Dichtel static int rt6_score_route(struct rt6_info *rt, int oif, int strict); 10216a16cd3SDavid Ahern static size_t rt6_nlmsg_size(struct rt6_info *rt); 10316a16cd3SDavid Ahern static int rt6_fill_node(struct net *net, 10416a16cd3SDavid Ahern struct sk_buff *skb, struct rt6_info *rt, 10516a16cd3SDavid Ahern struct in6_addr *dst, struct in6_addr *src, 10616a16cd3SDavid Ahern int iif, int type, u32 portid, u32 seq, 10716a16cd3SDavid Ahern unsigned int flags); 10835732d01SWei Wang static struct rt6_info *rt6_find_cached_rt(struct rt6_info *rt, 10935732d01SWei Wang struct in6_addr *daddr, 11035732d01SWei Wang struct in6_addr *saddr); 1111da177e4SLinus Torvalds 11270ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 113efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 114b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 115830218c1SDavid Ahern const struct in6_addr *gwaddr, 116830218c1SDavid Ahern struct net_device *dev, 11795c96174SEric Dumazet unsigned int pref); 118efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 119b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 120830218c1SDavid Ahern const struct in6_addr *gwaddr, 121830218c1SDavid Ahern struct net_device *dev); 12270ceb4f5SYOSHIFUJI Hideaki #endif 12370ceb4f5SYOSHIFUJI Hideaki 1248d0b94afSMartin KaFai Lau struct uncached_list { 1258d0b94afSMartin KaFai Lau spinlock_t lock; 1268d0b94afSMartin KaFai Lau struct list_head head; 1278d0b94afSMartin KaFai Lau }; 1288d0b94afSMartin KaFai Lau 1298d0b94afSMartin KaFai Lau static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list); 1308d0b94afSMartin KaFai Lau 1318d0b94afSMartin KaFai Lau static void rt6_uncached_list_add(struct rt6_info *rt) 1328d0b94afSMartin KaFai Lau { 1338d0b94afSMartin KaFai Lau struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list); 1348d0b94afSMartin KaFai Lau 1358d0b94afSMartin KaFai Lau rt->rt6i_uncached_list = ul; 1368d0b94afSMartin KaFai Lau 1378d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1388d0b94afSMartin KaFai Lau list_add_tail(&rt->rt6i_uncached, &ul->head); 1398d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1408d0b94afSMartin KaFai Lau } 1418d0b94afSMartin KaFai Lau 1428d0b94afSMartin KaFai Lau static void rt6_uncached_list_del(struct rt6_info *rt) 1438d0b94afSMartin KaFai Lau { 1448d0b94afSMartin KaFai Lau if (!list_empty(&rt->rt6i_uncached)) { 1458d0b94afSMartin KaFai Lau struct uncached_list *ul = rt->rt6i_uncached_list; 1468d0b94afSMartin KaFai Lau 1478d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1488d0b94afSMartin KaFai Lau list_del(&rt->rt6i_uncached); 1498d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1508d0b94afSMartin KaFai Lau } 1518d0b94afSMartin KaFai Lau } 1528d0b94afSMartin KaFai Lau 1538d0b94afSMartin KaFai Lau static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev) 1548d0b94afSMartin KaFai Lau { 1558d0b94afSMartin KaFai Lau struct net_device *loopback_dev = net->loopback_dev; 1568d0b94afSMartin KaFai Lau int cpu; 1578d0b94afSMartin KaFai Lau 158e332bc67SEric W. Biederman if (dev == loopback_dev) 159e332bc67SEric W. Biederman return; 160e332bc67SEric W. Biederman 1618d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 1628d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 1638d0b94afSMartin KaFai Lau struct rt6_info *rt; 1648d0b94afSMartin KaFai Lau 1658d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1668d0b94afSMartin KaFai Lau list_for_each_entry(rt, &ul->head, rt6i_uncached) { 1678d0b94afSMartin KaFai Lau struct inet6_dev *rt_idev = rt->rt6i_idev; 1688d0b94afSMartin KaFai Lau struct net_device *rt_dev = rt->dst.dev; 1698d0b94afSMartin KaFai Lau 170e332bc67SEric W. Biederman if (rt_idev->dev == dev) { 1718d0b94afSMartin KaFai Lau rt->rt6i_idev = in6_dev_get(loopback_dev); 1728d0b94afSMartin KaFai Lau in6_dev_put(rt_idev); 1738d0b94afSMartin KaFai Lau } 1748d0b94afSMartin KaFai Lau 175e332bc67SEric W. Biederman if (rt_dev == dev) { 1768d0b94afSMartin KaFai Lau rt->dst.dev = loopback_dev; 1778d0b94afSMartin KaFai Lau dev_hold(rt->dst.dev); 1788d0b94afSMartin KaFai Lau dev_put(rt_dev); 1798d0b94afSMartin KaFai Lau } 1808d0b94afSMartin KaFai Lau } 1818d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1828d0b94afSMartin KaFai Lau } 1838d0b94afSMartin KaFai Lau } 1848d0b94afSMartin KaFai Lau 185d52d3997SMartin KaFai Lau static u32 *rt6_pcpu_cow_metrics(struct rt6_info *rt) 186d52d3997SMartin KaFai Lau { 187d52d3997SMartin KaFai Lau return dst_metrics_write_ptr(rt->dst.from); 188d52d3997SMartin KaFai Lau } 189d52d3997SMartin KaFai Lau 19006582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old) 19106582540SDavid S. Miller { 19206582540SDavid S. Miller struct rt6_info *rt = (struct rt6_info *)dst; 19306582540SDavid S. Miller 194d52d3997SMartin KaFai Lau if (rt->rt6i_flags & RTF_PCPU) 195d52d3997SMartin KaFai Lau return rt6_pcpu_cow_metrics(rt); 196d52d3997SMartin KaFai Lau else if (rt->rt6i_flags & RTF_CACHE) 1974b32b5adSMartin KaFai Lau return NULL; 1984b32b5adSMartin KaFai Lau else 1993b471175SMartin KaFai Lau return dst_cow_metrics_generic(dst, old); 20006582540SDavid S. Miller } 20106582540SDavid S. Miller 202f894cbf8SDavid S. Miller static inline const void *choose_neigh_daddr(struct rt6_info *rt, 203f894cbf8SDavid S. Miller struct sk_buff *skb, 204f894cbf8SDavid S. Miller const void *daddr) 20539232973SDavid S. Miller { 20639232973SDavid S. Miller struct in6_addr *p = &rt->rt6i_gateway; 20739232973SDavid S. Miller 208a7563f34SDavid S. Miller if (!ipv6_addr_any(p)) 20939232973SDavid S. Miller return (const void *) p; 210f894cbf8SDavid S. Miller else if (skb) 211f894cbf8SDavid S. Miller return &ipv6_hdr(skb)->daddr; 21239232973SDavid S. Miller return daddr; 21339232973SDavid S. Miller } 21439232973SDavid S. Miller 215f894cbf8SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst, 216f894cbf8SDavid S. Miller struct sk_buff *skb, 217f894cbf8SDavid S. Miller const void *daddr) 218d3aaeb38SDavid S. Miller { 21939232973SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 22039232973SDavid S. Miller struct neighbour *n; 22139232973SDavid S. Miller 222f894cbf8SDavid S. Miller daddr = choose_neigh_daddr(rt, skb, daddr); 2238e022ee6SYOSHIFUJI Hideaki / 吉藤英明 n = __ipv6_neigh_lookup(dst->dev, daddr); 224f83c7790SDavid S. Miller if (n) 225f83c7790SDavid S. Miller return n; 226f83c7790SDavid S. Miller return neigh_create(&nd_tbl, daddr, dst->dev); 227f83c7790SDavid S. Miller } 228f83c7790SDavid S. Miller 22963fca65dSJulian Anastasov static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr) 23063fca65dSJulian Anastasov { 23163fca65dSJulian Anastasov struct net_device *dev = dst->dev; 23263fca65dSJulian Anastasov struct rt6_info *rt = (struct rt6_info *)dst; 23363fca65dSJulian Anastasov 23463fca65dSJulian Anastasov daddr = choose_neigh_daddr(rt, NULL, daddr); 23563fca65dSJulian Anastasov if (!daddr) 23663fca65dSJulian Anastasov return; 23763fca65dSJulian Anastasov if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 23863fca65dSJulian Anastasov return; 23963fca65dSJulian Anastasov if (ipv6_addr_is_multicast((const struct in6_addr *)daddr)) 24063fca65dSJulian Anastasov return; 24163fca65dSJulian Anastasov __ipv6_confirm_neigh(dev, daddr); 24263fca65dSJulian Anastasov } 24363fca65dSJulian Anastasov 2449a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = { 2451da177e4SLinus Torvalds .family = AF_INET6, 2461da177e4SLinus Torvalds .gc = ip6_dst_gc, 2471da177e4SLinus Torvalds .gc_thresh = 1024, 2481da177e4SLinus Torvalds .check = ip6_dst_check, 2490dbaee3bSDavid S. Miller .default_advmss = ip6_default_advmss, 250ebb762f2SSteffen Klassert .mtu = ip6_mtu, 25106582540SDavid S. Miller .cow_metrics = ipv6_cow_metrics, 2521da177e4SLinus Torvalds .destroy = ip6_dst_destroy, 2531da177e4SLinus Torvalds .ifdown = ip6_dst_ifdown, 2541da177e4SLinus Torvalds .negative_advice = ip6_negative_advice, 2551da177e4SLinus Torvalds .link_failure = ip6_link_failure, 2561da177e4SLinus Torvalds .update_pmtu = ip6_rt_update_pmtu, 2576e157b6aSDavid S. Miller .redirect = rt6_do_redirect, 2589f8955ccSEric W. Biederman .local_out = __ip6_local_out, 259d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 26063fca65dSJulian Anastasov .confirm_neigh = ip6_confirm_neigh, 2611da177e4SLinus Torvalds }; 2621da177e4SLinus Torvalds 263ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst) 264ec831ea7SRoland Dreier { 265618f9bc7SSteffen Klassert unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); 266618f9bc7SSteffen Klassert 267618f9bc7SSteffen Klassert return mtu ? : dst->dev->mtu; 268ec831ea7SRoland Dreier } 269ec831ea7SRoland Dreier 2706700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk, 2716700c270SDavid S. Miller struct sk_buff *skb, u32 mtu) 27214e50e57SDavid S. Miller { 27314e50e57SDavid S. Miller } 27414e50e57SDavid S. Miller 2756700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk, 2766700c270SDavid S. Miller struct sk_buff *skb) 277b587ee3bSDavid S. Miller { 278b587ee3bSDavid S. Miller } 279b587ee3bSDavid S. Miller 28014e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = { 28114e50e57SDavid S. Miller .family = AF_INET6, 28214e50e57SDavid S. Miller .destroy = ip6_dst_destroy, 28314e50e57SDavid S. Miller .check = ip6_dst_check, 284ebb762f2SSteffen Klassert .mtu = ip6_blackhole_mtu, 285214f45c9SEric Dumazet .default_advmss = ip6_default_advmss, 28614e50e57SDavid S. Miller .update_pmtu = ip6_rt_blackhole_update_pmtu, 287b587ee3bSDavid S. Miller .redirect = ip6_rt_blackhole_redirect, 2880a1f5962SMartin KaFai Lau .cow_metrics = dst_cow_metrics_generic, 289d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 29014e50e57SDavid S. Miller }; 29114e50e57SDavid S. Miller 29262fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = { 29314edd87dSLi RongQing [RTAX_HOPLIMIT - 1] = 0, 29462fa8a84SDavid S. Miller }; 29562fa8a84SDavid S. Miller 296fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = { 2971da177e4SLinus Torvalds .dst = { 2981da177e4SLinus Torvalds .__refcnt = ATOMIC_INIT(1), 2991da177e4SLinus Torvalds .__use = 1, 3002c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 3011da177e4SLinus Torvalds .error = -ENETUNREACH, 3021da177e4SLinus Torvalds .input = ip6_pkt_discard, 3031da177e4SLinus Torvalds .output = ip6_pkt_discard_out, 3041da177e4SLinus Torvalds }, 3051da177e4SLinus Torvalds .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 3064f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 3071da177e4SLinus Torvalds .rt6i_metric = ~(u32) 0, 3081da177e4SLinus Torvalds .rt6i_ref = ATOMIC_INIT(1), 3091da177e4SLinus Torvalds }; 3101da177e4SLinus Torvalds 311101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES 312101367c2SThomas Graf 313fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = { 314101367c2SThomas Graf .dst = { 315101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 316101367c2SThomas Graf .__use = 1, 3172c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 318101367c2SThomas Graf .error = -EACCES, 3199ce8ade0SThomas Graf .input = ip6_pkt_prohibit, 3209ce8ade0SThomas Graf .output = ip6_pkt_prohibit_out, 321101367c2SThomas Graf }, 322101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 3234f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 324101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 325101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 326101367c2SThomas Graf }; 327101367c2SThomas Graf 328fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = { 329101367c2SThomas Graf .dst = { 330101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 331101367c2SThomas Graf .__use = 1, 3322c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 333101367c2SThomas Graf .error = -EINVAL, 334352e512cSHerbert Xu .input = dst_discard, 335ede2059dSEric W. Biederman .output = dst_discard_out, 336101367c2SThomas Graf }, 337101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 3384f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 339101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 340101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 341101367c2SThomas Graf }; 342101367c2SThomas Graf 343101367c2SThomas Graf #endif 344101367c2SThomas Graf 345ebfa45f0SMartin KaFai Lau static void rt6_info_init(struct rt6_info *rt) 346ebfa45f0SMartin KaFai Lau { 347ebfa45f0SMartin KaFai Lau struct dst_entry *dst = &rt->dst; 348ebfa45f0SMartin KaFai Lau 349ebfa45f0SMartin KaFai Lau memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst)); 350ebfa45f0SMartin KaFai Lau INIT_LIST_HEAD(&rt->rt6i_siblings); 351ebfa45f0SMartin KaFai Lau INIT_LIST_HEAD(&rt->rt6i_uncached); 352ebfa45f0SMartin KaFai Lau } 353ebfa45f0SMartin KaFai Lau 3541da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */ 355d52d3997SMartin KaFai Lau static struct rt6_info *__ip6_dst_alloc(struct net *net, 356957c665fSDavid S. Miller struct net_device *dev, 357ad706862SMartin KaFai Lau int flags) 3581da177e4SLinus Torvalds { 35997bab73fSDavid S. Miller struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev, 360b2a9c0edSWei Wang 1, DST_OBSOLETE_FORCE_CHK, flags); 361cf911662SDavid S. Miller 362ebfa45f0SMartin KaFai Lau if (rt) 363ebfa45f0SMartin KaFai Lau rt6_info_init(rt); 3648104891bSSteffen Klassert 365cf911662SDavid S. Miller return rt; 3661da177e4SLinus Torvalds } 3671da177e4SLinus Torvalds 3689ab179d8SDavid Ahern struct rt6_info *ip6_dst_alloc(struct net *net, 369d52d3997SMartin KaFai Lau struct net_device *dev, 370ad706862SMartin KaFai Lau int flags) 371d52d3997SMartin KaFai Lau { 372ad706862SMartin KaFai Lau struct rt6_info *rt = __ip6_dst_alloc(net, dev, flags); 373d52d3997SMartin KaFai Lau 374d52d3997SMartin KaFai Lau if (rt) { 375d52d3997SMartin KaFai Lau rt->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, GFP_ATOMIC); 376d52d3997SMartin KaFai Lau if (rt->rt6i_pcpu) { 377d52d3997SMartin KaFai Lau int cpu; 378d52d3997SMartin KaFai Lau 379d52d3997SMartin KaFai Lau for_each_possible_cpu(cpu) { 380d52d3997SMartin KaFai Lau struct rt6_info **p; 381d52d3997SMartin KaFai Lau 382d52d3997SMartin KaFai Lau p = per_cpu_ptr(rt->rt6i_pcpu, cpu); 383d52d3997SMartin KaFai Lau /* no one shares rt */ 384d52d3997SMartin KaFai Lau *p = NULL; 385d52d3997SMartin KaFai Lau } 386d52d3997SMartin KaFai Lau } else { 387587fea74SWei Wang dst_release_immediate(&rt->dst); 388d52d3997SMartin KaFai Lau return NULL; 389d52d3997SMartin KaFai Lau } 390d52d3997SMartin KaFai Lau } 391d52d3997SMartin KaFai Lau 392d52d3997SMartin KaFai Lau return rt; 393d52d3997SMartin KaFai Lau } 3949ab179d8SDavid Ahern EXPORT_SYMBOL(ip6_dst_alloc); 395d52d3997SMartin KaFai Lau 3961da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst) 3971da177e4SLinus Torvalds { 3981da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 39935732d01SWei Wang struct rt6_exception_bucket *bucket; 400ecd98837SYOSHIFUJI Hideaki / 吉藤英明 struct dst_entry *from = dst->from; 4018d0b94afSMartin KaFai Lau struct inet6_dev *idev; 4021da177e4SLinus Torvalds 4038e2ec639SYan, Zheng dst_destroy_metrics_generic(dst); 404d52d3997SMartin KaFai Lau free_percpu(rt->rt6i_pcpu); 4058d0b94afSMartin KaFai Lau rt6_uncached_list_del(rt); 4068d0b94afSMartin KaFai Lau 4078d0b94afSMartin KaFai Lau idev = rt->rt6i_idev; 40838308473SDavid S. Miller if (idev) { 4091da177e4SLinus Torvalds rt->rt6i_idev = NULL; 4101da177e4SLinus Torvalds in6_dev_put(idev); 4111da177e4SLinus Torvalds } 41235732d01SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1); 41335732d01SWei Wang if (bucket) { 41435732d01SWei Wang rt->rt6i_exception_bucket = NULL; 41535732d01SWei Wang kfree(bucket); 41635732d01SWei Wang } 4171716a961SGao feng 418ecd98837SYOSHIFUJI Hideaki / 吉藤英明 dst->from = NULL; 419ecd98837SYOSHIFUJI Hideaki / 吉藤英明 dst_release(from); 420b3419363SDavid S. Miller } 421b3419363SDavid S. Miller 4221da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 4231da177e4SLinus Torvalds int how) 4241da177e4SLinus Torvalds { 4251da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 4261da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 4275a3e55d6SDenis V. Lunev struct net_device *loopback_dev = 428c346dca1SYOSHIFUJI Hideaki dev_net(dev)->loopback_dev; 4291da177e4SLinus Torvalds 430e5645f51SWei Wang if (idev && idev->dev != loopback_dev) { 431e5645f51SWei Wang struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev); 43238308473SDavid S. Miller if (loopback_idev) { 4331da177e4SLinus Torvalds rt->rt6i_idev = loopback_idev; 4341da177e4SLinus Torvalds in6_dev_put(idev); 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds } 43797cac082SDavid S. Miller } 4381da177e4SLinus Torvalds 4395973fb1eSMartin KaFai Lau static bool __rt6_check_expired(const struct rt6_info *rt) 4405973fb1eSMartin KaFai Lau { 4415973fb1eSMartin KaFai Lau if (rt->rt6i_flags & RTF_EXPIRES) 4425973fb1eSMartin KaFai Lau return time_after(jiffies, rt->dst.expires); 4435973fb1eSMartin KaFai Lau else 4445973fb1eSMartin KaFai Lau return false; 4455973fb1eSMartin KaFai Lau } 4465973fb1eSMartin KaFai Lau 447a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt) 4481da177e4SLinus Torvalds { 4491716a961SGao feng if (rt->rt6i_flags & RTF_EXPIRES) { 4501716a961SGao feng if (time_after(jiffies, rt->dst.expires)) 451a50feda5SEric Dumazet return true; 4521716a961SGao feng } else if (rt->dst.from) { 4531e2ea8adSXin Long return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK || 4541e2ea8adSXin Long rt6_check_expired((struct rt6_info *)rt->dst.from); 4551716a961SGao feng } 456a50feda5SEric Dumazet return false; 4571da177e4SLinus Torvalds } 4581da177e4SLinus Torvalds 45951ebd318SNicolas Dichtel static struct rt6_info *rt6_multipath_select(struct rt6_info *match, 46052bd4c0cSNicolas Dichtel struct flowi6 *fl6, int oif, 46152bd4c0cSNicolas Dichtel int strict) 46251ebd318SNicolas Dichtel { 46351ebd318SNicolas Dichtel struct rt6_info *sibling, *next_sibling; 46451ebd318SNicolas Dichtel int route_choosen; 46551ebd318SNicolas Dichtel 466b673d6ccSJakub Sitnicki /* We might have already computed the hash for ICMPv6 errors. In such 467b673d6ccSJakub Sitnicki * case it will always be non-zero. Otherwise now is the time to do it. 468b673d6ccSJakub Sitnicki */ 469b673d6ccSJakub Sitnicki if (!fl6->mp_hash) 470b673d6ccSJakub Sitnicki fl6->mp_hash = rt6_multipath_hash(fl6, NULL); 471b673d6ccSJakub Sitnicki 472b673d6ccSJakub Sitnicki route_choosen = fl6->mp_hash % (match->rt6i_nsiblings + 1); 47351ebd318SNicolas Dichtel /* Don't change the route, if route_choosen == 0 47451ebd318SNicolas Dichtel * (siblings does not include ourself) 47551ebd318SNicolas Dichtel */ 47651ebd318SNicolas Dichtel if (route_choosen) 47751ebd318SNicolas Dichtel list_for_each_entry_safe(sibling, next_sibling, 47851ebd318SNicolas Dichtel &match->rt6i_siblings, rt6i_siblings) { 47951ebd318SNicolas Dichtel route_choosen--; 48051ebd318SNicolas Dichtel if (route_choosen == 0) { 48152bd4c0cSNicolas Dichtel if (rt6_score_route(sibling, oif, strict) < 0) 48252bd4c0cSNicolas Dichtel break; 48351ebd318SNicolas Dichtel match = sibling; 48451ebd318SNicolas Dichtel break; 48551ebd318SNicolas Dichtel } 48651ebd318SNicolas Dichtel } 48751ebd318SNicolas Dichtel return match; 48851ebd318SNicolas Dichtel } 48951ebd318SNicolas Dichtel 4901da177e4SLinus Torvalds /* 491c71099acSThomas Graf * Route lookup. Any table->tb6_lock is implied. 4921da177e4SLinus Torvalds */ 4931da177e4SLinus Torvalds 4948ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net, 4958ed67789SDaniel Lezcano struct rt6_info *rt, 496b71d1d42SEric Dumazet const struct in6_addr *saddr, 4971da177e4SLinus Torvalds int oif, 498d420895eSYOSHIFUJI Hideaki int flags) 4991da177e4SLinus Torvalds { 5001da177e4SLinus Torvalds struct rt6_info *local = NULL; 5011da177e4SLinus Torvalds struct rt6_info *sprt; 5021da177e4SLinus Torvalds 503dd3abc4eSYOSHIFUJI Hideaki if (!oif && ipv6_addr_any(saddr)) 504dd3abc4eSYOSHIFUJI Hideaki goto out; 505dd3abc4eSYOSHIFUJI Hideaki 506d8d1f30bSChangli Gao for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) { 507d1918542SDavid S. Miller struct net_device *dev = sprt->dst.dev; 508dd3abc4eSYOSHIFUJI Hideaki 509dd3abc4eSYOSHIFUJI Hideaki if (oif) { 5101da177e4SLinus Torvalds if (dev->ifindex == oif) 5111da177e4SLinus Torvalds return sprt; 5121da177e4SLinus Torvalds if (dev->flags & IFF_LOOPBACK) { 51338308473SDavid S. Miller if (!sprt->rt6i_idev || 5141da177e4SLinus Torvalds sprt->rt6i_idev->dev->ifindex != oif) { 51517fb0b2bSDavid Ahern if (flags & RT6_LOOKUP_F_IFACE) 5161da177e4SLinus Torvalds continue; 51717fb0b2bSDavid Ahern if (local && 51817fb0b2bSDavid Ahern local->rt6i_idev->dev->ifindex == oif) 5191da177e4SLinus Torvalds continue; 5201da177e4SLinus Torvalds } 5211da177e4SLinus Torvalds local = sprt; 5221da177e4SLinus Torvalds } 523dd3abc4eSYOSHIFUJI Hideaki } else { 524dd3abc4eSYOSHIFUJI Hideaki if (ipv6_chk_addr(net, saddr, dev, 525dd3abc4eSYOSHIFUJI Hideaki flags & RT6_LOOKUP_F_IFACE)) 526dd3abc4eSYOSHIFUJI Hideaki return sprt; 527dd3abc4eSYOSHIFUJI Hideaki } 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds 530dd3abc4eSYOSHIFUJI Hideaki if (oif) { 5311da177e4SLinus Torvalds if (local) 5321da177e4SLinus Torvalds return local; 5331da177e4SLinus Torvalds 534d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE) 5358ed67789SDaniel Lezcano return net->ipv6.ip6_null_entry; 5361da177e4SLinus Torvalds } 537dd3abc4eSYOSHIFUJI Hideaki out: 5381da177e4SLinus Torvalds return rt; 5391da177e4SLinus Torvalds } 5401da177e4SLinus Torvalds 54127097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 542c2f17e82SHannes Frederic Sowa struct __rt6_probe_work { 543c2f17e82SHannes Frederic Sowa struct work_struct work; 544c2f17e82SHannes Frederic Sowa struct in6_addr target; 545c2f17e82SHannes Frederic Sowa struct net_device *dev; 546c2f17e82SHannes Frederic Sowa }; 547c2f17e82SHannes Frederic Sowa 548c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w) 549c2f17e82SHannes Frederic Sowa { 550c2f17e82SHannes Frederic Sowa struct in6_addr mcaddr; 551c2f17e82SHannes Frederic Sowa struct __rt6_probe_work *work = 552c2f17e82SHannes Frederic Sowa container_of(w, struct __rt6_probe_work, work); 553c2f17e82SHannes Frederic Sowa 554c2f17e82SHannes Frederic Sowa addrconf_addr_solict_mult(&work->target, &mcaddr); 555adc176c5SErik Nordmark ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0); 556c2f17e82SHannes Frederic Sowa dev_put(work->dev); 557662f5533SMichael Büsch kfree(work); 558c2f17e82SHannes Frederic Sowa } 559c2f17e82SHannes Frederic Sowa 56027097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt) 56127097255SYOSHIFUJI Hideaki { 562990edb42SMartin KaFai Lau struct __rt6_probe_work *work; 563f2c31e32SEric Dumazet struct neighbour *neigh; 56427097255SYOSHIFUJI Hideaki /* 56527097255SYOSHIFUJI Hideaki * Okay, this does not seem to be appropriate 56627097255SYOSHIFUJI Hideaki * for now, however, we need to check if it 56727097255SYOSHIFUJI Hideaki * is really so; aka Router Reachability Probing. 56827097255SYOSHIFUJI Hideaki * 56927097255SYOSHIFUJI Hideaki * Router Reachability Probe MUST be rate-limited 57027097255SYOSHIFUJI Hideaki * to no more than one per minute. 57127097255SYOSHIFUJI Hideaki */ 5722152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (!rt || !(rt->rt6i_flags & RTF_GATEWAY)) 573fdd6681dSAmerigo Wang return; 5742152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 5752152caeaSYOSHIFUJI Hideaki / 吉藤英明 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 5762152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 5778d6c31bfSMartin KaFai Lau if (neigh->nud_state & NUD_VALID) 5788d6c31bfSMartin KaFai Lau goto out; 5798d6c31bfSMartin KaFai Lau 580990edb42SMartin KaFai Lau work = NULL; 5812152caeaSYOSHIFUJI Hideaki / 吉藤英明 write_lock(&neigh->lock); 582990edb42SMartin KaFai Lau if (!(neigh->nud_state & NUD_VALID) && 583990edb42SMartin KaFai Lau time_after(jiffies, 584990edb42SMartin KaFai Lau neigh->updated + 585990edb42SMartin KaFai Lau rt->rt6i_idev->cnf.rtr_probe_interval)) { 586c2f17e82SHannes Frederic Sowa work = kmalloc(sizeof(*work), GFP_ATOMIC); 587990edb42SMartin KaFai Lau if (work) 5887e980569SJiri Benc __neigh_set_probe_once(neigh); 589990edb42SMartin KaFai Lau } 590c2f17e82SHannes Frederic Sowa write_unlock(&neigh->lock); 591990edb42SMartin KaFai Lau } else { 592990edb42SMartin KaFai Lau work = kmalloc(sizeof(*work), GFP_ATOMIC); 593990edb42SMartin KaFai Lau } 594c2f17e82SHannes Frederic Sowa 595c2f17e82SHannes Frederic Sowa if (work) { 596c2f17e82SHannes Frederic Sowa INIT_WORK(&work->work, rt6_probe_deferred); 597c2f17e82SHannes Frederic Sowa work->target = rt->rt6i_gateway; 598c2f17e82SHannes Frederic Sowa dev_hold(rt->dst.dev); 599c2f17e82SHannes Frederic Sowa work->dev = rt->dst.dev; 600c2f17e82SHannes Frederic Sowa schedule_work(&work->work); 601c2f17e82SHannes Frederic Sowa } 602990edb42SMartin KaFai Lau 6038d6c31bfSMartin KaFai Lau out: 6042152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 605f2c31e32SEric Dumazet } 60627097255SYOSHIFUJI Hideaki #else 60727097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt) 60827097255SYOSHIFUJI Hideaki { 60927097255SYOSHIFUJI Hideaki } 61027097255SYOSHIFUJI Hideaki #endif 61127097255SYOSHIFUJI Hideaki 6121da177e4SLinus Torvalds /* 613554cfb7eSYOSHIFUJI Hideaki * Default Router Selection (RFC 2461 6.3.6) 6141da177e4SLinus Torvalds */ 615b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif) 6161da177e4SLinus Torvalds { 617d1918542SDavid S. Miller struct net_device *dev = rt->dst.dev; 618161980f4SDavid S. Miller if (!oif || dev->ifindex == oif) 619554cfb7eSYOSHIFUJI Hideaki return 2; 620161980f4SDavid S. Miller if ((dev->flags & IFF_LOOPBACK) && 621161980f4SDavid S. Miller rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif) 622161980f4SDavid S. Miller return 1; 623554cfb7eSYOSHIFUJI Hideaki return 0; 6241da177e4SLinus Torvalds } 6251da177e4SLinus Torvalds 626afc154e9SHannes Frederic Sowa static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt) 6271da177e4SLinus Torvalds { 628f2c31e32SEric Dumazet struct neighbour *neigh; 629afc154e9SHannes Frederic Sowa enum rt6_nud_state ret = RT6_NUD_FAIL_HARD; 630f2c31e32SEric Dumazet 6314d0c5911SYOSHIFUJI Hideaki if (rt->rt6i_flags & RTF_NONEXTHOP || 6324d0c5911SYOSHIFUJI Hideaki !(rt->rt6i_flags & RTF_GATEWAY)) 633afc154e9SHannes Frederic Sowa return RT6_NUD_SUCCEED; 634145a3621SYOSHIFUJI Hideaki / 吉藤英明 635145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 636145a3621SYOSHIFUJI Hideaki / 吉藤英明 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 637145a3621SYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 638145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_lock(&neigh->lock); 639554cfb7eSYOSHIFUJI Hideaki if (neigh->nud_state & NUD_VALID) 640afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 641398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 642a5a81f0bSPaul Marks else if (!(neigh->nud_state & NUD_FAILED)) 643afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 6447e980569SJiri Benc else 6457e980569SJiri Benc ret = RT6_NUD_FAIL_PROBE; 646398bcbebSYOSHIFUJI Hideaki #endif 647145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_unlock(&neigh->lock); 648afc154e9SHannes Frederic Sowa } else { 649afc154e9SHannes Frederic Sowa ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ? 6507e980569SJiri Benc RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR; 651a5a81f0bSPaul Marks } 652145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 653145a3621SYOSHIFUJI Hideaki / 吉藤英明 654a5a81f0bSPaul Marks return ret; 6551da177e4SLinus Torvalds } 6561da177e4SLinus Torvalds 657554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif, 658554cfb7eSYOSHIFUJI Hideaki int strict) 659554cfb7eSYOSHIFUJI Hideaki { 660a5a81f0bSPaul Marks int m; 6614d0c5911SYOSHIFUJI Hideaki 6624d0c5911SYOSHIFUJI Hideaki m = rt6_check_dev(rt, oif); 66377d16f45SYOSHIFUJI Hideaki if (!m && (strict & RT6_LOOKUP_F_IFACE)) 664afc154e9SHannes Frederic Sowa return RT6_NUD_FAIL_HARD; 665ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 666ebacaaa0SYOSHIFUJI Hideaki m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2; 667ebacaaa0SYOSHIFUJI Hideaki #endif 668afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) { 669afc154e9SHannes Frederic Sowa int n = rt6_check_neigh(rt); 670afc154e9SHannes Frederic Sowa if (n < 0) 671afc154e9SHannes Frederic Sowa return n; 672afc154e9SHannes Frederic Sowa } 673554cfb7eSYOSHIFUJI Hideaki return m; 674554cfb7eSYOSHIFUJI Hideaki } 675554cfb7eSYOSHIFUJI Hideaki 676f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict, 677afc154e9SHannes Frederic Sowa int *mpri, struct rt6_info *match, 678afc154e9SHannes Frederic Sowa bool *do_rr) 679554cfb7eSYOSHIFUJI Hideaki { 680554cfb7eSYOSHIFUJI Hideaki int m; 681afc154e9SHannes Frederic Sowa bool match_do_rr = false; 68235103d11SAndy Gospodarek struct inet6_dev *idev = rt->rt6i_idev; 68335103d11SAndy Gospodarek struct net_device *dev = rt->dst.dev; 68435103d11SAndy Gospodarek 68535103d11SAndy Gospodarek if (dev && !netif_carrier_ok(dev) && 686d5d32e4bSDavid Ahern idev->cnf.ignore_routes_with_linkdown && 687d5d32e4bSDavid Ahern !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE)) 68835103d11SAndy Gospodarek goto out; 689554cfb7eSYOSHIFUJI Hideaki 690554cfb7eSYOSHIFUJI Hideaki if (rt6_check_expired(rt)) 691f11e6659SDavid S. Miller goto out; 692554cfb7eSYOSHIFUJI Hideaki 693554cfb7eSYOSHIFUJI Hideaki m = rt6_score_route(rt, oif, strict); 6947e980569SJiri Benc if (m == RT6_NUD_FAIL_DO_RR) { 695afc154e9SHannes Frederic Sowa match_do_rr = true; 696afc154e9SHannes Frederic Sowa m = 0; /* lowest valid score */ 6977e980569SJiri Benc } else if (m == RT6_NUD_FAIL_HARD) { 698f11e6659SDavid S. Miller goto out; 6991da177e4SLinus Torvalds } 700f11e6659SDavid S. Miller 701afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) 702afc154e9SHannes Frederic Sowa rt6_probe(rt); 703afc154e9SHannes Frederic Sowa 7047e980569SJiri Benc /* note that m can be RT6_NUD_FAIL_PROBE at this point */ 705afc154e9SHannes Frederic Sowa if (m > *mpri) { 706afc154e9SHannes Frederic Sowa *do_rr = match_do_rr; 707afc154e9SHannes Frederic Sowa *mpri = m; 708afc154e9SHannes Frederic Sowa match = rt; 709afc154e9SHannes Frederic Sowa } 710f11e6659SDavid S. Miller out: 711f11e6659SDavid S. Miller return match; 7121da177e4SLinus Torvalds } 7131da177e4SLinus Torvalds 714f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn, 715*8d1040e8SWei Wang struct rt6_info *leaf, 716f11e6659SDavid S. Miller struct rt6_info *rr_head, 717afc154e9SHannes Frederic Sowa u32 metric, int oif, int strict, 718afc154e9SHannes Frederic Sowa bool *do_rr) 719f11e6659SDavid S. Miller { 7209fbdcfafSSteffen Klassert struct rt6_info *rt, *match, *cont; 721f11e6659SDavid S. Miller int mpri = -1; 722f11e6659SDavid S. Miller 723f11e6659SDavid S. Miller match = NULL; 7249fbdcfafSSteffen Klassert cont = NULL; 7259fbdcfafSSteffen Klassert for (rt = rr_head; rt; rt = rt->dst.rt6_next) { 7269fbdcfafSSteffen Klassert if (rt->rt6i_metric != metric) { 7279fbdcfafSSteffen Klassert cont = rt; 7289fbdcfafSSteffen Klassert break; 7299fbdcfafSSteffen Klassert } 7309fbdcfafSSteffen Klassert 731afc154e9SHannes Frederic Sowa match = find_match(rt, oif, strict, &mpri, match, do_rr); 7329fbdcfafSSteffen Klassert } 7339fbdcfafSSteffen Klassert 734*8d1040e8SWei Wang for (rt = leaf; rt && rt != rr_head; rt = rt->dst.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 7469fbdcfafSSteffen Klassert for (rt = cont; rt; rt = rt->dst.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 752*8d1040e8SWei Wang static struct rt6_info *rt6_select(struct net *net, struct fib6_node *fn, 753*8d1040e8SWei Wang int oif, int strict) 754f11e6659SDavid S. Miller { 755*8d1040e8SWei Wang struct rt6_info *leaf = fn->leaf; 756f11e6659SDavid S. Miller struct rt6_info *match, *rt0; 757afc154e9SHannes Frederic Sowa bool do_rr = false; 758f11e6659SDavid S. Miller 759*8d1040e8SWei Wang if (!leaf) 760*8d1040e8SWei Wang return net->ipv6.ip6_null_entry; 761*8d1040e8SWei Wang 762f11e6659SDavid S. Miller rt0 = fn->rr_ptr; 763f11e6659SDavid S. Miller if (!rt0) 764*8d1040e8SWei Wang fn->rr_ptr = rt0 = leaf; 765f11e6659SDavid S. Miller 766*8d1040e8SWei Wang match = find_rr_leaf(fn, leaf, rt0, rt0->rt6i_metric, oif, strict, 767afc154e9SHannes Frederic Sowa &do_rr); 768f11e6659SDavid S. Miller 769afc154e9SHannes Frederic Sowa if (do_rr) { 770d8d1f30bSChangli Gao struct rt6_info *next = rt0->dst.rt6_next; 771f11e6659SDavid S. Miller 772554cfb7eSYOSHIFUJI Hideaki /* no entries matched; do round-robin */ 773f11e6659SDavid S. Miller if (!next || next->rt6i_metric != rt0->rt6i_metric) 774*8d1040e8SWei Wang next = leaf; 775f11e6659SDavid S. Miller 776f11e6659SDavid S. Miller if (next != rt0) 777f11e6659SDavid S. Miller fn->rr_ptr = next; 778554cfb7eSYOSHIFUJI Hideaki } 779554cfb7eSYOSHIFUJI Hideaki 780a02cec21SEric Dumazet return match ? match : net->ipv6.ip6_null_entry; 7811da177e4SLinus Torvalds } 7821da177e4SLinus Torvalds 7838b9df265SMartin KaFai Lau static bool rt6_is_gw_or_nonexthop(const struct rt6_info *rt) 7848b9df265SMartin KaFai Lau { 7858b9df265SMartin KaFai Lau return (rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY)); 7868b9df265SMartin KaFai Lau } 7878b9df265SMartin KaFai Lau 78870ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 78970ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 790b71d1d42SEric Dumazet const struct in6_addr *gwaddr) 79170ceb4f5SYOSHIFUJI Hideaki { 792c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 79370ceb4f5SYOSHIFUJI Hideaki struct route_info *rinfo = (struct route_info *) opt; 79470ceb4f5SYOSHIFUJI Hideaki struct in6_addr prefix_buf, *prefix; 79570ceb4f5SYOSHIFUJI Hideaki unsigned int pref; 7964bed72e4SYOSHIFUJI Hideaki unsigned long lifetime; 79770ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt; 79870ceb4f5SYOSHIFUJI Hideaki 79970ceb4f5SYOSHIFUJI Hideaki if (len < sizeof(struct route_info)) { 80070ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 80170ceb4f5SYOSHIFUJI Hideaki } 80270ceb4f5SYOSHIFUJI Hideaki 80370ceb4f5SYOSHIFUJI Hideaki /* Sanity check for prefix_len and length */ 80470ceb4f5SYOSHIFUJI Hideaki if (rinfo->length > 3) { 80570ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 80670ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 128) { 80770ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 80870ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 64) { 80970ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 2) { 81070ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 81170ceb4f5SYOSHIFUJI Hideaki } 81270ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 0) { 81370ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 1) { 81470ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 81570ceb4f5SYOSHIFUJI Hideaki } 81670ceb4f5SYOSHIFUJI Hideaki } 81770ceb4f5SYOSHIFUJI Hideaki 81870ceb4f5SYOSHIFUJI Hideaki pref = rinfo->route_pref; 81970ceb4f5SYOSHIFUJI Hideaki if (pref == ICMPV6_ROUTER_PREF_INVALID) 8203933fc95SJens Rosenboom return -EINVAL; 82170ceb4f5SYOSHIFUJI Hideaki 8224bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); 82370ceb4f5SYOSHIFUJI Hideaki 82470ceb4f5SYOSHIFUJI Hideaki if (rinfo->length == 3) 82570ceb4f5SYOSHIFUJI Hideaki prefix = (struct in6_addr *)rinfo->prefix; 82670ceb4f5SYOSHIFUJI Hideaki else { 82770ceb4f5SYOSHIFUJI Hideaki /* this function is safe */ 82870ceb4f5SYOSHIFUJI Hideaki ipv6_addr_prefix(&prefix_buf, 82970ceb4f5SYOSHIFUJI Hideaki (struct in6_addr *)rinfo->prefix, 83070ceb4f5SYOSHIFUJI Hideaki rinfo->prefix_len); 83170ceb4f5SYOSHIFUJI Hideaki prefix = &prefix_buf; 83270ceb4f5SYOSHIFUJI Hideaki } 83370ceb4f5SYOSHIFUJI Hideaki 834f104a567SDuan Jiong if (rinfo->prefix_len == 0) 835f104a567SDuan Jiong rt = rt6_get_dflt_router(gwaddr, dev); 836f104a567SDuan Jiong else 837f104a567SDuan Jiong rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, 838830218c1SDavid Ahern gwaddr, dev); 83970ceb4f5SYOSHIFUJI Hideaki 84070ceb4f5SYOSHIFUJI Hideaki if (rt && !lifetime) { 841e0a1ad73SThomas Graf ip6_del_rt(rt); 84270ceb4f5SYOSHIFUJI Hideaki rt = NULL; 84370ceb4f5SYOSHIFUJI Hideaki } 84470ceb4f5SYOSHIFUJI Hideaki 84570ceb4f5SYOSHIFUJI Hideaki if (!rt && lifetime) 846830218c1SDavid Ahern rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, 847830218c1SDavid Ahern dev, pref); 84870ceb4f5SYOSHIFUJI Hideaki else if (rt) 84970ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags = RTF_ROUTEINFO | 85070ceb4f5SYOSHIFUJI Hideaki (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 85170ceb4f5SYOSHIFUJI Hideaki 85270ceb4f5SYOSHIFUJI Hideaki if (rt) { 8531716a961SGao feng if (!addrconf_finite_timeout(lifetime)) 8541716a961SGao feng rt6_clean_expires(rt); 8551716a961SGao feng else 8561716a961SGao feng rt6_set_expires(rt, jiffies + HZ * lifetime); 8571716a961SGao feng 85894e187c0SAmerigo Wang ip6_rt_put(rt); 85970ceb4f5SYOSHIFUJI Hideaki } 86070ceb4f5SYOSHIFUJI Hideaki return 0; 86170ceb4f5SYOSHIFUJI Hideaki } 86270ceb4f5SYOSHIFUJI Hideaki #endif 86370ceb4f5SYOSHIFUJI Hideaki 864a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn, 865a3c00e46SMartin KaFai Lau struct in6_addr *saddr) 866a3c00e46SMartin KaFai Lau { 867a3c00e46SMartin KaFai Lau struct fib6_node *pn; 868a3c00e46SMartin KaFai Lau while (1) { 869a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_TL_ROOT) 870a3c00e46SMartin KaFai Lau return NULL; 871a3c00e46SMartin KaFai Lau pn = fn->parent; 872a3c00e46SMartin KaFai Lau if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) 873a3c00e46SMartin KaFai Lau fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); 874a3c00e46SMartin KaFai Lau else 875a3c00e46SMartin KaFai Lau fn = pn; 876a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_RTINFO) 877a3c00e46SMartin KaFai Lau return fn; 878a3c00e46SMartin KaFai Lau } 879a3c00e46SMartin KaFai Lau } 880c71099acSThomas Graf 881d3843fe5SWei Wang static bool ip6_hold_safe(struct net *net, struct rt6_info **prt, 882d3843fe5SWei Wang bool null_fallback) 883d3843fe5SWei Wang { 884d3843fe5SWei Wang struct rt6_info *rt = *prt; 885d3843fe5SWei Wang 886d3843fe5SWei Wang if (dst_hold_safe(&rt->dst)) 887d3843fe5SWei Wang return true; 888d3843fe5SWei Wang if (null_fallback) { 889d3843fe5SWei Wang rt = net->ipv6.ip6_null_entry; 890d3843fe5SWei Wang dst_hold(&rt->dst); 891d3843fe5SWei Wang } else { 892d3843fe5SWei Wang rt = NULL; 893d3843fe5SWei Wang } 894d3843fe5SWei Wang *prt = rt; 895d3843fe5SWei Wang return false; 896d3843fe5SWei Wang } 897d3843fe5SWei Wang 8988ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net, 8998ed67789SDaniel Lezcano struct fib6_table *table, 9004c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 9011da177e4SLinus Torvalds { 9022b760fcfSWei Wang struct rt6_info *rt, *rt_cache; 9031da177e4SLinus Torvalds struct fib6_node *fn; 9041da177e4SLinus Torvalds 905c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 9064c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 907c71099acSThomas Graf restart: 908c71099acSThomas Graf rt = fn->leaf; 9094c9483b2SDavid S. Miller rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags); 91051ebd318SNicolas Dichtel if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0) 91152bd4c0cSNicolas Dichtel rt = rt6_multipath_select(rt, fl6, fl6->flowi6_oif, flags); 912a3c00e46SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 913a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 914a3c00e46SMartin KaFai Lau if (fn) 915a3c00e46SMartin KaFai Lau goto restart; 916a3c00e46SMartin KaFai Lau } 9172b760fcfSWei Wang /* Search through exception table */ 9182b760fcfSWei Wang rt_cache = rt6_find_cached_rt(rt, &fl6->daddr, &fl6->saddr); 9192b760fcfSWei Wang if (rt_cache) 9202b760fcfSWei Wang rt = rt_cache; 9212b760fcfSWei Wang 922d3843fe5SWei Wang if (ip6_hold_safe(net, &rt, true)) 923d3843fe5SWei Wang dst_use_noref(&rt->dst, jiffies); 924d3843fe5SWei Wang 925c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 926b811580dSDavid Ahern 927b811580dSDavid Ahern trace_fib6_table_lookup(net, rt, table->tb6_id, fl6); 928b811580dSDavid Ahern 9291da177e4SLinus Torvalds return rt; 930c71099acSThomas Graf 931c71099acSThomas Graf } 932c71099acSThomas Graf 933ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6, 934ea6e574eSFlorian Westphal int flags) 935ea6e574eSFlorian Westphal { 936ea6e574eSFlorian Westphal return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup); 937ea6e574eSFlorian Westphal } 938ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup); 939ea6e574eSFlorian Westphal 9409acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, 9419acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *saddr, int oif, int strict) 942c71099acSThomas Graf { 9434c9483b2SDavid S. Miller struct flowi6 fl6 = { 9444c9483b2SDavid S. Miller .flowi6_oif = oif, 9454c9483b2SDavid S. Miller .daddr = *daddr, 946c71099acSThomas Graf }; 947c71099acSThomas Graf struct dst_entry *dst; 94877d16f45SYOSHIFUJI Hideaki int flags = strict ? RT6_LOOKUP_F_IFACE : 0; 949c71099acSThomas Graf 950adaa70bbSThomas Graf if (saddr) { 9514c9483b2SDavid S. Miller memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 952adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 953adaa70bbSThomas Graf } 954adaa70bbSThomas Graf 9554c9483b2SDavid S. Miller dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup); 956c71099acSThomas Graf if (dst->error == 0) 957c71099acSThomas Graf return (struct rt6_info *) dst; 958c71099acSThomas Graf 959c71099acSThomas Graf dst_release(dst); 960c71099acSThomas Graf 9611da177e4SLinus Torvalds return NULL; 9621da177e4SLinus Torvalds } 9637159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup); 9647159039aSYOSHIFUJI Hideaki 965c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock. 9661cfb71eeSWei Wang * It takes new route entry, the addition fails by any reason the 9671cfb71eeSWei Wang * route is released. 9681cfb71eeSWei Wang * Caller must hold dst before calling it. 9691da177e4SLinus Torvalds */ 9701da177e4SLinus Torvalds 971e5fd387aSMichal Kubeček static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info, 972333c4301SDavid Ahern struct mx6_config *mxc, 973333c4301SDavid Ahern struct netlink_ext_ack *extack) 9741da177e4SLinus Torvalds { 9751da177e4SLinus Torvalds int err; 976c71099acSThomas Graf struct fib6_table *table; 9771da177e4SLinus Torvalds 978c71099acSThomas Graf table = rt->rt6i_table; 979c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 980333c4301SDavid Ahern err = fib6_add(&table->tb6_root, rt, info, mxc, extack); 981c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 9821da177e4SLinus Torvalds 9831da177e4SLinus Torvalds return err; 9841da177e4SLinus Torvalds } 9851da177e4SLinus Torvalds 98640e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt) 98740e22e8fSThomas Graf { 988e715b6d3SFlorian Westphal struct nl_info info = { .nl_net = dev_net(rt->dst.dev), }; 989e715b6d3SFlorian Westphal struct mx6_config mxc = { .mx = NULL, }; 990e715b6d3SFlorian Westphal 9911cfb71eeSWei Wang /* Hold dst to account for the reference from the fib6 tree */ 9921cfb71eeSWei Wang dst_hold(&rt->dst); 993333c4301SDavid Ahern return __ip6_ins_rt(rt, &info, &mxc, NULL); 99440e22e8fSThomas Graf } 99540e22e8fSThomas Graf 9964832c30dSDavid Ahern /* called with rcu_lock held */ 9974832c30dSDavid Ahern static struct net_device *ip6_rt_get_dev_rcu(struct rt6_info *rt) 9984832c30dSDavid Ahern { 9994832c30dSDavid Ahern struct net_device *dev = rt->dst.dev; 10004832c30dSDavid Ahern 10014832c30dSDavid Ahern if (rt->rt6i_flags & RTF_LOCAL) { 10024832c30dSDavid Ahern /* for copies of local routes, dst->dev needs to be the 10034832c30dSDavid Ahern * device if it is a master device, the master device if 10044832c30dSDavid Ahern * device is enslaved, and the loopback as the default 10054832c30dSDavid Ahern */ 10064832c30dSDavid Ahern if (netif_is_l3_slave(dev) && 10074832c30dSDavid Ahern !rt6_need_strict(&rt->rt6i_dst.addr)) 10084832c30dSDavid Ahern dev = l3mdev_master_dev_rcu(dev); 10094832c30dSDavid Ahern else if (!netif_is_l3_master(dev)) 10104832c30dSDavid Ahern dev = dev_net(dev)->loopback_dev; 10114832c30dSDavid Ahern /* last case is netif_is_l3_master(dev) is true in which 10124832c30dSDavid Ahern * case we want dev returned to be dev 10134832c30dSDavid Ahern */ 10144832c30dSDavid Ahern } 10154832c30dSDavid Ahern 10164832c30dSDavid Ahern return dev; 10174832c30dSDavid Ahern } 10184832c30dSDavid Ahern 10198b9df265SMartin KaFai Lau static struct rt6_info *ip6_rt_cache_alloc(struct rt6_info *ort, 102021efcfa0SEric Dumazet const struct in6_addr *daddr, 1021b71d1d42SEric Dumazet const struct in6_addr *saddr) 10221da177e4SLinus Torvalds { 10234832c30dSDavid Ahern struct net_device *dev; 10241da177e4SLinus Torvalds struct rt6_info *rt; 10251da177e4SLinus Torvalds 10261da177e4SLinus Torvalds /* 10271da177e4SLinus Torvalds * Clone the route. 10281da177e4SLinus Torvalds */ 10291da177e4SLinus Torvalds 1030d52d3997SMartin KaFai Lau if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)) 103183a09abdSMartin KaFai Lau ort = (struct rt6_info *)ort->dst.from; 10321da177e4SLinus Torvalds 10334832c30dSDavid Ahern rcu_read_lock(); 10344832c30dSDavid Ahern dev = ip6_rt_get_dev_rcu(ort); 10354832c30dSDavid Ahern rt = __ip6_dst_alloc(dev_net(dev), dev, 0); 10364832c30dSDavid Ahern rcu_read_unlock(); 103783a09abdSMartin KaFai Lau if (!rt) 103883a09abdSMartin KaFai Lau return NULL; 103983a09abdSMartin KaFai Lau 104083a09abdSMartin KaFai Lau ip6_rt_copy_init(rt, ort); 10418b9df265SMartin KaFai Lau rt->rt6i_flags |= RTF_CACHE; 104283a09abdSMartin KaFai Lau rt->rt6i_metric = 0; 104383a09abdSMartin KaFai Lau rt->dst.flags |= DST_HOST; 104483a09abdSMartin KaFai Lau rt->rt6i_dst.addr = *daddr; 104583a09abdSMartin KaFai Lau rt->rt6i_dst.plen = 128; 10468b9df265SMartin KaFai Lau 10478b9df265SMartin KaFai Lau if (!rt6_is_gw_or_nonexthop(ort)) { 1048bb3c3686SDavid S. Miller if (ort->rt6i_dst.plen != 128 && 104921efcfa0SEric Dumazet ipv6_addr_equal(&ort->rt6i_dst.addr, daddr)) 105058c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 10511da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 10521da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 10534e3fd7a0SAlexey Dobriyan rt->rt6i_src.addr = *saddr; 10541da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 10551da177e4SLinus Torvalds } 10561da177e4SLinus Torvalds #endif 105795a9a5baSYOSHIFUJI Hideaki } 105895a9a5baSYOSHIFUJI Hideaki 1059299d9939SYOSHIFUJI Hideaki return rt; 1060299d9939SYOSHIFUJI Hideaki } 1061299d9939SYOSHIFUJI Hideaki 1062d52d3997SMartin KaFai Lau static struct rt6_info *ip6_rt_pcpu_alloc(struct rt6_info *rt) 1063d52d3997SMartin KaFai Lau { 10644832c30dSDavid Ahern struct net_device *dev; 1065d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1066d52d3997SMartin KaFai Lau 10674832c30dSDavid Ahern rcu_read_lock(); 10684832c30dSDavid Ahern dev = ip6_rt_get_dev_rcu(rt); 10694832c30dSDavid Ahern pcpu_rt = __ip6_dst_alloc(dev_net(dev), dev, rt->dst.flags); 10704832c30dSDavid Ahern rcu_read_unlock(); 1071d52d3997SMartin KaFai Lau if (!pcpu_rt) 1072d52d3997SMartin KaFai Lau return NULL; 1073d52d3997SMartin KaFai Lau ip6_rt_copy_init(pcpu_rt, rt); 1074d52d3997SMartin KaFai Lau pcpu_rt->rt6i_protocol = rt->rt6i_protocol; 1075d52d3997SMartin KaFai Lau pcpu_rt->rt6i_flags |= RTF_PCPU; 1076d52d3997SMartin KaFai Lau return pcpu_rt; 1077d52d3997SMartin KaFai Lau } 1078d52d3997SMartin KaFai Lau 1079d52d3997SMartin KaFai Lau /* It should be called with read_lock_bh(&tb6_lock) acquired */ 1080d52d3997SMartin KaFai Lau static struct rt6_info *rt6_get_pcpu_route(struct rt6_info *rt) 1081d52d3997SMartin KaFai Lau { 1082a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, **p; 1083d52d3997SMartin KaFai Lau 1084d52d3997SMartin KaFai Lau p = this_cpu_ptr(rt->rt6i_pcpu); 1085d52d3997SMartin KaFai Lau pcpu_rt = *p; 1086d52d3997SMartin KaFai Lau 1087d3843fe5SWei Wang if (pcpu_rt && ip6_hold_safe(NULL, &pcpu_rt, false)) 1088a73e4195SMartin KaFai Lau rt6_dst_from_metrics_check(pcpu_rt); 1089d3843fe5SWei Wang 1090a73e4195SMartin KaFai Lau return pcpu_rt; 1091a73e4195SMartin KaFai Lau } 1092a73e4195SMartin KaFai Lau 1093a73e4195SMartin KaFai Lau static struct rt6_info *rt6_make_pcpu_route(struct rt6_info *rt) 1094a73e4195SMartin KaFai Lau { 1095a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, *prev, **p; 1096d52d3997SMartin KaFai Lau 1097d52d3997SMartin KaFai Lau pcpu_rt = ip6_rt_pcpu_alloc(rt); 1098d52d3997SMartin KaFai Lau if (!pcpu_rt) { 1099d52d3997SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 1100d52d3997SMartin KaFai Lau 11019c7370a1SMartin KaFai Lau dst_hold(&net->ipv6.ip6_null_entry->dst); 11029c7370a1SMartin KaFai Lau return net->ipv6.ip6_null_entry; 1103d52d3997SMartin KaFai Lau } 1104d52d3997SMartin KaFai Lau 1105a94b9367SWei Wang dst_hold(&pcpu_rt->dst); 1106a73e4195SMartin KaFai Lau p = this_cpu_ptr(rt->rt6i_pcpu); 1107d52d3997SMartin KaFai Lau prev = cmpxchg(p, NULL, pcpu_rt); 1108d52d3997SMartin KaFai Lau if (prev) { 1109d52d3997SMartin KaFai Lau /* If someone did it before us, return prev instead */ 1110a94b9367SWei Wang /* release refcnt taken by ip6_rt_pcpu_alloc() */ 1111587fea74SWei Wang dst_release_immediate(&pcpu_rt->dst); 1112a94b9367SWei Wang /* release refcnt taken by above dst_hold() */ 1113a94b9367SWei Wang dst_release_immediate(&pcpu_rt->dst); 1114a94b9367SWei Wang dst_hold(&prev->dst); 1115d52d3997SMartin KaFai Lau pcpu_rt = prev; 1116d52d3997SMartin KaFai Lau } 1117a94b9367SWei Wang 1118d52d3997SMartin KaFai Lau rt6_dst_from_metrics_check(pcpu_rt); 1119d52d3997SMartin KaFai Lau return pcpu_rt; 1120d52d3997SMartin KaFai Lau } 1121d52d3997SMartin KaFai Lau 112235732d01SWei Wang /* exception hash table implementation 112335732d01SWei Wang */ 112435732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock); 112535732d01SWei Wang 112635732d01SWei Wang /* Remove rt6_ex from hash table and free the memory 112735732d01SWei Wang * Caller must hold rt6_exception_lock 112835732d01SWei Wang */ 112935732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket, 113035732d01SWei Wang struct rt6_exception *rt6_ex) 113135732d01SWei Wang { 113235732d01SWei Wang if (!bucket || !rt6_ex) 113335732d01SWei Wang return; 113435732d01SWei Wang rt6_ex->rt6i->rt6i_node = NULL; 113535732d01SWei Wang hlist_del_rcu(&rt6_ex->hlist); 113635732d01SWei Wang rt6_release(rt6_ex->rt6i); 113735732d01SWei Wang kfree_rcu(rt6_ex, rcu); 113835732d01SWei Wang WARN_ON_ONCE(!bucket->depth); 113935732d01SWei Wang bucket->depth--; 114035732d01SWei Wang } 114135732d01SWei Wang 114235732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory 114335732d01SWei Wang * Caller must hold rt6_exception_lock 114435732d01SWei Wang */ 114535732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket) 114635732d01SWei Wang { 114735732d01SWei Wang struct rt6_exception *rt6_ex, *oldest = NULL; 114835732d01SWei Wang 114935732d01SWei Wang if (!bucket) 115035732d01SWei Wang return; 115135732d01SWei Wang 115235732d01SWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 115335732d01SWei Wang if (!oldest || time_before(rt6_ex->stamp, oldest->stamp)) 115435732d01SWei Wang oldest = rt6_ex; 115535732d01SWei Wang } 115635732d01SWei Wang rt6_remove_exception(bucket, oldest); 115735732d01SWei Wang } 115835732d01SWei Wang 115935732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst, 116035732d01SWei Wang const struct in6_addr *src) 116135732d01SWei Wang { 116235732d01SWei Wang static u32 seed __read_mostly; 116335732d01SWei Wang u32 val; 116435732d01SWei Wang 116535732d01SWei Wang net_get_random_once(&seed, sizeof(seed)); 116635732d01SWei Wang val = jhash(dst, sizeof(*dst), seed); 116735732d01SWei Wang 116835732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 116935732d01SWei Wang if (src) 117035732d01SWei Wang val = jhash(src, sizeof(*src), val); 117135732d01SWei Wang #endif 117235732d01SWei Wang return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT); 117335732d01SWei Wang } 117435732d01SWei Wang 117535732d01SWei Wang /* Helper function to find the cached rt in the hash table 117635732d01SWei Wang * and update bucket pointer to point to the bucket for this 117735732d01SWei Wang * (daddr, saddr) pair 117835732d01SWei Wang * Caller must hold rt6_exception_lock 117935732d01SWei Wang */ 118035732d01SWei Wang static struct rt6_exception * 118135732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket, 118235732d01SWei Wang const struct in6_addr *daddr, 118335732d01SWei Wang const struct in6_addr *saddr) 118435732d01SWei Wang { 118535732d01SWei Wang struct rt6_exception *rt6_ex; 118635732d01SWei Wang u32 hval; 118735732d01SWei Wang 118835732d01SWei Wang if (!(*bucket) || !daddr) 118935732d01SWei Wang return NULL; 119035732d01SWei Wang 119135732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 119235732d01SWei Wang *bucket += hval; 119335732d01SWei Wang 119435732d01SWei Wang hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) { 119535732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 119635732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 119735732d01SWei Wang 119835732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 119935732d01SWei Wang if (matched && saddr) 120035732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 120135732d01SWei Wang #endif 120235732d01SWei Wang if (matched) 120335732d01SWei Wang return rt6_ex; 120435732d01SWei Wang } 120535732d01SWei Wang return NULL; 120635732d01SWei Wang } 120735732d01SWei Wang 120835732d01SWei Wang /* Helper function to find the cached rt in the hash table 120935732d01SWei Wang * and update bucket pointer to point to the bucket for this 121035732d01SWei Wang * (daddr, saddr) pair 121135732d01SWei Wang * Caller must hold rcu_read_lock() 121235732d01SWei Wang */ 121335732d01SWei Wang static struct rt6_exception * 121435732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket, 121535732d01SWei Wang const struct in6_addr *daddr, 121635732d01SWei Wang const struct in6_addr *saddr) 121735732d01SWei Wang { 121835732d01SWei Wang struct rt6_exception *rt6_ex; 121935732d01SWei Wang u32 hval; 122035732d01SWei Wang 122135732d01SWei Wang WARN_ON_ONCE(!rcu_read_lock_held()); 122235732d01SWei Wang 122335732d01SWei Wang if (!(*bucket) || !daddr) 122435732d01SWei Wang return NULL; 122535732d01SWei Wang 122635732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 122735732d01SWei Wang *bucket += hval; 122835732d01SWei Wang 122935732d01SWei Wang hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) { 123035732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 123135732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 123235732d01SWei Wang 123335732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 123435732d01SWei Wang if (matched && saddr) 123535732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 123635732d01SWei Wang #endif 123735732d01SWei Wang if (matched) 123835732d01SWei Wang return rt6_ex; 123935732d01SWei Wang } 124035732d01SWei Wang return NULL; 124135732d01SWei Wang } 124235732d01SWei Wang 124335732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt, 124435732d01SWei Wang struct rt6_info *ort) 124535732d01SWei Wang { 124635732d01SWei Wang struct rt6_exception_bucket *bucket; 124735732d01SWei Wang struct in6_addr *src_key = NULL; 124835732d01SWei Wang struct rt6_exception *rt6_ex; 124935732d01SWei Wang int err = 0; 125035732d01SWei Wang 125135732d01SWei Wang /* ort can't be a cache or pcpu route */ 125235732d01SWei Wang if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)) 125335732d01SWei Wang ort = (struct rt6_info *)ort->dst.from; 125435732d01SWei Wang WARN_ON_ONCE(ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)); 125535732d01SWei Wang 125635732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 125735732d01SWei Wang 125835732d01SWei Wang if (ort->exception_bucket_flushed) { 125935732d01SWei Wang err = -EINVAL; 126035732d01SWei Wang goto out; 126135732d01SWei Wang } 126235732d01SWei Wang 126335732d01SWei Wang bucket = rcu_dereference_protected(ort->rt6i_exception_bucket, 126435732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 126535732d01SWei Wang if (!bucket) { 126635732d01SWei Wang bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket), 126735732d01SWei Wang GFP_ATOMIC); 126835732d01SWei Wang if (!bucket) { 126935732d01SWei Wang err = -ENOMEM; 127035732d01SWei Wang goto out; 127135732d01SWei Wang } 127235732d01SWei Wang rcu_assign_pointer(ort->rt6i_exception_bucket, bucket); 127335732d01SWei Wang } 127435732d01SWei Wang 127535732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 127635732d01SWei Wang /* rt6i_src.plen != 0 indicates ort is in subtree 127735732d01SWei Wang * and exception table is indexed by a hash of 127835732d01SWei Wang * both rt6i_dst and rt6i_src. 127935732d01SWei Wang * Otherwise, the exception table is indexed by 128035732d01SWei Wang * a hash of only rt6i_dst. 128135732d01SWei Wang */ 128235732d01SWei Wang if (ort->rt6i_src.plen) 128335732d01SWei Wang src_key = &nrt->rt6i_src.addr; 128435732d01SWei Wang #endif 128560006a48SWei Wang 128660006a48SWei Wang /* Update rt6i_prefsrc as it could be changed 128760006a48SWei Wang * in rt6_remove_prefsrc() 128860006a48SWei Wang */ 128960006a48SWei Wang nrt->rt6i_prefsrc = ort->rt6i_prefsrc; 1290f5bbe7eeSWei Wang /* rt6_mtu_change() might lower mtu on ort. 1291f5bbe7eeSWei Wang * Only insert this exception route if its mtu 1292f5bbe7eeSWei Wang * is less than ort's mtu value. 1293f5bbe7eeSWei Wang */ 1294f5bbe7eeSWei Wang if (nrt->rt6i_pmtu >= dst_mtu(&ort->dst)) { 1295f5bbe7eeSWei Wang err = -EINVAL; 1296f5bbe7eeSWei Wang goto out; 1297f5bbe7eeSWei Wang } 129860006a48SWei Wang 129935732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr, 130035732d01SWei Wang src_key); 130135732d01SWei Wang if (rt6_ex) 130235732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 130335732d01SWei Wang 130435732d01SWei Wang rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC); 130535732d01SWei Wang if (!rt6_ex) { 130635732d01SWei Wang err = -ENOMEM; 130735732d01SWei Wang goto out; 130835732d01SWei Wang } 130935732d01SWei Wang rt6_ex->rt6i = nrt; 131035732d01SWei Wang rt6_ex->stamp = jiffies; 131135732d01SWei Wang atomic_inc(&nrt->rt6i_ref); 131235732d01SWei Wang nrt->rt6i_node = ort->rt6i_node; 131335732d01SWei Wang hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain); 131435732d01SWei Wang bucket->depth++; 131535732d01SWei Wang 131635732d01SWei Wang if (bucket->depth > FIB6_MAX_DEPTH) 131735732d01SWei Wang rt6_exception_remove_oldest(bucket); 131835732d01SWei Wang 131935732d01SWei Wang out: 132035732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 132135732d01SWei Wang 132235732d01SWei Wang /* Update fn->fn_sernum to invalidate all cached dst */ 132335732d01SWei Wang if (!err) 132435732d01SWei Wang fib6_update_sernum(ort); 132535732d01SWei Wang 132635732d01SWei Wang return err; 132735732d01SWei Wang } 132835732d01SWei Wang 132935732d01SWei Wang void rt6_flush_exceptions(struct rt6_info *rt) 133035732d01SWei Wang { 133135732d01SWei Wang struct rt6_exception_bucket *bucket; 133235732d01SWei Wang struct rt6_exception *rt6_ex; 133335732d01SWei Wang struct hlist_node *tmp; 133435732d01SWei Wang int i; 133535732d01SWei Wang 133635732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 133735732d01SWei Wang /* Prevent rt6_insert_exception() to recreate the bucket list */ 133835732d01SWei Wang rt->exception_bucket_flushed = 1; 133935732d01SWei Wang 134035732d01SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 134135732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 134235732d01SWei Wang if (!bucket) 134335732d01SWei Wang goto out; 134435732d01SWei Wang 134535732d01SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 134635732d01SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) 134735732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 134835732d01SWei Wang WARN_ON_ONCE(bucket->depth); 134935732d01SWei Wang bucket++; 135035732d01SWei Wang } 135135732d01SWei Wang 135235732d01SWei Wang out: 135335732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 135435732d01SWei Wang } 135535732d01SWei Wang 135635732d01SWei Wang /* Find cached rt in the hash table inside passed in rt 135735732d01SWei Wang * Caller has to hold rcu_read_lock() 135835732d01SWei Wang */ 135935732d01SWei Wang static struct rt6_info *rt6_find_cached_rt(struct rt6_info *rt, 136035732d01SWei Wang struct in6_addr *daddr, 136135732d01SWei Wang struct in6_addr *saddr) 136235732d01SWei Wang { 136335732d01SWei Wang struct rt6_exception_bucket *bucket; 136435732d01SWei Wang struct in6_addr *src_key = NULL; 136535732d01SWei Wang struct rt6_exception *rt6_ex; 136635732d01SWei Wang struct rt6_info *res = NULL; 136735732d01SWei Wang 136835732d01SWei Wang bucket = rcu_dereference(rt->rt6i_exception_bucket); 136935732d01SWei Wang 137035732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 137135732d01SWei Wang /* rt6i_src.plen != 0 indicates rt is in subtree 137235732d01SWei Wang * and exception table is indexed by a hash of 137335732d01SWei Wang * both rt6i_dst and rt6i_src. 137435732d01SWei Wang * Otherwise, the exception table is indexed by 137535732d01SWei Wang * a hash of only rt6i_dst. 137635732d01SWei Wang */ 137735732d01SWei Wang if (rt->rt6i_src.plen) 137835732d01SWei Wang src_key = saddr; 137935732d01SWei Wang #endif 138035732d01SWei Wang rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key); 138135732d01SWei Wang 138235732d01SWei Wang if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i)) 138335732d01SWei Wang res = rt6_ex->rt6i; 138435732d01SWei Wang 138535732d01SWei Wang return res; 138635732d01SWei Wang } 138735732d01SWei Wang 138835732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */ 138935732d01SWei Wang int rt6_remove_exception_rt(struct rt6_info *rt) 139035732d01SWei Wang { 139135732d01SWei Wang struct rt6_info *from = (struct rt6_info *)rt->dst.from; 139235732d01SWei Wang struct rt6_exception_bucket *bucket; 139335732d01SWei Wang struct in6_addr *src_key = NULL; 139435732d01SWei Wang struct rt6_exception *rt6_ex; 139535732d01SWei Wang int err; 139635732d01SWei Wang 139735732d01SWei Wang if (!from || 139835732d01SWei Wang !(rt->rt6i_flags | RTF_CACHE)) 139935732d01SWei Wang return -EINVAL; 140035732d01SWei Wang 140135732d01SWei Wang if (!rcu_access_pointer(from->rt6i_exception_bucket)) 140235732d01SWei Wang return -ENOENT; 140335732d01SWei Wang 140435732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 140535732d01SWei Wang bucket = rcu_dereference_protected(from->rt6i_exception_bucket, 140635732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 140735732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 140835732d01SWei Wang /* rt6i_src.plen != 0 indicates 'from' is in subtree 140935732d01SWei Wang * and exception table is indexed by a hash of 141035732d01SWei Wang * both rt6i_dst and rt6i_src. 141135732d01SWei Wang * Otherwise, the exception table is indexed by 141235732d01SWei Wang * a hash of only rt6i_dst. 141335732d01SWei Wang */ 141435732d01SWei Wang if (from->rt6i_src.plen) 141535732d01SWei Wang src_key = &rt->rt6i_src.addr; 141635732d01SWei Wang #endif 141735732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, 141835732d01SWei Wang &rt->rt6i_dst.addr, 141935732d01SWei Wang src_key); 142035732d01SWei Wang if (rt6_ex) { 142135732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 142235732d01SWei Wang err = 0; 142335732d01SWei Wang } else { 142435732d01SWei Wang err = -ENOENT; 142535732d01SWei Wang } 142635732d01SWei Wang 142735732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 142835732d01SWei Wang return err; 142935732d01SWei Wang } 143035732d01SWei Wang 143135732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and 143235732d01SWei Wang * refresh its stamp 143335732d01SWei Wang */ 143435732d01SWei Wang static void rt6_update_exception_stamp_rt(struct rt6_info *rt) 143535732d01SWei Wang { 143635732d01SWei Wang struct rt6_info *from = (struct rt6_info *)rt->dst.from; 143735732d01SWei Wang struct rt6_exception_bucket *bucket; 143835732d01SWei Wang struct in6_addr *src_key = NULL; 143935732d01SWei Wang struct rt6_exception *rt6_ex; 144035732d01SWei Wang 144135732d01SWei Wang if (!from || 144235732d01SWei Wang !(rt->rt6i_flags | RTF_CACHE)) 144335732d01SWei Wang return; 144435732d01SWei Wang 144535732d01SWei Wang rcu_read_lock(); 144635732d01SWei Wang bucket = rcu_dereference(from->rt6i_exception_bucket); 144735732d01SWei Wang 144835732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 144935732d01SWei Wang /* rt6i_src.plen != 0 indicates 'from' is in subtree 145035732d01SWei Wang * and exception table is indexed by a hash of 145135732d01SWei Wang * both rt6i_dst and rt6i_src. 145235732d01SWei Wang * Otherwise, the exception table is indexed by 145335732d01SWei Wang * a hash of only rt6i_dst. 145435732d01SWei Wang */ 145535732d01SWei Wang if (from->rt6i_src.plen) 145635732d01SWei Wang src_key = &rt->rt6i_src.addr; 145735732d01SWei Wang #endif 145835732d01SWei Wang rt6_ex = __rt6_find_exception_rcu(&bucket, 145935732d01SWei Wang &rt->rt6i_dst.addr, 146035732d01SWei Wang src_key); 146135732d01SWei Wang if (rt6_ex) 146235732d01SWei Wang rt6_ex->stamp = jiffies; 146335732d01SWei Wang 146435732d01SWei Wang rcu_read_unlock(); 146535732d01SWei Wang } 146635732d01SWei Wang 146760006a48SWei Wang static void rt6_exceptions_remove_prefsrc(struct rt6_info *rt) 146860006a48SWei Wang { 146960006a48SWei Wang struct rt6_exception_bucket *bucket; 147060006a48SWei Wang struct rt6_exception *rt6_ex; 147160006a48SWei Wang int i; 147260006a48SWei Wang 147360006a48SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 147460006a48SWei Wang lockdep_is_held(&rt6_exception_lock)); 147560006a48SWei Wang 147660006a48SWei Wang if (bucket) { 147760006a48SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 147860006a48SWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 147960006a48SWei Wang rt6_ex->rt6i->rt6i_prefsrc.plen = 0; 148060006a48SWei Wang } 148160006a48SWei Wang bucket++; 148260006a48SWei Wang } 148360006a48SWei Wang } 148460006a48SWei Wang } 148560006a48SWei Wang 1486f5bbe7eeSWei Wang static void rt6_exceptions_update_pmtu(struct rt6_info *rt, int mtu) 1487f5bbe7eeSWei Wang { 1488f5bbe7eeSWei Wang struct rt6_exception_bucket *bucket; 1489f5bbe7eeSWei Wang struct rt6_exception *rt6_ex; 1490f5bbe7eeSWei Wang int i; 1491f5bbe7eeSWei Wang 1492f5bbe7eeSWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1493f5bbe7eeSWei Wang lockdep_is_held(&rt6_exception_lock)); 1494f5bbe7eeSWei Wang 1495f5bbe7eeSWei Wang if (bucket) { 1496f5bbe7eeSWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1497f5bbe7eeSWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 1498f5bbe7eeSWei Wang struct rt6_info *entry = rt6_ex->rt6i; 1499f5bbe7eeSWei Wang /* For RTF_CACHE with rt6i_pmtu == 0 1500f5bbe7eeSWei Wang * (i.e. a redirected route), 1501f5bbe7eeSWei Wang * the metrics of its rt->dst.from has already 1502f5bbe7eeSWei Wang * been updated. 1503f5bbe7eeSWei Wang */ 1504f5bbe7eeSWei Wang if (entry->rt6i_pmtu && entry->rt6i_pmtu > mtu) 1505f5bbe7eeSWei Wang entry->rt6i_pmtu = mtu; 1506f5bbe7eeSWei Wang } 1507f5bbe7eeSWei Wang bucket++; 1508f5bbe7eeSWei Wang } 1509f5bbe7eeSWei Wang } 1510f5bbe7eeSWei Wang } 1511f5bbe7eeSWei Wang 1512b16cb459SWei Wang #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) 1513b16cb459SWei Wang 1514b16cb459SWei Wang static void rt6_exceptions_clean_tohost(struct rt6_info *rt, 1515b16cb459SWei Wang struct in6_addr *gateway) 1516b16cb459SWei Wang { 1517b16cb459SWei Wang struct rt6_exception_bucket *bucket; 1518b16cb459SWei Wang struct rt6_exception *rt6_ex; 1519b16cb459SWei Wang struct hlist_node *tmp; 1520b16cb459SWei Wang int i; 1521b16cb459SWei Wang 1522b16cb459SWei Wang if (!rcu_access_pointer(rt->rt6i_exception_bucket)) 1523b16cb459SWei Wang return; 1524b16cb459SWei Wang 1525b16cb459SWei Wang spin_lock_bh(&rt6_exception_lock); 1526b16cb459SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1527b16cb459SWei Wang lockdep_is_held(&rt6_exception_lock)); 1528b16cb459SWei Wang 1529b16cb459SWei Wang if (bucket) { 1530b16cb459SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1531b16cb459SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, 1532b16cb459SWei Wang &bucket->chain, hlist) { 1533b16cb459SWei Wang struct rt6_info *entry = rt6_ex->rt6i; 1534b16cb459SWei Wang 1535b16cb459SWei Wang if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) == 1536b16cb459SWei Wang RTF_CACHE_GATEWAY && 1537b16cb459SWei Wang ipv6_addr_equal(gateway, 1538b16cb459SWei Wang &entry->rt6i_gateway)) { 1539b16cb459SWei Wang rt6_remove_exception(bucket, rt6_ex); 1540b16cb459SWei Wang } 1541b16cb459SWei Wang } 1542b16cb459SWei Wang bucket++; 1543b16cb459SWei Wang } 1544b16cb459SWei Wang } 1545b16cb459SWei Wang 1546b16cb459SWei Wang spin_unlock_bh(&rt6_exception_lock); 1547b16cb459SWei Wang } 1548b16cb459SWei Wang 1549c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket, 1550c757faa8SWei Wang struct rt6_exception *rt6_ex, 1551c757faa8SWei Wang struct fib6_gc_args *gc_args, 1552c757faa8SWei Wang unsigned long now) 1553c757faa8SWei Wang { 1554c757faa8SWei Wang struct rt6_info *rt = rt6_ex->rt6i; 1555c757faa8SWei Wang 1556c757faa8SWei Wang if (atomic_read(&rt->dst.__refcnt) == 1 && 1557c757faa8SWei Wang time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) { 1558c757faa8SWei Wang RT6_TRACE("aging clone %p\n", rt); 1559c757faa8SWei Wang rt6_remove_exception(bucket, rt6_ex); 1560c757faa8SWei Wang return; 1561c757faa8SWei Wang } else if (rt->rt6i_flags & RTF_GATEWAY) { 1562c757faa8SWei Wang struct neighbour *neigh; 1563c757faa8SWei Wang __u8 neigh_flags = 0; 1564c757faa8SWei Wang 1565c757faa8SWei Wang neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway); 1566c757faa8SWei Wang if (neigh) { 1567c757faa8SWei Wang neigh_flags = neigh->flags; 1568c757faa8SWei Wang neigh_release(neigh); 1569c757faa8SWei Wang } 1570c757faa8SWei Wang if (!(neigh_flags & NTF_ROUTER)) { 1571c757faa8SWei Wang RT6_TRACE("purging route %p via non-router but gateway\n", 1572c757faa8SWei Wang rt); 1573c757faa8SWei Wang rt6_remove_exception(bucket, rt6_ex); 1574c757faa8SWei Wang return; 1575c757faa8SWei Wang } 1576c757faa8SWei Wang } 1577c757faa8SWei Wang gc_args->more++; 1578c757faa8SWei Wang } 1579c757faa8SWei Wang 1580c757faa8SWei Wang void rt6_age_exceptions(struct rt6_info *rt, 1581c757faa8SWei Wang struct fib6_gc_args *gc_args, 1582c757faa8SWei Wang unsigned long now) 1583c757faa8SWei Wang { 1584c757faa8SWei Wang struct rt6_exception_bucket *bucket; 1585c757faa8SWei Wang struct rt6_exception *rt6_ex; 1586c757faa8SWei Wang struct hlist_node *tmp; 1587c757faa8SWei Wang int i; 1588c757faa8SWei Wang 1589c757faa8SWei Wang if (!rcu_access_pointer(rt->rt6i_exception_bucket)) 1590c757faa8SWei Wang return; 1591c757faa8SWei Wang 1592c757faa8SWei Wang spin_lock_bh(&rt6_exception_lock); 1593c757faa8SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1594c757faa8SWei Wang lockdep_is_held(&rt6_exception_lock)); 1595c757faa8SWei Wang 1596c757faa8SWei Wang if (bucket) { 1597c757faa8SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1598c757faa8SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, 1599c757faa8SWei Wang &bucket->chain, hlist) { 1600c757faa8SWei Wang rt6_age_examine_exception(bucket, rt6_ex, 1601c757faa8SWei Wang gc_args, now); 1602c757faa8SWei Wang } 1603c757faa8SWei Wang bucket++; 1604c757faa8SWei Wang } 1605c757faa8SWei Wang } 1606c757faa8SWei Wang spin_unlock_bh(&rt6_exception_lock); 1607c757faa8SWei Wang } 1608c757faa8SWei Wang 16099ff74384SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, 16109ff74384SDavid Ahern int oif, struct flowi6 *fl6, int flags) 16111da177e4SLinus Torvalds { 1612367efcb9SMartin KaFai Lau struct fib6_node *fn, *saved_fn; 16132b760fcfSWei Wang struct rt6_info *rt, *rt_cache; 1614c71099acSThomas Graf int strict = 0; 16151da177e4SLinus Torvalds 161677d16f45SYOSHIFUJI Hideaki strict |= flags & RT6_LOOKUP_F_IFACE; 1617d5d32e4bSDavid Ahern strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE; 1618367efcb9SMartin KaFai Lau if (net->ipv6.devconf_all->forwarding == 0) 1619367efcb9SMartin KaFai Lau strict |= RT6_LOOKUP_F_REACHABLE; 16201da177e4SLinus Torvalds 1621c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 16221da177e4SLinus Torvalds 16234c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1624367efcb9SMartin KaFai Lau saved_fn = fn; 16251da177e4SLinus Torvalds 1626ca254490SDavid Ahern if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) 1627ca254490SDavid Ahern oif = 0; 1628ca254490SDavid Ahern 1629a3c00e46SMartin KaFai Lau redo_rt6_select: 1630*8d1040e8SWei Wang rt = rt6_select(net, fn, oif, strict); 163152bd4c0cSNicolas Dichtel if (rt->rt6i_nsiblings) 1632367efcb9SMartin KaFai Lau rt = rt6_multipath_select(rt, fl6, oif, strict); 1633a3c00e46SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 1634a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1635a3c00e46SMartin KaFai Lau if (fn) 1636a3c00e46SMartin KaFai Lau goto redo_rt6_select; 1637367efcb9SMartin KaFai Lau else if (strict & RT6_LOOKUP_F_REACHABLE) { 1638367efcb9SMartin KaFai Lau /* also consider unreachable route */ 1639367efcb9SMartin KaFai Lau strict &= ~RT6_LOOKUP_F_REACHABLE; 1640367efcb9SMartin KaFai Lau fn = saved_fn; 1641367efcb9SMartin KaFai Lau goto redo_rt6_select; 1642367efcb9SMartin KaFai Lau } 1643a3c00e46SMartin KaFai Lau } 1644a3c00e46SMartin KaFai Lau 16452b760fcfSWei Wang /*Search through exception table */ 16462b760fcfSWei Wang rt_cache = rt6_find_cached_rt(rt, &fl6->daddr, &fl6->saddr); 16472b760fcfSWei Wang if (rt_cache) 16482b760fcfSWei Wang rt = rt_cache; 1649d52d3997SMartin KaFai Lau 1650d3843fe5SWei Wang if (rt == net->ipv6.ip6_null_entry) { 1651c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 1652d3843fe5SWei Wang dst_hold(&rt->dst); 1653d3843fe5SWei Wang trace_fib6_table_lookup(net, rt, table->tb6_id, fl6); 1654d3843fe5SWei Wang return rt; 1655d3843fe5SWei Wang } else if (rt->rt6i_flags & RTF_CACHE) { 1656d3843fe5SWei Wang if (ip6_hold_safe(net, &rt, true)) { 1657d3843fe5SWei Wang dst_use_noref(&rt->dst, jiffies); 1658d52d3997SMartin KaFai Lau rt6_dst_from_metrics_check(rt); 1659d3843fe5SWei Wang } 1660d3843fe5SWei Wang read_unlock_bh(&table->tb6_lock); 1661b811580dSDavid Ahern trace_fib6_table_lookup(net, rt, table->tb6_id, fl6); 1662d52d3997SMartin KaFai Lau return rt; 16633da59bd9SMartin KaFai Lau } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) && 16643da59bd9SMartin KaFai Lau !(rt->rt6i_flags & RTF_GATEWAY))) { 16653da59bd9SMartin KaFai Lau /* Create a RTF_CACHE clone which will not be 16663da59bd9SMartin KaFai Lau * owned by the fib6 tree. It is for the special case where 16673da59bd9SMartin KaFai Lau * the daddr in the skb during the neighbor look-up is different 16683da59bd9SMartin KaFai Lau * from the fl6->daddr used to look-up route here. 16693da59bd9SMartin KaFai Lau */ 1670c71099acSThomas Graf 16713da59bd9SMartin KaFai Lau struct rt6_info *uncached_rt; 16723da59bd9SMartin KaFai Lau 1673d3843fe5SWei Wang if (ip6_hold_safe(net, &rt, true)) { 1674d3843fe5SWei Wang dst_use_noref(&rt->dst, jiffies); 1675d3843fe5SWei Wang } else { 1676d3843fe5SWei Wang read_unlock_bh(&table->tb6_lock); 1677d3843fe5SWei Wang uncached_rt = rt; 1678d3843fe5SWei Wang goto uncached_rt_out; 1679d3843fe5SWei Wang } 1680d52d3997SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 1681d52d3997SMartin KaFai Lau 16823da59bd9SMartin KaFai Lau uncached_rt = ip6_rt_cache_alloc(rt, &fl6->daddr, NULL); 16833da59bd9SMartin KaFai Lau dst_release(&rt->dst); 16843da59bd9SMartin KaFai Lau 16851cfb71eeSWei Wang if (uncached_rt) { 16861cfb71eeSWei Wang /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc() 16871cfb71eeSWei Wang * No need for another dst_hold() 16881cfb71eeSWei Wang */ 16898d0b94afSMartin KaFai Lau rt6_uncached_list_add(uncached_rt); 16901cfb71eeSWei Wang } else { 16913da59bd9SMartin KaFai Lau uncached_rt = net->ipv6.ip6_null_entry; 16923da59bd9SMartin KaFai Lau dst_hold(&uncached_rt->dst); 16931cfb71eeSWei Wang } 1694b811580dSDavid Ahern 1695d3843fe5SWei Wang uncached_rt_out: 1696b811580dSDavid Ahern trace_fib6_table_lookup(net, uncached_rt, table->tb6_id, fl6); 16973da59bd9SMartin KaFai Lau return uncached_rt; 16983da59bd9SMartin KaFai Lau 1699d52d3997SMartin KaFai Lau } else { 1700d52d3997SMartin KaFai Lau /* Get a percpu copy */ 1701d52d3997SMartin KaFai Lau 1702d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1703d52d3997SMartin KaFai Lau 1704d3843fe5SWei Wang dst_use_noref(&rt->dst, jiffies); 1705d52d3997SMartin KaFai Lau pcpu_rt = rt6_get_pcpu_route(rt); 1706d52d3997SMartin KaFai Lau 17079c7370a1SMartin KaFai Lau if (pcpu_rt) { 1708d52d3997SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 17099c7370a1SMartin KaFai Lau } else { 1710a94b9367SWei Wang /* atomic_inc_not_zero() is needed when using rcu */ 1711a94b9367SWei Wang if (atomic_inc_not_zero(&rt->rt6i_ref)) { 17129c7370a1SMartin KaFai Lau /* We have to do the read_unlock first 17139c7370a1SMartin KaFai Lau * because rt6_make_pcpu_route() may trigger 17149c7370a1SMartin KaFai Lau * ip6_dst_gc() which will take the write_lock. 1715a94b9367SWei Wang * 1716a94b9367SWei Wang * No dst_hold() on rt is needed because grabbing 1717a94b9367SWei Wang * rt->rt6i_ref makes sure rt can't be released. 17189c7370a1SMartin KaFai Lau */ 17199c7370a1SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 17209c7370a1SMartin KaFai Lau pcpu_rt = rt6_make_pcpu_route(rt); 1721a94b9367SWei Wang rt6_release(rt); 1722a94b9367SWei Wang } else { 1723a94b9367SWei Wang /* rt is already removed from tree */ 1724a94b9367SWei Wang read_unlock_bh(&table->tb6_lock); 1725a94b9367SWei Wang pcpu_rt = net->ipv6.ip6_null_entry; 1726a94b9367SWei Wang dst_hold(&pcpu_rt->dst); 1727a94b9367SWei Wang } 17289c7370a1SMartin KaFai Lau } 1729d52d3997SMartin KaFai Lau 1730b811580dSDavid Ahern trace_fib6_table_lookup(net, pcpu_rt, table->tb6_id, fl6); 1731d52d3997SMartin KaFai Lau return pcpu_rt; 1732d52d3997SMartin KaFai Lau } 1733c71099acSThomas Graf } 17349ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route); 1735c71099acSThomas Graf 17368ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table, 17374c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 17384acad72dSPavel Emelyanov { 17394c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags); 17404acad72dSPavel Emelyanov } 17414acad72dSPavel Emelyanov 1742d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net, 174372331bc0SShmulik Ladkani struct net_device *dev, 174472331bc0SShmulik Ladkani struct flowi6 *fl6, int flags) 174572331bc0SShmulik Ladkani { 174672331bc0SShmulik Ladkani if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) 174772331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_IFACE; 174872331bc0SShmulik Ladkani 174972331bc0SShmulik Ladkani return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input); 175072331bc0SShmulik Ladkani } 1751d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup); 175272331bc0SShmulik Ladkani 175323aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb, 175423aebdacSJakub Sitnicki struct flow_keys *keys) 175523aebdacSJakub Sitnicki { 175623aebdacSJakub Sitnicki const struct ipv6hdr *outer_iph = ipv6_hdr(skb); 175723aebdacSJakub Sitnicki const struct ipv6hdr *key_iph = outer_iph; 175823aebdacSJakub Sitnicki const struct ipv6hdr *inner_iph; 175923aebdacSJakub Sitnicki const struct icmp6hdr *icmph; 176023aebdacSJakub Sitnicki struct ipv6hdr _inner_iph; 176123aebdacSJakub Sitnicki 176223aebdacSJakub Sitnicki if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6)) 176323aebdacSJakub Sitnicki goto out; 176423aebdacSJakub Sitnicki 176523aebdacSJakub Sitnicki icmph = icmp6_hdr(skb); 176623aebdacSJakub Sitnicki if (icmph->icmp6_type != ICMPV6_DEST_UNREACH && 176723aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_PKT_TOOBIG && 176823aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_TIME_EXCEED && 176923aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_PARAMPROB) 177023aebdacSJakub Sitnicki goto out; 177123aebdacSJakub Sitnicki 177223aebdacSJakub Sitnicki inner_iph = skb_header_pointer(skb, 177323aebdacSJakub Sitnicki skb_transport_offset(skb) + sizeof(*icmph), 177423aebdacSJakub Sitnicki sizeof(_inner_iph), &_inner_iph); 177523aebdacSJakub Sitnicki if (!inner_iph) 177623aebdacSJakub Sitnicki goto out; 177723aebdacSJakub Sitnicki 177823aebdacSJakub Sitnicki key_iph = inner_iph; 177923aebdacSJakub Sitnicki out: 178023aebdacSJakub Sitnicki memset(keys, 0, sizeof(*keys)); 178123aebdacSJakub Sitnicki keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 178223aebdacSJakub Sitnicki keys->addrs.v6addrs.src = key_iph->saddr; 178323aebdacSJakub Sitnicki keys->addrs.v6addrs.dst = key_iph->daddr; 178423aebdacSJakub Sitnicki keys->tags.flow_label = ip6_flowinfo(key_iph); 178523aebdacSJakub Sitnicki keys->basic.ip_proto = key_iph->nexthdr; 178623aebdacSJakub Sitnicki } 178723aebdacSJakub Sitnicki 178823aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */ 178923aebdacSJakub Sitnicki u32 rt6_multipath_hash(const struct flowi6 *fl6, const struct sk_buff *skb) 179023aebdacSJakub Sitnicki { 179123aebdacSJakub Sitnicki struct flow_keys hash_keys; 179223aebdacSJakub Sitnicki 179323aebdacSJakub Sitnicki if (skb) { 179423aebdacSJakub Sitnicki ip6_multipath_l3_keys(skb, &hash_keys); 179523aebdacSJakub Sitnicki return flow_hash_from_keys(&hash_keys); 179623aebdacSJakub Sitnicki } 179723aebdacSJakub Sitnicki 179823aebdacSJakub Sitnicki return get_hash_from_flowi6(fl6); 179923aebdacSJakub Sitnicki } 180023aebdacSJakub Sitnicki 1801c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 1802c71099acSThomas Graf { 1803b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 1804c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 1805adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 1806904af04dSJiri Benc struct ip_tunnel_info *tun_info; 18074c9483b2SDavid S. Miller struct flowi6 fl6 = { 1808e0d56fddSDavid Ahern .flowi6_iif = skb->dev->ifindex, 18094c9483b2SDavid S. Miller .daddr = iph->daddr, 18104c9483b2SDavid S. Miller .saddr = iph->saddr, 18116502ca52SYOSHIFUJI Hideaki / 吉藤英明 .flowlabel = ip6_flowinfo(iph), 18124c9483b2SDavid S. Miller .flowi6_mark = skb->mark, 18134c9483b2SDavid S. Miller .flowi6_proto = iph->nexthdr, 1814c71099acSThomas Graf }; 1815adaa70bbSThomas Graf 1816904af04dSJiri Benc tun_info = skb_tunnel_info(skb); 181746fa062aSJiri Benc if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) 1818904af04dSJiri Benc fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id; 181923aebdacSJakub Sitnicki if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6)) 182023aebdacSJakub Sitnicki fl6.mp_hash = rt6_multipath_hash(&fl6, skb); 182106e9d040SJiri Benc skb_dst_drop(skb); 182272331bc0SShmulik Ladkani skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags)); 1823c71099acSThomas Graf } 1824c71099acSThomas Graf 18258ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table, 18264c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 1827c71099acSThomas Graf { 18284c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags); 1829c71099acSThomas Graf } 1830c71099acSThomas Graf 18316f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk, 18326f21c96aSPaolo Abeni struct flowi6 *fl6, int flags) 1833c71099acSThomas Graf { 1834d46a9d67SDavid Ahern bool any_src; 1835c71099acSThomas Graf 18364c1feac5SDavid Ahern if (rt6_need_strict(&fl6->daddr)) { 18374c1feac5SDavid Ahern struct dst_entry *dst; 18384c1feac5SDavid Ahern 18394c1feac5SDavid Ahern dst = l3mdev_link_scope_lookup(net, fl6); 1840ca254490SDavid Ahern if (dst) 1841ca254490SDavid Ahern return dst; 18424c1feac5SDavid Ahern } 1843ca254490SDavid Ahern 18441fb9489bSPavel Emelyanov fl6->flowi6_iif = LOOPBACK_IFINDEX; 18454dc27d1cSDavid McCullough 1846d46a9d67SDavid Ahern any_src = ipv6_addr_any(&fl6->saddr); 1847741a11d9SDavid Ahern if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) || 1848d46a9d67SDavid Ahern (fl6->flowi6_oif && any_src)) 184977d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 1850c71099acSThomas Graf 1851d46a9d67SDavid Ahern if (!any_src) 1852adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 18530c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 18540c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 1855adaa70bbSThomas Graf 18564c9483b2SDavid S. Miller return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output); 18571da177e4SLinus Torvalds } 18586f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags); 18591da177e4SLinus Torvalds 18602774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 186114e50e57SDavid S. Miller { 18625c1e6aa3SDavid S. Miller struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; 18631dbe3252SWei Wang struct net_device *loopback_dev = net->loopback_dev; 186414e50e57SDavid S. Miller struct dst_entry *new = NULL; 186514e50e57SDavid S. Miller 18661dbe3252SWei Wang rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1, 1867b2a9c0edSWei Wang DST_OBSOLETE_NONE, 0); 186814e50e57SDavid S. Miller if (rt) { 18690a1f5962SMartin KaFai Lau rt6_info_init(rt); 18700a1f5962SMartin KaFai Lau 1871d8d1f30bSChangli Gao new = &rt->dst; 187214e50e57SDavid S. Miller new->__use = 1; 1873352e512cSHerbert Xu new->input = dst_discard; 1874ede2059dSEric W. Biederman new->output = dst_discard_out; 187514e50e57SDavid S. Miller 1876defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 187714e50e57SDavid S. Miller 18781dbe3252SWei Wang rt->rt6i_idev = in6_dev_get(loopback_dev); 18794e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 18800a1f5962SMartin KaFai Lau rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU; 188114e50e57SDavid S. Miller rt->rt6i_metric = 0; 188214e50e57SDavid S. Miller 188314e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 188414e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 188514e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 188614e50e57SDavid S. Miller #endif 188714e50e57SDavid S. Miller } 188814e50e57SDavid S. Miller 188969ead7afSDavid S. Miller dst_release(dst_orig); 189069ead7afSDavid S. Miller return new ? new : ERR_PTR(-ENOMEM); 189114e50e57SDavid S. Miller } 189214e50e57SDavid S. Miller 18931da177e4SLinus Torvalds /* 18941da177e4SLinus Torvalds * Destination cache support functions 18951da177e4SLinus Torvalds */ 18961da177e4SLinus Torvalds 18974b32b5adSMartin KaFai Lau static void rt6_dst_from_metrics_check(struct rt6_info *rt) 18984b32b5adSMartin KaFai Lau { 18994b32b5adSMartin KaFai Lau if (rt->dst.from && 19004b32b5adSMartin KaFai Lau dst_metrics_ptr(&rt->dst) != dst_metrics_ptr(rt->dst.from)) 19014b32b5adSMartin KaFai Lau dst_init_metrics(&rt->dst, dst_metrics_ptr(rt->dst.from), true); 19024b32b5adSMartin KaFai Lau } 19034b32b5adSMartin KaFai Lau 19043da59bd9SMartin KaFai Lau static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie) 19053da59bd9SMartin KaFai Lau { 190636143645SSteffen Klassert u32 rt_cookie = 0; 1907c5cff856SWei Wang 1908c5cff856SWei Wang if (!rt6_get_cookie_safe(rt, &rt_cookie) || rt_cookie != cookie) 19093da59bd9SMartin KaFai Lau return NULL; 19103da59bd9SMartin KaFai Lau 19113da59bd9SMartin KaFai Lau if (rt6_check_expired(rt)) 19123da59bd9SMartin KaFai Lau return NULL; 19133da59bd9SMartin KaFai Lau 19143da59bd9SMartin KaFai Lau return &rt->dst; 19153da59bd9SMartin KaFai Lau } 19163da59bd9SMartin KaFai Lau 19173da59bd9SMartin KaFai Lau static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, u32 cookie) 19183da59bd9SMartin KaFai Lau { 19195973fb1eSMartin KaFai Lau if (!__rt6_check_expired(rt) && 19205973fb1eSMartin KaFai Lau rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && 19213da59bd9SMartin KaFai Lau rt6_check((struct rt6_info *)(rt->dst.from), cookie)) 19223da59bd9SMartin KaFai Lau return &rt->dst; 19233da59bd9SMartin KaFai Lau else 19243da59bd9SMartin KaFai Lau return NULL; 19253da59bd9SMartin KaFai Lau } 19263da59bd9SMartin KaFai Lau 19271da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 19281da177e4SLinus Torvalds { 19291da177e4SLinus Torvalds struct rt6_info *rt; 19301da177e4SLinus Torvalds 19311da177e4SLinus Torvalds rt = (struct rt6_info *) dst; 19321da177e4SLinus Torvalds 19336f3118b5SNicolas Dichtel /* All IPV6 dsts are created with ->obsolete set to the value 19346f3118b5SNicolas Dichtel * DST_OBSOLETE_FORCE_CHK which forces validation calls down 19356f3118b5SNicolas Dichtel * into this function always. 19366f3118b5SNicolas Dichtel */ 1937e3bc10bdSHannes Frederic Sowa 19384b32b5adSMartin KaFai Lau rt6_dst_from_metrics_check(rt); 19394b32b5adSMartin KaFai Lau 194002bcf4e0SMartin KaFai Lau if (rt->rt6i_flags & RTF_PCPU || 1941a4c2fd7fSWei Wang (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->dst.from)) 19423da59bd9SMartin KaFai Lau return rt6_dst_from_check(rt, cookie); 19433da59bd9SMartin KaFai Lau else 19443da59bd9SMartin KaFai Lau return rt6_check(rt, cookie); 19451da177e4SLinus Torvalds } 19461da177e4SLinus Torvalds 19471da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 19481da177e4SLinus Torvalds { 19491da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 19501da177e4SLinus Torvalds 19511da177e4SLinus Torvalds if (rt) { 195254c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 195354c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 1954e0a1ad73SThomas Graf ip6_del_rt(rt); 195554c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 19561da177e4SLinus Torvalds } 195754c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 195854c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 195954c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 196054c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 196154c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 196254c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 19631da177e4SLinus Torvalds } 19641da177e4SLinus Torvalds 19651da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 19661da177e4SLinus Torvalds { 19671da177e4SLinus Torvalds struct rt6_info *rt; 19681da177e4SLinus Torvalds 19693ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 19701da177e4SLinus Torvalds 1971adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 19721da177e4SLinus Torvalds if (rt) { 19731eb4f758SHannes Frederic Sowa if (rt->rt6i_flags & RTF_CACHE) { 1974ad65a2f0SWei Wang if (dst_hold_safe(&rt->dst)) 19758e3d5be7SMartin KaFai Lau ip6_del_rt(rt); 1976c5cff856SWei Wang } else { 1977c5cff856SWei Wang struct fib6_node *fn; 1978c5cff856SWei Wang 1979c5cff856SWei Wang rcu_read_lock(); 1980c5cff856SWei Wang fn = rcu_dereference(rt->rt6i_node); 1981c5cff856SWei Wang if (fn && (rt->rt6i_flags & RTF_DEFAULT)) 1982c5cff856SWei Wang fn->fn_sernum = -1; 1983c5cff856SWei Wang rcu_read_unlock(); 19841da177e4SLinus Torvalds } 19851da177e4SLinus Torvalds } 19861eb4f758SHannes Frederic Sowa } 19871da177e4SLinus Torvalds 198845e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu) 198945e4fd26SMartin KaFai Lau { 199045e4fd26SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 199145e4fd26SMartin KaFai Lau 199245e4fd26SMartin KaFai Lau rt->rt6i_flags |= RTF_MODIFIED; 199345e4fd26SMartin KaFai Lau rt->rt6i_pmtu = mtu; 199445e4fd26SMartin KaFai Lau rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires); 199545e4fd26SMartin KaFai Lau } 199645e4fd26SMartin KaFai Lau 19970d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt) 19980d3f6d29SMartin KaFai Lau { 19990d3f6d29SMartin KaFai Lau return !(rt->rt6i_flags & RTF_CACHE) && 20004e587ea7SWei Wang (rt->rt6i_flags & RTF_PCPU || 20014e587ea7SWei Wang rcu_access_pointer(rt->rt6i_node)); 20020d3f6d29SMartin KaFai Lau } 20030d3f6d29SMartin KaFai Lau 200445e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk, 200545e4fd26SMartin KaFai Lau const struct ipv6hdr *iph, u32 mtu) 20061da177e4SLinus Torvalds { 20070dec879fSJulian Anastasov const struct in6_addr *daddr, *saddr; 20081da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info *)dst; 20091da177e4SLinus Torvalds 201045e4fd26SMartin KaFai Lau if (rt6->rt6i_flags & RTF_LOCAL) 201145e4fd26SMartin KaFai Lau return; 201245e4fd26SMartin KaFai Lau 201319bda36cSXin Long if (dst_metric_locked(dst, RTAX_MTU)) 201419bda36cSXin Long return; 201519bda36cSXin Long 201645e4fd26SMartin KaFai Lau if (iph) { 201745e4fd26SMartin KaFai Lau daddr = &iph->daddr; 201845e4fd26SMartin KaFai Lau saddr = &iph->saddr; 201945e4fd26SMartin KaFai Lau } else if (sk) { 202045e4fd26SMartin KaFai Lau daddr = &sk->sk_v6_daddr; 202145e4fd26SMartin KaFai Lau saddr = &inet6_sk(sk)->saddr; 202245e4fd26SMartin KaFai Lau } else { 20230dec879fSJulian Anastasov daddr = NULL; 20240dec879fSJulian Anastasov saddr = NULL; 20251da177e4SLinus Torvalds } 20260dec879fSJulian Anastasov dst_confirm_neigh(dst, daddr); 20270dec879fSJulian Anastasov mtu = max_t(u32, mtu, IPV6_MIN_MTU); 20280dec879fSJulian Anastasov if (mtu >= dst_mtu(dst)) 20290dec879fSJulian Anastasov return; 20300dec879fSJulian Anastasov 20310dec879fSJulian Anastasov if (!rt6_cache_allowed_for_pmtu(rt6)) { 20320dec879fSJulian Anastasov rt6_do_update_pmtu(rt6, mtu); 20332b760fcfSWei Wang /* update rt6_ex->stamp for cache */ 20342b760fcfSWei Wang if (rt6->rt6i_flags & RTF_CACHE) 20352b760fcfSWei Wang rt6_update_exception_stamp_rt(rt6); 20360dec879fSJulian Anastasov } else if (daddr) { 20370dec879fSJulian Anastasov struct rt6_info *nrt6; 20380dec879fSJulian Anastasov 203945e4fd26SMartin KaFai Lau nrt6 = ip6_rt_cache_alloc(rt6, daddr, saddr); 204045e4fd26SMartin KaFai Lau if (nrt6) { 204145e4fd26SMartin KaFai Lau rt6_do_update_pmtu(nrt6, mtu); 20422b760fcfSWei Wang if (rt6_insert_exception(nrt6, rt6)) 20432b760fcfSWei Wang dst_release_immediate(&nrt6->dst); 204445e4fd26SMartin KaFai Lau } 204545e4fd26SMartin KaFai Lau } 204645e4fd26SMartin KaFai Lau } 204745e4fd26SMartin KaFai Lau 204845e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 204945e4fd26SMartin KaFai Lau struct sk_buff *skb, u32 mtu) 205045e4fd26SMartin KaFai Lau { 205145e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu); 20521da177e4SLinus Torvalds } 20531da177e4SLinus Torvalds 205442ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, 2055e2d118a1SLorenzo Colitti int oif, u32 mark, kuid_t uid) 205681aded24SDavid S. Miller { 205781aded24SDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 205881aded24SDavid S. Miller struct dst_entry *dst; 205981aded24SDavid S. Miller struct flowi6 fl6; 206081aded24SDavid S. Miller 206181aded24SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 206281aded24SDavid S. Miller fl6.flowi6_oif = oif; 20631b3c61dcSLorenzo Colitti fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark); 206481aded24SDavid S. Miller fl6.daddr = iph->daddr; 206581aded24SDavid S. Miller fl6.saddr = iph->saddr; 20666502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 2067e2d118a1SLorenzo Colitti fl6.flowi6_uid = uid; 206881aded24SDavid S. Miller 206981aded24SDavid S. Miller dst = ip6_route_output(net, NULL, &fl6); 207081aded24SDavid S. Miller if (!dst->error) 207145e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu)); 207281aded24SDavid S. Miller dst_release(dst); 207381aded24SDavid S. Miller } 207481aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu); 207581aded24SDavid S. Miller 207681aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) 207781aded24SDavid S. Miller { 207833c162a9SMartin KaFai Lau struct dst_entry *dst; 207933c162a9SMartin KaFai Lau 208081aded24SDavid S. Miller ip6_update_pmtu(skb, sock_net(sk), mtu, 2081e2d118a1SLorenzo Colitti sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid); 208233c162a9SMartin KaFai Lau 208333c162a9SMartin KaFai Lau dst = __sk_dst_get(sk); 208433c162a9SMartin KaFai Lau if (!dst || !dst->obsolete || 208533c162a9SMartin KaFai Lau dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) 208633c162a9SMartin KaFai Lau return; 208733c162a9SMartin KaFai Lau 208833c162a9SMartin KaFai Lau bh_lock_sock(sk); 208933c162a9SMartin KaFai Lau if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) 209033c162a9SMartin KaFai Lau ip6_datagram_dst_update(sk, false); 209133c162a9SMartin KaFai Lau bh_unlock_sock(sk); 209281aded24SDavid S. Miller } 209381aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); 209481aded24SDavid S. Miller 2095b55b76b2SDuan Jiong /* Handle redirects */ 2096b55b76b2SDuan Jiong struct ip6rd_flowi { 2097b55b76b2SDuan Jiong struct flowi6 fl6; 2098b55b76b2SDuan Jiong struct in6_addr gateway; 2099b55b76b2SDuan Jiong }; 2100b55b76b2SDuan Jiong 2101b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net, 2102b55b76b2SDuan Jiong struct fib6_table *table, 2103b55b76b2SDuan Jiong struct flowi6 *fl6, 2104b55b76b2SDuan Jiong int flags) 2105b55b76b2SDuan Jiong { 2106b55b76b2SDuan Jiong struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; 21072b760fcfSWei Wang struct rt6_info *rt, *rt_cache; 2108b55b76b2SDuan Jiong struct fib6_node *fn; 2109b55b76b2SDuan Jiong 2110b55b76b2SDuan Jiong /* Get the "current" route for this destination and 211167c408cfSAlexander Alemayhu * check if the redirect has come from appropriate router. 2112b55b76b2SDuan Jiong * 2113b55b76b2SDuan Jiong * RFC 4861 specifies that redirects should only be 2114b55b76b2SDuan Jiong * accepted if they come from the nexthop to the target. 2115b55b76b2SDuan Jiong * Due to the way the routes are chosen, this notion 2116b55b76b2SDuan Jiong * is a bit fuzzy and one might need to check all possible 2117b55b76b2SDuan Jiong * routes. 2118b55b76b2SDuan Jiong */ 2119b55b76b2SDuan Jiong 2120b55b76b2SDuan Jiong read_lock_bh(&table->tb6_lock); 2121b55b76b2SDuan Jiong fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 2122b55b76b2SDuan Jiong restart: 2123b55b76b2SDuan Jiong for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 2124b55b76b2SDuan Jiong if (rt6_check_expired(rt)) 2125b55b76b2SDuan Jiong continue; 2126b55b76b2SDuan Jiong if (rt->dst.error) 2127b55b76b2SDuan Jiong break; 2128b55b76b2SDuan Jiong if (!(rt->rt6i_flags & RTF_GATEWAY)) 2129b55b76b2SDuan Jiong continue; 2130b55b76b2SDuan Jiong if (fl6->flowi6_oif != rt->dst.dev->ifindex) 2131b55b76b2SDuan Jiong continue; 21322b760fcfSWei Wang /* rt_cache's gateway might be different from its 'parent' 21332b760fcfSWei Wang * in the case of an ip redirect. 21342b760fcfSWei Wang * So we keep searching in the exception table if the gateway 21352b760fcfSWei Wang * is different. 21362b760fcfSWei Wang */ 21372b760fcfSWei Wang if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway)) { 21382b760fcfSWei Wang rt_cache = rt6_find_cached_rt(rt, 21392b760fcfSWei Wang &fl6->daddr, 21402b760fcfSWei Wang &fl6->saddr); 21412b760fcfSWei Wang if (rt_cache && 21422b760fcfSWei Wang ipv6_addr_equal(&rdfl->gateway, 21432b760fcfSWei Wang &rt_cache->rt6i_gateway)) { 21442b760fcfSWei Wang rt = rt_cache; 21452b760fcfSWei Wang break; 21462b760fcfSWei Wang } 2147b55b76b2SDuan Jiong continue; 21482b760fcfSWei Wang } 2149b55b76b2SDuan Jiong break; 2150b55b76b2SDuan Jiong } 2151b55b76b2SDuan Jiong 2152b55b76b2SDuan Jiong if (!rt) 2153b55b76b2SDuan Jiong rt = net->ipv6.ip6_null_entry; 2154b55b76b2SDuan Jiong else if (rt->dst.error) { 2155b55b76b2SDuan Jiong rt = net->ipv6.ip6_null_entry; 2156b0a1ba59SMartin KaFai Lau goto out; 2157b0a1ba59SMartin KaFai Lau } 2158b0a1ba59SMartin KaFai Lau 2159b0a1ba59SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 2160a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 2161a3c00e46SMartin KaFai Lau if (fn) 2162a3c00e46SMartin KaFai Lau goto restart; 2163b55b76b2SDuan Jiong } 2164a3c00e46SMartin KaFai Lau 2165b0a1ba59SMartin KaFai Lau out: 2166d3843fe5SWei Wang ip6_hold_safe(net, &rt, true); 2167b55b76b2SDuan Jiong 2168b55b76b2SDuan Jiong read_unlock_bh(&table->tb6_lock); 2169b55b76b2SDuan Jiong 2170b811580dSDavid Ahern trace_fib6_table_lookup(net, rt, table->tb6_id, fl6); 2171b55b76b2SDuan Jiong return rt; 2172b55b76b2SDuan Jiong }; 2173b55b76b2SDuan Jiong 2174b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net, 2175b55b76b2SDuan Jiong const struct flowi6 *fl6, 2176b55b76b2SDuan Jiong const struct in6_addr *gateway) 2177b55b76b2SDuan Jiong { 2178b55b76b2SDuan Jiong int flags = RT6_LOOKUP_F_HAS_SADDR; 2179b55b76b2SDuan Jiong struct ip6rd_flowi rdfl; 2180b55b76b2SDuan Jiong 2181b55b76b2SDuan Jiong rdfl.fl6 = *fl6; 2182b55b76b2SDuan Jiong rdfl.gateway = *gateway; 2183b55b76b2SDuan Jiong 2184b55b76b2SDuan Jiong return fib6_rule_lookup(net, &rdfl.fl6, 2185b55b76b2SDuan Jiong flags, __ip6_route_redirect); 2186b55b76b2SDuan Jiong } 2187b55b76b2SDuan Jiong 2188e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, 2189e2d118a1SLorenzo Colitti kuid_t uid) 21903a5ad2eeSDavid S. Miller { 21913a5ad2eeSDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 21923a5ad2eeSDavid S. Miller struct dst_entry *dst; 21933a5ad2eeSDavid S. Miller struct flowi6 fl6; 21943a5ad2eeSDavid S. Miller 21953a5ad2eeSDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 2196e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 21973a5ad2eeSDavid S. Miller fl6.flowi6_oif = oif; 21983a5ad2eeSDavid S. Miller fl6.flowi6_mark = mark; 21993a5ad2eeSDavid S. Miller fl6.daddr = iph->daddr; 22003a5ad2eeSDavid S. Miller fl6.saddr = iph->saddr; 22016502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 2202e2d118a1SLorenzo Colitti fl6.flowi6_uid = uid; 22033a5ad2eeSDavid S. Miller 2204b55b76b2SDuan Jiong dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr); 22056700c270SDavid S. Miller rt6_do_redirect(dst, NULL, skb); 22063a5ad2eeSDavid S. Miller dst_release(dst); 22073a5ad2eeSDavid S. Miller } 22083a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect); 22093a5ad2eeSDavid S. Miller 2210c92a59ecSDuan Jiong void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif, 2211c92a59ecSDuan Jiong u32 mark) 2212c92a59ecSDuan Jiong { 2213c92a59ecSDuan Jiong const struct ipv6hdr *iph = ipv6_hdr(skb); 2214c92a59ecSDuan Jiong const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); 2215c92a59ecSDuan Jiong struct dst_entry *dst; 2216c92a59ecSDuan Jiong struct flowi6 fl6; 2217c92a59ecSDuan Jiong 2218c92a59ecSDuan Jiong memset(&fl6, 0, sizeof(fl6)); 2219e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 2220c92a59ecSDuan Jiong fl6.flowi6_oif = oif; 2221c92a59ecSDuan Jiong fl6.flowi6_mark = mark; 2222c92a59ecSDuan Jiong fl6.daddr = msg->dest; 2223c92a59ecSDuan Jiong fl6.saddr = iph->daddr; 2224e2d118a1SLorenzo Colitti fl6.flowi6_uid = sock_net_uid(net, NULL); 2225c92a59ecSDuan Jiong 2226b55b76b2SDuan Jiong dst = ip6_route_redirect(net, &fl6, &iph->saddr); 2227c92a59ecSDuan Jiong rt6_do_redirect(dst, NULL, skb); 2228c92a59ecSDuan Jiong dst_release(dst); 2229c92a59ecSDuan Jiong } 2230c92a59ecSDuan Jiong 22313a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) 22323a5ad2eeSDavid S. Miller { 2233e2d118a1SLorenzo Colitti ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark, 2234e2d118a1SLorenzo Colitti sk->sk_uid); 22353a5ad2eeSDavid S. Miller } 22363a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect); 22373a5ad2eeSDavid S. Miller 22380dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 22391da177e4SLinus Torvalds { 22400dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 22410dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 22420dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 22430dbaee3bSDavid S. Miller 22441da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 22451da177e4SLinus Torvalds 22465578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 22475578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 22481da177e4SLinus Torvalds 22491da177e4SLinus Torvalds /* 22501da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 22511da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 22521da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 22531da177e4SLinus Torvalds * rely only on pmtu discovery" 22541da177e4SLinus Torvalds */ 22551da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 22561da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 22571da177e4SLinus Torvalds return mtu; 22581da177e4SLinus Torvalds } 22591da177e4SLinus Torvalds 2260ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst) 2261d33e4553SDavid S. Miller { 22624b32b5adSMartin KaFai Lau const struct rt6_info *rt = (const struct rt6_info *)dst; 22634b32b5adSMartin KaFai Lau unsigned int mtu = rt->rt6i_pmtu; 2264d33e4553SDavid S. Miller struct inet6_dev *idev; 2265618f9bc7SSteffen Klassert 2266618f9bc7SSteffen Klassert if (mtu) 226730f78d8eSEric Dumazet goto out; 2268618f9bc7SSteffen Klassert 22694b32b5adSMartin KaFai Lau mtu = dst_metric_raw(dst, RTAX_MTU); 22704b32b5adSMartin KaFai Lau if (mtu) 22714b32b5adSMartin KaFai Lau goto out; 22724b32b5adSMartin KaFai Lau 2273618f9bc7SSteffen Klassert mtu = IPV6_MIN_MTU; 2274d33e4553SDavid S. Miller 2275d33e4553SDavid S. Miller rcu_read_lock(); 2276d33e4553SDavid S. Miller idev = __in6_dev_get(dst->dev); 2277d33e4553SDavid S. Miller if (idev) 2278d33e4553SDavid S. Miller mtu = idev->cnf.mtu6; 2279d33e4553SDavid S. Miller rcu_read_unlock(); 2280d33e4553SDavid S. Miller 228130f78d8eSEric Dumazet out: 228214972cbdSRoopa Prabhu mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 228314972cbdSRoopa Prabhu 228414972cbdSRoopa Prabhu return mtu - lwtunnel_headroom(dst->lwtstate, mtu); 2285d33e4553SDavid S. Miller } 2286d33e4553SDavid S. Miller 22873b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 228887a11578SDavid S. Miller struct flowi6 *fl6) 22891da177e4SLinus Torvalds { 229087a11578SDavid S. Miller struct dst_entry *dst; 22911da177e4SLinus Torvalds struct rt6_info *rt; 22921da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 2293c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 22941da177e4SLinus Torvalds 229538308473SDavid S. Miller if (unlikely(!idev)) 2296122bdf67SEric Dumazet return ERR_PTR(-ENODEV); 22971da177e4SLinus Torvalds 2298ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, dev, 0); 229938308473SDavid S. Miller if (unlikely(!rt)) { 23001da177e4SLinus Torvalds in6_dev_put(idev); 230187a11578SDavid S. Miller dst = ERR_PTR(-ENOMEM); 23021da177e4SLinus Torvalds goto out; 23031da177e4SLinus Torvalds } 23041da177e4SLinus Torvalds 23058e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 23068e2ec639SYan, Zheng rt->dst.output = ip6_output; 2307550bab42SJulian Anastasov rt->rt6i_gateway = fl6->daddr; 230887a11578SDavid S. Miller rt->rt6i_dst.addr = fl6->daddr; 23098e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 23108e2ec639SYan, Zheng rt->rt6i_idev = idev; 231114edd87dSLi RongQing dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); 23121da177e4SLinus Torvalds 2313587fea74SWei Wang /* Add this dst into uncached_list so that rt6_ifdown() can 2314587fea74SWei Wang * do proper release of the net_device 2315587fea74SWei Wang */ 2316587fea74SWei Wang rt6_uncached_list_add(rt); 23171da177e4SLinus Torvalds 231887a11578SDavid S. Miller dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); 231987a11578SDavid S. Miller 23201da177e4SLinus Torvalds out: 232187a11578SDavid S. Miller return dst; 23221da177e4SLinus Torvalds } 23231da177e4SLinus Torvalds 2324569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops) 23251da177e4SLinus Torvalds { 232686393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 23277019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 23287019b78eSDaniel Lezcano int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; 23297019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 23307019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 23317019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 2332fc66f95cSEric Dumazet int entries; 23331da177e4SLinus Torvalds 2334fc66f95cSEric Dumazet entries = dst_entries_get_fast(ops); 233549a18d86SMichal Kubeček if (time_after(rt_last_gc + rt_min_interval, jiffies) && 2336fc66f95cSEric Dumazet entries <= rt_max_size) 23371da177e4SLinus Torvalds goto out; 23381da177e4SLinus Torvalds 23396891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire++; 234014956643SLi RongQing fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true); 2341fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 2342fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 23437019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; 23441da177e4SLinus Torvalds out: 23457019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; 2346fc66f95cSEric Dumazet return entries > rt_max_size; 23471da177e4SLinus Torvalds } 23481da177e4SLinus Torvalds 2349e715b6d3SFlorian Westphal static int ip6_convert_metrics(struct mx6_config *mxc, 2350e715b6d3SFlorian Westphal const struct fib6_config *cfg) 2351e715b6d3SFlorian Westphal { 2352c3a8d947SDaniel Borkmann bool ecn_ca = false; 2353e715b6d3SFlorian Westphal struct nlattr *nla; 2354e715b6d3SFlorian Westphal int remaining; 2355e715b6d3SFlorian Westphal u32 *mp; 2356e715b6d3SFlorian Westphal 235763159f29SIan Morris if (!cfg->fc_mx) 2358e715b6d3SFlorian Westphal return 0; 2359e715b6d3SFlorian Westphal 2360e715b6d3SFlorian Westphal mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL); 2361e715b6d3SFlorian Westphal if (unlikely(!mp)) 2362e715b6d3SFlorian Westphal return -ENOMEM; 2363e715b6d3SFlorian Westphal 2364e715b6d3SFlorian Westphal nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { 2365e715b6d3SFlorian Westphal int type = nla_type(nla); 2366ea697639SDaniel Borkmann u32 val; 2367ea697639SDaniel Borkmann 23681bb14807SDaniel Borkmann if (!type) 23691bb14807SDaniel Borkmann continue; 2370e715b6d3SFlorian Westphal if (unlikely(type > RTAX_MAX)) 2371e715b6d3SFlorian Westphal goto err; 23721bb14807SDaniel Borkmann 2373ea697639SDaniel Borkmann if (type == RTAX_CC_ALGO) { 2374ea697639SDaniel Borkmann char tmp[TCP_CA_NAME_MAX]; 2375e715b6d3SFlorian Westphal 2376ea697639SDaniel Borkmann nla_strlcpy(tmp, nla, sizeof(tmp)); 2377c3a8d947SDaniel Borkmann val = tcp_ca_get_key_by_name(tmp, &ecn_ca); 2378ea697639SDaniel Borkmann if (val == TCP_CA_UNSPEC) 2379ea697639SDaniel Borkmann goto err; 2380ea697639SDaniel Borkmann } else { 2381ea697639SDaniel Borkmann val = nla_get_u32(nla); 2382ea697639SDaniel Borkmann } 2383626abd59SPaolo Abeni if (type == RTAX_HOPLIMIT && val > 255) 2384626abd59SPaolo Abeni val = 255; 2385b8d3e416SDaniel Borkmann if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK)) 2386b8d3e416SDaniel Borkmann goto err; 2387ea697639SDaniel Borkmann 2388ea697639SDaniel Borkmann mp[type - 1] = val; 2389e715b6d3SFlorian Westphal __set_bit(type - 1, mxc->mx_valid); 2390e715b6d3SFlorian Westphal } 2391e715b6d3SFlorian Westphal 2392c3a8d947SDaniel Borkmann if (ecn_ca) { 2393c3a8d947SDaniel Borkmann __set_bit(RTAX_FEATURES - 1, mxc->mx_valid); 2394c3a8d947SDaniel Borkmann mp[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA; 2395c3a8d947SDaniel Borkmann } 2396e715b6d3SFlorian Westphal 2397c3a8d947SDaniel Borkmann mxc->mx = mp; 2398e715b6d3SFlorian Westphal return 0; 2399e715b6d3SFlorian Westphal err: 2400e715b6d3SFlorian Westphal kfree(mp); 2401e715b6d3SFlorian Westphal return -EINVAL; 2402e715b6d3SFlorian Westphal } 24031da177e4SLinus Torvalds 24048c14586fSDavid Ahern static struct rt6_info *ip6_nh_lookup_table(struct net *net, 24058c14586fSDavid Ahern struct fib6_config *cfg, 24068c14586fSDavid Ahern const struct in6_addr *gw_addr) 24078c14586fSDavid Ahern { 24088c14586fSDavid Ahern struct flowi6 fl6 = { 24098c14586fSDavid Ahern .flowi6_oif = cfg->fc_ifindex, 24108c14586fSDavid Ahern .daddr = *gw_addr, 24118c14586fSDavid Ahern .saddr = cfg->fc_prefsrc, 24128c14586fSDavid Ahern }; 24138c14586fSDavid Ahern struct fib6_table *table; 24148c14586fSDavid Ahern struct rt6_info *rt; 2415d5d32e4bSDavid Ahern int flags = RT6_LOOKUP_F_IFACE | RT6_LOOKUP_F_IGNORE_LINKSTATE; 24168c14586fSDavid Ahern 24178c14586fSDavid Ahern table = fib6_get_table(net, cfg->fc_table); 24188c14586fSDavid Ahern if (!table) 24198c14586fSDavid Ahern return NULL; 24208c14586fSDavid Ahern 24218c14586fSDavid Ahern if (!ipv6_addr_any(&cfg->fc_prefsrc)) 24228c14586fSDavid Ahern flags |= RT6_LOOKUP_F_HAS_SADDR; 24238c14586fSDavid Ahern 24248c14586fSDavid Ahern rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, flags); 24258c14586fSDavid Ahern 24268c14586fSDavid Ahern /* if table lookup failed, fall back to full lookup */ 24278c14586fSDavid Ahern if (rt == net->ipv6.ip6_null_entry) { 24288c14586fSDavid Ahern ip6_rt_put(rt); 24298c14586fSDavid Ahern rt = NULL; 24308c14586fSDavid Ahern } 24318c14586fSDavid Ahern 24328c14586fSDavid Ahern return rt; 24338c14586fSDavid Ahern } 24348c14586fSDavid Ahern 2435333c4301SDavid Ahern static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg, 2436333c4301SDavid Ahern struct netlink_ext_ack *extack) 24371da177e4SLinus Torvalds { 24385578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 24391da177e4SLinus Torvalds struct rt6_info *rt = NULL; 24401da177e4SLinus Torvalds struct net_device *dev = NULL; 24411da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 2442c71099acSThomas Graf struct fib6_table *table; 24431da177e4SLinus Torvalds int addr_type; 24448c5b83f0SRoopa Prabhu int err = -EINVAL; 24451da177e4SLinus Torvalds 2446557c44beSDavid Ahern /* RTF_PCPU is an internal flag; can not be set by userspace */ 2447d5d531cbSDavid Ahern if (cfg->fc_flags & RTF_PCPU) { 2448d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU"); 2449557c44beSDavid Ahern goto out; 2450d5d531cbSDavid Ahern } 2451557c44beSDavid Ahern 2452d5d531cbSDavid Ahern if (cfg->fc_dst_len > 128) { 2453d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid prefix length"); 24548c5b83f0SRoopa Prabhu goto out; 2455d5d531cbSDavid Ahern } 2456d5d531cbSDavid Ahern if (cfg->fc_src_len > 128) { 2457d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address length"); 2458d5d531cbSDavid Ahern goto out; 2459d5d531cbSDavid Ahern } 24601da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 2461d5d531cbSDavid Ahern if (cfg->fc_src_len) { 2462d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2463d5d531cbSDavid Ahern "Specifying source address requires IPV6_SUBTREES to be enabled"); 24648c5b83f0SRoopa Prabhu goto out; 2465d5d531cbSDavid Ahern } 24661da177e4SLinus Torvalds #endif 246786872cb5SThomas Graf if (cfg->fc_ifindex) { 24681da177e4SLinus Torvalds err = -ENODEV; 24695578689aSDaniel Lezcano dev = dev_get_by_index(net, cfg->fc_ifindex); 24701da177e4SLinus Torvalds if (!dev) 24711da177e4SLinus Torvalds goto out; 24721da177e4SLinus Torvalds idev = in6_dev_get(dev); 24731da177e4SLinus Torvalds if (!idev) 24741da177e4SLinus Torvalds goto out; 24751da177e4SLinus Torvalds } 24761da177e4SLinus Torvalds 247786872cb5SThomas Graf if (cfg->fc_metric == 0) 247886872cb5SThomas Graf cfg->fc_metric = IP6_RT_PRIO_USER; 24791da177e4SLinus Torvalds 2480c71099acSThomas Graf err = -ENOBUFS; 248138308473SDavid S. Miller if (cfg->fc_nlinfo.nlh && 2482d71314b4SMatti Vaittinen !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { 2483d71314b4SMatti Vaittinen table = fib6_get_table(net, cfg->fc_table); 248438308473SDavid S. Miller if (!table) { 2485f3213831SJoe Perches pr_warn("NLM_F_CREATE should be specified when creating new route\n"); 2486d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 2487d71314b4SMatti Vaittinen } 2488d71314b4SMatti Vaittinen } else { 2489d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 2490d71314b4SMatti Vaittinen } 249138308473SDavid S. Miller 249238308473SDavid S. Miller if (!table) 2493c71099acSThomas Graf goto out; 2494c71099acSThomas Graf 2495ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, NULL, 2496ad706862SMartin KaFai Lau (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT); 24971da177e4SLinus Torvalds 249838308473SDavid S. Miller if (!rt) { 24991da177e4SLinus Torvalds err = -ENOMEM; 25001da177e4SLinus Torvalds goto out; 25011da177e4SLinus Torvalds } 25021da177e4SLinus Torvalds 25031716a961SGao feng if (cfg->fc_flags & RTF_EXPIRES) 25041716a961SGao feng rt6_set_expires(rt, jiffies + 25051716a961SGao feng clock_t_to_jiffies(cfg->fc_expires)); 25061716a961SGao feng else 25071716a961SGao feng rt6_clean_expires(rt); 25081da177e4SLinus Torvalds 250986872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 251086872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 251186872cb5SThomas Graf rt->rt6i_protocol = cfg->fc_protocol; 251286872cb5SThomas Graf 251386872cb5SThomas Graf addr_type = ipv6_addr_type(&cfg->fc_dst); 25141da177e4SLinus Torvalds 25151da177e4SLinus Torvalds if (addr_type & IPV6_ADDR_MULTICAST) 2516d8d1f30bSChangli Gao rt->dst.input = ip6_mc_input; 2517ab79ad14SMaciej Żenczykowski else if (cfg->fc_flags & RTF_LOCAL) 2518ab79ad14SMaciej Żenczykowski rt->dst.input = ip6_input; 25191da177e4SLinus Torvalds else 2520d8d1f30bSChangli Gao rt->dst.input = ip6_forward; 25211da177e4SLinus Torvalds 2522d8d1f30bSChangli Gao rt->dst.output = ip6_output; 25231da177e4SLinus Torvalds 252419e42e45SRoopa Prabhu if (cfg->fc_encap) { 252519e42e45SRoopa Prabhu struct lwtunnel_state *lwtstate; 252619e42e45SRoopa Prabhu 252730357d7dSDavid Ahern err = lwtunnel_build_state(cfg->fc_encap_type, 2528127eb7cdSTom Herbert cfg->fc_encap, AF_INET6, cfg, 25299ae28727SDavid Ahern &lwtstate, extack); 253019e42e45SRoopa Prabhu if (err) 253119e42e45SRoopa Prabhu goto out; 253261adedf3SJiri Benc rt->dst.lwtstate = lwtstate_get(lwtstate); 253361adedf3SJiri Benc if (lwtunnel_output_redirect(rt->dst.lwtstate)) { 253461adedf3SJiri Benc rt->dst.lwtstate->orig_output = rt->dst.output; 253561adedf3SJiri Benc rt->dst.output = lwtunnel_output; 253619e42e45SRoopa Prabhu } 253761adedf3SJiri Benc if (lwtunnel_input_redirect(rt->dst.lwtstate)) { 253861adedf3SJiri Benc rt->dst.lwtstate->orig_input = rt->dst.input; 253961adedf3SJiri Benc rt->dst.input = lwtunnel_input; 254025368623STom Herbert } 254125368623STom Herbert } 254219e42e45SRoopa Prabhu 254386872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 254486872cb5SThomas Graf rt->rt6i_dst.plen = cfg->fc_dst_len; 2545afc4eef8SMartin KaFai Lau if (rt->rt6i_dst.plen == 128) 254611d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 25471da177e4SLinus Torvalds 25481da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 254986872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len); 255086872cb5SThomas Graf rt->rt6i_src.plen = cfg->fc_src_len; 25511da177e4SLinus Torvalds #endif 25521da177e4SLinus Torvalds 255386872cb5SThomas Graf rt->rt6i_metric = cfg->fc_metric; 25541da177e4SLinus Torvalds 25551da177e4SLinus Torvalds /* We cannot add true routes via loopback here, 25561da177e4SLinus Torvalds they would result in kernel looping; promote them to reject routes 25571da177e4SLinus Torvalds */ 255886872cb5SThomas Graf if ((cfg->fc_flags & RTF_REJECT) || 255938308473SDavid S. Miller (dev && (dev->flags & IFF_LOOPBACK) && 256038308473SDavid S. Miller !(addr_type & IPV6_ADDR_LOOPBACK) && 256138308473SDavid S. Miller !(cfg->fc_flags & RTF_LOCAL))) { 25621da177e4SLinus Torvalds /* hold loopback dev/idev if we haven't done so. */ 25635578689aSDaniel Lezcano if (dev != net->loopback_dev) { 25641da177e4SLinus Torvalds if (dev) { 25651da177e4SLinus Torvalds dev_put(dev); 25661da177e4SLinus Torvalds in6_dev_put(idev); 25671da177e4SLinus Torvalds } 25685578689aSDaniel Lezcano dev = net->loopback_dev; 25691da177e4SLinus Torvalds dev_hold(dev); 25701da177e4SLinus Torvalds idev = in6_dev_get(dev); 25711da177e4SLinus Torvalds if (!idev) { 25721da177e4SLinus Torvalds err = -ENODEV; 25731da177e4SLinus Torvalds goto out; 25741da177e4SLinus Torvalds } 25751da177e4SLinus Torvalds } 25761da177e4SLinus Torvalds rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP; 2577ef2c7d7bSNicolas Dichtel switch (cfg->fc_type) { 2578ef2c7d7bSNicolas Dichtel case RTN_BLACKHOLE: 2579ef2c7d7bSNicolas Dichtel rt->dst.error = -EINVAL; 2580ede2059dSEric W. Biederman rt->dst.output = dst_discard_out; 25817150aedeSKamala R rt->dst.input = dst_discard; 2582ef2c7d7bSNicolas Dichtel break; 2583ef2c7d7bSNicolas Dichtel case RTN_PROHIBIT: 2584ef2c7d7bSNicolas Dichtel rt->dst.error = -EACCES; 25857150aedeSKamala R rt->dst.output = ip6_pkt_prohibit_out; 25867150aedeSKamala R rt->dst.input = ip6_pkt_prohibit; 2587ef2c7d7bSNicolas Dichtel break; 2588b4949ab2SNicolas Dichtel case RTN_THROW: 25890315e382SNikola Forró case RTN_UNREACHABLE: 2590ef2c7d7bSNicolas Dichtel default: 25917150aedeSKamala R rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN 25920315e382SNikola Forró : (cfg->fc_type == RTN_UNREACHABLE) 25930315e382SNikola Forró ? -EHOSTUNREACH : -ENETUNREACH; 25947150aedeSKamala R rt->dst.output = ip6_pkt_discard_out; 25957150aedeSKamala R rt->dst.input = ip6_pkt_discard; 2596ef2c7d7bSNicolas Dichtel break; 2597ef2c7d7bSNicolas Dichtel } 25981da177e4SLinus Torvalds goto install_route; 25991da177e4SLinus Torvalds } 26001da177e4SLinus Torvalds 260186872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY) { 2602b71d1d42SEric Dumazet const struct in6_addr *gw_addr; 26031da177e4SLinus Torvalds int gwa_type; 26041da177e4SLinus Torvalds 260586872cb5SThomas Graf gw_addr = &cfg->fc_gateway; 2606330567b7SFlorian Westphal gwa_type = ipv6_addr_type(gw_addr); 260748ed7b26SFlorian Westphal 260848ed7b26SFlorian Westphal /* if gw_addr is local we will fail to detect this in case 260948ed7b26SFlorian Westphal * address is still TENTATIVE (DAD in progress). rt6_lookup() 261048ed7b26SFlorian Westphal * will return already-added prefix route via interface that 261148ed7b26SFlorian Westphal * prefix route was assigned to, which might be non-loopback. 261248ed7b26SFlorian Westphal */ 261348ed7b26SFlorian Westphal err = -EINVAL; 2614330567b7SFlorian Westphal if (ipv6_chk_addr_and_flags(net, gw_addr, 2615330567b7SFlorian Westphal gwa_type & IPV6_ADDR_LINKLOCAL ? 2616d5d531cbSDavid Ahern dev : NULL, 0, 0)) { 2617d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid gateway address"); 261848ed7b26SFlorian Westphal goto out; 2619d5d531cbSDavid Ahern } 26204e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = *gw_addr; 26211da177e4SLinus Torvalds 26221da177e4SLinus Torvalds if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) { 26238c14586fSDavid Ahern struct rt6_info *grt = NULL; 26241da177e4SLinus Torvalds 26251da177e4SLinus Torvalds /* IPv6 strictly inhibits using not link-local 26261da177e4SLinus Torvalds addresses as nexthop address. 26271da177e4SLinus Torvalds Otherwise, router will not able to send redirects. 26281da177e4SLinus Torvalds It is very good, but in some (rare!) circumstances 26291da177e4SLinus Torvalds (SIT, PtP, NBMA NOARP links) it is handy to allow 26301da177e4SLinus Torvalds some exceptions. --ANK 263196d5822cSErik Nordmark We allow IPv4-mapped nexthops to support RFC4798-type 263296d5822cSErik Nordmark addressing 26331da177e4SLinus Torvalds */ 263496d5822cSErik Nordmark if (!(gwa_type & (IPV6_ADDR_UNICAST | 2635d5d531cbSDavid Ahern IPV6_ADDR_MAPPED))) { 2636d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2637d5d531cbSDavid Ahern "Invalid gateway address"); 26381da177e4SLinus Torvalds goto out; 2639d5d531cbSDavid Ahern } 26401da177e4SLinus Torvalds 2641a435a07fSVincent Bernat if (cfg->fc_table) { 26428c14586fSDavid Ahern grt = ip6_nh_lookup_table(net, cfg, gw_addr); 26438c14586fSDavid Ahern 2644a435a07fSVincent Bernat if (grt) { 2645a435a07fSVincent Bernat if (grt->rt6i_flags & RTF_GATEWAY || 2646a435a07fSVincent Bernat (dev && dev != grt->dst.dev)) { 2647a435a07fSVincent Bernat ip6_rt_put(grt); 2648a435a07fSVincent Bernat grt = NULL; 2649a435a07fSVincent Bernat } 2650a435a07fSVincent Bernat } 2651a435a07fSVincent Bernat } 2652a435a07fSVincent Bernat 26538c14586fSDavid Ahern if (!grt) 26548c14586fSDavid Ahern grt = rt6_lookup(net, gw_addr, NULL, 26558c14586fSDavid Ahern cfg->fc_ifindex, 1); 26561da177e4SLinus Torvalds 26571da177e4SLinus Torvalds err = -EHOSTUNREACH; 265838308473SDavid S. Miller if (!grt) 26591da177e4SLinus Torvalds goto out; 26601da177e4SLinus Torvalds if (dev) { 2661d1918542SDavid S. Miller if (dev != grt->dst.dev) { 266294e187c0SAmerigo Wang ip6_rt_put(grt); 26631da177e4SLinus Torvalds goto out; 26641da177e4SLinus Torvalds } 26651da177e4SLinus Torvalds } else { 2666d1918542SDavid S. Miller dev = grt->dst.dev; 26671da177e4SLinus Torvalds idev = grt->rt6i_idev; 26681da177e4SLinus Torvalds dev_hold(dev); 26691da177e4SLinus Torvalds in6_dev_hold(grt->rt6i_idev); 26701da177e4SLinus Torvalds } 26711da177e4SLinus Torvalds if (!(grt->rt6i_flags & RTF_GATEWAY)) 26721da177e4SLinus Torvalds err = 0; 267394e187c0SAmerigo Wang ip6_rt_put(grt); 26741da177e4SLinus Torvalds 26751da177e4SLinus Torvalds if (err) 26761da177e4SLinus Torvalds goto out; 26771da177e4SLinus Torvalds } 26781da177e4SLinus Torvalds err = -EINVAL; 2679d5d531cbSDavid Ahern if (!dev) { 2680d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Egress device not specified"); 26811da177e4SLinus Torvalds goto out; 2682d5d531cbSDavid Ahern } else if (dev->flags & IFF_LOOPBACK) { 2683d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2684d5d531cbSDavid Ahern "Egress device can not be loopback device for this route"); 2685d5d531cbSDavid Ahern goto out; 2686d5d531cbSDavid Ahern } 26871da177e4SLinus Torvalds } 26881da177e4SLinus Torvalds 26891da177e4SLinus Torvalds err = -ENODEV; 269038308473SDavid S. Miller if (!dev) 26911da177e4SLinus Torvalds goto out; 26921da177e4SLinus Torvalds 2693c3968a85SDaniel Walter if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 2694c3968a85SDaniel Walter if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 2695d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address"); 2696c3968a85SDaniel Walter err = -EINVAL; 2697c3968a85SDaniel Walter goto out; 2698c3968a85SDaniel Walter } 26994e3fd7a0SAlexey Dobriyan rt->rt6i_prefsrc.addr = cfg->fc_prefsrc; 2700c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 128; 2701c3968a85SDaniel Walter } else 2702c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 2703c3968a85SDaniel Walter 270486872cb5SThomas Graf rt->rt6i_flags = cfg->fc_flags; 27051da177e4SLinus Torvalds 27061da177e4SLinus Torvalds install_route: 2707d8d1f30bSChangli Gao rt->dst.dev = dev; 27081da177e4SLinus Torvalds rt->rt6i_idev = idev; 2709c71099acSThomas Graf rt->rt6i_table = table; 271063152fc0SDaniel Lezcano 2711c346dca1SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = dev_net(dev); 271263152fc0SDaniel Lezcano 27138c5b83f0SRoopa Prabhu return rt; 27141da177e4SLinus Torvalds out: 27151da177e4SLinus Torvalds if (dev) 27161da177e4SLinus Torvalds dev_put(dev); 27171da177e4SLinus Torvalds if (idev) 27181da177e4SLinus Torvalds in6_dev_put(idev); 2719587fea74SWei Wang if (rt) 2720587fea74SWei Wang dst_release_immediate(&rt->dst); 27216b9ea5a6SRoopa Prabhu 27228c5b83f0SRoopa Prabhu return ERR_PTR(err); 27236b9ea5a6SRoopa Prabhu } 27246b9ea5a6SRoopa Prabhu 2725333c4301SDavid Ahern int ip6_route_add(struct fib6_config *cfg, 2726333c4301SDavid Ahern struct netlink_ext_ack *extack) 27276b9ea5a6SRoopa Prabhu { 27286b9ea5a6SRoopa Prabhu struct mx6_config mxc = { .mx = NULL, }; 27298c5b83f0SRoopa Prabhu struct rt6_info *rt; 27306b9ea5a6SRoopa Prabhu int err; 27316b9ea5a6SRoopa Prabhu 2732333c4301SDavid Ahern rt = ip6_route_info_create(cfg, extack); 27338c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 27348c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 27358c5b83f0SRoopa Prabhu rt = NULL; 27366b9ea5a6SRoopa Prabhu goto out; 27378c5b83f0SRoopa Prabhu } 27386b9ea5a6SRoopa Prabhu 27396b9ea5a6SRoopa Prabhu err = ip6_convert_metrics(&mxc, cfg); 27406b9ea5a6SRoopa Prabhu if (err) 27416b9ea5a6SRoopa Prabhu goto out; 27426b9ea5a6SRoopa Prabhu 2743333c4301SDavid Ahern err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, &mxc, extack); 27446b9ea5a6SRoopa Prabhu 27456b9ea5a6SRoopa Prabhu kfree(mxc.mx); 27466b9ea5a6SRoopa Prabhu 27476b9ea5a6SRoopa Prabhu return err; 27486b9ea5a6SRoopa Prabhu out: 2749587fea74SWei Wang if (rt) 2750587fea74SWei Wang dst_release_immediate(&rt->dst); 27516b9ea5a6SRoopa Prabhu 27521da177e4SLinus Torvalds return err; 27531da177e4SLinus Torvalds } 27541da177e4SLinus Torvalds 275586872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info) 27561da177e4SLinus Torvalds { 27571da177e4SLinus Torvalds int err; 2758c71099acSThomas Graf struct fib6_table *table; 2759d1918542SDavid S. Miller struct net *net = dev_net(rt->dst.dev); 27601da177e4SLinus Torvalds 2761a4c2fd7fSWei Wang if (rt == net->ipv6.ip6_null_entry) { 27626825a26cSGao feng err = -ENOENT; 27636825a26cSGao feng goto out; 27646825a26cSGao feng } 27656c813a72SPatrick McHardy 2766c71099acSThomas Graf table = rt->rt6i_table; 2767c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 276886872cb5SThomas Graf err = fib6_del(rt, info); 2769c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 27701da177e4SLinus Torvalds 27716825a26cSGao feng out: 277294e187c0SAmerigo Wang ip6_rt_put(rt); 27731da177e4SLinus Torvalds return err; 27741da177e4SLinus Torvalds } 27751da177e4SLinus Torvalds 2776e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt) 2777e0a1ad73SThomas Graf { 27784d1169c1SDenis V. Lunev struct nl_info info = { 2779d1918542SDavid S. Miller .nl_net = dev_net(rt->dst.dev), 27804d1169c1SDenis V. Lunev }; 2781528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 2782e0a1ad73SThomas Graf } 2783e0a1ad73SThomas Graf 27840ae81335SDavid Ahern static int __ip6_del_rt_siblings(struct rt6_info *rt, struct fib6_config *cfg) 27850ae81335SDavid Ahern { 27860ae81335SDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 2787e3330039SWANG Cong struct net *net = info->nl_net; 278816a16cd3SDavid Ahern struct sk_buff *skb = NULL; 27890ae81335SDavid Ahern struct fib6_table *table; 2790e3330039SWANG Cong int err = -ENOENT; 27910ae81335SDavid Ahern 2792e3330039SWANG Cong if (rt == net->ipv6.ip6_null_entry) 2793e3330039SWANG Cong goto out_put; 27940ae81335SDavid Ahern table = rt->rt6i_table; 27950ae81335SDavid Ahern write_lock_bh(&table->tb6_lock); 27960ae81335SDavid Ahern 27970ae81335SDavid Ahern if (rt->rt6i_nsiblings && cfg->fc_delete_all_nh) { 27980ae81335SDavid Ahern struct rt6_info *sibling, *next_sibling; 27990ae81335SDavid Ahern 280016a16cd3SDavid Ahern /* prefer to send a single notification with all hops */ 280116a16cd3SDavid Ahern skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 280216a16cd3SDavid Ahern if (skb) { 280316a16cd3SDavid Ahern u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 280416a16cd3SDavid Ahern 2805e3330039SWANG Cong if (rt6_fill_node(net, skb, rt, 280616a16cd3SDavid Ahern NULL, NULL, 0, RTM_DELROUTE, 280716a16cd3SDavid Ahern info->portid, seq, 0) < 0) { 280816a16cd3SDavid Ahern kfree_skb(skb); 280916a16cd3SDavid Ahern skb = NULL; 281016a16cd3SDavid Ahern } else 281116a16cd3SDavid Ahern info->skip_notify = 1; 281216a16cd3SDavid Ahern } 281316a16cd3SDavid Ahern 28140ae81335SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 28150ae81335SDavid Ahern &rt->rt6i_siblings, 28160ae81335SDavid Ahern rt6i_siblings) { 28170ae81335SDavid Ahern err = fib6_del(sibling, info); 28180ae81335SDavid Ahern if (err) 2819e3330039SWANG Cong goto out_unlock; 28200ae81335SDavid Ahern } 28210ae81335SDavid Ahern } 28220ae81335SDavid Ahern 28230ae81335SDavid Ahern err = fib6_del(rt, info); 2824e3330039SWANG Cong out_unlock: 28250ae81335SDavid Ahern write_unlock_bh(&table->tb6_lock); 2826e3330039SWANG Cong out_put: 28270ae81335SDavid Ahern ip6_rt_put(rt); 282816a16cd3SDavid Ahern 282916a16cd3SDavid Ahern if (skb) { 2830e3330039SWANG Cong rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 283116a16cd3SDavid Ahern info->nlh, gfp_any()); 283216a16cd3SDavid Ahern } 28330ae81335SDavid Ahern return err; 28340ae81335SDavid Ahern } 28350ae81335SDavid Ahern 2836333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg, 2837333c4301SDavid Ahern struct netlink_ext_ack *extack) 28381da177e4SLinus Torvalds { 28392b760fcfSWei Wang struct rt6_info *rt, *rt_cache; 2840c71099acSThomas Graf struct fib6_table *table; 28411da177e4SLinus Torvalds struct fib6_node *fn; 28421da177e4SLinus Torvalds int err = -ESRCH; 28431da177e4SLinus Torvalds 28445578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 2845d5d531cbSDavid Ahern if (!table) { 2846d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "FIB table does not exist"); 2847c71099acSThomas Graf return err; 2848d5d531cbSDavid Ahern } 28491da177e4SLinus Torvalds 2850c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 2851c71099acSThomas Graf 2852c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 285386872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 285438fbeeeeSWei Wang &cfg->fc_src, cfg->fc_src_len, 28552b760fcfSWei Wang !(cfg->fc_flags & RTF_CACHE)); 28561da177e4SLinus Torvalds 28571da177e4SLinus Torvalds if (fn) { 2858d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 28592b760fcfSWei Wang if (cfg->fc_flags & RTF_CACHE) { 28602b760fcfSWei Wang rt_cache = rt6_find_cached_rt(rt, &cfg->fc_dst, 28612b760fcfSWei Wang &cfg->fc_src); 28622b760fcfSWei Wang if (!rt_cache) 28631f56a01fSMartin KaFai Lau continue; 28642b760fcfSWei Wang rt = rt_cache; 28652b760fcfSWei Wang } 286686872cb5SThomas Graf if (cfg->fc_ifindex && 2867d1918542SDavid S. Miller (!rt->dst.dev || 2868d1918542SDavid S. Miller rt->dst.dev->ifindex != cfg->fc_ifindex)) 28691da177e4SLinus Torvalds continue; 287086872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 287186872cb5SThomas Graf !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 28721da177e4SLinus Torvalds continue; 287386872cb5SThomas Graf if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric) 28741da177e4SLinus Torvalds continue; 2875c2ed1880SMantas M if (cfg->fc_protocol && cfg->fc_protocol != rt->rt6i_protocol) 2876c2ed1880SMantas M continue; 2877d3843fe5SWei Wang if (!dst_hold_safe(&rt->dst)) 2878d3843fe5SWei Wang break; 2879c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 28801da177e4SLinus Torvalds 28810ae81335SDavid Ahern /* if gateway was specified only delete the one hop */ 28820ae81335SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY) 288386872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 28840ae81335SDavid Ahern 28850ae81335SDavid Ahern return __ip6_del_rt_siblings(rt, cfg); 28861da177e4SLinus Torvalds } 28871da177e4SLinus Torvalds } 2888c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 28891da177e4SLinus Torvalds 28901da177e4SLinus Torvalds return err; 28911da177e4SLinus Torvalds } 28921da177e4SLinus Torvalds 28936700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 2894a6279458SYOSHIFUJI Hideaki { 2895a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 2896e8599ff4SDavid S. Miller struct rt6_info *rt, *nrt = NULL; 2897e8599ff4SDavid S. Miller struct ndisc_options ndopts; 2898e8599ff4SDavid S. Miller struct inet6_dev *in6_dev; 2899e8599ff4SDavid S. Miller struct neighbour *neigh; 290071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 struct rd_msg *msg; 29016e157b6aSDavid S. Miller int optlen, on_link; 29026e157b6aSDavid S. Miller u8 *lladdr; 2903e8599ff4SDavid S. Miller 290429a3cad5SSimon Horman optlen = skb_tail_pointer(skb) - skb_transport_header(skb); 290571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 optlen -= sizeof(*msg); 2906e8599ff4SDavid S. Miller 2907e8599ff4SDavid S. Miller if (optlen < 0) { 29086e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); 2909e8599ff4SDavid S. Miller return; 2910e8599ff4SDavid S. Miller } 2911e8599ff4SDavid S. Miller 291271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 msg = (struct rd_msg *)icmp6_hdr(skb); 2913e8599ff4SDavid S. Miller 291471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_is_multicast(&msg->dest)) { 29156e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); 2916e8599ff4SDavid S. Miller return; 2917e8599ff4SDavid S. Miller } 2918e8599ff4SDavid S. Miller 29196e157b6aSDavid S. Miller on_link = 0; 292071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_equal(&msg->dest, &msg->target)) { 2921e8599ff4SDavid S. Miller on_link = 1; 292271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 } else if (ipv6_addr_type(&msg->target) != 2923e8599ff4SDavid S. Miller (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { 29246e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); 2925e8599ff4SDavid S. Miller return; 2926e8599ff4SDavid S. Miller } 2927e8599ff4SDavid S. Miller 2928e8599ff4SDavid S. Miller in6_dev = __in6_dev_get(skb->dev); 2929e8599ff4SDavid S. Miller if (!in6_dev) 2930e8599ff4SDavid S. Miller return; 2931e8599ff4SDavid S. Miller if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) 2932e8599ff4SDavid S. Miller return; 2933e8599ff4SDavid S. Miller 2934e8599ff4SDavid S. Miller /* RFC2461 8.1: 2935e8599ff4SDavid S. Miller * The IP source address of the Redirect MUST be the same as the current 2936e8599ff4SDavid S. Miller * first-hop router for the specified ICMP Destination Address. 2937e8599ff4SDavid S. Miller */ 2938e8599ff4SDavid S. Miller 2939f997c55cSAlexander Aring if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { 2940e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); 2941e8599ff4SDavid S. Miller return; 2942e8599ff4SDavid S. Miller } 29436e157b6aSDavid S. Miller 29446e157b6aSDavid S. Miller lladdr = NULL; 2945e8599ff4SDavid S. Miller if (ndopts.nd_opts_tgt_lladdr) { 2946e8599ff4SDavid S. Miller lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, 2947e8599ff4SDavid S. Miller skb->dev); 2948e8599ff4SDavid S. Miller if (!lladdr) { 2949e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); 2950e8599ff4SDavid S. Miller return; 2951e8599ff4SDavid S. Miller } 2952e8599ff4SDavid S. Miller } 2953e8599ff4SDavid S. Miller 29546e157b6aSDavid S. Miller rt = (struct rt6_info *) dst; 2955ec13ad1dSMatthias Schiffer if (rt->rt6i_flags & RTF_REJECT) { 29566e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); 29576e157b6aSDavid S. Miller return; 29586e157b6aSDavid S. Miller } 29596e157b6aSDavid S. Miller 29606e157b6aSDavid S. Miller /* Redirect received -> path was valid. 29616e157b6aSDavid S. Miller * Look, redirects are sent only in response to data packets, 29626e157b6aSDavid S. Miller * so that this nexthop apparently is reachable. --ANK 29636e157b6aSDavid S. Miller */ 29640dec879fSJulian Anastasov dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); 29656e157b6aSDavid S. Miller 296671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); 2967e8599ff4SDavid S. Miller if (!neigh) 2968e8599ff4SDavid S. Miller return; 2969e8599ff4SDavid S. Miller 29701da177e4SLinus Torvalds /* 29711da177e4SLinus Torvalds * We have finally decided to accept it. 29721da177e4SLinus Torvalds */ 29731da177e4SLinus Torvalds 2974f997c55cSAlexander Aring ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, 29751da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 29761da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 29771da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 2978f997c55cSAlexander Aring NEIGH_UPDATE_F_ISROUTER)), 2979f997c55cSAlexander Aring NDISC_REDIRECT, &ndopts); 29801da177e4SLinus Torvalds 298183a09abdSMartin KaFai Lau nrt = ip6_rt_cache_alloc(rt, &msg->dest, NULL); 298238308473SDavid S. Miller if (!nrt) 29831da177e4SLinus Torvalds goto out; 29841da177e4SLinus Torvalds 29851da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 29861da177e4SLinus Torvalds if (on_link) 29871da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 29881da177e4SLinus Torvalds 2989b91d5329SXin Long nrt->rt6i_protocol = RTPROT_REDIRECT; 29904e3fd7a0SAlexey Dobriyan nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 29911da177e4SLinus Torvalds 29922b760fcfSWei Wang /* No need to remove rt from the exception table if rt is 29932b760fcfSWei Wang * a cached route because rt6_insert_exception() will 29942b760fcfSWei Wang * takes care of it 29952b760fcfSWei Wang */ 29962b760fcfSWei Wang if (rt6_insert_exception(nrt, rt)) { 29972b760fcfSWei Wang dst_release_immediate(&nrt->dst); 29982b760fcfSWei Wang goto out; 29992b760fcfSWei Wang } 30001da177e4SLinus Torvalds 3001d8d1f30bSChangli Gao netevent.old = &rt->dst; 3002d8d1f30bSChangli Gao netevent.new = &nrt->dst; 300371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 netevent.daddr = &msg->dest; 300460592833SYOSHIFUJI Hideaki / 吉藤英明 netevent.neigh = neigh; 30058d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 30068d71740cSTom Tucker 30071da177e4SLinus Torvalds out: 3008e8599ff4SDavid S. Miller neigh_release(neigh); 30096e157b6aSDavid S. Miller } 30106e157b6aSDavid S. Miller 30111da177e4SLinus Torvalds /* 30121da177e4SLinus Torvalds * Misc support functions 30131da177e4SLinus Torvalds */ 30141da177e4SLinus Torvalds 30154b32b5adSMartin KaFai Lau static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from) 30164b32b5adSMartin KaFai Lau { 30174b32b5adSMartin KaFai Lau BUG_ON(from->dst.from); 30184b32b5adSMartin KaFai Lau 30194b32b5adSMartin KaFai Lau rt->rt6i_flags &= ~RTF_EXPIRES; 30204b32b5adSMartin KaFai Lau dst_hold(&from->dst); 30214b32b5adSMartin KaFai Lau rt->dst.from = &from->dst; 30224b32b5adSMartin KaFai Lau dst_init_metrics(&rt->dst, dst_metrics_ptr(&from->dst), true); 30234b32b5adSMartin KaFai Lau } 30244b32b5adSMartin KaFai Lau 302583a09abdSMartin KaFai Lau static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort) 30261da177e4SLinus Torvalds { 3027d8d1f30bSChangli Gao rt->dst.input = ort->dst.input; 3028d8d1f30bSChangli Gao rt->dst.output = ort->dst.output; 302983a09abdSMartin KaFai Lau rt->rt6i_dst = ort->rt6i_dst; 3030d8d1f30bSChangli Gao rt->dst.error = ort->dst.error; 30311da177e4SLinus Torvalds rt->rt6i_idev = ort->rt6i_idev; 30321da177e4SLinus Torvalds if (rt->rt6i_idev) 30331da177e4SLinus Torvalds in6_dev_hold(rt->rt6i_idev); 3034d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 30354e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 30361716a961SGao feng rt->rt6i_flags = ort->rt6i_flags; 30371716a961SGao feng rt6_set_from(rt, ort); 303883a09abdSMartin KaFai Lau rt->rt6i_metric = ort->rt6i_metric; 30391da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 304083a09abdSMartin KaFai Lau rt->rt6i_src = ort->rt6i_src; 30411da177e4SLinus Torvalds #endif 304283a09abdSMartin KaFai Lau rt->rt6i_prefsrc = ort->rt6i_prefsrc; 3043c71099acSThomas Graf rt->rt6i_table = ort->rt6i_table; 304461adedf3SJiri Benc rt->dst.lwtstate = lwtstate_get(ort->dst.lwtstate); 30451da177e4SLinus Torvalds } 30461da177e4SLinus Torvalds 304770ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 3048efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 3049b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 3050830218c1SDavid Ahern const struct in6_addr *gwaddr, 3051830218c1SDavid Ahern struct net_device *dev) 305270ceb4f5SYOSHIFUJI Hideaki { 3053830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; 3054830218c1SDavid Ahern int ifindex = dev->ifindex; 305570ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 305670ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt = NULL; 3057c71099acSThomas Graf struct fib6_table *table; 305870ceb4f5SYOSHIFUJI Hideaki 3059830218c1SDavid Ahern table = fib6_get_table(net, tb_id); 306038308473SDavid S. Miller if (!table) 3061c71099acSThomas Graf return NULL; 3062c71099acSThomas Graf 30635744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 306438fbeeeeSWei Wang fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true); 306570ceb4f5SYOSHIFUJI Hideaki if (!fn) 306670ceb4f5SYOSHIFUJI Hideaki goto out; 306770ceb4f5SYOSHIFUJI Hideaki 3068d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 3069d1918542SDavid S. Miller if (rt->dst.dev->ifindex != ifindex) 307070ceb4f5SYOSHIFUJI Hideaki continue; 307170ceb4f5SYOSHIFUJI Hideaki if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) 307270ceb4f5SYOSHIFUJI Hideaki continue; 307370ceb4f5SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr)) 307470ceb4f5SYOSHIFUJI Hideaki continue; 3075d3843fe5SWei Wang ip6_hold_safe(NULL, &rt, false); 307670ceb4f5SYOSHIFUJI Hideaki break; 307770ceb4f5SYOSHIFUJI Hideaki } 307870ceb4f5SYOSHIFUJI Hideaki out: 30795744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 308070ceb4f5SYOSHIFUJI Hideaki return rt; 308170ceb4f5SYOSHIFUJI Hideaki } 308270ceb4f5SYOSHIFUJI Hideaki 3083efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 3084b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 3085830218c1SDavid Ahern const struct in6_addr *gwaddr, 3086830218c1SDavid Ahern struct net_device *dev, 308795c96174SEric Dumazet unsigned int pref) 308870ceb4f5SYOSHIFUJI Hideaki { 308986872cb5SThomas Graf struct fib6_config cfg = { 3090238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 3091830218c1SDavid Ahern .fc_ifindex = dev->ifindex, 309286872cb5SThomas Graf .fc_dst_len = prefixlen, 309386872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 309486872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 3095b91d5329SXin Long .fc_protocol = RTPROT_RA, 309615e47304SEric W. Biederman .fc_nlinfo.portid = 0, 3097efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 3098efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 309986872cb5SThomas Graf }; 310070ceb4f5SYOSHIFUJI Hideaki 3101830218c1SDavid Ahern cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO, 31024e3fd7a0SAlexey Dobriyan cfg.fc_dst = *prefix; 31034e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 310486872cb5SThomas Graf 3105e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 3106e317da96SYOSHIFUJI Hideaki if (!prefixlen) 310786872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 310870ceb4f5SYOSHIFUJI Hideaki 3109333c4301SDavid Ahern ip6_route_add(&cfg, NULL); 311070ceb4f5SYOSHIFUJI Hideaki 3111830218c1SDavid Ahern return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); 311270ceb4f5SYOSHIFUJI Hideaki } 311370ceb4f5SYOSHIFUJI Hideaki #endif 311470ceb4f5SYOSHIFUJI Hideaki 3115b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev) 31161da177e4SLinus Torvalds { 3117830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; 31181da177e4SLinus Torvalds struct rt6_info *rt; 3119c71099acSThomas Graf struct fib6_table *table; 31201da177e4SLinus Torvalds 3121830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), tb_id); 312238308473SDavid S. Miller if (!table) 3123c71099acSThomas Graf return NULL; 31241da177e4SLinus Torvalds 31255744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 3126d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 3127d1918542SDavid S. Miller if (dev == rt->dst.dev && 3128045927ffSYOSHIFUJI Hideaki ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 31291da177e4SLinus Torvalds ipv6_addr_equal(&rt->rt6i_gateway, addr)) 31301da177e4SLinus Torvalds break; 31311da177e4SLinus Torvalds } 31321da177e4SLinus Torvalds if (rt) 3133d3843fe5SWei Wang ip6_hold_safe(NULL, &rt, false); 31345744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 31351da177e4SLinus Torvalds return rt; 31361da177e4SLinus Torvalds } 31371da177e4SLinus Torvalds 3138b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr, 3139ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 3140ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 31411da177e4SLinus Torvalds { 314286872cb5SThomas Graf struct fib6_config cfg = { 3143ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, 3144238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 314586872cb5SThomas Graf .fc_ifindex = dev->ifindex, 314686872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 314786872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 3148b91d5329SXin Long .fc_protocol = RTPROT_RA, 314915e47304SEric W. Biederman .fc_nlinfo.portid = 0, 31505578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 3151c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 315286872cb5SThomas Graf }; 31531da177e4SLinus Torvalds 31544e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 31551da177e4SLinus Torvalds 3156333c4301SDavid Ahern if (!ip6_route_add(&cfg, NULL)) { 3157830218c1SDavid Ahern struct fib6_table *table; 3158830218c1SDavid Ahern 3159830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), cfg.fc_table); 3160830218c1SDavid Ahern if (table) 3161830218c1SDavid Ahern table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; 3162830218c1SDavid Ahern } 31631da177e4SLinus Torvalds 31641da177e4SLinus Torvalds return rt6_get_dflt_router(gwaddr, dev); 31651da177e4SLinus Torvalds } 31661da177e4SLinus Torvalds 3167830218c1SDavid Ahern static void __rt6_purge_dflt_routers(struct fib6_table *table) 31681da177e4SLinus Torvalds { 31691da177e4SLinus Torvalds struct rt6_info *rt; 31701da177e4SLinus Torvalds 31711da177e4SLinus Torvalds restart: 3172c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 3173d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 31743e8b0ac3SLorenzo Colitti if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) && 31753e8b0ac3SLorenzo Colitti (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) { 3176d3843fe5SWei Wang if (dst_hold_safe(&rt->dst)) { 3177c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 3178e0a1ad73SThomas Graf ip6_del_rt(rt); 3179d3843fe5SWei Wang } else { 3180d3843fe5SWei Wang read_unlock_bh(&table->tb6_lock); 3181d3843fe5SWei Wang } 31821da177e4SLinus Torvalds goto restart; 31831da177e4SLinus Torvalds } 31841da177e4SLinus Torvalds } 3185c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 3186830218c1SDavid Ahern 3187830218c1SDavid Ahern table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; 3188830218c1SDavid Ahern } 3189830218c1SDavid Ahern 3190830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net) 3191830218c1SDavid Ahern { 3192830218c1SDavid Ahern struct fib6_table *table; 3193830218c1SDavid Ahern struct hlist_head *head; 3194830218c1SDavid Ahern unsigned int h; 3195830218c1SDavid Ahern 3196830218c1SDavid Ahern rcu_read_lock(); 3197830218c1SDavid Ahern 3198830218c1SDavid Ahern for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { 3199830218c1SDavid Ahern head = &net->ipv6.fib_table_hash[h]; 3200830218c1SDavid Ahern hlist_for_each_entry_rcu(table, head, tb6_hlist) { 3201830218c1SDavid Ahern if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) 3202830218c1SDavid Ahern __rt6_purge_dflt_routers(table); 3203830218c1SDavid Ahern } 3204830218c1SDavid Ahern } 3205830218c1SDavid Ahern 3206830218c1SDavid Ahern rcu_read_unlock(); 32071da177e4SLinus Torvalds } 32081da177e4SLinus Torvalds 32095578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 32105578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 321186872cb5SThomas Graf struct fib6_config *cfg) 321286872cb5SThomas Graf { 321386872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 321486872cb5SThomas Graf 3215ca254490SDavid Ahern cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? 3216ca254490SDavid Ahern : RT6_TABLE_MAIN; 321786872cb5SThomas Graf cfg->fc_ifindex = rtmsg->rtmsg_ifindex; 321886872cb5SThomas Graf cfg->fc_metric = rtmsg->rtmsg_metric; 321986872cb5SThomas Graf cfg->fc_expires = rtmsg->rtmsg_info; 322086872cb5SThomas Graf cfg->fc_dst_len = rtmsg->rtmsg_dst_len; 322186872cb5SThomas Graf cfg->fc_src_len = rtmsg->rtmsg_src_len; 322286872cb5SThomas Graf cfg->fc_flags = rtmsg->rtmsg_flags; 322386872cb5SThomas Graf 32245578689aSDaniel Lezcano cfg->fc_nlinfo.nl_net = net; 3225f1243c2dSBenjamin Thery 32264e3fd7a0SAlexey Dobriyan cfg->fc_dst = rtmsg->rtmsg_dst; 32274e3fd7a0SAlexey Dobriyan cfg->fc_src = rtmsg->rtmsg_src; 32284e3fd7a0SAlexey Dobriyan cfg->fc_gateway = rtmsg->rtmsg_gateway; 322986872cb5SThomas Graf } 323086872cb5SThomas Graf 32315578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) 32321da177e4SLinus Torvalds { 323386872cb5SThomas Graf struct fib6_config cfg; 32341da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 32351da177e4SLinus Torvalds int err; 32361da177e4SLinus Torvalds 32371da177e4SLinus Torvalds switch (cmd) { 32381da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 32391da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 3240af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 32411da177e4SLinus Torvalds return -EPERM; 32421da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 32431da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 32441da177e4SLinus Torvalds if (err) 32451da177e4SLinus Torvalds return -EFAULT; 32461da177e4SLinus Torvalds 32475578689aSDaniel Lezcano rtmsg_to_fib6_config(net, &rtmsg, &cfg); 324886872cb5SThomas Graf 32491da177e4SLinus Torvalds rtnl_lock(); 32501da177e4SLinus Torvalds switch (cmd) { 32511da177e4SLinus Torvalds case SIOCADDRT: 3252333c4301SDavid Ahern err = ip6_route_add(&cfg, NULL); 32531da177e4SLinus Torvalds break; 32541da177e4SLinus Torvalds case SIOCDELRT: 3255333c4301SDavid Ahern err = ip6_route_del(&cfg, NULL); 32561da177e4SLinus Torvalds break; 32571da177e4SLinus Torvalds default: 32581da177e4SLinus Torvalds err = -EINVAL; 32591da177e4SLinus Torvalds } 32601da177e4SLinus Torvalds rtnl_unlock(); 32611da177e4SLinus Torvalds 32621da177e4SLinus Torvalds return err; 32633ff50b79SStephen Hemminger } 32641da177e4SLinus Torvalds 32651da177e4SLinus Torvalds return -EINVAL; 32661da177e4SLinus Torvalds } 32671da177e4SLinus Torvalds 32681da177e4SLinus Torvalds /* 32691da177e4SLinus Torvalds * Drop the packet on the floor 32701da177e4SLinus Torvalds */ 32711da177e4SLinus Torvalds 3272d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 32731da177e4SLinus Torvalds { 3274612f09e8SYOSHIFUJI Hideaki int type; 3275adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 3276612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 3277612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 32780660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 327945bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 32803bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 32813bd653c8SDenis V. Lunev IPSTATS_MIB_INADDRERRORS); 3282612f09e8SYOSHIFUJI Hideaki break; 3283612f09e8SYOSHIFUJI Hideaki } 3284612f09e8SYOSHIFUJI Hideaki /* FALLTHROUGH */ 3285612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 32863bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 32873bd653c8SDenis V. Lunev ipstats_mib_noroutes); 3288612f09e8SYOSHIFUJI Hideaki break; 3289612f09e8SYOSHIFUJI Hideaki } 32903ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 32911da177e4SLinus Torvalds kfree_skb(skb); 32921da177e4SLinus Torvalds return 0; 32931da177e4SLinus Torvalds } 32941da177e4SLinus Torvalds 32959ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 32969ce8ade0SThomas Graf { 3297612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 32989ce8ade0SThomas Graf } 32999ce8ade0SThomas Graf 3300ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb) 33011da177e4SLinus Torvalds { 3302adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 3303612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 33041da177e4SLinus Torvalds } 33051da177e4SLinus Torvalds 33069ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 33079ce8ade0SThomas Graf { 3308612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 33099ce8ade0SThomas Graf } 33109ce8ade0SThomas Graf 3311ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb) 33129ce8ade0SThomas Graf { 3313adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 3314612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 33159ce8ade0SThomas Graf } 33169ce8ade0SThomas Graf 33171da177e4SLinus Torvalds /* 33181da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 33191da177e4SLinus Torvalds */ 33201da177e4SLinus Torvalds 33211da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, 33221da177e4SLinus Torvalds const struct in6_addr *addr, 33238f031519SDavid S. Miller bool anycast) 33241da177e4SLinus Torvalds { 3325ca254490SDavid Ahern u32 tb_id; 3326c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 33274832c30dSDavid Ahern struct net_device *dev = idev->dev; 33285f02ce24SDavid Ahern struct rt6_info *rt; 33295f02ce24SDavid Ahern 33305f02ce24SDavid Ahern rt = ip6_dst_alloc(net, dev, DST_NOCOUNT); 3331a3300ef4SHannes Frederic Sowa if (!rt) 33321da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 33331da177e4SLinus Torvalds 33341da177e4SLinus Torvalds in6_dev_hold(idev); 33351da177e4SLinus Torvalds 333611d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 3337d8d1f30bSChangli Gao rt->dst.input = ip6_input; 3338d8d1f30bSChangli Gao rt->dst.output = ip6_output; 33391da177e4SLinus Torvalds rt->rt6i_idev = idev; 33401da177e4SLinus Torvalds 334194b5e0f9SDavid Ahern rt->rt6i_protocol = RTPROT_KERNEL; 33421da177e4SLinus Torvalds rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP; 334358c4fb86SYOSHIFUJI Hideaki if (anycast) 334458c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 334558c4fb86SYOSHIFUJI Hideaki else 33461da177e4SLinus Torvalds rt->rt6i_flags |= RTF_LOCAL; 33471da177e4SLinus Torvalds 3348550bab42SJulian Anastasov rt->rt6i_gateway = *addr; 33494e3fd7a0SAlexey Dobriyan rt->rt6i_dst.addr = *addr; 33501da177e4SLinus Torvalds rt->rt6i_dst.plen = 128; 3351ca254490SDavid Ahern tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL; 3352ca254490SDavid Ahern rt->rt6i_table = fib6_get_table(net, tb_id); 33531da177e4SLinus Torvalds 33541da177e4SLinus Torvalds return rt; 33551da177e4SLinus Torvalds } 33561da177e4SLinus Torvalds 3357c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 3358c3968a85SDaniel Walter struct arg_dev_net_ip { 3359c3968a85SDaniel Walter struct net_device *dev; 3360c3968a85SDaniel Walter struct net *net; 3361c3968a85SDaniel Walter struct in6_addr *addr; 3362c3968a85SDaniel Walter }; 3363c3968a85SDaniel Walter 3364c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg) 3365c3968a85SDaniel Walter { 3366c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 3367c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 3368c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 3369c3968a85SDaniel Walter 3370d1918542SDavid S. Miller if (((void *)rt->dst.dev == dev || !dev) && 3371c3968a85SDaniel Walter rt != net->ipv6.ip6_null_entry && 3372c3968a85SDaniel Walter ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) { 337360006a48SWei Wang spin_lock_bh(&rt6_exception_lock); 3374c3968a85SDaniel Walter /* remove prefsrc entry */ 3375c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 337660006a48SWei Wang /* need to update cache as well */ 337760006a48SWei Wang rt6_exceptions_remove_prefsrc(rt); 337860006a48SWei Wang spin_unlock_bh(&rt6_exception_lock); 3379c3968a85SDaniel Walter } 3380c3968a85SDaniel Walter return 0; 3381c3968a85SDaniel Walter } 3382c3968a85SDaniel Walter 3383c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 3384c3968a85SDaniel Walter { 3385c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 3386c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 3387c3968a85SDaniel Walter .dev = ifp->idev->dev, 3388c3968a85SDaniel Walter .net = net, 3389c3968a85SDaniel Walter .addr = &ifp->addr, 3390c3968a85SDaniel Walter }; 33910c3584d5SLi RongQing fib6_clean_all(net, fib6_remove_prefsrc, &adni); 3392c3968a85SDaniel Walter } 3393c3968a85SDaniel Walter 3394be7a010dSDuan Jiong #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY) 3395be7a010dSDuan Jiong 3396be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */ 3397be7a010dSDuan Jiong static int fib6_clean_tohost(struct rt6_info *rt, void *arg) 3398be7a010dSDuan Jiong { 3399be7a010dSDuan Jiong struct in6_addr *gateway = (struct in6_addr *)arg; 3400be7a010dSDuan Jiong 34012b760fcfSWei Wang if (((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) && 3402be7a010dSDuan Jiong ipv6_addr_equal(gateway, &rt->rt6i_gateway)) { 3403be7a010dSDuan Jiong return -1; 3404be7a010dSDuan Jiong } 3405b16cb459SWei Wang 3406b16cb459SWei Wang /* Further clean up cached routes in exception table. 3407b16cb459SWei Wang * This is needed because cached route may have a different 3408b16cb459SWei Wang * gateway than its 'parent' in the case of an ip redirect. 3409b16cb459SWei Wang */ 3410b16cb459SWei Wang rt6_exceptions_clean_tohost(rt, gateway); 3411b16cb459SWei Wang 3412be7a010dSDuan Jiong return 0; 3413be7a010dSDuan Jiong } 3414be7a010dSDuan Jiong 3415be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) 3416be7a010dSDuan Jiong { 3417be7a010dSDuan Jiong fib6_clean_all(net, fib6_clean_tohost, gateway); 3418be7a010dSDuan Jiong } 3419be7a010dSDuan Jiong 34208ed67789SDaniel Lezcano struct arg_dev_net { 34218ed67789SDaniel Lezcano struct net_device *dev; 34228ed67789SDaniel Lezcano struct net *net; 34238ed67789SDaniel Lezcano }; 34248ed67789SDaniel Lezcano 3425a1a22c12SDavid Ahern /* called with write lock held for table with rt */ 34261da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg) 34271da177e4SLinus Torvalds { 3428bc3ef660Sstephen hemminger const struct arg_dev_net *adn = arg; 3429bc3ef660Sstephen hemminger const struct net_device *dev = adn->dev; 34308ed67789SDaniel Lezcano 3431d1918542SDavid S. Miller if ((rt->dst.dev == dev || !dev) && 3432a1a22c12SDavid Ahern rt != adn->net->ipv6.ip6_null_entry && 3433a1a22c12SDavid Ahern (rt->rt6i_nsiblings == 0 || 34348397ed36SDavid Ahern (dev && netdev_unregistering(dev)) || 3435a1a22c12SDavid Ahern !rt->rt6i_idev->cnf.ignore_routes_with_linkdown)) 34361da177e4SLinus Torvalds return -1; 3437c159d30cSDavid S. Miller 34381da177e4SLinus Torvalds return 0; 34391da177e4SLinus Torvalds } 34401da177e4SLinus Torvalds 3441f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev) 34421da177e4SLinus Torvalds { 34438ed67789SDaniel Lezcano struct arg_dev_net adn = { 34448ed67789SDaniel Lezcano .dev = dev, 34458ed67789SDaniel Lezcano .net = net, 34468ed67789SDaniel Lezcano }; 34478ed67789SDaniel Lezcano 34480c3584d5SLi RongQing fib6_clean_all(net, fib6_ifdown, &adn); 3449e332bc67SEric W. Biederman if (dev) 34508d0b94afSMartin KaFai Lau rt6_uncached_list_flush_dev(net, dev); 34511da177e4SLinus Torvalds } 34521da177e4SLinus Torvalds 345395c96174SEric Dumazet struct rt6_mtu_change_arg { 34541da177e4SLinus Torvalds struct net_device *dev; 345595c96174SEric Dumazet unsigned int mtu; 34561da177e4SLinus Torvalds }; 34571da177e4SLinus Torvalds 34581da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg) 34591da177e4SLinus Torvalds { 34601da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 34611da177e4SLinus Torvalds struct inet6_dev *idev; 34621da177e4SLinus Torvalds 34631da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 34641da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 34651da177e4SLinus Torvalds We still use this lock to block changes 34661da177e4SLinus Torvalds caused by addrconf/ndisc. 34671da177e4SLinus Torvalds */ 34681da177e4SLinus Torvalds 34691da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 347038308473SDavid S. Miller if (!idev) 34711da177e4SLinus Torvalds return 0; 34721da177e4SLinus Torvalds 34731da177e4SLinus Torvalds /* For administrative MTU increase, there is no way to discover 34741da177e4SLinus Torvalds IPv6 PMTU increase, so PMTU increase should be updated here. 34751da177e4SLinus Torvalds Since RFC 1981 doesn't include administrative MTU increase 34761da177e4SLinus Torvalds update PMTU increase is a MUST. (i.e. jumbo frame) 34771da177e4SLinus Torvalds */ 34781da177e4SLinus Torvalds /* 34791da177e4SLinus Torvalds If new MTU is less than route PMTU, this new MTU will be the 34801da177e4SLinus Torvalds lowest MTU in the path, update the route PMTU to reflect PMTU 34811da177e4SLinus Torvalds decreases; if new MTU is greater than route PMTU, and the 34821da177e4SLinus Torvalds old MTU is the lowest MTU in the path, update the route PMTU 34831da177e4SLinus Torvalds to reflect the increase. In this case if the other nodes' MTU 34841da177e4SLinus Torvalds also have the lowest MTU, TOO BIG MESSAGE will be lead to 348567c408cfSAlexander Alemayhu PMTU discovery. 34861da177e4SLinus Torvalds */ 3487d1918542SDavid S. Miller if (rt->dst.dev == arg->dev && 3488fb56be83SMaciej Żenczykowski dst_metric_raw(&rt->dst, RTAX_MTU) && 34894b32b5adSMartin KaFai Lau !dst_metric_locked(&rt->dst, RTAX_MTU)) { 3490f5bbe7eeSWei Wang spin_lock_bh(&rt6_exception_lock); 34912b760fcfSWei Wang if (dst_mtu(&rt->dst) >= arg->mtu || 3492d8d1f30bSChangli Gao (dst_mtu(&rt->dst) < arg->mtu && 34934b32b5adSMartin KaFai Lau dst_mtu(&rt->dst) == idev->cnf.mtu6)) { 3494defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu); 3495566cfd8fSSimon Arlott } 3496f5bbe7eeSWei Wang rt6_exceptions_update_pmtu(rt, arg->mtu); 3497f5bbe7eeSWei Wang spin_unlock_bh(&rt6_exception_lock); 34984b32b5adSMartin KaFai Lau } 34991da177e4SLinus Torvalds return 0; 35001da177e4SLinus Torvalds } 35011da177e4SLinus Torvalds 350295c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu) 35031da177e4SLinus Torvalds { 3504c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 3505c71099acSThomas Graf .dev = dev, 3506c71099acSThomas Graf .mtu = mtu, 3507c71099acSThomas Graf }; 35081da177e4SLinus Torvalds 35090c3584d5SLi RongQing fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); 35101da177e4SLinus Torvalds } 35111da177e4SLinus Torvalds 3512ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 35135176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 351486872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 3515ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 351686872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 351786872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 351851ebd318SNicolas Dichtel [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 3519c78ba6d6SLubomir Rintel [RTA_PREF] = { .type = NLA_U8 }, 352019e42e45SRoopa Prabhu [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, 352119e42e45SRoopa Prabhu [RTA_ENCAP] = { .type = NLA_NESTED }, 352232bc201eSXin Long [RTA_EXPIRES] = { .type = NLA_U32 }, 3523622ec2c9SLorenzo Colitti [RTA_UID] = { .type = NLA_U32 }, 35243b45a410SLiping Zhang [RTA_MARK] = { .type = NLA_U32 }, 352586872cb5SThomas Graf }; 352686872cb5SThomas Graf 352786872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 3528333c4301SDavid Ahern struct fib6_config *cfg, 3529333c4301SDavid Ahern struct netlink_ext_ack *extack) 35301da177e4SLinus Torvalds { 353186872cb5SThomas Graf struct rtmsg *rtm; 353286872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 3533c78ba6d6SLubomir Rintel unsigned int pref; 353486872cb5SThomas Graf int err; 35351da177e4SLinus Torvalds 3536fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 3537fceb6435SJohannes Berg NULL); 353886872cb5SThomas Graf if (err < 0) 353986872cb5SThomas Graf goto errout; 35401da177e4SLinus Torvalds 354186872cb5SThomas Graf err = -EINVAL; 354286872cb5SThomas Graf rtm = nlmsg_data(nlh); 354386872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 354486872cb5SThomas Graf 354586872cb5SThomas Graf cfg->fc_table = rtm->rtm_table; 354686872cb5SThomas Graf cfg->fc_dst_len = rtm->rtm_dst_len; 354786872cb5SThomas Graf cfg->fc_src_len = rtm->rtm_src_len; 354886872cb5SThomas Graf cfg->fc_flags = RTF_UP; 354986872cb5SThomas Graf cfg->fc_protocol = rtm->rtm_protocol; 3550ef2c7d7bSNicolas Dichtel cfg->fc_type = rtm->rtm_type; 355186872cb5SThomas Graf 3552ef2c7d7bSNicolas Dichtel if (rtm->rtm_type == RTN_UNREACHABLE || 3553ef2c7d7bSNicolas Dichtel rtm->rtm_type == RTN_BLACKHOLE || 3554b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_PROHIBIT || 3555b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_THROW) 355686872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 355786872cb5SThomas Graf 3558ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 3559ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 3560ab79ad14SMaciej Żenczykowski 35611f56a01fSMartin KaFai Lau if (rtm->rtm_flags & RTM_F_CLONED) 35621f56a01fSMartin KaFai Lau cfg->fc_flags |= RTF_CACHE; 35631f56a01fSMartin KaFai Lau 356415e47304SEric W. Biederman cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid; 356586872cb5SThomas Graf cfg->fc_nlinfo.nlh = nlh; 35663b1e0a65SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = sock_net(skb->sk); 356786872cb5SThomas Graf 356886872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 356967b61f6cSJiri Benc cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); 357086872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 35711da177e4SLinus Torvalds } 357286872cb5SThomas Graf 357386872cb5SThomas Graf if (tb[RTA_DST]) { 357486872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 357586872cb5SThomas Graf 357686872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 357786872cb5SThomas Graf goto errout; 357886872cb5SThomas Graf 357986872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 35801da177e4SLinus Torvalds } 358186872cb5SThomas Graf 358286872cb5SThomas Graf if (tb[RTA_SRC]) { 358386872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 358486872cb5SThomas Graf 358586872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 358686872cb5SThomas Graf goto errout; 358786872cb5SThomas Graf 358886872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 35891da177e4SLinus Torvalds } 359086872cb5SThomas Graf 3591c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 359267b61f6cSJiri Benc cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); 3593c3968a85SDaniel Walter 359486872cb5SThomas Graf if (tb[RTA_OIF]) 359586872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 359686872cb5SThomas Graf 359786872cb5SThomas Graf if (tb[RTA_PRIORITY]) 359886872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 359986872cb5SThomas Graf 360086872cb5SThomas Graf if (tb[RTA_METRICS]) { 360186872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 360286872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 36031da177e4SLinus Torvalds } 360486872cb5SThomas Graf 360586872cb5SThomas Graf if (tb[RTA_TABLE]) 360686872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 360786872cb5SThomas Graf 360851ebd318SNicolas Dichtel if (tb[RTA_MULTIPATH]) { 360951ebd318SNicolas Dichtel cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); 361051ebd318SNicolas Dichtel cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); 36119ed59592SDavid Ahern 36129ed59592SDavid Ahern err = lwtunnel_valid_encap_type_attr(cfg->fc_mp, 3613c255bd68SDavid Ahern cfg->fc_mp_len, extack); 36149ed59592SDavid Ahern if (err < 0) 36159ed59592SDavid Ahern goto errout; 361651ebd318SNicolas Dichtel } 361751ebd318SNicolas Dichtel 3618c78ba6d6SLubomir Rintel if (tb[RTA_PREF]) { 3619c78ba6d6SLubomir Rintel pref = nla_get_u8(tb[RTA_PREF]); 3620c78ba6d6SLubomir Rintel if (pref != ICMPV6_ROUTER_PREF_LOW && 3621c78ba6d6SLubomir Rintel pref != ICMPV6_ROUTER_PREF_HIGH) 3622c78ba6d6SLubomir Rintel pref = ICMPV6_ROUTER_PREF_MEDIUM; 3623c78ba6d6SLubomir Rintel cfg->fc_flags |= RTF_PREF(pref); 3624c78ba6d6SLubomir Rintel } 3625c78ba6d6SLubomir Rintel 362619e42e45SRoopa Prabhu if (tb[RTA_ENCAP]) 362719e42e45SRoopa Prabhu cfg->fc_encap = tb[RTA_ENCAP]; 362819e42e45SRoopa Prabhu 36299ed59592SDavid Ahern if (tb[RTA_ENCAP_TYPE]) { 363019e42e45SRoopa Prabhu cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); 363119e42e45SRoopa Prabhu 3632c255bd68SDavid Ahern err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); 36339ed59592SDavid Ahern if (err < 0) 36349ed59592SDavid Ahern goto errout; 36359ed59592SDavid Ahern } 36369ed59592SDavid Ahern 363732bc201eSXin Long if (tb[RTA_EXPIRES]) { 363832bc201eSXin Long unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); 363932bc201eSXin Long 364032bc201eSXin Long if (addrconf_finite_timeout(timeout)) { 364132bc201eSXin Long cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); 364232bc201eSXin Long cfg->fc_flags |= RTF_EXPIRES; 364332bc201eSXin Long } 364432bc201eSXin Long } 364532bc201eSXin Long 364686872cb5SThomas Graf err = 0; 364786872cb5SThomas Graf errout: 364886872cb5SThomas Graf return err; 36491da177e4SLinus Torvalds } 36501da177e4SLinus Torvalds 36516b9ea5a6SRoopa Prabhu struct rt6_nh { 36526b9ea5a6SRoopa Prabhu struct rt6_info *rt6_info; 36536b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 36546b9ea5a6SRoopa Prabhu struct mx6_config mxc; 36556b9ea5a6SRoopa Prabhu struct list_head next; 36566b9ea5a6SRoopa Prabhu }; 36576b9ea5a6SRoopa Prabhu 36586b9ea5a6SRoopa Prabhu static void ip6_print_replace_route_err(struct list_head *rt6_nh_list) 36596b9ea5a6SRoopa Prabhu { 36606b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 36616b9ea5a6SRoopa Prabhu 36626b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 36637d4d5065SDavid Ahern pr_warn("IPV6: multipath route replace failed (check consistency of installed routes): %pI6c nexthop %pI6c ifi %d\n", 36646b9ea5a6SRoopa Prabhu &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway, 36656b9ea5a6SRoopa Prabhu nh->r_cfg.fc_ifindex); 36666b9ea5a6SRoopa Prabhu } 36676b9ea5a6SRoopa Prabhu } 36686b9ea5a6SRoopa Prabhu 36696b9ea5a6SRoopa Prabhu static int ip6_route_info_append(struct list_head *rt6_nh_list, 36706b9ea5a6SRoopa Prabhu struct rt6_info *rt, struct fib6_config *r_cfg) 36716b9ea5a6SRoopa Prabhu { 36726b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 36736b9ea5a6SRoopa Prabhu int err = -EEXIST; 36746b9ea5a6SRoopa Prabhu 36756b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 36766b9ea5a6SRoopa Prabhu /* check if rt6_info already exists */ 3677f06b7549SDavid Ahern if (rt6_duplicate_nexthop(nh->rt6_info, rt)) 36786b9ea5a6SRoopa Prabhu return err; 36796b9ea5a6SRoopa Prabhu } 36806b9ea5a6SRoopa Prabhu 36816b9ea5a6SRoopa Prabhu nh = kzalloc(sizeof(*nh), GFP_KERNEL); 36826b9ea5a6SRoopa Prabhu if (!nh) 36836b9ea5a6SRoopa Prabhu return -ENOMEM; 36846b9ea5a6SRoopa Prabhu nh->rt6_info = rt; 36856b9ea5a6SRoopa Prabhu err = ip6_convert_metrics(&nh->mxc, r_cfg); 36866b9ea5a6SRoopa Prabhu if (err) { 36876b9ea5a6SRoopa Prabhu kfree(nh); 36886b9ea5a6SRoopa Prabhu return err; 36896b9ea5a6SRoopa Prabhu } 36906b9ea5a6SRoopa Prabhu memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); 36916b9ea5a6SRoopa Prabhu list_add_tail(&nh->next, rt6_nh_list); 36926b9ea5a6SRoopa Prabhu 36936b9ea5a6SRoopa Prabhu return 0; 36946b9ea5a6SRoopa Prabhu } 36956b9ea5a6SRoopa Prabhu 36963b1137feSDavid Ahern static void ip6_route_mpath_notify(struct rt6_info *rt, 36973b1137feSDavid Ahern struct rt6_info *rt_last, 36983b1137feSDavid Ahern struct nl_info *info, 36993b1137feSDavid Ahern __u16 nlflags) 37003b1137feSDavid Ahern { 37013b1137feSDavid Ahern /* if this is an APPEND route, then rt points to the first route 37023b1137feSDavid Ahern * inserted and rt_last points to last route inserted. Userspace 37033b1137feSDavid Ahern * wants a consistent dump of the route which starts at the first 37043b1137feSDavid Ahern * nexthop. Since sibling routes are always added at the end of 37053b1137feSDavid Ahern * the list, find the first sibling of the last route appended 37063b1137feSDavid Ahern */ 37073b1137feSDavid Ahern if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->rt6i_nsiblings) { 37083b1137feSDavid Ahern rt = list_first_entry(&rt_last->rt6i_siblings, 37093b1137feSDavid Ahern struct rt6_info, 37103b1137feSDavid Ahern rt6i_siblings); 37113b1137feSDavid Ahern } 37123b1137feSDavid Ahern 37133b1137feSDavid Ahern if (rt) 37143b1137feSDavid Ahern inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); 37153b1137feSDavid Ahern } 37163b1137feSDavid Ahern 3717333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg, 3718333c4301SDavid Ahern struct netlink_ext_ack *extack) 371951ebd318SNicolas Dichtel { 37203b1137feSDavid Ahern struct rt6_info *rt_notif = NULL, *rt_last = NULL; 37213b1137feSDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 372251ebd318SNicolas Dichtel struct fib6_config r_cfg; 372351ebd318SNicolas Dichtel struct rtnexthop *rtnh; 37246b9ea5a6SRoopa Prabhu struct rt6_info *rt; 37256b9ea5a6SRoopa Prabhu struct rt6_nh *err_nh; 37266b9ea5a6SRoopa Prabhu struct rt6_nh *nh, *nh_safe; 37273b1137feSDavid Ahern __u16 nlflags; 372851ebd318SNicolas Dichtel int remaining; 372951ebd318SNicolas Dichtel int attrlen; 37306b9ea5a6SRoopa Prabhu int err = 1; 37316b9ea5a6SRoopa Prabhu int nhn = 0; 37326b9ea5a6SRoopa Prabhu int replace = (cfg->fc_nlinfo.nlh && 37336b9ea5a6SRoopa Prabhu (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); 37346b9ea5a6SRoopa Prabhu LIST_HEAD(rt6_nh_list); 373551ebd318SNicolas Dichtel 37363b1137feSDavid Ahern nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; 37373b1137feSDavid Ahern if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) 37383b1137feSDavid Ahern nlflags |= NLM_F_APPEND; 37393b1137feSDavid Ahern 374035f1b4e9SMichal Kubeček remaining = cfg->fc_mp_len; 374151ebd318SNicolas Dichtel rtnh = (struct rtnexthop *)cfg->fc_mp; 374251ebd318SNicolas Dichtel 37436b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry and build a list (rt6_nh_list) of 37446b9ea5a6SRoopa Prabhu * rt6_info structs per nexthop 37456b9ea5a6SRoopa Prabhu */ 374651ebd318SNicolas Dichtel while (rtnh_ok(rtnh, remaining)) { 374751ebd318SNicolas Dichtel memcpy(&r_cfg, cfg, sizeof(*cfg)); 374851ebd318SNicolas Dichtel if (rtnh->rtnh_ifindex) 374951ebd318SNicolas Dichtel r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 375051ebd318SNicolas Dichtel 375151ebd318SNicolas Dichtel attrlen = rtnh_attrlen(rtnh); 375251ebd318SNicolas Dichtel if (attrlen > 0) { 375351ebd318SNicolas Dichtel struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 375451ebd318SNicolas Dichtel 375551ebd318SNicolas Dichtel nla = nla_find(attrs, attrlen, RTA_GATEWAY); 375651ebd318SNicolas Dichtel if (nla) { 375767b61f6cSJiri Benc r_cfg.fc_gateway = nla_get_in6_addr(nla); 375851ebd318SNicolas Dichtel r_cfg.fc_flags |= RTF_GATEWAY; 375951ebd318SNicolas Dichtel } 376019e42e45SRoopa Prabhu r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); 376119e42e45SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); 376219e42e45SRoopa Prabhu if (nla) 376319e42e45SRoopa Prabhu r_cfg.fc_encap_type = nla_get_u16(nla); 376451ebd318SNicolas Dichtel } 37656b9ea5a6SRoopa Prabhu 3766333c4301SDavid Ahern rt = ip6_route_info_create(&r_cfg, extack); 37678c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 37688c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 37698c5b83f0SRoopa Prabhu rt = NULL; 37706b9ea5a6SRoopa Prabhu goto cleanup; 37718c5b83f0SRoopa Prabhu } 37726b9ea5a6SRoopa Prabhu 37736b9ea5a6SRoopa Prabhu err = ip6_route_info_append(&rt6_nh_list, rt, &r_cfg); 377451ebd318SNicolas Dichtel if (err) { 3775587fea74SWei Wang dst_release_immediate(&rt->dst); 37766b9ea5a6SRoopa Prabhu goto cleanup; 377751ebd318SNicolas Dichtel } 37786b9ea5a6SRoopa Prabhu 37796b9ea5a6SRoopa Prabhu rtnh = rtnh_next(rtnh, &remaining); 378051ebd318SNicolas Dichtel } 37816b9ea5a6SRoopa Prabhu 37823b1137feSDavid Ahern /* for add and replace send one notification with all nexthops. 37833b1137feSDavid Ahern * Skip the notification in fib6_add_rt2node and send one with 37843b1137feSDavid Ahern * the full route when done 37853b1137feSDavid Ahern */ 37863b1137feSDavid Ahern info->skip_notify = 1; 37873b1137feSDavid Ahern 37886b9ea5a6SRoopa Prabhu err_nh = NULL; 37896b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 37903b1137feSDavid Ahern rt_last = nh->rt6_info; 3791333c4301SDavid Ahern err = __ip6_ins_rt(nh->rt6_info, info, &nh->mxc, extack); 37923b1137feSDavid Ahern /* save reference to first route for notification */ 37933b1137feSDavid Ahern if (!rt_notif && !err) 37943b1137feSDavid Ahern rt_notif = nh->rt6_info; 37953b1137feSDavid Ahern 37966b9ea5a6SRoopa Prabhu /* nh->rt6_info is used or freed at this point, reset to NULL*/ 37976b9ea5a6SRoopa Prabhu nh->rt6_info = NULL; 37986b9ea5a6SRoopa Prabhu if (err) { 37996b9ea5a6SRoopa Prabhu if (replace && nhn) 38006b9ea5a6SRoopa Prabhu ip6_print_replace_route_err(&rt6_nh_list); 38016b9ea5a6SRoopa Prabhu err_nh = nh; 38026b9ea5a6SRoopa Prabhu goto add_errout; 38036b9ea5a6SRoopa Prabhu } 38046b9ea5a6SRoopa Prabhu 38051a72418bSNicolas Dichtel /* Because each route is added like a single route we remove 380627596472SMichal Kubeček * these flags after the first nexthop: if there is a collision, 380727596472SMichal Kubeček * we have already failed to add the first nexthop: 380827596472SMichal Kubeček * fib6_add_rt2node() has rejected it; when replacing, old 380927596472SMichal Kubeček * nexthops have been replaced by first new, the rest should 381027596472SMichal Kubeček * be added to it. 38111a72418bSNicolas Dichtel */ 381227596472SMichal Kubeček cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | 381327596472SMichal Kubeček NLM_F_REPLACE); 38146b9ea5a6SRoopa Prabhu nhn++; 38156b9ea5a6SRoopa Prabhu } 38166b9ea5a6SRoopa Prabhu 38173b1137feSDavid Ahern /* success ... tell user about new route */ 38183b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 38196b9ea5a6SRoopa Prabhu goto cleanup; 38206b9ea5a6SRoopa Prabhu 38216b9ea5a6SRoopa Prabhu add_errout: 38223b1137feSDavid Ahern /* send notification for routes that were added so that 38233b1137feSDavid Ahern * the delete notifications sent by ip6_route_del are 38243b1137feSDavid Ahern * coherent 38253b1137feSDavid Ahern */ 38263b1137feSDavid Ahern if (rt_notif) 38273b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 38283b1137feSDavid Ahern 38296b9ea5a6SRoopa Prabhu /* Delete routes that were already added */ 38306b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 38316b9ea5a6SRoopa Prabhu if (err_nh == nh) 38326b9ea5a6SRoopa Prabhu break; 3833333c4301SDavid Ahern ip6_route_del(&nh->r_cfg, extack); 38346b9ea5a6SRoopa Prabhu } 38356b9ea5a6SRoopa Prabhu 38366b9ea5a6SRoopa Prabhu cleanup: 38376b9ea5a6SRoopa Prabhu list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) { 3838587fea74SWei Wang if (nh->rt6_info) 3839587fea74SWei Wang dst_release_immediate(&nh->rt6_info->dst); 38406b9ea5a6SRoopa Prabhu kfree(nh->mxc.mx); 38416b9ea5a6SRoopa Prabhu list_del(&nh->next); 38426b9ea5a6SRoopa Prabhu kfree(nh); 38436b9ea5a6SRoopa Prabhu } 38446b9ea5a6SRoopa Prabhu 38456b9ea5a6SRoopa Prabhu return err; 38466b9ea5a6SRoopa Prabhu } 38476b9ea5a6SRoopa Prabhu 3848333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg, 3849333c4301SDavid Ahern struct netlink_ext_ack *extack) 38506b9ea5a6SRoopa Prabhu { 38516b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 38526b9ea5a6SRoopa Prabhu struct rtnexthop *rtnh; 38536b9ea5a6SRoopa Prabhu int remaining; 38546b9ea5a6SRoopa Prabhu int attrlen; 38556b9ea5a6SRoopa Prabhu int err = 1, last_err = 0; 38566b9ea5a6SRoopa Prabhu 38576b9ea5a6SRoopa Prabhu remaining = cfg->fc_mp_len; 38586b9ea5a6SRoopa Prabhu rtnh = (struct rtnexthop *)cfg->fc_mp; 38596b9ea5a6SRoopa Prabhu 38606b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry */ 38616b9ea5a6SRoopa Prabhu while (rtnh_ok(rtnh, remaining)) { 38626b9ea5a6SRoopa Prabhu memcpy(&r_cfg, cfg, sizeof(*cfg)); 38636b9ea5a6SRoopa Prabhu if (rtnh->rtnh_ifindex) 38646b9ea5a6SRoopa Prabhu r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 38656b9ea5a6SRoopa Prabhu 38666b9ea5a6SRoopa Prabhu attrlen = rtnh_attrlen(rtnh); 38676b9ea5a6SRoopa Prabhu if (attrlen > 0) { 38686b9ea5a6SRoopa Prabhu struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 38696b9ea5a6SRoopa Prabhu 38706b9ea5a6SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_GATEWAY); 38716b9ea5a6SRoopa Prabhu if (nla) { 38726b9ea5a6SRoopa Prabhu nla_memcpy(&r_cfg.fc_gateway, nla, 16); 38736b9ea5a6SRoopa Prabhu r_cfg.fc_flags |= RTF_GATEWAY; 38746b9ea5a6SRoopa Prabhu } 38756b9ea5a6SRoopa Prabhu } 3876333c4301SDavid Ahern err = ip6_route_del(&r_cfg, extack); 38776b9ea5a6SRoopa Prabhu if (err) 38786b9ea5a6SRoopa Prabhu last_err = err; 38796b9ea5a6SRoopa Prabhu 388051ebd318SNicolas Dichtel rtnh = rtnh_next(rtnh, &remaining); 388151ebd318SNicolas Dichtel } 388251ebd318SNicolas Dichtel 388351ebd318SNicolas Dichtel return last_err; 388451ebd318SNicolas Dichtel } 388551ebd318SNicolas Dichtel 3886c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, 3887c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 38881da177e4SLinus Torvalds { 388986872cb5SThomas Graf struct fib6_config cfg; 389086872cb5SThomas Graf int err; 38911da177e4SLinus Torvalds 3892333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 389386872cb5SThomas Graf if (err < 0) 389486872cb5SThomas Graf return err; 389586872cb5SThomas Graf 389651ebd318SNicolas Dichtel if (cfg.fc_mp) 3897333c4301SDavid Ahern return ip6_route_multipath_del(&cfg, extack); 38980ae81335SDavid Ahern else { 38990ae81335SDavid Ahern cfg.fc_delete_all_nh = 1; 3900333c4301SDavid Ahern return ip6_route_del(&cfg, extack); 39011da177e4SLinus Torvalds } 39020ae81335SDavid Ahern } 39031da177e4SLinus Torvalds 3904c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, 3905c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 39061da177e4SLinus Torvalds { 390786872cb5SThomas Graf struct fib6_config cfg; 390886872cb5SThomas Graf int err; 39091da177e4SLinus Torvalds 3910333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 391186872cb5SThomas Graf if (err < 0) 391286872cb5SThomas Graf return err; 391386872cb5SThomas Graf 391451ebd318SNicolas Dichtel if (cfg.fc_mp) 3915333c4301SDavid Ahern return ip6_route_multipath_add(&cfg, extack); 391651ebd318SNicolas Dichtel else 3917333c4301SDavid Ahern return ip6_route_add(&cfg, extack); 39181da177e4SLinus Torvalds } 39191da177e4SLinus Torvalds 3920beb1afacSDavid Ahern static size_t rt6_nlmsg_size(struct rt6_info *rt) 3921339bf98fSThomas Graf { 3922beb1afacSDavid Ahern int nexthop_len = 0; 3923beb1afacSDavid Ahern 3924beb1afacSDavid Ahern if (rt->rt6i_nsiblings) { 3925beb1afacSDavid Ahern nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */ 3926beb1afacSDavid Ahern + NLA_ALIGN(sizeof(struct rtnexthop)) 3927beb1afacSDavid Ahern + nla_total_size(16) /* RTA_GATEWAY */ 3928beb1afacSDavid Ahern + lwtunnel_get_encap_size(rt->dst.lwtstate); 3929beb1afacSDavid Ahern 3930beb1afacSDavid Ahern nexthop_len *= rt->rt6i_nsiblings; 3931beb1afacSDavid Ahern } 3932beb1afacSDavid Ahern 3933339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 3934339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 3935339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 3936339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 3937339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 3938339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 3939339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 3940339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 3941339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 39426a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 3943ea697639SDaniel Borkmann + nla_total_size(sizeof(struct rta_cacheinfo)) 3944c78ba6d6SLubomir Rintel + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ 394519e42e45SRoopa Prabhu + nla_total_size(1) /* RTA_PREF */ 3946beb1afacSDavid Ahern + lwtunnel_get_encap_size(rt->dst.lwtstate) 3947beb1afacSDavid Ahern + nexthop_len; 3948beb1afacSDavid Ahern } 3949beb1afacSDavid Ahern 3950beb1afacSDavid Ahern static int rt6_nexthop_info(struct sk_buff *skb, struct rt6_info *rt, 39515be083ceSDavid Ahern unsigned int *flags, bool skip_oif) 3952beb1afacSDavid Ahern { 3953beb1afacSDavid Ahern if (!netif_running(rt->dst.dev) || !netif_carrier_ok(rt->dst.dev)) { 3954beb1afacSDavid Ahern *flags |= RTNH_F_LINKDOWN; 3955beb1afacSDavid Ahern if (rt->rt6i_idev->cnf.ignore_routes_with_linkdown) 3956beb1afacSDavid Ahern *flags |= RTNH_F_DEAD; 3957beb1afacSDavid Ahern } 3958beb1afacSDavid Ahern 3959beb1afacSDavid Ahern if (rt->rt6i_flags & RTF_GATEWAY) { 3960beb1afacSDavid Ahern if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0) 3961beb1afacSDavid Ahern goto nla_put_failure; 3962beb1afacSDavid Ahern } 3963beb1afacSDavid Ahern 3964fe400799SIdo Schimmel if (rt->rt6i_nh_flags & RTNH_F_OFFLOAD) 396561e4d01eSIdo Schimmel *flags |= RTNH_F_OFFLOAD; 396661e4d01eSIdo Schimmel 39675be083ceSDavid Ahern /* not needed for multipath encoding b/c it has a rtnexthop struct */ 39685be083ceSDavid Ahern if (!skip_oif && rt->dst.dev && 3969beb1afacSDavid Ahern nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex)) 3970beb1afacSDavid Ahern goto nla_put_failure; 3971beb1afacSDavid Ahern 3972beb1afacSDavid Ahern if (rt->dst.lwtstate && 3973beb1afacSDavid Ahern lwtunnel_fill_encap(skb, rt->dst.lwtstate) < 0) 3974beb1afacSDavid Ahern goto nla_put_failure; 3975beb1afacSDavid Ahern 3976beb1afacSDavid Ahern return 0; 3977beb1afacSDavid Ahern 3978beb1afacSDavid Ahern nla_put_failure: 3979beb1afacSDavid Ahern return -EMSGSIZE; 3980beb1afacSDavid Ahern } 3981beb1afacSDavid Ahern 39825be083ceSDavid Ahern /* add multipath next hop */ 3983beb1afacSDavid Ahern static int rt6_add_nexthop(struct sk_buff *skb, struct rt6_info *rt) 3984beb1afacSDavid Ahern { 3985beb1afacSDavid Ahern struct rtnexthop *rtnh; 3986beb1afacSDavid Ahern unsigned int flags = 0; 3987beb1afacSDavid Ahern 3988beb1afacSDavid Ahern rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh)); 3989beb1afacSDavid Ahern if (!rtnh) 3990beb1afacSDavid Ahern goto nla_put_failure; 3991beb1afacSDavid Ahern 3992beb1afacSDavid Ahern rtnh->rtnh_hops = 0; 3993beb1afacSDavid Ahern rtnh->rtnh_ifindex = rt->dst.dev ? rt->dst.dev->ifindex : 0; 3994beb1afacSDavid Ahern 39955be083ceSDavid Ahern if (rt6_nexthop_info(skb, rt, &flags, true) < 0) 3996beb1afacSDavid Ahern goto nla_put_failure; 3997beb1afacSDavid Ahern 3998beb1afacSDavid Ahern rtnh->rtnh_flags = flags; 3999beb1afacSDavid Ahern 4000beb1afacSDavid Ahern /* length of rtnetlink header + attributes */ 4001beb1afacSDavid Ahern rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh; 4002beb1afacSDavid Ahern 4003beb1afacSDavid Ahern return 0; 4004beb1afacSDavid Ahern 4005beb1afacSDavid Ahern nla_put_failure: 4006beb1afacSDavid Ahern return -EMSGSIZE; 4007339bf98fSThomas Graf } 4008339bf98fSThomas Graf 4009191cd582SBrian Haley static int rt6_fill_node(struct net *net, 4010191cd582SBrian Haley struct sk_buff *skb, struct rt6_info *rt, 40110d51aa80SJamal Hadi Salim struct in6_addr *dst, struct in6_addr *src, 401215e47304SEric W. Biederman int iif, int type, u32 portid, u32 seq, 4013f8cfe2ceSDavid Ahern unsigned int flags) 40141da177e4SLinus Torvalds { 40154b32b5adSMartin KaFai Lau u32 metrics[RTAX_MAX]; 40161da177e4SLinus Torvalds struct rtmsg *rtm; 40171da177e4SLinus Torvalds struct nlmsghdr *nlh; 4018e3703b3dSThomas Graf long expires; 40199e762a4aSPatrick McHardy u32 table; 40201da177e4SLinus Torvalds 402115e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); 402238308473SDavid S. Miller if (!nlh) 402326932566SPatrick McHardy return -EMSGSIZE; 40242d7202bfSThomas Graf 40252d7202bfSThomas Graf rtm = nlmsg_data(nlh); 40261da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 40271da177e4SLinus Torvalds rtm->rtm_dst_len = rt->rt6i_dst.plen; 40281da177e4SLinus Torvalds rtm->rtm_src_len = rt->rt6i_src.plen; 40291da177e4SLinus Torvalds rtm->rtm_tos = 0; 4030c71099acSThomas Graf if (rt->rt6i_table) 40319e762a4aSPatrick McHardy table = rt->rt6i_table->tb6_id; 4032c71099acSThomas Graf else 40339e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 40349e762a4aSPatrick McHardy rtm->rtm_table = table; 4035c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, table)) 4036c78679e8SDavid S. Miller goto nla_put_failure; 4037ef2c7d7bSNicolas Dichtel if (rt->rt6i_flags & RTF_REJECT) { 4038ef2c7d7bSNicolas Dichtel switch (rt->dst.error) { 4039ef2c7d7bSNicolas Dichtel case -EINVAL: 4040ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_BLACKHOLE; 4041ef2c7d7bSNicolas Dichtel break; 4042ef2c7d7bSNicolas Dichtel case -EACCES: 4043ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_PROHIBIT; 4044ef2c7d7bSNicolas Dichtel break; 4045b4949ab2SNicolas Dichtel case -EAGAIN: 4046b4949ab2SNicolas Dichtel rtm->rtm_type = RTN_THROW; 4047b4949ab2SNicolas Dichtel break; 4048ef2c7d7bSNicolas Dichtel default: 40491da177e4SLinus Torvalds rtm->rtm_type = RTN_UNREACHABLE; 4050ef2c7d7bSNicolas Dichtel break; 4051ef2c7d7bSNicolas Dichtel } 4052ef2c7d7bSNicolas Dichtel } 4053ab79ad14SMaciej Żenczykowski else if (rt->rt6i_flags & RTF_LOCAL) 4054ab79ad14SMaciej Żenczykowski rtm->rtm_type = RTN_LOCAL; 40554ee39733SDavid Ahern else if (rt->rt6i_flags & RTF_ANYCAST) 40564ee39733SDavid Ahern rtm->rtm_type = RTN_ANYCAST; 4057d1918542SDavid S. Miller else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK)) 40581da177e4SLinus Torvalds rtm->rtm_type = RTN_LOCAL; 40591da177e4SLinus Torvalds else 40601da177e4SLinus Torvalds rtm->rtm_type = RTN_UNICAST; 40611da177e4SLinus Torvalds rtm->rtm_flags = 0; 40621da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 40631da177e4SLinus Torvalds rtm->rtm_protocol = rt->rt6i_protocol; 40641da177e4SLinus Torvalds 40651da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) 40661da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 40671da177e4SLinus Torvalds 40681da177e4SLinus Torvalds if (dst) { 4069930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_DST, dst)) 4070c78679e8SDavid S. Miller goto nla_put_failure; 40711da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 40721da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 4073930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr)) 4074c78679e8SDavid S. Miller goto nla_put_failure; 40751da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 40761da177e4SLinus Torvalds if (src) { 4077930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_SRC, src)) 4078c78679e8SDavid S. Miller goto nla_put_failure; 40791da177e4SLinus Torvalds rtm->rtm_src_len = 128; 4080c78679e8SDavid S. Miller } else if (rtm->rtm_src_len && 4081930345eaSJiri Benc nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr)) 4082c78679e8SDavid S. Miller goto nla_put_failure; 40831da177e4SLinus Torvalds #endif 40847bc570c8SYOSHIFUJI Hideaki if (iif) { 40857bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 40867bc570c8SYOSHIFUJI Hideaki if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) { 4087fd61c6baSDavid Ahern int err = ip6mr_get_route(net, skb, rtm, portid); 40882cf75070SNikolay Aleksandrov 40897bc570c8SYOSHIFUJI Hideaki if (err == 0) 40907bc570c8SYOSHIFUJI Hideaki return 0; 4091fd61c6baSDavid Ahern if (err < 0) 40927bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 40937bc570c8SYOSHIFUJI Hideaki } else 40947bc570c8SYOSHIFUJI Hideaki #endif 4095c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_IIF, iif)) 4096c78679e8SDavid S. Miller goto nla_put_failure; 40977bc570c8SYOSHIFUJI Hideaki } else if (dst) { 40981da177e4SLinus Torvalds struct in6_addr saddr_buf; 4099c78679e8SDavid S. Miller if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 && 4100930345eaSJiri Benc nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 4101c78679e8SDavid S. Miller goto nla_put_failure; 4102c3968a85SDaniel Walter } 4103c3968a85SDaniel Walter 4104c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) { 4105c3968a85SDaniel Walter struct in6_addr saddr_buf; 41064e3fd7a0SAlexey Dobriyan saddr_buf = rt->rt6i_prefsrc.addr; 4107930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 4108c78679e8SDavid S. Miller goto nla_put_failure; 41091da177e4SLinus Torvalds } 41102d7202bfSThomas Graf 41114b32b5adSMartin KaFai Lau memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics)); 41124b32b5adSMartin KaFai Lau if (rt->rt6i_pmtu) 41134b32b5adSMartin KaFai Lau metrics[RTAX_MTU - 1] = rt->rt6i_pmtu; 41144b32b5adSMartin KaFai Lau if (rtnetlink_put_metrics(skb, metrics) < 0) 41152d7202bfSThomas Graf goto nla_put_failure; 41162d7202bfSThomas Graf 4117beb1afacSDavid Ahern if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric)) 4118beb1afacSDavid Ahern goto nla_put_failure; 4119beb1afacSDavid Ahern 4120beb1afacSDavid Ahern /* For multipath routes, walk the siblings list and add 4121beb1afacSDavid Ahern * each as a nexthop within RTA_MULTIPATH. 4122beb1afacSDavid Ahern */ 4123beb1afacSDavid Ahern if (rt->rt6i_nsiblings) { 4124beb1afacSDavid Ahern struct rt6_info *sibling, *next_sibling; 4125beb1afacSDavid Ahern struct nlattr *mp; 4126beb1afacSDavid Ahern 4127beb1afacSDavid Ahern mp = nla_nest_start(skb, RTA_MULTIPATH); 4128beb1afacSDavid Ahern if (!mp) 4129beb1afacSDavid Ahern goto nla_put_failure; 4130beb1afacSDavid Ahern 4131beb1afacSDavid Ahern if (rt6_add_nexthop(skb, rt) < 0) 4132beb1afacSDavid Ahern goto nla_put_failure; 4133beb1afacSDavid Ahern 4134beb1afacSDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 4135beb1afacSDavid Ahern &rt->rt6i_siblings, rt6i_siblings) { 4136beb1afacSDavid Ahern if (rt6_add_nexthop(skb, sibling) < 0) 413794f826b8SEric Dumazet goto nla_put_failure; 413894f826b8SEric Dumazet } 41392d7202bfSThomas Graf 4140beb1afacSDavid Ahern nla_nest_end(skb, mp); 4141beb1afacSDavid Ahern } else { 41425be083ceSDavid Ahern if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0) 4143c78679e8SDavid S. Miller goto nla_put_failure; 4144beb1afacSDavid Ahern } 41458253947eSLi Wei 41468253947eSLi Wei expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0; 414769cdf8f9SYOSHIFUJI Hideaki 414887a50699SDavid S. Miller if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0) 4149e3703b3dSThomas Graf goto nla_put_failure; 41501da177e4SLinus Torvalds 4151c78ba6d6SLubomir Rintel if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags))) 4152c78ba6d6SLubomir Rintel goto nla_put_failure; 4153c78ba6d6SLubomir Rintel 415419e42e45SRoopa Prabhu 4155053c095aSJohannes Berg nlmsg_end(skb, nlh); 4156053c095aSJohannes Berg return 0; 41572d7202bfSThomas Graf 41582d7202bfSThomas Graf nla_put_failure: 415926932566SPatrick McHardy nlmsg_cancel(skb, nlh); 416026932566SPatrick McHardy return -EMSGSIZE; 41611da177e4SLinus Torvalds } 41621da177e4SLinus Torvalds 41631b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg) 41641da177e4SLinus Torvalds { 41651da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 41661f17e2f2SDavid Ahern struct net *net = arg->net; 41671f17e2f2SDavid Ahern 41681f17e2f2SDavid Ahern if (rt == net->ipv6.ip6_null_entry) 41691f17e2f2SDavid Ahern return 0; 41701da177e4SLinus Torvalds 41712d7202bfSThomas Graf if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { 41722d7202bfSThomas Graf struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); 4173f8cfe2ceSDavid Ahern 4174f8cfe2ceSDavid Ahern /* user wants prefix routes only */ 4175f8cfe2ceSDavid Ahern if (rtm->rtm_flags & RTM_F_PREFIX && 4176f8cfe2ceSDavid Ahern !(rt->rt6i_flags & RTF_PREFIX_RT)) { 4177f8cfe2ceSDavid Ahern /* success since this is not a prefix route */ 4178f8cfe2ceSDavid Ahern return 1; 4179f8cfe2ceSDavid Ahern } 4180f8cfe2ceSDavid Ahern } 41811da177e4SLinus Torvalds 41821f17e2f2SDavid Ahern return rt6_fill_node(net, 4183191cd582SBrian Haley arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE, 418415e47304SEric W. Biederman NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq, 4185f8cfe2ceSDavid Ahern NLM_F_MULTI); 41861da177e4SLinus Torvalds } 41871da177e4SLinus Torvalds 4188c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, 4189c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 41901da177e4SLinus Torvalds { 41913b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 4192ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 419318c3a61cSRoopa Prabhu int err, iif = 0, oif = 0; 419418c3a61cSRoopa Prabhu struct dst_entry *dst; 41951da177e4SLinus Torvalds struct rt6_info *rt; 4196ab364a6fSThomas Graf struct sk_buff *skb; 4197ab364a6fSThomas Graf struct rtmsg *rtm; 41984c9483b2SDavid S. Miller struct flowi6 fl6; 419918c3a61cSRoopa Prabhu bool fibmatch; 4200ab364a6fSThomas Graf 4201fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 4202c21ef3e3SDavid Ahern extack); 4203ab364a6fSThomas Graf if (err < 0) 4204ab364a6fSThomas Graf goto errout; 4205ab364a6fSThomas Graf 4206ab364a6fSThomas Graf err = -EINVAL; 42074c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 420838b7097bSHannes Frederic Sowa rtm = nlmsg_data(nlh); 420938b7097bSHannes Frederic Sowa fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0); 421018c3a61cSRoopa Prabhu fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); 4211ab364a6fSThomas Graf 4212ab364a6fSThomas Graf if (tb[RTA_SRC]) { 4213ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 4214ab364a6fSThomas Graf goto errout; 4215ab364a6fSThomas Graf 42164e3fd7a0SAlexey Dobriyan fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 4217ab364a6fSThomas Graf } 4218ab364a6fSThomas Graf 4219ab364a6fSThomas Graf if (tb[RTA_DST]) { 4220ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 4221ab364a6fSThomas Graf goto errout; 4222ab364a6fSThomas Graf 42234e3fd7a0SAlexey Dobriyan fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 4224ab364a6fSThomas Graf } 4225ab364a6fSThomas Graf 4226ab364a6fSThomas Graf if (tb[RTA_IIF]) 4227ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 4228ab364a6fSThomas Graf 4229ab364a6fSThomas Graf if (tb[RTA_OIF]) 423072331bc0SShmulik Ladkani oif = nla_get_u32(tb[RTA_OIF]); 4231ab364a6fSThomas Graf 42322e47b291SLorenzo Colitti if (tb[RTA_MARK]) 42332e47b291SLorenzo Colitti fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); 42342e47b291SLorenzo Colitti 4235622ec2c9SLorenzo Colitti if (tb[RTA_UID]) 4236622ec2c9SLorenzo Colitti fl6.flowi6_uid = make_kuid(current_user_ns(), 4237622ec2c9SLorenzo Colitti nla_get_u32(tb[RTA_UID])); 4238622ec2c9SLorenzo Colitti else 4239622ec2c9SLorenzo Colitti fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); 4240622ec2c9SLorenzo Colitti 4241ab364a6fSThomas Graf if (iif) { 4242ab364a6fSThomas Graf struct net_device *dev; 424372331bc0SShmulik Ladkani int flags = 0; 424472331bc0SShmulik Ladkani 4245121622dbSFlorian Westphal rcu_read_lock(); 4246121622dbSFlorian Westphal 4247121622dbSFlorian Westphal dev = dev_get_by_index_rcu(net, iif); 4248ab364a6fSThomas Graf if (!dev) { 4249121622dbSFlorian Westphal rcu_read_unlock(); 4250ab364a6fSThomas Graf err = -ENODEV; 4251ab364a6fSThomas Graf goto errout; 4252ab364a6fSThomas Graf } 425372331bc0SShmulik Ladkani 425472331bc0SShmulik Ladkani fl6.flowi6_iif = iif; 425572331bc0SShmulik Ladkani 425672331bc0SShmulik Ladkani if (!ipv6_addr_any(&fl6.saddr)) 425772331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_HAS_SADDR; 425872331bc0SShmulik Ladkani 425918c3a61cSRoopa Prabhu if (!fibmatch) 426018c3a61cSRoopa Prabhu dst = ip6_route_input_lookup(net, dev, &fl6, flags); 4261401481e0SArnd Bergmann else 4262401481e0SArnd Bergmann dst = ip6_route_lookup(net, &fl6, 0); 4263121622dbSFlorian Westphal 4264121622dbSFlorian Westphal rcu_read_unlock(); 426572331bc0SShmulik Ladkani } else { 426672331bc0SShmulik Ladkani fl6.flowi6_oif = oif; 426772331bc0SShmulik Ladkani 426818c3a61cSRoopa Prabhu if (!fibmatch) 426918c3a61cSRoopa Prabhu dst = ip6_route_output(net, NULL, &fl6); 4270401481e0SArnd Bergmann else 4271401481e0SArnd Bergmann dst = ip6_route_lookup(net, &fl6, 0); 427218c3a61cSRoopa Prabhu } 427318c3a61cSRoopa Prabhu 427418c3a61cSRoopa Prabhu 427518c3a61cSRoopa Prabhu rt = container_of(dst, struct rt6_info, dst); 427618c3a61cSRoopa Prabhu if (rt->dst.error) { 427718c3a61cSRoopa Prabhu err = rt->dst.error; 427818c3a61cSRoopa Prabhu ip6_rt_put(rt); 427918c3a61cSRoopa Prabhu goto errout; 4280ab364a6fSThomas Graf } 42811da177e4SLinus Torvalds 42829d6acb3bSWANG Cong if (rt == net->ipv6.ip6_null_entry) { 42839d6acb3bSWANG Cong err = rt->dst.error; 42849d6acb3bSWANG Cong ip6_rt_put(rt); 42859d6acb3bSWANG Cong goto errout; 42869d6acb3bSWANG Cong } 42879d6acb3bSWANG Cong 42881da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 428938308473SDavid S. Miller if (!skb) { 429094e187c0SAmerigo Wang ip6_rt_put(rt); 4291ab364a6fSThomas Graf err = -ENOBUFS; 4292ab364a6fSThomas Graf goto errout; 4293ab364a6fSThomas Graf } 42941da177e4SLinus Torvalds 4295d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 429618c3a61cSRoopa Prabhu if (fibmatch) 429718c3a61cSRoopa Prabhu err = rt6_fill_node(net, skb, rt, NULL, NULL, iif, 429818c3a61cSRoopa Prabhu RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 429918c3a61cSRoopa Prabhu nlh->nlmsg_seq, 0); 430018c3a61cSRoopa Prabhu else 43014c9483b2SDavid S. Miller err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif, 430215e47304SEric W. Biederman RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 4303f8cfe2ceSDavid Ahern nlh->nlmsg_seq, 0); 43041da177e4SLinus Torvalds if (err < 0) { 4305ab364a6fSThomas Graf kfree_skb(skb); 4306ab364a6fSThomas Graf goto errout; 43071da177e4SLinus Torvalds } 43081da177e4SLinus Torvalds 430915e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 4310ab364a6fSThomas Graf errout: 43111da177e4SLinus Torvalds return err; 43121da177e4SLinus Torvalds } 43131da177e4SLinus Torvalds 431437a1d361SRoopa Prabhu void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info, 431537a1d361SRoopa Prabhu unsigned int nlm_flags) 43161da177e4SLinus Torvalds { 43171da177e4SLinus Torvalds struct sk_buff *skb; 43185578689aSDaniel Lezcano struct net *net = info->nl_net; 4319528c4cebSDenis V. Lunev u32 seq; 4320528c4cebSDenis V. Lunev int err; 43210d51aa80SJamal Hadi Salim 4322528c4cebSDenis V. Lunev err = -ENOBUFS; 432338308473SDavid S. Miller seq = info->nlh ? info->nlh->nlmsg_seq : 0; 432486872cb5SThomas Graf 432519e42e45SRoopa Prabhu skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 432638308473SDavid S. Miller if (!skb) 432721713ebcSThomas Graf goto errout; 43281da177e4SLinus Torvalds 4329191cd582SBrian Haley err = rt6_fill_node(net, skb, rt, NULL, NULL, 0, 4330f8cfe2ceSDavid Ahern event, info->portid, seq, nlm_flags); 433126932566SPatrick McHardy if (err < 0) { 433226932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 433326932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 433426932566SPatrick McHardy kfree_skb(skb); 433526932566SPatrick McHardy goto errout; 433626932566SPatrick McHardy } 433715e47304SEric W. Biederman rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 43385578689aSDaniel Lezcano info->nlh, gfp_any()); 43391ce85fe4SPablo Neira Ayuso return; 434021713ebcSThomas Graf errout: 434121713ebcSThomas Graf if (err < 0) 43425578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 43431da177e4SLinus Torvalds } 43441da177e4SLinus Torvalds 43458ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 4346351638e7SJiri Pirko unsigned long event, void *ptr) 43478ed67789SDaniel Lezcano { 4348351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 4349c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 43508ed67789SDaniel Lezcano 4351242d3a49SWANG Cong if (!(dev->flags & IFF_LOOPBACK)) 4352242d3a49SWANG Cong return NOTIFY_OK; 4353242d3a49SWANG Cong 4354242d3a49SWANG Cong if (event == NETDEV_REGISTER) { 4355d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 43568ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 43578ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 4358d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 43598ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 4360d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 43618ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 43628ed67789SDaniel Lezcano #endif 436376da0704SWANG Cong } else if (event == NETDEV_UNREGISTER && 436476da0704SWANG Cong dev->reg_state != NETREG_UNREGISTERED) { 436576da0704SWANG Cong /* NETDEV_UNREGISTER could be fired for multiple times by 436676da0704SWANG Cong * netdev_wait_allrefs(). Make sure we only call this once. 436776da0704SWANG Cong */ 436812d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev); 4369242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 437012d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev); 437112d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev); 4372242d3a49SWANG Cong #endif 43738ed67789SDaniel Lezcano } 43748ed67789SDaniel Lezcano 43758ed67789SDaniel Lezcano return NOTIFY_OK; 43768ed67789SDaniel Lezcano } 43778ed67789SDaniel Lezcano 43781da177e4SLinus Torvalds /* 43791da177e4SLinus Torvalds * /proc 43801da177e4SLinus Torvalds */ 43811da177e4SLinus Torvalds 43821da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 43831da177e4SLinus Torvalds 438433120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = { 438533120b30SAlexey Dobriyan .owner = THIS_MODULE, 438633120b30SAlexey Dobriyan .open = ipv6_route_open, 438733120b30SAlexey Dobriyan .read = seq_read, 438833120b30SAlexey Dobriyan .llseek = seq_lseek, 43898d2ca1d7SHannes Frederic Sowa .release = seq_release_net, 439033120b30SAlexey Dobriyan }; 439133120b30SAlexey Dobriyan 43921da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 43931da177e4SLinus Torvalds { 439469ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 43951da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 439669ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 439769ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 439869ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_alloc, 439969ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 440069ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 4401fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 440269ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 44031da177e4SLinus Torvalds 44041da177e4SLinus Torvalds return 0; 44051da177e4SLinus Torvalds } 44061da177e4SLinus Torvalds 44071da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file) 44081da177e4SLinus Torvalds { 4409de05c557SPavel Emelyanov return single_open_net(inode, file, rt6_stats_seq_show); 441069ddb805SDaniel Lezcano } 441169ddb805SDaniel Lezcano 44129a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = { 44131da177e4SLinus Torvalds .owner = THIS_MODULE, 44141da177e4SLinus Torvalds .open = rt6_stats_seq_open, 44151da177e4SLinus Torvalds .read = seq_read, 44161da177e4SLinus Torvalds .llseek = seq_lseek, 4417b6fcbdb4SPavel Emelyanov .release = single_release_net, 44181da177e4SLinus Torvalds }; 44191da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 44201da177e4SLinus Torvalds 44211da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 44221da177e4SLinus Torvalds 44231da177e4SLinus Torvalds static 4424fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, 44251da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 44261da177e4SLinus Torvalds { 4427c486da34SLucian Adrian Grijincu struct net *net; 4428c486da34SLucian Adrian Grijincu int delay; 4429c486da34SLucian Adrian Grijincu if (!write) 4430c486da34SLucian Adrian Grijincu return -EINVAL; 4431c486da34SLucian Adrian Grijincu 4432c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 4433c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 44348d65af78SAlexey Dobriyan proc_dointvec(ctl, write, buffer, lenp, ppos); 44352ac3ac8fSMichal Kubeček fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0); 44361da177e4SLinus Torvalds return 0; 44371da177e4SLinus Torvalds } 44381da177e4SLinus Torvalds 4439fe2c6338SJoe Perches struct ctl_table ipv6_route_table_template[] = { 44401da177e4SLinus Torvalds { 44411da177e4SLinus Torvalds .procname = "flush", 44424990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 44431da177e4SLinus Torvalds .maxlen = sizeof(int), 444489c8b3a1SDave Jones .mode = 0200, 44456d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 44461da177e4SLinus Torvalds }, 44471da177e4SLinus Torvalds { 44481da177e4SLinus Torvalds .procname = "gc_thresh", 44499a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 44501da177e4SLinus Torvalds .maxlen = sizeof(int), 44511da177e4SLinus Torvalds .mode = 0644, 44526d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 44531da177e4SLinus Torvalds }, 44541da177e4SLinus Torvalds { 44551da177e4SLinus Torvalds .procname = "max_size", 44564990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 44571da177e4SLinus Torvalds .maxlen = sizeof(int), 44581da177e4SLinus Torvalds .mode = 0644, 44596d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 44601da177e4SLinus Torvalds }, 44611da177e4SLinus Torvalds { 44621da177e4SLinus Torvalds .procname = "gc_min_interval", 44634990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 44641da177e4SLinus Torvalds .maxlen = sizeof(int), 44651da177e4SLinus Torvalds .mode = 0644, 44666d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 44671da177e4SLinus Torvalds }, 44681da177e4SLinus Torvalds { 44691da177e4SLinus Torvalds .procname = "gc_timeout", 44704990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 44711da177e4SLinus Torvalds .maxlen = sizeof(int), 44721da177e4SLinus Torvalds .mode = 0644, 44736d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 44741da177e4SLinus Torvalds }, 44751da177e4SLinus Torvalds { 44761da177e4SLinus Torvalds .procname = "gc_interval", 44774990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 44781da177e4SLinus Torvalds .maxlen = sizeof(int), 44791da177e4SLinus Torvalds .mode = 0644, 44806d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 44811da177e4SLinus Torvalds }, 44821da177e4SLinus Torvalds { 44831da177e4SLinus Torvalds .procname = "gc_elasticity", 44844990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 44851da177e4SLinus Torvalds .maxlen = sizeof(int), 44861da177e4SLinus Torvalds .mode = 0644, 4487f3d3f616SMin Zhang .proc_handler = proc_dointvec, 44881da177e4SLinus Torvalds }, 44891da177e4SLinus Torvalds { 44901da177e4SLinus Torvalds .procname = "mtu_expires", 44914990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 44921da177e4SLinus Torvalds .maxlen = sizeof(int), 44931da177e4SLinus Torvalds .mode = 0644, 44946d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 44951da177e4SLinus Torvalds }, 44961da177e4SLinus Torvalds { 44971da177e4SLinus Torvalds .procname = "min_adv_mss", 44984990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 44991da177e4SLinus Torvalds .maxlen = sizeof(int), 45001da177e4SLinus Torvalds .mode = 0644, 4501f3d3f616SMin Zhang .proc_handler = proc_dointvec, 45021da177e4SLinus Torvalds }, 45031da177e4SLinus Torvalds { 45041da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 45054990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 45061da177e4SLinus Torvalds .maxlen = sizeof(int), 45071da177e4SLinus Torvalds .mode = 0644, 45086d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 45091da177e4SLinus Torvalds }, 4510f8572d8fSEric W. Biederman { } 45111da177e4SLinus Torvalds }; 45121da177e4SLinus Torvalds 45132c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 4514760f2d01SDaniel Lezcano { 4515760f2d01SDaniel Lezcano struct ctl_table *table; 4516760f2d01SDaniel Lezcano 4517760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 4518760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 4519760f2d01SDaniel Lezcano GFP_KERNEL); 45205ee09105SYOSHIFUJI Hideaki 45215ee09105SYOSHIFUJI Hideaki if (table) { 45225ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 4523c486da34SLucian Adrian Grijincu table[0].extra1 = net; 452486393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 45255ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 45265ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 45275ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 45285ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 45295ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 45305ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 45315ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 45329c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 4533464dc801SEric W. Biederman 4534464dc801SEric W. Biederman /* Don't export sysctls to unprivileged users */ 4535464dc801SEric W. Biederman if (net->user_ns != &init_user_ns) 4536464dc801SEric W. Biederman table[0].procname = NULL; 45375ee09105SYOSHIFUJI Hideaki } 45385ee09105SYOSHIFUJI Hideaki 4539760f2d01SDaniel Lezcano return table; 4540760f2d01SDaniel Lezcano } 45411da177e4SLinus Torvalds #endif 45421da177e4SLinus Torvalds 45432c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 4544cdb18761SDaniel Lezcano { 4545633d424bSPavel Emelyanov int ret = -ENOMEM; 45468ed67789SDaniel Lezcano 454786393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 454886393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 4549f2fc6a54SBenjamin Thery 4550fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 4551fc66f95cSEric Dumazet goto out_ip6_dst_ops; 4552fc66f95cSEric Dumazet 45538ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 45548ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 45558ed67789SDaniel Lezcano GFP_KERNEL); 45568ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 4557fc66f95cSEric Dumazet goto out_ip6_dst_entries; 4558d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.path = 45598ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_null_entry; 4560d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 456162fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 456262fa8a84SDavid S. Miller ip6_template_metrics, true); 45638ed67789SDaniel Lezcano 45648ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 4565feca7d8cSVincent Bernat net->ipv6.fib6_has_custom_rules = false; 45668ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 45678ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 45688ed67789SDaniel Lezcano GFP_KERNEL); 456968fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 457068fffc67SPeter Zijlstra goto out_ip6_null_entry; 4571d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.path = 45728ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_prohibit_entry; 4573d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 457462fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 457562fa8a84SDavid S. Miller ip6_template_metrics, true); 45768ed67789SDaniel Lezcano 45778ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 45788ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 45798ed67789SDaniel Lezcano GFP_KERNEL); 458068fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 458168fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 4582d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.path = 45838ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_blk_hole_entry; 4584d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 458562fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 458662fa8a84SDavid S. Miller ip6_template_metrics, true); 45878ed67789SDaniel Lezcano #endif 45888ed67789SDaniel Lezcano 4589b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 4590b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 4591b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 4592b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 4593b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 4594b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 4595b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 4596b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 4597b339a47cSPeter Zijlstra 45986891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 45996891a346SBenjamin Thery 46008ed67789SDaniel Lezcano ret = 0; 46018ed67789SDaniel Lezcano out: 46028ed67789SDaniel Lezcano return ret; 4603f2fc6a54SBenjamin Thery 460468fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 460568fffc67SPeter Zijlstra out_ip6_prohibit_entry: 460668fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 460768fffc67SPeter Zijlstra out_ip6_null_entry: 460868fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 460968fffc67SPeter Zijlstra #endif 4610fc66f95cSEric Dumazet out_ip6_dst_entries: 4611fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 4612f2fc6a54SBenjamin Thery out_ip6_dst_ops: 4613f2fc6a54SBenjamin Thery goto out; 4614cdb18761SDaniel Lezcano } 4615cdb18761SDaniel Lezcano 46162c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 4617cdb18761SDaniel Lezcano { 46188ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 46198ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 46208ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 46218ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 46228ed67789SDaniel Lezcano #endif 462341bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 4624cdb18761SDaniel Lezcano } 4625cdb18761SDaniel Lezcano 4626d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net) 4627d189634eSThomas Graf { 4628d189634eSThomas Graf #ifdef CONFIG_PROC_FS 4629d4beaa66SGao feng proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops); 4630d4beaa66SGao feng proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops); 4631d189634eSThomas Graf #endif 4632d189634eSThomas Graf return 0; 4633d189634eSThomas Graf } 4634d189634eSThomas Graf 4635d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net) 4636d189634eSThomas Graf { 4637d189634eSThomas Graf #ifdef CONFIG_PROC_FS 4638ece31ffdSGao feng remove_proc_entry("ipv6_route", net->proc_net); 4639ece31ffdSGao feng remove_proc_entry("rt6_stats", net->proc_net); 4640d189634eSThomas Graf #endif 4641d189634eSThomas Graf } 4642d189634eSThomas Graf 4643cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 4644cdb18761SDaniel Lezcano .init = ip6_route_net_init, 4645cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 4646cdb18761SDaniel Lezcano }; 4647cdb18761SDaniel Lezcano 4648c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net) 4649c3426b47SDavid S. Miller { 4650c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 4651c3426b47SDavid S. Miller 4652c3426b47SDavid S. Miller if (!bp) 4653c3426b47SDavid S. Miller return -ENOMEM; 4654c3426b47SDavid S. Miller inet_peer_base_init(bp); 4655c3426b47SDavid S. Miller net->ipv6.peers = bp; 4656c3426b47SDavid S. Miller return 0; 4657c3426b47SDavid S. Miller } 4658c3426b47SDavid S. Miller 4659c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net) 4660c3426b47SDavid S. Miller { 4661c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv6.peers; 4662c3426b47SDavid S. Miller 4663c3426b47SDavid S. Miller net->ipv6.peers = NULL; 466456a6b248SDavid S. Miller inetpeer_invalidate_tree(bp); 4665c3426b47SDavid S. Miller kfree(bp); 4666c3426b47SDavid S. Miller } 4667c3426b47SDavid S. Miller 46682b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = { 4669c3426b47SDavid S. Miller .init = ipv6_inetpeer_init, 4670c3426b47SDavid S. Miller .exit = ipv6_inetpeer_exit, 4671c3426b47SDavid S. Miller }; 4672c3426b47SDavid S. Miller 4673d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = { 4674d189634eSThomas Graf .init = ip6_route_net_init_late, 4675d189634eSThomas Graf .exit = ip6_route_net_exit_late, 4676d189634eSThomas Graf }; 4677d189634eSThomas Graf 46788ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 46798ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 4680242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY - 10, 46818ed67789SDaniel Lezcano }; 46828ed67789SDaniel Lezcano 46832f460933SWANG Cong void __init ip6_route_init_special_entries(void) 46842f460933SWANG Cong { 46852f460933SWANG Cong /* Registering of the loopback is done before this portion of code, 46862f460933SWANG Cong * the loopback reference in rt6_info will not be taken, do it 46872f460933SWANG Cong * manually for init_net */ 46882f460933SWANG Cong init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 46892f460933SWANG Cong init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 46902f460933SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 46912f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 46922f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 46932f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 46942f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 46952f460933SWANG Cong #endif 46962f460933SWANG Cong } 46972f460933SWANG Cong 4698433d49c3SDaniel Lezcano int __init ip6_route_init(void) 46991da177e4SLinus Torvalds { 4700433d49c3SDaniel Lezcano int ret; 47018d0b94afSMartin KaFai Lau int cpu; 4702433d49c3SDaniel Lezcano 47039a7ec3a9SDaniel Lezcano ret = -ENOMEM; 47049a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 47059a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 47069a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 47079a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 4708c19a28e1SFernando Carrijo goto out; 470914e50e57SDavid S. Miller 4710fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 47118ed67789SDaniel Lezcano if (ret) 4712bdb3289fSDaniel Lezcano goto out_kmem_cache; 4713bdb3289fSDaniel Lezcano 4714c3426b47SDavid S. Miller ret = register_pernet_subsys(&ipv6_inetpeer_ops); 4715c3426b47SDavid S. Miller if (ret) 4716e8803b6cSDavid S. Miller goto out_dst_entries; 47172a0c451aSThomas Graf 47187e52b33bSDavid S. Miller ret = register_pernet_subsys(&ip6_route_net_ops); 47197e52b33bSDavid S. Miller if (ret) 47207e52b33bSDavid S. Miller goto out_register_inetpeer; 4721c3426b47SDavid S. Miller 47225dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 47235dc121e9SArnaud Ebalard 4724e8803b6cSDavid S. Miller ret = fib6_init(); 4725433d49c3SDaniel Lezcano if (ret) 47268ed67789SDaniel Lezcano goto out_register_subsys; 4727433d49c3SDaniel Lezcano 4728433d49c3SDaniel Lezcano ret = xfrm6_init(); 4729433d49c3SDaniel Lezcano if (ret) 4730e8803b6cSDavid S. Miller goto out_fib6_init; 4731c35b7e72SDaniel Lezcano 4732433d49c3SDaniel Lezcano ret = fib6_rules_init(); 4733433d49c3SDaniel Lezcano if (ret) 4734433d49c3SDaniel Lezcano goto xfrm6_init; 47357e5449c2SDaniel Lezcano 4736d189634eSThomas Graf ret = register_pernet_subsys(&ip6_route_net_late_ops); 4737d189634eSThomas Graf if (ret) 4738d189634eSThomas Graf goto fib6_rules_init; 4739d189634eSThomas Graf 4740433d49c3SDaniel Lezcano ret = -ENOBUFS; 4741b97bac64SFlorian Westphal if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, 0) || 4742b97bac64SFlorian Westphal __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, 0) || 4743e3a22b7fSFlorian Westphal __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, 4744e3a22b7fSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED)) 4745d189634eSThomas Graf goto out_register_late_subsys; 4746433d49c3SDaniel Lezcano 47478ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 4748cdb18761SDaniel Lezcano if (ret) 4749d189634eSThomas Graf goto out_register_late_subsys; 47508ed67789SDaniel Lezcano 47518d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 47528d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 47538d0b94afSMartin KaFai Lau 47548d0b94afSMartin KaFai Lau INIT_LIST_HEAD(&ul->head); 47558d0b94afSMartin KaFai Lau spin_lock_init(&ul->lock); 47568d0b94afSMartin KaFai Lau } 47578d0b94afSMartin KaFai Lau 4758433d49c3SDaniel Lezcano out: 4759433d49c3SDaniel Lezcano return ret; 4760433d49c3SDaniel Lezcano 4761d189634eSThomas Graf out_register_late_subsys: 4762d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 4763433d49c3SDaniel Lezcano fib6_rules_init: 4764433d49c3SDaniel Lezcano fib6_rules_cleanup(); 4765433d49c3SDaniel Lezcano xfrm6_init: 4766433d49c3SDaniel Lezcano xfrm6_fini(); 47672a0c451aSThomas Graf out_fib6_init: 47682a0c451aSThomas Graf fib6_gc_cleanup(); 47698ed67789SDaniel Lezcano out_register_subsys: 47708ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 47717e52b33bSDavid S. Miller out_register_inetpeer: 47727e52b33bSDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 4773fc66f95cSEric Dumazet out_dst_entries: 4774fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 4775433d49c3SDaniel Lezcano out_kmem_cache: 4776f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 4777433d49c3SDaniel Lezcano goto out; 47781da177e4SLinus Torvalds } 47791da177e4SLinus Torvalds 47801da177e4SLinus Torvalds void ip6_route_cleanup(void) 47811da177e4SLinus Torvalds { 47828ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 4783d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 4784101367c2SThomas Graf fib6_rules_cleanup(); 47851da177e4SLinus Torvalds xfrm6_fini(); 47861da177e4SLinus Torvalds fib6_gc_cleanup(); 4787c3426b47SDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 47888ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 478941bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 4790f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 47911da177e4SLinus Torvalds } 4792