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->rt6i_uncached_list = ul; 1328d0b94afSMartin KaFai Lau 1338d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1348d0b94afSMartin KaFai Lau list_add_tail(&rt->rt6i_uncached, &ul->head); 1358d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1368d0b94afSMartin KaFai Lau } 1378d0b94afSMartin KaFai Lau 1388d0b94afSMartin KaFai Lau static void rt6_uncached_list_del(struct rt6_info *rt) 1398d0b94afSMartin KaFai Lau { 1408d0b94afSMartin KaFai Lau if (!list_empty(&rt->rt6i_uncached)) { 1418d0b94afSMartin KaFai Lau struct uncached_list *ul = rt->rt6i_uncached_list; 1428d0b94afSMartin KaFai Lau 1438d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1448d0b94afSMartin KaFai Lau list_del(&rt->rt6i_uncached); 1458d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1468d0b94afSMartin KaFai Lau } 1478d0b94afSMartin KaFai Lau } 1488d0b94afSMartin KaFai Lau 1498d0b94afSMartin KaFai Lau static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev) 1508d0b94afSMartin KaFai Lau { 1518d0b94afSMartin KaFai Lau struct net_device *loopback_dev = net->loopback_dev; 1528d0b94afSMartin KaFai Lau int cpu; 1538d0b94afSMartin KaFai Lau 154e332bc67SEric W. Biederman if (dev == loopback_dev) 155e332bc67SEric W. Biederman return; 156e332bc67SEric W. Biederman 1578d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 1588d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 1598d0b94afSMartin KaFai Lau struct rt6_info *rt; 1608d0b94afSMartin KaFai Lau 1618d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1628d0b94afSMartin KaFai Lau list_for_each_entry(rt, &ul->head, rt6i_uncached) { 1638d0b94afSMartin KaFai Lau struct inet6_dev *rt_idev = rt->rt6i_idev; 1648d0b94afSMartin KaFai Lau struct net_device *rt_dev = rt->dst.dev; 1658d0b94afSMartin KaFai Lau 166e332bc67SEric W. Biederman if (rt_idev->dev == dev) { 1678d0b94afSMartin KaFai Lau rt->rt6i_idev = in6_dev_get(loopback_dev); 1688d0b94afSMartin KaFai Lau in6_dev_put(rt_idev); 1698d0b94afSMartin KaFai Lau } 1708d0b94afSMartin KaFai Lau 171e332bc67SEric W. Biederman if (rt_dev == dev) { 1728d0b94afSMartin KaFai Lau rt->dst.dev = loopback_dev; 1738d0b94afSMartin KaFai Lau dev_hold(rt->dst.dev); 1748d0b94afSMartin KaFai Lau dev_put(rt_dev); 1758d0b94afSMartin KaFai Lau } 1768d0b94afSMartin KaFai Lau } 1778d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1788d0b94afSMartin KaFai Lau } 1798d0b94afSMartin KaFai Lau } 1808d0b94afSMartin KaFai Lau 181d52d3997SMartin KaFai Lau static u32 *rt6_pcpu_cow_metrics(struct rt6_info *rt) 182d52d3997SMartin KaFai Lau { 183d52d3997SMartin KaFai Lau return dst_metrics_write_ptr(rt->dst.from); 184d52d3997SMartin KaFai Lau } 185d52d3997SMartin KaFai Lau 18606582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old) 18706582540SDavid S. Miller { 18806582540SDavid S. Miller struct rt6_info *rt = (struct rt6_info *)dst; 18906582540SDavid S. Miller 190d52d3997SMartin KaFai Lau if (rt->rt6i_flags & RTF_PCPU) 191d52d3997SMartin KaFai Lau return rt6_pcpu_cow_metrics(rt); 192d52d3997SMartin KaFai Lau else if (rt->rt6i_flags & RTF_CACHE) 1934b32b5adSMartin KaFai Lau return NULL; 1944b32b5adSMartin KaFai Lau else 1953b471175SMartin KaFai Lau return dst_cow_metrics_generic(dst, old); 19606582540SDavid S. Miller } 19706582540SDavid S. Miller 198f894cbf8SDavid S. Miller static inline const void *choose_neigh_daddr(struct rt6_info *rt, 199f894cbf8SDavid S. Miller struct sk_buff *skb, 200f894cbf8SDavid S. Miller const void *daddr) 20139232973SDavid S. Miller { 20239232973SDavid S. Miller struct in6_addr *p = &rt->rt6i_gateway; 20339232973SDavid S. Miller 204a7563f34SDavid S. Miller if (!ipv6_addr_any(p)) 20539232973SDavid S. Miller return (const void *) p; 206f894cbf8SDavid S. Miller else if (skb) 207f894cbf8SDavid S. Miller return &ipv6_hdr(skb)->daddr; 20839232973SDavid S. Miller return daddr; 20939232973SDavid S. Miller } 21039232973SDavid S. Miller 211f894cbf8SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst, 212f894cbf8SDavid S. Miller struct sk_buff *skb, 213f894cbf8SDavid S. Miller const void *daddr) 214d3aaeb38SDavid S. Miller { 21539232973SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 21639232973SDavid S. Miller struct neighbour *n; 21739232973SDavid S. Miller 218f894cbf8SDavid S. Miller daddr = choose_neigh_daddr(rt, skb, daddr); 2198e022ee6SYOSHIFUJI Hideaki / 吉藤英明 n = __ipv6_neigh_lookup(dst->dev, daddr); 220f83c7790SDavid S. Miller if (n) 221f83c7790SDavid S. Miller return n; 222f83c7790SDavid S. Miller return neigh_create(&nd_tbl, daddr, dst->dev); 223f83c7790SDavid S. Miller } 224f83c7790SDavid S. Miller 22563fca65dSJulian Anastasov static void ip6_confirm_neigh(const struct dst_entry *dst, const void *daddr) 22663fca65dSJulian Anastasov { 22763fca65dSJulian Anastasov struct net_device *dev = dst->dev; 22863fca65dSJulian Anastasov struct rt6_info *rt = (struct rt6_info *)dst; 22963fca65dSJulian Anastasov 23063fca65dSJulian Anastasov daddr = choose_neigh_daddr(rt, NULL, daddr); 23163fca65dSJulian Anastasov if (!daddr) 23263fca65dSJulian Anastasov return; 23363fca65dSJulian Anastasov if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 23463fca65dSJulian Anastasov return; 23563fca65dSJulian Anastasov if (ipv6_addr_is_multicast((const struct in6_addr *)daddr)) 23663fca65dSJulian Anastasov return; 23763fca65dSJulian Anastasov __ipv6_confirm_neigh(dev, daddr); 23863fca65dSJulian Anastasov } 23963fca65dSJulian Anastasov 2409a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = { 2411da177e4SLinus Torvalds .family = AF_INET6, 2421da177e4SLinus Torvalds .gc = ip6_dst_gc, 2431da177e4SLinus Torvalds .gc_thresh = 1024, 2441da177e4SLinus Torvalds .check = ip6_dst_check, 2450dbaee3bSDavid S. Miller .default_advmss = ip6_default_advmss, 246ebb762f2SSteffen Klassert .mtu = ip6_mtu, 24706582540SDavid S. Miller .cow_metrics = ipv6_cow_metrics, 2481da177e4SLinus Torvalds .destroy = ip6_dst_destroy, 2491da177e4SLinus Torvalds .ifdown = ip6_dst_ifdown, 2501da177e4SLinus Torvalds .negative_advice = ip6_negative_advice, 2511da177e4SLinus Torvalds .link_failure = ip6_link_failure, 2521da177e4SLinus Torvalds .update_pmtu = ip6_rt_update_pmtu, 2536e157b6aSDavid S. Miller .redirect = rt6_do_redirect, 2549f8955ccSEric W. Biederman .local_out = __ip6_local_out, 255d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 25663fca65dSJulian Anastasov .confirm_neigh = ip6_confirm_neigh, 2571da177e4SLinus Torvalds }; 2581da177e4SLinus Torvalds 259ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst) 260ec831ea7SRoland Dreier { 261618f9bc7SSteffen Klassert unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); 262618f9bc7SSteffen Klassert 263618f9bc7SSteffen Klassert return mtu ? : dst->dev->mtu; 264ec831ea7SRoland Dreier } 265ec831ea7SRoland Dreier 2666700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk, 2676700c270SDavid S. Miller struct sk_buff *skb, u32 mtu) 26814e50e57SDavid S. Miller { 26914e50e57SDavid S. Miller } 27014e50e57SDavid S. Miller 2716700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk, 2726700c270SDavid S. Miller struct sk_buff *skb) 273b587ee3bSDavid S. Miller { 274b587ee3bSDavid S. Miller } 275b587ee3bSDavid S. Miller 27614e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = { 27714e50e57SDavid S. Miller .family = AF_INET6, 27814e50e57SDavid S. Miller .destroy = ip6_dst_destroy, 27914e50e57SDavid S. Miller .check = ip6_dst_check, 280ebb762f2SSteffen Klassert .mtu = ip6_blackhole_mtu, 281214f45c9SEric Dumazet .default_advmss = ip6_default_advmss, 28214e50e57SDavid S. Miller .update_pmtu = ip6_rt_blackhole_update_pmtu, 283b587ee3bSDavid S. Miller .redirect = ip6_rt_blackhole_redirect, 2840a1f5962SMartin KaFai Lau .cow_metrics = dst_cow_metrics_generic, 285d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 28614e50e57SDavid S. Miller }; 28714e50e57SDavid S. Miller 28862fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = { 28914edd87dSLi RongQing [RTAX_HOPLIMIT - 1] = 0, 29062fa8a84SDavid S. Miller }; 29162fa8a84SDavid S. Miller 292fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = { 2931da177e4SLinus Torvalds .dst = { 2941da177e4SLinus Torvalds .__refcnt = ATOMIC_INIT(1), 2951da177e4SLinus Torvalds .__use = 1, 2962c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 2971da177e4SLinus Torvalds .error = -ENETUNREACH, 2981da177e4SLinus Torvalds .input = ip6_pkt_discard, 2991da177e4SLinus Torvalds .output = ip6_pkt_discard_out, 3001da177e4SLinus Torvalds }, 3011da177e4SLinus Torvalds .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 3024f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 3031da177e4SLinus Torvalds .rt6i_metric = ~(u32) 0, 3041da177e4SLinus Torvalds .rt6i_ref = ATOMIC_INIT(1), 3051da177e4SLinus Torvalds }; 3061da177e4SLinus Torvalds 307101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES 308101367c2SThomas Graf 309fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = { 310101367c2SThomas Graf .dst = { 311101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 312101367c2SThomas Graf .__use = 1, 3132c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 314101367c2SThomas Graf .error = -EACCES, 3159ce8ade0SThomas Graf .input = ip6_pkt_prohibit, 3169ce8ade0SThomas Graf .output = ip6_pkt_prohibit_out, 317101367c2SThomas Graf }, 318101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 3194f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 320101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 321101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 322101367c2SThomas Graf }; 323101367c2SThomas Graf 324fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = { 325101367c2SThomas Graf .dst = { 326101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 327101367c2SThomas Graf .__use = 1, 3282c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 329101367c2SThomas Graf .error = -EINVAL, 330352e512cSHerbert Xu .input = dst_discard, 331ede2059dSEric W. Biederman .output = dst_discard_out, 332101367c2SThomas Graf }, 333101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 3344f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 335101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 336101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 337101367c2SThomas Graf }; 338101367c2SThomas Graf 339101367c2SThomas Graf #endif 340101367c2SThomas Graf 341ebfa45f0SMartin KaFai Lau static void rt6_info_init(struct rt6_info *rt) 342ebfa45f0SMartin KaFai Lau { 343ebfa45f0SMartin KaFai Lau struct dst_entry *dst = &rt->dst; 344ebfa45f0SMartin KaFai Lau 345ebfa45f0SMartin KaFai Lau memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst)); 346ebfa45f0SMartin KaFai Lau INIT_LIST_HEAD(&rt->rt6i_siblings); 347ebfa45f0SMartin KaFai Lau INIT_LIST_HEAD(&rt->rt6i_uncached); 348ebfa45f0SMartin KaFai Lau } 349ebfa45f0SMartin KaFai Lau 3501da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */ 351d52d3997SMartin KaFai Lau static struct rt6_info *__ip6_dst_alloc(struct net *net, 352957c665fSDavid S. Miller struct net_device *dev, 353ad706862SMartin KaFai Lau int flags) 3541da177e4SLinus Torvalds { 35597bab73fSDavid S. Miller struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev, 356b2a9c0edSWei Wang 1, DST_OBSOLETE_FORCE_CHK, flags); 357cf911662SDavid S. Miller 358ebfa45f0SMartin KaFai Lau if (rt) 359ebfa45f0SMartin KaFai Lau rt6_info_init(rt); 3608104891bSSteffen Klassert 361cf911662SDavid S. Miller return rt; 3621da177e4SLinus Torvalds } 3631da177e4SLinus Torvalds 3649ab179d8SDavid Ahern struct rt6_info *ip6_dst_alloc(struct net *net, 365d52d3997SMartin KaFai Lau struct net_device *dev, 366ad706862SMartin KaFai Lau int flags) 367d52d3997SMartin KaFai Lau { 368ad706862SMartin KaFai Lau struct rt6_info *rt = __ip6_dst_alloc(net, dev, flags); 369d52d3997SMartin KaFai Lau 370d52d3997SMartin KaFai Lau if (rt) { 371d52d3997SMartin KaFai Lau rt->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, GFP_ATOMIC); 372d52d3997SMartin KaFai Lau if (rt->rt6i_pcpu) { 373d52d3997SMartin KaFai Lau int cpu; 374d52d3997SMartin KaFai Lau 375d52d3997SMartin KaFai Lau for_each_possible_cpu(cpu) { 376d52d3997SMartin KaFai Lau struct rt6_info **p; 377d52d3997SMartin KaFai Lau 378d52d3997SMartin KaFai Lau p = per_cpu_ptr(rt->rt6i_pcpu, cpu); 379d52d3997SMartin KaFai Lau /* no one shares rt */ 380d52d3997SMartin KaFai Lau *p = NULL; 381d52d3997SMartin KaFai Lau } 382d52d3997SMartin KaFai Lau } else { 383587fea74SWei Wang dst_release_immediate(&rt->dst); 384d52d3997SMartin KaFai Lau return NULL; 385d52d3997SMartin KaFai Lau } 386d52d3997SMartin KaFai Lau } 387d52d3997SMartin KaFai Lau 388d52d3997SMartin KaFai Lau return rt; 389d52d3997SMartin KaFai Lau } 3909ab179d8SDavid Ahern EXPORT_SYMBOL(ip6_dst_alloc); 391d52d3997SMartin KaFai Lau 3921da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst) 3931da177e4SLinus Torvalds { 3941da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 395ecd98837SYOSHIFUJI Hideaki / 吉藤英明 struct dst_entry *from = dst->from; 3968d0b94afSMartin KaFai Lau struct inet6_dev *idev; 3971da177e4SLinus Torvalds 3988e2ec639SYan, Zheng dst_destroy_metrics_generic(dst); 399d52d3997SMartin KaFai Lau free_percpu(rt->rt6i_pcpu); 4008d0b94afSMartin KaFai Lau rt6_uncached_list_del(rt); 4018d0b94afSMartin KaFai Lau 4028d0b94afSMartin KaFai Lau idev = rt->rt6i_idev; 40338308473SDavid S. Miller if (idev) { 4041da177e4SLinus Torvalds rt->rt6i_idev = NULL; 4051da177e4SLinus Torvalds in6_dev_put(idev); 4061da177e4SLinus Torvalds } 4071716a961SGao feng 408ecd98837SYOSHIFUJI Hideaki / 吉藤英明 dst->from = NULL; 409ecd98837SYOSHIFUJI Hideaki / 吉藤英明 dst_release(from); 410b3419363SDavid S. Miller } 411b3419363SDavid S. Miller 4121da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 4131da177e4SLinus Torvalds int how) 4141da177e4SLinus Torvalds { 4151da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 4161da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 4175a3e55d6SDenis V. Lunev struct net_device *loopback_dev = 418c346dca1SYOSHIFUJI Hideaki dev_net(dev)->loopback_dev; 4191da177e4SLinus Torvalds 420e5645f51SWei Wang if (idev && idev->dev != loopback_dev) { 421e5645f51SWei Wang struct inet6_dev *loopback_idev = in6_dev_get(loopback_dev); 42238308473SDavid S. Miller if (loopback_idev) { 4231da177e4SLinus Torvalds rt->rt6i_idev = loopback_idev; 4241da177e4SLinus Torvalds in6_dev_put(idev); 4251da177e4SLinus Torvalds } 4261da177e4SLinus Torvalds } 42797cac082SDavid S. Miller } 4281da177e4SLinus Torvalds 4295973fb1eSMartin KaFai Lau static bool __rt6_check_expired(const struct rt6_info *rt) 4305973fb1eSMartin KaFai Lau { 4315973fb1eSMartin KaFai Lau if (rt->rt6i_flags & RTF_EXPIRES) 4325973fb1eSMartin KaFai Lau return time_after(jiffies, rt->dst.expires); 4335973fb1eSMartin KaFai Lau else 4345973fb1eSMartin KaFai Lau return false; 4355973fb1eSMartin KaFai Lau } 4365973fb1eSMartin KaFai Lau 437a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt) 4381da177e4SLinus Torvalds { 4391716a961SGao feng if (rt->rt6i_flags & RTF_EXPIRES) { 4401716a961SGao feng if (time_after(jiffies, rt->dst.expires)) 441a50feda5SEric Dumazet return true; 4421716a961SGao feng } else if (rt->dst.from) { 4433fd91fb3SLi RongQing return rt6_check_expired((struct rt6_info *) rt->dst.from); 4441716a961SGao feng } 445a50feda5SEric Dumazet return false; 4461da177e4SLinus Torvalds } 4471da177e4SLinus Torvalds 44851ebd318SNicolas Dichtel /* Multipath route selection: 44951ebd318SNicolas Dichtel * Hash based function using packet header and flowlabel. 45051ebd318SNicolas Dichtel * Adapted from fib_info_hashfn() 45151ebd318SNicolas Dichtel */ 45251ebd318SNicolas Dichtel static int rt6_info_hash_nhsfn(unsigned int candidate_count, 45351ebd318SNicolas Dichtel const struct flowi6 *fl6) 45451ebd318SNicolas Dichtel { 455644d0e65STom Herbert return get_hash_from_flowi6(fl6) % candidate_count; 45651ebd318SNicolas Dichtel } 45751ebd318SNicolas Dichtel 45851ebd318SNicolas Dichtel static struct rt6_info *rt6_multipath_select(struct rt6_info *match, 45952bd4c0cSNicolas Dichtel struct flowi6 *fl6, int oif, 46052bd4c0cSNicolas Dichtel int strict) 46151ebd318SNicolas Dichtel { 46251ebd318SNicolas Dichtel struct rt6_info *sibling, *next_sibling; 46351ebd318SNicolas Dichtel int route_choosen; 46451ebd318SNicolas Dichtel 46551ebd318SNicolas Dichtel route_choosen = rt6_info_hash_nhsfn(match->rt6i_nsiblings + 1, fl6); 46651ebd318SNicolas Dichtel /* Don't change the route, if route_choosen == 0 46751ebd318SNicolas Dichtel * (siblings does not include ourself) 46851ebd318SNicolas Dichtel */ 46951ebd318SNicolas Dichtel if (route_choosen) 47051ebd318SNicolas Dichtel list_for_each_entry_safe(sibling, next_sibling, 47151ebd318SNicolas Dichtel &match->rt6i_siblings, rt6i_siblings) { 47251ebd318SNicolas Dichtel route_choosen--; 47351ebd318SNicolas Dichtel if (route_choosen == 0) { 47452bd4c0cSNicolas Dichtel if (rt6_score_route(sibling, oif, strict) < 0) 47552bd4c0cSNicolas Dichtel break; 47651ebd318SNicolas Dichtel match = sibling; 47751ebd318SNicolas Dichtel break; 47851ebd318SNicolas Dichtel } 47951ebd318SNicolas Dichtel } 48051ebd318SNicolas Dichtel return match; 48151ebd318SNicolas Dichtel } 48251ebd318SNicolas Dichtel 4831da177e4SLinus Torvalds /* 484c71099acSThomas Graf * Route lookup. Any table->tb6_lock is implied. 4851da177e4SLinus Torvalds */ 4861da177e4SLinus Torvalds 4878ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net, 4888ed67789SDaniel Lezcano struct rt6_info *rt, 489b71d1d42SEric Dumazet const struct in6_addr *saddr, 4901da177e4SLinus Torvalds int oif, 491d420895eSYOSHIFUJI Hideaki int flags) 4921da177e4SLinus Torvalds { 4931da177e4SLinus Torvalds struct rt6_info *local = NULL; 4941da177e4SLinus Torvalds struct rt6_info *sprt; 4951da177e4SLinus Torvalds 496dd3abc4eSYOSHIFUJI Hideaki if (!oif && ipv6_addr_any(saddr)) 497dd3abc4eSYOSHIFUJI Hideaki goto out; 498dd3abc4eSYOSHIFUJI Hideaki 499d8d1f30bSChangli Gao for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) { 500d1918542SDavid S. Miller struct net_device *dev = sprt->dst.dev; 501dd3abc4eSYOSHIFUJI Hideaki 502dd3abc4eSYOSHIFUJI Hideaki if (oif) { 5031da177e4SLinus Torvalds if (dev->ifindex == oif) 5041da177e4SLinus Torvalds return sprt; 5051da177e4SLinus Torvalds if (dev->flags & IFF_LOOPBACK) { 50638308473SDavid S. Miller if (!sprt->rt6i_idev || 5071da177e4SLinus Torvalds sprt->rt6i_idev->dev->ifindex != oif) { 50817fb0b2bSDavid Ahern if (flags & RT6_LOOKUP_F_IFACE) 5091da177e4SLinus Torvalds continue; 51017fb0b2bSDavid Ahern if (local && 51117fb0b2bSDavid Ahern local->rt6i_idev->dev->ifindex == oif) 5121da177e4SLinus Torvalds continue; 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds local = sprt; 5151da177e4SLinus Torvalds } 516dd3abc4eSYOSHIFUJI Hideaki } else { 517dd3abc4eSYOSHIFUJI Hideaki if (ipv6_chk_addr(net, saddr, dev, 518dd3abc4eSYOSHIFUJI Hideaki flags & RT6_LOOKUP_F_IFACE)) 519dd3abc4eSYOSHIFUJI Hideaki return sprt; 520dd3abc4eSYOSHIFUJI Hideaki } 5211da177e4SLinus Torvalds } 5221da177e4SLinus Torvalds 523dd3abc4eSYOSHIFUJI Hideaki if (oif) { 5241da177e4SLinus Torvalds if (local) 5251da177e4SLinus Torvalds return local; 5261da177e4SLinus Torvalds 527d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE) 5288ed67789SDaniel Lezcano return net->ipv6.ip6_null_entry; 5291da177e4SLinus Torvalds } 530dd3abc4eSYOSHIFUJI Hideaki out: 5311da177e4SLinus Torvalds return rt; 5321da177e4SLinus Torvalds } 5331da177e4SLinus Torvalds 53427097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 535c2f17e82SHannes Frederic Sowa struct __rt6_probe_work { 536c2f17e82SHannes Frederic Sowa struct work_struct work; 537c2f17e82SHannes Frederic Sowa struct in6_addr target; 538c2f17e82SHannes Frederic Sowa struct net_device *dev; 539c2f17e82SHannes Frederic Sowa }; 540c2f17e82SHannes Frederic Sowa 541c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w) 542c2f17e82SHannes Frederic Sowa { 543c2f17e82SHannes Frederic Sowa struct in6_addr mcaddr; 544c2f17e82SHannes Frederic Sowa struct __rt6_probe_work *work = 545c2f17e82SHannes Frederic Sowa container_of(w, struct __rt6_probe_work, work); 546c2f17e82SHannes Frederic Sowa 547c2f17e82SHannes Frederic Sowa addrconf_addr_solict_mult(&work->target, &mcaddr); 548adc176c5SErik Nordmark ndisc_send_ns(work->dev, &work->target, &mcaddr, NULL, 0); 549c2f17e82SHannes Frederic Sowa dev_put(work->dev); 550662f5533SMichael Büsch kfree(work); 551c2f17e82SHannes Frederic Sowa } 552c2f17e82SHannes Frederic Sowa 55327097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt) 55427097255SYOSHIFUJI Hideaki { 555990edb42SMartin KaFai Lau struct __rt6_probe_work *work; 556f2c31e32SEric Dumazet struct neighbour *neigh; 55727097255SYOSHIFUJI Hideaki /* 55827097255SYOSHIFUJI Hideaki * Okay, this does not seem to be appropriate 55927097255SYOSHIFUJI Hideaki * for now, however, we need to check if it 56027097255SYOSHIFUJI Hideaki * is really so; aka Router Reachability Probing. 56127097255SYOSHIFUJI Hideaki * 56227097255SYOSHIFUJI Hideaki * Router Reachability Probe MUST be rate-limited 56327097255SYOSHIFUJI Hideaki * to no more than one per minute. 56427097255SYOSHIFUJI Hideaki */ 5652152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (!rt || !(rt->rt6i_flags & RTF_GATEWAY)) 566fdd6681dSAmerigo Wang return; 5672152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 5682152caeaSYOSHIFUJI Hideaki / 吉藤英明 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 5692152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 5708d6c31bfSMartin KaFai Lau if (neigh->nud_state & NUD_VALID) 5718d6c31bfSMartin KaFai Lau goto out; 5728d6c31bfSMartin KaFai Lau 573990edb42SMartin KaFai Lau work = NULL; 5742152caeaSYOSHIFUJI Hideaki / 吉藤英明 write_lock(&neigh->lock); 575990edb42SMartin KaFai Lau if (!(neigh->nud_state & NUD_VALID) && 576990edb42SMartin KaFai Lau time_after(jiffies, 577990edb42SMartin KaFai Lau neigh->updated + 578990edb42SMartin KaFai Lau rt->rt6i_idev->cnf.rtr_probe_interval)) { 579c2f17e82SHannes Frederic Sowa work = kmalloc(sizeof(*work), GFP_ATOMIC); 580990edb42SMartin KaFai Lau if (work) 5817e980569SJiri Benc __neigh_set_probe_once(neigh); 582990edb42SMartin KaFai Lau } 583c2f17e82SHannes Frederic Sowa write_unlock(&neigh->lock); 584990edb42SMartin KaFai Lau } else { 585990edb42SMartin KaFai Lau work = kmalloc(sizeof(*work), GFP_ATOMIC); 586990edb42SMartin KaFai Lau } 587c2f17e82SHannes Frederic Sowa 588c2f17e82SHannes Frederic Sowa if (work) { 589c2f17e82SHannes Frederic Sowa INIT_WORK(&work->work, rt6_probe_deferred); 590c2f17e82SHannes Frederic Sowa work->target = rt->rt6i_gateway; 591c2f17e82SHannes Frederic Sowa dev_hold(rt->dst.dev); 592c2f17e82SHannes Frederic Sowa work->dev = rt->dst.dev; 593c2f17e82SHannes Frederic Sowa schedule_work(&work->work); 594c2f17e82SHannes Frederic Sowa } 595990edb42SMartin KaFai Lau 5968d6c31bfSMartin KaFai Lau out: 5972152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 598f2c31e32SEric Dumazet } 59927097255SYOSHIFUJI Hideaki #else 60027097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt) 60127097255SYOSHIFUJI Hideaki { 60227097255SYOSHIFUJI Hideaki } 60327097255SYOSHIFUJI Hideaki #endif 60427097255SYOSHIFUJI Hideaki 6051da177e4SLinus Torvalds /* 606554cfb7eSYOSHIFUJI Hideaki * Default Router Selection (RFC 2461 6.3.6) 6071da177e4SLinus Torvalds */ 608b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif) 6091da177e4SLinus Torvalds { 610d1918542SDavid S. Miller struct net_device *dev = rt->dst.dev; 611161980f4SDavid S. Miller if (!oif || dev->ifindex == oif) 612554cfb7eSYOSHIFUJI Hideaki return 2; 613161980f4SDavid S. Miller if ((dev->flags & IFF_LOOPBACK) && 614161980f4SDavid S. Miller rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif) 615161980f4SDavid S. Miller return 1; 616554cfb7eSYOSHIFUJI Hideaki return 0; 6171da177e4SLinus Torvalds } 6181da177e4SLinus Torvalds 619afc154e9SHannes Frederic Sowa static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt) 6201da177e4SLinus Torvalds { 621f2c31e32SEric Dumazet struct neighbour *neigh; 622afc154e9SHannes Frederic Sowa enum rt6_nud_state ret = RT6_NUD_FAIL_HARD; 623f2c31e32SEric Dumazet 6244d0c5911SYOSHIFUJI Hideaki if (rt->rt6i_flags & RTF_NONEXTHOP || 6254d0c5911SYOSHIFUJI Hideaki !(rt->rt6i_flags & RTF_GATEWAY)) 626afc154e9SHannes Frederic Sowa return RT6_NUD_SUCCEED; 627145a3621SYOSHIFUJI Hideaki / 吉藤英明 628145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 629145a3621SYOSHIFUJI Hideaki / 吉藤英明 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 630145a3621SYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 631145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_lock(&neigh->lock); 632554cfb7eSYOSHIFUJI Hideaki if (neigh->nud_state & NUD_VALID) 633afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 634398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 635a5a81f0bSPaul Marks else if (!(neigh->nud_state & NUD_FAILED)) 636afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 6377e980569SJiri Benc else 6387e980569SJiri Benc ret = RT6_NUD_FAIL_PROBE; 639398bcbebSYOSHIFUJI Hideaki #endif 640145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_unlock(&neigh->lock); 641afc154e9SHannes Frederic Sowa } else { 642afc154e9SHannes Frederic Sowa ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ? 6437e980569SJiri Benc RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR; 644a5a81f0bSPaul Marks } 645145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 646145a3621SYOSHIFUJI Hideaki / 吉藤英明 647a5a81f0bSPaul Marks return ret; 6481da177e4SLinus Torvalds } 6491da177e4SLinus Torvalds 650554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif, 651554cfb7eSYOSHIFUJI Hideaki int strict) 652554cfb7eSYOSHIFUJI Hideaki { 653a5a81f0bSPaul Marks int m; 6544d0c5911SYOSHIFUJI Hideaki 6554d0c5911SYOSHIFUJI Hideaki m = rt6_check_dev(rt, oif); 65677d16f45SYOSHIFUJI Hideaki if (!m && (strict & RT6_LOOKUP_F_IFACE)) 657afc154e9SHannes Frederic Sowa return RT6_NUD_FAIL_HARD; 658ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 659ebacaaa0SYOSHIFUJI Hideaki m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2; 660ebacaaa0SYOSHIFUJI Hideaki #endif 661afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) { 662afc154e9SHannes Frederic Sowa int n = rt6_check_neigh(rt); 663afc154e9SHannes Frederic Sowa if (n < 0) 664afc154e9SHannes Frederic Sowa return n; 665afc154e9SHannes Frederic Sowa } 666554cfb7eSYOSHIFUJI Hideaki return m; 667554cfb7eSYOSHIFUJI Hideaki } 668554cfb7eSYOSHIFUJI Hideaki 669f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict, 670afc154e9SHannes Frederic Sowa int *mpri, struct rt6_info *match, 671afc154e9SHannes Frederic Sowa bool *do_rr) 672554cfb7eSYOSHIFUJI Hideaki { 673554cfb7eSYOSHIFUJI Hideaki int m; 674afc154e9SHannes Frederic Sowa bool match_do_rr = false; 67535103d11SAndy Gospodarek struct inet6_dev *idev = rt->rt6i_idev; 67635103d11SAndy Gospodarek struct net_device *dev = rt->dst.dev; 67735103d11SAndy Gospodarek 67835103d11SAndy Gospodarek if (dev && !netif_carrier_ok(dev) && 679d5d32e4bSDavid Ahern idev->cnf.ignore_routes_with_linkdown && 680d5d32e4bSDavid Ahern !(strict & RT6_LOOKUP_F_IGNORE_LINKSTATE)) 68135103d11SAndy Gospodarek goto out; 682554cfb7eSYOSHIFUJI Hideaki 683554cfb7eSYOSHIFUJI Hideaki if (rt6_check_expired(rt)) 684f11e6659SDavid S. Miller goto out; 685554cfb7eSYOSHIFUJI Hideaki 686554cfb7eSYOSHIFUJI Hideaki m = rt6_score_route(rt, oif, strict); 6877e980569SJiri Benc if (m == RT6_NUD_FAIL_DO_RR) { 688afc154e9SHannes Frederic Sowa match_do_rr = true; 689afc154e9SHannes Frederic Sowa m = 0; /* lowest valid score */ 6907e980569SJiri Benc } else if (m == RT6_NUD_FAIL_HARD) { 691f11e6659SDavid S. Miller goto out; 6921da177e4SLinus Torvalds } 693f11e6659SDavid S. Miller 694afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) 695afc154e9SHannes Frederic Sowa rt6_probe(rt); 696afc154e9SHannes Frederic Sowa 6977e980569SJiri Benc /* note that m can be RT6_NUD_FAIL_PROBE at this point */ 698afc154e9SHannes Frederic Sowa if (m > *mpri) { 699afc154e9SHannes Frederic Sowa *do_rr = match_do_rr; 700afc154e9SHannes Frederic Sowa *mpri = m; 701afc154e9SHannes Frederic Sowa match = rt; 702afc154e9SHannes Frederic Sowa } 703f11e6659SDavid S. Miller out: 704f11e6659SDavid S. Miller return match; 7051da177e4SLinus Torvalds } 7061da177e4SLinus Torvalds 707f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn, 708f11e6659SDavid S. Miller struct rt6_info *rr_head, 709afc154e9SHannes Frederic Sowa u32 metric, int oif, int strict, 710afc154e9SHannes Frederic Sowa bool *do_rr) 711f11e6659SDavid S. Miller { 7129fbdcfafSSteffen Klassert struct rt6_info *rt, *match, *cont; 713f11e6659SDavid S. Miller int mpri = -1; 714f11e6659SDavid S. Miller 715f11e6659SDavid S. Miller match = NULL; 7169fbdcfafSSteffen Klassert cont = NULL; 7179fbdcfafSSteffen Klassert for (rt = rr_head; rt; rt = rt->dst.rt6_next) { 7189fbdcfafSSteffen Klassert if (rt->rt6i_metric != metric) { 7199fbdcfafSSteffen Klassert cont = rt; 7209fbdcfafSSteffen Klassert break; 7219fbdcfafSSteffen Klassert } 7229fbdcfafSSteffen Klassert 723afc154e9SHannes Frederic Sowa match = find_match(rt, oif, strict, &mpri, match, do_rr); 7249fbdcfafSSteffen Klassert } 7259fbdcfafSSteffen Klassert 7269fbdcfafSSteffen Klassert for (rt = fn->leaf; rt && rt != rr_head; rt = rt->dst.rt6_next) { 7279fbdcfafSSteffen Klassert if (rt->rt6i_metric != metric) { 7289fbdcfafSSteffen Klassert cont = rt; 7299fbdcfafSSteffen Klassert break; 7309fbdcfafSSteffen Klassert } 7319fbdcfafSSteffen Klassert 7329fbdcfafSSteffen Klassert match = find_match(rt, oif, strict, &mpri, match, do_rr); 7339fbdcfafSSteffen Klassert } 7349fbdcfafSSteffen Klassert 7359fbdcfafSSteffen Klassert if (match || !cont) 7369fbdcfafSSteffen Klassert return match; 7379fbdcfafSSteffen Klassert 7389fbdcfafSSteffen Klassert for (rt = cont; rt; rt = rt->dst.rt6_next) 739afc154e9SHannes Frederic Sowa match = find_match(rt, oif, strict, &mpri, match, do_rr); 740f11e6659SDavid S. Miller 741f11e6659SDavid S. Miller return match; 742f11e6659SDavid S. Miller } 743f11e6659SDavid S. Miller 744f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict) 745f11e6659SDavid S. Miller { 746f11e6659SDavid S. Miller struct rt6_info *match, *rt0; 7478ed67789SDaniel Lezcano struct net *net; 748afc154e9SHannes Frederic Sowa bool do_rr = false; 749f11e6659SDavid S. Miller 750f11e6659SDavid S. Miller rt0 = fn->rr_ptr; 751f11e6659SDavid S. Miller if (!rt0) 752f11e6659SDavid S. Miller fn->rr_ptr = rt0 = fn->leaf; 753f11e6659SDavid S. Miller 754afc154e9SHannes Frederic Sowa match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict, 755afc154e9SHannes Frederic Sowa &do_rr); 756f11e6659SDavid S. Miller 757afc154e9SHannes Frederic Sowa if (do_rr) { 758d8d1f30bSChangli Gao struct rt6_info *next = rt0->dst.rt6_next; 759f11e6659SDavid S. Miller 760554cfb7eSYOSHIFUJI Hideaki /* no entries matched; do round-robin */ 761f11e6659SDavid S. Miller if (!next || next->rt6i_metric != rt0->rt6i_metric) 762f11e6659SDavid S. Miller next = fn->leaf; 763f11e6659SDavid S. Miller 764f11e6659SDavid S. Miller if (next != rt0) 765f11e6659SDavid S. Miller fn->rr_ptr = next; 766554cfb7eSYOSHIFUJI Hideaki } 767554cfb7eSYOSHIFUJI Hideaki 768d1918542SDavid S. Miller net = dev_net(rt0->dst.dev); 769a02cec21SEric Dumazet return match ? match : net->ipv6.ip6_null_entry; 7701da177e4SLinus Torvalds } 7711da177e4SLinus Torvalds 7728b9df265SMartin KaFai Lau static bool rt6_is_gw_or_nonexthop(const struct rt6_info *rt) 7738b9df265SMartin KaFai Lau { 7748b9df265SMartin KaFai Lau return (rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY)); 7758b9df265SMartin KaFai Lau } 7768b9df265SMartin KaFai Lau 77770ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 77870ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 779b71d1d42SEric Dumazet const struct in6_addr *gwaddr) 78070ceb4f5SYOSHIFUJI Hideaki { 781c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 78270ceb4f5SYOSHIFUJI Hideaki struct route_info *rinfo = (struct route_info *) opt; 78370ceb4f5SYOSHIFUJI Hideaki struct in6_addr prefix_buf, *prefix; 78470ceb4f5SYOSHIFUJI Hideaki unsigned int pref; 7854bed72e4SYOSHIFUJI Hideaki unsigned long lifetime; 78670ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt; 78770ceb4f5SYOSHIFUJI Hideaki 78870ceb4f5SYOSHIFUJI Hideaki if (len < sizeof(struct route_info)) { 78970ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 79070ceb4f5SYOSHIFUJI Hideaki } 79170ceb4f5SYOSHIFUJI Hideaki 79270ceb4f5SYOSHIFUJI Hideaki /* Sanity check for prefix_len and length */ 79370ceb4f5SYOSHIFUJI Hideaki if (rinfo->length > 3) { 79470ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 79570ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 128) { 79670ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 79770ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 64) { 79870ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 2) { 79970ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 80070ceb4f5SYOSHIFUJI Hideaki } 80170ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 0) { 80270ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 1) { 80370ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 80470ceb4f5SYOSHIFUJI Hideaki } 80570ceb4f5SYOSHIFUJI Hideaki } 80670ceb4f5SYOSHIFUJI Hideaki 80770ceb4f5SYOSHIFUJI Hideaki pref = rinfo->route_pref; 80870ceb4f5SYOSHIFUJI Hideaki if (pref == ICMPV6_ROUTER_PREF_INVALID) 8093933fc95SJens Rosenboom return -EINVAL; 81070ceb4f5SYOSHIFUJI Hideaki 8114bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); 81270ceb4f5SYOSHIFUJI Hideaki 81370ceb4f5SYOSHIFUJI Hideaki if (rinfo->length == 3) 81470ceb4f5SYOSHIFUJI Hideaki prefix = (struct in6_addr *)rinfo->prefix; 81570ceb4f5SYOSHIFUJI Hideaki else { 81670ceb4f5SYOSHIFUJI Hideaki /* this function is safe */ 81770ceb4f5SYOSHIFUJI Hideaki ipv6_addr_prefix(&prefix_buf, 81870ceb4f5SYOSHIFUJI Hideaki (struct in6_addr *)rinfo->prefix, 81970ceb4f5SYOSHIFUJI Hideaki rinfo->prefix_len); 82070ceb4f5SYOSHIFUJI Hideaki prefix = &prefix_buf; 82170ceb4f5SYOSHIFUJI Hideaki } 82270ceb4f5SYOSHIFUJI Hideaki 823f104a567SDuan Jiong if (rinfo->prefix_len == 0) 824f104a567SDuan Jiong rt = rt6_get_dflt_router(gwaddr, dev); 825f104a567SDuan Jiong else 826f104a567SDuan Jiong rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, 827830218c1SDavid Ahern gwaddr, dev); 82870ceb4f5SYOSHIFUJI Hideaki 82970ceb4f5SYOSHIFUJI Hideaki if (rt && !lifetime) { 830e0a1ad73SThomas Graf ip6_del_rt(rt); 83170ceb4f5SYOSHIFUJI Hideaki rt = NULL; 83270ceb4f5SYOSHIFUJI Hideaki } 83370ceb4f5SYOSHIFUJI Hideaki 83470ceb4f5SYOSHIFUJI Hideaki if (!rt && lifetime) 835830218c1SDavid Ahern rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, 836830218c1SDavid Ahern dev, pref); 83770ceb4f5SYOSHIFUJI Hideaki else if (rt) 83870ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags = RTF_ROUTEINFO | 83970ceb4f5SYOSHIFUJI Hideaki (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 84070ceb4f5SYOSHIFUJI Hideaki 84170ceb4f5SYOSHIFUJI Hideaki if (rt) { 8421716a961SGao feng if (!addrconf_finite_timeout(lifetime)) 8431716a961SGao feng rt6_clean_expires(rt); 8441716a961SGao feng else 8451716a961SGao feng rt6_set_expires(rt, jiffies + HZ * lifetime); 8461716a961SGao feng 84794e187c0SAmerigo Wang ip6_rt_put(rt); 84870ceb4f5SYOSHIFUJI Hideaki } 84970ceb4f5SYOSHIFUJI Hideaki return 0; 85070ceb4f5SYOSHIFUJI Hideaki } 85170ceb4f5SYOSHIFUJI Hideaki #endif 85270ceb4f5SYOSHIFUJI Hideaki 853a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn, 854a3c00e46SMartin KaFai Lau struct in6_addr *saddr) 855a3c00e46SMartin KaFai Lau { 856a3c00e46SMartin KaFai Lau struct fib6_node *pn; 857a3c00e46SMartin KaFai Lau while (1) { 858a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_TL_ROOT) 859a3c00e46SMartin KaFai Lau return NULL; 860a3c00e46SMartin KaFai Lau pn = fn->parent; 861a3c00e46SMartin KaFai Lau if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) 862a3c00e46SMartin KaFai Lau fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); 863a3c00e46SMartin KaFai Lau else 864a3c00e46SMartin KaFai Lau fn = pn; 865a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_RTINFO) 866a3c00e46SMartin KaFai Lau return fn; 867a3c00e46SMartin KaFai Lau } 868a3c00e46SMartin KaFai Lau } 869c71099acSThomas Graf 8708ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net, 8718ed67789SDaniel Lezcano struct fib6_table *table, 8724c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 8731da177e4SLinus Torvalds { 8741da177e4SLinus Torvalds struct fib6_node *fn; 8751da177e4SLinus Torvalds struct rt6_info *rt; 8761da177e4SLinus Torvalds 877c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 8784c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 879c71099acSThomas Graf restart: 880c71099acSThomas Graf rt = fn->leaf; 8814c9483b2SDavid S. Miller rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags); 88251ebd318SNicolas Dichtel if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0) 88352bd4c0cSNicolas Dichtel rt = rt6_multipath_select(rt, fl6, fl6->flowi6_oif, flags); 884a3c00e46SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 885a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 886a3c00e46SMartin KaFai Lau if (fn) 887a3c00e46SMartin KaFai Lau goto restart; 888a3c00e46SMartin KaFai Lau } 889d8d1f30bSChangli Gao dst_use(&rt->dst, jiffies); 890c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 891b811580dSDavid Ahern 892b811580dSDavid Ahern trace_fib6_table_lookup(net, rt, table->tb6_id, fl6); 893b811580dSDavid Ahern 8941da177e4SLinus Torvalds return rt; 895c71099acSThomas Graf 896c71099acSThomas Graf } 897c71099acSThomas Graf 898ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6, 899ea6e574eSFlorian Westphal int flags) 900ea6e574eSFlorian Westphal { 901ea6e574eSFlorian Westphal return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup); 902ea6e574eSFlorian Westphal } 903ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup); 904ea6e574eSFlorian Westphal 9059acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, 9069acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *saddr, int oif, int strict) 907c71099acSThomas Graf { 9084c9483b2SDavid S. Miller struct flowi6 fl6 = { 9094c9483b2SDavid S. Miller .flowi6_oif = oif, 9104c9483b2SDavid S. Miller .daddr = *daddr, 911c71099acSThomas Graf }; 912c71099acSThomas Graf struct dst_entry *dst; 91377d16f45SYOSHIFUJI Hideaki int flags = strict ? RT6_LOOKUP_F_IFACE : 0; 914c71099acSThomas Graf 915adaa70bbSThomas Graf if (saddr) { 9164c9483b2SDavid S. Miller memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 917adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 918adaa70bbSThomas Graf } 919adaa70bbSThomas Graf 9204c9483b2SDavid S. Miller dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup); 921c71099acSThomas Graf if (dst->error == 0) 922c71099acSThomas Graf return (struct rt6_info *) dst; 923c71099acSThomas Graf 924c71099acSThomas Graf dst_release(dst); 925c71099acSThomas Graf 9261da177e4SLinus Torvalds return NULL; 9271da177e4SLinus Torvalds } 9287159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup); 9297159039aSYOSHIFUJI Hideaki 930c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock. 9311cfb71eeSWei Wang * It takes new route entry, the addition fails by any reason the 9321cfb71eeSWei Wang * route is released. 9331cfb71eeSWei Wang * Caller must hold dst before calling it. 9341da177e4SLinus Torvalds */ 9351da177e4SLinus Torvalds 936e5fd387aSMichal Kubeček static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info, 937333c4301SDavid Ahern struct mx6_config *mxc, 938333c4301SDavid Ahern struct netlink_ext_ack *extack) 9391da177e4SLinus Torvalds { 9401da177e4SLinus Torvalds int err; 941c71099acSThomas Graf struct fib6_table *table; 9421da177e4SLinus Torvalds 943c71099acSThomas Graf table = rt->rt6i_table; 944c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 945333c4301SDavid Ahern err = fib6_add(&table->tb6_root, rt, info, mxc, extack); 946c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 9471da177e4SLinus Torvalds 9481da177e4SLinus Torvalds return err; 9491da177e4SLinus Torvalds } 9501da177e4SLinus Torvalds 95140e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt) 95240e22e8fSThomas Graf { 953e715b6d3SFlorian Westphal struct nl_info info = { .nl_net = dev_net(rt->dst.dev), }; 954e715b6d3SFlorian Westphal struct mx6_config mxc = { .mx = NULL, }; 955e715b6d3SFlorian Westphal 9561cfb71eeSWei Wang /* Hold dst to account for the reference from the fib6 tree */ 9571cfb71eeSWei Wang dst_hold(&rt->dst); 958333c4301SDavid Ahern return __ip6_ins_rt(rt, &info, &mxc, NULL); 95940e22e8fSThomas Graf } 96040e22e8fSThomas Graf 9618b9df265SMartin KaFai Lau static struct rt6_info *ip6_rt_cache_alloc(struct rt6_info *ort, 96221efcfa0SEric Dumazet const struct in6_addr *daddr, 963b71d1d42SEric Dumazet const struct in6_addr *saddr) 9641da177e4SLinus Torvalds { 9651da177e4SLinus Torvalds struct rt6_info *rt; 9661da177e4SLinus Torvalds 9671da177e4SLinus Torvalds /* 9681da177e4SLinus Torvalds * Clone the route. 9691da177e4SLinus Torvalds */ 9701da177e4SLinus Torvalds 971d52d3997SMartin KaFai Lau if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)) 97283a09abdSMartin KaFai Lau ort = (struct rt6_info *)ort->dst.from; 9731da177e4SLinus Torvalds 974ad706862SMartin KaFai Lau rt = __ip6_dst_alloc(dev_net(ort->dst.dev), ort->dst.dev, 0); 97583a09abdSMartin KaFai Lau 97683a09abdSMartin KaFai Lau if (!rt) 97783a09abdSMartin KaFai Lau return NULL; 97883a09abdSMartin KaFai Lau 97983a09abdSMartin KaFai Lau ip6_rt_copy_init(rt, ort); 9808b9df265SMartin KaFai Lau rt->rt6i_flags |= RTF_CACHE; 98183a09abdSMartin KaFai Lau rt->rt6i_metric = 0; 98283a09abdSMartin KaFai Lau rt->dst.flags |= DST_HOST; 98383a09abdSMartin KaFai Lau rt->rt6i_dst.addr = *daddr; 98483a09abdSMartin KaFai Lau rt->rt6i_dst.plen = 128; 9858b9df265SMartin KaFai Lau 9868b9df265SMartin KaFai Lau if (!rt6_is_gw_or_nonexthop(ort)) { 987bb3c3686SDavid S. Miller if (ort->rt6i_dst.plen != 128 && 98821efcfa0SEric Dumazet ipv6_addr_equal(&ort->rt6i_dst.addr, daddr)) 98958c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 9901da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 9911da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 9924e3fd7a0SAlexey Dobriyan rt->rt6i_src.addr = *saddr; 9931da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 9941da177e4SLinus Torvalds } 9951da177e4SLinus Torvalds #endif 99695a9a5baSYOSHIFUJI Hideaki } 99795a9a5baSYOSHIFUJI Hideaki 998299d9939SYOSHIFUJI Hideaki return rt; 999299d9939SYOSHIFUJI Hideaki } 1000299d9939SYOSHIFUJI Hideaki 1001d52d3997SMartin KaFai Lau static struct rt6_info *ip6_rt_pcpu_alloc(struct rt6_info *rt) 1002d52d3997SMartin KaFai Lau { 1003d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1004d52d3997SMartin KaFai Lau 1005d52d3997SMartin KaFai Lau pcpu_rt = __ip6_dst_alloc(dev_net(rt->dst.dev), 1006ad706862SMartin KaFai Lau rt->dst.dev, rt->dst.flags); 1007d52d3997SMartin KaFai Lau 1008d52d3997SMartin KaFai Lau if (!pcpu_rt) 1009d52d3997SMartin KaFai Lau return NULL; 1010d52d3997SMartin KaFai Lau ip6_rt_copy_init(pcpu_rt, rt); 1011d52d3997SMartin KaFai Lau pcpu_rt->rt6i_protocol = rt->rt6i_protocol; 1012d52d3997SMartin KaFai Lau pcpu_rt->rt6i_flags |= RTF_PCPU; 1013d52d3997SMartin KaFai Lau return pcpu_rt; 1014d52d3997SMartin KaFai Lau } 1015d52d3997SMartin KaFai Lau 1016d52d3997SMartin KaFai Lau /* It should be called with read_lock_bh(&tb6_lock) acquired */ 1017d52d3997SMartin KaFai Lau static struct rt6_info *rt6_get_pcpu_route(struct rt6_info *rt) 1018d52d3997SMartin KaFai Lau { 1019a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, **p; 1020d52d3997SMartin KaFai Lau 1021d52d3997SMartin KaFai Lau p = this_cpu_ptr(rt->rt6i_pcpu); 1022d52d3997SMartin KaFai Lau pcpu_rt = *p; 1023d52d3997SMartin KaFai Lau 1024a73e4195SMartin KaFai Lau if (pcpu_rt) { 1025a73e4195SMartin KaFai Lau dst_hold(&pcpu_rt->dst); 1026a73e4195SMartin KaFai Lau rt6_dst_from_metrics_check(pcpu_rt); 1027a73e4195SMartin KaFai Lau } 1028a73e4195SMartin KaFai Lau return pcpu_rt; 1029a73e4195SMartin KaFai Lau } 1030a73e4195SMartin KaFai Lau 1031a73e4195SMartin KaFai Lau static struct rt6_info *rt6_make_pcpu_route(struct rt6_info *rt) 1032a73e4195SMartin KaFai Lau { 10339c7370a1SMartin KaFai Lau struct fib6_table *table = rt->rt6i_table; 1034a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, *prev, **p; 1035d52d3997SMartin KaFai Lau 1036d52d3997SMartin KaFai Lau pcpu_rt = ip6_rt_pcpu_alloc(rt); 1037d52d3997SMartin KaFai Lau if (!pcpu_rt) { 1038d52d3997SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 1039d52d3997SMartin KaFai Lau 10409c7370a1SMartin KaFai Lau dst_hold(&net->ipv6.ip6_null_entry->dst); 10419c7370a1SMartin KaFai Lau return net->ipv6.ip6_null_entry; 1042d52d3997SMartin KaFai Lau } 1043d52d3997SMartin KaFai Lau 10449c7370a1SMartin KaFai Lau read_lock_bh(&table->tb6_lock); 10459c7370a1SMartin KaFai Lau if (rt->rt6i_pcpu) { 1046a73e4195SMartin KaFai Lau p = this_cpu_ptr(rt->rt6i_pcpu); 1047d52d3997SMartin KaFai Lau prev = cmpxchg(p, NULL, pcpu_rt); 1048d52d3997SMartin KaFai Lau if (prev) { 1049d52d3997SMartin KaFai Lau /* If someone did it before us, return prev instead */ 1050587fea74SWei Wang dst_release_immediate(&pcpu_rt->dst); 1051d52d3997SMartin KaFai Lau pcpu_rt = prev; 1052d52d3997SMartin KaFai Lau } 10539c7370a1SMartin KaFai Lau } else { 10549c7370a1SMartin KaFai Lau /* rt has been removed from the fib6 tree 10559c7370a1SMartin KaFai Lau * before we have a chance to acquire the read_lock. 10569c7370a1SMartin KaFai Lau * In this case, don't brother to create a pcpu rt 10579c7370a1SMartin KaFai Lau * since rt is going away anyway. The next 10589c7370a1SMartin KaFai Lau * dst_check() will trigger a re-lookup. 10599c7370a1SMartin KaFai Lau */ 1060587fea74SWei Wang dst_release_immediate(&pcpu_rt->dst); 10619c7370a1SMartin KaFai Lau pcpu_rt = rt; 10629c7370a1SMartin KaFai Lau } 1063d52d3997SMartin KaFai Lau dst_hold(&pcpu_rt->dst); 1064d52d3997SMartin KaFai Lau rt6_dst_from_metrics_check(pcpu_rt); 10659c7370a1SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 1066d52d3997SMartin KaFai Lau return pcpu_rt; 1067d52d3997SMartin KaFai Lau } 1068d52d3997SMartin KaFai Lau 10699ff74384SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, 10709ff74384SDavid Ahern int oif, struct flowi6 *fl6, int flags) 10711da177e4SLinus Torvalds { 1072367efcb9SMartin KaFai Lau struct fib6_node *fn, *saved_fn; 107345e4fd26SMartin KaFai Lau struct rt6_info *rt; 1074c71099acSThomas Graf int strict = 0; 10751da177e4SLinus Torvalds 107677d16f45SYOSHIFUJI Hideaki strict |= flags & RT6_LOOKUP_F_IFACE; 1077d5d32e4bSDavid Ahern strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE; 1078367efcb9SMartin KaFai Lau if (net->ipv6.devconf_all->forwarding == 0) 1079367efcb9SMartin KaFai Lau strict |= RT6_LOOKUP_F_REACHABLE; 10801da177e4SLinus Torvalds 1081c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 10821da177e4SLinus Torvalds 10834c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1084367efcb9SMartin KaFai Lau saved_fn = fn; 10851da177e4SLinus Torvalds 1086ca254490SDavid Ahern if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) 1087ca254490SDavid Ahern oif = 0; 1088ca254490SDavid Ahern 1089a3c00e46SMartin KaFai Lau redo_rt6_select: 1090367efcb9SMartin KaFai Lau rt = rt6_select(fn, oif, strict); 109152bd4c0cSNicolas Dichtel if (rt->rt6i_nsiblings) 1092367efcb9SMartin KaFai Lau rt = rt6_multipath_select(rt, fl6, oif, strict); 1093a3c00e46SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 1094a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1095a3c00e46SMartin KaFai Lau if (fn) 1096a3c00e46SMartin KaFai Lau goto redo_rt6_select; 1097367efcb9SMartin KaFai Lau else if (strict & RT6_LOOKUP_F_REACHABLE) { 1098367efcb9SMartin KaFai Lau /* also consider unreachable route */ 1099367efcb9SMartin KaFai Lau strict &= ~RT6_LOOKUP_F_REACHABLE; 1100367efcb9SMartin KaFai Lau fn = saved_fn; 1101367efcb9SMartin KaFai Lau goto redo_rt6_select; 1102367efcb9SMartin KaFai Lau } 1103a3c00e46SMartin KaFai Lau } 1104a3c00e46SMartin KaFai Lau 1105d52d3997SMartin KaFai Lau 1106d52d3997SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry || (rt->rt6i_flags & RTF_CACHE)) { 11073da59bd9SMartin KaFai Lau dst_use(&rt->dst, jiffies); 1108c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 11091da177e4SLinus Torvalds 1110d52d3997SMartin KaFai Lau rt6_dst_from_metrics_check(rt); 1111b811580dSDavid Ahern 1112b811580dSDavid Ahern trace_fib6_table_lookup(net, rt, table->tb6_id, fl6); 1113d52d3997SMartin KaFai Lau return rt; 11143da59bd9SMartin KaFai Lau } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) && 11153da59bd9SMartin KaFai Lau !(rt->rt6i_flags & RTF_GATEWAY))) { 11163da59bd9SMartin KaFai Lau /* Create a RTF_CACHE clone which will not be 11173da59bd9SMartin KaFai Lau * owned by the fib6 tree. It is for the special case where 11183da59bd9SMartin KaFai Lau * the daddr in the skb during the neighbor look-up is different 11193da59bd9SMartin KaFai Lau * from the fl6->daddr used to look-up route here. 11203da59bd9SMartin KaFai Lau */ 1121c71099acSThomas Graf 11223da59bd9SMartin KaFai Lau struct rt6_info *uncached_rt; 11233da59bd9SMartin KaFai Lau 1124d52d3997SMartin KaFai Lau dst_use(&rt->dst, jiffies); 1125d52d3997SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 1126d52d3997SMartin KaFai Lau 11273da59bd9SMartin KaFai Lau uncached_rt = ip6_rt_cache_alloc(rt, &fl6->daddr, NULL); 11283da59bd9SMartin KaFai Lau dst_release(&rt->dst); 11293da59bd9SMartin KaFai Lau 11301cfb71eeSWei Wang if (uncached_rt) { 11311cfb71eeSWei Wang /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc() 11321cfb71eeSWei Wang * No need for another dst_hold() 11331cfb71eeSWei Wang */ 11348d0b94afSMartin KaFai Lau rt6_uncached_list_add(uncached_rt); 11351cfb71eeSWei Wang } else { 11363da59bd9SMartin KaFai Lau uncached_rt = net->ipv6.ip6_null_entry; 11373da59bd9SMartin KaFai Lau dst_hold(&uncached_rt->dst); 11381cfb71eeSWei Wang } 1139b811580dSDavid Ahern 1140b811580dSDavid Ahern trace_fib6_table_lookup(net, uncached_rt, table->tb6_id, fl6); 11413da59bd9SMartin KaFai Lau return uncached_rt; 11423da59bd9SMartin KaFai Lau 1143d52d3997SMartin KaFai Lau } else { 1144d52d3997SMartin KaFai Lau /* Get a percpu copy */ 1145d52d3997SMartin KaFai Lau 1146d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1147d52d3997SMartin KaFai Lau 1148d52d3997SMartin KaFai Lau rt->dst.lastuse = jiffies; 1149d52d3997SMartin KaFai Lau rt->dst.__use++; 1150d52d3997SMartin KaFai Lau pcpu_rt = rt6_get_pcpu_route(rt); 1151d52d3997SMartin KaFai Lau 11529c7370a1SMartin KaFai Lau if (pcpu_rt) { 1153d52d3997SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 11549c7370a1SMartin KaFai Lau } else { 11559c7370a1SMartin KaFai Lau /* We have to do the read_unlock first 11569c7370a1SMartin KaFai Lau * because rt6_make_pcpu_route() may trigger 11579c7370a1SMartin KaFai Lau * ip6_dst_gc() which will take the write_lock. 11589c7370a1SMartin KaFai Lau */ 11599c7370a1SMartin KaFai Lau dst_hold(&rt->dst); 11609c7370a1SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 11619c7370a1SMartin KaFai Lau pcpu_rt = rt6_make_pcpu_route(rt); 11629c7370a1SMartin KaFai Lau dst_release(&rt->dst); 11639c7370a1SMartin KaFai Lau } 1164d52d3997SMartin KaFai Lau 1165b811580dSDavid Ahern trace_fib6_table_lookup(net, pcpu_rt, table->tb6_id, fl6); 1166d52d3997SMartin KaFai Lau return pcpu_rt; 11679c7370a1SMartin KaFai Lau 1168d52d3997SMartin KaFai Lau } 1169c71099acSThomas Graf } 11709ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route); 1171c71099acSThomas Graf 11728ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table, 11734c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 11744acad72dSPavel Emelyanov { 11754c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags); 11764acad72dSPavel Emelyanov } 11774acad72dSPavel Emelyanov 1178d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net, 117972331bc0SShmulik Ladkani struct net_device *dev, 118072331bc0SShmulik Ladkani struct flowi6 *fl6, int flags) 118172331bc0SShmulik Ladkani { 118272331bc0SShmulik Ladkani if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) 118372331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_IFACE; 118472331bc0SShmulik Ladkani 118572331bc0SShmulik Ladkani return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input); 118672331bc0SShmulik Ladkani } 1187d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup); 118872331bc0SShmulik Ladkani 1189c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 1190c71099acSThomas Graf { 1191b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 1192c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 1193adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 1194904af04dSJiri Benc struct ip_tunnel_info *tun_info; 11954c9483b2SDavid S. Miller struct flowi6 fl6 = { 1196e0d56fddSDavid Ahern .flowi6_iif = skb->dev->ifindex, 11974c9483b2SDavid S. Miller .daddr = iph->daddr, 11984c9483b2SDavid S. Miller .saddr = iph->saddr, 11996502ca52SYOSHIFUJI Hideaki / 吉藤英明 .flowlabel = ip6_flowinfo(iph), 12004c9483b2SDavid S. Miller .flowi6_mark = skb->mark, 12014c9483b2SDavid S. Miller .flowi6_proto = iph->nexthdr, 1202c71099acSThomas Graf }; 1203adaa70bbSThomas Graf 1204904af04dSJiri Benc tun_info = skb_tunnel_info(skb); 120546fa062aSJiri Benc if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) 1206904af04dSJiri Benc fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id; 120706e9d040SJiri Benc skb_dst_drop(skb); 120872331bc0SShmulik Ladkani skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags)); 1209c71099acSThomas Graf } 1210c71099acSThomas Graf 12118ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table, 12124c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 1213c71099acSThomas Graf { 12144c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags); 1215c71099acSThomas Graf } 1216c71099acSThomas Graf 12176f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk, 12186f21c96aSPaolo Abeni struct flowi6 *fl6, int flags) 1219c71099acSThomas Graf { 1220d46a9d67SDavid Ahern bool any_src; 1221c71099acSThomas Graf 12224c1feac5SDavid Ahern if (rt6_need_strict(&fl6->daddr)) { 12234c1feac5SDavid Ahern struct dst_entry *dst; 12244c1feac5SDavid Ahern 12254c1feac5SDavid Ahern dst = l3mdev_link_scope_lookup(net, fl6); 1226ca254490SDavid Ahern if (dst) 1227ca254490SDavid Ahern return dst; 12284c1feac5SDavid Ahern } 1229ca254490SDavid Ahern 12301fb9489bSPavel Emelyanov fl6->flowi6_iif = LOOPBACK_IFINDEX; 12314dc27d1cSDavid McCullough 1232d46a9d67SDavid Ahern any_src = ipv6_addr_any(&fl6->saddr); 1233741a11d9SDavid Ahern if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) || 1234d46a9d67SDavid Ahern (fl6->flowi6_oif && any_src)) 123577d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 1236c71099acSThomas Graf 1237d46a9d67SDavid Ahern if (!any_src) 1238adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 12390c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 12400c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 1241adaa70bbSThomas Graf 12424c9483b2SDavid S. Miller return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output); 12431da177e4SLinus Torvalds } 12446f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags); 12451da177e4SLinus Torvalds 12462774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 124714e50e57SDavid S. Miller { 12485c1e6aa3SDavid S. Miller struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; 12491dbe3252SWei Wang struct net_device *loopback_dev = net->loopback_dev; 125014e50e57SDavid S. Miller struct dst_entry *new = NULL; 125114e50e57SDavid S. Miller 12521dbe3252SWei Wang rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1, 1253b2a9c0edSWei Wang DST_OBSOLETE_NONE, 0); 125414e50e57SDavid S. Miller if (rt) { 12550a1f5962SMartin KaFai Lau rt6_info_init(rt); 12560a1f5962SMartin KaFai Lau 1257d8d1f30bSChangli Gao new = &rt->dst; 125814e50e57SDavid S. Miller new->__use = 1; 1259352e512cSHerbert Xu new->input = dst_discard; 1260ede2059dSEric W. Biederman new->output = dst_discard_out; 126114e50e57SDavid S. Miller 1262defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 126314e50e57SDavid S. Miller 12641dbe3252SWei Wang rt->rt6i_idev = in6_dev_get(loopback_dev); 12654e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 12660a1f5962SMartin KaFai Lau rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU; 126714e50e57SDavid S. Miller rt->rt6i_metric = 0; 126814e50e57SDavid S. Miller 126914e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 127014e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 127114e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 127214e50e57SDavid S. Miller #endif 127314e50e57SDavid S. Miller } 127414e50e57SDavid S. Miller 127569ead7afSDavid S. Miller dst_release(dst_orig); 127669ead7afSDavid S. Miller return new ? new : ERR_PTR(-ENOMEM); 127714e50e57SDavid S. Miller } 127814e50e57SDavid S. Miller 12791da177e4SLinus Torvalds /* 12801da177e4SLinus Torvalds * Destination cache support functions 12811da177e4SLinus Torvalds */ 12821da177e4SLinus Torvalds 12834b32b5adSMartin KaFai Lau static void rt6_dst_from_metrics_check(struct rt6_info *rt) 12844b32b5adSMartin KaFai Lau { 12854b32b5adSMartin KaFai Lau if (rt->dst.from && 12864b32b5adSMartin KaFai Lau dst_metrics_ptr(&rt->dst) != dst_metrics_ptr(rt->dst.from)) 12874b32b5adSMartin KaFai Lau dst_init_metrics(&rt->dst, dst_metrics_ptr(rt->dst.from), true); 12884b32b5adSMartin KaFai Lau } 12894b32b5adSMartin KaFai Lau 12903da59bd9SMartin KaFai Lau static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie) 12913da59bd9SMartin KaFai Lau { 12923da59bd9SMartin KaFai Lau if (!rt->rt6i_node || (rt->rt6i_node->fn_sernum != cookie)) 12933da59bd9SMartin KaFai Lau return NULL; 12943da59bd9SMartin KaFai Lau 12953da59bd9SMartin KaFai Lau if (rt6_check_expired(rt)) 12963da59bd9SMartin KaFai Lau return NULL; 12973da59bd9SMartin KaFai Lau 12983da59bd9SMartin KaFai Lau return &rt->dst; 12993da59bd9SMartin KaFai Lau } 13003da59bd9SMartin KaFai Lau 13013da59bd9SMartin KaFai Lau static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, u32 cookie) 13023da59bd9SMartin KaFai Lau { 13035973fb1eSMartin KaFai Lau if (!__rt6_check_expired(rt) && 13045973fb1eSMartin KaFai Lau rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && 13053da59bd9SMartin KaFai Lau rt6_check((struct rt6_info *)(rt->dst.from), cookie)) 13063da59bd9SMartin KaFai Lau return &rt->dst; 13073da59bd9SMartin KaFai Lau else 13083da59bd9SMartin KaFai Lau return NULL; 13093da59bd9SMartin KaFai Lau } 13103da59bd9SMartin KaFai Lau 13111da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 13121da177e4SLinus Torvalds { 13131da177e4SLinus Torvalds struct rt6_info *rt; 13141da177e4SLinus Torvalds 13151da177e4SLinus Torvalds rt = (struct rt6_info *) dst; 13161da177e4SLinus Torvalds 13176f3118b5SNicolas Dichtel /* All IPV6 dsts are created with ->obsolete set to the value 13186f3118b5SNicolas Dichtel * DST_OBSOLETE_FORCE_CHK which forces validation calls down 13196f3118b5SNicolas Dichtel * into this function always. 13206f3118b5SNicolas Dichtel */ 1321e3bc10bdSHannes Frederic Sowa 13224b32b5adSMartin KaFai Lau rt6_dst_from_metrics_check(rt); 13234b32b5adSMartin KaFai Lau 132402bcf4e0SMartin KaFai Lau if (rt->rt6i_flags & RTF_PCPU || 1325a4c2fd7fSWei Wang (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->dst.from)) 13263da59bd9SMartin KaFai Lau return rt6_dst_from_check(rt, cookie); 13273da59bd9SMartin KaFai Lau else 13283da59bd9SMartin KaFai Lau return rt6_check(rt, cookie); 13291da177e4SLinus Torvalds } 13301da177e4SLinus Torvalds 13311da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 13321da177e4SLinus Torvalds { 13331da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 13341da177e4SLinus Torvalds 13351da177e4SLinus Torvalds if (rt) { 133654c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 133754c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 1338e0a1ad73SThomas Graf ip6_del_rt(rt); 133954c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 13401da177e4SLinus Torvalds } 134154c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 134254c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 134354c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 134454c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 134554c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 134654c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 13471da177e4SLinus Torvalds } 13481da177e4SLinus Torvalds 13491da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 13501da177e4SLinus Torvalds { 13511da177e4SLinus Torvalds struct rt6_info *rt; 13521da177e4SLinus Torvalds 13533ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 13541da177e4SLinus Torvalds 1355adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 13561da177e4SLinus Torvalds if (rt) { 13571eb4f758SHannes Frederic Sowa if (rt->rt6i_flags & RTF_CACHE) { 1358ad65a2f0SWei Wang if (dst_hold_safe(&rt->dst)) 13598e3d5be7SMartin KaFai Lau ip6_del_rt(rt); 13601eb4f758SHannes Frederic Sowa } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) { 13611da177e4SLinus Torvalds rt->rt6i_node->fn_sernum = -1; 13621da177e4SLinus Torvalds } 13631da177e4SLinus Torvalds } 13641eb4f758SHannes Frederic Sowa } 13651da177e4SLinus Torvalds 136645e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu) 136745e4fd26SMartin KaFai Lau { 136845e4fd26SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 136945e4fd26SMartin KaFai Lau 137045e4fd26SMartin KaFai Lau rt->rt6i_flags |= RTF_MODIFIED; 137145e4fd26SMartin KaFai Lau rt->rt6i_pmtu = mtu; 137245e4fd26SMartin KaFai Lau rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires); 137345e4fd26SMartin KaFai Lau } 137445e4fd26SMartin KaFai Lau 13750d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt) 13760d3f6d29SMartin KaFai Lau { 13770d3f6d29SMartin KaFai Lau return !(rt->rt6i_flags & RTF_CACHE) && 13780d3f6d29SMartin KaFai Lau (rt->rt6i_flags & RTF_PCPU || rt->rt6i_node); 13790d3f6d29SMartin KaFai Lau } 13800d3f6d29SMartin KaFai Lau 138145e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk, 138245e4fd26SMartin KaFai Lau const struct ipv6hdr *iph, u32 mtu) 13831da177e4SLinus Torvalds { 13840dec879fSJulian Anastasov const struct in6_addr *daddr, *saddr; 13851da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info *)dst; 13861da177e4SLinus Torvalds 138745e4fd26SMartin KaFai Lau if (rt6->rt6i_flags & RTF_LOCAL) 138845e4fd26SMartin KaFai Lau return; 138945e4fd26SMartin KaFai Lau 139019bda36cSXin Long if (dst_metric_locked(dst, RTAX_MTU)) 139119bda36cSXin Long return; 139219bda36cSXin Long 139345e4fd26SMartin KaFai Lau if (iph) { 139445e4fd26SMartin KaFai Lau daddr = &iph->daddr; 139545e4fd26SMartin KaFai Lau saddr = &iph->saddr; 139645e4fd26SMartin KaFai Lau } else if (sk) { 139745e4fd26SMartin KaFai Lau daddr = &sk->sk_v6_daddr; 139845e4fd26SMartin KaFai Lau saddr = &inet6_sk(sk)->saddr; 139945e4fd26SMartin KaFai Lau } else { 14000dec879fSJulian Anastasov daddr = NULL; 14010dec879fSJulian Anastasov saddr = NULL; 14021da177e4SLinus Torvalds } 14030dec879fSJulian Anastasov dst_confirm_neigh(dst, daddr); 14040dec879fSJulian Anastasov mtu = max_t(u32, mtu, IPV6_MIN_MTU); 14050dec879fSJulian Anastasov if (mtu >= dst_mtu(dst)) 14060dec879fSJulian Anastasov return; 14070dec879fSJulian Anastasov 14080dec879fSJulian Anastasov if (!rt6_cache_allowed_for_pmtu(rt6)) { 14090dec879fSJulian Anastasov rt6_do_update_pmtu(rt6, mtu); 14100dec879fSJulian Anastasov } else if (daddr) { 14110dec879fSJulian Anastasov struct rt6_info *nrt6; 14120dec879fSJulian Anastasov 141345e4fd26SMartin KaFai Lau nrt6 = ip6_rt_cache_alloc(rt6, daddr, saddr); 141445e4fd26SMartin KaFai Lau if (nrt6) { 141545e4fd26SMartin KaFai Lau rt6_do_update_pmtu(nrt6, mtu); 141645e4fd26SMartin KaFai Lau 141745e4fd26SMartin KaFai Lau /* ip6_ins_rt(nrt6) will bump the 141845e4fd26SMartin KaFai Lau * rt6->rt6i_node->fn_sernum 141945e4fd26SMartin KaFai Lau * which will fail the next rt6_check() and 142045e4fd26SMartin KaFai Lau * invalidate the sk->sk_dst_cache. 142145e4fd26SMartin KaFai Lau */ 142245e4fd26SMartin KaFai Lau ip6_ins_rt(nrt6); 14231cfb71eeSWei Wang /* Release the reference taken in 14241cfb71eeSWei Wang * ip6_rt_cache_alloc() 14251cfb71eeSWei Wang */ 14261cfb71eeSWei Wang dst_release(&nrt6->dst); 142745e4fd26SMartin KaFai Lau } 142845e4fd26SMartin KaFai Lau } 142945e4fd26SMartin KaFai Lau } 143045e4fd26SMartin KaFai Lau 143145e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 143245e4fd26SMartin KaFai Lau struct sk_buff *skb, u32 mtu) 143345e4fd26SMartin KaFai Lau { 143445e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu); 14351da177e4SLinus Torvalds } 14361da177e4SLinus Torvalds 143742ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, 1438e2d118a1SLorenzo Colitti int oif, u32 mark, kuid_t uid) 143981aded24SDavid S. Miller { 144081aded24SDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 144181aded24SDavid S. Miller struct dst_entry *dst; 144281aded24SDavid S. Miller struct flowi6 fl6; 144381aded24SDavid S. Miller 144481aded24SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 144581aded24SDavid S. Miller fl6.flowi6_oif = oif; 14461b3c61dcSLorenzo Colitti fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark); 144781aded24SDavid S. Miller fl6.daddr = iph->daddr; 144881aded24SDavid S. Miller fl6.saddr = iph->saddr; 14496502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 1450e2d118a1SLorenzo Colitti fl6.flowi6_uid = uid; 145181aded24SDavid S. Miller 145281aded24SDavid S. Miller dst = ip6_route_output(net, NULL, &fl6); 145381aded24SDavid S. Miller if (!dst->error) 145445e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu)); 145581aded24SDavid S. Miller dst_release(dst); 145681aded24SDavid S. Miller } 145781aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu); 145881aded24SDavid S. Miller 145981aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) 146081aded24SDavid S. Miller { 146133c162a9SMartin KaFai Lau struct dst_entry *dst; 146233c162a9SMartin KaFai Lau 146381aded24SDavid S. Miller ip6_update_pmtu(skb, sock_net(sk), mtu, 1464e2d118a1SLorenzo Colitti sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid); 146533c162a9SMartin KaFai Lau 146633c162a9SMartin KaFai Lau dst = __sk_dst_get(sk); 146733c162a9SMartin KaFai Lau if (!dst || !dst->obsolete || 146833c162a9SMartin KaFai Lau dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) 146933c162a9SMartin KaFai Lau return; 147033c162a9SMartin KaFai Lau 147133c162a9SMartin KaFai Lau bh_lock_sock(sk); 147233c162a9SMartin KaFai Lau if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) 147333c162a9SMartin KaFai Lau ip6_datagram_dst_update(sk, false); 147433c162a9SMartin KaFai Lau bh_unlock_sock(sk); 147581aded24SDavid S. Miller } 147681aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); 147781aded24SDavid S. Miller 1478b55b76b2SDuan Jiong /* Handle redirects */ 1479b55b76b2SDuan Jiong struct ip6rd_flowi { 1480b55b76b2SDuan Jiong struct flowi6 fl6; 1481b55b76b2SDuan Jiong struct in6_addr gateway; 1482b55b76b2SDuan Jiong }; 1483b55b76b2SDuan Jiong 1484b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net, 1485b55b76b2SDuan Jiong struct fib6_table *table, 1486b55b76b2SDuan Jiong struct flowi6 *fl6, 1487b55b76b2SDuan Jiong int flags) 1488b55b76b2SDuan Jiong { 1489b55b76b2SDuan Jiong struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; 1490b55b76b2SDuan Jiong struct rt6_info *rt; 1491b55b76b2SDuan Jiong struct fib6_node *fn; 1492b55b76b2SDuan Jiong 1493b55b76b2SDuan Jiong /* Get the "current" route for this destination and 149467c408cfSAlexander Alemayhu * check if the redirect has come from appropriate router. 1495b55b76b2SDuan Jiong * 1496b55b76b2SDuan Jiong * RFC 4861 specifies that redirects should only be 1497b55b76b2SDuan Jiong * accepted if they come from the nexthop to the target. 1498b55b76b2SDuan Jiong * Due to the way the routes are chosen, this notion 1499b55b76b2SDuan Jiong * is a bit fuzzy and one might need to check all possible 1500b55b76b2SDuan Jiong * routes. 1501b55b76b2SDuan Jiong */ 1502b55b76b2SDuan Jiong 1503b55b76b2SDuan Jiong read_lock_bh(&table->tb6_lock); 1504b55b76b2SDuan Jiong fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1505b55b76b2SDuan Jiong restart: 1506b55b76b2SDuan Jiong for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 1507b55b76b2SDuan Jiong if (rt6_check_expired(rt)) 1508b55b76b2SDuan Jiong continue; 1509b55b76b2SDuan Jiong if (rt->dst.error) 1510b55b76b2SDuan Jiong break; 1511b55b76b2SDuan Jiong if (!(rt->rt6i_flags & RTF_GATEWAY)) 1512b55b76b2SDuan Jiong continue; 1513b55b76b2SDuan Jiong if (fl6->flowi6_oif != rt->dst.dev->ifindex) 1514b55b76b2SDuan Jiong continue; 1515b55b76b2SDuan Jiong if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway)) 1516b55b76b2SDuan Jiong continue; 1517b55b76b2SDuan Jiong break; 1518b55b76b2SDuan Jiong } 1519b55b76b2SDuan Jiong 1520b55b76b2SDuan Jiong if (!rt) 1521b55b76b2SDuan Jiong rt = net->ipv6.ip6_null_entry; 1522b55b76b2SDuan Jiong else if (rt->dst.error) { 1523b55b76b2SDuan Jiong rt = net->ipv6.ip6_null_entry; 1524b0a1ba59SMartin KaFai Lau goto out; 1525b0a1ba59SMartin KaFai Lau } 1526b0a1ba59SMartin KaFai Lau 1527b0a1ba59SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 1528a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1529a3c00e46SMartin KaFai Lau if (fn) 1530a3c00e46SMartin KaFai Lau goto restart; 1531b55b76b2SDuan Jiong } 1532a3c00e46SMartin KaFai Lau 1533b0a1ba59SMartin KaFai Lau out: 1534b55b76b2SDuan Jiong dst_hold(&rt->dst); 1535b55b76b2SDuan Jiong 1536b55b76b2SDuan Jiong read_unlock_bh(&table->tb6_lock); 1537b55b76b2SDuan Jiong 1538b811580dSDavid Ahern trace_fib6_table_lookup(net, rt, table->tb6_id, fl6); 1539b55b76b2SDuan Jiong return rt; 1540b55b76b2SDuan Jiong }; 1541b55b76b2SDuan Jiong 1542b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net, 1543b55b76b2SDuan Jiong const struct flowi6 *fl6, 1544b55b76b2SDuan Jiong const struct in6_addr *gateway) 1545b55b76b2SDuan Jiong { 1546b55b76b2SDuan Jiong int flags = RT6_LOOKUP_F_HAS_SADDR; 1547b55b76b2SDuan Jiong struct ip6rd_flowi rdfl; 1548b55b76b2SDuan Jiong 1549b55b76b2SDuan Jiong rdfl.fl6 = *fl6; 1550b55b76b2SDuan Jiong rdfl.gateway = *gateway; 1551b55b76b2SDuan Jiong 1552b55b76b2SDuan Jiong return fib6_rule_lookup(net, &rdfl.fl6, 1553b55b76b2SDuan Jiong flags, __ip6_route_redirect); 1554b55b76b2SDuan Jiong } 1555b55b76b2SDuan Jiong 1556e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, 1557e2d118a1SLorenzo Colitti kuid_t uid) 15583a5ad2eeSDavid S. Miller { 15593a5ad2eeSDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 15603a5ad2eeSDavid S. Miller struct dst_entry *dst; 15613a5ad2eeSDavid S. Miller struct flowi6 fl6; 15623a5ad2eeSDavid S. Miller 15633a5ad2eeSDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 1564e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 15653a5ad2eeSDavid S. Miller fl6.flowi6_oif = oif; 15663a5ad2eeSDavid S. Miller fl6.flowi6_mark = mark; 15673a5ad2eeSDavid S. Miller fl6.daddr = iph->daddr; 15683a5ad2eeSDavid S. Miller fl6.saddr = iph->saddr; 15696502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 1570e2d118a1SLorenzo Colitti fl6.flowi6_uid = uid; 15713a5ad2eeSDavid S. Miller 1572b55b76b2SDuan Jiong dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr); 15736700c270SDavid S. Miller rt6_do_redirect(dst, NULL, skb); 15743a5ad2eeSDavid S. Miller dst_release(dst); 15753a5ad2eeSDavid S. Miller } 15763a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect); 15773a5ad2eeSDavid S. Miller 1578c92a59ecSDuan Jiong void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif, 1579c92a59ecSDuan Jiong u32 mark) 1580c92a59ecSDuan Jiong { 1581c92a59ecSDuan Jiong const struct ipv6hdr *iph = ipv6_hdr(skb); 1582c92a59ecSDuan Jiong const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); 1583c92a59ecSDuan Jiong struct dst_entry *dst; 1584c92a59ecSDuan Jiong struct flowi6 fl6; 1585c92a59ecSDuan Jiong 1586c92a59ecSDuan Jiong memset(&fl6, 0, sizeof(fl6)); 1587e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 1588c92a59ecSDuan Jiong fl6.flowi6_oif = oif; 1589c92a59ecSDuan Jiong fl6.flowi6_mark = mark; 1590c92a59ecSDuan Jiong fl6.daddr = msg->dest; 1591c92a59ecSDuan Jiong fl6.saddr = iph->daddr; 1592e2d118a1SLorenzo Colitti fl6.flowi6_uid = sock_net_uid(net, NULL); 1593c92a59ecSDuan Jiong 1594b55b76b2SDuan Jiong dst = ip6_route_redirect(net, &fl6, &iph->saddr); 1595c92a59ecSDuan Jiong rt6_do_redirect(dst, NULL, skb); 1596c92a59ecSDuan Jiong dst_release(dst); 1597c92a59ecSDuan Jiong } 1598c92a59ecSDuan Jiong 15993a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) 16003a5ad2eeSDavid S. Miller { 1601e2d118a1SLorenzo Colitti ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark, 1602e2d118a1SLorenzo Colitti sk->sk_uid); 16033a5ad2eeSDavid S. Miller } 16043a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect); 16053a5ad2eeSDavid S. Miller 16060dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 16071da177e4SLinus Torvalds { 16080dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 16090dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 16100dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 16110dbaee3bSDavid S. Miller 16121da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 16131da177e4SLinus Torvalds 16145578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 16155578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 16161da177e4SLinus Torvalds 16171da177e4SLinus Torvalds /* 16181da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 16191da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 16201da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 16211da177e4SLinus Torvalds * rely only on pmtu discovery" 16221da177e4SLinus Torvalds */ 16231da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 16241da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 16251da177e4SLinus Torvalds return mtu; 16261da177e4SLinus Torvalds } 16271da177e4SLinus Torvalds 1628ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst) 1629d33e4553SDavid S. Miller { 16304b32b5adSMartin KaFai Lau const struct rt6_info *rt = (const struct rt6_info *)dst; 16314b32b5adSMartin KaFai Lau unsigned int mtu = rt->rt6i_pmtu; 1632d33e4553SDavid S. Miller struct inet6_dev *idev; 1633618f9bc7SSteffen Klassert 1634618f9bc7SSteffen Klassert if (mtu) 163530f78d8eSEric Dumazet goto out; 1636618f9bc7SSteffen Klassert 16374b32b5adSMartin KaFai Lau mtu = dst_metric_raw(dst, RTAX_MTU); 16384b32b5adSMartin KaFai Lau if (mtu) 16394b32b5adSMartin KaFai Lau goto out; 16404b32b5adSMartin KaFai Lau 1641618f9bc7SSteffen Klassert mtu = IPV6_MIN_MTU; 1642d33e4553SDavid S. Miller 1643d33e4553SDavid S. Miller rcu_read_lock(); 1644d33e4553SDavid S. Miller idev = __in6_dev_get(dst->dev); 1645d33e4553SDavid S. Miller if (idev) 1646d33e4553SDavid S. Miller mtu = idev->cnf.mtu6; 1647d33e4553SDavid S. Miller rcu_read_unlock(); 1648d33e4553SDavid S. Miller 164930f78d8eSEric Dumazet out: 165014972cbdSRoopa Prabhu mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 165114972cbdSRoopa Prabhu 165214972cbdSRoopa Prabhu return mtu - lwtunnel_headroom(dst->lwtstate, mtu); 1653d33e4553SDavid S. Miller } 1654d33e4553SDavid S. Miller 16553b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 165687a11578SDavid S. Miller struct flowi6 *fl6) 16571da177e4SLinus Torvalds { 165887a11578SDavid S. Miller struct dst_entry *dst; 16591da177e4SLinus Torvalds struct rt6_info *rt; 16601da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 1661c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 16621da177e4SLinus Torvalds 166338308473SDavid S. Miller if (unlikely(!idev)) 1664122bdf67SEric Dumazet return ERR_PTR(-ENODEV); 16651da177e4SLinus Torvalds 1666ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, dev, 0); 166738308473SDavid S. Miller if (unlikely(!rt)) { 16681da177e4SLinus Torvalds in6_dev_put(idev); 166987a11578SDavid S. Miller dst = ERR_PTR(-ENOMEM); 16701da177e4SLinus Torvalds goto out; 16711da177e4SLinus Torvalds } 16721da177e4SLinus Torvalds 16738e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 16748e2ec639SYan, Zheng rt->dst.output = ip6_output; 1675550bab42SJulian Anastasov rt->rt6i_gateway = fl6->daddr; 167687a11578SDavid S. Miller rt->rt6i_dst.addr = fl6->daddr; 16778e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 16788e2ec639SYan, Zheng rt->rt6i_idev = idev; 167914edd87dSLi RongQing dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); 16801da177e4SLinus Torvalds 1681587fea74SWei Wang /* Add this dst into uncached_list so that rt6_ifdown() can 1682587fea74SWei Wang * do proper release of the net_device 1683587fea74SWei Wang */ 1684587fea74SWei Wang rt6_uncached_list_add(rt); 16851da177e4SLinus Torvalds 168687a11578SDavid S. Miller dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); 168787a11578SDavid S. Miller 16881da177e4SLinus Torvalds out: 168987a11578SDavid S. Miller return dst; 16901da177e4SLinus Torvalds } 16911da177e4SLinus Torvalds 1692569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops) 16931da177e4SLinus Torvalds { 169486393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 16957019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 16967019b78eSDaniel Lezcano int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; 16977019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 16987019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 16997019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 1700fc66f95cSEric Dumazet int entries; 17011da177e4SLinus Torvalds 1702fc66f95cSEric Dumazet entries = dst_entries_get_fast(ops); 170349a18d86SMichal Kubeček if (time_after(rt_last_gc + rt_min_interval, jiffies) && 1704fc66f95cSEric Dumazet entries <= rt_max_size) 17051da177e4SLinus Torvalds goto out; 17061da177e4SLinus Torvalds 17076891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire++; 170814956643SLi RongQing fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true); 1709fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 1710fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 17117019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; 17121da177e4SLinus Torvalds out: 17137019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; 1714fc66f95cSEric Dumazet return entries > rt_max_size; 17151da177e4SLinus Torvalds } 17161da177e4SLinus Torvalds 1717e715b6d3SFlorian Westphal static int ip6_convert_metrics(struct mx6_config *mxc, 1718e715b6d3SFlorian Westphal const struct fib6_config *cfg) 1719e715b6d3SFlorian Westphal { 1720c3a8d947SDaniel Borkmann bool ecn_ca = false; 1721e715b6d3SFlorian Westphal struct nlattr *nla; 1722e715b6d3SFlorian Westphal int remaining; 1723e715b6d3SFlorian Westphal u32 *mp; 1724e715b6d3SFlorian Westphal 172563159f29SIan Morris if (!cfg->fc_mx) 1726e715b6d3SFlorian Westphal return 0; 1727e715b6d3SFlorian Westphal 1728e715b6d3SFlorian Westphal mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL); 1729e715b6d3SFlorian Westphal if (unlikely(!mp)) 1730e715b6d3SFlorian Westphal return -ENOMEM; 1731e715b6d3SFlorian Westphal 1732e715b6d3SFlorian Westphal nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { 1733e715b6d3SFlorian Westphal int type = nla_type(nla); 1734ea697639SDaniel Borkmann u32 val; 1735ea697639SDaniel Borkmann 17361bb14807SDaniel Borkmann if (!type) 17371bb14807SDaniel Borkmann continue; 1738e715b6d3SFlorian Westphal if (unlikely(type > RTAX_MAX)) 1739e715b6d3SFlorian Westphal goto err; 17401bb14807SDaniel Borkmann 1741ea697639SDaniel Borkmann if (type == RTAX_CC_ALGO) { 1742ea697639SDaniel Borkmann char tmp[TCP_CA_NAME_MAX]; 1743e715b6d3SFlorian Westphal 1744ea697639SDaniel Borkmann nla_strlcpy(tmp, nla, sizeof(tmp)); 1745c3a8d947SDaniel Borkmann val = tcp_ca_get_key_by_name(tmp, &ecn_ca); 1746ea697639SDaniel Borkmann if (val == TCP_CA_UNSPEC) 1747ea697639SDaniel Borkmann goto err; 1748ea697639SDaniel Borkmann } else { 1749ea697639SDaniel Borkmann val = nla_get_u32(nla); 1750ea697639SDaniel Borkmann } 1751626abd59SPaolo Abeni if (type == RTAX_HOPLIMIT && val > 255) 1752626abd59SPaolo Abeni val = 255; 1753b8d3e416SDaniel Borkmann if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK)) 1754b8d3e416SDaniel Borkmann goto err; 1755ea697639SDaniel Borkmann 1756ea697639SDaniel Borkmann mp[type - 1] = val; 1757e715b6d3SFlorian Westphal __set_bit(type - 1, mxc->mx_valid); 1758e715b6d3SFlorian Westphal } 1759e715b6d3SFlorian Westphal 1760c3a8d947SDaniel Borkmann if (ecn_ca) { 1761c3a8d947SDaniel Borkmann __set_bit(RTAX_FEATURES - 1, mxc->mx_valid); 1762c3a8d947SDaniel Borkmann mp[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA; 1763c3a8d947SDaniel Borkmann } 1764e715b6d3SFlorian Westphal 1765c3a8d947SDaniel Borkmann mxc->mx = mp; 1766e715b6d3SFlorian Westphal return 0; 1767e715b6d3SFlorian Westphal err: 1768e715b6d3SFlorian Westphal kfree(mp); 1769e715b6d3SFlorian Westphal return -EINVAL; 1770e715b6d3SFlorian Westphal } 17711da177e4SLinus Torvalds 17728c14586fSDavid Ahern static struct rt6_info *ip6_nh_lookup_table(struct net *net, 17738c14586fSDavid Ahern struct fib6_config *cfg, 17748c14586fSDavid Ahern const struct in6_addr *gw_addr) 17758c14586fSDavid Ahern { 17768c14586fSDavid Ahern struct flowi6 fl6 = { 17778c14586fSDavid Ahern .flowi6_oif = cfg->fc_ifindex, 17788c14586fSDavid Ahern .daddr = *gw_addr, 17798c14586fSDavid Ahern .saddr = cfg->fc_prefsrc, 17808c14586fSDavid Ahern }; 17818c14586fSDavid Ahern struct fib6_table *table; 17828c14586fSDavid Ahern struct rt6_info *rt; 1783d5d32e4bSDavid Ahern int flags = RT6_LOOKUP_F_IFACE | RT6_LOOKUP_F_IGNORE_LINKSTATE; 17848c14586fSDavid Ahern 17858c14586fSDavid Ahern table = fib6_get_table(net, cfg->fc_table); 17868c14586fSDavid Ahern if (!table) 17878c14586fSDavid Ahern return NULL; 17888c14586fSDavid Ahern 17898c14586fSDavid Ahern if (!ipv6_addr_any(&cfg->fc_prefsrc)) 17908c14586fSDavid Ahern flags |= RT6_LOOKUP_F_HAS_SADDR; 17918c14586fSDavid Ahern 17928c14586fSDavid Ahern rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, flags); 17938c14586fSDavid Ahern 17948c14586fSDavid Ahern /* if table lookup failed, fall back to full lookup */ 17958c14586fSDavid Ahern if (rt == net->ipv6.ip6_null_entry) { 17968c14586fSDavid Ahern ip6_rt_put(rt); 17978c14586fSDavid Ahern rt = NULL; 17988c14586fSDavid Ahern } 17998c14586fSDavid Ahern 18008c14586fSDavid Ahern return rt; 18018c14586fSDavid Ahern } 18028c14586fSDavid Ahern 1803333c4301SDavid Ahern static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg, 1804333c4301SDavid Ahern struct netlink_ext_ack *extack) 18051da177e4SLinus Torvalds { 18065578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 18071da177e4SLinus Torvalds struct rt6_info *rt = NULL; 18081da177e4SLinus Torvalds struct net_device *dev = NULL; 18091da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 1810c71099acSThomas Graf struct fib6_table *table; 18111da177e4SLinus Torvalds int addr_type; 18128c5b83f0SRoopa Prabhu int err = -EINVAL; 18131da177e4SLinus Torvalds 1814557c44beSDavid Ahern /* RTF_PCPU is an internal flag; can not be set by userspace */ 1815d5d531cbSDavid Ahern if (cfg->fc_flags & RTF_PCPU) { 1816d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU"); 1817557c44beSDavid Ahern goto out; 1818d5d531cbSDavid Ahern } 1819557c44beSDavid Ahern 1820d5d531cbSDavid Ahern if (cfg->fc_dst_len > 128) { 1821d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid prefix length"); 18228c5b83f0SRoopa Prabhu goto out; 1823d5d531cbSDavid Ahern } 1824d5d531cbSDavid Ahern if (cfg->fc_src_len > 128) { 1825d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address length"); 1826d5d531cbSDavid Ahern goto out; 1827d5d531cbSDavid Ahern } 18281da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 1829d5d531cbSDavid Ahern if (cfg->fc_src_len) { 1830d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 1831d5d531cbSDavid Ahern "Specifying source address requires IPV6_SUBTREES to be enabled"); 18328c5b83f0SRoopa Prabhu goto out; 1833d5d531cbSDavid Ahern } 18341da177e4SLinus Torvalds #endif 183586872cb5SThomas Graf if (cfg->fc_ifindex) { 18361da177e4SLinus Torvalds err = -ENODEV; 18375578689aSDaniel Lezcano dev = dev_get_by_index(net, cfg->fc_ifindex); 18381da177e4SLinus Torvalds if (!dev) 18391da177e4SLinus Torvalds goto out; 18401da177e4SLinus Torvalds idev = in6_dev_get(dev); 18411da177e4SLinus Torvalds if (!idev) 18421da177e4SLinus Torvalds goto out; 18431da177e4SLinus Torvalds } 18441da177e4SLinus Torvalds 184586872cb5SThomas Graf if (cfg->fc_metric == 0) 184686872cb5SThomas Graf cfg->fc_metric = IP6_RT_PRIO_USER; 18471da177e4SLinus Torvalds 1848c71099acSThomas Graf err = -ENOBUFS; 184938308473SDavid S. Miller if (cfg->fc_nlinfo.nlh && 1850d71314b4SMatti Vaittinen !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { 1851d71314b4SMatti Vaittinen table = fib6_get_table(net, cfg->fc_table); 185238308473SDavid S. Miller if (!table) { 1853f3213831SJoe Perches pr_warn("NLM_F_CREATE should be specified when creating new route\n"); 1854d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 1855d71314b4SMatti Vaittinen } 1856d71314b4SMatti Vaittinen } else { 1857d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 1858d71314b4SMatti Vaittinen } 185938308473SDavid S. Miller 186038308473SDavid S. Miller if (!table) 1861c71099acSThomas Graf goto out; 1862c71099acSThomas Graf 1863ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, NULL, 1864ad706862SMartin KaFai Lau (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT); 18651da177e4SLinus Torvalds 186638308473SDavid S. Miller if (!rt) { 18671da177e4SLinus Torvalds err = -ENOMEM; 18681da177e4SLinus Torvalds goto out; 18691da177e4SLinus Torvalds } 18701da177e4SLinus Torvalds 18711716a961SGao feng if (cfg->fc_flags & RTF_EXPIRES) 18721716a961SGao feng rt6_set_expires(rt, jiffies + 18731716a961SGao feng clock_t_to_jiffies(cfg->fc_expires)); 18741716a961SGao feng else 18751716a961SGao feng rt6_clean_expires(rt); 18761da177e4SLinus Torvalds 187786872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 187886872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 187986872cb5SThomas Graf rt->rt6i_protocol = cfg->fc_protocol; 188086872cb5SThomas Graf 188186872cb5SThomas Graf addr_type = ipv6_addr_type(&cfg->fc_dst); 18821da177e4SLinus Torvalds 18831da177e4SLinus Torvalds if (addr_type & IPV6_ADDR_MULTICAST) 1884d8d1f30bSChangli Gao rt->dst.input = ip6_mc_input; 1885ab79ad14SMaciej Żenczykowski else if (cfg->fc_flags & RTF_LOCAL) 1886ab79ad14SMaciej Żenczykowski rt->dst.input = ip6_input; 18871da177e4SLinus Torvalds else 1888d8d1f30bSChangli Gao rt->dst.input = ip6_forward; 18891da177e4SLinus Torvalds 1890d8d1f30bSChangli Gao rt->dst.output = ip6_output; 18911da177e4SLinus Torvalds 189219e42e45SRoopa Prabhu if (cfg->fc_encap) { 189319e42e45SRoopa Prabhu struct lwtunnel_state *lwtstate; 189419e42e45SRoopa Prabhu 189530357d7dSDavid Ahern err = lwtunnel_build_state(cfg->fc_encap_type, 1896127eb7cdSTom Herbert cfg->fc_encap, AF_INET6, cfg, 18979ae28727SDavid Ahern &lwtstate, extack); 189819e42e45SRoopa Prabhu if (err) 189919e42e45SRoopa Prabhu goto out; 190061adedf3SJiri Benc rt->dst.lwtstate = lwtstate_get(lwtstate); 190161adedf3SJiri Benc if (lwtunnel_output_redirect(rt->dst.lwtstate)) { 190261adedf3SJiri Benc rt->dst.lwtstate->orig_output = rt->dst.output; 190361adedf3SJiri Benc rt->dst.output = lwtunnel_output; 190419e42e45SRoopa Prabhu } 190561adedf3SJiri Benc if (lwtunnel_input_redirect(rt->dst.lwtstate)) { 190661adedf3SJiri Benc rt->dst.lwtstate->orig_input = rt->dst.input; 190761adedf3SJiri Benc rt->dst.input = lwtunnel_input; 190825368623STom Herbert } 190925368623STom Herbert } 191019e42e45SRoopa Prabhu 191186872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 191286872cb5SThomas Graf rt->rt6i_dst.plen = cfg->fc_dst_len; 1913afc4eef8SMartin KaFai Lau if (rt->rt6i_dst.plen == 128) 191411d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 19151da177e4SLinus Torvalds 19161da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 191786872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len); 191886872cb5SThomas Graf rt->rt6i_src.plen = cfg->fc_src_len; 19191da177e4SLinus Torvalds #endif 19201da177e4SLinus Torvalds 192186872cb5SThomas Graf rt->rt6i_metric = cfg->fc_metric; 19221da177e4SLinus Torvalds 19231da177e4SLinus Torvalds /* We cannot add true routes via loopback here, 19241da177e4SLinus Torvalds they would result in kernel looping; promote them to reject routes 19251da177e4SLinus Torvalds */ 192686872cb5SThomas Graf if ((cfg->fc_flags & RTF_REJECT) || 192738308473SDavid S. Miller (dev && (dev->flags & IFF_LOOPBACK) && 192838308473SDavid S. Miller !(addr_type & IPV6_ADDR_LOOPBACK) && 192938308473SDavid S. Miller !(cfg->fc_flags & RTF_LOCAL))) { 19301da177e4SLinus Torvalds /* hold loopback dev/idev if we haven't done so. */ 19315578689aSDaniel Lezcano if (dev != net->loopback_dev) { 19321da177e4SLinus Torvalds if (dev) { 19331da177e4SLinus Torvalds dev_put(dev); 19341da177e4SLinus Torvalds in6_dev_put(idev); 19351da177e4SLinus Torvalds } 19365578689aSDaniel Lezcano dev = net->loopback_dev; 19371da177e4SLinus Torvalds dev_hold(dev); 19381da177e4SLinus Torvalds idev = in6_dev_get(dev); 19391da177e4SLinus Torvalds if (!idev) { 19401da177e4SLinus Torvalds err = -ENODEV; 19411da177e4SLinus Torvalds goto out; 19421da177e4SLinus Torvalds } 19431da177e4SLinus Torvalds } 19441da177e4SLinus Torvalds rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP; 1945ef2c7d7bSNicolas Dichtel switch (cfg->fc_type) { 1946ef2c7d7bSNicolas Dichtel case RTN_BLACKHOLE: 1947ef2c7d7bSNicolas Dichtel rt->dst.error = -EINVAL; 1948ede2059dSEric W. Biederman rt->dst.output = dst_discard_out; 19497150aedeSKamala R rt->dst.input = dst_discard; 1950ef2c7d7bSNicolas Dichtel break; 1951ef2c7d7bSNicolas Dichtel case RTN_PROHIBIT: 1952ef2c7d7bSNicolas Dichtel rt->dst.error = -EACCES; 19537150aedeSKamala R rt->dst.output = ip6_pkt_prohibit_out; 19547150aedeSKamala R rt->dst.input = ip6_pkt_prohibit; 1955ef2c7d7bSNicolas Dichtel break; 1956b4949ab2SNicolas Dichtel case RTN_THROW: 19570315e382SNikola Forró case RTN_UNREACHABLE: 1958ef2c7d7bSNicolas Dichtel default: 19597150aedeSKamala R rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN 19600315e382SNikola Forró : (cfg->fc_type == RTN_UNREACHABLE) 19610315e382SNikola Forró ? -EHOSTUNREACH : -ENETUNREACH; 19627150aedeSKamala R rt->dst.output = ip6_pkt_discard_out; 19637150aedeSKamala R rt->dst.input = ip6_pkt_discard; 1964ef2c7d7bSNicolas Dichtel break; 1965ef2c7d7bSNicolas Dichtel } 19661da177e4SLinus Torvalds goto install_route; 19671da177e4SLinus Torvalds } 19681da177e4SLinus Torvalds 196986872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY) { 1970b71d1d42SEric Dumazet const struct in6_addr *gw_addr; 19711da177e4SLinus Torvalds int gwa_type; 19721da177e4SLinus Torvalds 197386872cb5SThomas Graf gw_addr = &cfg->fc_gateway; 1974330567b7SFlorian Westphal gwa_type = ipv6_addr_type(gw_addr); 197548ed7b26SFlorian Westphal 197648ed7b26SFlorian Westphal /* if gw_addr is local we will fail to detect this in case 197748ed7b26SFlorian Westphal * address is still TENTATIVE (DAD in progress). rt6_lookup() 197848ed7b26SFlorian Westphal * will return already-added prefix route via interface that 197948ed7b26SFlorian Westphal * prefix route was assigned to, which might be non-loopback. 198048ed7b26SFlorian Westphal */ 198148ed7b26SFlorian Westphal err = -EINVAL; 1982330567b7SFlorian Westphal if (ipv6_chk_addr_and_flags(net, gw_addr, 1983330567b7SFlorian Westphal gwa_type & IPV6_ADDR_LINKLOCAL ? 1984d5d531cbSDavid Ahern dev : NULL, 0, 0)) { 1985d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid gateway address"); 198648ed7b26SFlorian Westphal goto out; 1987d5d531cbSDavid Ahern } 19884e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = *gw_addr; 19891da177e4SLinus Torvalds 19901da177e4SLinus Torvalds if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) { 19918c14586fSDavid Ahern struct rt6_info *grt = NULL; 19921da177e4SLinus Torvalds 19931da177e4SLinus Torvalds /* IPv6 strictly inhibits using not link-local 19941da177e4SLinus Torvalds addresses as nexthop address. 19951da177e4SLinus Torvalds Otherwise, router will not able to send redirects. 19961da177e4SLinus Torvalds It is very good, but in some (rare!) circumstances 19971da177e4SLinus Torvalds (SIT, PtP, NBMA NOARP links) it is handy to allow 19981da177e4SLinus Torvalds some exceptions. --ANK 199996d5822cSErik Nordmark We allow IPv4-mapped nexthops to support RFC4798-type 200096d5822cSErik Nordmark addressing 20011da177e4SLinus Torvalds */ 200296d5822cSErik Nordmark if (!(gwa_type & (IPV6_ADDR_UNICAST | 2003d5d531cbSDavid Ahern IPV6_ADDR_MAPPED))) { 2004d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2005d5d531cbSDavid Ahern "Invalid gateway address"); 20061da177e4SLinus Torvalds goto out; 2007d5d531cbSDavid Ahern } 20081da177e4SLinus Torvalds 2009a435a07fSVincent Bernat if (cfg->fc_table) { 20108c14586fSDavid Ahern grt = ip6_nh_lookup_table(net, cfg, gw_addr); 20118c14586fSDavid Ahern 2012a435a07fSVincent Bernat if (grt) { 2013a435a07fSVincent Bernat if (grt->rt6i_flags & RTF_GATEWAY || 2014a435a07fSVincent Bernat (dev && dev != grt->dst.dev)) { 2015a435a07fSVincent Bernat ip6_rt_put(grt); 2016a435a07fSVincent Bernat grt = NULL; 2017a435a07fSVincent Bernat } 2018a435a07fSVincent Bernat } 2019a435a07fSVincent Bernat } 2020a435a07fSVincent Bernat 20218c14586fSDavid Ahern if (!grt) 20228c14586fSDavid Ahern grt = rt6_lookup(net, gw_addr, NULL, 20238c14586fSDavid Ahern cfg->fc_ifindex, 1); 20241da177e4SLinus Torvalds 20251da177e4SLinus Torvalds err = -EHOSTUNREACH; 202638308473SDavid S. Miller if (!grt) 20271da177e4SLinus Torvalds goto out; 20281da177e4SLinus Torvalds if (dev) { 2029d1918542SDavid S. Miller if (dev != grt->dst.dev) { 203094e187c0SAmerigo Wang ip6_rt_put(grt); 20311da177e4SLinus Torvalds goto out; 20321da177e4SLinus Torvalds } 20331da177e4SLinus Torvalds } else { 2034d1918542SDavid S. Miller dev = grt->dst.dev; 20351da177e4SLinus Torvalds idev = grt->rt6i_idev; 20361da177e4SLinus Torvalds dev_hold(dev); 20371da177e4SLinus Torvalds in6_dev_hold(grt->rt6i_idev); 20381da177e4SLinus Torvalds } 20391da177e4SLinus Torvalds if (!(grt->rt6i_flags & RTF_GATEWAY)) 20401da177e4SLinus Torvalds err = 0; 204194e187c0SAmerigo Wang ip6_rt_put(grt); 20421da177e4SLinus Torvalds 20431da177e4SLinus Torvalds if (err) 20441da177e4SLinus Torvalds goto out; 20451da177e4SLinus Torvalds } 20461da177e4SLinus Torvalds err = -EINVAL; 2047d5d531cbSDavid Ahern if (!dev) { 2048d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Egress device not specified"); 20491da177e4SLinus Torvalds goto out; 2050d5d531cbSDavid Ahern } else if (dev->flags & IFF_LOOPBACK) { 2051d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2052d5d531cbSDavid Ahern "Egress device can not be loopback device for this route"); 2053d5d531cbSDavid Ahern goto out; 2054d5d531cbSDavid Ahern } 20551da177e4SLinus Torvalds } 20561da177e4SLinus Torvalds 20571da177e4SLinus Torvalds err = -ENODEV; 205838308473SDavid S. Miller if (!dev) 20591da177e4SLinus Torvalds goto out; 20601da177e4SLinus Torvalds 2061c3968a85SDaniel Walter if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 2062c3968a85SDaniel Walter if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 2063d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address"); 2064c3968a85SDaniel Walter err = -EINVAL; 2065c3968a85SDaniel Walter goto out; 2066c3968a85SDaniel Walter } 20674e3fd7a0SAlexey Dobriyan rt->rt6i_prefsrc.addr = cfg->fc_prefsrc; 2068c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 128; 2069c3968a85SDaniel Walter } else 2070c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 2071c3968a85SDaniel Walter 207286872cb5SThomas Graf rt->rt6i_flags = cfg->fc_flags; 20731da177e4SLinus Torvalds 20741da177e4SLinus Torvalds install_route: 2075d8d1f30bSChangli Gao rt->dst.dev = dev; 20761da177e4SLinus Torvalds rt->rt6i_idev = idev; 2077c71099acSThomas Graf rt->rt6i_table = table; 207863152fc0SDaniel Lezcano 2079c346dca1SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = dev_net(dev); 208063152fc0SDaniel Lezcano 20818c5b83f0SRoopa Prabhu return rt; 20821da177e4SLinus Torvalds out: 20831da177e4SLinus Torvalds if (dev) 20841da177e4SLinus Torvalds dev_put(dev); 20851da177e4SLinus Torvalds if (idev) 20861da177e4SLinus Torvalds in6_dev_put(idev); 2087587fea74SWei Wang if (rt) 2088587fea74SWei Wang dst_release_immediate(&rt->dst); 20896b9ea5a6SRoopa Prabhu 20908c5b83f0SRoopa Prabhu return ERR_PTR(err); 20916b9ea5a6SRoopa Prabhu } 20926b9ea5a6SRoopa Prabhu 2093333c4301SDavid Ahern int ip6_route_add(struct fib6_config *cfg, 2094333c4301SDavid Ahern struct netlink_ext_ack *extack) 20956b9ea5a6SRoopa Prabhu { 20966b9ea5a6SRoopa Prabhu struct mx6_config mxc = { .mx = NULL, }; 20978c5b83f0SRoopa Prabhu struct rt6_info *rt; 20986b9ea5a6SRoopa Prabhu int err; 20996b9ea5a6SRoopa Prabhu 2100333c4301SDavid Ahern rt = ip6_route_info_create(cfg, extack); 21018c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 21028c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 21038c5b83f0SRoopa Prabhu rt = NULL; 21046b9ea5a6SRoopa Prabhu goto out; 21058c5b83f0SRoopa Prabhu } 21066b9ea5a6SRoopa Prabhu 21076b9ea5a6SRoopa Prabhu err = ip6_convert_metrics(&mxc, cfg); 21086b9ea5a6SRoopa Prabhu if (err) 21096b9ea5a6SRoopa Prabhu goto out; 21106b9ea5a6SRoopa Prabhu 2111333c4301SDavid Ahern err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, &mxc, extack); 21126b9ea5a6SRoopa Prabhu 21136b9ea5a6SRoopa Prabhu kfree(mxc.mx); 21146b9ea5a6SRoopa Prabhu 21156b9ea5a6SRoopa Prabhu return err; 21166b9ea5a6SRoopa Prabhu out: 2117587fea74SWei Wang if (rt) 2118587fea74SWei Wang dst_release_immediate(&rt->dst); 21196b9ea5a6SRoopa Prabhu 21201da177e4SLinus Torvalds return err; 21211da177e4SLinus Torvalds } 21221da177e4SLinus Torvalds 212386872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info) 21241da177e4SLinus Torvalds { 21251da177e4SLinus Torvalds int err; 2126c71099acSThomas Graf struct fib6_table *table; 2127d1918542SDavid S. Miller struct net *net = dev_net(rt->dst.dev); 21281da177e4SLinus Torvalds 2129a4c2fd7fSWei Wang if (rt == net->ipv6.ip6_null_entry) { 21306825a26cSGao feng err = -ENOENT; 21316825a26cSGao feng goto out; 21326825a26cSGao feng } 21336c813a72SPatrick McHardy 2134c71099acSThomas Graf table = rt->rt6i_table; 2135c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 213686872cb5SThomas Graf err = fib6_del(rt, info); 2137c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 21381da177e4SLinus Torvalds 21396825a26cSGao feng out: 214094e187c0SAmerigo Wang ip6_rt_put(rt); 21411da177e4SLinus Torvalds return err; 21421da177e4SLinus Torvalds } 21431da177e4SLinus Torvalds 2144e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt) 2145e0a1ad73SThomas Graf { 21464d1169c1SDenis V. Lunev struct nl_info info = { 2147d1918542SDavid S. Miller .nl_net = dev_net(rt->dst.dev), 21484d1169c1SDenis V. Lunev }; 2149528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 2150e0a1ad73SThomas Graf } 2151e0a1ad73SThomas Graf 21520ae81335SDavid Ahern static int __ip6_del_rt_siblings(struct rt6_info *rt, struct fib6_config *cfg) 21530ae81335SDavid Ahern { 21540ae81335SDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 2155e3330039SWANG Cong struct net *net = info->nl_net; 215616a16cd3SDavid Ahern struct sk_buff *skb = NULL; 21570ae81335SDavid Ahern struct fib6_table *table; 2158e3330039SWANG Cong int err = -ENOENT; 21590ae81335SDavid Ahern 2160e3330039SWANG Cong if (rt == net->ipv6.ip6_null_entry) 2161e3330039SWANG Cong goto out_put; 21620ae81335SDavid Ahern table = rt->rt6i_table; 21630ae81335SDavid Ahern write_lock_bh(&table->tb6_lock); 21640ae81335SDavid Ahern 21650ae81335SDavid Ahern if (rt->rt6i_nsiblings && cfg->fc_delete_all_nh) { 21660ae81335SDavid Ahern struct rt6_info *sibling, *next_sibling; 21670ae81335SDavid Ahern 216816a16cd3SDavid Ahern /* prefer to send a single notification with all hops */ 216916a16cd3SDavid Ahern skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 217016a16cd3SDavid Ahern if (skb) { 217116a16cd3SDavid Ahern u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 217216a16cd3SDavid Ahern 2173e3330039SWANG Cong if (rt6_fill_node(net, skb, rt, 217416a16cd3SDavid Ahern NULL, NULL, 0, RTM_DELROUTE, 217516a16cd3SDavid Ahern info->portid, seq, 0) < 0) { 217616a16cd3SDavid Ahern kfree_skb(skb); 217716a16cd3SDavid Ahern skb = NULL; 217816a16cd3SDavid Ahern } else 217916a16cd3SDavid Ahern info->skip_notify = 1; 218016a16cd3SDavid Ahern } 218116a16cd3SDavid Ahern 21820ae81335SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 21830ae81335SDavid Ahern &rt->rt6i_siblings, 21840ae81335SDavid Ahern rt6i_siblings) { 21850ae81335SDavid Ahern err = fib6_del(sibling, info); 21860ae81335SDavid Ahern if (err) 2187e3330039SWANG Cong goto out_unlock; 21880ae81335SDavid Ahern } 21890ae81335SDavid Ahern } 21900ae81335SDavid Ahern 21910ae81335SDavid Ahern err = fib6_del(rt, info); 2192e3330039SWANG Cong out_unlock: 21930ae81335SDavid Ahern write_unlock_bh(&table->tb6_lock); 2194e3330039SWANG Cong out_put: 21950ae81335SDavid Ahern ip6_rt_put(rt); 219616a16cd3SDavid Ahern 219716a16cd3SDavid Ahern if (skb) { 2198e3330039SWANG Cong rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 219916a16cd3SDavid Ahern info->nlh, gfp_any()); 220016a16cd3SDavid Ahern } 22010ae81335SDavid Ahern return err; 22020ae81335SDavid Ahern } 22030ae81335SDavid Ahern 2204333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg, 2205333c4301SDavid Ahern struct netlink_ext_ack *extack) 22061da177e4SLinus Torvalds { 2207c71099acSThomas Graf struct fib6_table *table; 22081da177e4SLinus Torvalds struct fib6_node *fn; 22091da177e4SLinus Torvalds struct rt6_info *rt; 22101da177e4SLinus Torvalds int err = -ESRCH; 22111da177e4SLinus Torvalds 22125578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 2213d5d531cbSDavid Ahern if (!table) { 2214d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "FIB table does not exist"); 2215c71099acSThomas Graf return err; 2216d5d531cbSDavid Ahern } 22171da177e4SLinus Torvalds 2218c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 2219c71099acSThomas Graf 2220c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 222186872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 222286872cb5SThomas Graf &cfg->fc_src, cfg->fc_src_len); 22231da177e4SLinus Torvalds 22241da177e4SLinus Torvalds if (fn) { 2225d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 22261f56a01fSMartin KaFai Lau if ((rt->rt6i_flags & RTF_CACHE) && 22271f56a01fSMartin KaFai Lau !(cfg->fc_flags & RTF_CACHE)) 22281f56a01fSMartin KaFai Lau continue; 222986872cb5SThomas Graf if (cfg->fc_ifindex && 2230d1918542SDavid S. Miller (!rt->dst.dev || 2231d1918542SDavid S. Miller rt->dst.dev->ifindex != cfg->fc_ifindex)) 22321da177e4SLinus Torvalds continue; 223386872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 223486872cb5SThomas Graf !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 22351da177e4SLinus Torvalds continue; 223686872cb5SThomas Graf if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric) 22371da177e4SLinus Torvalds continue; 2238c2ed1880SMantas M if (cfg->fc_protocol && cfg->fc_protocol != rt->rt6i_protocol) 2239c2ed1880SMantas M continue; 2240d8d1f30bSChangli Gao dst_hold(&rt->dst); 2241c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 22421da177e4SLinus Torvalds 22430ae81335SDavid Ahern /* if gateway was specified only delete the one hop */ 22440ae81335SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY) 224586872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 22460ae81335SDavid Ahern 22470ae81335SDavid Ahern return __ip6_del_rt_siblings(rt, cfg); 22481da177e4SLinus Torvalds } 22491da177e4SLinus Torvalds } 2250c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 22511da177e4SLinus Torvalds 22521da177e4SLinus Torvalds return err; 22531da177e4SLinus Torvalds } 22541da177e4SLinus Torvalds 22556700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 2256a6279458SYOSHIFUJI Hideaki { 2257a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 2258e8599ff4SDavid S. Miller struct rt6_info *rt, *nrt = NULL; 2259e8599ff4SDavid S. Miller struct ndisc_options ndopts; 2260e8599ff4SDavid S. Miller struct inet6_dev *in6_dev; 2261e8599ff4SDavid S. Miller struct neighbour *neigh; 226271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 struct rd_msg *msg; 22636e157b6aSDavid S. Miller int optlen, on_link; 22646e157b6aSDavid S. Miller u8 *lladdr; 2265e8599ff4SDavid S. Miller 226629a3cad5SSimon Horman optlen = skb_tail_pointer(skb) - skb_transport_header(skb); 226771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 optlen -= sizeof(*msg); 2268e8599ff4SDavid S. Miller 2269e8599ff4SDavid S. Miller if (optlen < 0) { 22706e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); 2271e8599ff4SDavid S. Miller return; 2272e8599ff4SDavid S. Miller } 2273e8599ff4SDavid S. Miller 227471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 msg = (struct rd_msg *)icmp6_hdr(skb); 2275e8599ff4SDavid S. Miller 227671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_is_multicast(&msg->dest)) { 22776e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); 2278e8599ff4SDavid S. Miller return; 2279e8599ff4SDavid S. Miller } 2280e8599ff4SDavid S. Miller 22816e157b6aSDavid S. Miller on_link = 0; 228271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_equal(&msg->dest, &msg->target)) { 2283e8599ff4SDavid S. Miller on_link = 1; 228471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 } else if (ipv6_addr_type(&msg->target) != 2285e8599ff4SDavid S. Miller (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { 22866e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); 2287e8599ff4SDavid S. Miller return; 2288e8599ff4SDavid S. Miller } 2289e8599ff4SDavid S. Miller 2290e8599ff4SDavid S. Miller in6_dev = __in6_dev_get(skb->dev); 2291e8599ff4SDavid S. Miller if (!in6_dev) 2292e8599ff4SDavid S. Miller return; 2293e8599ff4SDavid S. Miller if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) 2294e8599ff4SDavid S. Miller return; 2295e8599ff4SDavid S. Miller 2296e8599ff4SDavid S. Miller /* RFC2461 8.1: 2297e8599ff4SDavid S. Miller * The IP source address of the Redirect MUST be the same as the current 2298e8599ff4SDavid S. Miller * first-hop router for the specified ICMP Destination Address. 2299e8599ff4SDavid S. Miller */ 2300e8599ff4SDavid S. Miller 2301f997c55cSAlexander Aring if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { 2302e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); 2303e8599ff4SDavid S. Miller return; 2304e8599ff4SDavid S. Miller } 23056e157b6aSDavid S. Miller 23066e157b6aSDavid S. Miller lladdr = NULL; 2307e8599ff4SDavid S. Miller if (ndopts.nd_opts_tgt_lladdr) { 2308e8599ff4SDavid S. Miller lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, 2309e8599ff4SDavid S. Miller skb->dev); 2310e8599ff4SDavid S. Miller if (!lladdr) { 2311e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); 2312e8599ff4SDavid S. Miller return; 2313e8599ff4SDavid S. Miller } 2314e8599ff4SDavid S. Miller } 2315e8599ff4SDavid S. Miller 23166e157b6aSDavid S. Miller rt = (struct rt6_info *) dst; 2317ec13ad1dSMatthias Schiffer if (rt->rt6i_flags & RTF_REJECT) { 23186e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); 23196e157b6aSDavid S. Miller return; 23206e157b6aSDavid S. Miller } 23216e157b6aSDavid S. Miller 23226e157b6aSDavid S. Miller /* Redirect received -> path was valid. 23236e157b6aSDavid S. Miller * Look, redirects are sent only in response to data packets, 23246e157b6aSDavid S. Miller * so that this nexthop apparently is reachable. --ANK 23256e157b6aSDavid S. Miller */ 23260dec879fSJulian Anastasov dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); 23276e157b6aSDavid S. Miller 232871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); 2329e8599ff4SDavid S. Miller if (!neigh) 2330e8599ff4SDavid S. Miller return; 2331e8599ff4SDavid S. Miller 23321da177e4SLinus Torvalds /* 23331da177e4SLinus Torvalds * We have finally decided to accept it. 23341da177e4SLinus Torvalds */ 23351da177e4SLinus Torvalds 2336f997c55cSAlexander Aring ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, 23371da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 23381da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 23391da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 2340f997c55cSAlexander Aring NEIGH_UPDATE_F_ISROUTER)), 2341f997c55cSAlexander Aring NDISC_REDIRECT, &ndopts); 23421da177e4SLinus Torvalds 234383a09abdSMartin KaFai Lau nrt = ip6_rt_cache_alloc(rt, &msg->dest, NULL); 234438308473SDavid S. Miller if (!nrt) 23451da177e4SLinus Torvalds goto out; 23461da177e4SLinus Torvalds 23471da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 23481da177e4SLinus Torvalds if (on_link) 23491da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 23501da177e4SLinus Torvalds 2351b91d5329SXin Long nrt->rt6i_protocol = RTPROT_REDIRECT; 23524e3fd7a0SAlexey Dobriyan nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 23531da177e4SLinus Torvalds 235440e22e8fSThomas Graf if (ip6_ins_rt(nrt)) 23551cfb71eeSWei Wang goto out_release; 23561da177e4SLinus Torvalds 2357d8d1f30bSChangli Gao netevent.old = &rt->dst; 2358d8d1f30bSChangli Gao netevent.new = &nrt->dst; 235971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 netevent.daddr = &msg->dest; 236060592833SYOSHIFUJI Hideaki / 吉藤英明 netevent.neigh = neigh; 23618d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 23628d71740cSTom Tucker 23631da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) { 23646e157b6aSDavid S. Miller rt = (struct rt6_info *) dst_clone(&rt->dst); 2365e0a1ad73SThomas Graf ip6_del_rt(rt); 23661da177e4SLinus Torvalds } 23671da177e4SLinus Torvalds 23681cfb71eeSWei Wang out_release: 23691cfb71eeSWei Wang /* Release the reference taken in 23701cfb71eeSWei Wang * ip6_rt_cache_alloc() 23711cfb71eeSWei Wang */ 23721cfb71eeSWei Wang dst_release(&nrt->dst); 23731cfb71eeSWei Wang 23741da177e4SLinus Torvalds out: 2375e8599ff4SDavid S. Miller neigh_release(neigh); 23766e157b6aSDavid S. Miller } 23776e157b6aSDavid S. Miller 23781da177e4SLinus Torvalds /* 23791da177e4SLinus Torvalds * Misc support functions 23801da177e4SLinus Torvalds */ 23811da177e4SLinus Torvalds 23824b32b5adSMartin KaFai Lau static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from) 23834b32b5adSMartin KaFai Lau { 23844b32b5adSMartin KaFai Lau BUG_ON(from->dst.from); 23854b32b5adSMartin KaFai Lau 23864b32b5adSMartin KaFai Lau rt->rt6i_flags &= ~RTF_EXPIRES; 23874b32b5adSMartin KaFai Lau dst_hold(&from->dst); 23884b32b5adSMartin KaFai Lau rt->dst.from = &from->dst; 23894b32b5adSMartin KaFai Lau dst_init_metrics(&rt->dst, dst_metrics_ptr(&from->dst), true); 23904b32b5adSMartin KaFai Lau } 23914b32b5adSMartin KaFai Lau 239283a09abdSMartin KaFai Lau static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort) 23931da177e4SLinus Torvalds { 2394d8d1f30bSChangli Gao rt->dst.input = ort->dst.input; 2395d8d1f30bSChangli Gao rt->dst.output = ort->dst.output; 239683a09abdSMartin KaFai Lau rt->rt6i_dst = ort->rt6i_dst; 2397d8d1f30bSChangli Gao rt->dst.error = ort->dst.error; 23981da177e4SLinus Torvalds rt->rt6i_idev = ort->rt6i_idev; 23991da177e4SLinus Torvalds if (rt->rt6i_idev) 24001da177e4SLinus Torvalds in6_dev_hold(rt->rt6i_idev); 2401d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 24024e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 24031716a961SGao feng rt->rt6i_flags = ort->rt6i_flags; 24041716a961SGao feng rt6_set_from(rt, ort); 240583a09abdSMartin KaFai Lau rt->rt6i_metric = ort->rt6i_metric; 24061da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 240783a09abdSMartin KaFai Lau rt->rt6i_src = ort->rt6i_src; 24081da177e4SLinus Torvalds #endif 240983a09abdSMartin KaFai Lau rt->rt6i_prefsrc = ort->rt6i_prefsrc; 2410c71099acSThomas Graf rt->rt6i_table = ort->rt6i_table; 241161adedf3SJiri Benc rt->dst.lwtstate = lwtstate_get(ort->dst.lwtstate); 24121da177e4SLinus Torvalds } 24131da177e4SLinus Torvalds 241470ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 2415efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 2416b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 2417830218c1SDavid Ahern const struct in6_addr *gwaddr, 2418830218c1SDavid Ahern struct net_device *dev) 241970ceb4f5SYOSHIFUJI Hideaki { 2420830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; 2421830218c1SDavid Ahern int ifindex = dev->ifindex; 242270ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 242370ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt = NULL; 2424c71099acSThomas Graf struct fib6_table *table; 242570ceb4f5SYOSHIFUJI Hideaki 2426830218c1SDavid Ahern table = fib6_get_table(net, tb_id); 242738308473SDavid S. Miller if (!table) 2428c71099acSThomas Graf return NULL; 2429c71099acSThomas Graf 24305744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 2431c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0); 243270ceb4f5SYOSHIFUJI Hideaki if (!fn) 243370ceb4f5SYOSHIFUJI Hideaki goto out; 243470ceb4f5SYOSHIFUJI Hideaki 2435d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 2436d1918542SDavid S. Miller if (rt->dst.dev->ifindex != ifindex) 243770ceb4f5SYOSHIFUJI Hideaki continue; 243870ceb4f5SYOSHIFUJI Hideaki if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) 243970ceb4f5SYOSHIFUJI Hideaki continue; 244070ceb4f5SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr)) 244170ceb4f5SYOSHIFUJI Hideaki continue; 2442d8d1f30bSChangli Gao dst_hold(&rt->dst); 244370ceb4f5SYOSHIFUJI Hideaki break; 244470ceb4f5SYOSHIFUJI Hideaki } 244570ceb4f5SYOSHIFUJI Hideaki out: 24465744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 244770ceb4f5SYOSHIFUJI Hideaki return rt; 244870ceb4f5SYOSHIFUJI Hideaki } 244970ceb4f5SYOSHIFUJI Hideaki 2450efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 2451b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 2452830218c1SDavid Ahern const struct in6_addr *gwaddr, 2453830218c1SDavid Ahern struct net_device *dev, 245495c96174SEric Dumazet unsigned int pref) 245570ceb4f5SYOSHIFUJI Hideaki { 245686872cb5SThomas Graf struct fib6_config cfg = { 2457238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 2458830218c1SDavid Ahern .fc_ifindex = dev->ifindex, 245986872cb5SThomas Graf .fc_dst_len = prefixlen, 246086872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 246186872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 2462b91d5329SXin Long .fc_protocol = RTPROT_RA, 246315e47304SEric W. Biederman .fc_nlinfo.portid = 0, 2464efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 2465efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 246686872cb5SThomas Graf }; 246770ceb4f5SYOSHIFUJI Hideaki 2468830218c1SDavid Ahern cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO, 24694e3fd7a0SAlexey Dobriyan cfg.fc_dst = *prefix; 24704e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 247186872cb5SThomas Graf 2472e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 2473e317da96SYOSHIFUJI Hideaki if (!prefixlen) 247486872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 247570ceb4f5SYOSHIFUJI Hideaki 2476333c4301SDavid Ahern ip6_route_add(&cfg, NULL); 247770ceb4f5SYOSHIFUJI Hideaki 2478830218c1SDavid Ahern return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); 247970ceb4f5SYOSHIFUJI Hideaki } 248070ceb4f5SYOSHIFUJI Hideaki #endif 248170ceb4f5SYOSHIFUJI Hideaki 2482b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev) 24831da177e4SLinus Torvalds { 2484830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; 24851da177e4SLinus Torvalds struct rt6_info *rt; 2486c71099acSThomas Graf struct fib6_table *table; 24871da177e4SLinus Torvalds 2488830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), tb_id); 248938308473SDavid S. Miller if (!table) 2490c71099acSThomas Graf return NULL; 24911da177e4SLinus Torvalds 24925744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 2493d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 2494d1918542SDavid S. Miller if (dev == rt->dst.dev && 2495045927ffSYOSHIFUJI Hideaki ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 24961da177e4SLinus Torvalds ipv6_addr_equal(&rt->rt6i_gateway, addr)) 24971da177e4SLinus Torvalds break; 24981da177e4SLinus Torvalds } 24991da177e4SLinus Torvalds if (rt) 2500d8d1f30bSChangli Gao dst_hold(&rt->dst); 25015744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 25021da177e4SLinus Torvalds return rt; 25031da177e4SLinus Torvalds } 25041da177e4SLinus Torvalds 2505b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr, 2506ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 2507ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 25081da177e4SLinus Torvalds { 250986872cb5SThomas Graf struct fib6_config cfg = { 2510ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, 2511238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 251286872cb5SThomas Graf .fc_ifindex = dev->ifindex, 251386872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 251486872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 2515b91d5329SXin Long .fc_protocol = RTPROT_RA, 251615e47304SEric W. Biederman .fc_nlinfo.portid = 0, 25175578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 2518c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 251986872cb5SThomas Graf }; 25201da177e4SLinus Torvalds 25214e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 25221da177e4SLinus Torvalds 2523333c4301SDavid Ahern if (!ip6_route_add(&cfg, NULL)) { 2524830218c1SDavid Ahern struct fib6_table *table; 2525830218c1SDavid Ahern 2526830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), cfg.fc_table); 2527830218c1SDavid Ahern if (table) 2528830218c1SDavid Ahern table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; 2529830218c1SDavid Ahern } 25301da177e4SLinus Torvalds 25311da177e4SLinus Torvalds return rt6_get_dflt_router(gwaddr, dev); 25321da177e4SLinus Torvalds } 25331da177e4SLinus Torvalds 2534830218c1SDavid Ahern static void __rt6_purge_dflt_routers(struct fib6_table *table) 25351da177e4SLinus Torvalds { 25361da177e4SLinus Torvalds struct rt6_info *rt; 25371da177e4SLinus Torvalds 25381da177e4SLinus Torvalds restart: 2539c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 2540d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 25413e8b0ac3SLorenzo Colitti if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) && 25423e8b0ac3SLorenzo Colitti (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) { 2543d8d1f30bSChangli Gao dst_hold(&rt->dst); 2544c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 2545e0a1ad73SThomas Graf ip6_del_rt(rt); 25461da177e4SLinus Torvalds goto restart; 25471da177e4SLinus Torvalds } 25481da177e4SLinus Torvalds } 2549c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 2550830218c1SDavid Ahern 2551830218c1SDavid Ahern table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; 2552830218c1SDavid Ahern } 2553830218c1SDavid Ahern 2554830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net) 2555830218c1SDavid Ahern { 2556830218c1SDavid Ahern struct fib6_table *table; 2557830218c1SDavid Ahern struct hlist_head *head; 2558830218c1SDavid Ahern unsigned int h; 2559830218c1SDavid Ahern 2560830218c1SDavid Ahern rcu_read_lock(); 2561830218c1SDavid Ahern 2562830218c1SDavid Ahern for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { 2563830218c1SDavid Ahern head = &net->ipv6.fib_table_hash[h]; 2564830218c1SDavid Ahern hlist_for_each_entry_rcu(table, head, tb6_hlist) { 2565830218c1SDavid Ahern if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) 2566830218c1SDavid Ahern __rt6_purge_dflt_routers(table); 2567830218c1SDavid Ahern } 2568830218c1SDavid Ahern } 2569830218c1SDavid Ahern 2570830218c1SDavid Ahern rcu_read_unlock(); 25711da177e4SLinus Torvalds } 25721da177e4SLinus Torvalds 25735578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 25745578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 257586872cb5SThomas Graf struct fib6_config *cfg) 257686872cb5SThomas Graf { 257786872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 257886872cb5SThomas Graf 2579ca254490SDavid Ahern cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? 2580ca254490SDavid Ahern : RT6_TABLE_MAIN; 258186872cb5SThomas Graf cfg->fc_ifindex = rtmsg->rtmsg_ifindex; 258286872cb5SThomas Graf cfg->fc_metric = rtmsg->rtmsg_metric; 258386872cb5SThomas Graf cfg->fc_expires = rtmsg->rtmsg_info; 258486872cb5SThomas Graf cfg->fc_dst_len = rtmsg->rtmsg_dst_len; 258586872cb5SThomas Graf cfg->fc_src_len = rtmsg->rtmsg_src_len; 258686872cb5SThomas Graf cfg->fc_flags = rtmsg->rtmsg_flags; 258786872cb5SThomas Graf 25885578689aSDaniel Lezcano cfg->fc_nlinfo.nl_net = net; 2589f1243c2dSBenjamin Thery 25904e3fd7a0SAlexey Dobriyan cfg->fc_dst = rtmsg->rtmsg_dst; 25914e3fd7a0SAlexey Dobriyan cfg->fc_src = rtmsg->rtmsg_src; 25924e3fd7a0SAlexey Dobriyan cfg->fc_gateway = rtmsg->rtmsg_gateway; 259386872cb5SThomas Graf } 259486872cb5SThomas Graf 25955578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) 25961da177e4SLinus Torvalds { 259786872cb5SThomas Graf struct fib6_config cfg; 25981da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 25991da177e4SLinus Torvalds int err; 26001da177e4SLinus Torvalds 26011da177e4SLinus Torvalds switch (cmd) { 26021da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 26031da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 2604af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 26051da177e4SLinus Torvalds return -EPERM; 26061da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 26071da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 26081da177e4SLinus Torvalds if (err) 26091da177e4SLinus Torvalds return -EFAULT; 26101da177e4SLinus Torvalds 26115578689aSDaniel Lezcano rtmsg_to_fib6_config(net, &rtmsg, &cfg); 261286872cb5SThomas Graf 26131da177e4SLinus Torvalds rtnl_lock(); 26141da177e4SLinus Torvalds switch (cmd) { 26151da177e4SLinus Torvalds case SIOCADDRT: 2616333c4301SDavid Ahern err = ip6_route_add(&cfg, NULL); 26171da177e4SLinus Torvalds break; 26181da177e4SLinus Torvalds case SIOCDELRT: 2619333c4301SDavid Ahern err = ip6_route_del(&cfg, NULL); 26201da177e4SLinus Torvalds break; 26211da177e4SLinus Torvalds default: 26221da177e4SLinus Torvalds err = -EINVAL; 26231da177e4SLinus Torvalds } 26241da177e4SLinus Torvalds rtnl_unlock(); 26251da177e4SLinus Torvalds 26261da177e4SLinus Torvalds return err; 26273ff50b79SStephen Hemminger } 26281da177e4SLinus Torvalds 26291da177e4SLinus Torvalds return -EINVAL; 26301da177e4SLinus Torvalds } 26311da177e4SLinus Torvalds 26321da177e4SLinus Torvalds /* 26331da177e4SLinus Torvalds * Drop the packet on the floor 26341da177e4SLinus Torvalds */ 26351da177e4SLinus Torvalds 2636d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 26371da177e4SLinus Torvalds { 2638612f09e8SYOSHIFUJI Hideaki int type; 2639adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 2640612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 2641612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 26420660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 264345bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 26443bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 26453bd653c8SDenis V. Lunev IPSTATS_MIB_INADDRERRORS); 2646612f09e8SYOSHIFUJI Hideaki break; 2647612f09e8SYOSHIFUJI Hideaki } 2648612f09e8SYOSHIFUJI Hideaki /* FALLTHROUGH */ 2649612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 26503bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 26513bd653c8SDenis V. Lunev ipstats_mib_noroutes); 2652612f09e8SYOSHIFUJI Hideaki break; 2653612f09e8SYOSHIFUJI Hideaki } 26543ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 26551da177e4SLinus Torvalds kfree_skb(skb); 26561da177e4SLinus Torvalds return 0; 26571da177e4SLinus Torvalds } 26581da177e4SLinus Torvalds 26599ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 26609ce8ade0SThomas Graf { 2661612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 26629ce8ade0SThomas Graf } 26639ce8ade0SThomas Graf 2664ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb) 26651da177e4SLinus Torvalds { 2666adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 2667612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 26681da177e4SLinus Torvalds } 26691da177e4SLinus Torvalds 26709ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 26719ce8ade0SThomas Graf { 2672612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 26739ce8ade0SThomas Graf } 26749ce8ade0SThomas Graf 2675ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb) 26769ce8ade0SThomas Graf { 2677adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 2678612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 26799ce8ade0SThomas Graf } 26809ce8ade0SThomas Graf 26811da177e4SLinus Torvalds /* 26821da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 26831da177e4SLinus Torvalds */ 26841da177e4SLinus Torvalds 26851da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, 26861da177e4SLinus Torvalds const struct in6_addr *addr, 26878f031519SDavid S. Miller bool anycast) 26881da177e4SLinus Torvalds { 2689ca254490SDavid Ahern u32 tb_id; 2690c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 26915f02ce24SDavid Ahern struct net_device *dev = net->loopback_dev; 26925f02ce24SDavid Ahern struct rt6_info *rt; 26935f02ce24SDavid Ahern 26945f02ce24SDavid Ahern /* use L3 Master device as loopback for host routes if device 26955f02ce24SDavid Ahern * is enslaved and address is not link local or multicast 26965f02ce24SDavid Ahern */ 26975f02ce24SDavid Ahern if (!rt6_need_strict(addr)) 26985f02ce24SDavid Ahern dev = l3mdev_master_dev_rcu(idev->dev) ? : dev; 26995f02ce24SDavid Ahern 27005f02ce24SDavid Ahern rt = ip6_dst_alloc(net, dev, DST_NOCOUNT); 2701a3300ef4SHannes Frederic Sowa if (!rt) 27021da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 27031da177e4SLinus Torvalds 27041da177e4SLinus Torvalds in6_dev_hold(idev); 27051da177e4SLinus Torvalds 270611d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 2707d8d1f30bSChangli Gao rt->dst.input = ip6_input; 2708d8d1f30bSChangli Gao rt->dst.output = ip6_output; 27091da177e4SLinus Torvalds rt->rt6i_idev = idev; 27101da177e4SLinus Torvalds 271194b5e0f9SDavid Ahern rt->rt6i_protocol = RTPROT_KERNEL; 27121da177e4SLinus Torvalds rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP; 271358c4fb86SYOSHIFUJI Hideaki if (anycast) 271458c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 271558c4fb86SYOSHIFUJI Hideaki else 27161da177e4SLinus Torvalds rt->rt6i_flags |= RTF_LOCAL; 27171da177e4SLinus Torvalds 2718550bab42SJulian Anastasov rt->rt6i_gateway = *addr; 27194e3fd7a0SAlexey Dobriyan rt->rt6i_dst.addr = *addr; 27201da177e4SLinus Torvalds rt->rt6i_dst.plen = 128; 2721ca254490SDavid Ahern tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL; 2722ca254490SDavid Ahern rt->rt6i_table = fib6_get_table(net, tb_id); 27231da177e4SLinus Torvalds 27241da177e4SLinus Torvalds return rt; 27251da177e4SLinus Torvalds } 27261da177e4SLinus Torvalds 2727c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 2728c3968a85SDaniel Walter struct arg_dev_net_ip { 2729c3968a85SDaniel Walter struct net_device *dev; 2730c3968a85SDaniel Walter struct net *net; 2731c3968a85SDaniel Walter struct in6_addr *addr; 2732c3968a85SDaniel Walter }; 2733c3968a85SDaniel Walter 2734c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg) 2735c3968a85SDaniel Walter { 2736c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 2737c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 2738c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 2739c3968a85SDaniel Walter 2740d1918542SDavid S. Miller if (((void *)rt->dst.dev == dev || !dev) && 2741c3968a85SDaniel Walter rt != net->ipv6.ip6_null_entry && 2742c3968a85SDaniel Walter ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) { 2743c3968a85SDaniel Walter /* remove prefsrc entry */ 2744c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 2745c3968a85SDaniel Walter } 2746c3968a85SDaniel Walter return 0; 2747c3968a85SDaniel Walter } 2748c3968a85SDaniel Walter 2749c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 2750c3968a85SDaniel Walter { 2751c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 2752c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 2753c3968a85SDaniel Walter .dev = ifp->idev->dev, 2754c3968a85SDaniel Walter .net = net, 2755c3968a85SDaniel Walter .addr = &ifp->addr, 2756c3968a85SDaniel Walter }; 27570c3584d5SLi RongQing fib6_clean_all(net, fib6_remove_prefsrc, &adni); 2758c3968a85SDaniel Walter } 2759c3968a85SDaniel Walter 2760be7a010dSDuan Jiong #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY) 2761be7a010dSDuan Jiong #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) 2762be7a010dSDuan Jiong 2763be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */ 2764be7a010dSDuan Jiong static int fib6_clean_tohost(struct rt6_info *rt, void *arg) 2765be7a010dSDuan Jiong { 2766be7a010dSDuan Jiong struct in6_addr *gateway = (struct in6_addr *)arg; 2767be7a010dSDuan Jiong 2768be7a010dSDuan Jiong if ((((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) || 2769be7a010dSDuan Jiong ((rt->rt6i_flags & RTF_CACHE_GATEWAY) == RTF_CACHE_GATEWAY)) && 2770be7a010dSDuan Jiong ipv6_addr_equal(gateway, &rt->rt6i_gateway)) { 2771be7a010dSDuan Jiong return -1; 2772be7a010dSDuan Jiong } 2773be7a010dSDuan Jiong return 0; 2774be7a010dSDuan Jiong } 2775be7a010dSDuan Jiong 2776be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) 2777be7a010dSDuan Jiong { 2778be7a010dSDuan Jiong fib6_clean_all(net, fib6_clean_tohost, gateway); 2779be7a010dSDuan Jiong } 2780be7a010dSDuan Jiong 27818ed67789SDaniel Lezcano struct arg_dev_net { 27828ed67789SDaniel Lezcano struct net_device *dev; 27838ed67789SDaniel Lezcano struct net *net; 27848ed67789SDaniel Lezcano }; 27858ed67789SDaniel Lezcano 2786a1a22c12SDavid Ahern /* called with write lock held for table with rt */ 27871da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg) 27881da177e4SLinus Torvalds { 2789bc3ef660Sstephen hemminger const struct arg_dev_net *adn = arg; 2790bc3ef660Sstephen hemminger const struct net_device *dev = adn->dev; 27918ed67789SDaniel Lezcano 2792d1918542SDavid S. Miller if ((rt->dst.dev == dev || !dev) && 2793a1a22c12SDavid Ahern rt != adn->net->ipv6.ip6_null_entry && 2794a1a22c12SDavid Ahern (rt->rt6i_nsiblings == 0 || 27958397ed36SDavid Ahern (dev && netdev_unregistering(dev)) || 2796a1a22c12SDavid Ahern !rt->rt6i_idev->cnf.ignore_routes_with_linkdown)) 27971da177e4SLinus Torvalds return -1; 2798c159d30cSDavid S. Miller 27991da177e4SLinus Torvalds return 0; 28001da177e4SLinus Torvalds } 28011da177e4SLinus Torvalds 2802f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev) 28031da177e4SLinus Torvalds { 28048ed67789SDaniel Lezcano struct arg_dev_net adn = { 28058ed67789SDaniel Lezcano .dev = dev, 28068ed67789SDaniel Lezcano .net = net, 28078ed67789SDaniel Lezcano }; 28088ed67789SDaniel Lezcano 28090c3584d5SLi RongQing fib6_clean_all(net, fib6_ifdown, &adn); 2810e332bc67SEric W. Biederman if (dev) 28118d0b94afSMartin KaFai Lau rt6_uncached_list_flush_dev(net, dev); 28121da177e4SLinus Torvalds } 28131da177e4SLinus Torvalds 281495c96174SEric Dumazet struct rt6_mtu_change_arg { 28151da177e4SLinus Torvalds struct net_device *dev; 281695c96174SEric Dumazet unsigned int mtu; 28171da177e4SLinus Torvalds }; 28181da177e4SLinus Torvalds 28191da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg) 28201da177e4SLinus Torvalds { 28211da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 28221da177e4SLinus Torvalds struct inet6_dev *idev; 28231da177e4SLinus Torvalds 28241da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 28251da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 28261da177e4SLinus Torvalds We still use this lock to block changes 28271da177e4SLinus Torvalds caused by addrconf/ndisc. 28281da177e4SLinus Torvalds */ 28291da177e4SLinus Torvalds 28301da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 283138308473SDavid S. Miller if (!idev) 28321da177e4SLinus Torvalds return 0; 28331da177e4SLinus Torvalds 28341da177e4SLinus Torvalds /* For administrative MTU increase, there is no way to discover 28351da177e4SLinus Torvalds IPv6 PMTU increase, so PMTU increase should be updated here. 28361da177e4SLinus Torvalds Since RFC 1981 doesn't include administrative MTU increase 28371da177e4SLinus Torvalds update PMTU increase is a MUST. (i.e. jumbo frame) 28381da177e4SLinus Torvalds */ 28391da177e4SLinus Torvalds /* 28401da177e4SLinus Torvalds If new MTU is less than route PMTU, this new MTU will be the 28411da177e4SLinus Torvalds lowest MTU in the path, update the route PMTU to reflect PMTU 28421da177e4SLinus Torvalds decreases; if new MTU is greater than route PMTU, and the 28431da177e4SLinus Torvalds old MTU is the lowest MTU in the path, update the route PMTU 28441da177e4SLinus Torvalds to reflect the increase. In this case if the other nodes' MTU 28451da177e4SLinus Torvalds also have the lowest MTU, TOO BIG MESSAGE will be lead to 284667c408cfSAlexander Alemayhu PMTU discovery. 28471da177e4SLinus Torvalds */ 2848d1918542SDavid S. Miller if (rt->dst.dev == arg->dev && 2849fb56be83SMaciej Żenczykowski dst_metric_raw(&rt->dst, RTAX_MTU) && 28504b32b5adSMartin KaFai Lau !dst_metric_locked(&rt->dst, RTAX_MTU)) { 28514b32b5adSMartin KaFai Lau if (rt->rt6i_flags & RTF_CACHE) { 28524b32b5adSMartin KaFai Lau /* For RTF_CACHE with rt6i_pmtu == 0 28534b32b5adSMartin KaFai Lau * (i.e. a redirected route), 28544b32b5adSMartin KaFai Lau * the metrics of its rt->dst.from has already 28554b32b5adSMartin KaFai Lau * been updated. 28564b32b5adSMartin KaFai Lau */ 28574b32b5adSMartin KaFai Lau if (rt->rt6i_pmtu && rt->rt6i_pmtu > arg->mtu) 28584b32b5adSMartin KaFai Lau rt->rt6i_pmtu = arg->mtu; 28594b32b5adSMartin KaFai Lau } else if (dst_mtu(&rt->dst) >= arg->mtu || 2860d8d1f30bSChangli Gao (dst_mtu(&rt->dst) < arg->mtu && 28614b32b5adSMartin KaFai Lau dst_mtu(&rt->dst) == idev->cnf.mtu6)) { 2862defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu); 2863566cfd8fSSimon Arlott } 28644b32b5adSMartin KaFai Lau } 28651da177e4SLinus Torvalds return 0; 28661da177e4SLinus Torvalds } 28671da177e4SLinus Torvalds 286895c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu) 28691da177e4SLinus Torvalds { 2870c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 2871c71099acSThomas Graf .dev = dev, 2872c71099acSThomas Graf .mtu = mtu, 2873c71099acSThomas Graf }; 28741da177e4SLinus Torvalds 28750c3584d5SLi RongQing fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); 28761da177e4SLinus Torvalds } 28771da177e4SLinus Torvalds 2878ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 28795176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 288086872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 2881ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 288286872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 288386872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 288451ebd318SNicolas Dichtel [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 2885c78ba6d6SLubomir Rintel [RTA_PREF] = { .type = NLA_U8 }, 288619e42e45SRoopa Prabhu [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, 288719e42e45SRoopa Prabhu [RTA_ENCAP] = { .type = NLA_NESTED }, 288832bc201eSXin Long [RTA_EXPIRES] = { .type = NLA_U32 }, 2889622ec2c9SLorenzo Colitti [RTA_UID] = { .type = NLA_U32 }, 28903b45a410SLiping Zhang [RTA_MARK] = { .type = NLA_U32 }, 289186872cb5SThomas Graf }; 289286872cb5SThomas Graf 289386872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 2894333c4301SDavid Ahern struct fib6_config *cfg, 2895333c4301SDavid Ahern struct netlink_ext_ack *extack) 28961da177e4SLinus Torvalds { 289786872cb5SThomas Graf struct rtmsg *rtm; 289886872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 2899c78ba6d6SLubomir Rintel unsigned int pref; 290086872cb5SThomas Graf int err; 29011da177e4SLinus Torvalds 2902fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 2903fceb6435SJohannes Berg NULL); 290486872cb5SThomas Graf if (err < 0) 290586872cb5SThomas Graf goto errout; 29061da177e4SLinus Torvalds 290786872cb5SThomas Graf err = -EINVAL; 290886872cb5SThomas Graf rtm = nlmsg_data(nlh); 290986872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 291086872cb5SThomas Graf 291186872cb5SThomas Graf cfg->fc_table = rtm->rtm_table; 291286872cb5SThomas Graf cfg->fc_dst_len = rtm->rtm_dst_len; 291386872cb5SThomas Graf cfg->fc_src_len = rtm->rtm_src_len; 291486872cb5SThomas Graf cfg->fc_flags = RTF_UP; 291586872cb5SThomas Graf cfg->fc_protocol = rtm->rtm_protocol; 2916ef2c7d7bSNicolas Dichtel cfg->fc_type = rtm->rtm_type; 291786872cb5SThomas Graf 2918ef2c7d7bSNicolas Dichtel if (rtm->rtm_type == RTN_UNREACHABLE || 2919ef2c7d7bSNicolas Dichtel rtm->rtm_type == RTN_BLACKHOLE || 2920b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_PROHIBIT || 2921b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_THROW) 292286872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 292386872cb5SThomas Graf 2924ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 2925ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 2926ab79ad14SMaciej Żenczykowski 29271f56a01fSMartin KaFai Lau if (rtm->rtm_flags & RTM_F_CLONED) 29281f56a01fSMartin KaFai Lau cfg->fc_flags |= RTF_CACHE; 29291f56a01fSMartin KaFai Lau 293015e47304SEric W. Biederman cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid; 293186872cb5SThomas Graf cfg->fc_nlinfo.nlh = nlh; 29323b1e0a65SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = sock_net(skb->sk); 293386872cb5SThomas Graf 293486872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 293567b61f6cSJiri Benc cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); 293686872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 29371da177e4SLinus Torvalds } 293886872cb5SThomas Graf 293986872cb5SThomas Graf if (tb[RTA_DST]) { 294086872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 294186872cb5SThomas Graf 294286872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 294386872cb5SThomas Graf goto errout; 294486872cb5SThomas Graf 294586872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 29461da177e4SLinus Torvalds } 294786872cb5SThomas Graf 294886872cb5SThomas Graf if (tb[RTA_SRC]) { 294986872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 295086872cb5SThomas Graf 295186872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 295286872cb5SThomas Graf goto errout; 295386872cb5SThomas Graf 295486872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 29551da177e4SLinus Torvalds } 295686872cb5SThomas Graf 2957c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 295867b61f6cSJiri Benc cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); 2959c3968a85SDaniel Walter 296086872cb5SThomas Graf if (tb[RTA_OIF]) 296186872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 296286872cb5SThomas Graf 296386872cb5SThomas Graf if (tb[RTA_PRIORITY]) 296486872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 296586872cb5SThomas Graf 296686872cb5SThomas Graf if (tb[RTA_METRICS]) { 296786872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 296886872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 29691da177e4SLinus Torvalds } 297086872cb5SThomas Graf 297186872cb5SThomas Graf if (tb[RTA_TABLE]) 297286872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 297386872cb5SThomas Graf 297451ebd318SNicolas Dichtel if (tb[RTA_MULTIPATH]) { 297551ebd318SNicolas Dichtel cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); 297651ebd318SNicolas Dichtel cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); 29779ed59592SDavid Ahern 29789ed59592SDavid Ahern err = lwtunnel_valid_encap_type_attr(cfg->fc_mp, 2979c255bd68SDavid Ahern cfg->fc_mp_len, extack); 29809ed59592SDavid Ahern if (err < 0) 29819ed59592SDavid Ahern goto errout; 298251ebd318SNicolas Dichtel } 298351ebd318SNicolas Dichtel 2984c78ba6d6SLubomir Rintel if (tb[RTA_PREF]) { 2985c78ba6d6SLubomir Rintel pref = nla_get_u8(tb[RTA_PREF]); 2986c78ba6d6SLubomir Rintel if (pref != ICMPV6_ROUTER_PREF_LOW && 2987c78ba6d6SLubomir Rintel pref != ICMPV6_ROUTER_PREF_HIGH) 2988c78ba6d6SLubomir Rintel pref = ICMPV6_ROUTER_PREF_MEDIUM; 2989c78ba6d6SLubomir Rintel cfg->fc_flags |= RTF_PREF(pref); 2990c78ba6d6SLubomir Rintel } 2991c78ba6d6SLubomir Rintel 299219e42e45SRoopa Prabhu if (tb[RTA_ENCAP]) 299319e42e45SRoopa Prabhu cfg->fc_encap = tb[RTA_ENCAP]; 299419e42e45SRoopa Prabhu 29959ed59592SDavid Ahern if (tb[RTA_ENCAP_TYPE]) { 299619e42e45SRoopa Prabhu cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); 299719e42e45SRoopa Prabhu 2998c255bd68SDavid Ahern err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); 29999ed59592SDavid Ahern if (err < 0) 30009ed59592SDavid Ahern goto errout; 30019ed59592SDavid Ahern } 30029ed59592SDavid Ahern 300332bc201eSXin Long if (tb[RTA_EXPIRES]) { 300432bc201eSXin Long unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); 300532bc201eSXin Long 300632bc201eSXin Long if (addrconf_finite_timeout(timeout)) { 300732bc201eSXin Long cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); 300832bc201eSXin Long cfg->fc_flags |= RTF_EXPIRES; 300932bc201eSXin Long } 301032bc201eSXin Long } 301132bc201eSXin Long 301286872cb5SThomas Graf err = 0; 301386872cb5SThomas Graf errout: 301486872cb5SThomas Graf return err; 30151da177e4SLinus Torvalds } 30161da177e4SLinus Torvalds 30176b9ea5a6SRoopa Prabhu struct rt6_nh { 30186b9ea5a6SRoopa Prabhu struct rt6_info *rt6_info; 30196b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 30206b9ea5a6SRoopa Prabhu struct mx6_config mxc; 30216b9ea5a6SRoopa Prabhu struct list_head next; 30226b9ea5a6SRoopa Prabhu }; 30236b9ea5a6SRoopa Prabhu 30246b9ea5a6SRoopa Prabhu static void ip6_print_replace_route_err(struct list_head *rt6_nh_list) 30256b9ea5a6SRoopa Prabhu { 30266b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 30276b9ea5a6SRoopa Prabhu 30286b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 30297d4d5065SDavid Ahern pr_warn("IPV6: multipath route replace failed (check consistency of installed routes): %pI6c nexthop %pI6c ifi %d\n", 30306b9ea5a6SRoopa Prabhu &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway, 30316b9ea5a6SRoopa Prabhu nh->r_cfg.fc_ifindex); 30326b9ea5a6SRoopa Prabhu } 30336b9ea5a6SRoopa Prabhu } 30346b9ea5a6SRoopa Prabhu 30356b9ea5a6SRoopa Prabhu static int ip6_route_info_append(struct list_head *rt6_nh_list, 30366b9ea5a6SRoopa Prabhu struct rt6_info *rt, struct fib6_config *r_cfg) 30376b9ea5a6SRoopa Prabhu { 30386b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 30396b9ea5a6SRoopa Prabhu int err = -EEXIST; 30406b9ea5a6SRoopa Prabhu 30416b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 30426b9ea5a6SRoopa Prabhu /* check if rt6_info already exists */ 3043f06b7549SDavid Ahern if (rt6_duplicate_nexthop(nh->rt6_info, rt)) 30446b9ea5a6SRoopa Prabhu return err; 30456b9ea5a6SRoopa Prabhu } 30466b9ea5a6SRoopa Prabhu 30476b9ea5a6SRoopa Prabhu nh = kzalloc(sizeof(*nh), GFP_KERNEL); 30486b9ea5a6SRoopa Prabhu if (!nh) 30496b9ea5a6SRoopa Prabhu return -ENOMEM; 30506b9ea5a6SRoopa Prabhu nh->rt6_info = rt; 30516b9ea5a6SRoopa Prabhu err = ip6_convert_metrics(&nh->mxc, r_cfg); 30526b9ea5a6SRoopa Prabhu if (err) { 30536b9ea5a6SRoopa Prabhu kfree(nh); 30546b9ea5a6SRoopa Prabhu return err; 30556b9ea5a6SRoopa Prabhu } 30566b9ea5a6SRoopa Prabhu memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); 30576b9ea5a6SRoopa Prabhu list_add_tail(&nh->next, rt6_nh_list); 30586b9ea5a6SRoopa Prabhu 30596b9ea5a6SRoopa Prabhu return 0; 30606b9ea5a6SRoopa Prabhu } 30616b9ea5a6SRoopa Prabhu 30623b1137feSDavid Ahern static void ip6_route_mpath_notify(struct rt6_info *rt, 30633b1137feSDavid Ahern struct rt6_info *rt_last, 30643b1137feSDavid Ahern struct nl_info *info, 30653b1137feSDavid Ahern __u16 nlflags) 30663b1137feSDavid Ahern { 30673b1137feSDavid Ahern /* if this is an APPEND route, then rt points to the first route 30683b1137feSDavid Ahern * inserted and rt_last points to last route inserted. Userspace 30693b1137feSDavid Ahern * wants a consistent dump of the route which starts at the first 30703b1137feSDavid Ahern * nexthop. Since sibling routes are always added at the end of 30713b1137feSDavid Ahern * the list, find the first sibling of the last route appended 30723b1137feSDavid Ahern */ 30733b1137feSDavid Ahern if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->rt6i_nsiblings) { 30743b1137feSDavid Ahern rt = list_first_entry(&rt_last->rt6i_siblings, 30753b1137feSDavid Ahern struct rt6_info, 30763b1137feSDavid Ahern rt6i_siblings); 30773b1137feSDavid Ahern } 30783b1137feSDavid Ahern 30793b1137feSDavid Ahern if (rt) 30803b1137feSDavid Ahern inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); 30813b1137feSDavid Ahern } 30823b1137feSDavid Ahern 3083333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg, 3084333c4301SDavid Ahern struct netlink_ext_ack *extack) 308551ebd318SNicolas Dichtel { 30863b1137feSDavid Ahern struct rt6_info *rt_notif = NULL, *rt_last = NULL; 30873b1137feSDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 308851ebd318SNicolas Dichtel struct fib6_config r_cfg; 308951ebd318SNicolas Dichtel struct rtnexthop *rtnh; 30906b9ea5a6SRoopa Prabhu struct rt6_info *rt; 30916b9ea5a6SRoopa Prabhu struct rt6_nh *err_nh; 30926b9ea5a6SRoopa Prabhu struct rt6_nh *nh, *nh_safe; 30933b1137feSDavid Ahern __u16 nlflags; 309451ebd318SNicolas Dichtel int remaining; 309551ebd318SNicolas Dichtel int attrlen; 30966b9ea5a6SRoopa Prabhu int err = 1; 30976b9ea5a6SRoopa Prabhu int nhn = 0; 30986b9ea5a6SRoopa Prabhu int replace = (cfg->fc_nlinfo.nlh && 30996b9ea5a6SRoopa Prabhu (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); 31006b9ea5a6SRoopa Prabhu LIST_HEAD(rt6_nh_list); 310151ebd318SNicolas Dichtel 31023b1137feSDavid Ahern nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; 31033b1137feSDavid Ahern if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) 31043b1137feSDavid Ahern nlflags |= NLM_F_APPEND; 31053b1137feSDavid Ahern 310635f1b4e9SMichal Kubeček remaining = cfg->fc_mp_len; 310751ebd318SNicolas Dichtel rtnh = (struct rtnexthop *)cfg->fc_mp; 310851ebd318SNicolas Dichtel 31096b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry and build a list (rt6_nh_list) of 31106b9ea5a6SRoopa Prabhu * rt6_info structs per nexthop 31116b9ea5a6SRoopa Prabhu */ 311251ebd318SNicolas Dichtel while (rtnh_ok(rtnh, remaining)) { 311351ebd318SNicolas Dichtel memcpy(&r_cfg, cfg, sizeof(*cfg)); 311451ebd318SNicolas Dichtel if (rtnh->rtnh_ifindex) 311551ebd318SNicolas Dichtel r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 311651ebd318SNicolas Dichtel 311751ebd318SNicolas Dichtel attrlen = rtnh_attrlen(rtnh); 311851ebd318SNicolas Dichtel if (attrlen > 0) { 311951ebd318SNicolas Dichtel struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 312051ebd318SNicolas Dichtel 312151ebd318SNicolas Dichtel nla = nla_find(attrs, attrlen, RTA_GATEWAY); 312251ebd318SNicolas Dichtel if (nla) { 312367b61f6cSJiri Benc r_cfg.fc_gateway = nla_get_in6_addr(nla); 312451ebd318SNicolas Dichtel r_cfg.fc_flags |= RTF_GATEWAY; 312551ebd318SNicolas Dichtel } 312619e42e45SRoopa Prabhu r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); 312719e42e45SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); 312819e42e45SRoopa Prabhu if (nla) 312919e42e45SRoopa Prabhu r_cfg.fc_encap_type = nla_get_u16(nla); 313051ebd318SNicolas Dichtel } 31316b9ea5a6SRoopa Prabhu 3132333c4301SDavid Ahern rt = ip6_route_info_create(&r_cfg, extack); 31338c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 31348c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 31358c5b83f0SRoopa Prabhu rt = NULL; 31366b9ea5a6SRoopa Prabhu goto cleanup; 31378c5b83f0SRoopa Prabhu } 31386b9ea5a6SRoopa Prabhu 31396b9ea5a6SRoopa Prabhu err = ip6_route_info_append(&rt6_nh_list, rt, &r_cfg); 314051ebd318SNicolas Dichtel if (err) { 3141587fea74SWei Wang dst_release_immediate(&rt->dst); 31426b9ea5a6SRoopa Prabhu goto cleanup; 314351ebd318SNicolas Dichtel } 31446b9ea5a6SRoopa Prabhu 31456b9ea5a6SRoopa Prabhu rtnh = rtnh_next(rtnh, &remaining); 314651ebd318SNicolas Dichtel } 31476b9ea5a6SRoopa Prabhu 31483b1137feSDavid Ahern /* for add and replace send one notification with all nexthops. 31493b1137feSDavid Ahern * Skip the notification in fib6_add_rt2node and send one with 31503b1137feSDavid Ahern * the full route when done 31513b1137feSDavid Ahern */ 31523b1137feSDavid Ahern info->skip_notify = 1; 31533b1137feSDavid Ahern 31546b9ea5a6SRoopa Prabhu err_nh = NULL; 31556b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 31563b1137feSDavid Ahern rt_last = nh->rt6_info; 3157333c4301SDavid Ahern err = __ip6_ins_rt(nh->rt6_info, info, &nh->mxc, extack); 31583b1137feSDavid Ahern /* save reference to first route for notification */ 31593b1137feSDavid Ahern if (!rt_notif && !err) 31603b1137feSDavid Ahern rt_notif = nh->rt6_info; 31613b1137feSDavid Ahern 31626b9ea5a6SRoopa Prabhu /* nh->rt6_info is used or freed at this point, reset to NULL*/ 31636b9ea5a6SRoopa Prabhu nh->rt6_info = NULL; 31646b9ea5a6SRoopa Prabhu if (err) { 31656b9ea5a6SRoopa Prabhu if (replace && nhn) 31666b9ea5a6SRoopa Prabhu ip6_print_replace_route_err(&rt6_nh_list); 31676b9ea5a6SRoopa Prabhu err_nh = nh; 31686b9ea5a6SRoopa Prabhu goto add_errout; 31696b9ea5a6SRoopa Prabhu } 31706b9ea5a6SRoopa Prabhu 31711a72418bSNicolas Dichtel /* Because each route is added like a single route we remove 317227596472SMichal Kubeček * these flags after the first nexthop: if there is a collision, 317327596472SMichal Kubeček * we have already failed to add the first nexthop: 317427596472SMichal Kubeček * fib6_add_rt2node() has rejected it; when replacing, old 317527596472SMichal Kubeček * nexthops have been replaced by first new, the rest should 317627596472SMichal Kubeček * be added to it. 31771a72418bSNicolas Dichtel */ 317827596472SMichal Kubeček cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | 317927596472SMichal Kubeček NLM_F_REPLACE); 31806b9ea5a6SRoopa Prabhu nhn++; 31816b9ea5a6SRoopa Prabhu } 31826b9ea5a6SRoopa Prabhu 31833b1137feSDavid Ahern /* success ... tell user about new route */ 31843b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 31856b9ea5a6SRoopa Prabhu goto cleanup; 31866b9ea5a6SRoopa Prabhu 31876b9ea5a6SRoopa Prabhu add_errout: 31883b1137feSDavid Ahern /* send notification for routes that were added so that 31893b1137feSDavid Ahern * the delete notifications sent by ip6_route_del are 31903b1137feSDavid Ahern * coherent 31913b1137feSDavid Ahern */ 31923b1137feSDavid Ahern if (rt_notif) 31933b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 31943b1137feSDavid Ahern 31956b9ea5a6SRoopa Prabhu /* Delete routes that were already added */ 31966b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 31976b9ea5a6SRoopa Prabhu if (err_nh == nh) 31986b9ea5a6SRoopa Prabhu break; 3199333c4301SDavid Ahern ip6_route_del(&nh->r_cfg, extack); 32006b9ea5a6SRoopa Prabhu } 32016b9ea5a6SRoopa Prabhu 32026b9ea5a6SRoopa Prabhu cleanup: 32036b9ea5a6SRoopa Prabhu list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) { 3204587fea74SWei Wang if (nh->rt6_info) 3205587fea74SWei Wang dst_release_immediate(&nh->rt6_info->dst); 32066b9ea5a6SRoopa Prabhu kfree(nh->mxc.mx); 32076b9ea5a6SRoopa Prabhu list_del(&nh->next); 32086b9ea5a6SRoopa Prabhu kfree(nh); 32096b9ea5a6SRoopa Prabhu } 32106b9ea5a6SRoopa Prabhu 32116b9ea5a6SRoopa Prabhu return err; 32126b9ea5a6SRoopa Prabhu } 32136b9ea5a6SRoopa Prabhu 3214333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg, 3215333c4301SDavid Ahern struct netlink_ext_ack *extack) 32166b9ea5a6SRoopa Prabhu { 32176b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 32186b9ea5a6SRoopa Prabhu struct rtnexthop *rtnh; 32196b9ea5a6SRoopa Prabhu int remaining; 32206b9ea5a6SRoopa Prabhu int attrlen; 32216b9ea5a6SRoopa Prabhu int err = 1, last_err = 0; 32226b9ea5a6SRoopa Prabhu 32236b9ea5a6SRoopa Prabhu remaining = cfg->fc_mp_len; 32246b9ea5a6SRoopa Prabhu rtnh = (struct rtnexthop *)cfg->fc_mp; 32256b9ea5a6SRoopa Prabhu 32266b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry */ 32276b9ea5a6SRoopa Prabhu while (rtnh_ok(rtnh, remaining)) { 32286b9ea5a6SRoopa Prabhu memcpy(&r_cfg, cfg, sizeof(*cfg)); 32296b9ea5a6SRoopa Prabhu if (rtnh->rtnh_ifindex) 32306b9ea5a6SRoopa Prabhu r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 32316b9ea5a6SRoopa Prabhu 32326b9ea5a6SRoopa Prabhu attrlen = rtnh_attrlen(rtnh); 32336b9ea5a6SRoopa Prabhu if (attrlen > 0) { 32346b9ea5a6SRoopa Prabhu struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 32356b9ea5a6SRoopa Prabhu 32366b9ea5a6SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_GATEWAY); 32376b9ea5a6SRoopa Prabhu if (nla) { 32386b9ea5a6SRoopa Prabhu nla_memcpy(&r_cfg.fc_gateway, nla, 16); 32396b9ea5a6SRoopa Prabhu r_cfg.fc_flags |= RTF_GATEWAY; 32406b9ea5a6SRoopa Prabhu } 32416b9ea5a6SRoopa Prabhu } 3242333c4301SDavid Ahern err = ip6_route_del(&r_cfg, extack); 32436b9ea5a6SRoopa Prabhu if (err) 32446b9ea5a6SRoopa Prabhu last_err = err; 32456b9ea5a6SRoopa Prabhu 324651ebd318SNicolas Dichtel rtnh = rtnh_next(rtnh, &remaining); 324751ebd318SNicolas Dichtel } 324851ebd318SNicolas Dichtel 324951ebd318SNicolas Dichtel return last_err; 325051ebd318SNicolas Dichtel } 325151ebd318SNicolas Dichtel 3252c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, 3253c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 32541da177e4SLinus Torvalds { 325586872cb5SThomas Graf struct fib6_config cfg; 325686872cb5SThomas Graf int err; 32571da177e4SLinus Torvalds 3258333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 325986872cb5SThomas Graf if (err < 0) 326086872cb5SThomas Graf return err; 326186872cb5SThomas Graf 326251ebd318SNicolas Dichtel if (cfg.fc_mp) 3263333c4301SDavid Ahern return ip6_route_multipath_del(&cfg, extack); 32640ae81335SDavid Ahern else { 32650ae81335SDavid Ahern cfg.fc_delete_all_nh = 1; 3266333c4301SDavid Ahern return ip6_route_del(&cfg, extack); 32671da177e4SLinus Torvalds } 32680ae81335SDavid Ahern } 32691da177e4SLinus Torvalds 3270c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, 3271c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 32721da177e4SLinus Torvalds { 327386872cb5SThomas Graf struct fib6_config cfg; 327486872cb5SThomas Graf int err; 32751da177e4SLinus Torvalds 3276333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 327786872cb5SThomas Graf if (err < 0) 327886872cb5SThomas Graf return err; 327986872cb5SThomas Graf 328051ebd318SNicolas Dichtel if (cfg.fc_mp) 3281333c4301SDavid Ahern return ip6_route_multipath_add(&cfg, extack); 328251ebd318SNicolas Dichtel else 3283333c4301SDavid Ahern return ip6_route_add(&cfg, extack); 32841da177e4SLinus Torvalds } 32851da177e4SLinus Torvalds 3286beb1afacSDavid Ahern static size_t rt6_nlmsg_size(struct rt6_info *rt) 3287339bf98fSThomas Graf { 3288beb1afacSDavid Ahern int nexthop_len = 0; 3289beb1afacSDavid Ahern 3290beb1afacSDavid Ahern if (rt->rt6i_nsiblings) { 3291beb1afacSDavid Ahern nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */ 3292beb1afacSDavid Ahern + NLA_ALIGN(sizeof(struct rtnexthop)) 3293beb1afacSDavid Ahern + nla_total_size(16) /* RTA_GATEWAY */ 3294beb1afacSDavid Ahern + lwtunnel_get_encap_size(rt->dst.lwtstate); 3295beb1afacSDavid Ahern 3296beb1afacSDavid Ahern nexthop_len *= rt->rt6i_nsiblings; 3297beb1afacSDavid Ahern } 3298beb1afacSDavid Ahern 3299339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 3300339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 3301339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 3302339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 3303339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 3304339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 3305339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 3306339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 3307339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 33086a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 3309ea697639SDaniel Borkmann + nla_total_size(sizeof(struct rta_cacheinfo)) 3310c78ba6d6SLubomir Rintel + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ 331119e42e45SRoopa Prabhu + nla_total_size(1) /* RTA_PREF */ 3312beb1afacSDavid Ahern + lwtunnel_get_encap_size(rt->dst.lwtstate) 3313beb1afacSDavid Ahern + nexthop_len; 3314beb1afacSDavid Ahern } 3315beb1afacSDavid Ahern 3316beb1afacSDavid Ahern static int rt6_nexthop_info(struct sk_buff *skb, struct rt6_info *rt, 33175be083ceSDavid Ahern unsigned int *flags, bool skip_oif) 3318beb1afacSDavid Ahern { 3319beb1afacSDavid Ahern if (!netif_running(rt->dst.dev) || !netif_carrier_ok(rt->dst.dev)) { 3320beb1afacSDavid Ahern *flags |= RTNH_F_LINKDOWN; 3321beb1afacSDavid Ahern if (rt->rt6i_idev->cnf.ignore_routes_with_linkdown) 3322beb1afacSDavid Ahern *flags |= RTNH_F_DEAD; 3323beb1afacSDavid Ahern } 3324beb1afacSDavid Ahern 3325beb1afacSDavid Ahern if (rt->rt6i_flags & RTF_GATEWAY) { 3326beb1afacSDavid Ahern if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0) 3327beb1afacSDavid Ahern goto nla_put_failure; 3328beb1afacSDavid Ahern } 3329beb1afacSDavid Ahern 33305be083ceSDavid Ahern /* not needed for multipath encoding b/c it has a rtnexthop struct */ 33315be083ceSDavid Ahern if (!skip_oif && rt->dst.dev && 3332beb1afacSDavid Ahern nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex)) 3333beb1afacSDavid Ahern goto nla_put_failure; 3334beb1afacSDavid Ahern 3335beb1afacSDavid Ahern if (rt->dst.lwtstate && 3336beb1afacSDavid Ahern lwtunnel_fill_encap(skb, rt->dst.lwtstate) < 0) 3337beb1afacSDavid Ahern goto nla_put_failure; 3338beb1afacSDavid Ahern 3339beb1afacSDavid Ahern return 0; 3340beb1afacSDavid Ahern 3341beb1afacSDavid Ahern nla_put_failure: 3342beb1afacSDavid Ahern return -EMSGSIZE; 3343beb1afacSDavid Ahern } 3344beb1afacSDavid Ahern 33455be083ceSDavid Ahern /* add multipath next hop */ 3346beb1afacSDavid Ahern static int rt6_add_nexthop(struct sk_buff *skb, struct rt6_info *rt) 3347beb1afacSDavid Ahern { 3348beb1afacSDavid Ahern struct rtnexthop *rtnh; 3349beb1afacSDavid Ahern unsigned int flags = 0; 3350beb1afacSDavid Ahern 3351beb1afacSDavid Ahern rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh)); 3352beb1afacSDavid Ahern if (!rtnh) 3353beb1afacSDavid Ahern goto nla_put_failure; 3354beb1afacSDavid Ahern 3355beb1afacSDavid Ahern rtnh->rtnh_hops = 0; 3356beb1afacSDavid Ahern rtnh->rtnh_ifindex = rt->dst.dev ? rt->dst.dev->ifindex : 0; 3357beb1afacSDavid Ahern 33585be083ceSDavid Ahern if (rt6_nexthop_info(skb, rt, &flags, true) < 0) 3359beb1afacSDavid Ahern goto nla_put_failure; 3360beb1afacSDavid Ahern 3361beb1afacSDavid Ahern rtnh->rtnh_flags = flags; 3362beb1afacSDavid Ahern 3363beb1afacSDavid Ahern /* length of rtnetlink header + attributes */ 3364beb1afacSDavid Ahern rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh; 3365beb1afacSDavid Ahern 3366beb1afacSDavid Ahern return 0; 3367beb1afacSDavid Ahern 3368beb1afacSDavid Ahern nla_put_failure: 3369beb1afacSDavid Ahern return -EMSGSIZE; 3370339bf98fSThomas Graf } 3371339bf98fSThomas Graf 3372191cd582SBrian Haley static int rt6_fill_node(struct net *net, 3373191cd582SBrian Haley struct sk_buff *skb, struct rt6_info *rt, 33740d51aa80SJamal Hadi Salim struct in6_addr *dst, struct in6_addr *src, 337515e47304SEric W. Biederman int iif, int type, u32 portid, u32 seq, 3376f8cfe2ceSDavid Ahern unsigned int flags) 33771da177e4SLinus Torvalds { 33784b32b5adSMartin KaFai Lau u32 metrics[RTAX_MAX]; 33791da177e4SLinus Torvalds struct rtmsg *rtm; 33801da177e4SLinus Torvalds struct nlmsghdr *nlh; 3381e3703b3dSThomas Graf long expires; 33829e762a4aSPatrick McHardy u32 table; 33831da177e4SLinus Torvalds 338415e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); 338538308473SDavid S. Miller if (!nlh) 338626932566SPatrick McHardy return -EMSGSIZE; 33872d7202bfSThomas Graf 33882d7202bfSThomas Graf rtm = nlmsg_data(nlh); 33891da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 33901da177e4SLinus Torvalds rtm->rtm_dst_len = rt->rt6i_dst.plen; 33911da177e4SLinus Torvalds rtm->rtm_src_len = rt->rt6i_src.plen; 33921da177e4SLinus Torvalds rtm->rtm_tos = 0; 3393c71099acSThomas Graf if (rt->rt6i_table) 33949e762a4aSPatrick McHardy table = rt->rt6i_table->tb6_id; 3395c71099acSThomas Graf else 33969e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 33979e762a4aSPatrick McHardy rtm->rtm_table = table; 3398c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, table)) 3399c78679e8SDavid S. Miller goto nla_put_failure; 3400ef2c7d7bSNicolas Dichtel if (rt->rt6i_flags & RTF_REJECT) { 3401ef2c7d7bSNicolas Dichtel switch (rt->dst.error) { 3402ef2c7d7bSNicolas Dichtel case -EINVAL: 3403ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_BLACKHOLE; 3404ef2c7d7bSNicolas Dichtel break; 3405ef2c7d7bSNicolas Dichtel case -EACCES: 3406ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_PROHIBIT; 3407ef2c7d7bSNicolas Dichtel break; 3408b4949ab2SNicolas Dichtel case -EAGAIN: 3409b4949ab2SNicolas Dichtel rtm->rtm_type = RTN_THROW; 3410b4949ab2SNicolas Dichtel break; 3411ef2c7d7bSNicolas Dichtel default: 34121da177e4SLinus Torvalds rtm->rtm_type = RTN_UNREACHABLE; 3413ef2c7d7bSNicolas Dichtel break; 3414ef2c7d7bSNicolas Dichtel } 3415ef2c7d7bSNicolas Dichtel } 3416ab79ad14SMaciej Żenczykowski else if (rt->rt6i_flags & RTF_LOCAL) 3417ab79ad14SMaciej Żenczykowski rtm->rtm_type = RTN_LOCAL; 34184ee39733SDavid Ahern else if (rt->rt6i_flags & RTF_ANYCAST) 34194ee39733SDavid Ahern rtm->rtm_type = RTN_ANYCAST; 3420d1918542SDavid S. Miller else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK)) 34211da177e4SLinus Torvalds rtm->rtm_type = RTN_LOCAL; 34221da177e4SLinus Torvalds else 34231da177e4SLinus Torvalds rtm->rtm_type = RTN_UNICAST; 34241da177e4SLinus Torvalds rtm->rtm_flags = 0; 34251da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 34261da177e4SLinus Torvalds rtm->rtm_protocol = rt->rt6i_protocol; 34271da177e4SLinus Torvalds 34281da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) 34291da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 34301da177e4SLinus Torvalds 34311da177e4SLinus Torvalds if (dst) { 3432930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_DST, dst)) 3433c78679e8SDavid S. Miller goto nla_put_failure; 34341da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 34351da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 3436930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr)) 3437c78679e8SDavid S. Miller goto nla_put_failure; 34381da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 34391da177e4SLinus Torvalds if (src) { 3440930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_SRC, src)) 3441c78679e8SDavid S. Miller goto nla_put_failure; 34421da177e4SLinus Torvalds rtm->rtm_src_len = 128; 3443c78679e8SDavid S. Miller } else if (rtm->rtm_src_len && 3444930345eaSJiri Benc nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr)) 3445c78679e8SDavid S. Miller goto nla_put_failure; 34461da177e4SLinus Torvalds #endif 34477bc570c8SYOSHIFUJI Hideaki if (iif) { 34487bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 34497bc570c8SYOSHIFUJI Hideaki if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) { 3450fd61c6baSDavid Ahern int err = ip6mr_get_route(net, skb, rtm, portid); 34512cf75070SNikolay Aleksandrov 34527bc570c8SYOSHIFUJI Hideaki if (err == 0) 34537bc570c8SYOSHIFUJI Hideaki return 0; 3454fd61c6baSDavid Ahern if (err < 0) 34557bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 34567bc570c8SYOSHIFUJI Hideaki } else 34577bc570c8SYOSHIFUJI Hideaki #endif 3458c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_IIF, iif)) 3459c78679e8SDavid S. Miller goto nla_put_failure; 34607bc570c8SYOSHIFUJI Hideaki } else if (dst) { 34611da177e4SLinus Torvalds struct in6_addr saddr_buf; 3462c78679e8SDavid S. Miller if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 && 3463930345eaSJiri Benc nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 3464c78679e8SDavid S. Miller goto nla_put_failure; 3465c3968a85SDaniel Walter } 3466c3968a85SDaniel Walter 3467c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) { 3468c3968a85SDaniel Walter struct in6_addr saddr_buf; 34694e3fd7a0SAlexey Dobriyan saddr_buf = rt->rt6i_prefsrc.addr; 3470930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 3471c78679e8SDavid S. Miller goto nla_put_failure; 34721da177e4SLinus Torvalds } 34732d7202bfSThomas Graf 34744b32b5adSMartin KaFai Lau memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics)); 34754b32b5adSMartin KaFai Lau if (rt->rt6i_pmtu) 34764b32b5adSMartin KaFai Lau metrics[RTAX_MTU - 1] = rt->rt6i_pmtu; 34774b32b5adSMartin KaFai Lau if (rtnetlink_put_metrics(skb, metrics) < 0) 34782d7202bfSThomas Graf goto nla_put_failure; 34792d7202bfSThomas Graf 3480beb1afacSDavid Ahern if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric)) 3481beb1afacSDavid Ahern goto nla_put_failure; 3482beb1afacSDavid Ahern 3483beb1afacSDavid Ahern /* For multipath routes, walk the siblings list and add 3484beb1afacSDavid Ahern * each as a nexthop within RTA_MULTIPATH. 3485beb1afacSDavid Ahern */ 3486beb1afacSDavid Ahern if (rt->rt6i_nsiblings) { 3487beb1afacSDavid Ahern struct rt6_info *sibling, *next_sibling; 3488beb1afacSDavid Ahern struct nlattr *mp; 3489beb1afacSDavid Ahern 3490beb1afacSDavid Ahern mp = nla_nest_start(skb, RTA_MULTIPATH); 3491beb1afacSDavid Ahern if (!mp) 3492beb1afacSDavid Ahern goto nla_put_failure; 3493beb1afacSDavid Ahern 3494beb1afacSDavid Ahern if (rt6_add_nexthop(skb, rt) < 0) 3495beb1afacSDavid Ahern goto nla_put_failure; 3496beb1afacSDavid Ahern 3497beb1afacSDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 3498beb1afacSDavid Ahern &rt->rt6i_siblings, rt6i_siblings) { 3499beb1afacSDavid Ahern if (rt6_add_nexthop(skb, sibling) < 0) 350094f826b8SEric Dumazet goto nla_put_failure; 350194f826b8SEric Dumazet } 35022d7202bfSThomas Graf 3503beb1afacSDavid Ahern nla_nest_end(skb, mp); 3504beb1afacSDavid Ahern } else { 35055be083ceSDavid Ahern if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0) 3506c78679e8SDavid S. Miller goto nla_put_failure; 3507beb1afacSDavid Ahern } 35088253947eSLi Wei 35098253947eSLi Wei expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0; 351069cdf8f9SYOSHIFUJI Hideaki 351187a50699SDavid S. Miller if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0) 3512e3703b3dSThomas Graf goto nla_put_failure; 35131da177e4SLinus Torvalds 3514c78ba6d6SLubomir Rintel if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags))) 3515c78ba6d6SLubomir Rintel goto nla_put_failure; 3516c78ba6d6SLubomir Rintel 351719e42e45SRoopa Prabhu 3518053c095aSJohannes Berg nlmsg_end(skb, nlh); 3519053c095aSJohannes Berg return 0; 35202d7202bfSThomas Graf 35212d7202bfSThomas Graf nla_put_failure: 352226932566SPatrick McHardy nlmsg_cancel(skb, nlh); 352326932566SPatrick McHardy return -EMSGSIZE; 35241da177e4SLinus Torvalds } 35251da177e4SLinus Torvalds 35261b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg) 35271da177e4SLinus Torvalds { 35281da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 35291f17e2f2SDavid Ahern struct net *net = arg->net; 35301f17e2f2SDavid Ahern 35311f17e2f2SDavid Ahern if (rt == net->ipv6.ip6_null_entry) 35321f17e2f2SDavid Ahern return 0; 35331da177e4SLinus Torvalds 35342d7202bfSThomas Graf if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { 35352d7202bfSThomas Graf struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); 3536f8cfe2ceSDavid Ahern 3537f8cfe2ceSDavid Ahern /* user wants prefix routes only */ 3538f8cfe2ceSDavid Ahern if (rtm->rtm_flags & RTM_F_PREFIX && 3539f8cfe2ceSDavid Ahern !(rt->rt6i_flags & RTF_PREFIX_RT)) { 3540f8cfe2ceSDavid Ahern /* success since this is not a prefix route */ 3541f8cfe2ceSDavid Ahern return 1; 3542f8cfe2ceSDavid Ahern } 3543f8cfe2ceSDavid Ahern } 35441da177e4SLinus Torvalds 35451f17e2f2SDavid Ahern return rt6_fill_node(net, 3546191cd582SBrian Haley arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE, 354715e47304SEric W. Biederman NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq, 3548f8cfe2ceSDavid Ahern NLM_F_MULTI); 35491da177e4SLinus Torvalds } 35501da177e4SLinus Torvalds 3551c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, 3552c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 35531da177e4SLinus Torvalds { 35543b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 3555ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 355618c3a61cSRoopa Prabhu int err, iif = 0, oif = 0; 355718c3a61cSRoopa Prabhu struct dst_entry *dst; 35581da177e4SLinus Torvalds struct rt6_info *rt; 3559ab364a6fSThomas Graf struct sk_buff *skb; 3560ab364a6fSThomas Graf struct rtmsg *rtm; 35614c9483b2SDavid S. Miller struct flowi6 fl6; 356218c3a61cSRoopa Prabhu bool fibmatch; 3563ab364a6fSThomas Graf 3564fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 3565c21ef3e3SDavid Ahern extack); 3566ab364a6fSThomas Graf if (err < 0) 3567ab364a6fSThomas Graf goto errout; 3568ab364a6fSThomas Graf 3569ab364a6fSThomas Graf err = -EINVAL; 35704c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 357138b7097bSHannes Frederic Sowa rtm = nlmsg_data(nlh); 357238b7097bSHannes Frederic Sowa fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0); 357318c3a61cSRoopa Prabhu fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); 3574ab364a6fSThomas Graf 3575ab364a6fSThomas Graf if (tb[RTA_SRC]) { 3576ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 3577ab364a6fSThomas Graf goto errout; 3578ab364a6fSThomas Graf 35794e3fd7a0SAlexey Dobriyan fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 3580ab364a6fSThomas Graf } 3581ab364a6fSThomas Graf 3582ab364a6fSThomas Graf if (tb[RTA_DST]) { 3583ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 3584ab364a6fSThomas Graf goto errout; 3585ab364a6fSThomas Graf 35864e3fd7a0SAlexey Dobriyan fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 3587ab364a6fSThomas Graf } 3588ab364a6fSThomas Graf 3589ab364a6fSThomas Graf if (tb[RTA_IIF]) 3590ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 3591ab364a6fSThomas Graf 3592ab364a6fSThomas Graf if (tb[RTA_OIF]) 359372331bc0SShmulik Ladkani oif = nla_get_u32(tb[RTA_OIF]); 3594ab364a6fSThomas Graf 35952e47b291SLorenzo Colitti if (tb[RTA_MARK]) 35962e47b291SLorenzo Colitti fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); 35972e47b291SLorenzo Colitti 3598622ec2c9SLorenzo Colitti if (tb[RTA_UID]) 3599622ec2c9SLorenzo Colitti fl6.flowi6_uid = make_kuid(current_user_ns(), 3600622ec2c9SLorenzo Colitti nla_get_u32(tb[RTA_UID])); 3601622ec2c9SLorenzo Colitti else 3602622ec2c9SLorenzo Colitti fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); 3603622ec2c9SLorenzo Colitti 3604ab364a6fSThomas Graf if (iif) { 3605ab364a6fSThomas Graf struct net_device *dev; 360672331bc0SShmulik Ladkani int flags = 0; 360772331bc0SShmulik Ladkani 36085578689aSDaniel Lezcano dev = __dev_get_by_index(net, iif); 3609ab364a6fSThomas Graf if (!dev) { 3610ab364a6fSThomas Graf err = -ENODEV; 3611ab364a6fSThomas Graf goto errout; 3612ab364a6fSThomas Graf } 361372331bc0SShmulik Ladkani 361472331bc0SShmulik Ladkani fl6.flowi6_iif = iif; 361572331bc0SShmulik Ladkani 361672331bc0SShmulik Ladkani if (!ipv6_addr_any(&fl6.saddr)) 361772331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_HAS_SADDR; 361872331bc0SShmulik Ladkani 361918c3a61cSRoopa Prabhu if (!fibmatch) 362018c3a61cSRoopa Prabhu dst = ip6_route_input_lookup(net, dev, &fl6, flags); 362172331bc0SShmulik Ladkani } else { 362272331bc0SShmulik Ladkani fl6.flowi6_oif = oif; 362372331bc0SShmulik Ladkani 362418c3a61cSRoopa Prabhu if (!fibmatch) 362518c3a61cSRoopa Prabhu dst = ip6_route_output(net, NULL, &fl6); 362618c3a61cSRoopa Prabhu } 362718c3a61cSRoopa Prabhu 362818c3a61cSRoopa Prabhu if (fibmatch) 362918c3a61cSRoopa Prabhu dst = ip6_route_lookup(net, &fl6, 0); 363018c3a61cSRoopa Prabhu 363118c3a61cSRoopa Prabhu rt = container_of(dst, struct rt6_info, dst); 363218c3a61cSRoopa Prabhu if (rt->dst.error) { 363318c3a61cSRoopa Prabhu err = rt->dst.error; 363418c3a61cSRoopa Prabhu ip6_rt_put(rt); 363518c3a61cSRoopa Prabhu goto errout; 3636ab364a6fSThomas Graf } 36371da177e4SLinus Torvalds 36389d6acb3bSWANG Cong if (rt == net->ipv6.ip6_null_entry) { 36399d6acb3bSWANG Cong err = rt->dst.error; 36409d6acb3bSWANG Cong ip6_rt_put(rt); 36419d6acb3bSWANG Cong goto errout; 36429d6acb3bSWANG Cong } 36439d6acb3bSWANG Cong 36441da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 364538308473SDavid S. Miller if (!skb) { 364694e187c0SAmerigo Wang ip6_rt_put(rt); 3647ab364a6fSThomas Graf err = -ENOBUFS; 3648ab364a6fSThomas Graf goto errout; 3649ab364a6fSThomas Graf } 36501da177e4SLinus Torvalds 3651d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 365218c3a61cSRoopa Prabhu if (fibmatch) 365318c3a61cSRoopa Prabhu err = rt6_fill_node(net, skb, rt, NULL, NULL, iif, 365418c3a61cSRoopa Prabhu RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 365518c3a61cSRoopa Prabhu nlh->nlmsg_seq, 0); 365618c3a61cSRoopa Prabhu else 36574c9483b2SDavid S. Miller err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif, 365815e47304SEric W. Biederman RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 3659f8cfe2ceSDavid Ahern nlh->nlmsg_seq, 0); 36601da177e4SLinus Torvalds if (err < 0) { 3661ab364a6fSThomas Graf kfree_skb(skb); 3662ab364a6fSThomas Graf goto errout; 36631da177e4SLinus Torvalds } 36641da177e4SLinus Torvalds 366515e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 3666ab364a6fSThomas Graf errout: 36671da177e4SLinus Torvalds return err; 36681da177e4SLinus Torvalds } 36691da177e4SLinus Torvalds 367037a1d361SRoopa Prabhu void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info, 367137a1d361SRoopa Prabhu unsigned int nlm_flags) 36721da177e4SLinus Torvalds { 36731da177e4SLinus Torvalds struct sk_buff *skb; 36745578689aSDaniel Lezcano struct net *net = info->nl_net; 3675528c4cebSDenis V. Lunev u32 seq; 3676528c4cebSDenis V. Lunev int err; 36770d51aa80SJamal Hadi Salim 3678528c4cebSDenis V. Lunev err = -ENOBUFS; 367938308473SDavid S. Miller seq = info->nlh ? info->nlh->nlmsg_seq : 0; 368086872cb5SThomas Graf 368119e42e45SRoopa Prabhu skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 368238308473SDavid S. Miller if (!skb) 368321713ebcSThomas Graf goto errout; 36841da177e4SLinus Torvalds 3685191cd582SBrian Haley err = rt6_fill_node(net, skb, rt, NULL, NULL, 0, 3686f8cfe2ceSDavid Ahern event, info->portid, seq, nlm_flags); 368726932566SPatrick McHardy if (err < 0) { 368826932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 368926932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 369026932566SPatrick McHardy kfree_skb(skb); 369126932566SPatrick McHardy goto errout; 369226932566SPatrick McHardy } 369315e47304SEric W. Biederman rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 36945578689aSDaniel Lezcano info->nlh, gfp_any()); 36951ce85fe4SPablo Neira Ayuso return; 369621713ebcSThomas Graf errout: 369721713ebcSThomas Graf if (err < 0) 36985578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 36991da177e4SLinus Torvalds } 37001da177e4SLinus Torvalds 37018ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 3702351638e7SJiri Pirko unsigned long event, void *ptr) 37038ed67789SDaniel Lezcano { 3704351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 3705c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 37068ed67789SDaniel Lezcano 3707242d3a49SWANG Cong if (!(dev->flags & IFF_LOOPBACK)) 3708242d3a49SWANG Cong return NOTIFY_OK; 3709242d3a49SWANG Cong 3710242d3a49SWANG Cong if (event == NETDEV_REGISTER) { 3711d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 37128ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 37138ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 3714d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 37158ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 3716d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 37178ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 37188ed67789SDaniel Lezcano #endif 371976da0704SWANG Cong } else if (event == NETDEV_UNREGISTER && 372076da0704SWANG Cong dev->reg_state != NETREG_UNREGISTERED) { 372176da0704SWANG Cong /* NETDEV_UNREGISTER could be fired for multiple times by 372276da0704SWANG Cong * netdev_wait_allrefs(). Make sure we only call this once. 372376da0704SWANG Cong */ 3724*12d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev); 3725242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 3726*12d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev); 3727*12d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev); 3728242d3a49SWANG Cong #endif 37298ed67789SDaniel Lezcano } 37308ed67789SDaniel Lezcano 37318ed67789SDaniel Lezcano return NOTIFY_OK; 37328ed67789SDaniel Lezcano } 37338ed67789SDaniel Lezcano 37341da177e4SLinus Torvalds /* 37351da177e4SLinus Torvalds * /proc 37361da177e4SLinus Torvalds */ 37371da177e4SLinus Torvalds 37381da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 37391da177e4SLinus Torvalds 374033120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = { 374133120b30SAlexey Dobriyan .owner = THIS_MODULE, 374233120b30SAlexey Dobriyan .open = ipv6_route_open, 374333120b30SAlexey Dobriyan .read = seq_read, 374433120b30SAlexey Dobriyan .llseek = seq_lseek, 37458d2ca1d7SHannes Frederic Sowa .release = seq_release_net, 374633120b30SAlexey Dobriyan }; 374733120b30SAlexey Dobriyan 37481da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 37491da177e4SLinus Torvalds { 375069ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 37511da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 375269ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 375369ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 375469ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_alloc, 375569ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 375669ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 3757fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 375869ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 37591da177e4SLinus Torvalds 37601da177e4SLinus Torvalds return 0; 37611da177e4SLinus Torvalds } 37621da177e4SLinus Torvalds 37631da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file) 37641da177e4SLinus Torvalds { 3765de05c557SPavel Emelyanov return single_open_net(inode, file, rt6_stats_seq_show); 376669ddb805SDaniel Lezcano } 376769ddb805SDaniel Lezcano 37689a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = { 37691da177e4SLinus Torvalds .owner = THIS_MODULE, 37701da177e4SLinus Torvalds .open = rt6_stats_seq_open, 37711da177e4SLinus Torvalds .read = seq_read, 37721da177e4SLinus Torvalds .llseek = seq_lseek, 3773b6fcbdb4SPavel Emelyanov .release = single_release_net, 37741da177e4SLinus Torvalds }; 37751da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 37761da177e4SLinus Torvalds 37771da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 37781da177e4SLinus Torvalds 37791da177e4SLinus Torvalds static 3780fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, 37811da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 37821da177e4SLinus Torvalds { 3783c486da34SLucian Adrian Grijincu struct net *net; 3784c486da34SLucian Adrian Grijincu int delay; 3785c486da34SLucian Adrian Grijincu if (!write) 3786c486da34SLucian Adrian Grijincu return -EINVAL; 3787c486da34SLucian Adrian Grijincu 3788c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 3789c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 37908d65af78SAlexey Dobriyan proc_dointvec(ctl, write, buffer, lenp, ppos); 37912ac3ac8fSMichal Kubeček fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0); 37921da177e4SLinus Torvalds return 0; 37931da177e4SLinus Torvalds } 37941da177e4SLinus Torvalds 3795fe2c6338SJoe Perches struct ctl_table ipv6_route_table_template[] = { 37961da177e4SLinus Torvalds { 37971da177e4SLinus Torvalds .procname = "flush", 37984990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 37991da177e4SLinus Torvalds .maxlen = sizeof(int), 380089c8b3a1SDave Jones .mode = 0200, 38016d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 38021da177e4SLinus Torvalds }, 38031da177e4SLinus Torvalds { 38041da177e4SLinus Torvalds .procname = "gc_thresh", 38059a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 38061da177e4SLinus Torvalds .maxlen = sizeof(int), 38071da177e4SLinus Torvalds .mode = 0644, 38086d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 38091da177e4SLinus Torvalds }, 38101da177e4SLinus Torvalds { 38111da177e4SLinus Torvalds .procname = "max_size", 38124990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 38131da177e4SLinus Torvalds .maxlen = sizeof(int), 38141da177e4SLinus Torvalds .mode = 0644, 38156d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 38161da177e4SLinus Torvalds }, 38171da177e4SLinus Torvalds { 38181da177e4SLinus Torvalds .procname = "gc_min_interval", 38194990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 38201da177e4SLinus Torvalds .maxlen = sizeof(int), 38211da177e4SLinus Torvalds .mode = 0644, 38226d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 38231da177e4SLinus Torvalds }, 38241da177e4SLinus Torvalds { 38251da177e4SLinus Torvalds .procname = "gc_timeout", 38264990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 38271da177e4SLinus Torvalds .maxlen = sizeof(int), 38281da177e4SLinus Torvalds .mode = 0644, 38296d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 38301da177e4SLinus Torvalds }, 38311da177e4SLinus Torvalds { 38321da177e4SLinus Torvalds .procname = "gc_interval", 38334990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 38341da177e4SLinus Torvalds .maxlen = sizeof(int), 38351da177e4SLinus Torvalds .mode = 0644, 38366d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 38371da177e4SLinus Torvalds }, 38381da177e4SLinus Torvalds { 38391da177e4SLinus Torvalds .procname = "gc_elasticity", 38404990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 38411da177e4SLinus Torvalds .maxlen = sizeof(int), 38421da177e4SLinus Torvalds .mode = 0644, 3843f3d3f616SMin Zhang .proc_handler = proc_dointvec, 38441da177e4SLinus Torvalds }, 38451da177e4SLinus Torvalds { 38461da177e4SLinus Torvalds .procname = "mtu_expires", 38474990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 38481da177e4SLinus Torvalds .maxlen = sizeof(int), 38491da177e4SLinus Torvalds .mode = 0644, 38506d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 38511da177e4SLinus Torvalds }, 38521da177e4SLinus Torvalds { 38531da177e4SLinus Torvalds .procname = "min_adv_mss", 38544990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 38551da177e4SLinus Torvalds .maxlen = sizeof(int), 38561da177e4SLinus Torvalds .mode = 0644, 3857f3d3f616SMin Zhang .proc_handler = proc_dointvec, 38581da177e4SLinus Torvalds }, 38591da177e4SLinus Torvalds { 38601da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 38614990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 38621da177e4SLinus Torvalds .maxlen = sizeof(int), 38631da177e4SLinus Torvalds .mode = 0644, 38646d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 38651da177e4SLinus Torvalds }, 3866f8572d8fSEric W. Biederman { } 38671da177e4SLinus Torvalds }; 38681da177e4SLinus Torvalds 38692c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 3870760f2d01SDaniel Lezcano { 3871760f2d01SDaniel Lezcano struct ctl_table *table; 3872760f2d01SDaniel Lezcano 3873760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 3874760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 3875760f2d01SDaniel Lezcano GFP_KERNEL); 38765ee09105SYOSHIFUJI Hideaki 38775ee09105SYOSHIFUJI Hideaki if (table) { 38785ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 3879c486da34SLucian Adrian Grijincu table[0].extra1 = net; 388086393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 38815ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 38825ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 38835ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 38845ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 38855ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 38865ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 38875ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 38889c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 3889464dc801SEric W. Biederman 3890464dc801SEric W. Biederman /* Don't export sysctls to unprivileged users */ 3891464dc801SEric W. Biederman if (net->user_ns != &init_user_ns) 3892464dc801SEric W. Biederman table[0].procname = NULL; 38935ee09105SYOSHIFUJI Hideaki } 38945ee09105SYOSHIFUJI Hideaki 3895760f2d01SDaniel Lezcano return table; 3896760f2d01SDaniel Lezcano } 38971da177e4SLinus Torvalds #endif 38981da177e4SLinus Torvalds 38992c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 3900cdb18761SDaniel Lezcano { 3901633d424bSPavel Emelyanov int ret = -ENOMEM; 39028ed67789SDaniel Lezcano 390386393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 390486393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 3905f2fc6a54SBenjamin Thery 3906fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 3907fc66f95cSEric Dumazet goto out_ip6_dst_ops; 3908fc66f95cSEric Dumazet 39098ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 39108ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 39118ed67789SDaniel Lezcano GFP_KERNEL); 39128ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 3913fc66f95cSEric Dumazet goto out_ip6_dst_entries; 3914d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.path = 39158ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_null_entry; 3916d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 391762fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 391862fa8a84SDavid S. Miller ip6_template_metrics, true); 39198ed67789SDaniel Lezcano 39208ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 39218ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 39228ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 39238ed67789SDaniel Lezcano GFP_KERNEL); 392468fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 392568fffc67SPeter Zijlstra goto out_ip6_null_entry; 3926d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.path = 39278ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_prohibit_entry; 3928d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 392962fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 393062fa8a84SDavid S. Miller ip6_template_metrics, true); 39318ed67789SDaniel Lezcano 39328ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 39338ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 39348ed67789SDaniel Lezcano GFP_KERNEL); 393568fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 393668fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 3937d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.path = 39388ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_blk_hole_entry; 3939d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 394062fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 394162fa8a84SDavid S. Miller ip6_template_metrics, true); 39428ed67789SDaniel Lezcano #endif 39438ed67789SDaniel Lezcano 3944b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 3945b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 3946b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 3947b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 3948b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 3949b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 3950b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 3951b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 3952b339a47cSPeter Zijlstra 39536891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 39546891a346SBenjamin Thery 39558ed67789SDaniel Lezcano ret = 0; 39568ed67789SDaniel Lezcano out: 39578ed67789SDaniel Lezcano return ret; 3958f2fc6a54SBenjamin Thery 395968fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 396068fffc67SPeter Zijlstra out_ip6_prohibit_entry: 396168fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 396268fffc67SPeter Zijlstra out_ip6_null_entry: 396368fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 396468fffc67SPeter Zijlstra #endif 3965fc66f95cSEric Dumazet out_ip6_dst_entries: 3966fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 3967f2fc6a54SBenjamin Thery out_ip6_dst_ops: 3968f2fc6a54SBenjamin Thery goto out; 3969cdb18761SDaniel Lezcano } 3970cdb18761SDaniel Lezcano 39712c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 3972cdb18761SDaniel Lezcano { 39738ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 39748ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 39758ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 39768ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 39778ed67789SDaniel Lezcano #endif 397841bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 3979cdb18761SDaniel Lezcano } 3980cdb18761SDaniel Lezcano 3981d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net) 3982d189634eSThomas Graf { 3983d189634eSThomas Graf #ifdef CONFIG_PROC_FS 3984d4beaa66SGao feng proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops); 3985d4beaa66SGao feng proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops); 3986d189634eSThomas Graf #endif 3987d189634eSThomas Graf return 0; 3988d189634eSThomas Graf } 3989d189634eSThomas Graf 3990d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net) 3991d189634eSThomas Graf { 3992d189634eSThomas Graf #ifdef CONFIG_PROC_FS 3993ece31ffdSGao feng remove_proc_entry("ipv6_route", net->proc_net); 3994ece31ffdSGao feng remove_proc_entry("rt6_stats", net->proc_net); 3995d189634eSThomas Graf #endif 3996d189634eSThomas Graf } 3997d189634eSThomas Graf 3998cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 3999cdb18761SDaniel Lezcano .init = ip6_route_net_init, 4000cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 4001cdb18761SDaniel Lezcano }; 4002cdb18761SDaniel Lezcano 4003c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net) 4004c3426b47SDavid S. Miller { 4005c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 4006c3426b47SDavid S. Miller 4007c3426b47SDavid S. Miller if (!bp) 4008c3426b47SDavid S. Miller return -ENOMEM; 4009c3426b47SDavid S. Miller inet_peer_base_init(bp); 4010c3426b47SDavid S. Miller net->ipv6.peers = bp; 4011c3426b47SDavid S. Miller return 0; 4012c3426b47SDavid S. Miller } 4013c3426b47SDavid S. Miller 4014c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net) 4015c3426b47SDavid S. Miller { 4016c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv6.peers; 4017c3426b47SDavid S. Miller 4018c3426b47SDavid S. Miller net->ipv6.peers = NULL; 401956a6b248SDavid S. Miller inetpeer_invalidate_tree(bp); 4020c3426b47SDavid S. Miller kfree(bp); 4021c3426b47SDavid S. Miller } 4022c3426b47SDavid S. Miller 40232b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = { 4024c3426b47SDavid S. Miller .init = ipv6_inetpeer_init, 4025c3426b47SDavid S. Miller .exit = ipv6_inetpeer_exit, 4026c3426b47SDavid S. Miller }; 4027c3426b47SDavid S. Miller 4028d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = { 4029d189634eSThomas Graf .init = ip6_route_net_init_late, 4030d189634eSThomas Graf .exit = ip6_route_net_exit_late, 4031d189634eSThomas Graf }; 4032d189634eSThomas Graf 40338ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 40348ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 4035242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY - 10, 40368ed67789SDaniel Lezcano }; 40378ed67789SDaniel Lezcano 40382f460933SWANG Cong void __init ip6_route_init_special_entries(void) 40392f460933SWANG Cong { 40402f460933SWANG Cong /* Registering of the loopback is done before this portion of code, 40412f460933SWANG Cong * the loopback reference in rt6_info will not be taken, do it 40422f460933SWANG Cong * manually for init_net */ 40432f460933SWANG Cong init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 40442f460933SWANG Cong init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 40452f460933SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 40462f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 40472f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 40482f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 40492f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 40502f460933SWANG Cong #endif 40512f460933SWANG Cong } 40522f460933SWANG Cong 4053433d49c3SDaniel Lezcano int __init ip6_route_init(void) 40541da177e4SLinus Torvalds { 4055433d49c3SDaniel Lezcano int ret; 40568d0b94afSMartin KaFai Lau int cpu; 4057433d49c3SDaniel Lezcano 40589a7ec3a9SDaniel Lezcano ret = -ENOMEM; 40599a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 40609a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 40619a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 40629a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 4063c19a28e1SFernando Carrijo goto out; 406414e50e57SDavid S. Miller 4065fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 40668ed67789SDaniel Lezcano if (ret) 4067bdb3289fSDaniel Lezcano goto out_kmem_cache; 4068bdb3289fSDaniel Lezcano 4069c3426b47SDavid S. Miller ret = register_pernet_subsys(&ipv6_inetpeer_ops); 4070c3426b47SDavid S. Miller if (ret) 4071e8803b6cSDavid S. Miller goto out_dst_entries; 40722a0c451aSThomas Graf 40737e52b33bSDavid S. Miller ret = register_pernet_subsys(&ip6_route_net_ops); 40747e52b33bSDavid S. Miller if (ret) 40757e52b33bSDavid S. Miller goto out_register_inetpeer; 4076c3426b47SDavid S. Miller 40775dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 40785dc121e9SArnaud Ebalard 4079e8803b6cSDavid S. Miller ret = fib6_init(); 4080433d49c3SDaniel Lezcano if (ret) 40818ed67789SDaniel Lezcano goto out_register_subsys; 4082433d49c3SDaniel Lezcano 4083433d49c3SDaniel Lezcano ret = xfrm6_init(); 4084433d49c3SDaniel Lezcano if (ret) 4085e8803b6cSDavid S. Miller goto out_fib6_init; 4086c35b7e72SDaniel Lezcano 4087433d49c3SDaniel Lezcano ret = fib6_rules_init(); 4088433d49c3SDaniel Lezcano if (ret) 4089433d49c3SDaniel Lezcano goto xfrm6_init; 40907e5449c2SDaniel Lezcano 4091d189634eSThomas Graf ret = register_pernet_subsys(&ip6_route_net_late_ops); 4092d189634eSThomas Graf if (ret) 4093d189634eSThomas Graf goto fib6_rules_init; 4094d189634eSThomas Graf 4095433d49c3SDaniel Lezcano ret = -ENOBUFS; 4096c7ac8679SGreg Rose if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) || 4097c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) || 4098c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL)) 4099d189634eSThomas Graf goto out_register_late_subsys; 4100433d49c3SDaniel Lezcano 41018ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 4102cdb18761SDaniel Lezcano if (ret) 4103d189634eSThomas Graf goto out_register_late_subsys; 41048ed67789SDaniel Lezcano 41058d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 41068d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 41078d0b94afSMartin KaFai Lau 41088d0b94afSMartin KaFai Lau INIT_LIST_HEAD(&ul->head); 41098d0b94afSMartin KaFai Lau spin_lock_init(&ul->lock); 41108d0b94afSMartin KaFai Lau } 41118d0b94afSMartin KaFai Lau 4112433d49c3SDaniel Lezcano out: 4113433d49c3SDaniel Lezcano return ret; 4114433d49c3SDaniel Lezcano 4115d189634eSThomas Graf out_register_late_subsys: 4116d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 4117433d49c3SDaniel Lezcano fib6_rules_init: 4118433d49c3SDaniel Lezcano fib6_rules_cleanup(); 4119433d49c3SDaniel Lezcano xfrm6_init: 4120433d49c3SDaniel Lezcano xfrm6_fini(); 41212a0c451aSThomas Graf out_fib6_init: 41222a0c451aSThomas Graf fib6_gc_cleanup(); 41238ed67789SDaniel Lezcano out_register_subsys: 41248ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 41257e52b33bSDavid S. Miller out_register_inetpeer: 41267e52b33bSDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 4127fc66f95cSEric Dumazet out_dst_entries: 4128fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 4129433d49c3SDaniel Lezcano out_kmem_cache: 4130f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 4131433d49c3SDaniel Lezcano goto out; 41321da177e4SLinus Torvalds } 41331da177e4SLinus Torvalds 41341da177e4SLinus Torvalds void ip6_route_cleanup(void) 41351da177e4SLinus Torvalds { 41368ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 4137d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 4138101367c2SThomas Graf fib6_rules_cleanup(); 41391da177e4SLinus Torvalds xfrm6_fini(); 41401da177e4SLinus Torvalds fib6_gc_cleanup(); 4141c3426b47SDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 41428ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 414341bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 4144f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 41451da177e4SLinus Torvalds } 4146