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 9614832c30dSDavid Ahern /* called with rcu_lock held */ 9624832c30dSDavid Ahern static struct net_device *ip6_rt_get_dev_rcu(struct rt6_info *rt) 9634832c30dSDavid Ahern { 9644832c30dSDavid Ahern struct net_device *dev = rt->dst.dev; 9654832c30dSDavid Ahern 9664832c30dSDavid Ahern if (rt->rt6i_flags & RTF_LOCAL) { 9674832c30dSDavid Ahern /* for copies of local routes, dst->dev needs to be the 9684832c30dSDavid Ahern * device if it is a master device, the master device if 9694832c30dSDavid Ahern * device is enslaved, and the loopback as the default 9704832c30dSDavid Ahern */ 9714832c30dSDavid Ahern if (netif_is_l3_slave(dev) && 9724832c30dSDavid Ahern !rt6_need_strict(&rt->rt6i_dst.addr)) 9734832c30dSDavid Ahern dev = l3mdev_master_dev_rcu(dev); 9744832c30dSDavid Ahern else if (!netif_is_l3_master(dev)) 9754832c30dSDavid Ahern dev = dev_net(dev)->loopback_dev; 9764832c30dSDavid Ahern /* last case is netif_is_l3_master(dev) is true in which 9774832c30dSDavid Ahern * case we want dev returned to be dev 9784832c30dSDavid Ahern */ 9794832c30dSDavid Ahern } 9804832c30dSDavid Ahern 9814832c30dSDavid Ahern return dev; 9824832c30dSDavid Ahern } 9834832c30dSDavid Ahern 9848b9df265SMartin KaFai Lau static struct rt6_info *ip6_rt_cache_alloc(struct rt6_info *ort, 98521efcfa0SEric Dumazet const struct in6_addr *daddr, 986b71d1d42SEric Dumazet const struct in6_addr *saddr) 9871da177e4SLinus Torvalds { 9884832c30dSDavid Ahern struct net_device *dev; 9891da177e4SLinus Torvalds struct rt6_info *rt; 9901da177e4SLinus Torvalds 9911da177e4SLinus Torvalds /* 9921da177e4SLinus Torvalds * Clone the route. 9931da177e4SLinus Torvalds */ 9941da177e4SLinus Torvalds 995d52d3997SMartin KaFai Lau if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)) 99683a09abdSMartin KaFai Lau ort = (struct rt6_info *)ort->dst.from; 9971da177e4SLinus Torvalds 9984832c30dSDavid Ahern rcu_read_lock(); 9994832c30dSDavid Ahern dev = ip6_rt_get_dev_rcu(ort); 10004832c30dSDavid Ahern rt = __ip6_dst_alloc(dev_net(dev), dev, 0); 10014832c30dSDavid Ahern rcu_read_unlock(); 100283a09abdSMartin KaFai Lau if (!rt) 100383a09abdSMartin KaFai Lau return NULL; 100483a09abdSMartin KaFai Lau 100583a09abdSMartin KaFai Lau ip6_rt_copy_init(rt, ort); 10068b9df265SMartin KaFai Lau rt->rt6i_flags |= RTF_CACHE; 100783a09abdSMartin KaFai Lau rt->rt6i_metric = 0; 100883a09abdSMartin KaFai Lau rt->dst.flags |= DST_HOST; 100983a09abdSMartin KaFai Lau rt->rt6i_dst.addr = *daddr; 101083a09abdSMartin KaFai Lau rt->rt6i_dst.plen = 128; 10118b9df265SMartin KaFai Lau 10128b9df265SMartin KaFai Lau if (!rt6_is_gw_or_nonexthop(ort)) { 1013bb3c3686SDavid S. Miller if (ort->rt6i_dst.plen != 128 && 101421efcfa0SEric Dumazet ipv6_addr_equal(&ort->rt6i_dst.addr, daddr)) 101558c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 10161da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 10171da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 10184e3fd7a0SAlexey Dobriyan rt->rt6i_src.addr = *saddr; 10191da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 10201da177e4SLinus Torvalds } 10211da177e4SLinus Torvalds #endif 102295a9a5baSYOSHIFUJI Hideaki } 102395a9a5baSYOSHIFUJI Hideaki 1024299d9939SYOSHIFUJI Hideaki return rt; 1025299d9939SYOSHIFUJI Hideaki } 1026299d9939SYOSHIFUJI Hideaki 1027d52d3997SMartin KaFai Lau static struct rt6_info *ip6_rt_pcpu_alloc(struct rt6_info *rt) 1028d52d3997SMartin KaFai Lau { 10294832c30dSDavid Ahern struct net_device *dev; 1030d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1031d52d3997SMartin KaFai Lau 10324832c30dSDavid Ahern rcu_read_lock(); 10334832c30dSDavid Ahern dev = ip6_rt_get_dev_rcu(rt); 10344832c30dSDavid Ahern pcpu_rt = __ip6_dst_alloc(dev_net(dev), dev, rt->dst.flags); 10354832c30dSDavid Ahern rcu_read_unlock(); 1036d52d3997SMartin KaFai Lau if (!pcpu_rt) 1037d52d3997SMartin KaFai Lau return NULL; 1038d52d3997SMartin KaFai Lau ip6_rt_copy_init(pcpu_rt, rt); 1039d52d3997SMartin KaFai Lau pcpu_rt->rt6i_protocol = rt->rt6i_protocol; 1040d52d3997SMartin KaFai Lau pcpu_rt->rt6i_flags |= RTF_PCPU; 1041d52d3997SMartin KaFai Lau return pcpu_rt; 1042d52d3997SMartin KaFai Lau } 1043d52d3997SMartin KaFai Lau 1044d52d3997SMartin KaFai Lau /* It should be called with read_lock_bh(&tb6_lock) acquired */ 1045d52d3997SMartin KaFai Lau static struct rt6_info *rt6_get_pcpu_route(struct rt6_info *rt) 1046d52d3997SMartin KaFai Lau { 1047a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, **p; 1048d52d3997SMartin KaFai Lau 1049d52d3997SMartin KaFai Lau p = this_cpu_ptr(rt->rt6i_pcpu); 1050d52d3997SMartin KaFai Lau pcpu_rt = *p; 1051d52d3997SMartin KaFai Lau 1052a73e4195SMartin KaFai Lau if (pcpu_rt) { 1053a73e4195SMartin KaFai Lau dst_hold(&pcpu_rt->dst); 1054a73e4195SMartin KaFai Lau rt6_dst_from_metrics_check(pcpu_rt); 1055a73e4195SMartin KaFai Lau } 1056a73e4195SMartin KaFai Lau return pcpu_rt; 1057a73e4195SMartin KaFai Lau } 1058a73e4195SMartin KaFai Lau 1059a73e4195SMartin KaFai Lau static struct rt6_info *rt6_make_pcpu_route(struct rt6_info *rt) 1060a73e4195SMartin KaFai Lau { 10619c7370a1SMartin KaFai Lau struct fib6_table *table = rt->rt6i_table; 1062a73e4195SMartin KaFai Lau struct rt6_info *pcpu_rt, *prev, **p; 1063d52d3997SMartin KaFai Lau 1064d52d3997SMartin KaFai Lau pcpu_rt = ip6_rt_pcpu_alloc(rt); 1065d52d3997SMartin KaFai Lau if (!pcpu_rt) { 1066d52d3997SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 1067d52d3997SMartin KaFai Lau 10689c7370a1SMartin KaFai Lau dst_hold(&net->ipv6.ip6_null_entry->dst); 10699c7370a1SMartin KaFai Lau return net->ipv6.ip6_null_entry; 1070d52d3997SMartin KaFai Lau } 1071d52d3997SMartin KaFai Lau 10729c7370a1SMartin KaFai Lau read_lock_bh(&table->tb6_lock); 10739c7370a1SMartin KaFai Lau if (rt->rt6i_pcpu) { 1074a73e4195SMartin KaFai Lau p = this_cpu_ptr(rt->rt6i_pcpu); 1075d52d3997SMartin KaFai Lau prev = cmpxchg(p, NULL, pcpu_rt); 1076d52d3997SMartin KaFai Lau if (prev) { 1077d52d3997SMartin KaFai Lau /* If someone did it before us, return prev instead */ 1078587fea74SWei Wang dst_release_immediate(&pcpu_rt->dst); 1079d52d3997SMartin KaFai Lau pcpu_rt = prev; 1080d52d3997SMartin KaFai Lau } 10819c7370a1SMartin KaFai Lau } else { 10829c7370a1SMartin KaFai Lau /* rt has been removed from the fib6 tree 10839c7370a1SMartin KaFai Lau * before we have a chance to acquire the read_lock. 10849c7370a1SMartin KaFai Lau * In this case, don't brother to create a pcpu rt 10859c7370a1SMartin KaFai Lau * since rt is going away anyway. The next 10869c7370a1SMartin KaFai Lau * dst_check() will trigger a re-lookup. 10879c7370a1SMartin KaFai Lau */ 1088587fea74SWei Wang dst_release_immediate(&pcpu_rt->dst); 10899c7370a1SMartin KaFai Lau pcpu_rt = rt; 10909c7370a1SMartin KaFai Lau } 1091d52d3997SMartin KaFai Lau dst_hold(&pcpu_rt->dst); 1092d52d3997SMartin KaFai Lau rt6_dst_from_metrics_check(pcpu_rt); 10939c7370a1SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 1094d52d3997SMartin KaFai Lau return pcpu_rt; 1095d52d3997SMartin KaFai Lau } 1096d52d3997SMartin KaFai Lau 10979ff74384SDavid Ahern struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, 10989ff74384SDavid Ahern int oif, struct flowi6 *fl6, int flags) 10991da177e4SLinus Torvalds { 1100367efcb9SMartin KaFai Lau struct fib6_node *fn, *saved_fn; 110145e4fd26SMartin KaFai Lau struct rt6_info *rt; 1102c71099acSThomas Graf int strict = 0; 11031da177e4SLinus Torvalds 110477d16f45SYOSHIFUJI Hideaki strict |= flags & RT6_LOOKUP_F_IFACE; 1105d5d32e4bSDavid Ahern strict |= flags & RT6_LOOKUP_F_IGNORE_LINKSTATE; 1106367efcb9SMartin KaFai Lau if (net->ipv6.devconf_all->forwarding == 0) 1107367efcb9SMartin KaFai Lau strict |= RT6_LOOKUP_F_REACHABLE; 11081da177e4SLinus Torvalds 1109c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 11101da177e4SLinus Torvalds 11114c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1112367efcb9SMartin KaFai Lau saved_fn = fn; 11131da177e4SLinus Torvalds 1114ca254490SDavid Ahern if (fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) 1115ca254490SDavid Ahern oif = 0; 1116ca254490SDavid Ahern 1117a3c00e46SMartin KaFai Lau redo_rt6_select: 1118367efcb9SMartin KaFai Lau rt = rt6_select(fn, oif, strict); 111952bd4c0cSNicolas Dichtel if (rt->rt6i_nsiblings) 1120367efcb9SMartin KaFai Lau rt = rt6_multipath_select(rt, fl6, oif, strict); 1121a3c00e46SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 1122a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1123a3c00e46SMartin KaFai Lau if (fn) 1124a3c00e46SMartin KaFai Lau goto redo_rt6_select; 1125367efcb9SMartin KaFai Lau else if (strict & RT6_LOOKUP_F_REACHABLE) { 1126367efcb9SMartin KaFai Lau /* also consider unreachable route */ 1127367efcb9SMartin KaFai Lau strict &= ~RT6_LOOKUP_F_REACHABLE; 1128367efcb9SMartin KaFai Lau fn = saved_fn; 1129367efcb9SMartin KaFai Lau goto redo_rt6_select; 1130367efcb9SMartin KaFai Lau } 1131a3c00e46SMartin KaFai Lau } 1132a3c00e46SMartin KaFai Lau 1133d52d3997SMartin KaFai Lau 1134d52d3997SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry || (rt->rt6i_flags & RTF_CACHE)) { 11353da59bd9SMartin KaFai Lau dst_use(&rt->dst, jiffies); 1136c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 11371da177e4SLinus Torvalds 1138d52d3997SMartin KaFai Lau rt6_dst_from_metrics_check(rt); 1139b811580dSDavid Ahern 1140b811580dSDavid Ahern trace_fib6_table_lookup(net, rt, table->tb6_id, fl6); 1141d52d3997SMartin KaFai Lau return rt; 11423da59bd9SMartin KaFai Lau } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) && 11433da59bd9SMartin KaFai Lau !(rt->rt6i_flags & RTF_GATEWAY))) { 11443da59bd9SMartin KaFai Lau /* Create a RTF_CACHE clone which will not be 11453da59bd9SMartin KaFai Lau * owned by the fib6 tree. It is for the special case where 11463da59bd9SMartin KaFai Lau * the daddr in the skb during the neighbor look-up is different 11473da59bd9SMartin KaFai Lau * from the fl6->daddr used to look-up route here. 11483da59bd9SMartin KaFai Lau */ 1149c71099acSThomas Graf 11503da59bd9SMartin KaFai Lau struct rt6_info *uncached_rt; 11513da59bd9SMartin KaFai Lau 1152d52d3997SMartin KaFai Lau dst_use(&rt->dst, jiffies); 1153d52d3997SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 1154d52d3997SMartin KaFai Lau 11553da59bd9SMartin KaFai Lau uncached_rt = ip6_rt_cache_alloc(rt, &fl6->daddr, NULL); 11563da59bd9SMartin KaFai Lau dst_release(&rt->dst); 11573da59bd9SMartin KaFai Lau 11581cfb71eeSWei Wang if (uncached_rt) { 11591cfb71eeSWei Wang /* Uncached_rt's refcnt is taken during ip6_rt_cache_alloc() 11601cfb71eeSWei Wang * No need for another dst_hold() 11611cfb71eeSWei Wang */ 11628d0b94afSMartin KaFai Lau rt6_uncached_list_add(uncached_rt); 11631cfb71eeSWei Wang } else { 11643da59bd9SMartin KaFai Lau uncached_rt = net->ipv6.ip6_null_entry; 11653da59bd9SMartin KaFai Lau dst_hold(&uncached_rt->dst); 11661cfb71eeSWei Wang } 1167b811580dSDavid Ahern 1168b811580dSDavid Ahern trace_fib6_table_lookup(net, uncached_rt, table->tb6_id, fl6); 11693da59bd9SMartin KaFai Lau return uncached_rt; 11703da59bd9SMartin KaFai Lau 1171d52d3997SMartin KaFai Lau } else { 1172d52d3997SMartin KaFai Lau /* Get a percpu copy */ 1173d52d3997SMartin KaFai Lau 1174d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1175d52d3997SMartin KaFai Lau 1176d52d3997SMartin KaFai Lau rt->dst.lastuse = jiffies; 1177d52d3997SMartin KaFai Lau rt->dst.__use++; 1178d52d3997SMartin KaFai Lau pcpu_rt = rt6_get_pcpu_route(rt); 1179d52d3997SMartin KaFai Lau 11809c7370a1SMartin KaFai Lau if (pcpu_rt) { 1181d52d3997SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 11829c7370a1SMartin KaFai Lau } else { 11839c7370a1SMartin KaFai Lau /* We have to do the read_unlock first 11849c7370a1SMartin KaFai Lau * because rt6_make_pcpu_route() may trigger 11859c7370a1SMartin KaFai Lau * ip6_dst_gc() which will take the write_lock. 11869c7370a1SMartin KaFai Lau */ 11879c7370a1SMartin KaFai Lau dst_hold(&rt->dst); 11889c7370a1SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 11899c7370a1SMartin KaFai Lau pcpu_rt = rt6_make_pcpu_route(rt); 11909c7370a1SMartin KaFai Lau dst_release(&rt->dst); 11919c7370a1SMartin KaFai Lau } 1192d52d3997SMartin KaFai Lau 1193b811580dSDavid Ahern trace_fib6_table_lookup(net, pcpu_rt, table->tb6_id, fl6); 1194d52d3997SMartin KaFai Lau return pcpu_rt; 11959c7370a1SMartin KaFai Lau 1196d52d3997SMartin KaFai Lau } 1197c71099acSThomas Graf } 11989ff74384SDavid Ahern EXPORT_SYMBOL_GPL(ip6_pol_route); 1199c71099acSThomas Graf 12008ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table, 12014c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 12024acad72dSPavel Emelyanov { 12034c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags); 12044acad72dSPavel Emelyanov } 12054acad72dSPavel Emelyanov 1206d409b847SMahesh Bandewar struct dst_entry *ip6_route_input_lookup(struct net *net, 120772331bc0SShmulik Ladkani struct net_device *dev, 120872331bc0SShmulik Ladkani struct flowi6 *fl6, int flags) 120972331bc0SShmulik Ladkani { 121072331bc0SShmulik Ladkani if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) 121172331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_IFACE; 121272331bc0SShmulik Ladkani 121372331bc0SShmulik Ladkani return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input); 121472331bc0SShmulik Ladkani } 1215d409b847SMahesh Bandewar EXPORT_SYMBOL_GPL(ip6_route_input_lookup); 121672331bc0SShmulik Ladkani 1217*23aebdacSJakub Sitnicki static void ip6_multipath_l3_keys(const struct sk_buff *skb, 1218*23aebdacSJakub Sitnicki struct flow_keys *keys) 1219*23aebdacSJakub Sitnicki { 1220*23aebdacSJakub Sitnicki const struct ipv6hdr *outer_iph = ipv6_hdr(skb); 1221*23aebdacSJakub Sitnicki const struct ipv6hdr *key_iph = outer_iph; 1222*23aebdacSJakub Sitnicki const struct ipv6hdr *inner_iph; 1223*23aebdacSJakub Sitnicki const struct icmp6hdr *icmph; 1224*23aebdacSJakub Sitnicki struct ipv6hdr _inner_iph; 1225*23aebdacSJakub Sitnicki 1226*23aebdacSJakub Sitnicki if (likely(outer_iph->nexthdr != IPPROTO_ICMPV6)) 1227*23aebdacSJakub Sitnicki goto out; 1228*23aebdacSJakub Sitnicki 1229*23aebdacSJakub Sitnicki icmph = icmp6_hdr(skb); 1230*23aebdacSJakub Sitnicki if (icmph->icmp6_type != ICMPV6_DEST_UNREACH && 1231*23aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_PKT_TOOBIG && 1232*23aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_TIME_EXCEED && 1233*23aebdacSJakub Sitnicki icmph->icmp6_type != ICMPV6_PARAMPROB) 1234*23aebdacSJakub Sitnicki goto out; 1235*23aebdacSJakub Sitnicki 1236*23aebdacSJakub Sitnicki inner_iph = skb_header_pointer(skb, 1237*23aebdacSJakub Sitnicki skb_transport_offset(skb) + sizeof(*icmph), 1238*23aebdacSJakub Sitnicki sizeof(_inner_iph), &_inner_iph); 1239*23aebdacSJakub Sitnicki if (!inner_iph) 1240*23aebdacSJakub Sitnicki goto out; 1241*23aebdacSJakub Sitnicki 1242*23aebdacSJakub Sitnicki key_iph = inner_iph; 1243*23aebdacSJakub Sitnicki out: 1244*23aebdacSJakub Sitnicki memset(keys, 0, sizeof(*keys)); 1245*23aebdacSJakub Sitnicki keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 1246*23aebdacSJakub Sitnicki keys->addrs.v6addrs.src = key_iph->saddr; 1247*23aebdacSJakub Sitnicki keys->addrs.v6addrs.dst = key_iph->daddr; 1248*23aebdacSJakub Sitnicki keys->tags.flow_label = ip6_flowinfo(key_iph); 1249*23aebdacSJakub Sitnicki keys->basic.ip_proto = key_iph->nexthdr; 1250*23aebdacSJakub Sitnicki } 1251*23aebdacSJakub Sitnicki 1252*23aebdacSJakub Sitnicki /* if skb is set it will be used and fl6 can be NULL */ 1253*23aebdacSJakub Sitnicki u32 rt6_multipath_hash(const struct flowi6 *fl6, const struct sk_buff *skb) 1254*23aebdacSJakub Sitnicki { 1255*23aebdacSJakub Sitnicki struct flow_keys hash_keys; 1256*23aebdacSJakub Sitnicki 1257*23aebdacSJakub Sitnicki if (skb) { 1258*23aebdacSJakub Sitnicki ip6_multipath_l3_keys(skb, &hash_keys); 1259*23aebdacSJakub Sitnicki return flow_hash_from_keys(&hash_keys); 1260*23aebdacSJakub Sitnicki } 1261*23aebdacSJakub Sitnicki 1262*23aebdacSJakub Sitnicki return get_hash_from_flowi6(fl6); 1263*23aebdacSJakub Sitnicki } 1264*23aebdacSJakub Sitnicki 1265c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 1266c71099acSThomas Graf { 1267b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 1268c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 1269adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 1270904af04dSJiri Benc struct ip_tunnel_info *tun_info; 12714c9483b2SDavid S. Miller struct flowi6 fl6 = { 1272e0d56fddSDavid Ahern .flowi6_iif = skb->dev->ifindex, 12734c9483b2SDavid S. Miller .daddr = iph->daddr, 12744c9483b2SDavid S. Miller .saddr = iph->saddr, 12756502ca52SYOSHIFUJI Hideaki / 吉藤英明 .flowlabel = ip6_flowinfo(iph), 12764c9483b2SDavid S. Miller .flowi6_mark = skb->mark, 12774c9483b2SDavid S. Miller .flowi6_proto = iph->nexthdr, 1278c71099acSThomas Graf }; 1279adaa70bbSThomas Graf 1280904af04dSJiri Benc tun_info = skb_tunnel_info(skb); 128146fa062aSJiri Benc if (tun_info && !(tun_info->mode & IP_TUNNEL_INFO_TX)) 1282904af04dSJiri Benc fl6.flowi6_tun_key.tun_id = tun_info->key.tun_id; 1283*23aebdacSJakub Sitnicki if (unlikely(fl6.flowi6_proto == IPPROTO_ICMPV6)) 1284*23aebdacSJakub Sitnicki fl6.mp_hash = rt6_multipath_hash(&fl6, skb); 128506e9d040SJiri Benc skb_dst_drop(skb); 128672331bc0SShmulik Ladkani skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags)); 1287c71099acSThomas Graf } 1288c71099acSThomas Graf 12898ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table, 12904c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 1291c71099acSThomas Graf { 12924c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags); 1293c71099acSThomas Graf } 1294c71099acSThomas Graf 12956f21c96aSPaolo Abeni struct dst_entry *ip6_route_output_flags(struct net *net, const struct sock *sk, 12966f21c96aSPaolo Abeni struct flowi6 *fl6, int flags) 1297c71099acSThomas Graf { 1298d46a9d67SDavid Ahern bool any_src; 1299c71099acSThomas Graf 13004c1feac5SDavid Ahern if (rt6_need_strict(&fl6->daddr)) { 13014c1feac5SDavid Ahern struct dst_entry *dst; 13024c1feac5SDavid Ahern 13034c1feac5SDavid Ahern dst = l3mdev_link_scope_lookup(net, fl6); 1304ca254490SDavid Ahern if (dst) 1305ca254490SDavid Ahern return dst; 13064c1feac5SDavid Ahern } 1307ca254490SDavid Ahern 13081fb9489bSPavel Emelyanov fl6->flowi6_iif = LOOPBACK_IFINDEX; 13094dc27d1cSDavid McCullough 1310d46a9d67SDavid Ahern any_src = ipv6_addr_any(&fl6->saddr); 1311741a11d9SDavid Ahern if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr) || 1312d46a9d67SDavid Ahern (fl6->flowi6_oif && any_src)) 131377d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 1314c71099acSThomas Graf 1315d46a9d67SDavid Ahern if (!any_src) 1316adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 13170c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 13180c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 1319adaa70bbSThomas Graf 13204c9483b2SDavid S. Miller return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output); 13211da177e4SLinus Torvalds } 13226f21c96aSPaolo Abeni EXPORT_SYMBOL_GPL(ip6_route_output_flags); 13231da177e4SLinus Torvalds 13242774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 132514e50e57SDavid S. Miller { 13265c1e6aa3SDavid S. Miller struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; 13271dbe3252SWei Wang struct net_device *loopback_dev = net->loopback_dev; 132814e50e57SDavid S. Miller struct dst_entry *new = NULL; 132914e50e57SDavid S. Miller 13301dbe3252SWei Wang rt = dst_alloc(&ip6_dst_blackhole_ops, loopback_dev, 1, 1331b2a9c0edSWei Wang DST_OBSOLETE_NONE, 0); 133214e50e57SDavid S. Miller if (rt) { 13330a1f5962SMartin KaFai Lau rt6_info_init(rt); 13340a1f5962SMartin KaFai Lau 1335d8d1f30bSChangli Gao new = &rt->dst; 133614e50e57SDavid S. Miller new->__use = 1; 1337352e512cSHerbert Xu new->input = dst_discard; 1338ede2059dSEric W. Biederman new->output = dst_discard_out; 133914e50e57SDavid S. Miller 1340defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 134114e50e57SDavid S. Miller 13421dbe3252SWei Wang rt->rt6i_idev = in6_dev_get(loopback_dev); 13434e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 13440a1f5962SMartin KaFai Lau rt->rt6i_flags = ort->rt6i_flags & ~RTF_PCPU; 134514e50e57SDavid S. Miller rt->rt6i_metric = 0; 134614e50e57SDavid S. Miller 134714e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 134814e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 134914e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 135014e50e57SDavid S. Miller #endif 135114e50e57SDavid S. Miller } 135214e50e57SDavid S. Miller 135369ead7afSDavid S. Miller dst_release(dst_orig); 135469ead7afSDavid S. Miller return new ? new : ERR_PTR(-ENOMEM); 135514e50e57SDavid S. Miller } 135614e50e57SDavid S. Miller 13571da177e4SLinus Torvalds /* 13581da177e4SLinus Torvalds * Destination cache support functions 13591da177e4SLinus Torvalds */ 13601da177e4SLinus Torvalds 13614b32b5adSMartin KaFai Lau static void rt6_dst_from_metrics_check(struct rt6_info *rt) 13624b32b5adSMartin KaFai Lau { 13634b32b5adSMartin KaFai Lau if (rt->dst.from && 13644b32b5adSMartin KaFai Lau dst_metrics_ptr(&rt->dst) != dst_metrics_ptr(rt->dst.from)) 13654b32b5adSMartin KaFai Lau dst_init_metrics(&rt->dst, dst_metrics_ptr(rt->dst.from), true); 13664b32b5adSMartin KaFai Lau } 13674b32b5adSMartin KaFai Lau 13683da59bd9SMartin KaFai Lau static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie) 13693da59bd9SMartin KaFai Lau { 13703da59bd9SMartin KaFai Lau if (!rt->rt6i_node || (rt->rt6i_node->fn_sernum != cookie)) 13713da59bd9SMartin KaFai Lau return NULL; 13723da59bd9SMartin KaFai Lau 13733da59bd9SMartin KaFai Lau if (rt6_check_expired(rt)) 13743da59bd9SMartin KaFai Lau return NULL; 13753da59bd9SMartin KaFai Lau 13763da59bd9SMartin KaFai Lau return &rt->dst; 13773da59bd9SMartin KaFai Lau } 13783da59bd9SMartin KaFai Lau 13793da59bd9SMartin KaFai Lau static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, u32 cookie) 13803da59bd9SMartin KaFai Lau { 13815973fb1eSMartin KaFai Lau if (!__rt6_check_expired(rt) && 13825973fb1eSMartin KaFai Lau rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && 13833da59bd9SMartin KaFai Lau rt6_check((struct rt6_info *)(rt->dst.from), cookie)) 13843da59bd9SMartin KaFai Lau return &rt->dst; 13853da59bd9SMartin KaFai Lau else 13863da59bd9SMartin KaFai Lau return NULL; 13873da59bd9SMartin KaFai Lau } 13883da59bd9SMartin KaFai Lau 13891da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 13901da177e4SLinus Torvalds { 13911da177e4SLinus Torvalds struct rt6_info *rt; 13921da177e4SLinus Torvalds 13931da177e4SLinus Torvalds rt = (struct rt6_info *) dst; 13941da177e4SLinus Torvalds 13956f3118b5SNicolas Dichtel /* All IPV6 dsts are created with ->obsolete set to the value 13966f3118b5SNicolas Dichtel * DST_OBSOLETE_FORCE_CHK which forces validation calls down 13976f3118b5SNicolas Dichtel * into this function always. 13986f3118b5SNicolas Dichtel */ 1399e3bc10bdSHannes Frederic Sowa 14004b32b5adSMartin KaFai Lau rt6_dst_from_metrics_check(rt); 14014b32b5adSMartin KaFai Lau 140202bcf4e0SMartin KaFai Lau if (rt->rt6i_flags & RTF_PCPU || 1403a4c2fd7fSWei Wang (unlikely(!list_empty(&rt->rt6i_uncached)) && rt->dst.from)) 14043da59bd9SMartin KaFai Lau return rt6_dst_from_check(rt, cookie); 14053da59bd9SMartin KaFai Lau else 14063da59bd9SMartin KaFai Lau return rt6_check(rt, cookie); 14071da177e4SLinus Torvalds } 14081da177e4SLinus Torvalds 14091da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 14101da177e4SLinus Torvalds { 14111da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds if (rt) { 141454c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 141554c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 1416e0a1ad73SThomas Graf ip6_del_rt(rt); 141754c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 14181da177e4SLinus Torvalds } 141954c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 142054c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 142154c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 142254c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 142354c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 142454c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 14251da177e4SLinus Torvalds } 14261da177e4SLinus Torvalds 14271da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 14281da177e4SLinus Torvalds { 14291da177e4SLinus Torvalds struct rt6_info *rt; 14301da177e4SLinus Torvalds 14313ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 14321da177e4SLinus Torvalds 1433adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 14341da177e4SLinus Torvalds if (rt) { 14351eb4f758SHannes Frederic Sowa if (rt->rt6i_flags & RTF_CACHE) { 1436ad65a2f0SWei Wang if (dst_hold_safe(&rt->dst)) 14378e3d5be7SMartin KaFai Lau ip6_del_rt(rt); 14381eb4f758SHannes Frederic Sowa } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) { 14391da177e4SLinus Torvalds rt->rt6i_node->fn_sernum = -1; 14401da177e4SLinus Torvalds } 14411da177e4SLinus Torvalds } 14421eb4f758SHannes Frederic Sowa } 14431da177e4SLinus Torvalds 144445e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu) 144545e4fd26SMartin KaFai Lau { 144645e4fd26SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 144745e4fd26SMartin KaFai Lau 144845e4fd26SMartin KaFai Lau rt->rt6i_flags |= RTF_MODIFIED; 144945e4fd26SMartin KaFai Lau rt->rt6i_pmtu = mtu; 145045e4fd26SMartin KaFai Lau rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires); 145145e4fd26SMartin KaFai Lau } 145245e4fd26SMartin KaFai Lau 14530d3f6d29SMartin KaFai Lau static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt) 14540d3f6d29SMartin KaFai Lau { 14550d3f6d29SMartin KaFai Lau return !(rt->rt6i_flags & RTF_CACHE) && 14560d3f6d29SMartin KaFai Lau (rt->rt6i_flags & RTF_PCPU || rt->rt6i_node); 14570d3f6d29SMartin KaFai Lau } 14580d3f6d29SMartin KaFai Lau 145945e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk, 146045e4fd26SMartin KaFai Lau const struct ipv6hdr *iph, u32 mtu) 14611da177e4SLinus Torvalds { 14620dec879fSJulian Anastasov const struct in6_addr *daddr, *saddr; 14631da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info *)dst; 14641da177e4SLinus Torvalds 146545e4fd26SMartin KaFai Lau if (rt6->rt6i_flags & RTF_LOCAL) 146645e4fd26SMartin KaFai Lau return; 146745e4fd26SMartin KaFai Lau 146819bda36cSXin Long if (dst_metric_locked(dst, RTAX_MTU)) 146919bda36cSXin Long return; 147019bda36cSXin Long 147145e4fd26SMartin KaFai Lau if (iph) { 147245e4fd26SMartin KaFai Lau daddr = &iph->daddr; 147345e4fd26SMartin KaFai Lau saddr = &iph->saddr; 147445e4fd26SMartin KaFai Lau } else if (sk) { 147545e4fd26SMartin KaFai Lau daddr = &sk->sk_v6_daddr; 147645e4fd26SMartin KaFai Lau saddr = &inet6_sk(sk)->saddr; 147745e4fd26SMartin KaFai Lau } else { 14780dec879fSJulian Anastasov daddr = NULL; 14790dec879fSJulian Anastasov saddr = NULL; 14801da177e4SLinus Torvalds } 14810dec879fSJulian Anastasov dst_confirm_neigh(dst, daddr); 14820dec879fSJulian Anastasov mtu = max_t(u32, mtu, IPV6_MIN_MTU); 14830dec879fSJulian Anastasov if (mtu >= dst_mtu(dst)) 14840dec879fSJulian Anastasov return; 14850dec879fSJulian Anastasov 14860dec879fSJulian Anastasov if (!rt6_cache_allowed_for_pmtu(rt6)) { 14870dec879fSJulian Anastasov rt6_do_update_pmtu(rt6, mtu); 14880dec879fSJulian Anastasov } else if (daddr) { 14890dec879fSJulian Anastasov struct rt6_info *nrt6; 14900dec879fSJulian Anastasov 149145e4fd26SMartin KaFai Lau nrt6 = ip6_rt_cache_alloc(rt6, daddr, saddr); 149245e4fd26SMartin KaFai Lau if (nrt6) { 149345e4fd26SMartin KaFai Lau rt6_do_update_pmtu(nrt6, mtu); 149445e4fd26SMartin KaFai Lau 149545e4fd26SMartin KaFai Lau /* ip6_ins_rt(nrt6) will bump the 149645e4fd26SMartin KaFai Lau * rt6->rt6i_node->fn_sernum 149745e4fd26SMartin KaFai Lau * which will fail the next rt6_check() and 149845e4fd26SMartin KaFai Lau * invalidate the sk->sk_dst_cache. 149945e4fd26SMartin KaFai Lau */ 150045e4fd26SMartin KaFai Lau ip6_ins_rt(nrt6); 15011cfb71eeSWei Wang /* Release the reference taken in 15021cfb71eeSWei Wang * ip6_rt_cache_alloc() 15031cfb71eeSWei Wang */ 15041cfb71eeSWei Wang dst_release(&nrt6->dst); 150545e4fd26SMartin KaFai Lau } 150645e4fd26SMartin KaFai Lau } 150745e4fd26SMartin KaFai Lau } 150845e4fd26SMartin KaFai Lau 150945e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 151045e4fd26SMartin KaFai Lau struct sk_buff *skb, u32 mtu) 151145e4fd26SMartin KaFai Lau { 151245e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu); 15131da177e4SLinus Torvalds } 15141da177e4SLinus Torvalds 151542ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, 1516e2d118a1SLorenzo Colitti int oif, u32 mark, kuid_t uid) 151781aded24SDavid S. Miller { 151881aded24SDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 151981aded24SDavid S. Miller struct dst_entry *dst; 152081aded24SDavid S. Miller struct flowi6 fl6; 152181aded24SDavid S. Miller 152281aded24SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 152381aded24SDavid S. Miller fl6.flowi6_oif = oif; 15241b3c61dcSLorenzo Colitti fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark); 152581aded24SDavid S. Miller fl6.daddr = iph->daddr; 152681aded24SDavid S. Miller fl6.saddr = iph->saddr; 15276502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 1528e2d118a1SLorenzo Colitti fl6.flowi6_uid = uid; 152981aded24SDavid S. Miller 153081aded24SDavid S. Miller dst = ip6_route_output(net, NULL, &fl6); 153181aded24SDavid S. Miller if (!dst->error) 153245e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu)); 153381aded24SDavid S. Miller dst_release(dst); 153481aded24SDavid S. Miller } 153581aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu); 153681aded24SDavid S. Miller 153781aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) 153881aded24SDavid S. Miller { 153933c162a9SMartin KaFai Lau struct dst_entry *dst; 154033c162a9SMartin KaFai Lau 154181aded24SDavid S. Miller ip6_update_pmtu(skb, sock_net(sk), mtu, 1542e2d118a1SLorenzo Colitti sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid); 154333c162a9SMartin KaFai Lau 154433c162a9SMartin KaFai Lau dst = __sk_dst_get(sk); 154533c162a9SMartin KaFai Lau if (!dst || !dst->obsolete || 154633c162a9SMartin KaFai Lau dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) 154733c162a9SMartin KaFai Lau return; 154833c162a9SMartin KaFai Lau 154933c162a9SMartin KaFai Lau bh_lock_sock(sk); 155033c162a9SMartin KaFai Lau if (!sock_owned_by_user(sk) && !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) 155133c162a9SMartin KaFai Lau ip6_datagram_dst_update(sk, false); 155233c162a9SMartin KaFai Lau bh_unlock_sock(sk); 155381aded24SDavid S. Miller } 155481aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); 155581aded24SDavid S. Miller 1556b55b76b2SDuan Jiong /* Handle redirects */ 1557b55b76b2SDuan Jiong struct ip6rd_flowi { 1558b55b76b2SDuan Jiong struct flowi6 fl6; 1559b55b76b2SDuan Jiong struct in6_addr gateway; 1560b55b76b2SDuan Jiong }; 1561b55b76b2SDuan Jiong 1562b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net, 1563b55b76b2SDuan Jiong struct fib6_table *table, 1564b55b76b2SDuan Jiong struct flowi6 *fl6, 1565b55b76b2SDuan Jiong int flags) 1566b55b76b2SDuan Jiong { 1567b55b76b2SDuan Jiong struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; 1568b55b76b2SDuan Jiong struct rt6_info *rt; 1569b55b76b2SDuan Jiong struct fib6_node *fn; 1570b55b76b2SDuan Jiong 1571b55b76b2SDuan Jiong /* Get the "current" route for this destination and 157267c408cfSAlexander Alemayhu * check if the redirect has come from appropriate router. 1573b55b76b2SDuan Jiong * 1574b55b76b2SDuan Jiong * RFC 4861 specifies that redirects should only be 1575b55b76b2SDuan Jiong * accepted if they come from the nexthop to the target. 1576b55b76b2SDuan Jiong * Due to the way the routes are chosen, this notion 1577b55b76b2SDuan Jiong * is a bit fuzzy and one might need to check all possible 1578b55b76b2SDuan Jiong * routes. 1579b55b76b2SDuan Jiong */ 1580b55b76b2SDuan Jiong 1581b55b76b2SDuan Jiong read_lock_bh(&table->tb6_lock); 1582b55b76b2SDuan Jiong fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1583b55b76b2SDuan Jiong restart: 1584b55b76b2SDuan Jiong for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 1585b55b76b2SDuan Jiong if (rt6_check_expired(rt)) 1586b55b76b2SDuan Jiong continue; 1587b55b76b2SDuan Jiong if (rt->dst.error) 1588b55b76b2SDuan Jiong break; 1589b55b76b2SDuan Jiong if (!(rt->rt6i_flags & RTF_GATEWAY)) 1590b55b76b2SDuan Jiong continue; 1591b55b76b2SDuan Jiong if (fl6->flowi6_oif != rt->dst.dev->ifindex) 1592b55b76b2SDuan Jiong continue; 1593b55b76b2SDuan Jiong if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway)) 1594b55b76b2SDuan Jiong continue; 1595b55b76b2SDuan Jiong break; 1596b55b76b2SDuan Jiong } 1597b55b76b2SDuan Jiong 1598b55b76b2SDuan Jiong if (!rt) 1599b55b76b2SDuan Jiong rt = net->ipv6.ip6_null_entry; 1600b55b76b2SDuan Jiong else if (rt->dst.error) { 1601b55b76b2SDuan Jiong rt = net->ipv6.ip6_null_entry; 1602b0a1ba59SMartin KaFai Lau goto out; 1603b0a1ba59SMartin KaFai Lau } 1604b0a1ba59SMartin KaFai Lau 1605b0a1ba59SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 1606a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1607a3c00e46SMartin KaFai Lau if (fn) 1608a3c00e46SMartin KaFai Lau goto restart; 1609b55b76b2SDuan Jiong } 1610a3c00e46SMartin KaFai Lau 1611b0a1ba59SMartin KaFai Lau out: 1612b55b76b2SDuan Jiong dst_hold(&rt->dst); 1613b55b76b2SDuan Jiong 1614b55b76b2SDuan Jiong read_unlock_bh(&table->tb6_lock); 1615b55b76b2SDuan Jiong 1616b811580dSDavid Ahern trace_fib6_table_lookup(net, rt, table->tb6_id, fl6); 1617b55b76b2SDuan Jiong return rt; 1618b55b76b2SDuan Jiong }; 1619b55b76b2SDuan Jiong 1620b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net, 1621b55b76b2SDuan Jiong const struct flowi6 *fl6, 1622b55b76b2SDuan Jiong const struct in6_addr *gateway) 1623b55b76b2SDuan Jiong { 1624b55b76b2SDuan Jiong int flags = RT6_LOOKUP_F_HAS_SADDR; 1625b55b76b2SDuan Jiong struct ip6rd_flowi rdfl; 1626b55b76b2SDuan Jiong 1627b55b76b2SDuan Jiong rdfl.fl6 = *fl6; 1628b55b76b2SDuan Jiong rdfl.gateway = *gateway; 1629b55b76b2SDuan Jiong 1630b55b76b2SDuan Jiong return fib6_rule_lookup(net, &rdfl.fl6, 1631b55b76b2SDuan Jiong flags, __ip6_route_redirect); 1632b55b76b2SDuan Jiong } 1633b55b76b2SDuan Jiong 1634e2d118a1SLorenzo Colitti void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark, 1635e2d118a1SLorenzo Colitti kuid_t uid) 16363a5ad2eeSDavid S. Miller { 16373a5ad2eeSDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 16383a5ad2eeSDavid S. Miller struct dst_entry *dst; 16393a5ad2eeSDavid S. Miller struct flowi6 fl6; 16403a5ad2eeSDavid S. Miller 16413a5ad2eeSDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 1642e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 16433a5ad2eeSDavid S. Miller fl6.flowi6_oif = oif; 16443a5ad2eeSDavid S. Miller fl6.flowi6_mark = mark; 16453a5ad2eeSDavid S. Miller fl6.daddr = iph->daddr; 16463a5ad2eeSDavid S. Miller fl6.saddr = iph->saddr; 16476502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 1648e2d118a1SLorenzo Colitti fl6.flowi6_uid = uid; 16493a5ad2eeSDavid S. Miller 1650b55b76b2SDuan Jiong dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr); 16516700c270SDavid S. Miller rt6_do_redirect(dst, NULL, skb); 16523a5ad2eeSDavid S. Miller dst_release(dst); 16533a5ad2eeSDavid S. Miller } 16543a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect); 16553a5ad2eeSDavid S. Miller 1656c92a59ecSDuan Jiong void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif, 1657c92a59ecSDuan Jiong u32 mark) 1658c92a59ecSDuan Jiong { 1659c92a59ecSDuan Jiong const struct ipv6hdr *iph = ipv6_hdr(skb); 1660c92a59ecSDuan Jiong const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); 1661c92a59ecSDuan Jiong struct dst_entry *dst; 1662c92a59ecSDuan Jiong struct flowi6 fl6; 1663c92a59ecSDuan Jiong 1664c92a59ecSDuan Jiong memset(&fl6, 0, sizeof(fl6)); 1665e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 1666c92a59ecSDuan Jiong fl6.flowi6_oif = oif; 1667c92a59ecSDuan Jiong fl6.flowi6_mark = mark; 1668c92a59ecSDuan Jiong fl6.daddr = msg->dest; 1669c92a59ecSDuan Jiong fl6.saddr = iph->daddr; 1670e2d118a1SLorenzo Colitti fl6.flowi6_uid = sock_net_uid(net, NULL); 1671c92a59ecSDuan Jiong 1672b55b76b2SDuan Jiong dst = ip6_route_redirect(net, &fl6, &iph->saddr); 1673c92a59ecSDuan Jiong rt6_do_redirect(dst, NULL, skb); 1674c92a59ecSDuan Jiong dst_release(dst); 1675c92a59ecSDuan Jiong } 1676c92a59ecSDuan Jiong 16773a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) 16783a5ad2eeSDavid S. Miller { 1679e2d118a1SLorenzo Colitti ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark, 1680e2d118a1SLorenzo Colitti sk->sk_uid); 16813a5ad2eeSDavid S. Miller } 16823a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect); 16833a5ad2eeSDavid S. Miller 16840dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 16851da177e4SLinus Torvalds { 16860dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 16870dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 16880dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 16890dbaee3bSDavid S. Miller 16901da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 16911da177e4SLinus Torvalds 16925578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 16935578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 16941da177e4SLinus Torvalds 16951da177e4SLinus Torvalds /* 16961da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 16971da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 16981da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 16991da177e4SLinus Torvalds * rely only on pmtu discovery" 17001da177e4SLinus Torvalds */ 17011da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 17021da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 17031da177e4SLinus Torvalds return mtu; 17041da177e4SLinus Torvalds } 17051da177e4SLinus Torvalds 1706ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst) 1707d33e4553SDavid S. Miller { 17084b32b5adSMartin KaFai Lau const struct rt6_info *rt = (const struct rt6_info *)dst; 17094b32b5adSMartin KaFai Lau unsigned int mtu = rt->rt6i_pmtu; 1710d33e4553SDavid S. Miller struct inet6_dev *idev; 1711618f9bc7SSteffen Klassert 1712618f9bc7SSteffen Klassert if (mtu) 171330f78d8eSEric Dumazet goto out; 1714618f9bc7SSteffen Klassert 17154b32b5adSMartin KaFai Lau mtu = dst_metric_raw(dst, RTAX_MTU); 17164b32b5adSMartin KaFai Lau if (mtu) 17174b32b5adSMartin KaFai Lau goto out; 17184b32b5adSMartin KaFai Lau 1719618f9bc7SSteffen Klassert mtu = IPV6_MIN_MTU; 1720d33e4553SDavid S. Miller 1721d33e4553SDavid S. Miller rcu_read_lock(); 1722d33e4553SDavid S. Miller idev = __in6_dev_get(dst->dev); 1723d33e4553SDavid S. Miller if (idev) 1724d33e4553SDavid S. Miller mtu = idev->cnf.mtu6; 1725d33e4553SDavid S. Miller rcu_read_unlock(); 1726d33e4553SDavid S. Miller 172730f78d8eSEric Dumazet out: 172814972cbdSRoopa Prabhu mtu = min_t(unsigned int, mtu, IP6_MAX_MTU); 172914972cbdSRoopa Prabhu 173014972cbdSRoopa Prabhu return mtu - lwtunnel_headroom(dst->lwtstate, mtu); 1731d33e4553SDavid S. Miller } 1732d33e4553SDavid S. Miller 17333b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 173487a11578SDavid S. Miller struct flowi6 *fl6) 17351da177e4SLinus Torvalds { 173687a11578SDavid S. Miller struct dst_entry *dst; 17371da177e4SLinus Torvalds struct rt6_info *rt; 17381da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 1739c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 17401da177e4SLinus Torvalds 174138308473SDavid S. Miller if (unlikely(!idev)) 1742122bdf67SEric Dumazet return ERR_PTR(-ENODEV); 17431da177e4SLinus Torvalds 1744ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, dev, 0); 174538308473SDavid S. Miller if (unlikely(!rt)) { 17461da177e4SLinus Torvalds in6_dev_put(idev); 174787a11578SDavid S. Miller dst = ERR_PTR(-ENOMEM); 17481da177e4SLinus Torvalds goto out; 17491da177e4SLinus Torvalds } 17501da177e4SLinus Torvalds 17518e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 17528e2ec639SYan, Zheng rt->dst.output = ip6_output; 1753550bab42SJulian Anastasov rt->rt6i_gateway = fl6->daddr; 175487a11578SDavid S. Miller rt->rt6i_dst.addr = fl6->daddr; 17558e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 17568e2ec639SYan, Zheng rt->rt6i_idev = idev; 175714edd87dSLi RongQing dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); 17581da177e4SLinus Torvalds 1759587fea74SWei Wang /* Add this dst into uncached_list so that rt6_ifdown() can 1760587fea74SWei Wang * do proper release of the net_device 1761587fea74SWei Wang */ 1762587fea74SWei Wang rt6_uncached_list_add(rt); 17631da177e4SLinus Torvalds 176487a11578SDavid S. Miller dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); 176587a11578SDavid S. Miller 17661da177e4SLinus Torvalds out: 176787a11578SDavid S. Miller return dst; 17681da177e4SLinus Torvalds } 17691da177e4SLinus Torvalds 1770569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops) 17711da177e4SLinus Torvalds { 177286393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 17737019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 17747019b78eSDaniel Lezcano int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; 17757019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 17767019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 17777019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 1778fc66f95cSEric Dumazet int entries; 17791da177e4SLinus Torvalds 1780fc66f95cSEric Dumazet entries = dst_entries_get_fast(ops); 178149a18d86SMichal Kubeček if (time_after(rt_last_gc + rt_min_interval, jiffies) && 1782fc66f95cSEric Dumazet entries <= rt_max_size) 17831da177e4SLinus Torvalds goto out; 17841da177e4SLinus Torvalds 17856891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire++; 178614956643SLi RongQing fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true); 1787fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 1788fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 17897019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; 17901da177e4SLinus Torvalds out: 17917019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; 1792fc66f95cSEric Dumazet return entries > rt_max_size; 17931da177e4SLinus Torvalds } 17941da177e4SLinus Torvalds 1795e715b6d3SFlorian Westphal static int ip6_convert_metrics(struct mx6_config *mxc, 1796e715b6d3SFlorian Westphal const struct fib6_config *cfg) 1797e715b6d3SFlorian Westphal { 1798c3a8d947SDaniel Borkmann bool ecn_ca = false; 1799e715b6d3SFlorian Westphal struct nlattr *nla; 1800e715b6d3SFlorian Westphal int remaining; 1801e715b6d3SFlorian Westphal u32 *mp; 1802e715b6d3SFlorian Westphal 180363159f29SIan Morris if (!cfg->fc_mx) 1804e715b6d3SFlorian Westphal return 0; 1805e715b6d3SFlorian Westphal 1806e715b6d3SFlorian Westphal mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL); 1807e715b6d3SFlorian Westphal if (unlikely(!mp)) 1808e715b6d3SFlorian Westphal return -ENOMEM; 1809e715b6d3SFlorian Westphal 1810e715b6d3SFlorian Westphal nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { 1811e715b6d3SFlorian Westphal int type = nla_type(nla); 1812ea697639SDaniel Borkmann u32 val; 1813ea697639SDaniel Borkmann 18141bb14807SDaniel Borkmann if (!type) 18151bb14807SDaniel Borkmann continue; 1816e715b6d3SFlorian Westphal if (unlikely(type > RTAX_MAX)) 1817e715b6d3SFlorian Westphal goto err; 18181bb14807SDaniel Borkmann 1819ea697639SDaniel Borkmann if (type == RTAX_CC_ALGO) { 1820ea697639SDaniel Borkmann char tmp[TCP_CA_NAME_MAX]; 1821e715b6d3SFlorian Westphal 1822ea697639SDaniel Borkmann nla_strlcpy(tmp, nla, sizeof(tmp)); 1823c3a8d947SDaniel Borkmann val = tcp_ca_get_key_by_name(tmp, &ecn_ca); 1824ea697639SDaniel Borkmann if (val == TCP_CA_UNSPEC) 1825ea697639SDaniel Borkmann goto err; 1826ea697639SDaniel Borkmann } else { 1827ea697639SDaniel Borkmann val = nla_get_u32(nla); 1828ea697639SDaniel Borkmann } 1829626abd59SPaolo Abeni if (type == RTAX_HOPLIMIT && val > 255) 1830626abd59SPaolo Abeni val = 255; 1831b8d3e416SDaniel Borkmann if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK)) 1832b8d3e416SDaniel Borkmann goto err; 1833ea697639SDaniel Borkmann 1834ea697639SDaniel Borkmann mp[type - 1] = val; 1835e715b6d3SFlorian Westphal __set_bit(type - 1, mxc->mx_valid); 1836e715b6d3SFlorian Westphal } 1837e715b6d3SFlorian Westphal 1838c3a8d947SDaniel Borkmann if (ecn_ca) { 1839c3a8d947SDaniel Borkmann __set_bit(RTAX_FEATURES - 1, mxc->mx_valid); 1840c3a8d947SDaniel Borkmann mp[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA; 1841c3a8d947SDaniel Borkmann } 1842e715b6d3SFlorian Westphal 1843c3a8d947SDaniel Borkmann mxc->mx = mp; 1844e715b6d3SFlorian Westphal return 0; 1845e715b6d3SFlorian Westphal err: 1846e715b6d3SFlorian Westphal kfree(mp); 1847e715b6d3SFlorian Westphal return -EINVAL; 1848e715b6d3SFlorian Westphal } 18491da177e4SLinus Torvalds 18508c14586fSDavid Ahern static struct rt6_info *ip6_nh_lookup_table(struct net *net, 18518c14586fSDavid Ahern struct fib6_config *cfg, 18528c14586fSDavid Ahern const struct in6_addr *gw_addr) 18538c14586fSDavid Ahern { 18548c14586fSDavid Ahern struct flowi6 fl6 = { 18558c14586fSDavid Ahern .flowi6_oif = cfg->fc_ifindex, 18568c14586fSDavid Ahern .daddr = *gw_addr, 18578c14586fSDavid Ahern .saddr = cfg->fc_prefsrc, 18588c14586fSDavid Ahern }; 18598c14586fSDavid Ahern struct fib6_table *table; 18608c14586fSDavid Ahern struct rt6_info *rt; 1861d5d32e4bSDavid Ahern int flags = RT6_LOOKUP_F_IFACE | RT6_LOOKUP_F_IGNORE_LINKSTATE; 18628c14586fSDavid Ahern 18638c14586fSDavid Ahern table = fib6_get_table(net, cfg->fc_table); 18648c14586fSDavid Ahern if (!table) 18658c14586fSDavid Ahern return NULL; 18668c14586fSDavid Ahern 18678c14586fSDavid Ahern if (!ipv6_addr_any(&cfg->fc_prefsrc)) 18688c14586fSDavid Ahern flags |= RT6_LOOKUP_F_HAS_SADDR; 18698c14586fSDavid Ahern 18708c14586fSDavid Ahern rt = ip6_pol_route(net, table, cfg->fc_ifindex, &fl6, flags); 18718c14586fSDavid Ahern 18728c14586fSDavid Ahern /* if table lookup failed, fall back to full lookup */ 18738c14586fSDavid Ahern if (rt == net->ipv6.ip6_null_entry) { 18748c14586fSDavid Ahern ip6_rt_put(rt); 18758c14586fSDavid Ahern rt = NULL; 18768c14586fSDavid Ahern } 18778c14586fSDavid Ahern 18788c14586fSDavid Ahern return rt; 18798c14586fSDavid Ahern } 18808c14586fSDavid Ahern 1881333c4301SDavid Ahern static struct rt6_info *ip6_route_info_create(struct fib6_config *cfg, 1882333c4301SDavid Ahern struct netlink_ext_ack *extack) 18831da177e4SLinus Torvalds { 18845578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 18851da177e4SLinus Torvalds struct rt6_info *rt = NULL; 18861da177e4SLinus Torvalds struct net_device *dev = NULL; 18871da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 1888c71099acSThomas Graf struct fib6_table *table; 18891da177e4SLinus Torvalds int addr_type; 18908c5b83f0SRoopa Prabhu int err = -EINVAL; 18911da177e4SLinus Torvalds 1892557c44beSDavid Ahern /* RTF_PCPU is an internal flag; can not be set by userspace */ 1893d5d531cbSDavid Ahern if (cfg->fc_flags & RTF_PCPU) { 1894d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Userspace can not set RTF_PCPU"); 1895557c44beSDavid Ahern goto out; 1896d5d531cbSDavid Ahern } 1897557c44beSDavid Ahern 1898d5d531cbSDavid Ahern if (cfg->fc_dst_len > 128) { 1899d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid prefix length"); 19008c5b83f0SRoopa Prabhu goto out; 1901d5d531cbSDavid Ahern } 1902d5d531cbSDavid Ahern if (cfg->fc_src_len > 128) { 1903d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address length"); 1904d5d531cbSDavid Ahern goto out; 1905d5d531cbSDavid Ahern } 19061da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 1907d5d531cbSDavid Ahern if (cfg->fc_src_len) { 1908d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 1909d5d531cbSDavid Ahern "Specifying source address requires IPV6_SUBTREES to be enabled"); 19108c5b83f0SRoopa Prabhu goto out; 1911d5d531cbSDavid Ahern } 19121da177e4SLinus Torvalds #endif 191386872cb5SThomas Graf if (cfg->fc_ifindex) { 19141da177e4SLinus Torvalds err = -ENODEV; 19155578689aSDaniel Lezcano dev = dev_get_by_index(net, cfg->fc_ifindex); 19161da177e4SLinus Torvalds if (!dev) 19171da177e4SLinus Torvalds goto out; 19181da177e4SLinus Torvalds idev = in6_dev_get(dev); 19191da177e4SLinus Torvalds if (!idev) 19201da177e4SLinus Torvalds goto out; 19211da177e4SLinus Torvalds } 19221da177e4SLinus Torvalds 192386872cb5SThomas Graf if (cfg->fc_metric == 0) 192486872cb5SThomas Graf cfg->fc_metric = IP6_RT_PRIO_USER; 19251da177e4SLinus Torvalds 1926c71099acSThomas Graf err = -ENOBUFS; 192738308473SDavid S. Miller if (cfg->fc_nlinfo.nlh && 1928d71314b4SMatti Vaittinen !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { 1929d71314b4SMatti Vaittinen table = fib6_get_table(net, cfg->fc_table); 193038308473SDavid S. Miller if (!table) { 1931f3213831SJoe Perches pr_warn("NLM_F_CREATE should be specified when creating new route\n"); 1932d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 1933d71314b4SMatti Vaittinen } 1934d71314b4SMatti Vaittinen } else { 1935d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 1936d71314b4SMatti Vaittinen } 193738308473SDavid S. Miller 193838308473SDavid S. Miller if (!table) 1939c71099acSThomas Graf goto out; 1940c71099acSThomas Graf 1941ad706862SMartin KaFai Lau rt = ip6_dst_alloc(net, NULL, 1942ad706862SMartin KaFai Lau (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT); 19431da177e4SLinus Torvalds 194438308473SDavid S. Miller if (!rt) { 19451da177e4SLinus Torvalds err = -ENOMEM; 19461da177e4SLinus Torvalds goto out; 19471da177e4SLinus Torvalds } 19481da177e4SLinus Torvalds 19491716a961SGao feng if (cfg->fc_flags & RTF_EXPIRES) 19501716a961SGao feng rt6_set_expires(rt, jiffies + 19511716a961SGao feng clock_t_to_jiffies(cfg->fc_expires)); 19521716a961SGao feng else 19531716a961SGao feng rt6_clean_expires(rt); 19541da177e4SLinus Torvalds 195586872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 195686872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 195786872cb5SThomas Graf rt->rt6i_protocol = cfg->fc_protocol; 195886872cb5SThomas Graf 195986872cb5SThomas Graf addr_type = ipv6_addr_type(&cfg->fc_dst); 19601da177e4SLinus Torvalds 19611da177e4SLinus Torvalds if (addr_type & IPV6_ADDR_MULTICAST) 1962d8d1f30bSChangli Gao rt->dst.input = ip6_mc_input; 1963ab79ad14SMaciej Żenczykowski else if (cfg->fc_flags & RTF_LOCAL) 1964ab79ad14SMaciej Żenczykowski rt->dst.input = ip6_input; 19651da177e4SLinus Torvalds else 1966d8d1f30bSChangli Gao rt->dst.input = ip6_forward; 19671da177e4SLinus Torvalds 1968d8d1f30bSChangli Gao rt->dst.output = ip6_output; 19691da177e4SLinus Torvalds 197019e42e45SRoopa Prabhu if (cfg->fc_encap) { 197119e42e45SRoopa Prabhu struct lwtunnel_state *lwtstate; 197219e42e45SRoopa Prabhu 197330357d7dSDavid Ahern err = lwtunnel_build_state(cfg->fc_encap_type, 1974127eb7cdSTom Herbert cfg->fc_encap, AF_INET6, cfg, 19759ae28727SDavid Ahern &lwtstate, extack); 197619e42e45SRoopa Prabhu if (err) 197719e42e45SRoopa Prabhu goto out; 197861adedf3SJiri Benc rt->dst.lwtstate = lwtstate_get(lwtstate); 197961adedf3SJiri Benc if (lwtunnel_output_redirect(rt->dst.lwtstate)) { 198061adedf3SJiri Benc rt->dst.lwtstate->orig_output = rt->dst.output; 198161adedf3SJiri Benc rt->dst.output = lwtunnel_output; 198219e42e45SRoopa Prabhu } 198361adedf3SJiri Benc if (lwtunnel_input_redirect(rt->dst.lwtstate)) { 198461adedf3SJiri Benc rt->dst.lwtstate->orig_input = rt->dst.input; 198561adedf3SJiri Benc rt->dst.input = lwtunnel_input; 198625368623STom Herbert } 198725368623STom Herbert } 198819e42e45SRoopa Prabhu 198986872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 199086872cb5SThomas Graf rt->rt6i_dst.plen = cfg->fc_dst_len; 1991afc4eef8SMartin KaFai Lau if (rt->rt6i_dst.plen == 128) 199211d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 19931da177e4SLinus Torvalds 19941da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 199586872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len); 199686872cb5SThomas Graf rt->rt6i_src.plen = cfg->fc_src_len; 19971da177e4SLinus Torvalds #endif 19981da177e4SLinus Torvalds 199986872cb5SThomas Graf rt->rt6i_metric = cfg->fc_metric; 20001da177e4SLinus Torvalds 20011da177e4SLinus Torvalds /* We cannot add true routes via loopback here, 20021da177e4SLinus Torvalds they would result in kernel looping; promote them to reject routes 20031da177e4SLinus Torvalds */ 200486872cb5SThomas Graf if ((cfg->fc_flags & RTF_REJECT) || 200538308473SDavid S. Miller (dev && (dev->flags & IFF_LOOPBACK) && 200638308473SDavid S. Miller !(addr_type & IPV6_ADDR_LOOPBACK) && 200738308473SDavid S. Miller !(cfg->fc_flags & RTF_LOCAL))) { 20081da177e4SLinus Torvalds /* hold loopback dev/idev if we haven't done so. */ 20095578689aSDaniel Lezcano if (dev != net->loopback_dev) { 20101da177e4SLinus Torvalds if (dev) { 20111da177e4SLinus Torvalds dev_put(dev); 20121da177e4SLinus Torvalds in6_dev_put(idev); 20131da177e4SLinus Torvalds } 20145578689aSDaniel Lezcano dev = net->loopback_dev; 20151da177e4SLinus Torvalds dev_hold(dev); 20161da177e4SLinus Torvalds idev = in6_dev_get(dev); 20171da177e4SLinus Torvalds if (!idev) { 20181da177e4SLinus Torvalds err = -ENODEV; 20191da177e4SLinus Torvalds goto out; 20201da177e4SLinus Torvalds } 20211da177e4SLinus Torvalds } 20221da177e4SLinus Torvalds rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP; 2023ef2c7d7bSNicolas Dichtel switch (cfg->fc_type) { 2024ef2c7d7bSNicolas Dichtel case RTN_BLACKHOLE: 2025ef2c7d7bSNicolas Dichtel rt->dst.error = -EINVAL; 2026ede2059dSEric W. Biederman rt->dst.output = dst_discard_out; 20277150aedeSKamala R rt->dst.input = dst_discard; 2028ef2c7d7bSNicolas Dichtel break; 2029ef2c7d7bSNicolas Dichtel case RTN_PROHIBIT: 2030ef2c7d7bSNicolas Dichtel rt->dst.error = -EACCES; 20317150aedeSKamala R rt->dst.output = ip6_pkt_prohibit_out; 20327150aedeSKamala R rt->dst.input = ip6_pkt_prohibit; 2033ef2c7d7bSNicolas Dichtel break; 2034b4949ab2SNicolas Dichtel case RTN_THROW: 20350315e382SNikola Forró case RTN_UNREACHABLE: 2036ef2c7d7bSNicolas Dichtel default: 20377150aedeSKamala R rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN 20380315e382SNikola Forró : (cfg->fc_type == RTN_UNREACHABLE) 20390315e382SNikola Forró ? -EHOSTUNREACH : -ENETUNREACH; 20407150aedeSKamala R rt->dst.output = ip6_pkt_discard_out; 20417150aedeSKamala R rt->dst.input = ip6_pkt_discard; 2042ef2c7d7bSNicolas Dichtel break; 2043ef2c7d7bSNicolas Dichtel } 20441da177e4SLinus Torvalds goto install_route; 20451da177e4SLinus Torvalds } 20461da177e4SLinus Torvalds 204786872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY) { 2048b71d1d42SEric Dumazet const struct in6_addr *gw_addr; 20491da177e4SLinus Torvalds int gwa_type; 20501da177e4SLinus Torvalds 205186872cb5SThomas Graf gw_addr = &cfg->fc_gateway; 2052330567b7SFlorian Westphal gwa_type = ipv6_addr_type(gw_addr); 205348ed7b26SFlorian Westphal 205448ed7b26SFlorian Westphal /* if gw_addr is local we will fail to detect this in case 205548ed7b26SFlorian Westphal * address is still TENTATIVE (DAD in progress). rt6_lookup() 205648ed7b26SFlorian Westphal * will return already-added prefix route via interface that 205748ed7b26SFlorian Westphal * prefix route was assigned to, which might be non-loopback. 205848ed7b26SFlorian Westphal */ 205948ed7b26SFlorian Westphal err = -EINVAL; 2060330567b7SFlorian Westphal if (ipv6_chk_addr_and_flags(net, gw_addr, 2061330567b7SFlorian Westphal gwa_type & IPV6_ADDR_LINKLOCAL ? 2062d5d531cbSDavid Ahern dev : NULL, 0, 0)) { 2063d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid gateway address"); 206448ed7b26SFlorian Westphal goto out; 2065d5d531cbSDavid Ahern } 20664e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = *gw_addr; 20671da177e4SLinus Torvalds 20681da177e4SLinus Torvalds if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) { 20698c14586fSDavid Ahern struct rt6_info *grt = NULL; 20701da177e4SLinus Torvalds 20711da177e4SLinus Torvalds /* IPv6 strictly inhibits using not link-local 20721da177e4SLinus Torvalds addresses as nexthop address. 20731da177e4SLinus Torvalds Otherwise, router will not able to send redirects. 20741da177e4SLinus Torvalds It is very good, but in some (rare!) circumstances 20751da177e4SLinus Torvalds (SIT, PtP, NBMA NOARP links) it is handy to allow 20761da177e4SLinus Torvalds some exceptions. --ANK 207796d5822cSErik Nordmark We allow IPv4-mapped nexthops to support RFC4798-type 207896d5822cSErik Nordmark addressing 20791da177e4SLinus Torvalds */ 208096d5822cSErik Nordmark if (!(gwa_type & (IPV6_ADDR_UNICAST | 2081d5d531cbSDavid Ahern IPV6_ADDR_MAPPED))) { 2082d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2083d5d531cbSDavid Ahern "Invalid gateway address"); 20841da177e4SLinus Torvalds goto out; 2085d5d531cbSDavid Ahern } 20861da177e4SLinus Torvalds 2087a435a07fSVincent Bernat if (cfg->fc_table) { 20888c14586fSDavid Ahern grt = ip6_nh_lookup_table(net, cfg, gw_addr); 20898c14586fSDavid Ahern 2090a435a07fSVincent Bernat if (grt) { 2091a435a07fSVincent Bernat if (grt->rt6i_flags & RTF_GATEWAY || 2092a435a07fSVincent Bernat (dev && dev != grt->dst.dev)) { 2093a435a07fSVincent Bernat ip6_rt_put(grt); 2094a435a07fSVincent Bernat grt = NULL; 2095a435a07fSVincent Bernat } 2096a435a07fSVincent Bernat } 2097a435a07fSVincent Bernat } 2098a435a07fSVincent Bernat 20998c14586fSDavid Ahern if (!grt) 21008c14586fSDavid Ahern grt = rt6_lookup(net, gw_addr, NULL, 21018c14586fSDavid Ahern cfg->fc_ifindex, 1); 21021da177e4SLinus Torvalds 21031da177e4SLinus Torvalds err = -EHOSTUNREACH; 210438308473SDavid S. Miller if (!grt) 21051da177e4SLinus Torvalds goto out; 21061da177e4SLinus Torvalds if (dev) { 2107d1918542SDavid S. Miller if (dev != grt->dst.dev) { 210894e187c0SAmerigo Wang ip6_rt_put(grt); 21091da177e4SLinus Torvalds goto out; 21101da177e4SLinus Torvalds } 21111da177e4SLinus Torvalds } else { 2112d1918542SDavid S. Miller dev = grt->dst.dev; 21131da177e4SLinus Torvalds idev = grt->rt6i_idev; 21141da177e4SLinus Torvalds dev_hold(dev); 21151da177e4SLinus Torvalds in6_dev_hold(grt->rt6i_idev); 21161da177e4SLinus Torvalds } 21171da177e4SLinus Torvalds if (!(grt->rt6i_flags & RTF_GATEWAY)) 21181da177e4SLinus Torvalds err = 0; 211994e187c0SAmerigo Wang ip6_rt_put(grt); 21201da177e4SLinus Torvalds 21211da177e4SLinus Torvalds if (err) 21221da177e4SLinus Torvalds goto out; 21231da177e4SLinus Torvalds } 21241da177e4SLinus Torvalds err = -EINVAL; 2125d5d531cbSDavid Ahern if (!dev) { 2126d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Egress device not specified"); 21271da177e4SLinus Torvalds goto out; 2128d5d531cbSDavid Ahern } else if (dev->flags & IFF_LOOPBACK) { 2129d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, 2130d5d531cbSDavid Ahern "Egress device can not be loopback device for this route"); 2131d5d531cbSDavid Ahern goto out; 2132d5d531cbSDavid Ahern } 21331da177e4SLinus Torvalds } 21341da177e4SLinus Torvalds 21351da177e4SLinus Torvalds err = -ENODEV; 213638308473SDavid S. Miller if (!dev) 21371da177e4SLinus Torvalds goto out; 21381da177e4SLinus Torvalds 2139c3968a85SDaniel Walter if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 2140c3968a85SDaniel Walter if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 2141d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "Invalid source address"); 2142c3968a85SDaniel Walter err = -EINVAL; 2143c3968a85SDaniel Walter goto out; 2144c3968a85SDaniel Walter } 21454e3fd7a0SAlexey Dobriyan rt->rt6i_prefsrc.addr = cfg->fc_prefsrc; 2146c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 128; 2147c3968a85SDaniel Walter } else 2148c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 2149c3968a85SDaniel Walter 215086872cb5SThomas Graf rt->rt6i_flags = cfg->fc_flags; 21511da177e4SLinus Torvalds 21521da177e4SLinus Torvalds install_route: 2153d8d1f30bSChangli Gao rt->dst.dev = dev; 21541da177e4SLinus Torvalds rt->rt6i_idev = idev; 2155c71099acSThomas Graf rt->rt6i_table = table; 215663152fc0SDaniel Lezcano 2157c346dca1SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = dev_net(dev); 215863152fc0SDaniel Lezcano 21598c5b83f0SRoopa Prabhu return rt; 21601da177e4SLinus Torvalds out: 21611da177e4SLinus Torvalds if (dev) 21621da177e4SLinus Torvalds dev_put(dev); 21631da177e4SLinus Torvalds if (idev) 21641da177e4SLinus Torvalds in6_dev_put(idev); 2165587fea74SWei Wang if (rt) 2166587fea74SWei Wang dst_release_immediate(&rt->dst); 21676b9ea5a6SRoopa Prabhu 21688c5b83f0SRoopa Prabhu return ERR_PTR(err); 21696b9ea5a6SRoopa Prabhu } 21706b9ea5a6SRoopa Prabhu 2171333c4301SDavid Ahern int ip6_route_add(struct fib6_config *cfg, 2172333c4301SDavid Ahern struct netlink_ext_ack *extack) 21736b9ea5a6SRoopa Prabhu { 21746b9ea5a6SRoopa Prabhu struct mx6_config mxc = { .mx = NULL, }; 21758c5b83f0SRoopa Prabhu struct rt6_info *rt; 21766b9ea5a6SRoopa Prabhu int err; 21776b9ea5a6SRoopa Prabhu 2178333c4301SDavid Ahern rt = ip6_route_info_create(cfg, extack); 21798c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 21808c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 21818c5b83f0SRoopa Prabhu rt = NULL; 21826b9ea5a6SRoopa Prabhu goto out; 21838c5b83f0SRoopa Prabhu } 21846b9ea5a6SRoopa Prabhu 21856b9ea5a6SRoopa Prabhu err = ip6_convert_metrics(&mxc, cfg); 21866b9ea5a6SRoopa Prabhu if (err) 21876b9ea5a6SRoopa Prabhu goto out; 21886b9ea5a6SRoopa Prabhu 2189333c4301SDavid Ahern err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, &mxc, extack); 21906b9ea5a6SRoopa Prabhu 21916b9ea5a6SRoopa Prabhu kfree(mxc.mx); 21926b9ea5a6SRoopa Prabhu 21936b9ea5a6SRoopa Prabhu return err; 21946b9ea5a6SRoopa Prabhu out: 2195587fea74SWei Wang if (rt) 2196587fea74SWei Wang dst_release_immediate(&rt->dst); 21976b9ea5a6SRoopa Prabhu 21981da177e4SLinus Torvalds return err; 21991da177e4SLinus Torvalds } 22001da177e4SLinus Torvalds 220186872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info) 22021da177e4SLinus Torvalds { 22031da177e4SLinus Torvalds int err; 2204c71099acSThomas Graf struct fib6_table *table; 2205d1918542SDavid S. Miller struct net *net = dev_net(rt->dst.dev); 22061da177e4SLinus Torvalds 2207a4c2fd7fSWei Wang if (rt == net->ipv6.ip6_null_entry) { 22086825a26cSGao feng err = -ENOENT; 22096825a26cSGao feng goto out; 22106825a26cSGao feng } 22116c813a72SPatrick McHardy 2212c71099acSThomas Graf table = rt->rt6i_table; 2213c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 221486872cb5SThomas Graf err = fib6_del(rt, info); 2215c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 22161da177e4SLinus Torvalds 22176825a26cSGao feng out: 221894e187c0SAmerigo Wang ip6_rt_put(rt); 22191da177e4SLinus Torvalds return err; 22201da177e4SLinus Torvalds } 22211da177e4SLinus Torvalds 2222e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt) 2223e0a1ad73SThomas Graf { 22244d1169c1SDenis V. Lunev struct nl_info info = { 2225d1918542SDavid S. Miller .nl_net = dev_net(rt->dst.dev), 22264d1169c1SDenis V. Lunev }; 2227528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 2228e0a1ad73SThomas Graf } 2229e0a1ad73SThomas Graf 22300ae81335SDavid Ahern static int __ip6_del_rt_siblings(struct rt6_info *rt, struct fib6_config *cfg) 22310ae81335SDavid Ahern { 22320ae81335SDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 2233e3330039SWANG Cong struct net *net = info->nl_net; 223416a16cd3SDavid Ahern struct sk_buff *skb = NULL; 22350ae81335SDavid Ahern struct fib6_table *table; 2236e3330039SWANG Cong int err = -ENOENT; 22370ae81335SDavid Ahern 2238e3330039SWANG Cong if (rt == net->ipv6.ip6_null_entry) 2239e3330039SWANG Cong goto out_put; 22400ae81335SDavid Ahern table = rt->rt6i_table; 22410ae81335SDavid Ahern write_lock_bh(&table->tb6_lock); 22420ae81335SDavid Ahern 22430ae81335SDavid Ahern if (rt->rt6i_nsiblings && cfg->fc_delete_all_nh) { 22440ae81335SDavid Ahern struct rt6_info *sibling, *next_sibling; 22450ae81335SDavid Ahern 224616a16cd3SDavid Ahern /* prefer to send a single notification with all hops */ 224716a16cd3SDavid Ahern skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 224816a16cd3SDavid Ahern if (skb) { 224916a16cd3SDavid Ahern u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; 225016a16cd3SDavid Ahern 2251e3330039SWANG Cong if (rt6_fill_node(net, skb, rt, 225216a16cd3SDavid Ahern NULL, NULL, 0, RTM_DELROUTE, 225316a16cd3SDavid Ahern info->portid, seq, 0) < 0) { 225416a16cd3SDavid Ahern kfree_skb(skb); 225516a16cd3SDavid Ahern skb = NULL; 225616a16cd3SDavid Ahern } else 225716a16cd3SDavid Ahern info->skip_notify = 1; 225816a16cd3SDavid Ahern } 225916a16cd3SDavid Ahern 22600ae81335SDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 22610ae81335SDavid Ahern &rt->rt6i_siblings, 22620ae81335SDavid Ahern rt6i_siblings) { 22630ae81335SDavid Ahern err = fib6_del(sibling, info); 22640ae81335SDavid Ahern if (err) 2265e3330039SWANG Cong goto out_unlock; 22660ae81335SDavid Ahern } 22670ae81335SDavid Ahern } 22680ae81335SDavid Ahern 22690ae81335SDavid Ahern err = fib6_del(rt, info); 2270e3330039SWANG Cong out_unlock: 22710ae81335SDavid Ahern write_unlock_bh(&table->tb6_lock); 2272e3330039SWANG Cong out_put: 22730ae81335SDavid Ahern ip6_rt_put(rt); 227416a16cd3SDavid Ahern 227516a16cd3SDavid Ahern if (skb) { 2276e3330039SWANG Cong rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 227716a16cd3SDavid Ahern info->nlh, gfp_any()); 227816a16cd3SDavid Ahern } 22790ae81335SDavid Ahern return err; 22800ae81335SDavid Ahern } 22810ae81335SDavid Ahern 2282333c4301SDavid Ahern static int ip6_route_del(struct fib6_config *cfg, 2283333c4301SDavid Ahern struct netlink_ext_ack *extack) 22841da177e4SLinus Torvalds { 2285c71099acSThomas Graf struct fib6_table *table; 22861da177e4SLinus Torvalds struct fib6_node *fn; 22871da177e4SLinus Torvalds struct rt6_info *rt; 22881da177e4SLinus Torvalds int err = -ESRCH; 22891da177e4SLinus Torvalds 22905578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 2291d5d531cbSDavid Ahern if (!table) { 2292d5d531cbSDavid Ahern NL_SET_ERR_MSG(extack, "FIB table does not exist"); 2293c71099acSThomas Graf return err; 2294d5d531cbSDavid Ahern } 22951da177e4SLinus Torvalds 2296c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 2297c71099acSThomas Graf 2298c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 229986872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 230086872cb5SThomas Graf &cfg->fc_src, cfg->fc_src_len); 23011da177e4SLinus Torvalds 23021da177e4SLinus Torvalds if (fn) { 2303d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 23041f56a01fSMartin KaFai Lau if ((rt->rt6i_flags & RTF_CACHE) && 23051f56a01fSMartin KaFai Lau !(cfg->fc_flags & RTF_CACHE)) 23061f56a01fSMartin KaFai Lau continue; 230786872cb5SThomas Graf if (cfg->fc_ifindex && 2308d1918542SDavid S. Miller (!rt->dst.dev || 2309d1918542SDavid S. Miller rt->dst.dev->ifindex != cfg->fc_ifindex)) 23101da177e4SLinus Torvalds continue; 231186872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 231286872cb5SThomas Graf !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 23131da177e4SLinus Torvalds continue; 231486872cb5SThomas Graf if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric) 23151da177e4SLinus Torvalds continue; 2316c2ed1880SMantas M if (cfg->fc_protocol && cfg->fc_protocol != rt->rt6i_protocol) 2317c2ed1880SMantas M continue; 2318d8d1f30bSChangli Gao dst_hold(&rt->dst); 2319c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 23201da177e4SLinus Torvalds 23210ae81335SDavid Ahern /* if gateway was specified only delete the one hop */ 23220ae81335SDavid Ahern if (cfg->fc_flags & RTF_GATEWAY) 232386872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 23240ae81335SDavid Ahern 23250ae81335SDavid Ahern return __ip6_del_rt_siblings(rt, cfg); 23261da177e4SLinus Torvalds } 23271da177e4SLinus Torvalds } 2328c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 23291da177e4SLinus Torvalds 23301da177e4SLinus Torvalds return err; 23311da177e4SLinus Torvalds } 23321da177e4SLinus Torvalds 23336700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 2334a6279458SYOSHIFUJI Hideaki { 2335a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 2336e8599ff4SDavid S. Miller struct rt6_info *rt, *nrt = NULL; 2337e8599ff4SDavid S. Miller struct ndisc_options ndopts; 2338e8599ff4SDavid S. Miller struct inet6_dev *in6_dev; 2339e8599ff4SDavid S. Miller struct neighbour *neigh; 234071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 struct rd_msg *msg; 23416e157b6aSDavid S. Miller int optlen, on_link; 23426e157b6aSDavid S. Miller u8 *lladdr; 2343e8599ff4SDavid S. Miller 234429a3cad5SSimon Horman optlen = skb_tail_pointer(skb) - skb_transport_header(skb); 234571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 optlen -= sizeof(*msg); 2346e8599ff4SDavid S. Miller 2347e8599ff4SDavid S. Miller if (optlen < 0) { 23486e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); 2349e8599ff4SDavid S. Miller return; 2350e8599ff4SDavid S. Miller } 2351e8599ff4SDavid S. Miller 235271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 msg = (struct rd_msg *)icmp6_hdr(skb); 2353e8599ff4SDavid S. Miller 235471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_is_multicast(&msg->dest)) { 23556e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); 2356e8599ff4SDavid S. Miller return; 2357e8599ff4SDavid S. Miller } 2358e8599ff4SDavid S. Miller 23596e157b6aSDavid S. Miller on_link = 0; 236071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_equal(&msg->dest, &msg->target)) { 2361e8599ff4SDavid S. Miller on_link = 1; 236271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 } else if (ipv6_addr_type(&msg->target) != 2363e8599ff4SDavid S. Miller (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { 23646e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); 2365e8599ff4SDavid S. Miller return; 2366e8599ff4SDavid S. Miller } 2367e8599ff4SDavid S. Miller 2368e8599ff4SDavid S. Miller in6_dev = __in6_dev_get(skb->dev); 2369e8599ff4SDavid S. Miller if (!in6_dev) 2370e8599ff4SDavid S. Miller return; 2371e8599ff4SDavid S. Miller if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) 2372e8599ff4SDavid S. Miller return; 2373e8599ff4SDavid S. Miller 2374e8599ff4SDavid S. Miller /* RFC2461 8.1: 2375e8599ff4SDavid S. Miller * The IP source address of the Redirect MUST be the same as the current 2376e8599ff4SDavid S. Miller * first-hop router for the specified ICMP Destination Address. 2377e8599ff4SDavid S. Miller */ 2378e8599ff4SDavid S. Miller 2379f997c55cSAlexander Aring if (!ndisc_parse_options(skb->dev, msg->opt, optlen, &ndopts)) { 2380e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); 2381e8599ff4SDavid S. Miller return; 2382e8599ff4SDavid S. Miller } 23836e157b6aSDavid S. Miller 23846e157b6aSDavid S. Miller lladdr = NULL; 2385e8599ff4SDavid S. Miller if (ndopts.nd_opts_tgt_lladdr) { 2386e8599ff4SDavid S. Miller lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, 2387e8599ff4SDavid S. Miller skb->dev); 2388e8599ff4SDavid S. Miller if (!lladdr) { 2389e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); 2390e8599ff4SDavid S. Miller return; 2391e8599ff4SDavid S. Miller } 2392e8599ff4SDavid S. Miller } 2393e8599ff4SDavid S. Miller 23946e157b6aSDavid S. Miller rt = (struct rt6_info *) dst; 2395ec13ad1dSMatthias Schiffer if (rt->rt6i_flags & RTF_REJECT) { 23966e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); 23976e157b6aSDavid S. Miller return; 23986e157b6aSDavid S. Miller } 23996e157b6aSDavid S. Miller 24006e157b6aSDavid S. Miller /* Redirect received -> path was valid. 24016e157b6aSDavid S. Miller * Look, redirects are sent only in response to data packets, 24026e157b6aSDavid S. Miller * so that this nexthop apparently is reachable. --ANK 24036e157b6aSDavid S. Miller */ 24040dec879fSJulian Anastasov dst_confirm_neigh(&rt->dst, &ipv6_hdr(skb)->saddr); 24056e157b6aSDavid S. Miller 240671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); 2407e8599ff4SDavid S. Miller if (!neigh) 2408e8599ff4SDavid S. Miller return; 2409e8599ff4SDavid S. Miller 24101da177e4SLinus Torvalds /* 24111da177e4SLinus Torvalds * We have finally decided to accept it. 24121da177e4SLinus Torvalds */ 24131da177e4SLinus Torvalds 2414f997c55cSAlexander Aring ndisc_update(skb->dev, neigh, lladdr, NUD_STALE, 24151da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 24161da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 24171da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 2418f997c55cSAlexander Aring NEIGH_UPDATE_F_ISROUTER)), 2419f997c55cSAlexander Aring NDISC_REDIRECT, &ndopts); 24201da177e4SLinus Torvalds 242183a09abdSMartin KaFai Lau nrt = ip6_rt_cache_alloc(rt, &msg->dest, NULL); 242238308473SDavid S. Miller if (!nrt) 24231da177e4SLinus Torvalds goto out; 24241da177e4SLinus Torvalds 24251da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 24261da177e4SLinus Torvalds if (on_link) 24271da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 24281da177e4SLinus Torvalds 2429b91d5329SXin Long nrt->rt6i_protocol = RTPROT_REDIRECT; 24304e3fd7a0SAlexey Dobriyan nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 24311da177e4SLinus Torvalds 243240e22e8fSThomas Graf if (ip6_ins_rt(nrt)) 24331cfb71eeSWei Wang goto out_release; 24341da177e4SLinus Torvalds 2435d8d1f30bSChangli Gao netevent.old = &rt->dst; 2436d8d1f30bSChangli Gao netevent.new = &nrt->dst; 243771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 netevent.daddr = &msg->dest; 243860592833SYOSHIFUJI Hideaki / 吉藤英明 netevent.neigh = neigh; 24398d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 24408d71740cSTom Tucker 24411da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) { 24426e157b6aSDavid S. Miller rt = (struct rt6_info *) dst_clone(&rt->dst); 2443e0a1ad73SThomas Graf ip6_del_rt(rt); 24441da177e4SLinus Torvalds } 24451da177e4SLinus Torvalds 24461cfb71eeSWei Wang out_release: 24471cfb71eeSWei Wang /* Release the reference taken in 24481cfb71eeSWei Wang * ip6_rt_cache_alloc() 24491cfb71eeSWei Wang */ 24501cfb71eeSWei Wang dst_release(&nrt->dst); 24511cfb71eeSWei Wang 24521da177e4SLinus Torvalds out: 2453e8599ff4SDavid S. Miller neigh_release(neigh); 24546e157b6aSDavid S. Miller } 24556e157b6aSDavid S. Miller 24561da177e4SLinus Torvalds /* 24571da177e4SLinus Torvalds * Misc support functions 24581da177e4SLinus Torvalds */ 24591da177e4SLinus Torvalds 24604b32b5adSMartin KaFai Lau static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from) 24614b32b5adSMartin KaFai Lau { 24624b32b5adSMartin KaFai Lau BUG_ON(from->dst.from); 24634b32b5adSMartin KaFai Lau 24644b32b5adSMartin KaFai Lau rt->rt6i_flags &= ~RTF_EXPIRES; 24654b32b5adSMartin KaFai Lau dst_hold(&from->dst); 24664b32b5adSMartin KaFai Lau rt->dst.from = &from->dst; 24674b32b5adSMartin KaFai Lau dst_init_metrics(&rt->dst, dst_metrics_ptr(&from->dst), true); 24684b32b5adSMartin KaFai Lau } 24694b32b5adSMartin KaFai Lau 247083a09abdSMartin KaFai Lau static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort) 24711da177e4SLinus Torvalds { 2472d8d1f30bSChangli Gao rt->dst.input = ort->dst.input; 2473d8d1f30bSChangli Gao rt->dst.output = ort->dst.output; 247483a09abdSMartin KaFai Lau rt->rt6i_dst = ort->rt6i_dst; 2475d8d1f30bSChangli Gao rt->dst.error = ort->dst.error; 24761da177e4SLinus Torvalds rt->rt6i_idev = ort->rt6i_idev; 24771da177e4SLinus Torvalds if (rt->rt6i_idev) 24781da177e4SLinus Torvalds in6_dev_hold(rt->rt6i_idev); 2479d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 24804e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 24811716a961SGao feng rt->rt6i_flags = ort->rt6i_flags; 24821716a961SGao feng rt6_set_from(rt, ort); 248383a09abdSMartin KaFai Lau rt->rt6i_metric = ort->rt6i_metric; 24841da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 248583a09abdSMartin KaFai Lau rt->rt6i_src = ort->rt6i_src; 24861da177e4SLinus Torvalds #endif 248783a09abdSMartin KaFai Lau rt->rt6i_prefsrc = ort->rt6i_prefsrc; 2488c71099acSThomas Graf rt->rt6i_table = ort->rt6i_table; 248961adedf3SJiri Benc rt->dst.lwtstate = lwtstate_get(ort->dst.lwtstate); 24901da177e4SLinus Torvalds } 24911da177e4SLinus Torvalds 249270ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 2493efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 2494b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 2495830218c1SDavid Ahern const struct in6_addr *gwaddr, 2496830218c1SDavid Ahern struct net_device *dev) 249770ceb4f5SYOSHIFUJI Hideaki { 2498830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO; 2499830218c1SDavid Ahern int ifindex = dev->ifindex; 250070ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 250170ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt = NULL; 2502c71099acSThomas Graf struct fib6_table *table; 250370ceb4f5SYOSHIFUJI Hideaki 2504830218c1SDavid Ahern table = fib6_get_table(net, tb_id); 250538308473SDavid S. Miller if (!table) 2506c71099acSThomas Graf return NULL; 2507c71099acSThomas Graf 25085744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 2509c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0); 251070ceb4f5SYOSHIFUJI Hideaki if (!fn) 251170ceb4f5SYOSHIFUJI Hideaki goto out; 251270ceb4f5SYOSHIFUJI Hideaki 2513d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 2514d1918542SDavid S. Miller if (rt->dst.dev->ifindex != ifindex) 251570ceb4f5SYOSHIFUJI Hideaki continue; 251670ceb4f5SYOSHIFUJI Hideaki if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) 251770ceb4f5SYOSHIFUJI Hideaki continue; 251870ceb4f5SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr)) 251970ceb4f5SYOSHIFUJI Hideaki continue; 2520d8d1f30bSChangli Gao dst_hold(&rt->dst); 252170ceb4f5SYOSHIFUJI Hideaki break; 252270ceb4f5SYOSHIFUJI Hideaki } 252370ceb4f5SYOSHIFUJI Hideaki out: 25245744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 252570ceb4f5SYOSHIFUJI Hideaki return rt; 252670ceb4f5SYOSHIFUJI Hideaki } 252770ceb4f5SYOSHIFUJI Hideaki 2528efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 2529b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 2530830218c1SDavid Ahern const struct in6_addr *gwaddr, 2531830218c1SDavid Ahern struct net_device *dev, 253295c96174SEric Dumazet unsigned int pref) 253370ceb4f5SYOSHIFUJI Hideaki { 253486872cb5SThomas Graf struct fib6_config cfg = { 2535238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 2536830218c1SDavid Ahern .fc_ifindex = dev->ifindex, 253786872cb5SThomas Graf .fc_dst_len = prefixlen, 253886872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 253986872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 2540b91d5329SXin Long .fc_protocol = RTPROT_RA, 254115e47304SEric W. Biederman .fc_nlinfo.portid = 0, 2542efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 2543efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 254486872cb5SThomas Graf }; 254570ceb4f5SYOSHIFUJI Hideaki 2546830218c1SDavid Ahern cfg.fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_INFO, 25474e3fd7a0SAlexey Dobriyan cfg.fc_dst = *prefix; 25484e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 254986872cb5SThomas Graf 2550e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 2551e317da96SYOSHIFUJI Hideaki if (!prefixlen) 255286872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 255370ceb4f5SYOSHIFUJI Hideaki 2554333c4301SDavid Ahern ip6_route_add(&cfg, NULL); 255570ceb4f5SYOSHIFUJI Hideaki 2556830218c1SDavid Ahern return rt6_get_route_info(net, prefix, prefixlen, gwaddr, dev); 255770ceb4f5SYOSHIFUJI Hideaki } 255870ceb4f5SYOSHIFUJI Hideaki #endif 255970ceb4f5SYOSHIFUJI Hideaki 2560b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev) 25611da177e4SLinus Torvalds { 2562830218c1SDavid Ahern u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT; 25631da177e4SLinus Torvalds struct rt6_info *rt; 2564c71099acSThomas Graf struct fib6_table *table; 25651da177e4SLinus Torvalds 2566830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), tb_id); 256738308473SDavid S. Miller if (!table) 2568c71099acSThomas Graf return NULL; 25691da177e4SLinus Torvalds 25705744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 2571d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 2572d1918542SDavid S. Miller if (dev == rt->dst.dev && 2573045927ffSYOSHIFUJI Hideaki ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 25741da177e4SLinus Torvalds ipv6_addr_equal(&rt->rt6i_gateway, addr)) 25751da177e4SLinus Torvalds break; 25761da177e4SLinus Torvalds } 25771da177e4SLinus Torvalds if (rt) 2578d8d1f30bSChangli Gao dst_hold(&rt->dst); 25795744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 25801da177e4SLinus Torvalds return rt; 25811da177e4SLinus Torvalds } 25821da177e4SLinus Torvalds 2583b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr, 2584ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 2585ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 25861da177e4SLinus Torvalds { 258786872cb5SThomas Graf struct fib6_config cfg = { 2588ca254490SDavid Ahern .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_DFLT, 2589238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 259086872cb5SThomas Graf .fc_ifindex = dev->ifindex, 259186872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 259286872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 2593b91d5329SXin Long .fc_protocol = RTPROT_RA, 259415e47304SEric W. Biederman .fc_nlinfo.portid = 0, 25955578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 2596c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 259786872cb5SThomas Graf }; 25981da177e4SLinus Torvalds 25994e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 26001da177e4SLinus Torvalds 2601333c4301SDavid Ahern if (!ip6_route_add(&cfg, NULL)) { 2602830218c1SDavid Ahern struct fib6_table *table; 2603830218c1SDavid Ahern 2604830218c1SDavid Ahern table = fib6_get_table(dev_net(dev), cfg.fc_table); 2605830218c1SDavid Ahern if (table) 2606830218c1SDavid Ahern table->flags |= RT6_TABLE_HAS_DFLT_ROUTER; 2607830218c1SDavid Ahern } 26081da177e4SLinus Torvalds 26091da177e4SLinus Torvalds return rt6_get_dflt_router(gwaddr, dev); 26101da177e4SLinus Torvalds } 26111da177e4SLinus Torvalds 2612830218c1SDavid Ahern static void __rt6_purge_dflt_routers(struct fib6_table *table) 26131da177e4SLinus Torvalds { 26141da177e4SLinus Torvalds struct rt6_info *rt; 26151da177e4SLinus Torvalds 26161da177e4SLinus Torvalds restart: 2617c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 2618d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 26193e8b0ac3SLorenzo Colitti if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) && 26203e8b0ac3SLorenzo Colitti (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) { 2621d8d1f30bSChangli Gao dst_hold(&rt->dst); 2622c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 2623e0a1ad73SThomas Graf ip6_del_rt(rt); 26241da177e4SLinus Torvalds goto restart; 26251da177e4SLinus Torvalds } 26261da177e4SLinus Torvalds } 2627c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 2628830218c1SDavid Ahern 2629830218c1SDavid Ahern table->flags &= ~RT6_TABLE_HAS_DFLT_ROUTER; 2630830218c1SDavid Ahern } 2631830218c1SDavid Ahern 2632830218c1SDavid Ahern void rt6_purge_dflt_routers(struct net *net) 2633830218c1SDavid Ahern { 2634830218c1SDavid Ahern struct fib6_table *table; 2635830218c1SDavid Ahern struct hlist_head *head; 2636830218c1SDavid Ahern unsigned int h; 2637830218c1SDavid Ahern 2638830218c1SDavid Ahern rcu_read_lock(); 2639830218c1SDavid Ahern 2640830218c1SDavid Ahern for (h = 0; h < FIB6_TABLE_HASHSZ; h++) { 2641830218c1SDavid Ahern head = &net->ipv6.fib_table_hash[h]; 2642830218c1SDavid Ahern hlist_for_each_entry_rcu(table, head, tb6_hlist) { 2643830218c1SDavid Ahern if (table->flags & RT6_TABLE_HAS_DFLT_ROUTER) 2644830218c1SDavid Ahern __rt6_purge_dflt_routers(table); 2645830218c1SDavid Ahern } 2646830218c1SDavid Ahern } 2647830218c1SDavid Ahern 2648830218c1SDavid Ahern rcu_read_unlock(); 26491da177e4SLinus Torvalds } 26501da177e4SLinus Torvalds 26515578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 26525578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 265386872cb5SThomas Graf struct fib6_config *cfg) 265486872cb5SThomas Graf { 265586872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 265686872cb5SThomas Graf 2657ca254490SDavid Ahern cfg->fc_table = l3mdev_fib_table_by_index(net, rtmsg->rtmsg_ifindex) ? 2658ca254490SDavid Ahern : RT6_TABLE_MAIN; 265986872cb5SThomas Graf cfg->fc_ifindex = rtmsg->rtmsg_ifindex; 266086872cb5SThomas Graf cfg->fc_metric = rtmsg->rtmsg_metric; 266186872cb5SThomas Graf cfg->fc_expires = rtmsg->rtmsg_info; 266286872cb5SThomas Graf cfg->fc_dst_len = rtmsg->rtmsg_dst_len; 266386872cb5SThomas Graf cfg->fc_src_len = rtmsg->rtmsg_src_len; 266486872cb5SThomas Graf cfg->fc_flags = rtmsg->rtmsg_flags; 266586872cb5SThomas Graf 26665578689aSDaniel Lezcano cfg->fc_nlinfo.nl_net = net; 2667f1243c2dSBenjamin Thery 26684e3fd7a0SAlexey Dobriyan cfg->fc_dst = rtmsg->rtmsg_dst; 26694e3fd7a0SAlexey Dobriyan cfg->fc_src = rtmsg->rtmsg_src; 26704e3fd7a0SAlexey Dobriyan cfg->fc_gateway = rtmsg->rtmsg_gateway; 267186872cb5SThomas Graf } 267286872cb5SThomas Graf 26735578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) 26741da177e4SLinus Torvalds { 267586872cb5SThomas Graf struct fib6_config cfg; 26761da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 26771da177e4SLinus Torvalds int err; 26781da177e4SLinus Torvalds 26791da177e4SLinus Torvalds switch (cmd) { 26801da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 26811da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 2682af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 26831da177e4SLinus Torvalds return -EPERM; 26841da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 26851da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 26861da177e4SLinus Torvalds if (err) 26871da177e4SLinus Torvalds return -EFAULT; 26881da177e4SLinus Torvalds 26895578689aSDaniel Lezcano rtmsg_to_fib6_config(net, &rtmsg, &cfg); 269086872cb5SThomas Graf 26911da177e4SLinus Torvalds rtnl_lock(); 26921da177e4SLinus Torvalds switch (cmd) { 26931da177e4SLinus Torvalds case SIOCADDRT: 2694333c4301SDavid Ahern err = ip6_route_add(&cfg, NULL); 26951da177e4SLinus Torvalds break; 26961da177e4SLinus Torvalds case SIOCDELRT: 2697333c4301SDavid Ahern err = ip6_route_del(&cfg, NULL); 26981da177e4SLinus Torvalds break; 26991da177e4SLinus Torvalds default: 27001da177e4SLinus Torvalds err = -EINVAL; 27011da177e4SLinus Torvalds } 27021da177e4SLinus Torvalds rtnl_unlock(); 27031da177e4SLinus Torvalds 27041da177e4SLinus Torvalds return err; 27053ff50b79SStephen Hemminger } 27061da177e4SLinus Torvalds 27071da177e4SLinus Torvalds return -EINVAL; 27081da177e4SLinus Torvalds } 27091da177e4SLinus Torvalds 27101da177e4SLinus Torvalds /* 27111da177e4SLinus Torvalds * Drop the packet on the floor 27121da177e4SLinus Torvalds */ 27131da177e4SLinus Torvalds 2714d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 27151da177e4SLinus Torvalds { 2716612f09e8SYOSHIFUJI Hideaki int type; 2717adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 2718612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 2719612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 27200660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 272145bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 27223bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 27233bd653c8SDenis V. Lunev IPSTATS_MIB_INADDRERRORS); 2724612f09e8SYOSHIFUJI Hideaki break; 2725612f09e8SYOSHIFUJI Hideaki } 2726612f09e8SYOSHIFUJI Hideaki /* FALLTHROUGH */ 2727612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 27283bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 27293bd653c8SDenis V. Lunev ipstats_mib_noroutes); 2730612f09e8SYOSHIFUJI Hideaki break; 2731612f09e8SYOSHIFUJI Hideaki } 27323ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 27331da177e4SLinus Torvalds kfree_skb(skb); 27341da177e4SLinus Torvalds return 0; 27351da177e4SLinus Torvalds } 27361da177e4SLinus Torvalds 27379ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 27389ce8ade0SThomas Graf { 2739612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 27409ce8ade0SThomas Graf } 27419ce8ade0SThomas Graf 2742ede2059dSEric W. Biederman static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb) 27431da177e4SLinus Torvalds { 2744adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 2745612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 27461da177e4SLinus Torvalds } 27471da177e4SLinus Torvalds 27489ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 27499ce8ade0SThomas Graf { 2750612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 27519ce8ade0SThomas Graf } 27529ce8ade0SThomas Graf 2753ede2059dSEric W. Biederman static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb) 27549ce8ade0SThomas Graf { 2755adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 2756612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 27579ce8ade0SThomas Graf } 27589ce8ade0SThomas Graf 27591da177e4SLinus Torvalds /* 27601da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 27611da177e4SLinus Torvalds */ 27621da177e4SLinus Torvalds 27631da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, 27641da177e4SLinus Torvalds const struct in6_addr *addr, 27658f031519SDavid S. Miller bool anycast) 27661da177e4SLinus Torvalds { 2767ca254490SDavid Ahern u32 tb_id; 2768c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 27694832c30dSDavid Ahern struct net_device *dev = idev->dev; 27705f02ce24SDavid Ahern struct rt6_info *rt; 27715f02ce24SDavid Ahern 27725f02ce24SDavid Ahern rt = ip6_dst_alloc(net, dev, DST_NOCOUNT); 2773a3300ef4SHannes Frederic Sowa if (!rt) 27741da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 27751da177e4SLinus Torvalds 27761da177e4SLinus Torvalds in6_dev_hold(idev); 27771da177e4SLinus Torvalds 277811d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 2779d8d1f30bSChangli Gao rt->dst.input = ip6_input; 2780d8d1f30bSChangli Gao rt->dst.output = ip6_output; 27811da177e4SLinus Torvalds rt->rt6i_idev = idev; 27821da177e4SLinus Torvalds 278394b5e0f9SDavid Ahern rt->rt6i_protocol = RTPROT_KERNEL; 27841da177e4SLinus Torvalds rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP; 278558c4fb86SYOSHIFUJI Hideaki if (anycast) 278658c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 278758c4fb86SYOSHIFUJI Hideaki else 27881da177e4SLinus Torvalds rt->rt6i_flags |= RTF_LOCAL; 27891da177e4SLinus Torvalds 2790550bab42SJulian Anastasov rt->rt6i_gateway = *addr; 27914e3fd7a0SAlexey Dobriyan rt->rt6i_dst.addr = *addr; 27921da177e4SLinus Torvalds rt->rt6i_dst.plen = 128; 2793ca254490SDavid Ahern tb_id = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL; 2794ca254490SDavid Ahern rt->rt6i_table = fib6_get_table(net, tb_id); 27951da177e4SLinus Torvalds 27961da177e4SLinus Torvalds return rt; 27971da177e4SLinus Torvalds } 27981da177e4SLinus Torvalds 2799c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 2800c3968a85SDaniel Walter struct arg_dev_net_ip { 2801c3968a85SDaniel Walter struct net_device *dev; 2802c3968a85SDaniel Walter struct net *net; 2803c3968a85SDaniel Walter struct in6_addr *addr; 2804c3968a85SDaniel Walter }; 2805c3968a85SDaniel Walter 2806c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg) 2807c3968a85SDaniel Walter { 2808c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 2809c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 2810c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 2811c3968a85SDaniel Walter 2812d1918542SDavid S. Miller if (((void *)rt->dst.dev == dev || !dev) && 2813c3968a85SDaniel Walter rt != net->ipv6.ip6_null_entry && 2814c3968a85SDaniel Walter ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) { 2815c3968a85SDaniel Walter /* remove prefsrc entry */ 2816c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 2817c3968a85SDaniel Walter } 2818c3968a85SDaniel Walter return 0; 2819c3968a85SDaniel Walter } 2820c3968a85SDaniel Walter 2821c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 2822c3968a85SDaniel Walter { 2823c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 2824c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 2825c3968a85SDaniel Walter .dev = ifp->idev->dev, 2826c3968a85SDaniel Walter .net = net, 2827c3968a85SDaniel Walter .addr = &ifp->addr, 2828c3968a85SDaniel Walter }; 28290c3584d5SLi RongQing fib6_clean_all(net, fib6_remove_prefsrc, &adni); 2830c3968a85SDaniel Walter } 2831c3968a85SDaniel Walter 2832be7a010dSDuan Jiong #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY) 2833be7a010dSDuan Jiong #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) 2834be7a010dSDuan Jiong 2835be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */ 2836be7a010dSDuan Jiong static int fib6_clean_tohost(struct rt6_info *rt, void *arg) 2837be7a010dSDuan Jiong { 2838be7a010dSDuan Jiong struct in6_addr *gateway = (struct in6_addr *)arg; 2839be7a010dSDuan Jiong 2840be7a010dSDuan Jiong if ((((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) || 2841be7a010dSDuan Jiong ((rt->rt6i_flags & RTF_CACHE_GATEWAY) == RTF_CACHE_GATEWAY)) && 2842be7a010dSDuan Jiong ipv6_addr_equal(gateway, &rt->rt6i_gateway)) { 2843be7a010dSDuan Jiong return -1; 2844be7a010dSDuan Jiong } 2845be7a010dSDuan Jiong return 0; 2846be7a010dSDuan Jiong } 2847be7a010dSDuan Jiong 2848be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) 2849be7a010dSDuan Jiong { 2850be7a010dSDuan Jiong fib6_clean_all(net, fib6_clean_tohost, gateway); 2851be7a010dSDuan Jiong } 2852be7a010dSDuan Jiong 28538ed67789SDaniel Lezcano struct arg_dev_net { 28548ed67789SDaniel Lezcano struct net_device *dev; 28558ed67789SDaniel Lezcano struct net *net; 28568ed67789SDaniel Lezcano }; 28578ed67789SDaniel Lezcano 2858a1a22c12SDavid Ahern /* called with write lock held for table with rt */ 28591da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg) 28601da177e4SLinus Torvalds { 2861bc3ef660Sstephen hemminger const struct arg_dev_net *adn = arg; 2862bc3ef660Sstephen hemminger const struct net_device *dev = adn->dev; 28638ed67789SDaniel Lezcano 2864d1918542SDavid S. Miller if ((rt->dst.dev == dev || !dev) && 2865a1a22c12SDavid Ahern rt != adn->net->ipv6.ip6_null_entry && 2866a1a22c12SDavid Ahern (rt->rt6i_nsiblings == 0 || 28678397ed36SDavid Ahern (dev && netdev_unregistering(dev)) || 2868a1a22c12SDavid Ahern !rt->rt6i_idev->cnf.ignore_routes_with_linkdown)) 28691da177e4SLinus Torvalds return -1; 2870c159d30cSDavid S. Miller 28711da177e4SLinus Torvalds return 0; 28721da177e4SLinus Torvalds } 28731da177e4SLinus Torvalds 2874f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev) 28751da177e4SLinus Torvalds { 28768ed67789SDaniel Lezcano struct arg_dev_net adn = { 28778ed67789SDaniel Lezcano .dev = dev, 28788ed67789SDaniel Lezcano .net = net, 28798ed67789SDaniel Lezcano }; 28808ed67789SDaniel Lezcano 28810c3584d5SLi RongQing fib6_clean_all(net, fib6_ifdown, &adn); 2882e332bc67SEric W. Biederman if (dev) 28838d0b94afSMartin KaFai Lau rt6_uncached_list_flush_dev(net, dev); 28841da177e4SLinus Torvalds } 28851da177e4SLinus Torvalds 288695c96174SEric Dumazet struct rt6_mtu_change_arg { 28871da177e4SLinus Torvalds struct net_device *dev; 288895c96174SEric Dumazet unsigned int mtu; 28891da177e4SLinus Torvalds }; 28901da177e4SLinus Torvalds 28911da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg) 28921da177e4SLinus Torvalds { 28931da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 28941da177e4SLinus Torvalds struct inet6_dev *idev; 28951da177e4SLinus Torvalds 28961da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 28971da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 28981da177e4SLinus Torvalds We still use this lock to block changes 28991da177e4SLinus Torvalds caused by addrconf/ndisc. 29001da177e4SLinus Torvalds */ 29011da177e4SLinus Torvalds 29021da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 290338308473SDavid S. Miller if (!idev) 29041da177e4SLinus Torvalds return 0; 29051da177e4SLinus Torvalds 29061da177e4SLinus Torvalds /* For administrative MTU increase, there is no way to discover 29071da177e4SLinus Torvalds IPv6 PMTU increase, so PMTU increase should be updated here. 29081da177e4SLinus Torvalds Since RFC 1981 doesn't include administrative MTU increase 29091da177e4SLinus Torvalds update PMTU increase is a MUST. (i.e. jumbo frame) 29101da177e4SLinus Torvalds */ 29111da177e4SLinus Torvalds /* 29121da177e4SLinus Torvalds If new MTU is less than route PMTU, this new MTU will be the 29131da177e4SLinus Torvalds lowest MTU in the path, update the route PMTU to reflect PMTU 29141da177e4SLinus Torvalds decreases; if new MTU is greater than route PMTU, and the 29151da177e4SLinus Torvalds old MTU is the lowest MTU in the path, update the route PMTU 29161da177e4SLinus Torvalds to reflect the increase. In this case if the other nodes' MTU 29171da177e4SLinus Torvalds also have the lowest MTU, TOO BIG MESSAGE will be lead to 291867c408cfSAlexander Alemayhu PMTU discovery. 29191da177e4SLinus Torvalds */ 2920d1918542SDavid S. Miller if (rt->dst.dev == arg->dev && 2921fb56be83SMaciej Żenczykowski dst_metric_raw(&rt->dst, RTAX_MTU) && 29224b32b5adSMartin KaFai Lau !dst_metric_locked(&rt->dst, RTAX_MTU)) { 29234b32b5adSMartin KaFai Lau if (rt->rt6i_flags & RTF_CACHE) { 29244b32b5adSMartin KaFai Lau /* For RTF_CACHE with rt6i_pmtu == 0 29254b32b5adSMartin KaFai Lau * (i.e. a redirected route), 29264b32b5adSMartin KaFai Lau * the metrics of its rt->dst.from has already 29274b32b5adSMartin KaFai Lau * been updated. 29284b32b5adSMartin KaFai Lau */ 29294b32b5adSMartin KaFai Lau if (rt->rt6i_pmtu && rt->rt6i_pmtu > arg->mtu) 29304b32b5adSMartin KaFai Lau rt->rt6i_pmtu = arg->mtu; 29314b32b5adSMartin KaFai Lau } else if (dst_mtu(&rt->dst) >= arg->mtu || 2932d8d1f30bSChangli Gao (dst_mtu(&rt->dst) < arg->mtu && 29334b32b5adSMartin KaFai Lau dst_mtu(&rt->dst) == idev->cnf.mtu6)) { 2934defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu); 2935566cfd8fSSimon Arlott } 29364b32b5adSMartin KaFai Lau } 29371da177e4SLinus Torvalds return 0; 29381da177e4SLinus Torvalds } 29391da177e4SLinus Torvalds 294095c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu) 29411da177e4SLinus Torvalds { 2942c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 2943c71099acSThomas Graf .dev = dev, 2944c71099acSThomas Graf .mtu = mtu, 2945c71099acSThomas Graf }; 29461da177e4SLinus Torvalds 29470c3584d5SLi RongQing fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); 29481da177e4SLinus Torvalds } 29491da177e4SLinus Torvalds 2950ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 29515176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 295286872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 2953ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 295486872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 295586872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 295651ebd318SNicolas Dichtel [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 2957c78ba6d6SLubomir Rintel [RTA_PREF] = { .type = NLA_U8 }, 295819e42e45SRoopa Prabhu [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, 295919e42e45SRoopa Prabhu [RTA_ENCAP] = { .type = NLA_NESTED }, 296032bc201eSXin Long [RTA_EXPIRES] = { .type = NLA_U32 }, 2961622ec2c9SLorenzo Colitti [RTA_UID] = { .type = NLA_U32 }, 29623b45a410SLiping Zhang [RTA_MARK] = { .type = NLA_U32 }, 296386872cb5SThomas Graf }; 296486872cb5SThomas Graf 296586872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 2966333c4301SDavid Ahern struct fib6_config *cfg, 2967333c4301SDavid Ahern struct netlink_ext_ack *extack) 29681da177e4SLinus Torvalds { 296986872cb5SThomas Graf struct rtmsg *rtm; 297086872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 2971c78ba6d6SLubomir Rintel unsigned int pref; 297286872cb5SThomas Graf int err; 29731da177e4SLinus Torvalds 2974fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 2975fceb6435SJohannes Berg NULL); 297686872cb5SThomas Graf if (err < 0) 297786872cb5SThomas Graf goto errout; 29781da177e4SLinus Torvalds 297986872cb5SThomas Graf err = -EINVAL; 298086872cb5SThomas Graf rtm = nlmsg_data(nlh); 298186872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 298286872cb5SThomas Graf 298386872cb5SThomas Graf cfg->fc_table = rtm->rtm_table; 298486872cb5SThomas Graf cfg->fc_dst_len = rtm->rtm_dst_len; 298586872cb5SThomas Graf cfg->fc_src_len = rtm->rtm_src_len; 298686872cb5SThomas Graf cfg->fc_flags = RTF_UP; 298786872cb5SThomas Graf cfg->fc_protocol = rtm->rtm_protocol; 2988ef2c7d7bSNicolas Dichtel cfg->fc_type = rtm->rtm_type; 298986872cb5SThomas Graf 2990ef2c7d7bSNicolas Dichtel if (rtm->rtm_type == RTN_UNREACHABLE || 2991ef2c7d7bSNicolas Dichtel rtm->rtm_type == RTN_BLACKHOLE || 2992b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_PROHIBIT || 2993b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_THROW) 299486872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 299586872cb5SThomas Graf 2996ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 2997ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 2998ab79ad14SMaciej Żenczykowski 29991f56a01fSMartin KaFai Lau if (rtm->rtm_flags & RTM_F_CLONED) 30001f56a01fSMartin KaFai Lau cfg->fc_flags |= RTF_CACHE; 30011f56a01fSMartin KaFai Lau 300215e47304SEric W. Biederman cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid; 300386872cb5SThomas Graf cfg->fc_nlinfo.nlh = nlh; 30043b1e0a65SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = sock_net(skb->sk); 300586872cb5SThomas Graf 300686872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 300767b61f6cSJiri Benc cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); 300886872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 30091da177e4SLinus Torvalds } 301086872cb5SThomas Graf 301186872cb5SThomas Graf if (tb[RTA_DST]) { 301286872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 301386872cb5SThomas Graf 301486872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 301586872cb5SThomas Graf goto errout; 301686872cb5SThomas Graf 301786872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 30181da177e4SLinus Torvalds } 301986872cb5SThomas Graf 302086872cb5SThomas Graf if (tb[RTA_SRC]) { 302186872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 302286872cb5SThomas Graf 302386872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 302486872cb5SThomas Graf goto errout; 302586872cb5SThomas Graf 302686872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 30271da177e4SLinus Torvalds } 302886872cb5SThomas Graf 3029c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 303067b61f6cSJiri Benc cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); 3031c3968a85SDaniel Walter 303286872cb5SThomas Graf if (tb[RTA_OIF]) 303386872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 303486872cb5SThomas Graf 303586872cb5SThomas Graf if (tb[RTA_PRIORITY]) 303686872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 303786872cb5SThomas Graf 303886872cb5SThomas Graf if (tb[RTA_METRICS]) { 303986872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 304086872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 30411da177e4SLinus Torvalds } 304286872cb5SThomas Graf 304386872cb5SThomas Graf if (tb[RTA_TABLE]) 304486872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 304586872cb5SThomas Graf 304651ebd318SNicolas Dichtel if (tb[RTA_MULTIPATH]) { 304751ebd318SNicolas Dichtel cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); 304851ebd318SNicolas Dichtel cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); 30499ed59592SDavid Ahern 30509ed59592SDavid Ahern err = lwtunnel_valid_encap_type_attr(cfg->fc_mp, 3051c255bd68SDavid Ahern cfg->fc_mp_len, extack); 30529ed59592SDavid Ahern if (err < 0) 30539ed59592SDavid Ahern goto errout; 305451ebd318SNicolas Dichtel } 305551ebd318SNicolas Dichtel 3056c78ba6d6SLubomir Rintel if (tb[RTA_PREF]) { 3057c78ba6d6SLubomir Rintel pref = nla_get_u8(tb[RTA_PREF]); 3058c78ba6d6SLubomir Rintel if (pref != ICMPV6_ROUTER_PREF_LOW && 3059c78ba6d6SLubomir Rintel pref != ICMPV6_ROUTER_PREF_HIGH) 3060c78ba6d6SLubomir Rintel pref = ICMPV6_ROUTER_PREF_MEDIUM; 3061c78ba6d6SLubomir Rintel cfg->fc_flags |= RTF_PREF(pref); 3062c78ba6d6SLubomir Rintel } 3063c78ba6d6SLubomir Rintel 306419e42e45SRoopa Prabhu if (tb[RTA_ENCAP]) 306519e42e45SRoopa Prabhu cfg->fc_encap = tb[RTA_ENCAP]; 306619e42e45SRoopa Prabhu 30679ed59592SDavid Ahern if (tb[RTA_ENCAP_TYPE]) { 306819e42e45SRoopa Prabhu cfg->fc_encap_type = nla_get_u16(tb[RTA_ENCAP_TYPE]); 306919e42e45SRoopa Prabhu 3070c255bd68SDavid Ahern err = lwtunnel_valid_encap_type(cfg->fc_encap_type, extack); 30719ed59592SDavid Ahern if (err < 0) 30729ed59592SDavid Ahern goto errout; 30739ed59592SDavid Ahern } 30749ed59592SDavid Ahern 307532bc201eSXin Long if (tb[RTA_EXPIRES]) { 307632bc201eSXin Long unsigned long timeout = addrconf_timeout_fixup(nla_get_u32(tb[RTA_EXPIRES]), HZ); 307732bc201eSXin Long 307832bc201eSXin Long if (addrconf_finite_timeout(timeout)) { 307932bc201eSXin Long cfg->fc_expires = jiffies_to_clock_t(timeout * HZ); 308032bc201eSXin Long cfg->fc_flags |= RTF_EXPIRES; 308132bc201eSXin Long } 308232bc201eSXin Long } 308332bc201eSXin Long 308486872cb5SThomas Graf err = 0; 308586872cb5SThomas Graf errout: 308686872cb5SThomas Graf return err; 30871da177e4SLinus Torvalds } 30881da177e4SLinus Torvalds 30896b9ea5a6SRoopa Prabhu struct rt6_nh { 30906b9ea5a6SRoopa Prabhu struct rt6_info *rt6_info; 30916b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 30926b9ea5a6SRoopa Prabhu struct mx6_config mxc; 30936b9ea5a6SRoopa Prabhu struct list_head next; 30946b9ea5a6SRoopa Prabhu }; 30956b9ea5a6SRoopa Prabhu 30966b9ea5a6SRoopa Prabhu static void ip6_print_replace_route_err(struct list_head *rt6_nh_list) 30976b9ea5a6SRoopa Prabhu { 30986b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 30996b9ea5a6SRoopa Prabhu 31006b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 31017d4d5065SDavid Ahern pr_warn("IPV6: multipath route replace failed (check consistency of installed routes): %pI6c nexthop %pI6c ifi %d\n", 31026b9ea5a6SRoopa Prabhu &nh->r_cfg.fc_dst, &nh->r_cfg.fc_gateway, 31036b9ea5a6SRoopa Prabhu nh->r_cfg.fc_ifindex); 31046b9ea5a6SRoopa Prabhu } 31056b9ea5a6SRoopa Prabhu } 31066b9ea5a6SRoopa Prabhu 31076b9ea5a6SRoopa Prabhu static int ip6_route_info_append(struct list_head *rt6_nh_list, 31086b9ea5a6SRoopa Prabhu struct rt6_info *rt, struct fib6_config *r_cfg) 31096b9ea5a6SRoopa Prabhu { 31106b9ea5a6SRoopa Prabhu struct rt6_nh *nh; 31116b9ea5a6SRoopa Prabhu int err = -EEXIST; 31126b9ea5a6SRoopa Prabhu 31136b9ea5a6SRoopa Prabhu list_for_each_entry(nh, rt6_nh_list, next) { 31146b9ea5a6SRoopa Prabhu /* check if rt6_info already exists */ 3115f06b7549SDavid Ahern if (rt6_duplicate_nexthop(nh->rt6_info, rt)) 31166b9ea5a6SRoopa Prabhu return err; 31176b9ea5a6SRoopa Prabhu } 31186b9ea5a6SRoopa Prabhu 31196b9ea5a6SRoopa Prabhu nh = kzalloc(sizeof(*nh), GFP_KERNEL); 31206b9ea5a6SRoopa Prabhu if (!nh) 31216b9ea5a6SRoopa Prabhu return -ENOMEM; 31226b9ea5a6SRoopa Prabhu nh->rt6_info = rt; 31236b9ea5a6SRoopa Prabhu err = ip6_convert_metrics(&nh->mxc, r_cfg); 31246b9ea5a6SRoopa Prabhu if (err) { 31256b9ea5a6SRoopa Prabhu kfree(nh); 31266b9ea5a6SRoopa Prabhu return err; 31276b9ea5a6SRoopa Prabhu } 31286b9ea5a6SRoopa Prabhu memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg)); 31296b9ea5a6SRoopa Prabhu list_add_tail(&nh->next, rt6_nh_list); 31306b9ea5a6SRoopa Prabhu 31316b9ea5a6SRoopa Prabhu return 0; 31326b9ea5a6SRoopa Prabhu } 31336b9ea5a6SRoopa Prabhu 31343b1137feSDavid Ahern static void ip6_route_mpath_notify(struct rt6_info *rt, 31353b1137feSDavid Ahern struct rt6_info *rt_last, 31363b1137feSDavid Ahern struct nl_info *info, 31373b1137feSDavid Ahern __u16 nlflags) 31383b1137feSDavid Ahern { 31393b1137feSDavid Ahern /* if this is an APPEND route, then rt points to the first route 31403b1137feSDavid Ahern * inserted and rt_last points to last route inserted. Userspace 31413b1137feSDavid Ahern * wants a consistent dump of the route which starts at the first 31423b1137feSDavid Ahern * nexthop. Since sibling routes are always added at the end of 31433b1137feSDavid Ahern * the list, find the first sibling of the last route appended 31443b1137feSDavid Ahern */ 31453b1137feSDavid Ahern if ((nlflags & NLM_F_APPEND) && rt_last && rt_last->rt6i_nsiblings) { 31463b1137feSDavid Ahern rt = list_first_entry(&rt_last->rt6i_siblings, 31473b1137feSDavid Ahern struct rt6_info, 31483b1137feSDavid Ahern rt6i_siblings); 31493b1137feSDavid Ahern } 31503b1137feSDavid Ahern 31513b1137feSDavid Ahern if (rt) 31523b1137feSDavid Ahern inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags); 31533b1137feSDavid Ahern } 31543b1137feSDavid Ahern 3155333c4301SDavid Ahern static int ip6_route_multipath_add(struct fib6_config *cfg, 3156333c4301SDavid Ahern struct netlink_ext_ack *extack) 315751ebd318SNicolas Dichtel { 31583b1137feSDavid Ahern struct rt6_info *rt_notif = NULL, *rt_last = NULL; 31593b1137feSDavid Ahern struct nl_info *info = &cfg->fc_nlinfo; 316051ebd318SNicolas Dichtel struct fib6_config r_cfg; 316151ebd318SNicolas Dichtel struct rtnexthop *rtnh; 31626b9ea5a6SRoopa Prabhu struct rt6_info *rt; 31636b9ea5a6SRoopa Prabhu struct rt6_nh *err_nh; 31646b9ea5a6SRoopa Prabhu struct rt6_nh *nh, *nh_safe; 31653b1137feSDavid Ahern __u16 nlflags; 316651ebd318SNicolas Dichtel int remaining; 316751ebd318SNicolas Dichtel int attrlen; 31686b9ea5a6SRoopa Prabhu int err = 1; 31696b9ea5a6SRoopa Prabhu int nhn = 0; 31706b9ea5a6SRoopa Prabhu int replace = (cfg->fc_nlinfo.nlh && 31716b9ea5a6SRoopa Prabhu (cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE)); 31726b9ea5a6SRoopa Prabhu LIST_HEAD(rt6_nh_list); 317351ebd318SNicolas Dichtel 31743b1137feSDavid Ahern nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE; 31753b1137feSDavid Ahern if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND) 31763b1137feSDavid Ahern nlflags |= NLM_F_APPEND; 31773b1137feSDavid Ahern 317835f1b4e9SMichal Kubeček remaining = cfg->fc_mp_len; 317951ebd318SNicolas Dichtel rtnh = (struct rtnexthop *)cfg->fc_mp; 318051ebd318SNicolas Dichtel 31816b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry and build a list (rt6_nh_list) of 31826b9ea5a6SRoopa Prabhu * rt6_info structs per nexthop 31836b9ea5a6SRoopa Prabhu */ 318451ebd318SNicolas Dichtel while (rtnh_ok(rtnh, remaining)) { 318551ebd318SNicolas Dichtel memcpy(&r_cfg, cfg, sizeof(*cfg)); 318651ebd318SNicolas Dichtel if (rtnh->rtnh_ifindex) 318751ebd318SNicolas Dichtel r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 318851ebd318SNicolas Dichtel 318951ebd318SNicolas Dichtel attrlen = rtnh_attrlen(rtnh); 319051ebd318SNicolas Dichtel if (attrlen > 0) { 319151ebd318SNicolas Dichtel struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 319251ebd318SNicolas Dichtel 319351ebd318SNicolas Dichtel nla = nla_find(attrs, attrlen, RTA_GATEWAY); 319451ebd318SNicolas Dichtel if (nla) { 319567b61f6cSJiri Benc r_cfg.fc_gateway = nla_get_in6_addr(nla); 319651ebd318SNicolas Dichtel r_cfg.fc_flags |= RTF_GATEWAY; 319751ebd318SNicolas Dichtel } 319819e42e45SRoopa Prabhu r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP); 319919e42e45SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE); 320019e42e45SRoopa Prabhu if (nla) 320119e42e45SRoopa Prabhu r_cfg.fc_encap_type = nla_get_u16(nla); 320251ebd318SNicolas Dichtel } 32036b9ea5a6SRoopa Prabhu 3204333c4301SDavid Ahern rt = ip6_route_info_create(&r_cfg, extack); 32058c5b83f0SRoopa Prabhu if (IS_ERR(rt)) { 32068c5b83f0SRoopa Prabhu err = PTR_ERR(rt); 32078c5b83f0SRoopa Prabhu rt = NULL; 32086b9ea5a6SRoopa Prabhu goto cleanup; 32098c5b83f0SRoopa Prabhu } 32106b9ea5a6SRoopa Prabhu 32116b9ea5a6SRoopa Prabhu err = ip6_route_info_append(&rt6_nh_list, rt, &r_cfg); 321251ebd318SNicolas Dichtel if (err) { 3213587fea74SWei Wang dst_release_immediate(&rt->dst); 32146b9ea5a6SRoopa Prabhu goto cleanup; 321551ebd318SNicolas Dichtel } 32166b9ea5a6SRoopa Prabhu 32176b9ea5a6SRoopa Prabhu rtnh = rtnh_next(rtnh, &remaining); 321851ebd318SNicolas Dichtel } 32196b9ea5a6SRoopa Prabhu 32203b1137feSDavid Ahern /* for add and replace send one notification with all nexthops. 32213b1137feSDavid Ahern * Skip the notification in fib6_add_rt2node and send one with 32223b1137feSDavid Ahern * the full route when done 32233b1137feSDavid Ahern */ 32243b1137feSDavid Ahern info->skip_notify = 1; 32253b1137feSDavid Ahern 32266b9ea5a6SRoopa Prabhu err_nh = NULL; 32276b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 32283b1137feSDavid Ahern rt_last = nh->rt6_info; 3229333c4301SDavid Ahern err = __ip6_ins_rt(nh->rt6_info, info, &nh->mxc, extack); 32303b1137feSDavid Ahern /* save reference to first route for notification */ 32313b1137feSDavid Ahern if (!rt_notif && !err) 32323b1137feSDavid Ahern rt_notif = nh->rt6_info; 32333b1137feSDavid Ahern 32346b9ea5a6SRoopa Prabhu /* nh->rt6_info is used or freed at this point, reset to NULL*/ 32356b9ea5a6SRoopa Prabhu nh->rt6_info = NULL; 32366b9ea5a6SRoopa Prabhu if (err) { 32376b9ea5a6SRoopa Prabhu if (replace && nhn) 32386b9ea5a6SRoopa Prabhu ip6_print_replace_route_err(&rt6_nh_list); 32396b9ea5a6SRoopa Prabhu err_nh = nh; 32406b9ea5a6SRoopa Prabhu goto add_errout; 32416b9ea5a6SRoopa Prabhu } 32426b9ea5a6SRoopa Prabhu 32431a72418bSNicolas Dichtel /* Because each route is added like a single route we remove 324427596472SMichal Kubeček * these flags after the first nexthop: if there is a collision, 324527596472SMichal Kubeček * we have already failed to add the first nexthop: 324627596472SMichal Kubeček * fib6_add_rt2node() has rejected it; when replacing, old 324727596472SMichal Kubeček * nexthops have been replaced by first new, the rest should 324827596472SMichal Kubeček * be added to it. 32491a72418bSNicolas Dichtel */ 325027596472SMichal Kubeček cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | 325127596472SMichal Kubeček NLM_F_REPLACE); 32526b9ea5a6SRoopa Prabhu nhn++; 32536b9ea5a6SRoopa Prabhu } 32546b9ea5a6SRoopa Prabhu 32553b1137feSDavid Ahern /* success ... tell user about new route */ 32563b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 32576b9ea5a6SRoopa Prabhu goto cleanup; 32586b9ea5a6SRoopa Prabhu 32596b9ea5a6SRoopa Prabhu add_errout: 32603b1137feSDavid Ahern /* send notification for routes that were added so that 32613b1137feSDavid Ahern * the delete notifications sent by ip6_route_del are 32623b1137feSDavid Ahern * coherent 32633b1137feSDavid Ahern */ 32643b1137feSDavid Ahern if (rt_notif) 32653b1137feSDavid Ahern ip6_route_mpath_notify(rt_notif, rt_last, info, nlflags); 32663b1137feSDavid Ahern 32676b9ea5a6SRoopa Prabhu /* Delete routes that were already added */ 32686b9ea5a6SRoopa Prabhu list_for_each_entry(nh, &rt6_nh_list, next) { 32696b9ea5a6SRoopa Prabhu if (err_nh == nh) 32706b9ea5a6SRoopa Prabhu break; 3271333c4301SDavid Ahern ip6_route_del(&nh->r_cfg, extack); 32726b9ea5a6SRoopa Prabhu } 32736b9ea5a6SRoopa Prabhu 32746b9ea5a6SRoopa Prabhu cleanup: 32756b9ea5a6SRoopa Prabhu list_for_each_entry_safe(nh, nh_safe, &rt6_nh_list, next) { 3276587fea74SWei Wang if (nh->rt6_info) 3277587fea74SWei Wang dst_release_immediate(&nh->rt6_info->dst); 32786b9ea5a6SRoopa Prabhu kfree(nh->mxc.mx); 32796b9ea5a6SRoopa Prabhu list_del(&nh->next); 32806b9ea5a6SRoopa Prabhu kfree(nh); 32816b9ea5a6SRoopa Prabhu } 32826b9ea5a6SRoopa Prabhu 32836b9ea5a6SRoopa Prabhu return err; 32846b9ea5a6SRoopa Prabhu } 32856b9ea5a6SRoopa Prabhu 3286333c4301SDavid Ahern static int ip6_route_multipath_del(struct fib6_config *cfg, 3287333c4301SDavid Ahern struct netlink_ext_ack *extack) 32886b9ea5a6SRoopa Prabhu { 32896b9ea5a6SRoopa Prabhu struct fib6_config r_cfg; 32906b9ea5a6SRoopa Prabhu struct rtnexthop *rtnh; 32916b9ea5a6SRoopa Prabhu int remaining; 32926b9ea5a6SRoopa Prabhu int attrlen; 32936b9ea5a6SRoopa Prabhu int err = 1, last_err = 0; 32946b9ea5a6SRoopa Prabhu 32956b9ea5a6SRoopa Prabhu remaining = cfg->fc_mp_len; 32966b9ea5a6SRoopa Prabhu rtnh = (struct rtnexthop *)cfg->fc_mp; 32976b9ea5a6SRoopa Prabhu 32986b9ea5a6SRoopa Prabhu /* Parse a Multipath Entry */ 32996b9ea5a6SRoopa Prabhu while (rtnh_ok(rtnh, remaining)) { 33006b9ea5a6SRoopa Prabhu memcpy(&r_cfg, cfg, sizeof(*cfg)); 33016b9ea5a6SRoopa Prabhu if (rtnh->rtnh_ifindex) 33026b9ea5a6SRoopa Prabhu r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 33036b9ea5a6SRoopa Prabhu 33046b9ea5a6SRoopa Prabhu attrlen = rtnh_attrlen(rtnh); 33056b9ea5a6SRoopa Prabhu if (attrlen > 0) { 33066b9ea5a6SRoopa Prabhu struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 33076b9ea5a6SRoopa Prabhu 33086b9ea5a6SRoopa Prabhu nla = nla_find(attrs, attrlen, RTA_GATEWAY); 33096b9ea5a6SRoopa Prabhu if (nla) { 33106b9ea5a6SRoopa Prabhu nla_memcpy(&r_cfg.fc_gateway, nla, 16); 33116b9ea5a6SRoopa Prabhu r_cfg.fc_flags |= RTF_GATEWAY; 33126b9ea5a6SRoopa Prabhu } 33136b9ea5a6SRoopa Prabhu } 3314333c4301SDavid Ahern err = ip6_route_del(&r_cfg, extack); 33156b9ea5a6SRoopa Prabhu if (err) 33166b9ea5a6SRoopa Prabhu last_err = err; 33176b9ea5a6SRoopa Prabhu 331851ebd318SNicolas Dichtel rtnh = rtnh_next(rtnh, &remaining); 331951ebd318SNicolas Dichtel } 332051ebd318SNicolas Dichtel 332151ebd318SNicolas Dichtel return last_err; 332251ebd318SNicolas Dichtel } 332351ebd318SNicolas Dichtel 3324c21ef3e3SDavid Ahern static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, 3325c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 33261da177e4SLinus Torvalds { 332786872cb5SThomas Graf struct fib6_config cfg; 332886872cb5SThomas Graf int err; 33291da177e4SLinus Torvalds 3330333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 333186872cb5SThomas Graf if (err < 0) 333286872cb5SThomas Graf return err; 333386872cb5SThomas Graf 333451ebd318SNicolas Dichtel if (cfg.fc_mp) 3335333c4301SDavid Ahern return ip6_route_multipath_del(&cfg, extack); 33360ae81335SDavid Ahern else { 33370ae81335SDavid Ahern cfg.fc_delete_all_nh = 1; 3338333c4301SDavid Ahern return ip6_route_del(&cfg, extack); 33391da177e4SLinus Torvalds } 33400ae81335SDavid Ahern } 33411da177e4SLinus Torvalds 3342c21ef3e3SDavid Ahern static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, 3343c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 33441da177e4SLinus Torvalds { 334586872cb5SThomas Graf struct fib6_config cfg; 334686872cb5SThomas Graf int err; 33471da177e4SLinus Torvalds 3348333c4301SDavid Ahern err = rtm_to_fib6_config(skb, nlh, &cfg, extack); 334986872cb5SThomas Graf if (err < 0) 335086872cb5SThomas Graf return err; 335186872cb5SThomas Graf 335251ebd318SNicolas Dichtel if (cfg.fc_mp) 3353333c4301SDavid Ahern return ip6_route_multipath_add(&cfg, extack); 335451ebd318SNicolas Dichtel else 3355333c4301SDavid Ahern return ip6_route_add(&cfg, extack); 33561da177e4SLinus Torvalds } 33571da177e4SLinus Torvalds 3358beb1afacSDavid Ahern static size_t rt6_nlmsg_size(struct rt6_info *rt) 3359339bf98fSThomas Graf { 3360beb1afacSDavid Ahern int nexthop_len = 0; 3361beb1afacSDavid Ahern 3362beb1afacSDavid Ahern if (rt->rt6i_nsiblings) { 3363beb1afacSDavid Ahern nexthop_len = nla_total_size(0) /* RTA_MULTIPATH */ 3364beb1afacSDavid Ahern + NLA_ALIGN(sizeof(struct rtnexthop)) 3365beb1afacSDavid Ahern + nla_total_size(16) /* RTA_GATEWAY */ 3366beb1afacSDavid Ahern + lwtunnel_get_encap_size(rt->dst.lwtstate); 3367beb1afacSDavid Ahern 3368beb1afacSDavid Ahern nexthop_len *= rt->rt6i_nsiblings; 3369beb1afacSDavid Ahern } 3370beb1afacSDavid Ahern 3371339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 3372339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 3373339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 3374339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 3375339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 3376339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 3377339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 3378339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 3379339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 33806a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 3381ea697639SDaniel Borkmann + nla_total_size(sizeof(struct rta_cacheinfo)) 3382c78ba6d6SLubomir Rintel + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ 338319e42e45SRoopa Prabhu + nla_total_size(1) /* RTA_PREF */ 3384beb1afacSDavid Ahern + lwtunnel_get_encap_size(rt->dst.lwtstate) 3385beb1afacSDavid Ahern + nexthop_len; 3386beb1afacSDavid Ahern } 3387beb1afacSDavid Ahern 3388beb1afacSDavid Ahern static int rt6_nexthop_info(struct sk_buff *skb, struct rt6_info *rt, 33895be083ceSDavid Ahern unsigned int *flags, bool skip_oif) 3390beb1afacSDavid Ahern { 3391beb1afacSDavid Ahern if (!netif_running(rt->dst.dev) || !netif_carrier_ok(rt->dst.dev)) { 3392beb1afacSDavid Ahern *flags |= RTNH_F_LINKDOWN; 3393beb1afacSDavid Ahern if (rt->rt6i_idev->cnf.ignore_routes_with_linkdown) 3394beb1afacSDavid Ahern *flags |= RTNH_F_DEAD; 3395beb1afacSDavid Ahern } 3396beb1afacSDavid Ahern 3397beb1afacSDavid Ahern if (rt->rt6i_flags & RTF_GATEWAY) { 3398beb1afacSDavid Ahern if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0) 3399beb1afacSDavid Ahern goto nla_put_failure; 3400beb1afacSDavid Ahern } 3401beb1afacSDavid Ahern 3402fe400799SIdo Schimmel if (rt->rt6i_nh_flags & RTNH_F_OFFLOAD) 340361e4d01eSIdo Schimmel *flags |= RTNH_F_OFFLOAD; 340461e4d01eSIdo Schimmel 34055be083ceSDavid Ahern /* not needed for multipath encoding b/c it has a rtnexthop struct */ 34065be083ceSDavid Ahern if (!skip_oif && rt->dst.dev && 3407beb1afacSDavid Ahern nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex)) 3408beb1afacSDavid Ahern goto nla_put_failure; 3409beb1afacSDavid Ahern 3410beb1afacSDavid Ahern if (rt->dst.lwtstate && 3411beb1afacSDavid Ahern lwtunnel_fill_encap(skb, rt->dst.lwtstate) < 0) 3412beb1afacSDavid Ahern goto nla_put_failure; 3413beb1afacSDavid Ahern 3414beb1afacSDavid Ahern return 0; 3415beb1afacSDavid Ahern 3416beb1afacSDavid Ahern nla_put_failure: 3417beb1afacSDavid Ahern return -EMSGSIZE; 3418beb1afacSDavid Ahern } 3419beb1afacSDavid Ahern 34205be083ceSDavid Ahern /* add multipath next hop */ 3421beb1afacSDavid Ahern static int rt6_add_nexthop(struct sk_buff *skb, struct rt6_info *rt) 3422beb1afacSDavid Ahern { 3423beb1afacSDavid Ahern struct rtnexthop *rtnh; 3424beb1afacSDavid Ahern unsigned int flags = 0; 3425beb1afacSDavid Ahern 3426beb1afacSDavid Ahern rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh)); 3427beb1afacSDavid Ahern if (!rtnh) 3428beb1afacSDavid Ahern goto nla_put_failure; 3429beb1afacSDavid Ahern 3430beb1afacSDavid Ahern rtnh->rtnh_hops = 0; 3431beb1afacSDavid Ahern rtnh->rtnh_ifindex = rt->dst.dev ? rt->dst.dev->ifindex : 0; 3432beb1afacSDavid Ahern 34335be083ceSDavid Ahern if (rt6_nexthop_info(skb, rt, &flags, true) < 0) 3434beb1afacSDavid Ahern goto nla_put_failure; 3435beb1afacSDavid Ahern 3436beb1afacSDavid Ahern rtnh->rtnh_flags = flags; 3437beb1afacSDavid Ahern 3438beb1afacSDavid Ahern /* length of rtnetlink header + attributes */ 3439beb1afacSDavid Ahern rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh; 3440beb1afacSDavid Ahern 3441beb1afacSDavid Ahern return 0; 3442beb1afacSDavid Ahern 3443beb1afacSDavid Ahern nla_put_failure: 3444beb1afacSDavid Ahern return -EMSGSIZE; 3445339bf98fSThomas Graf } 3446339bf98fSThomas Graf 3447191cd582SBrian Haley static int rt6_fill_node(struct net *net, 3448191cd582SBrian Haley struct sk_buff *skb, struct rt6_info *rt, 34490d51aa80SJamal Hadi Salim struct in6_addr *dst, struct in6_addr *src, 345015e47304SEric W. Biederman int iif, int type, u32 portid, u32 seq, 3451f8cfe2ceSDavid Ahern unsigned int flags) 34521da177e4SLinus Torvalds { 34534b32b5adSMartin KaFai Lau u32 metrics[RTAX_MAX]; 34541da177e4SLinus Torvalds struct rtmsg *rtm; 34551da177e4SLinus Torvalds struct nlmsghdr *nlh; 3456e3703b3dSThomas Graf long expires; 34579e762a4aSPatrick McHardy u32 table; 34581da177e4SLinus Torvalds 345915e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); 346038308473SDavid S. Miller if (!nlh) 346126932566SPatrick McHardy return -EMSGSIZE; 34622d7202bfSThomas Graf 34632d7202bfSThomas Graf rtm = nlmsg_data(nlh); 34641da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 34651da177e4SLinus Torvalds rtm->rtm_dst_len = rt->rt6i_dst.plen; 34661da177e4SLinus Torvalds rtm->rtm_src_len = rt->rt6i_src.plen; 34671da177e4SLinus Torvalds rtm->rtm_tos = 0; 3468c71099acSThomas Graf if (rt->rt6i_table) 34699e762a4aSPatrick McHardy table = rt->rt6i_table->tb6_id; 3470c71099acSThomas Graf else 34719e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 34729e762a4aSPatrick McHardy rtm->rtm_table = table; 3473c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, table)) 3474c78679e8SDavid S. Miller goto nla_put_failure; 3475ef2c7d7bSNicolas Dichtel if (rt->rt6i_flags & RTF_REJECT) { 3476ef2c7d7bSNicolas Dichtel switch (rt->dst.error) { 3477ef2c7d7bSNicolas Dichtel case -EINVAL: 3478ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_BLACKHOLE; 3479ef2c7d7bSNicolas Dichtel break; 3480ef2c7d7bSNicolas Dichtel case -EACCES: 3481ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_PROHIBIT; 3482ef2c7d7bSNicolas Dichtel break; 3483b4949ab2SNicolas Dichtel case -EAGAIN: 3484b4949ab2SNicolas Dichtel rtm->rtm_type = RTN_THROW; 3485b4949ab2SNicolas Dichtel break; 3486ef2c7d7bSNicolas Dichtel default: 34871da177e4SLinus Torvalds rtm->rtm_type = RTN_UNREACHABLE; 3488ef2c7d7bSNicolas Dichtel break; 3489ef2c7d7bSNicolas Dichtel } 3490ef2c7d7bSNicolas Dichtel } 3491ab79ad14SMaciej Żenczykowski else if (rt->rt6i_flags & RTF_LOCAL) 3492ab79ad14SMaciej Żenczykowski rtm->rtm_type = RTN_LOCAL; 34934ee39733SDavid Ahern else if (rt->rt6i_flags & RTF_ANYCAST) 34944ee39733SDavid Ahern rtm->rtm_type = RTN_ANYCAST; 3495d1918542SDavid S. Miller else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK)) 34961da177e4SLinus Torvalds rtm->rtm_type = RTN_LOCAL; 34971da177e4SLinus Torvalds else 34981da177e4SLinus Torvalds rtm->rtm_type = RTN_UNICAST; 34991da177e4SLinus Torvalds rtm->rtm_flags = 0; 35001da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 35011da177e4SLinus Torvalds rtm->rtm_protocol = rt->rt6i_protocol; 35021da177e4SLinus Torvalds 35031da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) 35041da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 35051da177e4SLinus Torvalds 35061da177e4SLinus Torvalds if (dst) { 3507930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_DST, dst)) 3508c78679e8SDavid S. Miller goto nla_put_failure; 35091da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 35101da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 3511930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr)) 3512c78679e8SDavid S. Miller goto nla_put_failure; 35131da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 35141da177e4SLinus Torvalds if (src) { 3515930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_SRC, src)) 3516c78679e8SDavid S. Miller goto nla_put_failure; 35171da177e4SLinus Torvalds rtm->rtm_src_len = 128; 3518c78679e8SDavid S. Miller } else if (rtm->rtm_src_len && 3519930345eaSJiri Benc nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr)) 3520c78679e8SDavid S. Miller goto nla_put_failure; 35211da177e4SLinus Torvalds #endif 35227bc570c8SYOSHIFUJI Hideaki if (iif) { 35237bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 35247bc570c8SYOSHIFUJI Hideaki if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) { 3525fd61c6baSDavid Ahern int err = ip6mr_get_route(net, skb, rtm, portid); 35262cf75070SNikolay Aleksandrov 35277bc570c8SYOSHIFUJI Hideaki if (err == 0) 35287bc570c8SYOSHIFUJI Hideaki return 0; 3529fd61c6baSDavid Ahern if (err < 0) 35307bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 35317bc570c8SYOSHIFUJI Hideaki } else 35327bc570c8SYOSHIFUJI Hideaki #endif 3533c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_IIF, iif)) 3534c78679e8SDavid S. Miller goto nla_put_failure; 35357bc570c8SYOSHIFUJI Hideaki } else if (dst) { 35361da177e4SLinus Torvalds struct in6_addr saddr_buf; 3537c78679e8SDavid S. Miller if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 && 3538930345eaSJiri Benc nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 3539c78679e8SDavid S. Miller goto nla_put_failure; 3540c3968a85SDaniel Walter } 3541c3968a85SDaniel Walter 3542c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) { 3543c3968a85SDaniel Walter struct in6_addr saddr_buf; 35444e3fd7a0SAlexey Dobriyan saddr_buf = rt->rt6i_prefsrc.addr; 3545930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 3546c78679e8SDavid S. Miller goto nla_put_failure; 35471da177e4SLinus Torvalds } 35482d7202bfSThomas Graf 35494b32b5adSMartin KaFai Lau memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics)); 35504b32b5adSMartin KaFai Lau if (rt->rt6i_pmtu) 35514b32b5adSMartin KaFai Lau metrics[RTAX_MTU - 1] = rt->rt6i_pmtu; 35524b32b5adSMartin KaFai Lau if (rtnetlink_put_metrics(skb, metrics) < 0) 35532d7202bfSThomas Graf goto nla_put_failure; 35542d7202bfSThomas Graf 3555beb1afacSDavid Ahern if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric)) 3556beb1afacSDavid Ahern goto nla_put_failure; 3557beb1afacSDavid Ahern 3558beb1afacSDavid Ahern /* For multipath routes, walk the siblings list and add 3559beb1afacSDavid Ahern * each as a nexthop within RTA_MULTIPATH. 3560beb1afacSDavid Ahern */ 3561beb1afacSDavid Ahern if (rt->rt6i_nsiblings) { 3562beb1afacSDavid Ahern struct rt6_info *sibling, *next_sibling; 3563beb1afacSDavid Ahern struct nlattr *mp; 3564beb1afacSDavid Ahern 3565beb1afacSDavid Ahern mp = nla_nest_start(skb, RTA_MULTIPATH); 3566beb1afacSDavid Ahern if (!mp) 3567beb1afacSDavid Ahern goto nla_put_failure; 3568beb1afacSDavid Ahern 3569beb1afacSDavid Ahern if (rt6_add_nexthop(skb, rt) < 0) 3570beb1afacSDavid Ahern goto nla_put_failure; 3571beb1afacSDavid Ahern 3572beb1afacSDavid Ahern list_for_each_entry_safe(sibling, next_sibling, 3573beb1afacSDavid Ahern &rt->rt6i_siblings, rt6i_siblings) { 3574beb1afacSDavid Ahern if (rt6_add_nexthop(skb, sibling) < 0) 357594f826b8SEric Dumazet goto nla_put_failure; 357694f826b8SEric Dumazet } 35772d7202bfSThomas Graf 3578beb1afacSDavid Ahern nla_nest_end(skb, mp); 3579beb1afacSDavid Ahern } else { 35805be083ceSDavid Ahern if (rt6_nexthop_info(skb, rt, &rtm->rtm_flags, false) < 0) 3581c78679e8SDavid S. Miller goto nla_put_failure; 3582beb1afacSDavid Ahern } 35838253947eSLi Wei 35848253947eSLi Wei expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0; 358569cdf8f9SYOSHIFUJI Hideaki 358687a50699SDavid S. Miller if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0) 3587e3703b3dSThomas Graf goto nla_put_failure; 35881da177e4SLinus Torvalds 3589c78ba6d6SLubomir Rintel if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags))) 3590c78ba6d6SLubomir Rintel goto nla_put_failure; 3591c78ba6d6SLubomir Rintel 359219e42e45SRoopa Prabhu 3593053c095aSJohannes Berg nlmsg_end(skb, nlh); 3594053c095aSJohannes Berg return 0; 35952d7202bfSThomas Graf 35962d7202bfSThomas Graf nla_put_failure: 359726932566SPatrick McHardy nlmsg_cancel(skb, nlh); 359826932566SPatrick McHardy return -EMSGSIZE; 35991da177e4SLinus Torvalds } 36001da177e4SLinus Torvalds 36011b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg) 36021da177e4SLinus Torvalds { 36031da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 36041f17e2f2SDavid Ahern struct net *net = arg->net; 36051f17e2f2SDavid Ahern 36061f17e2f2SDavid Ahern if (rt == net->ipv6.ip6_null_entry) 36071f17e2f2SDavid Ahern return 0; 36081da177e4SLinus Torvalds 36092d7202bfSThomas Graf if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { 36102d7202bfSThomas Graf struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); 3611f8cfe2ceSDavid Ahern 3612f8cfe2ceSDavid Ahern /* user wants prefix routes only */ 3613f8cfe2ceSDavid Ahern if (rtm->rtm_flags & RTM_F_PREFIX && 3614f8cfe2ceSDavid Ahern !(rt->rt6i_flags & RTF_PREFIX_RT)) { 3615f8cfe2ceSDavid Ahern /* success since this is not a prefix route */ 3616f8cfe2ceSDavid Ahern return 1; 3617f8cfe2ceSDavid Ahern } 3618f8cfe2ceSDavid Ahern } 36191da177e4SLinus Torvalds 36201f17e2f2SDavid Ahern return rt6_fill_node(net, 3621191cd582SBrian Haley arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE, 362215e47304SEric W. Biederman NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq, 3623f8cfe2ceSDavid Ahern NLM_F_MULTI); 36241da177e4SLinus Torvalds } 36251da177e4SLinus Torvalds 3626c21ef3e3SDavid Ahern static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, 3627c21ef3e3SDavid Ahern struct netlink_ext_ack *extack) 36281da177e4SLinus Torvalds { 36293b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 3630ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 363118c3a61cSRoopa Prabhu int err, iif = 0, oif = 0; 363218c3a61cSRoopa Prabhu struct dst_entry *dst; 36331da177e4SLinus Torvalds struct rt6_info *rt; 3634ab364a6fSThomas Graf struct sk_buff *skb; 3635ab364a6fSThomas Graf struct rtmsg *rtm; 36364c9483b2SDavid S. Miller struct flowi6 fl6; 363718c3a61cSRoopa Prabhu bool fibmatch; 3638ab364a6fSThomas Graf 3639fceb6435SJohannes Berg err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy, 3640c21ef3e3SDavid Ahern extack); 3641ab364a6fSThomas Graf if (err < 0) 3642ab364a6fSThomas Graf goto errout; 3643ab364a6fSThomas Graf 3644ab364a6fSThomas Graf err = -EINVAL; 36454c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 364638b7097bSHannes Frederic Sowa rtm = nlmsg_data(nlh); 364738b7097bSHannes Frederic Sowa fl6.flowlabel = ip6_make_flowinfo(rtm->rtm_tos, 0); 364818c3a61cSRoopa Prabhu fibmatch = !!(rtm->rtm_flags & RTM_F_FIB_MATCH); 3649ab364a6fSThomas Graf 3650ab364a6fSThomas Graf if (tb[RTA_SRC]) { 3651ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 3652ab364a6fSThomas Graf goto errout; 3653ab364a6fSThomas Graf 36544e3fd7a0SAlexey Dobriyan fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 3655ab364a6fSThomas Graf } 3656ab364a6fSThomas Graf 3657ab364a6fSThomas Graf if (tb[RTA_DST]) { 3658ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 3659ab364a6fSThomas Graf goto errout; 3660ab364a6fSThomas Graf 36614e3fd7a0SAlexey Dobriyan fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 3662ab364a6fSThomas Graf } 3663ab364a6fSThomas Graf 3664ab364a6fSThomas Graf if (tb[RTA_IIF]) 3665ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 3666ab364a6fSThomas Graf 3667ab364a6fSThomas Graf if (tb[RTA_OIF]) 366872331bc0SShmulik Ladkani oif = nla_get_u32(tb[RTA_OIF]); 3669ab364a6fSThomas Graf 36702e47b291SLorenzo Colitti if (tb[RTA_MARK]) 36712e47b291SLorenzo Colitti fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); 36722e47b291SLorenzo Colitti 3673622ec2c9SLorenzo Colitti if (tb[RTA_UID]) 3674622ec2c9SLorenzo Colitti fl6.flowi6_uid = make_kuid(current_user_ns(), 3675622ec2c9SLorenzo Colitti nla_get_u32(tb[RTA_UID])); 3676622ec2c9SLorenzo Colitti else 3677622ec2c9SLorenzo Colitti fl6.flowi6_uid = iif ? INVALID_UID : current_uid(); 3678622ec2c9SLorenzo Colitti 3679ab364a6fSThomas Graf if (iif) { 3680ab364a6fSThomas Graf struct net_device *dev; 368172331bc0SShmulik Ladkani int flags = 0; 368272331bc0SShmulik Ladkani 3683121622dbSFlorian Westphal rcu_read_lock(); 3684121622dbSFlorian Westphal 3685121622dbSFlorian Westphal dev = dev_get_by_index_rcu(net, iif); 3686ab364a6fSThomas Graf if (!dev) { 3687121622dbSFlorian Westphal rcu_read_unlock(); 3688ab364a6fSThomas Graf err = -ENODEV; 3689ab364a6fSThomas Graf goto errout; 3690ab364a6fSThomas Graf } 369172331bc0SShmulik Ladkani 369272331bc0SShmulik Ladkani fl6.flowi6_iif = iif; 369372331bc0SShmulik Ladkani 369472331bc0SShmulik Ladkani if (!ipv6_addr_any(&fl6.saddr)) 369572331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_HAS_SADDR; 369672331bc0SShmulik Ladkani 369718c3a61cSRoopa Prabhu if (!fibmatch) 369818c3a61cSRoopa Prabhu dst = ip6_route_input_lookup(net, dev, &fl6, flags); 3699401481e0SArnd Bergmann else 3700401481e0SArnd Bergmann dst = ip6_route_lookup(net, &fl6, 0); 3701121622dbSFlorian Westphal 3702121622dbSFlorian Westphal rcu_read_unlock(); 370372331bc0SShmulik Ladkani } else { 370472331bc0SShmulik Ladkani fl6.flowi6_oif = oif; 370572331bc0SShmulik Ladkani 370618c3a61cSRoopa Prabhu if (!fibmatch) 370718c3a61cSRoopa Prabhu dst = ip6_route_output(net, NULL, &fl6); 3708401481e0SArnd Bergmann else 3709401481e0SArnd Bergmann dst = ip6_route_lookup(net, &fl6, 0); 371018c3a61cSRoopa Prabhu } 371118c3a61cSRoopa Prabhu 371218c3a61cSRoopa Prabhu 371318c3a61cSRoopa Prabhu rt = container_of(dst, struct rt6_info, dst); 371418c3a61cSRoopa Prabhu if (rt->dst.error) { 371518c3a61cSRoopa Prabhu err = rt->dst.error; 371618c3a61cSRoopa Prabhu ip6_rt_put(rt); 371718c3a61cSRoopa Prabhu goto errout; 3718ab364a6fSThomas Graf } 37191da177e4SLinus Torvalds 37209d6acb3bSWANG Cong if (rt == net->ipv6.ip6_null_entry) { 37219d6acb3bSWANG Cong err = rt->dst.error; 37229d6acb3bSWANG Cong ip6_rt_put(rt); 37239d6acb3bSWANG Cong goto errout; 37249d6acb3bSWANG Cong } 37259d6acb3bSWANG Cong 37261da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 372738308473SDavid S. Miller if (!skb) { 372894e187c0SAmerigo Wang ip6_rt_put(rt); 3729ab364a6fSThomas Graf err = -ENOBUFS; 3730ab364a6fSThomas Graf goto errout; 3731ab364a6fSThomas Graf } 37321da177e4SLinus Torvalds 3733d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 373418c3a61cSRoopa Prabhu if (fibmatch) 373518c3a61cSRoopa Prabhu err = rt6_fill_node(net, skb, rt, NULL, NULL, iif, 373618c3a61cSRoopa Prabhu RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 373718c3a61cSRoopa Prabhu nlh->nlmsg_seq, 0); 373818c3a61cSRoopa Prabhu else 37394c9483b2SDavid S. Miller err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif, 374015e47304SEric W. Biederman RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 3741f8cfe2ceSDavid Ahern nlh->nlmsg_seq, 0); 37421da177e4SLinus Torvalds if (err < 0) { 3743ab364a6fSThomas Graf kfree_skb(skb); 3744ab364a6fSThomas Graf goto errout; 37451da177e4SLinus Torvalds } 37461da177e4SLinus Torvalds 374715e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 3748ab364a6fSThomas Graf errout: 37491da177e4SLinus Torvalds return err; 37501da177e4SLinus Torvalds } 37511da177e4SLinus Torvalds 375237a1d361SRoopa Prabhu void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info, 375337a1d361SRoopa Prabhu unsigned int nlm_flags) 37541da177e4SLinus Torvalds { 37551da177e4SLinus Torvalds struct sk_buff *skb; 37565578689aSDaniel Lezcano struct net *net = info->nl_net; 3757528c4cebSDenis V. Lunev u32 seq; 3758528c4cebSDenis V. Lunev int err; 37590d51aa80SJamal Hadi Salim 3760528c4cebSDenis V. Lunev err = -ENOBUFS; 376138308473SDavid S. Miller seq = info->nlh ? info->nlh->nlmsg_seq : 0; 376286872cb5SThomas Graf 376319e42e45SRoopa Prabhu skb = nlmsg_new(rt6_nlmsg_size(rt), gfp_any()); 376438308473SDavid S. Miller if (!skb) 376521713ebcSThomas Graf goto errout; 37661da177e4SLinus Torvalds 3767191cd582SBrian Haley err = rt6_fill_node(net, skb, rt, NULL, NULL, 0, 3768f8cfe2ceSDavid Ahern event, info->portid, seq, nlm_flags); 376926932566SPatrick McHardy if (err < 0) { 377026932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 377126932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 377226932566SPatrick McHardy kfree_skb(skb); 377326932566SPatrick McHardy goto errout; 377426932566SPatrick McHardy } 377515e47304SEric W. Biederman rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 37765578689aSDaniel Lezcano info->nlh, gfp_any()); 37771ce85fe4SPablo Neira Ayuso return; 377821713ebcSThomas Graf errout: 377921713ebcSThomas Graf if (err < 0) 37805578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 37811da177e4SLinus Torvalds } 37821da177e4SLinus Torvalds 37838ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 3784351638e7SJiri Pirko unsigned long event, void *ptr) 37858ed67789SDaniel Lezcano { 3786351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 3787c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 37888ed67789SDaniel Lezcano 3789242d3a49SWANG Cong if (!(dev->flags & IFF_LOOPBACK)) 3790242d3a49SWANG Cong return NOTIFY_OK; 3791242d3a49SWANG Cong 3792242d3a49SWANG Cong if (event == NETDEV_REGISTER) { 3793d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 37948ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 37958ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 3796d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 37978ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 3798d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 37998ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 38008ed67789SDaniel Lezcano #endif 380176da0704SWANG Cong } else if (event == NETDEV_UNREGISTER && 380276da0704SWANG Cong dev->reg_state != NETREG_UNREGISTERED) { 380376da0704SWANG Cong /* NETDEV_UNREGISTER could be fired for multiple times by 380476da0704SWANG Cong * netdev_wait_allrefs(). Make sure we only call this once. 380576da0704SWANG Cong */ 380612d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev); 3807242d3a49SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 380812d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev); 380912d94a80SEric Dumazet in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev); 3810242d3a49SWANG Cong #endif 38118ed67789SDaniel Lezcano } 38128ed67789SDaniel Lezcano 38138ed67789SDaniel Lezcano return NOTIFY_OK; 38148ed67789SDaniel Lezcano } 38158ed67789SDaniel Lezcano 38161da177e4SLinus Torvalds /* 38171da177e4SLinus Torvalds * /proc 38181da177e4SLinus Torvalds */ 38191da177e4SLinus Torvalds 38201da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 38211da177e4SLinus Torvalds 382233120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = { 382333120b30SAlexey Dobriyan .owner = THIS_MODULE, 382433120b30SAlexey Dobriyan .open = ipv6_route_open, 382533120b30SAlexey Dobriyan .read = seq_read, 382633120b30SAlexey Dobriyan .llseek = seq_lseek, 38278d2ca1d7SHannes Frederic Sowa .release = seq_release_net, 382833120b30SAlexey Dobriyan }; 382933120b30SAlexey Dobriyan 38301da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 38311da177e4SLinus Torvalds { 383269ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 38331da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 383469ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 383569ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 383669ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_alloc, 383769ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 383869ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 3839fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 384069ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 38411da177e4SLinus Torvalds 38421da177e4SLinus Torvalds return 0; 38431da177e4SLinus Torvalds } 38441da177e4SLinus Torvalds 38451da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file) 38461da177e4SLinus Torvalds { 3847de05c557SPavel Emelyanov return single_open_net(inode, file, rt6_stats_seq_show); 384869ddb805SDaniel Lezcano } 384969ddb805SDaniel Lezcano 38509a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = { 38511da177e4SLinus Torvalds .owner = THIS_MODULE, 38521da177e4SLinus Torvalds .open = rt6_stats_seq_open, 38531da177e4SLinus Torvalds .read = seq_read, 38541da177e4SLinus Torvalds .llseek = seq_lseek, 3855b6fcbdb4SPavel Emelyanov .release = single_release_net, 38561da177e4SLinus Torvalds }; 38571da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 38581da177e4SLinus Torvalds 38591da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 38601da177e4SLinus Torvalds 38611da177e4SLinus Torvalds static 3862fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, 38631da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 38641da177e4SLinus Torvalds { 3865c486da34SLucian Adrian Grijincu struct net *net; 3866c486da34SLucian Adrian Grijincu int delay; 3867c486da34SLucian Adrian Grijincu if (!write) 3868c486da34SLucian Adrian Grijincu return -EINVAL; 3869c486da34SLucian Adrian Grijincu 3870c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 3871c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 38728d65af78SAlexey Dobriyan proc_dointvec(ctl, write, buffer, lenp, ppos); 38732ac3ac8fSMichal Kubeček fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0); 38741da177e4SLinus Torvalds return 0; 38751da177e4SLinus Torvalds } 38761da177e4SLinus Torvalds 3877fe2c6338SJoe Perches struct ctl_table ipv6_route_table_template[] = { 38781da177e4SLinus Torvalds { 38791da177e4SLinus Torvalds .procname = "flush", 38804990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 38811da177e4SLinus Torvalds .maxlen = sizeof(int), 388289c8b3a1SDave Jones .mode = 0200, 38836d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 38841da177e4SLinus Torvalds }, 38851da177e4SLinus Torvalds { 38861da177e4SLinus Torvalds .procname = "gc_thresh", 38879a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 38881da177e4SLinus Torvalds .maxlen = sizeof(int), 38891da177e4SLinus Torvalds .mode = 0644, 38906d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 38911da177e4SLinus Torvalds }, 38921da177e4SLinus Torvalds { 38931da177e4SLinus Torvalds .procname = "max_size", 38944990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 38951da177e4SLinus Torvalds .maxlen = sizeof(int), 38961da177e4SLinus Torvalds .mode = 0644, 38976d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 38981da177e4SLinus Torvalds }, 38991da177e4SLinus Torvalds { 39001da177e4SLinus Torvalds .procname = "gc_min_interval", 39014990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 39021da177e4SLinus Torvalds .maxlen = sizeof(int), 39031da177e4SLinus Torvalds .mode = 0644, 39046d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 39051da177e4SLinus Torvalds }, 39061da177e4SLinus Torvalds { 39071da177e4SLinus Torvalds .procname = "gc_timeout", 39084990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 39091da177e4SLinus Torvalds .maxlen = sizeof(int), 39101da177e4SLinus Torvalds .mode = 0644, 39116d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 39121da177e4SLinus Torvalds }, 39131da177e4SLinus Torvalds { 39141da177e4SLinus Torvalds .procname = "gc_interval", 39154990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 39161da177e4SLinus Torvalds .maxlen = sizeof(int), 39171da177e4SLinus Torvalds .mode = 0644, 39186d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 39191da177e4SLinus Torvalds }, 39201da177e4SLinus Torvalds { 39211da177e4SLinus Torvalds .procname = "gc_elasticity", 39224990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 39231da177e4SLinus Torvalds .maxlen = sizeof(int), 39241da177e4SLinus Torvalds .mode = 0644, 3925f3d3f616SMin Zhang .proc_handler = proc_dointvec, 39261da177e4SLinus Torvalds }, 39271da177e4SLinus Torvalds { 39281da177e4SLinus Torvalds .procname = "mtu_expires", 39294990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 39301da177e4SLinus Torvalds .maxlen = sizeof(int), 39311da177e4SLinus Torvalds .mode = 0644, 39326d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 39331da177e4SLinus Torvalds }, 39341da177e4SLinus Torvalds { 39351da177e4SLinus Torvalds .procname = "min_adv_mss", 39364990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 39371da177e4SLinus Torvalds .maxlen = sizeof(int), 39381da177e4SLinus Torvalds .mode = 0644, 3939f3d3f616SMin Zhang .proc_handler = proc_dointvec, 39401da177e4SLinus Torvalds }, 39411da177e4SLinus Torvalds { 39421da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 39434990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 39441da177e4SLinus Torvalds .maxlen = sizeof(int), 39451da177e4SLinus Torvalds .mode = 0644, 39466d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 39471da177e4SLinus Torvalds }, 3948f8572d8fSEric W. Biederman { } 39491da177e4SLinus Torvalds }; 39501da177e4SLinus Torvalds 39512c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 3952760f2d01SDaniel Lezcano { 3953760f2d01SDaniel Lezcano struct ctl_table *table; 3954760f2d01SDaniel Lezcano 3955760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 3956760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 3957760f2d01SDaniel Lezcano GFP_KERNEL); 39585ee09105SYOSHIFUJI Hideaki 39595ee09105SYOSHIFUJI Hideaki if (table) { 39605ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 3961c486da34SLucian Adrian Grijincu table[0].extra1 = net; 396286393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 39635ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 39645ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 39655ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 39665ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 39675ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 39685ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 39695ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 39709c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 3971464dc801SEric W. Biederman 3972464dc801SEric W. Biederman /* Don't export sysctls to unprivileged users */ 3973464dc801SEric W. Biederman if (net->user_ns != &init_user_ns) 3974464dc801SEric W. Biederman table[0].procname = NULL; 39755ee09105SYOSHIFUJI Hideaki } 39765ee09105SYOSHIFUJI Hideaki 3977760f2d01SDaniel Lezcano return table; 3978760f2d01SDaniel Lezcano } 39791da177e4SLinus Torvalds #endif 39801da177e4SLinus Torvalds 39812c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 3982cdb18761SDaniel Lezcano { 3983633d424bSPavel Emelyanov int ret = -ENOMEM; 39848ed67789SDaniel Lezcano 398586393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 398686393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 3987f2fc6a54SBenjamin Thery 3988fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 3989fc66f95cSEric Dumazet goto out_ip6_dst_ops; 3990fc66f95cSEric Dumazet 39918ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 39928ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 39938ed67789SDaniel Lezcano GFP_KERNEL); 39948ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 3995fc66f95cSEric Dumazet goto out_ip6_dst_entries; 3996d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.path = 39978ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_null_entry; 3998d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 399962fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 400062fa8a84SDavid S. Miller ip6_template_metrics, true); 40018ed67789SDaniel Lezcano 40028ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 4003feca7d8cSVincent Bernat net->ipv6.fib6_has_custom_rules = false; 40048ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 40058ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 40068ed67789SDaniel Lezcano GFP_KERNEL); 400768fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 400868fffc67SPeter Zijlstra goto out_ip6_null_entry; 4009d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.path = 40108ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_prohibit_entry; 4011d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 401262fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 401362fa8a84SDavid S. Miller ip6_template_metrics, true); 40148ed67789SDaniel Lezcano 40158ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 40168ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 40178ed67789SDaniel Lezcano GFP_KERNEL); 401868fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 401968fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 4020d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.path = 40218ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_blk_hole_entry; 4022d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 402362fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 402462fa8a84SDavid S. Miller ip6_template_metrics, true); 40258ed67789SDaniel Lezcano #endif 40268ed67789SDaniel Lezcano 4027b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 4028b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 4029b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 4030b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 4031b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 4032b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 4033b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 4034b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 4035b339a47cSPeter Zijlstra 40366891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 40376891a346SBenjamin Thery 40388ed67789SDaniel Lezcano ret = 0; 40398ed67789SDaniel Lezcano out: 40408ed67789SDaniel Lezcano return ret; 4041f2fc6a54SBenjamin Thery 404268fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 404368fffc67SPeter Zijlstra out_ip6_prohibit_entry: 404468fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 404568fffc67SPeter Zijlstra out_ip6_null_entry: 404668fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 404768fffc67SPeter Zijlstra #endif 4048fc66f95cSEric Dumazet out_ip6_dst_entries: 4049fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 4050f2fc6a54SBenjamin Thery out_ip6_dst_ops: 4051f2fc6a54SBenjamin Thery goto out; 4052cdb18761SDaniel Lezcano } 4053cdb18761SDaniel Lezcano 40542c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 4055cdb18761SDaniel Lezcano { 40568ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 40578ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 40588ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 40598ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 40608ed67789SDaniel Lezcano #endif 406141bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 4062cdb18761SDaniel Lezcano } 4063cdb18761SDaniel Lezcano 4064d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net) 4065d189634eSThomas Graf { 4066d189634eSThomas Graf #ifdef CONFIG_PROC_FS 4067d4beaa66SGao feng proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops); 4068d4beaa66SGao feng proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops); 4069d189634eSThomas Graf #endif 4070d189634eSThomas Graf return 0; 4071d189634eSThomas Graf } 4072d189634eSThomas Graf 4073d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net) 4074d189634eSThomas Graf { 4075d189634eSThomas Graf #ifdef CONFIG_PROC_FS 4076ece31ffdSGao feng remove_proc_entry("ipv6_route", net->proc_net); 4077ece31ffdSGao feng remove_proc_entry("rt6_stats", net->proc_net); 4078d189634eSThomas Graf #endif 4079d189634eSThomas Graf } 4080d189634eSThomas Graf 4081cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 4082cdb18761SDaniel Lezcano .init = ip6_route_net_init, 4083cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 4084cdb18761SDaniel Lezcano }; 4085cdb18761SDaniel Lezcano 4086c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net) 4087c3426b47SDavid S. Miller { 4088c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 4089c3426b47SDavid S. Miller 4090c3426b47SDavid S. Miller if (!bp) 4091c3426b47SDavid S. Miller return -ENOMEM; 4092c3426b47SDavid S. Miller inet_peer_base_init(bp); 4093c3426b47SDavid S. Miller net->ipv6.peers = bp; 4094c3426b47SDavid S. Miller return 0; 4095c3426b47SDavid S. Miller } 4096c3426b47SDavid S. Miller 4097c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net) 4098c3426b47SDavid S. Miller { 4099c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv6.peers; 4100c3426b47SDavid S. Miller 4101c3426b47SDavid S. Miller net->ipv6.peers = NULL; 410256a6b248SDavid S. Miller inetpeer_invalidate_tree(bp); 4103c3426b47SDavid S. Miller kfree(bp); 4104c3426b47SDavid S. Miller } 4105c3426b47SDavid S. Miller 41062b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = { 4107c3426b47SDavid S. Miller .init = ipv6_inetpeer_init, 4108c3426b47SDavid S. Miller .exit = ipv6_inetpeer_exit, 4109c3426b47SDavid S. Miller }; 4110c3426b47SDavid S. Miller 4111d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = { 4112d189634eSThomas Graf .init = ip6_route_net_init_late, 4113d189634eSThomas Graf .exit = ip6_route_net_exit_late, 4114d189634eSThomas Graf }; 4115d189634eSThomas Graf 41168ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 41178ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 4118242d3a49SWANG Cong .priority = ADDRCONF_NOTIFY_PRIORITY - 10, 41198ed67789SDaniel Lezcano }; 41208ed67789SDaniel Lezcano 41212f460933SWANG Cong void __init ip6_route_init_special_entries(void) 41222f460933SWANG Cong { 41232f460933SWANG Cong /* Registering of the loopback is done before this portion of code, 41242f460933SWANG Cong * the loopback reference in rt6_info will not be taken, do it 41252f460933SWANG Cong * manually for init_net */ 41262f460933SWANG Cong init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 41272f460933SWANG Cong init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 41282f460933SWANG Cong #ifdef CONFIG_IPV6_MULTIPLE_TABLES 41292f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 41302f460933SWANG Cong init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 41312f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 41322f460933SWANG Cong init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 41332f460933SWANG Cong #endif 41342f460933SWANG Cong } 41352f460933SWANG Cong 4136433d49c3SDaniel Lezcano int __init ip6_route_init(void) 41371da177e4SLinus Torvalds { 4138433d49c3SDaniel Lezcano int ret; 41398d0b94afSMartin KaFai Lau int cpu; 4140433d49c3SDaniel Lezcano 41419a7ec3a9SDaniel Lezcano ret = -ENOMEM; 41429a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 41439a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 41449a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 41459a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 4146c19a28e1SFernando Carrijo goto out; 414714e50e57SDavid S. Miller 4148fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 41498ed67789SDaniel Lezcano if (ret) 4150bdb3289fSDaniel Lezcano goto out_kmem_cache; 4151bdb3289fSDaniel Lezcano 4152c3426b47SDavid S. Miller ret = register_pernet_subsys(&ipv6_inetpeer_ops); 4153c3426b47SDavid S. Miller if (ret) 4154e8803b6cSDavid S. Miller goto out_dst_entries; 41552a0c451aSThomas Graf 41567e52b33bSDavid S. Miller ret = register_pernet_subsys(&ip6_route_net_ops); 41577e52b33bSDavid S. Miller if (ret) 41587e52b33bSDavid S. Miller goto out_register_inetpeer; 4159c3426b47SDavid S. Miller 41605dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 41615dc121e9SArnaud Ebalard 4162e8803b6cSDavid S. Miller ret = fib6_init(); 4163433d49c3SDaniel Lezcano if (ret) 41648ed67789SDaniel Lezcano goto out_register_subsys; 4165433d49c3SDaniel Lezcano 4166433d49c3SDaniel Lezcano ret = xfrm6_init(); 4167433d49c3SDaniel Lezcano if (ret) 4168e8803b6cSDavid S. Miller goto out_fib6_init; 4169c35b7e72SDaniel Lezcano 4170433d49c3SDaniel Lezcano ret = fib6_rules_init(); 4171433d49c3SDaniel Lezcano if (ret) 4172433d49c3SDaniel Lezcano goto xfrm6_init; 41737e5449c2SDaniel Lezcano 4174d189634eSThomas Graf ret = register_pernet_subsys(&ip6_route_net_late_ops); 4175d189634eSThomas Graf if (ret) 4176d189634eSThomas Graf goto fib6_rules_init; 4177d189634eSThomas Graf 4178433d49c3SDaniel Lezcano ret = -ENOBUFS; 4179b97bac64SFlorian Westphal if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, 0) || 4180b97bac64SFlorian Westphal __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, 0) || 4181e3a22b7fSFlorian Westphal __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, 4182e3a22b7fSFlorian Westphal RTNL_FLAG_DOIT_UNLOCKED)) 4183d189634eSThomas Graf goto out_register_late_subsys; 4184433d49c3SDaniel Lezcano 41858ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 4186cdb18761SDaniel Lezcano if (ret) 4187d189634eSThomas Graf goto out_register_late_subsys; 41888ed67789SDaniel Lezcano 41898d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 41908d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 41918d0b94afSMartin KaFai Lau 41928d0b94afSMartin KaFai Lau INIT_LIST_HEAD(&ul->head); 41938d0b94afSMartin KaFai Lau spin_lock_init(&ul->lock); 41948d0b94afSMartin KaFai Lau } 41958d0b94afSMartin KaFai Lau 4196433d49c3SDaniel Lezcano out: 4197433d49c3SDaniel Lezcano return ret; 4198433d49c3SDaniel Lezcano 4199d189634eSThomas Graf out_register_late_subsys: 4200d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 4201433d49c3SDaniel Lezcano fib6_rules_init: 4202433d49c3SDaniel Lezcano fib6_rules_cleanup(); 4203433d49c3SDaniel Lezcano xfrm6_init: 4204433d49c3SDaniel Lezcano xfrm6_fini(); 42052a0c451aSThomas Graf out_fib6_init: 42062a0c451aSThomas Graf fib6_gc_cleanup(); 42078ed67789SDaniel Lezcano out_register_subsys: 42088ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 42097e52b33bSDavid S. Miller out_register_inetpeer: 42107e52b33bSDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 4211fc66f95cSEric Dumazet out_dst_entries: 4212fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 4213433d49c3SDaniel Lezcano out_kmem_cache: 4214f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 4215433d49c3SDaniel Lezcano goto out; 42161da177e4SLinus Torvalds } 42171da177e4SLinus Torvalds 42181da177e4SLinus Torvalds void ip6_route_cleanup(void) 42191da177e4SLinus Torvalds { 42208ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 4221d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 4222101367c2SThomas Graf fib6_rules_cleanup(); 42231da177e4SLinus Torvalds xfrm6_fini(); 42241da177e4SLinus Torvalds fib6_gc_cleanup(); 4225c3426b47SDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 42268ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 422741bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 4228f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 42291da177e4SLinus Torvalds } 4230