11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Linux INET6 implementation 31da177e4SLinus Torvalds * FIB front-end. 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Authors: 61da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 91da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 101da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 111da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 121da177e4SLinus Torvalds */ 131da177e4SLinus Torvalds 141da177e4SLinus Torvalds /* Changes: 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI 171da177e4SLinus Torvalds * reworked default router selection. 181da177e4SLinus Torvalds * - respect outgoing interface 191da177e4SLinus Torvalds * - select from (probably) reachable routers (i.e. 201da177e4SLinus Torvalds * routers in REACHABLE, STALE, DELAY or PROBE states). 211da177e4SLinus Torvalds * - always select the same router if it is (probably) 221da177e4SLinus Torvalds * reachable. otherwise, round-robin the list. 23c0bece9fSYOSHIFUJI Hideaki * Ville Nuorvala 24c0bece9fSYOSHIFUJI Hideaki * Fixed routing subtrees. 251da177e4SLinus Torvalds */ 261da177e4SLinus Torvalds 27f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt 28f3213831SJoe Perches 294fc268d2SRandy Dunlap #include <linux/capability.h> 301da177e4SLinus Torvalds #include <linux/errno.h> 31bc3b2d7fSPaul Gortmaker #include <linux/export.h> 321da177e4SLinus Torvalds #include <linux/types.h> 331da177e4SLinus Torvalds #include <linux/times.h> 341da177e4SLinus Torvalds #include <linux/socket.h> 351da177e4SLinus Torvalds #include <linux/sockios.h> 361da177e4SLinus Torvalds #include <linux/net.h> 371da177e4SLinus Torvalds #include <linux/route.h> 381da177e4SLinus Torvalds #include <linux/netdevice.h> 391da177e4SLinus Torvalds #include <linux/in6.h> 407bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h> 411da177e4SLinus Torvalds #include <linux/init.h> 421da177e4SLinus Torvalds #include <linux/if_arp.h> 431da177e4SLinus Torvalds #include <linux/proc_fs.h> 441da177e4SLinus Torvalds #include <linux/seq_file.h> 455b7c931dSDaniel Lezcano #include <linux/nsproxy.h> 465a0e3ad6STejun Heo #include <linux/slab.h> 4735732d01SWei Wang #include <linux/jhash.h> 48457c4cbcSEric W. Biederman #include <net/net_namespace.h> 491da177e4SLinus Torvalds #include <net/snmp.h> 501da177e4SLinus Torvalds #include <net/ipv6.h> 511da177e4SLinus Torvalds #include <net/ip6_fib.h> 521da177e4SLinus Torvalds #include <net/ip6_route.h> 531da177e4SLinus Torvalds #include <net/ndisc.h> 541da177e4SLinus Torvalds #include <net/addrconf.h> 551da177e4SLinus Torvalds #include <net/tcp.h> 561da177e4SLinus Torvalds #include <linux/rtnetlink.h> 571da177e4SLinus Torvalds #include <net/dst.h> 58904af04dSJiri Benc #include <net/dst_metadata.h> 591da177e4SLinus Torvalds #include <net/xfrm.h> 608d71740cSTom Tucker #include <net/netevent.h> 6121713ebcSThomas Graf #include <net/netlink.h> 6251ebd318SNicolas Dichtel #include <net/nexthop.h> 6319e42e45SRoopa Prabhu #include <net/lwtunnel.h> 64904af04dSJiri Benc #include <net/ip_tunnels.h> 65ca254490SDavid Ahern #include <net/l3mdev.h> 66b811580dSDavid Ahern #include <trace/events/fib6.h> 671da177e4SLinus Torvalds 687c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 711da177e4SLinus Torvalds #include <linux/sysctl.h> 721da177e4SLinus Torvalds #endif 731da177e4SLinus Torvalds 74afc154e9SHannes Frederic Sowa enum rt6_nud_state { 757e980569SJiri Benc RT6_NUD_FAIL_HARD = -3, 767e980569SJiri Benc RT6_NUD_FAIL_PROBE = -2, 777e980569SJiri Benc RT6_NUD_FAIL_DO_RR = -1, 78afc154e9SHannes Frederic Sowa RT6_NUD_SUCCEED = 1 79afc154e9SHannes Frederic Sowa }; 80afc154e9SHannes Frederic Sowa 8183a09abdSMartin KaFai Lau static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort); 821da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie); 830dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst); 84ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst); 851da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *); 861da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *); 871da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *, 881da177e4SLinus Torvalds struct net_device *dev, int how); 89569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops); 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds static int ip6_pkt_discard(struct sk_buff *skb); 92ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb); 937150aedeSKamala R static int ip6_pkt_prohibit(struct sk_buff *skb); 94ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb); 951da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb); 966700c270SDavid S. Miller static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 976700c270SDavid S. Miller struct sk_buff *skb, u32 mtu); 986700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, 996700c270SDavid S. Miller struct sk_buff *skb); 1004b32b5adSMartin KaFai Lau static void rt6_dst_from_metrics_check(struct rt6_info *rt); 10152bd4c0cSNicolas Dichtel static int rt6_score_route(struct rt6_info *rt, int oif, int strict); 10216a16cd3SDavid Ahern static size_t rt6_nlmsg_size(struct rt6_info *rt); 10316a16cd3SDavid Ahern static int rt6_fill_node(struct net *net, 10416a16cd3SDavid Ahern struct sk_buff *skb, struct rt6_info *rt, 10516a16cd3SDavid Ahern struct in6_addr *dst, struct in6_addr *src, 10616a16cd3SDavid Ahern int iif, int type, u32 portid, u32 seq, 10716a16cd3SDavid Ahern unsigned int flags); 10835732d01SWei Wang static struct rt6_info *rt6_find_cached_rt(struct rt6_info *rt, 10935732d01SWei Wang struct in6_addr *daddr, 11035732d01SWei Wang struct in6_addr *saddr); 1111da177e4SLinus Torvalds 11270ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 113efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 114b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 115830218c1SDavid Ahern const struct in6_addr *gwaddr, 116830218c1SDavid Ahern struct net_device *dev, 11795c96174SEric Dumazet unsigned int pref); 118efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 119b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 120830218c1SDavid Ahern const struct in6_addr *gwaddr, 121830218c1SDavid Ahern struct net_device *dev); 12270ceb4f5SYOSHIFUJI Hideaki #endif 12370ceb4f5SYOSHIFUJI Hideaki 1248d0b94afSMartin KaFai Lau struct uncached_list { 1258d0b94afSMartin KaFai Lau spinlock_t lock; 1268d0b94afSMartin KaFai Lau struct list_head head; 1278d0b94afSMartin KaFai Lau }; 1288d0b94afSMartin KaFai Lau 1298d0b94afSMartin KaFai Lau static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list); 1308d0b94afSMartin KaFai Lau 1318d0b94afSMartin KaFai Lau static void rt6_uncached_list_add(struct rt6_info *rt) 1328d0b94afSMartin KaFai Lau { 1338d0b94afSMartin KaFai Lau struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list); 1348d0b94afSMartin KaFai Lau 1358d0b94afSMartin KaFai Lau rt->rt6i_uncached_list = ul; 1368d0b94afSMartin KaFai Lau 1378d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1388d0b94afSMartin KaFai Lau list_add_tail(&rt->rt6i_uncached, &ul->head); 1398d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1408d0b94afSMartin KaFai Lau } 1418d0b94afSMartin KaFai Lau 1428d0b94afSMartin KaFai Lau static void rt6_uncached_list_del(struct rt6_info *rt) 1438d0b94afSMartin KaFai Lau { 1448d0b94afSMartin KaFai Lau if (!list_empty(&rt->rt6i_uncached)) { 1458d0b94afSMartin KaFai Lau struct uncached_list *ul = rt->rt6i_uncached_list; 1468d0b94afSMartin KaFai Lau 1478d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1488d0b94afSMartin KaFai Lau list_del(&rt->rt6i_uncached); 1498d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1508d0b94afSMartin KaFai Lau } 1518d0b94afSMartin KaFai Lau } 1528d0b94afSMartin KaFai Lau 1538d0b94afSMartin KaFai Lau static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev) 1548d0b94afSMartin KaFai Lau { 1558d0b94afSMartin KaFai Lau struct net_device *loopback_dev = net->loopback_dev; 1568d0b94afSMartin KaFai Lau int cpu; 1578d0b94afSMartin KaFai Lau 158e332bc67SEric W. Biederman if (dev == loopback_dev) 159e332bc67SEric W. Biederman return; 160e332bc67SEric W. Biederman 1618d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 1628d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 1638d0b94afSMartin KaFai Lau struct rt6_info *rt; 1648d0b94afSMartin KaFai Lau 1658d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1668d0b94afSMartin KaFai Lau list_for_each_entry(rt, &ul->head, rt6i_uncached) { 1678d0b94afSMartin KaFai Lau struct inet6_dev *rt_idev = rt->rt6i_idev; 1688d0b94afSMartin KaFai Lau struct net_device *rt_dev = rt->dst.dev; 1698d0b94afSMartin KaFai Lau 170e332bc67SEric W. Biederman if (rt_idev->dev == dev) { 1718d0b94afSMartin KaFai Lau rt->rt6i_idev = in6_dev_get(loopback_dev); 1728d0b94afSMartin KaFai Lau in6_dev_put(rt_idev); 1738d0b94afSMartin KaFai Lau } 1748d0b94afSMartin KaFai Lau 175e332bc67SEric W. Biederman if (rt_dev == dev) { 1768d0b94afSMartin KaFai Lau rt->dst.dev = loopback_dev; 1778d0b94afSMartin KaFai Lau dev_hold(rt->dst.dev); 1788d0b94afSMartin KaFai Lau dev_put(rt_dev); 1798d0b94afSMartin KaFai Lau } 1808d0b94afSMartin KaFai Lau } 1818d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1828d0b94afSMartin KaFai Lau } 1838d0b94afSMartin KaFai Lau } 1848d0b94afSMartin KaFai Lau 185d52d3997SMartin KaFai Lau static u32 *rt6_pcpu_cow_metrics(struct rt6_info *rt) 186d52d3997SMartin KaFai Lau { 187d52d3997SMartin KaFai Lau return dst_metrics_write_ptr(rt->dst.from); 188d52d3997SMartin KaFai Lau } 189d52d3997SMartin KaFai Lau 19006582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old) 19106582540SDavid S. Miller { 19206582540SDavid S. Miller struct rt6_info *rt = (struct rt6_info *)dst; 19306582540SDavid S. Miller 194d52d3997SMartin KaFai Lau if (rt->rt6i_flags & RTF_PCPU) 195d52d3997SMartin KaFai Lau return rt6_pcpu_cow_metrics(rt); 196d52d3997SMartin KaFai Lau else if (rt->rt6i_flags & RTF_CACHE) 1974b32b5adSMartin KaFai Lau return NULL; 1984b32b5adSMartin KaFai Lau else 1993b471175SMartin KaFai Lau return dst_cow_metrics_generic(dst, old); 20006582540SDavid S. Miller } 20106582540SDavid S. Miller 202f894cbf8SDavid S. Miller static inline const void *choose_neigh_daddr(struct rt6_info *rt, 203f894cbf8SDavid S. Miller struct sk_buff *skb, 204f894cbf8SDavid S. Miller const void *daddr) 20539232973SDavid S. Miller { 20639232973SDavid S. Miller struct in6_addr *p = &rt->rt6i_gateway; 20739232973SDavid S. Miller 208a7563f34SDavid S. Miller if (!ipv6_addr_any(p)) 20939232973SDavid S. Miller return (const void *) p; 210f894cbf8SDavid S. Miller else if (skb) 211f894cbf8SDavid S. Miller return &ipv6_hdr(skb)->daddr; 21239232973SDavid S. Miller return daddr; 21339232973SDavid S. Miller } 21439232973SDavid S. Miller 215f894cbf8SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst, 216f894cbf8SDavid S. Miller struct sk_buff *skb, 217f894cbf8SDavid S. Miller const void *daddr) 218d3aaeb38SDavid S. Miller { 21939232973SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 22039232973SDavid S. Miller struct neighbour *n; 22139232973SDavid S. Miller 222f894cbf8SDavid S. Miller daddr = choose_neigh_daddr(rt, skb, daddr); 2238e022ee6SYOSHIFUJI Hideaki / 吉藤英明 n = __ipv6_neigh_lookup(dst->dev, daddr); 224f83c7790SDavid S. Miller if (n) 225f83c7790SDavid S. Miller return n; 226f83c7790SDavid S. Miller return neigh_create(&nd_tbl, daddr, dst->dev); 227f83c7790SDavid S. Miller } 228f83c7790SDavid S. Miller 22963fca65dSJulian Anastasov static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr) 23063fca65dSJulian Anastasov { 23163fca65dSJulian Anastasov struct net_device *dev = dst->dev; 23263fca65dSJulian Anastasov struct rt6_info *rt = (struct rt6_info *)dst; 23363fca65dSJulian Anastasov 23463fca65dSJulian Anastasov daddr = choose_neigh_daddr(rt, NULL, daddr); 23563fca65dSJulian Anastasov if (!daddr) 23663fca65dSJulian Anastasov return; 23763fca65dSJulian Anastasov if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 23863fca65dSJulian Anastasov return; 23963fca65dSJulian Anastasov if (ipv6_addr_is_multicast((const struct in6_addr *)daddr)) 24063fca65dSJulian Anastasov return; 24163fca65dSJulian Anastasov __ipv6_confirm_neigh(dev, daddr); 24263fca65dSJulian Anastasov } 24363fca65dSJulian Anastasov 2449a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = { 2451da177e4SLinus Torvalds .family = AF_INET6, 2461da177e4SLinus Torvalds .gc = ip6_dst_gc, 2471da177e4SLinus Torvalds .gc_thresh = 1024, 2481da177e4SLinus Torvalds .check = ip6_dst_check, 2490dbaee3bSDavid S. Miller .default_advmss = ip6_default_advmss, 250ebb762f2SSteffen Klassert .mtu = ip6_mtu, 25106582540SDavid S. Miller .cow_metrics = ipv6_cow_metrics, 2521da177e4SLinus Torvalds .destroy = ip6_dst_destroy, 2531da177e4SLinus Torvalds .ifdown = ip6_dst_ifdown, 2541da177e4SLinus Torvalds .negative_advice = ip6_negative_advice, 2551da177e4SLinus Torvalds .link_failure = ip6_link_failure, 2561da177e4SLinus Torvalds .update_pmtu = ip6_rt_update_pmtu, 2576e157b6aSDavid S. Miller .redirect = rt6_do_redirect, 2589f8955ccSEric W. Biederman .local_out = __ip6_local_out, 259d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 26063fca65dSJulian Anastasov .confirm_neigh = ip6_confirm_neigh, 2611da177e4SLinus Torvalds }; 2621da177e4SLinus Torvalds 263ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst) 264ec831ea7SRoland Dreier { 265618f9bc7SSteffen Klassert unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); 266618f9bc7SSteffen Klassert 267618f9bc7SSteffen Klassert return mtu ? : dst->dev->mtu; 268ec831ea7SRoland Dreier } 269ec831ea7SRoland Dreier 2706700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk, 2716700c270SDavid S. Miller struct sk_buff *skb, u32 mtu) 27214e50e57SDavid S. Miller { 27314e50e57SDavid S. Miller } 27414e50e57SDavid S. Miller 2756700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk, 2766700c270SDavid S. Miller struct sk_buff *skb) 277b587ee3bSDavid S. Miller { 278b587ee3bSDavid S. Miller } 279b587ee3bSDavid S. Miller 28014e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = { 28114e50e57SDavid S. Miller .family = AF_INET6, 28214e50e57SDavid S. Miller .destroy = ip6_dst_destroy, 28314e50e57SDavid S. Miller .check = ip6_dst_check, 284ebb762f2SSteffen Klassert .mtu = ip6_blackhole_mtu, 285214f45c9SEric Dumazet .default_advmss = ip6_default_advmss, 28614e50e57SDavid S. Miller .update_pmtu = ip6_rt_blackhole_update_pmtu, 287b587ee3bSDavid S. Miller .redirect = ip6_rt_blackhole_redirect, 2880a1f5962SMartin KaFai Lau .cow_metrics = dst_cow_metrics_generic, 289d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 29014e50e57SDavid S. Miller }; 29114e50e57SDavid S. Miller 29262fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = { 29314edd87dSLi RongQing [RTAX_HOPLIMIT - 1] = 0, 29462fa8a84SDavid S. Miller }; 29562fa8a84SDavid S. Miller 296fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = { 2971da177e4SLinus Torvalds .dst = { 2981da177e4SLinus Torvalds .__refcnt = ATOMIC_INIT(1), 2991da177e4SLinus Torvalds .__use = 1, 3002c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 3011da177e4SLinus Torvalds .error = -ENETUNREACH, 3021da177e4SLinus Torvalds .input = ip6_pkt_discard, 3031da177e4SLinus Torvalds .output = ip6_pkt_discard_out, 3041da177e4SLinus Torvalds }, 3051da177e4SLinus Torvalds .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 3064f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 3071da177e4SLinus Torvalds .rt6i_metric = ~(u32) 0, 3081da177e4SLinus Torvalds .rt6i_ref = ATOMIC_INIT(1), 3091da177e4SLinus Torvalds }; 3101da177e4SLinus Torvalds 311101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES 312101367c2SThomas Graf 313fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = { 314101367c2SThomas Graf .dst = { 315101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 316101367c2SThomas Graf .__use = 1, 3172c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 318101367c2SThomas Graf .error = -EACCES, 3199ce8ade0SThomas Graf .input = ip6_pkt_prohibit, 3209ce8ade0SThomas Graf .output = ip6_pkt_prohibit_out, 321101367c2SThomas Graf }, 322101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 3234f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 324101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 325101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 326101367c2SThomas Graf }; 327101367c2SThomas Graf 328fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = { 329101367c2SThomas Graf .dst = { 330101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 331101367c2SThomas Graf .__use = 1, 3322c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 333101367c2SThomas Graf .error = -EINVAL, 334352e512cSHerbert Xu .input = dst_discard, 335ede2059dSEric W. Biederman .output = dst_discard_out, 336101367c2SThomas Graf }, 337101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 3384f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 339101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 340101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 341101367c2SThomas Graf }; 342101367c2SThomas Graf 343101367c2SThomas Graf #endif 344101367c2SThomas Graf 345ebfa45f0SMartin KaFai Lau static void rt6_info_init(struct rt6_info *rt) 346ebfa45f0SMartin KaFai Lau { 347ebfa45f0SMartin KaFai Lau struct dst_entry *dst = &rt->dst; 348ebfa45f0SMartin KaFai Lau 349ebfa45f0SMartin KaFai Lau memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst)); 350ebfa45f0SMartin KaFai Lau INIT_LIST_HEAD(&rt->rt6i_siblings); 351ebfa45f0SMartin KaFai Lau INIT_LIST_HEAD(&rt->rt6i_uncached); 352ebfa45f0SMartin KaFai Lau } 353ebfa45f0SMartin KaFai Lau 3541da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */ 355d52d3997SMartin KaFai Lau static struct rt6_info *__ip6_dst_alloc(struct net *net, 356957c665fSDavid S. Miller struct net_device *dev, 357ad706862SMartin KaFai Lau int flags) 3581da177e4SLinus Torvalds { 35997bab73fSDavid S. Miller struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev, 360b2a9c0edSWei Wang 1, DST_OBSOLETE_FORCE_CHK, flags); 361cf911662SDavid S. Miller 362ebfa45f0SMartin KaFai Lau if (rt) 363ebfa45f0SMartin KaFai Lau rt6_info_init(rt); 3648104891bSSteffen Klassert 365cf911662SDavid S. Miller return rt; 3661da177e4SLinus Torvalds } 3671da177e4SLinus Torvalds 3689ab179d8SDavid Ahern struct rt6_info *ip6_dst_alloc(struct net *net, 369d52d3997SMartin KaFai Lau struct net_device *dev, 370ad706862SMartin KaFai Lau int flags) 371d52d3997SMartin KaFai Lau { 372ad706862SMartin KaFai Lau struct rt6_info *rt = __ip6_dst_alloc(net, dev, flags); 373d52d3997SMartin KaFai Lau 374d52d3997SMartin KaFai Lau if (rt) { 375d52d3997SMartin KaFai Lau rt->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, GFP_ATOMIC); 376d52d3997SMartin KaFai Lau if (rt->rt6i_pcpu) { 377d52d3997SMartin KaFai Lau int cpu; 378d52d3997SMartin KaFai Lau 379d52d3997SMartin KaFai Lau for_each_possible_cpu(cpu) { 380d52d3997SMartin KaFai Lau struct rt6_info **p; 381d52d3997SMartin KaFai Lau 382d52d3997SMartin KaFai Lau p = per_cpu_ptr(rt->rt6i_pcpu, cpu); 383d52d3997SMartin KaFai Lau /* no one shares rt */ 384d52d3997SMartin KaFai Lau *p = NULL; 385d52d3997SMartin KaFai Lau } 386d52d3997SMartin KaFai Lau } else { 387587fea74SWei Wang dst_release_immediate(&rt->dst); 388d52d3997SMartin KaFai Lau return NULL; 389d52d3997SMartin KaFai Lau } 390d52d3997SMartin KaFai Lau } 391d52d3997SMartin KaFai Lau 392d52d3997SMartin KaFai Lau return rt; 393d52d3997SMartin KaFai Lau } 3949ab179d8SDavid Ahern EXPORT_SYMBOL(ip6_dst_alloc); 395d52d3997SMartin KaFai Lau 3961da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst) 3971da177e4SLinus Torvalds { 3981da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 39935732d01SWei Wang struct rt6_exception_bucket *bucket; 400ecd98837SYOSHIFUJI Hideaki / 吉藤英明 struct dst_entry *from = dst->from; 4018d0b94afSMartin KaFai Lau struct inet6_dev *idev; 4021da177e4SLinus Torvalds 4038e2ec639SYan, Zheng dst_destroy_metrics_generic(dst); 404d52d3997SMartin KaFai Lau free_percpu(rt->rt6i_pcpu); 4058d0b94afSMartin KaFai Lau rt6_uncached_list_del(rt); 4068d0b94afSMartin KaFai Lau 4078d0b94afSMartin KaFai Lau idev = rt->rt6i_idev; 40838308473SDavid S. Miller if (idev) { 4091da177e4SLinus Torvalds rt->rt6i_idev = NULL; 4101da177e4SLinus Torvalds in6_dev_put(idev); 4111da177e4SLinus Torvalds } 41235732d01SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1); 41335732d01SWei Wang if (bucket) { 41435732d01SWei Wang rt->rt6i_exception_bucket = NULL; 41535732d01SWei Wang kfree(bucket); 41635732d01SWei Wang } 4171716a961SGao feng 418ecd98837SYOSHIFUJI Hideaki / 吉藤英明 dst->from = NULL; 419ecd98837SYOSHIFUJI Hideaki / 吉藤英明 dst_release(from); 420b3419363SDavid S. Miller } 421b3419363SDavid S. Miller 4221da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 4231da177e4SLinus Torvalds int how) 4241da177e4SLinus Torvalds { 4251da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 4261da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 4275a3e55d6SDenis V. Lunev struct net_device *loopback_dev = 428c346dca1SYOSHIFUJI Hideaki dev_net(dev)->loopback_dev; 4291da177e4SLinus Torvalds 430e5645f51SWei Wang if (idev && idev->dev != loopback_dev) { 431e5645f51SWei Wang struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev); 43238308473SDavid S. Miller if (loopback_idev) { 4331da177e4SLinus Torvalds rt->rt6i_idev = loopback_idev; 4341da177e4SLinus Torvalds in6_dev_put(idev); 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds } 43797cac082SDavid S. Miller } 4381da177e4SLinus Torvalds 4395973fb1eSMartin KaFai Lau static bool __rt6_check_expired(const struct rt6_info *rt) 4405973fb1eSMartin KaFai Lau { 4415973fb1eSMartin KaFai Lau if (rt->rt6i_flags & RTF_EXPIRES) 4425973fb1eSMartin KaFai Lau return time_after(jiffies, rt->dst.expires); 4435973fb1eSMartin KaFai Lau else 4445973fb1eSMartin KaFai Lau return false; 4455973fb1eSMartin KaFai Lau } 4465973fb1eSMartin KaFai Lau 447a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt) 4481da177e4SLinus Torvalds { 4491716a961SGao feng if (rt->rt6i_flags & RTF_EXPIRES) { 4501716a961SGao feng if (time_after(jiffies, rt->dst.expires)) 451a50feda5SEric Dumazet return true; 4521716a961SGao feng } else if (rt->dst.from) { 4531e2ea8adSXin Long return rt->dst.obsolete != DST_OBSOLETE_FORCE_CHK || 4541e2ea8adSXin Long rt6_check_expired((struct rt6_info *)rt->dst.from); 4551716a961SGao feng } 456a50feda5SEric Dumazet return false; 4571da177e4SLinus Torvalds } 4581da177e4SLinus Torvalds 45951ebd318SNicolas Dichtel static struct rt6_info *rt6_multipath_select(struct rt6_info *match, 46052bd4c0cSNicolas Dichtel struct flowi6 *fl6, int oif, 46152bd4c0cSNicolas Dichtel int strict) 46251ebd318SNicolas Dichtel { 46351ebd318SNicolas Dichtel struct rt6_info *sibling, *next_sibling; 46451ebd318SNicolas Dichtel int route_choosen; 46551ebd318SNicolas Dichtel 466b673d6ccSJakub Sitnicki /* We might have already computed the hash for ICMPv6 errors. In such 467b673d6ccSJakub Sitnicki * case it will always be non-zero. Otherwise now is the time to do it. 468b673d6ccSJakub Sitnicki */ 469b673d6ccSJakub Sitnicki if (!fl6->mp_hash) 470b673d6ccSJakub Sitnicki fl6->mp_hash = rt6_multipath_hash(fl6, NULL); 471b673d6ccSJakub Sitnicki 472b673d6ccSJakub Sitnicki route_choosen = fl6->mp_hash % (match->rt6i_nsiblings + 1); 47351ebd318SNicolas Dichtel /* Don't change the route, if route_choosen == 0 47451ebd318SNicolas Dichtel * (siblings does not include ourself) 47551ebd318SNicolas Dichtel */ 47651ebd318SNicolas Dichtel if (route_choosen) 47751ebd318SNicolas Dichtel list_for_each_entry_safe(sibling, next_sibling, 47851ebd318SNicolas Dichtel &match->rt6i_siblings, rt6i_siblings) { 47951ebd318SNicolas Dichtel route_choosen--; 48051ebd318SNicolas Dichtel if (route_choosen == 0) { 48152bd4c0cSNicolas Dichtel if (rt6_score_route(sibling, oif, strict) < 0) 48252bd4c0cSNicolas Dichtel break; 48351ebd318SNicolas Dichtel match = sibling; 48451ebd318SNicolas Dichtel break; 48551ebd318SNicolas Dichtel } 48651ebd318SNicolas Dichtel } 48751ebd318SNicolas Dichtel return match; 48851ebd318SNicolas Dichtel } 48951ebd318SNicolas Dichtel 4901da177e4SLinus Torvalds /* 491c71099acSThomas Graf * Route lookup. Any table->tb6_lock is implied. 4921da177e4SLinus Torvalds */ 4931da177e4SLinus Torvalds 4948ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net, 4958ed67789SDaniel Lezcano struct rt6_info *rt, 496b71d1d42SEric Dumazet const struct in6_addr *saddr, 4971da177e4SLinus Torvalds int oif, 498d420895eSYOSHIFUJI Hideaki int flags) 4991da177e4SLinus Torvalds { 5001da177e4SLinus Torvalds struct rt6_info *local = NULL; 5011da177e4SLinus Torvalds struct rt6_info *sprt; 5021da177e4SLinus Torvalds 503dd3abc4eSYOSHIFUJI Hideaki if (!oif && ipv6_addr_any(saddr)) 504dd3abc4eSYOSHIFUJI Hideaki goto out; 505dd3abc4eSYOSHIFUJI Hideaki 506d8d1f30bSChangli Gao for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) { 507d1918542SDavid S. Miller struct net_device *dev = sprt->dst.dev; 508dd3abc4eSYOSHIFUJI Hideaki 509dd3abc4eSYOSHIFUJI Hideaki if (oif) { 5101da177e4SLinus Torvalds if (dev->ifindex == oif) 5111da177e4SLinus Torvalds return sprt; 5121da177e4SLinus Torvalds if (dev->flags & IFF_LOOPBACK) { 51338308473SDavid S. Miller if (!sprt->rt6i_idev || 5141da177e4SLinus Torvalds sprt->rt6i_idev->dev->ifindex != oif) { 51517fb0b2bSDavid Ahern if (flags & RT6_LOOKUP_F_IFACE) 5161da177e4SLinus Torvalds continue; 51717fb0b2bSDavid Ahern if (local && 51817fb0b2bSDavid Ahern local->rt6i_idev->dev->ifindex == oif) 5191da177e4SLinus Torvalds continue; 5201da177e4SLinus Torvalds } 5211da177e4SLinus Torvalds local = sprt; 5221da177e4SLinus Torvalds } 523dd3abc4eSYOSHIFUJI Hideaki } else { 524dd3abc4eSYOSHIFUJI Hideaki if (ipv6_chk_addr(net, saddr, dev, 525dd3abc4eSYOSHIFUJI Hideaki flags & RT6_LOOKUP_F_IFACE)) 526dd3abc4eSYOSHIFUJI Hideaki return sprt; 527dd3abc4eSYOSHIFUJI Hideaki } 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds 530dd3abc4eSYOSHIFUJI Hideaki if (oif) { 5311da177e4SLinus Torvalds if (local) 5321da177e4SLinus Torvalds return local; 5331da177e4SLinus Torvalds 534d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE) 5358ed67789SDaniel Lezcano return net->ipv6.ip6_null_entry; 5361da177e4SLinus Torvalds } 537dd3abc4eSYOSHIFUJI Hideaki out: 5381da177e4SLinus Torvalds return rt; 5391da177e4SLinus Torvalds } 5401da177e4SLinus Torvalds 54127097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 542c2f17e82SHannes Frederic Sowa struct __rt6_probe_work { 543c2f17e82SHannes Frederic Sowa struct work_struct work; 544c2f17e82SHannes Frederic Sowa struct in6_addr target; 545c2f17e82SHannes Frederic Sowa struct net_device *dev; 546c2f17e82SHannes Frederic Sowa }; 547c2f17e82SHannes Frederic Sowa 548c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w) 549c2f17e82SHannes Frederic Sowa { 550c2f17e82SHannes Frederic Sowa struct in6_addr mcaddr; 551c2f17e82SHannes Frederic Sowa struct __rt6_probe_work *work = 552c2f17e82SHannes Frederic Sowa container_of(w, struct __rt6_probe_work, work); 553c2f17e82SHannes Frederic Sowa 554c2f17e82SHannes Frederic Sowa addrconf_addr_solict_mult(&work->target, &mcaddr); 555adc176c5SErik Nordmark ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0); 556c2f17e82SHannes Frederic Sowa dev_put(work->dev); 557662f5533SMichael Büsch kfree(work); 558c2f17e82SHannes Frederic Sowa } 559c2f17e82SHannes Frederic Sowa 56027097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt) 56127097255SYOSHIFUJI Hideaki { 562990edb42SMartin KaFai Lau struct __rt6_probe_work *work; 563f2c31e32SEric Dumazet struct neighbour *neigh; 56427097255SYOSHIFUJI Hideaki /* 56527097255SYOSHIFUJI Hideaki * Okay, this does not seem to be appropriate 56627097255SYOSHIFUJI Hideaki * for now, however, we need to check if it 56727097255SYOSHIFUJI Hideaki * is really so; aka Router Reachability Probing. 56827097255SYOSHIFUJI Hideaki * 56927097255SYOSHIFUJI Hideaki * Router Reachability Probe MUST be rate-limited 57027097255SYOSHIFUJI Hideaki * to no more than one per minute. 57127097255SYOSHIFUJI Hideaki */ 5722152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (!rt || !(rt->rt6i_flags & RTF_GATEWAY)) 573fdd6681dSAmerigo Wang return; 5742152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 5752152caeaSYOSHIFUJI Hideaki / 吉藤英明 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 5762152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 5778d6c31bfSMartin KaFai Lau if (neigh->nud_state & NUD_VALID) 5788d6c31bfSMartin KaFai Lau goto out; 5798d6c31bfSMartin KaFai Lau 580990edb42SMartin KaFai Lau work = NULL; 5812152caeaSYOSHIFUJI Hideaki / 吉藤英明 write_lock(&neigh->lock); 582990edb42SMartin KaFai Lau if (!(neigh->nud_state & NUD_VALID) && 583990edb42SMartin KaFai Lau time_after(jiffies, 584990edb42SMartin KaFai Lau neigh->updated + 585990edb42SMartin KaFai Lau rt->rt6i_idev->cnf.rtr_probe_interval)) { 586c2f17e82SHannes Frederic Sowa work = kmalloc(sizeof(*work), GFP_ATOMIC); 587990edb42SMartin KaFai Lau if (work) 5887e980569SJiri Benc __neigh_set_probe_once(neigh); 589990edb42SMartin KaFai Lau } 590c2f17e82SHannes Frederic Sowa write_unlock(&neigh->lock); 591990edb42SMartin KaFai Lau } else { 592990edb42SMartin KaFai Lau work = kmalloc(sizeof(*work), GFP_ATOMIC); 593990edb42SMartin KaFai Lau } 594c2f17e82SHannes Frederic Sowa 595c2f17e82SHannes Frederic Sowa if (work) { 596c2f17e82SHannes Frederic Sowa INIT_WORK(&work->work, rt6_probe_deferred); 597c2f17e82SHannes Frederic Sowa work->target = rt->rt6i_gateway; 598c2f17e82SHannes Frederic Sowa dev_hold(rt->dst.dev); 599c2f17e82SHannes Frederic Sowa work->dev = rt->dst.dev; 600c2f17e82SHannes Frederic Sowa schedule_work(&work->work); 601c2f17e82SHannes Frederic Sowa } 602990edb42SMartin KaFai Lau 6038d6c31bfSMartin KaFai Lau out: 6042152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 605f2c31e32SEric Dumazet } 60627097255SYOSHIFUJI Hideaki #else 60727097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt) 60827097255SYOSHIFUJI Hideaki { 60927097255SYOSHIFUJI Hideaki } 61027097255SYOSHIFUJI Hideaki #endif 61127097255SYOSHIFUJI Hideaki 6121da177e4SLinus Torvalds /* 613554cfb7eSYOSHIFUJI Hideaki * Default Router Selection (RFC 2461 6.3.6) 6141da177e4SLinus Torvalds */ 615b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif) 6161da177e4SLinus Torvalds { 617d1918542SDavid S. Miller struct net_device *dev = rt->dst.dev; 618161980f4SDavid S. Miller if (!oif || dev->ifindex == oif) 619554cfb7eSYOSHIFUJI Hideaki return 2; 620161980f4SDavid S. Miller if ((dev->flags & IFF_LOOPBACK) && 621161980f4SDavid S. Miller rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif) 622161980f4SDavid S. Miller return 1; 623554cfb7eSYOSHIFUJI Hideaki return 0; 6241da177e4SLinus Torvalds } 6251da177e4SLinus Torvalds 626afc154e9SHannes Frederic Sowa static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt) 6271da177e4SLinus Torvalds { 628f2c31e32SEric Dumazet struct neighbour *neigh; 629afc154e9SHannes Frederic Sowa enum rt6_nud_state ret = RT6_NUD_FAIL_HARD; 630f2c31e32SEric Dumazet 6314d0c5911SYOSHIFUJI Hideaki if (rt->rt6i_flags & RTF_NONEXTHOP || 6324d0c5911SYOSHIFUJI Hideaki !(rt->rt6i_flags & RTF_GATEWAY)) 633afc154e9SHannes Frederic Sowa return RT6_NUD_SUCCEED; 634145a3621SYOSHIFUJI Hideaki / 吉藤英明 635145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 636145a3621SYOSHIFUJI Hideaki / 吉藤英明 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 637145a3621SYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 638145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_lock(&neigh->lock); 639554cfb7eSYOSHIFUJI Hideaki if (neigh->nud_state & NUD_VALID) 640afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 641398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 642a5a81f0bSPaul Marks else if (!(neigh->nud_state & NUD_FAILED)) 643afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 6447e980569SJiri Benc else 6457e980569SJiri Benc ret = RT6_NUD_FAIL_PROBE; 646398bcbebSYOSHIFUJI Hideaki #endif 647145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_unlock(&neigh->lock); 648afc154e9SHannes Frederic Sowa } else { 649afc154e9SHannes Frederic Sowa ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ? 6507e980569SJiri Benc RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR; 651a5a81f0bSPaul Marks } 652145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 653145a3621SYOSHIFUJI Hideaki / 吉藤英明 654a5a81f0bSPaul Marks return ret; 6551da177e4SLinus Torvalds } 6561da177e4SLinus Torvalds 657554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif, 658554cfb7eSYOSHIFUJI Hideaki int strict) 659554cfb7eSYOSHIFUJI Hideaki { 660a5a81f0bSPaul Marks int m; 6614d0c5911SYOSHIFUJI Hideaki 6624d0c5911SYOSHIFUJI Hideaki m = rt6_check_dev(rt, oif); 66377d16f45SYOSHIFUJI Hideaki if (!m && (strict & RT6_LOOKUP_F_IFACE)) 664afc154e9SHannes Frederic Sowa return RT6_NUD_FAIL_HARD; 665ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 666ebacaaa0SYOSHIFUJI Hideaki m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2; 667ebacaaa0SYOSHIFUJI Hideaki #endif 668afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) { 669afc154e9SHannes Frederic Sowa int n = rt6_check_neigh(rt); 670afc154e9SHannes Frederic Sowa if (n < 0) 671afc154e9SHannes Frederic Sowa return n; 672afc154e9SHannes Frederic Sowa } 673554cfb7eSYOSHIFUJI Hideaki return m; 674554cfb7eSYOSHIFUJI Hideaki } 675554cfb7eSYOSHIFUJI Hideaki 676f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict, 677afc154e9SHannes Frederic Sowa int *mpri, struct rt6_info *match, 678afc154e9SHannes Frederic Sowa bool *do_rr) 679554cfb7eSYOSHIFUJI Hideaki { 680554cfb7eSYOSHIFUJI Hideaki int m; 681afc154e9SHannes Frederic Sowa bool match_do_rr = false; 68235103d11SAndy Gospodarek struct inet6_dev *idev = rt->rt6i_idev; 68335103d11SAndy Gospodarek struct net_device *dev = rt->dst.dev; 68435103d11SAndy Gospodarek 68535103d11SAndy Gospodarek if (dev && !netif_carrier_ok(dev) && 686d5d32e4bSDavid Ahern idev->cnf.ignore_routes_with_linkdown && 687d5d32e4bSDavid Ahern !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE)) 68835103d11SAndy Gospodarek goto out; 689554cfb7eSYOSHIFUJI Hideaki 690554cfb7eSYOSHIFUJI Hideaki if (rt6_check_expired(rt)) 691f11e6659SDavid S. Miller goto out; 692554cfb7eSYOSHIFUJI Hideaki 693554cfb7eSYOSHIFUJI Hideaki m = rt6_score_route(rt, oif, strict); 6947e980569SJiri Benc if (m == RT6_NUD_FAIL_DO_RR) { 695afc154e9SHannes Frederic Sowa match_do_rr = true; 696afc154e9SHannes Frederic Sowa m = 0; /* lowest valid score */ 6977e980569SJiri Benc } else if (m == RT6_NUD_FAIL_HARD) { 698f11e6659SDavid S. Miller goto out; 6991da177e4SLinus Torvalds } 700f11e6659SDavid S. Miller 701afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) 702afc154e9SHannes Frederic Sowa rt6_probe(rt); 703afc154e9SHannes Frederic Sowa 7047e980569SJiri Benc /* note that m can be RT6_NUD_FAIL_PROBE at this point */ 705afc154e9SHannes Frederic Sowa if (m > *mpri) { 706afc154e9SHannes Frederic Sowa *do_rr = match_do_rr; 707afc154e9SHannes Frederic Sowa *mpri = m; 708afc154e9SHannes Frederic Sowa match = rt; 709afc154e9SHannes Frederic Sowa } 710f11e6659SDavid S. Miller out: 711f11e6659SDavid S. Miller return match; 7121da177e4SLinus Torvalds } 7131da177e4SLinus Torvalds 714f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn, 715f11e6659SDavid S. Miller struct rt6_info *rr_head, 716afc154e9SHannes Frederic Sowa u32 metric, int oif, int strict, 717afc154e9SHannes Frederic Sowa bool *do_rr) 718f11e6659SDavid S. Miller { 7199fbdcfafSSteffen Klassert struct rt6_info *rt, *match, *cont; 720f11e6659SDavid S. Miller int mpri = -1; 721f11e6659SDavid S. Miller 722f11e6659SDavid S. Miller match = NULL; 7239fbdcfafSSteffen Klassert cont = NULL; 7249fbdcfafSSteffen Klassert for (rt = rr_head; rt; rt = rt->dst.rt6_next) { 7259fbdcfafSSteffen Klassert if (rt->rt6i_metric != metric) { 7269fbdcfafSSteffen Klassert cont = rt; 7279fbdcfafSSteffen Klassert break; 7289fbdcfafSSteffen Klassert } 7299fbdcfafSSteffen Klassert 730afc154e9SHannes Frederic Sowa match = find_match(rt, oif, strict, &mpri, match, do_rr); 7319fbdcfafSSteffen Klassert } 7329fbdcfafSSteffen Klassert 7339fbdcfafSSteffen Klassert for (rt = fn->leaf; rt && rt != rr_head; rt = rt->dst.rt6_next) { 7349fbdcfafSSteffen Klassert if (rt->rt6i_metric != metric) { 7359fbdcfafSSteffen Klassert cont = rt; 7369fbdcfafSSteffen Klassert break; 7379fbdcfafSSteffen Klassert } 7389fbdcfafSSteffen Klassert 7399fbdcfafSSteffen Klassert match = find_match(rt, oif, strict, &mpri, match, do_rr); 7409fbdcfafSSteffen Klassert } 7419fbdcfafSSteffen Klassert 7429fbdcfafSSteffen Klassert if (match || !cont) 7439fbdcfafSSteffen Klassert return match; 7449fbdcfafSSteffen Klassert 7459fbdcfafSSteffen Klassert for (rt = cont; rt; rt = rt->dst.rt6_next) 746afc154e9SHannes Frederic Sowa match = find_match(rt, oif, strict, &mpri, match, do_rr); 747f11e6659SDavid S. Miller 748f11e6659SDavid S. Miller return match; 749f11e6659SDavid S. Miller } 750f11e6659SDavid S. Miller 751f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict) 752f11e6659SDavid S. Miller { 753f11e6659SDavid S. Miller struct rt6_info *match, *rt0; 7548ed67789SDaniel Lezcano struct net *net; 755afc154e9SHannes Frederic Sowa bool do_rr = false; 756f11e6659SDavid S. Miller 757f11e6659SDavid S. Miller rt0 = fn->rr_ptr; 758f11e6659SDavid S. Miller if (!rt0) 759f11e6659SDavid S. Miller fn->rr_ptr = rt0 = fn->leaf; 760f11e6659SDavid S. Miller 761afc154e9SHannes Frederic Sowa match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict, 762afc154e9SHannes Frederic Sowa &do_rr); 763f11e6659SDavid S. Miller 764afc154e9SHannes Frederic Sowa if (do_rr) { 765d8d1f30bSChangli Gao struct rt6_info *next = rt0->dst.rt6_next; 766f11e6659SDavid S. Miller 767554cfb7eSYOSHIFUJI Hideaki /* no entries matched; do round-robin */ 768f11e6659SDavid S. Miller if (!next || next->rt6i_metric != rt0->rt6i_metric) 769f11e6659SDavid S. Miller next = fn->leaf; 770f11e6659SDavid S. Miller 771f11e6659SDavid S. Miller if (next != rt0) 772f11e6659SDavid S. Miller fn->rr_ptr = next; 773554cfb7eSYOSHIFUJI Hideaki } 774554cfb7eSYOSHIFUJI Hideaki 775d1918542SDavid S. Miller net = dev_net(rt0->dst.dev); 776a02cec21SEric Dumazet return match ? match : net->ipv6.ip6_null_entry; 7771da177e4SLinus Torvalds } 7781da177e4SLinus Torvalds 7798b9df265SMartin KaFai Lau static bool rt6_is_gw_or_nonexthop(const struct rt6_info *rt) 7808b9df265SMartin KaFai Lau { 7818b9df265SMartin KaFai Lau return (rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY)); 7828b9df265SMartin KaFai Lau } 7838b9df265SMartin KaFai Lau 78470ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 78570ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 786b71d1d42SEric Dumazet const struct in6_addr *gwaddr) 78770ceb4f5SYOSHIFUJI Hideaki { 788c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 78970ceb4f5SYOSHIFUJI Hideaki struct route_info *rinfo = (struct route_info *) opt; 79070ceb4f5SYOSHIFUJI Hideaki struct in6_addr prefix_buf, *prefix; 79170ceb4f5SYOSHIFUJI Hideaki unsigned int pref; 7924bed72e4SYOSHIFUJI Hideaki unsigned long lifetime; 79370ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt; 79470ceb4f5SYOSHIFUJI Hideaki 79570ceb4f5SYOSHIFUJI Hideaki if (len < sizeof(struct route_info)) { 79670ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 79770ceb4f5SYOSHIFUJI Hideaki } 79870ceb4f5SYOSHIFUJI Hideaki 79970ceb4f5SYOSHIFUJI Hideaki /* Sanity check for prefix_len and length */ 80070ceb4f5SYOSHIFUJI Hideaki if (rinfo->length > 3) { 80170ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 80270ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 128) { 80370ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 80470ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 64) { 80570ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 2) { 80670ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 80770ceb4f5SYOSHIFUJI Hideaki } 80870ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 0) { 80970ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 1) { 81070ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 81170ceb4f5SYOSHIFUJI Hideaki } 81270ceb4f5SYOSHIFUJI Hideaki } 81370ceb4f5SYOSHIFUJI Hideaki 81470ceb4f5SYOSHIFUJI Hideaki pref = rinfo->route_pref; 81570ceb4f5SYOSHIFUJI Hideaki if (pref == ICMPV6_ROUTER_PREF_INVALID) 8163933fc95SJens Rosenboom return -EINVAL; 81770ceb4f5SYOSHIFUJI Hideaki 8184bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); 81970ceb4f5SYOSHIFUJI Hideaki 82070ceb4f5SYOSHIFUJI Hideaki if (rinfo->length == 3) 82170ceb4f5SYOSHIFUJI Hideaki prefix = (struct in6_addr *)rinfo->prefix; 82270ceb4f5SYOSHIFUJI Hideaki else { 82370ceb4f5SYOSHIFUJI Hideaki /* this function is safe */ 82470ceb4f5SYOSHIFUJI Hideaki ipv6_addr_prefix(&prefix_buf, 82570ceb4f5SYOSHIFUJI Hideaki (struct in6_addr *)rinfo->prefix, 82670ceb4f5SYOSHIFUJI Hideaki rinfo->prefix_len); 82770ceb4f5SYOSHIFUJI Hideaki prefix = &prefix_buf; 82870ceb4f5SYOSHIFUJI Hideaki } 82970ceb4f5SYOSHIFUJI Hideaki 830f104a567SDuan Jiong if (rinfo->prefix_len == 0) 831f104a567SDuan Jiong rt = rt6_get_dflt_router(gwaddr, dev); 832f104a567SDuan Jiong else 833f104a567SDuan Jiong rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, 834830218c1SDavid Ahern gwaddr, dev); 83570ceb4f5SYOSHIFUJI Hideaki 83670ceb4f5SYOSHIFUJI Hideaki if (rt && !lifetime) { 837e0a1ad73SThomas Graf ip6_del_rt(rt); 83870ceb4f5SYOSHIFUJI Hideaki rt = NULL; 83970ceb4f5SYOSHIFUJI Hideaki } 84070ceb4f5SYOSHIFUJI Hideaki 84170ceb4f5SYOSHIFUJI Hideaki if (!rt && lifetime) 842830218c1SDavid Ahern rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, 843830218c1SDavid Ahern dev, pref); 84470ceb4f5SYOSHIFUJI Hideaki else if (rt) 84570ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags = RTF_ROUTEINFO | 84670ceb4f5SYOSHIFUJI Hideaki (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 84770ceb4f5SYOSHIFUJI Hideaki 84870ceb4f5SYOSHIFUJI Hideaki if (rt) { 8491716a961SGao feng if (!addrconf_finite_timeout(lifetime)) 8501716a961SGao feng rt6_clean_expires(rt); 8511716a961SGao feng else 8521716a961SGao feng rt6_set_expires(rt, jiffies + HZ * lifetime); 8531716a961SGao feng 85494e187c0SAmerigo Wang ip6_rt_put(rt); 85570ceb4f5SYOSHIFUJI Hideaki } 85670ceb4f5SYOSHIFUJI Hideaki return 0; 85770ceb4f5SYOSHIFUJI Hideaki } 85870ceb4f5SYOSHIFUJI Hideaki #endif 85970ceb4f5SYOSHIFUJI Hideaki 860a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn, 861a3c00e46SMartin KaFai Lau struct in6_addr *saddr) 862a3c00e46SMartin KaFai Lau { 863a3c00e46SMartin KaFai Lau struct fib6_node *pn; 864a3c00e46SMartin KaFai Lau while (1) { 865a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_TL_ROOT) 866a3c00e46SMartin KaFai Lau return NULL; 867a3c00e46SMartin KaFai Lau pn = fn->parent; 868a3c00e46SMartin KaFai Lau if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) 869a3c00e46SMartin KaFai Lau fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); 870a3c00e46SMartin KaFai Lau else 871a3c00e46SMartin KaFai Lau fn = pn; 872a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_RTINFO) 873a3c00e46SMartin KaFai Lau return fn; 874a3c00e46SMartin KaFai Lau } 875a3c00e46SMartin KaFai Lau } 876c71099acSThomas Graf 8778ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net, 8788ed67789SDaniel Lezcano struct fib6_table *table, 8794c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 8801da177e4SLinus Torvalds { 8811da177e4SLinus Torvalds struct fib6_node *fn; 8821da177e4SLinus Torvalds struct rt6_info *rt; 8831da177e4SLinus Torvalds 884c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 8854c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 886c71099acSThomas Graf restart: 887c71099acSThomas Graf rt = fn->leaf; 8884c9483b2SDavid S. Miller rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags); 88951ebd318SNicolas Dichtel if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0) 89052bd4c0cSNicolas Dichtel rt = rt6_multipath_select(rt, fl6, fl6->flowi6_oif, flags); 891a3c00e46SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 892a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 893a3c00e46SMartin KaFai Lau if (fn) 894a3c00e46SMartin KaFai Lau goto restart; 895a3c00e46SMartin KaFai Lau } 896d8d1f30bSChangli Gao dst_use(&rt->dst, jiffies); 897c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 898b811580dSDavid Ahern 899b811580dSDavid Ahern trace_fib6_table_lookup(net, rt, table->tb6_id, fl6); 900b811580dSDavid Ahern 9011da177e4SLinus Torvalds return rt; 902c71099acSThomas Graf 903c71099acSThomas Graf } 904c71099acSThomas Graf 905ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6, 906ea6e574eSFlorian Westphal int flags) 907ea6e574eSFlorian Westphal { 908ea6e574eSFlorian Westphal return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup); 909ea6e574eSFlorian Westphal } 910ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup); 911ea6e574eSFlorian Westphal 9129acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, 9139acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *saddr, int oif, int strict) 914c71099acSThomas Graf { 9154c9483b2SDavid S. Miller struct flowi6 fl6 = { 9164c9483b2SDavid S. Miller .flowi6_oif = oif, 9174c9483b2SDavid S. Miller .daddr = *daddr, 918c71099acSThomas Graf }; 919c71099acSThomas Graf struct dst_entry *dst; 92077d16f45SYOSHIFUJI Hideaki int flags = strict ? RT6_LOOKUP_F_IFACE : 0; 921c71099acSThomas Graf 922adaa70bbSThomas Graf if (saddr) { 9234c9483b2SDavid S. Miller memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 924adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 925adaa70bbSThomas Graf } 926adaa70bbSThomas Graf 9274c9483b2SDavid S. Miller dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup); 928c71099acSThomas Graf if (dst->error == 0) 929c71099acSThomas Graf return (struct rt6_info *) dst; 930c71099acSThomas Graf 931c71099acSThomas Graf dst_release(dst); 932c71099acSThomas Graf 9331da177e4SLinus Torvalds return NULL; 9341da177e4SLinus Torvalds } 9357159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup); 9367159039aSYOSHIFUJI Hideaki 937c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock. 9381cfb71eeSWei Wang * It takes new route entry, the addition fails by any reason the 9391cfb71eeSWei Wang * route is released. 9401cfb71eeSWei Wang * Caller must hold dst before calling it. 9411da177e4SLinus Torvalds */ 9421da177e4SLinus Torvalds 943e5fd387aSMichal Kubeček static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info, 944333c4301SDavid Ahern struct mx6_config *mxc, 945333c4301SDavid Ahern struct netlink_ext_ack *extack) 9461da177e4SLinus Torvalds { 9471da177e4SLinus Torvalds int err; 948c71099acSThomas Graf struct fib6_table *table; 9491da177e4SLinus Torvalds 950c71099acSThomas Graf table = rt->rt6i_table; 951c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 952333c4301SDavid Ahern err = fib6_add(&table->tb6_root, rt, info, mxc, extack); 953c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds return err; 9561da177e4SLinus Torvalds } 9571da177e4SLinus Torvalds 95840e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt) 95940e22e8fSThomas Graf { 960e715b6d3SFlorian Westphal struct nl_info info = { .nl_net = dev_net(rt->dst.dev), }; 961e715b6d3SFlorian Westphal struct mx6_config mxc = { .mx = NULL, }; 962e715b6d3SFlorian Westphal 9631cfb71eeSWei Wang /* Hold dst to account for the reference from the fib6 tree */ 9641cfb71eeSWei Wang dst_hold(&rt->dst); 965333c4301SDavid Ahern return __ip6_ins_rt(rt, &info, &mxc, NULL); 96640e22e8fSThomas Graf } 96740e22e8fSThomas Graf 9684832c30dSDavid Ahern /* called with rcu_lock held */ 9694832c30dSDavid Ahern static struct net_device *ip6_rt_get_dev_rcu(struct rt6_info *rt) 9704832c30dSDavid Ahern { 9714832c30dSDavid Ahern struct net_device *dev = rt->dst.dev; 9724832c30dSDavid Ahern 9734832c30dSDavid Ahern if (rt->rt6i_flags & RTF_LOCAL) { 9744832c30dSDavid Ahern /* for copies of local routes, dst->dev needs to be the 9754832c30dSDavid Ahern * device if it is a master device, the master device if 9764832c30dSDavid Ahern * device is enslaved, and the loopback as the default 9774832c30dSDavid Ahern */ 9784832c30dSDavid Ahern if (netif_is_l3_slave(dev) && 9794832c30dSDavid Ahern !rt6_need_strict(&rt->rt6i_dst.addr)) 9804832c30dSDavid Ahern dev = l3mdev_master_dev_rcu(dev); 9814832c30dSDavid Ahern else if (!netif_is_l3_master(dev)) 9824832c30dSDavid Ahern dev = dev_net(dev)->loopback_dev; 9834832c30dSDavid Ahern /* last case is netif_is_l3_master(dev) is true in which 9844832c30dSDavid Ahern * case we want dev returned to be dev 9854832c30dSDavid Ahern */ 9864832c30dSDavid Ahern } 9874832c30dSDavid Ahern 9884832c30dSDavid Ahern return dev; 9894832c30dSDavid Ahern } 9904832c30dSDavid Ahern 9918b9df265SMartin KaFai Lau static struct rt6_info *ip6_rt_cache_alloc(struct rt6_info *ort, 99221efcfa0SEric Dumazet const struct in6_addr *daddr, 993b71d1d42SEric Dumazet const struct in6_addr *saddr) 9941da177e4SLinus Torvalds { 9954832c30dSDavid Ahern struct net_device *dev; 9961da177e4SLinus Torvalds struct rt6_info *rt; 9971da177e4SLinus Torvalds 9981da177e4SLinus Torvalds /* 9991da177e4SLinus Torvalds * Clone the route. 10001da177e4SLinus Torvalds */ 10011da177e4SLinus Torvalds 1002d52d3997SMartin KaFai Lau if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)) 100383a09abdSMartin KaFai Lau ort = (struct rt6_info *)ort->dst.from; 10041da177e4SLinus Torvalds 10054832c30dSDavid Ahern rcu_read_lock(); 10064832c30dSDavid Ahern dev = ip6_rt_get_dev_rcu(ort); 10074832c30dSDavid Ahern rt = __ip6_dst_alloc(dev_net(dev), dev, 0); 10084832c30dSDavid Ahern rcu_read_unlock(); 100983a09abdSMartin KaFai Lau if (!rt) 101083a09abdSMartin KaFai Lau return NULL; 101183a09abdSMartin KaFai Lau 101283a09abdSMartin KaFai Lau ip6_rt_copy_init(rt, ort); 10138b9df265SMartin KaFai Lau rt->rt6i_flags |= RTF_CACHE; 101483a09abdSMartin KaFai Lau rt->rt6i_metric = 0; 101583a09abdSMartin KaFai Lau rt->dst.flags |= DST_HOST; 101683a09abdSMartin KaFai Lau rt->rt6i_dst.addr = *daddr; 101783a09abdSMartin KaFai Lau rt->rt6i_dst.plen = 128; 10188b9df265SMartin KaFai Lau 10198b9df265SMartin KaFai Lau if (!rt6_is_gw_or_nonexthop(ort)) { 1020bb3c3686SDavid S. Miller if (ort->rt6i_dst.plen != 128 && 102121efcfa0SEric Dumazet ipv6_addr_equal(&ort->rt6i_dst.addr, daddr)) 102258c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 10231da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 10241da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 10254e3fd7a0SAlexey Dobriyan rt->rt6i_src.addr = *saddr; 10261da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 10271da177e4SLinus Torvalds } 10281da177e4SLinus Torvalds #endif 102995a9a5baSYOSHIFUJI Hideaki } 103095a9a5baSYOSHIFUJI Hideaki 1031299d9939SYOSHIFUJI Hideaki return rt; 1032299d9939SYOSHIFUJI Hideaki } 1033299d9939SYOSHIFUJI Hideaki 1034d52d3997SMartin KaFai Lau static struct rt6_info *ip6_rt_pcpu_alloc(struct rt6_info *rt) 1035d52d3997SMartin KaFai Lau { 10364832c30dSDavid Ahern struct net_device *dev; 1037d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1038d52d3997SMartin KaFai Lau 10394832c30dSDavid Ahern rcu_read_lock(); 10404832c30dSDavid Ahern dev = ip6_rt_get_dev_rcu(rt); 10414832c30dSDavid Ahern pcpu_rt = __ip6_dst_alloc(dev_net(dev), dev, rt->dst.flags); 10424832c30dSDavid Ahern rcu_read_unlock(); 1043d52d3997SMartin KaFai Lau if (!pcpu_rt) 1044d52d3997SMartin KaFai Lau return NULL; 1045d52d3997SMartin KaFai Lau ip6_rt_copy_init(pcpu_rt, rt); 1046d52d3997SMartin KaFai Lau pcpu_rt->rt6i_protocol = rt->rt6i_protocol; 1047d52d3997SMartin KaFai Lau pcpu_rt->rt6i_flags |= RTF_PCPU; 1048d52d3997SMartin KaFai Lau return pcpu_rt; 1049d52d3997SMartin KaFai Lau } 1050d52d3997SMartin KaFai Lau 1051d52d3997SMartin KaFai Lau /* It should be called with read_lock_bh(&tb6_lock) acquired */ 1052d52d3997SMartin KaFai Lau static struct rt6_info *rt6_get_pcpu_route(struct rt6_info *rt) 1053d52d3997SMartin KaFai Lau { 1054a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, **p; 1055d52d3997SMartin KaFai Lau 1056d52d3997SMartin KaFai Lau p = this_cpu_ptr(rt->rt6i_pcpu); 1057d52d3997SMartin KaFai Lau pcpu_rt = *p; 1058d52d3997SMartin KaFai Lau 1059a73e4195SMartin KaFai Lau if (pcpu_rt) { 1060a73e4195SMartin KaFai Lau dst_hold(&pcpu_rt->dst); 1061a73e4195SMartin KaFai Lau rt6_dst_from_metrics_check(pcpu_rt); 1062a73e4195SMartin KaFai Lau } 1063a73e4195SMartin KaFai Lau return pcpu_rt; 1064a73e4195SMartin KaFai Lau } 1065a73e4195SMartin KaFai Lau 1066a73e4195SMartin KaFai Lau static struct rt6_info *rt6_make_pcpu_route(struct rt6_info *rt) 1067a73e4195SMartin KaFai Lau { 10689c7370a1SMartin KaFai Lau struct fib6_table *table = rt->rt6i_table; 1069a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, *prev, **p; 1070d52d3997SMartin KaFai Lau 1071d52d3997SMartin KaFai Lau pcpu_rt = ip6_rt_pcpu_alloc(rt); 1072d52d3997SMartin KaFai Lau if (!pcpu_rt) { 1073d52d3997SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 1074d52d3997SMartin KaFai Lau 10759c7370a1SMartin KaFai Lau dst_hold(&net->ipv6.ip6_null_entry->dst); 10769c7370a1SMartin KaFai Lau return net->ipv6.ip6_null_entry; 1077d52d3997SMartin KaFai Lau } 1078d52d3997SMartin KaFai Lau 10799c7370a1SMartin KaFai Lau read_lock_bh(&table->tb6_lock); 10809c7370a1SMartin KaFai Lau if (rt->rt6i_pcpu) { 1081a73e4195SMartin KaFai Lau p = this_cpu_ptr(rt->rt6i_pcpu); 1082d52d3997SMartin KaFai Lau prev = cmpxchg(p, NULL, pcpu_rt); 1083d52d3997SMartin KaFai Lau if (prev) { 1084d52d3997SMartin KaFai Lau /* If someone did it before us, return prev instead */ 1085587fea74SWei Wang dst_release_immediate(&pcpu_rt->dst); 1086d52d3997SMartin KaFai Lau pcpu_rt = prev; 1087d52d3997SMartin KaFai Lau } 10889c7370a1SMartin KaFai Lau } else { 10899c7370a1SMartin KaFai Lau /* rt has been removed from the fib6 tree 10909c7370a1SMartin KaFai Lau * before we have a chance to acquire the read_lock. 10919c7370a1SMartin KaFai Lau * In this case, don't brother to create a pcpu rt 10929c7370a1SMartin KaFai Lau * since rt is going away anyway. The next 10939c7370a1SMartin KaFai Lau * dst_check() will trigger a re-lookup. 10949c7370a1SMartin KaFai Lau */ 1095587fea74SWei Wang dst_release_immediate(&pcpu_rt->dst); 10969c7370a1SMartin KaFai Lau pcpu_rt = rt; 10979c7370a1SMartin KaFai Lau } 1098d52d3997SMartin KaFai Lau dst_hold(&pcpu_rt->dst); 1099d52d3997SMartin KaFai Lau rt6_dst_from_metrics_check(pcpu_rt); 11009c7370a1SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 1101d52d3997SMartin KaFai Lau return pcpu_rt; 1102d52d3997SMartin KaFai Lau } 1103d52d3997SMartin KaFai Lau 110435732d01SWei Wang /* exception hash table implementation 110535732d01SWei Wang */ 110635732d01SWei Wang static DEFINE_SPINLOCK(rt6_exception_lock); 110735732d01SWei Wang 110835732d01SWei Wang /* Remove rt6_ex from hash table and free the memory 110935732d01SWei Wang * Caller must hold rt6_exception_lock 111035732d01SWei Wang */ 111135732d01SWei Wang static void rt6_remove_exception(struct rt6_exception_bucket *bucket, 111235732d01SWei Wang struct rt6_exception *rt6_ex) 111335732d01SWei Wang { 111435732d01SWei Wang if (!bucket || !rt6_ex) 111535732d01SWei Wang return; 111635732d01SWei Wang rt6_ex->rt6i->rt6i_node = NULL; 111735732d01SWei Wang hlist_del_rcu(&rt6_ex->hlist); 111835732d01SWei Wang rt6_release(rt6_ex->rt6i); 111935732d01SWei Wang kfree_rcu(rt6_ex, rcu); 112035732d01SWei Wang WARN_ON_ONCE(!bucket->depth); 112135732d01SWei Wang bucket->depth--; 112235732d01SWei Wang } 112335732d01SWei Wang 112435732d01SWei Wang /* Remove oldest rt6_ex in bucket and free the memory 112535732d01SWei Wang * Caller must hold rt6_exception_lock 112635732d01SWei Wang */ 112735732d01SWei Wang static void rt6_exception_remove_oldest(struct rt6_exception_bucket *bucket) 112835732d01SWei Wang { 112935732d01SWei Wang struct rt6_exception *rt6_ex, *oldest = NULL; 113035732d01SWei Wang 113135732d01SWei Wang if (!bucket) 113235732d01SWei Wang return; 113335732d01SWei Wang 113435732d01SWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 113535732d01SWei Wang if (!oldest || time_before(rt6_ex->stamp, oldest->stamp)) 113635732d01SWei Wang oldest = rt6_ex; 113735732d01SWei Wang } 113835732d01SWei Wang rt6_remove_exception(bucket, oldest); 113935732d01SWei Wang } 114035732d01SWei Wang 114135732d01SWei Wang static u32 rt6_exception_hash(const struct in6_addr *dst, 114235732d01SWei Wang const struct in6_addr *src) 114335732d01SWei Wang { 114435732d01SWei Wang static u32 seed __read_mostly; 114535732d01SWei Wang u32 val; 114635732d01SWei Wang 114735732d01SWei Wang net_get_random_once(&seed, sizeof(seed)); 114835732d01SWei Wang val = jhash(dst, sizeof(*dst), seed); 114935732d01SWei Wang 115035732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 115135732d01SWei Wang if (src) 115235732d01SWei Wang val = jhash(src, sizeof(*src), val); 115335732d01SWei Wang #endif 115435732d01SWei Wang return hash_32(val, FIB6_EXCEPTION_BUCKET_SIZE_SHIFT); 115535732d01SWei Wang } 115635732d01SWei Wang 115735732d01SWei Wang /* Helper function to find the cached rt in the hash table 115835732d01SWei Wang * and update bucket pointer to point to the bucket for this 115935732d01SWei Wang * (daddr, saddr) pair 116035732d01SWei Wang * Caller must hold rt6_exception_lock 116135732d01SWei Wang */ 116235732d01SWei Wang static struct rt6_exception * 116335732d01SWei Wang __rt6_find_exception_spinlock(struct rt6_exception_bucket **bucket, 116435732d01SWei Wang const struct in6_addr *daddr, 116535732d01SWei Wang const struct in6_addr *saddr) 116635732d01SWei Wang { 116735732d01SWei Wang struct rt6_exception *rt6_ex; 116835732d01SWei Wang u32 hval; 116935732d01SWei Wang 117035732d01SWei Wang if (!(*bucket) || !daddr) 117135732d01SWei Wang return NULL; 117235732d01SWei Wang 117335732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 117435732d01SWei Wang *bucket += hval; 117535732d01SWei Wang 117635732d01SWei Wang hlist_for_each_entry(rt6_ex, &(*bucket)->chain, hlist) { 117735732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 117835732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 117935732d01SWei Wang 118035732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 118135732d01SWei Wang if (matched && saddr) 118235732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 118335732d01SWei Wang #endif 118435732d01SWei Wang if (matched) 118535732d01SWei Wang return rt6_ex; 118635732d01SWei Wang } 118735732d01SWei Wang return NULL; 118835732d01SWei Wang } 118935732d01SWei Wang 119035732d01SWei Wang /* Helper function to find the cached rt in the hash table 119135732d01SWei Wang * and update bucket pointer to point to the bucket for this 119235732d01SWei Wang * (daddr, saddr) pair 119335732d01SWei Wang * Caller must hold rcu_read_lock() 119435732d01SWei Wang */ 119535732d01SWei Wang static struct rt6_exception * 119635732d01SWei Wang __rt6_find_exception_rcu(struct rt6_exception_bucket **bucket, 119735732d01SWei Wang const struct in6_addr *daddr, 119835732d01SWei Wang const struct in6_addr *saddr) 119935732d01SWei Wang { 120035732d01SWei Wang struct rt6_exception *rt6_ex; 120135732d01SWei Wang u32 hval; 120235732d01SWei Wang 120335732d01SWei Wang WARN_ON_ONCE(!rcu_read_lock_held()); 120435732d01SWei Wang 120535732d01SWei Wang if (!(*bucket) || !daddr) 120635732d01SWei Wang return NULL; 120735732d01SWei Wang 120835732d01SWei Wang hval = rt6_exception_hash(daddr, saddr); 120935732d01SWei Wang *bucket += hval; 121035732d01SWei Wang 121135732d01SWei Wang hlist_for_each_entry_rcu(rt6_ex, &(*bucket)->chain, hlist) { 121235732d01SWei Wang struct rt6_info *rt6 = rt6_ex->rt6i; 121335732d01SWei Wang bool matched = ipv6_addr_equal(daddr, &rt6->rt6i_dst.addr); 121435732d01SWei Wang 121535732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 121635732d01SWei Wang if (matched && saddr) 121735732d01SWei Wang matched = ipv6_addr_equal(saddr, &rt6->rt6i_src.addr); 121835732d01SWei Wang #endif 121935732d01SWei Wang if (matched) 122035732d01SWei Wang return rt6_ex; 122135732d01SWei Wang } 122235732d01SWei Wang return NULL; 122335732d01SWei Wang } 122435732d01SWei Wang 122535732d01SWei Wang static int rt6_insert_exception(struct rt6_info *nrt, 122635732d01SWei Wang struct rt6_info *ort) 122735732d01SWei Wang { 122835732d01SWei Wang struct rt6_exception_bucket *bucket; 122935732d01SWei Wang struct in6_addr *src_key = NULL; 123035732d01SWei Wang struct rt6_exception *rt6_ex; 123135732d01SWei Wang int err = 0; 123235732d01SWei Wang 123335732d01SWei Wang /* ort can't be a cache or pcpu route */ 123435732d01SWei Wang if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)) 123535732d01SWei Wang ort = (struct rt6_info *)ort->dst.from; 123635732d01SWei Wang WARN_ON_ONCE(ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)); 123735732d01SWei Wang 123835732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 123935732d01SWei Wang 124035732d01SWei Wang if (ort->exception_bucket_flushed) { 124135732d01SWei Wang err = -EINVAL; 124235732d01SWei Wang goto out; 124335732d01SWei Wang } 124435732d01SWei Wang 124535732d01SWei Wang bucket = rcu_dereference_protected(ort->rt6i_exception_bucket, 124635732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 124735732d01SWei Wang if (!bucket) { 124835732d01SWei Wang bucket = kcalloc(FIB6_EXCEPTION_BUCKET_SIZE, sizeof(*bucket), 124935732d01SWei Wang GFP_ATOMIC); 125035732d01SWei Wang if (!bucket) { 125135732d01SWei Wang err = -ENOMEM; 125235732d01SWei Wang goto out; 125335732d01SWei Wang } 125435732d01SWei Wang rcu_assign_pointer(ort->rt6i_exception_bucket, bucket); 125535732d01SWei Wang } 125635732d01SWei Wang 125735732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 125835732d01SWei Wang /* rt6i_src.plen != 0 indicates ort is in subtree 125935732d01SWei Wang * and exception table is indexed by a hash of 126035732d01SWei Wang * both rt6i_dst and rt6i_src. 126135732d01SWei Wang * Otherwise, the exception table is indexed by 126235732d01SWei Wang * a hash of only rt6i_dst. 126335732d01SWei Wang */ 126435732d01SWei Wang if (ort->rt6i_src.plen) 126535732d01SWei Wang src_key = &nrt->rt6i_src.addr; 126635732d01SWei Wang #endif 126760006a48SWei Wang 126860006a48SWei Wang /* Update rt6i_prefsrc as it could be changed 126960006a48SWei Wang * in rt6_remove_prefsrc() 127060006a48SWei Wang */ 127160006a48SWei Wang nrt->rt6i_prefsrc = ort->rt6i_prefsrc; 1272f5bbe7eeSWei Wang /* rt6_mtu_change() might lower mtu on ort. 1273f5bbe7eeSWei Wang * Only insert this exception route if its mtu 1274f5bbe7eeSWei Wang * is less than ort's mtu value. 1275f5bbe7eeSWei Wang */ 1276f5bbe7eeSWei Wang if (nrt->rt6i_pmtu >= dst_mtu(&ort->dst)) { 1277f5bbe7eeSWei Wang err = -EINVAL; 1278f5bbe7eeSWei Wang goto out; 1279f5bbe7eeSWei Wang } 128060006a48SWei Wang 128135732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, &nrt->rt6i_dst.addr, 128235732d01SWei Wang src_key); 128335732d01SWei Wang if (rt6_ex) 128435732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 128535732d01SWei Wang 128635732d01SWei Wang rt6_ex = kzalloc(sizeof(*rt6_ex), GFP_ATOMIC); 128735732d01SWei Wang if (!rt6_ex) { 128835732d01SWei Wang err = -ENOMEM; 128935732d01SWei Wang goto out; 129035732d01SWei Wang } 129135732d01SWei Wang rt6_ex->rt6i = nrt; 129235732d01SWei Wang rt6_ex->stamp = jiffies; 129335732d01SWei Wang atomic_inc(&nrt->rt6i_ref); 129435732d01SWei Wang nrt->rt6i_node = ort->rt6i_node; 129535732d01SWei Wang hlist_add_head_rcu(&rt6_ex->hlist, &bucket->chain); 129635732d01SWei Wang bucket->depth++; 129735732d01SWei Wang 129835732d01SWei Wang if (bucket->depth > FIB6_MAX_DEPTH) 129935732d01SWei Wang rt6_exception_remove_oldest(bucket); 130035732d01SWei Wang 130135732d01SWei Wang out: 130235732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 130335732d01SWei Wang 130435732d01SWei Wang /* Update fn->fn_sernum to invalidate all cached dst */ 130535732d01SWei Wang if (!err) 130635732d01SWei Wang fib6_update_sernum(ort); 130735732d01SWei Wang 130835732d01SWei Wang return err; 130935732d01SWei Wang } 131035732d01SWei Wang 131135732d01SWei Wang void rt6_flush_exceptions(struct rt6_info *rt) 131235732d01SWei Wang { 131335732d01SWei Wang struct rt6_exception_bucket *bucket; 131435732d01SWei Wang struct rt6_exception *rt6_ex; 131535732d01SWei Wang struct hlist_node *tmp; 131635732d01SWei Wang int i; 131735732d01SWei Wang 131835732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 131935732d01SWei Wang /* Prevent rt6_insert_exception() to recreate the bucket list */ 132035732d01SWei Wang rt->exception_bucket_flushed = 1; 132135732d01SWei Wang 132235732d01SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 132335732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 132435732d01SWei Wang if (!bucket) 132535732d01SWei Wang goto out; 132635732d01SWei Wang 132735732d01SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 132835732d01SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, &bucket->chain, hlist) 132935732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 133035732d01SWei Wang WARN_ON_ONCE(bucket->depth); 133135732d01SWei Wang bucket++; 133235732d01SWei Wang } 133335732d01SWei Wang 133435732d01SWei Wang out: 133535732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 133635732d01SWei Wang } 133735732d01SWei Wang 133835732d01SWei Wang /* Find cached rt in the hash table inside passed in rt 133935732d01SWei Wang * Caller has to hold rcu_read_lock() 134035732d01SWei Wang */ 134135732d01SWei Wang static struct rt6_info *rt6_find_cached_rt(struct rt6_info *rt, 134235732d01SWei Wang struct in6_addr *daddr, 134335732d01SWei Wang struct in6_addr *saddr) 134435732d01SWei Wang { 134535732d01SWei Wang struct rt6_exception_bucket *bucket; 134635732d01SWei Wang struct in6_addr *src_key = NULL; 134735732d01SWei Wang struct rt6_exception *rt6_ex; 134835732d01SWei Wang struct rt6_info *res = NULL; 134935732d01SWei Wang 135035732d01SWei Wang bucket = rcu_dereference(rt->rt6i_exception_bucket); 135135732d01SWei Wang 135235732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 135335732d01SWei Wang /* rt6i_src.plen != 0 indicates rt is in subtree 135435732d01SWei Wang * and exception table is indexed by a hash of 135535732d01SWei Wang * both rt6i_dst and rt6i_src. 135635732d01SWei Wang * Otherwise, the exception table is indexed by 135735732d01SWei Wang * a hash of only rt6i_dst. 135835732d01SWei Wang */ 135935732d01SWei Wang if (rt->rt6i_src.plen) 136035732d01SWei Wang src_key = saddr; 136135732d01SWei Wang #endif 136235732d01SWei Wang rt6_ex = __rt6_find_exception_rcu(&bucket, daddr, src_key); 136335732d01SWei Wang 136435732d01SWei Wang if (rt6_ex && !rt6_check_expired(rt6_ex->rt6i)) 136535732d01SWei Wang res = rt6_ex->rt6i; 136635732d01SWei Wang 136735732d01SWei Wang return res; 136835732d01SWei Wang } 136935732d01SWei Wang 137035732d01SWei Wang /* Remove the passed in cached rt from the hash table that contains it */ 137135732d01SWei Wang int rt6_remove_exception_rt(struct rt6_info *rt) 137235732d01SWei Wang { 137335732d01SWei Wang struct rt6_info *from = (struct rt6_info *)rt->dst.from; 137435732d01SWei Wang struct rt6_exception_bucket *bucket; 137535732d01SWei Wang struct in6_addr *src_key = NULL; 137635732d01SWei Wang struct rt6_exception *rt6_ex; 137735732d01SWei Wang int err; 137835732d01SWei Wang 137935732d01SWei Wang if (!from || 138035732d01SWei Wang !(rt->rt6i_flags | RTF_CACHE)) 138135732d01SWei Wang return -EINVAL; 138235732d01SWei Wang 138335732d01SWei Wang if (!rcu_access_pointer(from->rt6i_exception_bucket)) 138435732d01SWei Wang return -ENOENT; 138535732d01SWei Wang 138635732d01SWei Wang spin_lock_bh(&rt6_exception_lock); 138735732d01SWei Wang bucket = rcu_dereference_protected(from->rt6i_exception_bucket, 138835732d01SWei Wang lockdep_is_held(&rt6_exception_lock)); 138935732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 139035732d01SWei Wang /* rt6i_src.plen != 0 indicates 'from' is in subtree 139135732d01SWei Wang * and exception table is indexed by a hash of 139235732d01SWei Wang * both rt6i_dst and rt6i_src. 139335732d01SWei Wang * Otherwise, the exception table is indexed by 139435732d01SWei Wang * a hash of only rt6i_dst. 139535732d01SWei Wang */ 139635732d01SWei Wang if (from->rt6i_src.plen) 139735732d01SWei Wang src_key = &rt->rt6i_src.addr; 139835732d01SWei Wang #endif 139935732d01SWei Wang rt6_ex = __rt6_find_exception_spinlock(&bucket, 140035732d01SWei Wang &rt->rt6i_dst.addr, 140135732d01SWei Wang src_key); 140235732d01SWei Wang if (rt6_ex) { 140335732d01SWei Wang rt6_remove_exception(bucket, rt6_ex); 140435732d01SWei Wang err = 0; 140535732d01SWei Wang } else { 140635732d01SWei Wang err = -ENOENT; 140735732d01SWei Wang } 140835732d01SWei Wang 140935732d01SWei Wang spin_unlock_bh(&rt6_exception_lock); 141035732d01SWei Wang return err; 141135732d01SWei Wang } 141235732d01SWei Wang 141335732d01SWei Wang /* Find rt6_ex which contains the passed in rt cache and 141435732d01SWei Wang * refresh its stamp 141535732d01SWei Wang */ 141635732d01SWei Wang static void rt6_update_exception_stamp_rt(struct rt6_info *rt) 141735732d01SWei Wang { 141835732d01SWei Wang struct rt6_info *from = (struct rt6_info *)rt->dst.from; 141935732d01SWei Wang struct rt6_exception_bucket *bucket; 142035732d01SWei Wang struct in6_addr *src_key = NULL; 142135732d01SWei Wang struct rt6_exception *rt6_ex; 142235732d01SWei Wang 142335732d01SWei Wang if (!from || 142435732d01SWei Wang !(rt->rt6i_flags | RTF_CACHE)) 142535732d01SWei Wang return; 142635732d01SWei Wang 142735732d01SWei Wang rcu_read_lock(); 142835732d01SWei Wang bucket = rcu_dereference(from->rt6i_exception_bucket); 142935732d01SWei Wang 143035732d01SWei Wang #ifdef CONFIG_IPV6_SUBTREES 143135732d01SWei Wang /* rt6i_src.plen != 0 indicates 'from' is in subtree 143235732d01SWei Wang * and exception table is indexed by a hash of 143335732d01SWei Wang * both rt6i_dst and rt6i_src. 143435732d01SWei Wang * Otherwise, the exception table is indexed by 143535732d01SWei Wang * a hash of only rt6i_dst. 143635732d01SWei Wang */ 143735732d01SWei Wang if (from->rt6i_src.plen) 143835732d01SWei Wang src_key = &rt->rt6i_src.addr; 143935732d01SWei Wang #endif 144035732d01SWei Wang rt6_ex = __rt6_find_exception_rcu(&bucket, 144135732d01SWei Wang &rt->rt6i_dst.addr, 144235732d01SWei Wang src_key); 144335732d01SWei Wang if (rt6_ex) 144435732d01SWei Wang rt6_ex->stamp = jiffies; 144535732d01SWei Wang 144635732d01SWei Wang rcu_read_unlock(); 144735732d01SWei Wang } 144835732d01SWei Wang 144960006a48SWei Wang static void rt6_exceptions_remove_prefsrc(struct rt6_info *rt) 145060006a48SWei Wang { 145160006a48SWei Wang struct rt6_exception_bucket *bucket; 145260006a48SWei Wang struct rt6_exception *rt6_ex; 145360006a48SWei Wang int i; 145460006a48SWei Wang 145560006a48SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 145660006a48SWei Wang lockdep_is_held(&rt6_exception_lock)); 145760006a48SWei Wang 145860006a48SWei Wang if (bucket) { 145960006a48SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 146060006a48SWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 146160006a48SWei Wang rt6_ex->rt6i->rt6i_prefsrc.plen = 0; 146260006a48SWei Wang } 146360006a48SWei Wang bucket++; 146460006a48SWei Wang } 146560006a48SWei Wang } 146660006a48SWei Wang } 146760006a48SWei Wang 1468f5bbe7eeSWei Wang static void rt6_exceptions_update_pmtu(struct rt6_info *rt, int mtu) 1469f5bbe7eeSWei Wang { 1470f5bbe7eeSWei Wang struct rt6_exception_bucket *bucket; 1471f5bbe7eeSWei Wang struct rt6_exception *rt6_ex; 1472f5bbe7eeSWei Wang int i; 1473f5bbe7eeSWei Wang 1474f5bbe7eeSWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1475f5bbe7eeSWei Wang lockdep_is_held(&rt6_exception_lock)); 1476f5bbe7eeSWei Wang 1477f5bbe7eeSWei Wang if (bucket) { 1478f5bbe7eeSWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1479f5bbe7eeSWei Wang hlist_for_each_entry(rt6_ex, &bucket->chain, hlist) { 1480f5bbe7eeSWei Wang struct rt6_info *entry = rt6_ex->rt6i; 1481f5bbe7eeSWei Wang /* For RTF_CACHE with rt6i_pmtu == 0 1482f5bbe7eeSWei Wang * (i.e. a redirected route), 1483f5bbe7eeSWei Wang * the metrics of its rt->dst.from has already 1484f5bbe7eeSWei Wang * been updated. 1485f5bbe7eeSWei Wang */ 1486f5bbe7eeSWei Wang if (entry->rt6i_pmtu && entry->rt6i_pmtu > mtu) 1487f5bbe7eeSWei Wang entry->rt6i_pmtu = mtu; 1488f5bbe7eeSWei Wang } 1489f5bbe7eeSWei Wang bucket++; 1490f5bbe7eeSWei Wang } 1491f5bbe7eeSWei Wang } 1492f5bbe7eeSWei Wang } 1493f5bbe7eeSWei Wang 1494b16cb459SWei Wang #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) 1495b16cb459SWei Wang 1496b16cb459SWei Wang static void rt6_exceptions_clean_tohost(struct rt6_info *rt, 1497b16cb459SWei Wang struct in6_addr *gateway) 1498b16cb459SWei Wang { 1499b16cb459SWei Wang struct rt6_exception_bucket *bucket; 1500b16cb459SWei Wang struct rt6_exception *rt6_ex; 1501b16cb459SWei Wang struct hlist_node *tmp; 1502b16cb459SWei Wang int i; 1503b16cb459SWei Wang 1504b16cb459SWei Wang if (!rcu_access_pointer(rt->rt6i_exception_bucket)) 1505b16cb459SWei Wang return; 1506b16cb459SWei Wang 1507b16cb459SWei Wang spin_lock_bh(&rt6_exception_lock); 1508b16cb459SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1509b16cb459SWei Wang lockdep_is_held(&rt6_exception_lock)); 1510b16cb459SWei Wang 1511b16cb459SWei Wang if (bucket) { 1512b16cb459SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1513b16cb459SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, 1514b16cb459SWei Wang &bucket->chain, hlist) { 1515b16cb459SWei Wang struct rt6_info *entry = rt6_ex->rt6i; 1516b16cb459SWei Wang 1517b16cb459SWei Wang if ((entry->rt6i_flags & RTF_CACHE_GATEWAY) == 1518b16cb459SWei Wang RTF_CACHE_GATEWAY && 1519b16cb459SWei Wang ipv6_addr_equal(gateway, 1520b16cb459SWei Wang &entry->rt6i_gateway)) { 1521b16cb459SWei Wang rt6_remove_exception(bucket, rt6_ex); 1522b16cb459SWei Wang } 1523b16cb459SWei Wang } 1524b16cb459SWei Wang bucket++; 1525b16cb459SWei Wang } 1526b16cb459SWei Wang } 1527b16cb459SWei Wang 1528b16cb459SWei Wang spin_unlock_bh(&rt6_exception_lock); 1529b16cb459SWei Wang } 1530b16cb459SWei Wang 1531*c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket, 1532*c757faa8SWei Wang struct rt6_exception *rt6_ex, 1533*c757faa8SWei Wang struct fib6_gc_args *gc_args, 1534*c757faa8SWei Wang unsigned long now) 1535*c757faa8SWei Wang { 1536*c757faa8SWei Wang struct rt6_info *rt = rt6_ex->rt6i; 1537*c757faa8SWei Wang 1538*c757faa8SWei Wang if (atomic_read(&rt->dst.__refcnt) == 1 && 1539*c757faa8SWei Wang time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) { 1540*c757faa8SWei Wang RT6_TRACE("aging clone %p\n", rt); 1541*c757faa8SWei Wang rt6_remove_exception(bucket, rt6_ex); 1542*c757faa8SWei Wang return; 1543*c757faa8SWei Wang } else if (rt->rt6i_flags & RTF_GATEWAY) { 1544*c757faa8SWei Wang struct neighbour *neigh; 1545*c757faa8SWei Wang __u8 neigh_flags = 0; 1546*c757faa8SWei Wang 1547*c757faa8SWei Wang neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway); 1548*c757faa8SWei Wang if (neigh) { 1549*c757faa8SWei Wang neigh_flags = neigh->flags; 1550*c757faa8SWei Wang neigh_release(neigh); 1551*c757faa8SWei Wang } 1552*c757faa8SWei Wang if (!(neigh_flags & NTF_ROUTER)) { 1553*c757faa8SWei Wang RT6_TRACE("purging route %p via non-router but gateway\n", 1554*c757faa8SWei Wang rt); 1555*c757faa8SWei Wang rt6_remove_exception(bucket, rt6_ex); 1556*c757faa8SWei Wang return; 1557*c757faa8SWei Wang } 1558*c757faa8SWei Wang } 1559*c757faa8SWei Wang gc_args->more++; 1560*c757faa8SWei Wang } 1561*c757faa8SWei Wang 1562*c757faa8SWei Wang void rt6_age_exceptions(struct rt6_info *rt, 1563*c757faa8SWei Wang struct fib6_gc_args *gc_args, 1564*c757faa8SWei Wang unsigned long now) 1565*c757faa8SWei Wang { 1566*c757faa8SWei Wang struct rt6_exception_bucket *bucket; 1567*c757faa8SWei Wang struct rt6_exception *rt6_ex; 1568*c757faa8SWei Wang struct hlist_node *tmp; 1569*c757faa8SWei Wang int i; 1570*c757faa8SWei Wang 1571*c757faa8SWei Wang if (!rcu_access_pointer(rt->rt6i_exception_bucket)) 1572*c757faa8SWei Wang return; 1573*c757faa8SWei Wang 1574*c757faa8SWei Wang spin_lock_bh(&rt6_exception_lock); 1575*c757faa8SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1576*c757faa8SWei Wang lockdep_is_held(&rt6_exception_lock)); 1577*c757faa8SWei Wang 1578*c757faa8SWei Wang if (bucket) { 1579*c757faa8SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1580*c757faa8SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, 1581*c757faa8SWei Wang &bucket->chain, hlist) { 1582*c757faa8SWei Wang rt6_age_examine_exception(bucket, rt6_ex, 1583*c757faa8SWei Wang gc_args, now); 1584*c757faa8SWei Wang } 1585*c757faa8SWei Wang bucket++; 1586*c757faa8SWei Wang } 1587*c757faa8SWei Wang } 1588*c757faa8SWei Wang spin_unlock_bh(&rt6_exception_lock); 1589*c757faa8SWei Wang } 1590*c757faa8SWei Wang 15919ff74384SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, 15929ff74384SDavid Ahern int oif, struct flowi6 *fl6, int flags) 15931da177e4SLinus Torvalds { 1594367efcb9SMartin KaFai Lau struct fib6_node *fn, *saved_fn; 159545e4fd26SMartin KaFai Lau struct rt6_info *rt; 1596c71099acSThomas Graf int strict = 0; 15971da177e4SLinus Torvalds 159877d16f45SYOSHIFUJI Hideaki strict |= flags & RT6_LOOKUP_F_IFACE; 1599d5d32e4bSDavid Ahern strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE; 1600367efcb9SMartin KaFai Lau if (net->ipv6.devconf_all->forwarding == 0) 1601367efcb9SMartin KaFai Lau strict |= RT6_LOOKUP_F_REACHABLE; 16021da177e4SLinus Torvalds 1603c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 16041da177e4SLinus Torvalds 16054c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1606367efcb9SMartin KaFai Lau saved_fn = fn; 16071da177e4SLinus Torvalds 1608ca254490SDavid Ahern if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) 1609ca254490SDavid Ahern oif = 0; 1610ca254490SDavid Ahern 1611a3c00e46SMartin KaFai Lau redo_rt6_select: 1612367efcb9SMartin KaFai Lau rt = rt6_select(fn, oif, strict); 161352bd4c0cSNicolas Dichtel if (rt->rt6i_nsiblings) 1614367efcb9SMartin KaFai Lau rt = rt6_multipath_select(rt, fl6, oif, strict); 1615a3c00e46SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 1616a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1617a3c00e46SMartin KaFai Lau if (fn) 1618a3c00e46SMartin KaFai Lau goto redo_rt6_select; 1619367efcb9SMartin KaFai Lau else if (strict & RT6_LOOKUP_F_REACHABLE) { 1620367efcb9SMartin KaFai Lau /* also consider unreachable route */ 1621367efcb9SMartin KaFai Lau strict &= ~RT6_LOOKUP_F_REACHABLE; 1622367efcb9SMartin KaFai Lau fn = saved_fn; 1623367efcb9SMartin KaFai Lau goto redo_rt6_select; 1624367efcb9SMartin KaFai Lau } 1625a3c00e46SMartin KaFai Lau } 1626a3c00e46SMartin KaFai Lau 1627d52d3997SMartin KaFai Lau 1628d52d3997SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry || (rt->rt6i_flags & RTF_CACHE)) { 16293da59bd9SMartin KaFai Lau dst_use(&rt->dst, jiffies); 1630c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 16311da177e4SLinus Torvalds 1632d52d3997SMartin KaFai Lau rt6_dst_from_metrics_check(rt); 1633b811580dSDavid Ahern 1634b811580dSDavid Ahern trace_fib6_table_lookup(net, rt, table->tb6_id, fl6); 1635d52d3997SMartin KaFai Lau return rt; 16363da59bd9SMartin KaFai Lau } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) && 16373da59bd9SMartin KaFai Lau !(rt->rt6i_flags & RTF_GATEWAY))) { 16383da59bd9SMartin KaFai Lau /* Create a RTF_CACHE clone which will not be 16393da59bd9SMartin KaFai Lau * owned by the fib6 tree. It is for the special case where 16403da59bd9SMartin KaFai Lau * the daddr in the skb during the neighbor look-up is different 16413da59bd9SMartin KaFai Lau * from the fl6->daddr used to look-up route here. 16423da59bd9SMartin KaFai Lau */ 1643c71099acSThomas Graf 16443da59bd9SMartin KaFai Lau struct rt6_info *uncached_rt; 16453da59bd9SMartin KaFai Lau 1646d52d3997SMartin KaFai Lau dst_use(&rt->dst, jiffies); 1647d52d3997SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 1648d52d3997SMartin KaFai Lau 16493da59bd9SMartin KaFai Lau uncached_rt = ip6_rt_cache_alloc(rt, &fl6->daddr, NULL); 16503da59bd9SMartin KaFai Lau dst_release(&rt->dst); 16513da59bd9SMartin KaFai Lau 16521cfb71eeSWei Wang if (uncached_rt) { 16531cfb71eeSWei Wang /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc() 16541cfb71eeSWei Wang * No need for another dst_hold() 16551cfb71eeSWei Wang */ 16568d0b94afSMartin KaFai Lau rt6_uncached_list_add(uncached_rt); 16571cfb71eeSWei Wang } else { 16583da59bd9SMartin KaFai Lau uncached_rt = net->ipv6.ip6_null_entry; 16593da59bd9SMartin KaFai Lau dst_hold(&uncached_rt->dst); 16601cfb71eeSWei Wang } 1661b811580dSDavid Ahern 1662b811580dSDavid Ahern trace_fib6_table_lookup(net, uncached_rt, table->tb6_id, fl6); 16633da59bd9SMartin KaFai Lau return uncached_rt; 16643da59bd9SMartin KaFai Lau 1665d52d3997SMartin KaFai Lau } else { 1666d52d3997SMartin KaFai Lau /* Get a percpu copy */ 1667d52d3997SMartin KaFai Lau 1668d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1669d52d3997SMartin KaFai Lau 1670d52d3997SMartin KaFai Lau rt->dst.lastuse = jiffies; 1671d52d3997SMartin KaFai Lau rt->dst.__use++; 1672d52d3997SMartin KaFai Lau pcpu_rt = rt6_get_pcpu_route(rt); 1673d52d3997SMartin KaFai Lau 16749c7370a1SMartin KaFai Lau if (pcpu_rt) { 1675d52d3997SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 16769c7370a1SMartin KaFai Lau } else { 16779c7370a1SMartin KaFai Lau /* We have to do the read_unlock first 16789c7370a1SMartin KaFai Lau * because rt6_make_pcpu_route() may trigger 16799c7370a1SMartin KaFai Lau * ip6_dst_gc() which will take the write_lock. 16809c7370a1SMartin KaFai Lau */ 16819c7370a1SMartin KaFai Lau dst_hold(&rt->dst); 16829c7370a1SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 16839c7370a1SMartin KaFai Lau pcpu_rt = rt6_make_pcpu_route(rt); 16849c7370a1SMartin KaFai Lau dst_release(&rt->dst); 16859c7370a1SMartin KaFai Lau } 1686d52d3997SMartin KaFai Lau 1687b811580dSDavid Ahern trace_fib6_table_lookup(net, pcpu_rt, table->tb6_id, fl6); 1688d52d3997SMartin KaFai Lau return pcpu_rt; 16899c7370a1SMartin KaFai Lau 1690d52d3997SMartin KaFai Lau } 1691c71099acSThomas Graf } 16929ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route); 1693c71099acSThomas Graf 16948ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table, 16954c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 16964acad72dSPavel Emelyanov { 16974c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags); 16984acad72dSPavel Emelyanov } 16994acad72dSPavel Emelyanov 1700d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net, 170172331bc0SShmulik Ladkani struct net_device *dev, 170272331bc0SShmulik Ladkani struct flowi6 *fl6, int flags) 170372331bc0SShmulik Ladkani { 170472331bc0SShmulik Ladkani if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) 170572331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_IFACE; 170672331bc0SShmulik Ladkani 170772331bc0SShmulik Ladkani return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input); 170872331bc0SShmulik Ladkani } 1709d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup); 171072331bc0SShmulik Ladkani 171123aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb, 171223aebdacSJakub Sitnicki struct flow_keys *keys) 171323aebdacSJakub Sitnicki { 171423aebdacSJakub Sitnicki const struct ipv6hdr *outer_iph = ipv6_hdr(skb); 171523aebdacSJakub Sitnicki const struct ipv6hdr *key_iph = outer_iph; 171623aebdacSJakub Sitnicki const struct ipv6hdr *inner_iph; 171723aebdacSJakub Sitnicki const struct icmp6hdr *icmph; 171823aebdacSJakub Sitnicki struct ipv6hdr _inner_iph; 171923aebdacSJakub Sitnicki 172023aebdacSJakub Sitnicki if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6)) 172123aebdacSJakub Sitnicki goto out; 172223aebdacSJakub Sitnicki 172323aebdacSJakub Sitnicki icmph = icmp6_hdr(skb); 172423aebdacSJakub Sitnicki if (icmph->icmp6_type != ICMPV6_DEST_UNREACH && 172523aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_PKT_TOOBIG && 172623aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_TIME_EXCEED && 172723aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_PARAMPROB) 172823aebdacSJakub Sitnicki goto out; 172923aebdacSJakub Sitnicki 173023aebdacSJakub Sitnicki inner_iph = skb_header_pointer(skb, 173123aebdacSJakub Sitnicki skb_transport_offset(skb) + sizeof(*icmph), 173223aebdacSJakub Sitnicki sizeof(_inner_iph), &_inner_iph); 173323aebdacSJakub Sitnicki if (!inner_iph) 173423aebdacSJakub Sitnicki goto out; 173523aebdacSJakub Sitnicki 173623aebdacSJakub Sitnicki key_iph = inner_iph; 173723aebdacSJakub Sitnicki out: 173823aebdacSJakub Sitnicki memset(keys, 0, sizeof(*keys)); 173923aebdacSJakub Sitnicki keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 174023aebdacSJakub Sitnicki keys->addrs.v6addrs.src = key_iph->saddr; 174123aebdacSJakub Sitnicki keys->addrs.v6addrs.dst = key_iph->daddr; 174223aebdacSJakub Sitnicki keys->tags.flow_label = ip6_flowinfo(key_iph); 174323aebdacSJakub Sitnicki keys->basic.ip_proto = key_iph->nexthdr; 174423aebdacSJakub Sitnicki } 174523aebdacSJakub Sitnicki 174623aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */ 174723aebdacSJakub Sitnicki u32 rt6_multipath_hash(const struct flowi6 *fl6, const struct sk_buff *skb) 174823aebdacSJakub Sitnicki { 174923aebdacSJakub Sitnicki struct flow_keys hash_keys; 175023aebdacSJakub Sitnicki 175123aebdacSJakub Sitnicki if (skb) { 175223aebdacSJakub Sitnicki ip6_multipath_l3_keys(skb, &hash_keys); 175323aebdacSJakub Sitnicki return flow_hash_from_keys(&hash_keys); 175423aebdacSJakub Sitnicki } 175523aebdacSJakub Sitnicki 175623aebdacSJakub Sitnicki return get_hash_from_flowi6(fl6); 175723aebdacSJakub Sitnicki } 175823aebdacSJakub Sitnicki 1759c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 1760c71099acSThomas Graf { 1761b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 1762c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 1763adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 1764904af04dSJiri Benc struct ip_tunnel_info *tun_info; 17654c9483b2SDavid S. Miller struct flowi6 fl6 = { 1766e0d56fddSDavid Ahern .flowi6_iif = skb->dev->ifindex, 17674c9483b2SDavid S. Miller .daddr = iph->daddr, 17684c9483b2SDavid S. Miller .saddr = iph->saddr, 17696502ca52SYOSHIFUJI Hideaki / 吉藤英明 .flowlabel = ip6_flowinfo(iph), 17704c9483b2SDavid S. Miller .flowi6_mark = skb->mark, 17714c9483b2SDavid S. Miller .flowi6_proto = iph->nexthdr, 1772c71099acSThomas Graf }; 1773adaa70bbSThomas Graf 1774904af04dSJiri Benc tun_info = skb_tunnel_info(skb); 177546fa062aSJiri Benc if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) 1776904af04dSJiri Benc fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id; 177723aebdacSJakub Sitnicki if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6)) 177823aebdacSJakub Sitnicki fl6.mp_hash = rt6_multipath_hash(&fl6, skb); 177906e9d040SJiri Benc skb_dst_drop(skb); 178072331bc0SShmulik Ladkani skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags)); 1781c71099acSThomas Graf } 1782c71099acSThomas Graf 17838ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table, 17844c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 1785c71099acSThomas Graf { 17864c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags); 1787c71099acSThomas Graf } 1788c71099acSThomas Graf 17896f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk, 17906f21c96aSPaolo Abeni struct flowi6 *fl6, int flags) 1791c71099acSThomas Graf { 1792d46a9d67SDavid Ahern bool any_src; 1793c71099acSThomas Graf 17944c1feac5SDavid Ahern if (rt6_need_strict(&fl6->daddr)) { 17954c1feac5SDavid Ahern struct dst_entry *dst; 17964c1feac5SDavid Ahern 17974c1feac5SDavid Ahern dst = l3mdev_link_scope_lookup(net, fl6); 1798ca254490SDavid Ahern if (dst) 1799ca254490SDavid Ahern return dst; 18004c1feac5SDavid Ahern } 1801ca254490SDavid Ahern 18021fb9489bSPavel Emelyanov fl6->flowi6_iif = LOOPBACK_IFINDEX; 18034dc27d1cSDavid McCullough 1804d46a9d67SDavid Ahern any_src = ipv6_addr_any(&fl6->saddr); 1805741a11d9SDavid Ahern if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) || 1806d46a9d67SDavid Ahern (fl6->flowi6_oif && any_src)) 180777d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 1808c71099acSThomas Graf 1809d46a9d67SDavid Ahern if (!any_src) 1810adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 18110c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 18120c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 1813adaa70bbSThomas Graf 18144c9483b2SDavid S. Miller return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output); 18151da177e4SLinus Torvalds } 18166f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags); 18171da177e4SLinus Torvalds 18182774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 181914e50e57SDavid S. Miller { 18205c1e6aa3SDavid S. Miller struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; 18211dbe3252SWei Wang struct net_device *loopback_dev = net->loopback_dev; 182214e50e57SDavid S. Miller struct dst_entry *new = NULL; 182314e50e57SDavid S. Miller 18241dbe3252SWei Wang rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1, 1825b2a9c0edSWei Wang DST_OBSOLETE_NONE, 0); 182614e50e57SDavid S. Miller if (rt) { 18270a1f5962SMartin KaFai Lau rt6_info_init(rt); 18280a1f5962SMartin KaFai Lau 1829d8d1f30bSChangli Gao new = &rt->dst; 183014e50e57SDavid S. Miller new->__use = 1; 1831352e512cSHerbert Xu new->input = dst_discard; 1832ede2059dSEric W. Biederman new->output = dst_discard_out; 183314e50e57SDavid S. Miller 1834defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 183514e50e57SDavid S. Miller 18361dbe3252SWei Wang rt->rt6i_idev = in6_dev_get(loopback_dev); 18374e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 18380a1f5962SMartin KaFai Lau rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU; 183914e50e57SDavid S. Miller rt->rt6i_metric = 0; 184014e50e57SDavid S. Miller 184114e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 184214e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 184314e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 184414e50e57SDavid S. Miller #endif 184514e50e57SDavid S. Miller } 184614e50e57SDavid S. Miller 184769ead7afSDavid S. Miller dst_release(dst_orig); 184869ead7afSDavid S. Miller return new ? new : ERR_PTR(-ENOMEM); 184914e50e57SDavid S. Miller } 185014e50e57SDavid S. Miller 18511da177e4SLinus Torvalds /* 18521da177e4SLinus Torvalds * Destination cache support functions 18531da177e4SLinus Torvalds */ 18541da177e4SLinus Torvalds 18554b32b5adSMartin KaFai Lau static void rt6_dst_from_metrics_check(struct rt6_info *rt) 18564b32b5adSMartin KaFai Lau { 18574b32b5adSMartin KaFai Lau if (rt->dst.from && 18584b32b5adSMartin KaFai Lau dst_metrics_ptr(&rt->dst) != dst_metrics_ptr(rt->dst.from)) 18594b32b5adSMartin KaFai Lau dst_init_metrics(&rt->dst, dst_metrics_ptr(rt->dst.from), true); 18604b32b5adSMartin KaFai Lau } 18614b32b5adSMartin KaFai Lau 18623da59bd9SMartin KaFai Lau static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie) 18633da59bd9SMartin KaFai Lau { 186436143645SSteffen Klassert u32 rt_cookie = 0; 1865c5cff856SWei Wang 1866c5cff856SWei Wang if (!rt6_get_cookie_safe(rt, &rt_cookie) || rt_cookie != cookie) 18673da59bd9SMartin KaFai Lau return NULL; 18683da59bd9SMartin KaFai Lau 18693da59bd9SMartin KaFai Lau if (rt6_check_expired(rt)) 18703da59bd9SMartin KaFai Lau return NULL; 18713da59bd9SMartin KaFai Lau 18723da59bd9SMartin KaFai Lau return &rt->dst; 18733da59bd9SMartin KaFai Lau } 18743da59bd9SMartin KaFai Lau 18753da59bd9SMartin KaFai Lau static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, u32 cookie) 18763da59bd9SMartin KaFai Lau { 18775973fb1eSMartin KaFai Lau if (!__rt6_check_expired(rt) && 18785973fb1eSMartin KaFai Lau rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && 18793da59bd9SMartin KaFai Lau rt6_check((struct rt6_info *)(rt->dst.from), cookie)) 18803da59bd9SMartin KaFai Lau return &rt->dst; 18813da59bd9SMartin KaFai Lau else 18823da59bd9SMartin KaFai Lau return NULL; 18833da59bd9SMartin KaFai Lau } 18843da59bd9SMartin KaFai Lau 18851da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 18861da177e4SLinus Torvalds { 18871da177e4SLinus Torvalds struct rt6_info *rt; 18881da177e4SLinus Torvalds 18891da177e4SLinus Torvalds rt = (struct rt6_info *) dst; 18901da177e4SLinus Torvalds 18916f3118b5SNicolas Dichtel /* All IPV6 dsts are created with ->obsolete set to the value 18926f3118b5SNicolas Dichtel * DST_OBSOLETE_FORCE_CHK which forces validation calls down 18936f3118b5SNicolas Dichtel * into this function always. 18946f3118b5SNicolas Dichtel */ 1895e3bc10bdSHannes Frederic Sowa 18964b32b5adSMartin KaFai Lau rt6_dst_from_metrics_check(rt); 18974b32b5adSMartin KaFai Lau 189802bcf4e0SMartin KaFai Lau if (rt->rt6i_flags & RTF_PCPU || 1899a4c2fd7fSWei Wang (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->dst.from)) 19003da59bd9SMartin KaFai Lau return rt6_dst_from_check(rt, cookie); 19013da59bd9SMartin KaFai Lau else 19023da59bd9SMartin KaFai Lau return rt6_check(rt, cookie); 19031da177e4SLinus Torvalds } 19041da177e4SLinus Torvalds 19051da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 19061da177e4SLinus Torvalds { 19071da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 19081da177e4SLinus Torvalds 19091da177e4SLinus Torvalds if (rt) { 191054c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 191154c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 1912e0a1ad73SThomas Graf ip6_del_rt(rt); 191354c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 19141da177e4SLinus Torvalds } 191554c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 191654c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 191754c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 191854c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 191954c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 192054c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 19211da177e4SLinus Torvalds } 19221da177e4SLinus Torvalds 19231da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 19241da177e4SLinus Torvalds { 19251da177e4SLinus Torvalds struct rt6_info *rt; 19261da177e4SLinus Torvalds 19273ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 19281da177e4SLinus Torvalds 1929adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 19301da177e4SLinus Torvalds if (rt) { 19311eb4f758SHannes Frederic Sowa if (rt->rt6i_flags & RTF_CACHE) { 1932ad65a2f0SWei Wang if (dst_hold_safe(&rt->dst)) 19338e3d5be7SMartin KaFai Lau ip6_del_rt(rt); 1934c5cff856SWei Wang } else { 1935c5cff856SWei Wang struct fib6_node *fn; 1936c5cff856SWei Wang 1937c5cff856SWei Wang rcu_read_lock(); 1938c5cff856SWei Wang fn = rcu_dereference(rt->rt6i_node); 1939c5cff856SWei Wang if (fn && (rt->rt6i_flags & RTF_DEFAULT)) 1940c5cff856SWei Wang fn->fn_sernum = -1; 1941c5cff856SWei Wang rcu_read_unlock(); 19421da177e4SLinus Torvalds } 19431da177e4SLinus Torvalds } 19441eb4f758SHannes Frederic Sowa } 19451da177e4SLinus Torvalds 194645e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu) 194745e4fd26SMartin KaFai Lau { 194845e4fd26SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 194945e4fd26SMartin KaFai Lau 195045e4fd26SMartin KaFai Lau rt->rt6i_flags |= RTF_MODIFIED; 195145e4fd26SMartin KaFai Lau rt->rt6i_pmtu = mtu; 195245e4fd26SMartin KaFai Lau rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires); 195345e4fd26SMartin KaFai Lau } 195445e4fd26SMartin KaFai Lau 19550d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt) 19560d3f6d29SMartin KaFai Lau { 19570d3f6d29SMartin KaFai Lau return !(rt->rt6i_flags & RTF_CACHE) && 19584e587ea7SWei Wang (rt->rt6i_flags & RTF_PCPU || 19594e587ea7SWei Wang rcu_access_pointer(rt->rt6i_node)); 19600d3f6d29SMartin KaFai Lau } 19610d3f6d29SMartin KaFai Lau 196245e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk, 196345e4fd26SMartin KaFai Lau const struct ipv6hdr *iph, u32 mtu) 19641da177e4SLinus Torvalds { 19650dec879fSJulian Anastasov const struct in6_addr *daddr, *saddr; 19661da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info *)dst; 19671da177e4SLinus Torvalds 196845e4fd26SMartin KaFai Lau if (rt6->rt6i_flags & RTF_LOCAL) 196945e4fd26SMartin KaFai Lau return; 197045e4fd26SMartin KaFai Lau 197119bda36cSXin Long if (dst_metric_locked(dst, RTAX_MTU)) 197219bda36cSXin Long return; 197319bda36cSXin Long 197445e4fd26SMartin KaFai Lau if (iph) { 197545e4fd26SMartin KaFai Lau daddr = &iph->daddr; 197645e4fd26SMartin KaFai Lau saddr = &iph->saddr; 197745e4fd26SMartin KaFai Lau } else if (sk) { 197845e4fd26SMartin KaFai Lau daddr = &sk->sk_v6_daddr; 197945e4fd26SMartin KaFai Lau saddr = &inet6_sk(sk)->saddr; 198045e4fd26SMartin KaFai Lau } else { 19810dec879fSJulian Anastasov daddr = NULL; 19820dec879fSJulian Anastasov saddr = NULL; 19831da177e4SLinus Torvalds } 19840dec879fSJulian Anastasov dst_confirm_neigh(dst, daddr); 19850dec879fSJulian Anastasov mtu = max_t(u32, mtu, IPV6_MIN_MTU); 19860dec879fSJulian Anastasov if (mtu >= dst_mtu(dst)) 19870dec879fSJulian Anastasov return; 19880dec879fSJulian Anastasov 19890dec879fSJulian Anastasov if (!rt6_cache_allowed_for_pmtu(rt6)) { 19900dec879fSJulian Anastasov rt6_do_update_pmtu(rt6, mtu); 19910dec879fSJulian Anastasov } else if (daddr) { 19920dec879fSJulian Anastasov struct rt6_info *nrt6; 19930dec879fSJulian Anastasov 199445e4fd26SMartin KaFai Lau nrt6 = ip6_rt_cache_alloc(rt6, daddr, saddr); 199545e4fd26SMartin KaFai Lau if (nrt6) { 199645e4fd26SMartin KaFai Lau rt6_do_update_pmtu(nrt6, mtu); 199745e4fd26SMartin KaFai Lau 199845e4fd26SMartin KaFai Lau /* ip6_ins_rt(nrt6) will bump the 199945e4fd26SMartin KaFai Lau * rt6->rt6i_node->fn_sernum 200045e4fd26SMartin KaFai Lau * which will fail the next rt6_check() and 200145e4fd26SMartin KaFai Lau * invalidate the sk->sk_dst_cache. 200245e4fd26SMartin KaFai Lau */ 200345e4fd26SMartin KaFai Lau ip6_ins_rt(nrt6); 20041cfb71eeSWei Wang /* Release the reference taken in 20051cfb71eeSWei Wang * ip6_rt_cache_alloc() 20061cfb71eeSWei Wang */ 20071cfb71eeSWei Wang dst_release(&nrt6->dst); 200845e4fd26SMartin KaFai Lau } 200945e4fd26SMartin KaFai Lau } 201045e4fd26SMartin KaFai Lau } 201145e4fd26SMartin KaFai Lau 201245e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 201345e4fd26SMartin KaFai Lau struct sk_buff *skb, u32 mtu) 201445e4fd26SMartin KaFai Lau { 201545e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu); 20161da177e4SLinus Torvalds } 20171da177e4SLinus Torvalds 201842ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, 2019e2d118a1SLorenzo Colitti int oif, u32 mark, kuid_t uid) 202081aded24SDavid S. Miller { 202181aded24SDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 202281aded24SDavid S. Miller struct dst_entry *dst; 202381aded24SDavid S. Miller struct flowi6 fl6; 202481aded24SDavid S. Miller 202581aded24SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 202681aded24SDavid S. Miller fl6.flowi6_oif = oif; 20271b3c61dcSLorenzo Colitti fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark); 202881aded24SDavid S. Miller fl6.daddr = iph->daddr; 202981aded24SDavid S. Miller fl6.saddr = iph->saddr; 20306502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 2031e2d118a1SLorenzo Colitti fl6.flowi6_uid = uid; 203281aded24SDavid S. Miller 203381aded24SDavid S. Miller dst = ip6_route_output(net, NULL, &fl6); 203481aded24SDavid S. Miller if (!dst->error) 203545e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu)); 203681aded24SDavid S. Miller dst_release(dst); 203781aded24SDavid S. Miller } 203881aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu); 203981aded24SDavid S. Miller 204081aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) 204181aded24SDavid S. Miller { 204233c162a9SMartin KaFai Lau struct dst_entry *dst; 204333c162a9SMartin KaFai Lau 204481aded24SDavid S. Miller ip6_update_pmtu(skb, sock_net(sk), mtu, 2045e2d118a1SLorenzo Colitti sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid); 204633c162a9SMartin KaFai Lau 204733c162a9SMartin KaFai Lau dst = __sk_dst_get(sk); 204833c162a9SMartin KaFai Lau if (!dst || !dst->obsolete || 204933c162a9SMartin KaFai Lau dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) 205033c162a9SMartin KaFai Lau return; 205133c162a9SMartin KaFai Lau 205233c162a9SMartin KaFai Lau bh_lock_sock(sk); 205333c162a9SMartin KaFai Lau if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) 205433c162a9SMartin KaFai Lau ip6_datagram_dst_update(sk, false); 205533c162a9SMartin KaFai Lau bh_unlock_sock(sk); 205681aded24SDavid S. Miller } 205781aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); 205881aded24SDavid S. Miller 2059b55b76b2SDuan Jiong /* Handle redirects */ 2060b55b76b2SDuan Jiong struct ip6rd_flowi { 2061b55b76b2SDuan Jiong struct flowi6 fl6; 2062b55b76b2SDuan Jiong struct in6_addr gateway; 2063b55b76b2SDuan Jiong }; 2064b55b76b2SDuan Jiong 2065b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net, 2066b55b76b2SDuan Jiong struct fib6_table *table, 2067b55b76b2SDuan Jiong struct flowi6 *fl6, 2068b55b76b2SDuan Jiong int flags) 2069b55b76b2SDuan Jiong { 2070b55b76b2SDuan Jiong struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; 2071b55b76b2SDuan Jiong struct rt6_info *rt; 2072b55b76b2SDuan Jiong struct fib6_node *fn; 2073b55b76b2SDuan Jiong 2074b55b76b2SDuan Jiong /* Get the "current" route for this destination and 207567c408cfSAlexander Alemayhu * check if the redirect has come from appropriate router. 2076b55b76b2SDuan Jiong * 2077b55b76b2SDuan Jiong * RFC 4861 specifies that redirects should only be 2078b55b76b2SDuan Jiong * accepted if they come from the nexthop to the target. 2079b55b76b2SDuan Jiong * Due to the way the routes are chosen, this notion 2080b55b76b2SDuan Jiong * is a bit fuzzy and one might need to check all possible 2081b55b76b2SDuan Jiong * routes. 2082b55b76b2SDuan Jiong */ 2083b55b76b2SDuan Jiong 2084b55b76b2SDuan Jiong read_lock_bh(&table->tb6_lock); 2085b55b76b2SDuan Jiong fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 2086b55b76b2SDuan Jiong restart: 2087b55b76b2SDuan Jiong for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 2088b55b76b2SDuan Jiong if (rt6_check_expired(rt)) 2089b55b76b2SDuan Jiong continue; 2090b55b76b2SDuan Jiong if (rt->dst.error) 2091b55b76b2SDuan Jiong break; 2092b55b76b2SDuan Jiong if (!(rt->rt6i_flags & RTF_GATEWAY)) 2093b55b76b2SDuan Jiong continue; 2094b55b76b2SDuan Jiong if (fl6->flowi6_oif != rt->dst.dev->ifindex) 2095b55b76b2SDuan Jiong continue; 2096b55b76b2SDuan Jiong if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway)) 2097b55b76b2SDuan Jiong continue; 2098b55b76b2SDuan Jiong break; 2099b55b76b2SDuan Jiong } 2100b55b76b2SDuan Jiong 2101b55b76b2SDuan Jiong if (!rt) 2102b55b76b2SDuan Jiong rt = net->ipv6.ip6_null_entry; 2103b55b76b2SDuan Jiong else if (rt->dst.error) { 2104b55b76b2SDuan Jiong rt = net->ipv6.ip6_null_entry; 2105b0a1ba59SMartin KaFai Lau goto out; 2106b0a1ba59SMartin KaFai Lau } 2107b0a1ba59SMartin KaFai Lau 2108b0a1ba59SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 2109a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 2110a3c00e46SMartin KaFai Lau if (fn) 2111a3c00e46SMartin KaFai Lau goto restart; 2112b55b76b2SDuan Jiong } 2113a3c00e46SMartin KaFai Lau 2114b0a1ba59SMartin KaFai Lau out: 2115b55b76b2SDuan Jiong dst_hold(&rt->dst); 2116b55b76b2SDuan Jiong 2117b55b76b2SDuan Jiong read_unlock_bh(&table->tb6_lock); 2118b55b76b2SDuan Jiong 2119b811580dSDavid Ahern trace_fib6_table_lookup(net, rt, table->tb6_id, fl6); 2120b55b76b2SDuan Jiong return rt; 2121b55b76b2SDuan Jiong }; 2122b55b76b2SDuan Jiong 2123b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net, 2124b55b76b2SDuan Jiong const struct flowi6 *fl6, 2125b55b76b2SDuan Jiong const struct in6_addr *gateway) 2126b55b76b2SDuan Jiong { 2127b55b76b2SDuan Jiong int flags = RT6_LOOKUP_F_HAS_SADDR; 2128b55b76b2SDuan Jiong struct ip6rd_flowi rdfl; 2129b55b76b2SDuan Jiong 2130b55b76b2SDuan Jiong rdfl.fl6 = *fl6; 2131b55b76b2SDuan Jiong rdfl.gateway = *gateway; 2132b55b76b2SDuan Jiong 2133b55b76b2SDuan Jiong return fib6_rule_lookup(net, &rdfl.fl6, 2134b55b76b2SDuan Jiong flags, __ip6_route_redirect); 2135b55b76b2SDuan Jiong } 2136b55b76b2SDuan Jiong 2137e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, 2138e2d118a1SLorenzo Colitti kuid_t uid) 21393a5ad2eeSDavid S. Miller { 21403a5ad2eeSDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 21413a5ad2eeSDavid S. Miller struct dst_entry *dst; 21423a5ad2eeSDavid S. Miller struct flowi6 fl6; 21433a5ad2eeSDavid S. Miller 21443a5ad2eeSDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 2145e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 21463a5ad2eeSDavid S. Miller fl6.flowi6_oif = oif; 21473a5ad2eeSDavid S. Miller fl6.flowi6_mark = mark; 21483a5ad2eeSDavid S. Miller fl6.daddr = iph->daddr; 21493a5ad2eeSDavid S. Miller fl6.saddr = iph->saddr; 21506502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 2151e2d118a1SLorenzo Colitti fl6.flowi6_uid = uid; 21523a5ad2eeSDavid S. Miller 2153b55b76b2SDuan Jiong dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr); 21546700c270SDavid S. Miller rt6_do_redirect(dst, NULL, skb); 21553a5ad2eeSDavid S. Miller dst_release(dst); 21563a5ad2eeSDavid S. Miller } 21573a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect); 21583a5ad2eeSDavid S. Miller 2159c92a59ecSDuan Jiong void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif, 2160c92a59ecSDuan Jiong u32 mark) 2161c92a59ecSDuan Jiong { 2162c92a59ecSDuan Jiong const struct ipv6hdr *iph = ipv6_hdr(skb); 2163c92a59ecSDuan Jiong const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); 2164c92a59ecSDuan Jiong struct dst_entry *dst; 2165c92a59ecSDuan Jiong struct flowi6 fl6; 2166c92a59ecSDuan Jiong 2167c92a59ecSDuan Jiong memset(&fl6, 0, sizeof(fl6)); 2168e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 2169c92a59ecSDuan Jiong fl6.flowi6_oif = oif; 2170c92a59ecSDuan Jiong fl6.flowi6_mark = mark; 2171c92a59ecSDuan Jiong fl6.daddr = msg->dest; 2172c92a59ecSDuan Jiong fl6.saddr = iph->daddr; 2173e2d118a1SLorenzo Colitti fl6.flowi6_uid = sock_net_uid(net, NULL); 2174c92a59ecSDuan Jiong 2175b55b76b2SDuan Jiong dst = ip6_route_redirect(net, &fl6, &iph->saddr); 2176c92a59ecSDuan Jiong rt6_do_redirect(dst, NULL, skb); 2177c92a59ecSDuan Jiong dst_release(dst); 2178c92a59ecSDuan Jiong } 2179c92a59ecSDuan Jiong 21803a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) 21813a5ad2eeSDavid S. Miller { 2182e2d118a1SLorenzo Colitti ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark, 2183e2d118a1SLorenzo Colitti sk->sk_uid); 21843a5ad2eeSDavid S. Miller } 21853a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect); 21863a5ad2eeSDavid S. Miller 21870dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 21881da177e4SLinus Torvalds { 21890dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 21900dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 21910dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 21920dbaee3bSDavid S. Miller 21931da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 21941da177e4SLinus Torvalds 21955578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 21965578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 21971da177e4SLinus Torvalds 21981da177e4SLinus Torvalds /* 21991da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 22001da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 22011da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 22021da177e4SLinus Torvalds * rely only on pmtu discovery" 22031da177e4SLinus Torvalds */ 22041da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 22051da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 22061da177e4SLinus Torvalds return mtu; 22071da177e4SLinus Torvalds } 22081da177e4SLinus Torvalds 2209ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst) 2210d33e4553SDavid S. Miller { 22114b32b5adSMartin KaFai Lau const struct rt6_info *rt = (const struct rt6_info *)dst; 22124b32b5adSMartin KaFai Lau unsigned int mtu = rt->rt6i_pmtu; 2213d33e4553SDavid S. Miller struct inet6_dev *idev; 2214618f9bc7SSteffen Klassert 2215618f9bc7SSteffen Klassert if (mtu) 221630f78d8eSEric Dumazet goto out; 2217618f9bc7SSteffen Klassert 22184b32b5adSMartin KaFai Lau mtu = dst_metric_raw(dst, RTAX_MTU); 22194b32b5adSMartin KaFai Lau if (mtu) 22204b32b5adSMartin KaFai Lau goto out; 22214b32b5adSMartin KaFai Lau 2222618f9bc7SSteffen Klassert mtu = IPV6_MIN_MTU; 2223d33e4553SDavid S. Miller 2224d33e4553SDavid S. Miller rcu_read_lock(); 2225d33e4553SDavid S. Miller idev = __in6_dev_get(dst->dev); 2226d33e4553SDavid S. Miller if (idev) 2227d33e4553SDavid S. Miller mtu = idev->cnf.mtu6; 2228d33e4553SDavid S. Miller rcu_read_unlock(); 2229d33e4553SDavid S. Miller 223030f78d8eSEric Dumazet out: 223114972cbdSRoopa Prabhu mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 223214972cbdSRoopa Prabhu 223314972cbdSRoopa Prabhu return mtu - lwtunnel_headroom(dst->lwtstate, mtu); 2234d33e4553SDavid S. Miller } 2235d33e4553SDavid S. Miller 22363b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 223787a11578SDavid S. Miller struct flowi6 *fl6) 22381da177e4SLinus Torvalds { 223987a11578SDavid S. Miller struct dst_entry *dst; 22401da177e4SLinus Torvalds struct rt6_info *rt; 22411da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 2242c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 22431da177e4SLinus Torvalds 224438308473SDavid S. Miller if (unlikely(!idev)) 2245122bdf67SEric Dumazet return ERR_PTR(-ENODEV); 22461da177e4SLinus Torvalds 2247ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, dev, 0); 224838308473SDavid S. Miller if (unlikely(!rt)) { 22491da177e4SLinus Torvalds in6_dev_put(idev); 225087a11578SDavid S. Miller dst = ERR_PTR(-ENOMEM); 22511da177e4SLinus Torvalds goto out; 22521da177e4SLinus Torvalds } 22531da177e4SLinus Torvalds 22548e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 22558e2ec639SYan, Zheng rt->dst.output = ip6_output; 2256550bab42SJulian Anastasov rt->rt6i_gateway = fl6->daddr; 225787a11578SDavid S. Miller rt->rt6i_dst.addr = fl6->daddr; 22588e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 22598e2ec639SYan, Zheng rt->rt6i_idev = idev; 226014edd87dSLi RongQing dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); 22611da177e4SLinus Torvalds 2262587fea74SWei Wang /* Add this dst into uncached_list so that rt6_ifdown() can 2263587fea74SWei Wang * do proper release of the net_device 2264587fea74SWei Wang */ 2265587fea74SWei Wang rt6_uncached_list_add(rt); 22661da177e4SLinus Torvalds 226787a11578SDavid S. Miller dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); 226887a11578SDavid S. Miller 22691da177e4SLinus Torvalds out: 227087a11578SDavid S. Miller return dst; 22711da177e4SLinus Torvalds } 22721da177e4SLinus Torvalds 2273569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops) 22741da177e4SLinus Torvalds { 227586393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 22767019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 22777019b78eSDaniel Lezcano int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; 22787019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 22797019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 22807019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 2281fc66f95cSEric Dumazet int entries; 22821da177e4SLinus Torvalds 2283fc66f95cSEric Dumazet entries = dst_entries_get_fast(ops); 228449a18d86SMichal Kubeček if (time_after(rt_last_gc + rt_min_interval, jiffies) && 2285fc66f95cSEric Dumazet entries <= rt_max_size) 22861da177e4SLinus Torvalds goto out; 22871da177e4SLinus Torvalds 22886891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire++; 228914956643SLi RongQing fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true); 2290fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 2291fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 22927019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; 22931da177e4SLinus Torvalds out: 22947019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; 2295fc66f95cSEric Dumazet return entries > rt_max_size; 22961da177e4SLinus Torvalds } 22971da177e4SLinus Torvalds 2298e715b6d3SFlorian Westphal static int ip6_convert_metrics(struct mx6_config *mxc, 2299e715b6d3SFlorian Westphal const struct fib6_config *cfg) 2300e715b6d3SFlorian Westphal { 2301c3a8d947SDaniel Borkmann bool ecn_ca = false; 2302e715b6d3SFlorian Westphal struct nlattr *nla; 2303e715b6d3SFlorian Westphal int remaining; 2304e715b6d3SFlorian Westphal u32 *mp; 2305e715b6d3SFlorian Westphal 230663159f29SIan Morris if (!cfg->fc_mx) 2307e715b6d3SFlorian Westphal return 0; 2308e715b6d3SFlorian Westphal 2309e715b6d3SFlorian Westphal mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL); 2310e715b6d3SFlorian Westphal if (unlikely(!mp)) 2311e715b6d3SFlorian Westphal return -ENOMEM; 2312e715b6d3SFlorian Westphal 2313e715b6d3SFlorian Westphal nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { 2314e715b6d3SFlorian Westphal int type = nla_type(nla); 2315ea697639SDaniel Borkmann u32 val; 2316ea697639SDaniel Borkmann 23171bb14807SDaniel Borkmann if (!type) 23181bb14807SDaniel Borkmann continue; 2319e715b6d3SFlorian Westphal if (unlikely(type > RTAX_MAX)) 2320e715b6d3SFlorian Westphal goto err; 23211bb14807SDaniel Borkmann 2322ea697639SDaniel Borkmann if (type == RTAX_CC_ALGO) { 2323ea697639SDaniel Borkmann char tmp[TCP_CA_NAME_MAX]; 2324e715b6d3SFlorian Westphal 2325ea697639SDaniel Borkmann nla_strlcpy(tmp, nla, sizeof(tmp)); 2326c3a8d947SDaniel Borkmann val = tcp_ca_get_key_by_name(tmp, &ecn_ca); 2327ea697639SDaniel Borkmann if (val == TCP_CA_UNSPEC) 2328ea697639SDaniel Borkmann goto err; 2329ea697639SDaniel Borkmann } else { 2330ea697639SDaniel Borkmann val = nla_get_u32(nla); 2331ea697639SDaniel Borkmann } 2332626abd59SPaolo Abeni if (type == RTAX_HOPLIMIT && val > 255) 2333626abd59SPaolo Abeni val = 255; 2334b8d3e416SDaniel Borkmann if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK)) 2335b8d3e416SDaniel Borkmann goto err; 2336ea697639SDaniel Borkmann 2337ea697639SDaniel Borkmann mp[type - 1] = val; 2338e715b6d3SFlorian Westphal __set_bit(type - 1, mxc->mx_valid); 2339e715b6d3SFlorian Westphal } 2340e715b6d3SFlorian Westphal 2341c3a8d947SDaniel Borkmann if (ecn_ca) { 2342c3a8d947SDaniel Borkmann __set_bit(RTAX_FEATURES - 1, mxc->mx_valid); 2343c3a8d947SDaniel Borkmann mp[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA; 2344c3a8d947SDaniel Borkmann } 2345e715b6d3SFlorian Westphal 2346c3a8d947SDaniel Borkmann mxc->mx = mp; 2347e715b6d3SFlorian Westphal return 0; 2348e715b6d3SFlorian Westphal err: 2349e715b6d3SFlorian Westphal kfree(mp); 2350e715b6d3SFlorian Westphal return -EINVAL; 2351e715b6d3SFlorian Westphal } 23521da177e4SLinus Torvalds 23538c14586fSDavid Ahern static struct rt6_info *ip6_nh_lookup_table(struct net *net, 23548c14586fSDavid Ahern struct fib6_config *cfg, 23558c14586fSDavid Ahern const struct in6_addr *gw_addr) 23568c14586fSDavid Ahern { 23578c14586fSDavid Ahern struct flowi6 fl6 = { 23588c14586fSDavid Ahern .flowi6_oif = cfg->fc_ifindex, 23598c14586fSDavid Ahern .daddr = *gw_addr, 23608c14586fSDavid Ahern .saddr = cfg->fc_prefsrc, 23618c14586fSDavid Ahern }; 23628c14586fSDavid Ahern struct fib6_table *table; 23638c14586fSDavid Ahern struct rt6_info *rt; 2364d5d32e4bSDavid Ahern int flags = RT6_LOOKUP_F_IFACE | RT6_LOOKUP_F_IGNORE_LINKSTATE; 23658c14586fSDavid Ahern 23668c14586fSDavid Ahern table = fib6_get_table(net, cfg->fc_table); 23678c14586fSDavid Ahern if (!table) 23688c14586fSDavid Ahern return NULL; 23698c14586fSDavid Ahern 23708c14586fSDavid Ahern if (!ipv6_addr_any(&cfg->fc_prefsrc)) 23718c14586fSDavid Ahern flags |= RT6_LOOKUP_F_HAS_SADDR; 23728c14586fSDavid Ahern 23738c14586fSDavid Ahern rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, flags); 23748c14586fSDavid Ahern 23758c14586fSDavid Ahern /* if table lookup failed, fall back to full lookup */ 23768c14586fSDavid Ahern if (rt == net->ipv6.ip6_null_entry) { 23778c14586fSDavid Ahern ip6_rt_put(rt); 23788c14586fSDavid Ahern rt = NULL; 23798c14586fSDavid Ahern } 23808c14586fSDavid Ahern 23818c14586fSDavid Ahern return rt; 23828c14586fSDavid Ahern } 23838c14586fSDavid Ahern 2384333c4301SDavid Ahern static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg, 2385333c4301SDavid Ahern struct netlink_ext_ack *extack) 23861da177e4SLinus Torvalds { 23875578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 23881da177e4SLinus Torvalds struct rt6_info *rt = NULL; 23891da177e4SLinus Torvalds struct net_device *dev = NULL; 23901da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 2391c71099acSThomas Graf struct fib6_table *table; 23921da177e4SLinus Torvalds int addr_type; 23938c5b83f0SRoopa Prabhu int err = -EINVAL; 23941da177e4SLinus Torvalds 2395557c44beSDavid Ahern /* RTF_PCPU is an internal flag; can not be set by userspace */ 2396d5d531cbSDavid Ahern if (cfg->fc_flags & RTF_PCPU) { 2397d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU"); 2398557c44beSDavid Ahern goto out; 2399d5d531cbSDavid Ahern } 2400557c44beSDavid Ahern 2401d5d531cbSDavid Ahern if (cfg->fc_dst_len > 128) { 2402d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid prefix length"); 24038c5b83f0SRoopa Prabhu goto out; 2404d5d531cbSDavid Ahern } 2405d5d531cbSDavid Ahern if (cfg->fc_src_len > 128) { 2406d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address length"); 2407d5d531cbSDavid Ahern goto out; 2408d5d531cbSDavid Ahern } 24091da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 2410d5d531cbSDavid Ahern if (cfg->fc_src_len) { 2411d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2412d5d531cbSDavid Ahern "Specifying source address requires IPV6_SUBTREES to be enabled"); 24138c5b83f0SRoopa Prabhu goto out; 2414d5d531cbSDavid Ahern } 24151da177e4SLinus Torvalds #endif 241686872cb5SThomas Graf if (cfg->fc_ifindex) { 24171da177e4SLinus Torvalds err = -ENODEV; 24185578689aSDaniel Lezcano dev = dev_get_by_index(net, cfg->fc_ifindex); 24191da177e4SLinus Torvalds if (!dev) 24201da177e4SLinus Torvalds goto out; 24211da177e4SLinus Torvalds idev = in6_dev_get(dev); 24221da177e4SLinus Torvalds if (!idev) 24231da177e4SLinus Torvalds goto out; 24241da177e4SLinus Torvalds } 24251da177e4SLinus Torvalds 242686872cb5SThomas Graf if (cfg->fc_metric == 0) 242786872cb5SThomas Graf cfg->fc_metric = IP6_RT_PRIO_USER; 24281da177e4SLinus Torvalds 2429c71099acSThomas Graf err = -ENOBUFS; 243038308473SDavid S. Miller if (cfg->fc_nlinfo.nlh && 2431d71314b4SMatti Vaittinen !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { 2432d71314b4SMatti Vaittinen table = fib6_get_table(net, cfg->fc_table); 243338308473SDavid S. Miller if (!table) { 2434f3213831SJoe Perches pr_warn("NLM_F_CREATE should be specified when creating new route\n"); 2435d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 2436d71314b4SMatti Vaittinen } 2437d71314b4SMatti Vaittinen } else { 2438d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 2439d71314b4SMatti Vaittinen } 244038308473SDavid S. Miller 244138308473SDavid S. Miller if (!table) 2442c71099acSThomas Graf goto out; 2443c71099acSThomas Graf 2444ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, NULL, 2445ad706862SMartin KaFai Lau (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT); 24461da177e4SLinus Torvalds 244738308473SDavid S. Miller if (!rt) { 24481da177e4SLinus Torvalds err = -ENOMEM; 24491da177e4SLinus Torvalds goto out; 24501da177e4SLinus Torvalds } 24511da177e4SLinus Torvalds 24521716a961SGao feng if (cfg->fc_flags & RTF_EXPIRES) 24531716a961SGao feng rt6_set_expires(rt, jiffies + 24541716a961SGao feng clock_t_to_jiffies(cfg->fc_expires)); 24551716a961SGao feng else 24561716a961SGao feng rt6_clean_expires(rt); 24571da177e4SLinus Torvalds 245886872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 245986872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 246086872cb5SThomas Graf rt->rt6i_protocol = cfg->fc_protocol; 246186872cb5SThomas Graf 246286872cb5SThomas Graf addr_type = ipv6_addr_type(&cfg->fc_dst); 24631da177e4SLinus Torvalds 24641da177e4SLinus Torvalds if (addr_type & IPV6_ADDR_MULTICAST) 2465d8d1f30bSChangli Gao rt->dst.input = ip6_mc_input; 2466ab79ad14SMaciej Żenczykowski else if (cfg->fc_flags & RTF_LOCAL) 2467ab79ad14SMaciej Żenczykowski rt->dst.input = ip6_input; 24681da177e4SLinus Torvalds else 2469d8d1f30bSChangli Gao rt->dst.input = ip6_forward; 24701da177e4SLinus Torvalds 2471d8d1f30bSChangli Gao rt->dst.output = ip6_output; 24721da177e4SLinus Torvalds 247319e42e45SRoopa Prabhu if (cfg->fc_encap) { 247419e42e45SRoopa Prabhu struct lwtunnel_state *lwtstate; 247519e42e45SRoopa Prabhu 247630357d7dSDavid Ahern err = lwtunnel_build_state(cfg->fc_encap_type, 2477127eb7cdSTom Herbert cfg->fc_encap, AF_INET6, cfg, 24789ae28727SDavid Ahern &lwtstate, extack); 247919e42e45SRoopa Prabhu if (err) 248019e42e45SRoopa Prabhu goto out; 248161adedf3SJiri Benc rt->dst.lwtstate = lwtstate_get(lwtstate); 248261adedf3SJiri Benc if (lwtunnel_output_redirect(rt->dst.lwtstate)) { 248361adedf3SJiri Benc rt->dst.lwtstate->orig_output = rt->dst.output; 248461adedf3SJiri Benc rt->dst.output = lwtunnel_output; 248519e42e45SRoopa Prabhu } 248661adedf3SJiri Benc if (lwtunnel_input_redirect(rt->dst.lwtstate)) { 248761adedf3SJiri Benc rt->dst.lwtstate->orig_input = rt->dst.input; 248861adedf3SJiri Benc rt->dst.input = lwtunnel_input; 248925368623STom Herbert } 249025368623STom Herbert } 249119e42e45SRoopa Prabhu 249286872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 249386872cb5SThomas Graf rt->rt6i_dst.plen = cfg->fc_dst_len; 2494afc4eef8SMartin KaFai Lau if (rt->rt6i_dst.plen == 128) 249511d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 24961da177e4SLinus Torvalds 24971da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 249886872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len); 249986872cb5SThomas Graf rt->rt6i_src.plen = cfg->fc_src_len; 25001da177e4SLinus Torvalds #endif 25011da177e4SLinus Torvalds 250286872cb5SThomas Graf rt->rt6i_metric = cfg->fc_metric; 25031da177e4SLinus Torvalds 25041da177e4SLinus Torvalds /* We cannot add true routes via loopback here, 25051da177e4SLinus Torvalds they would result in kernel looping; promote them to reject routes 25061da177e4SLinus Torvalds */ 250786872cb5SThomas Graf if ((cfg->fc_flags & RTF_REJECT) || 250838308473SDavid S. Miller (dev && (dev->flags & IFF_LOOPBACK) && 250938308473SDavid S. Miller !(addr_type & IPV6_ADDR_LOOPBACK) && 251038308473SDavid S. Miller !(cfg->fc_flags & RTF_LOCAL))) { 25111da177e4SLinus Torvalds /* hold loopback dev/idev if we haven't done so. */ 25125578689aSDaniel Lezcano if (dev != net->loopback_dev) { 25131da177e4SLinus Torvalds if (dev) { 25141da177e4SLinus Torvalds dev_put(dev); 25151da177e4SLinus Torvalds in6_dev_put(idev); 25161da177e4SLinus Torvalds } 25175578689aSDaniel Lezcano dev = net->loopback_dev; 25181da177e4SLinus Torvalds dev_hold(dev); 25191da177e4SLinus Torvalds idev = in6_dev_get(dev); 25201da177e4SLinus Torvalds if (!idev) { 25211da177e4SLinus Torvalds err = -ENODEV; 25221da177e4SLinus Torvalds goto out; 25231da177e4SLinus Torvalds } 25241da177e4SLinus Torvalds } 25251da177e4SLinus Torvalds rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP; 2526ef2c7d7bSNicolas Dichtel switch (cfg->fc_type) { 2527ef2c7d7bSNicolas Dichtel case RTN_BLACKHOLE: 2528ef2c7d7bSNicolas Dichtel rt->dst.error = -EINVAL; 2529ede2059dSEric W. Biederman rt->dst.output = dst_discard_out; 25307150aedeSKamala R rt->dst.input = dst_discard; 2531ef2c7d7bSNicolas Dichtel break; 2532ef2c7d7bSNicolas Dichtel case RTN_PROHIBIT: 2533ef2c7d7bSNicolas Dichtel rt->dst.error = -EACCES; 25347150aedeSKamala R rt->dst.output = ip6_pkt_prohibit_out; 25357150aedeSKamala R rt->dst.input = ip6_pkt_prohibit; 2536ef2c7d7bSNicolas Dichtel break; 2537b4949ab2SNicolas Dichtel case RTN_THROW: 25380315e382SNikola Forró case RTN_UNREACHABLE: 2539ef2c7d7bSNicolas Dichtel default: 25407150aedeSKamala R rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN 25410315e382SNikola Forró : (cfg->fc_type == RTN_UNREACHABLE) 25420315e382SNikola Forró ? -EHOSTUNREACH : -ENETUNREACH; 25437150aedeSKamala R rt->dst.output = ip6_pkt_discard_out; 25447150aedeSKamala R rt->dst.input = ip6_pkt_discard; 2545ef2c7d7bSNicolas Dichtel break; 2546ef2c7d7bSNicolas Dichtel } 25471da177e4SLinus Torvalds goto install_route; 25481da177e4SLinus Torvalds } 25491da177e4SLinus Torvalds 255086872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY) { 2551b71d1d42SEric Dumazet const struct in6_addr *gw_addr; 25521da177e4SLinus Torvalds int gwa_type; 25531da177e4SLinus Torvalds 255486872cb5SThomas Graf gw_addr = &cfg->fc_gateway; 2555330567b7SFlorian Westphal gwa_type = ipv6_addr_type(gw_addr); 255648ed7b26SFlorian Westphal 255748ed7b26SFlorian Westphal /* if gw_addr is local we will fail to detect this in case 255848ed7b26SFlorian Westphal * address is still TENTATIVE (DAD in progress). rt6_lookup() 255948ed7b26SFlorian Westphal * will return already-added prefix route via interface that 256048ed7b26SFlorian Westphal * prefix route was assigned to, which might be non-loopback. 256148ed7b26SFlorian Westphal */ 256248ed7b26SFlorian Westphal err = -EINVAL; 2563330567b7SFlorian Westphal if (ipv6_chk_addr_and_flags(net, gw_addr, 2564330567b7SFlorian Westphal gwa_type & IPV6_ADDR_LINKLOCAL ? 2565d5d531cbSDavid Ahern dev : NULL, 0, 0)) { 2566d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid gateway address"); 256748ed7b26SFlorian Westphal goto out; 2568d5d531cbSDavid Ahern } 25694e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = *gw_addr; 25701da177e4SLinus Torvalds 25711da177e4SLinus Torvalds if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) { 25728c14586fSDavid Ahern struct rt6_info *grt = NULL; 25731da177e4SLinus Torvalds 25741da177e4SLinus Torvalds /* IPv6 strictly inhibits using not link-local 25751da177e4SLinus Torvalds addresses as nexthop address. 25761da177e4SLinus Torvalds Otherwise, router will not able to send redirects. 25771da177e4SLinus Torvalds It is very good, but in some (rare!) circumstances 25781da177e4SLinus Torvalds (SIT, PtP, NBMA NOARP links) it is handy to allow 25791da177e4SLinus Torvalds some exceptions. --ANK 258096d5822cSErik Nordmark We allow IPv4-mapped nexthops to support RFC4798-type 258196d5822cSErik Nordmark addressing 25821da177e4SLinus Torvalds */ 258396d5822cSErik Nordmark if (!(gwa_type & (IPV6_ADDR_UNICAST | 2584d5d531cbSDavid Ahern IPV6_ADDR_MAPPED))) { 2585d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2586d5d531cbSDavid Ahern "Invalid gateway address"); 25871da177e4SLinus Torvalds goto out; 2588d5d531cbSDavid Ahern } 25891da177e4SLinus Torvalds 2590a435a07fSVincent Bernat if (cfg->fc_table) { 25918c14586fSDavid Ahern grt = ip6_nh_lookup_table(net, cfg, gw_addr); 25928c14586fSDavid Ahern 2593a435a07fSVincent Bernat if (grt) { 2594a435a07fSVincent Bernat if (grt->rt6i_flags & RTF_GATEWAY || 2595a435a07fSVincent Bernat (dev && dev != grt->dst.dev)) { 2596a435a07fSVincent Bernat ip6_rt_put(grt); 2597a435a07fSVincent Bernat grt = NULL; 2598a435a07fSVincent Bernat } 2599a435a07fSVincent Bernat } 2600a435a07fSVincent Bernat } 2601a435a07fSVincent Bernat 26028c14586fSDavid Ahern if (!grt) 26038c14586fSDavid Ahern grt = rt6_lookup(net, gw_addr, NULL, 26048c14586fSDavid Ahern cfg->fc_ifindex, 1); 26051da177e4SLinus Torvalds 26061da177e4SLinus Torvalds err = -EHOSTUNREACH; 260738308473SDavid S. Miller if (!grt) 26081da177e4SLinus Torvalds goto out; 26091da177e4SLinus Torvalds if (dev) { 2610d1918542SDavid S. Miller if (dev != grt->dst.dev) { 261194e187c0SAmerigo Wang ip6_rt_put(grt); 26121da177e4SLinus Torvalds goto out; 26131da177e4SLinus Torvalds } 26141da177e4SLinus Torvalds } else { 2615d1918542SDavid S. Miller dev = grt->dst.dev; 26161da177e4SLinus Torvalds idev = grt->rt6i_idev; 26171da177e4SLinus Torvalds dev_hold(dev); 26181da177e4SLinus Torvalds in6_dev_hold(grt->rt6i_idev); 26191da177e4SLinus Torvalds } 26201da177e4SLinus Torvalds if (!(grt->rt6i_flags & RTF_GATEWAY)) 26211da177e4SLinus Torvalds err = 0; 262294e187c0SAmerigo Wang ip6_rt_put(grt); 26231da177e4SLinus Torvalds 26241da177e4SLinus Torvalds if (err) 26251da177e4SLinus Torvalds goto out; 26261da177e4SLinus Torvalds } 26271da177e4SLinus Torvalds err = -EINVAL; 2628d5d531cbSDavid Ahern if (!dev) { 2629d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Egress device not specified"); 26301da177e4SLinus Torvalds goto out; 2631d5d531cbSDavid Ahern } else if (dev->flags & IFF_LOOPBACK) { 2632d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2633d5d531cbSDavid Ahern "Egress device can not be loopback device for this route"); 2634d5d531cbSDavid Ahern goto out; 2635d5d531cbSDavid Ahern } 26361da177e4SLinus Torvalds } 26371da177e4SLinus Torvalds 26381da177e4SLinus Torvalds err = -ENODEV; 263938308473SDavid S. Miller if (!dev) 26401da177e4SLinus Torvalds goto out; 26411da177e4SLinus Torvalds 2642c3968a85SDaniel Walter if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 2643c3968a85SDaniel Walter if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 2644d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address"); 2645c3968a85SDaniel Walter err = -EINVAL; 2646c3968a85SDaniel Walter goto out; 2647c3968a85SDaniel Walter } 26484e3fd7a0SAlexey Dobriyan rt->rt6i_prefsrc.addr = cfg->fc_prefsrc; 2649c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 128; 2650c3968a85SDaniel Walter } else 2651c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 2652c3968a85SDaniel Walter 265386872cb5SThomas Graf rt->rt6i_flags = cfg->fc_flags; 26541da177e4SLinus Torvalds 26551da177e4SLinus Torvalds install_route: 2656d8d1f30bSChangli Gao rt->dst.dev = dev; 26571da177e4SLinus Torvalds rt->rt6i_idev = idev; 2658c71099acSThomas Graf rt->rt6i_table = table; 265963152fc0SDaniel Lezcano 2660c346dca1SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = dev_net(dev); 266163152fc0SDaniel Lezcano 26628c5b83f0SRoopa Prabhu return rt; 26631da177e4SLinus Torvalds out: 26641da177e4SLinus Torvalds if (dev) 26651da177e4SLinus Torvalds dev_put(dev); 26661da177e4SLinus Torvalds if (idev) 26671da177e4SLinus Torvalds in6_dev_put(idev); 2668587fea74SWei Wang if (rt) 2669587fea74SWei Wang dst_release_immediate(&rt->dst); 26706b9ea5a6SRoopa Prabhu 26718c5b83f0SRoopa Prabhu return ERR_PTR(err); 26726b9ea5a6SRoopa Prabhu } 26736b9ea5a6SRoopa Prabhu 2674333c4301SDavid Ahern int ip6_route_add(struct fib6_config *cfg, 2675333c4301SDavid Ahern struct netlink_ext_ack *extack) 26766b9ea5a6SRoopa Prabhu { 26776b9ea5a6SRoopa Prabhu struct mx6_config mxc = { .mx = NULL, }; 26788c5b83f0SRoopa Prabhu struct rt6_info *rt; 26796b9ea5a6SRoopa Prabhu int err; 26806b9ea5a6SRoopa Prabhu 2681333c4301SDavid Ahern rt = ip6_route_info_create(cfg, extack); 26828c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 26838c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 26848c5b83f0SRoopa Prabhu rt = NULL; 26856b9ea5a6SRoopa Prabhu goto out; 26868c5b83f0SRoopa Prabhu } 26876b9ea5a6SRoopa Prabhu 26886b9ea5a6SRoopa Prabhu err = ip6_convert_metrics(&mxc, cfg); 26896b9ea5a6SRoopa Prabhu if (err) 26906b9ea5a6SRoopa Prabhu goto out; 26916b9ea5a6SRoopa Prabhu 2692333c4301SDavid Ahern err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, &mxc, extack); 26936b9ea5a6SRoopa Prabhu 26946b9ea5a6SRoopa Prabhu kfree(mxc.mx); 26956b9ea5a6SRoopa Prabhu 26966b9ea5a6SRoopa Prabhu return err; 26976b9ea5a6SRoopa Prabhu out: 2698587fea74SWei Wang if (rt) 2699587fea74SWei Wang dst_release_immediate(&rt->dst); 27006b9ea5a6SRoopa Prabhu 27011da177e4SLinus Torvalds return err; 27021da177e4SLinus Torvalds } 27031da177e4SLinus Torvalds 270486872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info) 27051da177e4SLinus Torvalds { 27061da177e4SLinus Torvalds int err; 2707c71099acSThomas Graf struct fib6_table *table; 2708d1918542SDavid S. Miller struct net *net = dev_net(rt->dst.dev); 27091da177e4SLinus Torvalds 2710a4c2fd7fSWei Wang if (rt == net->ipv6.ip6_null_entry) { 27116825a26cSGao feng err = -ENOENT; 27126825a26cSGao feng goto out; 27136825a26cSGao feng } 27146c813a72SPatrick McHardy 2715c71099acSThomas Graf table = rt->rt6i_table; 2716c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 271786872cb5SThomas Graf err = fib6_del(rt, info); 2718c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 27191da177e4SLinus Torvalds 27206825a26cSGao feng out: 272194e187c0SAmerigo Wang ip6_rt_put(rt); 27221da177e4SLinus Torvalds return err; 27231da177e4SLinus Torvalds } 27241da177e4SLinus Torvalds 2725e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt) 2726e0a1ad73SThomas Graf { 27274d1169c1SDenis V. Lunev struct nl_info info = { 2728d1918542SDavid S. Miller .nl_net = dev_net(rt->dst.dev), 27294d1169c1SDenis V. Lunev }; 2730528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 2731e0a1ad73SThomas Graf } 2732e0a1ad73SThomas Graf 27330ae81335SDavid Ahern static int __ip6_del_rt_siblings(struct rt6_info *rt, struct fib6_config *cfg) 27340ae81335SDavid Ahern { 27350ae81335SDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 2736e3330039SWANG Cong struct net *net = info->nl_net; 273716a16cd3SDavid Ahern struct sk_buff *skb = NULL; 27380ae81335SDavid Ahern struct fib6_table *table; 2739e3330039SWANG Cong int err = -ENOENT; 27400ae81335SDavid Ahern 2741e3330039SWANG Cong if (rt == net->ipv6.ip6_null_entry) 2742e3330039SWANG Cong goto out_put; 27430ae81335SDavid Ahern table = rt->rt6i_table; 27440ae81335SDavid Ahern write_lock_bh(&table->tb6_lock); 27450ae81335SDavid Ahern 27460ae81335SDavid Ahern if (rt->rt6i_nsiblings && cfg->fc_delete_all_nh) { 27470ae81335SDavid Ahern struct rt6_info *sibling, *next_sibling; 27480ae81335SDavid Ahern 274916a16cd3SDavid Ahern /* prefer to send a single notification with all hops */ 275016a16cd3SDavid Ahern skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 275116a16cd3SDavid Ahern if (skb) { 275216a16cd3SDavid Ahern u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 275316a16cd3SDavid Ahern 2754e3330039SWANG Cong if (rt6_fill_node(net, skb, rt, 275516a16cd3SDavid Ahern NULL, NULL, 0, RTM_DELROUTE, 275616a16cd3SDavid Ahern info->portid, seq, 0) < 0) { 275716a16cd3SDavid Ahern kfree_skb(skb); 275816a16cd3SDavid Ahern skb = NULL; 275916a16cd3SDavid Ahern } else 276016a16cd3SDavid Ahern info->skip_notify = 1; 276116a16cd3SDavid Ahern } 276216a16cd3SDavid Ahern 27630ae81335SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 27640ae81335SDavid Ahern &rt->rt6i_siblings, 27650ae81335SDavid Ahern rt6i_siblings) { 27660ae81335SDavid Ahern err = fib6_del(sibling, info); 27670ae81335SDavid Ahern if (err) 2768e3330039SWANG Cong goto out_unlock; 27690ae81335SDavid Ahern } 27700ae81335SDavid Ahern } 27710ae81335SDavid Ahern 27720ae81335SDavid Ahern err = fib6_del(rt, info); 2773e3330039SWANG Cong out_unlock: 27740ae81335SDavid Ahern write_unlock_bh(&table->tb6_lock); 2775e3330039SWANG Cong out_put: 27760ae81335SDavid Ahern ip6_rt_put(rt); 277716a16cd3SDavid Ahern 277816a16cd3SDavid Ahern if (skb) { 2779e3330039SWANG Cong rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 278016a16cd3SDavid Ahern info->nlh, gfp_any()); 278116a16cd3SDavid Ahern } 27820ae81335SDavid Ahern return err; 27830ae81335SDavid Ahern } 27840ae81335SDavid Ahern 2785333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg, 2786333c4301SDavid Ahern struct netlink_ext_ack *extack) 27871da177e4SLinus Torvalds { 2788c71099acSThomas Graf struct fib6_table *table; 27891da177e4SLinus Torvalds struct fib6_node *fn; 27901da177e4SLinus Torvalds struct rt6_info *rt; 27911da177e4SLinus Torvalds int err = -ESRCH; 27921da177e4SLinus Torvalds 27935578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 2794d5d531cbSDavid Ahern if (!table) { 2795d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "FIB table does not exist"); 2796c71099acSThomas Graf return err; 2797d5d531cbSDavid Ahern } 27981da177e4SLinus Torvalds 2799c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 2800c71099acSThomas Graf 2801c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 280286872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 280386872cb5SThomas Graf &cfg->fc_src, cfg->fc_src_len); 28041da177e4SLinus Torvalds 28051da177e4SLinus Torvalds if (fn) { 2806d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 28071f56a01fSMartin KaFai Lau if ((rt->rt6i_flags & RTF_CACHE) && 28081f56a01fSMartin KaFai Lau !(cfg->fc_flags & RTF_CACHE)) 28091f56a01fSMartin KaFai Lau continue; 281086872cb5SThomas Graf if (cfg->fc_ifindex && 2811d1918542SDavid S. Miller (!rt->dst.dev || 2812d1918542SDavid S. Miller rt->dst.dev->ifindex != cfg->fc_ifindex)) 28131da177e4SLinus Torvalds continue; 281486872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 281586872cb5SThomas Graf !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 28161da177e4SLinus Torvalds continue; 281786872cb5SThomas Graf if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric) 28181da177e4SLinus Torvalds continue; 2819c2ed1880SMantas M if (cfg->fc_protocol && cfg->fc_protocol != rt->rt6i_protocol) 2820c2ed1880SMantas M continue; 2821d8d1f30bSChangli Gao dst_hold(&rt->dst); 2822c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 28231da177e4SLinus Torvalds 28240ae81335SDavid Ahern /* if gateway was specified only delete the one hop */ 28250ae81335SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY) 282686872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 28270ae81335SDavid Ahern 28280ae81335SDavid Ahern return __ip6_del_rt_siblings(rt, cfg); 28291da177e4SLinus Torvalds } 28301da177e4SLinus Torvalds } 2831c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 28321da177e4SLinus Torvalds 28331da177e4SLinus Torvalds return err; 28341da177e4SLinus Torvalds } 28351da177e4SLinus Torvalds 28366700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 2837a6279458SYOSHIFUJI Hideaki { 2838a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 2839e8599ff4SDavid S. Miller struct rt6_info *rt, *nrt = NULL; 2840e8599ff4SDavid S. Miller struct ndisc_options ndopts; 2841e8599ff4SDavid S. Miller struct inet6_dev *in6_dev; 2842e8599ff4SDavid S. Miller struct neighbour *neigh; 284371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 struct rd_msg *msg; 28446e157b6aSDavid S. Miller int optlen, on_link; 28456e157b6aSDavid S. Miller u8 *lladdr; 2846e8599ff4SDavid S. Miller 284729a3cad5SSimon Horman optlen = skb_tail_pointer(skb) - skb_transport_header(skb); 284871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 optlen -= sizeof(*msg); 2849e8599ff4SDavid S. Miller 2850e8599ff4SDavid S. Miller if (optlen < 0) { 28516e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); 2852e8599ff4SDavid S. Miller return; 2853e8599ff4SDavid S. Miller } 2854e8599ff4SDavid S. Miller 285571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 msg = (struct rd_msg *)icmp6_hdr(skb); 2856e8599ff4SDavid S. Miller 285771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_is_multicast(&msg->dest)) { 28586e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); 2859e8599ff4SDavid S. Miller return; 2860e8599ff4SDavid S. Miller } 2861e8599ff4SDavid S. Miller 28626e157b6aSDavid S. Miller on_link = 0; 286371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_equal(&msg->dest, &msg->target)) { 2864e8599ff4SDavid S. Miller on_link = 1; 286571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 } else if (ipv6_addr_type(&msg->target) != 2866e8599ff4SDavid S. Miller (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { 28676e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); 2868e8599ff4SDavid S. Miller return; 2869e8599ff4SDavid S. Miller } 2870e8599ff4SDavid S. Miller 2871e8599ff4SDavid S. Miller in6_dev = __in6_dev_get(skb->dev); 2872e8599ff4SDavid S. Miller if (!in6_dev) 2873e8599ff4SDavid S. Miller return; 2874e8599ff4SDavid S. Miller if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) 2875e8599ff4SDavid S. Miller return; 2876e8599ff4SDavid S. Miller 2877e8599ff4SDavid S. Miller /* RFC2461 8.1: 2878e8599ff4SDavid S. Miller * The IP source address of the Redirect MUST be the same as the current 2879e8599ff4SDavid S. Miller * first-hop router for the specified ICMP Destination Address. 2880e8599ff4SDavid S. Miller */ 2881e8599ff4SDavid S. Miller 2882f997c55cSAlexander Aring if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { 2883e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); 2884e8599ff4SDavid S. Miller return; 2885e8599ff4SDavid S. Miller } 28866e157b6aSDavid S. Miller 28876e157b6aSDavid S. Miller lladdr = NULL; 2888e8599ff4SDavid S. Miller if (ndopts.nd_opts_tgt_lladdr) { 2889e8599ff4SDavid S. Miller lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, 2890e8599ff4SDavid S. Miller skb->dev); 2891e8599ff4SDavid S. Miller if (!lladdr) { 2892e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); 2893e8599ff4SDavid S. Miller return; 2894e8599ff4SDavid S. Miller } 2895e8599ff4SDavid S. Miller } 2896e8599ff4SDavid S. Miller 28976e157b6aSDavid S. Miller rt = (struct rt6_info *) dst; 2898ec13ad1dSMatthias Schiffer if (rt->rt6i_flags & RTF_REJECT) { 28996e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); 29006e157b6aSDavid S. Miller return; 29016e157b6aSDavid S. Miller } 29026e157b6aSDavid S. Miller 29036e157b6aSDavid S. Miller /* Redirect received -> path was valid. 29046e157b6aSDavid S. Miller * Look, redirects are sent only in response to data packets, 29056e157b6aSDavid S. Miller * so that this nexthop apparently is reachable. --ANK 29066e157b6aSDavid S. Miller */ 29070dec879fSJulian Anastasov dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); 29086e157b6aSDavid S. Miller 290971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); 2910e8599ff4SDavid S. Miller if (!neigh) 2911e8599ff4SDavid S. Miller return; 2912e8599ff4SDavid S. Miller 29131da177e4SLinus Torvalds /* 29141da177e4SLinus Torvalds * We have finally decided to accept it. 29151da177e4SLinus Torvalds */ 29161da177e4SLinus Torvalds 2917f997c55cSAlexander Aring ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, 29181da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 29191da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 29201da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 2921f997c55cSAlexander Aring NEIGH_UPDATE_F_ISROUTER)), 2922f997c55cSAlexander Aring NDISC_REDIRECT, &ndopts); 29231da177e4SLinus Torvalds 292483a09abdSMartin KaFai Lau nrt = ip6_rt_cache_alloc(rt, &msg->dest, NULL); 292538308473SDavid S. Miller if (!nrt) 29261da177e4SLinus Torvalds goto out; 29271da177e4SLinus Torvalds 29281da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 29291da177e4SLinus Torvalds if (on_link) 29301da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 29311da177e4SLinus Torvalds 2932b91d5329SXin Long nrt->rt6i_protocol = RTPROT_REDIRECT; 29334e3fd7a0SAlexey Dobriyan nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 29341da177e4SLinus Torvalds 293540e22e8fSThomas Graf if (ip6_ins_rt(nrt)) 29361cfb71eeSWei Wang goto out_release; 29371da177e4SLinus Torvalds 2938d8d1f30bSChangli Gao netevent.old = &rt->dst; 2939d8d1f30bSChangli Gao netevent.new = &nrt->dst; 294071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 netevent.daddr = &msg->dest; 294160592833SYOSHIFUJI Hideaki / 吉藤英明 netevent.neigh = neigh; 29428d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 29438d71740cSTom Tucker 29441da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) { 29456e157b6aSDavid S. Miller rt = (struct rt6_info *) dst_clone(&rt->dst); 2946e0a1ad73SThomas Graf ip6_del_rt(rt); 29471da177e4SLinus Torvalds } 29481da177e4SLinus Torvalds 29491cfb71eeSWei Wang out_release: 29501cfb71eeSWei Wang /* Release the reference taken in 29511cfb71eeSWei Wang * ip6_rt_cache_alloc() 29521cfb71eeSWei Wang */ 29531cfb71eeSWei Wang dst_release(&nrt->dst); 29541cfb71eeSWei Wang 29551da177e4SLinus Torvalds out: 2956e8599ff4SDavid S. Miller neigh_release(neigh); 29576e157b6aSDavid S. Miller } 29586e157b6aSDavid S. Miller 29591da177e4SLinus Torvalds /* 29601da177e4SLinus Torvalds * Misc support functions 29611da177e4SLinus Torvalds */ 29621da177e4SLinus Torvalds 29634b32b5adSMartin KaFai Lau static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from) 29644b32b5adSMartin KaFai Lau { 29654b32b5adSMartin KaFai Lau BUG_ON(from->dst.from); 29664b32b5adSMartin KaFai Lau 29674b32b5adSMartin KaFai Lau rt->rt6i_flags &= ~RTF_EXPIRES; 29684b32b5adSMartin KaFai Lau dst_hold(&from->dst); 29694b32b5adSMartin KaFai Lau rt->dst.from = &from->dst; 29704b32b5adSMartin KaFai Lau dst_init_metrics(&rt->dst, dst_metrics_ptr(&from->dst), true); 29714b32b5adSMartin KaFai Lau } 29724b32b5adSMartin KaFai Lau 297383a09abdSMartin KaFai Lau static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort) 29741da177e4SLinus Torvalds { 2975d8d1f30bSChangli Gao rt->dst.input = ort->dst.input; 2976d8d1f30bSChangli Gao rt->dst.output = ort->dst.output; 297783a09abdSMartin KaFai Lau rt->rt6i_dst = ort->rt6i_dst; 2978d8d1f30bSChangli Gao rt->dst.error = ort->dst.error; 29791da177e4SLinus Torvalds rt->rt6i_idev = ort->rt6i_idev; 29801da177e4SLinus Torvalds if (rt->rt6i_idev) 29811da177e4SLinus Torvalds in6_dev_hold(rt->rt6i_idev); 2982d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 29834e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 29841716a961SGao feng rt->rt6i_flags = ort->rt6i_flags; 29851716a961SGao feng rt6_set_from(rt, ort); 298683a09abdSMartin KaFai Lau rt->rt6i_metric = ort->rt6i_metric; 29871da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 298883a09abdSMartin KaFai Lau rt->rt6i_src = ort->rt6i_src; 29891da177e4SLinus Torvalds #endif 299083a09abdSMartin KaFai Lau rt->rt6i_prefsrc = ort->rt6i_prefsrc; 2991c71099acSThomas Graf rt->rt6i_table = ort->rt6i_table; 299261adedf3SJiri Benc rt->dst.lwtstate = lwtstate_get(ort->dst.lwtstate); 29931da177e4SLinus Torvalds } 29941da177e4SLinus Torvalds 299570ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 2996efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 2997b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 2998830218c1SDavid Ahern const struct in6_addr *gwaddr, 2999830218c1SDavid Ahern struct net_device *dev) 300070ceb4f5SYOSHIFUJI Hideaki { 3001830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; 3002830218c1SDavid Ahern int ifindex = dev->ifindex; 300370ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 300470ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt = NULL; 3005c71099acSThomas Graf struct fib6_table *table; 300670ceb4f5SYOSHIFUJI Hideaki 3007830218c1SDavid Ahern table = fib6_get_table(net, tb_id); 300838308473SDavid S. Miller if (!table) 3009c71099acSThomas Graf return NULL; 3010c71099acSThomas Graf 30115744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 3012c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0); 301370ceb4f5SYOSHIFUJI Hideaki if (!fn) 301470ceb4f5SYOSHIFUJI Hideaki goto out; 301570ceb4f5SYOSHIFUJI Hideaki 3016d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 3017d1918542SDavid S. Miller if (rt->dst.dev->ifindex != ifindex) 301870ceb4f5SYOSHIFUJI Hideaki continue; 301970ceb4f5SYOSHIFUJI Hideaki if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) 302070ceb4f5SYOSHIFUJI Hideaki continue; 302170ceb4f5SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr)) 302270ceb4f5SYOSHIFUJI Hideaki continue; 3023d8d1f30bSChangli Gao dst_hold(&rt->dst); 302470ceb4f5SYOSHIFUJI Hideaki break; 302570ceb4f5SYOSHIFUJI Hideaki } 302670ceb4f5SYOSHIFUJI Hideaki out: 30275744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 302870ceb4f5SYOSHIFUJI Hideaki return rt; 302970ceb4f5SYOSHIFUJI Hideaki } 303070ceb4f5SYOSHIFUJI Hideaki 3031efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 3032b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 3033830218c1SDavid Ahern const struct in6_addr *gwaddr, 3034830218c1SDavid Ahern struct net_device *dev, 303595c96174SEric Dumazet unsigned int pref) 303670ceb4f5SYOSHIFUJI Hideaki { 303786872cb5SThomas Graf struct fib6_config cfg = { 3038238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 3039830218c1SDavid Ahern .fc_ifindex = dev->ifindex, 304086872cb5SThomas Graf .fc_dst_len = prefixlen, 304186872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 304286872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 3043b91d5329SXin Long .fc_protocol = RTPROT_RA, 304415e47304SEric W. Biederman .fc_nlinfo.portid = 0, 3045efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 3046efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 304786872cb5SThomas Graf }; 304870ceb4f5SYOSHIFUJI Hideaki 3049830218c1SDavid Ahern cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO, 30504e3fd7a0SAlexey Dobriyan cfg.fc_dst = *prefix; 30514e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 305286872cb5SThomas Graf 3053e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 3054e317da96SYOSHIFUJI Hideaki if (!prefixlen) 305586872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 305670ceb4f5SYOSHIFUJI Hideaki 3057333c4301SDavid Ahern ip6_route_add(&cfg, NULL); 305870ceb4f5SYOSHIFUJI Hideaki 3059830218c1SDavid Ahern return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); 306070ceb4f5SYOSHIFUJI Hideaki } 306170ceb4f5SYOSHIFUJI Hideaki #endif 306270ceb4f5SYOSHIFUJI Hideaki 3063b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev) 30641da177e4SLinus Torvalds { 3065830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; 30661da177e4SLinus Torvalds struct rt6_info *rt; 3067c71099acSThomas Graf struct fib6_table *table; 30681da177e4SLinus Torvalds 3069830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), tb_id); 307038308473SDavid S. Miller if (!table) 3071c71099acSThomas Graf return NULL; 30721da177e4SLinus Torvalds 30735744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 3074d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 3075d1918542SDavid S. Miller if (dev == rt->dst.dev && 3076045927ffSYOSHIFUJI Hideaki ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 30771da177e4SLinus Torvalds ipv6_addr_equal(&rt->rt6i_gateway, addr)) 30781da177e4SLinus Torvalds break; 30791da177e4SLinus Torvalds } 30801da177e4SLinus Torvalds if (rt) 3081d8d1f30bSChangli Gao dst_hold(&rt->dst); 30825744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 30831da177e4SLinus Torvalds return rt; 30841da177e4SLinus Torvalds } 30851da177e4SLinus Torvalds 3086b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr, 3087ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 3088ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 30891da177e4SLinus Torvalds { 309086872cb5SThomas Graf struct fib6_config cfg = { 3091ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, 3092238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 309386872cb5SThomas Graf .fc_ifindex = dev->ifindex, 309486872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 309586872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 3096b91d5329SXin Long .fc_protocol = RTPROT_RA, 309715e47304SEric W. Biederman .fc_nlinfo.portid = 0, 30985578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 3099c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 310086872cb5SThomas Graf }; 31011da177e4SLinus Torvalds 31024e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 31031da177e4SLinus Torvalds 3104333c4301SDavid Ahern if (!ip6_route_add(&cfg, NULL)) { 3105830218c1SDavid Ahern struct fib6_table *table; 3106830218c1SDavid Ahern 3107830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), cfg.fc_table); 3108830218c1SDavid Ahern if (table) 3109830218c1SDavid Ahern table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; 3110830218c1SDavid Ahern } 31111da177e4SLinus Torvalds 31121da177e4SLinus Torvalds return rt6_get_dflt_router(gwaddr, dev); 31131da177e4SLinus Torvalds } 31141da177e4SLinus Torvalds 3115830218c1SDavid Ahern static void __rt6_purge_dflt_routers(struct fib6_table *table) 31161da177e4SLinus Torvalds { 31171da177e4SLinus Torvalds struct rt6_info *rt; 31181da177e4SLinus Torvalds 31191da177e4SLinus Torvalds restart: 3120c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 3121d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 31223e8b0ac3SLorenzo Colitti if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) && 31233e8b0ac3SLorenzo Colitti (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) { 3124d8d1f30bSChangli Gao dst_hold(&rt->dst); 3125c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 3126e0a1ad73SThomas Graf ip6_del_rt(rt); 31271da177e4SLinus Torvalds goto restart; 31281da177e4SLinus Torvalds } 31291da177e4SLinus Torvalds } 3130c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 3131830218c1SDavid Ahern 3132830218c1SDavid Ahern table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; 3133830218c1SDavid Ahern } 3134830218c1SDavid Ahern 3135830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net) 3136830218c1SDavid Ahern { 3137830218c1SDavid Ahern struct fib6_table *table; 3138830218c1SDavid Ahern struct hlist_head *head; 3139830218c1SDavid Ahern unsigned int h; 3140830218c1SDavid Ahern 3141830218c1SDavid Ahern rcu_read_lock(); 3142830218c1SDavid Ahern 3143830218c1SDavid Ahern for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { 3144830218c1SDavid Ahern head = &net->ipv6.fib_table_hash[h]; 3145830218c1SDavid Ahern hlist_for_each_entry_rcu(table, head, tb6_hlist) { 3146830218c1SDavid Ahern if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) 3147830218c1SDavid Ahern __rt6_purge_dflt_routers(table); 3148830218c1SDavid Ahern } 3149830218c1SDavid Ahern } 3150830218c1SDavid Ahern 3151830218c1SDavid Ahern rcu_read_unlock(); 31521da177e4SLinus Torvalds } 31531da177e4SLinus Torvalds 31545578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 31555578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 315686872cb5SThomas Graf struct fib6_config *cfg) 315786872cb5SThomas Graf { 315886872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 315986872cb5SThomas Graf 3160ca254490SDavid Ahern cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? 3161ca254490SDavid Ahern : RT6_TABLE_MAIN; 316286872cb5SThomas Graf cfg->fc_ifindex = rtmsg->rtmsg_ifindex; 316386872cb5SThomas Graf cfg->fc_metric = rtmsg->rtmsg_metric; 316486872cb5SThomas Graf cfg->fc_expires = rtmsg->rtmsg_info; 316586872cb5SThomas Graf cfg->fc_dst_len = rtmsg->rtmsg_dst_len; 316686872cb5SThomas Graf cfg->fc_src_len = rtmsg->rtmsg_src_len; 316786872cb5SThomas Graf cfg->fc_flags = rtmsg->rtmsg_flags; 316886872cb5SThomas Graf 31695578689aSDaniel Lezcano cfg->fc_nlinfo.nl_net = net; 3170f1243c2dSBenjamin Thery 31714e3fd7a0SAlexey Dobriyan cfg->fc_dst = rtmsg->rtmsg_dst; 31724e3fd7a0SAlexey Dobriyan cfg->fc_src = rtmsg->rtmsg_src; 31734e3fd7a0SAlexey Dobriyan cfg->fc_gateway = rtmsg->rtmsg_gateway; 317486872cb5SThomas Graf } 317586872cb5SThomas Graf 31765578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) 31771da177e4SLinus Torvalds { 317886872cb5SThomas Graf struct fib6_config cfg; 31791da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 31801da177e4SLinus Torvalds int err; 31811da177e4SLinus Torvalds 31821da177e4SLinus Torvalds switch (cmd) { 31831da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 31841da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 3185af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 31861da177e4SLinus Torvalds return -EPERM; 31871da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 31881da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 31891da177e4SLinus Torvalds if (err) 31901da177e4SLinus Torvalds return -EFAULT; 31911da177e4SLinus Torvalds 31925578689aSDaniel Lezcano rtmsg_to_fib6_config(net, &rtmsg, &cfg); 319386872cb5SThomas Graf 31941da177e4SLinus Torvalds rtnl_lock(); 31951da177e4SLinus Torvalds switch (cmd) { 31961da177e4SLinus Torvalds case SIOCADDRT: 3197333c4301SDavid Ahern err = ip6_route_add(&cfg, NULL); 31981da177e4SLinus Torvalds break; 31991da177e4SLinus Torvalds case SIOCDELRT: 3200333c4301SDavid Ahern err = ip6_route_del(&cfg, NULL); 32011da177e4SLinus Torvalds break; 32021da177e4SLinus Torvalds default: 32031da177e4SLinus Torvalds err = -EINVAL; 32041da177e4SLinus Torvalds } 32051da177e4SLinus Torvalds rtnl_unlock(); 32061da177e4SLinus Torvalds 32071da177e4SLinus Torvalds return err; 32083ff50b79SStephen Hemminger } 32091da177e4SLinus Torvalds 32101da177e4SLinus Torvalds return -EINVAL; 32111da177e4SLinus Torvalds } 32121da177e4SLinus Torvalds 32131da177e4SLinus Torvalds /* 32141da177e4SLinus Torvalds * Drop the packet on the floor 32151da177e4SLinus Torvalds */ 32161da177e4SLinus Torvalds 3217d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 32181da177e4SLinus Torvalds { 3219612f09e8SYOSHIFUJI Hideaki int type; 3220adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 3221612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 3222612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 32230660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 322445bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 32253bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 32263bd653c8SDenis V. Lunev IPSTATS_MIB_INADDRERRORS); 3227612f09e8SYOSHIFUJI Hideaki break; 3228612f09e8SYOSHIFUJI Hideaki } 3229612f09e8SYOSHIFUJI Hideaki /* FALLTHROUGH */ 3230612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 32313bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 32323bd653c8SDenis V. Lunev ipstats_mib_noroutes); 3233612f09e8SYOSHIFUJI Hideaki break; 3234612f09e8SYOSHIFUJI Hideaki } 32353ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 32361da177e4SLinus Torvalds kfree_skb(skb); 32371da177e4SLinus Torvalds return 0; 32381da177e4SLinus Torvalds } 32391da177e4SLinus Torvalds 32409ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 32419ce8ade0SThomas Graf { 3242612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 32439ce8ade0SThomas Graf } 32449ce8ade0SThomas Graf 3245ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb) 32461da177e4SLinus Torvalds { 3247adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 3248612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 32491da177e4SLinus Torvalds } 32501da177e4SLinus Torvalds 32519ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 32529ce8ade0SThomas Graf { 3253612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 32549ce8ade0SThomas Graf } 32559ce8ade0SThomas Graf 3256ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb) 32579ce8ade0SThomas Graf { 3258adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 3259612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 32609ce8ade0SThomas Graf } 32619ce8ade0SThomas Graf 32621da177e4SLinus Torvalds /* 32631da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 32641da177e4SLinus Torvalds */ 32651da177e4SLinus Torvalds 32661da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, 32671da177e4SLinus Torvalds const struct in6_addr *addr, 32688f031519SDavid S. Miller bool anycast) 32691da177e4SLinus Torvalds { 3270ca254490SDavid Ahern u32 tb_id; 3271c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 32724832c30dSDavid Ahern struct net_device *dev = idev->dev; 32735f02ce24SDavid Ahern struct rt6_info *rt; 32745f02ce24SDavid Ahern 32755f02ce24SDavid Ahern rt = ip6_dst_alloc(net, dev, DST_NOCOUNT); 3276a3300ef4SHannes Frederic Sowa if (!rt) 32771da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 32781da177e4SLinus Torvalds 32791da177e4SLinus Torvalds in6_dev_hold(idev); 32801da177e4SLinus Torvalds 328111d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 3282d8d1f30bSChangli Gao rt->dst.input = ip6_input; 3283d8d1f30bSChangli Gao rt->dst.output = ip6_output; 32841da177e4SLinus Torvalds rt->rt6i_idev = idev; 32851da177e4SLinus Torvalds 328694b5e0f9SDavid Ahern rt->rt6i_protocol = RTPROT_KERNEL; 32871da177e4SLinus Torvalds rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP; 328858c4fb86SYOSHIFUJI Hideaki if (anycast) 328958c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 329058c4fb86SYOSHIFUJI Hideaki else 32911da177e4SLinus Torvalds rt->rt6i_flags |= RTF_LOCAL; 32921da177e4SLinus Torvalds 3293550bab42SJulian Anastasov rt->rt6i_gateway = *addr; 32944e3fd7a0SAlexey Dobriyan rt->rt6i_dst.addr = *addr; 32951da177e4SLinus Torvalds rt->rt6i_dst.plen = 128; 3296ca254490SDavid Ahern tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL; 3297ca254490SDavid Ahern rt->rt6i_table = fib6_get_table(net, tb_id); 32981da177e4SLinus Torvalds 32991da177e4SLinus Torvalds return rt; 33001da177e4SLinus Torvalds } 33011da177e4SLinus Torvalds 3302c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 3303c3968a85SDaniel Walter struct arg_dev_net_ip { 3304c3968a85SDaniel Walter struct net_device *dev; 3305c3968a85SDaniel Walter struct net *net; 3306c3968a85SDaniel Walter struct in6_addr *addr; 3307c3968a85SDaniel Walter }; 3308c3968a85SDaniel Walter 3309c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg) 3310c3968a85SDaniel Walter { 3311c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 3312c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 3313c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 3314c3968a85SDaniel Walter 3315d1918542SDavid S. Miller if (((void *)rt->dst.dev == dev || !dev) && 3316c3968a85SDaniel Walter rt != net->ipv6.ip6_null_entry && 3317c3968a85SDaniel Walter ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) { 331860006a48SWei Wang spin_lock_bh(&rt6_exception_lock); 3319c3968a85SDaniel Walter /* remove prefsrc entry */ 3320c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 332160006a48SWei Wang /* need to update cache as well */ 332260006a48SWei Wang rt6_exceptions_remove_prefsrc(rt); 332360006a48SWei Wang spin_unlock_bh(&rt6_exception_lock); 3324c3968a85SDaniel Walter } 3325c3968a85SDaniel Walter return 0; 3326c3968a85SDaniel Walter } 3327c3968a85SDaniel Walter 3328c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 3329c3968a85SDaniel Walter { 3330c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 3331c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 3332c3968a85SDaniel Walter .dev = ifp->idev->dev, 3333c3968a85SDaniel Walter .net = net, 3334c3968a85SDaniel Walter .addr = &ifp->addr, 3335c3968a85SDaniel Walter }; 33360c3584d5SLi RongQing fib6_clean_all(net, fib6_remove_prefsrc, &adni); 3337c3968a85SDaniel Walter } 3338c3968a85SDaniel Walter 3339be7a010dSDuan Jiong #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY) 3340be7a010dSDuan Jiong 3341be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */ 3342be7a010dSDuan Jiong static int fib6_clean_tohost(struct rt6_info *rt, void *arg) 3343be7a010dSDuan Jiong { 3344be7a010dSDuan Jiong struct in6_addr *gateway = (struct in6_addr *)arg; 3345be7a010dSDuan Jiong 3346b16cb459SWei Wang /* RTF_CACHE_GATEWAY case will be removed once the exception 3347b16cb459SWei Wang * table is hooked up to store all cached routes. 3348b16cb459SWei Wang */ 3349be7a010dSDuan Jiong if ((((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) || 3350be7a010dSDuan Jiong ((rt->rt6i_flags & RTF_CACHE_GATEWAY) == RTF_CACHE_GATEWAY)) && 3351be7a010dSDuan Jiong ipv6_addr_equal(gateway, &rt->rt6i_gateway)) { 3352be7a010dSDuan Jiong return -1; 3353be7a010dSDuan Jiong } 3354b16cb459SWei Wang 3355b16cb459SWei Wang /* Further clean up cached routes in exception table. 3356b16cb459SWei Wang * This is needed because cached route may have a different 3357b16cb459SWei Wang * gateway than its 'parent' in the case of an ip redirect. 3358b16cb459SWei Wang */ 3359b16cb459SWei Wang rt6_exceptions_clean_tohost(rt, gateway); 3360b16cb459SWei Wang 3361be7a010dSDuan Jiong return 0; 3362be7a010dSDuan Jiong } 3363be7a010dSDuan Jiong 3364be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) 3365be7a010dSDuan Jiong { 3366be7a010dSDuan Jiong fib6_clean_all(net, fib6_clean_tohost, gateway); 3367be7a010dSDuan Jiong } 3368be7a010dSDuan Jiong 33698ed67789SDaniel Lezcano struct arg_dev_net { 33708ed67789SDaniel Lezcano struct net_device *dev; 33718ed67789SDaniel Lezcano struct net *net; 33728ed67789SDaniel Lezcano }; 33738ed67789SDaniel Lezcano 3374a1a22c12SDavid Ahern /* called with write lock held for table with rt */ 33751da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg) 33761da177e4SLinus Torvalds { 3377bc3ef660Sstephen hemminger const struct arg_dev_net *adn = arg; 3378bc3ef660Sstephen hemminger const struct net_device *dev = adn->dev; 33798ed67789SDaniel Lezcano 3380d1918542SDavid S. Miller if ((rt->dst.dev == dev || !dev) && 3381a1a22c12SDavid Ahern rt != adn->net->ipv6.ip6_null_entry && 3382a1a22c12SDavid Ahern (rt->rt6i_nsiblings == 0 || 33838397ed36SDavid Ahern (dev && netdev_unregistering(dev)) || 3384a1a22c12SDavid Ahern !rt->rt6i_idev->cnf.ignore_routes_with_linkdown)) 33851da177e4SLinus Torvalds return -1; 3386c159d30cSDavid S. Miller 33871da177e4SLinus Torvalds return 0; 33881da177e4SLinus Torvalds } 33891da177e4SLinus Torvalds 3390f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev) 33911da177e4SLinus Torvalds { 33928ed67789SDaniel Lezcano struct arg_dev_net adn = { 33938ed67789SDaniel Lezcano .dev = dev, 33948ed67789SDaniel Lezcano .net = net, 33958ed67789SDaniel Lezcano }; 33968ed67789SDaniel Lezcano 33970c3584d5SLi RongQing fib6_clean_all(net, fib6_ifdown, &adn); 3398e332bc67SEric W. Biederman if (dev) 33998d0b94afSMartin KaFai Lau rt6_uncached_list_flush_dev(net, dev); 34001da177e4SLinus Torvalds } 34011da177e4SLinus Torvalds 340295c96174SEric Dumazet struct rt6_mtu_change_arg { 34031da177e4SLinus Torvalds struct net_device *dev; 340495c96174SEric Dumazet unsigned int mtu; 34051da177e4SLinus Torvalds }; 34061da177e4SLinus Torvalds 34071da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg) 34081da177e4SLinus Torvalds { 34091da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 34101da177e4SLinus Torvalds struct inet6_dev *idev; 34111da177e4SLinus Torvalds 34121da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 34131da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 34141da177e4SLinus Torvalds We still use this lock to block changes 34151da177e4SLinus Torvalds caused by addrconf/ndisc. 34161da177e4SLinus Torvalds */ 34171da177e4SLinus Torvalds 34181da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 341938308473SDavid S. Miller if (!idev) 34201da177e4SLinus Torvalds return 0; 34211da177e4SLinus Torvalds 34221da177e4SLinus Torvalds /* For administrative MTU increase, there is no way to discover 34231da177e4SLinus Torvalds IPv6 PMTU increase, so PMTU increase should be updated here. 34241da177e4SLinus Torvalds Since RFC 1981 doesn't include administrative MTU increase 34251da177e4SLinus Torvalds update PMTU increase is a MUST. (i.e. jumbo frame) 34261da177e4SLinus Torvalds */ 34271da177e4SLinus Torvalds /* 34281da177e4SLinus Torvalds If new MTU is less than route PMTU, this new MTU will be the 34291da177e4SLinus Torvalds lowest MTU in the path, update the route PMTU to reflect PMTU 34301da177e4SLinus Torvalds decreases; if new MTU is greater than route PMTU, and the 34311da177e4SLinus Torvalds old MTU is the lowest MTU in the path, update the route PMTU 34321da177e4SLinus Torvalds to reflect the increase. In this case if the other nodes' MTU 34331da177e4SLinus Torvalds also have the lowest MTU, TOO BIG MESSAGE will be lead to 343467c408cfSAlexander Alemayhu PMTU discovery. 34351da177e4SLinus Torvalds */ 3436d1918542SDavid S. Miller if (rt->dst.dev == arg->dev && 3437fb56be83SMaciej Żenczykowski dst_metric_raw(&rt->dst, RTAX_MTU) && 34384b32b5adSMartin KaFai Lau !dst_metric_locked(&rt->dst, RTAX_MTU)) { 3439f5bbe7eeSWei Wang spin_lock_bh(&rt6_exception_lock); 3440f5bbe7eeSWei Wang /* This case will be removed once the exception table 3441f5bbe7eeSWei Wang * is hooked up. 3442f5bbe7eeSWei Wang */ 34434b32b5adSMartin KaFai Lau if (rt->rt6i_flags & RTF_CACHE) { 34444b32b5adSMartin KaFai Lau /* For RTF_CACHE with rt6i_pmtu == 0 34454b32b5adSMartin KaFai Lau * (i.e. a redirected route), 34464b32b5adSMartin KaFai Lau * the metrics of its rt->dst.from has already 34474b32b5adSMartin KaFai Lau * been updated. 34484b32b5adSMartin KaFai Lau */ 34494b32b5adSMartin KaFai Lau if (rt->rt6i_pmtu && rt->rt6i_pmtu > arg->mtu) 34504b32b5adSMartin KaFai Lau rt->rt6i_pmtu = arg->mtu; 34514b32b5adSMartin KaFai Lau } else if (dst_mtu(&rt->dst) >= arg->mtu || 3452d8d1f30bSChangli Gao (dst_mtu(&rt->dst) < arg->mtu && 34534b32b5adSMartin KaFai Lau dst_mtu(&rt->dst) == idev->cnf.mtu6)) { 3454defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu); 3455566cfd8fSSimon Arlott } 3456f5bbe7eeSWei Wang rt6_exceptions_update_pmtu(rt, arg->mtu); 3457f5bbe7eeSWei Wang spin_unlock_bh(&rt6_exception_lock); 34584b32b5adSMartin KaFai Lau } 34591da177e4SLinus Torvalds return 0; 34601da177e4SLinus Torvalds } 34611da177e4SLinus Torvalds 346295c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu) 34631da177e4SLinus Torvalds { 3464c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 3465c71099acSThomas Graf .dev = dev, 3466c71099acSThomas Graf .mtu = mtu, 3467c71099acSThomas Graf }; 34681da177e4SLinus Torvalds 34690c3584d5SLi RongQing fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); 34701da177e4SLinus Torvalds } 34711da177e4SLinus Torvalds 3472ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 34735176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 347486872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 3475ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 347686872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 347786872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 347851ebd318SNicolas Dichtel [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 3479c78ba6d6SLubomir Rintel [RTA_PREF] = { .type = NLA_U8 }, 348019e42e45SRoopa Prabhu [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, 348119e42e45SRoopa Prabhu [RTA_ENCAP] = { .type = NLA_NESTED }, 348232bc201eSXin Long [RTA_EXPIRES] = { .type = NLA_U32 }, 3483622ec2c9SLorenzo Colitti [RTA_UID] = { .type = NLA_U32 }, 34843b45a410SLiping Zhang [RTA_MARK] = { .type = NLA_U32 }, 348586872cb5SThomas Graf }; 348686872cb5SThomas Graf 348786872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 3488333c4301SDavid Ahern struct fib6_config *cfg, 3489333c4301SDavid Ahern struct netlink_ext_ack *extack) 34901da177e4SLinus Torvalds { 349186872cb5SThomas Graf struct rtmsg *rtm; 349286872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 3493c78ba6d6SLubomir Rintel unsigned int pref; 349486872cb5SThomas Graf int err; 34951da177e4SLinus Torvalds 3496fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 3497fceb6435SJohannes Berg NULL); 349886872cb5SThomas Graf if (err < 0) 349986872cb5SThomas Graf goto errout; 35001da177e4SLinus Torvalds 350186872cb5SThomas Graf err = -EINVAL; 350286872cb5SThomas Graf rtm = nlmsg_data(nlh); 350386872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 350486872cb5SThomas Graf 350586872cb5SThomas Graf cfg->fc_table = rtm->rtm_table; 350686872cb5SThomas Graf cfg->fc_dst_len = rtm->rtm_dst_len; 350786872cb5SThomas Graf cfg->fc_src_len = rtm->rtm_src_len; 350886872cb5SThomas Graf cfg->fc_flags = RTF_UP; 350986872cb5SThomas Graf cfg->fc_protocol = rtm->rtm_protocol; 3510ef2c7d7bSNicolas Dichtel cfg->fc_type = rtm->rtm_type; 351186872cb5SThomas Graf 3512ef2c7d7bSNicolas Dichtel if (rtm->rtm_type == RTN_UNREACHABLE || 3513ef2c7d7bSNicolas Dichtel rtm->rtm_type == RTN_BLACKHOLE || 3514b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_PROHIBIT || 3515b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_THROW) 351686872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 351786872cb5SThomas Graf 3518ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 3519ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 3520ab79ad14SMaciej Żenczykowski 35211f56a01fSMartin KaFai Lau if (rtm->rtm_flags & RTM_F_CLONED) 35221f56a01fSMartin KaFai Lau cfg->fc_flags |= RTF_CACHE; 35231f56a01fSMartin KaFai Lau 352415e47304SEric W. Biederman cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid; 352586872cb5SThomas Graf cfg->fc_nlinfo.nlh = nlh; 35263b1e0a65SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = sock_net(skb->sk); 352786872cb5SThomas Graf 352886872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 352967b61f6cSJiri Benc cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); 353086872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 35311da177e4SLinus Torvalds } 353286872cb5SThomas Graf 353386872cb5SThomas Graf if (tb[RTA_DST]) { 353486872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 353586872cb5SThomas Graf 353686872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 353786872cb5SThomas Graf goto errout; 353886872cb5SThomas Graf 353986872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 35401da177e4SLinus Torvalds } 354186872cb5SThomas Graf 354286872cb5SThomas Graf if (tb[RTA_SRC]) { 354386872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 354486872cb5SThomas Graf 354586872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 354686872cb5SThomas Graf goto errout; 354786872cb5SThomas Graf 354886872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 35491da177e4SLinus Torvalds } 355086872cb5SThomas Graf 3551c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 355267b61f6cSJiri Benc cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); 3553c3968a85SDaniel Walter 355486872cb5SThomas Graf if (tb[RTA_OIF]) 355586872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 355686872cb5SThomas Graf 355786872cb5SThomas Graf if (tb[RTA_PRIORITY]) 355886872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 355986872cb5SThomas Graf 356086872cb5SThomas Graf if (tb[RTA_METRICS]) { 356186872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 356286872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 35631da177e4SLinus Torvalds } 356486872cb5SThomas Graf 356586872cb5SThomas Graf if (tb[RTA_TABLE]) 356686872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 356786872cb5SThomas Graf 356851ebd318SNicolas Dichtel if (tb[RTA_MULTIPATH]) { 356951ebd318SNicolas Dichtel cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); 357051ebd318SNicolas Dichtel cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); 35719ed59592SDavid Ahern 35729ed59592SDavid Ahern err = lwtunnel_valid_encap_type_attr(cfg->fc_mp, 3573c255bd68SDavid Ahern cfg->fc_mp_len, extack); 35749ed59592SDavid Ahern if (err < 0) 35759ed59592SDavid Ahern goto errout; 357651ebd318SNicolas Dichtel } 357751ebd318SNicolas Dichtel 3578c78ba6d6SLubomir Rintel if (tb[RTA_PREF]) { 3579c78ba6d6SLubomir Rintel pref = nla_get_u8(tb[RTA_PREF]); 3580c78ba6d6SLubomir Rintel if (pref != ICMPV6_ROUTER_PREF_LOW && 3581c78ba6d6SLubomir Rintel pref != ICMPV6_ROUTER_PREF_HIGH) 3582c78ba6d6SLubomir Rintel pref = ICMPV6_ROUTER_PREF_MEDIUM; 3583c78ba6d6SLubomir Rintel cfg->fc_flags |= RTF_PREF(pref); 3584c78ba6d6SLubomir Rintel } 3585c78ba6d6SLubomir Rintel 358619e42e45SRoopa Prabhu if (tb[RTA_ENCAP]) 358719e42e45SRoopa Prabhu cfg->fc_encap = tb[RTA_ENCAP]; 358819e42e45SRoopa Prabhu 35899ed59592SDavid Ahern if (tb[RTA_ENCAP_TYPE]) { 359019e42e45SRoopa Prabhu cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); 359119e42e45SRoopa Prabhu 3592c255bd68SDavid Ahern err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); 35939ed59592SDavid Ahern if (err < 0) 35949ed59592SDavid Ahern goto errout; 35959ed59592SDavid Ahern } 35969ed59592SDavid Ahern 359732bc201eSXin Long if (tb[RTA_EXPIRES]) { 359832bc201eSXin Long unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); 359932bc201eSXin Long 360032bc201eSXin Long if (addrconf_finite_timeout(timeout)) { 360132bc201eSXin Long cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); 360232bc201eSXin Long cfg->fc_flags |= RTF_EXPIRES; 360332bc201eSXin Long } 360432bc201eSXin Long } 360532bc201eSXin Long 360686872cb5SThomas Graf err = 0; 360786872cb5SThomas Graf errout: 360886872cb5SThomas Graf return err; 36091da177e4SLinus Torvalds } 36101da177e4SLinus Torvalds 36116b9ea5a6SRoopa Prabhu struct rt6_nh { 36126b9ea5a6SRoopa Prabhu struct rt6_info *rt6_info; 36136b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 36146b9ea5a6SRoopa Prabhu struct mx6_config mxc; 36156b9ea5a6SRoopa Prabhu struct list_head next; 36166b9ea5a6SRoopa Prabhu }; 36176b9ea5a6SRoopa Prabhu 36186b9ea5a6SRoopa Prabhu static void ip6_print_replace_route_err(struct list_head *rt6_nh_list) 36196b9ea5a6SRoopa Prabhu { 36206b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 36216b9ea5a6SRoopa Prabhu 36226b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 36237d4d5065SDavid Ahern pr_warn("IPV6: multipath route replace failed (check consistency of installed routes): %pI6c nexthop %pI6c ifi %d\n", 36246b9ea5a6SRoopa Prabhu &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway, 36256b9ea5a6SRoopa Prabhu nh->r_cfg.fc_ifindex); 36266b9ea5a6SRoopa Prabhu } 36276b9ea5a6SRoopa Prabhu } 36286b9ea5a6SRoopa Prabhu 36296b9ea5a6SRoopa Prabhu static int ip6_route_info_append(struct list_head *rt6_nh_list, 36306b9ea5a6SRoopa Prabhu struct rt6_info *rt, struct fib6_config *r_cfg) 36316b9ea5a6SRoopa Prabhu { 36326b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 36336b9ea5a6SRoopa Prabhu int err = -EEXIST; 36346b9ea5a6SRoopa Prabhu 36356b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 36366b9ea5a6SRoopa Prabhu /* check if rt6_info already exists */ 3637f06b7549SDavid Ahern if (rt6_duplicate_nexthop(nh->rt6_info, rt)) 36386b9ea5a6SRoopa Prabhu return err; 36396b9ea5a6SRoopa Prabhu } 36406b9ea5a6SRoopa Prabhu 36416b9ea5a6SRoopa Prabhu nh = kzalloc(sizeof(*nh), GFP_KERNEL); 36426b9ea5a6SRoopa Prabhu if (!nh) 36436b9ea5a6SRoopa Prabhu return -ENOMEM; 36446b9ea5a6SRoopa Prabhu nh->rt6_info = rt; 36456b9ea5a6SRoopa Prabhu err = ip6_convert_metrics(&nh->mxc, r_cfg); 36466b9ea5a6SRoopa Prabhu if (err) { 36476b9ea5a6SRoopa Prabhu kfree(nh); 36486b9ea5a6SRoopa Prabhu return err; 36496b9ea5a6SRoopa Prabhu } 36506b9ea5a6SRoopa Prabhu memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); 36516b9ea5a6SRoopa Prabhu list_add_tail(&nh->next, rt6_nh_list); 36526b9ea5a6SRoopa Prabhu 36536b9ea5a6SRoopa Prabhu return 0; 36546b9ea5a6SRoopa Prabhu } 36556b9ea5a6SRoopa Prabhu 36563b1137feSDavid Ahern static void ip6_route_mpath_notify(struct rt6_info *rt, 36573b1137feSDavid Ahern struct rt6_info *rt_last, 36583b1137feSDavid Ahern struct nl_info *info, 36593b1137feSDavid Ahern __u16 nlflags) 36603b1137feSDavid Ahern { 36613b1137feSDavid Ahern /* if this is an APPEND route, then rt points to the first route 36623b1137feSDavid Ahern * inserted and rt_last points to last route inserted. Userspace 36633b1137feSDavid Ahern * wants a consistent dump of the route which starts at the first 36643b1137feSDavid Ahern * nexthop. Since sibling routes are always added at the end of 36653b1137feSDavid Ahern * the list, find the first sibling of the last route appended 36663b1137feSDavid Ahern */ 36673b1137feSDavid Ahern if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->rt6i_nsiblings) { 36683b1137feSDavid Ahern rt = list_first_entry(&rt_last->rt6i_siblings, 36693b1137feSDavid Ahern struct rt6_info, 36703b1137feSDavid Ahern rt6i_siblings); 36713b1137feSDavid Ahern } 36723b1137feSDavid Ahern 36733b1137feSDavid Ahern if (rt) 36743b1137feSDavid Ahern inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); 36753b1137feSDavid Ahern } 36763b1137feSDavid Ahern 3677333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg, 3678333c4301SDavid Ahern struct netlink_ext_ack *extack) 367951ebd318SNicolas Dichtel { 36803b1137feSDavid Ahern struct rt6_info *rt_notif = NULL, *rt_last = NULL; 36813b1137feSDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 368251ebd318SNicolas Dichtel struct fib6_config r_cfg; 368351ebd318SNicolas Dichtel struct rtnexthop *rtnh; 36846b9ea5a6SRoopa Prabhu struct rt6_info *rt; 36856b9ea5a6SRoopa Prabhu struct rt6_nh *err_nh; 36866b9ea5a6SRoopa Prabhu struct rt6_nh *nh, *nh_safe; 36873b1137feSDavid Ahern __u16 nlflags; 368851ebd318SNicolas Dichtel int remaining; 368951ebd318SNicolas Dichtel int attrlen; 36906b9ea5a6SRoopa Prabhu int err = 1; 36916b9ea5a6SRoopa Prabhu int nhn = 0; 36926b9ea5a6SRoopa Prabhu int replace = (cfg->fc_nlinfo.nlh && 36936b9ea5a6SRoopa Prabhu (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); 36946b9ea5a6SRoopa Prabhu LIST_HEAD(rt6_nh_list); 369551ebd318SNicolas Dichtel 36963b1137feSDavid Ahern nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; 36973b1137feSDavid Ahern if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) 36983b1137feSDavid Ahern nlflags |= NLM_F_APPEND; 36993b1137feSDavid Ahern 370035f1b4e9SMichal Kubeček remaining = cfg->fc_mp_len; 370151ebd318SNicolas Dichtel rtnh = (struct rtnexthop *)cfg->fc_mp; 370251ebd318SNicolas Dichtel 37036b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry and build a list (rt6_nh_list) of 37046b9ea5a6SRoopa Prabhu * rt6_info structs per nexthop 37056b9ea5a6SRoopa Prabhu */ 370651ebd318SNicolas Dichtel while (rtnh_ok(rtnh, remaining)) { 370751ebd318SNicolas Dichtel memcpy(&r_cfg, cfg, sizeof(*cfg)); 370851ebd318SNicolas Dichtel if (rtnh->rtnh_ifindex) 370951ebd318SNicolas Dichtel r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 371051ebd318SNicolas Dichtel 371151ebd318SNicolas Dichtel attrlen = rtnh_attrlen(rtnh); 371251ebd318SNicolas Dichtel if (attrlen > 0) { 371351ebd318SNicolas Dichtel struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 371451ebd318SNicolas Dichtel 371551ebd318SNicolas Dichtel nla = nla_find(attrs, attrlen, RTA_GATEWAY); 371651ebd318SNicolas Dichtel if (nla) { 371767b61f6cSJiri Benc r_cfg.fc_gateway = nla_get_in6_addr(nla); 371851ebd318SNicolas Dichtel r_cfg.fc_flags |= RTF_GATEWAY; 371951ebd318SNicolas Dichtel } 372019e42e45SRoopa Prabhu r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); 372119e42e45SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); 372219e42e45SRoopa Prabhu if (nla) 372319e42e45SRoopa Prabhu r_cfg.fc_encap_type = nla_get_u16(nla); 372451ebd318SNicolas Dichtel } 37256b9ea5a6SRoopa Prabhu 3726333c4301SDavid Ahern rt = ip6_route_info_create(&r_cfg, extack); 37278c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 37288c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 37298c5b83f0SRoopa Prabhu rt = NULL; 37306b9ea5a6SRoopa Prabhu goto cleanup; 37318c5b83f0SRoopa Prabhu } 37326b9ea5a6SRoopa Prabhu 37336b9ea5a6SRoopa Prabhu err = ip6_route_info_append(&rt6_nh_list, rt, &r_cfg); 373451ebd318SNicolas Dichtel if (err) { 3735587fea74SWei Wang dst_release_immediate(&rt->dst); 37366b9ea5a6SRoopa Prabhu goto cleanup; 373751ebd318SNicolas Dichtel } 37386b9ea5a6SRoopa Prabhu 37396b9ea5a6SRoopa Prabhu rtnh = rtnh_next(rtnh, &remaining); 374051ebd318SNicolas Dichtel } 37416b9ea5a6SRoopa Prabhu 37423b1137feSDavid Ahern /* for add and replace send one notification with all nexthops. 37433b1137feSDavid Ahern * Skip the notification in fib6_add_rt2node and send one with 37443b1137feSDavid Ahern * the full route when done 37453b1137feSDavid Ahern */ 37463b1137feSDavid Ahern info->skip_notify = 1; 37473b1137feSDavid Ahern 37486b9ea5a6SRoopa Prabhu err_nh = NULL; 37496b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 37503b1137feSDavid Ahern rt_last = nh->rt6_info; 3751333c4301SDavid Ahern err = __ip6_ins_rt(nh->rt6_info, info, &nh->mxc, extack); 37523b1137feSDavid Ahern /* save reference to first route for notification */ 37533b1137feSDavid Ahern if (!rt_notif && !err) 37543b1137feSDavid Ahern rt_notif = nh->rt6_info; 37553b1137feSDavid Ahern 37566b9ea5a6SRoopa Prabhu /* nh->rt6_info is used or freed at this point, reset to NULL*/ 37576b9ea5a6SRoopa Prabhu nh->rt6_info = NULL; 37586b9ea5a6SRoopa Prabhu if (err) { 37596b9ea5a6SRoopa Prabhu if (replace && nhn) 37606b9ea5a6SRoopa Prabhu ip6_print_replace_route_err(&rt6_nh_list); 37616b9ea5a6SRoopa Prabhu err_nh = nh; 37626b9ea5a6SRoopa Prabhu goto add_errout; 37636b9ea5a6SRoopa Prabhu } 37646b9ea5a6SRoopa Prabhu 37651a72418bSNicolas Dichtel /* Because each route is added like a single route we remove 376627596472SMichal Kubeček * these flags after the first nexthop: if there is a collision, 376727596472SMichal Kubeček * we have already failed to add the first nexthop: 376827596472SMichal Kubeček * fib6_add_rt2node() has rejected it; when replacing, old 376927596472SMichal Kubeček * nexthops have been replaced by first new, the rest should 377027596472SMichal Kubeček * be added to it. 37711a72418bSNicolas Dichtel */ 377227596472SMichal Kubeček cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | 377327596472SMichal Kubeček NLM_F_REPLACE); 37746b9ea5a6SRoopa Prabhu nhn++; 37756b9ea5a6SRoopa Prabhu } 37766b9ea5a6SRoopa Prabhu 37773b1137feSDavid Ahern /* success ... tell user about new route */ 37783b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 37796b9ea5a6SRoopa Prabhu goto cleanup; 37806b9ea5a6SRoopa Prabhu 37816b9ea5a6SRoopa Prabhu add_errout: 37823b1137feSDavid Ahern /* send notification for routes that were added so that 37833b1137feSDavid Ahern * the delete notifications sent by ip6_route_del are 37843b1137feSDavid Ahern * coherent 37853b1137feSDavid Ahern */ 37863b1137feSDavid Ahern if (rt_notif) 37873b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 37883b1137feSDavid Ahern 37896b9ea5a6SRoopa Prabhu /* Delete routes that were already added */ 37906b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 37916b9ea5a6SRoopa Prabhu if (err_nh == nh) 37926b9ea5a6SRoopa Prabhu break; 3793333c4301SDavid Ahern ip6_route_del(&nh->r_cfg, extack); 37946b9ea5a6SRoopa Prabhu } 37956b9ea5a6SRoopa Prabhu 37966b9ea5a6SRoopa Prabhu cleanup: 37976b9ea5a6SRoopa Prabhu list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) { 3798587fea74SWei Wang if (nh->rt6_info) 3799587fea74SWei Wang dst_release_immediate(&nh->rt6_info->dst); 38006b9ea5a6SRoopa Prabhu kfree(nh->mxc.mx); 38016b9ea5a6SRoopa Prabhu list_del(&nh->next); 38026b9ea5a6SRoopa Prabhu kfree(nh); 38036b9ea5a6SRoopa Prabhu } 38046b9ea5a6SRoopa Prabhu 38056b9ea5a6SRoopa Prabhu return err; 38066b9ea5a6SRoopa Prabhu } 38076b9ea5a6SRoopa Prabhu 3808333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg, 3809333c4301SDavid Ahern struct netlink_ext_ack *extack) 38106b9ea5a6SRoopa Prabhu { 38116b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 38126b9ea5a6SRoopa Prabhu struct rtnexthop *rtnh; 38136b9ea5a6SRoopa Prabhu int remaining; 38146b9ea5a6SRoopa Prabhu int attrlen; 38156b9ea5a6SRoopa Prabhu int err = 1, last_err = 0; 38166b9ea5a6SRoopa Prabhu 38176b9ea5a6SRoopa Prabhu remaining = cfg->fc_mp_len; 38186b9ea5a6SRoopa Prabhu rtnh = (struct rtnexthop *)cfg->fc_mp; 38196b9ea5a6SRoopa Prabhu 38206b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry */ 38216b9ea5a6SRoopa Prabhu while (rtnh_ok(rtnh, remaining)) { 38226b9ea5a6SRoopa Prabhu memcpy(&r_cfg, cfg, sizeof(*cfg)); 38236b9ea5a6SRoopa Prabhu if (rtnh->rtnh_ifindex) 38246b9ea5a6SRoopa Prabhu r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 38256b9ea5a6SRoopa Prabhu 38266b9ea5a6SRoopa Prabhu attrlen = rtnh_attrlen(rtnh); 38276b9ea5a6SRoopa Prabhu if (attrlen > 0) { 38286b9ea5a6SRoopa Prabhu struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 38296b9ea5a6SRoopa Prabhu 38306b9ea5a6SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_GATEWAY); 38316b9ea5a6SRoopa Prabhu if (nla) { 38326b9ea5a6SRoopa Prabhu nla_memcpy(&r_cfg.fc_gateway, nla, 16); 38336b9ea5a6SRoopa Prabhu r_cfg.fc_flags |= RTF_GATEWAY; 38346b9ea5a6SRoopa Prabhu } 38356b9ea5a6SRoopa Prabhu } 3836333c4301SDavid Ahern err = ip6_route_del(&r_cfg, extack); 38376b9ea5a6SRoopa Prabhu if (err) 38386b9ea5a6SRoopa Prabhu last_err = err; 38396b9ea5a6SRoopa Prabhu 384051ebd318SNicolas Dichtel rtnh = rtnh_next(rtnh, &remaining); 384151ebd318SNicolas Dichtel } 384251ebd318SNicolas Dichtel 384351ebd318SNicolas Dichtel return last_err; 384451ebd318SNicolas Dichtel } 384551ebd318SNicolas Dichtel 3846c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, 3847c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 38481da177e4SLinus Torvalds { 384986872cb5SThomas Graf struct fib6_config cfg; 385086872cb5SThomas Graf int err; 38511da177e4SLinus Torvalds 3852333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 385386872cb5SThomas Graf if (err < 0) 385486872cb5SThomas Graf return err; 385586872cb5SThomas Graf 385651ebd318SNicolas Dichtel if (cfg.fc_mp) 3857333c4301SDavid Ahern return ip6_route_multipath_del(&cfg, extack); 38580ae81335SDavid Ahern else { 38590ae81335SDavid Ahern cfg.fc_delete_all_nh = 1; 3860333c4301SDavid Ahern return ip6_route_del(&cfg, extack); 38611da177e4SLinus Torvalds } 38620ae81335SDavid Ahern } 38631da177e4SLinus Torvalds 3864c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, 3865c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 38661da177e4SLinus Torvalds { 386786872cb5SThomas Graf struct fib6_config cfg; 386886872cb5SThomas Graf int err; 38691da177e4SLinus Torvalds 3870333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 387186872cb5SThomas Graf if (err < 0) 387286872cb5SThomas Graf return err; 387386872cb5SThomas Graf 387451ebd318SNicolas Dichtel if (cfg.fc_mp) 3875333c4301SDavid Ahern return ip6_route_multipath_add(&cfg, extack); 387651ebd318SNicolas Dichtel else 3877333c4301SDavid Ahern return ip6_route_add(&cfg, extack); 38781da177e4SLinus Torvalds } 38791da177e4SLinus Torvalds 3880beb1afacSDavid Ahern static size_t rt6_nlmsg_size(struct rt6_info *rt) 3881339bf98fSThomas Graf { 3882beb1afacSDavid Ahern int nexthop_len = 0; 3883beb1afacSDavid Ahern 3884beb1afacSDavid Ahern if (rt->rt6i_nsiblings) { 3885beb1afacSDavid Ahern nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */ 3886beb1afacSDavid Ahern + NLA_ALIGN(sizeof(struct rtnexthop)) 3887beb1afacSDavid Ahern + nla_total_size(16) /* RTA_GATEWAY */ 3888beb1afacSDavid Ahern + lwtunnel_get_encap_size(rt->dst.lwtstate); 3889beb1afacSDavid Ahern 3890beb1afacSDavid Ahern nexthop_len *= rt->rt6i_nsiblings; 3891beb1afacSDavid Ahern } 3892beb1afacSDavid Ahern 3893339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 3894339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 3895339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 3896339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 3897339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 3898339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 3899339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 3900339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 3901339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 39026a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 3903ea697639SDaniel Borkmann + nla_total_size(sizeof(struct rta_cacheinfo)) 3904c78ba6d6SLubomir Rintel + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ 390519e42e45SRoopa Prabhu + nla_total_size(1) /* RTA_PREF */ 3906beb1afacSDavid Ahern + lwtunnel_get_encap_size(rt->dst.lwtstate) 3907beb1afacSDavid Ahern + nexthop_len; 3908beb1afacSDavid Ahern } 3909beb1afacSDavid Ahern 3910beb1afacSDavid Ahern static int rt6_nexthop_info(struct sk_buff *skb, struct rt6_info *rt, 39115be083ceSDavid Ahern unsigned int *flags, bool skip_oif) 3912beb1afacSDavid Ahern { 3913beb1afacSDavid Ahern if (!netif_running(rt->dst.dev) || !netif_carrier_ok(rt->dst.dev)) { 3914beb1afacSDavid Ahern *flags |= RTNH_F_LINKDOWN; 3915beb1afacSDavid Ahern if (rt->rt6i_idev->cnf.ignore_routes_with_linkdown) 3916beb1afacSDavid Ahern *flags |= RTNH_F_DEAD; 3917beb1afacSDavid Ahern } 3918beb1afacSDavid Ahern 3919beb1afacSDavid Ahern if (rt->rt6i_flags & RTF_GATEWAY) { 3920beb1afacSDavid Ahern if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0) 3921beb1afacSDavid Ahern goto nla_put_failure; 3922beb1afacSDavid Ahern } 3923beb1afacSDavid Ahern 3924fe400799SIdo Schimmel if (rt->rt6i_nh_flags & RTNH_F_OFFLOAD) 392561e4d01eSIdo Schimmel *flags |= RTNH_F_OFFLOAD; 392661e4d01eSIdo Schimmel 39275be083ceSDavid Ahern /* not needed for multipath encoding b/c it has a rtnexthop struct */ 39285be083ceSDavid Ahern if (!skip_oif && rt->dst.dev && 3929beb1afacSDavid Ahern nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex)) 3930beb1afacSDavid Ahern goto nla_put_failure; 3931beb1afacSDavid Ahern 3932beb1afacSDavid Ahern if (rt->dst.lwtstate && 3933beb1afacSDavid Ahern lwtunnel_fill_encap(skb, rt->dst.lwtstate) < 0) 3934beb1afacSDavid Ahern goto nla_put_failure; 3935beb1afacSDavid Ahern 3936beb1afacSDavid Ahern return 0; 3937beb1afacSDavid Ahern 3938beb1afacSDavid Ahern nla_put_failure: 3939beb1afacSDavid Ahern return -EMSGSIZE; 3940beb1afacSDavid Ahern } 3941beb1afacSDavid Ahern 39425be083ceSDavid Ahern /* add multipath next hop */ 3943beb1afacSDavid Ahern static int rt6_add_nexthop(struct sk_buff *skb, struct rt6_info *rt) 3944beb1afacSDavid Ahern { 3945beb1afacSDavid Ahern struct rtnexthop *rtnh; 3946beb1afacSDavid Ahern unsigned int flags = 0; 3947beb1afacSDavid Ahern 3948beb1afacSDavid Ahern rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh)); 3949beb1afacSDavid Ahern if (!rtnh) 3950beb1afacSDavid Ahern goto nla_put_failure; 3951beb1afacSDavid Ahern 3952beb1afacSDavid Ahern rtnh->rtnh_hops = 0; 3953beb1afacSDavid Ahern rtnh->rtnh_ifindex = rt->dst.dev ? rt->dst.dev->ifindex : 0; 3954beb1afacSDavid Ahern 39555be083ceSDavid Ahern if (rt6_nexthop_info(skb, rt, &flags, true) < 0) 3956beb1afacSDavid Ahern goto nla_put_failure; 3957beb1afacSDavid Ahern 3958beb1afacSDavid Ahern rtnh->rtnh_flags = flags; 3959beb1afacSDavid Ahern 3960beb1afacSDavid Ahern /* length of rtnetlink header + attributes */ 3961beb1afacSDavid Ahern rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh; 3962beb1afacSDavid Ahern 3963beb1afacSDavid Ahern return 0; 3964beb1afacSDavid Ahern 3965beb1afacSDavid Ahern nla_put_failure: 3966beb1afacSDavid Ahern return -EMSGSIZE; 3967339bf98fSThomas Graf } 3968339bf98fSThomas Graf 3969191cd582SBrian Haley static int rt6_fill_node(struct net *net, 3970191cd582SBrian Haley struct sk_buff *skb, struct rt6_info *rt, 39710d51aa80SJamal Hadi Salim struct in6_addr *dst, struct in6_addr *src, 397215e47304SEric W. Biederman int iif, int type, u32 portid, u32 seq, 3973f8cfe2ceSDavid Ahern unsigned int flags) 39741da177e4SLinus Torvalds { 39754b32b5adSMartin KaFai Lau u32 metrics[RTAX_MAX]; 39761da177e4SLinus Torvalds struct rtmsg *rtm; 39771da177e4SLinus Torvalds struct nlmsghdr *nlh; 3978e3703b3dSThomas Graf long expires; 39799e762a4aSPatrick McHardy u32 table; 39801da177e4SLinus Torvalds 398115e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); 398238308473SDavid S. Miller if (!nlh) 398326932566SPatrick McHardy return -EMSGSIZE; 39842d7202bfSThomas Graf 39852d7202bfSThomas Graf rtm = nlmsg_data(nlh); 39861da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 39871da177e4SLinus Torvalds rtm->rtm_dst_len = rt->rt6i_dst.plen; 39881da177e4SLinus Torvalds rtm->rtm_src_len = rt->rt6i_src.plen; 39891da177e4SLinus Torvalds rtm->rtm_tos = 0; 3990c71099acSThomas Graf if (rt->rt6i_table) 39919e762a4aSPatrick McHardy table = rt->rt6i_table->tb6_id; 3992c71099acSThomas Graf else 39939e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 39949e762a4aSPatrick McHardy rtm->rtm_table = table; 3995c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, table)) 3996c78679e8SDavid S. Miller goto nla_put_failure; 3997ef2c7d7bSNicolas Dichtel if (rt->rt6i_flags & RTF_REJECT) { 3998ef2c7d7bSNicolas Dichtel switch (rt->dst.error) { 3999ef2c7d7bSNicolas Dichtel case -EINVAL: 4000ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_BLACKHOLE; 4001ef2c7d7bSNicolas Dichtel break; 4002ef2c7d7bSNicolas Dichtel case -EACCES: 4003ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_PROHIBIT; 4004ef2c7d7bSNicolas Dichtel break; 4005b4949ab2SNicolas Dichtel case -EAGAIN: 4006b4949ab2SNicolas Dichtel rtm->rtm_type = RTN_THROW; 4007b4949ab2SNicolas Dichtel break; 4008ef2c7d7bSNicolas Dichtel default: 40091da177e4SLinus Torvalds rtm->rtm_type = RTN_UNREACHABLE; 4010ef2c7d7bSNicolas Dichtel break; 4011ef2c7d7bSNicolas Dichtel } 4012ef2c7d7bSNicolas Dichtel } 4013ab79ad14SMaciej Żenczykowski else if (rt->rt6i_flags & RTF_LOCAL) 4014ab79ad14SMaciej Żenczykowski rtm->rtm_type = RTN_LOCAL; 40154ee39733SDavid Ahern else if (rt->rt6i_flags & RTF_ANYCAST) 40164ee39733SDavid Ahern rtm->rtm_type = RTN_ANYCAST; 4017d1918542SDavid S. Miller else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK)) 40181da177e4SLinus Torvalds rtm->rtm_type = RTN_LOCAL; 40191da177e4SLinus Torvalds else 40201da177e4SLinus Torvalds rtm->rtm_type = RTN_UNICAST; 40211da177e4SLinus Torvalds rtm->rtm_flags = 0; 40221da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 40231da177e4SLinus Torvalds rtm->rtm_protocol = rt->rt6i_protocol; 40241da177e4SLinus Torvalds 40251da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) 40261da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 40271da177e4SLinus Torvalds 40281da177e4SLinus Torvalds if (dst) { 4029930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_DST, dst)) 4030c78679e8SDavid S. Miller goto nla_put_failure; 40311da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 40321da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 4033930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr)) 4034c78679e8SDavid S. Miller goto nla_put_failure; 40351da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 40361da177e4SLinus Torvalds if (src) { 4037930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_SRC, src)) 4038c78679e8SDavid S. Miller goto nla_put_failure; 40391da177e4SLinus Torvalds rtm->rtm_src_len = 128; 4040c78679e8SDavid S. Miller } else if (rtm->rtm_src_len && 4041930345eaSJiri Benc nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr)) 4042c78679e8SDavid S. Miller goto nla_put_failure; 40431da177e4SLinus Torvalds #endif 40447bc570c8SYOSHIFUJI Hideaki if (iif) { 40457bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 40467bc570c8SYOSHIFUJI Hideaki if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) { 4047fd61c6baSDavid Ahern int err = ip6mr_get_route(net, skb, rtm, portid); 40482cf75070SNikolay Aleksandrov 40497bc570c8SYOSHIFUJI Hideaki if (err == 0) 40507bc570c8SYOSHIFUJI Hideaki return 0; 4051fd61c6baSDavid Ahern if (err < 0) 40527bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 40537bc570c8SYOSHIFUJI Hideaki } else 40547bc570c8SYOSHIFUJI Hideaki #endif 4055c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_IIF, iif)) 4056c78679e8SDavid S. Miller goto nla_put_failure; 40577bc570c8SYOSHIFUJI Hideaki } else if (dst) { 40581da177e4SLinus Torvalds struct in6_addr saddr_buf; 4059c78679e8SDavid S. Miller if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 && 4060930345eaSJiri Benc nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 4061c78679e8SDavid S. Miller goto nla_put_failure; 4062c3968a85SDaniel Walter } 4063c3968a85SDaniel Walter 4064c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) { 4065c3968a85SDaniel Walter struct in6_addr saddr_buf; 40664e3fd7a0SAlexey Dobriyan saddr_buf = rt->rt6i_prefsrc.addr; 4067930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 4068c78679e8SDavid S. Miller goto nla_put_failure; 40691da177e4SLinus Torvalds } 40702d7202bfSThomas Graf 40714b32b5adSMartin KaFai Lau memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics)); 40724b32b5adSMartin KaFai Lau if (rt->rt6i_pmtu) 40734b32b5adSMartin KaFai Lau metrics[RTAX_MTU - 1] = rt->rt6i_pmtu; 40744b32b5adSMartin KaFai Lau if (rtnetlink_put_metrics(skb, metrics) < 0) 40752d7202bfSThomas Graf goto nla_put_failure; 40762d7202bfSThomas Graf 4077beb1afacSDavid Ahern if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric)) 4078beb1afacSDavid Ahern goto nla_put_failure; 4079beb1afacSDavid Ahern 4080beb1afacSDavid Ahern /* For multipath routes, walk the siblings list and add 4081beb1afacSDavid Ahern * each as a nexthop within RTA_MULTIPATH. 4082beb1afacSDavid Ahern */ 4083beb1afacSDavid Ahern if (rt->rt6i_nsiblings) { 4084beb1afacSDavid Ahern struct rt6_info *sibling, *next_sibling; 4085beb1afacSDavid Ahern struct nlattr *mp; 4086beb1afacSDavid Ahern 4087beb1afacSDavid Ahern mp = nla_nest_start(skb, RTA_MULTIPATH); 4088beb1afacSDavid Ahern if (!mp) 4089beb1afacSDavid Ahern goto nla_put_failure; 4090beb1afacSDavid Ahern 4091beb1afacSDavid Ahern if (rt6_add_nexthop(skb, rt) < 0) 4092beb1afacSDavid Ahern goto nla_put_failure; 4093beb1afacSDavid Ahern 4094beb1afacSDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 4095beb1afacSDavid Ahern &rt->rt6i_siblings, rt6i_siblings) { 4096beb1afacSDavid Ahern if (rt6_add_nexthop(skb, sibling) < 0) 409794f826b8SEric Dumazet goto nla_put_failure; 409894f826b8SEric Dumazet } 40992d7202bfSThomas Graf 4100beb1afacSDavid Ahern nla_nest_end(skb, mp); 4101beb1afacSDavid Ahern } else { 41025be083ceSDavid Ahern if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0) 4103c78679e8SDavid S. Miller goto nla_put_failure; 4104beb1afacSDavid Ahern } 41058253947eSLi Wei 41068253947eSLi Wei expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0; 410769cdf8f9SYOSHIFUJI Hideaki 410887a50699SDavid S. Miller if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0) 4109e3703b3dSThomas Graf goto nla_put_failure; 41101da177e4SLinus Torvalds 4111c78ba6d6SLubomir Rintel if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags))) 4112c78ba6d6SLubomir Rintel goto nla_put_failure; 4113c78ba6d6SLubomir Rintel 411419e42e45SRoopa Prabhu 4115053c095aSJohannes Berg nlmsg_end(skb, nlh); 4116053c095aSJohannes Berg return 0; 41172d7202bfSThomas Graf 41182d7202bfSThomas Graf nla_put_failure: 411926932566SPatrick McHardy nlmsg_cancel(skb, nlh); 412026932566SPatrick McHardy return -EMSGSIZE; 41211da177e4SLinus Torvalds } 41221da177e4SLinus Torvalds 41231b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg) 41241da177e4SLinus Torvalds { 41251da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 41261f17e2f2SDavid Ahern struct net *net = arg->net; 41271f17e2f2SDavid Ahern 41281f17e2f2SDavid Ahern if (rt == net->ipv6.ip6_null_entry) 41291f17e2f2SDavid Ahern return 0; 41301da177e4SLinus Torvalds 41312d7202bfSThomas Graf if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { 41322d7202bfSThomas Graf struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); 4133f8cfe2ceSDavid Ahern 4134f8cfe2ceSDavid Ahern /* user wants prefix routes only */ 4135f8cfe2ceSDavid Ahern if (rtm->rtm_flags & RTM_F_PREFIX && 4136f8cfe2ceSDavid Ahern !(rt->rt6i_flags & RTF_PREFIX_RT)) { 4137f8cfe2ceSDavid Ahern /* success since this is not a prefix route */ 4138f8cfe2ceSDavid Ahern return 1; 4139f8cfe2ceSDavid Ahern } 4140f8cfe2ceSDavid Ahern } 41411da177e4SLinus Torvalds 41421f17e2f2SDavid Ahern return rt6_fill_node(net, 4143191cd582SBrian Haley arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE, 414415e47304SEric W. Biederman NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq, 4145f8cfe2ceSDavid Ahern NLM_F_MULTI); 41461da177e4SLinus Torvalds } 41471da177e4SLinus Torvalds 4148c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, 4149c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 41501da177e4SLinus Torvalds { 41513b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 4152ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 415318c3a61cSRoopa Prabhu int err, iif = 0, oif = 0; 415418c3a61cSRoopa Prabhu struct dst_entry *dst; 41551da177e4SLinus Torvalds struct rt6_info *rt; 4156ab364a6fSThomas Graf struct sk_buff *skb; 4157ab364a6fSThomas Graf struct rtmsg *rtm; 41584c9483b2SDavid S. Miller struct flowi6 fl6; 415918c3a61cSRoopa Prabhu bool fibmatch; 4160ab364a6fSThomas Graf 4161fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 4162c21ef3e3SDavid Ahern extack); 4163ab364a6fSThomas Graf if (err < 0) 4164ab364a6fSThomas Graf goto errout; 4165ab364a6fSThomas Graf 4166ab364a6fSThomas Graf err = -EINVAL; 41674c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 416838b7097bSHannes Frederic Sowa rtm = nlmsg_data(nlh); 416938b7097bSHannes Frederic Sowa fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0); 417018c3a61cSRoopa Prabhu fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); 4171ab364a6fSThomas Graf 4172ab364a6fSThomas Graf if (tb[RTA_SRC]) { 4173ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 4174ab364a6fSThomas Graf goto errout; 4175ab364a6fSThomas Graf 41764e3fd7a0SAlexey Dobriyan fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 4177ab364a6fSThomas Graf } 4178ab364a6fSThomas Graf 4179ab364a6fSThomas Graf if (tb[RTA_DST]) { 4180ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 4181ab364a6fSThomas Graf goto errout; 4182ab364a6fSThomas Graf 41834e3fd7a0SAlexey Dobriyan fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 4184ab364a6fSThomas Graf } 4185ab364a6fSThomas Graf 4186ab364a6fSThomas Graf if (tb[RTA_IIF]) 4187ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 4188ab364a6fSThomas Graf 4189ab364a6fSThomas Graf if (tb[RTA_OIF]) 419072331bc0SShmulik Ladkani oif = nla_get_u32(tb[RTA_OIF]); 4191ab364a6fSThomas Graf 41922e47b291SLorenzo Colitti if (tb[RTA_MARK]) 41932e47b291SLorenzo Colitti fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); 41942e47b291SLorenzo Colitti 4195622ec2c9SLorenzo Colitti if (tb[RTA_UID]) 4196622ec2c9SLorenzo Colitti fl6.flowi6_uid = make_kuid(current_user_ns(), 4197622ec2c9SLorenzo Colitti nla_get_u32(tb[RTA_UID])); 4198622ec2c9SLorenzo Colitti else 4199622ec2c9SLorenzo Colitti fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); 4200622ec2c9SLorenzo Colitti 4201ab364a6fSThomas Graf if (iif) { 4202ab364a6fSThomas Graf struct net_device *dev; 420372331bc0SShmulik Ladkani int flags = 0; 420472331bc0SShmulik Ladkani 4205121622dbSFlorian Westphal rcu_read_lock(); 4206121622dbSFlorian Westphal 4207121622dbSFlorian Westphal dev = dev_get_by_index_rcu(net, iif); 4208ab364a6fSThomas Graf if (!dev) { 4209121622dbSFlorian Westphal rcu_read_unlock(); 4210ab364a6fSThomas Graf err = -ENODEV; 4211ab364a6fSThomas Graf goto errout; 4212ab364a6fSThomas Graf } 421372331bc0SShmulik Ladkani 421472331bc0SShmulik Ladkani fl6.flowi6_iif = iif; 421572331bc0SShmulik Ladkani 421672331bc0SShmulik Ladkani if (!ipv6_addr_any(&fl6.saddr)) 421772331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_HAS_SADDR; 421872331bc0SShmulik Ladkani 421918c3a61cSRoopa Prabhu if (!fibmatch) 422018c3a61cSRoopa Prabhu dst = ip6_route_input_lookup(net, dev, &fl6, flags); 4221401481e0SArnd Bergmann else 4222401481e0SArnd Bergmann dst = ip6_route_lookup(net, &fl6, 0); 4223121622dbSFlorian Westphal 4224121622dbSFlorian Westphal rcu_read_unlock(); 422572331bc0SShmulik Ladkani } else { 422672331bc0SShmulik Ladkani fl6.flowi6_oif = oif; 422772331bc0SShmulik Ladkani 422818c3a61cSRoopa Prabhu if (!fibmatch) 422918c3a61cSRoopa Prabhu dst = ip6_route_output(net, NULL, &fl6); 4230401481e0SArnd Bergmann else 4231401481e0SArnd Bergmann dst = ip6_route_lookup(net, &fl6, 0); 423218c3a61cSRoopa Prabhu } 423318c3a61cSRoopa Prabhu 423418c3a61cSRoopa Prabhu 423518c3a61cSRoopa Prabhu rt = container_of(dst, struct rt6_info, dst); 423618c3a61cSRoopa Prabhu if (rt->dst.error) { 423718c3a61cSRoopa Prabhu err = rt->dst.error; 423818c3a61cSRoopa Prabhu ip6_rt_put(rt); 423918c3a61cSRoopa Prabhu goto errout; 4240ab364a6fSThomas Graf } 42411da177e4SLinus Torvalds 42429d6acb3bSWANG Cong if (rt == net->ipv6.ip6_null_entry) { 42439d6acb3bSWANG Cong err = rt->dst.error; 42449d6acb3bSWANG Cong ip6_rt_put(rt); 42459d6acb3bSWANG Cong goto errout; 42469d6acb3bSWANG Cong } 42479d6acb3bSWANG Cong 42481da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 424938308473SDavid S. Miller if (!skb) { 425094e187c0SAmerigo Wang ip6_rt_put(rt); 4251ab364a6fSThomas Graf err = -ENOBUFS; 4252ab364a6fSThomas Graf goto errout; 4253ab364a6fSThomas Graf } 42541da177e4SLinus Torvalds 4255d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 425618c3a61cSRoopa Prabhu if (fibmatch) 425718c3a61cSRoopa Prabhu err = rt6_fill_node(net, skb, rt, NULL, NULL, iif, 425818c3a61cSRoopa Prabhu RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 425918c3a61cSRoopa Prabhu nlh->nlmsg_seq, 0); 426018c3a61cSRoopa Prabhu else 42614c9483b2SDavid S. Miller err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif, 426215e47304SEric W. Biederman RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 4263f8cfe2ceSDavid Ahern nlh->nlmsg_seq, 0); 42641da177e4SLinus Torvalds if (err < 0) { 4265ab364a6fSThomas Graf kfree_skb(skb); 4266ab364a6fSThomas Graf goto errout; 42671da177e4SLinus Torvalds } 42681da177e4SLinus Torvalds 426915e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 4270ab364a6fSThomas Graf errout: 42711da177e4SLinus Torvalds return err; 42721da177e4SLinus Torvalds } 42731da177e4SLinus Torvalds 427437a1d361SRoopa Prabhu void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info, 427537a1d361SRoopa Prabhu unsigned int nlm_flags) 42761da177e4SLinus Torvalds { 42771da177e4SLinus Torvalds struct sk_buff *skb; 42785578689aSDaniel Lezcano struct net *net = info->nl_net; 4279528c4cebSDenis V. Lunev u32 seq; 4280528c4cebSDenis V. Lunev int err; 42810d51aa80SJamal Hadi Salim 4282528c4cebSDenis V. Lunev err = -ENOBUFS; 428338308473SDavid S. Miller seq = info->nlh ? info->nlh->nlmsg_seq : 0; 428486872cb5SThomas Graf 428519e42e45SRoopa Prabhu skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 428638308473SDavid S. Miller if (!skb) 428721713ebcSThomas Graf goto errout; 42881da177e4SLinus Torvalds 4289191cd582SBrian Haley err = rt6_fill_node(net, skb, rt, NULL, NULL, 0, 4290f8cfe2ceSDavid Ahern event, info->portid, seq, nlm_flags); 429126932566SPatrick McHardy if (err < 0) { 429226932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 429326932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 429426932566SPatrick McHardy kfree_skb(skb); 429526932566SPatrick McHardy goto errout; 429626932566SPatrick McHardy } 429715e47304SEric W. Biederman rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 42985578689aSDaniel Lezcano info->nlh, gfp_any()); 42991ce85fe4SPablo Neira Ayuso return; 430021713ebcSThomas Graf errout: 430121713ebcSThomas Graf if (err < 0) 43025578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 43031da177e4SLinus Torvalds } 43041da177e4SLinus Torvalds 43058ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 4306351638e7SJiri Pirko unsigned long event, void *ptr) 43078ed67789SDaniel Lezcano { 4308351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 4309c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 43108ed67789SDaniel Lezcano 4311242d3a49SWANG Cong if (!(dev->flags & IFF_LOOPBACK)) 4312242d3a49SWANG Cong return NOTIFY_OK; 4313242d3a49SWANG Cong 4314242d3a49SWANG Cong if (event == NETDEV_REGISTER) { 4315d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 43168ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 43178ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 4318d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 43198ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 4320d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 43218ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 43228ed67789SDaniel Lezcano #endif 432376da0704SWANG Cong } else if (event == NETDEV_UNREGISTER && 432476da0704SWANG Cong dev->reg_state != NETREG_UNREGISTERED) { 432576da0704SWANG Cong /* NETDEV_UNREGISTER could be fired for multiple times by 432676da0704SWANG Cong * netdev_wait_allrefs(). Make sure we only call this once. 432776da0704SWANG Cong */ 432812d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev); 4329242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 433012d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev); 433112d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev); 4332242d3a49SWANG Cong #endif 43338ed67789SDaniel Lezcano } 43348ed67789SDaniel Lezcano 43358ed67789SDaniel Lezcano return NOTIFY_OK; 43368ed67789SDaniel Lezcano } 43378ed67789SDaniel Lezcano 43381da177e4SLinus Torvalds /* 43391da177e4SLinus Torvalds * /proc 43401da177e4SLinus Torvalds */ 43411da177e4SLinus Torvalds 43421da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 43431da177e4SLinus Torvalds 434433120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = { 434533120b30SAlexey Dobriyan .owner = THIS_MODULE, 434633120b30SAlexey Dobriyan .open = ipv6_route_open, 434733120b30SAlexey Dobriyan .read = seq_read, 434833120b30SAlexey Dobriyan .llseek = seq_lseek, 43498d2ca1d7SHannes Frederic Sowa .release = seq_release_net, 435033120b30SAlexey Dobriyan }; 435133120b30SAlexey Dobriyan 43521da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 43531da177e4SLinus Torvalds { 435469ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 43551da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 435669ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 435769ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 435869ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_alloc, 435969ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 436069ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 4361fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 436269ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 43631da177e4SLinus Torvalds 43641da177e4SLinus Torvalds return 0; 43651da177e4SLinus Torvalds } 43661da177e4SLinus Torvalds 43671da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file) 43681da177e4SLinus Torvalds { 4369de05c557SPavel Emelyanov return single_open_net(inode, file, rt6_stats_seq_show); 437069ddb805SDaniel Lezcano } 437169ddb805SDaniel Lezcano 43729a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = { 43731da177e4SLinus Torvalds .owner = THIS_MODULE, 43741da177e4SLinus Torvalds .open = rt6_stats_seq_open, 43751da177e4SLinus Torvalds .read = seq_read, 43761da177e4SLinus Torvalds .llseek = seq_lseek, 4377b6fcbdb4SPavel Emelyanov .release = single_release_net, 43781da177e4SLinus Torvalds }; 43791da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 43801da177e4SLinus Torvalds 43811da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 43821da177e4SLinus Torvalds 43831da177e4SLinus Torvalds static 4384fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, 43851da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 43861da177e4SLinus Torvalds { 4387c486da34SLucian Adrian Grijincu struct net *net; 4388c486da34SLucian Adrian Grijincu int delay; 4389c486da34SLucian Adrian Grijincu if (!write) 4390c486da34SLucian Adrian Grijincu return -EINVAL; 4391c486da34SLucian Adrian Grijincu 4392c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 4393c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 43948d65af78SAlexey Dobriyan proc_dointvec(ctl, write, buffer, lenp, ppos); 43952ac3ac8fSMichal Kubeček fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0); 43961da177e4SLinus Torvalds return 0; 43971da177e4SLinus Torvalds } 43981da177e4SLinus Torvalds 4399fe2c6338SJoe Perches struct ctl_table ipv6_route_table_template[] = { 44001da177e4SLinus Torvalds { 44011da177e4SLinus Torvalds .procname = "flush", 44024990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 44031da177e4SLinus Torvalds .maxlen = sizeof(int), 440489c8b3a1SDave Jones .mode = 0200, 44056d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 44061da177e4SLinus Torvalds }, 44071da177e4SLinus Torvalds { 44081da177e4SLinus Torvalds .procname = "gc_thresh", 44099a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 44101da177e4SLinus Torvalds .maxlen = sizeof(int), 44111da177e4SLinus Torvalds .mode = 0644, 44126d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 44131da177e4SLinus Torvalds }, 44141da177e4SLinus Torvalds { 44151da177e4SLinus Torvalds .procname = "max_size", 44164990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 44171da177e4SLinus Torvalds .maxlen = sizeof(int), 44181da177e4SLinus Torvalds .mode = 0644, 44196d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 44201da177e4SLinus Torvalds }, 44211da177e4SLinus Torvalds { 44221da177e4SLinus Torvalds .procname = "gc_min_interval", 44234990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 44241da177e4SLinus Torvalds .maxlen = sizeof(int), 44251da177e4SLinus Torvalds .mode = 0644, 44266d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 44271da177e4SLinus Torvalds }, 44281da177e4SLinus Torvalds { 44291da177e4SLinus Torvalds .procname = "gc_timeout", 44304990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 44311da177e4SLinus Torvalds .maxlen = sizeof(int), 44321da177e4SLinus Torvalds .mode = 0644, 44336d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 44341da177e4SLinus Torvalds }, 44351da177e4SLinus Torvalds { 44361da177e4SLinus Torvalds .procname = "gc_interval", 44374990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 44381da177e4SLinus Torvalds .maxlen = sizeof(int), 44391da177e4SLinus Torvalds .mode = 0644, 44406d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 44411da177e4SLinus Torvalds }, 44421da177e4SLinus Torvalds { 44431da177e4SLinus Torvalds .procname = "gc_elasticity", 44444990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 44451da177e4SLinus Torvalds .maxlen = sizeof(int), 44461da177e4SLinus Torvalds .mode = 0644, 4447f3d3f616SMin Zhang .proc_handler = proc_dointvec, 44481da177e4SLinus Torvalds }, 44491da177e4SLinus Torvalds { 44501da177e4SLinus Torvalds .procname = "mtu_expires", 44514990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 44521da177e4SLinus Torvalds .maxlen = sizeof(int), 44531da177e4SLinus Torvalds .mode = 0644, 44546d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 44551da177e4SLinus Torvalds }, 44561da177e4SLinus Torvalds { 44571da177e4SLinus Torvalds .procname = "min_adv_mss", 44584990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 44591da177e4SLinus Torvalds .maxlen = sizeof(int), 44601da177e4SLinus Torvalds .mode = 0644, 4461f3d3f616SMin Zhang .proc_handler = proc_dointvec, 44621da177e4SLinus Torvalds }, 44631da177e4SLinus Torvalds { 44641da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 44654990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 44661da177e4SLinus Torvalds .maxlen = sizeof(int), 44671da177e4SLinus Torvalds .mode = 0644, 44686d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 44691da177e4SLinus Torvalds }, 4470f8572d8fSEric W. Biederman { } 44711da177e4SLinus Torvalds }; 44721da177e4SLinus Torvalds 44732c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 4474760f2d01SDaniel Lezcano { 4475760f2d01SDaniel Lezcano struct ctl_table *table; 4476760f2d01SDaniel Lezcano 4477760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 4478760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 4479760f2d01SDaniel Lezcano GFP_KERNEL); 44805ee09105SYOSHIFUJI Hideaki 44815ee09105SYOSHIFUJI Hideaki if (table) { 44825ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 4483c486da34SLucian Adrian Grijincu table[0].extra1 = net; 448486393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 44855ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 44865ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 44875ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 44885ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 44895ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 44905ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 44915ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 44929c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 4493464dc801SEric W. Biederman 4494464dc801SEric W. Biederman /* Don't export sysctls to unprivileged users */ 4495464dc801SEric W. Biederman if (net->user_ns != &init_user_ns) 4496464dc801SEric W. Biederman table[0].procname = NULL; 44975ee09105SYOSHIFUJI Hideaki } 44985ee09105SYOSHIFUJI Hideaki 4499760f2d01SDaniel Lezcano return table; 4500760f2d01SDaniel Lezcano } 45011da177e4SLinus Torvalds #endif 45021da177e4SLinus Torvalds 45032c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 4504cdb18761SDaniel Lezcano { 4505633d424bSPavel Emelyanov int ret = -ENOMEM; 45068ed67789SDaniel Lezcano 450786393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 450886393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 4509f2fc6a54SBenjamin Thery 4510fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 4511fc66f95cSEric Dumazet goto out_ip6_dst_ops; 4512fc66f95cSEric Dumazet 45138ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 45148ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 45158ed67789SDaniel Lezcano GFP_KERNEL); 45168ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 4517fc66f95cSEric Dumazet goto out_ip6_dst_entries; 4518d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.path = 45198ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_null_entry; 4520d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 452162fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 452262fa8a84SDavid S. Miller ip6_template_metrics, true); 45238ed67789SDaniel Lezcano 45248ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 4525feca7d8cSVincent Bernat net->ipv6.fib6_has_custom_rules = false; 45268ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 45278ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 45288ed67789SDaniel Lezcano GFP_KERNEL); 452968fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 453068fffc67SPeter Zijlstra goto out_ip6_null_entry; 4531d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.path = 45328ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_prohibit_entry; 4533d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 453462fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 453562fa8a84SDavid S. Miller ip6_template_metrics, true); 45368ed67789SDaniel Lezcano 45378ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 45388ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 45398ed67789SDaniel Lezcano GFP_KERNEL); 454068fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 454168fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 4542d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.path = 45438ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_blk_hole_entry; 4544d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 454562fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 454662fa8a84SDavid S. Miller ip6_template_metrics, true); 45478ed67789SDaniel Lezcano #endif 45488ed67789SDaniel Lezcano 4549b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 4550b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 4551b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 4552b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 4553b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 4554b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 4555b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 4556b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 4557b339a47cSPeter Zijlstra 45586891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 45596891a346SBenjamin Thery 45608ed67789SDaniel Lezcano ret = 0; 45618ed67789SDaniel Lezcano out: 45628ed67789SDaniel Lezcano return ret; 4563f2fc6a54SBenjamin Thery 456468fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 456568fffc67SPeter Zijlstra out_ip6_prohibit_entry: 456668fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 456768fffc67SPeter Zijlstra out_ip6_null_entry: 456868fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 456968fffc67SPeter Zijlstra #endif 4570fc66f95cSEric Dumazet out_ip6_dst_entries: 4571fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 4572f2fc6a54SBenjamin Thery out_ip6_dst_ops: 4573f2fc6a54SBenjamin Thery goto out; 4574cdb18761SDaniel Lezcano } 4575cdb18761SDaniel Lezcano 45762c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 4577cdb18761SDaniel Lezcano { 45788ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 45798ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 45808ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 45818ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 45828ed67789SDaniel Lezcano #endif 458341bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 4584cdb18761SDaniel Lezcano } 4585cdb18761SDaniel Lezcano 4586d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net) 4587d189634eSThomas Graf { 4588d189634eSThomas Graf #ifdef CONFIG_PROC_FS 4589d4beaa66SGao feng proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops); 4590d4beaa66SGao feng proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops); 4591d189634eSThomas Graf #endif 4592d189634eSThomas Graf return 0; 4593d189634eSThomas Graf } 4594d189634eSThomas Graf 4595d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net) 4596d189634eSThomas Graf { 4597d189634eSThomas Graf #ifdef CONFIG_PROC_FS 4598ece31ffdSGao feng remove_proc_entry("ipv6_route", net->proc_net); 4599ece31ffdSGao feng remove_proc_entry("rt6_stats", net->proc_net); 4600d189634eSThomas Graf #endif 4601d189634eSThomas Graf } 4602d189634eSThomas Graf 4603cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 4604cdb18761SDaniel Lezcano .init = ip6_route_net_init, 4605cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 4606cdb18761SDaniel Lezcano }; 4607cdb18761SDaniel Lezcano 4608c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net) 4609c3426b47SDavid S. Miller { 4610c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 4611c3426b47SDavid S. Miller 4612c3426b47SDavid S. Miller if (!bp) 4613c3426b47SDavid S. Miller return -ENOMEM; 4614c3426b47SDavid S. Miller inet_peer_base_init(bp); 4615c3426b47SDavid S. Miller net->ipv6.peers = bp; 4616c3426b47SDavid S. Miller return 0; 4617c3426b47SDavid S. Miller } 4618c3426b47SDavid S. Miller 4619c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net) 4620c3426b47SDavid S. Miller { 4621c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv6.peers; 4622c3426b47SDavid S. Miller 4623c3426b47SDavid S. Miller net->ipv6.peers = NULL; 462456a6b248SDavid S. Miller inetpeer_invalidate_tree(bp); 4625c3426b47SDavid S. Miller kfree(bp); 4626c3426b47SDavid S. Miller } 4627c3426b47SDavid S. Miller 46282b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = { 4629c3426b47SDavid S. Miller .init = ipv6_inetpeer_init, 4630c3426b47SDavid S. Miller .exit = ipv6_inetpeer_exit, 4631c3426b47SDavid S. Miller }; 4632c3426b47SDavid S. Miller 4633d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = { 4634d189634eSThomas Graf .init = ip6_route_net_init_late, 4635d189634eSThomas Graf .exit = ip6_route_net_exit_late, 4636d189634eSThomas Graf }; 4637d189634eSThomas Graf 46388ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 46398ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 4640242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY - 10, 46418ed67789SDaniel Lezcano }; 46428ed67789SDaniel Lezcano 46432f460933SWANG Cong void __init ip6_route_init_special_entries(void) 46442f460933SWANG Cong { 46452f460933SWANG Cong /* Registering of the loopback is done before this portion of code, 46462f460933SWANG Cong * the loopback reference in rt6_info will not be taken, do it 46472f460933SWANG Cong * manually for init_net */ 46482f460933SWANG Cong init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 46492f460933SWANG Cong init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 46502f460933SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 46512f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 46522f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 46532f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 46542f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 46552f460933SWANG Cong #endif 46562f460933SWANG Cong } 46572f460933SWANG Cong 4658433d49c3SDaniel Lezcano int __init ip6_route_init(void) 46591da177e4SLinus Torvalds { 4660433d49c3SDaniel Lezcano int ret; 46618d0b94afSMartin KaFai Lau int cpu; 4662433d49c3SDaniel Lezcano 46639a7ec3a9SDaniel Lezcano ret = -ENOMEM; 46649a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 46659a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 46669a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 46679a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 4668c19a28e1SFernando Carrijo goto out; 466914e50e57SDavid S. Miller 4670fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 46718ed67789SDaniel Lezcano if (ret) 4672bdb3289fSDaniel Lezcano goto out_kmem_cache; 4673bdb3289fSDaniel Lezcano 4674c3426b47SDavid S. Miller ret = register_pernet_subsys(&ipv6_inetpeer_ops); 4675c3426b47SDavid S. Miller if (ret) 4676e8803b6cSDavid S. Miller goto out_dst_entries; 46772a0c451aSThomas Graf 46787e52b33bSDavid S. Miller ret = register_pernet_subsys(&ip6_route_net_ops); 46797e52b33bSDavid S. Miller if (ret) 46807e52b33bSDavid S. Miller goto out_register_inetpeer; 4681c3426b47SDavid S. Miller 46825dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 46835dc121e9SArnaud Ebalard 4684e8803b6cSDavid S. Miller ret = fib6_init(); 4685433d49c3SDaniel Lezcano if (ret) 46868ed67789SDaniel Lezcano goto out_register_subsys; 4687433d49c3SDaniel Lezcano 4688433d49c3SDaniel Lezcano ret = xfrm6_init(); 4689433d49c3SDaniel Lezcano if (ret) 4690e8803b6cSDavid S. Miller goto out_fib6_init; 4691c35b7e72SDaniel Lezcano 4692433d49c3SDaniel Lezcano ret = fib6_rules_init(); 4693433d49c3SDaniel Lezcano if (ret) 4694433d49c3SDaniel Lezcano goto xfrm6_init; 46957e5449c2SDaniel Lezcano 4696d189634eSThomas Graf ret = register_pernet_subsys(&ip6_route_net_late_ops); 4697d189634eSThomas Graf if (ret) 4698d189634eSThomas Graf goto fib6_rules_init; 4699d189634eSThomas Graf 4700433d49c3SDaniel Lezcano ret = -ENOBUFS; 4701b97bac64SFlorian Westphal if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, 0) || 4702b97bac64SFlorian Westphal __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, 0) || 4703e3a22b7fSFlorian Westphal __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, 4704e3a22b7fSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED)) 4705d189634eSThomas Graf goto out_register_late_subsys; 4706433d49c3SDaniel Lezcano 47078ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 4708cdb18761SDaniel Lezcano if (ret) 4709d189634eSThomas Graf goto out_register_late_subsys; 47108ed67789SDaniel Lezcano 47118d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 47128d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 47138d0b94afSMartin KaFai Lau 47148d0b94afSMartin KaFai Lau INIT_LIST_HEAD(&ul->head); 47158d0b94afSMartin KaFai Lau spin_lock_init(&ul->lock); 47168d0b94afSMartin KaFai Lau } 47178d0b94afSMartin KaFai Lau 4718433d49c3SDaniel Lezcano out: 4719433d49c3SDaniel Lezcano return ret; 4720433d49c3SDaniel Lezcano 4721d189634eSThomas Graf out_register_late_subsys: 4722d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 4723433d49c3SDaniel Lezcano fib6_rules_init: 4724433d49c3SDaniel Lezcano fib6_rules_cleanup(); 4725433d49c3SDaniel Lezcano xfrm6_init: 4726433d49c3SDaniel Lezcano xfrm6_fini(); 47272a0c451aSThomas Graf out_fib6_init: 47282a0c451aSThomas Graf fib6_gc_cleanup(); 47298ed67789SDaniel Lezcano out_register_subsys: 47308ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 47317e52b33bSDavid S. Miller out_register_inetpeer: 47327e52b33bSDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 4733fc66f95cSEric Dumazet out_dst_entries: 4734fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 4735433d49c3SDaniel Lezcano out_kmem_cache: 4736f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 4737433d49c3SDaniel Lezcano goto out; 47381da177e4SLinus Torvalds } 47391da177e4SLinus Torvalds 47401da177e4SLinus Torvalds void ip6_route_cleanup(void) 47411da177e4SLinus Torvalds { 47428ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 4743d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 4744101367c2SThomas Graf fib6_rules_cleanup(); 47451da177e4SLinus Torvalds xfrm6_fini(); 47461da177e4SLinus Torvalds fib6_gc_cleanup(); 4747c3426b47SDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 47488ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 474941bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 4750f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 47511da177e4SLinus Torvalds } 4752