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> 571da177e4SLinus Torvalds #include <net/xfrm.h> 588d71740cSTom Tucker #include <net/netevent.h> 5921713ebcSThomas Graf #include <net/netlink.h> 6051ebd318SNicolas Dichtel #include <net/nexthop.h> 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds #include <asm/uaccess.h> 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 651da177e4SLinus Torvalds #include <linux/sysctl.h> 661da177e4SLinus Torvalds #endif 671da177e4SLinus Torvalds 68afc154e9SHannes Frederic Sowa enum rt6_nud_state { 697e980569SJiri Benc RT6_NUD_FAIL_HARD = -3, 707e980569SJiri Benc RT6_NUD_FAIL_PROBE = -2, 717e980569SJiri Benc RT6_NUD_FAIL_DO_RR = -1, 72afc154e9SHannes Frederic Sowa RT6_NUD_SUCCEED = 1 73afc154e9SHannes Frederic Sowa }; 74afc154e9SHannes Frederic Sowa 7583a09abdSMartin KaFai Lau static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort); 761da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie); 770dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst); 78ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst); 791da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *); 801da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *); 811da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *, 821da177e4SLinus Torvalds struct net_device *dev, int how); 83569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops); 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds static int ip6_pkt_discard(struct sk_buff *skb); 86aad88724SEric Dumazet static int ip6_pkt_discard_out(struct sock *sk, struct sk_buff *skb); 877150aedeSKamala R static int ip6_pkt_prohibit(struct sk_buff *skb); 88aad88724SEric Dumazet static int ip6_pkt_prohibit_out(struct sock *sk, struct sk_buff *skb); 891da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb); 906700c270SDavid S. Miller static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 916700c270SDavid S. Miller struct sk_buff *skb, u32 mtu); 926700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, 936700c270SDavid S. Miller struct sk_buff *skb); 944b32b5adSMartin KaFai Lau static void rt6_dst_from_metrics_check(struct rt6_info *rt); 9552bd4c0cSNicolas Dichtel static int rt6_score_route(struct rt6_info *rt, int oif, int strict); 961da177e4SLinus Torvalds 9770ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 98efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 99b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 100b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex, 10195c96174SEric Dumazet unsigned int pref); 102efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 103b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 104b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex); 10570ceb4f5SYOSHIFUJI Hideaki #endif 10670ceb4f5SYOSHIFUJI Hideaki 1078d0b94afSMartin KaFai Lau struct uncached_list { 1088d0b94afSMartin KaFai Lau spinlock_t lock; 1098d0b94afSMartin KaFai Lau struct list_head head; 1108d0b94afSMartin KaFai Lau }; 1118d0b94afSMartin KaFai Lau 1128d0b94afSMartin KaFai Lau static DEFINE_PER_CPU_ALIGNED(struct uncached_list, rt6_uncached_list); 1138d0b94afSMartin KaFai Lau 1148d0b94afSMartin KaFai Lau static void rt6_uncached_list_add(struct rt6_info *rt) 1158d0b94afSMartin KaFai Lau { 1168d0b94afSMartin KaFai Lau struct uncached_list *ul = raw_cpu_ptr(&rt6_uncached_list); 1178d0b94afSMartin KaFai Lau 1188d0b94afSMartin KaFai Lau rt->dst.flags |= DST_NOCACHE; 1198d0b94afSMartin KaFai Lau rt->rt6i_uncached_list = ul; 1208d0b94afSMartin KaFai Lau 1218d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1228d0b94afSMartin KaFai Lau list_add_tail(&rt->rt6i_uncached, &ul->head); 1238d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1248d0b94afSMartin KaFai Lau } 1258d0b94afSMartin KaFai Lau 1268d0b94afSMartin KaFai Lau static void rt6_uncached_list_del(struct rt6_info *rt) 1278d0b94afSMartin KaFai Lau { 1288d0b94afSMartin KaFai Lau if (!list_empty(&rt->rt6i_uncached)) { 1298d0b94afSMartin KaFai Lau struct uncached_list *ul = rt->rt6i_uncached_list; 1308d0b94afSMartin KaFai Lau 1318d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1328d0b94afSMartin KaFai Lau list_del(&rt->rt6i_uncached); 1338d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1348d0b94afSMartin KaFai Lau } 1358d0b94afSMartin KaFai Lau } 1368d0b94afSMartin KaFai Lau 1378d0b94afSMartin KaFai Lau static void rt6_uncached_list_flush_dev(struct net *net, struct net_device *dev) 1388d0b94afSMartin KaFai Lau { 1398d0b94afSMartin KaFai Lau struct net_device *loopback_dev = net->loopback_dev; 1408d0b94afSMartin KaFai Lau int cpu; 1418d0b94afSMartin KaFai Lau 1428d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 1438d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 1448d0b94afSMartin KaFai Lau struct rt6_info *rt; 1458d0b94afSMartin KaFai Lau 1468d0b94afSMartin KaFai Lau spin_lock_bh(&ul->lock); 1478d0b94afSMartin KaFai Lau list_for_each_entry(rt, &ul->head, rt6i_uncached) { 1488d0b94afSMartin KaFai Lau struct inet6_dev *rt_idev = rt->rt6i_idev; 1498d0b94afSMartin KaFai Lau struct net_device *rt_dev = rt->dst.dev; 1508d0b94afSMartin KaFai Lau 1518d0b94afSMartin KaFai Lau if (rt_idev && (rt_idev->dev == dev || !dev) && 1528d0b94afSMartin KaFai Lau rt_idev->dev != loopback_dev) { 1538d0b94afSMartin KaFai Lau rt->rt6i_idev = in6_dev_get(loopback_dev); 1548d0b94afSMartin KaFai Lau in6_dev_put(rt_idev); 1558d0b94afSMartin KaFai Lau } 1568d0b94afSMartin KaFai Lau 1578d0b94afSMartin KaFai Lau if (rt_dev && (rt_dev == dev || !dev) && 1588d0b94afSMartin KaFai Lau rt_dev != loopback_dev) { 1598d0b94afSMartin KaFai Lau rt->dst.dev = loopback_dev; 1608d0b94afSMartin KaFai Lau dev_hold(rt->dst.dev); 1618d0b94afSMartin KaFai Lau dev_put(rt_dev); 1628d0b94afSMartin KaFai Lau } 1638d0b94afSMartin KaFai Lau } 1648d0b94afSMartin KaFai Lau spin_unlock_bh(&ul->lock); 1658d0b94afSMartin KaFai Lau } 1668d0b94afSMartin KaFai Lau } 1678d0b94afSMartin KaFai Lau 168d52d3997SMartin KaFai Lau static u32 *rt6_pcpu_cow_metrics(struct rt6_info *rt) 169d52d3997SMartin KaFai Lau { 170d52d3997SMartin KaFai Lau return dst_metrics_write_ptr(rt->dst.from); 171d52d3997SMartin KaFai Lau } 172d52d3997SMartin KaFai Lau 17306582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old) 17406582540SDavid S. Miller { 17506582540SDavid S. Miller struct rt6_info *rt = (struct rt6_info *)dst; 17606582540SDavid S. Miller 177d52d3997SMartin KaFai Lau if (rt->rt6i_flags & RTF_PCPU) 178d52d3997SMartin KaFai Lau return rt6_pcpu_cow_metrics(rt); 179d52d3997SMartin KaFai Lau else if (rt->rt6i_flags & RTF_CACHE) 1804b32b5adSMartin KaFai Lau return NULL; 1814b32b5adSMartin KaFai Lau else 1823b471175SMartin KaFai Lau return dst_cow_metrics_generic(dst, old); 18306582540SDavid S. Miller } 18406582540SDavid S. Miller 185f894cbf8SDavid S. Miller static inline const void *choose_neigh_daddr(struct rt6_info *rt, 186f894cbf8SDavid S. Miller struct sk_buff *skb, 187f894cbf8SDavid S. Miller const void *daddr) 18839232973SDavid S. Miller { 18939232973SDavid S. Miller struct in6_addr *p = &rt->rt6i_gateway; 19039232973SDavid S. Miller 191a7563f34SDavid S. Miller if (!ipv6_addr_any(p)) 19239232973SDavid S. Miller return (const void *) p; 193f894cbf8SDavid S. Miller else if (skb) 194f894cbf8SDavid S. Miller return &ipv6_hdr(skb)->daddr; 19539232973SDavid S. Miller return daddr; 19639232973SDavid S. Miller } 19739232973SDavid S. Miller 198f894cbf8SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst, 199f894cbf8SDavid S. Miller struct sk_buff *skb, 200f894cbf8SDavid S. Miller const void *daddr) 201d3aaeb38SDavid S. Miller { 20239232973SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 20339232973SDavid S. Miller struct neighbour *n; 20439232973SDavid S. Miller 205f894cbf8SDavid S. Miller daddr = choose_neigh_daddr(rt, skb, daddr); 2068e022ee6SYOSHIFUJI Hideaki / 吉藤英明 n = __ipv6_neigh_lookup(dst->dev, daddr); 207f83c7790SDavid S. Miller if (n) 208f83c7790SDavid S. Miller return n; 209f83c7790SDavid S. Miller return neigh_create(&nd_tbl, daddr, dst->dev); 210f83c7790SDavid S. Miller } 211f83c7790SDavid S. Miller 2129a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = { 2131da177e4SLinus Torvalds .family = AF_INET6, 2141da177e4SLinus Torvalds .gc = ip6_dst_gc, 2151da177e4SLinus Torvalds .gc_thresh = 1024, 2161da177e4SLinus Torvalds .check = ip6_dst_check, 2170dbaee3bSDavid S. Miller .default_advmss = ip6_default_advmss, 218ebb762f2SSteffen Klassert .mtu = ip6_mtu, 21906582540SDavid S. Miller .cow_metrics = ipv6_cow_metrics, 2201da177e4SLinus Torvalds .destroy = ip6_dst_destroy, 2211da177e4SLinus Torvalds .ifdown = ip6_dst_ifdown, 2221da177e4SLinus Torvalds .negative_advice = ip6_negative_advice, 2231da177e4SLinus Torvalds .link_failure = ip6_link_failure, 2241da177e4SLinus Torvalds .update_pmtu = ip6_rt_update_pmtu, 2256e157b6aSDavid S. Miller .redirect = rt6_do_redirect, 2261ac06e03SHerbert Xu .local_out = __ip6_local_out, 227d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 2281da177e4SLinus Torvalds }; 2291da177e4SLinus Torvalds 230ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst) 231ec831ea7SRoland Dreier { 232618f9bc7SSteffen Klassert unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); 233618f9bc7SSteffen Klassert 234618f9bc7SSteffen Klassert return mtu ? : dst->dev->mtu; 235ec831ea7SRoland Dreier } 236ec831ea7SRoland Dreier 2376700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk, 2386700c270SDavid S. Miller struct sk_buff *skb, u32 mtu) 23914e50e57SDavid S. Miller { 24014e50e57SDavid S. Miller } 24114e50e57SDavid S. Miller 2426700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk, 2436700c270SDavid S. Miller struct sk_buff *skb) 244b587ee3bSDavid S. Miller { 245b587ee3bSDavid S. Miller } 246b587ee3bSDavid S. Miller 2470972ddb2SHeld Bernhard static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst, 2480972ddb2SHeld Bernhard unsigned long old) 2490972ddb2SHeld Bernhard { 2500972ddb2SHeld Bernhard return NULL; 2510972ddb2SHeld Bernhard } 2520972ddb2SHeld Bernhard 25314e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = { 25414e50e57SDavid S. Miller .family = AF_INET6, 25514e50e57SDavid S. Miller .destroy = ip6_dst_destroy, 25614e50e57SDavid S. Miller .check = ip6_dst_check, 257ebb762f2SSteffen Klassert .mtu = ip6_blackhole_mtu, 258214f45c9SEric Dumazet .default_advmss = ip6_default_advmss, 25914e50e57SDavid S. Miller .update_pmtu = ip6_rt_blackhole_update_pmtu, 260b587ee3bSDavid S. Miller .redirect = ip6_rt_blackhole_redirect, 2610972ddb2SHeld Bernhard .cow_metrics = ip6_rt_blackhole_cow_metrics, 262d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 26314e50e57SDavid S. Miller }; 26414e50e57SDavid S. Miller 26562fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = { 26614edd87dSLi RongQing [RTAX_HOPLIMIT - 1] = 0, 26762fa8a84SDavid S. Miller }; 26862fa8a84SDavid S. Miller 269fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = { 2701da177e4SLinus Torvalds .dst = { 2711da177e4SLinus Torvalds .__refcnt = ATOMIC_INIT(1), 2721da177e4SLinus Torvalds .__use = 1, 2732c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 2741da177e4SLinus Torvalds .error = -ENETUNREACH, 2751da177e4SLinus Torvalds .input = ip6_pkt_discard, 2761da177e4SLinus Torvalds .output = ip6_pkt_discard_out, 2771da177e4SLinus Torvalds }, 2781da177e4SLinus Torvalds .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 2794f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 2801da177e4SLinus Torvalds .rt6i_metric = ~(u32) 0, 2811da177e4SLinus Torvalds .rt6i_ref = ATOMIC_INIT(1), 2821da177e4SLinus Torvalds }; 2831da177e4SLinus Torvalds 284101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES 285101367c2SThomas Graf 286fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = { 287101367c2SThomas Graf .dst = { 288101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 289101367c2SThomas Graf .__use = 1, 2902c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 291101367c2SThomas Graf .error = -EACCES, 2929ce8ade0SThomas Graf .input = ip6_pkt_prohibit, 2939ce8ade0SThomas Graf .output = ip6_pkt_prohibit_out, 294101367c2SThomas Graf }, 295101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 2964f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 297101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 298101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 299101367c2SThomas Graf }; 300101367c2SThomas Graf 301fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = { 302101367c2SThomas Graf .dst = { 303101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 304101367c2SThomas Graf .__use = 1, 3052c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 306101367c2SThomas Graf .error = -EINVAL, 307352e512cSHerbert Xu .input = dst_discard, 308aad88724SEric Dumazet .output = dst_discard_sk, 309101367c2SThomas Graf }, 310101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 3114f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 312101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 313101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 314101367c2SThomas Graf }; 315101367c2SThomas Graf 316101367c2SThomas Graf #endif 317101367c2SThomas Graf 3181da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */ 319d52d3997SMartin KaFai Lau static struct rt6_info *__ip6_dst_alloc(struct net *net, 320957c665fSDavid S. Miller struct net_device *dev, 3218b96d22dSDavid S. Miller int flags, 3228b96d22dSDavid S. Miller struct fib6_table *table) 3231da177e4SLinus Torvalds { 32497bab73fSDavid S. Miller struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev, 3256f3118b5SNicolas Dichtel 0, DST_OBSOLETE_FORCE_CHK, flags); 326cf911662SDavid S. Miller 32797bab73fSDavid S. Miller if (rt) { 3288104891bSSteffen Klassert struct dst_entry *dst = &rt->dst; 3298104891bSSteffen Klassert 3308104891bSSteffen Klassert memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst)); 33151ebd318SNicolas Dichtel INIT_LIST_HEAD(&rt->rt6i_siblings); 3328d0b94afSMartin KaFai Lau INIT_LIST_HEAD(&rt->rt6i_uncached); 33397bab73fSDavid S. Miller } 334cf911662SDavid S. Miller return rt; 3351da177e4SLinus Torvalds } 3361da177e4SLinus Torvalds 337d52d3997SMartin KaFai Lau static struct rt6_info *ip6_dst_alloc(struct net *net, 338d52d3997SMartin KaFai Lau struct net_device *dev, 339d52d3997SMartin KaFai Lau int flags, 340d52d3997SMartin KaFai Lau struct fib6_table *table) 341d52d3997SMartin KaFai Lau { 342d52d3997SMartin KaFai Lau struct rt6_info *rt = __ip6_dst_alloc(net, dev, flags, table); 343d52d3997SMartin KaFai Lau 344d52d3997SMartin KaFai Lau if (rt) { 345d52d3997SMartin KaFai Lau rt->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, GFP_ATOMIC); 346d52d3997SMartin KaFai Lau if (rt->rt6i_pcpu) { 347d52d3997SMartin KaFai Lau int cpu; 348d52d3997SMartin KaFai Lau 349d52d3997SMartin KaFai Lau for_each_possible_cpu(cpu) { 350d52d3997SMartin KaFai Lau struct rt6_info **p; 351d52d3997SMartin KaFai Lau 352d52d3997SMartin KaFai Lau p = per_cpu_ptr(rt->rt6i_pcpu, cpu); 353d52d3997SMartin KaFai Lau /* no one shares rt */ 354d52d3997SMartin KaFai Lau *p = NULL; 355d52d3997SMartin KaFai Lau } 356d52d3997SMartin KaFai Lau } else { 357d52d3997SMartin KaFai Lau dst_destroy((struct dst_entry *)rt); 358d52d3997SMartin KaFai Lau return NULL; 359d52d3997SMartin KaFai Lau } 360d52d3997SMartin KaFai Lau } 361d52d3997SMartin KaFai Lau 362d52d3997SMartin KaFai Lau return rt; 363d52d3997SMartin KaFai Lau } 364d52d3997SMartin KaFai Lau 3651da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst) 3661da177e4SLinus Torvalds { 3671da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 368ecd98837SYOSHIFUJI Hideaki / 吉藤英明 struct dst_entry *from = dst->from; 3698d0b94afSMartin KaFai Lau struct inet6_dev *idev; 3701da177e4SLinus Torvalds 3718e2ec639SYan, Zheng dst_destroy_metrics_generic(dst); 372d52d3997SMartin KaFai Lau free_percpu(rt->rt6i_pcpu); 3738d0b94afSMartin KaFai Lau rt6_uncached_list_del(rt); 3748d0b94afSMartin KaFai Lau 3758d0b94afSMartin KaFai Lau idev = rt->rt6i_idev; 37638308473SDavid S. Miller if (idev) { 3771da177e4SLinus Torvalds rt->rt6i_idev = NULL; 3781da177e4SLinus Torvalds in6_dev_put(idev); 3791da177e4SLinus Torvalds } 3801716a961SGao feng 381ecd98837SYOSHIFUJI Hideaki / 吉藤英明 dst->from = NULL; 382ecd98837SYOSHIFUJI Hideaki / 吉藤英明 dst_release(from); 383b3419363SDavid S. Miller } 384b3419363SDavid S. Miller 3851da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 3861da177e4SLinus Torvalds int how) 3871da177e4SLinus Torvalds { 3881da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 3891da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 3905a3e55d6SDenis V. Lunev struct net_device *loopback_dev = 391c346dca1SYOSHIFUJI Hideaki dev_net(dev)->loopback_dev; 3921da177e4SLinus Torvalds 39397cac082SDavid S. Miller if (dev != loopback_dev) { 39497cac082SDavid S. Miller if (idev && idev->dev == dev) { 3955a3e55d6SDenis V. Lunev struct inet6_dev *loopback_idev = 3965a3e55d6SDenis V. Lunev in6_dev_get(loopback_dev); 39738308473SDavid S. Miller if (loopback_idev) { 3981da177e4SLinus Torvalds rt->rt6i_idev = loopback_idev; 3991da177e4SLinus Torvalds in6_dev_put(idev); 4001da177e4SLinus Torvalds } 4011da177e4SLinus Torvalds } 40297cac082SDavid S. Miller } 4031da177e4SLinus Torvalds } 4041da177e4SLinus Torvalds 405a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt) 4061da177e4SLinus Torvalds { 4071716a961SGao feng if (rt->rt6i_flags & RTF_EXPIRES) { 4081716a961SGao feng if (time_after(jiffies, rt->dst.expires)) 409a50feda5SEric Dumazet return true; 4101716a961SGao feng } else if (rt->dst.from) { 4113fd91fb3SLi RongQing return rt6_check_expired((struct rt6_info *) rt->dst.from); 4121716a961SGao feng } 413a50feda5SEric Dumazet return false; 4141da177e4SLinus Torvalds } 4151da177e4SLinus Torvalds 41651ebd318SNicolas Dichtel /* Multipath route selection: 41751ebd318SNicolas Dichtel * Hash based function using packet header and flowlabel. 41851ebd318SNicolas Dichtel * Adapted from fib_info_hashfn() 41951ebd318SNicolas Dichtel */ 42051ebd318SNicolas Dichtel static int rt6_info_hash_nhsfn(unsigned int candidate_count, 42151ebd318SNicolas Dichtel const struct flowi6 *fl6) 42251ebd318SNicolas Dichtel { 42351ebd318SNicolas Dichtel unsigned int val = fl6->flowi6_proto; 42451ebd318SNicolas Dichtel 425c08977bbSYOSHIFUJI Hideaki / 吉藤英明 val ^= ipv6_addr_hash(&fl6->daddr); 426c08977bbSYOSHIFUJI Hideaki / 吉藤英明 val ^= ipv6_addr_hash(&fl6->saddr); 42751ebd318SNicolas Dichtel 42851ebd318SNicolas Dichtel /* Work only if this not encapsulated */ 42951ebd318SNicolas Dichtel switch (fl6->flowi6_proto) { 43051ebd318SNicolas Dichtel case IPPROTO_UDP: 43151ebd318SNicolas Dichtel case IPPROTO_TCP: 43251ebd318SNicolas Dichtel case IPPROTO_SCTP: 433b3ce5ae1SNicolas Dichtel val ^= (__force u16)fl6->fl6_sport; 434b3ce5ae1SNicolas Dichtel val ^= (__force u16)fl6->fl6_dport; 43551ebd318SNicolas Dichtel break; 43651ebd318SNicolas Dichtel 43751ebd318SNicolas Dichtel case IPPROTO_ICMPV6: 438b3ce5ae1SNicolas Dichtel val ^= (__force u16)fl6->fl6_icmp_type; 439b3ce5ae1SNicolas Dichtel val ^= (__force u16)fl6->fl6_icmp_code; 44051ebd318SNicolas Dichtel break; 44151ebd318SNicolas Dichtel } 44251ebd318SNicolas Dichtel /* RFC6438 recommands to use flowlabel */ 443b3ce5ae1SNicolas Dichtel val ^= (__force u32)fl6->flowlabel; 44451ebd318SNicolas Dichtel 44551ebd318SNicolas Dichtel /* Perhaps, we need to tune, this function? */ 44651ebd318SNicolas Dichtel val = val ^ (val >> 7) ^ (val >> 12); 44751ebd318SNicolas Dichtel return val % candidate_count; 44851ebd318SNicolas Dichtel } 44951ebd318SNicolas Dichtel 45051ebd318SNicolas Dichtel static struct rt6_info *rt6_multipath_select(struct rt6_info *match, 45152bd4c0cSNicolas Dichtel struct flowi6 *fl6, int oif, 45252bd4c0cSNicolas Dichtel int strict) 45351ebd318SNicolas Dichtel { 45451ebd318SNicolas Dichtel struct rt6_info *sibling, *next_sibling; 45551ebd318SNicolas Dichtel int route_choosen; 45651ebd318SNicolas Dichtel 45751ebd318SNicolas Dichtel route_choosen = rt6_info_hash_nhsfn(match->rt6i_nsiblings + 1, fl6); 45851ebd318SNicolas Dichtel /* Don't change the route, if route_choosen == 0 45951ebd318SNicolas Dichtel * (siblings does not include ourself) 46051ebd318SNicolas Dichtel */ 46151ebd318SNicolas Dichtel if (route_choosen) 46251ebd318SNicolas Dichtel list_for_each_entry_safe(sibling, next_sibling, 46351ebd318SNicolas Dichtel &match->rt6i_siblings, rt6i_siblings) { 46451ebd318SNicolas Dichtel route_choosen--; 46551ebd318SNicolas Dichtel if (route_choosen == 0) { 46652bd4c0cSNicolas Dichtel if (rt6_score_route(sibling, oif, strict) < 0) 46752bd4c0cSNicolas Dichtel break; 46851ebd318SNicolas Dichtel match = sibling; 46951ebd318SNicolas Dichtel break; 47051ebd318SNicolas Dichtel } 47151ebd318SNicolas Dichtel } 47251ebd318SNicolas Dichtel return match; 47351ebd318SNicolas Dichtel } 47451ebd318SNicolas Dichtel 4751da177e4SLinus Torvalds /* 476c71099acSThomas Graf * Route lookup. Any table->tb6_lock is implied. 4771da177e4SLinus Torvalds */ 4781da177e4SLinus Torvalds 4798ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net, 4808ed67789SDaniel Lezcano struct rt6_info *rt, 481b71d1d42SEric Dumazet const struct in6_addr *saddr, 4821da177e4SLinus Torvalds int oif, 483d420895eSYOSHIFUJI Hideaki int flags) 4841da177e4SLinus Torvalds { 4851da177e4SLinus Torvalds struct rt6_info *local = NULL; 4861da177e4SLinus Torvalds struct rt6_info *sprt; 4871da177e4SLinus Torvalds 488dd3abc4eSYOSHIFUJI Hideaki if (!oif && ipv6_addr_any(saddr)) 489dd3abc4eSYOSHIFUJI Hideaki goto out; 490dd3abc4eSYOSHIFUJI Hideaki 491d8d1f30bSChangli Gao for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) { 492d1918542SDavid S. Miller struct net_device *dev = sprt->dst.dev; 493dd3abc4eSYOSHIFUJI Hideaki 494dd3abc4eSYOSHIFUJI Hideaki if (oif) { 4951da177e4SLinus Torvalds if (dev->ifindex == oif) 4961da177e4SLinus Torvalds return sprt; 4971da177e4SLinus Torvalds if (dev->flags & IFF_LOOPBACK) { 49838308473SDavid S. Miller if (!sprt->rt6i_idev || 4991da177e4SLinus Torvalds sprt->rt6i_idev->dev->ifindex != oif) { 500d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE && oif) 5011da177e4SLinus Torvalds continue; 5021da177e4SLinus Torvalds if (local && (!oif || 5031da177e4SLinus Torvalds local->rt6i_idev->dev->ifindex == oif)) 5041da177e4SLinus Torvalds continue; 5051da177e4SLinus Torvalds } 5061da177e4SLinus Torvalds local = sprt; 5071da177e4SLinus Torvalds } 508dd3abc4eSYOSHIFUJI Hideaki } else { 509dd3abc4eSYOSHIFUJI Hideaki if (ipv6_chk_addr(net, saddr, dev, 510dd3abc4eSYOSHIFUJI Hideaki flags & RT6_LOOKUP_F_IFACE)) 511dd3abc4eSYOSHIFUJI Hideaki return sprt; 512dd3abc4eSYOSHIFUJI Hideaki } 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds 515dd3abc4eSYOSHIFUJI Hideaki if (oif) { 5161da177e4SLinus Torvalds if (local) 5171da177e4SLinus Torvalds return local; 5181da177e4SLinus Torvalds 519d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE) 5208ed67789SDaniel Lezcano return net->ipv6.ip6_null_entry; 5211da177e4SLinus Torvalds } 522dd3abc4eSYOSHIFUJI Hideaki out: 5231da177e4SLinus Torvalds return rt; 5241da177e4SLinus Torvalds } 5251da177e4SLinus Torvalds 52627097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 527c2f17e82SHannes Frederic Sowa struct __rt6_probe_work { 528c2f17e82SHannes Frederic Sowa struct work_struct work; 529c2f17e82SHannes Frederic Sowa struct in6_addr target; 530c2f17e82SHannes Frederic Sowa struct net_device *dev; 531c2f17e82SHannes Frederic Sowa }; 532c2f17e82SHannes Frederic Sowa 533c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w) 534c2f17e82SHannes Frederic Sowa { 535c2f17e82SHannes Frederic Sowa struct in6_addr mcaddr; 536c2f17e82SHannes Frederic Sowa struct __rt6_probe_work *work = 537c2f17e82SHannes Frederic Sowa container_of(w, struct __rt6_probe_work, work); 538c2f17e82SHannes Frederic Sowa 539c2f17e82SHannes Frederic Sowa addrconf_addr_solict_mult(&work->target, &mcaddr); 540c2f17e82SHannes Frederic Sowa ndisc_send_ns(work->dev, NULL, &work->target, &mcaddr, NULL); 541c2f17e82SHannes Frederic Sowa dev_put(work->dev); 542662f5533SMichael Büsch kfree(work); 543c2f17e82SHannes Frederic Sowa } 544c2f17e82SHannes Frederic Sowa 54527097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt) 54627097255SYOSHIFUJI Hideaki { 547f2c31e32SEric Dumazet struct neighbour *neigh; 54827097255SYOSHIFUJI Hideaki /* 54927097255SYOSHIFUJI Hideaki * Okay, this does not seem to be appropriate 55027097255SYOSHIFUJI Hideaki * for now, however, we need to check if it 55127097255SYOSHIFUJI Hideaki * is really so; aka Router Reachability Probing. 55227097255SYOSHIFUJI Hideaki * 55327097255SYOSHIFUJI Hideaki * Router Reachability Probe MUST be rate-limited 55427097255SYOSHIFUJI Hideaki * to no more than one per minute. 55527097255SYOSHIFUJI Hideaki */ 5562152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (!rt || !(rt->rt6i_flags & RTF_GATEWAY)) 557fdd6681dSAmerigo Wang return; 5582152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 5592152caeaSYOSHIFUJI Hideaki / 吉藤英明 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 5602152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 5612152caeaSYOSHIFUJI Hideaki / 吉藤英明 write_lock(&neigh->lock); 5622152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (neigh->nud_state & NUD_VALID) 5632152caeaSYOSHIFUJI Hideaki / 吉藤英明 goto out; 5647ff74a59SYOSHIFUJI Hideaki / 吉藤英明 } 5652152caeaSYOSHIFUJI Hideaki / 吉藤英明 5662152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (!neigh || 56752e16356SYOSHIFUJI Hideaki time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) { 568c2f17e82SHannes Frederic Sowa struct __rt6_probe_work *work; 56927097255SYOSHIFUJI Hideaki 570c2f17e82SHannes Frederic Sowa work = kmalloc(sizeof(*work), GFP_ATOMIC); 571c2f17e82SHannes Frederic Sowa 572c2f17e82SHannes Frederic Sowa if (neigh && work) 5737e980569SJiri Benc __neigh_set_probe_once(neigh); 5742152caeaSYOSHIFUJI Hideaki / 吉藤英明 575c2f17e82SHannes Frederic Sowa if (neigh) 576c2f17e82SHannes Frederic Sowa write_unlock(&neigh->lock); 577c2f17e82SHannes Frederic Sowa 578c2f17e82SHannes Frederic Sowa if (work) { 579c2f17e82SHannes Frederic Sowa INIT_WORK(&work->work, rt6_probe_deferred); 580c2f17e82SHannes Frederic Sowa work->target = rt->rt6i_gateway; 581c2f17e82SHannes Frederic Sowa dev_hold(rt->dst.dev); 582c2f17e82SHannes Frederic Sowa work->dev = rt->dst.dev; 583c2f17e82SHannes Frederic Sowa schedule_work(&work->work); 584c2f17e82SHannes Frederic Sowa } 585f2c31e32SEric Dumazet } else { 5862152caeaSYOSHIFUJI Hideaki / 吉藤英明 out: 5872152caeaSYOSHIFUJI Hideaki / 吉藤英明 write_unlock(&neigh->lock); 58827097255SYOSHIFUJI Hideaki } 5892152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 590f2c31e32SEric Dumazet } 59127097255SYOSHIFUJI Hideaki #else 59227097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt) 59327097255SYOSHIFUJI Hideaki { 59427097255SYOSHIFUJI Hideaki } 59527097255SYOSHIFUJI Hideaki #endif 59627097255SYOSHIFUJI Hideaki 5971da177e4SLinus Torvalds /* 598554cfb7eSYOSHIFUJI Hideaki * Default Router Selection (RFC 2461 6.3.6) 5991da177e4SLinus Torvalds */ 600b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif) 6011da177e4SLinus Torvalds { 602d1918542SDavid S. Miller struct net_device *dev = rt->dst.dev; 603161980f4SDavid S. Miller if (!oif || dev->ifindex == oif) 604554cfb7eSYOSHIFUJI Hideaki return 2; 605161980f4SDavid S. Miller if ((dev->flags & IFF_LOOPBACK) && 606161980f4SDavid S. Miller rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif) 607161980f4SDavid S. Miller return 1; 608554cfb7eSYOSHIFUJI Hideaki return 0; 6091da177e4SLinus Torvalds } 6101da177e4SLinus Torvalds 611afc154e9SHannes Frederic Sowa static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt) 6121da177e4SLinus Torvalds { 613f2c31e32SEric Dumazet struct neighbour *neigh; 614afc154e9SHannes Frederic Sowa enum rt6_nud_state ret = RT6_NUD_FAIL_HARD; 615f2c31e32SEric Dumazet 6164d0c5911SYOSHIFUJI Hideaki if (rt->rt6i_flags & RTF_NONEXTHOP || 6174d0c5911SYOSHIFUJI Hideaki !(rt->rt6i_flags & RTF_GATEWAY)) 618afc154e9SHannes Frederic Sowa return RT6_NUD_SUCCEED; 619145a3621SYOSHIFUJI Hideaki / 吉藤英明 620145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 621145a3621SYOSHIFUJI Hideaki / 吉藤英明 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 622145a3621SYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 623145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_lock(&neigh->lock); 624554cfb7eSYOSHIFUJI Hideaki if (neigh->nud_state & NUD_VALID) 625afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 626398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 627a5a81f0bSPaul Marks else if (!(neigh->nud_state & NUD_FAILED)) 628afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 6297e980569SJiri Benc else 6307e980569SJiri Benc ret = RT6_NUD_FAIL_PROBE; 631398bcbebSYOSHIFUJI Hideaki #endif 632145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_unlock(&neigh->lock); 633afc154e9SHannes Frederic Sowa } else { 634afc154e9SHannes Frederic Sowa ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ? 6357e980569SJiri Benc RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR; 636a5a81f0bSPaul Marks } 637145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 638145a3621SYOSHIFUJI Hideaki / 吉藤英明 639a5a81f0bSPaul Marks return ret; 6401da177e4SLinus Torvalds } 6411da177e4SLinus Torvalds 642554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif, 643554cfb7eSYOSHIFUJI Hideaki int strict) 644554cfb7eSYOSHIFUJI Hideaki { 645a5a81f0bSPaul Marks int m; 6464d0c5911SYOSHIFUJI Hideaki 6474d0c5911SYOSHIFUJI Hideaki m = rt6_check_dev(rt, oif); 64877d16f45SYOSHIFUJI Hideaki if (!m && (strict & RT6_LOOKUP_F_IFACE)) 649afc154e9SHannes Frederic Sowa return RT6_NUD_FAIL_HARD; 650ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 651ebacaaa0SYOSHIFUJI Hideaki m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2; 652ebacaaa0SYOSHIFUJI Hideaki #endif 653afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) { 654afc154e9SHannes Frederic Sowa int n = rt6_check_neigh(rt); 655afc154e9SHannes Frederic Sowa if (n < 0) 656afc154e9SHannes Frederic Sowa return n; 657afc154e9SHannes Frederic Sowa } 658554cfb7eSYOSHIFUJI Hideaki return m; 659554cfb7eSYOSHIFUJI Hideaki } 660554cfb7eSYOSHIFUJI Hideaki 661f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict, 662afc154e9SHannes Frederic Sowa int *mpri, struct rt6_info *match, 663afc154e9SHannes Frederic Sowa bool *do_rr) 664554cfb7eSYOSHIFUJI Hideaki { 665554cfb7eSYOSHIFUJI Hideaki int m; 666afc154e9SHannes Frederic Sowa bool match_do_rr = false; 667554cfb7eSYOSHIFUJI Hideaki 668554cfb7eSYOSHIFUJI Hideaki if (rt6_check_expired(rt)) 669f11e6659SDavid S. Miller goto out; 670554cfb7eSYOSHIFUJI Hideaki 671554cfb7eSYOSHIFUJI Hideaki m = rt6_score_route(rt, oif, strict); 6727e980569SJiri Benc if (m == RT6_NUD_FAIL_DO_RR) { 673afc154e9SHannes Frederic Sowa match_do_rr = true; 674afc154e9SHannes Frederic Sowa m = 0; /* lowest valid score */ 6757e980569SJiri Benc } else if (m == RT6_NUD_FAIL_HARD) { 676f11e6659SDavid S. Miller goto out; 6771da177e4SLinus Torvalds } 678f11e6659SDavid S. Miller 679afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) 680afc154e9SHannes Frederic Sowa rt6_probe(rt); 681afc154e9SHannes Frederic Sowa 6827e980569SJiri Benc /* note that m can be RT6_NUD_FAIL_PROBE at this point */ 683afc154e9SHannes Frederic Sowa if (m > *mpri) { 684afc154e9SHannes Frederic Sowa *do_rr = match_do_rr; 685afc154e9SHannes Frederic Sowa *mpri = m; 686afc154e9SHannes Frederic Sowa match = rt; 687afc154e9SHannes Frederic Sowa } 688f11e6659SDavid S. Miller out: 689f11e6659SDavid S. Miller return match; 6901da177e4SLinus Torvalds } 6911da177e4SLinus Torvalds 692f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn, 693f11e6659SDavid S. Miller struct rt6_info *rr_head, 694afc154e9SHannes Frederic Sowa u32 metric, int oif, int strict, 695afc154e9SHannes Frederic Sowa bool *do_rr) 696f11e6659SDavid S. Miller { 6979fbdcfafSSteffen Klassert struct rt6_info *rt, *match, *cont; 698f11e6659SDavid S. Miller int mpri = -1; 699f11e6659SDavid S. Miller 700f11e6659SDavid S. Miller match = NULL; 7019fbdcfafSSteffen Klassert cont = NULL; 7029fbdcfafSSteffen Klassert for (rt = rr_head; rt; rt = rt->dst.rt6_next) { 7039fbdcfafSSteffen Klassert if (rt->rt6i_metric != metric) { 7049fbdcfafSSteffen Klassert cont = rt; 7059fbdcfafSSteffen Klassert break; 7069fbdcfafSSteffen Klassert } 7079fbdcfafSSteffen Klassert 708afc154e9SHannes Frederic Sowa match = find_match(rt, oif, strict, &mpri, match, do_rr); 7099fbdcfafSSteffen Klassert } 7109fbdcfafSSteffen Klassert 7119fbdcfafSSteffen Klassert for (rt = fn->leaf; rt && rt != rr_head; rt = rt->dst.rt6_next) { 7129fbdcfafSSteffen Klassert if (rt->rt6i_metric != metric) { 7139fbdcfafSSteffen Klassert cont = rt; 7149fbdcfafSSteffen Klassert break; 7159fbdcfafSSteffen Klassert } 7169fbdcfafSSteffen Klassert 7179fbdcfafSSteffen Klassert match = find_match(rt, oif, strict, &mpri, match, do_rr); 7189fbdcfafSSteffen Klassert } 7199fbdcfafSSteffen Klassert 7209fbdcfafSSteffen Klassert if (match || !cont) 7219fbdcfafSSteffen Klassert return match; 7229fbdcfafSSteffen Klassert 7239fbdcfafSSteffen Klassert for (rt = cont; rt; rt = rt->dst.rt6_next) 724afc154e9SHannes Frederic Sowa match = find_match(rt, oif, strict, &mpri, match, do_rr); 725f11e6659SDavid S. Miller 726f11e6659SDavid S. Miller return match; 727f11e6659SDavid S. Miller } 728f11e6659SDavid S. Miller 729f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict) 730f11e6659SDavid S. Miller { 731f11e6659SDavid S. Miller struct rt6_info *match, *rt0; 7328ed67789SDaniel Lezcano struct net *net; 733afc154e9SHannes Frederic Sowa bool do_rr = false; 734f11e6659SDavid S. Miller 735f11e6659SDavid S. Miller rt0 = fn->rr_ptr; 736f11e6659SDavid S. Miller if (!rt0) 737f11e6659SDavid S. Miller fn->rr_ptr = rt0 = fn->leaf; 738f11e6659SDavid S. Miller 739afc154e9SHannes Frederic Sowa match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict, 740afc154e9SHannes Frederic Sowa &do_rr); 741f11e6659SDavid S. Miller 742afc154e9SHannes Frederic Sowa if (do_rr) { 743d8d1f30bSChangli Gao struct rt6_info *next = rt0->dst.rt6_next; 744f11e6659SDavid S. Miller 745554cfb7eSYOSHIFUJI Hideaki /* no entries matched; do round-robin */ 746f11e6659SDavid S. Miller if (!next || next->rt6i_metric != rt0->rt6i_metric) 747f11e6659SDavid S. Miller next = fn->leaf; 748f11e6659SDavid S. Miller 749f11e6659SDavid S. Miller if (next != rt0) 750f11e6659SDavid S. Miller fn->rr_ptr = next; 751554cfb7eSYOSHIFUJI Hideaki } 752554cfb7eSYOSHIFUJI Hideaki 753d1918542SDavid S. Miller net = dev_net(rt0->dst.dev); 754a02cec21SEric Dumazet return match ? match : net->ipv6.ip6_null_entry; 7551da177e4SLinus Torvalds } 7561da177e4SLinus Torvalds 7578b9df265SMartin KaFai Lau static bool rt6_is_gw_or_nonexthop(const struct rt6_info *rt) 7588b9df265SMartin KaFai Lau { 7598b9df265SMartin KaFai Lau return (rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY)); 7608b9df265SMartin KaFai Lau } 7618b9df265SMartin KaFai Lau 76270ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 76370ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 764b71d1d42SEric Dumazet const struct in6_addr *gwaddr) 76570ceb4f5SYOSHIFUJI Hideaki { 766c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 76770ceb4f5SYOSHIFUJI Hideaki struct route_info *rinfo = (struct route_info *) opt; 76870ceb4f5SYOSHIFUJI Hideaki struct in6_addr prefix_buf, *prefix; 76970ceb4f5SYOSHIFUJI Hideaki unsigned int pref; 7704bed72e4SYOSHIFUJI Hideaki unsigned long lifetime; 77170ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt; 77270ceb4f5SYOSHIFUJI Hideaki 77370ceb4f5SYOSHIFUJI Hideaki if (len < sizeof(struct route_info)) { 77470ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 77570ceb4f5SYOSHIFUJI Hideaki } 77670ceb4f5SYOSHIFUJI Hideaki 77770ceb4f5SYOSHIFUJI Hideaki /* Sanity check for prefix_len and length */ 77870ceb4f5SYOSHIFUJI Hideaki if (rinfo->length > 3) { 77970ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 78070ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 128) { 78170ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 78270ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 64) { 78370ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 2) { 78470ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 78570ceb4f5SYOSHIFUJI Hideaki } 78670ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 0) { 78770ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 1) { 78870ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 78970ceb4f5SYOSHIFUJI Hideaki } 79070ceb4f5SYOSHIFUJI Hideaki } 79170ceb4f5SYOSHIFUJI Hideaki 79270ceb4f5SYOSHIFUJI Hideaki pref = rinfo->route_pref; 79370ceb4f5SYOSHIFUJI Hideaki if (pref == ICMPV6_ROUTER_PREF_INVALID) 7943933fc95SJens Rosenboom return -EINVAL; 79570ceb4f5SYOSHIFUJI Hideaki 7964bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); 79770ceb4f5SYOSHIFUJI Hideaki 79870ceb4f5SYOSHIFUJI Hideaki if (rinfo->length == 3) 79970ceb4f5SYOSHIFUJI Hideaki prefix = (struct in6_addr *)rinfo->prefix; 80070ceb4f5SYOSHIFUJI Hideaki else { 80170ceb4f5SYOSHIFUJI Hideaki /* this function is safe */ 80270ceb4f5SYOSHIFUJI Hideaki ipv6_addr_prefix(&prefix_buf, 80370ceb4f5SYOSHIFUJI Hideaki (struct in6_addr *)rinfo->prefix, 80470ceb4f5SYOSHIFUJI Hideaki rinfo->prefix_len); 80570ceb4f5SYOSHIFUJI Hideaki prefix = &prefix_buf; 80670ceb4f5SYOSHIFUJI Hideaki } 80770ceb4f5SYOSHIFUJI Hideaki 808f104a567SDuan Jiong if (rinfo->prefix_len == 0) 809f104a567SDuan Jiong rt = rt6_get_dflt_router(gwaddr, dev); 810f104a567SDuan Jiong else 811f104a567SDuan Jiong rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, 812f104a567SDuan Jiong gwaddr, dev->ifindex); 81370ceb4f5SYOSHIFUJI Hideaki 81470ceb4f5SYOSHIFUJI Hideaki if (rt && !lifetime) { 815e0a1ad73SThomas Graf ip6_del_rt(rt); 81670ceb4f5SYOSHIFUJI Hideaki rt = NULL; 81770ceb4f5SYOSHIFUJI Hideaki } 81870ceb4f5SYOSHIFUJI Hideaki 81970ceb4f5SYOSHIFUJI Hideaki if (!rt && lifetime) 820efa2cea0SDaniel Lezcano rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex, 82170ceb4f5SYOSHIFUJI Hideaki pref); 82270ceb4f5SYOSHIFUJI Hideaki else if (rt) 82370ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags = RTF_ROUTEINFO | 82470ceb4f5SYOSHIFUJI Hideaki (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 82570ceb4f5SYOSHIFUJI Hideaki 82670ceb4f5SYOSHIFUJI Hideaki if (rt) { 8271716a961SGao feng if (!addrconf_finite_timeout(lifetime)) 8281716a961SGao feng rt6_clean_expires(rt); 8291716a961SGao feng else 8301716a961SGao feng rt6_set_expires(rt, jiffies + HZ * lifetime); 8311716a961SGao feng 83294e187c0SAmerigo Wang ip6_rt_put(rt); 83370ceb4f5SYOSHIFUJI Hideaki } 83470ceb4f5SYOSHIFUJI Hideaki return 0; 83570ceb4f5SYOSHIFUJI Hideaki } 83670ceb4f5SYOSHIFUJI Hideaki #endif 83770ceb4f5SYOSHIFUJI Hideaki 838a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn, 839a3c00e46SMartin KaFai Lau struct in6_addr *saddr) 840a3c00e46SMartin KaFai Lau { 841a3c00e46SMartin KaFai Lau struct fib6_node *pn; 842a3c00e46SMartin KaFai Lau while (1) { 843a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_TL_ROOT) 844a3c00e46SMartin KaFai Lau return NULL; 845a3c00e46SMartin KaFai Lau pn = fn->parent; 846a3c00e46SMartin KaFai Lau if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) 847a3c00e46SMartin KaFai Lau fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); 848a3c00e46SMartin KaFai Lau else 849a3c00e46SMartin KaFai Lau fn = pn; 850a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_RTINFO) 851a3c00e46SMartin KaFai Lau return fn; 852a3c00e46SMartin KaFai Lau } 853a3c00e46SMartin KaFai Lau } 854c71099acSThomas Graf 8558ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net, 8568ed67789SDaniel Lezcano struct fib6_table *table, 8574c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 8581da177e4SLinus Torvalds { 8591da177e4SLinus Torvalds struct fib6_node *fn; 8601da177e4SLinus Torvalds struct rt6_info *rt; 8611da177e4SLinus Torvalds 862c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 8634c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 864c71099acSThomas Graf restart: 865c71099acSThomas Graf rt = fn->leaf; 8664c9483b2SDavid S. Miller rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags); 86751ebd318SNicolas Dichtel if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0) 86852bd4c0cSNicolas Dichtel rt = rt6_multipath_select(rt, fl6, fl6->flowi6_oif, flags); 869a3c00e46SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 870a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 871a3c00e46SMartin KaFai Lau if (fn) 872a3c00e46SMartin KaFai Lau goto restart; 873a3c00e46SMartin KaFai Lau } 874d8d1f30bSChangli Gao dst_use(&rt->dst, jiffies); 875c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 8761da177e4SLinus Torvalds return rt; 877c71099acSThomas Graf 878c71099acSThomas Graf } 879c71099acSThomas Graf 880ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6, 881ea6e574eSFlorian Westphal int flags) 882ea6e574eSFlorian Westphal { 883ea6e574eSFlorian Westphal return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup); 884ea6e574eSFlorian Westphal } 885ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup); 886ea6e574eSFlorian Westphal 8879acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, 8889acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *saddr, int oif, int strict) 889c71099acSThomas Graf { 8904c9483b2SDavid S. Miller struct flowi6 fl6 = { 8914c9483b2SDavid S. Miller .flowi6_oif = oif, 8924c9483b2SDavid S. Miller .daddr = *daddr, 893c71099acSThomas Graf }; 894c71099acSThomas Graf struct dst_entry *dst; 89577d16f45SYOSHIFUJI Hideaki int flags = strict ? RT6_LOOKUP_F_IFACE : 0; 896c71099acSThomas Graf 897adaa70bbSThomas Graf if (saddr) { 8984c9483b2SDavid S. Miller memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 899adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 900adaa70bbSThomas Graf } 901adaa70bbSThomas Graf 9024c9483b2SDavid S. Miller dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup); 903c71099acSThomas Graf if (dst->error == 0) 904c71099acSThomas Graf return (struct rt6_info *) dst; 905c71099acSThomas Graf 906c71099acSThomas Graf dst_release(dst); 907c71099acSThomas Graf 9081da177e4SLinus Torvalds return NULL; 9091da177e4SLinus Torvalds } 9107159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup); 9117159039aSYOSHIFUJI Hideaki 912c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock. 9131da177e4SLinus Torvalds It takes new route entry, the addition fails by any reason the 9141da177e4SLinus Torvalds route is freed. In any case, if caller does not hold it, it may 9151da177e4SLinus Torvalds be destroyed. 9161da177e4SLinus Torvalds */ 9171da177e4SLinus Torvalds 918e5fd387aSMichal Kubeček static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info, 919e715b6d3SFlorian Westphal struct mx6_config *mxc) 9201da177e4SLinus Torvalds { 9211da177e4SLinus Torvalds int err; 922c71099acSThomas Graf struct fib6_table *table; 9231da177e4SLinus Torvalds 924c71099acSThomas Graf table = rt->rt6i_table; 925c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 926e715b6d3SFlorian Westphal err = fib6_add(&table->tb6_root, rt, info, mxc); 927c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 9281da177e4SLinus Torvalds 9291da177e4SLinus Torvalds return err; 9301da177e4SLinus Torvalds } 9311da177e4SLinus Torvalds 93240e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt) 93340e22e8fSThomas Graf { 934e715b6d3SFlorian Westphal struct nl_info info = { .nl_net = dev_net(rt->dst.dev), }; 935e715b6d3SFlorian Westphal struct mx6_config mxc = { .mx = NULL, }; 936e715b6d3SFlorian Westphal 937e715b6d3SFlorian Westphal return __ip6_ins_rt(rt, &info, &mxc); 93840e22e8fSThomas Graf } 93940e22e8fSThomas Graf 9408b9df265SMartin KaFai Lau static struct rt6_info *ip6_rt_cache_alloc(struct rt6_info *ort, 94121efcfa0SEric Dumazet const struct in6_addr *daddr, 942b71d1d42SEric Dumazet const struct in6_addr *saddr) 9431da177e4SLinus Torvalds { 9441da177e4SLinus Torvalds struct rt6_info *rt; 9451da177e4SLinus Torvalds 9461da177e4SLinus Torvalds /* 9471da177e4SLinus Torvalds * Clone the route. 9481da177e4SLinus Torvalds */ 9491da177e4SLinus Torvalds 950d52d3997SMartin KaFai Lau if (ort->rt6i_flags & (RTF_CACHE | RTF_PCPU)) 95183a09abdSMartin KaFai Lau ort = (struct rt6_info *)ort->dst.from; 9521da177e4SLinus Torvalds 953d52d3997SMartin KaFai Lau rt = __ip6_dst_alloc(dev_net(ort->dst.dev), ort->dst.dev, 95483a09abdSMartin KaFai Lau 0, ort->rt6i_table); 95583a09abdSMartin KaFai Lau 95683a09abdSMartin KaFai Lau if (!rt) 95783a09abdSMartin KaFai Lau return NULL; 95883a09abdSMartin KaFai Lau 95983a09abdSMartin KaFai Lau ip6_rt_copy_init(rt, ort); 9608b9df265SMartin KaFai Lau rt->rt6i_flags |= RTF_CACHE; 96183a09abdSMartin KaFai Lau rt->rt6i_metric = 0; 96283a09abdSMartin KaFai Lau rt->dst.flags |= DST_HOST; 96383a09abdSMartin KaFai Lau rt->rt6i_dst.addr = *daddr; 96483a09abdSMartin KaFai Lau rt->rt6i_dst.plen = 128; 9658b9df265SMartin KaFai Lau 9668b9df265SMartin KaFai Lau if (!rt6_is_gw_or_nonexthop(ort)) { 967bb3c3686SDavid S. Miller if (ort->rt6i_dst.plen != 128 && 96821efcfa0SEric Dumazet ipv6_addr_equal(&ort->rt6i_dst.addr, daddr)) 96958c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 9701da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 9711da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 9724e3fd7a0SAlexey Dobriyan rt->rt6i_src.addr = *saddr; 9731da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 9741da177e4SLinus Torvalds } 9751da177e4SLinus Torvalds #endif 97695a9a5baSYOSHIFUJI Hideaki } 97795a9a5baSYOSHIFUJI Hideaki 978299d9939SYOSHIFUJI Hideaki return rt; 979299d9939SYOSHIFUJI Hideaki } 980299d9939SYOSHIFUJI Hideaki 981d52d3997SMartin KaFai Lau static struct rt6_info *ip6_rt_pcpu_alloc(struct rt6_info *rt) 982d52d3997SMartin KaFai Lau { 983d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 984d52d3997SMartin KaFai Lau 985d52d3997SMartin KaFai Lau pcpu_rt = __ip6_dst_alloc(dev_net(rt->dst.dev), 986d52d3997SMartin KaFai Lau rt->dst.dev, rt->dst.flags, 987d52d3997SMartin KaFai Lau rt->rt6i_table); 988d52d3997SMartin KaFai Lau 989d52d3997SMartin KaFai Lau if (!pcpu_rt) 990d52d3997SMartin KaFai Lau return NULL; 991d52d3997SMartin KaFai Lau ip6_rt_copy_init(pcpu_rt, rt); 992d52d3997SMartin KaFai Lau pcpu_rt->rt6i_protocol = rt->rt6i_protocol; 993d52d3997SMartin KaFai Lau pcpu_rt->rt6i_flags |= RTF_PCPU; 994d52d3997SMartin KaFai Lau return pcpu_rt; 995d52d3997SMartin KaFai Lau } 996d52d3997SMartin KaFai Lau 997d52d3997SMartin KaFai Lau /* It should be called with read_lock_bh(&tb6_lock) acquired */ 998d52d3997SMartin KaFai Lau static struct rt6_info *rt6_get_pcpu_route(struct rt6_info *rt) 999d52d3997SMartin KaFai Lau { 1000d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt, *prev, **p; 1001d52d3997SMartin KaFai Lau 1002d52d3997SMartin KaFai Lau p = this_cpu_ptr(rt->rt6i_pcpu); 1003d52d3997SMartin KaFai Lau pcpu_rt = *p; 1004d52d3997SMartin KaFai Lau 1005d52d3997SMartin KaFai Lau if (pcpu_rt) 1006d52d3997SMartin KaFai Lau goto done; 1007d52d3997SMartin KaFai Lau 1008d52d3997SMartin KaFai Lau pcpu_rt = ip6_rt_pcpu_alloc(rt); 1009d52d3997SMartin KaFai Lau if (!pcpu_rt) { 1010d52d3997SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 1011d52d3997SMartin KaFai Lau 1012d52d3997SMartin KaFai Lau pcpu_rt = net->ipv6.ip6_null_entry; 1013d52d3997SMartin KaFai Lau goto done; 1014d52d3997SMartin KaFai Lau } 1015d52d3997SMartin KaFai Lau 1016d52d3997SMartin KaFai Lau prev = cmpxchg(p, NULL, pcpu_rt); 1017d52d3997SMartin KaFai Lau if (prev) { 1018d52d3997SMartin KaFai Lau /* If someone did it before us, return prev instead */ 1019d52d3997SMartin KaFai Lau dst_destroy(&pcpu_rt->dst); 1020d52d3997SMartin KaFai Lau pcpu_rt = prev; 1021d52d3997SMartin KaFai Lau } 1022d52d3997SMartin KaFai Lau 1023d52d3997SMartin KaFai Lau done: 1024d52d3997SMartin KaFai Lau dst_hold(&pcpu_rt->dst); 1025d52d3997SMartin KaFai Lau rt6_dst_from_metrics_check(pcpu_rt); 1026d52d3997SMartin KaFai Lau return pcpu_rt; 1027d52d3997SMartin KaFai Lau } 1028d52d3997SMartin KaFai Lau 10298ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif, 10304c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 10311da177e4SLinus Torvalds { 1032367efcb9SMartin KaFai Lau struct fib6_node *fn, *saved_fn; 103345e4fd26SMartin KaFai Lau struct rt6_info *rt; 1034c71099acSThomas Graf int strict = 0; 10351da177e4SLinus Torvalds 103677d16f45SYOSHIFUJI Hideaki strict |= flags & RT6_LOOKUP_F_IFACE; 1037367efcb9SMartin KaFai Lau if (net->ipv6.devconf_all->forwarding == 0) 1038367efcb9SMartin KaFai Lau strict |= RT6_LOOKUP_F_REACHABLE; 10391da177e4SLinus Torvalds 1040c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 10411da177e4SLinus Torvalds 10424c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1043367efcb9SMartin KaFai Lau saved_fn = fn; 10441da177e4SLinus Torvalds 1045a3c00e46SMartin KaFai Lau redo_rt6_select: 1046367efcb9SMartin KaFai Lau rt = rt6_select(fn, oif, strict); 104752bd4c0cSNicolas Dichtel if (rt->rt6i_nsiblings) 1048367efcb9SMartin KaFai Lau rt = rt6_multipath_select(rt, fl6, oif, strict); 1049a3c00e46SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 1050a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1051a3c00e46SMartin KaFai Lau if (fn) 1052a3c00e46SMartin KaFai Lau goto redo_rt6_select; 1053367efcb9SMartin KaFai Lau else if (strict & RT6_LOOKUP_F_REACHABLE) { 1054367efcb9SMartin KaFai Lau /* also consider unreachable route */ 1055367efcb9SMartin KaFai Lau strict &= ~RT6_LOOKUP_F_REACHABLE; 1056367efcb9SMartin KaFai Lau fn = saved_fn; 1057367efcb9SMartin KaFai Lau goto redo_rt6_select; 1058367efcb9SMartin KaFai Lau } 1059a3c00e46SMartin KaFai Lau } 1060a3c00e46SMartin KaFai Lau 1061d52d3997SMartin KaFai Lau 1062d52d3997SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry || (rt->rt6i_flags & RTF_CACHE)) { 10633da59bd9SMartin KaFai Lau dst_use(&rt->dst, jiffies); 1064c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 10651da177e4SLinus Torvalds 1066d52d3997SMartin KaFai Lau rt6_dst_from_metrics_check(rt); 1067d52d3997SMartin KaFai Lau return rt; 10683da59bd9SMartin KaFai Lau } else if (unlikely((fl6->flowi6_flags & FLOWI_FLAG_KNOWN_NH) && 10693da59bd9SMartin KaFai Lau !(rt->rt6i_flags & RTF_GATEWAY))) { 10703da59bd9SMartin KaFai Lau /* Create a RTF_CACHE clone which will not be 10713da59bd9SMartin KaFai Lau * owned by the fib6 tree. It is for the special case where 10723da59bd9SMartin KaFai Lau * the daddr in the skb during the neighbor look-up is different 10733da59bd9SMartin KaFai Lau * from the fl6->daddr used to look-up route here. 10743da59bd9SMartin KaFai Lau */ 1075c71099acSThomas Graf 10763da59bd9SMartin KaFai Lau struct rt6_info *uncached_rt; 10773da59bd9SMartin KaFai Lau 1078d52d3997SMartin KaFai Lau dst_use(&rt->dst, jiffies); 1079d52d3997SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 1080d52d3997SMartin KaFai Lau 10813da59bd9SMartin KaFai Lau uncached_rt = ip6_rt_cache_alloc(rt, &fl6->daddr, NULL); 10823da59bd9SMartin KaFai Lau dst_release(&rt->dst); 10833da59bd9SMartin KaFai Lau 10843da59bd9SMartin KaFai Lau if (uncached_rt) 10858d0b94afSMartin KaFai Lau rt6_uncached_list_add(uncached_rt); 10863da59bd9SMartin KaFai Lau else 10873da59bd9SMartin KaFai Lau uncached_rt = net->ipv6.ip6_null_entry; 1088d52d3997SMartin KaFai Lau 10893da59bd9SMartin KaFai Lau dst_hold(&uncached_rt->dst); 10903da59bd9SMartin KaFai Lau return uncached_rt; 10913da59bd9SMartin KaFai Lau 1092d52d3997SMartin KaFai Lau } else { 1093d52d3997SMartin KaFai Lau /* Get a percpu copy */ 1094d52d3997SMartin KaFai Lau 1095d52d3997SMartin KaFai Lau struct rt6_info *pcpu_rt; 1096d52d3997SMartin KaFai Lau 1097d52d3997SMartin KaFai Lau rt->dst.lastuse = jiffies; 1098d52d3997SMartin KaFai Lau rt->dst.__use++; 1099d52d3997SMartin KaFai Lau pcpu_rt = rt6_get_pcpu_route(rt); 1100d52d3997SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 1101d52d3997SMartin KaFai Lau 1102d52d3997SMartin KaFai Lau return pcpu_rt; 1103d52d3997SMartin KaFai Lau } 1104c71099acSThomas Graf } 1105c71099acSThomas Graf 11068ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table, 11074c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 11084acad72dSPavel Emelyanov { 11094c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags); 11104acad72dSPavel Emelyanov } 11114acad72dSPavel Emelyanov 111272331bc0SShmulik Ladkani static struct dst_entry *ip6_route_input_lookup(struct net *net, 111372331bc0SShmulik Ladkani struct net_device *dev, 111472331bc0SShmulik Ladkani struct flowi6 *fl6, int flags) 111572331bc0SShmulik Ladkani { 111672331bc0SShmulik Ladkani if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) 111772331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_IFACE; 111872331bc0SShmulik Ladkani 111972331bc0SShmulik Ladkani return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input); 112072331bc0SShmulik Ladkani } 112172331bc0SShmulik Ladkani 1122c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 1123c71099acSThomas Graf { 1124b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 1125c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 1126adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 11274c9483b2SDavid S. Miller struct flowi6 fl6 = { 11284c9483b2SDavid S. Miller .flowi6_iif = skb->dev->ifindex, 11294c9483b2SDavid S. Miller .daddr = iph->daddr, 11304c9483b2SDavid S. Miller .saddr = iph->saddr, 11316502ca52SYOSHIFUJI Hideaki / 吉藤英明 .flowlabel = ip6_flowinfo(iph), 11324c9483b2SDavid S. Miller .flowi6_mark = skb->mark, 11334c9483b2SDavid S. Miller .flowi6_proto = iph->nexthdr, 1134c71099acSThomas Graf }; 1135adaa70bbSThomas Graf 113672331bc0SShmulik Ladkani skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags)); 1137c71099acSThomas Graf } 1138c71099acSThomas Graf 11398ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table, 11404c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 1141c71099acSThomas Graf { 11424c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags); 1143c71099acSThomas Graf } 1144c71099acSThomas Graf 11459c7a4f9cSFlorian Westphal struct dst_entry *ip6_route_output(struct net *net, const struct sock *sk, 11464c9483b2SDavid S. Miller struct flowi6 *fl6) 1147c71099acSThomas Graf { 1148c71099acSThomas Graf int flags = 0; 1149c71099acSThomas Graf 11501fb9489bSPavel Emelyanov fl6->flowi6_iif = LOOPBACK_IFINDEX; 11514dc27d1cSDavid McCullough 11524c9483b2SDavid S. Miller if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr)) 115377d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 1154c71099acSThomas Graf 11554c9483b2SDavid S. Miller if (!ipv6_addr_any(&fl6->saddr)) 1156adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 11570c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 11580c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 1159adaa70bbSThomas Graf 11604c9483b2SDavid S. Miller return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output); 11611da177e4SLinus Torvalds } 11627159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output); 11631da177e4SLinus Torvalds 11642774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 116514e50e57SDavid S. Miller { 11665c1e6aa3SDavid S. Miller struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; 116714e50e57SDavid S. Miller struct dst_entry *new = NULL; 116814e50e57SDavid S. Miller 1169f5b0a874SDavid S. Miller rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, DST_OBSOLETE_NONE, 0); 117014e50e57SDavid S. Miller if (rt) { 1171d8d1f30bSChangli Gao new = &rt->dst; 117214e50e57SDavid S. Miller 11738104891bSSteffen Klassert memset(new + 1, 0, sizeof(*rt) - sizeof(*new)); 11748104891bSSteffen Klassert 117514e50e57SDavid S. Miller new->__use = 1; 1176352e512cSHerbert Xu new->input = dst_discard; 1177aad88724SEric Dumazet new->output = dst_discard_sk; 117814e50e57SDavid S. Miller 117921efcfa0SEric Dumazet if (dst_metrics_read_only(&ort->dst)) 118021efcfa0SEric Dumazet new->_metrics = ort->dst._metrics; 118121efcfa0SEric Dumazet else 1182defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 118314e50e57SDavid S. Miller rt->rt6i_idev = ort->rt6i_idev; 118414e50e57SDavid S. Miller if (rt->rt6i_idev) 118514e50e57SDavid S. Miller in6_dev_hold(rt->rt6i_idev); 118614e50e57SDavid S. Miller 11874e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 11881716a961SGao feng rt->rt6i_flags = ort->rt6i_flags; 118914e50e57SDavid S. Miller rt->rt6i_metric = 0; 119014e50e57SDavid S. Miller 119114e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 119214e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 119314e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 119414e50e57SDavid S. Miller #endif 119514e50e57SDavid S. Miller 119614e50e57SDavid S. Miller dst_free(new); 119714e50e57SDavid S. Miller } 119814e50e57SDavid S. Miller 119969ead7afSDavid S. Miller dst_release(dst_orig); 120069ead7afSDavid S. Miller return new ? new : ERR_PTR(-ENOMEM); 120114e50e57SDavid S. Miller } 120214e50e57SDavid S. Miller 12031da177e4SLinus Torvalds /* 12041da177e4SLinus Torvalds * Destination cache support functions 12051da177e4SLinus Torvalds */ 12061da177e4SLinus Torvalds 12074b32b5adSMartin KaFai Lau static void rt6_dst_from_metrics_check(struct rt6_info *rt) 12084b32b5adSMartin KaFai Lau { 12094b32b5adSMartin KaFai Lau if (rt->dst.from && 12104b32b5adSMartin KaFai Lau dst_metrics_ptr(&rt->dst) != dst_metrics_ptr(rt->dst.from)) 12114b32b5adSMartin KaFai Lau dst_init_metrics(&rt->dst, dst_metrics_ptr(rt->dst.from), true); 12124b32b5adSMartin KaFai Lau } 12134b32b5adSMartin KaFai Lau 12143da59bd9SMartin KaFai Lau static struct dst_entry *rt6_check(struct rt6_info *rt, u32 cookie) 12153da59bd9SMartin KaFai Lau { 12163da59bd9SMartin KaFai Lau if (!rt->rt6i_node || (rt->rt6i_node->fn_sernum != cookie)) 12173da59bd9SMartin KaFai Lau return NULL; 12183da59bd9SMartin KaFai Lau 12193da59bd9SMartin KaFai Lau if (rt6_check_expired(rt)) 12203da59bd9SMartin KaFai Lau return NULL; 12213da59bd9SMartin KaFai Lau 12223da59bd9SMartin KaFai Lau return &rt->dst; 12233da59bd9SMartin KaFai Lau } 12243da59bd9SMartin KaFai Lau 12253da59bd9SMartin KaFai Lau static struct dst_entry *rt6_dst_from_check(struct rt6_info *rt, u32 cookie) 12263da59bd9SMartin KaFai Lau { 12273da59bd9SMartin KaFai Lau if (rt->dst.obsolete == DST_OBSOLETE_FORCE_CHK && 12283da59bd9SMartin KaFai Lau rt6_check((struct rt6_info *)(rt->dst.from), cookie)) 12293da59bd9SMartin KaFai Lau return &rt->dst; 12303da59bd9SMartin KaFai Lau else 12313da59bd9SMartin KaFai Lau return NULL; 12323da59bd9SMartin KaFai Lau } 12333da59bd9SMartin KaFai Lau 12341da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 12351da177e4SLinus Torvalds { 12361da177e4SLinus Torvalds struct rt6_info *rt; 12371da177e4SLinus Torvalds 12381da177e4SLinus Torvalds rt = (struct rt6_info *) dst; 12391da177e4SLinus Torvalds 12406f3118b5SNicolas Dichtel /* All IPV6 dsts are created with ->obsolete set to the value 12416f3118b5SNicolas Dichtel * DST_OBSOLETE_FORCE_CHK which forces validation calls down 12426f3118b5SNicolas Dichtel * into this function always. 12436f3118b5SNicolas Dichtel */ 1244e3bc10bdSHannes Frederic Sowa 12454b32b5adSMartin KaFai Lau rt6_dst_from_metrics_check(rt); 12464b32b5adSMartin KaFai Lau 1247d52d3997SMartin KaFai Lau if ((rt->rt6i_flags & RTF_PCPU) || unlikely(dst->flags & DST_NOCACHE)) 12483da59bd9SMartin KaFai Lau return rt6_dst_from_check(rt, cookie); 12493da59bd9SMartin KaFai Lau else 12503da59bd9SMartin KaFai Lau return rt6_check(rt, cookie); 12511da177e4SLinus Torvalds } 12521da177e4SLinus Torvalds 12531da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 12541da177e4SLinus Torvalds { 12551da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 12561da177e4SLinus Torvalds 12571da177e4SLinus Torvalds if (rt) { 125854c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 125954c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 1260e0a1ad73SThomas Graf ip6_del_rt(rt); 126154c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 12621da177e4SLinus Torvalds } 126354c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 126454c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 126554c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 126654c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 126754c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 126854c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 12691da177e4SLinus Torvalds } 12701da177e4SLinus Torvalds 12711da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 12721da177e4SLinus Torvalds { 12731da177e4SLinus Torvalds struct rt6_info *rt; 12741da177e4SLinus Torvalds 12753ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 12761da177e4SLinus Torvalds 1277adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 12781da177e4SLinus Torvalds if (rt) { 12791eb4f758SHannes Frederic Sowa if (rt->rt6i_flags & RTF_CACHE) { 12801eb4f758SHannes Frederic Sowa dst_hold(&rt->dst); 12811eb4f758SHannes Frederic Sowa if (ip6_del_rt(rt)) 12821eb4f758SHannes Frederic Sowa dst_free(&rt->dst); 12831eb4f758SHannes Frederic Sowa } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) { 12841da177e4SLinus Torvalds rt->rt6i_node->fn_sernum = -1; 12851da177e4SLinus Torvalds } 12861da177e4SLinus Torvalds } 12871eb4f758SHannes Frederic Sowa } 12881da177e4SLinus Torvalds 128945e4fd26SMartin KaFai Lau static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu) 129045e4fd26SMartin KaFai Lau { 129145e4fd26SMartin KaFai Lau struct net *net = dev_net(rt->dst.dev); 129245e4fd26SMartin KaFai Lau 129345e4fd26SMartin KaFai Lau rt->rt6i_flags |= RTF_MODIFIED; 129445e4fd26SMartin KaFai Lau rt->rt6i_pmtu = mtu; 129545e4fd26SMartin KaFai Lau rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires); 129645e4fd26SMartin KaFai Lau } 129745e4fd26SMartin KaFai Lau 129845e4fd26SMartin KaFai Lau static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk, 129945e4fd26SMartin KaFai Lau const struct ipv6hdr *iph, u32 mtu) 13001da177e4SLinus Torvalds { 13011da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info *)dst; 13021da177e4SLinus Torvalds 130345e4fd26SMartin KaFai Lau if (rt6->rt6i_flags & RTF_LOCAL) 130445e4fd26SMartin KaFai Lau return; 130545e4fd26SMartin KaFai Lau 130681aded24SDavid S. Miller dst_confirm(dst); 130745e4fd26SMartin KaFai Lau mtu = max_t(u32, mtu, IPV6_MIN_MTU); 130845e4fd26SMartin KaFai Lau if (mtu >= dst_mtu(dst)) 130945e4fd26SMartin KaFai Lau return; 131081aded24SDavid S. Miller 131145e4fd26SMartin KaFai Lau if (rt6->rt6i_flags & RTF_CACHE) { 131245e4fd26SMartin KaFai Lau rt6_do_update_pmtu(rt6, mtu); 131345e4fd26SMartin KaFai Lau } else { 131445e4fd26SMartin KaFai Lau const struct in6_addr *daddr, *saddr; 131545e4fd26SMartin KaFai Lau struct rt6_info *nrt6; 13169d289715SHagen Paul Pfeifer 131745e4fd26SMartin KaFai Lau if (iph) { 131845e4fd26SMartin KaFai Lau daddr = &iph->daddr; 131945e4fd26SMartin KaFai Lau saddr = &iph->saddr; 132045e4fd26SMartin KaFai Lau } else if (sk) { 132145e4fd26SMartin KaFai Lau daddr = &sk->sk_v6_daddr; 132245e4fd26SMartin KaFai Lau saddr = &inet6_sk(sk)->saddr; 132345e4fd26SMartin KaFai Lau } else { 132445e4fd26SMartin KaFai Lau return; 13251da177e4SLinus Torvalds } 132645e4fd26SMartin KaFai Lau nrt6 = ip6_rt_cache_alloc(rt6, daddr, saddr); 132745e4fd26SMartin KaFai Lau if (nrt6) { 132845e4fd26SMartin KaFai Lau rt6_do_update_pmtu(nrt6, mtu); 132945e4fd26SMartin KaFai Lau 133045e4fd26SMartin KaFai Lau /* ip6_ins_rt(nrt6) will bump the 133145e4fd26SMartin KaFai Lau * rt6->rt6i_node->fn_sernum 133245e4fd26SMartin KaFai Lau * which will fail the next rt6_check() and 133345e4fd26SMartin KaFai Lau * invalidate the sk->sk_dst_cache. 133445e4fd26SMartin KaFai Lau */ 133545e4fd26SMartin KaFai Lau ip6_ins_rt(nrt6); 133645e4fd26SMartin KaFai Lau } 133745e4fd26SMartin KaFai Lau } 133845e4fd26SMartin KaFai Lau } 133945e4fd26SMartin KaFai Lau 134045e4fd26SMartin KaFai Lau static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 134145e4fd26SMartin KaFai Lau struct sk_buff *skb, u32 mtu) 134245e4fd26SMartin KaFai Lau { 134345e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, sk, skb ? ipv6_hdr(skb) : NULL, mtu); 13441da177e4SLinus Torvalds } 13451da177e4SLinus Torvalds 134642ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, 134742ae66c8SDavid S. Miller int oif, u32 mark) 134881aded24SDavid S. Miller { 134981aded24SDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 135081aded24SDavid S. Miller struct dst_entry *dst; 135181aded24SDavid S. Miller struct flowi6 fl6; 135281aded24SDavid S. Miller 135381aded24SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 135481aded24SDavid S. Miller fl6.flowi6_oif = oif; 13551b3c61dcSLorenzo Colitti fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark); 135681aded24SDavid S. Miller fl6.daddr = iph->daddr; 135781aded24SDavid S. Miller fl6.saddr = iph->saddr; 13586502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 135981aded24SDavid S. Miller 136081aded24SDavid S. Miller dst = ip6_route_output(net, NULL, &fl6); 136181aded24SDavid S. Miller if (!dst->error) 136245e4fd26SMartin KaFai Lau __ip6_rt_update_pmtu(dst, NULL, iph, ntohl(mtu)); 136381aded24SDavid S. Miller dst_release(dst); 136481aded24SDavid S. Miller } 136581aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu); 136681aded24SDavid S. Miller 136781aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) 136881aded24SDavid S. Miller { 136981aded24SDavid S. Miller ip6_update_pmtu(skb, sock_net(sk), mtu, 137081aded24SDavid S. Miller sk->sk_bound_dev_if, sk->sk_mark); 137181aded24SDavid S. Miller } 137281aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); 137381aded24SDavid S. Miller 1374b55b76b2SDuan Jiong /* Handle redirects */ 1375b55b76b2SDuan Jiong struct ip6rd_flowi { 1376b55b76b2SDuan Jiong struct flowi6 fl6; 1377b55b76b2SDuan Jiong struct in6_addr gateway; 1378b55b76b2SDuan Jiong }; 1379b55b76b2SDuan Jiong 1380b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net, 1381b55b76b2SDuan Jiong struct fib6_table *table, 1382b55b76b2SDuan Jiong struct flowi6 *fl6, 1383b55b76b2SDuan Jiong int flags) 1384b55b76b2SDuan Jiong { 1385b55b76b2SDuan Jiong struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; 1386b55b76b2SDuan Jiong struct rt6_info *rt; 1387b55b76b2SDuan Jiong struct fib6_node *fn; 1388b55b76b2SDuan Jiong 1389b55b76b2SDuan Jiong /* Get the "current" route for this destination and 1390b55b76b2SDuan Jiong * check if the redirect has come from approriate router. 1391b55b76b2SDuan Jiong * 1392b55b76b2SDuan Jiong * RFC 4861 specifies that redirects should only be 1393b55b76b2SDuan Jiong * accepted if they come from the nexthop to the target. 1394b55b76b2SDuan Jiong * Due to the way the routes are chosen, this notion 1395b55b76b2SDuan Jiong * is a bit fuzzy and one might need to check all possible 1396b55b76b2SDuan Jiong * routes. 1397b55b76b2SDuan Jiong */ 1398b55b76b2SDuan Jiong 1399b55b76b2SDuan Jiong read_lock_bh(&table->tb6_lock); 1400b55b76b2SDuan Jiong fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1401b55b76b2SDuan Jiong restart: 1402b55b76b2SDuan Jiong for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 1403b55b76b2SDuan Jiong if (rt6_check_expired(rt)) 1404b55b76b2SDuan Jiong continue; 1405b55b76b2SDuan Jiong if (rt->dst.error) 1406b55b76b2SDuan Jiong break; 1407b55b76b2SDuan Jiong if (!(rt->rt6i_flags & RTF_GATEWAY)) 1408b55b76b2SDuan Jiong continue; 1409b55b76b2SDuan Jiong if (fl6->flowi6_oif != rt->dst.dev->ifindex) 1410b55b76b2SDuan Jiong continue; 1411b55b76b2SDuan Jiong if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway)) 1412b55b76b2SDuan Jiong continue; 1413b55b76b2SDuan Jiong break; 1414b55b76b2SDuan Jiong } 1415b55b76b2SDuan Jiong 1416b55b76b2SDuan Jiong if (!rt) 1417b55b76b2SDuan Jiong rt = net->ipv6.ip6_null_entry; 1418b55b76b2SDuan Jiong else if (rt->dst.error) { 1419b55b76b2SDuan Jiong rt = net->ipv6.ip6_null_entry; 1420b0a1ba59SMartin KaFai Lau goto out; 1421b0a1ba59SMartin KaFai Lau } 1422b0a1ba59SMartin KaFai Lau 1423b0a1ba59SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 1424a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1425a3c00e46SMartin KaFai Lau if (fn) 1426a3c00e46SMartin KaFai Lau goto restart; 1427b55b76b2SDuan Jiong } 1428a3c00e46SMartin KaFai Lau 1429b0a1ba59SMartin KaFai Lau out: 1430b55b76b2SDuan Jiong dst_hold(&rt->dst); 1431b55b76b2SDuan Jiong 1432b55b76b2SDuan Jiong read_unlock_bh(&table->tb6_lock); 1433b55b76b2SDuan Jiong 1434b55b76b2SDuan Jiong return rt; 1435b55b76b2SDuan Jiong }; 1436b55b76b2SDuan Jiong 1437b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net, 1438b55b76b2SDuan Jiong const struct flowi6 *fl6, 1439b55b76b2SDuan Jiong const struct in6_addr *gateway) 1440b55b76b2SDuan Jiong { 1441b55b76b2SDuan Jiong int flags = RT6_LOOKUP_F_HAS_SADDR; 1442b55b76b2SDuan Jiong struct ip6rd_flowi rdfl; 1443b55b76b2SDuan Jiong 1444b55b76b2SDuan Jiong rdfl.fl6 = *fl6; 1445b55b76b2SDuan Jiong rdfl.gateway = *gateway; 1446b55b76b2SDuan Jiong 1447b55b76b2SDuan Jiong return fib6_rule_lookup(net, &rdfl.fl6, 1448b55b76b2SDuan Jiong flags, __ip6_route_redirect); 1449b55b76b2SDuan Jiong } 1450b55b76b2SDuan Jiong 14513a5ad2eeSDavid S. Miller void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark) 14523a5ad2eeSDavid S. Miller { 14533a5ad2eeSDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 14543a5ad2eeSDavid S. Miller struct dst_entry *dst; 14553a5ad2eeSDavid S. Miller struct flowi6 fl6; 14563a5ad2eeSDavid S. Miller 14573a5ad2eeSDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 1458e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 14593a5ad2eeSDavid S. Miller fl6.flowi6_oif = oif; 14603a5ad2eeSDavid S. Miller fl6.flowi6_mark = mark; 14613a5ad2eeSDavid S. Miller fl6.daddr = iph->daddr; 14623a5ad2eeSDavid S. Miller fl6.saddr = iph->saddr; 14636502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 14643a5ad2eeSDavid S. Miller 1465b55b76b2SDuan Jiong dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr); 14666700c270SDavid S. Miller rt6_do_redirect(dst, NULL, skb); 14673a5ad2eeSDavid S. Miller dst_release(dst); 14683a5ad2eeSDavid S. Miller } 14693a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect); 14703a5ad2eeSDavid S. Miller 1471c92a59ecSDuan Jiong void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif, 1472c92a59ecSDuan Jiong u32 mark) 1473c92a59ecSDuan Jiong { 1474c92a59ecSDuan Jiong const struct ipv6hdr *iph = ipv6_hdr(skb); 1475c92a59ecSDuan Jiong const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); 1476c92a59ecSDuan Jiong struct dst_entry *dst; 1477c92a59ecSDuan Jiong struct flowi6 fl6; 1478c92a59ecSDuan Jiong 1479c92a59ecSDuan Jiong memset(&fl6, 0, sizeof(fl6)); 1480e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 1481c92a59ecSDuan Jiong fl6.flowi6_oif = oif; 1482c92a59ecSDuan Jiong fl6.flowi6_mark = mark; 1483c92a59ecSDuan Jiong fl6.daddr = msg->dest; 1484c92a59ecSDuan Jiong fl6.saddr = iph->daddr; 1485c92a59ecSDuan Jiong 1486b55b76b2SDuan Jiong dst = ip6_route_redirect(net, &fl6, &iph->saddr); 1487c92a59ecSDuan Jiong rt6_do_redirect(dst, NULL, skb); 1488c92a59ecSDuan Jiong dst_release(dst); 1489c92a59ecSDuan Jiong } 1490c92a59ecSDuan Jiong 14913a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) 14923a5ad2eeSDavid S. Miller { 14933a5ad2eeSDavid S. Miller ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark); 14943a5ad2eeSDavid S. Miller } 14953a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect); 14963a5ad2eeSDavid S. Miller 14970dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 14981da177e4SLinus Torvalds { 14990dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 15000dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 15010dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 15020dbaee3bSDavid S. Miller 15031da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 15041da177e4SLinus Torvalds 15055578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 15065578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 15071da177e4SLinus Torvalds 15081da177e4SLinus Torvalds /* 15091da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 15101da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 15111da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 15121da177e4SLinus Torvalds * rely only on pmtu discovery" 15131da177e4SLinus Torvalds */ 15141da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 15151da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 15161da177e4SLinus Torvalds return mtu; 15171da177e4SLinus Torvalds } 15181da177e4SLinus Torvalds 1519ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst) 1520d33e4553SDavid S. Miller { 15214b32b5adSMartin KaFai Lau const struct rt6_info *rt = (const struct rt6_info *)dst; 15224b32b5adSMartin KaFai Lau unsigned int mtu = rt->rt6i_pmtu; 1523d33e4553SDavid S. Miller struct inet6_dev *idev; 1524618f9bc7SSteffen Klassert 1525618f9bc7SSteffen Klassert if (mtu) 152630f78d8eSEric Dumazet goto out; 1527618f9bc7SSteffen Klassert 15284b32b5adSMartin KaFai Lau mtu = dst_metric_raw(dst, RTAX_MTU); 15294b32b5adSMartin KaFai Lau if (mtu) 15304b32b5adSMartin KaFai Lau goto out; 15314b32b5adSMartin KaFai Lau 1532618f9bc7SSteffen Klassert mtu = IPV6_MIN_MTU; 1533d33e4553SDavid S. Miller 1534d33e4553SDavid S. Miller rcu_read_lock(); 1535d33e4553SDavid S. Miller idev = __in6_dev_get(dst->dev); 1536d33e4553SDavid S. Miller if (idev) 1537d33e4553SDavid S. Miller mtu = idev->cnf.mtu6; 1538d33e4553SDavid S. Miller rcu_read_unlock(); 1539d33e4553SDavid S. Miller 154030f78d8eSEric Dumazet out: 154130f78d8eSEric Dumazet return min_t(unsigned int, mtu, IP6_MAX_MTU); 1542d33e4553SDavid S. Miller } 1543d33e4553SDavid S. Miller 15443b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list; 15453b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock); 15465d0bbeebSThomas Graf 15473b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 154887a11578SDavid S. Miller struct flowi6 *fl6) 15491da177e4SLinus Torvalds { 155087a11578SDavid S. Miller struct dst_entry *dst; 15511da177e4SLinus Torvalds struct rt6_info *rt; 15521da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 1553c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 15541da177e4SLinus Torvalds 155538308473SDavid S. Miller if (unlikely(!idev)) 1556122bdf67SEric Dumazet return ERR_PTR(-ENODEV); 15571da177e4SLinus Torvalds 15588b96d22dSDavid S. Miller rt = ip6_dst_alloc(net, dev, 0, NULL); 155938308473SDavid S. Miller if (unlikely(!rt)) { 15601da177e4SLinus Torvalds in6_dev_put(idev); 156187a11578SDavid S. Miller dst = ERR_PTR(-ENOMEM); 15621da177e4SLinus Torvalds goto out; 15631da177e4SLinus Torvalds } 15641da177e4SLinus Torvalds 15658e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 15668e2ec639SYan, Zheng rt->dst.output = ip6_output; 1567d8d1f30bSChangli Gao atomic_set(&rt->dst.__refcnt, 1); 1568550bab42SJulian Anastasov rt->rt6i_gateway = fl6->daddr; 156987a11578SDavid S. Miller rt->rt6i_dst.addr = fl6->daddr; 15708e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 15718e2ec639SYan, Zheng rt->rt6i_idev = idev; 157214edd87dSLi RongQing dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); 15731da177e4SLinus Torvalds 15743b00944cSYOSHIFUJI Hideaki spin_lock_bh(&icmp6_dst_lock); 1575d8d1f30bSChangli Gao rt->dst.next = icmp6_dst_gc_list; 1576d8d1f30bSChangli Gao icmp6_dst_gc_list = &rt->dst; 15773b00944cSYOSHIFUJI Hideaki spin_unlock_bh(&icmp6_dst_lock); 15781da177e4SLinus Torvalds 15795578689aSDaniel Lezcano fib6_force_start_gc(net); 15801da177e4SLinus Torvalds 158187a11578SDavid S. Miller dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); 158287a11578SDavid S. Miller 15831da177e4SLinus Torvalds out: 158487a11578SDavid S. Miller return dst; 15851da177e4SLinus Torvalds } 15861da177e4SLinus Torvalds 15873d0f24a7SStephen Hemminger int icmp6_dst_gc(void) 15881da177e4SLinus Torvalds { 1589e9476e95SHagen Paul Pfeifer struct dst_entry *dst, **pprev; 15903d0f24a7SStephen Hemminger int more = 0; 15911da177e4SLinus Torvalds 15923b00944cSYOSHIFUJI Hideaki spin_lock_bh(&icmp6_dst_lock); 15933b00944cSYOSHIFUJI Hideaki pprev = &icmp6_dst_gc_list; 15945d0bbeebSThomas Graf 15951da177e4SLinus Torvalds while ((dst = *pprev) != NULL) { 15961da177e4SLinus Torvalds if (!atomic_read(&dst->__refcnt)) { 15971da177e4SLinus Torvalds *pprev = dst->next; 15981da177e4SLinus Torvalds dst_free(dst); 15991da177e4SLinus Torvalds } else { 16001da177e4SLinus Torvalds pprev = &dst->next; 16013d0f24a7SStephen Hemminger ++more; 16021da177e4SLinus Torvalds } 16031da177e4SLinus Torvalds } 16041da177e4SLinus Torvalds 16053b00944cSYOSHIFUJI Hideaki spin_unlock_bh(&icmp6_dst_lock); 16065d0bbeebSThomas Graf 16073d0f24a7SStephen Hemminger return more; 16081da177e4SLinus Torvalds } 16091da177e4SLinus Torvalds 16101e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg), 16111e493d19SDavid S. Miller void *arg) 16121e493d19SDavid S. Miller { 16131e493d19SDavid S. Miller struct dst_entry *dst, **pprev; 16141e493d19SDavid S. Miller 16151e493d19SDavid S. Miller spin_lock_bh(&icmp6_dst_lock); 16161e493d19SDavid S. Miller pprev = &icmp6_dst_gc_list; 16171e493d19SDavid S. Miller while ((dst = *pprev) != NULL) { 16181e493d19SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 16191e493d19SDavid S. Miller if (func(rt, arg)) { 16201e493d19SDavid S. Miller *pprev = dst->next; 16211e493d19SDavid S. Miller dst_free(dst); 16221e493d19SDavid S. Miller } else { 16231e493d19SDavid S. Miller pprev = &dst->next; 16241e493d19SDavid S. Miller } 16251e493d19SDavid S. Miller } 16261e493d19SDavid S. Miller spin_unlock_bh(&icmp6_dst_lock); 16271e493d19SDavid S. Miller } 16281e493d19SDavid S. Miller 1629569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops) 16301da177e4SLinus Torvalds { 163186393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 16327019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 16337019b78eSDaniel Lezcano int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; 16347019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 16357019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 16367019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 1637fc66f95cSEric Dumazet int entries; 16381da177e4SLinus Torvalds 1639fc66f95cSEric Dumazet entries = dst_entries_get_fast(ops); 164049a18d86SMichal Kubeček if (time_after(rt_last_gc + rt_min_interval, jiffies) && 1641fc66f95cSEric Dumazet entries <= rt_max_size) 16421da177e4SLinus Torvalds goto out; 16431da177e4SLinus Torvalds 16446891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire++; 164514956643SLi RongQing fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true); 1646fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 1647fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 16487019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; 16491da177e4SLinus Torvalds out: 16507019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; 1651fc66f95cSEric Dumazet return entries > rt_max_size; 16521da177e4SLinus Torvalds } 16531da177e4SLinus Torvalds 1654e715b6d3SFlorian Westphal static int ip6_convert_metrics(struct mx6_config *mxc, 1655e715b6d3SFlorian Westphal const struct fib6_config *cfg) 1656e715b6d3SFlorian Westphal { 1657e715b6d3SFlorian Westphal struct nlattr *nla; 1658e715b6d3SFlorian Westphal int remaining; 1659e715b6d3SFlorian Westphal u32 *mp; 1660e715b6d3SFlorian Westphal 166163159f29SIan Morris if (!cfg->fc_mx) 1662e715b6d3SFlorian Westphal return 0; 1663e715b6d3SFlorian Westphal 1664e715b6d3SFlorian Westphal mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL); 1665e715b6d3SFlorian Westphal if (unlikely(!mp)) 1666e715b6d3SFlorian Westphal return -ENOMEM; 1667e715b6d3SFlorian Westphal 1668e715b6d3SFlorian Westphal nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { 1669e715b6d3SFlorian Westphal int type = nla_type(nla); 1670e715b6d3SFlorian Westphal 1671e715b6d3SFlorian Westphal if (type) { 1672ea697639SDaniel Borkmann u32 val; 1673ea697639SDaniel Borkmann 1674e715b6d3SFlorian Westphal if (unlikely(type > RTAX_MAX)) 1675e715b6d3SFlorian Westphal goto err; 1676ea697639SDaniel Borkmann if (type == RTAX_CC_ALGO) { 1677ea697639SDaniel Borkmann char tmp[TCP_CA_NAME_MAX]; 1678e715b6d3SFlorian Westphal 1679ea697639SDaniel Borkmann nla_strlcpy(tmp, nla, sizeof(tmp)); 1680ea697639SDaniel Borkmann val = tcp_ca_get_key_by_name(tmp); 1681ea697639SDaniel Borkmann if (val == TCP_CA_UNSPEC) 1682ea697639SDaniel Borkmann goto err; 1683ea697639SDaniel Borkmann } else { 1684ea697639SDaniel Borkmann val = nla_get_u32(nla); 1685ea697639SDaniel Borkmann } 1686ea697639SDaniel Borkmann 1687ea697639SDaniel Borkmann mp[type - 1] = val; 1688e715b6d3SFlorian Westphal __set_bit(type - 1, mxc->mx_valid); 1689e715b6d3SFlorian Westphal } 1690e715b6d3SFlorian Westphal } 1691e715b6d3SFlorian Westphal 1692e715b6d3SFlorian Westphal mxc->mx = mp; 1693e715b6d3SFlorian Westphal 1694e715b6d3SFlorian Westphal return 0; 1695e715b6d3SFlorian Westphal err: 1696e715b6d3SFlorian Westphal kfree(mp); 1697e715b6d3SFlorian Westphal return -EINVAL; 1698e715b6d3SFlorian Westphal } 16991da177e4SLinus Torvalds 170086872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg) 17011da177e4SLinus Torvalds { 17021da177e4SLinus Torvalds int err; 17035578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 17041da177e4SLinus Torvalds struct rt6_info *rt = NULL; 17051da177e4SLinus Torvalds struct net_device *dev = NULL; 17061da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 1707c71099acSThomas Graf struct fib6_table *table; 1708e715b6d3SFlorian Westphal struct mx6_config mxc = { .mx = NULL, }; 17091da177e4SLinus Torvalds int addr_type; 17101da177e4SLinus Torvalds 171186872cb5SThomas Graf if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128) 17121da177e4SLinus Torvalds return -EINVAL; 17131da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 171486872cb5SThomas Graf if (cfg->fc_src_len) 17151da177e4SLinus Torvalds return -EINVAL; 17161da177e4SLinus Torvalds #endif 171786872cb5SThomas Graf if (cfg->fc_ifindex) { 17181da177e4SLinus Torvalds err = -ENODEV; 17195578689aSDaniel Lezcano dev = dev_get_by_index(net, cfg->fc_ifindex); 17201da177e4SLinus Torvalds if (!dev) 17211da177e4SLinus Torvalds goto out; 17221da177e4SLinus Torvalds idev = in6_dev_get(dev); 17231da177e4SLinus Torvalds if (!idev) 17241da177e4SLinus Torvalds goto out; 17251da177e4SLinus Torvalds } 17261da177e4SLinus Torvalds 172786872cb5SThomas Graf if (cfg->fc_metric == 0) 172886872cb5SThomas Graf cfg->fc_metric = IP6_RT_PRIO_USER; 17291da177e4SLinus Torvalds 1730c71099acSThomas Graf err = -ENOBUFS; 173138308473SDavid S. Miller if (cfg->fc_nlinfo.nlh && 1732d71314b4SMatti Vaittinen !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { 1733d71314b4SMatti Vaittinen table = fib6_get_table(net, cfg->fc_table); 173438308473SDavid S. Miller if (!table) { 1735f3213831SJoe Perches pr_warn("NLM_F_CREATE should be specified when creating new route\n"); 1736d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 1737d71314b4SMatti Vaittinen } 1738d71314b4SMatti Vaittinen } else { 1739d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 1740d71314b4SMatti Vaittinen } 174138308473SDavid S. Miller 174238308473SDavid S. Miller if (!table) 1743c71099acSThomas Graf goto out; 1744c71099acSThomas Graf 1745c88507fbSSabrina Dubroca rt = ip6_dst_alloc(net, NULL, (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT, table); 17461da177e4SLinus Torvalds 174738308473SDavid S. Miller if (!rt) { 17481da177e4SLinus Torvalds err = -ENOMEM; 17491da177e4SLinus Torvalds goto out; 17501da177e4SLinus Torvalds } 17511da177e4SLinus Torvalds 17521716a961SGao feng if (cfg->fc_flags & RTF_EXPIRES) 17531716a961SGao feng rt6_set_expires(rt, jiffies + 17541716a961SGao feng clock_t_to_jiffies(cfg->fc_expires)); 17551716a961SGao feng else 17561716a961SGao feng rt6_clean_expires(rt); 17571da177e4SLinus Torvalds 175886872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 175986872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 176086872cb5SThomas Graf rt->rt6i_protocol = cfg->fc_protocol; 176186872cb5SThomas Graf 176286872cb5SThomas Graf addr_type = ipv6_addr_type(&cfg->fc_dst); 17631da177e4SLinus Torvalds 17641da177e4SLinus Torvalds if (addr_type & IPV6_ADDR_MULTICAST) 1765d8d1f30bSChangli Gao rt->dst.input = ip6_mc_input; 1766ab79ad14SMaciej Żenczykowski else if (cfg->fc_flags & RTF_LOCAL) 1767ab79ad14SMaciej Żenczykowski rt->dst.input = ip6_input; 17681da177e4SLinus Torvalds else 1769d8d1f30bSChangli Gao rt->dst.input = ip6_forward; 17701da177e4SLinus Torvalds 1771d8d1f30bSChangli Gao rt->dst.output = ip6_output; 17721da177e4SLinus Torvalds 177386872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 177486872cb5SThomas Graf rt->rt6i_dst.plen = cfg->fc_dst_len; 1775afc4eef8SMartin KaFai Lau if (rt->rt6i_dst.plen == 128) 177611d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 17771da177e4SLinus Torvalds 17781da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 177986872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len); 178086872cb5SThomas Graf rt->rt6i_src.plen = cfg->fc_src_len; 17811da177e4SLinus Torvalds #endif 17821da177e4SLinus Torvalds 178386872cb5SThomas Graf rt->rt6i_metric = cfg->fc_metric; 17841da177e4SLinus Torvalds 17851da177e4SLinus Torvalds /* We cannot add true routes via loopback here, 17861da177e4SLinus Torvalds they would result in kernel looping; promote them to reject routes 17871da177e4SLinus Torvalds */ 178886872cb5SThomas Graf if ((cfg->fc_flags & RTF_REJECT) || 178938308473SDavid S. Miller (dev && (dev->flags & IFF_LOOPBACK) && 179038308473SDavid S. Miller !(addr_type & IPV6_ADDR_LOOPBACK) && 179138308473SDavid S. Miller !(cfg->fc_flags & RTF_LOCAL))) { 17921da177e4SLinus Torvalds /* hold loopback dev/idev if we haven't done so. */ 17935578689aSDaniel Lezcano if (dev != net->loopback_dev) { 17941da177e4SLinus Torvalds if (dev) { 17951da177e4SLinus Torvalds dev_put(dev); 17961da177e4SLinus Torvalds in6_dev_put(idev); 17971da177e4SLinus Torvalds } 17985578689aSDaniel Lezcano dev = net->loopback_dev; 17991da177e4SLinus Torvalds dev_hold(dev); 18001da177e4SLinus Torvalds idev = in6_dev_get(dev); 18011da177e4SLinus Torvalds if (!idev) { 18021da177e4SLinus Torvalds err = -ENODEV; 18031da177e4SLinus Torvalds goto out; 18041da177e4SLinus Torvalds } 18051da177e4SLinus Torvalds } 18061da177e4SLinus Torvalds rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP; 1807ef2c7d7bSNicolas Dichtel switch (cfg->fc_type) { 1808ef2c7d7bSNicolas Dichtel case RTN_BLACKHOLE: 1809ef2c7d7bSNicolas Dichtel rt->dst.error = -EINVAL; 1810aad88724SEric Dumazet rt->dst.output = dst_discard_sk; 18117150aedeSKamala R rt->dst.input = dst_discard; 1812ef2c7d7bSNicolas Dichtel break; 1813ef2c7d7bSNicolas Dichtel case RTN_PROHIBIT: 1814ef2c7d7bSNicolas Dichtel rt->dst.error = -EACCES; 18157150aedeSKamala R rt->dst.output = ip6_pkt_prohibit_out; 18167150aedeSKamala R rt->dst.input = ip6_pkt_prohibit; 1817ef2c7d7bSNicolas Dichtel break; 1818b4949ab2SNicolas Dichtel case RTN_THROW: 1819ef2c7d7bSNicolas Dichtel default: 18207150aedeSKamala R rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN 18217150aedeSKamala R : -ENETUNREACH; 18227150aedeSKamala R rt->dst.output = ip6_pkt_discard_out; 18237150aedeSKamala R rt->dst.input = ip6_pkt_discard; 1824ef2c7d7bSNicolas Dichtel break; 1825ef2c7d7bSNicolas Dichtel } 18261da177e4SLinus Torvalds goto install_route; 18271da177e4SLinus Torvalds } 18281da177e4SLinus Torvalds 182986872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY) { 1830b71d1d42SEric Dumazet const struct in6_addr *gw_addr; 18311da177e4SLinus Torvalds int gwa_type; 18321da177e4SLinus Torvalds 183386872cb5SThomas Graf gw_addr = &cfg->fc_gateway; 1834*330567b7SFlorian Westphal gwa_type = ipv6_addr_type(gw_addr); 183548ed7b26SFlorian Westphal 183648ed7b26SFlorian Westphal /* if gw_addr is local we will fail to detect this in case 183748ed7b26SFlorian Westphal * address is still TENTATIVE (DAD in progress). rt6_lookup() 183848ed7b26SFlorian Westphal * will return already-added prefix route via interface that 183948ed7b26SFlorian Westphal * prefix route was assigned to, which might be non-loopback. 184048ed7b26SFlorian Westphal */ 184148ed7b26SFlorian Westphal err = -EINVAL; 1842*330567b7SFlorian Westphal if (ipv6_chk_addr_and_flags(net, gw_addr, 1843*330567b7SFlorian Westphal gwa_type & IPV6_ADDR_LINKLOCAL ? 1844*330567b7SFlorian Westphal dev : NULL, 0, 0)) 184548ed7b26SFlorian Westphal goto out; 184648ed7b26SFlorian Westphal 18474e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = *gw_addr; 18481da177e4SLinus Torvalds 18491da177e4SLinus Torvalds if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) { 18501da177e4SLinus Torvalds struct rt6_info *grt; 18511da177e4SLinus Torvalds 18521da177e4SLinus Torvalds /* IPv6 strictly inhibits using not link-local 18531da177e4SLinus Torvalds addresses as nexthop address. 18541da177e4SLinus Torvalds Otherwise, router will not able to send redirects. 18551da177e4SLinus Torvalds It is very good, but in some (rare!) circumstances 18561da177e4SLinus Torvalds (SIT, PtP, NBMA NOARP links) it is handy to allow 18571da177e4SLinus Torvalds some exceptions. --ANK 18581da177e4SLinus Torvalds */ 18591da177e4SLinus Torvalds if (!(gwa_type & IPV6_ADDR_UNICAST)) 18601da177e4SLinus Torvalds goto out; 18611da177e4SLinus Torvalds 18625578689aSDaniel Lezcano grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1); 18631da177e4SLinus Torvalds 18641da177e4SLinus Torvalds err = -EHOSTUNREACH; 186538308473SDavid S. Miller if (!grt) 18661da177e4SLinus Torvalds goto out; 18671da177e4SLinus Torvalds if (dev) { 1868d1918542SDavid S. Miller if (dev != grt->dst.dev) { 186994e187c0SAmerigo Wang ip6_rt_put(grt); 18701da177e4SLinus Torvalds goto out; 18711da177e4SLinus Torvalds } 18721da177e4SLinus Torvalds } else { 1873d1918542SDavid S. Miller dev = grt->dst.dev; 18741da177e4SLinus Torvalds idev = grt->rt6i_idev; 18751da177e4SLinus Torvalds dev_hold(dev); 18761da177e4SLinus Torvalds in6_dev_hold(grt->rt6i_idev); 18771da177e4SLinus Torvalds } 18781da177e4SLinus Torvalds if (!(grt->rt6i_flags & RTF_GATEWAY)) 18791da177e4SLinus Torvalds err = 0; 188094e187c0SAmerigo Wang ip6_rt_put(grt); 18811da177e4SLinus Torvalds 18821da177e4SLinus Torvalds if (err) 18831da177e4SLinus Torvalds goto out; 18841da177e4SLinus Torvalds } 18851da177e4SLinus Torvalds err = -EINVAL; 188638308473SDavid S. Miller if (!dev || (dev->flags & IFF_LOOPBACK)) 18871da177e4SLinus Torvalds goto out; 18881da177e4SLinus Torvalds } 18891da177e4SLinus Torvalds 18901da177e4SLinus Torvalds err = -ENODEV; 189138308473SDavid S. Miller if (!dev) 18921da177e4SLinus Torvalds goto out; 18931da177e4SLinus Torvalds 1894c3968a85SDaniel Walter if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 1895c3968a85SDaniel Walter if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 1896c3968a85SDaniel Walter err = -EINVAL; 1897c3968a85SDaniel Walter goto out; 1898c3968a85SDaniel Walter } 18994e3fd7a0SAlexey Dobriyan rt->rt6i_prefsrc.addr = cfg->fc_prefsrc; 1900c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 128; 1901c3968a85SDaniel Walter } else 1902c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 1903c3968a85SDaniel Walter 190486872cb5SThomas Graf rt->rt6i_flags = cfg->fc_flags; 19051da177e4SLinus Torvalds 19061da177e4SLinus Torvalds install_route: 1907d8d1f30bSChangli Gao rt->dst.dev = dev; 19081da177e4SLinus Torvalds rt->rt6i_idev = idev; 1909c71099acSThomas Graf rt->rt6i_table = table; 191063152fc0SDaniel Lezcano 1911c346dca1SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = dev_net(dev); 191263152fc0SDaniel Lezcano 1913e715b6d3SFlorian Westphal err = ip6_convert_metrics(&mxc, cfg); 1914e715b6d3SFlorian Westphal if (err) 1915e715b6d3SFlorian Westphal goto out; 19161da177e4SLinus Torvalds 1917e715b6d3SFlorian Westphal err = __ip6_ins_rt(rt, &cfg->fc_nlinfo, &mxc); 1918e715b6d3SFlorian Westphal 1919e715b6d3SFlorian Westphal kfree(mxc.mx); 1920e715b6d3SFlorian Westphal return err; 19211da177e4SLinus Torvalds out: 19221da177e4SLinus Torvalds if (dev) 19231da177e4SLinus Torvalds dev_put(dev); 19241da177e4SLinus Torvalds if (idev) 19251da177e4SLinus Torvalds in6_dev_put(idev); 19261da177e4SLinus Torvalds if (rt) 1927d8d1f30bSChangli Gao dst_free(&rt->dst); 19281da177e4SLinus Torvalds return err; 19291da177e4SLinus Torvalds } 19301da177e4SLinus Torvalds 193186872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info) 19321da177e4SLinus Torvalds { 19331da177e4SLinus Torvalds int err; 1934c71099acSThomas Graf struct fib6_table *table; 1935d1918542SDavid S. Miller struct net *net = dev_net(rt->dst.dev); 19361da177e4SLinus Torvalds 19376825a26cSGao feng if (rt == net->ipv6.ip6_null_entry) { 19386825a26cSGao feng err = -ENOENT; 19396825a26cSGao feng goto out; 19406825a26cSGao feng } 19416c813a72SPatrick McHardy 1942c71099acSThomas Graf table = rt->rt6i_table; 1943c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 194486872cb5SThomas Graf err = fib6_del(rt, info); 1945c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 19461da177e4SLinus Torvalds 19476825a26cSGao feng out: 194894e187c0SAmerigo Wang ip6_rt_put(rt); 19491da177e4SLinus Torvalds return err; 19501da177e4SLinus Torvalds } 19511da177e4SLinus Torvalds 1952e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt) 1953e0a1ad73SThomas Graf { 19544d1169c1SDenis V. Lunev struct nl_info info = { 1955d1918542SDavid S. Miller .nl_net = dev_net(rt->dst.dev), 19564d1169c1SDenis V. Lunev }; 1957528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 1958e0a1ad73SThomas Graf } 1959e0a1ad73SThomas Graf 196086872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg) 19611da177e4SLinus Torvalds { 1962c71099acSThomas Graf struct fib6_table *table; 19631da177e4SLinus Torvalds struct fib6_node *fn; 19641da177e4SLinus Torvalds struct rt6_info *rt; 19651da177e4SLinus Torvalds int err = -ESRCH; 19661da177e4SLinus Torvalds 19675578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 196838308473SDavid S. Miller if (!table) 1969c71099acSThomas Graf return err; 19701da177e4SLinus Torvalds 1971c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 1972c71099acSThomas Graf 1973c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 197486872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 197586872cb5SThomas Graf &cfg->fc_src, cfg->fc_src_len); 19761da177e4SLinus Torvalds 19771da177e4SLinus Torvalds if (fn) { 1978d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 19791f56a01fSMartin KaFai Lau if ((rt->rt6i_flags & RTF_CACHE) && 19801f56a01fSMartin KaFai Lau !(cfg->fc_flags & RTF_CACHE)) 19811f56a01fSMartin KaFai Lau continue; 198286872cb5SThomas Graf if (cfg->fc_ifindex && 1983d1918542SDavid S. Miller (!rt->dst.dev || 1984d1918542SDavid S. Miller rt->dst.dev->ifindex != cfg->fc_ifindex)) 19851da177e4SLinus Torvalds continue; 198686872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 198786872cb5SThomas Graf !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 19881da177e4SLinus Torvalds continue; 198986872cb5SThomas Graf if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric) 19901da177e4SLinus Torvalds continue; 1991d8d1f30bSChangli Gao dst_hold(&rt->dst); 1992c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 19931da177e4SLinus Torvalds 199486872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 19951da177e4SLinus Torvalds } 19961da177e4SLinus Torvalds } 1997c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 19981da177e4SLinus Torvalds 19991da177e4SLinus Torvalds return err; 20001da177e4SLinus Torvalds } 20011da177e4SLinus Torvalds 20026700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 2003a6279458SYOSHIFUJI Hideaki { 2004e8599ff4SDavid S. Miller struct net *net = dev_net(skb->dev); 2005a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 2006e8599ff4SDavid S. Miller struct rt6_info *rt, *nrt = NULL; 2007e8599ff4SDavid S. Miller struct ndisc_options ndopts; 2008e8599ff4SDavid S. Miller struct inet6_dev *in6_dev; 2009e8599ff4SDavid S. Miller struct neighbour *neigh; 201071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 struct rd_msg *msg; 20116e157b6aSDavid S. Miller int optlen, on_link; 20126e157b6aSDavid S. Miller u8 *lladdr; 2013e8599ff4SDavid S. Miller 201429a3cad5SSimon Horman optlen = skb_tail_pointer(skb) - skb_transport_header(skb); 201571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 optlen -= sizeof(*msg); 2016e8599ff4SDavid S. Miller 2017e8599ff4SDavid S. Miller if (optlen < 0) { 20186e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); 2019e8599ff4SDavid S. Miller return; 2020e8599ff4SDavid S. Miller } 2021e8599ff4SDavid S. Miller 202271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 msg = (struct rd_msg *)icmp6_hdr(skb); 2023e8599ff4SDavid S. Miller 202471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_is_multicast(&msg->dest)) { 20256e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); 2026e8599ff4SDavid S. Miller return; 2027e8599ff4SDavid S. Miller } 2028e8599ff4SDavid S. Miller 20296e157b6aSDavid S. Miller on_link = 0; 203071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_equal(&msg->dest, &msg->target)) { 2031e8599ff4SDavid S. Miller on_link = 1; 203271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 } else if (ipv6_addr_type(&msg->target) != 2033e8599ff4SDavid S. Miller (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { 20346e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); 2035e8599ff4SDavid S. Miller return; 2036e8599ff4SDavid S. Miller } 2037e8599ff4SDavid S. Miller 2038e8599ff4SDavid S. Miller in6_dev = __in6_dev_get(skb->dev); 2039e8599ff4SDavid S. Miller if (!in6_dev) 2040e8599ff4SDavid S. Miller return; 2041e8599ff4SDavid S. Miller if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) 2042e8599ff4SDavid S. Miller return; 2043e8599ff4SDavid S. Miller 2044e8599ff4SDavid S. Miller /* RFC2461 8.1: 2045e8599ff4SDavid S. Miller * The IP source address of the Redirect MUST be the same as the current 2046e8599ff4SDavid S. Miller * first-hop router for the specified ICMP Destination Address. 2047e8599ff4SDavid S. Miller */ 2048e8599ff4SDavid S. Miller 204971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (!ndisc_parse_options(msg->opt, optlen, &ndopts)) { 2050e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); 2051e8599ff4SDavid S. Miller return; 2052e8599ff4SDavid S. Miller } 20536e157b6aSDavid S. Miller 20546e157b6aSDavid S. Miller lladdr = NULL; 2055e8599ff4SDavid S. Miller if (ndopts.nd_opts_tgt_lladdr) { 2056e8599ff4SDavid S. Miller lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, 2057e8599ff4SDavid S. Miller skb->dev); 2058e8599ff4SDavid S. Miller if (!lladdr) { 2059e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); 2060e8599ff4SDavid S. Miller return; 2061e8599ff4SDavid S. Miller } 2062e8599ff4SDavid S. Miller } 2063e8599ff4SDavid S. Miller 20646e157b6aSDavid S. Miller rt = (struct rt6_info *) dst; 20656e157b6aSDavid S. Miller if (rt == net->ipv6.ip6_null_entry) { 20666e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); 20676e157b6aSDavid S. Miller return; 20686e157b6aSDavid S. Miller } 20696e157b6aSDavid S. Miller 20706e157b6aSDavid S. Miller /* Redirect received -> path was valid. 20716e157b6aSDavid S. Miller * Look, redirects are sent only in response to data packets, 20726e157b6aSDavid S. Miller * so that this nexthop apparently is reachable. --ANK 20736e157b6aSDavid S. Miller */ 20746e157b6aSDavid S. Miller dst_confirm(&rt->dst); 20756e157b6aSDavid S. Miller 207671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); 2077e8599ff4SDavid S. Miller if (!neigh) 2078e8599ff4SDavid S. Miller return; 2079e8599ff4SDavid S. Miller 20801da177e4SLinus Torvalds /* 20811da177e4SLinus Torvalds * We have finally decided to accept it. 20821da177e4SLinus Torvalds */ 20831da177e4SLinus Torvalds 20841da177e4SLinus Torvalds neigh_update(neigh, lladdr, NUD_STALE, 20851da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 20861da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 20871da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 20881da177e4SLinus Torvalds NEIGH_UPDATE_F_ISROUTER)) 20891da177e4SLinus Torvalds ); 20901da177e4SLinus Torvalds 209183a09abdSMartin KaFai Lau nrt = ip6_rt_cache_alloc(rt, &msg->dest, NULL); 209238308473SDavid S. Miller if (!nrt) 20931da177e4SLinus Torvalds goto out; 20941da177e4SLinus Torvalds 20951da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 20961da177e4SLinus Torvalds if (on_link) 20971da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 20981da177e4SLinus Torvalds 20994e3fd7a0SAlexey Dobriyan nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 21001da177e4SLinus Torvalds 210140e22e8fSThomas Graf if (ip6_ins_rt(nrt)) 21021da177e4SLinus Torvalds goto out; 21031da177e4SLinus Torvalds 2104d8d1f30bSChangli Gao netevent.old = &rt->dst; 2105d8d1f30bSChangli Gao netevent.new = &nrt->dst; 210671bcdba0SYOSHIFUJI Hideaki / 吉藤英明 netevent.daddr = &msg->dest; 210760592833SYOSHIFUJI Hideaki / 吉藤英明 netevent.neigh = neigh; 21088d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 21098d71740cSTom Tucker 21101da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) { 21116e157b6aSDavid S. Miller rt = (struct rt6_info *) dst_clone(&rt->dst); 2112e0a1ad73SThomas Graf ip6_del_rt(rt); 21131da177e4SLinus Torvalds } 21141da177e4SLinus Torvalds 21151da177e4SLinus Torvalds out: 2116e8599ff4SDavid S. Miller neigh_release(neigh); 21176e157b6aSDavid S. Miller } 21186e157b6aSDavid S. Miller 21191da177e4SLinus Torvalds /* 21201da177e4SLinus Torvalds * Misc support functions 21211da177e4SLinus Torvalds */ 21221da177e4SLinus Torvalds 21234b32b5adSMartin KaFai Lau static void rt6_set_from(struct rt6_info *rt, struct rt6_info *from) 21244b32b5adSMartin KaFai Lau { 21254b32b5adSMartin KaFai Lau BUG_ON(from->dst.from); 21264b32b5adSMartin KaFai Lau 21274b32b5adSMartin KaFai Lau rt->rt6i_flags &= ~RTF_EXPIRES; 21284b32b5adSMartin KaFai Lau dst_hold(&from->dst); 21294b32b5adSMartin KaFai Lau rt->dst.from = &from->dst; 21304b32b5adSMartin KaFai Lau dst_init_metrics(&rt->dst, dst_metrics_ptr(&from->dst), true); 21314b32b5adSMartin KaFai Lau } 21324b32b5adSMartin KaFai Lau 213383a09abdSMartin KaFai Lau static void ip6_rt_copy_init(struct rt6_info *rt, struct rt6_info *ort) 21341da177e4SLinus Torvalds { 2135d8d1f30bSChangli Gao rt->dst.input = ort->dst.input; 2136d8d1f30bSChangli Gao rt->dst.output = ort->dst.output; 213783a09abdSMartin KaFai Lau rt->rt6i_dst = ort->rt6i_dst; 2138d8d1f30bSChangli Gao rt->dst.error = ort->dst.error; 21391da177e4SLinus Torvalds rt->rt6i_idev = ort->rt6i_idev; 21401da177e4SLinus Torvalds if (rt->rt6i_idev) 21411da177e4SLinus Torvalds in6_dev_hold(rt->rt6i_idev); 2142d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 21434e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 21441716a961SGao feng rt->rt6i_flags = ort->rt6i_flags; 21451716a961SGao feng rt6_set_from(rt, ort); 214683a09abdSMartin KaFai Lau rt->rt6i_metric = ort->rt6i_metric; 21471da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 214883a09abdSMartin KaFai Lau rt->rt6i_src = ort->rt6i_src; 21491da177e4SLinus Torvalds #endif 215083a09abdSMartin KaFai Lau rt->rt6i_prefsrc = ort->rt6i_prefsrc; 2151c71099acSThomas Graf rt->rt6i_table = ort->rt6i_table; 21521da177e4SLinus Torvalds } 21531da177e4SLinus Torvalds 215470ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 2155efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 2156b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 2157b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex) 215870ceb4f5SYOSHIFUJI Hideaki { 215970ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 216070ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt = NULL; 2161c71099acSThomas Graf struct fib6_table *table; 216270ceb4f5SYOSHIFUJI Hideaki 2163efa2cea0SDaniel Lezcano table = fib6_get_table(net, RT6_TABLE_INFO); 216438308473SDavid S. Miller if (!table) 2165c71099acSThomas Graf return NULL; 2166c71099acSThomas Graf 21675744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 2168c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0); 216970ceb4f5SYOSHIFUJI Hideaki if (!fn) 217070ceb4f5SYOSHIFUJI Hideaki goto out; 217170ceb4f5SYOSHIFUJI Hideaki 2172d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 2173d1918542SDavid S. Miller if (rt->dst.dev->ifindex != ifindex) 217470ceb4f5SYOSHIFUJI Hideaki continue; 217570ceb4f5SYOSHIFUJI Hideaki if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) 217670ceb4f5SYOSHIFUJI Hideaki continue; 217770ceb4f5SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr)) 217870ceb4f5SYOSHIFUJI Hideaki continue; 2179d8d1f30bSChangli Gao dst_hold(&rt->dst); 218070ceb4f5SYOSHIFUJI Hideaki break; 218170ceb4f5SYOSHIFUJI Hideaki } 218270ceb4f5SYOSHIFUJI Hideaki out: 21835744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 218470ceb4f5SYOSHIFUJI Hideaki return rt; 218570ceb4f5SYOSHIFUJI Hideaki } 218670ceb4f5SYOSHIFUJI Hideaki 2187efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 2188b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 2189b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex, 219095c96174SEric Dumazet unsigned int pref) 219170ceb4f5SYOSHIFUJI Hideaki { 219286872cb5SThomas Graf struct fib6_config cfg = { 219386872cb5SThomas Graf .fc_table = RT6_TABLE_INFO, 2194238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 219586872cb5SThomas Graf .fc_ifindex = ifindex, 219686872cb5SThomas Graf .fc_dst_len = prefixlen, 219786872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 219886872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 219915e47304SEric W. Biederman .fc_nlinfo.portid = 0, 2200efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 2201efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 220286872cb5SThomas Graf }; 220370ceb4f5SYOSHIFUJI Hideaki 22044e3fd7a0SAlexey Dobriyan cfg.fc_dst = *prefix; 22054e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 220686872cb5SThomas Graf 2207e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 2208e317da96SYOSHIFUJI Hideaki if (!prefixlen) 220986872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 221070ceb4f5SYOSHIFUJI Hideaki 221186872cb5SThomas Graf ip6_route_add(&cfg); 221270ceb4f5SYOSHIFUJI Hideaki 2213efa2cea0SDaniel Lezcano return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex); 221470ceb4f5SYOSHIFUJI Hideaki } 221570ceb4f5SYOSHIFUJI Hideaki #endif 221670ceb4f5SYOSHIFUJI Hideaki 2217b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev) 22181da177e4SLinus Torvalds { 22191da177e4SLinus Torvalds struct rt6_info *rt; 2220c71099acSThomas Graf struct fib6_table *table; 22211da177e4SLinus Torvalds 2222c346dca1SYOSHIFUJI Hideaki table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT); 222338308473SDavid S. Miller if (!table) 2224c71099acSThomas Graf return NULL; 22251da177e4SLinus Torvalds 22265744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 2227d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 2228d1918542SDavid S. Miller if (dev == rt->dst.dev && 2229045927ffSYOSHIFUJI Hideaki ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 22301da177e4SLinus Torvalds ipv6_addr_equal(&rt->rt6i_gateway, addr)) 22311da177e4SLinus Torvalds break; 22321da177e4SLinus Torvalds } 22331da177e4SLinus Torvalds if (rt) 2234d8d1f30bSChangli Gao dst_hold(&rt->dst); 22355744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 22361da177e4SLinus Torvalds return rt; 22371da177e4SLinus Torvalds } 22381da177e4SLinus Torvalds 2239b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr, 2240ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 2241ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 22421da177e4SLinus Torvalds { 224386872cb5SThomas Graf struct fib6_config cfg = { 224486872cb5SThomas Graf .fc_table = RT6_TABLE_DFLT, 2245238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 224686872cb5SThomas Graf .fc_ifindex = dev->ifindex, 224786872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 224886872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 224915e47304SEric W. Biederman .fc_nlinfo.portid = 0, 22505578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 2251c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 225286872cb5SThomas Graf }; 22531da177e4SLinus Torvalds 22544e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 22551da177e4SLinus Torvalds 225686872cb5SThomas Graf ip6_route_add(&cfg); 22571da177e4SLinus Torvalds 22581da177e4SLinus Torvalds return rt6_get_dflt_router(gwaddr, dev); 22591da177e4SLinus Torvalds } 22601da177e4SLinus Torvalds 22617b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net) 22621da177e4SLinus Torvalds { 22631da177e4SLinus Torvalds struct rt6_info *rt; 2264c71099acSThomas Graf struct fib6_table *table; 2265c71099acSThomas Graf 2266c71099acSThomas Graf /* NOTE: Keep consistent with rt6_get_dflt_router */ 22677b4da532SDaniel Lezcano table = fib6_get_table(net, RT6_TABLE_DFLT); 226838308473SDavid S. Miller if (!table) 2269c71099acSThomas Graf return; 22701da177e4SLinus Torvalds 22711da177e4SLinus Torvalds restart: 2272c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 2273d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 22743e8b0ac3SLorenzo Colitti if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) && 22753e8b0ac3SLorenzo Colitti (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) { 2276d8d1f30bSChangli Gao dst_hold(&rt->dst); 2277c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 2278e0a1ad73SThomas Graf ip6_del_rt(rt); 22791da177e4SLinus Torvalds goto restart; 22801da177e4SLinus Torvalds } 22811da177e4SLinus Torvalds } 2282c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 22831da177e4SLinus Torvalds } 22841da177e4SLinus Torvalds 22855578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 22865578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 228786872cb5SThomas Graf struct fib6_config *cfg) 228886872cb5SThomas Graf { 228986872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 229086872cb5SThomas Graf 229186872cb5SThomas Graf cfg->fc_table = RT6_TABLE_MAIN; 229286872cb5SThomas Graf cfg->fc_ifindex = rtmsg->rtmsg_ifindex; 229386872cb5SThomas Graf cfg->fc_metric = rtmsg->rtmsg_metric; 229486872cb5SThomas Graf cfg->fc_expires = rtmsg->rtmsg_info; 229586872cb5SThomas Graf cfg->fc_dst_len = rtmsg->rtmsg_dst_len; 229686872cb5SThomas Graf cfg->fc_src_len = rtmsg->rtmsg_src_len; 229786872cb5SThomas Graf cfg->fc_flags = rtmsg->rtmsg_flags; 229886872cb5SThomas Graf 22995578689aSDaniel Lezcano cfg->fc_nlinfo.nl_net = net; 2300f1243c2dSBenjamin Thery 23014e3fd7a0SAlexey Dobriyan cfg->fc_dst = rtmsg->rtmsg_dst; 23024e3fd7a0SAlexey Dobriyan cfg->fc_src = rtmsg->rtmsg_src; 23034e3fd7a0SAlexey Dobriyan cfg->fc_gateway = rtmsg->rtmsg_gateway; 230486872cb5SThomas Graf } 230586872cb5SThomas Graf 23065578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) 23071da177e4SLinus Torvalds { 230886872cb5SThomas Graf struct fib6_config cfg; 23091da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 23101da177e4SLinus Torvalds int err; 23111da177e4SLinus Torvalds 23121da177e4SLinus Torvalds switch (cmd) { 23131da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 23141da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 2315af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 23161da177e4SLinus Torvalds return -EPERM; 23171da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 23181da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 23191da177e4SLinus Torvalds if (err) 23201da177e4SLinus Torvalds return -EFAULT; 23211da177e4SLinus Torvalds 23225578689aSDaniel Lezcano rtmsg_to_fib6_config(net, &rtmsg, &cfg); 232386872cb5SThomas Graf 23241da177e4SLinus Torvalds rtnl_lock(); 23251da177e4SLinus Torvalds switch (cmd) { 23261da177e4SLinus Torvalds case SIOCADDRT: 232786872cb5SThomas Graf err = ip6_route_add(&cfg); 23281da177e4SLinus Torvalds break; 23291da177e4SLinus Torvalds case SIOCDELRT: 233086872cb5SThomas Graf err = ip6_route_del(&cfg); 23311da177e4SLinus Torvalds break; 23321da177e4SLinus Torvalds default: 23331da177e4SLinus Torvalds err = -EINVAL; 23341da177e4SLinus Torvalds } 23351da177e4SLinus Torvalds rtnl_unlock(); 23361da177e4SLinus Torvalds 23371da177e4SLinus Torvalds return err; 23383ff50b79SStephen Hemminger } 23391da177e4SLinus Torvalds 23401da177e4SLinus Torvalds return -EINVAL; 23411da177e4SLinus Torvalds } 23421da177e4SLinus Torvalds 23431da177e4SLinus Torvalds /* 23441da177e4SLinus Torvalds * Drop the packet on the floor 23451da177e4SLinus Torvalds */ 23461da177e4SLinus Torvalds 2347d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 23481da177e4SLinus Torvalds { 2349612f09e8SYOSHIFUJI Hideaki int type; 2350adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 2351612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 2352612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 23530660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 235445bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 23553bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 23563bd653c8SDenis V. Lunev IPSTATS_MIB_INADDRERRORS); 2357612f09e8SYOSHIFUJI Hideaki break; 2358612f09e8SYOSHIFUJI Hideaki } 2359612f09e8SYOSHIFUJI Hideaki /* FALLTHROUGH */ 2360612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 23613bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 23623bd653c8SDenis V. Lunev ipstats_mib_noroutes); 2363612f09e8SYOSHIFUJI Hideaki break; 2364612f09e8SYOSHIFUJI Hideaki } 23653ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 23661da177e4SLinus Torvalds kfree_skb(skb); 23671da177e4SLinus Torvalds return 0; 23681da177e4SLinus Torvalds } 23691da177e4SLinus Torvalds 23709ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 23719ce8ade0SThomas Graf { 2372612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 23739ce8ade0SThomas Graf } 23749ce8ade0SThomas Graf 2375aad88724SEric Dumazet static int ip6_pkt_discard_out(struct sock *sk, struct sk_buff *skb) 23761da177e4SLinus Torvalds { 2377adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 2378612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 23791da177e4SLinus Torvalds } 23801da177e4SLinus Torvalds 23819ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 23829ce8ade0SThomas Graf { 2383612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 23849ce8ade0SThomas Graf } 23859ce8ade0SThomas Graf 2386aad88724SEric Dumazet static int ip6_pkt_prohibit_out(struct sock *sk, struct sk_buff *skb) 23879ce8ade0SThomas Graf { 2388adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 2389612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 23909ce8ade0SThomas Graf } 23919ce8ade0SThomas Graf 23921da177e4SLinus Torvalds /* 23931da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 23941da177e4SLinus Torvalds */ 23951da177e4SLinus Torvalds 23961da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, 23971da177e4SLinus Torvalds const struct in6_addr *addr, 23988f031519SDavid S. Miller bool anycast) 23991da177e4SLinus Torvalds { 2400c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 2401a3300ef4SHannes Frederic Sowa struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev, 2402a3300ef4SHannes Frederic Sowa DST_NOCOUNT, NULL); 2403a3300ef4SHannes Frederic Sowa if (!rt) 24041da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 24051da177e4SLinus Torvalds 24061da177e4SLinus Torvalds in6_dev_hold(idev); 24071da177e4SLinus Torvalds 240811d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 2409d8d1f30bSChangli Gao rt->dst.input = ip6_input; 2410d8d1f30bSChangli Gao rt->dst.output = ip6_output; 24111da177e4SLinus Torvalds rt->rt6i_idev = idev; 24121da177e4SLinus Torvalds 24131da177e4SLinus Torvalds rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP; 241458c4fb86SYOSHIFUJI Hideaki if (anycast) 241558c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 241658c4fb86SYOSHIFUJI Hideaki else 24171da177e4SLinus Torvalds rt->rt6i_flags |= RTF_LOCAL; 24181da177e4SLinus Torvalds 2419550bab42SJulian Anastasov rt->rt6i_gateway = *addr; 24204e3fd7a0SAlexey Dobriyan rt->rt6i_dst.addr = *addr; 24211da177e4SLinus Torvalds rt->rt6i_dst.plen = 128; 24225578689aSDaniel Lezcano rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL); 24231da177e4SLinus Torvalds 2424d8d1f30bSChangli Gao atomic_set(&rt->dst.__refcnt, 1); 24251da177e4SLinus Torvalds 24261da177e4SLinus Torvalds return rt; 24271da177e4SLinus Torvalds } 24281da177e4SLinus Torvalds 2429c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net, 2430c3968a85SDaniel Walter struct rt6_info *rt, 2431b71d1d42SEric Dumazet const struct in6_addr *daddr, 2432c3968a85SDaniel Walter unsigned int prefs, 2433c3968a85SDaniel Walter struct in6_addr *saddr) 2434c3968a85SDaniel Walter { 2435e16e888bSMarkus Stenberg struct inet6_dev *idev = 2436e16e888bSMarkus Stenberg rt ? ip6_dst_idev((struct dst_entry *)rt) : NULL; 2437c3968a85SDaniel Walter int err = 0; 2438e16e888bSMarkus Stenberg if (rt && rt->rt6i_prefsrc.plen) 24394e3fd7a0SAlexey Dobriyan *saddr = rt->rt6i_prefsrc.addr; 2440c3968a85SDaniel Walter else 2441c3968a85SDaniel Walter err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL, 2442c3968a85SDaniel Walter daddr, prefs, saddr); 2443c3968a85SDaniel Walter return err; 2444c3968a85SDaniel Walter } 2445c3968a85SDaniel Walter 2446c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 2447c3968a85SDaniel Walter struct arg_dev_net_ip { 2448c3968a85SDaniel Walter struct net_device *dev; 2449c3968a85SDaniel Walter struct net *net; 2450c3968a85SDaniel Walter struct in6_addr *addr; 2451c3968a85SDaniel Walter }; 2452c3968a85SDaniel Walter 2453c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg) 2454c3968a85SDaniel Walter { 2455c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 2456c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 2457c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 2458c3968a85SDaniel Walter 2459d1918542SDavid S. Miller if (((void *)rt->dst.dev == dev || !dev) && 2460c3968a85SDaniel Walter rt != net->ipv6.ip6_null_entry && 2461c3968a85SDaniel Walter ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) { 2462c3968a85SDaniel Walter /* remove prefsrc entry */ 2463c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 2464c3968a85SDaniel Walter } 2465c3968a85SDaniel Walter return 0; 2466c3968a85SDaniel Walter } 2467c3968a85SDaniel Walter 2468c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 2469c3968a85SDaniel Walter { 2470c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 2471c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 2472c3968a85SDaniel Walter .dev = ifp->idev->dev, 2473c3968a85SDaniel Walter .net = net, 2474c3968a85SDaniel Walter .addr = &ifp->addr, 2475c3968a85SDaniel Walter }; 24760c3584d5SLi RongQing fib6_clean_all(net, fib6_remove_prefsrc, &adni); 2477c3968a85SDaniel Walter } 2478c3968a85SDaniel Walter 2479be7a010dSDuan Jiong #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY) 2480be7a010dSDuan Jiong #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) 2481be7a010dSDuan Jiong 2482be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */ 2483be7a010dSDuan Jiong static int fib6_clean_tohost(struct rt6_info *rt, void *arg) 2484be7a010dSDuan Jiong { 2485be7a010dSDuan Jiong struct in6_addr *gateway = (struct in6_addr *)arg; 2486be7a010dSDuan Jiong 2487be7a010dSDuan Jiong if ((((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) || 2488be7a010dSDuan Jiong ((rt->rt6i_flags & RTF_CACHE_GATEWAY) == RTF_CACHE_GATEWAY)) && 2489be7a010dSDuan Jiong ipv6_addr_equal(gateway, &rt->rt6i_gateway)) { 2490be7a010dSDuan Jiong return -1; 2491be7a010dSDuan Jiong } 2492be7a010dSDuan Jiong return 0; 2493be7a010dSDuan Jiong } 2494be7a010dSDuan Jiong 2495be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) 2496be7a010dSDuan Jiong { 2497be7a010dSDuan Jiong fib6_clean_all(net, fib6_clean_tohost, gateway); 2498be7a010dSDuan Jiong } 2499be7a010dSDuan Jiong 25008ed67789SDaniel Lezcano struct arg_dev_net { 25018ed67789SDaniel Lezcano struct net_device *dev; 25028ed67789SDaniel Lezcano struct net *net; 25038ed67789SDaniel Lezcano }; 25048ed67789SDaniel Lezcano 25051da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg) 25061da177e4SLinus Torvalds { 2507bc3ef660Sstephen hemminger const struct arg_dev_net *adn = arg; 2508bc3ef660Sstephen hemminger const struct net_device *dev = adn->dev; 25098ed67789SDaniel Lezcano 2510d1918542SDavid S. Miller if ((rt->dst.dev == dev || !dev) && 2511c159d30cSDavid S. Miller rt != adn->net->ipv6.ip6_null_entry) 25121da177e4SLinus Torvalds return -1; 2513c159d30cSDavid S. Miller 25141da177e4SLinus Torvalds return 0; 25151da177e4SLinus Torvalds } 25161da177e4SLinus Torvalds 2517f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev) 25181da177e4SLinus Torvalds { 25198ed67789SDaniel Lezcano struct arg_dev_net adn = { 25208ed67789SDaniel Lezcano .dev = dev, 25218ed67789SDaniel Lezcano .net = net, 25228ed67789SDaniel Lezcano }; 25238ed67789SDaniel Lezcano 25240c3584d5SLi RongQing fib6_clean_all(net, fib6_ifdown, &adn); 25251e493d19SDavid S. Miller icmp6_clean_all(fib6_ifdown, &adn); 25268d0b94afSMartin KaFai Lau rt6_uncached_list_flush_dev(net, dev); 25271da177e4SLinus Torvalds } 25281da177e4SLinus Torvalds 252995c96174SEric Dumazet struct rt6_mtu_change_arg { 25301da177e4SLinus Torvalds struct net_device *dev; 253195c96174SEric Dumazet unsigned int mtu; 25321da177e4SLinus Torvalds }; 25331da177e4SLinus Torvalds 25341da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg) 25351da177e4SLinus Torvalds { 25361da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 25371da177e4SLinus Torvalds struct inet6_dev *idev; 25381da177e4SLinus Torvalds 25391da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 25401da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 25411da177e4SLinus Torvalds We still use this lock to block changes 25421da177e4SLinus Torvalds caused by addrconf/ndisc. 25431da177e4SLinus Torvalds */ 25441da177e4SLinus Torvalds 25451da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 254638308473SDavid S. Miller if (!idev) 25471da177e4SLinus Torvalds return 0; 25481da177e4SLinus Torvalds 25491da177e4SLinus Torvalds /* For administrative MTU increase, there is no way to discover 25501da177e4SLinus Torvalds IPv6 PMTU increase, so PMTU increase should be updated here. 25511da177e4SLinus Torvalds Since RFC 1981 doesn't include administrative MTU increase 25521da177e4SLinus Torvalds update PMTU increase is a MUST. (i.e. jumbo frame) 25531da177e4SLinus Torvalds */ 25541da177e4SLinus Torvalds /* 25551da177e4SLinus Torvalds If new MTU is less than route PMTU, this new MTU will be the 25561da177e4SLinus Torvalds lowest MTU in the path, update the route PMTU to reflect PMTU 25571da177e4SLinus Torvalds decreases; if new MTU is greater than route PMTU, and the 25581da177e4SLinus Torvalds old MTU is the lowest MTU in the path, update the route PMTU 25591da177e4SLinus Torvalds to reflect the increase. In this case if the other nodes' MTU 25601da177e4SLinus Torvalds also have the lowest MTU, TOO BIG MESSAGE will be lead to 25611da177e4SLinus Torvalds PMTU discouvery. 25621da177e4SLinus Torvalds */ 2563d1918542SDavid S. Miller if (rt->dst.dev == arg->dev && 25644b32b5adSMartin KaFai Lau !dst_metric_locked(&rt->dst, RTAX_MTU)) { 25654b32b5adSMartin KaFai Lau if (rt->rt6i_flags & RTF_CACHE) { 25664b32b5adSMartin KaFai Lau /* For RTF_CACHE with rt6i_pmtu == 0 25674b32b5adSMartin KaFai Lau * (i.e. a redirected route), 25684b32b5adSMartin KaFai Lau * the metrics of its rt->dst.from has already 25694b32b5adSMartin KaFai Lau * been updated. 25704b32b5adSMartin KaFai Lau */ 25714b32b5adSMartin KaFai Lau if (rt->rt6i_pmtu && rt->rt6i_pmtu > arg->mtu) 25724b32b5adSMartin KaFai Lau rt->rt6i_pmtu = arg->mtu; 25734b32b5adSMartin KaFai Lau } else if (dst_mtu(&rt->dst) >= arg->mtu || 2574d8d1f30bSChangli Gao (dst_mtu(&rt->dst) < arg->mtu && 25754b32b5adSMartin KaFai Lau dst_mtu(&rt->dst) == idev->cnf.mtu6)) { 2576defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu); 2577566cfd8fSSimon Arlott } 25784b32b5adSMartin KaFai Lau } 25791da177e4SLinus Torvalds return 0; 25801da177e4SLinus Torvalds } 25811da177e4SLinus Torvalds 258295c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu) 25831da177e4SLinus Torvalds { 2584c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 2585c71099acSThomas Graf .dev = dev, 2586c71099acSThomas Graf .mtu = mtu, 2587c71099acSThomas Graf }; 25881da177e4SLinus Torvalds 25890c3584d5SLi RongQing fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); 25901da177e4SLinus Torvalds } 25911da177e4SLinus Torvalds 2592ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 25935176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 259486872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 2595ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 259686872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 259786872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 259851ebd318SNicolas Dichtel [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 2599c78ba6d6SLubomir Rintel [RTA_PREF] = { .type = NLA_U8 }, 260086872cb5SThomas Graf }; 260186872cb5SThomas Graf 260286872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 260386872cb5SThomas Graf struct fib6_config *cfg) 26041da177e4SLinus Torvalds { 260586872cb5SThomas Graf struct rtmsg *rtm; 260686872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 2607c78ba6d6SLubomir Rintel unsigned int pref; 260886872cb5SThomas Graf int err; 26091da177e4SLinus Torvalds 261086872cb5SThomas Graf err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy); 261186872cb5SThomas Graf if (err < 0) 261286872cb5SThomas Graf goto errout; 26131da177e4SLinus Torvalds 261486872cb5SThomas Graf err = -EINVAL; 261586872cb5SThomas Graf rtm = nlmsg_data(nlh); 261686872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 261786872cb5SThomas Graf 261886872cb5SThomas Graf cfg->fc_table = rtm->rtm_table; 261986872cb5SThomas Graf cfg->fc_dst_len = rtm->rtm_dst_len; 262086872cb5SThomas Graf cfg->fc_src_len = rtm->rtm_src_len; 262186872cb5SThomas Graf cfg->fc_flags = RTF_UP; 262286872cb5SThomas Graf cfg->fc_protocol = rtm->rtm_protocol; 2623ef2c7d7bSNicolas Dichtel cfg->fc_type = rtm->rtm_type; 262486872cb5SThomas Graf 2625ef2c7d7bSNicolas Dichtel if (rtm->rtm_type == RTN_UNREACHABLE || 2626ef2c7d7bSNicolas Dichtel rtm->rtm_type == RTN_BLACKHOLE || 2627b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_PROHIBIT || 2628b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_THROW) 262986872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 263086872cb5SThomas Graf 2631ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 2632ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 2633ab79ad14SMaciej Żenczykowski 26341f56a01fSMartin KaFai Lau if (rtm->rtm_flags & RTM_F_CLONED) 26351f56a01fSMartin KaFai Lau cfg->fc_flags |= RTF_CACHE; 26361f56a01fSMartin KaFai Lau 263715e47304SEric W. Biederman cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid; 263886872cb5SThomas Graf cfg->fc_nlinfo.nlh = nlh; 26393b1e0a65SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = sock_net(skb->sk); 264086872cb5SThomas Graf 264186872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 264267b61f6cSJiri Benc cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]); 264386872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 26441da177e4SLinus Torvalds } 264586872cb5SThomas Graf 264686872cb5SThomas Graf if (tb[RTA_DST]) { 264786872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 264886872cb5SThomas Graf 264986872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 265086872cb5SThomas Graf goto errout; 265186872cb5SThomas Graf 265286872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 26531da177e4SLinus Torvalds } 265486872cb5SThomas Graf 265586872cb5SThomas Graf if (tb[RTA_SRC]) { 265686872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 265786872cb5SThomas Graf 265886872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 265986872cb5SThomas Graf goto errout; 266086872cb5SThomas Graf 266186872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 26621da177e4SLinus Torvalds } 266386872cb5SThomas Graf 2664c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 266567b61f6cSJiri Benc cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]); 2666c3968a85SDaniel Walter 266786872cb5SThomas Graf if (tb[RTA_OIF]) 266886872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 266986872cb5SThomas Graf 267086872cb5SThomas Graf if (tb[RTA_PRIORITY]) 267186872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 267286872cb5SThomas Graf 267386872cb5SThomas Graf if (tb[RTA_METRICS]) { 267486872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 267586872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 26761da177e4SLinus Torvalds } 267786872cb5SThomas Graf 267886872cb5SThomas Graf if (tb[RTA_TABLE]) 267986872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 268086872cb5SThomas Graf 268151ebd318SNicolas Dichtel if (tb[RTA_MULTIPATH]) { 268251ebd318SNicolas Dichtel cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); 268351ebd318SNicolas Dichtel cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); 268451ebd318SNicolas Dichtel } 268551ebd318SNicolas Dichtel 2686c78ba6d6SLubomir Rintel if (tb[RTA_PREF]) { 2687c78ba6d6SLubomir Rintel pref = nla_get_u8(tb[RTA_PREF]); 2688c78ba6d6SLubomir Rintel if (pref != ICMPV6_ROUTER_PREF_LOW && 2689c78ba6d6SLubomir Rintel pref != ICMPV6_ROUTER_PREF_HIGH) 2690c78ba6d6SLubomir Rintel pref = ICMPV6_ROUTER_PREF_MEDIUM; 2691c78ba6d6SLubomir Rintel cfg->fc_flags |= RTF_PREF(pref); 2692c78ba6d6SLubomir Rintel } 2693c78ba6d6SLubomir Rintel 269486872cb5SThomas Graf err = 0; 269586872cb5SThomas Graf errout: 269686872cb5SThomas Graf return err; 26971da177e4SLinus Torvalds } 26981da177e4SLinus Torvalds 269951ebd318SNicolas Dichtel static int ip6_route_multipath(struct fib6_config *cfg, int add) 270051ebd318SNicolas Dichtel { 270151ebd318SNicolas Dichtel struct fib6_config r_cfg; 270251ebd318SNicolas Dichtel struct rtnexthop *rtnh; 270351ebd318SNicolas Dichtel int remaining; 270451ebd318SNicolas Dichtel int attrlen; 270551ebd318SNicolas Dichtel int err = 0, last_err = 0; 270651ebd318SNicolas Dichtel 270735f1b4e9SMichal Kubeček remaining = cfg->fc_mp_len; 270851ebd318SNicolas Dichtel beginning: 270951ebd318SNicolas Dichtel rtnh = (struct rtnexthop *)cfg->fc_mp; 271051ebd318SNicolas Dichtel 271151ebd318SNicolas Dichtel /* Parse a Multipath Entry */ 271251ebd318SNicolas Dichtel while (rtnh_ok(rtnh, remaining)) { 271351ebd318SNicolas Dichtel memcpy(&r_cfg, cfg, sizeof(*cfg)); 271451ebd318SNicolas Dichtel if (rtnh->rtnh_ifindex) 271551ebd318SNicolas Dichtel r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 271651ebd318SNicolas Dichtel 271751ebd318SNicolas Dichtel attrlen = rtnh_attrlen(rtnh); 271851ebd318SNicolas Dichtel if (attrlen > 0) { 271951ebd318SNicolas Dichtel struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 272051ebd318SNicolas Dichtel 272151ebd318SNicolas Dichtel nla = nla_find(attrs, attrlen, RTA_GATEWAY); 272251ebd318SNicolas Dichtel if (nla) { 272367b61f6cSJiri Benc r_cfg.fc_gateway = nla_get_in6_addr(nla); 272451ebd318SNicolas Dichtel r_cfg.fc_flags |= RTF_GATEWAY; 272551ebd318SNicolas Dichtel } 272651ebd318SNicolas Dichtel } 272751ebd318SNicolas Dichtel err = add ? ip6_route_add(&r_cfg) : ip6_route_del(&r_cfg); 272851ebd318SNicolas Dichtel if (err) { 272951ebd318SNicolas Dichtel last_err = err; 273051ebd318SNicolas Dichtel /* If we are trying to remove a route, do not stop the 273151ebd318SNicolas Dichtel * loop when ip6_route_del() fails (because next hop is 273251ebd318SNicolas Dichtel * already gone), we should try to remove all next hops. 273351ebd318SNicolas Dichtel */ 273451ebd318SNicolas Dichtel if (add) { 273551ebd318SNicolas Dichtel /* If add fails, we should try to delete all 273651ebd318SNicolas Dichtel * next hops that have been already added. 273751ebd318SNicolas Dichtel */ 273851ebd318SNicolas Dichtel add = 0; 273935f1b4e9SMichal Kubeček remaining = cfg->fc_mp_len - remaining; 274051ebd318SNicolas Dichtel goto beginning; 274151ebd318SNicolas Dichtel } 274251ebd318SNicolas Dichtel } 27431a72418bSNicolas Dichtel /* Because each route is added like a single route we remove 274427596472SMichal Kubeček * these flags after the first nexthop: if there is a collision, 274527596472SMichal Kubeček * we have already failed to add the first nexthop: 274627596472SMichal Kubeček * fib6_add_rt2node() has rejected it; when replacing, old 274727596472SMichal Kubeček * nexthops have been replaced by first new, the rest should 274827596472SMichal Kubeček * be added to it. 27491a72418bSNicolas Dichtel */ 275027596472SMichal Kubeček cfg->fc_nlinfo.nlh->nlmsg_flags &= ~(NLM_F_EXCL | 275127596472SMichal Kubeček NLM_F_REPLACE); 275251ebd318SNicolas Dichtel rtnh = rtnh_next(rtnh, &remaining); 275351ebd318SNicolas Dichtel } 275451ebd318SNicolas Dichtel 275551ebd318SNicolas Dichtel return last_err; 275651ebd318SNicolas Dichtel } 275751ebd318SNicolas Dichtel 2758661d2967SThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh) 27591da177e4SLinus Torvalds { 276086872cb5SThomas Graf struct fib6_config cfg; 276186872cb5SThomas Graf int err; 27621da177e4SLinus Torvalds 276386872cb5SThomas Graf err = rtm_to_fib6_config(skb, nlh, &cfg); 276486872cb5SThomas Graf if (err < 0) 276586872cb5SThomas Graf return err; 276686872cb5SThomas Graf 276751ebd318SNicolas Dichtel if (cfg.fc_mp) 276851ebd318SNicolas Dichtel return ip6_route_multipath(&cfg, 0); 276951ebd318SNicolas Dichtel else 277086872cb5SThomas Graf return ip6_route_del(&cfg); 27711da177e4SLinus Torvalds } 27721da177e4SLinus Torvalds 2773661d2967SThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh) 27741da177e4SLinus Torvalds { 277586872cb5SThomas Graf struct fib6_config cfg; 277686872cb5SThomas Graf int err; 27771da177e4SLinus Torvalds 277886872cb5SThomas Graf err = rtm_to_fib6_config(skb, nlh, &cfg); 277986872cb5SThomas Graf if (err < 0) 278086872cb5SThomas Graf return err; 278186872cb5SThomas Graf 278251ebd318SNicolas Dichtel if (cfg.fc_mp) 278351ebd318SNicolas Dichtel return ip6_route_multipath(&cfg, 1); 278451ebd318SNicolas Dichtel else 278586872cb5SThomas Graf return ip6_route_add(&cfg); 27861da177e4SLinus Torvalds } 27871da177e4SLinus Torvalds 2788339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void) 2789339bf98fSThomas Graf { 2790339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 2791339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 2792339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 2793339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 2794339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 2795339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 2796339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 2797339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 2798339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 27996a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 2800ea697639SDaniel Borkmann + nla_total_size(sizeof(struct rta_cacheinfo)) 2801c78ba6d6SLubomir Rintel + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */ 2802c78ba6d6SLubomir Rintel + nla_total_size(1); /* RTA_PREF */ 2803339bf98fSThomas Graf } 2804339bf98fSThomas Graf 2805191cd582SBrian Haley static int rt6_fill_node(struct net *net, 2806191cd582SBrian Haley struct sk_buff *skb, struct rt6_info *rt, 28070d51aa80SJamal Hadi Salim struct in6_addr *dst, struct in6_addr *src, 280815e47304SEric W. Biederman int iif, int type, u32 portid, u32 seq, 28097bc570c8SYOSHIFUJI Hideaki int prefix, int nowait, unsigned int flags) 28101da177e4SLinus Torvalds { 28114b32b5adSMartin KaFai Lau u32 metrics[RTAX_MAX]; 28121da177e4SLinus Torvalds struct rtmsg *rtm; 28131da177e4SLinus Torvalds struct nlmsghdr *nlh; 2814e3703b3dSThomas Graf long expires; 28159e762a4aSPatrick McHardy u32 table; 28161da177e4SLinus Torvalds 28171da177e4SLinus Torvalds if (prefix) { /* user wants prefix routes only */ 28181da177e4SLinus Torvalds if (!(rt->rt6i_flags & RTF_PREFIX_RT)) { 28191da177e4SLinus Torvalds /* success since this is not a prefix route */ 28201da177e4SLinus Torvalds return 1; 28211da177e4SLinus Torvalds } 28221da177e4SLinus Torvalds } 28231da177e4SLinus Torvalds 282415e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); 282538308473SDavid S. Miller if (!nlh) 282626932566SPatrick McHardy return -EMSGSIZE; 28272d7202bfSThomas Graf 28282d7202bfSThomas Graf rtm = nlmsg_data(nlh); 28291da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 28301da177e4SLinus Torvalds rtm->rtm_dst_len = rt->rt6i_dst.plen; 28311da177e4SLinus Torvalds rtm->rtm_src_len = rt->rt6i_src.plen; 28321da177e4SLinus Torvalds rtm->rtm_tos = 0; 2833c71099acSThomas Graf if (rt->rt6i_table) 28349e762a4aSPatrick McHardy table = rt->rt6i_table->tb6_id; 2835c71099acSThomas Graf else 28369e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 28379e762a4aSPatrick McHardy rtm->rtm_table = table; 2838c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, table)) 2839c78679e8SDavid S. Miller goto nla_put_failure; 2840ef2c7d7bSNicolas Dichtel if (rt->rt6i_flags & RTF_REJECT) { 2841ef2c7d7bSNicolas Dichtel switch (rt->dst.error) { 2842ef2c7d7bSNicolas Dichtel case -EINVAL: 2843ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_BLACKHOLE; 2844ef2c7d7bSNicolas Dichtel break; 2845ef2c7d7bSNicolas Dichtel case -EACCES: 2846ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_PROHIBIT; 2847ef2c7d7bSNicolas Dichtel break; 2848b4949ab2SNicolas Dichtel case -EAGAIN: 2849b4949ab2SNicolas Dichtel rtm->rtm_type = RTN_THROW; 2850b4949ab2SNicolas Dichtel break; 2851ef2c7d7bSNicolas Dichtel default: 28521da177e4SLinus Torvalds rtm->rtm_type = RTN_UNREACHABLE; 2853ef2c7d7bSNicolas Dichtel break; 2854ef2c7d7bSNicolas Dichtel } 2855ef2c7d7bSNicolas Dichtel } 2856ab79ad14SMaciej Żenczykowski else if (rt->rt6i_flags & RTF_LOCAL) 2857ab79ad14SMaciej Żenczykowski rtm->rtm_type = RTN_LOCAL; 2858d1918542SDavid S. Miller else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK)) 28591da177e4SLinus Torvalds rtm->rtm_type = RTN_LOCAL; 28601da177e4SLinus Torvalds else 28611da177e4SLinus Torvalds rtm->rtm_type = RTN_UNICAST; 28621da177e4SLinus Torvalds rtm->rtm_flags = 0; 28631da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 28641da177e4SLinus Torvalds rtm->rtm_protocol = rt->rt6i_protocol; 28651da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_DYNAMIC) 28661da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_REDIRECT; 2867f0396f60SDenis Ovsienko else if (rt->rt6i_flags & RTF_ADDRCONF) { 2868f0396f60SDenis Ovsienko if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO)) 28691da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_RA; 2870f0396f60SDenis Ovsienko else 2871f0396f60SDenis Ovsienko rtm->rtm_protocol = RTPROT_KERNEL; 2872f0396f60SDenis Ovsienko } 28731da177e4SLinus Torvalds 28741da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) 28751da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 28761da177e4SLinus Torvalds 28771da177e4SLinus Torvalds if (dst) { 2878930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_DST, dst)) 2879c78679e8SDavid S. Miller goto nla_put_failure; 28801da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 28811da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 2882930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr)) 2883c78679e8SDavid S. Miller goto nla_put_failure; 28841da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 28851da177e4SLinus Torvalds if (src) { 2886930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_SRC, src)) 2887c78679e8SDavid S. Miller goto nla_put_failure; 28881da177e4SLinus Torvalds rtm->rtm_src_len = 128; 2889c78679e8SDavid S. Miller } else if (rtm->rtm_src_len && 2890930345eaSJiri Benc nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr)) 2891c78679e8SDavid S. Miller goto nla_put_failure; 28921da177e4SLinus Torvalds #endif 28937bc570c8SYOSHIFUJI Hideaki if (iif) { 28947bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 28957bc570c8SYOSHIFUJI Hideaki if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) { 28968229efdaSBenjamin Thery int err = ip6mr_get_route(net, skb, rtm, nowait); 28977bc570c8SYOSHIFUJI Hideaki if (err <= 0) { 28987bc570c8SYOSHIFUJI Hideaki if (!nowait) { 28997bc570c8SYOSHIFUJI Hideaki if (err == 0) 29007bc570c8SYOSHIFUJI Hideaki return 0; 29017bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 29027bc570c8SYOSHIFUJI Hideaki } else { 29037bc570c8SYOSHIFUJI Hideaki if (err == -EMSGSIZE) 29047bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 29057bc570c8SYOSHIFUJI Hideaki } 29067bc570c8SYOSHIFUJI Hideaki } 29077bc570c8SYOSHIFUJI Hideaki } else 29087bc570c8SYOSHIFUJI Hideaki #endif 2909c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_IIF, iif)) 2910c78679e8SDavid S. Miller goto nla_put_failure; 29117bc570c8SYOSHIFUJI Hideaki } else if (dst) { 29121da177e4SLinus Torvalds struct in6_addr saddr_buf; 2913c78679e8SDavid S. Miller if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 && 2914930345eaSJiri Benc nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 2915c78679e8SDavid S. Miller goto nla_put_failure; 2916c3968a85SDaniel Walter } 2917c3968a85SDaniel Walter 2918c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) { 2919c3968a85SDaniel Walter struct in6_addr saddr_buf; 29204e3fd7a0SAlexey Dobriyan saddr_buf = rt->rt6i_prefsrc.addr; 2921930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf)) 2922c78679e8SDavid S. Miller goto nla_put_failure; 29231da177e4SLinus Torvalds } 29242d7202bfSThomas Graf 29254b32b5adSMartin KaFai Lau memcpy(metrics, dst_metrics_ptr(&rt->dst), sizeof(metrics)); 29264b32b5adSMartin KaFai Lau if (rt->rt6i_pmtu) 29274b32b5adSMartin KaFai Lau metrics[RTAX_MTU - 1] = rt->rt6i_pmtu; 29284b32b5adSMartin KaFai Lau if (rtnetlink_put_metrics(skb, metrics) < 0) 29292d7202bfSThomas Graf goto nla_put_failure; 29302d7202bfSThomas Graf 2931dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_GATEWAY) { 2932930345eaSJiri Benc if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0) 293394f826b8SEric Dumazet goto nla_put_failure; 293494f826b8SEric Dumazet } 29352d7202bfSThomas Graf 2936c78679e8SDavid S. Miller if (rt->dst.dev && 2937c78679e8SDavid S. Miller nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex)) 2938c78679e8SDavid S. Miller goto nla_put_failure; 2939c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric)) 2940c78679e8SDavid S. Miller goto nla_put_failure; 29418253947eSLi Wei 29428253947eSLi Wei expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0; 294369cdf8f9SYOSHIFUJI Hideaki 294487a50699SDavid S. Miller if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0) 2945e3703b3dSThomas Graf goto nla_put_failure; 29461da177e4SLinus Torvalds 2947c78ba6d6SLubomir Rintel if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags))) 2948c78ba6d6SLubomir Rintel goto nla_put_failure; 2949c78ba6d6SLubomir Rintel 2950053c095aSJohannes Berg nlmsg_end(skb, nlh); 2951053c095aSJohannes Berg return 0; 29522d7202bfSThomas Graf 29532d7202bfSThomas Graf nla_put_failure: 295426932566SPatrick McHardy nlmsg_cancel(skb, nlh); 295526932566SPatrick McHardy return -EMSGSIZE; 29561da177e4SLinus Torvalds } 29571da177e4SLinus Torvalds 29581b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg) 29591da177e4SLinus Torvalds { 29601da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 29611da177e4SLinus Torvalds int prefix; 29621da177e4SLinus Torvalds 29632d7202bfSThomas Graf if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { 29642d7202bfSThomas Graf struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); 29651da177e4SLinus Torvalds prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0; 29661da177e4SLinus Torvalds } else 29671da177e4SLinus Torvalds prefix = 0; 29681da177e4SLinus Torvalds 2969191cd582SBrian Haley return rt6_fill_node(arg->net, 2970191cd582SBrian Haley arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE, 297115e47304SEric W. Biederman NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq, 29727bc570c8SYOSHIFUJI Hideaki prefix, 0, NLM_F_MULTI); 29731da177e4SLinus Torvalds } 29741da177e4SLinus Torvalds 2975661d2967SThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh) 29761da177e4SLinus Torvalds { 29773b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 2978ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 29791da177e4SLinus Torvalds struct rt6_info *rt; 2980ab364a6fSThomas Graf struct sk_buff *skb; 2981ab364a6fSThomas Graf struct rtmsg *rtm; 29824c9483b2SDavid S. Miller struct flowi6 fl6; 298372331bc0SShmulik Ladkani int err, iif = 0, oif = 0; 2984ab364a6fSThomas Graf 2985ab364a6fSThomas Graf err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy); 2986ab364a6fSThomas Graf if (err < 0) 2987ab364a6fSThomas Graf goto errout; 2988ab364a6fSThomas Graf 2989ab364a6fSThomas Graf err = -EINVAL; 29904c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 2991ab364a6fSThomas Graf 2992ab364a6fSThomas Graf if (tb[RTA_SRC]) { 2993ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 2994ab364a6fSThomas Graf goto errout; 2995ab364a6fSThomas Graf 29964e3fd7a0SAlexey Dobriyan fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 2997ab364a6fSThomas Graf } 2998ab364a6fSThomas Graf 2999ab364a6fSThomas Graf if (tb[RTA_DST]) { 3000ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 3001ab364a6fSThomas Graf goto errout; 3002ab364a6fSThomas Graf 30034e3fd7a0SAlexey Dobriyan fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 3004ab364a6fSThomas Graf } 3005ab364a6fSThomas Graf 3006ab364a6fSThomas Graf if (tb[RTA_IIF]) 3007ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 3008ab364a6fSThomas Graf 3009ab364a6fSThomas Graf if (tb[RTA_OIF]) 301072331bc0SShmulik Ladkani oif = nla_get_u32(tb[RTA_OIF]); 3011ab364a6fSThomas Graf 30122e47b291SLorenzo Colitti if (tb[RTA_MARK]) 30132e47b291SLorenzo Colitti fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); 30142e47b291SLorenzo Colitti 3015ab364a6fSThomas Graf if (iif) { 3016ab364a6fSThomas Graf struct net_device *dev; 301772331bc0SShmulik Ladkani int flags = 0; 301872331bc0SShmulik Ladkani 30195578689aSDaniel Lezcano dev = __dev_get_by_index(net, iif); 3020ab364a6fSThomas Graf if (!dev) { 3021ab364a6fSThomas Graf err = -ENODEV; 3022ab364a6fSThomas Graf goto errout; 3023ab364a6fSThomas Graf } 302472331bc0SShmulik Ladkani 302572331bc0SShmulik Ladkani fl6.flowi6_iif = iif; 302672331bc0SShmulik Ladkani 302772331bc0SShmulik Ladkani if (!ipv6_addr_any(&fl6.saddr)) 302872331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_HAS_SADDR; 302972331bc0SShmulik Ladkani 303072331bc0SShmulik Ladkani rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6, 303172331bc0SShmulik Ladkani flags); 303272331bc0SShmulik Ladkani } else { 303372331bc0SShmulik Ladkani fl6.flowi6_oif = oif; 303472331bc0SShmulik Ladkani 303572331bc0SShmulik Ladkani rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6); 3036ab364a6fSThomas Graf } 30371da177e4SLinus Torvalds 30381da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 303938308473SDavid S. Miller if (!skb) { 304094e187c0SAmerigo Wang ip6_rt_put(rt); 3041ab364a6fSThomas Graf err = -ENOBUFS; 3042ab364a6fSThomas Graf goto errout; 3043ab364a6fSThomas Graf } 30441da177e4SLinus Torvalds 30451da177e4SLinus Torvalds /* Reserve room for dummy headers, this skb can pass 30461da177e4SLinus Torvalds through good chunk of routing engine. 30471da177e4SLinus Torvalds */ 3048459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 30491da177e4SLinus Torvalds skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr)); 30501da177e4SLinus Torvalds 3051d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 30521da177e4SLinus Torvalds 30534c9483b2SDavid S. Miller err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif, 305415e47304SEric W. Biederman RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 30557bc570c8SYOSHIFUJI Hideaki nlh->nlmsg_seq, 0, 0, 0); 30561da177e4SLinus Torvalds if (err < 0) { 3057ab364a6fSThomas Graf kfree_skb(skb); 3058ab364a6fSThomas Graf goto errout; 30591da177e4SLinus Torvalds } 30601da177e4SLinus Torvalds 306115e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 3062ab364a6fSThomas Graf errout: 30631da177e4SLinus Torvalds return err; 30641da177e4SLinus Torvalds } 30651da177e4SLinus Torvalds 306686872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info) 30671da177e4SLinus Torvalds { 30681da177e4SLinus Torvalds struct sk_buff *skb; 30695578689aSDaniel Lezcano struct net *net = info->nl_net; 3070528c4cebSDenis V. Lunev u32 seq; 3071528c4cebSDenis V. Lunev int err; 30720d51aa80SJamal Hadi Salim 3073528c4cebSDenis V. Lunev err = -ENOBUFS; 307438308473SDavid S. Miller seq = info->nlh ? info->nlh->nlmsg_seq : 0; 307586872cb5SThomas Graf 3076339bf98fSThomas Graf skb = nlmsg_new(rt6_nlmsg_size(), gfp_any()); 307738308473SDavid S. Miller if (!skb) 307821713ebcSThomas Graf goto errout; 30791da177e4SLinus Torvalds 3080191cd582SBrian Haley err = rt6_fill_node(net, skb, rt, NULL, NULL, 0, 308115e47304SEric W. Biederman event, info->portid, seq, 0, 0, 0); 308226932566SPatrick McHardy if (err < 0) { 308326932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 308426932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 308526932566SPatrick McHardy kfree_skb(skb); 308626932566SPatrick McHardy goto errout; 308726932566SPatrick McHardy } 308815e47304SEric W. Biederman rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 30895578689aSDaniel Lezcano info->nlh, gfp_any()); 30901ce85fe4SPablo Neira Ayuso return; 309121713ebcSThomas Graf errout: 309221713ebcSThomas Graf if (err < 0) 30935578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 30941da177e4SLinus Torvalds } 30951da177e4SLinus Torvalds 30968ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 3097351638e7SJiri Pirko unsigned long event, void *ptr) 30988ed67789SDaniel Lezcano { 3099351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 3100c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 31018ed67789SDaniel Lezcano 31028ed67789SDaniel Lezcano if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) { 3103d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 31048ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 31058ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 3106d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 31078ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 3108d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 31098ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 31108ed67789SDaniel Lezcano #endif 31118ed67789SDaniel Lezcano } 31128ed67789SDaniel Lezcano 31138ed67789SDaniel Lezcano return NOTIFY_OK; 31148ed67789SDaniel Lezcano } 31158ed67789SDaniel Lezcano 31161da177e4SLinus Torvalds /* 31171da177e4SLinus Torvalds * /proc 31181da177e4SLinus Torvalds */ 31191da177e4SLinus Torvalds 31201da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 31211da177e4SLinus Torvalds 312233120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = { 312333120b30SAlexey Dobriyan .owner = THIS_MODULE, 312433120b30SAlexey Dobriyan .open = ipv6_route_open, 312533120b30SAlexey Dobriyan .read = seq_read, 312633120b30SAlexey Dobriyan .llseek = seq_lseek, 31278d2ca1d7SHannes Frederic Sowa .release = seq_release_net, 312833120b30SAlexey Dobriyan }; 312933120b30SAlexey Dobriyan 31301da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 31311da177e4SLinus Torvalds { 313269ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 31331da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 313469ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 313569ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 313669ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_alloc, 313769ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 313869ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 3139fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 314069ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 31411da177e4SLinus Torvalds 31421da177e4SLinus Torvalds return 0; 31431da177e4SLinus Torvalds } 31441da177e4SLinus Torvalds 31451da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file) 31461da177e4SLinus Torvalds { 3147de05c557SPavel Emelyanov return single_open_net(inode, file, rt6_stats_seq_show); 314869ddb805SDaniel Lezcano } 314969ddb805SDaniel Lezcano 31509a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = { 31511da177e4SLinus Torvalds .owner = THIS_MODULE, 31521da177e4SLinus Torvalds .open = rt6_stats_seq_open, 31531da177e4SLinus Torvalds .read = seq_read, 31541da177e4SLinus Torvalds .llseek = seq_lseek, 3155b6fcbdb4SPavel Emelyanov .release = single_release_net, 31561da177e4SLinus Torvalds }; 31571da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 31581da177e4SLinus Torvalds 31591da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 31601da177e4SLinus Torvalds 31611da177e4SLinus Torvalds static 3162fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, 31631da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 31641da177e4SLinus Torvalds { 3165c486da34SLucian Adrian Grijincu struct net *net; 3166c486da34SLucian Adrian Grijincu int delay; 3167c486da34SLucian Adrian Grijincu if (!write) 3168c486da34SLucian Adrian Grijincu return -EINVAL; 3169c486da34SLucian Adrian Grijincu 3170c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 3171c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 31728d65af78SAlexey Dobriyan proc_dointvec(ctl, write, buffer, lenp, ppos); 31732ac3ac8fSMichal Kubeček fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0); 31741da177e4SLinus Torvalds return 0; 31751da177e4SLinus Torvalds } 31761da177e4SLinus Torvalds 3177fe2c6338SJoe Perches struct ctl_table ipv6_route_table_template[] = { 31781da177e4SLinus Torvalds { 31791da177e4SLinus Torvalds .procname = "flush", 31804990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 31811da177e4SLinus Torvalds .maxlen = sizeof(int), 318289c8b3a1SDave Jones .mode = 0200, 31836d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 31841da177e4SLinus Torvalds }, 31851da177e4SLinus Torvalds { 31861da177e4SLinus Torvalds .procname = "gc_thresh", 31879a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 31881da177e4SLinus Torvalds .maxlen = sizeof(int), 31891da177e4SLinus Torvalds .mode = 0644, 31906d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 31911da177e4SLinus Torvalds }, 31921da177e4SLinus Torvalds { 31931da177e4SLinus Torvalds .procname = "max_size", 31944990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 31951da177e4SLinus Torvalds .maxlen = sizeof(int), 31961da177e4SLinus Torvalds .mode = 0644, 31976d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 31981da177e4SLinus Torvalds }, 31991da177e4SLinus Torvalds { 32001da177e4SLinus Torvalds .procname = "gc_min_interval", 32014990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 32021da177e4SLinus Torvalds .maxlen = sizeof(int), 32031da177e4SLinus Torvalds .mode = 0644, 32046d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 32051da177e4SLinus Torvalds }, 32061da177e4SLinus Torvalds { 32071da177e4SLinus Torvalds .procname = "gc_timeout", 32084990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 32091da177e4SLinus Torvalds .maxlen = sizeof(int), 32101da177e4SLinus Torvalds .mode = 0644, 32116d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 32121da177e4SLinus Torvalds }, 32131da177e4SLinus Torvalds { 32141da177e4SLinus Torvalds .procname = "gc_interval", 32154990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 32161da177e4SLinus Torvalds .maxlen = sizeof(int), 32171da177e4SLinus Torvalds .mode = 0644, 32186d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 32191da177e4SLinus Torvalds }, 32201da177e4SLinus Torvalds { 32211da177e4SLinus Torvalds .procname = "gc_elasticity", 32224990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 32231da177e4SLinus Torvalds .maxlen = sizeof(int), 32241da177e4SLinus Torvalds .mode = 0644, 3225f3d3f616SMin Zhang .proc_handler = proc_dointvec, 32261da177e4SLinus Torvalds }, 32271da177e4SLinus Torvalds { 32281da177e4SLinus Torvalds .procname = "mtu_expires", 32294990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 32301da177e4SLinus Torvalds .maxlen = sizeof(int), 32311da177e4SLinus Torvalds .mode = 0644, 32326d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 32331da177e4SLinus Torvalds }, 32341da177e4SLinus Torvalds { 32351da177e4SLinus Torvalds .procname = "min_adv_mss", 32364990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 32371da177e4SLinus Torvalds .maxlen = sizeof(int), 32381da177e4SLinus Torvalds .mode = 0644, 3239f3d3f616SMin Zhang .proc_handler = proc_dointvec, 32401da177e4SLinus Torvalds }, 32411da177e4SLinus Torvalds { 32421da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 32434990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 32441da177e4SLinus Torvalds .maxlen = sizeof(int), 32451da177e4SLinus Torvalds .mode = 0644, 32466d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 32471da177e4SLinus Torvalds }, 3248f8572d8fSEric W. Biederman { } 32491da177e4SLinus Torvalds }; 32501da177e4SLinus Torvalds 32512c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 3252760f2d01SDaniel Lezcano { 3253760f2d01SDaniel Lezcano struct ctl_table *table; 3254760f2d01SDaniel Lezcano 3255760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 3256760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 3257760f2d01SDaniel Lezcano GFP_KERNEL); 32585ee09105SYOSHIFUJI Hideaki 32595ee09105SYOSHIFUJI Hideaki if (table) { 32605ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 3261c486da34SLucian Adrian Grijincu table[0].extra1 = net; 326286393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 32635ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 32645ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 32655ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 32665ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 32675ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 32685ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 32695ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 32709c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 3271464dc801SEric W. Biederman 3272464dc801SEric W. Biederman /* Don't export sysctls to unprivileged users */ 3273464dc801SEric W. Biederman if (net->user_ns != &init_user_ns) 3274464dc801SEric W. Biederman table[0].procname = NULL; 32755ee09105SYOSHIFUJI Hideaki } 32765ee09105SYOSHIFUJI Hideaki 3277760f2d01SDaniel Lezcano return table; 3278760f2d01SDaniel Lezcano } 32791da177e4SLinus Torvalds #endif 32801da177e4SLinus Torvalds 32812c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 3282cdb18761SDaniel Lezcano { 3283633d424bSPavel Emelyanov int ret = -ENOMEM; 32848ed67789SDaniel Lezcano 328586393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 328686393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 3287f2fc6a54SBenjamin Thery 3288fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 3289fc66f95cSEric Dumazet goto out_ip6_dst_ops; 3290fc66f95cSEric Dumazet 32918ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 32928ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 32938ed67789SDaniel Lezcano GFP_KERNEL); 32948ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 3295fc66f95cSEric Dumazet goto out_ip6_dst_entries; 3296d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.path = 32978ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_null_entry; 3298d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 329962fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 330062fa8a84SDavid S. Miller ip6_template_metrics, true); 33018ed67789SDaniel Lezcano 33028ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 33038ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 33048ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 33058ed67789SDaniel Lezcano GFP_KERNEL); 330668fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 330768fffc67SPeter Zijlstra goto out_ip6_null_entry; 3308d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.path = 33098ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_prohibit_entry; 3310d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 331162fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 331262fa8a84SDavid S. Miller ip6_template_metrics, true); 33138ed67789SDaniel Lezcano 33148ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 33158ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 33168ed67789SDaniel Lezcano GFP_KERNEL); 331768fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 331868fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 3319d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.path = 33208ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_blk_hole_entry; 3321d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 332262fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 332362fa8a84SDavid S. Miller ip6_template_metrics, true); 33248ed67789SDaniel Lezcano #endif 33258ed67789SDaniel Lezcano 3326b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 3327b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 3328b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 3329b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 3330b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 3331b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 3332b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 3333b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 3334b339a47cSPeter Zijlstra 33356891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 33366891a346SBenjamin Thery 33378ed67789SDaniel Lezcano ret = 0; 33388ed67789SDaniel Lezcano out: 33398ed67789SDaniel Lezcano return ret; 3340f2fc6a54SBenjamin Thery 334168fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 334268fffc67SPeter Zijlstra out_ip6_prohibit_entry: 334368fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 334468fffc67SPeter Zijlstra out_ip6_null_entry: 334568fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 334668fffc67SPeter Zijlstra #endif 3347fc66f95cSEric Dumazet out_ip6_dst_entries: 3348fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 3349f2fc6a54SBenjamin Thery out_ip6_dst_ops: 3350f2fc6a54SBenjamin Thery goto out; 3351cdb18761SDaniel Lezcano } 3352cdb18761SDaniel Lezcano 33532c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 3354cdb18761SDaniel Lezcano { 33558ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 33568ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 33578ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 33588ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 33598ed67789SDaniel Lezcano #endif 336041bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 3361cdb18761SDaniel Lezcano } 3362cdb18761SDaniel Lezcano 3363d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net) 3364d189634eSThomas Graf { 3365d189634eSThomas Graf #ifdef CONFIG_PROC_FS 3366d4beaa66SGao feng proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops); 3367d4beaa66SGao feng proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops); 3368d189634eSThomas Graf #endif 3369d189634eSThomas Graf return 0; 3370d189634eSThomas Graf } 3371d189634eSThomas Graf 3372d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net) 3373d189634eSThomas Graf { 3374d189634eSThomas Graf #ifdef CONFIG_PROC_FS 3375ece31ffdSGao feng remove_proc_entry("ipv6_route", net->proc_net); 3376ece31ffdSGao feng remove_proc_entry("rt6_stats", net->proc_net); 3377d189634eSThomas Graf #endif 3378d189634eSThomas Graf } 3379d189634eSThomas Graf 3380cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 3381cdb18761SDaniel Lezcano .init = ip6_route_net_init, 3382cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 3383cdb18761SDaniel Lezcano }; 3384cdb18761SDaniel Lezcano 3385c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net) 3386c3426b47SDavid S. Miller { 3387c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 3388c3426b47SDavid S. Miller 3389c3426b47SDavid S. Miller if (!bp) 3390c3426b47SDavid S. Miller return -ENOMEM; 3391c3426b47SDavid S. Miller inet_peer_base_init(bp); 3392c3426b47SDavid S. Miller net->ipv6.peers = bp; 3393c3426b47SDavid S. Miller return 0; 3394c3426b47SDavid S. Miller } 3395c3426b47SDavid S. Miller 3396c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net) 3397c3426b47SDavid S. Miller { 3398c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv6.peers; 3399c3426b47SDavid S. Miller 3400c3426b47SDavid S. Miller net->ipv6.peers = NULL; 340156a6b248SDavid S. Miller inetpeer_invalidate_tree(bp); 3402c3426b47SDavid S. Miller kfree(bp); 3403c3426b47SDavid S. Miller } 3404c3426b47SDavid S. Miller 34052b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = { 3406c3426b47SDavid S. Miller .init = ipv6_inetpeer_init, 3407c3426b47SDavid S. Miller .exit = ipv6_inetpeer_exit, 3408c3426b47SDavid S. Miller }; 3409c3426b47SDavid S. Miller 3410d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = { 3411d189634eSThomas Graf .init = ip6_route_net_init_late, 3412d189634eSThomas Graf .exit = ip6_route_net_exit_late, 3413d189634eSThomas Graf }; 3414d189634eSThomas Graf 34158ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 34168ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 34178ed67789SDaniel Lezcano .priority = 0, 34188ed67789SDaniel Lezcano }; 34198ed67789SDaniel Lezcano 3420433d49c3SDaniel Lezcano int __init ip6_route_init(void) 34211da177e4SLinus Torvalds { 3422433d49c3SDaniel Lezcano int ret; 34238d0b94afSMartin KaFai Lau int cpu; 3424433d49c3SDaniel Lezcano 34259a7ec3a9SDaniel Lezcano ret = -ENOMEM; 34269a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 34279a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 34289a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 34299a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 3430c19a28e1SFernando Carrijo goto out; 343114e50e57SDavid S. Miller 3432fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 34338ed67789SDaniel Lezcano if (ret) 3434bdb3289fSDaniel Lezcano goto out_kmem_cache; 3435bdb3289fSDaniel Lezcano 3436c3426b47SDavid S. Miller ret = register_pernet_subsys(&ipv6_inetpeer_ops); 3437c3426b47SDavid S. Miller if (ret) 3438e8803b6cSDavid S. Miller goto out_dst_entries; 34392a0c451aSThomas Graf 34407e52b33bSDavid S. Miller ret = register_pernet_subsys(&ip6_route_net_ops); 34417e52b33bSDavid S. Miller if (ret) 34427e52b33bSDavid S. Miller goto out_register_inetpeer; 3443c3426b47SDavid S. Miller 34445dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 34455dc121e9SArnaud Ebalard 34468ed67789SDaniel Lezcano /* Registering of the loopback is done before this portion of code, 34478ed67789SDaniel Lezcano * the loopback reference in rt6_info will not be taken, do it 34488ed67789SDaniel Lezcano * manually for init_net */ 3449d8d1f30bSChangli Gao init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 34508ed67789SDaniel Lezcano init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 3451bdb3289fSDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 3452d8d1f30bSChangli Gao init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 34538ed67789SDaniel Lezcano init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 3454d8d1f30bSChangli Gao init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 34558ed67789SDaniel Lezcano init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 3456bdb3289fSDaniel Lezcano #endif 3457e8803b6cSDavid S. Miller ret = fib6_init(); 3458433d49c3SDaniel Lezcano if (ret) 34598ed67789SDaniel Lezcano goto out_register_subsys; 3460433d49c3SDaniel Lezcano 3461433d49c3SDaniel Lezcano ret = xfrm6_init(); 3462433d49c3SDaniel Lezcano if (ret) 3463e8803b6cSDavid S. Miller goto out_fib6_init; 3464c35b7e72SDaniel Lezcano 3465433d49c3SDaniel Lezcano ret = fib6_rules_init(); 3466433d49c3SDaniel Lezcano if (ret) 3467433d49c3SDaniel Lezcano goto xfrm6_init; 34687e5449c2SDaniel Lezcano 3469d189634eSThomas Graf ret = register_pernet_subsys(&ip6_route_net_late_ops); 3470d189634eSThomas Graf if (ret) 3471d189634eSThomas Graf goto fib6_rules_init; 3472d189634eSThomas Graf 3473433d49c3SDaniel Lezcano ret = -ENOBUFS; 3474c7ac8679SGreg Rose if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) || 3475c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) || 3476c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL)) 3477d189634eSThomas Graf goto out_register_late_subsys; 3478433d49c3SDaniel Lezcano 34798ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 3480cdb18761SDaniel Lezcano if (ret) 3481d189634eSThomas Graf goto out_register_late_subsys; 34828ed67789SDaniel Lezcano 34838d0b94afSMartin KaFai Lau for_each_possible_cpu(cpu) { 34848d0b94afSMartin KaFai Lau struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); 34858d0b94afSMartin KaFai Lau 34868d0b94afSMartin KaFai Lau INIT_LIST_HEAD(&ul->head); 34878d0b94afSMartin KaFai Lau spin_lock_init(&ul->lock); 34888d0b94afSMartin KaFai Lau } 34898d0b94afSMartin KaFai Lau 3490433d49c3SDaniel Lezcano out: 3491433d49c3SDaniel Lezcano return ret; 3492433d49c3SDaniel Lezcano 3493d189634eSThomas Graf out_register_late_subsys: 3494d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 3495433d49c3SDaniel Lezcano fib6_rules_init: 3496433d49c3SDaniel Lezcano fib6_rules_cleanup(); 3497433d49c3SDaniel Lezcano xfrm6_init: 3498433d49c3SDaniel Lezcano xfrm6_fini(); 34992a0c451aSThomas Graf out_fib6_init: 35002a0c451aSThomas Graf fib6_gc_cleanup(); 35018ed67789SDaniel Lezcano out_register_subsys: 35028ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 35037e52b33bSDavid S. Miller out_register_inetpeer: 35047e52b33bSDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 3505fc66f95cSEric Dumazet out_dst_entries: 3506fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 3507433d49c3SDaniel Lezcano out_kmem_cache: 3508f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 3509433d49c3SDaniel Lezcano goto out; 35101da177e4SLinus Torvalds } 35111da177e4SLinus Torvalds 35121da177e4SLinus Torvalds void ip6_route_cleanup(void) 35131da177e4SLinus Torvalds { 35148ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 3515d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 3516101367c2SThomas Graf fib6_rules_cleanup(); 35171da177e4SLinus Torvalds xfrm6_fini(); 35181da177e4SLinus Torvalds fib6_gc_cleanup(); 3519c3426b47SDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 35208ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 352141bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 3522f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 35231da177e4SLinus Torvalds } 3524