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, 3571cfb71eeSWei Wang 1, DST_OBSOLETE_FORCE_CHK, flags); 358cf911662SDavid S. Miller 359ebfa45f0SMartin KaFai Lau if (rt) 360ebfa45f0SMartin KaFai Lau rt6_info_init(rt); 3618104891bSSteffen Klassert 362cf911662SDavid S. Miller return rt; 3631da177e4SLinus Torvalds } 3641da177e4SLinus Torvalds 3659ab179d8SDavid Ahern struct rt6_info *ip6_dst_alloc(struct net *net, 366d52d3997SMartin KaFai Lau struct net_device *dev, 367ad706862SMartin KaFai Lau int flags) 368d52d3997SMartin KaFai Lau { 369ad706862SMartin KaFai Lau struct rt6_info *rt = __ip6_dst_alloc(net, dev, flags); 370d52d3997SMartin KaFai Lau 371d52d3997SMartin KaFai Lau if (rt) { 372d52d3997SMartin KaFai Lau rt->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, GFP_ATOMIC); 373d52d3997SMartin KaFai Lau if (rt->rt6i_pcpu) { 374d52d3997SMartin KaFai Lau int cpu; 375d52d3997SMartin KaFai Lau 376d52d3997SMartin KaFai Lau for_each_possible_cpu(cpu) { 377d52d3997SMartin KaFai Lau struct rt6_info **p; 378d52d3997SMartin KaFai Lau 379d52d3997SMartin KaFai Lau p = per_cpu_ptr(rt->rt6i_pcpu, cpu); 380d52d3997SMartin KaFai Lau /* no one shares rt */ 381d52d3997SMartin KaFai Lau *p = NULL; 382d52d3997SMartin KaFai Lau } 383d52d3997SMartin KaFai Lau } else { 3841cfb71eeSWei Wang dst_release(&rt->dst); 3851cfb71eeSWei Wang if (!(flags & DST_NOCACHE)) 386d52d3997SMartin KaFai Lau dst_destroy((struct dst_entry *)rt); 387d52d3997SMartin KaFai Lau return NULL; 388d52d3997SMartin KaFai Lau } 389d52d3997SMartin KaFai Lau } 390d52d3997SMartin KaFai Lau 391d52d3997SMartin KaFai Lau return rt; 392d52d3997SMartin KaFai Lau } 3939ab179d8SDavid Ahern EXPORT_SYMBOL(ip6_dst_alloc); 394d52d3997SMartin KaFai Lau 3951da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst) 3961da177e4SLinus Torvalds { 3971da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 398ecd98837SYOSHIFUJI Hideaki / 吉藤英明 struct dst_entry *from = dst->from; 3998d0b94afSMartin KaFai Lau struct inet6_dev *idev; 4001da177e4SLinus Torvalds 4018e2ec639SYan, Zheng dst_destroy_metrics_generic(dst); 402d52d3997SMartin KaFai Lau free_percpu(rt->rt6i_pcpu); 4038d0b94afSMartin KaFai Lau rt6_uncached_list_del(rt); 4048d0b94afSMartin KaFai Lau 4058d0b94afSMartin KaFai Lau idev = rt->rt6i_idev; 40638308473SDavid S. Miller if (idev) { 4071da177e4SLinus Torvalds rt->rt6i_idev = NULL; 4081da177e4SLinus Torvalds in6_dev_put(idev); 4091da177e4SLinus Torvalds } 4101716a961SGao feng 411ecd98837SYOSHIFUJI Hideaki / 吉藤英明 dst->from = NULL; 412ecd98837SYOSHIFUJI Hideaki / 吉藤英明 dst_release(from); 413b3419363SDavid S. Miller } 414b3419363SDavid S. Miller 4151da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 4161da177e4SLinus Torvalds int how) 4171da177e4SLinus Torvalds { 4181da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 4191da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 4205a3e55d6SDenis V. Lunev struct net_device *loopback_dev = 421c346dca1SYOSHIFUJI Hideaki dev_net(dev)->loopback_dev; 4221da177e4SLinus Torvalds 42397cac082SDavid S. Miller if (dev != loopback_dev) { 42497cac082SDavid S. Miller if (idev && idev->dev == dev) { 4255a3e55d6SDenis V. Lunev struct inet6_dev *loopback_idev = 4265a3e55d6SDenis V. Lunev in6_dev_get(loopback_dev); 42738308473SDavid S. Miller if (loopback_idev) { 4281da177e4SLinus Torvalds rt->rt6i_idev = loopback_idev; 4291da177e4SLinus Torvalds in6_dev_put(idev); 4301da177e4SLinus Torvalds } 4311da177e4SLinus Torvalds } 43297cac082SDavid S. Miller } 4331da177e4SLinus Torvalds } 4341da177e4SLinus Torvalds 4355973fb1eSMartin KaFai Lau static bool __rt6_check_expired(const struct rt6_info *rt) 4365973fb1eSMartin KaFai Lau { 4375973fb1eSMartin KaFai Lau if (rt->rt6i_flags & RTF_EXPIRES) 4385973fb1eSMartin KaFai Lau return time_after(jiffies, rt->dst.expires); 4395973fb1eSMartin KaFai Lau else 4405973fb1eSMartin KaFai Lau return false; 4415973fb1eSMartin KaFai Lau } 4425973fb1eSMartin KaFai Lau 443a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt) 4441da177e4SLinus Torvalds { 4451716a961SGao feng if (rt->rt6i_flags & RTF_EXPIRES) { 4461716a961SGao feng if (time_after(jiffies, rt->dst.expires)) 447a50feda5SEric Dumazet return true; 4481716a961SGao feng } else if (rt->dst.from) { 4493fd91fb3SLi RongQing return rt6_check_expired((struct rt6_info *) rt->dst.from); 4501716a961SGao feng } 451a50feda5SEric Dumazet return false; 4521da177e4SLinus Torvalds } 4531da177e4SLinus Torvalds 45451ebd318SNicolas Dichtel /* Multipath route selection: 45551ebd318SNicolas Dichtel * Hash based function using packet header and flowlabel. 45651ebd318SNicolas Dichtel * Adapted from fib_info_hashfn() 45751ebd318SNicolas Dichtel */ 45851ebd318SNicolas Dichtel static int rt6_info_hash_nhsfn(unsigned int candidate_count, 45951ebd318SNicolas Dichtel const struct flowi6 *fl6) 46051ebd318SNicolas Dichtel { 461644d0e65STom Herbert return get_hash_from_flowi6(fl6) % candidate_count; 46251ebd318SNicolas Dichtel } 46351ebd318SNicolas Dichtel 46451ebd318SNicolas Dichtel static struct rt6_info *rt6_multipath_select(struct rt6_info *match, 46552bd4c0cSNicolas Dichtel struct flowi6 *fl6, int oif, 46652bd4c0cSNicolas Dichtel int strict) 46751ebd318SNicolas Dichtel { 46851ebd318SNicolas Dichtel struct rt6_info *sibling, *next_sibling; 46951ebd318SNicolas Dichtel int route_choosen; 47051ebd318SNicolas Dichtel 47151ebd318SNicolas Dichtel route_choosen = rt6_info_hash_nhsfn(match->rt6i_nsiblings + 1, fl6); 47251ebd318SNicolas Dichtel /* Don't change the route, if route_choosen == 0 47351ebd318SNicolas Dichtel * (siblings does not include ourself) 47451ebd318SNicolas Dichtel */ 47551ebd318SNicolas Dichtel if (route_choosen) 47651ebd318SNicolas Dichtel list_for_each_entry_safe(sibling, next_sibling, 47751ebd318SNicolas Dichtel &match->rt6i_siblings, rt6i_siblings) { 47851ebd318SNicolas Dichtel route_choosen--; 47951ebd318SNicolas Dichtel if (route_choosen == 0) { 48052bd4c0cSNicolas Dichtel if (rt6_score_route(sibling, oif, strict) < 0) 48152bd4c0cSNicolas Dichtel break; 48251ebd318SNicolas Dichtel match = sibling; 48351ebd318SNicolas Dichtel break; 48451ebd318SNicolas Dichtel } 48551ebd318SNicolas Dichtel } 48651ebd318SNicolas Dichtel return match; 48751ebd318SNicolas Dichtel } 48851ebd318SNicolas Dichtel 4891da177e4SLinus Torvalds /* 490c71099acSThomas Graf * Route lookup. Any table->tb6_lock is implied. 4911da177e4SLinus Torvalds */ 4921da177e4SLinus Torvalds 4938ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net, 4948ed67789SDaniel Lezcano struct rt6_info *rt, 495b71d1d42SEric Dumazet const struct in6_addr *saddr, 4961da177e4SLinus Torvalds int oif, 497d420895eSYOSHIFUJI Hideaki int flags) 4981da177e4SLinus Torvalds { 4991da177e4SLinus Torvalds struct rt6_info *local = NULL; 5001da177e4SLinus Torvalds struct rt6_info *sprt; 5011da177e4SLinus Torvalds 502dd3abc4eSYOSHIFUJI Hideaki if (!oif && ipv6_addr_any(saddr)) 503dd3abc4eSYOSHIFUJI Hideaki goto out; 504dd3abc4eSYOSHIFUJI Hideaki 505d8d1f30bSChangli Gao for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) { 506d1918542SDavid S. Miller struct net_device *dev = sprt->dst.dev; 507dd3abc4eSYOSHIFUJI Hideaki 508dd3abc4eSYOSHIFUJI Hideaki if (oif) { 5091da177e4SLinus Torvalds if (dev->ifindex == oif) 5101da177e4SLinus Torvalds return sprt; 5111da177e4SLinus Torvalds if (dev->flags & IFF_LOOPBACK) { 51238308473SDavid S. Miller if (!sprt->rt6i_idev || 5131da177e4SLinus Torvalds sprt->rt6i_idev->dev->ifindex != oif) { 51417fb0b2bSDavid Ahern if (flags & RT6_LOOKUP_F_IFACE) 5151da177e4SLinus Torvalds continue; 51617fb0b2bSDavid Ahern if (local && 51717fb0b2bSDavid Ahern local->rt6i_idev->dev->ifindex == oif) 5181da177e4SLinus Torvalds continue; 5191da177e4SLinus Torvalds } 5201da177e4SLinus Torvalds local = sprt; 5211da177e4SLinus Torvalds } 522dd3abc4eSYOSHIFUJI Hideaki } else { 523dd3abc4eSYOSHIFUJI Hideaki if (ipv6_chk_addr(net, saddr, dev, 524dd3abc4eSYOSHIFUJI Hideaki flags & RT6_LOOKUP_F_IFACE)) 525dd3abc4eSYOSHIFUJI Hideaki return sprt; 526dd3abc4eSYOSHIFUJI Hideaki } 5271da177e4SLinus Torvalds } 5281da177e4SLinus Torvalds 529dd3abc4eSYOSHIFUJI Hideaki if (oif) { 5301da177e4SLinus Torvalds if (local) 5311da177e4SLinus Torvalds return local; 5321da177e4SLinus Torvalds 533d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE) 5348ed67789SDaniel Lezcano return net->ipv6.ip6_null_entry; 5351da177e4SLinus Torvalds } 536dd3abc4eSYOSHIFUJI Hideaki out: 5371da177e4SLinus Torvalds return rt; 5381da177e4SLinus Torvalds } 5391da177e4SLinus Torvalds 54027097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 541c2f17e82SHannes Frederic Sowa struct __rt6_probe_work { 542c2f17e82SHannes Frederic Sowa struct work_struct work; 543c2f17e82SHannes Frederic Sowa struct in6_addr target; 544c2f17e82SHannes Frederic Sowa struct net_device *dev; 545c2f17e82SHannes Frederic Sowa }; 546c2f17e82SHannes Frederic Sowa 547c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w) 548c2f17e82SHannes Frederic Sowa { 549c2f17e82SHannes Frederic Sowa struct in6_addr mcaddr; 550c2f17e82SHannes Frederic Sowa struct __rt6_probe_work *work = 551c2f17e82SHannes Frederic Sowa container_of(w, struct __rt6_probe_work, work); 552c2f17e82SHannes Frederic Sowa 553c2f17e82SHannes Frederic Sowa addrconf_addr_solict_mult(&work->target, &mcaddr); 554adc176c5SErik Nordmark ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0); 555c2f17e82SHannes Frederic Sowa dev_put(work->dev); 556662f5533SMichael Büsch kfree(work); 557c2f17e82SHannes Frederic Sowa } 558c2f17e82SHannes Frederic Sowa 55927097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt) 56027097255SYOSHIFUJI Hideaki { 561990edb42SMartin KaFai Lau struct __rt6_probe_work *work; 562f2c31e32SEric Dumazet struct neighbour *neigh; 56327097255SYOSHIFUJI Hideaki /* 56427097255SYOSHIFUJI Hideaki * Okay, this does not seem to be appropriate 56527097255SYOSHIFUJI Hideaki * for now, however, we need to check if it 56627097255SYOSHIFUJI Hideaki * is really so; aka Router Reachability Probing. 56727097255SYOSHIFUJI Hideaki * 56827097255SYOSHIFUJI Hideaki * Router Reachability Probe MUST be rate-limited 56927097255SYOSHIFUJI Hideaki * to no more than one per minute. 57027097255SYOSHIFUJI Hideaki */ 5712152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (!rt || !(rt->rt6i_flags & RTF_GATEWAY)) 572fdd6681dSAmerigo Wang return; 5732152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 5742152caeaSYOSHIFUJI Hideaki / 吉藤英明 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 5752152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 5768d6c31bfSMartin KaFai Lau if (neigh->nud_state & NUD_VALID) 5778d6c31bfSMartin KaFai Lau goto out; 5788d6c31bfSMartin KaFai Lau 579990edb42SMartin KaFai Lau work = NULL; 5802152caeaSYOSHIFUJI Hideaki / 吉藤英明 write_lock(&neigh->lock); 581990edb42SMartin KaFai Lau if (!(neigh->nud_state & NUD_VALID) && 582990edb42SMartin KaFai Lau time_after(jiffies, 583990edb42SMartin KaFai Lau neigh->updated + 584990edb42SMartin KaFai Lau rt->rt6i_idev->cnf.rtr_probe_interval)) { 585c2f17e82SHannes Frederic Sowa work = kmalloc(sizeof(*work), GFP_ATOMIC); 586990edb42SMartin KaFai Lau if (work) 5877e980569SJiri Benc __neigh_set_probe_once(neigh); 588990edb42SMartin KaFai Lau } 589c2f17e82SHannes Frederic Sowa write_unlock(&neigh->lock); 590990edb42SMartin KaFai Lau } else { 591990edb42SMartin KaFai Lau work = kmalloc(sizeof(*work), GFP_ATOMIC); 592990edb42SMartin KaFai Lau } 593c2f17e82SHannes Frederic Sowa 594c2f17e82SHannes Frederic Sowa if (work) { 595c2f17e82SHannes Frederic Sowa INIT_WORK(&work->work, rt6_probe_deferred); 596c2f17e82SHannes Frederic Sowa work->target = rt->rt6i_gateway; 597c2f17e82SHannes Frederic Sowa dev_hold(rt->dst.dev); 598c2f17e82SHannes Frederic Sowa work->dev = rt->dst.dev; 599c2f17e82SHannes Frederic Sowa schedule_work(&work->work); 600c2f17e82SHannes Frederic Sowa } 601990edb42SMartin KaFai Lau 6028d6c31bfSMartin KaFai Lau out: 6032152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 604f2c31e32SEric Dumazet } 60527097255SYOSHIFUJI Hideaki #else 60627097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt) 60727097255SYOSHIFUJI Hideaki { 60827097255SYOSHIFUJI Hideaki } 60927097255SYOSHIFUJI Hideaki #endif 61027097255SYOSHIFUJI Hideaki 6111da177e4SLinus Torvalds /* 612554cfb7eSYOSHIFUJI Hideaki * Default Router Selection (RFC 2461 6.3.6) 6131da177e4SLinus Torvalds */ 614b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif) 6151da177e4SLinus Torvalds { 616d1918542SDavid S. Miller struct net_device *dev = rt->dst.dev; 617161980f4SDavid S. Miller if (!oif || dev->ifindex == oif) 618554cfb7eSYOSHIFUJI Hideaki return 2; 619161980f4SDavid S. Miller if ((dev->flags & IFF_LOOPBACK) && 620161980f4SDavid S. Miller rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif) 621161980f4SDavid S. Miller return 1; 622554cfb7eSYOSHIFUJI Hideaki return 0; 6231da177e4SLinus Torvalds } 6241da177e4SLinus Torvalds 625afc154e9SHannes Frederic Sowa static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt) 6261da177e4SLinus Torvalds { 627f2c31e32SEric Dumazet struct neighbour *neigh; 628afc154e9SHannes Frederic Sowa enum rt6_nud_state ret = RT6_NUD_FAIL_HARD; 629f2c31e32SEric Dumazet 6304d0c5911SYOSHIFUJI Hideaki if (rt->rt6i_flags & RTF_NONEXTHOP || 6314d0c5911SYOSHIFUJI Hideaki !(rt->rt6i_flags & RTF_GATEWAY)) 632afc154e9SHannes Frederic Sowa return RT6_NUD_SUCCEED; 633145a3621SYOSHIFUJI Hideaki / 吉藤英明 634145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 635145a3621SYOSHIFUJI Hideaki / 吉藤英明 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 636145a3621SYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 637145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_lock(&neigh->lock); 638554cfb7eSYOSHIFUJI Hideaki if (neigh->nud_state & NUD_VALID) 639afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 640398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 641a5a81f0bSPaul Marks else if (!(neigh->nud_state & NUD_FAILED)) 642afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 6437e980569SJiri Benc else 6447e980569SJiri Benc ret = RT6_NUD_FAIL_PROBE; 645398bcbebSYOSHIFUJI Hideaki #endif 646145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_unlock(&neigh->lock); 647afc154e9SHannes Frederic Sowa } else { 648afc154e9SHannes Frederic Sowa ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ? 6497e980569SJiri Benc RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR; 650a5a81f0bSPaul Marks } 651145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 652145a3621SYOSHIFUJI Hideaki / 吉藤英明 653a5a81f0bSPaul Marks return ret; 6541da177e4SLinus Torvalds } 6551da177e4SLinus Torvalds 656554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif, 657554cfb7eSYOSHIFUJI Hideaki int strict) 658554cfb7eSYOSHIFUJI Hideaki { 659a5a81f0bSPaul Marks int m; 6604d0c5911SYOSHIFUJI Hideaki 6614d0c5911SYOSHIFUJI Hideaki m = rt6_check_dev(rt, oif); 66277d16f45SYOSHIFUJI Hideaki if (!m && (strict & RT6_LOOKUP_F_IFACE)) 663afc154e9SHannes Frederic Sowa return RT6_NUD_FAIL_HARD; 664ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 665ebacaaa0SYOSHIFUJI Hideaki m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2; 666ebacaaa0SYOSHIFUJI Hideaki #endif 667afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) { 668afc154e9SHannes Frederic Sowa int n = rt6_check_neigh(rt); 669afc154e9SHannes Frederic Sowa if (n < 0) 670afc154e9SHannes Frederic Sowa return n; 671afc154e9SHannes Frederic Sowa } 672554cfb7eSYOSHIFUJI Hideaki return m; 673554cfb7eSYOSHIFUJI Hideaki } 674554cfb7eSYOSHIFUJI Hideaki 675f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict, 676afc154e9SHannes Frederic Sowa int *mpri, struct rt6_info *match, 677afc154e9SHannes Frederic Sowa bool *do_rr) 678554cfb7eSYOSHIFUJI Hideaki { 679554cfb7eSYOSHIFUJI Hideaki int m; 680afc154e9SHannes Frederic Sowa bool match_do_rr = false; 68135103d11SAndy Gospodarek struct inet6_dev *idev = rt->rt6i_idev; 68235103d11SAndy Gospodarek struct net_device *dev = rt->dst.dev; 68335103d11SAndy Gospodarek 68435103d11SAndy Gospodarek if (dev && !netif_carrier_ok(dev) && 685d5d32e4bSDavid Ahern idev->cnf.ignore_routes_with_linkdown && 686d5d32e4bSDavid Ahern !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE)) 68735103d11SAndy Gospodarek goto out; 688554cfb7eSYOSHIFUJI Hideaki 689554cfb7eSYOSHIFUJI Hideaki if (rt6_check_expired(rt)) 690f11e6659SDavid S. Miller goto out; 691554cfb7eSYOSHIFUJI Hideaki 692554cfb7eSYOSHIFUJI Hideaki m = rt6_score_route(rt, oif, strict); 6937e980569SJiri Benc if (m == RT6_NUD_FAIL_DO_RR) { 694afc154e9SHannes Frederic Sowa match_do_rr = true; 695afc154e9SHannes Frederic Sowa m = 0; /* lowest valid score */ 6967e980569SJiri Benc } else if (m == RT6_NUD_FAIL_HARD) { 697f11e6659SDavid S. Miller goto out; 6981da177e4SLinus Torvalds } 699f11e6659SDavid S. Miller 700afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) 701afc154e9SHannes Frederic Sowa rt6_probe(rt); 702afc154e9SHannes Frederic Sowa 7037e980569SJiri Benc /* note that m can be RT6_NUD_FAIL_PROBE at this point */ 704afc154e9SHannes Frederic Sowa if (m > *mpri) { 705afc154e9SHannes Frederic Sowa *do_rr = match_do_rr; 706afc154e9SHannes Frederic Sowa *mpri = m; 707afc154e9SHannes Frederic Sowa match = rt; 708afc154e9SHannes Frederic Sowa } 709f11e6659SDavid S. Miller out: 710f11e6659SDavid S. Miller return match; 7111da177e4SLinus Torvalds } 7121da177e4SLinus Torvalds 713f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn, 714f11e6659SDavid S. Miller struct rt6_info *rr_head, 715afc154e9SHannes Frederic Sowa u32 metric, int oif, int strict, 716afc154e9SHannes Frederic Sowa bool *do_rr) 717f11e6659SDavid S. Miller { 7189fbdcfafSSteffen Klassert struct rt6_info *rt, *match, *cont; 719f11e6659SDavid S. Miller int mpri = -1; 720f11e6659SDavid S. Miller 721f11e6659SDavid S. Miller match = NULL; 7229fbdcfafSSteffen Klassert cont = NULL; 7239fbdcfafSSteffen Klassert for (rt = rr_head; rt; rt = rt->dst.rt6_next) { 7249fbdcfafSSteffen Klassert if (rt->rt6i_metric != metric) { 7259fbdcfafSSteffen Klassert cont = rt; 7269fbdcfafSSteffen Klassert break; 7279fbdcfafSSteffen Klassert } 7289fbdcfafSSteffen Klassert 729afc154e9SHannes Frederic Sowa match = find_match(rt, oif, strict, &mpri, match, do_rr); 7309fbdcfafSSteffen Klassert } 7319fbdcfafSSteffen Klassert 7329fbdcfafSSteffen Klassert for (rt = fn->leaf; rt && rt != rr_head; rt = rt->dst.rt6_next) { 7339fbdcfafSSteffen Klassert if (rt->rt6i_metric != metric) { 7349fbdcfafSSteffen Klassert cont = rt; 7359fbdcfafSSteffen Klassert break; 7369fbdcfafSSteffen Klassert } 7379fbdcfafSSteffen Klassert 7389fbdcfafSSteffen Klassert match = find_match(rt, oif, strict, &mpri, match, do_rr); 7399fbdcfafSSteffen Klassert } 7409fbdcfafSSteffen Klassert 7419fbdcfafSSteffen Klassert if (match || !cont) 7429fbdcfafSSteffen Klassert return match; 7439fbdcfafSSteffen Klassert 7449fbdcfafSSteffen Klassert for (rt = cont; rt; rt = rt->dst.rt6_next) 745afc154e9SHannes Frederic Sowa match = find_match(rt, oif, strict, &mpri, match, do_rr); 746f11e6659SDavid S. Miller 747f11e6659SDavid S. Miller return match; 748f11e6659SDavid S. Miller } 749f11e6659SDavid S. Miller 750f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict) 751f11e6659SDavid S. Miller { 752f11e6659SDavid S. Miller struct rt6_info *match, *rt0; 7538ed67789SDaniel Lezcano struct net *net; 754afc154e9SHannes Frederic Sowa bool do_rr = false; 755f11e6659SDavid S. Miller 756f11e6659SDavid S. Miller rt0 = fn->rr_ptr; 757f11e6659SDavid S. Miller if (!rt0) 758f11e6659SDavid S. Miller fn->rr_ptr = rt0 = fn->leaf; 759f11e6659SDavid S. Miller 760afc154e9SHannes Frederic Sowa match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict, 761afc154e9SHannes Frederic Sowa &do_rr); 762f11e6659SDavid S. Miller 763afc154e9SHannes Frederic Sowa if (do_rr) { 764d8d1f30bSChangli Gao struct rt6_info *next = rt0->dst.rt6_next; 765f11e6659SDavid S. Miller 766554cfb7eSYOSHIFUJI Hideaki /* no entries matched; do round-robin */ 767f11e6659SDavid S. Miller if (!next || next->rt6i_metric != rt0->rt6i_metric) 768f11e6659SDavid S. Miller next = fn->leaf; 769f11e6659SDavid S. Miller 770f11e6659SDavid S. Miller if (next != rt0) 771f11e6659SDavid S. Miller fn->rr_ptr = next; 772554cfb7eSYOSHIFUJI Hideaki } 773554cfb7eSYOSHIFUJI Hideaki 774d1918542SDavid S. Miller net = dev_net(rt0->dst.dev); 775a02cec21SEric Dumazet return match ? match : net->ipv6.ip6_null_entry; 7761da177e4SLinus Torvalds } 7771da177e4SLinus Torvalds 7788b9df265SMartin KaFai Lau static bool rt6_is_gw_or_nonexthop(const struct rt6_info *rt) 7798b9df265SMartin KaFai Lau { 7808b9df265SMartin KaFai Lau return (rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY)); 7818b9df265SMartin KaFai Lau } 7828b9df265SMartin KaFai Lau 78370ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 78470ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 785b71d1d42SEric Dumazet const struct in6_addr *gwaddr) 78670ceb4f5SYOSHIFUJI Hideaki { 787c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 78870ceb4f5SYOSHIFUJI Hideaki struct route_info *rinfo = (struct route_info *) opt; 78970ceb4f5SYOSHIFUJI Hideaki struct in6_addr prefix_buf, *prefix; 79070ceb4f5SYOSHIFUJI Hideaki unsigned int pref; 7914bed72e4SYOSHIFUJI Hideaki unsigned long lifetime; 79270ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt; 79370ceb4f5SYOSHIFUJI Hideaki 79470ceb4f5SYOSHIFUJI Hideaki if (len < sizeof(struct route_info)) { 79570ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 79670ceb4f5SYOSHIFUJI Hideaki } 79770ceb4f5SYOSHIFUJI Hideaki 79870ceb4f5SYOSHIFUJI Hideaki /* Sanity check for prefix_len and length */ 79970ceb4f5SYOSHIFUJI Hideaki if (rinfo->length > 3) { 80070ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 80170ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 128) { 80270ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 80370ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 64) { 80470ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 2) { 80570ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 80670ceb4f5SYOSHIFUJI Hideaki } 80770ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 0) { 80870ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 1) { 80970ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 81070ceb4f5SYOSHIFUJI Hideaki } 81170ceb4f5SYOSHIFUJI Hideaki } 81270ceb4f5SYOSHIFUJI Hideaki 81370ceb4f5SYOSHIFUJI Hideaki pref = rinfo->route_pref; 81470ceb4f5SYOSHIFUJI Hideaki if (pref == ICMPV6_ROUTER_PREF_INVALID) 8153933fc95SJens Rosenboom return -EINVAL; 81670ceb4f5SYOSHIFUJI Hideaki 8174bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); 81870ceb4f5SYOSHIFUJI Hideaki 81970ceb4f5SYOSHIFUJI Hideaki if (rinfo->length == 3) 82070ceb4f5SYOSHIFUJI Hideaki prefix = (struct in6_addr *)rinfo->prefix; 82170ceb4f5SYOSHIFUJI Hideaki else { 82270ceb4f5SYOSHIFUJI Hideaki /* this function is safe */ 82370ceb4f5SYOSHIFUJI Hideaki ipv6_addr_prefix(&prefix_buf, 82470ceb4f5SYOSHIFUJI Hideaki (struct in6_addr *)rinfo->prefix, 82570ceb4f5SYOSHIFUJI Hideaki rinfo->prefix_len); 82670ceb4f5SYOSHIFUJI Hideaki prefix = &prefix_buf; 82770ceb4f5SYOSHIFUJI Hideaki } 82870ceb4f5SYOSHIFUJI Hideaki 829f104a567SDuan Jiong if (rinfo->prefix_len == 0) 830f104a567SDuan Jiong rt = rt6_get_dflt_router(gwaddr, dev); 831f104a567SDuan Jiong else 832f104a567SDuan Jiong rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, 833830218c1SDavid Ahern gwaddr, dev); 83470ceb4f5SYOSHIFUJI Hideaki 83570ceb4f5SYOSHIFUJI Hideaki if (rt && !lifetime) { 836e0a1ad73SThomas Graf ip6_del_rt(rt); 83770ceb4f5SYOSHIFUJI Hideaki rt = NULL; 83870ceb4f5SYOSHIFUJI Hideaki } 83970ceb4f5SYOSHIFUJI Hideaki 84070ceb4f5SYOSHIFUJI Hideaki if (!rt && lifetime) 841830218c1SDavid Ahern rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, 842830218c1SDavid Ahern dev, pref); 84370ceb4f5SYOSHIFUJI Hideaki else if (rt) 84470ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags = RTF_ROUTEINFO | 84570ceb4f5SYOSHIFUJI Hideaki (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 84670ceb4f5SYOSHIFUJI Hideaki 84770ceb4f5SYOSHIFUJI Hideaki if (rt) { 8481716a961SGao feng if (!addrconf_finite_timeout(lifetime)) 8491716a961SGao feng rt6_clean_expires(rt); 8501716a961SGao feng else 8511716a961SGao feng rt6_set_expires(rt, jiffies + HZ * lifetime); 8521716a961SGao feng 85394e187c0SAmerigo Wang ip6_rt_put(rt); 85470ceb4f5SYOSHIFUJI Hideaki } 85570ceb4f5SYOSHIFUJI Hideaki return 0; 85670ceb4f5SYOSHIFUJI Hideaki } 85770ceb4f5SYOSHIFUJI Hideaki #endif 85870ceb4f5SYOSHIFUJI Hideaki 859a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn, 860a3c00e46SMartin KaFai Lau struct in6_addr *saddr) 861a3c00e46SMartin KaFai Lau { 862a3c00e46SMartin KaFai Lau struct fib6_node *pn; 863a3c00e46SMartin KaFai Lau while (1) { 864a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_TL_ROOT) 865a3c00e46SMartin KaFai Lau return NULL; 866a3c00e46SMartin KaFai Lau pn = fn->parent; 867a3c00e46SMartin KaFai Lau if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) 868a3c00e46SMartin KaFai Lau fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); 869a3c00e46SMartin KaFai Lau else 870a3c00e46SMartin KaFai Lau fn = pn; 871a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_RTINFO) 872a3c00e46SMartin KaFai Lau return fn; 873a3c00e46SMartin KaFai Lau } 874a3c00e46SMartin KaFai Lau } 875c71099acSThomas Graf 8768ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net, 8778ed67789SDaniel Lezcano struct fib6_table *table, 8784c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 8791da177e4SLinus Torvalds { 8801da177e4SLinus Torvalds struct fib6_node *fn; 8811da177e4SLinus Torvalds struct rt6_info *rt; 8821da177e4SLinus Torvalds 883c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 8844c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 885c71099acSThomas Graf restart: 886c71099acSThomas Graf rt = fn->leaf; 8874c9483b2SDavid S. Miller rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags); 88851ebd318SNicolas Dichtel if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0) 88952bd4c0cSNicolas Dichtel rt = rt6_multipath_select(rt, fl6, fl6->flowi6_oif, flags); 890a3c00e46SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 891a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 892a3c00e46SMartin KaFai Lau if (fn) 893a3c00e46SMartin KaFai Lau goto restart; 894a3c00e46SMartin KaFai Lau } 895d8d1f30bSChangli Gao dst_use(&rt->dst, jiffies); 896c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 897b811580dSDavid Ahern 898b811580dSDavid Ahern trace_fib6_table_lookup(net, rt, table->tb6_id, fl6); 899b811580dSDavid Ahern 9001da177e4SLinus Torvalds return rt; 901c71099acSThomas Graf 902c71099acSThomas Graf } 903c71099acSThomas Graf 904ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6, 905ea6e574eSFlorian Westphal int flags) 906ea6e574eSFlorian Westphal { 907ea6e574eSFlorian Westphal return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup); 908ea6e574eSFlorian Westphal } 909ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup); 910ea6e574eSFlorian Westphal 9119acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, 9129acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *saddr, int oif, int strict) 913c71099acSThomas Graf { 9144c9483b2SDavid S. Miller struct flowi6 fl6 = { 9154c9483b2SDavid S. Miller .flowi6_oif = oif, 9164c9483b2SDavid S. Miller .daddr = *daddr, 917c71099acSThomas Graf }; 918c71099acSThomas Graf struct dst_entry *dst; 91977d16f45SYOSHIFUJI Hideaki int flags = strict ? RT6_LOOKUP_F_IFACE : 0; 920c71099acSThomas Graf 921adaa70bbSThomas Graf if (saddr) { 9224c9483b2SDavid S. Miller memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 923adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 924adaa70bbSThomas Graf } 925adaa70bbSThomas Graf 9264c9483b2SDavid S. Miller dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup); 927c71099acSThomas Graf if (dst->error == 0) 928c71099acSThomas Graf return (struct rt6_info *) dst; 929c71099acSThomas Graf 930c71099acSThomas Graf dst_release(dst); 931c71099acSThomas Graf 9321da177e4SLinus Torvalds return NULL; 9331da177e4SLinus Torvalds } 9347159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup); 9357159039aSYOSHIFUJI Hideaki 936c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock. 9371cfb71eeSWei Wang * It takes new route entry, the addition fails by any reason the 9381cfb71eeSWei Wang * route is released. 9391cfb71eeSWei Wang * Caller must hold dst before calling it. 9401da177e4SLinus Torvalds */ 9411da177e4SLinus Torvalds 942e5fd387aSMichal Kubeček static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info, 943333c4301SDavid Ahern struct mx6_config *mxc, 944333c4301SDavid Ahern struct netlink_ext_ack *extack) 9451da177e4SLinus Torvalds { 9461da177e4SLinus Torvalds int err; 947c71099acSThomas Graf struct fib6_table *table; 9481da177e4SLinus Torvalds 949c71099acSThomas Graf table = rt->rt6i_table; 950c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 951333c4301SDavid Ahern err = fib6_add(&table->tb6_root, rt, info, mxc, extack); 952c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 9531da177e4SLinus Torvalds 9541da177e4SLinus Torvalds return err; 9551da177e4SLinus Torvalds } 9561da177e4SLinus Torvalds 95740e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt) 95840e22e8fSThomas Graf { 959e715b6d3SFlorian Westphal struct nl_info info = { .nl_net = dev_net(rt->dst.dev), }; 960e715b6d3SFlorian Westphal struct mx6_config mxc = { .mx = NULL, }; 961e715b6d3SFlorian Westphal 9621cfb71eeSWei Wang /* Hold dst to account for the reference from the fib6 tree */ 9631cfb71eeSWei Wang dst_hold(&rt->dst); 964333c4301SDavid Ahern return __ip6_ins_rt(rt, &info, &mxc, NULL); 96540e22e8fSThomas Graf } 96640e22e8fSThomas Graf 9678b9df265SMartin KaFai Lau static struct rt6_info *ip6_rt_cache_alloc(struct rt6_info *ort, 96821efcfa0SEric Dumazet const struct in6_addr *daddr, 969b71d1d42SEric Dumazet const struct in6_addr *saddr) 9701da177e4SLinus Torvalds { 9711da177e4SLinus Torvalds struct rt6_info *rt; 9721da177e4SLinus Torvalds 9731da177e4SLinus Torvalds /* 9741da177e4SLinus Torvalds * Clone the route. 9751da177e4SLinus Torvalds */ 9761da177e4SLinus Torvalds 977d52d3997SMartin KaFai Lau if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)) 97883a09abdSMartin KaFai Lau ort = (struct rt6_info *)ort->dst.from; 9791da177e4SLinus Torvalds 980ad706862SMartin KaFai Lau rt = __ip6_dst_alloc(dev_net(ort->dst.dev), ort->dst.dev, 0); 98183a09abdSMartin KaFai Lau 98283a09abdSMartin KaFai Lau if (!rt) 98383a09abdSMartin KaFai Lau return NULL; 98483a09abdSMartin KaFai Lau 98583a09abdSMartin KaFai Lau ip6_rt_copy_init(rt, ort); 9868b9df265SMartin KaFai Lau rt->rt6i_flags |= RTF_CACHE; 98783a09abdSMartin KaFai Lau rt->rt6i_metric = 0; 98883a09abdSMartin KaFai Lau rt->dst.flags |= DST_HOST; 98983a09abdSMartin KaFai Lau rt->rt6i_dst.addr = *daddr; 99083a09abdSMartin KaFai Lau rt->rt6i_dst.plen = 128; 9918b9df265SMartin KaFai Lau 9928b9df265SMartin KaFai Lau if (!rt6_is_gw_or_nonexthop(ort)) { 993bb3c3686SDavid S. Miller if (ort->rt6i_dst.plen != 128 && 99421efcfa0SEric Dumazet ipv6_addr_equal(&ort->rt6i_dst.addr, daddr)) 99558c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 9961da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 9971da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 9984e3fd7a0SAlexey Dobriyan rt->rt6i_src.addr = *saddr; 9991da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 10001da177e4SLinus Torvalds } 10011da177e4SLinus Torvalds #endif 100295a9a5baSYOSHIFUJI Hideaki } 100395a9a5baSYOSHIFUJI Hideaki 1004299d9939SYOSHIFUJI Hideaki return rt; 1005299d9939SYOSHIFUJI Hideaki } 1006299d9939SYOSHIFUJI Hideaki 1007d52d3997SMartin KaFai Lau static struct rt6_info *ip6_rt_pcpu_alloc(struct rt6_info *rt) 1008d52d3997SMartin KaFai Lau { 1009d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1010d52d3997SMartin KaFai Lau 1011d52d3997SMartin KaFai Lau pcpu_rt = __ip6_dst_alloc(dev_net(rt->dst.dev), 1012ad706862SMartin KaFai Lau rt->dst.dev, rt->dst.flags); 1013d52d3997SMartin KaFai Lau 1014d52d3997SMartin KaFai Lau if (!pcpu_rt) 1015d52d3997SMartin KaFai Lau return NULL; 1016d52d3997SMartin KaFai Lau ip6_rt_copy_init(pcpu_rt, rt); 1017d52d3997SMartin KaFai Lau pcpu_rt->rt6i_protocol = rt->rt6i_protocol; 1018d52d3997SMartin KaFai Lau pcpu_rt->rt6i_flags |= RTF_PCPU; 1019d52d3997SMartin KaFai Lau return pcpu_rt; 1020d52d3997SMartin KaFai Lau } 1021d52d3997SMartin KaFai Lau 1022d52d3997SMartin KaFai Lau /* It should be called with read_lock_bh(&tb6_lock) acquired */ 1023d52d3997SMartin KaFai Lau static struct rt6_info *rt6_get_pcpu_route(struct rt6_info *rt) 1024d52d3997SMartin KaFai Lau { 1025a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, **p; 1026d52d3997SMartin KaFai Lau 1027d52d3997SMartin KaFai Lau p = this_cpu_ptr(rt->rt6i_pcpu); 1028d52d3997SMartin KaFai Lau pcpu_rt = *p; 1029d52d3997SMartin KaFai Lau 1030a73e4195SMartin KaFai Lau if (pcpu_rt) { 1031a73e4195SMartin KaFai Lau dst_hold(&pcpu_rt->dst); 1032a73e4195SMartin KaFai Lau rt6_dst_from_metrics_check(pcpu_rt); 1033a73e4195SMartin KaFai Lau } 1034a73e4195SMartin KaFai Lau return pcpu_rt; 1035a73e4195SMartin KaFai Lau } 1036a73e4195SMartin KaFai Lau 1037a73e4195SMartin KaFai Lau static struct rt6_info *rt6_make_pcpu_route(struct rt6_info *rt) 1038a73e4195SMartin KaFai Lau { 10399c7370a1SMartin KaFai Lau struct fib6_table *table = rt->rt6i_table; 1040a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, *prev, **p; 1041d52d3997SMartin KaFai Lau 1042d52d3997SMartin KaFai Lau pcpu_rt = ip6_rt_pcpu_alloc(rt); 1043d52d3997SMartin KaFai Lau if (!pcpu_rt) { 1044d52d3997SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 1045d52d3997SMartin KaFai Lau 10469c7370a1SMartin KaFai Lau dst_hold(&net->ipv6.ip6_null_entry->dst); 10479c7370a1SMartin KaFai Lau return net->ipv6.ip6_null_entry; 1048d52d3997SMartin KaFai Lau } 1049d52d3997SMartin KaFai Lau 10509c7370a1SMartin KaFai Lau read_lock_bh(&table->tb6_lock); 10519c7370a1SMartin KaFai Lau if (rt->rt6i_pcpu) { 1052a73e4195SMartin KaFai Lau p = this_cpu_ptr(rt->rt6i_pcpu); 1053d52d3997SMartin KaFai Lau prev = cmpxchg(p, NULL, pcpu_rt); 1054d52d3997SMartin KaFai Lau if (prev) { 1055d52d3997SMartin KaFai Lau /* If someone did it before us, return prev instead */ 10561cfb71eeSWei Wang dst_release(&pcpu_rt->dst); 1057d52d3997SMartin KaFai Lau dst_destroy(&pcpu_rt->dst); 1058d52d3997SMartin KaFai Lau pcpu_rt = prev; 1059d52d3997SMartin KaFai Lau } 10609c7370a1SMartin KaFai Lau } else { 10619c7370a1SMartin KaFai Lau /* rt has been removed from the fib6 tree 10629c7370a1SMartin KaFai Lau * before we have a chance to acquire the read_lock. 10639c7370a1SMartin KaFai Lau * In this case, don't brother to create a pcpu rt 10649c7370a1SMartin KaFai Lau * since rt is going away anyway. The next 10659c7370a1SMartin KaFai Lau * dst_check() will trigger a re-lookup. 10669c7370a1SMartin KaFai Lau */ 10671cfb71eeSWei Wang dst_release(&pcpu_rt->dst); 10689c7370a1SMartin KaFai Lau dst_destroy(&pcpu_rt->dst); 10699c7370a1SMartin KaFai Lau pcpu_rt = rt; 10709c7370a1SMartin KaFai Lau } 1071d52d3997SMartin KaFai Lau dst_hold(&pcpu_rt->dst); 1072d52d3997SMartin KaFai Lau rt6_dst_from_metrics_check(pcpu_rt); 10739c7370a1SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 1074d52d3997SMartin KaFai Lau return pcpu_rt; 1075d52d3997SMartin KaFai Lau } 1076d52d3997SMartin KaFai Lau 10779ff74384SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, 10789ff74384SDavid Ahern int oif, struct flowi6 *fl6, int flags) 10791da177e4SLinus Torvalds { 1080367efcb9SMartin KaFai Lau struct fib6_node *fn, *saved_fn; 108145e4fd26SMartin KaFai Lau struct rt6_info *rt; 1082c71099acSThomas Graf int strict = 0; 10831da177e4SLinus Torvalds 108477d16f45SYOSHIFUJI Hideaki strict |= flags & RT6_LOOKUP_F_IFACE; 1085d5d32e4bSDavid Ahern strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE; 1086367efcb9SMartin KaFai Lau if (net->ipv6.devconf_all->forwarding == 0) 1087367efcb9SMartin KaFai Lau strict |= RT6_LOOKUP_F_REACHABLE; 10881da177e4SLinus Torvalds 1089c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 10901da177e4SLinus Torvalds 10914c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1092367efcb9SMartin KaFai Lau saved_fn = fn; 10931da177e4SLinus Torvalds 1094ca254490SDavid Ahern if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) 1095ca254490SDavid Ahern oif = 0; 1096ca254490SDavid Ahern 1097a3c00e46SMartin KaFai Lau redo_rt6_select: 1098367efcb9SMartin KaFai Lau rt = rt6_select(fn, oif, strict); 109952bd4c0cSNicolas Dichtel if (rt->rt6i_nsiblings) 1100367efcb9SMartin KaFai Lau rt = rt6_multipath_select(rt, fl6, oif, strict); 1101a3c00e46SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 1102a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1103a3c00e46SMartin KaFai Lau if (fn) 1104a3c00e46SMartin KaFai Lau goto redo_rt6_select; 1105367efcb9SMartin KaFai Lau else if (strict & RT6_LOOKUP_F_REACHABLE) { 1106367efcb9SMartin KaFai Lau /* also consider unreachable route */ 1107367efcb9SMartin KaFai Lau strict &= ~RT6_LOOKUP_F_REACHABLE; 1108367efcb9SMartin KaFai Lau fn = saved_fn; 1109367efcb9SMartin KaFai Lau goto redo_rt6_select; 1110367efcb9SMartin KaFai Lau } 1111a3c00e46SMartin KaFai Lau } 1112a3c00e46SMartin KaFai Lau 1113d52d3997SMartin KaFai Lau 1114d52d3997SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry || (rt->rt6i_flags & RTF_CACHE)) { 11153da59bd9SMartin KaFai Lau dst_use(&rt->dst, jiffies); 1116c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 11171da177e4SLinus Torvalds 1118d52d3997SMartin KaFai Lau rt6_dst_from_metrics_check(rt); 1119b811580dSDavid Ahern 1120b811580dSDavid Ahern trace_fib6_table_lookup(net, rt, table->tb6_id, fl6); 1121d52d3997SMartin KaFai Lau return rt; 11223da59bd9SMartin KaFai Lau } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) && 11233da59bd9SMartin KaFai Lau !(rt->rt6i_flags & RTF_GATEWAY))) { 11243da59bd9SMartin KaFai Lau /* Create a RTF_CACHE clone which will not be 11253da59bd9SMartin KaFai Lau * owned by the fib6 tree. It is for the special case where 11263da59bd9SMartin KaFai Lau * the daddr in the skb during the neighbor look-up is different 11273da59bd9SMartin KaFai Lau * from the fl6->daddr used to look-up route here. 11283da59bd9SMartin KaFai Lau */ 1129c71099acSThomas Graf 11303da59bd9SMartin KaFai Lau struct rt6_info *uncached_rt; 11313da59bd9SMartin KaFai Lau 1132d52d3997SMartin KaFai Lau dst_use(&rt->dst, jiffies); 1133d52d3997SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 1134d52d3997SMartin KaFai Lau 11353da59bd9SMartin KaFai Lau uncached_rt = ip6_rt_cache_alloc(rt, &fl6->daddr, NULL); 11363da59bd9SMartin KaFai Lau dst_release(&rt->dst); 11373da59bd9SMartin KaFai Lau 11381cfb71eeSWei Wang if (uncached_rt) { 11391cfb71eeSWei Wang /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc() 11401cfb71eeSWei Wang * No need for another dst_hold() 11411cfb71eeSWei Wang */ 11428d0b94afSMartin KaFai Lau rt6_uncached_list_add(uncached_rt); 11431cfb71eeSWei Wang } else { 11443da59bd9SMartin KaFai Lau uncached_rt = net->ipv6.ip6_null_entry; 11453da59bd9SMartin KaFai Lau dst_hold(&uncached_rt->dst); 11461cfb71eeSWei Wang } 1147b811580dSDavid Ahern 1148b811580dSDavid Ahern trace_fib6_table_lookup(net, uncached_rt, table->tb6_id, fl6); 11493da59bd9SMartin KaFai Lau return uncached_rt; 11503da59bd9SMartin KaFai Lau 1151d52d3997SMartin KaFai Lau } else { 1152d52d3997SMartin KaFai Lau /* Get a percpu copy */ 1153d52d3997SMartin KaFai Lau 1154d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1155d52d3997SMartin KaFai Lau 1156d52d3997SMartin KaFai Lau rt->dst.lastuse = jiffies; 1157d52d3997SMartin KaFai Lau rt->dst.__use++; 1158d52d3997SMartin KaFai Lau pcpu_rt = rt6_get_pcpu_route(rt); 1159d52d3997SMartin KaFai Lau 11609c7370a1SMartin KaFai Lau if (pcpu_rt) { 1161d52d3997SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 11629c7370a1SMartin KaFai Lau } else { 11639c7370a1SMartin KaFai Lau /* We have to do the read_unlock first 11649c7370a1SMartin KaFai Lau * because rt6_make_pcpu_route() may trigger 11659c7370a1SMartin KaFai Lau * ip6_dst_gc() which will take the write_lock. 11669c7370a1SMartin KaFai Lau */ 11679c7370a1SMartin KaFai Lau dst_hold(&rt->dst); 11689c7370a1SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 11699c7370a1SMartin KaFai Lau pcpu_rt = rt6_make_pcpu_route(rt); 11709c7370a1SMartin KaFai Lau dst_release(&rt->dst); 11719c7370a1SMartin KaFai Lau } 1172d52d3997SMartin KaFai Lau 1173b811580dSDavid Ahern trace_fib6_table_lookup(net, pcpu_rt, table->tb6_id, fl6); 1174d52d3997SMartin KaFai Lau return pcpu_rt; 11759c7370a1SMartin KaFai Lau 1176d52d3997SMartin KaFai Lau } 1177c71099acSThomas Graf } 11789ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route); 1179c71099acSThomas Graf 11808ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table, 11814c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 11824acad72dSPavel Emelyanov { 11834c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags); 11844acad72dSPavel Emelyanov } 11854acad72dSPavel Emelyanov 1186d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net, 118772331bc0SShmulik Ladkani struct net_device *dev, 118872331bc0SShmulik Ladkani struct flowi6 *fl6, int flags) 118972331bc0SShmulik Ladkani { 119072331bc0SShmulik Ladkani if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) 119172331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_IFACE; 119272331bc0SShmulik Ladkani 119372331bc0SShmulik Ladkani return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input); 119472331bc0SShmulik Ladkani } 1195d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup); 119672331bc0SShmulik Ladkani 1197c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 1198c71099acSThomas Graf { 1199b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 1200c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 1201adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 1202904af04dSJiri Benc struct ip_tunnel_info *tun_info; 12034c9483b2SDavid S. Miller struct flowi6 fl6 = { 1204e0d56fddSDavid Ahern .flowi6_iif = skb->dev->ifindex, 12054c9483b2SDavid S. Miller .daddr = iph->daddr, 12064c9483b2SDavid S. Miller .saddr = iph->saddr, 12076502ca52SYOSHIFUJI Hideaki / 吉藤英明 .flowlabel = ip6_flowinfo(iph), 12084c9483b2SDavid S. Miller .flowi6_mark = skb->mark, 12094c9483b2SDavid S. Miller .flowi6_proto = iph->nexthdr, 1210c71099acSThomas Graf }; 1211adaa70bbSThomas Graf 1212904af04dSJiri Benc tun_info = skb_tunnel_info(skb); 121346fa062aSJiri Benc if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) 1214904af04dSJiri Benc fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id; 121506e9d040SJiri Benc skb_dst_drop(skb); 121672331bc0SShmulik Ladkani skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags)); 1217c71099acSThomas Graf } 1218c71099acSThomas Graf 12198ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table, 12204c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 1221c71099acSThomas Graf { 12224c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags); 1223c71099acSThomas Graf } 1224c71099acSThomas Graf 12256f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk, 12266f21c96aSPaolo Abeni struct flowi6 *fl6, int flags) 1227c71099acSThomas Graf { 1228d46a9d67SDavid Ahern bool any_src; 1229c71099acSThomas Graf 12304c1feac5SDavid Ahern if (rt6_need_strict(&fl6->daddr)) { 12314c1feac5SDavid Ahern struct dst_entry *dst; 12324c1feac5SDavid Ahern 12334c1feac5SDavid Ahern dst = l3mdev_link_scope_lookup(net, fl6); 1234ca254490SDavid Ahern if (dst) 1235ca254490SDavid Ahern return dst; 12364c1feac5SDavid Ahern } 1237ca254490SDavid Ahern 12381fb9489bSPavel Emelyanov fl6->flowi6_iif = LOOPBACK_IFINDEX; 12394dc27d1cSDavid McCullough 1240d46a9d67SDavid Ahern any_src = ipv6_addr_any(&fl6->saddr); 1241741a11d9SDavid Ahern if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) || 1242d46a9d67SDavid Ahern (fl6->flowi6_oif && any_src)) 124377d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 1244c71099acSThomas Graf 1245d46a9d67SDavid Ahern if (!any_src) 1246adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 12470c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 12480c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 1249adaa70bbSThomas Graf 12504c9483b2SDavid S. Miller return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output); 12511da177e4SLinus Torvalds } 12526f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags); 12531da177e4SLinus Torvalds 12542774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 125514e50e57SDavid S. Miller { 12565c1e6aa3SDavid S. Miller struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; 12571dbe3252SWei Wang struct net_device *loopback_dev = net->loopback_dev; 125814e50e57SDavid S. Miller struct dst_entry *new = NULL; 125914e50e57SDavid S. Miller 12601dbe3252SWei Wang 12611dbe3252SWei Wang rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1, 12621dbe3252SWei Wang DST_OBSOLETE_NONE, 0); 126314e50e57SDavid S. Miller if (rt) { 12640a1f5962SMartin KaFai Lau rt6_info_init(rt); 12650a1f5962SMartin KaFai Lau 1266d8d1f30bSChangli Gao new = &rt->dst; 126714e50e57SDavid S. Miller new->__use = 1; 1268352e512cSHerbert Xu new->input = dst_discard; 1269ede2059dSEric W. Biederman new->output = dst_discard_out; 127014e50e57SDavid S. Miller 1271defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 127214e50e57SDavid S. Miller 12731dbe3252SWei Wang rt->rt6i_idev = in6_dev_get(loopback_dev); 12744e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 12750a1f5962SMartin KaFai Lau rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU; 127614e50e57SDavid S. Miller rt->rt6i_metric = 0; 127714e50e57SDavid S. Miller 127814e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 127914e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 128014e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 128114e50e57SDavid S. Miller #endif 128214e50e57SDavid S. Miller 128314e50e57SDavid S. Miller dst_free(new); 128414e50e57SDavid S. Miller } 128514e50e57SDavid S. Miller 128669ead7afSDavid S. Miller dst_release(dst_orig); 128769ead7afSDavid S. Miller return new ? new : ERR_PTR(-ENOMEM); 128814e50e57SDavid S. Miller } 128914e50e57SDavid S. Miller 12901da177e4SLinus Torvalds /* 12911da177e4SLinus Torvalds * Destination cache support functions 12921da177e4SLinus Torvalds */ 12931da177e4SLinus Torvalds 12944b32b5adSMartin KaFai Lau static void rt6_dst_from_metrics_check(struct rt6_info *rt) 12954b32b5adSMartin KaFai Lau { 12964b32b5adSMartin KaFai Lau if (rt->dst.from && 12974b32b5adSMartin KaFai Lau dst_metrics_ptr(&rt->dst) != dst_metrics_ptr(rt->dst.from)) 12984b32b5adSMartin KaFai Lau dst_init_metrics(&rt->dst, dst_metrics_ptr(rt->dst.from), true); 12994b32b5adSMartin KaFai Lau } 13004b32b5adSMartin KaFai Lau 13013da59bd9SMartin KaFai Lau static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie) 13023da59bd9SMartin KaFai Lau { 13033da59bd9SMartin KaFai Lau if (!rt->rt6i_node || (rt->rt6i_node->fn_sernum != cookie)) 13043da59bd9SMartin KaFai Lau return NULL; 13053da59bd9SMartin KaFai Lau 13063da59bd9SMartin KaFai Lau if (rt6_check_expired(rt)) 13073da59bd9SMartin KaFai Lau return NULL; 13083da59bd9SMartin KaFai Lau 13093da59bd9SMartin KaFai Lau return &rt->dst; 13103da59bd9SMartin KaFai Lau } 13113da59bd9SMartin KaFai Lau 13123da59bd9SMartin KaFai Lau static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, u32 cookie) 13133da59bd9SMartin KaFai Lau { 13145973fb1eSMartin KaFai Lau if (!__rt6_check_expired(rt) && 13155973fb1eSMartin KaFai Lau rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && 13163da59bd9SMartin KaFai Lau rt6_check((struct rt6_info *)(rt->dst.from), cookie)) 13173da59bd9SMartin KaFai Lau return &rt->dst; 13183da59bd9SMartin KaFai Lau else 13193da59bd9SMartin KaFai Lau return NULL; 13203da59bd9SMartin KaFai Lau } 13213da59bd9SMartin KaFai Lau 13221da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 13231da177e4SLinus Torvalds { 13241da177e4SLinus Torvalds struct rt6_info *rt; 13251da177e4SLinus Torvalds 13261da177e4SLinus Torvalds rt = (struct rt6_info *) dst; 13271da177e4SLinus Torvalds 13286f3118b5SNicolas Dichtel /* All IPV6 dsts are created with ->obsolete set to the value 13296f3118b5SNicolas Dichtel * DST_OBSOLETE_FORCE_CHK which forces validation calls down 13306f3118b5SNicolas Dichtel * into this function always. 13316f3118b5SNicolas Dichtel */ 1332e3bc10bdSHannes Frederic Sowa 13334b32b5adSMartin KaFai Lau rt6_dst_from_metrics_check(rt); 13344b32b5adSMartin KaFai Lau 133502bcf4e0SMartin KaFai Lau if (rt->rt6i_flags & RTF_PCPU || 133602bcf4e0SMartin KaFai Lau (unlikely(dst->flags & DST_NOCACHE) && rt->dst.from)) 13373da59bd9SMartin KaFai Lau return rt6_dst_from_check(rt, cookie); 13383da59bd9SMartin KaFai Lau else 13393da59bd9SMartin KaFai Lau return rt6_check(rt, cookie); 13401da177e4SLinus Torvalds } 13411da177e4SLinus Torvalds 13421da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 13431da177e4SLinus Torvalds { 13441da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 13451da177e4SLinus Torvalds 13461da177e4SLinus Torvalds if (rt) { 134754c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 134854c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 1349e0a1ad73SThomas Graf ip6_del_rt(rt); 135054c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 13511da177e4SLinus Torvalds } 135254c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 135354c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 135454c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 135554c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 135654c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 135754c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 13581da177e4SLinus Torvalds } 13591da177e4SLinus Torvalds 13601da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 13611da177e4SLinus Torvalds { 13621da177e4SLinus Torvalds struct rt6_info *rt; 13631da177e4SLinus Torvalds 13643ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 13651da177e4SLinus Torvalds 1366adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 13671da177e4SLinus Torvalds if (rt) { 13681eb4f758SHannes Frederic Sowa if (rt->rt6i_flags & RTF_CACHE) { 1369*ad65a2f0SWei Wang if (dst_hold_safe(&rt->dst)) 13708e3d5be7SMartin KaFai Lau ip6_del_rt(rt); 13711eb4f758SHannes Frederic Sowa } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) { 13721da177e4SLinus Torvalds rt->rt6i_node->fn_sernum = -1; 13731da177e4SLinus Torvalds } 13741da177e4SLinus Torvalds } 13751eb4f758SHannes Frederic Sowa } 13761da177e4SLinus Torvalds 137745e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu) 137845e4fd26SMartin KaFai Lau { 137945e4fd26SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 138045e4fd26SMartin KaFai Lau 138145e4fd26SMartin KaFai Lau rt->rt6i_flags |= RTF_MODIFIED; 138245e4fd26SMartin KaFai Lau rt->rt6i_pmtu = mtu; 138345e4fd26SMartin KaFai Lau rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires); 138445e4fd26SMartin KaFai Lau } 138545e4fd26SMartin KaFai Lau 13860d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt) 13870d3f6d29SMartin KaFai Lau { 13880d3f6d29SMartin KaFai Lau return !(rt->rt6i_flags & RTF_CACHE) && 13890d3f6d29SMartin KaFai Lau (rt->rt6i_flags & RTF_PCPU || rt->rt6i_node); 13900d3f6d29SMartin KaFai Lau } 13910d3f6d29SMartin KaFai Lau 139245e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk, 139345e4fd26SMartin KaFai Lau const struct ipv6hdr *iph, u32 mtu) 13941da177e4SLinus Torvalds { 13950dec879fSJulian Anastasov const struct in6_addr *daddr, *saddr; 13961da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info *)dst; 13971da177e4SLinus Torvalds 139845e4fd26SMartin KaFai Lau if (rt6->rt6i_flags & RTF_LOCAL) 139945e4fd26SMartin KaFai Lau return; 140045e4fd26SMartin KaFai Lau 140119bda36cSXin Long if (dst_metric_locked(dst, RTAX_MTU)) 140219bda36cSXin Long return; 140319bda36cSXin Long 140445e4fd26SMartin KaFai Lau if (iph) { 140545e4fd26SMartin KaFai Lau daddr = &iph->daddr; 140645e4fd26SMartin KaFai Lau saddr = &iph->saddr; 140745e4fd26SMartin KaFai Lau } else if (sk) { 140845e4fd26SMartin KaFai Lau daddr = &sk->sk_v6_daddr; 140945e4fd26SMartin KaFai Lau saddr = &inet6_sk(sk)->saddr; 141045e4fd26SMartin KaFai Lau } else { 14110dec879fSJulian Anastasov daddr = NULL; 14120dec879fSJulian Anastasov saddr = NULL; 14131da177e4SLinus Torvalds } 14140dec879fSJulian Anastasov dst_confirm_neigh(dst, daddr); 14150dec879fSJulian Anastasov mtu = max_t(u32, mtu, IPV6_MIN_MTU); 14160dec879fSJulian Anastasov if (mtu >= dst_mtu(dst)) 14170dec879fSJulian Anastasov return; 14180dec879fSJulian Anastasov 14190dec879fSJulian Anastasov if (!rt6_cache_allowed_for_pmtu(rt6)) { 14200dec879fSJulian Anastasov rt6_do_update_pmtu(rt6, mtu); 14210dec879fSJulian Anastasov } else if (daddr) { 14220dec879fSJulian Anastasov struct rt6_info *nrt6; 14230dec879fSJulian Anastasov 142445e4fd26SMartin KaFai Lau nrt6 = ip6_rt_cache_alloc(rt6, daddr, saddr); 142545e4fd26SMartin KaFai Lau if (nrt6) { 142645e4fd26SMartin KaFai Lau rt6_do_update_pmtu(nrt6, mtu); 142745e4fd26SMartin KaFai Lau 142845e4fd26SMartin KaFai Lau /* ip6_ins_rt(nrt6) will bump the 142945e4fd26SMartin KaFai Lau * rt6->rt6i_node->fn_sernum 143045e4fd26SMartin KaFai Lau * which will fail the next rt6_check() and 143145e4fd26SMartin KaFai Lau * invalidate the sk->sk_dst_cache. 143245e4fd26SMartin KaFai Lau */ 143345e4fd26SMartin KaFai Lau ip6_ins_rt(nrt6); 14341cfb71eeSWei Wang /* Release the reference taken in 14351cfb71eeSWei Wang * ip6_rt_cache_alloc() 14361cfb71eeSWei Wang */ 14371cfb71eeSWei Wang dst_release(&nrt6->dst); 143845e4fd26SMartin KaFai Lau } 143945e4fd26SMartin KaFai Lau } 144045e4fd26SMartin KaFai Lau } 144145e4fd26SMartin KaFai Lau 144245e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 144345e4fd26SMartin KaFai Lau struct sk_buff *skb, u32 mtu) 144445e4fd26SMartin KaFai Lau { 144545e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu); 14461da177e4SLinus Torvalds } 14471da177e4SLinus Torvalds 144842ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, 1449e2d118a1SLorenzo Colitti int oif, u32 mark, kuid_t uid) 145081aded24SDavid S. Miller { 145181aded24SDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 145281aded24SDavid S. Miller struct dst_entry *dst; 145381aded24SDavid S. Miller struct flowi6 fl6; 145481aded24SDavid S. Miller 145581aded24SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 145681aded24SDavid S. Miller fl6.flowi6_oif = oif; 14571b3c61dcSLorenzo Colitti fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark); 145881aded24SDavid S. Miller fl6.daddr = iph->daddr; 145981aded24SDavid S. Miller fl6.saddr = iph->saddr; 14606502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 1461e2d118a1SLorenzo Colitti fl6.flowi6_uid = uid; 146281aded24SDavid S. Miller 146381aded24SDavid S. Miller dst = ip6_route_output(net, NULL, &fl6); 146481aded24SDavid S. Miller if (!dst->error) 146545e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu)); 146681aded24SDavid S. Miller dst_release(dst); 146781aded24SDavid S. Miller } 146881aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu); 146981aded24SDavid S. Miller 147081aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) 147181aded24SDavid S. Miller { 147233c162a9SMartin KaFai Lau struct dst_entry *dst; 147333c162a9SMartin KaFai Lau 147481aded24SDavid S. Miller ip6_update_pmtu(skb, sock_net(sk), mtu, 1475e2d118a1SLorenzo Colitti sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid); 147633c162a9SMartin KaFai Lau 147733c162a9SMartin KaFai Lau dst = __sk_dst_get(sk); 147833c162a9SMartin KaFai Lau if (!dst || !dst->obsolete || 147933c162a9SMartin KaFai Lau dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) 148033c162a9SMartin KaFai Lau return; 148133c162a9SMartin KaFai Lau 148233c162a9SMartin KaFai Lau bh_lock_sock(sk); 148333c162a9SMartin KaFai Lau if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) 148433c162a9SMartin KaFai Lau ip6_datagram_dst_update(sk, false); 148533c162a9SMartin KaFai Lau bh_unlock_sock(sk); 148681aded24SDavid S. Miller } 148781aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); 148881aded24SDavid S. Miller 1489b55b76b2SDuan Jiong /* Handle redirects */ 1490b55b76b2SDuan Jiong struct ip6rd_flowi { 1491b55b76b2SDuan Jiong struct flowi6 fl6; 1492b55b76b2SDuan Jiong struct in6_addr gateway; 1493b55b76b2SDuan Jiong }; 1494b55b76b2SDuan Jiong 1495b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net, 1496b55b76b2SDuan Jiong struct fib6_table *table, 1497b55b76b2SDuan Jiong struct flowi6 *fl6, 1498b55b76b2SDuan Jiong int flags) 1499b55b76b2SDuan Jiong { 1500b55b76b2SDuan Jiong struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; 1501b55b76b2SDuan Jiong struct rt6_info *rt; 1502b55b76b2SDuan Jiong struct fib6_node *fn; 1503b55b76b2SDuan Jiong 1504b55b76b2SDuan Jiong /* Get the "current" route for this destination and 150567c408cfSAlexander Alemayhu * check if the redirect has come from appropriate router. 1506b55b76b2SDuan Jiong * 1507b55b76b2SDuan Jiong * RFC 4861 specifies that redirects should only be 1508b55b76b2SDuan Jiong * accepted if they come from the nexthop to the target. 1509b55b76b2SDuan Jiong * Due to the way the routes are chosen, this notion 1510b55b76b2SDuan Jiong * is a bit fuzzy and one might need to check all possible 1511b55b76b2SDuan Jiong * routes. 1512b55b76b2SDuan Jiong */ 1513b55b76b2SDuan Jiong 1514b55b76b2SDuan Jiong read_lock_bh(&table->tb6_lock); 1515b55b76b2SDuan Jiong fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1516b55b76b2SDuan Jiong restart: 1517b55b76b2SDuan Jiong for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 1518b55b76b2SDuan Jiong if (rt6_check_expired(rt)) 1519b55b76b2SDuan Jiong continue; 1520b55b76b2SDuan Jiong if (rt->dst.error) 1521b55b76b2SDuan Jiong break; 1522b55b76b2SDuan Jiong if (!(rt->rt6i_flags & RTF_GATEWAY)) 1523b55b76b2SDuan Jiong continue; 1524b55b76b2SDuan Jiong if (fl6->flowi6_oif != rt->dst.dev->ifindex) 1525b55b76b2SDuan Jiong continue; 1526b55b76b2SDuan Jiong if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway)) 1527b55b76b2SDuan Jiong continue; 1528b55b76b2SDuan Jiong break; 1529b55b76b2SDuan Jiong } 1530b55b76b2SDuan Jiong 1531b55b76b2SDuan Jiong if (!rt) 1532b55b76b2SDuan Jiong rt = net->ipv6.ip6_null_entry; 1533b55b76b2SDuan Jiong else if (rt->dst.error) { 1534b55b76b2SDuan Jiong rt = net->ipv6.ip6_null_entry; 1535b0a1ba59SMartin KaFai Lau goto out; 1536b0a1ba59SMartin KaFai Lau } 1537b0a1ba59SMartin KaFai Lau 1538b0a1ba59SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 1539a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1540a3c00e46SMartin KaFai Lau if (fn) 1541a3c00e46SMartin KaFai Lau goto restart; 1542b55b76b2SDuan Jiong } 1543a3c00e46SMartin KaFai Lau 1544b0a1ba59SMartin KaFai Lau out: 1545b55b76b2SDuan Jiong dst_hold(&rt->dst); 1546b55b76b2SDuan Jiong 1547b55b76b2SDuan Jiong read_unlock_bh(&table->tb6_lock); 1548b55b76b2SDuan Jiong 1549b811580dSDavid Ahern trace_fib6_table_lookup(net, rt, table->tb6_id, fl6); 1550b55b76b2SDuan Jiong return rt; 1551b55b76b2SDuan Jiong }; 1552b55b76b2SDuan Jiong 1553b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net, 1554b55b76b2SDuan Jiong const struct flowi6 *fl6, 1555b55b76b2SDuan Jiong const struct in6_addr *gateway) 1556b55b76b2SDuan Jiong { 1557b55b76b2SDuan Jiong int flags = RT6_LOOKUP_F_HAS_SADDR; 1558b55b76b2SDuan Jiong struct ip6rd_flowi rdfl; 1559b55b76b2SDuan Jiong 1560b55b76b2SDuan Jiong rdfl.fl6 = *fl6; 1561b55b76b2SDuan Jiong rdfl.gateway = *gateway; 1562b55b76b2SDuan Jiong 1563b55b76b2SDuan Jiong return fib6_rule_lookup(net, &rdfl.fl6, 1564b55b76b2SDuan Jiong flags, __ip6_route_redirect); 1565b55b76b2SDuan Jiong } 1566b55b76b2SDuan Jiong 1567e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, 1568e2d118a1SLorenzo Colitti kuid_t uid) 15693a5ad2eeSDavid S. Miller { 15703a5ad2eeSDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 15713a5ad2eeSDavid S. Miller struct dst_entry *dst; 15723a5ad2eeSDavid S. Miller struct flowi6 fl6; 15733a5ad2eeSDavid S. Miller 15743a5ad2eeSDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 1575e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 15763a5ad2eeSDavid S. Miller fl6.flowi6_oif = oif; 15773a5ad2eeSDavid S. Miller fl6.flowi6_mark = mark; 15783a5ad2eeSDavid S. Miller fl6.daddr = iph->daddr; 15793a5ad2eeSDavid S. Miller fl6.saddr = iph->saddr; 15806502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 1581e2d118a1SLorenzo Colitti fl6.flowi6_uid = uid; 15823a5ad2eeSDavid S. Miller 1583b55b76b2SDuan Jiong dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr); 15846700c270SDavid S. Miller rt6_do_redirect(dst, NULL, skb); 15853a5ad2eeSDavid S. Miller dst_release(dst); 15863a5ad2eeSDavid S. Miller } 15873a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect); 15883a5ad2eeSDavid S. Miller 1589c92a59ecSDuan Jiong void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif, 1590c92a59ecSDuan Jiong u32 mark) 1591c92a59ecSDuan Jiong { 1592c92a59ecSDuan Jiong const struct ipv6hdr *iph = ipv6_hdr(skb); 1593c92a59ecSDuan Jiong const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); 1594c92a59ecSDuan Jiong struct dst_entry *dst; 1595c92a59ecSDuan Jiong struct flowi6 fl6; 1596c92a59ecSDuan Jiong 1597c92a59ecSDuan Jiong memset(&fl6, 0, sizeof(fl6)); 1598e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 1599c92a59ecSDuan Jiong fl6.flowi6_oif = oif; 1600c92a59ecSDuan Jiong fl6.flowi6_mark = mark; 1601c92a59ecSDuan Jiong fl6.daddr = msg->dest; 1602c92a59ecSDuan Jiong fl6.saddr = iph->daddr; 1603e2d118a1SLorenzo Colitti fl6.flowi6_uid = sock_net_uid(net, NULL); 1604c92a59ecSDuan Jiong 1605b55b76b2SDuan Jiong dst = ip6_route_redirect(net, &fl6, &iph->saddr); 1606c92a59ecSDuan Jiong rt6_do_redirect(dst, NULL, skb); 1607c92a59ecSDuan Jiong dst_release(dst); 1608c92a59ecSDuan Jiong } 1609c92a59ecSDuan Jiong 16103a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) 16113a5ad2eeSDavid S. Miller { 1612e2d118a1SLorenzo Colitti ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark, 1613e2d118a1SLorenzo Colitti sk->sk_uid); 16143a5ad2eeSDavid S. Miller } 16153a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect); 16163a5ad2eeSDavid S. Miller 16170dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 16181da177e4SLinus Torvalds { 16190dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 16200dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 16210dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 16220dbaee3bSDavid S. Miller 16231da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 16241da177e4SLinus Torvalds 16255578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 16265578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 16271da177e4SLinus Torvalds 16281da177e4SLinus Torvalds /* 16291da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 16301da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 16311da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 16321da177e4SLinus Torvalds * rely only on pmtu discovery" 16331da177e4SLinus Torvalds */ 16341da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 16351da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 16361da177e4SLinus Torvalds return mtu; 16371da177e4SLinus Torvalds } 16381da177e4SLinus Torvalds 1639ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst) 1640d33e4553SDavid S. Miller { 16414b32b5adSMartin KaFai Lau const struct rt6_info *rt = (const struct rt6_info *)dst; 16424b32b5adSMartin KaFai Lau unsigned int mtu = rt->rt6i_pmtu; 1643d33e4553SDavid S. Miller struct inet6_dev *idev; 1644618f9bc7SSteffen Klassert 1645618f9bc7SSteffen Klassert if (mtu) 164630f78d8eSEric Dumazet goto out; 1647618f9bc7SSteffen Klassert 16484b32b5adSMartin KaFai Lau mtu = dst_metric_raw(dst, RTAX_MTU); 16494b32b5adSMartin KaFai Lau if (mtu) 16504b32b5adSMartin KaFai Lau goto out; 16514b32b5adSMartin KaFai Lau 1652618f9bc7SSteffen Klassert mtu = IPV6_MIN_MTU; 1653d33e4553SDavid S. Miller 1654d33e4553SDavid S. Miller rcu_read_lock(); 1655d33e4553SDavid S. Miller idev = __in6_dev_get(dst->dev); 1656d33e4553SDavid S. Miller if (idev) 1657d33e4553SDavid S. Miller mtu = idev->cnf.mtu6; 1658d33e4553SDavid S. Miller rcu_read_unlock(); 1659d33e4553SDavid S. Miller 166030f78d8eSEric Dumazet out: 166114972cbdSRoopa Prabhu mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 166214972cbdSRoopa Prabhu 166314972cbdSRoopa Prabhu return mtu - lwtunnel_headroom(dst->lwtstate, mtu); 1664d33e4553SDavid S. Miller } 1665d33e4553SDavid S. Miller 16663b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list; 16673b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock); 16685d0bbeebSThomas Graf 16693b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 167087a11578SDavid S. Miller struct flowi6 *fl6) 16711da177e4SLinus Torvalds { 167287a11578SDavid S. Miller struct dst_entry *dst; 16731da177e4SLinus Torvalds struct rt6_info *rt; 16741da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 1675c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 16761da177e4SLinus Torvalds 167738308473SDavid S. Miller if (unlikely(!idev)) 1678122bdf67SEric Dumazet return ERR_PTR(-ENODEV); 16791da177e4SLinus Torvalds 1680ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, dev, 0); 168138308473SDavid S. Miller if (unlikely(!rt)) { 16821da177e4SLinus Torvalds in6_dev_put(idev); 168387a11578SDavid S. Miller dst = ERR_PTR(-ENOMEM); 16841da177e4SLinus Torvalds goto out; 16851da177e4SLinus Torvalds } 16861da177e4SLinus Torvalds 16878e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 16888e2ec639SYan, Zheng rt->dst.output = ip6_output; 1689550bab42SJulian Anastasov rt->rt6i_gateway = fl6->daddr; 169087a11578SDavid S. Miller rt->rt6i_dst.addr = fl6->daddr; 16918e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 16928e2ec639SYan, Zheng rt->rt6i_idev = idev; 169314edd87dSLi RongQing dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); 16941da177e4SLinus Torvalds 16953b00944cSYOSHIFUJI Hideaki spin_lock_bh(&icmp6_dst_lock); 1696d8d1f30bSChangli Gao rt->dst.next = icmp6_dst_gc_list; 1697d8d1f30bSChangli Gao icmp6_dst_gc_list = &rt->dst; 16983b00944cSYOSHIFUJI Hideaki spin_unlock_bh(&icmp6_dst_lock); 16991da177e4SLinus Torvalds 17005578689aSDaniel Lezcano fib6_force_start_gc(net); 17011da177e4SLinus Torvalds 170287a11578SDavid S. Miller dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); 170387a11578SDavid S. Miller 17041da177e4SLinus Torvalds out: 170587a11578SDavid S. Miller return dst; 17061da177e4SLinus Torvalds } 17071da177e4SLinus Torvalds 17083d0f24a7SStephen Hemminger int icmp6_dst_gc(void) 17091da177e4SLinus Torvalds { 1710e9476e95SHagen Paul Pfeifer struct dst_entry *dst, **pprev; 17113d0f24a7SStephen Hemminger int more = 0; 17121da177e4SLinus Torvalds 17133b00944cSYOSHIFUJI Hideaki spin_lock_bh(&icmp6_dst_lock); 17143b00944cSYOSHIFUJI Hideaki pprev = &icmp6_dst_gc_list; 17155d0bbeebSThomas Graf 17161da177e4SLinus Torvalds while ((dst = *pprev) != NULL) { 17171da177e4SLinus Torvalds if (!atomic_read(&dst->__refcnt)) { 17181da177e4SLinus Torvalds *pprev = dst->next; 17191da177e4SLinus Torvalds dst_free(dst); 17201da177e4SLinus Torvalds } else { 17211da177e4SLinus Torvalds pprev = &dst->next; 17223d0f24a7SStephen Hemminger ++more; 17231da177e4SLinus Torvalds } 17241da177e4SLinus Torvalds } 17251da177e4SLinus Torvalds 17263b00944cSYOSHIFUJI Hideaki spin_unlock_bh(&icmp6_dst_lock); 17275d0bbeebSThomas Graf 17283d0f24a7SStephen Hemminger return more; 17291da177e4SLinus Torvalds } 17301da177e4SLinus Torvalds 17311e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg), 17321e493d19SDavid S. Miller void *arg) 17331e493d19SDavid S. Miller { 17341e493d19SDavid S. Miller struct dst_entry *dst, **pprev; 17351e493d19SDavid S. Miller 17361e493d19SDavid S. Miller spin_lock_bh(&icmp6_dst_lock); 17371e493d19SDavid S. Miller pprev = &icmp6_dst_gc_list; 17381e493d19SDavid S. Miller while ((dst = *pprev) != NULL) { 17391e493d19SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 17401e493d19SDavid S. Miller if (func(rt, arg)) { 17411e493d19SDavid S. Miller *pprev = dst->next; 17421e493d19SDavid S. Miller dst_free(dst); 17431e493d19SDavid S. Miller } else { 17441e493d19SDavid S. Miller pprev = &dst->next; 17451e493d19SDavid S. Miller } 17461e493d19SDavid S. Miller } 17471e493d19SDavid S. Miller spin_unlock_bh(&icmp6_dst_lock); 17481e493d19SDavid S. Miller } 17491e493d19SDavid S. Miller 1750569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops) 17511da177e4SLinus Torvalds { 175286393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 17537019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 17547019b78eSDaniel Lezcano int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; 17557019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 17567019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 17577019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 1758fc66f95cSEric Dumazet int entries; 17591da177e4SLinus Torvalds 1760fc66f95cSEric Dumazet entries = dst_entries_get_fast(ops); 176149a18d86SMichal Kubeček if (time_after(rt_last_gc + rt_min_interval, jiffies) && 1762fc66f95cSEric Dumazet entries <= rt_max_size) 17631da177e4SLinus Torvalds goto out; 17641da177e4SLinus Torvalds 17656891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire++; 176614956643SLi RongQing fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true); 1767fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 1768fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 17697019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; 17701da177e4SLinus Torvalds out: 17717019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; 1772fc66f95cSEric Dumazet return entries > rt_max_size; 17731da177e4SLinus Torvalds } 17741da177e4SLinus Torvalds 1775e715b6d3SFlorian Westphal static int ip6_convert_metrics(struct mx6_config *mxc, 1776e715b6d3SFlorian Westphal const struct fib6_config *cfg) 1777e715b6d3SFlorian Westphal { 1778c3a8d947SDaniel Borkmann bool ecn_ca = false; 1779e715b6d3SFlorian Westphal struct nlattr *nla; 1780e715b6d3SFlorian Westphal int remaining; 1781e715b6d3SFlorian Westphal u32 *mp; 1782e715b6d3SFlorian Westphal 178363159f29SIan Morris if (!cfg->fc_mx) 1784e715b6d3SFlorian Westphal return 0; 1785e715b6d3SFlorian Westphal 1786e715b6d3SFlorian Westphal mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL); 1787e715b6d3SFlorian Westphal if (unlikely(!mp)) 1788e715b6d3SFlorian Westphal return -ENOMEM; 1789e715b6d3SFlorian Westphal 1790e715b6d3SFlorian Westphal nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { 1791e715b6d3SFlorian Westphal int type = nla_type(nla); 1792ea697639SDaniel Borkmann u32 val; 1793ea697639SDaniel Borkmann 17941bb14807SDaniel Borkmann if (!type) 17951bb14807SDaniel Borkmann continue; 1796e715b6d3SFlorian Westphal if (unlikely(type > RTAX_MAX)) 1797e715b6d3SFlorian Westphal goto err; 17981bb14807SDaniel Borkmann 1799ea697639SDaniel Borkmann if (type == RTAX_CC_ALGO) { 1800ea697639SDaniel Borkmann char tmp[TCP_CA_NAME_MAX]; 1801e715b6d3SFlorian Westphal 1802ea697639SDaniel Borkmann nla_strlcpy(tmp, nla, sizeof(tmp)); 1803c3a8d947SDaniel Borkmann val = tcp_ca_get_key_by_name(tmp, &ecn_ca); 1804ea697639SDaniel Borkmann if (val == TCP_CA_UNSPEC) 1805ea697639SDaniel Borkmann goto err; 1806ea697639SDaniel Borkmann } else { 1807ea697639SDaniel Borkmann val = nla_get_u32(nla); 1808ea697639SDaniel Borkmann } 1809626abd59SPaolo Abeni if (type == RTAX_HOPLIMIT && val > 255) 1810626abd59SPaolo Abeni val = 255; 1811b8d3e416SDaniel Borkmann if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK)) 1812b8d3e416SDaniel Borkmann goto err; 1813ea697639SDaniel Borkmann 1814ea697639SDaniel Borkmann mp[type - 1] = val; 1815e715b6d3SFlorian Westphal __set_bit(type - 1, mxc->mx_valid); 1816e715b6d3SFlorian Westphal } 1817e715b6d3SFlorian Westphal 1818c3a8d947SDaniel Borkmann if (ecn_ca) { 1819c3a8d947SDaniel Borkmann __set_bit(RTAX_FEATURES - 1, mxc->mx_valid); 1820c3a8d947SDaniel Borkmann mp[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA; 1821c3a8d947SDaniel Borkmann } 1822e715b6d3SFlorian Westphal 1823c3a8d947SDaniel Borkmann mxc->mx = mp; 1824e715b6d3SFlorian Westphal return 0; 1825e715b6d3SFlorian Westphal err: 1826e715b6d3SFlorian Westphal kfree(mp); 1827e715b6d3SFlorian Westphal return -EINVAL; 1828e715b6d3SFlorian Westphal } 18291da177e4SLinus Torvalds 18308c14586fSDavid Ahern static struct rt6_info *ip6_nh_lookup_table(struct net *net, 18318c14586fSDavid Ahern struct fib6_config *cfg, 18328c14586fSDavid Ahern const struct in6_addr *gw_addr) 18338c14586fSDavid Ahern { 18348c14586fSDavid Ahern struct flowi6 fl6 = { 18358c14586fSDavid Ahern .flowi6_oif = cfg->fc_ifindex, 18368c14586fSDavid Ahern .daddr = *gw_addr, 18378c14586fSDavid Ahern .saddr = cfg->fc_prefsrc, 18388c14586fSDavid Ahern }; 18398c14586fSDavid Ahern struct fib6_table *table; 18408c14586fSDavid Ahern struct rt6_info *rt; 1841d5d32e4bSDavid Ahern int flags = RT6_LOOKUP_F_IFACE | RT6_LOOKUP_F_IGNORE_LINKSTATE; 18428c14586fSDavid Ahern 18438c14586fSDavid Ahern table = fib6_get_table(net, cfg->fc_table); 18448c14586fSDavid Ahern if (!table) 18458c14586fSDavid Ahern return NULL; 18468c14586fSDavid Ahern 18478c14586fSDavid Ahern if (!ipv6_addr_any(&cfg->fc_prefsrc)) 18488c14586fSDavid Ahern flags |= RT6_LOOKUP_F_HAS_SADDR; 18498c14586fSDavid Ahern 18508c14586fSDavid Ahern rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, flags); 18518c14586fSDavid Ahern 18528c14586fSDavid Ahern /* if table lookup failed, fall back to full lookup */ 18538c14586fSDavid Ahern if (rt == net->ipv6.ip6_null_entry) { 18548c14586fSDavid Ahern ip6_rt_put(rt); 18558c14586fSDavid Ahern rt = NULL; 18568c14586fSDavid Ahern } 18578c14586fSDavid Ahern 18588c14586fSDavid Ahern return rt; 18598c14586fSDavid Ahern } 18608c14586fSDavid Ahern 1861333c4301SDavid Ahern static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg, 1862333c4301SDavid Ahern struct netlink_ext_ack *extack) 18631da177e4SLinus Torvalds { 18645578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 18651da177e4SLinus Torvalds struct rt6_info *rt = NULL; 18661da177e4SLinus Torvalds struct net_device *dev = NULL; 18671da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 1868c71099acSThomas Graf struct fib6_table *table; 18691da177e4SLinus Torvalds int addr_type; 18708c5b83f0SRoopa Prabhu int err = -EINVAL; 18711da177e4SLinus Torvalds 1872557c44beSDavid Ahern /* RTF_PCPU is an internal flag; can not be set by userspace */ 1873d5d531cbSDavid Ahern if (cfg->fc_flags & RTF_PCPU) { 1874d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU"); 1875557c44beSDavid Ahern goto out; 1876d5d531cbSDavid Ahern } 1877557c44beSDavid Ahern 1878d5d531cbSDavid Ahern if (cfg->fc_dst_len > 128) { 1879d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid prefix length"); 18808c5b83f0SRoopa Prabhu goto out; 1881d5d531cbSDavid Ahern } 1882d5d531cbSDavid Ahern if (cfg->fc_src_len > 128) { 1883d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address length"); 1884d5d531cbSDavid Ahern goto out; 1885d5d531cbSDavid Ahern } 18861da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 1887d5d531cbSDavid Ahern if (cfg->fc_src_len) { 1888d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 1889d5d531cbSDavid Ahern "Specifying source address requires IPV6_SUBTREES to be enabled"); 18908c5b83f0SRoopa Prabhu goto out; 1891d5d531cbSDavid Ahern } 18921da177e4SLinus Torvalds #endif 189386872cb5SThomas Graf if (cfg->fc_ifindex) { 18941da177e4SLinus Torvalds err = -ENODEV; 18955578689aSDaniel Lezcano dev = dev_get_by_index(net, cfg->fc_ifindex); 18961da177e4SLinus Torvalds if (!dev) 18971da177e4SLinus Torvalds goto out; 18981da177e4SLinus Torvalds idev = in6_dev_get(dev); 18991da177e4SLinus Torvalds if (!idev) 19001da177e4SLinus Torvalds goto out; 19011da177e4SLinus Torvalds } 19021da177e4SLinus Torvalds 190386872cb5SThomas Graf if (cfg->fc_metric == 0) 190486872cb5SThomas Graf cfg->fc_metric = IP6_RT_PRIO_USER; 19051da177e4SLinus Torvalds 1906c71099acSThomas Graf err = -ENOBUFS; 190738308473SDavid S. Miller if (cfg->fc_nlinfo.nlh && 1908d71314b4SMatti Vaittinen !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { 1909d71314b4SMatti Vaittinen table = fib6_get_table(net, cfg->fc_table); 191038308473SDavid S. Miller if (!table) { 1911f3213831SJoe Perches pr_warn("NLM_F_CREATE should be specified when creating new route\n"); 1912d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 1913d71314b4SMatti Vaittinen } 1914d71314b4SMatti Vaittinen } else { 1915d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 1916d71314b4SMatti Vaittinen } 191738308473SDavid S. Miller 191838308473SDavid S. Miller if (!table) 1919c71099acSThomas Graf goto out; 1920c71099acSThomas Graf 1921ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, NULL, 1922ad706862SMartin KaFai Lau (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT); 19231da177e4SLinus Torvalds 192438308473SDavid S. Miller if (!rt) { 19251da177e4SLinus Torvalds err = -ENOMEM; 19261da177e4SLinus Torvalds goto out; 19271da177e4SLinus Torvalds } 19281da177e4SLinus Torvalds 19291716a961SGao feng if (cfg->fc_flags & RTF_EXPIRES) 19301716a961SGao feng rt6_set_expires(rt, jiffies + 19311716a961SGao feng clock_t_to_jiffies(cfg->fc_expires)); 19321716a961SGao feng else 19331716a961SGao feng rt6_clean_expires(rt); 19341da177e4SLinus Torvalds 193586872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 193686872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 193786872cb5SThomas Graf rt->rt6i_protocol = cfg->fc_protocol; 193886872cb5SThomas Graf 193986872cb5SThomas Graf addr_type = ipv6_addr_type(&cfg->fc_dst); 19401da177e4SLinus Torvalds 19411da177e4SLinus Torvalds if (addr_type & IPV6_ADDR_MULTICAST) 1942d8d1f30bSChangli Gao rt->dst.input = ip6_mc_input; 1943ab79ad14SMaciej Żenczykowski else if (cfg->fc_flags & RTF_LOCAL) 1944ab79ad14SMaciej Żenczykowski rt->dst.input = ip6_input; 19451da177e4SLinus Torvalds else 1946d8d1f30bSChangli Gao rt->dst.input = ip6_forward; 19471da177e4SLinus Torvalds 1948d8d1f30bSChangli Gao rt->dst.output = ip6_output; 19491da177e4SLinus Torvalds 195019e42e45SRoopa Prabhu if (cfg->fc_encap) { 195119e42e45SRoopa Prabhu struct lwtunnel_state *lwtstate; 195219e42e45SRoopa Prabhu 195330357d7dSDavid Ahern err = lwtunnel_build_state(cfg->fc_encap_type, 1954127eb7cdSTom Herbert cfg->fc_encap, AF_INET6, cfg, 19559ae28727SDavid Ahern &lwtstate, extack); 195619e42e45SRoopa Prabhu if (err) 195719e42e45SRoopa Prabhu goto out; 195861adedf3SJiri Benc rt->dst.lwtstate = lwtstate_get(lwtstate); 195961adedf3SJiri Benc if (lwtunnel_output_redirect(rt->dst.lwtstate)) { 196061adedf3SJiri Benc rt->dst.lwtstate->orig_output = rt->dst.output; 196161adedf3SJiri Benc rt->dst.output = lwtunnel_output; 196219e42e45SRoopa Prabhu } 196361adedf3SJiri Benc if (lwtunnel_input_redirect(rt->dst.lwtstate)) { 196461adedf3SJiri Benc rt->dst.lwtstate->orig_input = rt->dst.input; 196561adedf3SJiri Benc rt->dst.input = lwtunnel_input; 196625368623STom Herbert } 196725368623STom Herbert } 196819e42e45SRoopa Prabhu 196986872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 197086872cb5SThomas Graf rt->rt6i_dst.plen = cfg->fc_dst_len; 1971afc4eef8SMartin KaFai Lau if (rt->rt6i_dst.plen == 128) 197211d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 19731da177e4SLinus Torvalds 19741da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 197586872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len); 197686872cb5SThomas Graf rt->rt6i_src.plen = cfg->fc_src_len; 19771da177e4SLinus Torvalds #endif 19781da177e4SLinus Torvalds 197986872cb5SThomas Graf rt->rt6i_metric = cfg->fc_metric; 19801da177e4SLinus Torvalds 19811da177e4SLinus Torvalds /* We cannot add true routes via loopback here, 19821da177e4SLinus Torvalds they would result in kernel looping; promote them to reject routes 19831da177e4SLinus Torvalds */ 198486872cb5SThomas Graf if ((cfg->fc_flags & RTF_REJECT) || 198538308473SDavid S. Miller (dev && (dev->flags & IFF_LOOPBACK) && 198638308473SDavid S. Miller !(addr_type & IPV6_ADDR_LOOPBACK) && 198738308473SDavid S. Miller !(cfg->fc_flags & RTF_LOCAL))) { 19881da177e4SLinus Torvalds /* hold loopback dev/idev if we haven't done so. */ 19895578689aSDaniel Lezcano if (dev != net->loopback_dev) { 19901da177e4SLinus Torvalds if (dev) { 19911da177e4SLinus Torvalds dev_put(dev); 19921da177e4SLinus Torvalds in6_dev_put(idev); 19931da177e4SLinus Torvalds } 19945578689aSDaniel Lezcano dev = net->loopback_dev; 19951da177e4SLinus Torvalds dev_hold(dev); 19961da177e4SLinus Torvalds idev = in6_dev_get(dev); 19971da177e4SLinus Torvalds if (!idev) { 19981da177e4SLinus Torvalds err = -ENODEV; 19991da177e4SLinus Torvalds goto out; 20001da177e4SLinus Torvalds } 20011da177e4SLinus Torvalds } 20021da177e4SLinus Torvalds rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP; 2003ef2c7d7bSNicolas Dichtel switch (cfg->fc_type) { 2004ef2c7d7bSNicolas Dichtel case RTN_BLACKHOLE: 2005ef2c7d7bSNicolas Dichtel rt->dst.error = -EINVAL; 2006ede2059dSEric W. Biederman rt->dst.output = dst_discard_out; 20077150aedeSKamala R rt->dst.input = dst_discard; 2008ef2c7d7bSNicolas Dichtel break; 2009ef2c7d7bSNicolas Dichtel case RTN_PROHIBIT: 2010ef2c7d7bSNicolas Dichtel rt->dst.error = -EACCES; 20117150aedeSKamala R rt->dst.output = ip6_pkt_prohibit_out; 20127150aedeSKamala R rt->dst.input = ip6_pkt_prohibit; 2013ef2c7d7bSNicolas Dichtel break; 2014b4949ab2SNicolas Dichtel case RTN_THROW: 20150315e382SNikola Forró case RTN_UNREACHABLE: 2016ef2c7d7bSNicolas Dichtel default: 20177150aedeSKamala R rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN 20180315e382SNikola Forró : (cfg->fc_type == RTN_UNREACHABLE) 20190315e382SNikola Forró ? -EHOSTUNREACH : -ENETUNREACH; 20207150aedeSKamala R rt->dst.output = ip6_pkt_discard_out; 20217150aedeSKamala R rt->dst.input = ip6_pkt_discard; 2022ef2c7d7bSNicolas Dichtel break; 2023ef2c7d7bSNicolas Dichtel } 20241da177e4SLinus Torvalds goto install_route; 20251da177e4SLinus Torvalds } 20261da177e4SLinus Torvalds 202786872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY) { 2028b71d1d42SEric Dumazet const struct in6_addr *gw_addr; 20291da177e4SLinus Torvalds int gwa_type; 20301da177e4SLinus Torvalds 203186872cb5SThomas Graf gw_addr = &cfg->fc_gateway; 2032330567b7SFlorian Westphal gwa_type = ipv6_addr_type(gw_addr); 203348ed7b26SFlorian Westphal 203448ed7b26SFlorian Westphal /* if gw_addr is local we will fail to detect this in case 203548ed7b26SFlorian Westphal * address is still TENTATIVE (DAD in progress). rt6_lookup() 203648ed7b26SFlorian Westphal * will return already-added prefix route via interface that 203748ed7b26SFlorian Westphal * prefix route was assigned to, which might be non-loopback. 203848ed7b26SFlorian Westphal */ 203948ed7b26SFlorian Westphal err = -EINVAL; 2040330567b7SFlorian Westphal if (ipv6_chk_addr_and_flags(net, gw_addr, 2041330567b7SFlorian Westphal gwa_type & IPV6_ADDR_LINKLOCAL ? 2042d5d531cbSDavid Ahern dev : NULL, 0, 0)) { 2043d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid gateway address"); 204448ed7b26SFlorian Westphal goto out; 2045d5d531cbSDavid Ahern } 20464e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = *gw_addr; 20471da177e4SLinus Torvalds 20481da177e4SLinus Torvalds if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) { 20498c14586fSDavid Ahern struct rt6_info *grt = NULL; 20501da177e4SLinus Torvalds 20511da177e4SLinus Torvalds /* IPv6 strictly inhibits using not link-local 20521da177e4SLinus Torvalds addresses as nexthop address. 20531da177e4SLinus Torvalds Otherwise, router will not able to send redirects. 20541da177e4SLinus Torvalds It is very good, but in some (rare!) circumstances 20551da177e4SLinus Torvalds (SIT, PtP, NBMA NOARP links) it is handy to allow 20561da177e4SLinus Torvalds some exceptions. --ANK 205796d5822cSErik Nordmark We allow IPv4-mapped nexthops to support RFC4798-type 205896d5822cSErik Nordmark addressing 20591da177e4SLinus Torvalds */ 206096d5822cSErik Nordmark if (!(gwa_type & (IPV6_ADDR_UNICAST | 2061d5d531cbSDavid Ahern IPV6_ADDR_MAPPED))) { 2062d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2063d5d531cbSDavid Ahern "Invalid gateway address"); 20641da177e4SLinus Torvalds goto out; 2065d5d531cbSDavid Ahern } 20661da177e4SLinus Torvalds 2067a435a07fSVincent Bernat if (cfg->fc_table) { 20688c14586fSDavid Ahern grt = ip6_nh_lookup_table(net, cfg, gw_addr); 20698c14586fSDavid Ahern 2070a435a07fSVincent Bernat if (grt) { 2071a435a07fSVincent Bernat if (grt->rt6i_flags & RTF_GATEWAY || 2072a435a07fSVincent Bernat (dev && dev != grt->dst.dev)) { 2073a435a07fSVincent Bernat ip6_rt_put(grt); 2074a435a07fSVincent Bernat grt = NULL; 2075a435a07fSVincent Bernat } 2076a435a07fSVincent Bernat } 2077a435a07fSVincent Bernat } 2078a435a07fSVincent Bernat 20798c14586fSDavid Ahern if (!grt) 20808c14586fSDavid Ahern grt = rt6_lookup(net, gw_addr, NULL, 20818c14586fSDavid Ahern cfg->fc_ifindex, 1); 20821da177e4SLinus Torvalds 20831da177e4SLinus Torvalds err = -EHOSTUNREACH; 208438308473SDavid S. Miller if (!grt) 20851da177e4SLinus Torvalds goto out; 20861da177e4SLinus Torvalds if (dev) { 2087d1918542SDavid S. Miller if (dev != grt->dst.dev) { 208894e187c0SAmerigo Wang ip6_rt_put(grt); 20891da177e4SLinus Torvalds goto out; 20901da177e4SLinus Torvalds } 20911da177e4SLinus Torvalds } else { 2092d1918542SDavid S. Miller dev = grt->dst.dev; 20931da177e4SLinus Torvalds idev = grt->rt6i_idev; 20941da177e4SLinus Torvalds dev_hold(dev); 20951da177e4SLinus Torvalds in6_dev_hold(grt->rt6i_idev); 20961da177e4SLinus Torvalds } 20971da177e4SLinus Torvalds if (!(grt->rt6i_flags & RTF_GATEWAY)) 20981da177e4SLinus Torvalds err = 0; 209994e187c0SAmerigo Wang ip6_rt_put(grt); 21001da177e4SLinus Torvalds 21011da177e4SLinus Torvalds if (err) 21021da177e4SLinus Torvalds goto out; 21031da177e4SLinus Torvalds } 21041da177e4SLinus Torvalds err = -EINVAL; 2105d5d531cbSDavid Ahern if (!dev) { 2106d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Egress device not specified"); 21071da177e4SLinus Torvalds goto out; 2108d5d531cbSDavid Ahern } else if (dev->flags & IFF_LOOPBACK) { 2109d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2110d5d531cbSDavid Ahern "Egress device can not be loopback device for this route"); 2111d5d531cbSDavid Ahern goto out; 2112d5d531cbSDavid Ahern } 21131da177e4SLinus Torvalds } 21141da177e4SLinus Torvalds 21151da177e4SLinus Torvalds err = -ENODEV; 211638308473SDavid S. Miller if (!dev) 21171da177e4SLinus Torvalds goto out; 21181da177e4SLinus Torvalds 2119c3968a85SDaniel Walter if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 2120c3968a85SDaniel Walter if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 2121d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address"); 2122c3968a85SDaniel Walter err = -EINVAL; 2123c3968a85SDaniel Walter goto out; 2124c3968a85SDaniel Walter } 21254e3fd7a0SAlexey Dobriyan rt->rt6i_prefsrc.addr = cfg->fc_prefsrc; 2126c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 128; 2127c3968a85SDaniel Walter } else 2128c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 2129c3968a85SDaniel Walter 213086872cb5SThomas Graf rt->rt6i_flags = cfg->fc_flags; 21311da177e4SLinus Torvalds 21321da177e4SLinus Torvalds install_route: 2133d8d1f30bSChangli Gao rt->dst.dev = dev; 21341da177e4SLinus Torvalds rt->rt6i_idev = idev; 2135c71099acSThomas Graf rt->rt6i_table = table; 213663152fc0SDaniel Lezcano 2137c346dca1SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = dev_net(dev); 213863152fc0SDaniel Lezcano 21398c5b83f0SRoopa Prabhu return rt; 21401da177e4SLinus Torvalds out: 21411da177e4SLinus Torvalds if (dev) 21421da177e4SLinus Torvalds dev_put(dev); 21431da177e4SLinus Torvalds if (idev) 21441da177e4SLinus Torvalds in6_dev_put(idev); 21451cfb71eeSWei Wang if (rt) { 21461cfb71eeSWei Wang dst_release(&rt->dst); 2147d8d1f30bSChangli Gao dst_free(&rt->dst); 21481cfb71eeSWei Wang } 21496b9ea5a6SRoopa Prabhu 21508c5b83f0SRoopa Prabhu return ERR_PTR(err); 21516b9ea5a6SRoopa Prabhu } 21526b9ea5a6SRoopa Prabhu 2153333c4301SDavid Ahern int ip6_route_add(struct fib6_config *cfg, 2154333c4301SDavid Ahern struct netlink_ext_ack *extack) 21556b9ea5a6SRoopa Prabhu { 21566b9ea5a6SRoopa Prabhu struct mx6_config mxc = { .mx = NULL, }; 21578c5b83f0SRoopa Prabhu struct rt6_info *rt; 21586b9ea5a6SRoopa Prabhu int err; 21596b9ea5a6SRoopa Prabhu 2160333c4301SDavid Ahern rt = ip6_route_info_create(cfg, extack); 21618c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 21628c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 21638c5b83f0SRoopa Prabhu rt = NULL; 21646b9ea5a6SRoopa Prabhu goto out; 21658c5b83f0SRoopa Prabhu } 21666b9ea5a6SRoopa Prabhu 21676b9ea5a6SRoopa Prabhu err = ip6_convert_metrics(&mxc, cfg); 21686b9ea5a6SRoopa Prabhu if (err) 21696b9ea5a6SRoopa Prabhu goto out; 21706b9ea5a6SRoopa Prabhu 2171333c4301SDavid Ahern err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, &mxc, extack); 21726b9ea5a6SRoopa Prabhu 21736b9ea5a6SRoopa Prabhu kfree(mxc.mx); 21746b9ea5a6SRoopa Prabhu 21756b9ea5a6SRoopa Prabhu return err; 21766b9ea5a6SRoopa Prabhu out: 21771cfb71eeSWei Wang if (rt) { 21781cfb71eeSWei Wang dst_release(&rt->dst); 21796b9ea5a6SRoopa Prabhu dst_free(&rt->dst); 21801cfb71eeSWei Wang } 21816b9ea5a6SRoopa Prabhu 21821da177e4SLinus Torvalds return err; 21831da177e4SLinus Torvalds } 21841da177e4SLinus Torvalds 218586872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info) 21861da177e4SLinus Torvalds { 21871da177e4SLinus Torvalds int err; 2188c71099acSThomas Graf struct fib6_table *table; 2189d1918542SDavid S. Miller struct net *net = dev_net(rt->dst.dev); 21901da177e4SLinus Torvalds 21918e3d5be7SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry || 21928e3d5be7SMartin KaFai Lau rt->dst.flags & DST_NOCACHE) { 21936825a26cSGao feng err = -ENOENT; 21946825a26cSGao feng goto out; 21956825a26cSGao feng } 21966c813a72SPatrick McHardy 2197c71099acSThomas Graf table = rt->rt6i_table; 2198c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 219986872cb5SThomas Graf err = fib6_del(rt, info); 2200c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 22011da177e4SLinus Torvalds 22026825a26cSGao feng out: 220394e187c0SAmerigo Wang ip6_rt_put(rt); 22041da177e4SLinus Torvalds return err; 22051da177e4SLinus Torvalds } 22061da177e4SLinus Torvalds 2207e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt) 2208e0a1ad73SThomas Graf { 22094d1169c1SDenis V. Lunev struct nl_info info = { 2210d1918542SDavid S. Miller .nl_net = dev_net(rt->dst.dev), 22114d1169c1SDenis V. Lunev }; 2212528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 2213e0a1ad73SThomas Graf } 2214e0a1ad73SThomas Graf 22150ae81335SDavid Ahern static int __ip6_del_rt_siblings(struct rt6_info *rt, struct fib6_config *cfg) 22160ae81335SDavid Ahern { 22170ae81335SDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 2218e3330039SWANG Cong struct net *net = info->nl_net; 221916a16cd3SDavid Ahern struct sk_buff *skb = NULL; 22200ae81335SDavid Ahern struct fib6_table *table; 2221e3330039SWANG Cong int err = -ENOENT; 22220ae81335SDavid Ahern 2223e3330039SWANG Cong if (rt == net->ipv6.ip6_null_entry) 2224e3330039SWANG Cong goto out_put; 22250ae81335SDavid Ahern table = rt->rt6i_table; 22260ae81335SDavid Ahern write_lock_bh(&table->tb6_lock); 22270ae81335SDavid Ahern 22280ae81335SDavid Ahern if (rt->rt6i_nsiblings && cfg->fc_delete_all_nh) { 22290ae81335SDavid Ahern struct rt6_info *sibling, *next_sibling; 22300ae81335SDavid Ahern 223116a16cd3SDavid Ahern /* prefer to send a single notification with all hops */ 223216a16cd3SDavid Ahern skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 223316a16cd3SDavid Ahern if (skb) { 223416a16cd3SDavid Ahern u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 223516a16cd3SDavid Ahern 2236e3330039SWANG Cong if (rt6_fill_node(net, skb, rt, 223716a16cd3SDavid Ahern NULL, NULL, 0, RTM_DELROUTE, 223816a16cd3SDavid Ahern info->portid, seq, 0) < 0) { 223916a16cd3SDavid Ahern kfree_skb(skb); 224016a16cd3SDavid Ahern skb = NULL; 224116a16cd3SDavid Ahern } else 224216a16cd3SDavid Ahern info->skip_notify = 1; 224316a16cd3SDavid Ahern } 224416a16cd3SDavid Ahern 22450ae81335SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 22460ae81335SDavid Ahern &rt->rt6i_siblings, 22470ae81335SDavid Ahern rt6i_siblings) { 22480ae81335SDavid Ahern err = fib6_del(sibling, info); 22490ae81335SDavid Ahern if (err) 2250e3330039SWANG Cong goto out_unlock; 22510ae81335SDavid Ahern } 22520ae81335SDavid Ahern } 22530ae81335SDavid Ahern 22540ae81335SDavid Ahern err = fib6_del(rt, info); 2255e3330039SWANG Cong out_unlock: 22560ae81335SDavid Ahern write_unlock_bh(&table->tb6_lock); 2257e3330039SWANG Cong out_put: 22580ae81335SDavid Ahern ip6_rt_put(rt); 225916a16cd3SDavid Ahern 226016a16cd3SDavid Ahern if (skb) { 2261e3330039SWANG Cong rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 226216a16cd3SDavid Ahern info->nlh, gfp_any()); 226316a16cd3SDavid Ahern } 22640ae81335SDavid Ahern return err; 22650ae81335SDavid Ahern } 22660ae81335SDavid Ahern 2267333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg, 2268333c4301SDavid Ahern struct netlink_ext_ack *extack) 22691da177e4SLinus Torvalds { 2270c71099acSThomas Graf struct fib6_table *table; 22711da177e4SLinus Torvalds struct fib6_node *fn; 22721da177e4SLinus Torvalds struct rt6_info *rt; 22731da177e4SLinus Torvalds int err = -ESRCH; 22741da177e4SLinus Torvalds 22755578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 2276d5d531cbSDavid Ahern if (!table) { 2277d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "FIB table does not exist"); 2278c71099acSThomas Graf return err; 2279d5d531cbSDavid Ahern } 22801da177e4SLinus Torvalds 2281c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 2282c71099acSThomas Graf 2283c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 228486872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 228586872cb5SThomas Graf &cfg->fc_src, cfg->fc_src_len); 22861da177e4SLinus Torvalds 22871da177e4SLinus Torvalds if (fn) { 2288d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 22891f56a01fSMartin KaFai Lau if ((rt->rt6i_flags & RTF_CACHE) && 22901f56a01fSMartin KaFai Lau !(cfg->fc_flags & RTF_CACHE)) 22911f56a01fSMartin KaFai Lau continue; 229286872cb5SThomas Graf if (cfg->fc_ifindex && 2293d1918542SDavid S. Miller (!rt->dst.dev || 2294d1918542SDavid S. Miller rt->dst.dev->ifindex != cfg->fc_ifindex)) 22951da177e4SLinus Torvalds continue; 229686872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 229786872cb5SThomas Graf !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 22981da177e4SLinus Torvalds continue; 229986872cb5SThomas Graf if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric) 23001da177e4SLinus Torvalds continue; 2301c2ed1880SMantas M if (cfg->fc_protocol && cfg->fc_protocol != rt->rt6i_protocol) 2302c2ed1880SMantas M continue; 2303d8d1f30bSChangli Gao dst_hold(&rt->dst); 2304c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 23051da177e4SLinus Torvalds 23060ae81335SDavid Ahern /* if gateway was specified only delete the one hop */ 23070ae81335SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY) 230886872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 23090ae81335SDavid Ahern 23100ae81335SDavid Ahern return __ip6_del_rt_siblings(rt, cfg); 23111da177e4SLinus Torvalds } 23121da177e4SLinus Torvalds } 2313c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 23141da177e4SLinus Torvalds 23151da177e4SLinus Torvalds return err; 23161da177e4SLinus Torvalds } 23171da177e4SLinus Torvalds 23186700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 2319a6279458SYOSHIFUJI Hideaki { 2320a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 2321e8599ff4SDavid S. Miller struct rt6_info *rt, *nrt = NULL; 2322e8599ff4SDavid S. Miller struct ndisc_options ndopts; 2323e8599ff4SDavid S. Miller struct inet6_dev *in6_dev; 2324e8599ff4SDavid S. Miller struct neighbour *neigh; 232571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 struct rd_msg *msg; 23266e157b6aSDavid S. Miller int optlen, on_link; 23276e157b6aSDavid S. Miller u8 *lladdr; 2328e8599ff4SDavid S. Miller 232929a3cad5SSimon Horman optlen = skb_tail_pointer(skb) - skb_transport_header(skb); 233071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 optlen -= sizeof(*msg); 2331e8599ff4SDavid S. Miller 2332e8599ff4SDavid S. Miller if (optlen < 0) { 23336e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); 2334e8599ff4SDavid S. Miller return; 2335e8599ff4SDavid S. Miller } 2336e8599ff4SDavid S. Miller 233771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 msg = (struct rd_msg *)icmp6_hdr(skb); 2338e8599ff4SDavid S. Miller 233971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_is_multicast(&msg->dest)) { 23406e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); 2341e8599ff4SDavid S. Miller return; 2342e8599ff4SDavid S. Miller } 2343e8599ff4SDavid S. Miller 23446e157b6aSDavid S. Miller on_link = 0; 234571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_equal(&msg->dest, &msg->target)) { 2346e8599ff4SDavid S. Miller on_link = 1; 234771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 } else if (ipv6_addr_type(&msg->target) != 2348e8599ff4SDavid S. Miller (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { 23496e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); 2350e8599ff4SDavid S. Miller return; 2351e8599ff4SDavid S. Miller } 2352e8599ff4SDavid S. Miller 2353e8599ff4SDavid S. Miller in6_dev = __in6_dev_get(skb->dev); 2354e8599ff4SDavid S. Miller if (!in6_dev) 2355e8599ff4SDavid S. Miller return; 2356e8599ff4SDavid S. Miller if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) 2357e8599ff4SDavid S. Miller return; 2358e8599ff4SDavid S. Miller 2359e8599ff4SDavid S. Miller /* RFC2461 8.1: 2360e8599ff4SDavid S. Miller * The IP source address of the Redirect MUST be the same as the current 2361e8599ff4SDavid S. Miller * first-hop router for the specified ICMP Destination Address. 2362e8599ff4SDavid S. Miller */ 2363e8599ff4SDavid S. Miller 2364f997c55cSAlexander Aring if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { 2365e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); 2366e8599ff4SDavid S. Miller return; 2367e8599ff4SDavid S. Miller } 23686e157b6aSDavid S. Miller 23696e157b6aSDavid S. Miller lladdr = NULL; 2370e8599ff4SDavid S. Miller if (ndopts.nd_opts_tgt_lladdr) { 2371e8599ff4SDavid S. Miller lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, 2372e8599ff4SDavid S. Miller skb->dev); 2373e8599ff4SDavid S. Miller if (!lladdr) { 2374e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); 2375e8599ff4SDavid S. Miller return; 2376e8599ff4SDavid S. Miller } 2377e8599ff4SDavid S. Miller } 2378e8599ff4SDavid S. Miller 23796e157b6aSDavid S. Miller rt = (struct rt6_info *) dst; 2380ec13ad1dSMatthias Schiffer if (rt->rt6i_flags & RTF_REJECT) { 23816e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); 23826e157b6aSDavid S. Miller return; 23836e157b6aSDavid S. Miller } 23846e157b6aSDavid S. Miller 23856e157b6aSDavid S. Miller /* Redirect received -> path was valid. 23866e157b6aSDavid S. Miller * Look, redirects are sent only in response to data packets, 23876e157b6aSDavid S. Miller * so that this nexthop apparently is reachable. --ANK 23886e157b6aSDavid S. Miller */ 23890dec879fSJulian Anastasov dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); 23906e157b6aSDavid S. Miller 239171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); 2392e8599ff4SDavid S. Miller if (!neigh) 2393e8599ff4SDavid S. Miller return; 2394e8599ff4SDavid S. Miller 23951da177e4SLinus Torvalds /* 23961da177e4SLinus Torvalds * We have finally decided to accept it. 23971da177e4SLinus Torvalds */ 23981da177e4SLinus Torvalds 2399f997c55cSAlexander Aring ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, 24001da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 24011da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 24021da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 2403f997c55cSAlexander Aring NEIGH_UPDATE_F_ISROUTER)), 2404f997c55cSAlexander Aring NDISC_REDIRECT, &ndopts); 24051da177e4SLinus Torvalds 240683a09abdSMartin KaFai Lau nrt = ip6_rt_cache_alloc(rt, &msg->dest, NULL); 240738308473SDavid S. Miller if (!nrt) 24081da177e4SLinus Torvalds goto out; 24091da177e4SLinus Torvalds 24101da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 24111da177e4SLinus Torvalds if (on_link) 24121da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 24131da177e4SLinus Torvalds 24144e3fd7a0SAlexey Dobriyan nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 24151da177e4SLinus Torvalds 241640e22e8fSThomas Graf if (ip6_ins_rt(nrt)) 24171cfb71eeSWei Wang goto out_release; 24181da177e4SLinus Torvalds 2419d8d1f30bSChangli Gao netevent.old = &rt->dst; 2420d8d1f30bSChangli Gao netevent.new = &nrt->dst; 242171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 netevent.daddr = &msg->dest; 242260592833SYOSHIFUJI Hideaki / 吉藤英明 netevent.neigh = neigh; 24238d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 24248d71740cSTom Tucker 24251da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) { 24266e157b6aSDavid S. Miller rt = (struct rt6_info *) dst_clone(&rt->dst); 2427e0a1ad73SThomas Graf ip6_del_rt(rt); 24281da177e4SLinus Torvalds } 24291da177e4SLinus Torvalds 24301cfb71eeSWei Wang out_release: 24311cfb71eeSWei Wang /* Release the reference taken in 24321cfb71eeSWei Wang * ip6_rt_cache_alloc() 24331cfb71eeSWei Wang */ 24341cfb71eeSWei Wang dst_release(&nrt->dst); 24351cfb71eeSWei Wang 24361da177e4SLinus Torvalds out: 2437e8599ff4SDavid S. Miller neigh_release(neigh); 24386e157b6aSDavid S. Miller } 24396e157b6aSDavid S. Miller 24401da177e4SLinus Torvalds /* 24411da177e4SLinus Torvalds * Misc support functions 24421da177e4SLinus Torvalds */ 24431da177e4SLinus Torvalds 24444b32b5adSMartin KaFai Lau static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from) 24454b32b5adSMartin KaFai Lau { 24464b32b5adSMartin KaFai Lau BUG_ON(from->dst.from); 24474b32b5adSMartin KaFai Lau 24484b32b5adSMartin KaFai Lau rt->rt6i_flags &= ~RTF_EXPIRES; 24494b32b5adSMartin KaFai Lau dst_hold(&from->dst); 24504b32b5adSMartin KaFai Lau rt->dst.from = &from->dst; 24514b32b5adSMartin KaFai Lau dst_init_metrics(&rt->dst, dst_metrics_ptr(&from->dst), true); 24524b32b5adSMartin KaFai Lau } 24534b32b5adSMartin KaFai Lau 245483a09abdSMartin KaFai Lau static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort) 24551da177e4SLinus Torvalds { 2456d8d1f30bSChangli Gao rt->dst.input = ort->dst.input; 2457d8d1f30bSChangli Gao rt->dst.output = ort->dst.output; 245883a09abdSMartin KaFai Lau rt->rt6i_dst = ort->rt6i_dst; 2459d8d1f30bSChangli Gao rt->dst.error = ort->dst.error; 24601da177e4SLinus Torvalds rt->rt6i_idev = ort->rt6i_idev; 24611da177e4SLinus Torvalds if (rt->rt6i_idev) 24621da177e4SLinus Torvalds in6_dev_hold(rt->rt6i_idev); 2463d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 24644e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 24651716a961SGao feng rt->rt6i_flags = ort->rt6i_flags; 24661716a961SGao feng rt6_set_from(rt, ort); 246783a09abdSMartin KaFai Lau rt->rt6i_metric = ort->rt6i_metric; 24681da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 246983a09abdSMartin KaFai Lau rt->rt6i_src = ort->rt6i_src; 24701da177e4SLinus Torvalds #endif 247183a09abdSMartin KaFai Lau rt->rt6i_prefsrc = ort->rt6i_prefsrc; 2472c71099acSThomas Graf rt->rt6i_table = ort->rt6i_table; 247361adedf3SJiri Benc rt->dst.lwtstate = lwtstate_get(ort->dst.lwtstate); 24741da177e4SLinus Torvalds } 24751da177e4SLinus Torvalds 247670ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 2477efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 2478b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 2479830218c1SDavid Ahern const struct in6_addr *gwaddr, 2480830218c1SDavid Ahern struct net_device *dev) 248170ceb4f5SYOSHIFUJI Hideaki { 2482830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; 2483830218c1SDavid Ahern int ifindex = dev->ifindex; 248470ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 248570ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt = NULL; 2486c71099acSThomas Graf struct fib6_table *table; 248770ceb4f5SYOSHIFUJI Hideaki 2488830218c1SDavid Ahern table = fib6_get_table(net, tb_id); 248938308473SDavid S. Miller if (!table) 2490c71099acSThomas Graf return NULL; 2491c71099acSThomas Graf 24925744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 2493c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0); 249470ceb4f5SYOSHIFUJI Hideaki if (!fn) 249570ceb4f5SYOSHIFUJI Hideaki goto out; 249670ceb4f5SYOSHIFUJI Hideaki 2497d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 2498d1918542SDavid S. Miller if (rt->dst.dev->ifindex != ifindex) 249970ceb4f5SYOSHIFUJI Hideaki continue; 250070ceb4f5SYOSHIFUJI Hideaki if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) 250170ceb4f5SYOSHIFUJI Hideaki continue; 250270ceb4f5SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr)) 250370ceb4f5SYOSHIFUJI Hideaki continue; 2504d8d1f30bSChangli Gao dst_hold(&rt->dst); 250570ceb4f5SYOSHIFUJI Hideaki break; 250670ceb4f5SYOSHIFUJI Hideaki } 250770ceb4f5SYOSHIFUJI Hideaki out: 25085744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 250970ceb4f5SYOSHIFUJI Hideaki return rt; 251070ceb4f5SYOSHIFUJI Hideaki } 251170ceb4f5SYOSHIFUJI Hideaki 2512efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 2513b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 2514830218c1SDavid Ahern const struct in6_addr *gwaddr, 2515830218c1SDavid Ahern struct net_device *dev, 251695c96174SEric Dumazet unsigned int pref) 251770ceb4f5SYOSHIFUJI Hideaki { 251886872cb5SThomas Graf struct fib6_config cfg = { 2519238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 2520830218c1SDavid Ahern .fc_ifindex = dev->ifindex, 252186872cb5SThomas Graf .fc_dst_len = prefixlen, 252286872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 252386872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 252415e47304SEric W. Biederman .fc_nlinfo.portid = 0, 2525efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 2526efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 252786872cb5SThomas Graf }; 252870ceb4f5SYOSHIFUJI Hideaki 2529830218c1SDavid Ahern cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO, 25304e3fd7a0SAlexey Dobriyan cfg.fc_dst = *prefix; 25314e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 253286872cb5SThomas Graf 2533e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 2534e317da96SYOSHIFUJI Hideaki if (!prefixlen) 253586872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 253670ceb4f5SYOSHIFUJI Hideaki 2537333c4301SDavid Ahern ip6_route_add(&cfg, NULL); 253870ceb4f5SYOSHIFUJI Hideaki 2539830218c1SDavid Ahern return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); 254070ceb4f5SYOSHIFUJI Hideaki } 254170ceb4f5SYOSHIFUJI Hideaki #endif 254270ceb4f5SYOSHIFUJI Hideaki 2543b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev) 25441da177e4SLinus Torvalds { 2545830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; 25461da177e4SLinus Torvalds struct rt6_info *rt; 2547c71099acSThomas Graf struct fib6_table *table; 25481da177e4SLinus Torvalds 2549830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), tb_id); 255038308473SDavid S. Miller if (!table) 2551c71099acSThomas Graf return NULL; 25521da177e4SLinus Torvalds 25535744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 2554d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 2555d1918542SDavid S. Miller if (dev == rt->dst.dev && 2556045927ffSYOSHIFUJI Hideaki ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 25571da177e4SLinus Torvalds ipv6_addr_equal(&rt->rt6i_gateway, addr)) 25581da177e4SLinus Torvalds break; 25591da177e4SLinus Torvalds } 25601da177e4SLinus Torvalds if (rt) 2561d8d1f30bSChangli Gao dst_hold(&rt->dst); 25625744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 25631da177e4SLinus Torvalds return rt; 25641da177e4SLinus Torvalds } 25651da177e4SLinus Torvalds 2566b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr, 2567ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 2568ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 25691da177e4SLinus Torvalds { 257086872cb5SThomas Graf struct fib6_config cfg = { 2571ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, 2572238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 257386872cb5SThomas Graf .fc_ifindex = dev->ifindex, 257486872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 257586872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 257615e47304SEric W. Biederman .fc_nlinfo.portid = 0, 25775578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 2578c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 257986872cb5SThomas Graf }; 25801da177e4SLinus Torvalds 25814e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 25821da177e4SLinus Torvalds 2583333c4301SDavid Ahern if (!ip6_route_add(&cfg, NULL)) { 2584830218c1SDavid Ahern struct fib6_table *table; 2585830218c1SDavid Ahern 2586830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), cfg.fc_table); 2587830218c1SDavid Ahern if (table) 2588830218c1SDavid Ahern table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; 2589830218c1SDavid Ahern } 25901da177e4SLinus Torvalds 25911da177e4SLinus Torvalds return rt6_get_dflt_router(gwaddr, dev); 25921da177e4SLinus Torvalds } 25931da177e4SLinus Torvalds 2594830218c1SDavid Ahern static void __rt6_purge_dflt_routers(struct fib6_table *table) 25951da177e4SLinus Torvalds { 25961da177e4SLinus Torvalds struct rt6_info *rt; 25971da177e4SLinus Torvalds 25981da177e4SLinus Torvalds restart: 2599c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 2600d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 26013e8b0ac3SLorenzo Colitti if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) && 26023e8b0ac3SLorenzo Colitti (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) { 2603d8d1f30bSChangli Gao dst_hold(&rt->dst); 2604c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 2605e0a1ad73SThomas Graf ip6_del_rt(rt); 26061da177e4SLinus Torvalds goto restart; 26071da177e4SLinus Torvalds } 26081da177e4SLinus Torvalds } 2609c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 2610830218c1SDavid Ahern 2611830218c1SDavid Ahern table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; 2612830218c1SDavid Ahern } 2613830218c1SDavid Ahern 2614830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net) 2615830218c1SDavid Ahern { 2616830218c1SDavid Ahern struct fib6_table *table; 2617830218c1SDavid Ahern struct hlist_head *head; 2618830218c1SDavid Ahern unsigned int h; 2619830218c1SDavid Ahern 2620830218c1SDavid Ahern rcu_read_lock(); 2621830218c1SDavid Ahern 2622830218c1SDavid Ahern for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { 2623830218c1SDavid Ahern head = &net->ipv6.fib_table_hash[h]; 2624830218c1SDavid Ahern hlist_for_each_entry_rcu(table, head, tb6_hlist) { 2625830218c1SDavid Ahern if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) 2626830218c1SDavid Ahern __rt6_purge_dflt_routers(table); 2627830218c1SDavid Ahern } 2628830218c1SDavid Ahern } 2629830218c1SDavid Ahern 2630830218c1SDavid Ahern rcu_read_unlock(); 26311da177e4SLinus Torvalds } 26321da177e4SLinus Torvalds 26335578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 26345578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 263586872cb5SThomas Graf struct fib6_config *cfg) 263686872cb5SThomas Graf { 263786872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 263886872cb5SThomas Graf 2639ca254490SDavid Ahern cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? 2640ca254490SDavid Ahern : RT6_TABLE_MAIN; 264186872cb5SThomas Graf cfg->fc_ifindex = rtmsg->rtmsg_ifindex; 264286872cb5SThomas Graf cfg->fc_metric = rtmsg->rtmsg_metric; 264386872cb5SThomas Graf cfg->fc_expires = rtmsg->rtmsg_info; 264486872cb5SThomas Graf cfg->fc_dst_len = rtmsg->rtmsg_dst_len; 264586872cb5SThomas Graf cfg->fc_src_len = rtmsg->rtmsg_src_len; 264686872cb5SThomas Graf cfg->fc_flags = rtmsg->rtmsg_flags; 264786872cb5SThomas Graf 26485578689aSDaniel Lezcano cfg->fc_nlinfo.nl_net = net; 2649f1243c2dSBenjamin Thery 26504e3fd7a0SAlexey Dobriyan cfg->fc_dst = rtmsg->rtmsg_dst; 26514e3fd7a0SAlexey Dobriyan cfg->fc_src = rtmsg->rtmsg_src; 26524e3fd7a0SAlexey Dobriyan cfg->fc_gateway = rtmsg->rtmsg_gateway; 265386872cb5SThomas Graf } 265486872cb5SThomas Graf 26555578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) 26561da177e4SLinus Torvalds { 265786872cb5SThomas Graf struct fib6_config cfg; 26581da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 26591da177e4SLinus Torvalds int err; 26601da177e4SLinus Torvalds 26611da177e4SLinus Torvalds switch (cmd) { 26621da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 26631da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 2664af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 26651da177e4SLinus Torvalds return -EPERM; 26661da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 26671da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 26681da177e4SLinus Torvalds if (err) 26691da177e4SLinus Torvalds return -EFAULT; 26701da177e4SLinus Torvalds 26715578689aSDaniel Lezcano rtmsg_to_fib6_config(net, &rtmsg, &cfg); 267286872cb5SThomas Graf 26731da177e4SLinus Torvalds rtnl_lock(); 26741da177e4SLinus Torvalds switch (cmd) { 26751da177e4SLinus Torvalds case SIOCADDRT: 2676333c4301SDavid Ahern err = ip6_route_add(&cfg, NULL); 26771da177e4SLinus Torvalds break; 26781da177e4SLinus Torvalds case SIOCDELRT: 2679333c4301SDavid Ahern err = ip6_route_del(&cfg, NULL); 26801da177e4SLinus Torvalds break; 26811da177e4SLinus Torvalds default: 26821da177e4SLinus Torvalds err = -EINVAL; 26831da177e4SLinus Torvalds } 26841da177e4SLinus Torvalds rtnl_unlock(); 26851da177e4SLinus Torvalds 26861da177e4SLinus Torvalds return err; 26873ff50b79SStephen Hemminger } 26881da177e4SLinus Torvalds 26891da177e4SLinus Torvalds return -EINVAL; 26901da177e4SLinus Torvalds } 26911da177e4SLinus Torvalds 26921da177e4SLinus Torvalds /* 26931da177e4SLinus Torvalds * Drop the packet on the floor 26941da177e4SLinus Torvalds */ 26951da177e4SLinus Torvalds 2696d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 26971da177e4SLinus Torvalds { 2698612f09e8SYOSHIFUJI Hideaki int type; 2699adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 2700612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 2701612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 27020660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 270345bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 27043bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 27053bd653c8SDenis V. Lunev IPSTATS_MIB_INADDRERRORS); 2706612f09e8SYOSHIFUJI Hideaki break; 2707612f09e8SYOSHIFUJI Hideaki } 2708612f09e8SYOSHIFUJI Hideaki /* FALLTHROUGH */ 2709612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 27103bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 27113bd653c8SDenis V. Lunev ipstats_mib_noroutes); 2712612f09e8SYOSHIFUJI Hideaki break; 2713612f09e8SYOSHIFUJI Hideaki } 27143ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 27151da177e4SLinus Torvalds kfree_skb(skb); 27161da177e4SLinus Torvalds return 0; 27171da177e4SLinus Torvalds } 27181da177e4SLinus Torvalds 27199ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 27209ce8ade0SThomas Graf { 2721612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 27229ce8ade0SThomas Graf } 27239ce8ade0SThomas Graf 2724ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb) 27251da177e4SLinus Torvalds { 2726adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 2727612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 27281da177e4SLinus Torvalds } 27291da177e4SLinus Torvalds 27309ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 27319ce8ade0SThomas Graf { 2732612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 27339ce8ade0SThomas Graf } 27349ce8ade0SThomas Graf 2735ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb) 27369ce8ade0SThomas Graf { 2737adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 2738612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 27399ce8ade0SThomas Graf } 27409ce8ade0SThomas Graf 27411da177e4SLinus Torvalds /* 27421da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 27431da177e4SLinus Torvalds */ 27441da177e4SLinus Torvalds 27451da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, 27461da177e4SLinus Torvalds const struct in6_addr *addr, 27478f031519SDavid S. Miller bool anycast) 27481da177e4SLinus Torvalds { 2749ca254490SDavid Ahern u32 tb_id; 2750c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 27515f02ce24SDavid Ahern struct net_device *dev = net->loopback_dev; 27525f02ce24SDavid Ahern struct rt6_info *rt; 27535f02ce24SDavid Ahern 27545f02ce24SDavid Ahern /* use L3 Master device as loopback for host routes if device 27555f02ce24SDavid Ahern * is enslaved and address is not link local or multicast 27565f02ce24SDavid Ahern */ 27575f02ce24SDavid Ahern if (!rt6_need_strict(addr)) 27585f02ce24SDavid Ahern dev = l3mdev_master_dev_rcu(idev->dev) ? : dev; 27595f02ce24SDavid Ahern 27605f02ce24SDavid Ahern rt = ip6_dst_alloc(net, dev, DST_NOCOUNT); 2761a3300ef4SHannes Frederic Sowa if (!rt) 27621da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 27631da177e4SLinus Torvalds 27641da177e4SLinus Torvalds in6_dev_hold(idev); 27651da177e4SLinus Torvalds 276611d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 2767d8d1f30bSChangli Gao rt->dst.input = ip6_input; 2768d8d1f30bSChangli Gao rt->dst.output = ip6_output; 27691da177e4SLinus Torvalds rt->rt6i_idev = idev; 27701da177e4SLinus Torvalds 277194b5e0f9SDavid Ahern rt->rt6i_protocol = RTPROT_KERNEL; 27721da177e4SLinus Torvalds rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP; 277358c4fb86SYOSHIFUJI Hideaki if (anycast) 277458c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 277558c4fb86SYOSHIFUJI Hideaki else 27761da177e4SLinus Torvalds rt->rt6i_flags |= RTF_LOCAL; 27771da177e4SLinus Torvalds 2778550bab42SJulian Anastasov rt->rt6i_gateway = *addr; 27794e3fd7a0SAlexey Dobriyan rt->rt6i_dst.addr = *addr; 27801da177e4SLinus Torvalds rt->rt6i_dst.plen = 128; 2781ca254490SDavid Ahern tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL; 2782ca254490SDavid Ahern rt->rt6i_table = fib6_get_table(net, tb_id); 27838e3d5be7SMartin KaFai Lau rt->dst.flags |= DST_NOCACHE; 27841da177e4SLinus Torvalds 27851da177e4SLinus Torvalds return rt; 27861da177e4SLinus Torvalds } 27871da177e4SLinus Torvalds 2788c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 2789c3968a85SDaniel Walter struct arg_dev_net_ip { 2790c3968a85SDaniel Walter struct net_device *dev; 2791c3968a85SDaniel Walter struct net *net; 2792c3968a85SDaniel Walter struct in6_addr *addr; 2793c3968a85SDaniel Walter }; 2794c3968a85SDaniel Walter 2795c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg) 2796c3968a85SDaniel Walter { 2797c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 2798c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 2799c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 2800c3968a85SDaniel Walter 2801d1918542SDavid S. Miller if (((void *)rt->dst.dev == dev || !dev) && 2802c3968a85SDaniel Walter rt != net->ipv6.ip6_null_entry && 2803c3968a85SDaniel Walter ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) { 2804c3968a85SDaniel Walter /* remove prefsrc entry */ 2805c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 2806c3968a85SDaniel Walter } 2807c3968a85SDaniel Walter return 0; 2808c3968a85SDaniel Walter } 2809c3968a85SDaniel Walter 2810c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 2811c3968a85SDaniel Walter { 2812c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 2813c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 2814c3968a85SDaniel Walter .dev = ifp->idev->dev, 2815c3968a85SDaniel Walter .net = net, 2816c3968a85SDaniel Walter .addr = &ifp->addr, 2817c3968a85SDaniel Walter }; 28180c3584d5SLi RongQing fib6_clean_all(net, fib6_remove_prefsrc, &adni); 2819c3968a85SDaniel Walter } 2820c3968a85SDaniel Walter 2821be7a010dSDuan Jiong #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY) 2822be7a010dSDuan Jiong #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) 2823be7a010dSDuan Jiong 2824be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */ 2825be7a010dSDuan Jiong static int fib6_clean_tohost(struct rt6_info *rt, void *arg) 2826be7a010dSDuan Jiong { 2827be7a010dSDuan Jiong struct in6_addr *gateway = (struct in6_addr *)arg; 2828be7a010dSDuan Jiong 2829be7a010dSDuan Jiong if ((((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) || 2830be7a010dSDuan Jiong ((rt->rt6i_flags & RTF_CACHE_GATEWAY) == RTF_CACHE_GATEWAY)) && 2831be7a010dSDuan Jiong ipv6_addr_equal(gateway, &rt->rt6i_gateway)) { 2832be7a010dSDuan Jiong return -1; 2833be7a010dSDuan Jiong } 2834be7a010dSDuan Jiong return 0; 2835be7a010dSDuan Jiong } 2836be7a010dSDuan Jiong 2837be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) 2838be7a010dSDuan Jiong { 2839be7a010dSDuan Jiong fib6_clean_all(net, fib6_clean_tohost, gateway); 2840be7a010dSDuan Jiong } 2841be7a010dSDuan Jiong 28428ed67789SDaniel Lezcano struct arg_dev_net { 28438ed67789SDaniel Lezcano struct net_device *dev; 28448ed67789SDaniel Lezcano struct net *net; 28458ed67789SDaniel Lezcano }; 28468ed67789SDaniel Lezcano 2847a1a22c12SDavid Ahern /* called with write lock held for table with rt */ 28481da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg) 28491da177e4SLinus Torvalds { 2850bc3ef660Sstephen hemminger const struct arg_dev_net *adn = arg; 2851bc3ef660Sstephen hemminger const struct net_device *dev = adn->dev; 28528ed67789SDaniel Lezcano 2853d1918542SDavid S. Miller if ((rt->dst.dev == dev || !dev) && 2854a1a22c12SDavid Ahern rt != adn->net->ipv6.ip6_null_entry && 2855a1a22c12SDavid Ahern (rt->rt6i_nsiblings == 0 || 28568397ed36SDavid Ahern (dev && netdev_unregistering(dev)) || 2857a1a22c12SDavid Ahern !rt->rt6i_idev->cnf.ignore_routes_with_linkdown)) 28581da177e4SLinus Torvalds return -1; 2859c159d30cSDavid S. Miller 28601da177e4SLinus Torvalds return 0; 28611da177e4SLinus Torvalds } 28621da177e4SLinus Torvalds 2863f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev) 28641da177e4SLinus Torvalds { 28658ed67789SDaniel Lezcano struct arg_dev_net adn = { 28668ed67789SDaniel Lezcano .dev = dev, 28678ed67789SDaniel Lezcano .net = net, 28688ed67789SDaniel Lezcano }; 28698ed67789SDaniel Lezcano 28700c3584d5SLi RongQing fib6_clean_all(net, fib6_ifdown, &adn); 28711e493d19SDavid S. Miller icmp6_clean_all(fib6_ifdown, &adn); 2872e332bc67SEric W. Biederman if (dev) 28738d0b94afSMartin KaFai Lau rt6_uncached_list_flush_dev(net, dev); 28741da177e4SLinus Torvalds } 28751da177e4SLinus Torvalds 287695c96174SEric Dumazet struct rt6_mtu_change_arg { 28771da177e4SLinus Torvalds struct net_device *dev; 287895c96174SEric Dumazet unsigned int mtu; 28791da177e4SLinus Torvalds }; 28801da177e4SLinus Torvalds 28811da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg) 28821da177e4SLinus Torvalds { 28831da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 28841da177e4SLinus Torvalds struct inet6_dev *idev; 28851da177e4SLinus Torvalds 28861da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 28871da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 28881da177e4SLinus Torvalds We still use this lock to block changes 28891da177e4SLinus Torvalds caused by addrconf/ndisc. 28901da177e4SLinus Torvalds */ 28911da177e4SLinus Torvalds 28921da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 289338308473SDavid S. Miller if (!idev) 28941da177e4SLinus Torvalds return 0; 28951da177e4SLinus Torvalds 28961da177e4SLinus Torvalds /* For administrative MTU increase, there is no way to discover 28971da177e4SLinus Torvalds IPv6 PMTU increase, so PMTU increase should be updated here. 28981da177e4SLinus Torvalds Since RFC 1981 doesn't include administrative MTU increase 28991da177e4SLinus Torvalds update PMTU increase is a MUST. (i.e. jumbo frame) 29001da177e4SLinus Torvalds */ 29011da177e4SLinus Torvalds /* 29021da177e4SLinus Torvalds If new MTU is less than route PMTU, this new MTU will be the 29031da177e4SLinus Torvalds lowest MTU in the path, update the route PMTU to reflect PMTU 29041da177e4SLinus Torvalds decreases; if new MTU is greater than route PMTU, and the 29051da177e4SLinus Torvalds old MTU is the lowest MTU in the path, update the route PMTU 29061da177e4SLinus Torvalds to reflect the increase. In this case if the other nodes' MTU 29071da177e4SLinus Torvalds also have the lowest MTU, TOO BIG MESSAGE will be lead to 290867c408cfSAlexander Alemayhu PMTU discovery. 29091da177e4SLinus Torvalds */ 2910d1918542SDavid S. Miller if (rt->dst.dev == arg->dev && 2911fb56be83SMaciej Żenczykowski dst_metric_raw(&rt->dst, RTAX_MTU) && 29124b32b5adSMartin KaFai Lau !dst_metric_locked(&rt->dst, RTAX_MTU)) { 29134b32b5adSMartin KaFai Lau if (rt->rt6i_flags & RTF_CACHE) { 29144b32b5adSMartin KaFai Lau /* For RTF_CACHE with rt6i_pmtu == 0 29154b32b5adSMartin KaFai Lau * (i.e. a redirected route), 29164b32b5adSMartin KaFai Lau * the metrics of its rt->dst.from has already 29174b32b5adSMartin KaFai Lau * been updated. 29184b32b5adSMartin KaFai Lau */ 29194b32b5adSMartin KaFai Lau if (rt->rt6i_pmtu && rt->rt6i_pmtu > arg->mtu) 29204b32b5adSMartin KaFai Lau rt->rt6i_pmtu = arg->mtu; 29214b32b5adSMartin KaFai Lau } else if (dst_mtu(&rt->dst) >= arg->mtu || 2922d8d1f30bSChangli Gao (dst_mtu(&rt->dst) < arg->mtu && 29234b32b5adSMartin KaFai Lau dst_mtu(&rt->dst) == idev->cnf.mtu6)) { 2924defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu); 2925566cfd8fSSimon Arlott } 29264b32b5adSMartin KaFai Lau } 29271da177e4SLinus Torvalds return 0; 29281da177e4SLinus Torvalds } 29291da177e4SLinus Torvalds 293095c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu) 29311da177e4SLinus Torvalds { 2932c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 2933c71099acSThomas Graf .dev = dev, 2934c71099acSThomas Graf .mtu = mtu, 2935c71099acSThomas Graf }; 29361da177e4SLinus Torvalds 29370c3584d5SLi RongQing fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); 29381da177e4SLinus Torvalds } 29391da177e4SLinus Torvalds 2940ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 29415176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 294286872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 2943ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 294486872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 294586872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 294651ebd318SNicolas Dichtel [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 2947c78ba6d6SLubomir Rintel [RTA_PREF] = { .type = NLA_U8 }, 294819e42e45SRoopa Prabhu [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, 294919e42e45SRoopa Prabhu [RTA_ENCAP] = { .type = NLA_NESTED }, 295032bc201eSXin Long [RTA_EXPIRES] = { .type = NLA_U32 }, 2951622ec2c9SLorenzo Colitti [RTA_UID] = { .type = NLA_U32 }, 29523b45a410SLiping Zhang [RTA_MARK] = { .type = NLA_U32 }, 295386872cb5SThomas Graf }; 295486872cb5SThomas Graf 295586872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 2956333c4301SDavid Ahern struct fib6_config *cfg, 2957333c4301SDavid Ahern struct netlink_ext_ack *extack) 29581da177e4SLinus Torvalds { 295986872cb5SThomas Graf struct rtmsg *rtm; 296086872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 2961c78ba6d6SLubomir Rintel unsigned int pref; 296286872cb5SThomas Graf int err; 29631da177e4SLinus Torvalds 2964fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 2965fceb6435SJohannes Berg NULL); 296686872cb5SThomas Graf if (err < 0) 296786872cb5SThomas Graf goto errout; 29681da177e4SLinus Torvalds 296986872cb5SThomas Graf err = -EINVAL; 297086872cb5SThomas Graf rtm = nlmsg_data(nlh); 297186872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 297286872cb5SThomas Graf 297386872cb5SThomas Graf cfg->fc_table = rtm->rtm_table; 297486872cb5SThomas Graf cfg->fc_dst_len = rtm->rtm_dst_len; 297586872cb5SThomas Graf cfg->fc_src_len = rtm->rtm_src_len; 297686872cb5SThomas Graf cfg->fc_flags = RTF_UP; 297786872cb5SThomas Graf cfg->fc_protocol = rtm->rtm_protocol; 2978ef2c7d7bSNicolas Dichtel cfg->fc_type = rtm->rtm_type; 297986872cb5SThomas Graf 2980ef2c7d7bSNicolas Dichtel if (rtm->rtm_type == RTN_UNREACHABLE || 2981ef2c7d7bSNicolas Dichtel rtm->rtm_type == RTN_BLACKHOLE || 2982b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_PROHIBIT || 2983b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_THROW) 298486872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 298586872cb5SThomas Graf 2986ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 2987ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 2988ab79ad14SMaciej Żenczykowski 29891f56a01fSMartin KaFai Lau if (rtm->rtm_flags & RTM_F_CLONED) 29901f56a01fSMartin KaFai Lau cfg->fc_flags |= RTF_CACHE; 29911f56a01fSMartin KaFai Lau 299215e47304SEric W. Biederman cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid; 299386872cb5SThomas Graf cfg->fc_nlinfo.nlh = nlh; 29943b1e0a65SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = sock_net(skb->sk); 299586872cb5SThomas Graf 299686872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 299767b61f6cSJiri Benc cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); 299886872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 29991da177e4SLinus Torvalds } 300086872cb5SThomas Graf 300186872cb5SThomas Graf if (tb[RTA_DST]) { 300286872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 300386872cb5SThomas Graf 300486872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 300586872cb5SThomas Graf goto errout; 300686872cb5SThomas Graf 300786872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 30081da177e4SLinus Torvalds } 300986872cb5SThomas Graf 301086872cb5SThomas Graf if (tb[RTA_SRC]) { 301186872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 301286872cb5SThomas Graf 301386872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 301486872cb5SThomas Graf goto errout; 301586872cb5SThomas Graf 301686872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 30171da177e4SLinus Torvalds } 301886872cb5SThomas Graf 3019c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 302067b61f6cSJiri Benc cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); 3021c3968a85SDaniel Walter 302286872cb5SThomas Graf if (tb[RTA_OIF]) 302386872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 302486872cb5SThomas Graf 302586872cb5SThomas Graf if (tb[RTA_PRIORITY]) 302686872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 302786872cb5SThomas Graf 302886872cb5SThomas Graf if (tb[RTA_METRICS]) { 302986872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 303086872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 30311da177e4SLinus Torvalds } 303286872cb5SThomas Graf 303386872cb5SThomas Graf if (tb[RTA_TABLE]) 303486872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 303586872cb5SThomas Graf 303651ebd318SNicolas Dichtel if (tb[RTA_MULTIPATH]) { 303751ebd318SNicolas Dichtel cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); 303851ebd318SNicolas Dichtel cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); 30399ed59592SDavid Ahern 30409ed59592SDavid Ahern err = lwtunnel_valid_encap_type_attr(cfg->fc_mp, 3041c255bd68SDavid Ahern cfg->fc_mp_len, extack); 30429ed59592SDavid Ahern if (err < 0) 30439ed59592SDavid Ahern goto errout; 304451ebd318SNicolas Dichtel } 304551ebd318SNicolas Dichtel 3046c78ba6d6SLubomir Rintel if (tb[RTA_PREF]) { 3047c78ba6d6SLubomir Rintel pref = nla_get_u8(tb[RTA_PREF]); 3048c78ba6d6SLubomir Rintel if (pref != ICMPV6_ROUTER_PREF_LOW && 3049c78ba6d6SLubomir Rintel pref != ICMPV6_ROUTER_PREF_HIGH) 3050c78ba6d6SLubomir Rintel pref = ICMPV6_ROUTER_PREF_MEDIUM; 3051c78ba6d6SLubomir Rintel cfg->fc_flags |= RTF_PREF(pref); 3052c78ba6d6SLubomir Rintel } 3053c78ba6d6SLubomir Rintel 305419e42e45SRoopa Prabhu if (tb[RTA_ENCAP]) 305519e42e45SRoopa Prabhu cfg->fc_encap = tb[RTA_ENCAP]; 305619e42e45SRoopa Prabhu 30579ed59592SDavid Ahern if (tb[RTA_ENCAP_TYPE]) { 305819e42e45SRoopa Prabhu cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); 305919e42e45SRoopa Prabhu 3060c255bd68SDavid Ahern err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); 30619ed59592SDavid Ahern if (err < 0) 30629ed59592SDavid Ahern goto errout; 30639ed59592SDavid Ahern } 30649ed59592SDavid Ahern 306532bc201eSXin Long if (tb[RTA_EXPIRES]) { 306632bc201eSXin Long unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); 306732bc201eSXin Long 306832bc201eSXin Long if (addrconf_finite_timeout(timeout)) { 306932bc201eSXin Long cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); 307032bc201eSXin Long cfg->fc_flags |= RTF_EXPIRES; 307132bc201eSXin Long } 307232bc201eSXin Long } 307332bc201eSXin Long 307486872cb5SThomas Graf err = 0; 307586872cb5SThomas Graf errout: 307686872cb5SThomas Graf return err; 30771da177e4SLinus Torvalds } 30781da177e4SLinus Torvalds 30796b9ea5a6SRoopa Prabhu struct rt6_nh { 30806b9ea5a6SRoopa Prabhu struct rt6_info *rt6_info; 30816b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 30826b9ea5a6SRoopa Prabhu struct mx6_config mxc; 30836b9ea5a6SRoopa Prabhu struct list_head next; 30846b9ea5a6SRoopa Prabhu }; 30856b9ea5a6SRoopa Prabhu 30866b9ea5a6SRoopa Prabhu static void ip6_print_replace_route_err(struct list_head *rt6_nh_list) 30876b9ea5a6SRoopa Prabhu { 30886b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 30896b9ea5a6SRoopa Prabhu 30906b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 30917d4d5065SDavid Ahern pr_warn("IPV6: multipath route replace failed (check consistency of installed routes): %pI6c nexthop %pI6c ifi %d\n", 30926b9ea5a6SRoopa Prabhu &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway, 30936b9ea5a6SRoopa Prabhu nh->r_cfg.fc_ifindex); 30946b9ea5a6SRoopa Prabhu } 30956b9ea5a6SRoopa Prabhu } 30966b9ea5a6SRoopa Prabhu 30976b9ea5a6SRoopa Prabhu static int ip6_route_info_append(struct list_head *rt6_nh_list, 30986b9ea5a6SRoopa Prabhu struct rt6_info *rt, struct fib6_config *r_cfg) 30996b9ea5a6SRoopa Prabhu { 31006b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 31016b9ea5a6SRoopa Prabhu struct rt6_info *rtnh; 31026b9ea5a6SRoopa Prabhu int err = -EEXIST; 31036b9ea5a6SRoopa Prabhu 31046b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 31056b9ea5a6SRoopa Prabhu /* check if rt6_info already exists */ 31066b9ea5a6SRoopa Prabhu rtnh = nh->rt6_info; 31076b9ea5a6SRoopa Prabhu 31086b9ea5a6SRoopa Prabhu if (rtnh->dst.dev == rt->dst.dev && 31096b9ea5a6SRoopa Prabhu rtnh->rt6i_idev == rt->rt6i_idev && 31106b9ea5a6SRoopa Prabhu ipv6_addr_equal(&rtnh->rt6i_gateway, 31116b9ea5a6SRoopa Prabhu &rt->rt6i_gateway)) 31126b9ea5a6SRoopa Prabhu return err; 31136b9ea5a6SRoopa Prabhu } 31146b9ea5a6SRoopa Prabhu 31156b9ea5a6SRoopa Prabhu nh = kzalloc(sizeof(*nh), GFP_KERNEL); 31166b9ea5a6SRoopa Prabhu if (!nh) 31176b9ea5a6SRoopa Prabhu return -ENOMEM; 31186b9ea5a6SRoopa Prabhu nh->rt6_info = rt; 31196b9ea5a6SRoopa Prabhu err = ip6_convert_metrics(&nh->mxc, r_cfg); 31206b9ea5a6SRoopa Prabhu if (err) { 31216b9ea5a6SRoopa Prabhu kfree(nh); 31226b9ea5a6SRoopa Prabhu return err; 31236b9ea5a6SRoopa Prabhu } 31246b9ea5a6SRoopa Prabhu memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); 31256b9ea5a6SRoopa Prabhu list_add_tail(&nh->next, rt6_nh_list); 31266b9ea5a6SRoopa Prabhu 31276b9ea5a6SRoopa Prabhu return 0; 31286b9ea5a6SRoopa Prabhu } 31296b9ea5a6SRoopa Prabhu 31303b1137feSDavid Ahern static void ip6_route_mpath_notify(struct rt6_info *rt, 31313b1137feSDavid Ahern struct rt6_info *rt_last, 31323b1137feSDavid Ahern struct nl_info *info, 31333b1137feSDavid Ahern __u16 nlflags) 31343b1137feSDavid Ahern { 31353b1137feSDavid Ahern /* if this is an APPEND route, then rt points to the first route 31363b1137feSDavid Ahern * inserted and rt_last points to last route inserted. Userspace 31373b1137feSDavid Ahern * wants a consistent dump of the route which starts at the first 31383b1137feSDavid Ahern * nexthop. Since sibling routes are always added at the end of 31393b1137feSDavid Ahern * the list, find the first sibling of the last route appended 31403b1137feSDavid Ahern */ 31413b1137feSDavid Ahern if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->rt6i_nsiblings) { 31423b1137feSDavid Ahern rt = list_first_entry(&rt_last->rt6i_siblings, 31433b1137feSDavid Ahern struct rt6_info, 31443b1137feSDavid Ahern rt6i_siblings); 31453b1137feSDavid Ahern } 31463b1137feSDavid Ahern 31473b1137feSDavid Ahern if (rt) 31483b1137feSDavid Ahern inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); 31493b1137feSDavid Ahern } 31503b1137feSDavid Ahern 3151333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg, 3152333c4301SDavid Ahern struct netlink_ext_ack *extack) 315351ebd318SNicolas Dichtel { 31543b1137feSDavid Ahern struct rt6_info *rt_notif = NULL, *rt_last = NULL; 31553b1137feSDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 315651ebd318SNicolas Dichtel struct fib6_config r_cfg; 315751ebd318SNicolas Dichtel struct rtnexthop *rtnh; 31586b9ea5a6SRoopa Prabhu struct rt6_info *rt; 31596b9ea5a6SRoopa Prabhu struct rt6_nh *err_nh; 31606b9ea5a6SRoopa Prabhu struct rt6_nh *nh, *nh_safe; 31613b1137feSDavid Ahern __u16 nlflags; 316251ebd318SNicolas Dichtel int remaining; 316351ebd318SNicolas Dichtel int attrlen; 31646b9ea5a6SRoopa Prabhu int err = 1; 31656b9ea5a6SRoopa Prabhu int nhn = 0; 31666b9ea5a6SRoopa Prabhu int replace = (cfg->fc_nlinfo.nlh && 31676b9ea5a6SRoopa Prabhu (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); 31686b9ea5a6SRoopa Prabhu LIST_HEAD(rt6_nh_list); 316951ebd318SNicolas Dichtel 31703b1137feSDavid Ahern nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; 31713b1137feSDavid Ahern if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) 31723b1137feSDavid Ahern nlflags |= NLM_F_APPEND; 31733b1137feSDavid Ahern 317435f1b4e9SMichal Kubeček remaining = cfg->fc_mp_len; 317551ebd318SNicolas Dichtel rtnh = (struct rtnexthop *)cfg->fc_mp; 317651ebd318SNicolas Dichtel 31776b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry and build a list (rt6_nh_list) of 31786b9ea5a6SRoopa Prabhu * rt6_info structs per nexthop 31796b9ea5a6SRoopa Prabhu */ 318051ebd318SNicolas Dichtel while (rtnh_ok(rtnh, remaining)) { 318151ebd318SNicolas Dichtel memcpy(&r_cfg, cfg, sizeof(*cfg)); 318251ebd318SNicolas Dichtel if (rtnh->rtnh_ifindex) 318351ebd318SNicolas Dichtel r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 318451ebd318SNicolas Dichtel 318551ebd318SNicolas Dichtel attrlen = rtnh_attrlen(rtnh); 318651ebd318SNicolas Dichtel if (attrlen > 0) { 318751ebd318SNicolas Dichtel struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 318851ebd318SNicolas Dichtel 318951ebd318SNicolas Dichtel nla = nla_find(attrs, attrlen, RTA_GATEWAY); 319051ebd318SNicolas Dichtel if (nla) { 319167b61f6cSJiri Benc r_cfg.fc_gateway = nla_get_in6_addr(nla); 319251ebd318SNicolas Dichtel r_cfg.fc_flags |= RTF_GATEWAY; 319351ebd318SNicolas Dichtel } 319419e42e45SRoopa Prabhu r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); 319519e42e45SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); 319619e42e45SRoopa Prabhu if (nla) 319719e42e45SRoopa Prabhu r_cfg.fc_encap_type = nla_get_u16(nla); 319851ebd318SNicolas Dichtel } 31996b9ea5a6SRoopa Prabhu 3200333c4301SDavid Ahern rt = ip6_route_info_create(&r_cfg, extack); 32018c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 32028c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 32038c5b83f0SRoopa Prabhu rt = NULL; 32046b9ea5a6SRoopa Prabhu goto cleanup; 32058c5b83f0SRoopa Prabhu } 32066b9ea5a6SRoopa Prabhu 32076b9ea5a6SRoopa Prabhu err = ip6_route_info_append(&rt6_nh_list, rt, &r_cfg); 320851ebd318SNicolas Dichtel if (err) { 32091cfb71eeSWei Wang dst_release(&rt->dst); 32106b9ea5a6SRoopa Prabhu dst_free(&rt->dst); 32116b9ea5a6SRoopa Prabhu goto cleanup; 321251ebd318SNicolas Dichtel } 32136b9ea5a6SRoopa Prabhu 32146b9ea5a6SRoopa Prabhu rtnh = rtnh_next(rtnh, &remaining); 321551ebd318SNicolas Dichtel } 32166b9ea5a6SRoopa Prabhu 32173b1137feSDavid Ahern /* for add and replace send one notification with all nexthops. 32183b1137feSDavid Ahern * Skip the notification in fib6_add_rt2node and send one with 32193b1137feSDavid Ahern * the full route when done 32203b1137feSDavid Ahern */ 32213b1137feSDavid Ahern info->skip_notify = 1; 32223b1137feSDavid Ahern 32236b9ea5a6SRoopa Prabhu err_nh = NULL; 32246b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 32253b1137feSDavid Ahern rt_last = nh->rt6_info; 3226333c4301SDavid Ahern err = __ip6_ins_rt(nh->rt6_info, info, &nh->mxc, extack); 32273b1137feSDavid Ahern /* save reference to first route for notification */ 32283b1137feSDavid Ahern if (!rt_notif && !err) 32293b1137feSDavid Ahern rt_notif = nh->rt6_info; 32303b1137feSDavid Ahern 32316b9ea5a6SRoopa Prabhu /* nh->rt6_info is used or freed at this point, reset to NULL*/ 32326b9ea5a6SRoopa Prabhu nh->rt6_info = NULL; 32336b9ea5a6SRoopa Prabhu if (err) { 32346b9ea5a6SRoopa Prabhu if (replace && nhn) 32356b9ea5a6SRoopa Prabhu ip6_print_replace_route_err(&rt6_nh_list); 32366b9ea5a6SRoopa Prabhu err_nh = nh; 32376b9ea5a6SRoopa Prabhu goto add_errout; 32386b9ea5a6SRoopa Prabhu } 32396b9ea5a6SRoopa Prabhu 32401a72418bSNicolas Dichtel /* Because each route is added like a single route we remove 324127596472SMichal Kubeček * these flags after the first nexthop: if there is a collision, 324227596472SMichal Kubeček * we have already failed to add the first nexthop: 324327596472SMichal Kubeček * fib6_add_rt2node() has rejected it; when replacing, old 324427596472SMichal Kubeček * nexthops have been replaced by first new, the rest should 324527596472SMichal Kubeček * be added to it. 32461a72418bSNicolas Dichtel */ 324727596472SMichal Kubeček cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | 324827596472SMichal Kubeček NLM_F_REPLACE); 32496b9ea5a6SRoopa Prabhu nhn++; 32506b9ea5a6SRoopa Prabhu } 32516b9ea5a6SRoopa Prabhu 32523b1137feSDavid Ahern /* success ... tell user about new route */ 32533b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 32546b9ea5a6SRoopa Prabhu goto cleanup; 32556b9ea5a6SRoopa Prabhu 32566b9ea5a6SRoopa Prabhu add_errout: 32573b1137feSDavid Ahern /* send notification for routes that were added so that 32583b1137feSDavid Ahern * the delete notifications sent by ip6_route_del are 32593b1137feSDavid Ahern * coherent 32603b1137feSDavid Ahern */ 32613b1137feSDavid Ahern if (rt_notif) 32623b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 32633b1137feSDavid Ahern 32646b9ea5a6SRoopa Prabhu /* Delete routes that were already added */ 32656b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 32666b9ea5a6SRoopa Prabhu if (err_nh == nh) 32676b9ea5a6SRoopa Prabhu break; 3268333c4301SDavid Ahern ip6_route_del(&nh->r_cfg, extack); 32696b9ea5a6SRoopa Prabhu } 32706b9ea5a6SRoopa Prabhu 32716b9ea5a6SRoopa Prabhu cleanup: 32726b9ea5a6SRoopa Prabhu list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) { 32731cfb71eeSWei Wang if (nh->rt6_info) { 32741cfb71eeSWei Wang dst_release(&nh->rt6_info->dst); 32756b9ea5a6SRoopa Prabhu dst_free(&nh->rt6_info->dst); 32761cfb71eeSWei Wang } 32776b9ea5a6SRoopa Prabhu kfree(nh->mxc.mx); 32786b9ea5a6SRoopa Prabhu list_del(&nh->next); 32796b9ea5a6SRoopa Prabhu kfree(nh); 32806b9ea5a6SRoopa Prabhu } 32816b9ea5a6SRoopa Prabhu 32826b9ea5a6SRoopa Prabhu return err; 32836b9ea5a6SRoopa Prabhu } 32846b9ea5a6SRoopa Prabhu 3285333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg, 3286333c4301SDavid Ahern struct netlink_ext_ack *extack) 32876b9ea5a6SRoopa Prabhu { 32886b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 32896b9ea5a6SRoopa Prabhu struct rtnexthop *rtnh; 32906b9ea5a6SRoopa Prabhu int remaining; 32916b9ea5a6SRoopa Prabhu int attrlen; 32926b9ea5a6SRoopa Prabhu int err = 1, last_err = 0; 32936b9ea5a6SRoopa Prabhu 32946b9ea5a6SRoopa Prabhu remaining = cfg->fc_mp_len; 32956b9ea5a6SRoopa Prabhu rtnh = (struct rtnexthop *)cfg->fc_mp; 32966b9ea5a6SRoopa Prabhu 32976b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry */ 32986b9ea5a6SRoopa Prabhu while (rtnh_ok(rtnh, remaining)) { 32996b9ea5a6SRoopa Prabhu memcpy(&r_cfg, cfg, sizeof(*cfg)); 33006b9ea5a6SRoopa Prabhu if (rtnh->rtnh_ifindex) 33016b9ea5a6SRoopa Prabhu r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 33026b9ea5a6SRoopa Prabhu 33036b9ea5a6SRoopa Prabhu attrlen = rtnh_attrlen(rtnh); 33046b9ea5a6SRoopa Prabhu if (attrlen > 0) { 33056b9ea5a6SRoopa Prabhu struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 33066b9ea5a6SRoopa Prabhu 33076b9ea5a6SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_GATEWAY); 33086b9ea5a6SRoopa Prabhu if (nla) { 33096b9ea5a6SRoopa Prabhu nla_memcpy(&r_cfg.fc_gateway, nla, 16); 33106b9ea5a6SRoopa Prabhu r_cfg.fc_flags |= RTF_GATEWAY; 33116b9ea5a6SRoopa Prabhu } 33126b9ea5a6SRoopa Prabhu } 3313333c4301SDavid Ahern err = ip6_route_del(&r_cfg, extack); 33146b9ea5a6SRoopa Prabhu if (err) 33156b9ea5a6SRoopa Prabhu last_err = err; 33166b9ea5a6SRoopa Prabhu 331751ebd318SNicolas Dichtel rtnh = rtnh_next(rtnh, &remaining); 331851ebd318SNicolas Dichtel } 331951ebd318SNicolas Dichtel 332051ebd318SNicolas Dichtel return last_err; 332151ebd318SNicolas Dichtel } 332251ebd318SNicolas Dichtel 3323c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, 3324c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 33251da177e4SLinus Torvalds { 332686872cb5SThomas Graf struct fib6_config cfg; 332786872cb5SThomas Graf int err; 33281da177e4SLinus Torvalds 3329333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 333086872cb5SThomas Graf if (err < 0) 333186872cb5SThomas Graf return err; 333286872cb5SThomas Graf 333351ebd318SNicolas Dichtel if (cfg.fc_mp) 3334333c4301SDavid Ahern return ip6_route_multipath_del(&cfg, extack); 33350ae81335SDavid Ahern else { 33360ae81335SDavid Ahern cfg.fc_delete_all_nh = 1; 3337333c4301SDavid Ahern return ip6_route_del(&cfg, extack); 33381da177e4SLinus Torvalds } 33390ae81335SDavid Ahern } 33401da177e4SLinus Torvalds 3341c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, 3342c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 33431da177e4SLinus Torvalds { 334486872cb5SThomas Graf struct fib6_config cfg; 334586872cb5SThomas Graf int err; 33461da177e4SLinus Torvalds 3347333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 334886872cb5SThomas Graf if (err < 0) 334986872cb5SThomas Graf return err; 335086872cb5SThomas Graf 335151ebd318SNicolas Dichtel if (cfg.fc_mp) 3352333c4301SDavid Ahern return ip6_route_multipath_add(&cfg, extack); 335351ebd318SNicolas Dichtel else 3354333c4301SDavid Ahern return ip6_route_add(&cfg, extack); 33551da177e4SLinus Torvalds } 33561da177e4SLinus Torvalds 3357beb1afacSDavid Ahern static size_t rt6_nlmsg_size(struct rt6_info *rt) 3358339bf98fSThomas Graf { 3359beb1afacSDavid Ahern int nexthop_len = 0; 3360beb1afacSDavid Ahern 3361beb1afacSDavid Ahern if (rt->rt6i_nsiblings) { 3362beb1afacSDavid Ahern nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */ 3363beb1afacSDavid Ahern + NLA_ALIGN(sizeof(struct rtnexthop)) 3364beb1afacSDavid Ahern + nla_total_size(16) /* RTA_GATEWAY */ 3365beb1afacSDavid Ahern + lwtunnel_get_encap_size(rt->dst.lwtstate); 3366beb1afacSDavid Ahern 3367beb1afacSDavid Ahern nexthop_len *= rt->rt6i_nsiblings; 3368beb1afacSDavid Ahern } 3369beb1afacSDavid Ahern 3370339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 3371339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 3372339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 3373339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 3374339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 3375339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 3376339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 3377339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 3378339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 33796a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 3380ea697639SDaniel Borkmann + nla_total_size(sizeof(struct rta_cacheinfo)) 3381c78ba6d6SLubomir Rintel + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ 338219e42e45SRoopa Prabhu + nla_total_size(1) /* RTA_PREF */ 3383beb1afacSDavid Ahern + lwtunnel_get_encap_size(rt->dst.lwtstate) 3384beb1afacSDavid Ahern + nexthop_len; 3385beb1afacSDavid Ahern } 3386beb1afacSDavid Ahern 3387beb1afacSDavid Ahern static int rt6_nexthop_info(struct sk_buff *skb, struct rt6_info *rt, 33885be083ceSDavid Ahern unsigned int *flags, bool skip_oif) 3389beb1afacSDavid Ahern { 3390beb1afacSDavid Ahern if (!netif_running(rt->dst.dev) || !netif_carrier_ok(rt->dst.dev)) { 3391beb1afacSDavid Ahern *flags |= RTNH_F_LINKDOWN; 3392beb1afacSDavid Ahern if (rt->rt6i_idev->cnf.ignore_routes_with_linkdown) 3393beb1afacSDavid Ahern *flags |= RTNH_F_DEAD; 3394beb1afacSDavid Ahern } 3395beb1afacSDavid Ahern 3396beb1afacSDavid Ahern if (rt->rt6i_flags & RTF_GATEWAY) { 3397beb1afacSDavid Ahern if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0) 3398beb1afacSDavid Ahern goto nla_put_failure; 3399beb1afacSDavid Ahern } 3400beb1afacSDavid Ahern 34015be083ceSDavid Ahern /* not needed for multipath encoding b/c it has a rtnexthop struct */ 34025be083ceSDavid Ahern if (!skip_oif && rt->dst.dev && 3403beb1afacSDavid Ahern nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex)) 3404beb1afacSDavid Ahern goto nla_put_failure; 3405beb1afacSDavid Ahern 3406beb1afacSDavid Ahern if (rt->dst.lwtstate && 3407beb1afacSDavid Ahern lwtunnel_fill_encap(skb, rt->dst.lwtstate) < 0) 3408beb1afacSDavid Ahern goto nla_put_failure; 3409beb1afacSDavid Ahern 3410beb1afacSDavid Ahern return 0; 3411beb1afacSDavid Ahern 3412beb1afacSDavid Ahern nla_put_failure: 3413beb1afacSDavid Ahern return -EMSGSIZE; 3414beb1afacSDavid Ahern } 3415beb1afacSDavid Ahern 34165be083ceSDavid Ahern /* add multipath next hop */ 3417beb1afacSDavid Ahern static int rt6_add_nexthop(struct sk_buff *skb, struct rt6_info *rt) 3418beb1afacSDavid Ahern { 3419beb1afacSDavid Ahern struct rtnexthop *rtnh; 3420beb1afacSDavid Ahern unsigned int flags = 0; 3421beb1afacSDavid Ahern 3422beb1afacSDavid Ahern rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh)); 3423beb1afacSDavid Ahern if (!rtnh) 3424beb1afacSDavid Ahern goto nla_put_failure; 3425beb1afacSDavid Ahern 3426beb1afacSDavid Ahern rtnh->rtnh_hops = 0; 3427beb1afacSDavid Ahern rtnh->rtnh_ifindex = rt->dst.dev ? rt->dst.dev->ifindex : 0; 3428beb1afacSDavid Ahern 34295be083ceSDavid Ahern if (rt6_nexthop_info(skb, rt, &flags, true) < 0) 3430beb1afacSDavid Ahern goto nla_put_failure; 3431beb1afacSDavid Ahern 3432beb1afacSDavid Ahern rtnh->rtnh_flags = flags; 3433beb1afacSDavid Ahern 3434beb1afacSDavid Ahern /* length of rtnetlink header + attributes */ 3435beb1afacSDavid Ahern rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh; 3436beb1afacSDavid Ahern 3437beb1afacSDavid Ahern return 0; 3438beb1afacSDavid Ahern 3439beb1afacSDavid Ahern nla_put_failure: 3440beb1afacSDavid Ahern return -EMSGSIZE; 3441339bf98fSThomas Graf } 3442339bf98fSThomas Graf 3443191cd582SBrian Haley static int rt6_fill_node(struct net *net, 3444191cd582SBrian Haley struct sk_buff *skb, struct rt6_info *rt, 34450d51aa80SJamal Hadi Salim struct in6_addr *dst, struct in6_addr *src, 344615e47304SEric W. Biederman int iif, int type, u32 portid, u32 seq, 3447f8cfe2ceSDavid Ahern unsigned int flags) 34481da177e4SLinus Torvalds { 34494b32b5adSMartin KaFai Lau u32 metrics[RTAX_MAX]; 34501da177e4SLinus Torvalds struct rtmsg *rtm; 34511da177e4SLinus Torvalds struct nlmsghdr *nlh; 3452e3703b3dSThomas Graf long expires; 34539e762a4aSPatrick McHardy u32 table; 34541da177e4SLinus Torvalds 345515e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); 345638308473SDavid S. Miller if (!nlh) 345726932566SPatrick McHardy return -EMSGSIZE; 34582d7202bfSThomas Graf 34592d7202bfSThomas Graf rtm = nlmsg_data(nlh); 34601da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 34611da177e4SLinus Torvalds rtm->rtm_dst_len = rt->rt6i_dst.plen; 34621da177e4SLinus Torvalds rtm->rtm_src_len = rt->rt6i_src.plen; 34631da177e4SLinus Torvalds rtm->rtm_tos = 0; 3464c71099acSThomas Graf if (rt->rt6i_table) 34659e762a4aSPatrick McHardy table = rt->rt6i_table->tb6_id; 3466c71099acSThomas Graf else 34679e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 34689e762a4aSPatrick McHardy rtm->rtm_table = table; 3469c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, table)) 3470c78679e8SDavid S. Miller goto nla_put_failure; 3471ef2c7d7bSNicolas Dichtel if (rt->rt6i_flags & RTF_REJECT) { 3472ef2c7d7bSNicolas Dichtel switch (rt->dst.error) { 3473ef2c7d7bSNicolas Dichtel case -EINVAL: 3474ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_BLACKHOLE; 3475ef2c7d7bSNicolas Dichtel break; 3476ef2c7d7bSNicolas Dichtel case -EACCES: 3477ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_PROHIBIT; 3478ef2c7d7bSNicolas Dichtel break; 3479b4949ab2SNicolas Dichtel case -EAGAIN: 3480b4949ab2SNicolas Dichtel rtm->rtm_type = RTN_THROW; 3481b4949ab2SNicolas Dichtel break; 3482ef2c7d7bSNicolas Dichtel default: 34831da177e4SLinus Torvalds rtm->rtm_type = RTN_UNREACHABLE; 3484ef2c7d7bSNicolas Dichtel break; 3485ef2c7d7bSNicolas Dichtel } 3486ef2c7d7bSNicolas Dichtel } 3487ab79ad14SMaciej Żenczykowski else if (rt->rt6i_flags & RTF_LOCAL) 3488ab79ad14SMaciej Żenczykowski rtm->rtm_type = RTN_LOCAL; 34894ee39733SDavid Ahern else if (rt->rt6i_flags & RTF_ANYCAST) 34904ee39733SDavid Ahern rtm->rtm_type = RTN_ANYCAST; 3491d1918542SDavid S. Miller else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK)) 34921da177e4SLinus Torvalds rtm->rtm_type = RTN_LOCAL; 34931da177e4SLinus Torvalds else 34941da177e4SLinus Torvalds rtm->rtm_type = RTN_UNICAST; 34951da177e4SLinus Torvalds rtm->rtm_flags = 0; 34961da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 34971da177e4SLinus Torvalds rtm->rtm_protocol = rt->rt6i_protocol; 34981da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_DYNAMIC) 34991da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_REDIRECT; 3500f0396f60SDenis Ovsienko else if (rt->rt6i_flags & RTF_ADDRCONF) { 3501f0396f60SDenis Ovsienko if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO)) 35021da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_RA; 3503f0396f60SDenis Ovsienko else 3504f0396f60SDenis Ovsienko rtm->rtm_protocol = RTPROT_KERNEL; 3505f0396f60SDenis Ovsienko } 35061da177e4SLinus Torvalds 35071da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) 35081da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 35091da177e4SLinus Torvalds 35101da177e4SLinus Torvalds if (dst) { 3511930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_DST, dst)) 3512c78679e8SDavid S. Miller goto nla_put_failure; 35131da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 35141da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 3515930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr)) 3516c78679e8SDavid S. Miller goto nla_put_failure; 35171da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 35181da177e4SLinus Torvalds if (src) { 3519930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_SRC, src)) 3520c78679e8SDavid S. Miller goto nla_put_failure; 35211da177e4SLinus Torvalds rtm->rtm_src_len = 128; 3522c78679e8SDavid S. Miller } else if (rtm->rtm_src_len && 3523930345eaSJiri Benc nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr)) 3524c78679e8SDavid S. Miller goto nla_put_failure; 35251da177e4SLinus Torvalds #endif 35267bc570c8SYOSHIFUJI Hideaki if (iif) { 35277bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 35287bc570c8SYOSHIFUJI Hideaki if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) { 3529fd61c6baSDavid Ahern int err = ip6mr_get_route(net, skb, rtm, portid); 35302cf75070SNikolay Aleksandrov 35317bc570c8SYOSHIFUJI Hideaki if (err == 0) 35327bc570c8SYOSHIFUJI Hideaki return 0; 3533fd61c6baSDavid Ahern if (err < 0) 35347bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 35357bc570c8SYOSHIFUJI Hideaki } else 35367bc570c8SYOSHIFUJI Hideaki #endif 3537c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_IIF, iif)) 3538c78679e8SDavid S. Miller goto nla_put_failure; 35397bc570c8SYOSHIFUJI Hideaki } else if (dst) { 35401da177e4SLinus Torvalds struct in6_addr saddr_buf; 3541c78679e8SDavid S. Miller if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 && 3542930345eaSJiri Benc nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 3543c78679e8SDavid S. Miller goto nla_put_failure; 3544c3968a85SDaniel Walter } 3545c3968a85SDaniel Walter 3546c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) { 3547c3968a85SDaniel Walter struct in6_addr saddr_buf; 35484e3fd7a0SAlexey Dobriyan saddr_buf = rt->rt6i_prefsrc.addr; 3549930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 3550c78679e8SDavid S. Miller goto nla_put_failure; 35511da177e4SLinus Torvalds } 35522d7202bfSThomas Graf 35534b32b5adSMartin KaFai Lau memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics)); 35544b32b5adSMartin KaFai Lau if (rt->rt6i_pmtu) 35554b32b5adSMartin KaFai Lau metrics[RTAX_MTU - 1] = rt->rt6i_pmtu; 35564b32b5adSMartin KaFai Lau if (rtnetlink_put_metrics(skb, metrics) < 0) 35572d7202bfSThomas Graf goto nla_put_failure; 35582d7202bfSThomas Graf 3559beb1afacSDavid Ahern if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric)) 3560beb1afacSDavid Ahern goto nla_put_failure; 3561beb1afacSDavid Ahern 3562beb1afacSDavid Ahern /* For multipath routes, walk the siblings list and add 3563beb1afacSDavid Ahern * each as a nexthop within RTA_MULTIPATH. 3564beb1afacSDavid Ahern */ 3565beb1afacSDavid Ahern if (rt->rt6i_nsiblings) { 3566beb1afacSDavid Ahern struct rt6_info *sibling, *next_sibling; 3567beb1afacSDavid Ahern struct nlattr *mp; 3568beb1afacSDavid Ahern 3569beb1afacSDavid Ahern mp = nla_nest_start(skb, RTA_MULTIPATH); 3570beb1afacSDavid Ahern if (!mp) 3571beb1afacSDavid Ahern goto nla_put_failure; 3572beb1afacSDavid Ahern 3573beb1afacSDavid Ahern if (rt6_add_nexthop(skb, rt) < 0) 3574beb1afacSDavid Ahern goto nla_put_failure; 3575beb1afacSDavid Ahern 3576beb1afacSDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 3577beb1afacSDavid Ahern &rt->rt6i_siblings, rt6i_siblings) { 3578beb1afacSDavid Ahern if (rt6_add_nexthop(skb, sibling) < 0) 357994f826b8SEric Dumazet goto nla_put_failure; 358094f826b8SEric Dumazet } 35812d7202bfSThomas Graf 3582beb1afacSDavid Ahern nla_nest_end(skb, mp); 3583beb1afacSDavid Ahern } else { 35845be083ceSDavid Ahern if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0) 3585c78679e8SDavid S. Miller goto nla_put_failure; 3586beb1afacSDavid Ahern } 35878253947eSLi Wei 35888253947eSLi Wei expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0; 358969cdf8f9SYOSHIFUJI Hideaki 359087a50699SDavid S. Miller if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0) 3591e3703b3dSThomas Graf goto nla_put_failure; 35921da177e4SLinus Torvalds 3593c78ba6d6SLubomir Rintel if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags))) 3594c78ba6d6SLubomir Rintel goto nla_put_failure; 3595c78ba6d6SLubomir Rintel 359619e42e45SRoopa Prabhu 3597053c095aSJohannes Berg nlmsg_end(skb, nlh); 3598053c095aSJohannes Berg return 0; 35992d7202bfSThomas Graf 36002d7202bfSThomas Graf nla_put_failure: 360126932566SPatrick McHardy nlmsg_cancel(skb, nlh); 360226932566SPatrick McHardy return -EMSGSIZE; 36031da177e4SLinus Torvalds } 36041da177e4SLinus Torvalds 36051b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg) 36061da177e4SLinus Torvalds { 36071da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 36081f17e2f2SDavid Ahern struct net *net = arg->net; 36091f17e2f2SDavid Ahern 36101f17e2f2SDavid Ahern if (rt == net->ipv6.ip6_null_entry) 36111f17e2f2SDavid Ahern return 0; 36121da177e4SLinus Torvalds 36132d7202bfSThomas Graf if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { 36142d7202bfSThomas Graf struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); 3615f8cfe2ceSDavid Ahern 3616f8cfe2ceSDavid Ahern /* user wants prefix routes only */ 3617f8cfe2ceSDavid Ahern if (rtm->rtm_flags & RTM_F_PREFIX && 3618f8cfe2ceSDavid Ahern !(rt->rt6i_flags & RTF_PREFIX_RT)) { 3619f8cfe2ceSDavid Ahern /* success since this is not a prefix route */ 3620f8cfe2ceSDavid Ahern return 1; 3621f8cfe2ceSDavid Ahern } 3622f8cfe2ceSDavid Ahern } 36231da177e4SLinus Torvalds 36241f17e2f2SDavid Ahern return rt6_fill_node(net, 3625191cd582SBrian Haley arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE, 362615e47304SEric W. Biederman NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq, 3627f8cfe2ceSDavid Ahern NLM_F_MULTI); 36281da177e4SLinus Torvalds } 36291da177e4SLinus Torvalds 3630c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, 3631c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 36321da177e4SLinus Torvalds { 36333b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 3634ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 363518c3a61cSRoopa Prabhu int err, iif = 0, oif = 0; 363618c3a61cSRoopa Prabhu struct dst_entry *dst; 36371da177e4SLinus Torvalds struct rt6_info *rt; 3638ab364a6fSThomas Graf struct sk_buff *skb; 3639ab364a6fSThomas Graf struct rtmsg *rtm; 36404c9483b2SDavid S. Miller struct flowi6 fl6; 364118c3a61cSRoopa Prabhu bool fibmatch; 3642ab364a6fSThomas Graf 3643fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 3644c21ef3e3SDavid Ahern extack); 3645ab364a6fSThomas Graf if (err < 0) 3646ab364a6fSThomas Graf goto errout; 3647ab364a6fSThomas Graf 3648ab364a6fSThomas Graf err = -EINVAL; 36494c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 365038b7097bSHannes Frederic Sowa rtm = nlmsg_data(nlh); 365138b7097bSHannes Frederic Sowa fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0); 365218c3a61cSRoopa Prabhu fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); 3653ab364a6fSThomas Graf 3654ab364a6fSThomas Graf if (tb[RTA_SRC]) { 3655ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 3656ab364a6fSThomas Graf goto errout; 3657ab364a6fSThomas Graf 36584e3fd7a0SAlexey Dobriyan fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 3659ab364a6fSThomas Graf } 3660ab364a6fSThomas Graf 3661ab364a6fSThomas Graf if (tb[RTA_DST]) { 3662ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 3663ab364a6fSThomas Graf goto errout; 3664ab364a6fSThomas Graf 36654e3fd7a0SAlexey Dobriyan fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 3666ab364a6fSThomas Graf } 3667ab364a6fSThomas Graf 3668ab364a6fSThomas Graf if (tb[RTA_IIF]) 3669ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 3670ab364a6fSThomas Graf 3671ab364a6fSThomas Graf if (tb[RTA_OIF]) 367272331bc0SShmulik Ladkani oif = nla_get_u32(tb[RTA_OIF]); 3673ab364a6fSThomas Graf 36742e47b291SLorenzo Colitti if (tb[RTA_MARK]) 36752e47b291SLorenzo Colitti fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); 36762e47b291SLorenzo Colitti 3677622ec2c9SLorenzo Colitti if (tb[RTA_UID]) 3678622ec2c9SLorenzo Colitti fl6.flowi6_uid = make_kuid(current_user_ns(), 3679622ec2c9SLorenzo Colitti nla_get_u32(tb[RTA_UID])); 3680622ec2c9SLorenzo Colitti else 3681622ec2c9SLorenzo Colitti fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); 3682622ec2c9SLorenzo Colitti 3683ab364a6fSThomas Graf if (iif) { 3684ab364a6fSThomas Graf struct net_device *dev; 368572331bc0SShmulik Ladkani int flags = 0; 368672331bc0SShmulik Ladkani 36875578689aSDaniel Lezcano dev = __dev_get_by_index(net, iif); 3688ab364a6fSThomas Graf if (!dev) { 3689ab364a6fSThomas Graf err = -ENODEV; 3690ab364a6fSThomas Graf goto errout; 3691ab364a6fSThomas Graf } 369272331bc0SShmulik Ladkani 369372331bc0SShmulik Ladkani fl6.flowi6_iif = iif; 369472331bc0SShmulik Ladkani 369572331bc0SShmulik Ladkani if (!ipv6_addr_any(&fl6.saddr)) 369672331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_HAS_SADDR; 369772331bc0SShmulik Ladkani 369818c3a61cSRoopa Prabhu if (!fibmatch) 369918c3a61cSRoopa Prabhu dst = ip6_route_input_lookup(net, dev, &fl6, flags); 370072331bc0SShmulik Ladkani } else { 370172331bc0SShmulik Ladkani fl6.flowi6_oif = oif; 370272331bc0SShmulik Ladkani 370318c3a61cSRoopa Prabhu if (!fibmatch) 370418c3a61cSRoopa Prabhu dst = ip6_route_output(net, NULL, &fl6); 370518c3a61cSRoopa Prabhu } 370618c3a61cSRoopa Prabhu 370718c3a61cSRoopa Prabhu if (fibmatch) 370818c3a61cSRoopa Prabhu dst = ip6_route_lookup(net, &fl6, 0); 370918c3a61cSRoopa Prabhu 371018c3a61cSRoopa Prabhu rt = container_of(dst, struct rt6_info, dst); 371118c3a61cSRoopa Prabhu if (rt->dst.error) { 371218c3a61cSRoopa Prabhu err = rt->dst.error; 371318c3a61cSRoopa Prabhu ip6_rt_put(rt); 371418c3a61cSRoopa Prabhu goto errout; 3715ab364a6fSThomas Graf } 37161da177e4SLinus Torvalds 37179d6acb3bSWANG Cong if (rt == net->ipv6.ip6_null_entry) { 37189d6acb3bSWANG Cong err = rt->dst.error; 37199d6acb3bSWANG Cong ip6_rt_put(rt); 37209d6acb3bSWANG Cong goto errout; 37219d6acb3bSWANG Cong } 37229d6acb3bSWANG Cong 37231da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 372438308473SDavid S. Miller if (!skb) { 372594e187c0SAmerigo Wang ip6_rt_put(rt); 3726ab364a6fSThomas Graf err = -ENOBUFS; 3727ab364a6fSThomas Graf goto errout; 3728ab364a6fSThomas Graf } 37291da177e4SLinus Torvalds 3730d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 373118c3a61cSRoopa Prabhu if (fibmatch) 373218c3a61cSRoopa Prabhu err = rt6_fill_node(net, skb, rt, NULL, NULL, iif, 373318c3a61cSRoopa Prabhu RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 373418c3a61cSRoopa Prabhu nlh->nlmsg_seq, 0); 373518c3a61cSRoopa Prabhu else 37364c9483b2SDavid S. Miller err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif, 373715e47304SEric W. Biederman RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 3738f8cfe2ceSDavid Ahern nlh->nlmsg_seq, 0); 37391da177e4SLinus Torvalds if (err < 0) { 3740ab364a6fSThomas Graf kfree_skb(skb); 3741ab364a6fSThomas Graf goto errout; 37421da177e4SLinus Torvalds } 37431da177e4SLinus Torvalds 374415e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 3745ab364a6fSThomas Graf errout: 37461da177e4SLinus Torvalds return err; 37471da177e4SLinus Torvalds } 37481da177e4SLinus Torvalds 374937a1d361SRoopa Prabhu void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info, 375037a1d361SRoopa Prabhu unsigned int nlm_flags) 37511da177e4SLinus Torvalds { 37521da177e4SLinus Torvalds struct sk_buff *skb; 37535578689aSDaniel Lezcano struct net *net = info->nl_net; 3754528c4cebSDenis V. Lunev u32 seq; 3755528c4cebSDenis V. Lunev int err; 37560d51aa80SJamal Hadi Salim 3757528c4cebSDenis V. Lunev err = -ENOBUFS; 375838308473SDavid S. Miller seq = info->nlh ? info->nlh->nlmsg_seq : 0; 375986872cb5SThomas Graf 376019e42e45SRoopa Prabhu skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 376138308473SDavid S. Miller if (!skb) 376221713ebcSThomas Graf goto errout; 37631da177e4SLinus Torvalds 3764191cd582SBrian Haley err = rt6_fill_node(net, skb, rt, NULL, NULL, 0, 3765f8cfe2ceSDavid Ahern event, info->portid, seq, nlm_flags); 376626932566SPatrick McHardy if (err < 0) { 376726932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 376826932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 376926932566SPatrick McHardy kfree_skb(skb); 377026932566SPatrick McHardy goto errout; 377126932566SPatrick McHardy } 377215e47304SEric W. Biederman rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 37735578689aSDaniel Lezcano info->nlh, gfp_any()); 37741ce85fe4SPablo Neira Ayuso return; 377521713ebcSThomas Graf errout: 377621713ebcSThomas Graf if (err < 0) 37775578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 37781da177e4SLinus Torvalds } 37791da177e4SLinus Torvalds 37808ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 3781351638e7SJiri Pirko unsigned long event, void *ptr) 37828ed67789SDaniel Lezcano { 3783351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 3784c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 37858ed67789SDaniel Lezcano 3786242d3a49SWANG Cong if (!(dev->flags & IFF_LOOPBACK)) 3787242d3a49SWANG Cong return NOTIFY_OK; 3788242d3a49SWANG Cong 3789242d3a49SWANG Cong if (event == NETDEV_REGISTER) { 3790d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 37918ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 37928ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 3793d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 37948ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 3795d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 37968ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 37978ed67789SDaniel Lezcano #endif 3798242d3a49SWANG Cong } else if (event == NETDEV_UNREGISTER) { 3799242d3a49SWANG Cong in6_dev_put(net->ipv6.ip6_null_entry->rt6i_idev); 3800242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 3801242d3a49SWANG Cong in6_dev_put(net->ipv6.ip6_prohibit_entry->rt6i_idev); 3802242d3a49SWANG Cong in6_dev_put(net->ipv6.ip6_blk_hole_entry->rt6i_idev); 3803242d3a49SWANG Cong #endif 38048ed67789SDaniel Lezcano } 38058ed67789SDaniel Lezcano 38068ed67789SDaniel Lezcano return NOTIFY_OK; 38078ed67789SDaniel Lezcano } 38088ed67789SDaniel Lezcano 38091da177e4SLinus Torvalds /* 38101da177e4SLinus Torvalds * /proc 38111da177e4SLinus Torvalds */ 38121da177e4SLinus Torvalds 38131da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 38141da177e4SLinus Torvalds 381533120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = { 381633120b30SAlexey Dobriyan .owner = THIS_MODULE, 381733120b30SAlexey Dobriyan .open = ipv6_route_open, 381833120b30SAlexey Dobriyan .read = seq_read, 381933120b30SAlexey Dobriyan .llseek = seq_lseek, 38208d2ca1d7SHannes Frederic Sowa .release = seq_release_net, 382133120b30SAlexey Dobriyan }; 382233120b30SAlexey Dobriyan 38231da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 38241da177e4SLinus Torvalds { 382569ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 38261da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 382769ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 382869ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 382969ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_alloc, 383069ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 383169ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 3832fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 383369ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 38341da177e4SLinus Torvalds 38351da177e4SLinus Torvalds return 0; 38361da177e4SLinus Torvalds } 38371da177e4SLinus Torvalds 38381da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file) 38391da177e4SLinus Torvalds { 3840de05c557SPavel Emelyanov return single_open_net(inode, file, rt6_stats_seq_show); 384169ddb805SDaniel Lezcano } 384269ddb805SDaniel Lezcano 38439a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = { 38441da177e4SLinus Torvalds .owner = THIS_MODULE, 38451da177e4SLinus Torvalds .open = rt6_stats_seq_open, 38461da177e4SLinus Torvalds .read = seq_read, 38471da177e4SLinus Torvalds .llseek = seq_lseek, 3848b6fcbdb4SPavel Emelyanov .release = single_release_net, 38491da177e4SLinus Torvalds }; 38501da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 38511da177e4SLinus Torvalds 38521da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 38531da177e4SLinus Torvalds 38541da177e4SLinus Torvalds static 3855fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, 38561da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 38571da177e4SLinus Torvalds { 3858c486da34SLucian Adrian Grijincu struct net *net; 3859c486da34SLucian Adrian Grijincu int delay; 3860c486da34SLucian Adrian Grijincu if (!write) 3861c486da34SLucian Adrian Grijincu return -EINVAL; 3862c486da34SLucian Adrian Grijincu 3863c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 3864c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 38658d65af78SAlexey Dobriyan proc_dointvec(ctl, write, buffer, lenp, ppos); 38662ac3ac8fSMichal Kubeček fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0); 38671da177e4SLinus Torvalds return 0; 38681da177e4SLinus Torvalds } 38691da177e4SLinus Torvalds 3870fe2c6338SJoe Perches struct ctl_table ipv6_route_table_template[] = { 38711da177e4SLinus Torvalds { 38721da177e4SLinus Torvalds .procname = "flush", 38734990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 38741da177e4SLinus Torvalds .maxlen = sizeof(int), 387589c8b3a1SDave Jones .mode = 0200, 38766d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 38771da177e4SLinus Torvalds }, 38781da177e4SLinus Torvalds { 38791da177e4SLinus Torvalds .procname = "gc_thresh", 38809a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 38811da177e4SLinus Torvalds .maxlen = sizeof(int), 38821da177e4SLinus Torvalds .mode = 0644, 38836d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 38841da177e4SLinus Torvalds }, 38851da177e4SLinus Torvalds { 38861da177e4SLinus Torvalds .procname = "max_size", 38874990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 38881da177e4SLinus Torvalds .maxlen = sizeof(int), 38891da177e4SLinus Torvalds .mode = 0644, 38906d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 38911da177e4SLinus Torvalds }, 38921da177e4SLinus Torvalds { 38931da177e4SLinus Torvalds .procname = "gc_min_interval", 38944990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 38951da177e4SLinus Torvalds .maxlen = sizeof(int), 38961da177e4SLinus Torvalds .mode = 0644, 38976d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 38981da177e4SLinus Torvalds }, 38991da177e4SLinus Torvalds { 39001da177e4SLinus Torvalds .procname = "gc_timeout", 39014990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 39021da177e4SLinus Torvalds .maxlen = sizeof(int), 39031da177e4SLinus Torvalds .mode = 0644, 39046d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 39051da177e4SLinus Torvalds }, 39061da177e4SLinus Torvalds { 39071da177e4SLinus Torvalds .procname = "gc_interval", 39084990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 39091da177e4SLinus Torvalds .maxlen = sizeof(int), 39101da177e4SLinus Torvalds .mode = 0644, 39116d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 39121da177e4SLinus Torvalds }, 39131da177e4SLinus Torvalds { 39141da177e4SLinus Torvalds .procname = "gc_elasticity", 39154990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 39161da177e4SLinus Torvalds .maxlen = sizeof(int), 39171da177e4SLinus Torvalds .mode = 0644, 3918f3d3f616SMin Zhang .proc_handler = proc_dointvec, 39191da177e4SLinus Torvalds }, 39201da177e4SLinus Torvalds { 39211da177e4SLinus Torvalds .procname = "mtu_expires", 39224990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 39231da177e4SLinus Torvalds .maxlen = sizeof(int), 39241da177e4SLinus Torvalds .mode = 0644, 39256d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 39261da177e4SLinus Torvalds }, 39271da177e4SLinus Torvalds { 39281da177e4SLinus Torvalds .procname = "min_adv_mss", 39294990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 39301da177e4SLinus Torvalds .maxlen = sizeof(int), 39311da177e4SLinus Torvalds .mode = 0644, 3932f3d3f616SMin Zhang .proc_handler = proc_dointvec, 39331da177e4SLinus Torvalds }, 39341da177e4SLinus Torvalds { 39351da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 39364990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 39371da177e4SLinus Torvalds .maxlen = sizeof(int), 39381da177e4SLinus Torvalds .mode = 0644, 39396d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 39401da177e4SLinus Torvalds }, 3941f8572d8fSEric W. Biederman { } 39421da177e4SLinus Torvalds }; 39431da177e4SLinus Torvalds 39442c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 3945760f2d01SDaniel Lezcano { 3946760f2d01SDaniel Lezcano struct ctl_table *table; 3947760f2d01SDaniel Lezcano 3948760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 3949760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 3950760f2d01SDaniel Lezcano GFP_KERNEL); 39515ee09105SYOSHIFUJI Hideaki 39525ee09105SYOSHIFUJI Hideaki if (table) { 39535ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 3954c486da34SLucian Adrian Grijincu table[0].extra1 = net; 395586393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 39565ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 39575ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 39585ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 39595ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 39605ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 39615ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 39625ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 39639c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 3964464dc801SEric W. Biederman 3965464dc801SEric W. Biederman /* Don't export sysctls to unprivileged users */ 3966464dc801SEric W. Biederman if (net->user_ns != &init_user_ns) 3967464dc801SEric W. Biederman table[0].procname = NULL; 39685ee09105SYOSHIFUJI Hideaki } 39695ee09105SYOSHIFUJI Hideaki 3970760f2d01SDaniel Lezcano return table; 3971760f2d01SDaniel Lezcano } 39721da177e4SLinus Torvalds #endif 39731da177e4SLinus Torvalds 39742c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 3975cdb18761SDaniel Lezcano { 3976633d424bSPavel Emelyanov int ret = -ENOMEM; 39778ed67789SDaniel Lezcano 397886393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 397986393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 3980f2fc6a54SBenjamin Thery 3981fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 3982fc66f95cSEric Dumazet goto out_ip6_dst_ops; 3983fc66f95cSEric Dumazet 39848ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 39858ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 39868ed67789SDaniel Lezcano GFP_KERNEL); 39878ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 3988fc66f95cSEric Dumazet goto out_ip6_dst_entries; 3989d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.path = 39908ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_null_entry; 3991d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 399262fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 399362fa8a84SDavid S. Miller ip6_template_metrics, true); 39948ed67789SDaniel Lezcano 39958ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 39968ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 39978ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 39988ed67789SDaniel Lezcano GFP_KERNEL); 399968fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 400068fffc67SPeter Zijlstra goto out_ip6_null_entry; 4001d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.path = 40028ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_prohibit_entry; 4003d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 400462fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 400562fa8a84SDavid S. Miller ip6_template_metrics, true); 40068ed67789SDaniel Lezcano 40078ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 40088ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 40098ed67789SDaniel Lezcano GFP_KERNEL); 401068fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 401168fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 4012d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.path = 40138ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_blk_hole_entry; 4014d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 401562fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 401662fa8a84SDavid S. Miller ip6_template_metrics, true); 40178ed67789SDaniel Lezcano #endif 40188ed67789SDaniel Lezcano 4019b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 4020b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 4021b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 4022b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 4023b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 4024b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 4025b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 4026b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 4027b339a47cSPeter Zijlstra 40286891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 40296891a346SBenjamin Thery 40308ed67789SDaniel Lezcano ret = 0; 40318ed67789SDaniel Lezcano out: 40328ed67789SDaniel Lezcano return ret; 4033f2fc6a54SBenjamin Thery 403468fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 403568fffc67SPeter Zijlstra out_ip6_prohibit_entry: 403668fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 403768fffc67SPeter Zijlstra out_ip6_null_entry: 403868fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 403968fffc67SPeter Zijlstra #endif 4040fc66f95cSEric Dumazet out_ip6_dst_entries: 4041fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 4042f2fc6a54SBenjamin Thery out_ip6_dst_ops: 4043f2fc6a54SBenjamin Thery goto out; 4044cdb18761SDaniel Lezcano } 4045cdb18761SDaniel Lezcano 40462c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 4047cdb18761SDaniel Lezcano { 40488ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 40498ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 40508ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 40518ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 40528ed67789SDaniel Lezcano #endif 405341bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 4054cdb18761SDaniel Lezcano } 4055cdb18761SDaniel Lezcano 4056d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net) 4057d189634eSThomas Graf { 4058d189634eSThomas Graf #ifdef CONFIG_PROC_FS 4059d4beaa66SGao feng proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops); 4060d4beaa66SGao feng proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops); 4061d189634eSThomas Graf #endif 4062d189634eSThomas Graf return 0; 4063d189634eSThomas Graf } 4064d189634eSThomas Graf 4065d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net) 4066d189634eSThomas Graf { 4067d189634eSThomas Graf #ifdef CONFIG_PROC_FS 4068ece31ffdSGao feng remove_proc_entry("ipv6_route", net->proc_net); 4069ece31ffdSGao feng remove_proc_entry("rt6_stats", net->proc_net); 4070d189634eSThomas Graf #endif 4071d189634eSThomas Graf } 4072d189634eSThomas Graf 4073cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 4074cdb18761SDaniel Lezcano .init = ip6_route_net_init, 4075cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 4076cdb18761SDaniel Lezcano }; 4077cdb18761SDaniel Lezcano 4078c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net) 4079c3426b47SDavid S. Miller { 4080c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 4081c3426b47SDavid S. Miller 4082c3426b47SDavid S. Miller if (!bp) 4083c3426b47SDavid S. Miller return -ENOMEM; 4084c3426b47SDavid S. Miller inet_peer_base_init(bp); 4085c3426b47SDavid S. Miller net->ipv6.peers = bp; 4086c3426b47SDavid S. Miller return 0; 4087c3426b47SDavid S. Miller } 4088c3426b47SDavid S. Miller 4089c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net) 4090c3426b47SDavid S. Miller { 4091c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv6.peers; 4092c3426b47SDavid S. Miller 4093c3426b47SDavid S. Miller net->ipv6.peers = NULL; 409456a6b248SDavid S. Miller inetpeer_invalidate_tree(bp); 4095c3426b47SDavid S. Miller kfree(bp); 4096c3426b47SDavid S. Miller } 4097c3426b47SDavid S. Miller 40982b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = { 4099c3426b47SDavid S. Miller .init = ipv6_inetpeer_init, 4100c3426b47SDavid S. Miller .exit = ipv6_inetpeer_exit, 4101c3426b47SDavid S. Miller }; 4102c3426b47SDavid S. Miller 4103d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = { 4104d189634eSThomas Graf .init = ip6_route_net_init_late, 4105d189634eSThomas Graf .exit = ip6_route_net_exit_late, 4106d189634eSThomas Graf }; 4107d189634eSThomas Graf 41088ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 41098ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 4110242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY - 10, 41118ed67789SDaniel Lezcano }; 41128ed67789SDaniel Lezcano 41132f460933SWANG Cong void __init ip6_route_init_special_entries(void) 41142f460933SWANG Cong { 41152f460933SWANG Cong /* Registering of the loopback is done before this portion of code, 41162f460933SWANG Cong * the loopback reference in rt6_info will not be taken, do it 41172f460933SWANG Cong * manually for init_net */ 41182f460933SWANG Cong init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 41192f460933SWANG Cong init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 41202f460933SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 41212f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 41222f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 41232f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 41242f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 41252f460933SWANG Cong #endif 41262f460933SWANG Cong } 41272f460933SWANG Cong 4128433d49c3SDaniel Lezcano int __init ip6_route_init(void) 41291da177e4SLinus Torvalds { 4130433d49c3SDaniel Lezcano int ret; 41318d0b94afSMartin KaFai Lau int cpu; 4132433d49c3SDaniel Lezcano 41339a7ec3a9SDaniel Lezcano ret = -ENOMEM; 41349a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 41359a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 41369a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 41379a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 4138c19a28e1SFernando Carrijo goto out; 413914e50e57SDavid S. Miller 4140fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 41418ed67789SDaniel Lezcano if (ret) 4142bdb3289fSDaniel Lezcano goto out_kmem_cache; 4143bdb3289fSDaniel Lezcano 4144c3426b47SDavid S. Miller ret = register_pernet_subsys(&ipv6_inetpeer_ops); 4145c3426b47SDavid S. Miller if (ret) 4146e8803b6cSDavid S. Miller goto out_dst_entries; 41472a0c451aSThomas Graf 41487e52b33bSDavid S. Miller ret = register_pernet_subsys(&ip6_route_net_ops); 41497e52b33bSDavid S. Miller if (ret) 41507e52b33bSDavid S. Miller goto out_register_inetpeer; 4151c3426b47SDavid S. Miller 41525dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 41535dc121e9SArnaud Ebalard 4154e8803b6cSDavid S. Miller ret = fib6_init(); 4155433d49c3SDaniel Lezcano if (ret) 41568ed67789SDaniel Lezcano goto out_register_subsys; 4157433d49c3SDaniel Lezcano 4158433d49c3SDaniel Lezcano ret = xfrm6_init(); 4159433d49c3SDaniel Lezcano if (ret) 4160e8803b6cSDavid S. Miller goto out_fib6_init; 4161c35b7e72SDaniel Lezcano 4162433d49c3SDaniel Lezcano ret = fib6_rules_init(); 4163433d49c3SDaniel Lezcano if (ret) 4164433d49c3SDaniel Lezcano goto xfrm6_init; 41657e5449c2SDaniel Lezcano 4166d189634eSThomas Graf ret = register_pernet_subsys(&ip6_route_net_late_ops); 4167d189634eSThomas Graf if (ret) 4168d189634eSThomas Graf goto fib6_rules_init; 4169d189634eSThomas Graf 4170433d49c3SDaniel Lezcano ret = -ENOBUFS; 4171c7ac8679SGreg Rose if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) || 4172c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) || 4173c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL)) 4174d189634eSThomas Graf goto out_register_late_subsys; 4175433d49c3SDaniel Lezcano 41768ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 4177cdb18761SDaniel Lezcano if (ret) 4178d189634eSThomas Graf goto out_register_late_subsys; 41798ed67789SDaniel Lezcano 41808d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 41818d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 41828d0b94afSMartin KaFai Lau 41838d0b94afSMartin KaFai Lau INIT_LIST_HEAD(&ul->head); 41848d0b94afSMartin KaFai Lau spin_lock_init(&ul->lock); 41858d0b94afSMartin KaFai Lau } 41868d0b94afSMartin KaFai Lau 4187433d49c3SDaniel Lezcano out: 4188433d49c3SDaniel Lezcano return ret; 4189433d49c3SDaniel Lezcano 4190d189634eSThomas Graf out_register_late_subsys: 4191d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 4192433d49c3SDaniel Lezcano fib6_rules_init: 4193433d49c3SDaniel Lezcano fib6_rules_cleanup(); 4194433d49c3SDaniel Lezcano xfrm6_init: 4195433d49c3SDaniel Lezcano xfrm6_fini(); 41962a0c451aSThomas Graf out_fib6_init: 41972a0c451aSThomas Graf fib6_gc_cleanup(); 41988ed67789SDaniel Lezcano out_register_subsys: 41998ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 42007e52b33bSDavid S. Miller out_register_inetpeer: 42017e52b33bSDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 4202fc66f95cSEric Dumazet out_dst_entries: 4203fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 4204433d49c3SDaniel Lezcano out_kmem_cache: 4205f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 4206433d49c3SDaniel Lezcano goto out; 42071da177e4SLinus Torvalds } 42081da177e4SLinus Torvalds 42091da177e4SLinus Torvalds void ip6_route_cleanup(void) 42101da177e4SLinus Torvalds { 42118ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 4212d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 4213101367c2SThomas Graf fib6_rules_cleanup(); 42141da177e4SLinus Torvalds xfrm6_fini(); 42151da177e4SLinus Torvalds fib6_gc_cleanup(); 4216c3426b47SDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 42178ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 421841bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 4219f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 42201da177e4SLinus Torvalds } 4221