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> 47457c4cbcSEric W. Biederman #include <net/net_namespace.h> 481da177e4SLinus Torvalds #include <net/snmp.h> 491da177e4SLinus Torvalds #include <net/ipv6.h> 501da177e4SLinus Torvalds #include <net/ip6_fib.h> 511da177e4SLinus Torvalds #include <net/ip6_route.h> 521da177e4SLinus Torvalds #include <net/ndisc.h> 531da177e4SLinus Torvalds #include <net/addrconf.h> 541da177e4SLinus Torvalds #include <net/tcp.h> 551da177e4SLinus Torvalds #include <linux/rtnetlink.h> 561da177e4SLinus Torvalds #include <net/dst.h> 57904af04dSJiri Benc #include <net/dst_metadata.h> 581da177e4SLinus Torvalds #include <net/xfrm.h> 598d71740cSTom Tucker #include <net/netevent.h> 6021713ebcSThomas Graf #include <net/netlink.h> 6151ebd318SNicolas Dichtel #include <net/nexthop.h> 6219e42e45SRoopa Prabhu #include <net/lwtunnel.h> 63904af04dSJiri Benc #include <net/ip_tunnels.h> 64ca254490SDavid Ahern #include <net/l3mdev.h> 65b811580dSDavid Ahern #include <trace/events/fib6.h> 661da177e4SLinus Torvalds 677c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 701da177e4SLinus Torvalds #include <linux/sysctl.h> 711da177e4SLinus Torvalds #endif 721da177e4SLinus Torvalds 73afc154e9SHannes Frederic Sowa enum rt6_nud_state { 747e980569SJiri Benc RT6_NUD_FAIL_HARD = -3, 757e980569SJiri Benc RT6_NUD_FAIL_PROBE = -2, 767e980569SJiri Benc RT6_NUD_FAIL_DO_RR = -1, 77afc154e9SHannes Frederic Sowa RT6_NUD_SUCCEED = 1 78afc154e9SHannes Frederic Sowa }; 79afc154e9SHannes Frederic Sowa 8083a09abdSMartin KaFai Lau static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort); 811da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie); 820dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst); 83ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst); 841da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *); 851da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *); 861da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *, 871da177e4SLinus Torvalds struct net_device *dev, int how); 88569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops); 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds static int ip6_pkt_discard(struct sk_buff *skb); 91ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb); 927150aedeSKamala R static int ip6_pkt_prohibit(struct sk_buff *skb); 93ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb); 941da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb); 956700c270SDavid S. Miller static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 966700c270SDavid S. Miller struct sk_buff *skb, u32 mtu); 976700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, 986700c270SDavid S. Miller struct sk_buff *skb); 994b32b5adSMartin KaFai Lau static void rt6_dst_from_metrics_check(struct rt6_info *rt); 10052bd4c0cSNicolas Dichtel static int rt6_score_route(struct rt6_info *rt, int oif, int strict); 10116a16cd3SDavid Ahern static size_t rt6_nlmsg_size(struct rt6_info *rt); 10216a16cd3SDavid Ahern static int rt6_fill_node(struct net *net, 10316a16cd3SDavid Ahern struct sk_buff *skb, struct rt6_info *rt, 10416a16cd3SDavid Ahern struct in6_addr *dst, struct in6_addr *src, 10516a16cd3SDavid Ahern int iif, int type, u32 portid, u32 seq, 10616a16cd3SDavid Ahern unsigned int flags); 1071da177e4SLinus Torvalds 10870ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 109efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 110b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 111830218c1SDavid Ahern const struct in6_addr *gwaddr, 112830218c1SDavid Ahern struct net_device *dev, 11395c96174SEric Dumazet unsigned int pref); 114efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 115b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 116830218c1SDavid Ahern const struct in6_addr *gwaddr, 117830218c1SDavid Ahern struct net_device *dev); 11870ceb4f5SYOSHIFUJI Hideaki #endif 11970ceb4f5SYOSHIFUJI Hideaki 1208d0b94afSMartin KaFai Lau struct uncached_list { 1218d0b94afSMartin KaFai Lau spinlock_t lock; 1228d0b94afSMartin KaFai Lau struct list_head head; 1238d0b94afSMartin KaFai Lau }; 1248d0b94afSMartin KaFai Lau 1258d0b94afSMartin KaFai Lau static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list); 1268d0b94afSMartin KaFai Lau 1278d0b94afSMartin KaFai Lau static void rt6_uncached_list_add(struct rt6_info *rt) 1288d0b94afSMartin KaFai Lau { 1298d0b94afSMartin KaFai Lau struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list); 1308d0b94afSMartin KaFai Lau 1318d0b94afSMartin KaFai Lau rt->dst.flags |= DST_NOCACHE; 1328d0b94afSMartin KaFai Lau rt->rt6i_uncached_list = ul; 1338d0b94afSMartin KaFai Lau 1348d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1358d0b94afSMartin KaFai Lau list_add_tail(&rt->rt6i_uncached, &ul->head); 1368d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1378d0b94afSMartin KaFai Lau } 1388d0b94afSMartin KaFai Lau 1398d0b94afSMartin KaFai Lau static void rt6_uncached_list_del(struct rt6_info *rt) 1408d0b94afSMartin KaFai Lau { 1418d0b94afSMartin KaFai Lau if (!list_empty(&rt->rt6i_uncached)) { 1428d0b94afSMartin KaFai Lau struct uncached_list *ul = rt->rt6i_uncached_list; 1438d0b94afSMartin KaFai Lau 1448d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1458d0b94afSMartin KaFai Lau list_del(&rt->rt6i_uncached); 1468d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1478d0b94afSMartin KaFai Lau } 1488d0b94afSMartin KaFai Lau } 1498d0b94afSMartin KaFai Lau 1508d0b94afSMartin KaFai Lau static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev) 1518d0b94afSMartin KaFai Lau { 1528d0b94afSMartin KaFai Lau struct net_device *loopback_dev = net->loopback_dev; 1538d0b94afSMartin KaFai Lau int cpu; 1548d0b94afSMartin KaFai Lau 155e332bc67SEric W. Biederman if (dev == loopback_dev) 156e332bc67SEric W. Biederman return; 157e332bc67SEric W. Biederman 1588d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 1598d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 1608d0b94afSMartin KaFai Lau struct rt6_info *rt; 1618d0b94afSMartin KaFai Lau 1628d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1638d0b94afSMartin KaFai Lau list_for_each_entry(rt, &ul->head, rt6i_uncached) { 1648d0b94afSMartin KaFai Lau struct inet6_dev *rt_idev = rt->rt6i_idev; 1658d0b94afSMartin KaFai Lau struct net_device *rt_dev = rt->dst.dev; 1668d0b94afSMartin KaFai Lau 167e332bc67SEric W. Biederman if (rt_idev->dev == dev) { 1688d0b94afSMartin KaFai Lau rt->rt6i_idev = in6_dev_get(loopback_dev); 1698d0b94afSMartin KaFai Lau in6_dev_put(rt_idev); 1708d0b94afSMartin KaFai Lau } 1718d0b94afSMartin KaFai Lau 172e332bc67SEric W. Biederman if (rt_dev == dev) { 1738d0b94afSMartin KaFai Lau rt->dst.dev = loopback_dev; 1748d0b94afSMartin KaFai Lau dev_hold(rt->dst.dev); 1758d0b94afSMartin KaFai Lau dev_put(rt_dev); 1768d0b94afSMartin KaFai Lau } 1778d0b94afSMartin KaFai Lau } 1788d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1798d0b94afSMartin KaFai Lau } 1808d0b94afSMartin KaFai Lau } 1818d0b94afSMartin KaFai Lau 182d52d3997SMartin KaFai Lau static u32 *rt6_pcpu_cow_metrics(struct rt6_info *rt) 183d52d3997SMartin KaFai Lau { 184d52d3997SMartin KaFai Lau return dst_metrics_write_ptr(rt->dst.from); 185d52d3997SMartin KaFai Lau } 186d52d3997SMartin KaFai Lau 18706582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old) 18806582540SDavid S. Miller { 18906582540SDavid S. Miller struct rt6_info *rt = (struct rt6_info *)dst; 19006582540SDavid S. Miller 191d52d3997SMartin KaFai Lau if (rt->rt6i_flags & RTF_PCPU) 192d52d3997SMartin KaFai Lau return rt6_pcpu_cow_metrics(rt); 193d52d3997SMartin KaFai Lau else if (rt->rt6i_flags & RTF_CACHE) 1944b32b5adSMartin KaFai Lau return NULL; 1954b32b5adSMartin KaFai Lau else 1963b471175SMartin KaFai Lau return dst_cow_metrics_generic(dst, old); 19706582540SDavid S. Miller } 19806582540SDavid S. Miller 199f894cbf8SDavid S. Miller static inline const void *choose_neigh_daddr(struct rt6_info *rt, 200f894cbf8SDavid S. Miller struct sk_buff *skb, 201f894cbf8SDavid S. Miller const void *daddr) 20239232973SDavid S. Miller { 20339232973SDavid S. Miller struct in6_addr *p = &rt->rt6i_gateway; 20439232973SDavid S. Miller 205a7563f34SDavid S. Miller if (!ipv6_addr_any(p)) 20639232973SDavid S. Miller return (const void *) p; 207f894cbf8SDavid S. Miller else if (skb) 208f894cbf8SDavid S. Miller return &ipv6_hdr(skb)->daddr; 20939232973SDavid S. Miller return daddr; 21039232973SDavid S. Miller } 21139232973SDavid S. Miller 212f894cbf8SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst, 213f894cbf8SDavid S. Miller struct sk_buff *skb, 214f894cbf8SDavid S. Miller const void *daddr) 215d3aaeb38SDavid S. Miller { 21639232973SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 21739232973SDavid S. Miller struct neighbour *n; 21839232973SDavid S. Miller 219f894cbf8SDavid S. Miller daddr = choose_neigh_daddr(rt, skb, daddr); 2208e022ee6SYOSHIFUJI Hideaki / 吉藤英明 n = __ipv6_neigh_lookup(dst->dev, daddr); 221f83c7790SDavid S. Miller if (n) 222f83c7790SDavid S. Miller return n; 223f83c7790SDavid S. Miller return neigh_create(&nd_tbl, daddr, dst->dev); 224f83c7790SDavid S. Miller } 225f83c7790SDavid S. Miller 22663fca65dSJulian Anastasov static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr) 22763fca65dSJulian Anastasov { 22863fca65dSJulian Anastasov struct net_device *dev = dst->dev; 22963fca65dSJulian Anastasov struct rt6_info *rt = (struct rt6_info *)dst; 23063fca65dSJulian Anastasov 23163fca65dSJulian Anastasov daddr = choose_neigh_daddr(rt, NULL, daddr); 23263fca65dSJulian Anastasov if (!daddr) 23363fca65dSJulian Anastasov return; 23463fca65dSJulian Anastasov if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 23563fca65dSJulian Anastasov return; 23663fca65dSJulian Anastasov if (ipv6_addr_is_multicast((const struct in6_addr *)daddr)) 23763fca65dSJulian Anastasov return; 23863fca65dSJulian Anastasov __ipv6_confirm_neigh(dev, daddr); 23963fca65dSJulian Anastasov } 24063fca65dSJulian Anastasov 2419a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = { 2421da177e4SLinus Torvalds .family = AF_INET6, 2431da177e4SLinus Torvalds .gc = ip6_dst_gc, 2441da177e4SLinus Torvalds .gc_thresh = 1024, 2451da177e4SLinus Torvalds .check = ip6_dst_check, 2460dbaee3bSDavid S. Miller .default_advmss = ip6_default_advmss, 247ebb762f2SSteffen Klassert .mtu = ip6_mtu, 24806582540SDavid S. Miller .cow_metrics = ipv6_cow_metrics, 2491da177e4SLinus Torvalds .destroy = ip6_dst_destroy, 2501da177e4SLinus Torvalds .ifdown = ip6_dst_ifdown, 2511da177e4SLinus Torvalds .negative_advice = ip6_negative_advice, 2521da177e4SLinus Torvalds .link_failure = ip6_link_failure, 2531da177e4SLinus Torvalds .update_pmtu = ip6_rt_update_pmtu, 2546e157b6aSDavid S. Miller .redirect = rt6_do_redirect, 2559f8955ccSEric W. Biederman .local_out = __ip6_local_out, 256d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 25763fca65dSJulian Anastasov .confirm_neigh = ip6_confirm_neigh, 2581da177e4SLinus Torvalds }; 2591da177e4SLinus Torvalds 260ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst) 261ec831ea7SRoland Dreier { 262618f9bc7SSteffen Klassert unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); 263618f9bc7SSteffen Klassert 264618f9bc7SSteffen Klassert return mtu ? : dst->dev->mtu; 265ec831ea7SRoland Dreier } 266ec831ea7SRoland Dreier 2676700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk, 2686700c270SDavid S. Miller struct sk_buff *skb, u32 mtu) 26914e50e57SDavid S. Miller { 27014e50e57SDavid S. Miller } 27114e50e57SDavid S. Miller 2726700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk, 2736700c270SDavid S. Miller struct sk_buff *skb) 274b587ee3bSDavid S. Miller { 275b587ee3bSDavid S. Miller } 276b587ee3bSDavid S. Miller 27714e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = { 27814e50e57SDavid S. Miller .family = AF_INET6, 27914e50e57SDavid S. Miller .destroy = ip6_dst_destroy, 28014e50e57SDavid S. Miller .check = ip6_dst_check, 281ebb762f2SSteffen Klassert .mtu = ip6_blackhole_mtu, 282214f45c9SEric Dumazet .default_advmss = ip6_default_advmss, 28314e50e57SDavid S. Miller .update_pmtu = ip6_rt_blackhole_update_pmtu, 284b587ee3bSDavid S. Miller .redirect = ip6_rt_blackhole_redirect, 2850a1f5962SMartin KaFai Lau .cow_metrics = dst_cow_metrics_generic, 286d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 28714e50e57SDavid S. Miller }; 28814e50e57SDavid S. Miller 28962fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = { 29014edd87dSLi RongQing [RTAX_HOPLIMIT - 1] = 0, 29162fa8a84SDavid S. Miller }; 29262fa8a84SDavid S. Miller 293fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = { 2941da177e4SLinus Torvalds .dst = { 2951da177e4SLinus Torvalds .__refcnt = ATOMIC_INIT(1), 2961da177e4SLinus Torvalds .__use = 1, 2972c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 2981da177e4SLinus Torvalds .error = -ENETUNREACH, 2991da177e4SLinus Torvalds .input = ip6_pkt_discard, 3001da177e4SLinus Torvalds .output = ip6_pkt_discard_out, 3011da177e4SLinus Torvalds }, 3021da177e4SLinus Torvalds .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 3034f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 3041da177e4SLinus Torvalds .rt6i_metric = ~(u32) 0, 3051da177e4SLinus Torvalds .rt6i_ref = ATOMIC_INIT(1), 3061da177e4SLinus Torvalds }; 3071da177e4SLinus Torvalds 308101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES 309101367c2SThomas Graf 310fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = { 311101367c2SThomas Graf .dst = { 312101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 313101367c2SThomas Graf .__use = 1, 3142c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 315101367c2SThomas Graf .error = -EACCES, 3169ce8ade0SThomas Graf .input = ip6_pkt_prohibit, 3179ce8ade0SThomas Graf .output = ip6_pkt_prohibit_out, 318101367c2SThomas Graf }, 319101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 3204f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 321101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 322101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 323101367c2SThomas Graf }; 324101367c2SThomas Graf 325fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = { 326101367c2SThomas Graf .dst = { 327101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 328101367c2SThomas Graf .__use = 1, 3292c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 330101367c2SThomas Graf .error = -EINVAL, 331352e512cSHerbert Xu .input = dst_discard, 332ede2059dSEric W. Biederman .output = dst_discard_out, 333101367c2SThomas Graf }, 334101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 3354f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 336101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 337101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 338101367c2SThomas Graf }; 339101367c2SThomas Graf 340101367c2SThomas Graf #endif 341101367c2SThomas Graf 342ebfa45f0SMartin KaFai Lau static void rt6_info_init(struct rt6_info *rt) 343ebfa45f0SMartin KaFai Lau { 344ebfa45f0SMartin KaFai Lau struct dst_entry *dst = &rt->dst; 345ebfa45f0SMartin KaFai Lau 346ebfa45f0SMartin KaFai Lau memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst)); 347ebfa45f0SMartin KaFai Lau INIT_LIST_HEAD(&rt->rt6i_siblings); 348ebfa45f0SMartin KaFai Lau INIT_LIST_HEAD(&rt->rt6i_uncached); 349ebfa45f0SMartin KaFai Lau } 350ebfa45f0SMartin KaFai Lau 3511da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */ 352d52d3997SMartin KaFai Lau static struct rt6_info *__ip6_dst_alloc(struct net *net, 353957c665fSDavid S. Miller struct net_device *dev, 354ad706862SMartin KaFai Lau int flags) 3551da177e4SLinus Torvalds { 35697bab73fSDavid S. Miller struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev, 357*587fea74SWei Wang 1, DST_OBSOLETE_FORCE_CHK, 358*587fea74SWei Wang flags | DST_NOGC); 359cf911662SDavid S. Miller 360ebfa45f0SMartin KaFai Lau if (rt) 361ebfa45f0SMartin KaFai Lau rt6_info_init(rt); 3628104891bSSteffen Klassert 363cf911662SDavid S. Miller return rt; 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds 3669ab179d8SDavid Ahern struct rt6_info *ip6_dst_alloc(struct net *net, 367d52d3997SMartin KaFai Lau struct net_device *dev, 368ad706862SMartin KaFai Lau int flags) 369d52d3997SMartin KaFai Lau { 370ad706862SMartin KaFai Lau struct rt6_info *rt = __ip6_dst_alloc(net, dev, flags); 371d52d3997SMartin KaFai Lau 372d52d3997SMartin KaFai Lau if (rt) { 373d52d3997SMartin KaFai Lau rt->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, GFP_ATOMIC); 374d52d3997SMartin KaFai Lau if (rt->rt6i_pcpu) { 375d52d3997SMartin KaFai Lau int cpu; 376d52d3997SMartin KaFai Lau 377d52d3997SMartin KaFai Lau for_each_possible_cpu(cpu) { 378d52d3997SMartin KaFai Lau struct rt6_info **p; 379d52d3997SMartin KaFai Lau 380d52d3997SMartin KaFai Lau p = per_cpu_ptr(rt->rt6i_pcpu, cpu); 381d52d3997SMartin KaFai Lau /* no one shares rt */ 382d52d3997SMartin KaFai Lau *p = NULL; 383d52d3997SMartin KaFai Lau } 384d52d3997SMartin KaFai Lau } else { 385*587fea74SWei Wang dst_release_immediate(&rt->dst); 386d52d3997SMartin KaFai Lau return NULL; 387d52d3997SMartin KaFai Lau } 388d52d3997SMartin KaFai Lau } 389d52d3997SMartin KaFai Lau 390d52d3997SMartin KaFai Lau return rt; 391d52d3997SMartin KaFai Lau } 3929ab179d8SDavid Ahern EXPORT_SYMBOL(ip6_dst_alloc); 393d52d3997SMartin KaFai Lau 3941da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst) 3951da177e4SLinus Torvalds { 3961da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 397ecd98837SYOSHIFUJI Hideaki / 吉藤英明 struct dst_entry *from = dst->from; 3988d0b94afSMartin KaFai Lau struct inet6_dev *idev; 3991da177e4SLinus Torvalds 4008e2ec639SYan, Zheng dst_destroy_metrics_generic(dst); 401d52d3997SMartin KaFai Lau free_percpu(rt->rt6i_pcpu); 4028d0b94afSMartin KaFai Lau rt6_uncached_list_del(rt); 4038d0b94afSMartin KaFai Lau 4048d0b94afSMartin KaFai Lau idev = rt->rt6i_idev; 40538308473SDavid S. Miller if (idev) { 4061da177e4SLinus Torvalds rt->rt6i_idev = NULL; 4071da177e4SLinus Torvalds in6_dev_put(idev); 4081da177e4SLinus Torvalds } 4091716a961SGao feng 410ecd98837SYOSHIFUJI Hideaki / 吉藤英明 dst->from = NULL; 411ecd98837SYOSHIFUJI Hideaki / 吉藤英明 dst_release(from); 412b3419363SDavid S. Miller } 413b3419363SDavid S. Miller 4141da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 4151da177e4SLinus Torvalds int how) 4161da177e4SLinus Torvalds { 4171da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 4181da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 4195a3e55d6SDenis V. Lunev struct net_device *loopback_dev = 420c346dca1SYOSHIFUJI Hideaki dev_net(dev)->loopback_dev; 4211da177e4SLinus Torvalds 42297cac082SDavid S. Miller if (dev != loopback_dev) { 42397cac082SDavid S. Miller if (idev && idev->dev == dev) { 4245a3e55d6SDenis V. Lunev struct inet6_dev *loopback_idev = 4255a3e55d6SDenis V. Lunev in6_dev_get(loopback_dev); 42638308473SDavid S. Miller if (loopback_idev) { 4271da177e4SLinus Torvalds rt->rt6i_idev = loopback_idev; 4281da177e4SLinus Torvalds in6_dev_put(idev); 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds } 43197cac082SDavid S. Miller } 4321da177e4SLinus Torvalds } 4331da177e4SLinus Torvalds 4345973fb1eSMartin KaFai Lau static bool __rt6_check_expired(const struct rt6_info *rt) 4355973fb1eSMartin KaFai Lau { 4365973fb1eSMartin KaFai Lau if (rt->rt6i_flags & RTF_EXPIRES) 4375973fb1eSMartin KaFai Lau return time_after(jiffies, rt->dst.expires); 4385973fb1eSMartin KaFai Lau else 4395973fb1eSMartin KaFai Lau return false; 4405973fb1eSMartin KaFai Lau } 4415973fb1eSMartin KaFai Lau 442a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt) 4431da177e4SLinus Torvalds { 4441716a961SGao feng if (rt->rt6i_flags & RTF_EXPIRES) { 4451716a961SGao feng if (time_after(jiffies, rt->dst.expires)) 446a50feda5SEric Dumazet return true; 4471716a961SGao feng } else if (rt->dst.from) { 4483fd91fb3SLi RongQing return rt6_check_expired((struct rt6_info *) rt->dst.from); 4491716a961SGao feng } 450a50feda5SEric Dumazet return false; 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds 45351ebd318SNicolas Dichtel /* Multipath route selection: 45451ebd318SNicolas Dichtel * Hash based function using packet header and flowlabel. 45551ebd318SNicolas Dichtel * Adapted from fib_info_hashfn() 45651ebd318SNicolas Dichtel */ 45751ebd318SNicolas Dichtel static int rt6_info_hash_nhsfn(unsigned int candidate_count, 45851ebd318SNicolas Dichtel const struct flowi6 *fl6) 45951ebd318SNicolas Dichtel { 460644d0e65STom Herbert return get_hash_from_flowi6(fl6) % candidate_count; 46151ebd318SNicolas Dichtel } 46251ebd318SNicolas Dichtel 46351ebd318SNicolas Dichtel static struct rt6_info *rt6_multipath_select(struct rt6_info *match, 46452bd4c0cSNicolas Dichtel struct flowi6 *fl6, int oif, 46552bd4c0cSNicolas Dichtel int strict) 46651ebd318SNicolas Dichtel { 46751ebd318SNicolas Dichtel struct rt6_info *sibling, *next_sibling; 46851ebd318SNicolas Dichtel int route_choosen; 46951ebd318SNicolas Dichtel 47051ebd318SNicolas Dichtel route_choosen = rt6_info_hash_nhsfn(match->rt6i_nsiblings + 1, fl6); 47151ebd318SNicolas Dichtel /* Don't change the route, if route_choosen == 0 47251ebd318SNicolas Dichtel * (siblings does not include ourself) 47351ebd318SNicolas Dichtel */ 47451ebd318SNicolas Dichtel if (route_choosen) 47551ebd318SNicolas Dichtel list_for_each_entry_safe(sibling, next_sibling, 47651ebd318SNicolas Dichtel &match->rt6i_siblings, rt6i_siblings) { 47751ebd318SNicolas Dichtel route_choosen--; 47851ebd318SNicolas Dichtel if (route_choosen == 0) { 47952bd4c0cSNicolas Dichtel if (rt6_score_route(sibling, oif, strict) < 0) 48052bd4c0cSNicolas Dichtel break; 48151ebd318SNicolas Dichtel match = sibling; 48251ebd318SNicolas Dichtel break; 48351ebd318SNicolas Dichtel } 48451ebd318SNicolas Dichtel } 48551ebd318SNicolas Dichtel return match; 48651ebd318SNicolas Dichtel } 48751ebd318SNicolas Dichtel 4881da177e4SLinus Torvalds /* 489c71099acSThomas Graf * Route lookup. Any table->tb6_lock is implied. 4901da177e4SLinus Torvalds */ 4911da177e4SLinus Torvalds 4928ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net, 4938ed67789SDaniel Lezcano struct rt6_info *rt, 494b71d1d42SEric Dumazet const struct in6_addr *saddr, 4951da177e4SLinus Torvalds int oif, 496d420895eSYOSHIFUJI Hideaki int flags) 4971da177e4SLinus Torvalds { 4981da177e4SLinus Torvalds struct rt6_info *local = NULL; 4991da177e4SLinus Torvalds struct rt6_info *sprt; 5001da177e4SLinus Torvalds 501dd3abc4eSYOSHIFUJI Hideaki if (!oif && ipv6_addr_any(saddr)) 502dd3abc4eSYOSHIFUJI Hideaki goto out; 503dd3abc4eSYOSHIFUJI Hideaki 504d8d1f30bSChangli Gao for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) { 505d1918542SDavid S. Miller struct net_device *dev = sprt->dst.dev; 506dd3abc4eSYOSHIFUJI Hideaki 507dd3abc4eSYOSHIFUJI Hideaki if (oif) { 5081da177e4SLinus Torvalds if (dev->ifindex == oif) 5091da177e4SLinus Torvalds return sprt; 5101da177e4SLinus Torvalds if (dev->flags & IFF_LOOPBACK) { 51138308473SDavid S. Miller if (!sprt->rt6i_idev || 5121da177e4SLinus Torvalds sprt->rt6i_idev->dev->ifindex != oif) { 51317fb0b2bSDavid Ahern if (flags & RT6_LOOKUP_F_IFACE) 5141da177e4SLinus Torvalds continue; 51517fb0b2bSDavid Ahern if (local && 51617fb0b2bSDavid Ahern local->rt6i_idev->dev->ifindex == oif) 5171da177e4SLinus Torvalds continue; 5181da177e4SLinus Torvalds } 5191da177e4SLinus Torvalds local = sprt; 5201da177e4SLinus Torvalds } 521dd3abc4eSYOSHIFUJI Hideaki } else { 522dd3abc4eSYOSHIFUJI Hideaki if (ipv6_chk_addr(net, saddr, dev, 523dd3abc4eSYOSHIFUJI Hideaki flags & RT6_LOOKUP_F_IFACE)) 524dd3abc4eSYOSHIFUJI Hideaki return sprt; 525dd3abc4eSYOSHIFUJI Hideaki } 5261da177e4SLinus Torvalds } 5271da177e4SLinus Torvalds 528dd3abc4eSYOSHIFUJI Hideaki if (oif) { 5291da177e4SLinus Torvalds if (local) 5301da177e4SLinus Torvalds return local; 5311da177e4SLinus Torvalds 532d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE) 5338ed67789SDaniel Lezcano return net->ipv6.ip6_null_entry; 5341da177e4SLinus Torvalds } 535dd3abc4eSYOSHIFUJI Hideaki out: 5361da177e4SLinus Torvalds return rt; 5371da177e4SLinus Torvalds } 5381da177e4SLinus Torvalds 53927097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 540c2f17e82SHannes Frederic Sowa struct __rt6_probe_work { 541c2f17e82SHannes Frederic Sowa struct work_struct work; 542c2f17e82SHannes Frederic Sowa struct in6_addr target; 543c2f17e82SHannes Frederic Sowa struct net_device *dev; 544c2f17e82SHannes Frederic Sowa }; 545c2f17e82SHannes Frederic Sowa 546c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w) 547c2f17e82SHannes Frederic Sowa { 548c2f17e82SHannes Frederic Sowa struct in6_addr mcaddr; 549c2f17e82SHannes Frederic Sowa struct __rt6_probe_work *work = 550c2f17e82SHannes Frederic Sowa container_of(w, struct __rt6_probe_work, work); 551c2f17e82SHannes Frederic Sowa 552c2f17e82SHannes Frederic Sowa addrconf_addr_solict_mult(&work->target, &mcaddr); 553adc176c5SErik Nordmark ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0); 554c2f17e82SHannes Frederic Sowa dev_put(work->dev); 555662f5533SMichael Büsch kfree(work); 556c2f17e82SHannes Frederic Sowa } 557c2f17e82SHannes Frederic Sowa 55827097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt) 55927097255SYOSHIFUJI Hideaki { 560990edb42SMartin KaFai Lau struct __rt6_probe_work *work; 561f2c31e32SEric Dumazet struct neighbour *neigh; 56227097255SYOSHIFUJI Hideaki /* 56327097255SYOSHIFUJI Hideaki * Okay, this does not seem to be appropriate 56427097255SYOSHIFUJI Hideaki * for now, however, we need to check if it 56527097255SYOSHIFUJI Hideaki * is really so; aka Router Reachability Probing. 56627097255SYOSHIFUJI Hideaki * 56727097255SYOSHIFUJI Hideaki * Router Reachability Probe MUST be rate-limited 56827097255SYOSHIFUJI Hideaki * to no more than one per minute. 56927097255SYOSHIFUJI Hideaki */ 5702152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (!rt || !(rt->rt6i_flags & RTF_GATEWAY)) 571fdd6681dSAmerigo Wang return; 5722152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 5732152caeaSYOSHIFUJI Hideaki / 吉藤英明 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 5742152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 5758d6c31bfSMartin KaFai Lau if (neigh->nud_state & NUD_VALID) 5768d6c31bfSMartin KaFai Lau goto out; 5778d6c31bfSMartin KaFai Lau 578990edb42SMartin KaFai Lau work = NULL; 5792152caeaSYOSHIFUJI Hideaki / 吉藤英明 write_lock(&neigh->lock); 580990edb42SMartin KaFai Lau if (!(neigh->nud_state & NUD_VALID) && 581990edb42SMartin KaFai Lau time_after(jiffies, 582990edb42SMartin KaFai Lau neigh->updated + 583990edb42SMartin KaFai Lau rt->rt6i_idev->cnf.rtr_probe_interval)) { 584c2f17e82SHannes Frederic Sowa work = kmalloc(sizeof(*work), GFP_ATOMIC); 585990edb42SMartin KaFai Lau if (work) 5867e980569SJiri Benc __neigh_set_probe_once(neigh); 587990edb42SMartin KaFai Lau } 588c2f17e82SHannes Frederic Sowa write_unlock(&neigh->lock); 589990edb42SMartin KaFai Lau } else { 590990edb42SMartin KaFai Lau work = kmalloc(sizeof(*work), GFP_ATOMIC); 591990edb42SMartin KaFai Lau } 592c2f17e82SHannes Frederic Sowa 593c2f17e82SHannes Frederic Sowa if (work) { 594c2f17e82SHannes Frederic Sowa INIT_WORK(&work->work, rt6_probe_deferred); 595c2f17e82SHannes Frederic Sowa work->target = rt->rt6i_gateway; 596c2f17e82SHannes Frederic Sowa dev_hold(rt->dst.dev); 597c2f17e82SHannes Frederic Sowa work->dev = rt->dst.dev; 598c2f17e82SHannes Frederic Sowa schedule_work(&work->work); 599c2f17e82SHannes Frederic Sowa } 600990edb42SMartin KaFai Lau 6018d6c31bfSMartin KaFai Lau out: 6022152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 603f2c31e32SEric Dumazet } 60427097255SYOSHIFUJI Hideaki #else 60527097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt) 60627097255SYOSHIFUJI Hideaki { 60727097255SYOSHIFUJI Hideaki } 60827097255SYOSHIFUJI Hideaki #endif 60927097255SYOSHIFUJI Hideaki 6101da177e4SLinus Torvalds /* 611554cfb7eSYOSHIFUJI Hideaki * Default Router Selection (RFC 2461 6.3.6) 6121da177e4SLinus Torvalds */ 613b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif) 6141da177e4SLinus Torvalds { 615d1918542SDavid S. Miller struct net_device *dev = rt->dst.dev; 616161980f4SDavid S. Miller if (!oif || dev->ifindex == oif) 617554cfb7eSYOSHIFUJI Hideaki return 2; 618161980f4SDavid S. Miller if ((dev->flags & IFF_LOOPBACK) && 619161980f4SDavid S. Miller rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif) 620161980f4SDavid S. Miller return 1; 621554cfb7eSYOSHIFUJI Hideaki return 0; 6221da177e4SLinus Torvalds } 6231da177e4SLinus Torvalds 624afc154e9SHannes Frederic Sowa static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt) 6251da177e4SLinus Torvalds { 626f2c31e32SEric Dumazet struct neighbour *neigh; 627afc154e9SHannes Frederic Sowa enum rt6_nud_state ret = RT6_NUD_FAIL_HARD; 628f2c31e32SEric Dumazet 6294d0c5911SYOSHIFUJI Hideaki if (rt->rt6i_flags & RTF_NONEXTHOP || 6304d0c5911SYOSHIFUJI Hideaki !(rt->rt6i_flags & RTF_GATEWAY)) 631afc154e9SHannes Frederic Sowa return RT6_NUD_SUCCEED; 632145a3621SYOSHIFUJI Hideaki / 吉藤英明 633145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 634145a3621SYOSHIFUJI Hideaki / 吉藤英明 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 635145a3621SYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 636145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_lock(&neigh->lock); 637554cfb7eSYOSHIFUJI Hideaki if (neigh->nud_state & NUD_VALID) 638afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 639398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 640a5a81f0bSPaul Marks else if (!(neigh->nud_state & NUD_FAILED)) 641afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 6427e980569SJiri Benc else 6437e980569SJiri Benc ret = RT6_NUD_FAIL_PROBE; 644398bcbebSYOSHIFUJI Hideaki #endif 645145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_unlock(&neigh->lock); 646afc154e9SHannes Frederic Sowa } else { 647afc154e9SHannes Frederic Sowa ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ? 6487e980569SJiri Benc RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR; 649a5a81f0bSPaul Marks } 650145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 651145a3621SYOSHIFUJI Hideaki / 吉藤英明 652a5a81f0bSPaul Marks return ret; 6531da177e4SLinus Torvalds } 6541da177e4SLinus Torvalds 655554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif, 656554cfb7eSYOSHIFUJI Hideaki int strict) 657554cfb7eSYOSHIFUJI Hideaki { 658a5a81f0bSPaul Marks int m; 6594d0c5911SYOSHIFUJI Hideaki 6604d0c5911SYOSHIFUJI Hideaki m = rt6_check_dev(rt, oif); 66177d16f45SYOSHIFUJI Hideaki if (!m && (strict & RT6_LOOKUP_F_IFACE)) 662afc154e9SHannes Frederic Sowa return RT6_NUD_FAIL_HARD; 663ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 664ebacaaa0SYOSHIFUJI Hideaki m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2; 665ebacaaa0SYOSHIFUJI Hideaki #endif 666afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) { 667afc154e9SHannes Frederic Sowa int n = rt6_check_neigh(rt); 668afc154e9SHannes Frederic Sowa if (n < 0) 669afc154e9SHannes Frederic Sowa return n; 670afc154e9SHannes Frederic Sowa } 671554cfb7eSYOSHIFUJI Hideaki return m; 672554cfb7eSYOSHIFUJI Hideaki } 673554cfb7eSYOSHIFUJI Hideaki 674f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict, 675afc154e9SHannes Frederic Sowa int *mpri, struct rt6_info *match, 676afc154e9SHannes Frederic Sowa bool *do_rr) 677554cfb7eSYOSHIFUJI Hideaki { 678554cfb7eSYOSHIFUJI Hideaki int m; 679afc154e9SHannes Frederic Sowa bool match_do_rr = false; 68035103d11SAndy Gospodarek struct inet6_dev *idev = rt->rt6i_idev; 68135103d11SAndy Gospodarek struct net_device *dev = rt->dst.dev; 68235103d11SAndy Gospodarek 68335103d11SAndy Gospodarek if (dev && !netif_carrier_ok(dev) && 684d5d32e4bSDavid Ahern idev->cnf.ignore_routes_with_linkdown && 685d5d32e4bSDavid Ahern !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE)) 68635103d11SAndy Gospodarek goto out; 687554cfb7eSYOSHIFUJI Hideaki 688554cfb7eSYOSHIFUJI Hideaki if (rt6_check_expired(rt)) 689f11e6659SDavid S. Miller goto out; 690554cfb7eSYOSHIFUJI Hideaki 691554cfb7eSYOSHIFUJI Hideaki m = rt6_score_route(rt, oif, strict); 6927e980569SJiri Benc if (m == RT6_NUD_FAIL_DO_RR) { 693afc154e9SHannes Frederic Sowa match_do_rr = true; 694afc154e9SHannes Frederic Sowa m = 0; /* lowest valid score */ 6957e980569SJiri Benc } else if (m == RT6_NUD_FAIL_HARD) { 696f11e6659SDavid S. Miller goto out; 6971da177e4SLinus Torvalds } 698f11e6659SDavid S. Miller 699afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) 700afc154e9SHannes Frederic Sowa rt6_probe(rt); 701afc154e9SHannes Frederic Sowa 7027e980569SJiri Benc /* note that m can be RT6_NUD_FAIL_PROBE at this point */ 703afc154e9SHannes Frederic Sowa if (m > *mpri) { 704afc154e9SHannes Frederic Sowa *do_rr = match_do_rr; 705afc154e9SHannes Frederic Sowa *mpri = m; 706afc154e9SHannes Frederic Sowa match = rt; 707afc154e9SHannes Frederic Sowa } 708f11e6659SDavid S. Miller out: 709f11e6659SDavid S. Miller return match; 7101da177e4SLinus Torvalds } 7111da177e4SLinus Torvalds 712f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn, 713f11e6659SDavid S. Miller struct rt6_info *rr_head, 714afc154e9SHannes Frederic Sowa u32 metric, int oif, int strict, 715afc154e9SHannes Frederic Sowa bool *do_rr) 716f11e6659SDavid S. Miller { 7179fbdcfafSSteffen Klassert struct rt6_info *rt, *match, *cont; 718f11e6659SDavid S. Miller int mpri = -1; 719f11e6659SDavid S. Miller 720f11e6659SDavid S. Miller match = NULL; 7219fbdcfafSSteffen Klassert cont = NULL; 7229fbdcfafSSteffen Klassert for (rt = rr_head; rt; rt = rt->dst.rt6_next) { 7239fbdcfafSSteffen Klassert if (rt->rt6i_metric != metric) { 7249fbdcfafSSteffen Klassert cont = rt; 7259fbdcfafSSteffen Klassert break; 7269fbdcfafSSteffen Klassert } 7279fbdcfafSSteffen Klassert 728afc154e9SHannes Frederic Sowa match = find_match(rt, oif, strict, &mpri, match, do_rr); 7299fbdcfafSSteffen Klassert } 7309fbdcfafSSteffen Klassert 7319fbdcfafSSteffen Klassert for (rt = fn->leaf; rt && rt != rr_head; rt = rt->dst.rt6_next) { 7329fbdcfafSSteffen Klassert if (rt->rt6i_metric != metric) { 7339fbdcfafSSteffen Klassert cont = rt; 7349fbdcfafSSteffen Klassert break; 7359fbdcfafSSteffen Klassert } 7369fbdcfafSSteffen Klassert 7379fbdcfafSSteffen Klassert match = find_match(rt, oif, strict, &mpri, match, do_rr); 7389fbdcfafSSteffen Klassert } 7399fbdcfafSSteffen Klassert 7409fbdcfafSSteffen Klassert if (match || !cont) 7419fbdcfafSSteffen Klassert return match; 7429fbdcfafSSteffen Klassert 7439fbdcfafSSteffen Klassert for (rt = cont; rt; rt = rt->dst.rt6_next) 744afc154e9SHannes Frederic Sowa match = find_match(rt, oif, strict, &mpri, match, do_rr); 745f11e6659SDavid S. Miller 746f11e6659SDavid S. Miller return match; 747f11e6659SDavid S. Miller } 748f11e6659SDavid S. Miller 749f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict) 750f11e6659SDavid S. Miller { 751f11e6659SDavid S. Miller struct rt6_info *match, *rt0; 7528ed67789SDaniel Lezcano struct net *net; 753afc154e9SHannes Frederic Sowa bool do_rr = false; 754f11e6659SDavid S. Miller 755f11e6659SDavid S. Miller rt0 = fn->rr_ptr; 756f11e6659SDavid S. Miller if (!rt0) 757f11e6659SDavid S. Miller fn->rr_ptr = rt0 = fn->leaf; 758f11e6659SDavid S. Miller 759afc154e9SHannes Frederic Sowa match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict, 760afc154e9SHannes Frederic Sowa &do_rr); 761f11e6659SDavid S. Miller 762afc154e9SHannes Frederic Sowa if (do_rr) { 763d8d1f30bSChangli Gao struct rt6_info *next = rt0->dst.rt6_next; 764f11e6659SDavid S. Miller 765554cfb7eSYOSHIFUJI Hideaki /* no entries matched; do round-robin */ 766f11e6659SDavid S. Miller if (!next || next->rt6i_metric != rt0->rt6i_metric) 767f11e6659SDavid S. Miller next = fn->leaf; 768f11e6659SDavid S. Miller 769f11e6659SDavid S. Miller if (next != rt0) 770f11e6659SDavid S. Miller fn->rr_ptr = next; 771554cfb7eSYOSHIFUJI Hideaki } 772554cfb7eSYOSHIFUJI Hideaki 773d1918542SDavid S. Miller net = dev_net(rt0->dst.dev); 774a02cec21SEric Dumazet return match ? match : net->ipv6.ip6_null_entry; 7751da177e4SLinus Torvalds } 7761da177e4SLinus Torvalds 7778b9df265SMartin KaFai Lau static bool rt6_is_gw_or_nonexthop(const struct rt6_info *rt) 7788b9df265SMartin KaFai Lau { 7798b9df265SMartin KaFai Lau return (rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY)); 7808b9df265SMartin KaFai Lau } 7818b9df265SMartin KaFai Lau 78270ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 78370ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 784b71d1d42SEric Dumazet const struct in6_addr *gwaddr) 78570ceb4f5SYOSHIFUJI Hideaki { 786c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 78770ceb4f5SYOSHIFUJI Hideaki struct route_info *rinfo = (struct route_info *) opt; 78870ceb4f5SYOSHIFUJI Hideaki struct in6_addr prefix_buf, *prefix; 78970ceb4f5SYOSHIFUJI Hideaki unsigned int pref; 7904bed72e4SYOSHIFUJI Hideaki unsigned long lifetime; 79170ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt; 79270ceb4f5SYOSHIFUJI Hideaki 79370ceb4f5SYOSHIFUJI Hideaki if (len < sizeof(struct route_info)) { 79470ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 79570ceb4f5SYOSHIFUJI Hideaki } 79670ceb4f5SYOSHIFUJI Hideaki 79770ceb4f5SYOSHIFUJI Hideaki /* Sanity check for prefix_len and length */ 79870ceb4f5SYOSHIFUJI Hideaki if (rinfo->length > 3) { 79970ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 80070ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 128) { 80170ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 80270ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 64) { 80370ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 2) { 80470ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 80570ceb4f5SYOSHIFUJI Hideaki } 80670ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 0) { 80770ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 1) { 80870ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 80970ceb4f5SYOSHIFUJI Hideaki } 81070ceb4f5SYOSHIFUJI Hideaki } 81170ceb4f5SYOSHIFUJI Hideaki 81270ceb4f5SYOSHIFUJI Hideaki pref = rinfo->route_pref; 81370ceb4f5SYOSHIFUJI Hideaki if (pref == ICMPV6_ROUTER_PREF_INVALID) 8143933fc95SJens Rosenboom return -EINVAL; 81570ceb4f5SYOSHIFUJI Hideaki 8164bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); 81770ceb4f5SYOSHIFUJI Hideaki 81870ceb4f5SYOSHIFUJI Hideaki if (rinfo->length == 3) 81970ceb4f5SYOSHIFUJI Hideaki prefix = (struct in6_addr *)rinfo->prefix; 82070ceb4f5SYOSHIFUJI Hideaki else { 82170ceb4f5SYOSHIFUJI Hideaki /* this function is safe */ 82270ceb4f5SYOSHIFUJI Hideaki ipv6_addr_prefix(&prefix_buf, 82370ceb4f5SYOSHIFUJI Hideaki (struct in6_addr *)rinfo->prefix, 82470ceb4f5SYOSHIFUJI Hideaki rinfo->prefix_len); 82570ceb4f5SYOSHIFUJI Hideaki prefix = &prefix_buf; 82670ceb4f5SYOSHIFUJI Hideaki } 82770ceb4f5SYOSHIFUJI Hideaki 828f104a567SDuan Jiong if (rinfo->prefix_len == 0) 829f104a567SDuan Jiong rt = rt6_get_dflt_router(gwaddr, dev); 830f104a567SDuan Jiong else 831f104a567SDuan Jiong rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, 832830218c1SDavid Ahern gwaddr, dev); 83370ceb4f5SYOSHIFUJI Hideaki 83470ceb4f5SYOSHIFUJI Hideaki if (rt && !lifetime) { 835e0a1ad73SThomas Graf ip6_del_rt(rt); 83670ceb4f5SYOSHIFUJI Hideaki rt = NULL; 83770ceb4f5SYOSHIFUJI Hideaki } 83870ceb4f5SYOSHIFUJI Hideaki 83970ceb4f5SYOSHIFUJI Hideaki if (!rt && lifetime) 840830218c1SDavid Ahern rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, 841830218c1SDavid Ahern dev, pref); 84270ceb4f5SYOSHIFUJI Hideaki else if (rt) 84370ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags = RTF_ROUTEINFO | 84470ceb4f5SYOSHIFUJI Hideaki (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 84570ceb4f5SYOSHIFUJI Hideaki 84670ceb4f5SYOSHIFUJI Hideaki if (rt) { 8471716a961SGao feng if (!addrconf_finite_timeout(lifetime)) 8481716a961SGao feng rt6_clean_expires(rt); 8491716a961SGao feng else 8501716a961SGao feng rt6_set_expires(rt, jiffies + HZ * lifetime); 8511716a961SGao feng 85294e187c0SAmerigo Wang ip6_rt_put(rt); 85370ceb4f5SYOSHIFUJI Hideaki } 85470ceb4f5SYOSHIFUJI Hideaki return 0; 85570ceb4f5SYOSHIFUJI Hideaki } 85670ceb4f5SYOSHIFUJI Hideaki #endif 85770ceb4f5SYOSHIFUJI Hideaki 858a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn, 859a3c00e46SMartin KaFai Lau struct in6_addr *saddr) 860a3c00e46SMartin KaFai Lau { 861a3c00e46SMartin KaFai Lau struct fib6_node *pn; 862a3c00e46SMartin KaFai Lau while (1) { 863a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_TL_ROOT) 864a3c00e46SMartin KaFai Lau return NULL; 865a3c00e46SMartin KaFai Lau pn = fn->parent; 866a3c00e46SMartin KaFai Lau if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) 867a3c00e46SMartin KaFai Lau fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); 868a3c00e46SMartin KaFai Lau else 869a3c00e46SMartin KaFai Lau fn = pn; 870a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_RTINFO) 871a3c00e46SMartin KaFai Lau return fn; 872a3c00e46SMartin KaFai Lau } 873a3c00e46SMartin KaFai Lau } 874c71099acSThomas Graf 8758ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net, 8768ed67789SDaniel Lezcano struct fib6_table *table, 8774c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 8781da177e4SLinus Torvalds { 8791da177e4SLinus Torvalds struct fib6_node *fn; 8801da177e4SLinus Torvalds struct rt6_info *rt; 8811da177e4SLinus Torvalds 882c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 8834c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 884c71099acSThomas Graf restart: 885c71099acSThomas Graf rt = fn->leaf; 8864c9483b2SDavid S. Miller rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags); 88751ebd318SNicolas Dichtel if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0) 88852bd4c0cSNicolas Dichtel rt = rt6_multipath_select(rt, fl6, fl6->flowi6_oif, flags); 889a3c00e46SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 890a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 891a3c00e46SMartin KaFai Lau if (fn) 892a3c00e46SMartin KaFai Lau goto restart; 893a3c00e46SMartin KaFai Lau } 894d8d1f30bSChangli Gao dst_use(&rt->dst, jiffies); 895c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 896b811580dSDavid Ahern 897b811580dSDavid Ahern trace_fib6_table_lookup(net, rt, table->tb6_id, fl6); 898b811580dSDavid Ahern 8991da177e4SLinus Torvalds return rt; 900c71099acSThomas Graf 901c71099acSThomas Graf } 902c71099acSThomas Graf 903ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6, 904ea6e574eSFlorian Westphal int flags) 905ea6e574eSFlorian Westphal { 906ea6e574eSFlorian Westphal return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup); 907ea6e574eSFlorian Westphal } 908ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup); 909ea6e574eSFlorian Westphal 9109acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, 9119acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *saddr, int oif, int strict) 912c71099acSThomas Graf { 9134c9483b2SDavid S. Miller struct flowi6 fl6 = { 9144c9483b2SDavid S. Miller .flowi6_oif = oif, 9154c9483b2SDavid S. Miller .daddr = *daddr, 916c71099acSThomas Graf }; 917c71099acSThomas Graf struct dst_entry *dst; 91877d16f45SYOSHIFUJI Hideaki int flags = strict ? RT6_LOOKUP_F_IFACE : 0; 919c71099acSThomas Graf 920adaa70bbSThomas Graf if (saddr) { 9214c9483b2SDavid S. Miller memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 922adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 923adaa70bbSThomas Graf } 924adaa70bbSThomas Graf 9254c9483b2SDavid S. Miller dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup); 926c71099acSThomas Graf if (dst->error == 0) 927c71099acSThomas Graf return (struct rt6_info *) dst; 928c71099acSThomas Graf 929c71099acSThomas Graf dst_release(dst); 930c71099acSThomas Graf 9311da177e4SLinus Torvalds return NULL; 9321da177e4SLinus Torvalds } 9337159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup); 9347159039aSYOSHIFUJI Hideaki 935c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock. 9361cfb71eeSWei Wang * It takes new route entry, the addition fails by any reason the 9371cfb71eeSWei Wang * route is released. 9381cfb71eeSWei Wang * Caller must hold dst before calling it. 9391da177e4SLinus Torvalds */ 9401da177e4SLinus Torvalds 941e5fd387aSMichal Kubeček static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info, 942333c4301SDavid Ahern struct mx6_config *mxc, 943333c4301SDavid Ahern struct netlink_ext_ack *extack) 9441da177e4SLinus Torvalds { 9451da177e4SLinus Torvalds int err; 946c71099acSThomas Graf struct fib6_table *table; 9471da177e4SLinus Torvalds 948c71099acSThomas Graf table = rt->rt6i_table; 949c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 950333c4301SDavid Ahern err = fib6_add(&table->tb6_root, rt, info, mxc, extack); 951c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 9521da177e4SLinus Torvalds 9531da177e4SLinus Torvalds return err; 9541da177e4SLinus Torvalds } 9551da177e4SLinus Torvalds 95640e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt) 95740e22e8fSThomas Graf { 958e715b6d3SFlorian Westphal struct nl_info info = { .nl_net = dev_net(rt->dst.dev), }; 959e715b6d3SFlorian Westphal struct mx6_config mxc = { .mx = NULL, }; 960e715b6d3SFlorian Westphal 9611cfb71eeSWei Wang /* Hold dst to account for the reference from the fib6 tree */ 9621cfb71eeSWei Wang dst_hold(&rt->dst); 963333c4301SDavid Ahern return __ip6_ins_rt(rt, &info, &mxc, NULL); 96440e22e8fSThomas Graf } 96540e22e8fSThomas Graf 9668b9df265SMartin KaFai Lau static struct rt6_info *ip6_rt_cache_alloc(struct rt6_info *ort, 96721efcfa0SEric Dumazet const struct in6_addr *daddr, 968b71d1d42SEric Dumazet const struct in6_addr *saddr) 9691da177e4SLinus Torvalds { 9701da177e4SLinus Torvalds struct rt6_info *rt; 9711da177e4SLinus Torvalds 9721da177e4SLinus Torvalds /* 9731da177e4SLinus Torvalds * Clone the route. 9741da177e4SLinus Torvalds */ 9751da177e4SLinus Torvalds 976d52d3997SMartin KaFai Lau if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)) 97783a09abdSMartin KaFai Lau ort = (struct rt6_info *)ort->dst.from; 9781da177e4SLinus Torvalds 979ad706862SMartin KaFai Lau rt = __ip6_dst_alloc(dev_net(ort->dst.dev), ort->dst.dev, 0); 98083a09abdSMartin KaFai Lau 98183a09abdSMartin KaFai Lau if (!rt) 98283a09abdSMartin KaFai Lau return NULL; 98383a09abdSMartin KaFai Lau 98483a09abdSMartin KaFai Lau ip6_rt_copy_init(rt, ort); 9858b9df265SMartin KaFai Lau rt->rt6i_flags |= RTF_CACHE; 98683a09abdSMartin KaFai Lau rt->rt6i_metric = 0; 98783a09abdSMartin KaFai Lau rt->dst.flags |= DST_HOST; 98883a09abdSMartin KaFai Lau rt->rt6i_dst.addr = *daddr; 98983a09abdSMartin KaFai Lau rt->rt6i_dst.plen = 128; 9908b9df265SMartin KaFai Lau 9918b9df265SMartin KaFai Lau if (!rt6_is_gw_or_nonexthop(ort)) { 992bb3c3686SDavid S. Miller if (ort->rt6i_dst.plen != 128 && 99321efcfa0SEric Dumazet ipv6_addr_equal(&ort->rt6i_dst.addr, daddr)) 99458c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 9951da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 9961da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 9974e3fd7a0SAlexey Dobriyan rt->rt6i_src.addr = *saddr; 9981da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 9991da177e4SLinus Torvalds } 10001da177e4SLinus Torvalds #endif 100195a9a5baSYOSHIFUJI Hideaki } 100295a9a5baSYOSHIFUJI Hideaki 1003299d9939SYOSHIFUJI Hideaki return rt; 1004299d9939SYOSHIFUJI Hideaki } 1005299d9939SYOSHIFUJI Hideaki 1006d52d3997SMartin KaFai Lau static struct rt6_info *ip6_rt_pcpu_alloc(struct rt6_info *rt) 1007d52d3997SMartin KaFai Lau { 1008d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1009d52d3997SMartin KaFai Lau 1010d52d3997SMartin KaFai Lau pcpu_rt = __ip6_dst_alloc(dev_net(rt->dst.dev), 1011ad706862SMartin KaFai Lau rt->dst.dev, rt->dst.flags); 1012d52d3997SMartin KaFai Lau 1013d52d3997SMartin KaFai Lau if (!pcpu_rt) 1014d52d3997SMartin KaFai Lau return NULL; 1015d52d3997SMartin KaFai Lau ip6_rt_copy_init(pcpu_rt, rt); 1016d52d3997SMartin KaFai Lau pcpu_rt->rt6i_protocol = rt->rt6i_protocol; 1017d52d3997SMartin KaFai Lau pcpu_rt->rt6i_flags |= RTF_PCPU; 1018d52d3997SMartin KaFai Lau return pcpu_rt; 1019d52d3997SMartin KaFai Lau } 1020d52d3997SMartin KaFai Lau 1021d52d3997SMartin KaFai Lau /* It should be called with read_lock_bh(&tb6_lock) acquired */ 1022d52d3997SMartin KaFai Lau static struct rt6_info *rt6_get_pcpu_route(struct rt6_info *rt) 1023d52d3997SMartin KaFai Lau { 1024a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, **p; 1025d52d3997SMartin KaFai Lau 1026d52d3997SMartin KaFai Lau p = this_cpu_ptr(rt->rt6i_pcpu); 1027d52d3997SMartin KaFai Lau pcpu_rt = *p; 1028d52d3997SMartin KaFai Lau 1029a73e4195SMartin KaFai Lau if (pcpu_rt) { 1030a73e4195SMartin KaFai Lau dst_hold(&pcpu_rt->dst); 1031a73e4195SMartin KaFai Lau rt6_dst_from_metrics_check(pcpu_rt); 1032a73e4195SMartin KaFai Lau } 1033a73e4195SMartin KaFai Lau return pcpu_rt; 1034a73e4195SMartin KaFai Lau } 1035a73e4195SMartin KaFai Lau 1036a73e4195SMartin KaFai Lau static struct rt6_info *rt6_make_pcpu_route(struct rt6_info *rt) 1037a73e4195SMartin KaFai Lau { 10389c7370a1SMartin KaFai Lau struct fib6_table *table = rt->rt6i_table; 1039a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, *prev, **p; 1040d52d3997SMartin KaFai Lau 1041d52d3997SMartin KaFai Lau pcpu_rt = ip6_rt_pcpu_alloc(rt); 1042d52d3997SMartin KaFai Lau if (!pcpu_rt) { 1043d52d3997SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 1044d52d3997SMartin KaFai Lau 10459c7370a1SMartin KaFai Lau dst_hold(&net->ipv6.ip6_null_entry->dst); 10469c7370a1SMartin KaFai Lau return net->ipv6.ip6_null_entry; 1047d52d3997SMartin KaFai Lau } 1048d52d3997SMartin KaFai Lau 10499c7370a1SMartin KaFai Lau read_lock_bh(&table->tb6_lock); 10509c7370a1SMartin KaFai Lau if (rt->rt6i_pcpu) { 1051a73e4195SMartin KaFai Lau p = this_cpu_ptr(rt->rt6i_pcpu); 1052d52d3997SMartin KaFai Lau prev = cmpxchg(p, NULL, pcpu_rt); 1053d52d3997SMartin KaFai Lau if (prev) { 1054d52d3997SMartin KaFai Lau /* If someone did it before us, return prev instead */ 1055*587fea74SWei Wang dst_release_immediate(&pcpu_rt->dst); 1056d52d3997SMartin KaFai Lau pcpu_rt = prev; 1057d52d3997SMartin KaFai Lau } 10589c7370a1SMartin KaFai Lau } else { 10599c7370a1SMartin KaFai Lau /* rt has been removed from the fib6 tree 10609c7370a1SMartin KaFai Lau * before we have a chance to acquire the read_lock. 10619c7370a1SMartin KaFai Lau * In this case, don't brother to create a pcpu rt 10629c7370a1SMartin KaFai Lau * since rt is going away anyway. The next 10639c7370a1SMartin KaFai Lau * dst_check() will trigger a re-lookup. 10649c7370a1SMartin KaFai Lau */ 1065*587fea74SWei Wang dst_release_immediate(&pcpu_rt->dst); 10669c7370a1SMartin KaFai Lau pcpu_rt = rt; 10679c7370a1SMartin KaFai Lau } 1068d52d3997SMartin KaFai Lau dst_hold(&pcpu_rt->dst); 1069d52d3997SMartin KaFai Lau rt6_dst_from_metrics_check(pcpu_rt); 10709c7370a1SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 1071d52d3997SMartin KaFai Lau return pcpu_rt; 1072d52d3997SMartin KaFai Lau } 1073d52d3997SMartin KaFai Lau 10749ff74384SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, 10759ff74384SDavid Ahern int oif, struct flowi6 *fl6, int flags) 10761da177e4SLinus Torvalds { 1077367efcb9SMartin KaFai Lau struct fib6_node *fn, *saved_fn; 107845e4fd26SMartin KaFai Lau struct rt6_info *rt; 1079c71099acSThomas Graf int strict = 0; 10801da177e4SLinus Torvalds 108177d16f45SYOSHIFUJI Hideaki strict |= flags & RT6_LOOKUP_F_IFACE; 1082d5d32e4bSDavid Ahern strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE; 1083367efcb9SMartin KaFai Lau if (net->ipv6.devconf_all->forwarding == 0) 1084367efcb9SMartin KaFai Lau strict |= RT6_LOOKUP_F_REACHABLE; 10851da177e4SLinus Torvalds 1086c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 10871da177e4SLinus Torvalds 10884c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1089367efcb9SMartin KaFai Lau saved_fn = fn; 10901da177e4SLinus Torvalds 1091ca254490SDavid Ahern if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) 1092ca254490SDavid Ahern oif = 0; 1093ca254490SDavid Ahern 1094a3c00e46SMartin KaFai Lau redo_rt6_select: 1095367efcb9SMartin KaFai Lau rt = rt6_select(fn, oif, strict); 109652bd4c0cSNicolas Dichtel if (rt->rt6i_nsiblings) 1097367efcb9SMartin KaFai Lau rt = rt6_multipath_select(rt, fl6, oif, strict); 1098a3c00e46SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 1099a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1100a3c00e46SMartin KaFai Lau if (fn) 1101a3c00e46SMartin KaFai Lau goto redo_rt6_select; 1102367efcb9SMartin KaFai Lau else if (strict & RT6_LOOKUP_F_REACHABLE) { 1103367efcb9SMartin KaFai Lau /* also consider unreachable route */ 1104367efcb9SMartin KaFai Lau strict &= ~RT6_LOOKUP_F_REACHABLE; 1105367efcb9SMartin KaFai Lau fn = saved_fn; 1106367efcb9SMartin KaFai Lau goto redo_rt6_select; 1107367efcb9SMartin KaFai Lau } 1108a3c00e46SMartin KaFai Lau } 1109a3c00e46SMartin KaFai Lau 1110d52d3997SMartin KaFai Lau 1111d52d3997SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry || (rt->rt6i_flags & RTF_CACHE)) { 11123da59bd9SMartin KaFai Lau dst_use(&rt->dst, jiffies); 1113c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 11141da177e4SLinus Torvalds 1115d52d3997SMartin KaFai Lau rt6_dst_from_metrics_check(rt); 1116b811580dSDavid Ahern 1117b811580dSDavid Ahern trace_fib6_table_lookup(net, rt, table->tb6_id, fl6); 1118d52d3997SMartin KaFai Lau return rt; 11193da59bd9SMartin KaFai Lau } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) && 11203da59bd9SMartin KaFai Lau !(rt->rt6i_flags & RTF_GATEWAY))) { 11213da59bd9SMartin KaFai Lau /* Create a RTF_CACHE clone which will not be 11223da59bd9SMartin KaFai Lau * owned by the fib6 tree. It is for the special case where 11233da59bd9SMartin KaFai Lau * the daddr in the skb during the neighbor look-up is different 11243da59bd9SMartin KaFai Lau * from the fl6->daddr used to look-up route here. 11253da59bd9SMartin KaFai Lau */ 1126c71099acSThomas Graf 11273da59bd9SMartin KaFai Lau struct rt6_info *uncached_rt; 11283da59bd9SMartin KaFai Lau 1129d52d3997SMartin KaFai Lau dst_use(&rt->dst, jiffies); 1130d52d3997SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 1131d52d3997SMartin KaFai Lau 11323da59bd9SMartin KaFai Lau uncached_rt = ip6_rt_cache_alloc(rt, &fl6->daddr, NULL); 11333da59bd9SMartin KaFai Lau dst_release(&rt->dst); 11343da59bd9SMartin KaFai Lau 11351cfb71eeSWei Wang if (uncached_rt) { 11361cfb71eeSWei Wang /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc() 11371cfb71eeSWei Wang * No need for another dst_hold() 11381cfb71eeSWei Wang */ 11398d0b94afSMartin KaFai Lau rt6_uncached_list_add(uncached_rt); 11401cfb71eeSWei Wang } else { 11413da59bd9SMartin KaFai Lau uncached_rt = net->ipv6.ip6_null_entry; 11423da59bd9SMartin KaFai Lau dst_hold(&uncached_rt->dst); 11431cfb71eeSWei Wang } 1144b811580dSDavid Ahern 1145b811580dSDavid Ahern trace_fib6_table_lookup(net, uncached_rt, table->tb6_id, fl6); 11463da59bd9SMartin KaFai Lau return uncached_rt; 11473da59bd9SMartin KaFai Lau 1148d52d3997SMartin KaFai Lau } else { 1149d52d3997SMartin KaFai Lau /* Get a percpu copy */ 1150d52d3997SMartin KaFai Lau 1151d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1152d52d3997SMartin KaFai Lau 1153d52d3997SMartin KaFai Lau rt->dst.lastuse = jiffies; 1154d52d3997SMartin KaFai Lau rt->dst.__use++; 1155d52d3997SMartin KaFai Lau pcpu_rt = rt6_get_pcpu_route(rt); 1156d52d3997SMartin KaFai Lau 11579c7370a1SMartin KaFai Lau if (pcpu_rt) { 1158d52d3997SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 11599c7370a1SMartin KaFai Lau } else { 11609c7370a1SMartin KaFai Lau /* We have to do the read_unlock first 11619c7370a1SMartin KaFai Lau * because rt6_make_pcpu_route() may trigger 11629c7370a1SMartin KaFai Lau * ip6_dst_gc() which will take the write_lock. 11639c7370a1SMartin KaFai Lau */ 11649c7370a1SMartin KaFai Lau dst_hold(&rt->dst); 11659c7370a1SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 11669c7370a1SMartin KaFai Lau pcpu_rt = rt6_make_pcpu_route(rt); 11679c7370a1SMartin KaFai Lau dst_release(&rt->dst); 11689c7370a1SMartin KaFai Lau } 1169d52d3997SMartin KaFai Lau 1170b811580dSDavid Ahern trace_fib6_table_lookup(net, pcpu_rt, table->tb6_id, fl6); 1171d52d3997SMartin KaFai Lau return pcpu_rt; 11729c7370a1SMartin KaFai Lau 1173d52d3997SMartin KaFai Lau } 1174c71099acSThomas Graf } 11759ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route); 1176c71099acSThomas Graf 11778ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table, 11784c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 11794acad72dSPavel Emelyanov { 11804c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags); 11814acad72dSPavel Emelyanov } 11824acad72dSPavel Emelyanov 1183d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net, 118472331bc0SShmulik Ladkani struct net_device *dev, 118572331bc0SShmulik Ladkani struct flowi6 *fl6, int flags) 118672331bc0SShmulik Ladkani { 118772331bc0SShmulik Ladkani if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) 118872331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_IFACE; 118972331bc0SShmulik Ladkani 119072331bc0SShmulik Ladkani return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input); 119172331bc0SShmulik Ladkani } 1192d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup); 119372331bc0SShmulik Ladkani 1194c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 1195c71099acSThomas Graf { 1196b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 1197c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 1198adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 1199904af04dSJiri Benc struct ip_tunnel_info *tun_info; 12004c9483b2SDavid S. Miller struct flowi6 fl6 = { 1201e0d56fddSDavid Ahern .flowi6_iif = skb->dev->ifindex, 12024c9483b2SDavid S. Miller .daddr = iph->daddr, 12034c9483b2SDavid S. Miller .saddr = iph->saddr, 12046502ca52SYOSHIFUJI Hideaki / 吉藤英明 .flowlabel = ip6_flowinfo(iph), 12054c9483b2SDavid S. Miller .flowi6_mark = skb->mark, 12064c9483b2SDavid S. Miller .flowi6_proto = iph->nexthdr, 1207c71099acSThomas Graf }; 1208adaa70bbSThomas Graf 1209904af04dSJiri Benc tun_info = skb_tunnel_info(skb); 121046fa062aSJiri Benc if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) 1211904af04dSJiri Benc fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id; 121206e9d040SJiri Benc skb_dst_drop(skb); 121372331bc0SShmulik Ladkani skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags)); 1214c71099acSThomas Graf } 1215c71099acSThomas Graf 12168ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table, 12174c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 1218c71099acSThomas Graf { 12194c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags); 1220c71099acSThomas Graf } 1221c71099acSThomas Graf 12226f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk, 12236f21c96aSPaolo Abeni struct flowi6 *fl6, int flags) 1224c71099acSThomas Graf { 1225d46a9d67SDavid Ahern bool any_src; 1226c71099acSThomas Graf 12274c1feac5SDavid Ahern if (rt6_need_strict(&fl6->daddr)) { 12284c1feac5SDavid Ahern struct dst_entry *dst; 12294c1feac5SDavid Ahern 12304c1feac5SDavid Ahern dst = l3mdev_link_scope_lookup(net, fl6); 1231ca254490SDavid Ahern if (dst) 1232ca254490SDavid Ahern return dst; 12334c1feac5SDavid Ahern } 1234ca254490SDavid Ahern 12351fb9489bSPavel Emelyanov fl6->flowi6_iif = LOOPBACK_IFINDEX; 12364dc27d1cSDavid McCullough 1237d46a9d67SDavid Ahern any_src = ipv6_addr_any(&fl6->saddr); 1238741a11d9SDavid Ahern if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) || 1239d46a9d67SDavid Ahern (fl6->flowi6_oif && any_src)) 124077d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 1241c71099acSThomas Graf 1242d46a9d67SDavid Ahern if (!any_src) 1243adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 12440c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 12450c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 1246adaa70bbSThomas Graf 12474c9483b2SDavid S. Miller return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output); 12481da177e4SLinus Torvalds } 12496f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags); 12501da177e4SLinus Torvalds 12512774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 125214e50e57SDavid S. Miller { 12535c1e6aa3SDavid S. Miller struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; 12541dbe3252SWei Wang struct net_device *loopback_dev = net->loopback_dev; 125514e50e57SDavid S. Miller struct dst_entry *new = NULL; 125614e50e57SDavid S. Miller 12571dbe3252SWei Wang rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1, 1258*587fea74SWei Wang DST_OBSOLETE_NONE, DST_NOGC); 125914e50e57SDavid S. Miller if (rt) { 12600a1f5962SMartin KaFai Lau rt6_info_init(rt); 12610a1f5962SMartin KaFai Lau 1262d8d1f30bSChangli Gao new = &rt->dst; 126314e50e57SDavid S. Miller new->__use = 1; 1264352e512cSHerbert Xu new->input = dst_discard; 1265ede2059dSEric W. Biederman new->output = dst_discard_out; 126614e50e57SDavid S. Miller 1267defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 126814e50e57SDavid S. Miller 12691dbe3252SWei Wang rt->rt6i_idev = in6_dev_get(loopback_dev); 12704e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 12710a1f5962SMartin KaFai Lau rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU; 127214e50e57SDavid S. Miller rt->rt6i_metric = 0; 127314e50e57SDavid S. Miller 127414e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 127514e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 127614e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 127714e50e57SDavid S. Miller #endif 127814e50e57SDavid S. Miller } 127914e50e57SDavid S. Miller 128069ead7afSDavid S. Miller dst_release(dst_orig); 128169ead7afSDavid S. Miller return new ? new : ERR_PTR(-ENOMEM); 128214e50e57SDavid S. Miller } 128314e50e57SDavid S. Miller 12841da177e4SLinus Torvalds /* 12851da177e4SLinus Torvalds * Destination cache support functions 12861da177e4SLinus Torvalds */ 12871da177e4SLinus Torvalds 12884b32b5adSMartin KaFai Lau static void rt6_dst_from_metrics_check(struct rt6_info *rt) 12894b32b5adSMartin KaFai Lau { 12904b32b5adSMartin KaFai Lau if (rt->dst.from && 12914b32b5adSMartin KaFai Lau dst_metrics_ptr(&rt->dst) != dst_metrics_ptr(rt->dst.from)) 12924b32b5adSMartin KaFai Lau dst_init_metrics(&rt->dst, dst_metrics_ptr(rt->dst.from), true); 12934b32b5adSMartin KaFai Lau } 12944b32b5adSMartin KaFai Lau 12953da59bd9SMartin KaFai Lau static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie) 12963da59bd9SMartin KaFai Lau { 12973da59bd9SMartin KaFai Lau if (!rt->rt6i_node || (rt->rt6i_node->fn_sernum != cookie)) 12983da59bd9SMartin KaFai Lau return NULL; 12993da59bd9SMartin KaFai Lau 13003da59bd9SMartin KaFai Lau if (rt6_check_expired(rt)) 13013da59bd9SMartin KaFai Lau return NULL; 13023da59bd9SMartin KaFai Lau 13033da59bd9SMartin KaFai Lau return &rt->dst; 13043da59bd9SMartin KaFai Lau } 13053da59bd9SMartin KaFai Lau 13063da59bd9SMartin KaFai Lau static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, u32 cookie) 13073da59bd9SMartin KaFai Lau { 13085973fb1eSMartin KaFai Lau if (!__rt6_check_expired(rt) && 13095973fb1eSMartin KaFai Lau rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && 13103da59bd9SMartin KaFai Lau rt6_check((struct rt6_info *)(rt->dst.from), cookie)) 13113da59bd9SMartin KaFai Lau return &rt->dst; 13123da59bd9SMartin KaFai Lau else 13133da59bd9SMartin KaFai Lau return NULL; 13143da59bd9SMartin KaFai Lau } 13153da59bd9SMartin KaFai Lau 13161da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 13171da177e4SLinus Torvalds { 13181da177e4SLinus Torvalds struct rt6_info *rt; 13191da177e4SLinus Torvalds 13201da177e4SLinus Torvalds rt = (struct rt6_info *) dst; 13211da177e4SLinus Torvalds 13226f3118b5SNicolas Dichtel /* All IPV6 dsts are created with ->obsolete set to the value 13236f3118b5SNicolas Dichtel * DST_OBSOLETE_FORCE_CHK which forces validation calls down 13246f3118b5SNicolas Dichtel * into this function always. 13256f3118b5SNicolas Dichtel */ 1326e3bc10bdSHannes Frederic Sowa 13274b32b5adSMartin KaFai Lau rt6_dst_from_metrics_check(rt); 13284b32b5adSMartin KaFai Lau 132902bcf4e0SMartin KaFai Lau if (rt->rt6i_flags & RTF_PCPU || 133002bcf4e0SMartin KaFai Lau (unlikely(dst->flags & DST_NOCACHE) && rt->dst.from)) 13313da59bd9SMartin KaFai Lau return rt6_dst_from_check(rt, cookie); 13323da59bd9SMartin KaFai Lau else 13333da59bd9SMartin KaFai Lau return rt6_check(rt, cookie); 13341da177e4SLinus Torvalds } 13351da177e4SLinus Torvalds 13361da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 13371da177e4SLinus Torvalds { 13381da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 13391da177e4SLinus Torvalds 13401da177e4SLinus Torvalds if (rt) { 134154c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 134254c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 1343e0a1ad73SThomas Graf ip6_del_rt(rt); 134454c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 13451da177e4SLinus Torvalds } 134654c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 134754c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 134854c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 134954c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 135054c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 135154c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 13521da177e4SLinus Torvalds } 13531da177e4SLinus Torvalds 13541da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 13551da177e4SLinus Torvalds { 13561da177e4SLinus Torvalds struct rt6_info *rt; 13571da177e4SLinus Torvalds 13583ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 13591da177e4SLinus Torvalds 1360adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 13611da177e4SLinus Torvalds if (rt) { 13621eb4f758SHannes Frederic Sowa if (rt->rt6i_flags & RTF_CACHE) { 1363ad65a2f0SWei Wang if (dst_hold_safe(&rt->dst)) 13648e3d5be7SMartin KaFai Lau ip6_del_rt(rt); 13651eb4f758SHannes Frederic Sowa } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) { 13661da177e4SLinus Torvalds rt->rt6i_node->fn_sernum = -1; 13671da177e4SLinus Torvalds } 13681da177e4SLinus Torvalds } 13691eb4f758SHannes Frederic Sowa } 13701da177e4SLinus Torvalds 137145e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu) 137245e4fd26SMartin KaFai Lau { 137345e4fd26SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 137445e4fd26SMartin KaFai Lau 137545e4fd26SMartin KaFai Lau rt->rt6i_flags |= RTF_MODIFIED; 137645e4fd26SMartin KaFai Lau rt->rt6i_pmtu = mtu; 137745e4fd26SMartin KaFai Lau rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires); 137845e4fd26SMartin KaFai Lau } 137945e4fd26SMartin KaFai Lau 13800d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt) 13810d3f6d29SMartin KaFai Lau { 13820d3f6d29SMartin KaFai Lau return !(rt->rt6i_flags & RTF_CACHE) && 13830d3f6d29SMartin KaFai Lau (rt->rt6i_flags & RTF_PCPU || rt->rt6i_node); 13840d3f6d29SMartin KaFai Lau } 13850d3f6d29SMartin KaFai Lau 138645e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk, 138745e4fd26SMartin KaFai Lau const struct ipv6hdr *iph, u32 mtu) 13881da177e4SLinus Torvalds { 13890dec879fSJulian Anastasov const struct in6_addr *daddr, *saddr; 13901da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info *)dst; 13911da177e4SLinus Torvalds 139245e4fd26SMartin KaFai Lau if (rt6->rt6i_flags & RTF_LOCAL) 139345e4fd26SMartin KaFai Lau return; 139445e4fd26SMartin KaFai Lau 139519bda36cSXin Long if (dst_metric_locked(dst, RTAX_MTU)) 139619bda36cSXin Long return; 139719bda36cSXin Long 139845e4fd26SMartin KaFai Lau if (iph) { 139945e4fd26SMartin KaFai Lau daddr = &iph->daddr; 140045e4fd26SMartin KaFai Lau saddr = &iph->saddr; 140145e4fd26SMartin KaFai Lau } else if (sk) { 140245e4fd26SMartin KaFai Lau daddr = &sk->sk_v6_daddr; 140345e4fd26SMartin KaFai Lau saddr = &inet6_sk(sk)->saddr; 140445e4fd26SMartin KaFai Lau } else { 14050dec879fSJulian Anastasov daddr = NULL; 14060dec879fSJulian Anastasov saddr = NULL; 14071da177e4SLinus Torvalds } 14080dec879fSJulian Anastasov dst_confirm_neigh(dst, daddr); 14090dec879fSJulian Anastasov mtu = max_t(u32, mtu, IPV6_MIN_MTU); 14100dec879fSJulian Anastasov if (mtu >= dst_mtu(dst)) 14110dec879fSJulian Anastasov return; 14120dec879fSJulian Anastasov 14130dec879fSJulian Anastasov if (!rt6_cache_allowed_for_pmtu(rt6)) { 14140dec879fSJulian Anastasov rt6_do_update_pmtu(rt6, mtu); 14150dec879fSJulian Anastasov } else if (daddr) { 14160dec879fSJulian Anastasov struct rt6_info *nrt6; 14170dec879fSJulian Anastasov 141845e4fd26SMartin KaFai Lau nrt6 = ip6_rt_cache_alloc(rt6, daddr, saddr); 141945e4fd26SMartin KaFai Lau if (nrt6) { 142045e4fd26SMartin KaFai Lau rt6_do_update_pmtu(nrt6, mtu); 142145e4fd26SMartin KaFai Lau 142245e4fd26SMartin KaFai Lau /* ip6_ins_rt(nrt6) will bump the 142345e4fd26SMartin KaFai Lau * rt6->rt6i_node->fn_sernum 142445e4fd26SMartin KaFai Lau * which will fail the next rt6_check() and 142545e4fd26SMartin KaFai Lau * invalidate the sk->sk_dst_cache. 142645e4fd26SMartin KaFai Lau */ 142745e4fd26SMartin KaFai Lau ip6_ins_rt(nrt6); 14281cfb71eeSWei Wang /* Release the reference taken in 14291cfb71eeSWei Wang * ip6_rt_cache_alloc() 14301cfb71eeSWei Wang */ 14311cfb71eeSWei Wang dst_release(&nrt6->dst); 143245e4fd26SMartin KaFai Lau } 143345e4fd26SMartin KaFai Lau } 143445e4fd26SMartin KaFai Lau } 143545e4fd26SMartin KaFai Lau 143645e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 143745e4fd26SMartin KaFai Lau struct sk_buff *skb, u32 mtu) 143845e4fd26SMartin KaFai Lau { 143945e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu); 14401da177e4SLinus Torvalds } 14411da177e4SLinus Torvalds 144242ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, 1443e2d118a1SLorenzo Colitti int oif, u32 mark, kuid_t uid) 144481aded24SDavid S. Miller { 144581aded24SDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 144681aded24SDavid S. Miller struct dst_entry *dst; 144781aded24SDavid S. Miller struct flowi6 fl6; 144881aded24SDavid S. Miller 144981aded24SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 145081aded24SDavid S. Miller fl6.flowi6_oif = oif; 14511b3c61dcSLorenzo Colitti fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark); 145281aded24SDavid S. Miller fl6.daddr = iph->daddr; 145381aded24SDavid S. Miller fl6.saddr = iph->saddr; 14546502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 1455e2d118a1SLorenzo Colitti fl6.flowi6_uid = uid; 145681aded24SDavid S. Miller 145781aded24SDavid S. Miller dst = ip6_route_output(net, NULL, &fl6); 145881aded24SDavid S. Miller if (!dst->error) 145945e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu)); 146081aded24SDavid S. Miller dst_release(dst); 146181aded24SDavid S. Miller } 146281aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu); 146381aded24SDavid S. Miller 146481aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) 146581aded24SDavid S. Miller { 146633c162a9SMartin KaFai Lau struct dst_entry *dst; 146733c162a9SMartin KaFai Lau 146881aded24SDavid S. Miller ip6_update_pmtu(skb, sock_net(sk), mtu, 1469e2d118a1SLorenzo Colitti sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid); 147033c162a9SMartin KaFai Lau 147133c162a9SMartin KaFai Lau dst = __sk_dst_get(sk); 147233c162a9SMartin KaFai Lau if (!dst || !dst->obsolete || 147333c162a9SMartin KaFai Lau dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) 147433c162a9SMartin KaFai Lau return; 147533c162a9SMartin KaFai Lau 147633c162a9SMartin KaFai Lau bh_lock_sock(sk); 147733c162a9SMartin KaFai Lau if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) 147833c162a9SMartin KaFai Lau ip6_datagram_dst_update(sk, false); 147933c162a9SMartin KaFai Lau bh_unlock_sock(sk); 148081aded24SDavid S. Miller } 148181aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); 148281aded24SDavid S. Miller 1483b55b76b2SDuan Jiong /* Handle redirects */ 1484b55b76b2SDuan Jiong struct ip6rd_flowi { 1485b55b76b2SDuan Jiong struct flowi6 fl6; 1486b55b76b2SDuan Jiong struct in6_addr gateway; 1487b55b76b2SDuan Jiong }; 1488b55b76b2SDuan Jiong 1489b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net, 1490b55b76b2SDuan Jiong struct fib6_table *table, 1491b55b76b2SDuan Jiong struct flowi6 *fl6, 1492b55b76b2SDuan Jiong int flags) 1493b55b76b2SDuan Jiong { 1494b55b76b2SDuan Jiong struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; 1495b55b76b2SDuan Jiong struct rt6_info *rt; 1496b55b76b2SDuan Jiong struct fib6_node *fn; 1497b55b76b2SDuan Jiong 1498b55b76b2SDuan Jiong /* Get the "current" route for this destination and 149967c408cfSAlexander Alemayhu * check if the redirect has come from appropriate router. 1500b55b76b2SDuan Jiong * 1501b55b76b2SDuan Jiong * RFC 4861 specifies that redirects should only be 1502b55b76b2SDuan Jiong * accepted if they come from the nexthop to the target. 1503b55b76b2SDuan Jiong * Due to the way the routes are chosen, this notion 1504b55b76b2SDuan Jiong * is a bit fuzzy and one might need to check all possible 1505b55b76b2SDuan Jiong * routes. 1506b55b76b2SDuan Jiong */ 1507b55b76b2SDuan Jiong 1508b55b76b2SDuan Jiong read_lock_bh(&table->tb6_lock); 1509b55b76b2SDuan Jiong fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1510b55b76b2SDuan Jiong restart: 1511b55b76b2SDuan Jiong for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 1512b55b76b2SDuan Jiong if (rt6_check_expired(rt)) 1513b55b76b2SDuan Jiong continue; 1514b55b76b2SDuan Jiong if (rt->dst.error) 1515b55b76b2SDuan Jiong break; 1516b55b76b2SDuan Jiong if (!(rt->rt6i_flags & RTF_GATEWAY)) 1517b55b76b2SDuan Jiong continue; 1518b55b76b2SDuan Jiong if (fl6->flowi6_oif != rt->dst.dev->ifindex) 1519b55b76b2SDuan Jiong continue; 1520b55b76b2SDuan Jiong if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway)) 1521b55b76b2SDuan Jiong continue; 1522b55b76b2SDuan Jiong break; 1523b55b76b2SDuan Jiong } 1524b55b76b2SDuan Jiong 1525b55b76b2SDuan Jiong if (!rt) 1526b55b76b2SDuan Jiong rt = net->ipv6.ip6_null_entry; 1527b55b76b2SDuan Jiong else if (rt->dst.error) { 1528b55b76b2SDuan Jiong rt = net->ipv6.ip6_null_entry; 1529b0a1ba59SMartin KaFai Lau goto out; 1530b0a1ba59SMartin KaFai Lau } 1531b0a1ba59SMartin KaFai Lau 1532b0a1ba59SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 1533a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1534a3c00e46SMartin KaFai Lau if (fn) 1535a3c00e46SMartin KaFai Lau goto restart; 1536b55b76b2SDuan Jiong } 1537a3c00e46SMartin KaFai Lau 1538b0a1ba59SMartin KaFai Lau out: 1539b55b76b2SDuan Jiong dst_hold(&rt->dst); 1540b55b76b2SDuan Jiong 1541b55b76b2SDuan Jiong read_unlock_bh(&table->tb6_lock); 1542b55b76b2SDuan Jiong 1543b811580dSDavid Ahern trace_fib6_table_lookup(net, rt, table->tb6_id, fl6); 1544b55b76b2SDuan Jiong return rt; 1545b55b76b2SDuan Jiong }; 1546b55b76b2SDuan Jiong 1547b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net, 1548b55b76b2SDuan Jiong const struct flowi6 *fl6, 1549b55b76b2SDuan Jiong const struct in6_addr *gateway) 1550b55b76b2SDuan Jiong { 1551b55b76b2SDuan Jiong int flags = RT6_LOOKUP_F_HAS_SADDR; 1552b55b76b2SDuan Jiong struct ip6rd_flowi rdfl; 1553b55b76b2SDuan Jiong 1554b55b76b2SDuan Jiong rdfl.fl6 = *fl6; 1555b55b76b2SDuan Jiong rdfl.gateway = *gateway; 1556b55b76b2SDuan Jiong 1557b55b76b2SDuan Jiong return fib6_rule_lookup(net, &rdfl.fl6, 1558b55b76b2SDuan Jiong flags, __ip6_route_redirect); 1559b55b76b2SDuan Jiong } 1560b55b76b2SDuan Jiong 1561e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, 1562e2d118a1SLorenzo Colitti kuid_t uid) 15633a5ad2eeSDavid S. Miller { 15643a5ad2eeSDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 15653a5ad2eeSDavid S. Miller struct dst_entry *dst; 15663a5ad2eeSDavid S. Miller struct flowi6 fl6; 15673a5ad2eeSDavid S. Miller 15683a5ad2eeSDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 1569e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 15703a5ad2eeSDavid S. Miller fl6.flowi6_oif = oif; 15713a5ad2eeSDavid S. Miller fl6.flowi6_mark = mark; 15723a5ad2eeSDavid S. Miller fl6.daddr = iph->daddr; 15733a5ad2eeSDavid S. Miller fl6.saddr = iph->saddr; 15746502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 1575e2d118a1SLorenzo Colitti fl6.flowi6_uid = uid; 15763a5ad2eeSDavid S. Miller 1577b55b76b2SDuan Jiong dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr); 15786700c270SDavid S. Miller rt6_do_redirect(dst, NULL, skb); 15793a5ad2eeSDavid S. Miller dst_release(dst); 15803a5ad2eeSDavid S. Miller } 15813a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect); 15823a5ad2eeSDavid S. Miller 1583c92a59ecSDuan Jiong void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif, 1584c92a59ecSDuan Jiong u32 mark) 1585c92a59ecSDuan Jiong { 1586c92a59ecSDuan Jiong const struct ipv6hdr *iph = ipv6_hdr(skb); 1587c92a59ecSDuan Jiong const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); 1588c92a59ecSDuan Jiong struct dst_entry *dst; 1589c92a59ecSDuan Jiong struct flowi6 fl6; 1590c92a59ecSDuan Jiong 1591c92a59ecSDuan Jiong memset(&fl6, 0, sizeof(fl6)); 1592e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 1593c92a59ecSDuan Jiong fl6.flowi6_oif = oif; 1594c92a59ecSDuan Jiong fl6.flowi6_mark = mark; 1595c92a59ecSDuan Jiong fl6.daddr = msg->dest; 1596c92a59ecSDuan Jiong fl6.saddr = iph->daddr; 1597e2d118a1SLorenzo Colitti fl6.flowi6_uid = sock_net_uid(net, NULL); 1598c92a59ecSDuan Jiong 1599b55b76b2SDuan Jiong dst = ip6_route_redirect(net, &fl6, &iph->saddr); 1600c92a59ecSDuan Jiong rt6_do_redirect(dst, NULL, skb); 1601c92a59ecSDuan Jiong dst_release(dst); 1602c92a59ecSDuan Jiong } 1603c92a59ecSDuan Jiong 16043a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) 16053a5ad2eeSDavid S. Miller { 1606e2d118a1SLorenzo Colitti ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark, 1607e2d118a1SLorenzo Colitti sk->sk_uid); 16083a5ad2eeSDavid S. Miller } 16093a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect); 16103a5ad2eeSDavid S. Miller 16110dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 16121da177e4SLinus Torvalds { 16130dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 16140dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 16150dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 16160dbaee3bSDavid S. Miller 16171da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 16181da177e4SLinus Torvalds 16195578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 16205578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 16211da177e4SLinus Torvalds 16221da177e4SLinus Torvalds /* 16231da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 16241da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 16251da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 16261da177e4SLinus Torvalds * rely only on pmtu discovery" 16271da177e4SLinus Torvalds */ 16281da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 16291da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 16301da177e4SLinus Torvalds return mtu; 16311da177e4SLinus Torvalds } 16321da177e4SLinus Torvalds 1633ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst) 1634d33e4553SDavid S. Miller { 16354b32b5adSMartin KaFai Lau const struct rt6_info *rt = (const struct rt6_info *)dst; 16364b32b5adSMartin KaFai Lau unsigned int mtu = rt->rt6i_pmtu; 1637d33e4553SDavid S. Miller struct inet6_dev *idev; 1638618f9bc7SSteffen Klassert 1639618f9bc7SSteffen Klassert if (mtu) 164030f78d8eSEric Dumazet goto out; 1641618f9bc7SSteffen Klassert 16424b32b5adSMartin KaFai Lau mtu = dst_metric_raw(dst, RTAX_MTU); 16434b32b5adSMartin KaFai Lau if (mtu) 16444b32b5adSMartin KaFai Lau goto out; 16454b32b5adSMartin KaFai Lau 1646618f9bc7SSteffen Klassert mtu = IPV6_MIN_MTU; 1647d33e4553SDavid S. Miller 1648d33e4553SDavid S. Miller rcu_read_lock(); 1649d33e4553SDavid S. Miller idev = __in6_dev_get(dst->dev); 1650d33e4553SDavid S. Miller if (idev) 1651d33e4553SDavid S. Miller mtu = idev->cnf.mtu6; 1652d33e4553SDavid S. Miller rcu_read_unlock(); 1653d33e4553SDavid S. Miller 165430f78d8eSEric Dumazet out: 165514972cbdSRoopa Prabhu mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 165614972cbdSRoopa Prabhu 165714972cbdSRoopa Prabhu return mtu - lwtunnel_headroom(dst->lwtstate, mtu); 1658d33e4553SDavid S. Miller } 1659d33e4553SDavid S. Miller 16603b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list; 16613b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock); 16625d0bbeebSThomas Graf 16633b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 166487a11578SDavid S. Miller struct flowi6 *fl6) 16651da177e4SLinus Torvalds { 166687a11578SDavid S. Miller struct dst_entry *dst; 16671da177e4SLinus Torvalds struct rt6_info *rt; 16681da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 1669c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 16701da177e4SLinus Torvalds 167138308473SDavid S. Miller if (unlikely(!idev)) 1672122bdf67SEric Dumazet return ERR_PTR(-ENODEV); 16731da177e4SLinus Torvalds 1674ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, dev, 0); 167538308473SDavid S. Miller if (unlikely(!rt)) { 16761da177e4SLinus Torvalds in6_dev_put(idev); 167787a11578SDavid S. Miller dst = ERR_PTR(-ENOMEM); 16781da177e4SLinus Torvalds goto out; 16791da177e4SLinus Torvalds } 16801da177e4SLinus Torvalds 16818e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 16828e2ec639SYan, Zheng rt->dst.output = ip6_output; 1683550bab42SJulian Anastasov rt->rt6i_gateway = fl6->daddr; 168487a11578SDavid S. Miller rt->rt6i_dst.addr = fl6->daddr; 16858e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 16868e2ec639SYan, Zheng rt->rt6i_idev = idev; 168714edd87dSLi RongQing dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); 16881da177e4SLinus Torvalds 1689*587fea74SWei Wang /* Add this dst into uncached_list so that rt6_ifdown() can 1690*587fea74SWei Wang * do proper release of the net_device 1691*587fea74SWei Wang */ 1692*587fea74SWei Wang rt6_uncached_list_add(rt); 16931da177e4SLinus Torvalds 169487a11578SDavid S. Miller dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); 169587a11578SDavid S. Miller 16961da177e4SLinus Torvalds out: 169787a11578SDavid S. Miller return dst; 16981da177e4SLinus Torvalds } 16991da177e4SLinus Torvalds 17003d0f24a7SStephen Hemminger int icmp6_dst_gc(void) 17011da177e4SLinus Torvalds { 1702e9476e95SHagen Paul Pfeifer struct dst_entry *dst, **pprev; 17033d0f24a7SStephen Hemminger int more = 0; 17041da177e4SLinus Torvalds 17053b00944cSYOSHIFUJI Hideaki spin_lock_bh(&icmp6_dst_lock); 17063b00944cSYOSHIFUJI Hideaki pprev = &icmp6_dst_gc_list; 17075d0bbeebSThomas Graf 17081da177e4SLinus Torvalds while ((dst = *pprev) != NULL) { 17091da177e4SLinus Torvalds if (!atomic_read(&dst->__refcnt)) { 17101da177e4SLinus Torvalds *pprev = dst->next; 17111da177e4SLinus Torvalds dst_free(dst); 17121da177e4SLinus Torvalds } else { 17131da177e4SLinus Torvalds pprev = &dst->next; 17143d0f24a7SStephen Hemminger ++more; 17151da177e4SLinus Torvalds } 17161da177e4SLinus Torvalds } 17171da177e4SLinus Torvalds 17183b00944cSYOSHIFUJI Hideaki spin_unlock_bh(&icmp6_dst_lock); 17195d0bbeebSThomas Graf 17203d0f24a7SStephen Hemminger return more; 17211da177e4SLinus Torvalds } 17221da177e4SLinus Torvalds 17231e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg), 17241e493d19SDavid S. Miller void *arg) 17251e493d19SDavid S. Miller { 17261e493d19SDavid S. Miller struct dst_entry *dst, **pprev; 17271e493d19SDavid S. Miller 17281e493d19SDavid S. Miller spin_lock_bh(&icmp6_dst_lock); 17291e493d19SDavid S. Miller pprev = &icmp6_dst_gc_list; 17301e493d19SDavid S. Miller while ((dst = *pprev) != NULL) { 17311e493d19SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 17321e493d19SDavid S. Miller if (func(rt, arg)) { 17331e493d19SDavid S. Miller *pprev = dst->next; 17341e493d19SDavid S. Miller dst_free(dst); 17351e493d19SDavid S. Miller } else { 17361e493d19SDavid S. Miller pprev = &dst->next; 17371e493d19SDavid S. Miller } 17381e493d19SDavid S. Miller } 17391e493d19SDavid S. Miller spin_unlock_bh(&icmp6_dst_lock); 17401e493d19SDavid S. Miller } 17411e493d19SDavid S. Miller 1742569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops) 17431da177e4SLinus Torvalds { 174486393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 17457019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 17467019b78eSDaniel Lezcano int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; 17477019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 17487019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 17497019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 1750fc66f95cSEric Dumazet int entries; 17511da177e4SLinus Torvalds 1752fc66f95cSEric Dumazet entries = dst_entries_get_fast(ops); 175349a18d86SMichal Kubeček if (time_after(rt_last_gc + rt_min_interval, jiffies) && 1754fc66f95cSEric Dumazet entries <= rt_max_size) 17551da177e4SLinus Torvalds goto out; 17561da177e4SLinus Torvalds 17576891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire++; 175814956643SLi RongQing fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true); 1759fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 1760fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 17617019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; 17621da177e4SLinus Torvalds out: 17637019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; 1764fc66f95cSEric Dumazet return entries > rt_max_size; 17651da177e4SLinus Torvalds } 17661da177e4SLinus Torvalds 1767e715b6d3SFlorian Westphal static int ip6_convert_metrics(struct mx6_config *mxc, 1768e715b6d3SFlorian Westphal const struct fib6_config *cfg) 1769e715b6d3SFlorian Westphal { 1770c3a8d947SDaniel Borkmann bool ecn_ca = false; 1771e715b6d3SFlorian Westphal struct nlattr *nla; 1772e715b6d3SFlorian Westphal int remaining; 1773e715b6d3SFlorian Westphal u32 *mp; 1774e715b6d3SFlorian Westphal 177563159f29SIan Morris if (!cfg->fc_mx) 1776e715b6d3SFlorian Westphal return 0; 1777e715b6d3SFlorian Westphal 1778e715b6d3SFlorian Westphal mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL); 1779e715b6d3SFlorian Westphal if (unlikely(!mp)) 1780e715b6d3SFlorian Westphal return -ENOMEM; 1781e715b6d3SFlorian Westphal 1782e715b6d3SFlorian Westphal nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { 1783e715b6d3SFlorian Westphal int type = nla_type(nla); 1784ea697639SDaniel Borkmann u32 val; 1785ea697639SDaniel Borkmann 17861bb14807SDaniel Borkmann if (!type) 17871bb14807SDaniel Borkmann continue; 1788e715b6d3SFlorian Westphal if (unlikely(type > RTAX_MAX)) 1789e715b6d3SFlorian Westphal goto err; 17901bb14807SDaniel Borkmann 1791ea697639SDaniel Borkmann if (type == RTAX_CC_ALGO) { 1792ea697639SDaniel Borkmann char tmp[TCP_CA_NAME_MAX]; 1793e715b6d3SFlorian Westphal 1794ea697639SDaniel Borkmann nla_strlcpy(tmp, nla, sizeof(tmp)); 1795c3a8d947SDaniel Borkmann val = tcp_ca_get_key_by_name(tmp, &ecn_ca); 1796ea697639SDaniel Borkmann if (val == TCP_CA_UNSPEC) 1797ea697639SDaniel Borkmann goto err; 1798ea697639SDaniel Borkmann } else { 1799ea697639SDaniel Borkmann val = nla_get_u32(nla); 1800ea697639SDaniel Borkmann } 1801626abd59SPaolo Abeni if (type == RTAX_HOPLIMIT && val > 255) 1802626abd59SPaolo Abeni val = 255; 1803b8d3e416SDaniel Borkmann if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK)) 1804b8d3e416SDaniel Borkmann goto err; 1805ea697639SDaniel Borkmann 1806ea697639SDaniel Borkmann mp[type - 1] = val; 1807e715b6d3SFlorian Westphal __set_bit(type - 1, mxc->mx_valid); 1808e715b6d3SFlorian Westphal } 1809e715b6d3SFlorian Westphal 1810c3a8d947SDaniel Borkmann if (ecn_ca) { 1811c3a8d947SDaniel Borkmann __set_bit(RTAX_FEATURES - 1, mxc->mx_valid); 1812c3a8d947SDaniel Borkmann mp[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA; 1813c3a8d947SDaniel Borkmann } 1814e715b6d3SFlorian Westphal 1815c3a8d947SDaniel Borkmann mxc->mx = mp; 1816e715b6d3SFlorian Westphal return 0; 1817e715b6d3SFlorian Westphal err: 1818e715b6d3SFlorian Westphal kfree(mp); 1819e715b6d3SFlorian Westphal return -EINVAL; 1820e715b6d3SFlorian Westphal } 18211da177e4SLinus Torvalds 18228c14586fSDavid Ahern static struct rt6_info *ip6_nh_lookup_table(struct net *net, 18238c14586fSDavid Ahern struct fib6_config *cfg, 18248c14586fSDavid Ahern const struct in6_addr *gw_addr) 18258c14586fSDavid Ahern { 18268c14586fSDavid Ahern struct flowi6 fl6 = { 18278c14586fSDavid Ahern .flowi6_oif = cfg->fc_ifindex, 18288c14586fSDavid Ahern .daddr = *gw_addr, 18298c14586fSDavid Ahern .saddr = cfg->fc_prefsrc, 18308c14586fSDavid Ahern }; 18318c14586fSDavid Ahern struct fib6_table *table; 18328c14586fSDavid Ahern struct rt6_info *rt; 1833d5d32e4bSDavid Ahern int flags = RT6_LOOKUP_F_IFACE | RT6_LOOKUP_F_IGNORE_LINKSTATE; 18348c14586fSDavid Ahern 18358c14586fSDavid Ahern table = fib6_get_table(net, cfg->fc_table); 18368c14586fSDavid Ahern if (!table) 18378c14586fSDavid Ahern return NULL; 18388c14586fSDavid Ahern 18398c14586fSDavid Ahern if (!ipv6_addr_any(&cfg->fc_prefsrc)) 18408c14586fSDavid Ahern flags |= RT6_LOOKUP_F_HAS_SADDR; 18418c14586fSDavid Ahern 18428c14586fSDavid Ahern rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, flags); 18438c14586fSDavid Ahern 18448c14586fSDavid Ahern /* if table lookup failed, fall back to full lookup */ 18458c14586fSDavid Ahern if (rt == net->ipv6.ip6_null_entry) { 18468c14586fSDavid Ahern ip6_rt_put(rt); 18478c14586fSDavid Ahern rt = NULL; 18488c14586fSDavid Ahern } 18498c14586fSDavid Ahern 18508c14586fSDavid Ahern return rt; 18518c14586fSDavid Ahern } 18528c14586fSDavid Ahern 1853333c4301SDavid Ahern static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg, 1854333c4301SDavid Ahern struct netlink_ext_ack *extack) 18551da177e4SLinus Torvalds { 18565578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 18571da177e4SLinus Torvalds struct rt6_info *rt = NULL; 18581da177e4SLinus Torvalds struct net_device *dev = NULL; 18591da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 1860c71099acSThomas Graf struct fib6_table *table; 18611da177e4SLinus Torvalds int addr_type; 18628c5b83f0SRoopa Prabhu int err = -EINVAL; 18631da177e4SLinus Torvalds 1864557c44beSDavid Ahern /* RTF_PCPU is an internal flag; can not be set by userspace */ 1865d5d531cbSDavid Ahern if (cfg->fc_flags & RTF_PCPU) { 1866d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU"); 1867557c44beSDavid Ahern goto out; 1868d5d531cbSDavid Ahern } 1869557c44beSDavid Ahern 1870d5d531cbSDavid Ahern if (cfg->fc_dst_len > 128) { 1871d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid prefix length"); 18728c5b83f0SRoopa Prabhu goto out; 1873d5d531cbSDavid Ahern } 1874d5d531cbSDavid Ahern if (cfg->fc_src_len > 128) { 1875d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address length"); 1876d5d531cbSDavid Ahern goto out; 1877d5d531cbSDavid Ahern } 18781da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 1879d5d531cbSDavid Ahern if (cfg->fc_src_len) { 1880d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 1881d5d531cbSDavid Ahern "Specifying source address requires IPV6_SUBTREES to be enabled"); 18828c5b83f0SRoopa Prabhu goto out; 1883d5d531cbSDavid Ahern } 18841da177e4SLinus Torvalds #endif 188586872cb5SThomas Graf if (cfg->fc_ifindex) { 18861da177e4SLinus Torvalds err = -ENODEV; 18875578689aSDaniel Lezcano dev = dev_get_by_index(net, cfg->fc_ifindex); 18881da177e4SLinus Torvalds if (!dev) 18891da177e4SLinus Torvalds goto out; 18901da177e4SLinus Torvalds idev = in6_dev_get(dev); 18911da177e4SLinus Torvalds if (!idev) 18921da177e4SLinus Torvalds goto out; 18931da177e4SLinus Torvalds } 18941da177e4SLinus Torvalds 189586872cb5SThomas Graf if (cfg->fc_metric == 0) 189686872cb5SThomas Graf cfg->fc_metric = IP6_RT_PRIO_USER; 18971da177e4SLinus Torvalds 1898c71099acSThomas Graf err = -ENOBUFS; 189938308473SDavid S. Miller if (cfg->fc_nlinfo.nlh && 1900d71314b4SMatti Vaittinen !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { 1901d71314b4SMatti Vaittinen table = fib6_get_table(net, cfg->fc_table); 190238308473SDavid S. Miller if (!table) { 1903f3213831SJoe Perches pr_warn("NLM_F_CREATE should be specified when creating new route\n"); 1904d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 1905d71314b4SMatti Vaittinen } 1906d71314b4SMatti Vaittinen } else { 1907d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 1908d71314b4SMatti Vaittinen } 190938308473SDavid S. Miller 191038308473SDavid S. Miller if (!table) 1911c71099acSThomas Graf goto out; 1912c71099acSThomas Graf 1913ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, NULL, 1914ad706862SMartin KaFai Lau (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT); 19151da177e4SLinus Torvalds 191638308473SDavid S. Miller if (!rt) { 19171da177e4SLinus Torvalds err = -ENOMEM; 19181da177e4SLinus Torvalds goto out; 19191da177e4SLinus Torvalds } 19201da177e4SLinus Torvalds 19211716a961SGao feng if (cfg->fc_flags & RTF_EXPIRES) 19221716a961SGao feng rt6_set_expires(rt, jiffies + 19231716a961SGao feng clock_t_to_jiffies(cfg->fc_expires)); 19241716a961SGao feng else 19251716a961SGao feng rt6_clean_expires(rt); 19261da177e4SLinus Torvalds 192786872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 192886872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 192986872cb5SThomas Graf rt->rt6i_protocol = cfg->fc_protocol; 193086872cb5SThomas Graf 193186872cb5SThomas Graf addr_type = ipv6_addr_type(&cfg->fc_dst); 19321da177e4SLinus Torvalds 19331da177e4SLinus Torvalds if (addr_type & IPV6_ADDR_MULTICAST) 1934d8d1f30bSChangli Gao rt->dst.input = ip6_mc_input; 1935ab79ad14SMaciej Żenczykowski else if (cfg->fc_flags & RTF_LOCAL) 1936ab79ad14SMaciej Żenczykowski rt->dst.input = ip6_input; 19371da177e4SLinus Torvalds else 1938d8d1f30bSChangli Gao rt->dst.input = ip6_forward; 19391da177e4SLinus Torvalds 1940d8d1f30bSChangli Gao rt->dst.output = ip6_output; 19411da177e4SLinus Torvalds 194219e42e45SRoopa Prabhu if (cfg->fc_encap) { 194319e42e45SRoopa Prabhu struct lwtunnel_state *lwtstate; 194419e42e45SRoopa Prabhu 194530357d7dSDavid Ahern err = lwtunnel_build_state(cfg->fc_encap_type, 1946127eb7cdSTom Herbert cfg->fc_encap, AF_INET6, cfg, 19479ae28727SDavid Ahern &lwtstate, extack); 194819e42e45SRoopa Prabhu if (err) 194919e42e45SRoopa Prabhu goto out; 195061adedf3SJiri Benc rt->dst.lwtstate = lwtstate_get(lwtstate); 195161adedf3SJiri Benc if (lwtunnel_output_redirect(rt->dst.lwtstate)) { 195261adedf3SJiri Benc rt->dst.lwtstate->orig_output = rt->dst.output; 195361adedf3SJiri Benc rt->dst.output = lwtunnel_output; 195419e42e45SRoopa Prabhu } 195561adedf3SJiri Benc if (lwtunnel_input_redirect(rt->dst.lwtstate)) { 195661adedf3SJiri Benc rt->dst.lwtstate->orig_input = rt->dst.input; 195761adedf3SJiri Benc rt->dst.input = lwtunnel_input; 195825368623STom Herbert } 195925368623STom Herbert } 196019e42e45SRoopa Prabhu 196186872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 196286872cb5SThomas Graf rt->rt6i_dst.plen = cfg->fc_dst_len; 1963afc4eef8SMartin KaFai Lau if (rt->rt6i_dst.plen == 128) 196411d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 19651da177e4SLinus Torvalds 19661da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 196786872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len); 196886872cb5SThomas Graf rt->rt6i_src.plen = cfg->fc_src_len; 19691da177e4SLinus Torvalds #endif 19701da177e4SLinus Torvalds 197186872cb5SThomas Graf rt->rt6i_metric = cfg->fc_metric; 19721da177e4SLinus Torvalds 19731da177e4SLinus Torvalds /* We cannot add true routes via loopback here, 19741da177e4SLinus Torvalds they would result in kernel looping; promote them to reject routes 19751da177e4SLinus Torvalds */ 197686872cb5SThomas Graf if ((cfg->fc_flags & RTF_REJECT) || 197738308473SDavid S. Miller (dev && (dev->flags & IFF_LOOPBACK) && 197838308473SDavid S. Miller !(addr_type & IPV6_ADDR_LOOPBACK) && 197938308473SDavid S. Miller !(cfg->fc_flags & RTF_LOCAL))) { 19801da177e4SLinus Torvalds /* hold loopback dev/idev if we haven't done so. */ 19815578689aSDaniel Lezcano if (dev != net->loopback_dev) { 19821da177e4SLinus Torvalds if (dev) { 19831da177e4SLinus Torvalds dev_put(dev); 19841da177e4SLinus Torvalds in6_dev_put(idev); 19851da177e4SLinus Torvalds } 19865578689aSDaniel Lezcano dev = net->loopback_dev; 19871da177e4SLinus Torvalds dev_hold(dev); 19881da177e4SLinus Torvalds idev = in6_dev_get(dev); 19891da177e4SLinus Torvalds if (!idev) { 19901da177e4SLinus Torvalds err = -ENODEV; 19911da177e4SLinus Torvalds goto out; 19921da177e4SLinus Torvalds } 19931da177e4SLinus Torvalds } 19941da177e4SLinus Torvalds rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP; 1995ef2c7d7bSNicolas Dichtel switch (cfg->fc_type) { 1996ef2c7d7bSNicolas Dichtel case RTN_BLACKHOLE: 1997ef2c7d7bSNicolas Dichtel rt->dst.error = -EINVAL; 1998ede2059dSEric W. Biederman rt->dst.output = dst_discard_out; 19997150aedeSKamala R rt->dst.input = dst_discard; 2000ef2c7d7bSNicolas Dichtel break; 2001ef2c7d7bSNicolas Dichtel case RTN_PROHIBIT: 2002ef2c7d7bSNicolas Dichtel rt->dst.error = -EACCES; 20037150aedeSKamala R rt->dst.output = ip6_pkt_prohibit_out; 20047150aedeSKamala R rt->dst.input = ip6_pkt_prohibit; 2005ef2c7d7bSNicolas Dichtel break; 2006b4949ab2SNicolas Dichtel case RTN_THROW: 20070315e382SNikola Forró case RTN_UNREACHABLE: 2008ef2c7d7bSNicolas Dichtel default: 20097150aedeSKamala R rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN 20100315e382SNikola Forró : (cfg->fc_type == RTN_UNREACHABLE) 20110315e382SNikola Forró ? -EHOSTUNREACH : -ENETUNREACH; 20127150aedeSKamala R rt->dst.output = ip6_pkt_discard_out; 20137150aedeSKamala R rt->dst.input = ip6_pkt_discard; 2014ef2c7d7bSNicolas Dichtel break; 2015ef2c7d7bSNicolas Dichtel } 20161da177e4SLinus Torvalds goto install_route; 20171da177e4SLinus Torvalds } 20181da177e4SLinus Torvalds 201986872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY) { 2020b71d1d42SEric Dumazet const struct in6_addr *gw_addr; 20211da177e4SLinus Torvalds int gwa_type; 20221da177e4SLinus Torvalds 202386872cb5SThomas Graf gw_addr = &cfg->fc_gateway; 2024330567b7SFlorian Westphal gwa_type = ipv6_addr_type(gw_addr); 202548ed7b26SFlorian Westphal 202648ed7b26SFlorian Westphal /* if gw_addr is local we will fail to detect this in case 202748ed7b26SFlorian Westphal * address is still TENTATIVE (DAD in progress). rt6_lookup() 202848ed7b26SFlorian Westphal * will return already-added prefix route via interface that 202948ed7b26SFlorian Westphal * prefix route was assigned to, which might be non-loopback. 203048ed7b26SFlorian Westphal */ 203148ed7b26SFlorian Westphal err = -EINVAL; 2032330567b7SFlorian Westphal if (ipv6_chk_addr_and_flags(net, gw_addr, 2033330567b7SFlorian Westphal gwa_type & IPV6_ADDR_LINKLOCAL ? 2034d5d531cbSDavid Ahern dev : NULL, 0, 0)) { 2035d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid gateway address"); 203648ed7b26SFlorian Westphal goto out; 2037d5d531cbSDavid Ahern } 20384e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = *gw_addr; 20391da177e4SLinus Torvalds 20401da177e4SLinus Torvalds if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) { 20418c14586fSDavid Ahern struct rt6_info *grt = NULL; 20421da177e4SLinus Torvalds 20431da177e4SLinus Torvalds /* IPv6 strictly inhibits using not link-local 20441da177e4SLinus Torvalds addresses as nexthop address. 20451da177e4SLinus Torvalds Otherwise, router will not able to send redirects. 20461da177e4SLinus Torvalds It is very good, but in some (rare!) circumstances 20471da177e4SLinus Torvalds (SIT, PtP, NBMA NOARP links) it is handy to allow 20481da177e4SLinus Torvalds some exceptions. --ANK 204996d5822cSErik Nordmark We allow IPv4-mapped nexthops to support RFC4798-type 205096d5822cSErik Nordmark addressing 20511da177e4SLinus Torvalds */ 205296d5822cSErik Nordmark if (!(gwa_type & (IPV6_ADDR_UNICAST | 2053d5d531cbSDavid Ahern IPV6_ADDR_MAPPED))) { 2054d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2055d5d531cbSDavid Ahern "Invalid gateway address"); 20561da177e4SLinus Torvalds goto out; 2057d5d531cbSDavid Ahern } 20581da177e4SLinus Torvalds 2059a435a07fSVincent Bernat if (cfg->fc_table) { 20608c14586fSDavid Ahern grt = ip6_nh_lookup_table(net, cfg, gw_addr); 20618c14586fSDavid Ahern 2062a435a07fSVincent Bernat if (grt) { 2063a435a07fSVincent Bernat if (grt->rt6i_flags & RTF_GATEWAY || 2064a435a07fSVincent Bernat (dev && dev != grt->dst.dev)) { 2065a435a07fSVincent Bernat ip6_rt_put(grt); 2066a435a07fSVincent Bernat grt = NULL; 2067a435a07fSVincent Bernat } 2068a435a07fSVincent Bernat } 2069a435a07fSVincent Bernat } 2070a435a07fSVincent Bernat 20718c14586fSDavid Ahern if (!grt) 20728c14586fSDavid Ahern grt = rt6_lookup(net, gw_addr, NULL, 20738c14586fSDavid Ahern cfg->fc_ifindex, 1); 20741da177e4SLinus Torvalds 20751da177e4SLinus Torvalds err = -EHOSTUNREACH; 207638308473SDavid S. Miller if (!grt) 20771da177e4SLinus Torvalds goto out; 20781da177e4SLinus Torvalds if (dev) { 2079d1918542SDavid S. Miller if (dev != grt->dst.dev) { 208094e187c0SAmerigo Wang ip6_rt_put(grt); 20811da177e4SLinus Torvalds goto out; 20821da177e4SLinus Torvalds } 20831da177e4SLinus Torvalds } else { 2084d1918542SDavid S. Miller dev = grt->dst.dev; 20851da177e4SLinus Torvalds idev = grt->rt6i_idev; 20861da177e4SLinus Torvalds dev_hold(dev); 20871da177e4SLinus Torvalds in6_dev_hold(grt->rt6i_idev); 20881da177e4SLinus Torvalds } 20891da177e4SLinus Torvalds if (!(grt->rt6i_flags & RTF_GATEWAY)) 20901da177e4SLinus Torvalds err = 0; 209194e187c0SAmerigo Wang ip6_rt_put(grt); 20921da177e4SLinus Torvalds 20931da177e4SLinus Torvalds if (err) 20941da177e4SLinus Torvalds goto out; 20951da177e4SLinus Torvalds } 20961da177e4SLinus Torvalds err = -EINVAL; 2097d5d531cbSDavid Ahern if (!dev) { 2098d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Egress device not specified"); 20991da177e4SLinus Torvalds goto out; 2100d5d531cbSDavid Ahern } else if (dev->flags & IFF_LOOPBACK) { 2101d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2102d5d531cbSDavid Ahern "Egress device can not be loopback device for this route"); 2103d5d531cbSDavid Ahern goto out; 2104d5d531cbSDavid Ahern } 21051da177e4SLinus Torvalds } 21061da177e4SLinus Torvalds 21071da177e4SLinus Torvalds err = -ENODEV; 210838308473SDavid S. Miller if (!dev) 21091da177e4SLinus Torvalds goto out; 21101da177e4SLinus Torvalds 2111c3968a85SDaniel Walter if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 2112c3968a85SDaniel Walter if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 2113d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address"); 2114c3968a85SDaniel Walter err = -EINVAL; 2115c3968a85SDaniel Walter goto out; 2116c3968a85SDaniel Walter } 21174e3fd7a0SAlexey Dobriyan rt->rt6i_prefsrc.addr = cfg->fc_prefsrc; 2118c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 128; 2119c3968a85SDaniel Walter } else 2120c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 2121c3968a85SDaniel Walter 212286872cb5SThomas Graf rt->rt6i_flags = cfg->fc_flags; 21231da177e4SLinus Torvalds 21241da177e4SLinus Torvalds install_route: 2125d8d1f30bSChangli Gao rt->dst.dev = dev; 21261da177e4SLinus Torvalds rt->rt6i_idev = idev; 2127c71099acSThomas Graf rt->rt6i_table = table; 212863152fc0SDaniel Lezcano 2129c346dca1SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = dev_net(dev); 213063152fc0SDaniel Lezcano 21318c5b83f0SRoopa Prabhu return rt; 21321da177e4SLinus Torvalds out: 21331da177e4SLinus Torvalds if (dev) 21341da177e4SLinus Torvalds dev_put(dev); 21351da177e4SLinus Torvalds if (idev) 21361da177e4SLinus Torvalds in6_dev_put(idev); 2137*587fea74SWei Wang if (rt) 2138*587fea74SWei Wang dst_release_immediate(&rt->dst); 21396b9ea5a6SRoopa Prabhu 21408c5b83f0SRoopa Prabhu return ERR_PTR(err); 21416b9ea5a6SRoopa Prabhu } 21426b9ea5a6SRoopa Prabhu 2143333c4301SDavid Ahern int ip6_route_add(struct fib6_config *cfg, 2144333c4301SDavid Ahern struct netlink_ext_ack *extack) 21456b9ea5a6SRoopa Prabhu { 21466b9ea5a6SRoopa Prabhu struct mx6_config mxc = { .mx = NULL, }; 21478c5b83f0SRoopa Prabhu struct rt6_info *rt; 21486b9ea5a6SRoopa Prabhu int err; 21496b9ea5a6SRoopa Prabhu 2150333c4301SDavid Ahern rt = ip6_route_info_create(cfg, extack); 21518c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 21528c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 21538c5b83f0SRoopa Prabhu rt = NULL; 21546b9ea5a6SRoopa Prabhu goto out; 21558c5b83f0SRoopa Prabhu } 21566b9ea5a6SRoopa Prabhu 21576b9ea5a6SRoopa Prabhu err = ip6_convert_metrics(&mxc, cfg); 21586b9ea5a6SRoopa Prabhu if (err) 21596b9ea5a6SRoopa Prabhu goto out; 21606b9ea5a6SRoopa Prabhu 2161333c4301SDavid Ahern err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, &mxc, extack); 21626b9ea5a6SRoopa Prabhu 21636b9ea5a6SRoopa Prabhu kfree(mxc.mx); 21646b9ea5a6SRoopa Prabhu 21656b9ea5a6SRoopa Prabhu return err; 21666b9ea5a6SRoopa Prabhu out: 2167*587fea74SWei Wang if (rt) 2168*587fea74SWei Wang dst_release_immediate(&rt->dst); 21696b9ea5a6SRoopa Prabhu 21701da177e4SLinus Torvalds return err; 21711da177e4SLinus Torvalds } 21721da177e4SLinus Torvalds 217386872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info) 21741da177e4SLinus Torvalds { 21751da177e4SLinus Torvalds int err; 2176c71099acSThomas Graf struct fib6_table *table; 2177d1918542SDavid S. Miller struct net *net = dev_net(rt->dst.dev); 21781da177e4SLinus Torvalds 21798e3d5be7SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry || 21808e3d5be7SMartin KaFai Lau rt->dst.flags & DST_NOCACHE) { 21816825a26cSGao feng err = -ENOENT; 21826825a26cSGao feng goto out; 21836825a26cSGao feng } 21846c813a72SPatrick McHardy 2185c71099acSThomas Graf table = rt->rt6i_table; 2186c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 218786872cb5SThomas Graf err = fib6_del(rt, info); 2188c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 21891da177e4SLinus Torvalds 21906825a26cSGao feng out: 219194e187c0SAmerigo Wang ip6_rt_put(rt); 21921da177e4SLinus Torvalds return err; 21931da177e4SLinus Torvalds } 21941da177e4SLinus Torvalds 2195e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt) 2196e0a1ad73SThomas Graf { 21974d1169c1SDenis V. Lunev struct nl_info info = { 2198d1918542SDavid S. Miller .nl_net = dev_net(rt->dst.dev), 21994d1169c1SDenis V. Lunev }; 2200528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 2201e0a1ad73SThomas Graf } 2202e0a1ad73SThomas Graf 22030ae81335SDavid Ahern static int __ip6_del_rt_siblings(struct rt6_info *rt, struct fib6_config *cfg) 22040ae81335SDavid Ahern { 22050ae81335SDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 2206e3330039SWANG Cong struct net *net = info->nl_net; 220716a16cd3SDavid Ahern struct sk_buff *skb = NULL; 22080ae81335SDavid Ahern struct fib6_table *table; 2209e3330039SWANG Cong int err = -ENOENT; 22100ae81335SDavid Ahern 2211e3330039SWANG Cong if (rt == net->ipv6.ip6_null_entry) 2212e3330039SWANG Cong goto out_put; 22130ae81335SDavid Ahern table = rt->rt6i_table; 22140ae81335SDavid Ahern write_lock_bh(&table->tb6_lock); 22150ae81335SDavid Ahern 22160ae81335SDavid Ahern if (rt->rt6i_nsiblings && cfg->fc_delete_all_nh) { 22170ae81335SDavid Ahern struct rt6_info *sibling, *next_sibling; 22180ae81335SDavid Ahern 221916a16cd3SDavid Ahern /* prefer to send a single notification with all hops */ 222016a16cd3SDavid Ahern skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 222116a16cd3SDavid Ahern if (skb) { 222216a16cd3SDavid Ahern u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 222316a16cd3SDavid Ahern 2224e3330039SWANG Cong if (rt6_fill_node(net, skb, rt, 222516a16cd3SDavid Ahern NULL, NULL, 0, RTM_DELROUTE, 222616a16cd3SDavid Ahern info->portid, seq, 0) < 0) { 222716a16cd3SDavid Ahern kfree_skb(skb); 222816a16cd3SDavid Ahern skb = NULL; 222916a16cd3SDavid Ahern } else 223016a16cd3SDavid Ahern info->skip_notify = 1; 223116a16cd3SDavid Ahern } 223216a16cd3SDavid Ahern 22330ae81335SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 22340ae81335SDavid Ahern &rt->rt6i_siblings, 22350ae81335SDavid Ahern rt6i_siblings) { 22360ae81335SDavid Ahern err = fib6_del(sibling, info); 22370ae81335SDavid Ahern if (err) 2238e3330039SWANG Cong goto out_unlock; 22390ae81335SDavid Ahern } 22400ae81335SDavid Ahern } 22410ae81335SDavid Ahern 22420ae81335SDavid Ahern err = fib6_del(rt, info); 2243e3330039SWANG Cong out_unlock: 22440ae81335SDavid Ahern write_unlock_bh(&table->tb6_lock); 2245e3330039SWANG Cong out_put: 22460ae81335SDavid Ahern ip6_rt_put(rt); 224716a16cd3SDavid Ahern 224816a16cd3SDavid Ahern if (skb) { 2249e3330039SWANG Cong rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 225016a16cd3SDavid Ahern info->nlh, gfp_any()); 225116a16cd3SDavid Ahern } 22520ae81335SDavid Ahern return err; 22530ae81335SDavid Ahern } 22540ae81335SDavid Ahern 2255333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg, 2256333c4301SDavid Ahern struct netlink_ext_ack *extack) 22571da177e4SLinus Torvalds { 2258c71099acSThomas Graf struct fib6_table *table; 22591da177e4SLinus Torvalds struct fib6_node *fn; 22601da177e4SLinus Torvalds struct rt6_info *rt; 22611da177e4SLinus Torvalds int err = -ESRCH; 22621da177e4SLinus Torvalds 22635578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 2264d5d531cbSDavid Ahern if (!table) { 2265d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "FIB table does not exist"); 2266c71099acSThomas Graf return err; 2267d5d531cbSDavid Ahern } 22681da177e4SLinus Torvalds 2269c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 2270c71099acSThomas Graf 2271c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 227286872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 227386872cb5SThomas Graf &cfg->fc_src, cfg->fc_src_len); 22741da177e4SLinus Torvalds 22751da177e4SLinus Torvalds if (fn) { 2276d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 22771f56a01fSMartin KaFai Lau if ((rt->rt6i_flags & RTF_CACHE) && 22781f56a01fSMartin KaFai Lau !(cfg->fc_flags & RTF_CACHE)) 22791f56a01fSMartin KaFai Lau continue; 228086872cb5SThomas Graf if (cfg->fc_ifindex && 2281d1918542SDavid S. Miller (!rt->dst.dev || 2282d1918542SDavid S. Miller rt->dst.dev->ifindex != cfg->fc_ifindex)) 22831da177e4SLinus Torvalds continue; 228486872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 228586872cb5SThomas Graf !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 22861da177e4SLinus Torvalds continue; 228786872cb5SThomas Graf if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric) 22881da177e4SLinus Torvalds continue; 2289c2ed1880SMantas M if (cfg->fc_protocol && cfg->fc_protocol != rt->rt6i_protocol) 2290c2ed1880SMantas M continue; 2291d8d1f30bSChangli Gao dst_hold(&rt->dst); 2292c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 22931da177e4SLinus Torvalds 22940ae81335SDavid Ahern /* if gateway was specified only delete the one hop */ 22950ae81335SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY) 229686872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 22970ae81335SDavid Ahern 22980ae81335SDavid Ahern return __ip6_del_rt_siblings(rt, cfg); 22991da177e4SLinus Torvalds } 23001da177e4SLinus Torvalds } 2301c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 23021da177e4SLinus Torvalds 23031da177e4SLinus Torvalds return err; 23041da177e4SLinus Torvalds } 23051da177e4SLinus Torvalds 23066700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 2307a6279458SYOSHIFUJI Hideaki { 2308a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 2309e8599ff4SDavid S. Miller struct rt6_info *rt, *nrt = NULL; 2310e8599ff4SDavid S. Miller struct ndisc_options ndopts; 2311e8599ff4SDavid S. Miller struct inet6_dev *in6_dev; 2312e8599ff4SDavid S. Miller struct neighbour *neigh; 231371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 struct rd_msg *msg; 23146e157b6aSDavid S. Miller int optlen, on_link; 23156e157b6aSDavid S. Miller u8 *lladdr; 2316e8599ff4SDavid S. Miller 231729a3cad5SSimon Horman optlen = skb_tail_pointer(skb) - skb_transport_header(skb); 231871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 optlen -= sizeof(*msg); 2319e8599ff4SDavid S. Miller 2320e8599ff4SDavid S. Miller if (optlen < 0) { 23216e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); 2322e8599ff4SDavid S. Miller return; 2323e8599ff4SDavid S. Miller } 2324e8599ff4SDavid S. Miller 232571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 msg = (struct rd_msg *)icmp6_hdr(skb); 2326e8599ff4SDavid S. Miller 232771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_is_multicast(&msg->dest)) { 23286e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); 2329e8599ff4SDavid S. Miller return; 2330e8599ff4SDavid S. Miller } 2331e8599ff4SDavid S. Miller 23326e157b6aSDavid S. Miller on_link = 0; 233371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_equal(&msg->dest, &msg->target)) { 2334e8599ff4SDavid S. Miller on_link = 1; 233571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 } else if (ipv6_addr_type(&msg->target) != 2336e8599ff4SDavid S. Miller (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { 23376e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); 2338e8599ff4SDavid S. Miller return; 2339e8599ff4SDavid S. Miller } 2340e8599ff4SDavid S. Miller 2341e8599ff4SDavid S. Miller in6_dev = __in6_dev_get(skb->dev); 2342e8599ff4SDavid S. Miller if (!in6_dev) 2343e8599ff4SDavid S. Miller return; 2344e8599ff4SDavid S. Miller if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) 2345e8599ff4SDavid S. Miller return; 2346e8599ff4SDavid S. Miller 2347e8599ff4SDavid S. Miller /* RFC2461 8.1: 2348e8599ff4SDavid S. Miller * The IP source address of the Redirect MUST be the same as the current 2349e8599ff4SDavid S. Miller * first-hop router for the specified ICMP Destination Address. 2350e8599ff4SDavid S. Miller */ 2351e8599ff4SDavid S. Miller 2352f997c55cSAlexander Aring if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { 2353e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); 2354e8599ff4SDavid S. Miller return; 2355e8599ff4SDavid S. Miller } 23566e157b6aSDavid S. Miller 23576e157b6aSDavid S. Miller lladdr = NULL; 2358e8599ff4SDavid S. Miller if (ndopts.nd_opts_tgt_lladdr) { 2359e8599ff4SDavid S. Miller lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, 2360e8599ff4SDavid S. Miller skb->dev); 2361e8599ff4SDavid S. Miller if (!lladdr) { 2362e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); 2363e8599ff4SDavid S. Miller return; 2364e8599ff4SDavid S. Miller } 2365e8599ff4SDavid S. Miller } 2366e8599ff4SDavid S. Miller 23676e157b6aSDavid S. Miller rt = (struct rt6_info *) dst; 2368ec13ad1dSMatthias Schiffer if (rt->rt6i_flags & RTF_REJECT) { 23696e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); 23706e157b6aSDavid S. Miller return; 23716e157b6aSDavid S. Miller } 23726e157b6aSDavid S. Miller 23736e157b6aSDavid S. Miller /* Redirect received -> path was valid. 23746e157b6aSDavid S. Miller * Look, redirects are sent only in response to data packets, 23756e157b6aSDavid S. Miller * so that this nexthop apparently is reachable. --ANK 23766e157b6aSDavid S. Miller */ 23770dec879fSJulian Anastasov dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); 23786e157b6aSDavid S. Miller 237971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); 2380e8599ff4SDavid S. Miller if (!neigh) 2381e8599ff4SDavid S. Miller return; 2382e8599ff4SDavid S. Miller 23831da177e4SLinus Torvalds /* 23841da177e4SLinus Torvalds * We have finally decided to accept it. 23851da177e4SLinus Torvalds */ 23861da177e4SLinus Torvalds 2387f997c55cSAlexander Aring ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, 23881da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 23891da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 23901da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 2391f997c55cSAlexander Aring NEIGH_UPDATE_F_ISROUTER)), 2392f997c55cSAlexander Aring NDISC_REDIRECT, &ndopts); 23931da177e4SLinus Torvalds 239483a09abdSMartin KaFai Lau nrt = ip6_rt_cache_alloc(rt, &msg->dest, NULL); 239538308473SDavid S. Miller if (!nrt) 23961da177e4SLinus Torvalds goto out; 23971da177e4SLinus Torvalds 23981da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 23991da177e4SLinus Torvalds if (on_link) 24001da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 24011da177e4SLinus Torvalds 24024e3fd7a0SAlexey Dobriyan nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 24031da177e4SLinus Torvalds 240440e22e8fSThomas Graf if (ip6_ins_rt(nrt)) 24051cfb71eeSWei Wang goto out_release; 24061da177e4SLinus Torvalds 2407d8d1f30bSChangli Gao netevent.old = &rt->dst; 2408d8d1f30bSChangli Gao netevent.new = &nrt->dst; 240971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 netevent.daddr = &msg->dest; 241060592833SYOSHIFUJI Hideaki / 吉藤英明 netevent.neigh = neigh; 24118d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 24128d71740cSTom Tucker 24131da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) { 24146e157b6aSDavid S. Miller rt = (struct rt6_info *) dst_clone(&rt->dst); 2415e0a1ad73SThomas Graf ip6_del_rt(rt); 24161da177e4SLinus Torvalds } 24171da177e4SLinus Torvalds 24181cfb71eeSWei Wang out_release: 24191cfb71eeSWei Wang /* Release the reference taken in 24201cfb71eeSWei Wang * ip6_rt_cache_alloc() 24211cfb71eeSWei Wang */ 24221cfb71eeSWei Wang dst_release(&nrt->dst); 24231cfb71eeSWei Wang 24241da177e4SLinus Torvalds out: 2425e8599ff4SDavid S. Miller neigh_release(neigh); 24266e157b6aSDavid S. Miller } 24276e157b6aSDavid S. Miller 24281da177e4SLinus Torvalds /* 24291da177e4SLinus Torvalds * Misc support functions 24301da177e4SLinus Torvalds */ 24311da177e4SLinus Torvalds 24324b32b5adSMartin KaFai Lau static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from) 24334b32b5adSMartin KaFai Lau { 24344b32b5adSMartin KaFai Lau BUG_ON(from->dst.from); 24354b32b5adSMartin KaFai Lau 24364b32b5adSMartin KaFai Lau rt->rt6i_flags &= ~RTF_EXPIRES; 24374b32b5adSMartin KaFai Lau dst_hold(&from->dst); 24384b32b5adSMartin KaFai Lau rt->dst.from = &from->dst; 24394b32b5adSMartin KaFai Lau dst_init_metrics(&rt->dst, dst_metrics_ptr(&from->dst), true); 24404b32b5adSMartin KaFai Lau } 24414b32b5adSMartin KaFai Lau 244283a09abdSMartin KaFai Lau static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort) 24431da177e4SLinus Torvalds { 2444d8d1f30bSChangli Gao rt->dst.input = ort->dst.input; 2445d8d1f30bSChangli Gao rt->dst.output = ort->dst.output; 244683a09abdSMartin KaFai Lau rt->rt6i_dst = ort->rt6i_dst; 2447d8d1f30bSChangli Gao rt->dst.error = ort->dst.error; 24481da177e4SLinus Torvalds rt->rt6i_idev = ort->rt6i_idev; 24491da177e4SLinus Torvalds if (rt->rt6i_idev) 24501da177e4SLinus Torvalds in6_dev_hold(rt->rt6i_idev); 2451d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 24524e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 24531716a961SGao feng rt->rt6i_flags = ort->rt6i_flags; 24541716a961SGao feng rt6_set_from(rt, ort); 245583a09abdSMartin KaFai Lau rt->rt6i_metric = ort->rt6i_metric; 24561da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 245783a09abdSMartin KaFai Lau rt->rt6i_src = ort->rt6i_src; 24581da177e4SLinus Torvalds #endif 245983a09abdSMartin KaFai Lau rt->rt6i_prefsrc = ort->rt6i_prefsrc; 2460c71099acSThomas Graf rt->rt6i_table = ort->rt6i_table; 246161adedf3SJiri Benc rt->dst.lwtstate = lwtstate_get(ort->dst.lwtstate); 24621da177e4SLinus Torvalds } 24631da177e4SLinus Torvalds 246470ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 2465efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 2466b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 2467830218c1SDavid Ahern const struct in6_addr *gwaddr, 2468830218c1SDavid Ahern struct net_device *dev) 246970ceb4f5SYOSHIFUJI Hideaki { 2470830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; 2471830218c1SDavid Ahern int ifindex = dev->ifindex; 247270ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 247370ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt = NULL; 2474c71099acSThomas Graf struct fib6_table *table; 247570ceb4f5SYOSHIFUJI Hideaki 2476830218c1SDavid Ahern table = fib6_get_table(net, tb_id); 247738308473SDavid S. Miller if (!table) 2478c71099acSThomas Graf return NULL; 2479c71099acSThomas Graf 24805744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 2481c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0); 248270ceb4f5SYOSHIFUJI Hideaki if (!fn) 248370ceb4f5SYOSHIFUJI Hideaki goto out; 248470ceb4f5SYOSHIFUJI Hideaki 2485d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 2486d1918542SDavid S. Miller if (rt->dst.dev->ifindex != ifindex) 248770ceb4f5SYOSHIFUJI Hideaki continue; 248870ceb4f5SYOSHIFUJI Hideaki if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) 248970ceb4f5SYOSHIFUJI Hideaki continue; 249070ceb4f5SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr)) 249170ceb4f5SYOSHIFUJI Hideaki continue; 2492d8d1f30bSChangli Gao dst_hold(&rt->dst); 249370ceb4f5SYOSHIFUJI Hideaki break; 249470ceb4f5SYOSHIFUJI Hideaki } 249570ceb4f5SYOSHIFUJI Hideaki out: 24965744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 249770ceb4f5SYOSHIFUJI Hideaki return rt; 249870ceb4f5SYOSHIFUJI Hideaki } 249970ceb4f5SYOSHIFUJI Hideaki 2500efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 2501b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 2502830218c1SDavid Ahern const struct in6_addr *gwaddr, 2503830218c1SDavid Ahern struct net_device *dev, 250495c96174SEric Dumazet unsigned int pref) 250570ceb4f5SYOSHIFUJI Hideaki { 250686872cb5SThomas Graf struct fib6_config cfg = { 2507238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 2508830218c1SDavid Ahern .fc_ifindex = dev->ifindex, 250986872cb5SThomas Graf .fc_dst_len = prefixlen, 251086872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 251186872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 251215e47304SEric W. Biederman .fc_nlinfo.portid = 0, 2513efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 2514efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 251586872cb5SThomas Graf }; 251670ceb4f5SYOSHIFUJI Hideaki 2517830218c1SDavid Ahern cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO, 25184e3fd7a0SAlexey Dobriyan cfg.fc_dst = *prefix; 25194e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 252086872cb5SThomas Graf 2521e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 2522e317da96SYOSHIFUJI Hideaki if (!prefixlen) 252386872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 252470ceb4f5SYOSHIFUJI Hideaki 2525333c4301SDavid Ahern ip6_route_add(&cfg, NULL); 252670ceb4f5SYOSHIFUJI Hideaki 2527830218c1SDavid Ahern return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); 252870ceb4f5SYOSHIFUJI Hideaki } 252970ceb4f5SYOSHIFUJI Hideaki #endif 253070ceb4f5SYOSHIFUJI Hideaki 2531b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev) 25321da177e4SLinus Torvalds { 2533830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; 25341da177e4SLinus Torvalds struct rt6_info *rt; 2535c71099acSThomas Graf struct fib6_table *table; 25361da177e4SLinus Torvalds 2537830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), tb_id); 253838308473SDavid S. Miller if (!table) 2539c71099acSThomas Graf return NULL; 25401da177e4SLinus Torvalds 25415744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 2542d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 2543d1918542SDavid S. Miller if (dev == rt->dst.dev && 2544045927ffSYOSHIFUJI Hideaki ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 25451da177e4SLinus Torvalds ipv6_addr_equal(&rt->rt6i_gateway, addr)) 25461da177e4SLinus Torvalds break; 25471da177e4SLinus Torvalds } 25481da177e4SLinus Torvalds if (rt) 2549d8d1f30bSChangli Gao dst_hold(&rt->dst); 25505744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 25511da177e4SLinus Torvalds return rt; 25521da177e4SLinus Torvalds } 25531da177e4SLinus Torvalds 2554b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr, 2555ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 2556ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 25571da177e4SLinus Torvalds { 255886872cb5SThomas Graf struct fib6_config cfg = { 2559ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, 2560238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 256186872cb5SThomas Graf .fc_ifindex = dev->ifindex, 256286872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 256386872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 256415e47304SEric W. Biederman .fc_nlinfo.portid = 0, 25655578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 2566c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 256786872cb5SThomas Graf }; 25681da177e4SLinus Torvalds 25694e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 25701da177e4SLinus Torvalds 2571333c4301SDavid Ahern if (!ip6_route_add(&cfg, NULL)) { 2572830218c1SDavid Ahern struct fib6_table *table; 2573830218c1SDavid Ahern 2574830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), cfg.fc_table); 2575830218c1SDavid Ahern if (table) 2576830218c1SDavid Ahern table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; 2577830218c1SDavid Ahern } 25781da177e4SLinus Torvalds 25791da177e4SLinus Torvalds return rt6_get_dflt_router(gwaddr, dev); 25801da177e4SLinus Torvalds } 25811da177e4SLinus Torvalds 2582830218c1SDavid Ahern static void __rt6_purge_dflt_routers(struct fib6_table *table) 25831da177e4SLinus Torvalds { 25841da177e4SLinus Torvalds struct rt6_info *rt; 25851da177e4SLinus Torvalds 25861da177e4SLinus Torvalds restart: 2587c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 2588d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 25893e8b0ac3SLorenzo Colitti if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) && 25903e8b0ac3SLorenzo Colitti (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) { 2591d8d1f30bSChangli Gao dst_hold(&rt->dst); 2592c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 2593e0a1ad73SThomas Graf ip6_del_rt(rt); 25941da177e4SLinus Torvalds goto restart; 25951da177e4SLinus Torvalds } 25961da177e4SLinus Torvalds } 2597c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 2598830218c1SDavid Ahern 2599830218c1SDavid Ahern table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; 2600830218c1SDavid Ahern } 2601830218c1SDavid Ahern 2602830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net) 2603830218c1SDavid Ahern { 2604830218c1SDavid Ahern struct fib6_table *table; 2605830218c1SDavid Ahern struct hlist_head *head; 2606830218c1SDavid Ahern unsigned int h; 2607830218c1SDavid Ahern 2608830218c1SDavid Ahern rcu_read_lock(); 2609830218c1SDavid Ahern 2610830218c1SDavid Ahern for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { 2611830218c1SDavid Ahern head = &net->ipv6.fib_table_hash[h]; 2612830218c1SDavid Ahern hlist_for_each_entry_rcu(table, head, tb6_hlist) { 2613830218c1SDavid Ahern if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) 2614830218c1SDavid Ahern __rt6_purge_dflt_routers(table); 2615830218c1SDavid Ahern } 2616830218c1SDavid Ahern } 2617830218c1SDavid Ahern 2618830218c1SDavid Ahern rcu_read_unlock(); 26191da177e4SLinus Torvalds } 26201da177e4SLinus Torvalds 26215578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 26225578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 262386872cb5SThomas Graf struct fib6_config *cfg) 262486872cb5SThomas Graf { 262586872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 262686872cb5SThomas Graf 2627ca254490SDavid Ahern cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? 2628ca254490SDavid Ahern : RT6_TABLE_MAIN; 262986872cb5SThomas Graf cfg->fc_ifindex = rtmsg->rtmsg_ifindex; 263086872cb5SThomas Graf cfg->fc_metric = rtmsg->rtmsg_metric; 263186872cb5SThomas Graf cfg->fc_expires = rtmsg->rtmsg_info; 263286872cb5SThomas Graf cfg->fc_dst_len = rtmsg->rtmsg_dst_len; 263386872cb5SThomas Graf cfg->fc_src_len = rtmsg->rtmsg_src_len; 263486872cb5SThomas Graf cfg->fc_flags = rtmsg->rtmsg_flags; 263586872cb5SThomas Graf 26365578689aSDaniel Lezcano cfg->fc_nlinfo.nl_net = net; 2637f1243c2dSBenjamin Thery 26384e3fd7a0SAlexey Dobriyan cfg->fc_dst = rtmsg->rtmsg_dst; 26394e3fd7a0SAlexey Dobriyan cfg->fc_src = rtmsg->rtmsg_src; 26404e3fd7a0SAlexey Dobriyan cfg->fc_gateway = rtmsg->rtmsg_gateway; 264186872cb5SThomas Graf } 264286872cb5SThomas Graf 26435578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) 26441da177e4SLinus Torvalds { 264586872cb5SThomas Graf struct fib6_config cfg; 26461da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 26471da177e4SLinus Torvalds int err; 26481da177e4SLinus Torvalds 26491da177e4SLinus Torvalds switch (cmd) { 26501da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 26511da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 2652af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 26531da177e4SLinus Torvalds return -EPERM; 26541da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 26551da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 26561da177e4SLinus Torvalds if (err) 26571da177e4SLinus Torvalds return -EFAULT; 26581da177e4SLinus Torvalds 26595578689aSDaniel Lezcano rtmsg_to_fib6_config(net, &rtmsg, &cfg); 266086872cb5SThomas Graf 26611da177e4SLinus Torvalds rtnl_lock(); 26621da177e4SLinus Torvalds switch (cmd) { 26631da177e4SLinus Torvalds case SIOCADDRT: 2664333c4301SDavid Ahern err = ip6_route_add(&cfg, NULL); 26651da177e4SLinus Torvalds break; 26661da177e4SLinus Torvalds case SIOCDELRT: 2667333c4301SDavid Ahern err = ip6_route_del(&cfg, NULL); 26681da177e4SLinus Torvalds break; 26691da177e4SLinus Torvalds default: 26701da177e4SLinus Torvalds err = -EINVAL; 26711da177e4SLinus Torvalds } 26721da177e4SLinus Torvalds rtnl_unlock(); 26731da177e4SLinus Torvalds 26741da177e4SLinus Torvalds return err; 26753ff50b79SStephen Hemminger } 26761da177e4SLinus Torvalds 26771da177e4SLinus Torvalds return -EINVAL; 26781da177e4SLinus Torvalds } 26791da177e4SLinus Torvalds 26801da177e4SLinus Torvalds /* 26811da177e4SLinus Torvalds * Drop the packet on the floor 26821da177e4SLinus Torvalds */ 26831da177e4SLinus Torvalds 2684d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 26851da177e4SLinus Torvalds { 2686612f09e8SYOSHIFUJI Hideaki int type; 2687adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 2688612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 2689612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 26900660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 269145bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 26923bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 26933bd653c8SDenis V. Lunev IPSTATS_MIB_INADDRERRORS); 2694612f09e8SYOSHIFUJI Hideaki break; 2695612f09e8SYOSHIFUJI Hideaki } 2696612f09e8SYOSHIFUJI Hideaki /* FALLTHROUGH */ 2697612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 26983bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 26993bd653c8SDenis V. Lunev ipstats_mib_noroutes); 2700612f09e8SYOSHIFUJI Hideaki break; 2701612f09e8SYOSHIFUJI Hideaki } 27023ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 27031da177e4SLinus Torvalds kfree_skb(skb); 27041da177e4SLinus Torvalds return 0; 27051da177e4SLinus Torvalds } 27061da177e4SLinus Torvalds 27079ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 27089ce8ade0SThomas Graf { 2709612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 27109ce8ade0SThomas Graf } 27119ce8ade0SThomas Graf 2712ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb) 27131da177e4SLinus Torvalds { 2714adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 2715612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 27161da177e4SLinus Torvalds } 27171da177e4SLinus Torvalds 27189ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 27199ce8ade0SThomas Graf { 2720612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 27219ce8ade0SThomas Graf } 27229ce8ade0SThomas Graf 2723ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb) 27249ce8ade0SThomas Graf { 2725adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 2726612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 27279ce8ade0SThomas Graf } 27289ce8ade0SThomas Graf 27291da177e4SLinus Torvalds /* 27301da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 27311da177e4SLinus Torvalds */ 27321da177e4SLinus Torvalds 27331da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, 27341da177e4SLinus Torvalds const struct in6_addr *addr, 27358f031519SDavid S. Miller bool anycast) 27361da177e4SLinus Torvalds { 2737ca254490SDavid Ahern u32 tb_id; 2738c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 27395f02ce24SDavid Ahern struct net_device *dev = net->loopback_dev; 27405f02ce24SDavid Ahern struct rt6_info *rt; 27415f02ce24SDavid Ahern 27425f02ce24SDavid Ahern /* use L3 Master device as loopback for host routes if device 27435f02ce24SDavid Ahern * is enslaved and address is not link local or multicast 27445f02ce24SDavid Ahern */ 27455f02ce24SDavid Ahern if (!rt6_need_strict(addr)) 27465f02ce24SDavid Ahern dev = l3mdev_master_dev_rcu(idev->dev) ? : dev; 27475f02ce24SDavid Ahern 27485f02ce24SDavid Ahern rt = ip6_dst_alloc(net, dev, DST_NOCOUNT); 2749a3300ef4SHannes Frederic Sowa if (!rt) 27501da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 27511da177e4SLinus Torvalds 27521da177e4SLinus Torvalds in6_dev_hold(idev); 27531da177e4SLinus Torvalds 275411d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 2755d8d1f30bSChangli Gao rt->dst.input = ip6_input; 2756d8d1f30bSChangli Gao rt->dst.output = ip6_output; 27571da177e4SLinus Torvalds rt->rt6i_idev = idev; 27581da177e4SLinus Torvalds 275994b5e0f9SDavid Ahern rt->rt6i_protocol = RTPROT_KERNEL; 27601da177e4SLinus Torvalds rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP; 276158c4fb86SYOSHIFUJI Hideaki if (anycast) 276258c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 276358c4fb86SYOSHIFUJI Hideaki else 27641da177e4SLinus Torvalds rt->rt6i_flags |= RTF_LOCAL; 27651da177e4SLinus Torvalds 2766550bab42SJulian Anastasov rt->rt6i_gateway = *addr; 27674e3fd7a0SAlexey Dobriyan rt->rt6i_dst.addr = *addr; 27681da177e4SLinus Torvalds rt->rt6i_dst.plen = 128; 2769ca254490SDavid Ahern tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL; 2770ca254490SDavid Ahern rt->rt6i_table = fib6_get_table(net, tb_id); 27718e3d5be7SMartin KaFai Lau rt->dst.flags |= DST_NOCACHE; 27721da177e4SLinus Torvalds 27731da177e4SLinus Torvalds return rt; 27741da177e4SLinus Torvalds } 27751da177e4SLinus Torvalds 2776c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 2777c3968a85SDaniel Walter struct arg_dev_net_ip { 2778c3968a85SDaniel Walter struct net_device *dev; 2779c3968a85SDaniel Walter struct net *net; 2780c3968a85SDaniel Walter struct in6_addr *addr; 2781c3968a85SDaniel Walter }; 2782c3968a85SDaniel Walter 2783c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg) 2784c3968a85SDaniel Walter { 2785c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 2786c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 2787c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 2788c3968a85SDaniel Walter 2789d1918542SDavid S. Miller if (((void *)rt->dst.dev == dev || !dev) && 2790c3968a85SDaniel Walter rt != net->ipv6.ip6_null_entry && 2791c3968a85SDaniel Walter ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) { 2792c3968a85SDaniel Walter /* remove prefsrc entry */ 2793c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 2794c3968a85SDaniel Walter } 2795c3968a85SDaniel Walter return 0; 2796c3968a85SDaniel Walter } 2797c3968a85SDaniel Walter 2798c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 2799c3968a85SDaniel Walter { 2800c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 2801c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 2802c3968a85SDaniel Walter .dev = ifp->idev->dev, 2803c3968a85SDaniel Walter .net = net, 2804c3968a85SDaniel Walter .addr = &ifp->addr, 2805c3968a85SDaniel Walter }; 28060c3584d5SLi RongQing fib6_clean_all(net, fib6_remove_prefsrc, &adni); 2807c3968a85SDaniel Walter } 2808c3968a85SDaniel Walter 2809be7a010dSDuan Jiong #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY) 2810be7a010dSDuan Jiong #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) 2811be7a010dSDuan Jiong 2812be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */ 2813be7a010dSDuan Jiong static int fib6_clean_tohost(struct rt6_info *rt, void *arg) 2814be7a010dSDuan Jiong { 2815be7a010dSDuan Jiong struct in6_addr *gateway = (struct in6_addr *)arg; 2816be7a010dSDuan Jiong 2817be7a010dSDuan Jiong if ((((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) || 2818be7a010dSDuan Jiong ((rt->rt6i_flags & RTF_CACHE_GATEWAY) == RTF_CACHE_GATEWAY)) && 2819be7a010dSDuan Jiong ipv6_addr_equal(gateway, &rt->rt6i_gateway)) { 2820be7a010dSDuan Jiong return -1; 2821be7a010dSDuan Jiong } 2822be7a010dSDuan Jiong return 0; 2823be7a010dSDuan Jiong } 2824be7a010dSDuan Jiong 2825be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) 2826be7a010dSDuan Jiong { 2827be7a010dSDuan Jiong fib6_clean_all(net, fib6_clean_tohost, gateway); 2828be7a010dSDuan Jiong } 2829be7a010dSDuan Jiong 28308ed67789SDaniel Lezcano struct arg_dev_net { 28318ed67789SDaniel Lezcano struct net_device *dev; 28328ed67789SDaniel Lezcano struct net *net; 28338ed67789SDaniel Lezcano }; 28348ed67789SDaniel Lezcano 2835a1a22c12SDavid Ahern /* called with write lock held for table with rt */ 28361da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg) 28371da177e4SLinus Torvalds { 2838bc3ef660Sstephen hemminger const struct arg_dev_net *adn = arg; 2839bc3ef660Sstephen hemminger const struct net_device *dev = adn->dev; 28408ed67789SDaniel Lezcano 2841d1918542SDavid S. Miller if ((rt->dst.dev == dev || !dev) && 2842a1a22c12SDavid Ahern rt != adn->net->ipv6.ip6_null_entry && 2843a1a22c12SDavid Ahern (rt->rt6i_nsiblings == 0 || 28448397ed36SDavid Ahern (dev && netdev_unregistering(dev)) || 2845a1a22c12SDavid Ahern !rt->rt6i_idev->cnf.ignore_routes_with_linkdown)) 28461da177e4SLinus Torvalds return -1; 2847c159d30cSDavid S. Miller 28481da177e4SLinus Torvalds return 0; 28491da177e4SLinus Torvalds } 28501da177e4SLinus Torvalds 2851f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev) 28521da177e4SLinus Torvalds { 28538ed67789SDaniel Lezcano struct arg_dev_net adn = { 28548ed67789SDaniel Lezcano .dev = dev, 28558ed67789SDaniel Lezcano .net = net, 28568ed67789SDaniel Lezcano }; 28578ed67789SDaniel Lezcano 28580c3584d5SLi RongQing fib6_clean_all(net, fib6_ifdown, &adn); 28591e493d19SDavid S. Miller icmp6_clean_all(fib6_ifdown, &adn); 2860e332bc67SEric W. Biederman if (dev) 28618d0b94afSMartin KaFai Lau rt6_uncached_list_flush_dev(net, dev); 28621da177e4SLinus Torvalds } 28631da177e4SLinus Torvalds 286495c96174SEric Dumazet struct rt6_mtu_change_arg { 28651da177e4SLinus Torvalds struct net_device *dev; 286695c96174SEric Dumazet unsigned int mtu; 28671da177e4SLinus Torvalds }; 28681da177e4SLinus Torvalds 28691da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg) 28701da177e4SLinus Torvalds { 28711da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 28721da177e4SLinus Torvalds struct inet6_dev *idev; 28731da177e4SLinus Torvalds 28741da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 28751da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 28761da177e4SLinus Torvalds We still use this lock to block changes 28771da177e4SLinus Torvalds caused by addrconf/ndisc. 28781da177e4SLinus Torvalds */ 28791da177e4SLinus Torvalds 28801da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 288138308473SDavid S. Miller if (!idev) 28821da177e4SLinus Torvalds return 0; 28831da177e4SLinus Torvalds 28841da177e4SLinus Torvalds /* For administrative MTU increase, there is no way to discover 28851da177e4SLinus Torvalds IPv6 PMTU increase, so PMTU increase should be updated here. 28861da177e4SLinus Torvalds Since RFC 1981 doesn't include administrative MTU increase 28871da177e4SLinus Torvalds update PMTU increase is a MUST. (i.e. jumbo frame) 28881da177e4SLinus Torvalds */ 28891da177e4SLinus Torvalds /* 28901da177e4SLinus Torvalds If new MTU is less than route PMTU, this new MTU will be the 28911da177e4SLinus Torvalds lowest MTU in the path, update the route PMTU to reflect PMTU 28921da177e4SLinus Torvalds decreases; if new MTU is greater than route PMTU, and the 28931da177e4SLinus Torvalds old MTU is the lowest MTU in the path, update the route PMTU 28941da177e4SLinus Torvalds to reflect the increase. In this case if the other nodes' MTU 28951da177e4SLinus Torvalds also have the lowest MTU, TOO BIG MESSAGE will be lead to 289667c408cfSAlexander Alemayhu PMTU discovery. 28971da177e4SLinus Torvalds */ 2898d1918542SDavid S. Miller if (rt->dst.dev == arg->dev && 2899fb56be83SMaciej Żenczykowski dst_metric_raw(&rt->dst, RTAX_MTU) && 29004b32b5adSMartin KaFai Lau !dst_metric_locked(&rt->dst, RTAX_MTU)) { 29014b32b5adSMartin KaFai Lau if (rt->rt6i_flags & RTF_CACHE) { 29024b32b5adSMartin KaFai Lau /* For RTF_CACHE with rt6i_pmtu == 0 29034b32b5adSMartin KaFai Lau * (i.e. a redirected route), 29044b32b5adSMartin KaFai Lau * the metrics of its rt->dst.from has already 29054b32b5adSMartin KaFai Lau * been updated. 29064b32b5adSMartin KaFai Lau */ 29074b32b5adSMartin KaFai Lau if (rt->rt6i_pmtu && rt->rt6i_pmtu > arg->mtu) 29084b32b5adSMartin KaFai Lau rt->rt6i_pmtu = arg->mtu; 29094b32b5adSMartin KaFai Lau } else if (dst_mtu(&rt->dst) >= arg->mtu || 2910d8d1f30bSChangli Gao (dst_mtu(&rt->dst) < arg->mtu && 29114b32b5adSMartin KaFai Lau dst_mtu(&rt->dst) == idev->cnf.mtu6)) { 2912defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu); 2913566cfd8fSSimon Arlott } 29144b32b5adSMartin KaFai Lau } 29151da177e4SLinus Torvalds return 0; 29161da177e4SLinus Torvalds } 29171da177e4SLinus Torvalds 291895c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu) 29191da177e4SLinus Torvalds { 2920c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 2921c71099acSThomas Graf .dev = dev, 2922c71099acSThomas Graf .mtu = mtu, 2923c71099acSThomas Graf }; 29241da177e4SLinus Torvalds 29250c3584d5SLi RongQing fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); 29261da177e4SLinus Torvalds } 29271da177e4SLinus Torvalds 2928ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 29295176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 293086872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 2931ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 293286872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 293386872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 293451ebd318SNicolas Dichtel [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 2935c78ba6d6SLubomir Rintel [RTA_PREF] = { .type = NLA_U8 }, 293619e42e45SRoopa Prabhu [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, 293719e42e45SRoopa Prabhu [RTA_ENCAP] = { .type = NLA_NESTED }, 293832bc201eSXin Long [RTA_EXPIRES] = { .type = NLA_U32 }, 2939622ec2c9SLorenzo Colitti [RTA_UID] = { .type = NLA_U32 }, 29403b45a410SLiping Zhang [RTA_MARK] = { .type = NLA_U32 }, 294186872cb5SThomas Graf }; 294286872cb5SThomas Graf 294386872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 2944333c4301SDavid Ahern struct fib6_config *cfg, 2945333c4301SDavid Ahern struct netlink_ext_ack *extack) 29461da177e4SLinus Torvalds { 294786872cb5SThomas Graf struct rtmsg *rtm; 294886872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 2949c78ba6d6SLubomir Rintel unsigned int pref; 295086872cb5SThomas Graf int err; 29511da177e4SLinus Torvalds 2952fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 2953fceb6435SJohannes Berg NULL); 295486872cb5SThomas Graf if (err < 0) 295586872cb5SThomas Graf goto errout; 29561da177e4SLinus Torvalds 295786872cb5SThomas Graf err = -EINVAL; 295886872cb5SThomas Graf rtm = nlmsg_data(nlh); 295986872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 296086872cb5SThomas Graf 296186872cb5SThomas Graf cfg->fc_table = rtm->rtm_table; 296286872cb5SThomas Graf cfg->fc_dst_len = rtm->rtm_dst_len; 296386872cb5SThomas Graf cfg->fc_src_len = rtm->rtm_src_len; 296486872cb5SThomas Graf cfg->fc_flags = RTF_UP; 296586872cb5SThomas Graf cfg->fc_protocol = rtm->rtm_protocol; 2966ef2c7d7bSNicolas Dichtel cfg->fc_type = rtm->rtm_type; 296786872cb5SThomas Graf 2968ef2c7d7bSNicolas Dichtel if (rtm->rtm_type == RTN_UNREACHABLE || 2969ef2c7d7bSNicolas Dichtel rtm->rtm_type == RTN_BLACKHOLE || 2970b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_PROHIBIT || 2971b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_THROW) 297286872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 297386872cb5SThomas Graf 2974ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 2975ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 2976ab79ad14SMaciej Żenczykowski 29771f56a01fSMartin KaFai Lau if (rtm->rtm_flags & RTM_F_CLONED) 29781f56a01fSMartin KaFai Lau cfg->fc_flags |= RTF_CACHE; 29791f56a01fSMartin KaFai Lau 298015e47304SEric W. Biederman cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid; 298186872cb5SThomas Graf cfg->fc_nlinfo.nlh = nlh; 29823b1e0a65SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = sock_net(skb->sk); 298386872cb5SThomas Graf 298486872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 298567b61f6cSJiri Benc cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); 298686872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 29871da177e4SLinus Torvalds } 298886872cb5SThomas Graf 298986872cb5SThomas Graf if (tb[RTA_DST]) { 299086872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 299186872cb5SThomas Graf 299286872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 299386872cb5SThomas Graf goto errout; 299486872cb5SThomas Graf 299586872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 29961da177e4SLinus Torvalds } 299786872cb5SThomas Graf 299886872cb5SThomas Graf if (tb[RTA_SRC]) { 299986872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 300086872cb5SThomas Graf 300186872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 300286872cb5SThomas Graf goto errout; 300386872cb5SThomas Graf 300486872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 30051da177e4SLinus Torvalds } 300686872cb5SThomas Graf 3007c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 300867b61f6cSJiri Benc cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); 3009c3968a85SDaniel Walter 301086872cb5SThomas Graf if (tb[RTA_OIF]) 301186872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 301286872cb5SThomas Graf 301386872cb5SThomas Graf if (tb[RTA_PRIORITY]) 301486872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 301586872cb5SThomas Graf 301686872cb5SThomas Graf if (tb[RTA_METRICS]) { 301786872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 301886872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 30191da177e4SLinus Torvalds } 302086872cb5SThomas Graf 302186872cb5SThomas Graf if (tb[RTA_TABLE]) 302286872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 302386872cb5SThomas Graf 302451ebd318SNicolas Dichtel if (tb[RTA_MULTIPATH]) { 302551ebd318SNicolas Dichtel cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); 302651ebd318SNicolas Dichtel cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); 30279ed59592SDavid Ahern 30289ed59592SDavid Ahern err = lwtunnel_valid_encap_type_attr(cfg->fc_mp, 3029c255bd68SDavid Ahern cfg->fc_mp_len, extack); 30309ed59592SDavid Ahern if (err < 0) 30319ed59592SDavid Ahern goto errout; 303251ebd318SNicolas Dichtel } 303351ebd318SNicolas Dichtel 3034c78ba6d6SLubomir Rintel if (tb[RTA_PREF]) { 3035c78ba6d6SLubomir Rintel pref = nla_get_u8(tb[RTA_PREF]); 3036c78ba6d6SLubomir Rintel if (pref != ICMPV6_ROUTER_PREF_LOW && 3037c78ba6d6SLubomir Rintel pref != ICMPV6_ROUTER_PREF_HIGH) 3038c78ba6d6SLubomir Rintel pref = ICMPV6_ROUTER_PREF_MEDIUM; 3039c78ba6d6SLubomir Rintel cfg->fc_flags |= RTF_PREF(pref); 3040c78ba6d6SLubomir Rintel } 3041c78ba6d6SLubomir Rintel 304219e42e45SRoopa Prabhu if (tb[RTA_ENCAP]) 304319e42e45SRoopa Prabhu cfg->fc_encap = tb[RTA_ENCAP]; 304419e42e45SRoopa Prabhu 30459ed59592SDavid Ahern if (tb[RTA_ENCAP_TYPE]) { 304619e42e45SRoopa Prabhu cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); 304719e42e45SRoopa Prabhu 3048c255bd68SDavid Ahern err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); 30499ed59592SDavid Ahern if (err < 0) 30509ed59592SDavid Ahern goto errout; 30519ed59592SDavid Ahern } 30529ed59592SDavid Ahern 305332bc201eSXin Long if (tb[RTA_EXPIRES]) { 305432bc201eSXin Long unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); 305532bc201eSXin Long 305632bc201eSXin Long if (addrconf_finite_timeout(timeout)) { 305732bc201eSXin Long cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); 305832bc201eSXin Long cfg->fc_flags |= RTF_EXPIRES; 305932bc201eSXin Long } 306032bc201eSXin Long } 306132bc201eSXin Long 306286872cb5SThomas Graf err = 0; 306386872cb5SThomas Graf errout: 306486872cb5SThomas Graf return err; 30651da177e4SLinus Torvalds } 30661da177e4SLinus Torvalds 30676b9ea5a6SRoopa Prabhu struct rt6_nh { 30686b9ea5a6SRoopa Prabhu struct rt6_info *rt6_info; 30696b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 30706b9ea5a6SRoopa Prabhu struct mx6_config mxc; 30716b9ea5a6SRoopa Prabhu struct list_head next; 30726b9ea5a6SRoopa Prabhu }; 30736b9ea5a6SRoopa Prabhu 30746b9ea5a6SRoopa Prabhu static void ip6_print_replace_route_err(struct list_head *rt6_nh_list) 30756b9ea5a6SRoopa Prabhu { 30766b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 30776b9ea5a6SRoopa Prabhu 30786b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 30797d4d5065SDavid Ahern pr_warn("IPV6: multipath route replace failed (check consistency of installed routes): %pI6c nexthop %pI6c ifi %d\n", 30806b9ea5a6SRoopa Prabhu &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway, 30816b9ea5a6SRoopa Prabhu nh->r_cfg.fc_ifindex); 30826b9ea5a6SRoopa Prabhu } 30836b9ea5a6SRoopa Prabhu } 30846b9ea5a6SRoopa Prabhu 30856b9ea5a6SRoopa Prabhu static int ip6_route_info_append(struct list_head *rt6_nh_list, 30866b9ea5a6SRoopa Prabhu struct rt6_info *rt, struct fib6_config *r_cfg) 30876b9ea5a6SRoopa Prabhu { 30886b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 30896b9ea5a6SRoopa Prabhu struct rt6_info *rtnh; 30906b9ea5a6SRoopa Prabhu int err = -EEXIST; 30916b9ea5a6SRoopa Prabhu 30926b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 30936b9ea5a6SRoopa Prabhu /* check if rt6_info already exists */ 30946b9ea5a6SRoopa Prabhu rtnh = nh->rt6_info; 30956b9ea5a6SRoopa Prabhu 30966b9ea5a6SRoopa Prabhu if (rtnh->dst.dev == rt->dst.dev && 30976b9ea5a6SRoopa Prabhu rtnh->rt6i_idev == rt->rt6i_idev && 30986b9ea5a6SRoopa Prabhu ipv6_addr_equal(&rtnh->rt6i_gateway, 30996b9ea5a6SRoopa Prabhu &rt->rt6i_gateway)) 31006b9ea5a6SRoopa Prabhu return err; 31016b9ea5a6SRoopa Prabhu } 31026b9ea5a6SRoopa Prabhu 31036b9ea5a6SRoopa Prabhu nh = kzalloc(sizeof(*nh), GFP_KERNEL); 31046b9ea5a6SRoopa Prabhu if (!nh) 31056b9ea5a6SRoopa Prabhu return -ENOMEM; 31066b9ea5a6SRoopa Prabhu nh->rt6_info = rt; 31076b9ea5a6SRoopa Prabhu err = ip6_convert_metrics(&nh->mxc, r_cfg); 31086b9ea5a6SRoopa Prabhu if (err) { 31096b9ea5a6SRoopa Prabhu kfree(nh); 31106b9ea5a6SRoopa Prabhu return err; 31116b9ea5a6SRoopa Prabhu } 31126b9ea5a6SRoopa Prabhu memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); 31136b9ea5a6SRoopa Prabhu list_add_tail(&nh->next, rt6_nh_list); 31146b9ea5a6SRoopa Prabhu 31156b9ea5a6SRoopa Prabhu return 0; 31166b9ea5a6SRoopa Prabhu } 31176b9ea5a6SRoopa Prabhu 31183b1137feSDavid Ahern static void ip6_route_mpath_notify(struct rt6_info *rt, 31193b1137feSDavid Ahern struct rt6_info *rt_last, 31203b1137feSDavid Ahern struct nl_info *info, 31213b1137feSDavid Ahern __u16 nlflags) 31223b1137feSDavid Ahern { 31233b1137feSDavid Ahern /* if this is an APPEND route, then rt points to the first route 31243b1137feSDavid Ahern * inserted and rt_last points to last route inserted. Userspace 31253b1137feSDavid Ahern * wants a consistent dump of the route which starts at the first 31263b1137feSDavid Ahern * nexthop. Since sibling routes are always added at the end of 31273b1137feSDavid Ahern * the list, find the first sibling of the last route appended 31283b1137feSDavid Ahern */ 31293b1137feSDavid Ahern if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->rt6i_nsiblings) { 31303b1137feSDavid Ahern rt = list_first_entry(&rt_last->rt6i_siblings, 31313b1137feSDavid Ahern struct rt6_info, 31323b1137feSDavid Ahern rt6i_siblings); 31333b1137feSDavid Ahern } 31343b1137feSDavid Ahern 31353b1137feSDavid Ahern if (rt) 31363b1137feSDavid Ahern inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); 31373b1137feSDavid Ahern } 31383b1137feSDavid Ahern 3139333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg, 3140333c4301SDavid Ahern struct netlink_ext_ack *extack) 314151ebd318SNicolas Dichtel { 31423b1137feSDavid Ahern struct rt6_info *rt_notif = NULL, *rt_last = NULL; 31433b1137feSDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 314451ebd318SNicolas Dichtel struct fib6_config r_cfg; 314551ebd318SNicolas Dichtel struct rtnexthop *rtnh; 31466b9ea5a6SRoopa Prabhu struct rt6_info *rt; 31476b9ea5a6SRoopa Prabhu struct rt6_nh *err_nh; 31486b9ea5a6SRoopa Prabhu struct rt6_nh *nh, *nh_safe; 31493b1137feSDavid Ahern __u16 nlflags; 315051ebd318SNicolas Dichtel int remaining; 315151ebd318SNicolas Dichtel int attrlen; 31526b9ea5a6SRoopa Prabhu int err = 1; 31536b9ea5a6SRoopa Prabhu int nhn = 0; 31546b9ea5a6SRoopa Prabhu int replace = (cfg->fc_nlinfo.nlh && 31556b9ea5a6SRoopa Prabhu (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); 31566b9ea5a6SRoopa Prabhu LIST_HEAD(rt6_nh_list); 315751ebd318SNicolas Dichtel 31583b1137feSDavid Ahern nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; 31593b1137feSDavid Ahern if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) 31603b1137feSDavid Ahern nlflags |= NLM_F_APPEND; 31613b1137feSDavid Ahern 316235f1b4e9SMichal Kubeček remaining = cfg->fc_mp_len; 316351ebd318SNicolas Dichtel rtnh = (struct rtnexthop *)cfg->fc_mp; 316451ebd318SNicolas Dichtel 31656b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry and build a list (rt6_nh_list) of 31666b9ea5a6SRoopa Prabhu * rt6_info structs per nexthop 31676b9ea5a6SRoopa Prabhu */ 316851ebd318SNicolas Dichtel while (rtnh_ok(rtnh, remaining)) { 316951ebd318SNicolas Dichtel memcpy(&r_cfg, cfg, sizeof(*cfg)); 317051ebd318SNicolas Dichtel if (rtnh->rtnh_ifindex) 317151ebd318SNicolas Dichtel r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 317251ebd318SNicolas Dichtel 317351ebd318SNicolas Dichtel attrlen = rtnh_attrlen(rtnh); 317451ebd318SNicolas Dichtel if (attrlen > 0) { 317551ebd318SNicolas Dichtel struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 317651ebd318SNicolas Dichtel 317751ebd318SNicolas Dichtel nla = nla_find(attrs, attrlen, RTA_GATEWAY); 317851ebd318SNicolas Dichtel if (nla) { 317967b61f6cSJiri Benc r_cfg.fc_gateway = nla_get_in6_addr(nla); 318051ebd318SNicolas Dichtel r_cfg.fc_flags |= RTF_GATEWAY; 318151ebd318SNicolas Dichtel } 318219e42e45SRoopa Prabhu r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); 318319e42e45SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); 318419e42e45SRoopa Prabhu if (nla) 318519e42e45SRoopa Prabhu r_cfg.fc_encap_type = nla_get_u16(nla); 318651ebd318SNicolas Dichtel } 31876b9ea5a6SRoopa Prabhu 3188333c4301SDavid Ahern rt = ip6_route_info_create(&r_cfg, extack); 31898c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 31908c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 31918c5b83f0SRoopa Prabhu rt = NULL; 31926b9ea5a6SRoopa Prabhu goto cleanup; 31938c5b83f0SRoopa Prabhu } 31946b9ea5a6SRoopa Prabhu 31956b9ea5a6SRoopa Prabhu err = ip6_route_info_append(&rt6_nh_list, rt, &r_cfg); 319651ebd318SNicolas Dichtel if (err) { 3197*587fea74SWei Wang dst_release_immediate(&rt->dst); 31986b9ea5a6SRoopa Prabhu goto cleanup; 319951ebd318SNicolas Dichtel } 32006b9ea5a6SRoopa Prabhu 32016b9ea5a6SRoopa Prabhu rtnh = rtnh_next(rtnh, &remaining); 320251ebd318SNicolas Dichtel } 32036b9ea5a6SRoopa Prabhu 32043b1137feSDavid Ahern /* for add and replace send one notification with all nexthops. 32053b1137feSDavid Ahern * Skip the notification in fib6_add_rt2node and send one with 32063b1137feSDavid Ahern * the full route when done 32073b1137feSDavid Ahern */ 32083b1137feSDavid Ahern info->skip_notify = 1; 32093b1137feSDavid Ahern 32106b9ea5a6SRoopa Prabhu err_nh = NULL; 32116b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 32123b1137feSDavid Ahern rt_last = nh->rt6_info; 3213333c4301SDavid Ahern err = __ip6_ins_rt(nh->rt6_info, info, &nh->mxc, extack); 32143b1137feSDavid Ahern /* save reference to first route for notification */ 32153b1137feSDavid Ahern if (!rt_notif && !err) 32163b1137feSDavid Ahern rt_notif = nh->rt6_info; 32173b1137feSDavid Ahern 32186b9ea5a6SRoopa Prabhu /* nh->rt6_info is used or freed at this point, reset to NULL*/ 32196b9ea5a6SRoopa Prabhu nh->rt6_info = NULL; 32206b9ea5a6SRoopa Prabhu if (err) { 32216b9ea5a6SRoopa Prabhu if (replace && nhn) 32226b9ea5a6SRoopa Prabhu ip6_print_replace_route_err(&rt6_nh_list); 32236b9ea5a6SRoopa Prabhu err_nh = nh; 32246b9ea5a6SRoopa Prabhu goto add_errout; 32256b9ea5a6SRoopa Prabhu } 32266b9ea5a6SRoopa Prabhu 32271a72418bSNicolas Dichtel /* Because each route is added like a single route we remove 322827596472SMichal Kubeček * these flags after the first nexthop: if there is a collision, 322927596472SMichal Kubeček * we have already failed to add the first nexthop: 323027596472SMichal Kubeček * fib6_add_rt2node() has rejected it; when replacing, old 323127596472SMichal Kubeček * nexthops have been replaced by first new, the rest should 323227596472SMichal Kubeček * be added to it. 32331a72418bSNicolas Dichtel */ 323427596472SMichal Kubeček cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | 323527596472SMichal Kubeček NLM_F_REPLACE); 32366b9ea5a6SRoopa Prabhu nhn++; 32376b9ea5a6SRoopa Prabhu } 32386b9ea5a6SRoopa Prabhu 32393b1137feSDavid Ahern /* success ... tell user about new route */ 32403b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 32416b9ea5a6SRoopa Prabhu goto cleanup; 32426b9ea5a6SRoopa Prabhu 32436b9ea5a6SRoopa Prabhu add_errout: 32443b1137feSDavid Ahern /* send notification for routes that were added so that 32453b1137feSDavid Ahern * the delete notifications sent by ip6_route_del are 32463b1137feSDavid Ahern * coherent 32473b1137feSDavid Ahern */ 32483b1137feSDavid Ahern if (rt_notif) 32493b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 32503b1137feSDavid Ahern 32516b9ea5a6SRoopa Prabhu /* Delete routes that were already added */ 32526b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 32536b9ea5a6SRoopa Prabhu if (err_nh == nh) 32546b9ea5a6SRoopa Prabhu break; 3255333c4301SDavid Ahern ip6_route_del(&nh->r_cfg, extack); 32566b9ea5a6SRoopa Prabhu } 32576b9ea5a6SRoopa Prabhu 32586b9ea5a6SRoopa Prabhu cleanup: 32596b9ea5a6SRoopa Prabhu list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) { 3260*587fea74SWei Wang if (nh->rt6_info) 3261*587fea74SWei Wang dst_release_immediate(&nh->rt6_info->dst); 32626b9ea5a6SRoopa Prabhu kfree(nh->mxc.mx); 32636b9ea5a6SRoopa Prabhu list_del(&nh->next); 32646b9ea5a6SRoopa Prabhu kfree(nh); 32656b9ea5a6SRoopa Prabhu } 32666b9ea5a6SRoopa Prabhu 32676b9ea5a6SRoopa Prabhu return err; 32686b9ea5a6SRoopa Prabhu } 32696b9ea5a6SRoopa Prabhu 3270333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg, 3271333c4301SDavid Ahern struct netlink_ext_ack *extack) 32726b9ea5a6SRoopa Prabhu { 32736b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 32746b9ea5a6SRoopa Prabhu struct rtnexthop *rtnh; 32756b9ea5a6SRoopa Prabhu int remaining; 32766b9ea5a6SRoopa Prabhu int attrlen; 32776b9ea5a6SRoopa Prabhu int err = 1, last_err = 0; 32786b9ea5a6SRoopa Prabhu 32796b9ea5a6SRoopa Prabhu remaining = cfg->fc_mp_len; 32806b9ea5a6SRoopa Prabhu rtnh = (struct rtnexthop *)cfg->fc_mp; 32816b9ea5a6SRoopa Prabhu 32826b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry */ 32836b9ea5a6SRoopa Prabhu while (rtnh_ok(rtnh, remaining)) { 32846b9ea5a6SRoopa Prabhu memcpy(&r_cfg, cfg, sizeof(*cfg)); 32856b9ea5a6SRoopa Prabhu if (rtnh->rtnh_ifindex) 32866b9ea5a6SRoopa Prabhu r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 32876b9ea5a6SRoopa Prabhu 32886b9ea5a6SRoopa Prabhu attrlen = rtnh_attrlen(rtnh); 32896b9ea5a6SRoopa Prabhu if (attrlen > 0) { 32906b9ea5a6SRoopa Prabhu struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 32916b9ea5a6SRoopa Prabhu 32926b9ea5a6SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_GATEWAY); 32936b9ea5a6SRoopa Prabhu if (nla) { 32946b9ea5a6SRoopa Prabhu nla_memcpy(&r_cfg.fc_gateway, nla, 16); 32956b9ea5a6SRoopa Prabhu r_cfg.fc_flags |= RTF_GATEWAY; 32966b9ea5a6SRoopa Prabhu } 32976b9ea5a6SRoopa Prabhu } 3298333c4301SDavid Ahern err = ip6_route_del(&r_cfg, extack); 32996b9ea5a6SRoopa Prabhu if (err) 33006b9ea5a6SRoopa Prabhu last_err = err; 33016b9ea5a6SRoopa Prabhu 330251ebd318SNicolas Dichtel rtnh = rtnh_next(rtnh, &remaining); 330351ebd318SNicolas Dichtel } 330451ebd318SNicolas Dichtel 330551ebd318SNicolas Dichtel return last_err; 330651ebd318SNicolas Dichtel } 330751ebd318SNicolas Dichtel 3308c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, 3309c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 33101da177e4SLinus Torvalds { 331186872cb5SThomas Graf struct fib6_config cfg; 331286872cb5SThomas Graf int err; 33131da177e4SLinus Torvalds 3314333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 331586872cb5SThomas Graf if (err < 0) 331686872cb5SThomas Graf return err; 331786872cb5SThomas Graf 331851ebd318SNicolas Dichtel if (cfg.fc_mp) 3319333c4301SDavid Ahern return ip6_route_multipath_del(&cfg, extack); 33200ae81335SDavid Ahern else { 33210ae81335SDavid Ahern cfg.fc_delete_all_nh = 1; 3322333c4301SDavid Ahern return ip6_route_del(&cfg, extack); 33231da177e4SLinus Torvalds } 33240ae81335SDavid Ahern } 33251da177e4SLinus Torvalds 3326c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, 3327c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 33281da177e4SLinus Torvalds { 332986872cb5SThomas Graf struct fib6_config cfg; 333086872cb5SThomas Graf int err; 33311da177e4SLinus Torvalds 3332333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 333386872cb5SThomas Graf if (err < 0) 333486872cb5SThomas Graf return err; 333586872cb5SThomas Graf 333651ebd318SNicolas Dichtel if (cfg.fc_mp) 3337333c4301SDavid Ahern return ip6_route_multipath_add(&cfg, extack); 333851ebd318SNicolas Dichtel else 3339333c4301SDavid Ahern return ip6_route_add(&cfg, extack); 33401da177e4SLinus Torvalds } 33411da177e4SLinus Torvalds 3342beb1afacSDavid Ahern static size_t rt6_nlmsg_size(struct rt6_info *rt) 3343339bf98fSThomas Graf { 3344beb1afacSDavid Ahern int nexthop_len = 0; 3345beb1afacSDavid Ahern 3346beb1afacSDavid Ahern if (rt->rt6i_nsiblings) { 3347beb1afacSDavid Ahern nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */ 3348beb1afacSDavid Ahern + NLA_ALIGN(sizeof(struct rtnexthop)) 3349beb1afacSDavid Ahern + nla_total_size(16) /* RTA_GATEWAY */ 3350beb1afacSDavid Ahern + lwtunnel_get_encap_size(rt->dst.lwtstate); 3351beb1afacSDavid Ahern 3352beb1afacSDavid Ahern nexthop_len *= rt->rt6i_nsiblings; 3353beb1afacSDavid Ahern } 3354beb1afacSDavid Ahern 3355339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 3356339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 3357339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 3358339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 3359339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 3360339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 3361339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 3362339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 3363339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 33646a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 3365ea697639SDaniel Borkmann + nla_total_size(sizeof(struct rta_cacheinfo)) 3366c78ba6d6SLubomir Rintel + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ 336719e42e45SRoopa Prabhu + nla_total_size(1) /* RTA_PREF */ 3368beb1afacSDavid Ahern + lwtunnel_get_encap_size(rt->dst.lwtstate) 3369beb1afacSDavid Ahern + nexthop_len; 3370beb1afacSDavid Ahern } 3371beb1afacSDavid Ahern 3372beb1afacSDavid Ahern static int rt6_nexthop_info(struct sk_buff *skb, struct rt6_info *rt, 33735be083ceSDavid Ahern unsigned int *flags, bool skip_oif) 3374beb1afacSDavid Ahern { 3375beb1afacSDavid Ahern if (!netif_running(rt->dst.dev) || !netif_carrier_ok(rt->dst.dev)) { 3376beb1afacSDavid Ahern *flags |= RTNH_F_LINKDOWN; 3377beb1afacSDavid Ahern if (rt->rt6i_idev->cnf.ignore_routes_with_linkdown) 3378beb1afacSDavid Ahern *flags |= RTNH_F_DEAD; 3379beb1afacSDavid Ahern } 3380beb1afacSDavid Ahern 3381beb1afacSDavid Ahern if (rt->rt6i_flags & RTF_GATEWAY) { 3382beb1afacSDavid Ahern if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0) 3383beb1afacSDavid Ahern goto nla_put_failure; 3384beb1afacSDavid Ahern } 3385beb1afacSDavid Ahern 33865be083ceSDavid Ahern /* not needed for multipath encoding b/c it has a rtnexthop struct */ 33875be083ceSDavid Ahern if (!skip_oif && rt->dst.dev && 3388beb1afacSDavid Ahern nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex)) 3389beb1afacSDavid Ahern goto nla_put_failure; 3390beb1afacSDavid Ahern 3391beb1afacSDavid Ahern if (rt->dst.lwtstate && 3392beb1afacSDavid Ahern lwtunnel_fill_encap(skb, rt->dst.lwtstate) < 0) 3393beb1afacSDavid Ahern goto nla_put_failure; 3394beb1afacSDavid Ahern 3395beb1afacSDavid Ahern return 0; 3396beb1afacSDavid Ahern 3397beb1afacSDavid Ahern nla_put_failure: 3398beb1afacSDavid Ahern return -EMSGSIZE; 3399beb1afacSDavid Ahern } 3400beb1afacSDavid Ahern 34015be083ceSDavid Ahern /* add multipath next hop */ 3402beb1afacSDavid Ahern static int rt6_add_nexthop(struct sk_buff *skb, struct rt6_info *rt) 3403beb1afacSDavid Ahern { 3404beb1afacSDavid Ahern struct rtnexthop *rtnh; 3405beb1afacSDavid Ahern unsigned int flags = 0; 3406beb1afacSDavid Ahern 3407beb1afacSDavid Ahern rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh)); 3408beb1afacSDavid Ahern if (!rtnh) 3409beb1afacSDavid Ahern goto nla_put_failure; 3410beb1afacSDavid Ahern 3411beb1afacSDavid Ahern rtnh->rtnh_hops = 0; 3412beb1afacSDavid Ahern rtnh->rtnh_ifindex = rt->dst.dev ? rt->dst.dev->ifindex : 0; 3413beb1afacSDavid Ahern 34145be083ceSDavid Ahern if (rt6_nexthop_info(skb, rt, &flags, true) < 0) 3415beb1afacSDavid Ahern goto nla_put_failure; 3416beb1afacSDavid Ahern 3417beb1afacSDavid Ahern rtnh->rtnh_flags = flags; 3418beb1afacSDavid Ahern 3419beb1afacSDavid Ahern /* length of rtnetlink header + attributes */ 3420beb1afacSDavid Ahern rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh; 3421beb1afacSDavid Ahern 3422beb1afacSDavid Ahern return 0; 3423beb1afacSDavid Ahern 3424beb1afacSDavid Ahern nla_put_failure: 3425beb1afacSDavid Ahern return -EMSGSIZE; 3426339bf98fSThomas Graf } 3427339bf98fSThomas Graf 3428191cd582SBrian Haley static int rt6_fill_node(struct net *net, 3429191cd582SBrian Haley struct sk_buff *skb, struct rt6_info *rt, 34300d51aa80SJamal Hadi Salim struct in6_addr *dst, struct in6_addr *src, 343115e47304SEric W. Biederman int iif, int type, u32 portid, u32 seq, 3432f8cfe2ceSDavid Ahern unsigned int flags) 34331da177e4SLinus Torvalds { 34344b32b5adSMartin KaFai Lau u32 metrics[RTAX_MAX]; 34351da177e4SLinus Torvalds struct rtmsg *rtm; 34361da177e4SLinus Torvalds struct nlmsghdr *nlh; 3437e3703b3dSThomas Graf long expires; 34389e762a4aSPatrick McHardy u32 table; 34391da177e4SLinus Torvalds 344015e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); 344138308473SDavid S. Miller if (!nlh) 344226932566SPatrick McHardy return -EMSGSIZE; 34432d7202bfSThomas Graf 34442d7202bfSThomas Graf rtm = nlmsg_data(nlh); 34451da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 34461da177e4SLinus Torvalds rtm->rtm_dst_len = rt->rt6i_dst.plen; 34471da177e4SLinus Torvalds rtm->rtm_src_len = rt->rt6i_src.plen; 34481da177e4SLinus Torvalds rtm->rtm_tos = 0; 3449c71099acSThomas Graf if (rt->rt6i_table) 34509e762a4aSPatrick McHardy table = rt->rt6i_table->tb6_id; 3451c71099acSThomas Graf else 34529e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 34539e762a4aSPatrick McHardy rtm->rtm_table = table; 3454c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, table)) 3455c78679e8SDavid S. Miller goto nla_put_failure; 3456ef2c7d7bSNicolas Dichtel if (rt->rt6i_flags & RTF_REJECT) { 3457ef2c7d7bSNicolas Dichtel switch (rt->dst.error) { 3458ef2c7d7bSNicolas Dichtel case -EINVAL: 3459ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_BLACKHOLE; 3460ef2c7d7bSNicolas Dichtel break; 3461ef2c7d7bSNicolas Dichtel case -EACCES: 3462ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_PROHIBIT; 3463ef2c7d7bSNicolas Dichtel break; 3464b4949ab2SNicolas Dichtel case -EAGAIN: 3465b4949ab2SNicolas Dichtel rtm->rtm_type = RTN_THROW; 3466b4949ab2SNicolas Dichtel break; 3467ef2c7d7bSNicolas Dichtel default: 34681da177e4SLinus Torvalds rtm->rtm_type = RTN_UNREACHABLE; 3469ef2c7d7bSNicolas Dichtel break; 3470ef2c7d7bSNicolas Dichtel } 3471ef2c7d7bSNicolas Dichtel } 3472ab79ad14SMaciej Żenczykowski else if (rt->rt6i_flags & RTF_LOCAL) 3473ab79ad14SMaciej Żenczykowski rtm->rtm_type = RTN_LOCAL; 34744ee39733SDavid Ahern else if (rt->rt6i_flags & RTF_ANYCAST) 34754ee39733SDavid Ahern rtm->rtm_type = RTN_ANYCAST; 3476d1918542SDavid S. Miller else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK)) 34771da177e4SLinus Torvalds rtm->rtm_type = RTN_LOCAL; 34781da177e4SLinus Torvalds else 34791da177e4SLinus Torvalds rtm->rtm_type = RTN_UNICAST; 34801da177e4SLinus Torvalds rtm->rtm_flags = 0; 34811da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 34821da177e4SLinus Torvalds rtm->rtm_protocol = rt->rt6i_protocol; 34831da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_DYNAMIC) 34841da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_REDIRECT; 3485f0396f60SDenis Ovsienko else if (rt->rt6i_flags & RTF_ADDRCONF) { 3486f0396f60SDenis Ovsienko if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO)) 34871da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_RA; 3488f0396f60SDenis Ovsienko else 3489f0396f60SDenis Ovsienko rtm->rtm_protocol = RTPROT_KERNEL; 3490f0396f60SDenis Ovsienko } 34911da177e4SLinus Torvalds 34921da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) 34931da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 34941da177e4SLinus Torvalds 34951da177e4SLinus Torvalds if (dst) { 3496930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_DST, dst)) 3497c78679e8SDavid S. Miller goto nla_put_failure; 34981da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 34991da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 3500930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr)) 3501c78679e8SDavid S. Miller goto nla_put_failure; 35021da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 35031da177e4SLinus Torvalds if (src) { 3504930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_SRC, src)) 3505c78679e8SDavid S. Miller goto nla_put_failure; 35061da177e4SLinus Torvalds rtm->rtm_src_len = 128; 3507c78679e8SDavid S. Miller } else if (rtm->rtm_src_len && 3508930345eaSJiri Benc nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr)) 3509c78679e8SDavid S. Miller goto nla_put_failure; 35101da177e4SLinus Torvalds #endif 35117bc570c8SYOSHIFUJI Hideaki if (iif) { 35127bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 35137bc570c8SYOSHIFUJI Hideaki if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) { 3514fd61c6baSDavid Ahern int err = ip6mr_get_route(net, skb, rtm, portid); 35152cf75070SNikolay Aleksandrov 35167bc570c8SYOSHIFUJI Hideaki if (err == 0) 35177bc570c8SYOSHIFUJI Hideaki return 0; 3518fd61c6baSDavid Ahern if (err < 0) 35197bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 35207bc570c8SYOSHIFUJI Hideaki } else 35217bc570c8SYOSHIFUJI Hideaki #endif 3522c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_IIF, iif)) 3523c78679e8SDavid S. Miller goto nla_put_failure; 35247bc570c8SYOSHIFUJI Hideaki } else if (dst) { 35251da177e4SLinus Torvalds struct in6_addr saddr_buf; 3526c78679e8SDavid S. Miller if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 && 3527930345eaSJiri Benc nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 3528c78679e8SDavid S. Miller goto nla_put_failure; 3529c3968a85SDaniel Walter } 3530c3968a85SDaniel Walter 3531c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) { 3532c3968a85SDaniel Walter struct in6_addr saddr_buf; 35334e3fd7a0SAlexey Dobriyan saddr_buf = rt->rt6i_prefsrc.addr; 3534930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 3535c78679e8SDavid S. Miller goto nla_put_failure; 35361da177e4SLinus Torvalds } 35372d7202bfSThomas Graf 35384b32b5adSMartin KaFai Lau memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics)); 35394b32b5adSMartin KaFai Lau if (rt->rt6i_pmtu) 35404b32b5adSMartin KaFai Lau metrics[RTAX_MTU - 1] = rt->rt6i_pmtu; 35414b32b5adSMartin KaFai Lau if (rtnetlink_put_metrics(skb, metrics) < 0) 35422d7202bfSThomas Graf goto nla_put_failure; 35432d7202bfSThomas Graf 3544beb1afacSDavid Ahern if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric)) 3545beb1afacSDavid Ahern goto nla_put_failure; 3546beb1afacSDavid Ahern 3547beb1afacSDavid Ahern /* For multipath routes, walk the siblings list and add 3548beb1afacSDavid Ahern * each as a nexthop within RTA_MULTIPATH. 3549beb1afacSDavid Ahern */ 3550beb1afacSDavid Ahern if (rt->rt6i_nsiblings) { 3551beb1afacSDavid Ahern struct rt6_info *sibling, *next_sibling; 3552beb1afacSDavid Ahern struct nlattr *mp; 3553beb1afacSDavid Ahern 3554beb1afacSDavid Ahern mp = nla_nest_start(skb, RTA_MULTIPATH); 3555beb1afacSDavid Ahern if (!mp) 3556beb1afacSDavid Ahern goto nla_put_failure; 3557beb1afacSDavid Ahern 3558beb1afacSDavid Ahern if (rt6_add_nexthop(skb, rt) < 0) 3559beb1afacSDavid Ahern goto nla_put_failure; 3560beb1afacSDavid Ahern 3561beb1afacSDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 3562beb1afacSDavid Ahern &rt->rt6i_siblings, rt6i_siblings) { 3563beb1afacSDavid Ahern if (rt6_add_nexthop(skb, sibling) < 0) 356494f826b8SEric Dumazet goto nla_put_failure; 356594f826b8SEric Dumazet } 35662d7202bfSThomas Graf 3567beb1afacSDavid Ahern nla_nest_end(skb, mp); 3568beb1afacSDavid Ahern } else { 35695be083ceSDavid Ahern if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0) 3570c78679e8SDavid S. Miller goto nla_put_failure; 3571beb1afacSDavid Ahern } 35728253947eSLi Wei 35738253947eSLi Wei expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0; 357469cdf8f9SYOSHIFUJI Hideaki 357587a50699SDavid S. Miller if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0) 3576e3703b3dSThomas Graf goto nla_put_failure; 35771da177e4SLinus Torvalds 3578c78ba6d6SLubomir Rintel if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags))) 3579c78ba6d6SLubomir Rintel goto nla_put_failure; 3580c78ba6d6SLubomir Rintel 358119e42e45SRoopa Prabhu 3582053c095aSJohannes Berg nlmsg_end(skb, nlh); 3583053c095aSJohannes Berg return 0; 35842d7202bfSThomas Graf 35852d7202bfSThomas Graf nla_put_failure: 358626932566SPatrick McHardy nlmsg_cancel(skb, nlh); 358726932566SPatrick McHardy return -EMSGSIZE; 35881da177e4SLinus Torvalds } 35891da177e4SLinus Torvalds 35901b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg) 35911da177e4SLinus Torvalds { 35921da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 35931f17e2f2SDavid Ahern struct net *net = arg->net; 35941f17e2f2SDavid Ahern 35951f17e2f2SDavid Ahern if (rt == net->ipv6.ip6_null_entry) 35961f17e2f2SDavid Ahern return 0; 35971da177e4SLinus Torvalds 35982d7202bfSThomas Graf if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { 35992d7202bfSThomas Graf struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); 3600f8cfe2ceSDavid Ahern 3601f8cfe2ceSDavid Ahern /* user wants prefix routes only */ 3602f8cfe2ceSDavid Ahern if (rtm->rtm_flags & RTM_F_PREFIX && 3603f8cfe2ceSDavid Ahern !(rt->rt6i_flags & RTF_PREFIX_RT)) { 3604f8cfe2ceSDavid Ahern /* success since this is not a prefix route */ 3605f8cfe2ceSDavid Ahern return 1; 3606f8cfe2ceSDavid Ahern } 3607f8cfe2ceSDavid Ahern } 36081da177e4SLinus Torvalds 36091f17e2f2SDavid Ahern return rt6_fill_node(net, 3610191cd582SBrian Haley arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE, 361115e47304SEric W. Biederman NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq, 3612f8cfe2ceSDavid Ahern NLM_F_MULTI); 36131da177e4SLinus Torvalds } 36141da177e4SLinus Torvalds 3615c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, 3616c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 36171da177e4SLinus Torvalds { 36183b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 3619ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 362018c3a61cSRoopa Prabhu int err, iif = 0, oif = 0; 362118c3a61cSRoopa Prabhu struct dst_entry *dst; 36221da177e4SLinus Torvalds struct rt6_info *rt; 3623ab364a6fSThomas Graf struct sk_buff *skb; 3624ab364a6fSThomas Graf struct rtmsg *rtm; 36254c9483b2SDavid S. Miller struct flowi6 fl6; 362618c3a61cSRoopa Prabhu bool fibmatch; 3627ab364a6fSThomas Graf 3628fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 3629c21ef3e3SDavid Ahern extack); 3630ab364a6fSThomas Graf if (err < 0) 3631ab364a6fSThomas Graf goto errout; 3632ab364a6fSThomas Graf 3633ab364a6fSThomas Graf err = -EINVAL; 36344c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 363538b7097bSHannes Frederic Sowa rtm = nlmsg_data(nlh); 363638b7097bSHannes Frederic Sowa fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0); 363718c3a61cSRoopa Prabhu fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); 3638ab364a6fSThomas Graf 3639ab364a6fSThomas Graf if (tb[RTA_SRC]) { 3640ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 3641ab364a6fSThomas Graf goto errout; 3642ab364a6fSThomas Graf 36434e3fd7a0SAlexey Dobriyan fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 3644ab364a6fSThomas Graf } 3645ab364a6fSThomas Graf 3646ab364a6fSThomas Graf if (tb[RTA_DST]) { 3647ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 3648ab364a6fSThomas Graf goto errout; 3649ab364a6fSThomas Graf 36504e3fd7a0SAlexey Dobriyan fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 3651ab364a6fSThomas Graf } 3652ab364a6fSThomas Graf 3653ab364a6fSThomas Graf if (tb[RTA_IIF]) 3654ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 3655ab364a6fSThomas Graf 3656ab364a6fSThomas Graf if (tb[RTA_OIF]) 365772331bc0SShmulik Ladkani oif = nla_get_u32(tb[RTA_OIF]); 3658ab364a6fSThomas Graf 36592e47b291SLorenzo Colitti if (tb[RTA_MARK]) 36602e47b291SLorenzo Colitti fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); 36612e47b291SLorenzo Colitti 3662622ec2c9SLorenzo Colitti if (tb[RTA_UID]) 3663622ec2c9SLorenzo Colitti fl6.flowi6_uid = make_kuid(current_user_ns(), 3664622ec2c9SLorenzo Colitti nla_get_u32(tb[RTA_UID])); 3665622ec2c9SLorenzo Colitti else 3666622ec2c9SLorenzo Colitti fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); 3667622ec2c9SLorenzo Colitti 3668ab364a6fSThomas Graf if (iif) { 3669ab364a6fSThomas Graf struct net_device *dev; 367072331bc0SShmulik Ladkani int flags = 0; 367172331bc0SShmulik Ladkani 36725578689aSDaniel Lezcano dev = __dev_get_by_index(net, iif); 3673ab364a6fSThomas Graf if (!dev) { 3674ab364a6fSThomas Graf err = -ENODEV; 3675ab364a6fSThomas Graf goto errout; 3676ab364a6fSThomas Graf } 367772331bc0SShmulik Ladkani 367872331bc0SShmulik Ladkani fl6.flowi6_iif = iif; 367972331bc0SShmulik Ladkani 368072331bc0SShmulik Ladkani if (!ipv6_addr_any(&fl6.saddr)) 368172331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_HAS_SADDR; 368272331bc0SShmulik Ladkani 368318c3a61cSRoopa Prabhu if (!fibmatch) 368418c3a61cSRoopa Prabhu dst = ip6_route_input_lookup(net, dev, &fl6, flags); 368572331bc0SShmulik Ladkani } else { 368672331bc0SShmulik Ladkani fl6.flowi6_oif = oif; 368772331bc0SShmulik Ladkani 368818c3a61cSRoopa Prabhu if (!fibmatch) 368918c3a61cSRoopa Prabhu dst = ip6_route_output(net, NULL, &fl6); 369018c3a61cSRoopa Prabhu } 369118c3a61cSRoopa Prabhu 369218c3a61cSRoopa Prabhu if (fibmatch) 369318c3a61cSRoopa Prabhu dst = ip6_route_lookup(net, &fl6, 0); 369418c3a61cSRoopa Prabhu 369518c3a61cSRoopa Prabhu rt = container_of(dst, struct rt6_info, dst); 369618c3a61cSRoopa Prabhu if (rt->dst.error) { 369718c3a61cSRoopa Prabhu err = rt->dst.error; 369818c3a61cSRoopa Prabhu ip6_rt_put(rt); 369918c3a61cSRoopa Prabhu goto errout; 3700ab364a6fSThomas Graf } 37011da177e4SLinus Torvalds 37029d6acb3bSWANG Cong if (rt == net->ipv6.ip6_null_entry) { 37039d6acb3bSWANG Cong err = rt->dst.error; 37049d6acb3bSWANG Cong ip6_rt_put(rt); 37059d6acb3bSWANG Cong goto errout; 37069d6acb3bSWANG Cong } 37079d6acb3bSWANG Cong 37081da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 370938308473SDavid S. Miller if (!skb) { 371094e187c0SAmerigo Wang ip6_rt_put(rt); 3711ab364a6fSThomas Graf err = -ENOBUFS; 3712ab364a6fSThomas Graf goto errout; 3713ab364a6fSThomas Graf } 37141da177e4SLinus Torvalds 3715d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 371618c3a61cSRoopa Prabhu if (fibmatch) 371718c3a61cSRoopa Prabhu err = rt6_fill_node(net, skb, rt, NULL, NULL, iif, 371818c3a61cSRoopa Prabhu RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 371918c3a61cSRoopa Prabhu nlh->nlmsg_seq, 0); 372018c3a61cSRoopa Prabhu else 37214c9483b2SDavid S. Miller err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif, 372215e47304SEric W. Biederman RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 3723f8cfe2ceSDavid Ahern nlh->nlmsg_seq, 0); 37241da177e4SLinus Torvalds if (err < 0) { 3725ab364a6fSThomas Graf kfree_skb(skb); 3726ab364a6fSThomas Graf goto errout; 37271da177e4SLinus Torvalds } 37281da177e4SLinus Torvalds 372915e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 3730ab364a6fSThomas Graf errout: 37311da177e4SLinus Torvalds return err; 37321da177e4SLinus Torvalds } 37331da177e4SLinus Torvalds 373437a1d361SRoopa Prabhu void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info, 373537a1d361SRoopa Prabhu unsigned int nlm_flags) 37361da177e4SLinus Torvalds { 37371da177e4SLinus Torvalds struct sk_buff *skb; 37385578689aSDaniel Lezcano struct net *net = info->nl_net; 3739528c4cebSDenis V. Lunev u32 seq; 3740528c4cebSDenis V. Lunev int err; 37410d51aa80SJamal Hadi Salim 3742528c4cebSDenis V. Lunev err = -ENOBUFS; 374338308473SDavid S. Miller seq = info->nlh ? info->nlh->nlmsg_seq : 0; 374486872cb5SThomas Graf 374519e42e45SRoopa Prabhu skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 374638308473SDavid S. Miller if (!skb) 374721713ebcSThomas Graf goto errout; 37481da177e4SLinus Torvalds 3749191cd582SBrian Haley err = rt6_fill_node(net, skb, rt, NULL, NULL, 0, 3750f8cfe2ceSDavid Ahern event, info->portid, seq, nlm_flags); 375126932566SPatrick McHardy if (err < 0) { 375226932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 375326932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 375426932566SPatrick McHardy kfree_skb(skb); 375526932566SPatrick McHardy goto errout; 375626932566SPatrick McHardy } 375715e47304SEric W. Biederman rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 37585578689aSDaniel Lezcano info->nlh, gfp_any()); 37591ce85fe4SPablo Neira Ayuso return; 376021713ebcSThomas Graf errout: 376121713ebcSThomas Graf if (err < 0) 37625578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 37631da177e4SLinus Torvalds } 37641da177e4SLinus Torvalds 37658ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 3766351638e7SJiri Pirko unsigned long event, void *ptr) 37678ed67789SDaniel Lezcano { 3768351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 3769c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 37708ed67789SDaniel Lezcano 3771242d3a49SWANG Cong if (!(dev->flags & IFF_LOOPBACK)) 3772242d3a49SWANG Cong return NOTIFY_OK; 3773242d3a49SWANG Cong 3774242d3a49SWANG Cong if (event == NETDEV_REGISTER) { 3775d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 37768ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 37778ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 3778d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 37798ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 3780d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 37818ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 37828ed67789SDaniel Lezcano #endif 3783242d3a49SWANG Cong } else if (event == NETDEV_UNREGISTER) { 3784242d3a49SWANG Cong in6_dev_put(net->ipv6.ip6_null_entry->rt6i_idev); 3785242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 3786242d3a49SWANG Cong in6_dev_put(net->ipv6.ip6_prohibit_entry->rt6i_idev); 3787242d3a49SWANG Cong in6_dev_put(net->ipv6.ip6_blk_hole_entry->rt6i_idev); 3788242d3a49SWANG Cong #endif 37898ed67789SDaniel Lezcano } 37908ed67789SDaniel Lezcano 37918ed67789SDaniel Lezcano return NOTIFY_OK; 37928ed67789SDaniel Lezcano } 37938ed67789SDaniel Lezcano 37941da177e4SLinus Torvalds /* 37951da177e4SLinus Torvalds * /proc 37961da177e4SLinus Torvalds */ 37971da177e4SLinus Torvalds 37981da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 37991da177e4SLinus Torvalds 380033120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = { 380133120b30SAlexey Dobriyan .owner = THIS_MODULE, 380233120b30SAlexey Dobriyan .open = ipv6_route_open, 380333120b30SAlexey Dobriyan .read = seq_read, 380433120b30SAlexey Dobriyan .llseek = seq_lseek, 38058d2ca1d7SHannes Frederic Sowa .release = seq_release_net, 380633120b30SAlexey Dobriyan }; 380733120b30SAlexey Dobriyan 38081da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 38091da177e4SLinus Torvalds { 381069ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 38111da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 381269ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 381369ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 381469ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_alloc, 381569ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 381669ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 3817fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 381869ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 38191da177e4SLinus Torvalds 38201da177e4SLinus Torvalds return 0; 38211da177e4SLinus Torvalds } 38221da177e4SLinus Torvalds 38231da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file) 38241da177e4SLinus Torvalds { 3825de05c557SPavel Emelyanov return single_open_net(inode, file, rt6_stats_seq_show); 382669ddb805SDaniel Lezcano } 382769ddb805SDaniel Lezcano 38289a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = { 38291da177e4SLinus Torvalds .owner = THIS_MODULE, 38301da177e4SLinus Torvalds .open = rt6_stats_seq_open, 38311da177e4SLinus Torvalds .read = seq_read, 38321da177e4SLinus Torvalds .llseek = seq_lseek, 3833b6fcbdb4SPavel Emelyanov .release = single_release_net, 38341da177e4SLinus Torvalds }; 38351da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 38361da177e4SLinus Torvalds 38371da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 38381da177e4SLinus Torvalds 38391da177e4SLinus Torvalds static 3840fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, 38411da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 38421da177e4SLinus Torvalds { 3843c486da34SLucian Adrian Grijincu struct net *net; 3844c486da34SLucian Adrian Grijincu int delay; 3845c486da34SLucian Adrian Grijincu if (!write) 3846c486da34SLucian Adrian Grijincu return -EINVAL; 3847c486da34SLucian Adrian Grijincu 3848c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 3849c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 38508d65af78SAlexey Dobriyan proc_dointvec(ctl, write, buffer, lenp, ppos); 38512ac3ac8fSMichal Kubeček fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0); 38521da177e4SLinus Torvalds return 0; 38531da177e4SLinus Torvalds } 38541da177e4SLinus Torvalds 3855fe2c6338SJoe Perches struct ctl_table ipv6_route_table_template[] = { 38561da177e4SLinus Torvalds { 38571da177e4SLinus Torvalds .procname = "flush", 38584990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 38591da177e4SLinus Torvalds .maxlen = sizeof(int), 386089c8b3a1SDave Jones .mode = 0200, 38616d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 38621da177e4SLinus Torvalds }, 38631da177e4SLinus Torvalds { 38641da177e4SLinus Torvalds .procname = "gc_thresh", 38659a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 38661da177e4SLinus Torvalds .maxlen = sizeof(int), 38671da177e4SLinus Torvalds .mode = 0644, 38686d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 38691da177e4SLinus Torvalds }, 38701da177e4SLinus Torvalds { 38711da177e4SLinus Torvalds .procname = "max_size", 38724990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 38731da177e4SLinus Torvalds .maxlen = sizeof(int), 38741da177e4SLinus Torvalds .mode = 0644, 38756d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 38761da177e4SLinus Torvalds }, 38771da177e4SLinus Torvalds { 38781da177e4SLinus Torvalds .procname = "gc_min_interval", 38794990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 38801da177e4SLinus Torvalds .maxlen = sizeof(int), 38811da177e4SLinus Torvalds .mode = 0644, 38826d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 38831da177e4SLinus Torvalds }, 38841da177e4SLinus Torvalds { 38851da177e4SLinus Torvalds .procname = "gc_timeout", 38864990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 38871da177e4SLinus Torvalds .maxlen = sizeof(int), 38881da177e4SLinus Torvalds .mode = 0644, 38896d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 38901da177e4SLinus Torvalds }, 38911da177e4SLinus Torvalds { 38921da177e4SLinus Torvalds .procname = "gc_interval", 38934990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 38941da177e4SLinus Torvalds .maxlen = sizeof(int), 38951da177e4SLinus Torvalds .mode = 0644, 38966d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 38971da177e4SLinus Torvalds }, 38981da177e4SLinus Torvalds { 38991da177e4SLinus Torvalds .procname = "gc_elasticity", 39004990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 39011da177e4SLinus Torvalds .maxlen = sizeof(int), 39021da177e4SLinus Torvalds .mode = 0644, 3903f3d3f616SMin Zhang .proc_handler = proc_dointvec, 39041da177e4SLinus Torvalds }, 39051da177e4SLinus Torvalds { 39061da177e4SLinus Torvalds .procname = "mtu_expires", 39074990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 39081da177e4SLinus Torvalds .maxlen = sizeof(int), 39091da177e4SLinus Torvalds .mode = 0644, 39106d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 39111da177e4SLinus Torvalds }, 39121da177e4SLinus Torvalds { 39131da177e4SLinus Torvalds .procname = "min_adv_mss", 39144990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 39151da177e4SLinus Torvalds .maxlen = sizeof(int), 39161da177e4SLinus Torvalds .mode = 0644, 3917f3d3f616SMin Zhang .proc_handler = proc_dointvec, 39181da177e4SLinus Torvalds }, 39191da177e4SLinus Torvalds { 39201da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 39214990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 39221da177e4SLinus Torvalds .maxlen = sizeof(int), 39231da177e4SLinus Torvalds .mode = 0644, 39246d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 39251da177e4SLinus Torvalds }, 3926f8572d8fSEric W. Biederman { } 39271da177e4SLinus Torvalds }; 39281da177e4SLinus Torvalds 39292c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 3930760f2d01SDaniel Lezcano { 3931760f2d01SDaniel Lezcano struct ctl_table *table; 3932760f2d01SDaniel Lezcano 3933760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 3934760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 3935760f2d01SDaniel Lezcano GFP_KERNEL); 39365ee09105SYOSHIFUJI Hideaki 39375ee09105SYOSHIFUJI Hideaki if (table) { 39385ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 3939c486da34SLucian Adrian Grijincu table[0].extra1 = net; 394086393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 39415ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 39425ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 39435ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 39445ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 39455ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 39465ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 39475ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 39489c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 3949464dc801SEric W. Biederman 3950464dc801SEric W. Biederman /* Don't export sysctls to unprivileged users */ 3951464dc801SEric W. Biederman if (net->user_ns != &init_user_ns) 3952464dc801SEric W. Biederman table[0].procname = NULL; 39535ee09105SYOSHIFUJI Hideaki } 39545ee09105SYOSHIFUJI Hideaki 3955760f2d01SDaniel Lezcano return table; 3956760f2d01SDaniel Lezcano } 39571da177e4SLinus Torvalds #endif 39581da177e4SLinus Torvalds 39592c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 3960cdb18761SDaniel Lezcano { 3961633d424bSPavel Emelyanov int ret = -ENOMEM; 39628ed67789SDaniel Lezcano 396386393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 396486393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 3965f2fc6a54SBenjamin Thery 3966fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 3967fc66f95cSEric Dumazet goto out_ip6_dst_ops; 3968fc66f95cSEric Dumazet 39698ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 39708ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 39718ed67789SDaniel Lezcano GFP_KERNEL); 39728ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 3973fc66f95cSEric Dumazet goto out_ip6_dst_entries; 3974d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.path = 39758ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_null_entry; 3976d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 397762fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 397862fa8a84SDavid S. Miller ip6_template_metrics, true); 39798ed67789SDaniel Lezcano 39808ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 39818ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 39828ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 39838ed67789SDaniel Lezcano GFP_KERNEL); 398468fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 398568fffc67SPeter Zijlstra goto out_ip6_null_entry; 3986d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.path = 39878ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_prohibit_entry; 3988d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 398962fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 399062fa8a84SDavid S. Miller ip6_template_metrics, true); 39918ed67789SDaniel Lezcano 39928ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 39938ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 39948ed67789SDaniel Lezcano GFP_KERNEL); 399568fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 399668fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 3997d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.path = 39988ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_blk_hole_entry; 3999d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 400062fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 400162fa8a84SDavid S. Miller ip6_template_metrics, true); 40028ed67789SDaniel Lezcano #endif 40038ed67789SDaniel Lezcano 4004b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 4005b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 4006b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 4007b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 4008b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 4009b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 4010b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 4011b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 4012b339a47cSPeter Zijlstra 40136891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 40146891a346SBenjamin Thery 40158ed67789SDaniel Lezcano ret = 0; 40168ed67789SDaniel Lezcano out: 40178ed67789SDaniel Lezcano return ret; 4018f2fc6a54SBenjamin Thery 401968fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 402068fffc67SPeter Zijlstra out_ip6_prohibit_entry: 402168fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 402268fffc67SPeter Zijlstra out_ip6_null_entry: 402368fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 402468fffc67SPeter Zijlstra #endif 4025fc66f95cSEric Dumazet out_ip6_dst_entries: 4026fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 4027f2fc6a54SBenjamin Thery out_ip6_dst_ops: 4028f2fc6a54SBenjamin Thery goto out; 4029cdb18761SDaniel Lezcano } 4030cdb18761SDaniel Lezcano 40312c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 4032cdb18761SDaniel Lezcano { 40338ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 40348ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 40358ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 40368ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 40378ed67789SDaniel Lezcano #endif 403841bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 4039cdb18761SDaniel Lezcano } 4040cdb18761SDaniel Lezcano 4041d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net) 4042d189634eSThomas Graf { 4043d189634eSThomas Graf #ifdef CONFIG_PROC_FS 4044d4beaa66SGao feng proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops); 4045d4beaa66SGao feng proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops); 4046d189634eSThomas Graf #endif 4047d189634eSThomas Graf return 0; 4048d189634eSThomas Graf } 4049d189634eSThomas Graf 4050d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net) 4051d189634eSThomas Graf { 4052d189634eSThomas Graf #ifdef CONFIG_PROC_FS 4053ece31ffdSGao feng remove_proc_entry("ipv6_route", net->proc_net); 4054ece31ffdSGao feng remove_proc_entry("rt6_stats", net->proc_net); 4055d189634eSThomas Graf #endif 4056d189634eSThomas Graf } 4057d189634eSThomas Graf 4058cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 4059cdb18761SDaniel Lezcano .init = ip6_route_net_init, 4060cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 4061cdb18761SDaniel Lezcano }; 4062cdb18761SDaniel Lezcano 4063c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net) 4064c3426b47SDavid S. Miller { 4065c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 4066c3426b47SDavid S. Miller 4067c3426b47SDavid S. Miller if (!bp) 4068c3426b47SDavid S. Miller return -ENOMEM; 4069c3426b47SDavid S. Miller inet_peer_base_init(bp); 4070c3426b47SDavid S. Miller net->ipv6.peers = bp; 4071c3426b47SDavid S. Miller return 0; 4072c3426b47SDavid S. Miller } 4073c3426b47SDavid S. Miller 4074c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net) 4075c3426b47SDavid S. Miller { 4076c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv6.peers; 4077c3426b47SDavid S. Miller 4078c3426b47SDavid S. Miller net->ipv6.peers = NULL; 407956a6b248SDavid S. Miller inetpeer_invalidate_tree(bp); 4080c3426b47SDavid S. Miller kfree(bp); 4081c3426b47SDavid S. Miller } 4082c3426b47SDavid S. Miller 40832b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = { 4084c3426b47SDavid S. Miller .init = ipv6_inetpeer_init, 4085c3426b47SDavid S. Miller .exit = ipv6_inetpeer_exit, 4086c3426b47SDavid S. Miller }; 4087c3426b47SDavid S. Miller 4088d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = { 4089d189634eSThomas Graf .init = ip6_route_net_init_late, 4090d189634eSThomas Graf .exit = ip6_route_net_exit_late, 4091d189634eSThomas Graf }; 4092d189634eSThomas Graf 40938ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 40948ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 4095242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY - 10, 40968ed67789SDaniel Lezcano }; 40978ed67789SDaniel Lezcano 40982f460933SWANG Cong void __init ip6_route_init_special_entries(void) 40992f460933SWANG Cong { 41002f460933SWANG Cong /* Registering of the loopback is done before this portion of code, 41012f460933SWANG Cong * the loopback reference in rt6_info will not be taken, do it 41022f460933SWANG Cong * manually for init_net */ 41032f460933SWANG Cong init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 41042f460933SWANG Cong init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 41052f460933SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 41062f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 41072f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 41082f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 41092f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 41102f460933SWANG Cong #endif 41112f460933SWANG Cong } 41122f460933SWANG Cong 4113433d49c3SDaniel Lezcano int __init ip6_route_init(void) 41141da177e4SLinus Torvalds { 4115433d49c3SDaniel Lezcano int ret; 41168d0b94afSMartin KaFai Lau int cpu; 4117433d49c3SDaniel Lezcano 41189a7ec3a9SDaniel Lezcano ret = -ENOMEM; 41199a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 41209a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 41219a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 41229a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 4123c19a28e1SFernando Carrijo goto out; 412414e50e57SDavid S. Miller 4125fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 41268ed67789SDaniel Lezcano if (ret) 4127bdb3289fSDaniel Lezcano goto out_kmem_cache; 4128bdb3289fSDaniel Lezcano 4129c3426b47SDavid S. Miller ret = register_pernet_subsys(&ipv6_inetpeer_ops); 4130c3426b47SDavid S. Miller if (ret) 4131e8803b6cSDavid S. Miller goto out_dst_entries; 41322a0c451aSThomas Graf 41337e52b33bSDavid S. Miller ret = register_pernet_subsys(&ip6_route_net_ops); 41347e52b33bSDavid S. Miller if (ret) 41357e52b33bSDavid S. Miller goto out_register_inetpeer; 4136c3426b47SDavid S. Miller 41375dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 41385dc121e9SArnaud Ebalard 4139e8803b6cSDavid S. Miller ret = fib6_init(); 4140433d49c3SDaniel Lezcano if (ret) 41418ed67789SDaniel Lezcano goto out_register_subsys; 4142433d49c3SDaniel Lezcano 4143433d49c3SDaniel Lezcano ret = xfrm6_init(); 4144433d49c3SDaniel Lezcano if (ret) 4145e8803b6cSDavid S. Miller goto out_fib6_init; 4146c35b7e72SDaniel Lezcano 4147433d49c3SDaniel Lezcano ret = fib6_rules_init(); 4148433d49c3SDaniel Lezcano if (ret) 4149433d49c3SDaniel Lezcano goto xfrm6_init; 41507e5449c2SDaniel Lezcano 4151d189634eSThomas Graf ret = register_pernet_subsys(&ip6_route_net_late_ops); 4152d189634eSThomas Graf if (ret) 4153d189634eSThomas Graf goto fib6_rules_init; 4154d189634eSThomas Graf 4155433d49c3SDaniel Lezcano ret = -ENOBUFS; 4156c7ac8679SGreg Rose if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) || 4157c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) || 4158c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL)) 4159d189634eSThomas Graf goto out_register_late_subsys; 4160433d49c3SDaniel Lezcano 41618ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 4162cdb18761SDaniel Lezcano if (ret) 4163d189634eSThomas Graf goto out_register_late_subsys; 41648ed67789SDaniel Lezcano 41658d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 41668d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 41678d0b94afSMartin KaFai Lau 41688d0b94afSMartin KaFai Lau INIT_LIST_HEAD(&ul->head); 41698d0b94afSMartin KaFai Lau spin_lock_init(&ul->lock); 41708d0b94afSMartin KaFai Lau } 41718d0b94afSMartin KaFai Lau 4172433d49c3SDaniel Lezcano out: 4173433d49c3SDaniel Lezcano return ret; 4174433d49c3SDaniel Lezcano 4175d189634eSThomas Graf out_register_late_subsys: 4176d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 4177433d49c3SDaniel Lezcano fib6_rules_init: 4178433d49c3SDaniel Lezcano fib6_rules_cleanup(); 4179433d49c3SDaniel Lezcano xfrm6_init: 4180433d49c3SDaniel Lezcano xfrm6_fini(); 41812a0c451aSThomas Graf out_fib6_init: 41822a0c451aSThomas Graf fib6_gc_cleanup(); 41838ed67789SDaniel Lezcano out_register_subsys: 41848ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 41857e52b33bSDavid S. Miller out_register_inetpeer: 41867e52b33bSDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 4187fc66f95cSEric Dumazet out_dst_entries: 4188fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 4189433d49c3SDaniel Lezcano out_kmem_cache: 4190f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 4191433d49c3SDaniel Lezcano goto out; 41921da177e4SLinus Torvalds } 41931da177e4SLinus Torvalds 41941da177e4SLinus Torvalds void ip6_route_cleanup(void) 41951da177e4SLinus Torvalds { 41968ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 4197d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 4198101367c2SThomas Graf fib6_rules_cleanup(); 41991da177e4SLinus Torvalds xfrm6_fini(); 42001da177e4SLinus Torvalds fib6_gc_cleanup(); 4201c3426b47SDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 42028ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 420341bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 4204f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 42051da177e4SLinus Torvalds } 4206