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 1531c757faa8SWei Wang static void rt6_age_examine_exception(struct rt6_exception_bucket *bucket, 1532c757faa8SWei Wang struct rt6_exception *rt6_ex, 1533c757faa8SWei Wang struct fib6_gc_args *gc_args, 1534c757faa8SWei Wang unsigned long now) 1535c757faa8SWei Wang { 1536c757faa8SWei Wang struct rt6_info *rt = rt6_ex->rt6i; 1537c757faa8SWei Wang 1538c757faa8SWei Wang if (atomic_read(&rt->dst.__refcnt) == 1 && 1539c757faa8SWei Wang time_after_eq(now, rt->dst.lastuse + gc_args->timeout)) { 1540c757faa8SWei Wang RT6_TRACE("aging clone %p\n", rt); 1541c757faa8SWei Wang rt6_remove_exception(bucket, rt6_ex); 1542c757faa8SWei Wang return; 1543c757faa8SWei Wang } else if (rt->rt6i_flags & RTF_GATEWAY) { 1544c757faa8SWei Wang struct neighbour *neigh; 1545c757faa8SWei Wang __u8 neigh_flags = 0; 1546c757faa8SWei Wang 1547c757faa8SWei Wang neigh = dst_neigh_lookup(&rt->dst, &rt->rt6i_gateway); 1548c757faa8SWei Wang if (neigh) { 1549c757faa8SWei Wang neigh_flags = neigh->flags; 1550c757faa8SWei Wang neigh_release(neigh); 1551c757faa8SWei Wang } 1552c757faa8SWei Wang if (!(neigh_flags & NTF_ROUTER)) { 1553c757faa8SWei Wang RT6_TRACE("purging route %p via non-router but gateway\n", 1554c757faa8SWei Wang rt); 1555c757faa8SWei Wang rt6_remove_exception(bucket, rt6_ex); 1556c757faa8SWei Wang return; 1557c757faa8SWei Wang } 1558c757faa8SWei Wang } 1559c757faa8SWei Wang gc_args->more++; 1560c757faa8SWei Wang } 1561c757faa8SWei Wang 1562c757faa8SWei Wang void rt6_age_exceptions(struct rt6_info *rt, 1563c757faa8SWei Wang struct fib6_gc_args *gc_args, 1564c757faa8SWei Wang unsigned long now) 1565c757faa8SWei Wang { 1566c757faa8SWei Wang struct rt6_exception_bucket *bucket; 1567c757faa8SWei Wang struct rt6_exception *rt6_ex; 1568c757faa8SWei Wang struct hlist_node *tmp; 1569c757faa8SWei Wang int i; 1570c757faa8SWei Wang 1571c757faa8SWei Wang if (!rcu_access_pointer(rt->rt6i_exception_bucket)) 1572c757faa8SWei Wang return; 1573c757faa8SWei Wang 1574c757faa8SWei Wang spin_lock_bh(&rt6_exception_lock); 1575c757faa8SWei Wang bucket = rcu_dereference_protected(rt->rt6i_exception_bucket, 1576c757faa8SWei Wang lockdep_is_held(&rt6_exception_lock)); 1577c757faa8SWei Wang 1578c757faa8SWei Wang if (bucket) { 1579c757faa8SWei Wang for (i = 0; i < FIB6_EXCEPTION_BUCKET_SIZE; i++) { 1580c757faa8SWei Wang hlist_for_each_entry_safe(rt6_ex, tmp, 1581c757faa8SWei Wang &bucket->chain, hlist) { 1582c757faa8SWei Wang rt6_age_examine_exception(bucket, rt6_ex, 1583c757faa8SWei Wang gc_args, now); 1584c757faa8SWei Wang } 1585c757faa8SWei Wang bucket++; 1586c757faa8SWei Wang } 1587c757faa8SWei Wang } 1588c757faa8SWei Wang spin_unlock_bh(&rt6_exception_lock); 1589c757faa8SWei Wang } 1590c757faa8SWei 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, 2803*38fbeeeeSWei Wang &cfg->fc_src, cfg->fc_src_len, 2804*38fbeeeeSWei Wang true); 28051da177e4SLinus Torvalds 28061da177e4SLinus Torvalds if (fn) { 2807d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 28081f56a01fSMartin KaFai Lau if ((rt->rt6i_flags & RTF_CACHE) && 28091f56a01fSMartin KaFai Lau !(cfg->fc_flags & RTF_CACHE)) 28101f56a01fSMartin KaFai Lau continue; 281186872cb5SThomas Graf if (cfg->fc_ifindex && 2812d1918542SDavid S. Miller (!rt->dst.dev || 2813d1918542SDavid S. Miller rt->dst.dev->ifindex != cfg->fc_ifindex)) 28141da177e4SLinus Torvalds continue; 281586872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 281686872cb5SThomas Graf !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 28171da177e4SLinus Torvalds continue; 281886872cb5SThomas Graf if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric) 28191da177e4SLinus Torvalds continue; 2820c2ed1880SMantas M if (cfg->fc_protocol && cfg->fc_protocol != rt->rt6i_protocol) 2821c2ed1880SMantas M continue; 2822d8d1f30bSChangli Gao dst_hold(&rt->dst); 2823c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 28241da177e4SLinus Torvalds 28250ae81335SDavid Ahern /* if gateway was specified only delete the one hop */ 28260ae81335SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY) 282786872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 28280ae81335SDavid Ahern 28290ae81335SDavid Ahern return __ip6_del_rt_siblings(rt, cfg); 28301da177e4SLinus Torvalds } 28311da177e4SLinus Torvalds } 2832c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 28331da177e4SLinus Torvalds 28341da177e4SLinus Torvalds return err; 28351da177e4SLinus Torvalds } 28361da177e4SLinus Torvalds 28376700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 2838a6279458SYOSHIFUJI Hideaki { 2839a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 2840e8599ff4SDavid S. Miller struct rt6_info *rt, *nrt = NULL; 2841e8599ff4SDavid S. Miller struct ndisc_options ndopts; 2842e8599ff4SDavid S. Miller struct inet6_dev *in6_dev; 2843e8599ff4SDavid S. Miller struct neighbour *neigh; 284471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 struct rd_msg *msg; 28456e157b6aSDavid S. Miller int optlen, on_link; 28466e157b6aSDavid S. Miller u8 *lladdr; 2847e8599ff4SDavid S. Miller 284829a3cad5SSimon Horman optlen = skb_tail_pointer(skb) - skb_transport_header(skb); 284971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 optlen -= sizeof(*msg); 2850e8599ff4SDavid S. Miller 2851e8599ff4SDavid S. Miller if (optlen < 0) { 28526e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); 2853e8599ff4SDavid S. Miller return; 2854e8599ff4SDavid S. Miller } 2855e8599ff4SDavid S. Miller 285671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 msg = (struct rd_msg *)icmp6_hdr(skb); 2857e8599ff4SDavid S. Miller 285871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_is_multicast(&msg->dest)) { 28596e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); 2860e8599ff4SDavid S. Miller return; 2861e8599ff4SDavid S. Miller } 2862e8599ff4SDavid S. Miller 28636e157b6aSDavid S. Miller on_link = 0; 286471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_equal(&msg->dest, &msg->target)) { 2865e8599ff4SDavid S. Miller on_link = 1; 286671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 } else if (ipv6_addr_type(&msg->target) != 2867e8599ff4SDavid S. Miller (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { 28686e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); 2869e8599ff4SDavid S. Miller return; 2870e8599ff4SDavid S. Miller } 2871e8599ff4SDavid S. Miller 2872e8599ff4SDavid S. Miller in6_dev = __in6_dev_get(skb->dev); 2873e8599ff4SDavid S. Miller if (!in6_dev) 2874e8599ff4SDavid S. Miller return; 2875e8599ff4SDavid S. Miller if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) 2876e8599ff4SDavid S. Miller return; 2877e8599ff4SDavid S. Miller 2878e8599ff4SDavid S. Miller /* RFC2461 8.1: 2879e8599ff4SDavid S. Miller * The IP source address of the Redirect MUST be the same as the current 2880e8599ff4SDavid S. Miller * first-hop router for the specified ICMP Destination Address. 2881e8599ff4SDavid S. Miller */ 2882e8599ff4SDavid S. Miller 2883f997c55cSAlexander Aring if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { 2884e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); 2885e8599ff4SDavid S. Miller return; 2886e8599ff4SDavid S. Miller } 28876e157b6aSDavid S. Miller 28886e157b6aSDavid S. Miller lladdr = NULL; 2889e8599ff4SDavid S. Miller if (ndopts.nd_opts_tgt_lladdr) { 2890e8599ff4SDavid S. Miller lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, 2891e8599ff4SDavid S. Miller skb->dev); 2892e8599ff4SDavid S. Miller if (!lladdr) { 2893e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); 2894e8599ff4SDavid S. Miller return; 2895e8599ff4SDavid S. Miller } 2896e8599ff4SDavid S. Miller } 2897e8599ff4SDavid S. Miller 28986e157b6aSDavid S. Miller rt = (struct rt6_info *) dst; 2899ec13ad1dSMatthias Schiffer if (rt->rt6i_flags & RTF_REJECT) { 29006e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); 29016e157b6aSDavid S. Miller return; 29026e157b6aSDavid S. Miller } 29036e157b6aSDavid S. Miller 29046e157b6aSDavid S. Miller /* Redirect received -> path was valid. 29056e157b6aSDavid S. Miller * Look, redirects are sent only in response to data packets, 29066e157b6aSDavid S. Miller * so that this nexthop apparently is reachable. --ANK 29076e157b6aSDavid S. Miller */ 29080dec879fSJulian Anastasov dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); 29096e157b6aSDavid S. Miller 291071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); 2911e8599ff4SDavid S. Miller if (!neigh) 2912e8599ff4SDavid S. Miller return; 2913e8599ff4SDavid S. Miller 29141da177e4SLinus Torvalds /* 29151da177e4SLinus Torvalds * We have finally decided to accept it. 29161da177e4SLinus Torvalds */ 29171da177e4SLinus Torvalds 2918f997c55cSAlexander Aring ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, 29191da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 29201da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 29211da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 2922f997c55cSAlexander Aring NEIGH_UPDATE_F_ISROUTER)), 2923f997c55cSAlexander Aring NDISC_REDIRECT, &ndopts); 29241da177e4SLinus Torvalds 292583a09abdSMartin KaFai Lau nrt = ip6_rt_cache_alloc(rt, &msg->dest, NULL); 292638308473SDavid S. Miller if (!nrt) 29271da177e4SLinus Torvalds goto out; 29281da177e4SLinus Torvalds 29291da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 29301da177e4SLinus Torvalds if (on_link) 29311da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 29321da177e4SLinus Torvalds 2933b91d5329SXin Long nrt->rt6i_protocol = RTPROT_REDIRECT; 29344e3fd7a0SAlexey Dobriyan nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 29351da177e4SLinus Torvalds 293640e22e8fSThomas Graf if (ip6_ins_rt(nrt)) 29371cfb71eeSWei Wang goto out_release; 29381da177e4SLinus Torvalds 2939d8d1f30bSChangli Gao netevent.old = &rt->dst; 2940d8d1f30bSChangli Gao netevent.new = &nrt->dst; 294171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 netevent.daddr = &msg->dest; 294260592833SYOSHIFUJI Hideaki / 吉藤英明 netevent.neigh = neigh; 29438d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 29448d71740cSTom Tucker 29451da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) { 29466e157b6aSDavid S. Miller rt = (struct rt6_info *) dst_clone(&rt->dst); 2947e0a1ad73SThomas Graf ip6_del_rt(rt); 29481da177e4SLinus Torvalds } 29491da177e4SLinus Torvalds 29501cfb71eeSWei Wang out_release: 29511cfb71eeSWei Wang /* Release the reference taken in 29521cfb71eeSWei Wang * ip6_rt_cache_alloc() 29531cfb71eeSWei Wang */ 29541cfb71eeSWei Wang dst_release(&nrt->dst); 29551cfb71eeSWei Wang 29561da177e4SLinus Torvalds out: 2957e8599ff4SDavid S. Miller neigh_release(neigh); 29586e157b6aSDavid S. Miller } 29596e157b6aSDavid S. Miller 29601da177e4SLinus Torvalds /* 29611da177e4SLinus Torvalds * Misc support functions 29621da177e4SLinus Torvalds */ 29631da177e4SLinus Torvalds 29644b32b5adSMartin KaFai Lau static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from) 29654b32b5adSMartin KaFai Lau { 29664b32b5adSMartin KaFai Lau BUG_ON(from->dst.from); 29674b32b5adSMartin KaFai Lau 29684b32b5adSMartin KaFai Lau rt->rt6i_flags &= ~RTF_EXPIRES; 29694b32b5adSMartin KaFai Lau dst_hold(&from->dst); 29704b32b5adSMartin KaFai Lau rt->dst.from = &from->dst; 29714b32b5adSMartin KaFai Lau dst_init_metrics(&rt->dst, dst_metrics_ptr(&from->dst), true); 29724b32b5adSMartin KaFai Lau } 29734b32b5adSMartin KaFai Lau 297483a09abdSMartin KaFai Lau static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort) 29751da177e4SLinus Torvalds { 2976d8d1f30bSChangli Gao rt->dst.input = ort->dst.input; 2977d8d1f30bSChangli Gao rt->dst.output = ort->dst.output; 297883a09abdSMartin KaFai Lau rt->rt6i_dst = ort->rt6i_dst; 2979d8d1f30bSChangli Gao rt->dst.error = ort->dst.error; 29801da177e4SLinus Torvalds rt->rt6i_idev = ort->rt6i_idev; 29811da177e4SLinus Torvalds if (rt->rt6i_idev) 29821da177e4SLinus Torvalds in6_dev_hold(rt->rt6i_idev); 2983d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 29844e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 29851716a961SGao feng rt->rt6i_flags = ort->rt6i_flags; 29861716a961SGao feng rt6_set_from(rt, ort); 298783a09abdSMartin KaFai Lau rt->rt6i_metric = ort->rt6i_metric; 29881da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 298983a09abdSMartin KaFai Lau rt->rt6i_src = ort->rt6i_src; 29901da177e4SLinus Torvalds #endif 299183a09abdSMartin KaFai Lau rt->rt6i_prefsrc = ort->rt6i_prefsrc; 2992c71099acSThomas Graf rt->rt6i_table = ort->rt6i_table; 299361adedf3SJiri Benc rt->dst.lwtstate = lwtstate_get(ort->dst.lwtstate); 29941da177e4SLinus Torvalds } 29951da177e4SLinus Torvalds 299670ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 2997efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 2998b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 2999830218c1SDavid Ahern const struct in6_addr *gwaddr, 3000830218c1SDavid Ahern struct net_device *dev) 300170ceb4f5SYOSHIFUJI Hideaki { 3002830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; 3003830218c1SDavid Ahern int ifindex = dev->ifindex; 300470ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 300570ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt = NULL; 3006c71099acSThomas Graf struct fib6_table *table; 300770ceb4f5SYOSHIFUJI Hideaki 3008830218c1SDavid Ahern table = fib6_get_table(net, tb_id); 300938308473SDavid S. Miller if (!table) 3010c71099acSThomas Graf return NULL; 3011c71099acSThomas Graf 30125744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 3013*38fbeeeeSWei Wang fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0, true); 301470ceb4f5SYOSHIFUJI Hideaki if (!fn) 301570ceb4f5SYOSHIFUJI Hideaki goto out; 301670ceb4f5SYOSHIFUJI Hideaki 3017d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 3018d1918542SDavid S. Miller if (rt->dst.dev->ifindex != ifindex) 301970ceb4f5SYOSHIFUJI Hideaki continue; 302070ceb4f5SYOSHIFUJI Hideaki if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) 302170ceb4f5SYOSHIFUJI Hideaki continue; 302270ceb4f5SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr)) 302370ceb4f5SYOSHIFUJI Hideaki continue; 3024d8d1f30bSChangli Gao dst_hold(&rt->dst); 302570ceb4f5SYOSHIFUJI Hideaki break; 302670ceb4f5SYOSHIFUJI Hideaki } 302770ceb4f5SYOSHIFUJI Hideaki out: 30285744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 302970ceb4f5SYOSHIFUJI Hideaki return rt; 303070ceb4f5SYOSHIFUJI Hideaki } 303170ceb4f5SYOSHIFUJI Hideaki 3032efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 3033b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 3034830218c1SDavid Ahern const struct in6_addr *gwaddr, 3035830218c1SDavid Ahern struct net_device *dev, 303695c96174SEric Dumazet unsigned int pref) 303770ceb4f5SYOSHIFUJI Hideaki { 303886872cb5SThomas Graf struct fib6_config cfg = { 3039238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 3040830218c1SDavid Ahern .fc_ifindex = dev->ifindex, 304186872cb5SThomas Graf .fc_dst_len = prefixlen, 304286872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 304386872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 3044b91d5329SXin Long .fc_protocol = RTPROT_RA, 304515e47304SEric W. Biederman .fc_nlinfo.portid = 0, 3046efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 3047efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 304886872cb5SThomas Graf }; 304970ceb4f5SYOSHIFUJI Hideaki 3050830218c1SDavid Ahern cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO, 30514e3fd7a0SAlexey Dobriyan cfg.fc_dst = *prefix; 30524e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 305386872cb5SThomas Graf 3054e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 3055e317da96SYOSHIFUJI Hideaki if (!prefixlen) 305686872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 305770ceb4f5SYOSHIFUJI Hideaki 3058333c4301SDavid Ahern ip6_route_add(&cfg, NULL); 305970ceb4f5SYOSHIFUJI Hideaki 3060830218c1SDavid Ahern return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); 306170ceb4f5SYOSHIFUJI Hideaki } 306270ceb4f5SYOSHIFUJI Hideaki #endif 306370ceb4f5SYOSHIFUJI Hideaki 3064b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev) 30651da177e4SLinus Torvalds { 3066830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; 30671da177e4SLinus Torvalds struct rt6_info *rt; 3068c71099acSThomas Graf struct fib6_table *table; 30691da177e4SLinus Torvalds 3070830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), tb_id); 307138308473SDavid S. Miller if (!table) 3072c71099acSThomas Graf return NULL; 30731da177e4SLinus Torvalds 30745744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 3075d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 3076d1918542SDavid S. Miller if (dev == rt->dst.dev && 3077045927ffSYOSHIFUJI Hideaki ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 30781da177e4SLinus Torvalds ipv6_addr_equal(&rt->rt6i_gateway, addr)) 30791da177e4SLinus Torvalds break; 30801da177e4SLinus Torvalds } 30811da177e4SLinus Torvalds if (rt) 3082d8d1f30bSChangli Gao dst_hold(&rt->dst); 30835744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 30841da177e4SLinus Torvalds return rt; 30851da177e4SLinus Torvalds } 30861da177e4SLinus Torvalds 3087b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr, 3088ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 3089ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 30901da177e4SLinus Torvalds { 309186872cb5SThomas Graf struct fib6_config cfg = { 3092ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, 3093238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 309486872cb5SThomas Graf .fc_ifindex = dev->ifindex, 309586872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 309686872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 3097b91d5329SXin Long .fc_protocol = RTPROT_RA, 309815e47304SEric W. Biederman .fc_nlinfo.portid = 0, 30995578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 3100c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 310186872cb5SThomas Graf }; 31021da177e4SLinus Torvalds 31034e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 31041da177e4SLinus Torvalds 3105333c4301SDavid Ahern if (!ip6_route_add(&cfg, NULL)) { 3106830218c1SDavid Ahern struct fib6_table *table; 3107830218c1SDavid Ahern 3108830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), cfg.fc_table); 3109830218c1SDavid Ahern if (table) 3110830218c1SDavid Ahern table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; 3111830218c1SDavid Ahern } 31121da177e4SLinus Torvalds 31131da177e4SLinus Torvalds return rt6_get_dflt_router(gwaddr, dev); 31141da177e4SLinus Torvalds } 31151da177e4SLinus Torvalds 3116830218c1SDavid Ahern static void __rt6_purge_dflt_routers(struct fib6_table *table) 31171da177e4SLinus Torvalds { 31181da177e4SLinus Torvalds struct rt6_info *rt; 31191da177e4SLinus Torvalds 31201da177e4SLinus Torvalds restart: 3121c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 3122d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 31233e8b0ac3SLorenzo Colitti if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) && 31243e8b0ac3SLorenzo Colitti (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) { 3125d8d1f30bSChangli Gao dst_hold(&rt->dst); 3126c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 3127e0a1ad73SThomas Graf ip6_del_rt(rt); 31281da177e4SLinus Torvalds goto restart; 31291da177e4SLinus Torvalds } 31301da177e4SLinus Torvalds } 3131c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 3132830218c1SDavid Ahern 3133830218c1SDavid Ahern table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; 3134830218c1SDavid Ahern } 3135830218c1SDavid Ahern 3136830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net) 3137830218c1SDavid Ahern { 3138830218c1SDavid Ahern struct fib6_table *table; 3139830218c1SDavid Ahern struct hlist_head *head; 3140830218c1SDavid Ahern unsigned int h; 3141830218c1SDavid Ahern 3142830218c1SDavid Ahern rcu_read_lock(); 3143830218c1SDavid Ahern 3144830218c1SDavid Ahern for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { 3145830218c1SDavid Ahern head = &net->ipv6.fib_table_hash[h]; 3146830218c1SDavid Ahern hlist_for_each_entry_rcu(table, head, tb6_hlist) { 3147830218c1SDavid Ahern if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) 3148830218c1SDavid Ahern __rt6_purge_dflt_routers(table); 3149830218c1SDavid Ahern } 3150830218c1SDavid Ahern } 3151830218c1SDavid Ahern 3152830218c1SDavid Ahern rcu_read_unlock(); 31531da177e4SLinus Torvalds } 31541da177e4SLinus Torvalds 31555578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 31565578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 315786872cb5SThomas Graf struct fib6_config *cfg) 315886872cb5SThomas Graf { 315986872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 316086872cb5SThomas Graf 3161ca254490SDavid Ahern cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? 3162ca254490SDavid Ahern : RT6_TABLE_MAIN; 316386872cb5SThomas Graf cfg->fc_ifindex = rtmsg->rtmsg_ifindex; 316486872cb5SThomas Graf cfg->fc_metric = rtmsg->rtmsg_metric; 316586872cb5SThomas Graf cfg->fc_expires = rtmsg->rtmsg_info; 316686872cb5SThomas Graf cfg->fc_dst_len = rtmsg->rtmsg_dst_len; 316786872cb5SThomas Graf cfg->fc_src_len = rtmsg->rtmsg_src_len; 316886872cb5SThomas Graf cfg->fc_flags = rtmsg->rtmsg_flags; 316986872cb5SThomas Graf 31705578689aSDaniel Lezcano cfg->fc_nlinfo.nl_net = net; 3171f1243c2dSBenjamin Thery 31724e3fd7a0SAlexey Dobriyan cfg->fc_dst = rtmsg->rtmsg_dst; 31734e3fd7a0SAlexey Dobriyan cfg->fc_src = rtmsg->rtmsg_src; 31744e3fd7a0SAlexey Dobriyan cfg->fc_gateway = rtmsg->rtmsg_gateway; 317586872cb5SThomas Graf } 317686872cb5SThomas Graf 31775578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) 31781da177e4SLinus Torvalds { 317986872cb5SThomas Graf struct fib6_config cfg; 31801da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 31811da177e4SLinus Torvalds int err; 31821da177e4SLinus Torvalds 31831da177e4SLinus Torvalds switch (cmd) { 31841da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 31851da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 3186af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 31871da177e4SLinus Torvalds return -EPERM; 31881da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 31891da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 31901da177e4SLinus Torvalds if (err) 31911da177e4SLinus Torvalds return -EFAULT; 31921da177e4SLinus Torvalds 31935578689aSDaniel Lezcano rtmsg_to_fib6_config(net, &rtmsg, &cfg); 319486872cb5SThomas Graf 31951da177e4SLinus Torvalds rtnl_lock(); 31961da177e4SLinus Torvalds switch (cmd) { 31971da177e4SLinus Torvalds case SIOCADDRT: 3198333c4301SDavid Ahern err = ip6_route_add(&cfg, NULL); 31991da177e4SLinus Torvalds break; 32001da177e4SLinus Torvalds case SIOCDELRT: 3201333c4301SDavid Ahern err = ip6_route_del(&cfg, NULL); 32021da177e4SLinus Torvalds break; 32031da177e4SLinus Torvalds default: 32041da177e4SLinus Torvalds err = -EINVAL; 32051da177e4SLinus Torvalds } 32061da177e4SLinus Torvalds rtnl_unlock(); 32071da177e4SLinus Torvalds 32081da177e4SLinus Torvalds return err; 32093ff50b79SStephen Hemminger } 32101da177e4SLinus Torvalds 32111da177e4SLinus Torvalds return -EINVAL; 32121da177e4SLinus Torvalds } 32131da177e4SLinus Torvalds 32141da177e4SLinus Torvalds /* 32151da177e4SLinus Torvalds * Drop the packet on the floor 32161da177e4SLinus Torvalds */ 32171da177e4SLinus Torvalds 3218d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 32191da177e4SLinus Torvalds { 3220612f09e8SYOSHIFUJI Hideaki int type; 3221adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 3222612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 3223612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 32240660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 322545bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 32263bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 32273bd653c8SDenis V. Lunev IPSTATS_MIB_INADDRERRORS); 3228612f09e8SYOSHIFUJI Hideaki break; 3229612f09e8SYOSHIFUJI Hideaki } 3230612f09e8SYOSHIFUJI Hideaki /* FALLTHROUGH */ 3231612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 32323bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 32333bd653c8SDenis V. Lunev ipstats_mib_noroutes); 3234612f09e8SYOSHIFUJI Hideaki break; 3235612f09e8SYOSHIFUJI Hideaki } 32363ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 32371da177e4SLinus Torvalds kfree_skb(skb); 32381da177e4SLinus Torvalds return 0; 32391da177e4SLinus Torvalds } 32401da177e4SLinus Torvalds 32419ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 32429ce8ade0SThomas Graf { 3243612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 32449ce8ade0SThomas Graf } 32459ce8ade0SThomas Graf 3246ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb) 32471da177e4SLinus Torvalds { 3248adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 3249612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 32501da177e4SLinus Torvalds } 32511da177e4SLinus Torvalds 32529ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 32539ce8ade0SThomas Graf { 3254612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 32559ce8ade0SThomas Graf } 32569ce8ade0SThomas Graf 3257ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb) 32589ce8ade0SThomas Graf { 3259adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 3260612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 32619ce8ade0SThomas Graf } 32629ce8ade0SThomas Graf 32631da177e4SLinus Torvalds /* 32641da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 32651da177e4SLinus Torvalds */ 32661da177e4SLinus Torvalds 32671da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, 32681da177e4SLinus Torvalds const struct in6_addr *addr, 32698f031519SDavid S. Miller bool anycast) 32701da177e4SLinus Torvalds { 3271ca254490SDavid Ahern u32 tb_id; 3272c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 32734832c30dSDavid Ahern struct net_device *dev = idev->dev; 32745f02ce24SDavid Ahern struct rt6_info *rt; 32755f02ce24SDavid Ahern 32765f02ce24SDavid Ahern rt = ip6_dst_alloc(net, dev, DST_NOCOUNT); 3277a3300ef4SHannes Frederic Sowa if (!rt) 32781da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 32791da177e4SLinus Torvalds 32801da177e4SLinus Torvalds in6_dev_hold(idev); 32811da177e4SLinus Torvalds 328211d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 3283d8d1f30bSChangli Gao rt->dst.input = ip6_input; 3284d8d1f30bSChangli Gao rt->dst.output = ip6_output; 32851da177e4SLinus Torvalds rt->rt6i_idev = idev; 32861da177e4SLinus Torvalds 328794b5e0f9SDavid Ahern rt->rt6i_protocol = RTPROT_KERNEL; 32881da177e4SLinus Torvalds rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP; 328958c4fb86SYOSHIFUJI Hideaki if (anycast) 329058c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 329158c4fb86SYOSHIFUJI Hideaki else 32921da177e4SLinus Torvalds rt->rt6i_flags |= RTF_LOCAL; 32931da177e4SLinus Torvalds 3294550bab42SJulian Anastasov rt->rt6i_gateway = *addr; 32954e3fd7a0SAlexey Dobriyan rt->rt6i_dst.addr = *addr; 32961da177e4SLinus Torvalds rt->rt6i_dst.plen = 128; 3297ca254490SDavid Ahern tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL; 3298ca254490SDavid Ahern rt->rt6i_table = fib6_get_table(net, tb_id); 32991da177e4SLinus Torvalds 33001da177e4SLinus Torvalds return rt; 33011da177e4SLinus Torvalds } 33021da177e4SLinus Torvalds 3303c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 3304c3968a85SDaniel Walter struct arg_dev_net_ip { 3305c3968a85SDaniel Walter struct net_device *dev; 3306c3968a85SDaniel Walter struct net *net; 3307c3968a85SDaniel Walter struct in6_addr *addr; 3308c3968a85SDaniel Walter }; 3309c3968a85SDaniel Walter 3310c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg) 3311c3968a85SDaniel Walter { 3312c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 3313c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 3314c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 3315c3968a85SDaniel Walter 3316d1918542SDavid S. Miller if (((void *)rt->dst.dev == dev || !dev) && 3317c3968a85SDaniel Walter rt != net->ipv6.ip6_null_entry && 3318c3968a85SDaniel Walter ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) { 331960006a48SWei Wang spin_lock_bh(&rt6_exception_lock); 3320c3968a85SDaniel Walter /* remove prefsrc entry */ 3321c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 332260006a48SWei Wang /* need to update cache as well */ 332360006a48SWei Wang rt6_exceptions_remove_prefsrc(rt); 332460006a48SWei Wang spin_unlock_bh(&rt6_exception_lock); 3325c3968a85SDaniel Walter } 3326c3968a85SDaniel Walter return 0; 3327c3968a85SDaniel Walter } 3328c3968a85SDaniel Walter 3329c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 3330c3968a85SDaniel Walter { 3331c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 3332c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 3333c3968a85SDaniel Walter .dev = ifp->idev->dev, 3334c3968a85SDaniel Walter .net = net, 3335c3968a85SDaniel Walter .addr = &ifp->addr, 3336c3968a85SDaniel Walter }; 33370c3584d5SLi RongQing fib6_clean_all(net, fib6_remove_prefsrc, &adni); 3338c3968a85SDaniel Walter } 3339c3968a85SDaniel Walter 3340be7a010dSDuan Jiong #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY) 3341be7a010dSDuan Jiong 3342be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */ 3343be7a010dSDuan Jiong static int fib6_clean_tohost(struct rt6_info *rt, void *arg) 3344be7a010dSDuan Jiong { 3345be7a010dSDuan Jiong struct in6_addr *gateway = (struct in6_addr *)arg; 3346be7a010dSDuan Jiong 3347b16cb459SWei Wang /* RTF_CACHE_GATEWAY case will be removed once the exception 3348b16cb459SWei Wang * table is hooked up to store all cached routes. 3349b16cb459SWei Wang */ 3350be7a010dSDuan Jiong if ((((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) || 3351be7a010dSDuan Jiong ((rt->rt6i_flags & RTF_CACHE_GATEWAY) == RTF_CACHE_GATEWAY)) && 3352be7a010dSDuan Jiong ipv6_addr_equal(gateway, &rt->rt6i_gateway)) { 3353be7a010dSDuan Jiong return -1; 3354be7a010dSDuan Jiong } 3355b16cb459SWei Wang 3356b16cb459SWei Wang /* Further clean up cached routes in exception table. 3357b16cb459SWei Wang * This is needed because cached route may have a different 3358b16cb459SWei Wang * gateway than its 'parent' in the case of an ip redirect. 3359b16cb459SWei Wang */ 3360b16cb459SWei Wang rt6_exceptions_clean_tohost(rt, gateway); 3361b16cb459SWei Wang 3362be7a010dSDuan Jiong return 0; 3363be7a010dSDuan Jiong } 3364be7a010dSDuan Jiong 3365be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) 3366be7a010dSDuan Jiong { 3367be7a010dSDuan Jiong fib6_clean_all(net, fib6_clean_tohost, gateway); 3368be7a010dSDuan Jiong } 3369be7a010dSDuan Jiong 33708ed67789SDaniel Lezcano struct arg_dev_net { 33718ed67789SDaniel Lezcano struct net_device *dev; 33728ed67789SDaniel Lezcano struct net *net; 33738ed67789SDaniel Lezcano }; 33748ed67789SDaniel Lezcano 3375a1a22c12SDavid Ahern /* called with write lock held for table with rt */ 33761da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg) 33771da177e4SLinus Torvalds { 3378bc3ef660Sstephen hemminger const struct arg_dev_net *adn = arg; 3379bc3ef660Sstephen hemminger const struct net_device *dev = adn->dev; 33808ed67789SDaniel Lezcano 3381d1918542SDavid S. Miller if ((rt->dst.dev == dev || !dev) && 3382a1a22c12SDavid Ahern rt != adn->net->ipv6.ip6_null_entry && 3383a1a22c12SDavid Ahern (rt->rt6i_nsiblings == 0 || 33848397ed36SDavid Ahern (dev && netdev_unregistering(dev)) || 3385a1a22c12SDavid Ahern !rt->rt6i_idev->cnf.ignore_routes_with_linkdown)) 33861da177e4SLinus Torvalds return -1; 3387c159d30cSDavid S. Miller 33881da177e4SLinus Torvalds return 0; 33891da177e4SLinus Torvalds } 33901da177e4SLinus Torvalds 3391f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev) 33921da177e4SLinus Torvalds { 33938ed67789SDaniel Lezcano struct arg_dev_net adn = { 33948ed67789SDaniel Lezcano .dev = dev, 33958ed67789SDaniel Lezcano .net = net, 33968ed67789SDaniel Lezcano }; 33978ed67789SDaniel Lezcano 33980c3584d5SLi RongQing fib6_clean_all(net, fib6_ifdown, &adn); 3399e332bc67SEric W. Biederman if (dev) 34008d0b94afSMartin KaFai Lau rt6_uncached_list_flush_dev(net, dev); 34011da177e4SLinus Torvalds } 34021da177e4SLinus Torvalds 340395c96174SEric Dumazet struct rt6_mtu_change_arg { 34041da177e4SLinus Torvalds struct net_device *dev; 340595c96174SEric Dumazet unsigned int mtu; 34061da177e4SLinus Torvalds }; 34071da177e4SLinus Torvalds 34081da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg) 34091da177e4SLinus Torvalds { 34101da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 34111da177e4SLinus Torvalds struct inet6_dev *idev; 34121da177e4SLinus Torvalds 34131da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 34141da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 34151da177e4SLinus Torvalds We still use this lock to block changes 34161da177e4SLinus Torvalds caused by addrconf/ndisc. 34171da177e4SLinus Torvalds */ 34181da177e4SLinus Torvalds 34191da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 342038308473SDavid S. Miller if (!idev) 34211da177e4SLinus Torvalds return 0; 34221da177e4SLinus Torvalds 34231da177e4SLinus Torvalds /* For administrative MTU increase, there is no way to discover 34241da177e4SLinus Torvalds IPv6 PMTU increase, so PMTU increase should be updated here. 34251da177e4SLinus Torvalds Since RFC 1981 doesn't include administrative MTU increase 34261da177e4SLinus Torvalds update PMTU increase is a MUST. (i.e. jumbo frame) 34271da177e4SLinus Torvalds */ 34281da177e4SLinus Torvalds /* 34291da177e4SLinus Torvalds If new MTU is less than route PMTU, this new MTU will be the 34301da177e4SLinus Torvalds lowest MTU in the path, update the route PMTU to reflect PMTU 34311da177e4SLinus Torvalds decreases; if new MTU is greater than route PMTU, and the 34321da177e4SLinus Torvalds old MTU is the lowest MTU in the path, update the route PMTU 34331da177e4SLinus Torvalds to reflect the increase. In this case if the other nodes' MTU 34341da177e4SLinus Torvalds also have the lowest MTU, TOO BIG MESSAGE will be lead to 343567c408cfSAlexander Alemayhu PMTU discovery. 34361da177e4SLinus Torvalds */ 3437d1918542SDavid S. Miller if (rt->dst.dev == arg->dev && 3438fb56be83SMaciej Żenczykowski dst_metric_raw(&rt->dst, RTAX_MTU) && 34394b32b5adSMartin KaFai Lau !dst_metric_locked(&rt->dst, RTAX_MTU)) { 3440f5bbe7eeSWei Wang spin_lock_bh(&rt6_exception_lock); 3441f5bbe7eeSWei Wang /* This case will be removed once the exception table 3442f5bbe7eeSWei Wang * is hooked up. 3443f5bbe7eeSWei Wang */ 34444b32b5adSMartin KaFai Lau if (rt->rt6i_flags & RTF_CACHE) { 34454b32b5adSMartin KaFai Lau /* For RTF_CACHE with rt6i_pmtu == 0 34464b32b5adSMartin KaFai Lau * (i.e. a redirected route), 34474b32b5adSMartin KaFai Lau * the metrics of its rt->dst.from has already 34484b32b5adSMartin KaFai Lau * been updated. 34494b32b5adSMartin KaFai Lau */ 34504b32b5adSMartin KaFai Lau if (rt->rt6i_pmtu && rt->rt6i_pmtu > arg->mtu) 34514b32b5adSMartin KaFai Lau rt->rt6i_pmtu = arg->mtu; 34524b32b5adSMartin KaFai Lau } else if (dst_mtu(&rt->dst) >= arg->mtu || 3453d8d1f30bSChangli Gao (dst_mtu(&rt->dst) < arg->mtu && 34544b32b5adSMartin KaFai Lau dst_mtu(&rt->dst) == idev->cnf.mtu6)) { 3455defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu); 3456566cfd8fSSimon Arlott } 3457f5bbe7eeSWei Wang rt6_exceptions_update_pmtu(rt, arg->mtu); 3458f5bbe7eeSWei Wang spin_unlock_bh(&rt6_exception_lock); 34594b32b5adSMartin KaFai Lau } 34601da177e4SLinus Torvalds return 0; 34611da177e4SLinus Torvalds } 34621da177e4SLinus Torvalds 346395c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu) 34641da177e4SLinus Torvalds { 3465c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 3466c71099acSThomas Graf .dev = dev, 3467c71099acSThomas Graf .mtu = mtu, 3468c71099acSThomas Graf }; 34691da177e4SLinus Torvalds 34700c3584d5SLi RongQing fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); 34711da177e4SLinus Torvalds } 34721da177e4SLinus Torvalds 3473ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 34745176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 347586872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 3476ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 347786872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 347886872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 347951ebd318SNicolas Dichtel [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 3480c78ba6d6SLubomir Rintel [RTA_PREF] = { .type = NLA_U8 }, 348119e42e45SRoopa Prabhu [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, 348219e42e45SRoopa Prabhu [RTA_ENCAP] = { .type = NLA_NESTED }, 348332bc201eSXin Long [RTA_EXPIRES] = { .type = NLA_U32 }, 3484622ec2c9SLorenzo Colitti [RTA_UID] = { .type = NLA_U32 }, 34853b45a410SLiping Zhang [RTA_MARK] = { .type = NLA_U32 }, 348686872cb5SThomas Graf }; 348786872cb5SThomas Graf 348886872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 3489333c4301SDavid Ahern struct fib6_config *cfg, 3490333c4301SDavid Ahern struct netlink_ext_ack *extack) 34911da177e4SLinus Torvalds { 349286872cb5SThomas Graf struct rtmsg *rtm; 349386872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 3494c78ba6d6SLubomir Rintel unsigned int pref; 349586872cb5SThomas Graf int err; 34961da177e4SLinus Torvalds 3497fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 3498fceb6435SJohannes Berg NULL); 349986872cb5SThomas Graf if (err < 0) 350086872cb5SThomas Graf goto errout; 35011da177e4SLinus Torvalds 350286872cb5SThomas Graf err = -EINVAL; 350386872cb5SThomas Graf rtm = nlmsg_data(nlh); 350486872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 350586872cb5SThomas Graf 350686872cb5SThomas Graf cfg->fc_table = rtm->rtm_table; 350786872cb5SThomas Graf cfg->fc_dst_len = rtm->rtm_dst_len; 350886872cb5SThomas Graf cfg->fc_src_len = rtm->rtm_src_len; 350986872cb5SThomas Graf cfg->fc_flags = RTF_UP; 351086872cb5SThomas Graf cfg->fc_protocol = rtm->rtm_protocol; 3511ef2c7d7bSNicolas Dichtel cfg->fc_type = rtm->rtm_type; 351286872cb5SThomas Graf 3513ef2c7d7bSNicolas Dichtel if (rtm->rtm_type == RTN_UNREACHABLE || 3514ef2c7d7bSNicolas Dichtel rtm->rtm_type == RTN_BLACKHOLE || 3515b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_PROHIBIT || 3516b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_THROW) 351786872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 351886872cb5SThomas Graf 3519ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 3520ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 3521ab79ad14SMaciej Żenczykowski 35221f56a01fSMartin KaFai Lau if (rtm->rtm_flags & RTM_F_CLONED) 35231f56a01fSMartin KaFai Lau cfg->fc_flags |= RTF_CACHE; 35241f56a01fSMartin KaFai Lau 352515e47304SEric W. Biederman cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid; 352686872cb5SThomas Graf cfg->fc_nlinfo.nlh = nlh; 35273b1e0a65SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = sock_net(skb->sk); 352886872cb5SThomas Graf 352986872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 353067b61f6cSJiri Benc cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); 353186872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 35321da177e4SLinus Torvalds } 353386872cb5SThomas Graf 353486872cb5SThomas Graf if (tb[RTA_DST]) { 353586872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 353686872cb5SThomas Graf 353786872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 353886872cb5SThomas Graf goto errout; 353986872cb5SThomas Graf 354086872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 35411da177e4SLinus Torvalds } 354286872cb5SThomas Graf 354386872cb5SThomas Graf if (tb[RTA_SRC]) { 354486872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 354586872cb5SThomas Graf 354686872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 354786872cb5SThomas Graf goto errout; 354886872cb5SThomas Graf 354986872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 35501da177e4SLinus Torvalds } 355186872cb5SThomas Graf 3552c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 355367b61f6cSJiri Benc cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); 3554c3968a85SDaniel Walter 355586872cb5SThomas Graf if (tb[RTA_OIF]) 355686872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 355786872cb5SThomas Graf 355886872cb5SThomas Graf if (tb[RTA_PRIORITY]) 355986872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 356086872cb5SThomas Graf 356186872cb5SThomas Graf if (tb[RTA_METRICS]) { 356286872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 356386872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 35641da177e4SLinus Torvalds } 356586872cb5SThomas Graf 356686872cb5SThomas Graf if (tb[RTA_TABLE]) 356786872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 356886872cb5SThomas Graf 356951ebd318SNicolas Dichtel if (tb[RTA_MULTIPATH]) { 357051ebd318SNicolas Dichtel cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); 357151ebd318SNicolas Dichtel cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); 35729ed59592SDavid Ahern 35739ed59592SDavid Ahern err = lwtunnel_valid_encap_type_attr(cfg->fc_mp, 3574c255bd68SDavid Ahern cfg->fc_mp_len, extack); 35759ed59592SDavid Ahern if (err < 0) 35769ed59592SDavid Ahern goto errout; 357751ebd318SNicolas Dichtel } 357851ebd318SNicolas Dichtel 3579c78ba6d6SLubomir Rintel if (tb[RTA_PREF]) { 3580c78ba6d6SLubomir Rintel pref = nla_get_u8(tb[RTA_PREF]); 3581c78ba6d6SLubomir Rintel if (pref != ICMPV6_ROUTER_PREF_LOW && 3582c78ba6d6SLubomir Rintel pref != ICMPV6_ROUTER_PREF_HIGH) 3583c78ba6d6SLubomir Rintel pref = ICMPV6_ROUTER_PREF_MEDIUM; 3584c78ba6d6SLubomir Rintel cfg->fc_flags |= RTF_PREF(pref); 3585c78ba6d6SLubomir Rintel } 3586c78ba6d6SLubomir Rintel 358719e42e45SRoopa Prabhu if (tb[RTA_ENCAP]) 358819e42e45SRoopa Prabhu cfg->fc_encap = tb[RTA_ENCAP]; 358919e42e45SRoopa Prabhu 35909ed59592SDavid Ahern if (tb[RTA_ENCAP_TYPE]) { 359119e42e45SRoopa Prabhu cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); 359219e42e45SRoopa Prabhu 3593c255bd68SDavid Ahern err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); 35949ed59592SDavid Ahern if (err < 0) 35959ed59592SDavid Ahern goto errout; 35969ed59592SDavid Ahern } 35979ed59592SDavid Ahern 359832bc201eSXin Long if (tb[RTA_EXPIRES]) { 359932bc201eSXin Long unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); 360032bc201eSXin Long 360132bc201eSXin Long if (addrconf_finite_timeout(timeout)) { 360232bc201eSXin Long cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); 360332bc201eSXin Long cfg->fc_flags |= RTF_EXPIRES; 360432bc201eSXin Long } 360532bc201eSXin Long } 360632bc201eSXin Long 360786872cb5SThomas Graf err = 0; 360886872cb5SThomas Graf errout: 360986872cb5SThomas Graf return err; 36101da177e4SLinus Torvalds } 36111da177e4SLinus Torvalds 36126b9ea5a6SRoopa Prabhu struct rt6_nh { 36136b9ea5a6SRoopa Prabhu struct rt6_info *rt6_info; 36146b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 36156b9ea5a6SRoopa Prabhu struct mx6_config mxc; 36166b9ea5a6SRoopa Prabhu struct list_head next; 36176b9ea5a6SRoopa Prabhu }; 36186b9ea5a6SRoopa Prabhu 36196b9ea5a6SRoopa Prabhu static void ip6_print_replace_route_err(struct list_head *rt6_nh_list) 36206b9ea5a6SRoopa Prabhu { 36216b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 36226b9ea5a6SRoopa Prabhu 36236b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 36247d4d5065SDavid Ahern pr_warn("IPV6: multipath route replace failed (check consistency of installed routes): %pI6c nexthop %pI6c ifi %d\n", 36256b9ea5a6SRoopa Prabhu &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway, 36266b9ea5a6SRoopa Prabhu nh->r_cfg.fc_ifindex); 36276b9ea5a6SRoopa Prabhu } 36286b9ea5a6SRoopa Prabhu } 36296b9ea5a6SRoopa Prabhu 36306b9ea5a6SRoopa Prabhu static int ip6_route_info_append(struct list_head *rt6_nh_list, 36316b9ea5a6SRoopa Prabhu struct rt6_info *rt, struct fib6_config *r_cfg) 36326b9ea5a6SRoopa Prabhu { 36336b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 36346b9ea5a6SRoopa Prabhu int err = -EEXIST; 36356b9ea5a6SRoopa Prabhu 36366b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 36376b9ea5a6SRoopa Prabhu /* check if rt6_info already exists */ 3638f06b7549SDavid Ahern if (rt6_duplicate_nexthop(nh->rt6_info, rt)) 36396b9ea5a6SRoopa Prabhu return err; 36406b9ea5a6SRoopa Prabhu } 36416b9ea5a6SRoopa Prabhu 36426b9ea5a6SRoopa Prabhu nh = kzalloc(sizeof(*nh), GFP_KERNEL); 36436b9ea5a6SRoopa Prabhu if (!nh) 36446b9ea5a6SRoopa Prabhu return -ENOMEM; 36456b9ea5a6SRoopa Prabhu nh->rt6_info = rt; 36466b9ea5a6SRoopa Prabhu err = ip6_convert_metrics(&nh->mxc, r_cfg); 36476b9ea5a6SRoopa Prabhu if (err) { 36486b9ea5a6SRoopa Prabhu kfree(nh); 36496b9ea5a6SRoopa Prabhu return err; 36506b9ea5a6SRoopa Prabhu } 36516b9ea5a6SRoopa Prabhu memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); 36526b9ea5a6SRoopa Prabhu list_add_tail(&nh->next, rt6_nh_list); 36536b9ea5a6SRoopa Prabhu 36546b9ea5a6SRoopa Prabhu return 0; 36556b9ea5a6SRoopa Prabhu } 36566b9ea5a6SRoopa Prabhu 36573b1137feSDavid Ahern static void ip6_route_mpath_notify(struct rt6_info *rt, 36583b1137feSDavid Ahern struct rt6_info *rt_last, 36593b1137feSDavid Ahern struct nl_info *info, 36603b1137feSDavid Ahern __u16 nlflags) 36613b1137feSDavid Ahern { 36623b1137feSDavid Ahern /* if this is an APPEND route, then rt points to the first route 36633b1137feSDavid Ahern * inserted and rt_last points to last route inserted. Userspace 36643b1137feSDavid Ahern * wants a consistent dump of the route which starts at the first 36653b1137feSDavid Ahern * nexthop. Since sibling routes are always added at the end of 36663b1137feSDavid Ahern * the list, find the first sibling of the last route appended 36673b1137feSDavid Ahern */ 36683b1137feSDavid Ahern if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->rt6i_nsiblings) { 36693b1137feSDavid Ahern rt = list_first_entry(&rt_last->rt6i_siblings, 36703b1137feSDavid Ahern struct rt6_info, 36713b1137feSDavid Ahern rt6i_siblings); 36723b1137feSDavid Ahern } 36733b1137feSDavid Ahern 36743b1137feSDavid Ahern if (rt) 36753b1137feSDavid Ahern inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); 36763b1137feSDavid Ahern } 36773b1137feSDavid Ahern 3678333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg, 3679333c4301SDavid Ahern struct netlink_ext_ack *extack) 368051ebd318SNicolas Dichtel { 36813b1137feSDavid Ahern struct rt6_info *rt_notif = NULL, *rt_last = NULL; 36823b1137feSDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 368351ebd318SNicolas Dichtel struct fib6_config r_cfg; 368451ebd318SNicolas Dichtel struct rtnexthop *rtnh; 36856b9ea5a6SRoopa Prabhu struct rt6_info *rt; 36866b9ea5a6SRoopa Prabhu struct rt6_nh *err_nh; 36876b9ea5a6SRoopa Prabhu struct rt6_nh *nh, *nh_safe; 36883b1137feSDavid Ahern __u16 nlflags; 368951ebd318SNicolas Dichtel int remaining; 369051ebd318SNicolas Dichtel int attrlen; 36916b9ea5a6SRoopa Prabhu int err = 1; 36926b9ea5a6SRoopa Prabhu int nhn = 0; 36936b9ea5a6SRoopa Prabhu int replace = (cfg->fc_nlinfo.nlh && 36946b9ea5a6SRoopa Prabhu (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); 36956b9ea5a6SRoopa Prabhu LIST_HEAD(rt6_nh_list); 369651ebd318SNicolas Dichtel 36973b1137feSDavid Ahern nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; 36983b1137feSDavid Ahern if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) 36993b1137feSDavid Ahern nlflags |= NLM_F_APPEND; 37003b1137feSDavid Ahern 370135f1b4e9SMichal Kubeček remaining = cfg->fc_mp_len; 370251ebd318SNicolas Dichtel rtnh = (struct rtnexthop *)cfg->fc_mp; 370351ebd318SNicolas Dichtel 37046b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry and build a list (rt6_nh_list) of 37056b9ea5a6SRoopa Prabhu * rt6_info structs per nexthop 37066b9ea5a6SRoopa Prabhu */ 370751ebd318SNicolas Dichtel while (rtnh_ok(rtnh, remaining)) { 370851ebd318SNicolas Dichtel memcpy(&r_cfg, cfg, sizeof(*cfg)); 370951ebd318SNicolas Dichtel if (rtnh->rtnh_ifindex) 371051ebd318SNicolas Dichtel r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 371151ebd318SNicolas Dichtel 371251ebd318SNicolas Dichtel attrlen = rtnh_attrlen(rtnh); 371351ebd318SNicolas Dichtel if (attrlen > 0) { 371451ebd318SNicolas Dichtel struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 371551ebd318SNicolas Dichtel 371651ebd318SNicolas Dichtel nla = nla_find(attrs, attrlen, RTA_GATEWAY); 371751ebd318SNicolas Dichtel if (nla) { 371867b61f6cSJiri Benc r_cfg.fc_gateway = nla_get_in6_addr(nla); 371951ebd318SNicolas Dichtel r_cfg.fc_flags |= RTF_GATEWAY; 372051ebd318SNicolas Dichtel } 372119e42e45SRoopa Prabhu r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); 372219e42e45SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); 372319e42e45SRoopa Prabhu if (nla) 372419e42e45SRoopa Prabhu r_cfg.fc_encap_type = nla_get_u16(nla); 372551ebd318SNicolas Dichtel } 37266b9ea5a6SRoopa Prabhu 3727333c4301SDavid Ahern rt = ip6_route_info_create(&r_cfg, extack); 37288c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 37298c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 37308c5b83f0SRoopa Prabhu rt = NULL; 37316b9ea5a6SRoopa Prabhu goto cleanup; 37328c5b83f0SRoopa Prabhu } 37336b9ea5a6SRoopa Prabhu 37346b9ea5a6SRoopa Prabhu err = ip6_route_info_append(&rt6_nh_list, rt, &r_cfg); 373551ebd318SNicolas Dichtel if (err) { 3736587fea74SWei Wang dst_release_immediate(&rt->dst); 37376b9ea5a6SRoopa Prabhu goto cleanup; 373851ebd318SNicolas Dichtel } 37396b9ea5a6SRoopa Prabhu 37406b9ea5a6SRoopa Prabhu rtnh = rtnh_next(rtnh, &remaining); 374151ebd318SNicolas Dichtel } 37426b9ea5a6SRoopa Prabhu 37433b1137feSDavid Ahern /* for add and replace send one notification with all nexthops. 37443b1137feSDavid Ahern * Skip the notification in fib6_add_rt2node and send one with 37453b1137feSDavid Ahern * the full route when done 37463b1137feSDavid Ahern */ 37473b1137feSDavid Ahern info->skip_notify = 1; 37483b1137feSDavid Ahern 37496b9ea5a6SRoopa Prabhu err_nh = NULL; 37506b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 37513b1137feSDavid Ahern rt_last = nh->rt6_info; 3752333c4301SDavid Ahern err = __ip6_ins_rt(nh->rt6_info, info, &nh->mxc, extack); 37533b1137feSDavid Ahern /* save reference to first route for notification */ 37543b1137feSDavid Ahern if (!rt_notif && !err) 37553b1137feSDavid Ahern rt_notif = nh->rt6_info; 37563b1137feSDavid Ahern 37576b9ea5a6SRoopa Prabhu /* nh->rt6_info is used or freed at this point, reset to NULL*/ 37586b9ea5a6SRoopa Prabhu nh->rt6_info = NULL; 37596b9ea5a6SRoopa Prabhu if (err) { 37606b9ea5a6SRoopa Prabhu if (replace && nhn) 37616b9ea5a6SRoopa Prabhu ip6_print_replace_route_err(&rt6_nh_list); 37626b9ea5a6SRoopa Prabhu err_nh = nh; 37636b9ea5a6SRoopa Prabhu goto add_errout; 37646b9ea5a6SRoopa Prabhu } 37656b9ea5a6SRoopa Prabhu 37661a72418bSNicolas Dichtel /* Because each route is added like a single route we remove 376727596472SMichal Kubeček * these flags after the first nexthop: if there is a collision, 376827596472SMichal Kubeček * we have already failed to add the first nexthop: 376927596472SMichal Kubeček * fib6_add_rt2node() has rejected it; when replacing, old 377027596472SMichal Kubeček * nexthops have been replaced by first new, the rest should 377127596472SMichal Kubeček * be added to it. 37721a72418bSNicolas Dichtel */ 377327596472SMichal Kubeček cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | 377427596472SMichal Kubeček NLM_F_REPLACE); 37756b9ea5a6SRoopa Prabhu nhn++; 37766b9ea5a6SRoopa Prabhu } 37776b9ea5a6SRoopa Prabhu 37783b1137feSDavid Ahern /* success ... tell user about new route */ 37793b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 37806b9ea5a6SRoopa Prabhu goto cleanup; 37816b9ea5a6SRoopa Prabhu 37826b9ea5a6SRoopa Prabhu add_errout: 37833b1137feSDavid Ahern /* send notification for routes that were added so that 37843b1137feSDavid Ahern * the delete notifications sent by ip6_route_del are 37853b1137feSDavid Ahern * coherent 37863b1137feSDavid Ahern */ 37873b1137feSDavid Ahern if (rt_notif) 37883b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 37893b1137feSDavid Ahern 37906b9ea5a6SRoopa Prabhu /* Delete routes that were already added */ 37916b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 37926b9ea5a6SRoopa Prabhu if (err_nh == nh) 37936b9ea5a6SRoopa Prabhu break; 3794333c4301SDavid Ahern ip6_route_del(&nh->r_cfg, extack); 37956b9ea5a6SRoopa Prabhu } 37966b9ea5a6SRoopa Prabhu 37976b9ea5a6SRoopa Prabhu cleanup: 37986b9ea5a6SRoopa Prabhu list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) { 3799587fea74SWei Wang if (nh->rt6_info) 3800587fea74SWei Wang dst_release_immediate(&nh->rt6_info->dst); 38016b9ea5a6SRoopa Prabhu kfree(nh->mxc.mx); 38026b9ea5a6SRoopa Prabhu list_del(&nh->next); 38036b9ea5a6SRoopa Prabhu kfree(nh); 38046b9ea5a6SRoopa Prabhu } 38056b9ea5a6SRoopa Prabhu 38066b9ea5a6SRoopa Prabhu return err; 38076b9ea5a6SRoopa Prabhu } 38086b9ea5a6SRoopa Prabhu 3809333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg, 3810333c4301SDavid Ahern struct netlink_ext_ack *extack) 38116b9ea5a6SRoopa Prabhu { 38126b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 38136b9ea5a6SRoopa Prabhu struct rtnexthop *rtnh; 38146b9ea5a6SRoopa Prabhu int remaining; 38156b9ea5a6SRoopa Prabhu int attrlen; 38166b9ea5a6SRoopa Prabhu int err = 1, last_err = 0; 38176b9ea5a6SRoopa Prabhu 38186b9ea5a6SRoopa Prabhu remaining = cfg->fc_mp_len; 38196b9ea5a6SRoopa Prabhu rtnh = (struct rtnexthop *)cfg->fc_mp; 38206b9ea5a6SRoopa Prabhu 38216b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry */ 38226b9ea5a6SRoopa Prabhu while (rtnh_ok(rtnh, remaining)) { 38236b9ea5a6SRoopa Prabhu memcpy(&r_cfg, cfg, sizeof(*cfg)); 38246b9ea5a6SRoopa Prabhu if (rtnh->rtnh_ifindex) 38256b9ea5a6SRoopa Prabhu r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 38266b9ea5a6SRoopa Prabhu 38276b9ea5a6SRoopa Prabhu attrlen = rtnh_attrlen(rtnh); 38286b9ea5a6SRoopa Prabhu if (attrlen > 0) { 38296b9ea5a6SRoopa Prabhu struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 38306b9ea5a6SRoopa Prabhu 38316b9ea5a6SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_GATEWAY); 38326b9ea5a6SRoopa Prabhu if (nla) { 38336b9ea5a6SRoopa Prabhu nla_memcpy(&r_cfg.fc_gateway, nla, 16); 38346b9ea5a6SRoopa Prabhu r_cfg.fc_flags |= RTF_GATEWAY; 38356b9ea5a6SRoopa Prabhu } 38366b9ea5a6SRoopa Prabhu } 3837333c4301SDavid Ahern err = ip6_route_del(&r_cfg, extack); 38386b9ea5a6SRoopa Prabhu if (err) 38396b9ea5a6SRoopa Prabhu last_err = err; 38406b9ea5a6SRoopa Prabhu 384151ebd318SNicolas Dichtel rtnh = rtnh_next(rtnh, &remaining); 384251ebd318SNicolas Dichtel } 384351ebd318SNicolas Dichtel 384451ebd318SNicolas Dichtel return last_err; 384551ebd318SNicolas Dichtel } 384651ebd318SNicolas Dichtel 3847c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, 3848c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 38491da177e4SLinus Torvalds { 385086872cb5SThomas Graf struct fib6_config cfg; 385186872cb5SThomas Graf int err; 38521da177e4SLinus Torvalds 3853333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 385486872cb5SThomas Graf if (err < 0) 385586872cb5SThomas Graf return err; 385686872cb5SThomas Graf 385751ebd318SNicolas Dichtel if (cfg.fc_mp) 3858333c4301SDavid Ahern return ip6_route_multipath_del(&cfg, extack); 38590ae81335SDavid Ahern else { 38600ae81335SDavid Ahern cfg.fc_delete_all_nh = 1; 3861333c4301SDavid Ahern return ip6_route_del(&cfg, extack); 38621da177e4SLinus Torvalds } 38630ae81335SDavid Ahern } 38641da177e4SLinus Torvalds 3865c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, 3866c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 38671da177e4SLinus Torvalds { 386886872cb5SThomas Graf struct fib6_config cfg; 386986872cb5SThomas Graf int err; 38701da177e4SLinus Torvalds 3871333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 387286872cb5SThomas Graf if (err < 0) 387386872cb5SThomas Graf return err; 387486872cb5SThomas Graf 387551ebd318SNicolas Dichtel if (cfg.fc_mp) 3876333c4301SDavid Ahern return ip6_route_multipath_add(&cfg, extack); 387751ebd318SNicolas Dichtel else 3878333c4301SDavid Ahern return ip6_route_add(&cfg, extack); 38791da177e4SLinus Torvalds } 38801da177e4SLinus Torvalds 3881beb1afacSDavid Ahern static size_t rt6_nlmsg_size(struct rt6_info *rt) 3882339bf98fSThomas Graf { 3883beb1afacSDavid Ahern int nexthop_len = 0; 3884beb1afacSDavid Ahern 3885beb1afacSDavid Ahern if (rt->rt6i_nsiblings) { 3886beb1afacSDavid Ahern nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */ 3887beb1afacSDavid Ahern + NLA_ALIGN(sizeof(struct rtnexthop)) 3888beb1afacSDavid Ahern + nla_total_size(16) /* RTA_GATEWAY */ 3889beb1afacSDavid Ahern + lwtunnel_get_encap_size(rt->dst.lwtstate); 3890beb1afacSDavid Ahern 3891beb1afacSDavid Ahern nexthop_len *= rt->rt6i_nsiblings; 3892beb1afacSDavid Ahern } 3893beb1afacSDavid Ahern 3894339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 3895339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 3896339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 3897339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 3898339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 3899339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 3900339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 3901339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 3902339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 39036a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 3904ea697639SDaniel Borkmann + nla_total_size(sizeof(struct rta_cacheinfo)) 3905c78ba6d6SLubomir Rintel + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ 390619e42e45SRoopa Prabhu + nla_total_size(1) /* RTA_PREF */ 3907beb1afacSDavid Ahern + lwtunnel_get_encap_size(rt->dst.lwtstate) 3908beb1afacSDavid Ahern + nexthop_len; 3909beb1afacSDavid Ahern } 3910beb1afacSDavid Ahern 3911beb1afacSDavid Ahern static int rt6_nexthop_info(struct sk_buff *skb, struct rt6_info *rt, 39125be083ceSDavid Ahern unsigned int *flags, bool skip_oif) 3913beb1afacSDavid Ahern { 3914beb1afacSDavid Ahern if (!netif_running(rt->dst.dev) || !netif_carrier_ok(rt->dst.dev)) { 3915beb1afacSDavid Ahern *flags |= RTNH_F_LINKDOWN; 3916beb1afacSDavid Ahern if (rt->rt6i_idev->cnf.ignore_routes_with_linkdown) 3917beb1afacSDavid Ahern *flags |= RTNH_F_DEAD; 3918beb1afacSDavid Ahern } 3919beb1afacSDavid Ahern 3920beb1afacSDavid Ahern if (rt->rt6i_flags & RTF_GATEWAY) { 3921beb1afacSDavid Ahern if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0) 3922beb1afacSDavid Ahern goto nla_put_failure; 3923beb1afacSDavid Ahern } 3924beb1afacSDavid Ahern 3925fe400799SIdo Schimmel if (rt->rt6i_nh_flags & RTNH_F_OFFLOAD) 392661e4d01eSIdo Schimmel *flags |= RTNH_F_OFFLOAD; 392761e4d01eSIdo Schimmel 39285be083ceSDavid Ahern /* not needed for multipath encoding b/c it has a rtnexthop struct */ 39295be083ceSDavid Ahern if (!skip_oif && rt->dst.dev && 3930beb1afacSDavid Ahern nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex)) 3931beb1afacSDavid Ahern goto nla_put_failure; 3932beb1afacSDavid Ahern 3933beb1afacSDavid Ahern if (rt->dst.lwtstate && 3934beb1afacSDavid Ahern lwtunnel_fill_encap(skb, rt->dst.lwtstate) < 0) 3935beb1afacSDavid Ahern goto nla_put_failure; 3936beb1afacSDavid Ahern 3937beb1afacSDavid Ahern return 0; 3938beb1afacSDavid Ahern 3939beb1afacSDavid Ahern nla_put_failure: 3940beb1afacSDavid Ahern return -EMSGSIZE; 3941beb1afacSDavid Ahern } 3942beb1afacSDavid Ahern 39435be083ceSDavid Ahern /* add multipath next hop */ 3944beb1afacSDavid Ahern static int rt6_add_nexthop(struct sk_buff *skb, struct rt6_info *rt) 3945beb1afacSDavid Ahern { 3946beb1afacSDavid Ahern struct rtnexthop *rtnh; 3947beb1afacSDavid Ahern unsigned int flags = 0; 3948beb1afacSDavid Ahern 3949beb1afacSDavid Ahern rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh)); 3950beb1afacSDavid Ahern if (!rtnh) 3951beb1afacSDavid Ahern goto nla_put_failure; 3952beb1afacSDavid Ahern 3953beb1afacSDavid Ahern rtnh->rtnh_hops = 0; 3954beb1afacSDavid Ahern rtnh->rtnh_ifindex = rt->dst.dev ? rt->dst.dev->ifindex : 0; 3955beb1afacSDavid Ahern 39565be083ceSDavid Ahern if (rt6_nexthop_info(skb, rt, &flags, true) < 0) 3957beb1afacSDavid Ahern goto nla_put_failure; 3958beb1afacSDavid Ahern 3959beb1afacSDavid Ahern rtnh->rtnh_flags = flags; 3960beb1afacSDavid Ahern 3961beb1afacSDavid Ahern /* length of rtnetlink header + attributes */ 3962beb1afacSDavid Ahern rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh; 3963beb1afacSDavid Ahern 3964beb1afacSDavid Ahern return 0; 3965beb1afacSDavid Ahern 3966beb1afacSDavid Ahern nla_put_failure: 3967beb1afacSDavid Ahern return -EMSGSIZE; 3968339bf98fSThomas Graf } 3969339bf98fSThomas Graf 3970191cd582SBrian Haley static int rt6_fill_node(struct net *net, 3971191cd582SBrian Haley struct sk_buff *skb, struct rt6_info *rt, 39720d51aa80SJamal Hadi Salim struct in6_addr *dst, struct in6_addr *src, 397315e47304SEric W. Biederman int iif, int type, u32 portid, u32 seq, 3974f8cfe2ceSDavid Ahern unsigned int flags) 39751da177e4SLinus Torvalds { 39764b32b5adSMartin KaFai Lau u32 metrics[RTAX_MAX]; 39771da177e4SLinus Torvalds struct rtmsg *rtm; 39781da177e4SLinus Torvalds struct nlmsghdr *nlh; 3979e3703b3dSThomas Graf long expires; 39809e762a4aSPatrick McHardy u32 table; 39811da177e4SLinus Torvalds 398215e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); 398338308473SDavid S. Miller if (!nlh) 398426932566SPatrick McHardy return -EMSGSIZE; 39852d7202bfSThomas Graf 39862d7202bfSThomas Graf rtm = nlmsg_data(nlh); 39871da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 39881da177e4SLinus Torvalds rtm->rtm_dst_len = rt->rt6i_dst.plen; 39891da177e4SLinus Torvalds rtm->rtm_src_len = rt->rt6i_src.plen; 39901da177e4SLinus Torvalds rtm->rtm_tos = 0; 3991c71099acSThomas Graf if (rt->rt6i_table) 39929e762a4aSPatrick McHardy table = rt->rt6i_table->tb6_id; 3993c71099acSThomas Graf else 39949e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 39959e762a4aSPatrick McHardy rtm->rtm_table = table; 3996c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, table)) 3997c78679e8SDavid S. Miller goto nla_put_failure; 3998ef2c7d7bSNicolas Dichtel if (rt->rt6i_flags & RTF_REJECT) { 3999ef2c7d7bSNicolas Dichtel switch (rt->dst.error) { 4000ef2c7d7bSNicolas Dichtel case -EINVAL: 4001ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_BLACKHOLE; 4002ef2c7d7bSNicolas Dichtel break; 4003ef2c7d7bSNicolas Dichtel case -EACCES: 4004ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_PROHIBIT; 4005ef2c7d7bSNicolas Dichtel break; 4006b4949ab2SNicolas Dichtel case -EAGAIN: 4007b4949ab2SNicolas Dichtel rtm->rtm_type = RTN_THROW; 4008b4949ab2SNicolas Dichtel break; 4009ef2c7d7bSNicolas Dichtel default: 40101da177e4SLinus Torvalds rtm->rtm_type = RTN_UNREACHABLE; 4011ef2c7d7bSNicolas Dichtel break; 4012ef2c7d7bSNicolas Dichtel } 4013ef2c7d7bSNicolas Dichtel } 4014ab79ad14SMaciej Żenczykowski else if (rt->rt6i_flags & RTF_LOCAL) 4015ab79ad14SMaciej Żenczykowski rtm->rtm_type = RTN_LOCAL; 40164ee39733SDavid Ahern else if (rt->rt6i_flags & RTF_ANYCAST) 40174ee39733SDavid Ahern rtm->rtm_type = RTN_ANYCAST; 4018d1918542SDavid S. Miller else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK)) 40191da177e4SLinus Torvalds rtm->rtm_type = RTN_LOCAL; 40201da177e4SLinus Torvalds else 40211da177e4SLinus Torvalds rtm->rtm_type = RTN_UNICAST; 40221da177e4SLinus Torvalds rtm->rtm_flags = 0; 40231da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 40241da177e4SLinus Torvalds rtm->rtm_protocol = rt->rt6i_protocol; 40251da177e4SLinus Torvalds 40261da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) 40271da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 40281da177e4SLinus Torvalds 40291da177e4SLinus Torvalds if (dst) { 4030930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_DST, dst)) 4031c78679e8SDavid S. Miller goto nla_put_failure; 40321da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 40331da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 4034930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr)) 4035c78679e8SDavid S. Miller goto nla_put_failure; 40361da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 40371da177e4SLinus Torvalds if (src) { 4038930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_SRC, src)) 4039c78679e8SDavid S. Miller goto nla_put_failure; 40401da177e4SLinus Torvalds rtm->rtm_src_len = 128; 4041c78679e8SDavid S. Miller } else if (rtm->rtm_src_len && 4042930345eaSJiri Benc nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr)) 4043c78679e8SDavid S. Miller goto nla_put_failure; 40441da177e4SLinus Torvalds #endif 40457bc570c8SYOSHIFUJI Hideaki if (iif) { 40467bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 40477bc570c8SYOSHIFUJI Hideaki if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) { 4048fd61c6baSDavid Ahern int err = ip6mr_get_route(net, skb, rtm, portid); 40492cf75070SNikolay Aleksandrov 40507bc570c8SYOSHIFUJI Hideaki if (err == 0) 40517bc570c8SYOSHIFUJI Hideaki return 0; 4052fd61c6baSDavid Ahern if (err < 0) 40537bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 40547bc570c8SYOSHIFUJI Hideaki } else 40557bc570c8SYOSHIFUJI Hideaki #endif 4056c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_IIF, iif)) 4057c78679e8SDavid S. Miller goto nla_put_failure; 40587bc570c8SYOSHIFUJI Hideaki } else if (dst) { 40591da177e4SLinus Torvalds struct in6_addr saddr_buf; 4060c78679e8SDavid S. Miller if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 && 4061930345eaSJiri Benc nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 4062c78679e8SDavid S. Miller goto nla_put_failure; 4063c3968a85SDaniel Walter } 4064c3968a85SDaniel Walter 4065c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) { 4066c3968a85SDaniel Walter struct in6_addr saddr_buf; 40674e3fd7a0SAlexey Dobriyan saddr_buf = rt->rt6i_prefsrc.addr; 4068930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 4069c78679e8SDavid S. Miller goto nla_put_failure; 40701da177e4SLinus Torvalds } 40712d7202bfSThomas Graf 40724b32b5adSMartin KaFai Lau memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics)); 40734b32b5adSMartin KaFai Lau if (rt->rt6i_pmtu) 40744b32b5adSMartin KaFai Lau metrics[RTAX_MTU - 1] = rt->rt6i_pmtu; 40754b32b5adSMartin KaFai Lau if (rtnetlink_put_metrics(skb, metrics) < 0) 40762d7202bfSThomas Graf goto nla_put_failure; 40772d7202bfSThomas Graf 4078beb1afacSDavid Ahern if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric)) 4079beb1afacSDavid Ahern goto nla_put_failure; 4080beb1afacSDavid Ahern 4081beb1afacSDavid Ahern /* For multipath routes, walk the siblings list and add 4082beb1afacSDavid Ahern * each as a nexthop within RTA_MULTIPATH. 4083beb1afacSDavid Ahern */ 4084beb1afacSDavid Ahern if (rt->rt6i_nsiblings) { 4085beb1afacSDavid Ahern struct rt6_info *sibling, *next_sibling; 4086beb1afacSDavid Ahern struct nlattr *mp; 4087beb1afacSDavid Ahern 4088beb1afacSDavid Ahern mp = nla_nest_start(skb, RTA_MULTIPATH); 4089beb1afacSDavid Ahern if (!mp) 4090beb1afacSDavid Ahern goto nla_put_failure; 4091beb1afacSDavid Ahern 4092beb1afacSDavid Ahern if (rt6_add_nexthop(skb, rt) < 0) 4093beb1afacSDavid Ahern goto nla_put_failure; 4094beb1afacSDavid Ahern 4095beb1afacSDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 4096beb1afacSDavid Ahern &rt->rt6i_siblings, rt6i_siblings) { 4097beb1afacSDavid Ahern if (rt6_add_nexthop(skb, sibling) < 0) 409894f826b8SEric Dumazet goto nla_put_failure; 409994f826b8SEric Dumazet } 41002d7202bfSThomas Graf 4101beb1afacSDavid Ahern nla_nest_end(skb, mp); 4102beb1afacSDavid Ahern } else { 41035be083ceSDavid Ahern if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0) 4104c78679e8SDavid S. Miller goto nla_put_failure; 4105beb1afacSDavid Ahern } 41068253947eSLi Wei 41078253947eSLi Wei expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0; 410869cdf8f9SYOSHIFUJI Hideaki 410987a50699SDavid S. Miller if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0) 4110e3703b3dSThomas Graf goto nla_put_failure; 41111da177e4SLinus Torvalds 4112c78ba6d6SLubomir Rintel if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags))) 4113c78ba6d6SLubomir Rintel goto nla_put_failure; 4114c78ba6d6SLubomir Rintel 411519e42e45SRoopa Prabhu 4116053c095aSJohannes Berg nlmsg_end(skb, nlh); 4117053c095aSJohannes Berg return 0; 41182d7202bfSThomas Graf 41192d7202bfSThomas Graf nla_put_failure: 412026932566SPatrick McHardy nlmsg_cancel(skb, nlh); 412126932566SPatrick McHardy return -EMSGSIZE; 41221da177e4SLinus Torvalds } 41231da177e4SLinus Torvalds 41241b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg) 41251da177e4SLinus Torvalds { 41261da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 41271f17e2f2SDavid Ahern struct net *net = arg->net; 41281f17e2f2SDavid Ahern 41291f17e2f2SDavid Ahern if (rt == net->ipv6.ip6_null_entry) 41301f17e2f2SDavid Ahern return 0; 41311da177e4SLinus Torvalds 41322d7202bfSThomas Graf if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { 41332d7202bfSThomas Graf struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); 4134f8cfe2ceSDavid Ahern 4135f8cfe2ceSDavid Ahern /* user wants prefix routes only */ 4136f8cfe2ceSDavid Ahern if (rtm->rtm_flags & RTM_F_PREFIX && 4137f8cfe2ceSDavid Ahern !(rt->rt6i_flags & RTF_PREFIX_RT)) { 4138f8cfe2ceSDavid Ahern /* success since this is not a prefix route */ 4139f8cfe2ceSDavid Ahern return 1; 4140f8cfe2ceSDavid Ahern } 4141f8cfe2ceSDavid Ahern } 41421da177e4SLinus Torvalds 41431f17e2f2SDavid Ahern return rt6_fill_node(net, 4144191cd582SBrian Haley arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE, 414515e47304SEric W. Biederman NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq, 4146f8cfe2ceSDavid Ahern NLM_F_MULTI); 41471da177e4SLinus Torvalds } 41481da177e4SLinus Torvalds 4149c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, 4150c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 41511da177e4SLinus Torvalds { 41523b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 4153ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 415418c3a61cSRoopa Prabhu int err, iif = 0, oif = 0; 415518c3a61cSRoopa Prabhu struct dst_entry *dst; 41561da177e4SLinus Torvalds struct rt6_info *rt; 4157ab364a6fSThomas Graf struct sk_buff *skb; 4158ab364a6fSThomas Graf struct rtmsg *rtm; 41594c9483b2SDavid S. Miller struct flowi6 fl6; 416018c3a61cSRoopa Prabhu bool fibmatch; 4161ab364a6fSThomas Graf 4162fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 4163c21ef3e3SDavid Ahern extack); 4164ab364a6fSThomas Graf if (err < 0) 4165ab364a6fSThomas Graf goto errout; 4166ab364a6fSThomas Graf 4167ab364a6fSThomas Graf err = -EINVAL; 41684c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 416938b7097bSHannes Frederic Sowa rtm = nlmsg_data(nlh); 417038b7097bSHannes Frederic Sowa fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0); 417118c3a61cSRoopa Prabhu fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); 4172ab364a6fSThomas Graf 4173ab364a6fSThomas Graf if (tb[RTA_SRC]) { 4174ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 4175ab364a6fSThomas Graf goto errout; 4176ab364a6fSThomas Graf 41774e3fd7a0SAlexey Dobriyan fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 4178ab364a6fSThomas Graf } 4179ab364a6fSThomas Graf 4180ab364a6fSThomas Graf if (tb[RTA_DST]) { 4181ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 4182ab364a6fSThomas Graf goto errout; 4183ab364a6fSThomas Graf 41844e3fd7a0SAlexey Dobriyan fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 4185ab364a6fSThomas Graf } 4186ab364a6fSThomas Graf 4187ab364a6fSThomas Graf if (tb[RTA_IIF]) 4188ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 4189ab364a6fSThomas Graf 4190ab364a6fSThomas Graf if (tb[RTA_OIF]) 419172331bc0SShmulik Ladkani oif = nla_get_u32(tb[RTA_OIF]); 4192ab364a6fSThomas Graf 41932e47b291SLorenzo Colitti if (tb[RTA_MARK]) 41942e47b291SLorenzo Colitti fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); 41952e47b291SLorenzo Colitti 4196622ec2c9SLorenzo Colitti if (tb[RTA_UID]) 4197622ec2c9SLorenzo Colitti fl6.flowi6_uid = make_kuid(current_user_ns(), 4198622ec2c9SLorenzo Colitti nla_get_u32(tb[RTA_UID])); 4199622ec2c9SLorenzo Colitti else 4200622ec2c9SLorenzo Colitti fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); 4201622ec2c9SLorenzo Colitti 4202ab364a6fSThomas Graf if (iif) { 4203ab364a6fSThomas Graf struct net_device *dev; 420472331bc0SShmulik Ladkani int flags = 0; 420572331bc0SShmulik Ladkani 4206121622dbSFlorian Westphal rcu_read_lock(); 4207121622dbSFlorian Westphal 4208121622dbSFlorian Westphal dev = dev_get_by_index_rcu(net, iif); 4209ab364a6fSThomas Graf if (!dev) { 4210121622dbSFlorian Westphal rcu_read_unlock(); 4211ab364a6fSThomas Graf err = -ENODEV; 4212ab364a6fSThomas Graf goto errout; 4213ab364a6fSThomas Graf } 421472331bc0SShmulik Ladkani 421572331bc0SShmulik Ladkani fl6.flowi6_iif = iif; 421672331bc0SShmulik Ladkani 421772331bc0SShmulik Ladkani if (!ipv6_addr_any(&fl6.saddr)) 421872331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_HAS_SADDR; 421972331bc0SShmulik Ladkani 422018c3a61cSRoopa Prabhu if (!fibmatch) 422118c3a61cSRoopa Prabhu dst = ip6_route_input_lookup(net, dev, &fl6, flags); 4222401481e0SArnd Bergmann else 4223401481e0SArnd Bergmann dst = ip6_route_lookup(net, &fl6, 0); 4224121622dbSFlorian Westphal 4225121622dbSFlorian Westphal rcu_read_unlock(); 422672331bc0SShmulik Ladkani } else { 422772331bc0SShmulik Ladkani fl6.flowi6_oif = oif; 422872331bc0SShmulik Ladkani 422918c3a61cSRoopa Prabhu if (!fibmatch) 423018c3a61cSRoopa Prabhu dst = ip6_route_output(net, NULL, &fl6); 4231401481e0SArnd Bergmann else 4232401481e0SArnd Bergmann dst = ip6_route_lookup(net, &fl6, 0); 423318c3a61cSRoopa Prabhu } 423418c3a61cSRoopa Prabhu 423518c3a61cSRoopa Prabhu 423618c3a61cSRoopa Prabhu rt = container_of(dst, struct rt6_info, dst); 423718c3a61cSRoopa Prabhu if (rt->dst.error) { 423818c3a61cSRoopa Prabhu err = rt->dst.error; 423918c3a61cSRoopa Prabhu ip6_rt_put(rt); 424018c3a61cSRoopa Prabhu goto errout; 4241ab364a6fSThomas Graf } 42421da177e4SLinus Torvalds 42439d6acb3bSWANG Cong if (rt == net->ipv6.ip6_null_entry) { 42449d6acb3bSWANG Cong err = rt->dst.error; 42459d6acb3bSWANG Cong ip6_rt_put(rt); 42469d6acb3bSWANG Cong goto errout; 42479d6acb3bSWANG Cong } 42489d6acb3bSWANG Cong 42491da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 425038308473SDavid S. Miller if (!skb) { 425194e187c0SAmerigo Wang ip6_rt_put(rt); 4252ab364a6fSThomas Graf err = -ENOBUFS; 4253ab364a6fSThomas Graf goto errout; 4254ab364a6fSThomas Graf } 42551da177e4SLinus Torvalds 4256d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 425718c3a61cSRoopa Prabhu if (fibmatch) 425818c3a61cSRoopa Prabhu err = rt6_fill_node(net, skb, rt, NULL, NULL, iif, 425918c3a61cSRoopa Prabhu RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 426018c3a61cSRoopa Prabhu nlh->nlmsg_seq, 0); 426118c3a61cSRoopa Prabhu else 42624c9483b2SDavid S. Miller err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif, 426315e47304SEric W. Biederman RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 4264f8cfe2ceSDavid Ahern nlh->nlmsg_seq, 0); 42651da177e4SLinus Torvalds if (err < 0) { 4266ab364a6fSThomas Graf kfree_skb(skb); 4267ab364a6fSThomas Graf goto errout; 42681da177e4SLinus Torvalds } 42691da177e4SLinus Torvalds 427015e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 4271ab364a6fSThomas Graf errout: 42721da177e4SLinus Torvalds return err; 42731da177e4SLinus Torvalds } 42741da177e4SLinus Torvalds 427537a1d361SRoopa Prabhu void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info, 427637a1d361SRoopa Prabhu unsigned int nlm_flags) 42771da177e4SLinus Torvalds { 42781da177e4SLinus Torvalds struct sk_buff *skb; 42795578689aSDaniel Lezcano struct net *net = info->nl_net; 4280528c4cebSDenis V. Lunev u32 seq; 4281528c4cebSDenis V. Lunev int err; 42820d51aa80SJamal Hadi Salim 4283528c4cebSDenis V. Lunev err = -ENOBUFS; 428438308473SDavid S. Miller seq = info->nlh ? info->nlh->nlmsg_seq : 0; 428586872cb5SThomas Graf 428619e42e45SRoopa Prabhu skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 428738308473SDavid S. Miller if (!skb) 428821713ebcSThomas Graf goto errout; 42891da177e4SLinus Torvalds 4290191cd582SBrian Haley err = rt6_fill_node(net, skb, rt, NULL, NULL, 0, 4291f8cfe2ceSDavid Ahern event, info->portid, seq, nlm_flags); 429226932566SPatrick McHardy if (err < 0) { 429326932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 429426932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 429526932566SPatrick McHardy kfree_skb(skb); 429626932566SPatrick McHardy goto errout; 429726932566SPatrick McHardy } 429815e47304SEric W. Biederman rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 42995578689aSDaniel Lezcano info->nlh, gfp_any()); 43001ce85fe4SPablo Neira Ayuso return; 430121713ebcSThomas Graf errout: 430221713ebcSThomas Graf if (err < 0) 43035578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 43041da177e4SLinus Torvalds } 43051da177e4SLinus Torvalds 43068ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 4307351638e7SJiri Pirko unsigned long event, void *ptr) 43088ed67789SDaniel Lezcano { 4309351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 4310c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 43118ed67789SDaniel Lezcano 4312242d3a49SWANG Cong if (!(dev->flags & IFF_LOOPBACK)) 4313242d3a49SWANG Cong return NOTIFY_OK; 4314242d3a49SWANG Cong 4315242d3a49SWANG Cong if (event == NETDEV_REGISTER) { 4316d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 43178ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 43188ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 4319d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 43208ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 4321d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 43228ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 43238ed67789SDaniel Lezcano #endif 432476da0704SWANG Cong } else if (event == NETDEV_UNREGISTER && 432576da0704SWANG Cong dev->reg_state != NETREG_UNREGISTERED) { 432676da0704SWANG Cong /* NETDEV_UNREGISTER could be fired for multiple times by 432776da0704SWANG Cong * netdev_wait_allrefs(). Make sure we only call this once. 432876da0704SWANG Cong */ 432912d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev); 4330242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 433112d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev); 433212d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev); 4333242d3a49SWANG Cong #endif 43348ed67789SDaniel Lezcano } 43358ed67789SDaniel Lezcano 43368ed67789SDaniel Lezcano return NOTIFY_OK; 43378ed67789SDaniel Lezcano } 43388ed67789SDaniel Lezcano 43391da177e4SLinus Torvalds /* 43401da177e4SLinus Torvalds * /proc 43411da177e4SLinus Torvalds */ 43421da177e4SLinus Torvalds 43431da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 43441da177e4SLinus Torvalds 434533120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = { 434633120b30SAlexey Dobriyan .owner = THIS_MODULE, 434733120b30SAlexey Dobriyan .open = ipv6_route_open, 434833120b30SAlexey Dobriyan .read = seq_read, 434933120b30SAlexey Dobriyan .llseek = seq_lseek, 43508d2ca1d7SHannes Frederic Sowa .release = seq_release_net, 435133120b30SAlexey Dobriyan }; 435233120b30SAlexey Dobriyan 43531da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 43541da177e4SLinus Torvalds { 435569ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 43561da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 435769ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 435869ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 435969ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_alloc, 436069ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 436169ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 4362fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 436369ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 43641da177e4SLinus Torvalds 43651da177e4SLinus Torvalds return 0; 43661da177e4SLinus Torvalds } 43671da177e4SLinus Torvalds 43681da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file) 43691da177e4SLinus Torvalds { 4370de05c557SPavel Emelyanov return single_open_net(inode, file, rt6_stats_seq_show); 437169ddb805SDaniel Lezcano } 437269ddb805SDaniel Lezcano 43739a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = { 43741da177e4SLinus Torvalds .owner = THIS_MODULE, 43751da177e4SLinus Torvalds .open = rt6_stats_seq_open, 43761da177e4SLinus Torvalds .read = seq_read, 43771da177e4SLinus Torvalds .llseek = seq_lseek, 4378b6fcbdb4SPavel Emelyanov .release = single_release_net, 43791da177e4SLinus Torvalds }; 43801da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 43811da177e4SLinus Torvalds 43821da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 43831da177e4SLinus Torvalds 43841da177e4SLinus Torvalds static 4385fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, 43861da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 43871da177e4SLinus Torvalds { 4388c486da34SLucian Adrian Grijincu struct net *net; 4389c486da34SLucian Adrian Grijincu int delay; 4390c486da34SLucian Adrian Grijincu if (!write) 4391c486da34SLucian Adrian Grijincu return -EINVAL; 4392c486da34SLucian Adrian Grijincu 4393c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 4394c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 43958d65af78SAlexey Dobriyan proc_dointvec(ctl, write, buffer, lenp, ppos); 43962ac3ac8fSMichal Kubeček fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0); 43971da177e4SLinus Torvalds return 0; 43981da177e4SLinus Torvalds } 43991da177e4SLinus Torvalds 4400fe2c6338SJoe Perches struct ctl_table ipv6_route_table_template[] = { 44011da177e4SLinus Torvalds { 44021da177e4SLinus Torvalds .procname = "flush", 44034990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 44041da177e4SLinus Torvalds .maxlen = sizeof(int), 440589c8b3a1SDave Jones .mode = 0200, 44066d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 44071da177e4SLinus Torvalds }, 44081da177e4SLinus Torvalds { 44091da177e4SLinus Torvalds .procname = "gc_thresh", 44109a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 44111da177e4SLinus Torvalds .maxlen = sizeof(int), 44121da177e4SLinus Torvalds .mode = 0644, 44136d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 44141da177e4SLinus Torvalds }, 44151da177e4SLinus Torvalds { 44161da177e4SLinus Torvalds .procname = "max_size", 44174990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 44181da177e4SLinus Torvalds .maxlen = sizeof(int), 44191da177e4SLinus Torvalds .mode = 0644, 44206d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 44211da177e4SLinus Torvalds }, 44221da177e4SLinus Torvalds { 44231da177e4SLinus Torvalds .procname = "gc_min_interval", 44244990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 44251da177e4SLinus Torvalds .maxlen = sizeof(int), 44261da177e4SLinus Torvalds .mode = 0644, 44276d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 44281da177e4SLinus Torvalds }, 44291da177e4SLinus Torvalds { 44301da177e4SLinus Torvalds .procname = "gc_timeout", 44314990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 44321da177e4SLinus Torvalds .maxlen = sizeof(int), 44331da177e4SLinus Torvalds .mode = 0644, 44346d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 44351da177e4SLinus Torvalds }, 44361da177e4SLinus Torvalds { 44371da177e4SLinus Torvalds .procname = "gc_interval", 44384990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 44391da177e4SLinus Torvalds .maxlen = sizeof(int), 44401da177e4SLinus Torvalds .mode = 0644, 44416d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 44421da177e4SLinus Torvalds }, 44431da177e4SLinus Torvalds { 44441da177e4SLinus Torvalds .procname = "gc_elasticity", 44454990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 44461da177e4SLinus Torvalds .maxlen = sizeof(int), 44471da177e4SLinus Torvalds .mode = 0644, 4448f3d3f616SMin Zhang .proc_handler = proc_dointvec, 44491da177e4SLinus Torvalds }, 44501da177e4SLinus Torvalds { 44511da177e4SLinus Torvalds .procname = "mtu_expires", 44524990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 44531da177e4SLinus Torvalds .maxlen = sizeof(int), 44541da177e4SLinus Torvalds .mode = 0644, 44556d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 44561da177e4SLinus Torvalds }, 44571da177e4SLinus Torvalds { 44581da177e4SLinus Torvalds .procname = "min_adv_mss", 44594990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 44601da177e4SLinus Torvalds .maxlen = sizeof(int), 44611da177e4SLinus Torvalds .mode = 0644, 4462f3d3f616SMin Zhang .proc_handler = proc_dointvec, 44631da177e4SLinus Torvalds }, 44641da177e4SLinus Torvalds { 44651da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 44664990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 44671da177e4SLinus Torvalds .maxlen = sizeof(int), 44681da177e4SLinus Torvalds .mode = 0644, 44696d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 44701da177e4SLinus Torvalds }, 4471f8572d8fSEric W. Biederman { } 44721da177e4SLinus Torvalds }; 44731da177e4SLinus Torvalds 44742c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 4475760f2d01SDaniel Lezcano { 4476760f2d01SDaniel Lezcano struct ctl_table *table; 4477760f2d01SDaniel Lezcano 4478760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 4479760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 4480760f2d01SDaniel Lezcano GFP_KERNEL); 44815ee09105SYOSHIFUJI Hideaki 44825ee09105SYOSHIFUJI Hideaki if (table) { 44835ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 4484c486da34SLucian Adrian Grijincu table[0].extra1 = net; 448586393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 44865ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 44875ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 44885ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 44895ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 44905ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 44915ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 44925ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 44939c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 4494464dc801SEric W. Biederman 4495464dc801SEric W. Biederman /* Don't export sysctls to unprivileged users */ 4496464dc801SEric W. Biederman if (net->user_ns != &init_user_ns) 4497464dc801SEric W. Biederman table[0].procname = NULL; 44985ee09105SYOSHIFUJI Hideaki } 44995ee09105SYOSHIFUJI Hideaki 4500760f2d01SDaniel Lezcano return table; 4501760f2d01SDaniel Lezcano } 45021da177e4SLinus Torvalds #endif 45031da177e4SLinus Torvalds 45042c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 4505cdb18761SDaniel Lezcano { 4506633d424bSPavel Emelyanov int ret = -ENOMEM; 45078ed67789SDaniel Lezcano 450886393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 450986393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 4510f2fc6a54SBenjamin Thery 4511fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 4512fc66f95cSEric Dumazet goto out_ip6_dst_ops; 4513fc66f95cSEric Dumazet 45148ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 45158ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 45168ed67789SDaniel Lezcano GFP_KERNEL); 45178ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 4518fc66f95cSEric Dumazet goto out_ip6_dst_entries; 4519d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.path = 45208ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_null_entry; 4521d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 452262fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 452362fa8a84SDavid S. Miller ip6_template_metrics, true); 45248ed67789SDaniel Lezcano 45258ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 4526feca7d8cSVincent Bernat net->ipv6.fib6_has_custom_rules = false; 45278ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 45288ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 45298ed67789SDaniel Lezcano GFP_KERNEL); 453068fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 453168fffc67SPeter Zijlstra goto out_ip6_null_entry; 4532d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.path = 45338ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_prohibit_entry; 4534d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 453562fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 453662fa8a84SDavid S. Miller ip6_template_metrics, true); 45378ed67789SDaniel Lezcano 45388ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 45398ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 45408ed67789SDaniel Lezcano GFP_KERNEL); 454168fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 454268fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 4543d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.path = 45448ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_blk_hole_entry; 4545d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 454662fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 454762fa8a84SDavid S. Miller ip6_template_metrics, true); 45488ed67789SDaniel Lezcano #endif 45498ed67789SDaniel Lezcano 4550b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 4551b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 4552b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 4553b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 4554b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 4555b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 4556b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 4557b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 4558b339a47cSPeter Zijlstra 45596891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 45606891a346SBenjamin Thery 45618ed67789SDaniel Lezcano ret = 0; 45628ed67789SDaniel Lezcano out: 45638ed67789SDaniel Lezcano return ret; 4564f2fc6a54SBenjamin Thery 456568fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 456668fffc67SPeter Zijlstra out_ip6_prohibit_entry: 456768fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 456868fffc67SPeter Zijlstra out_ip6_null_entry: 456968fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 457068fffc67SPeter Zijlstra #endif 4571fc66f95cSEric Dumazet out_ip6_dst_entries: 4572fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 4573f2fc6a54SBenjamin Thery out_ip6_dst_ops: 4574f2fc6a54SBenjamin Thery goto out; 4575cdb18761SDaniel Lezcano } 4576cdb18761SDaniel Lezcano 45772c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 4578cdb18761SDaniel Lezcano { 45798ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 45808ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 45818ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 45828ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 45838ed67789SDaniel Lezcano #endif 458441bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 4585cdb18761SDaniel Lezcano } 4586cdb18761SDaniel Lezcano 4587d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net) 4588d189634eSThomas Graf { 4589d189634eSThomas Graf #ifdef CONFIG_PROC_FS 4590d4beaa66SGao feng proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops); 4591d4beaa66SGao feng proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops); 4592d189634eSThomas Graf #endif 4593d189634eSThomas Graf return 0; 4594d189634eSThomas Graf } 4595d189634eSThomas Graf 4596d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net) 4597d189634eSThomas Graf { 4598d189634eSThomas Graf #ifdef CONFIG_PROC_FS 4599ece31ffdSGao feng remove_proc_entry("ipv6_route", net->proc_net); 4600ece31ffdSGao feng remove_proc_entry("rt6_stats", net->proc_net); 4601d189634eSThomas Graf #endif 4602d189634eSThomas Graf } 4603d189634eSThomas Graf 4604cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 4605cdb18761SDaniel Lezcano .init = ip6_route_net_init, 4606cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 4607cdb18761SDaniel Lezcano }; 4608cdb18761SDaniel Lezcano 4609c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net) 4610c3426b47SDavid S. Miller { 4611c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 4612c3426b47SDavid S. Miller 4613c3426b47SDavid S. Miller if (!bp) 4614c3426b47SDavid S. Miller return -ENOMEM; 4615c3426b47SDavid S. Miller inet_peer_base_init(bp); 4616c3426b47SDavid S. Miller net->ipv6.peers = bp; 4617c3426b47SDavid S. Miller return 0; 4618c3426b47SDavid S. Miller } 4619c3426b47SDavid S. Miller 4620c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net) 4621c3426b47SDavid S. Miller { 4622c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv6.peers; 4623c3426b47SDavid S. Miller 4624c3426b47SDavid S. Miller net->ipv6.peers = NULL; 462556a6b248SDavid S. Miller inetpeer_invalidate_tree(bp); 4626c3426b47SDavid S. Miller kfree(bp); 4627c3426b47SDavid S. Miller } 4628c3426b47SDavid S. Miller 46292b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = { 4630c3426b47SDavid S. Miller .init = ipv6_inetpeer_init, 4631c3426b47SDavid S. Miller .exit = ipv6_inetpeer_exit, 4632c3426b47SDavid S. Miller }; 4633c3426b47SDavid S. Miller 4634d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = { 4635d189634eSThomas Graf .init = ip6_route_net_init_late, 4636d189634eSThomas Graf .exit = ip6_route_net_exit_late, 4637d189634eSThomas Graf }; 4638d189634eSThomas Graf 46398ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 46408ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 4641242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY - 10, 46428ed67789SDaniel Lezcano }; 46438ed67789SDaniel Lezcano 46442f460933SWANG Cong void __init ip6_route_init_special_entries(void) 46452f460933SWANG Cong { 46462f460933SWANG Cong /* Registering of the loopback is done before this portion of code, 46472f460933SWANG Cong * the loopback reference in rt6_info will not be taken, do it 46482f460933SWANG Cong * manually for init_net */ 46492f460933SWANG Cong init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 46502f460933SWANG Cong init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 46512f460933SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 46522f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 46532f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 46542f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 46552f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 46562f460933SWANG Cong #endif 46572f460933SWANG Cong } 46582f460933SWANG Cong 4659433d49c3SDaniel Lezcano int __init ip6_route_init(void) 46601da177e4SLinus Torvalds { 4661433d49c3SDaniel Lezcano int ret; 46628d0b94afSMartin KaFai Lau int cpu; 4663433d49c3SDaniel Lezcano 46649a7ec3a9SDaniel Lezcano ret = -ENOMEM; 46659a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 46669a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 46679a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 46689a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 4669c19a28e1SFernando Carrijo goto out; 467014e50e57SDavid S. Miller 4671fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 46728ed67789SDaniel Lezcano if (ret) 4673bdb3289fSDaniel Lezcano goto out_kmem_cache; 4674bdb3289fSDaniel Lezcano 4675c3426b47SDavid S. Miller ret = register_pernet_subsys(&ipv6_inetpeer_ops); 4676c3426b47SDavid S. Miller if (ret) 4677e8803b6cSDavid S. Miller goto out_dst_entries; 46782a0c451aSThomas Graf 46797e52b33bSDavid S. Miller ret = register_pernet_subsys(&ip6_route_net_ops); 46807e52b33bSDavid S. Miller if (ret) 46817e52b33bSDavid S. Miller goto out_register_inetpeer; 4682c3426b47SDavid S. Miller 46835dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 46845dc121e9SArnaud Ebalard 4685e8803b6cSDavid S. Miller ret = fib6_init(); 4686433d49c3SDaniel Lezcano if (ret) 46878ed67789SDaniel Lezcano goto out_register_subsys; 4688433d49c3SDaniel Lezcano 4689433d49c3SDaniel Lezcano ret = xfrm6_init(); 4690433d49c3SDaniel Lezcano if (ret) 4691e8803b6cSDavid S. Miller goto out_fib6_init; 4692c35b7e72SDaniel Lezcano 4693433d49c3SDaniel Lezcano ret = fib6_rules_init(); 4694433d49c3SDaniel Lezcano if (ret) 4695433d49c3SDaniel Lezcano goto xfrm6_init; 46967e5449c2SDaniel Lezcano 4697d189634eSThomas Graf ret = register_pernet_subsys(&ip6_route_net_late_ops); 4698d189634eSThomas Graf if (ret) 4699d189634eSThomas Graf goto fib6_rules_init; 4700d189634eSThomas Graf 4701433d49c3SDaniel Lezcano ret = -ENOBUFS; 4702b97bac64SFlorian Westphal if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, 0) || 4703b97bac64SFlorian Westphal __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, 0) || 4704e3a22b7fSFlorian Westphal __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, 4705e3a22b7fSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED)) 4706d189634eSThomas Graf goto out_register_late_subsys; 4707433d49c3SDaniel Lezcano 47088ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 4709cdb18761SDaniel Lezcano if (ret) 4710d189634eSThomas Graf goto out_register_late_subsys; 47118ed67789SDaniel Lezcano 47128d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 47138d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 47148d0b94afSMartin KaFai Lau 47158d0b94afSMartin KaFai Lau INIT_LIST_HEAD(&ul->head); 47168d0b94afSMartin KaFai Lau spin_lock_init(&ul->lock); 47178d0b94afSMartin KaFai Lau } 47188d0b94afSMartin KaFai Lau 4719433d49c3SDaniel Lezcano out: 4720433d49c3SDaniel Lezcano return ret; 4721433d49c3SDaniel Lezcano 4722d189634eSThomas Graf out_register_late_subsys: 4723d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 4724433d49c3SDaniel Lezcano fib6_rules_init: 4725433d49c3SDaniel Lezcano fib6_rules_cleanup(); 4726433d49c3SDaniel Lezcano xfrm6_init: 4727433d49c3SDaniel Lezcano xfrm6_fini(); 47282a0c451aSThomas Graf out_fib6_init: 47292a0c451aSThomas Graf fib6_gc_cleanup(); 47308ed67789SDaniel Lezcano out_register_subsys: 47318ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 47327e52b33bSDavid S. Miller out_register_inetpeer: 47337e52b33bSDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 4734fc66f95cSEric Dumazet out_dst_entries: 4735fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 4736433d49c3SDaniel Lezcano out_kmem_cache: 4737f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 4738433d49c3SDaniel Lezcano goto out; 47391da177e4SLinus Torvalds } 47401da177e4SLinus Torvalds 47411da177e4SLinus Torvalds void ip6_route_cleanup(void) 47421da177e4SLinus Torvalds { 47438ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 4744d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 4745101367c2SThomas Graf fib6_rules_cleanup(); 47461da177e4SLinus Torvalds xfrm6_fini(); 47471da177e4SLinus Torvalds fib6_gc_cleanup(); 4748c3426b47SDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 47498ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 475041bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 4751f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 47521da177e4SLinus Torvalds } 4753