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> 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds #include <asm/uaccess.h> 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 641da177e4SLinus Torvalds #include <linux/sysctl.h> 651da177e4SLinus Torvalds #endif 661da177e4SLinus Torvalds 671716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort, 6821efcfa0SEric Dumazet const struct in6_addr *dest); 691da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie); 700dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst); 71ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst); 721da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *); 731da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *); 741da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *, 751da177e4SLinus Torvalds struct net_device *dev, int how); 76569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops); 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds static int ip6_pkt_discard(struct sk_buff *skb); 791da177e4SLinus Torvalds static int ip6_pkt_discard_out(struct sk_buff *skb); 801da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb); 811da177e4SLinus Torvalds static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu); 821da177e4SLinus Torvalds 8370ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 84efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 85b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 86b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex, 8795c96174SEric Dumazet unsigned int pref); 88efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 89b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 90b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex); 9170ceb4f5SYOSHIFUJI Hideaki #endif 9270ceb4f5SYOSHIFUJI Hideaki 9306582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old) 9406582540SDavid S. Miller { 9506582540SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 9606582540SDavid S. Miller struct inet_peer *peer; 9706582540SDavid S. Miller u32 *p = NULL; 9806582540SDavid S. Miller 998e2ec639SYan, Zheng if (!(rt->dst.flags & DST_HOST)) 1008e2ec639SYan, Zheng return NULL; 1018e2ec639SYan, Zheng 102fbfe95a4SDavid S. Miller peer = rt6_get_peer_create(rt); 10306582540SDavid S. Miller if (peer) { 10406582540SDavid S. Miller u32 *old_p = __DST_METRICS_PTR(old); 10506582540SDavid S. Miller unsigned long prev, new; 10606582540SDavid S. Miller 10706582540SDavid S. Miller p = peer->metrics; 10806582540SDavid S. Miller if (inet_metrics_new(peer)) 10906582540SDavid S. Miller memcpy(p, old_p, sizeof(u32) * RTAX_MAX); 11006582540SDavid S. Miller 11106582540SDavid S. Miller new = (unsigned long) p; 11206582540SDavid S. Miller prev = cmpxchg(&dst->_metrics, old, new); 11306582540SDavid S. Miller 11406582540SDavid S. Miller if (prev != old) { 11506582540SDavid S. Miller p = __DST_METRICS_PTR(prev); 11606582540SDavid S. Miller if (prev & DST_METRICS_READ_ONLY) 11706582540SDavid S. Miller p = NULL; 11806582540SDavid S. Miller } 11906582540SDavid S. Miller } 12006582540SDavid S. Miller return p; 12106582540SDavid S. Miller } 12206582540SDavid S. Miller 12339232973SDavid S. Miller static inline const void *choose_neigh_daddr(struct rt6_info *rt, const void *daddr) 12439232973SDavid S. Miller { 12539232973SDavid S. Miller struct in6_addr *p = &rt->rt6i_gateway; 12639232973SDavid S. Miller 127a7563f34SDavid S. Miller if (!ipv6_addr_any(p)) 12839232973SDavid S. Miller return (const void *) p; 12939232973SDavid S. Miller return daddr; 13039232973SDavid S. Miller } 13139232973SDavid S. Miller 132d3aaeb38SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst, const void *daddr) 133d3aaeb38SDavid S. Miller { 13439232973SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 13539232973SDavid S. Miller struct neighbour *n; 13639232973SDavid S. Miller 13739232973SDavid S. Miller daddr = choose_neigh_daddr(rt, daddr); 13839232973SDavid S. Miller n = __ipv6_neigh_lookup(&nd_tbl, dst->dev, daddr); 139f83c7790SDavid S. Miller if (n) 140f83c7790SDavid S. Miller return n; 141f83c7790SDavid S. Miller return neigh_create(&nd_tbl, daddr, dst->dev); 142f83c7790SDavid S. Miller } 143f83c7790SDavid S. Miller 1448ade06c6SDavid S. Miller static int rt6_bind_neighbour(struct rt6_info *rt, struct net_device *dev) 145f83c7790SDavid S. Miller { 1468ade06c6SDavid S. Miller struct neighbour *n = __ipv6_neigh_lookup(&nd_tbl, dev, &rt->rt6i_gateway); 1478ade06c6SDavid S. Miller if (!n) { 1488ade06c6SDavid S. Miller n = neigh_create(&nd_tbl, &rt->rt6i_gateway, dev); 149f83c7790SDavid S. Miller if (IS_ERR(n)) 150f83c7790SDavid S. Miller return PTR_ERR(n); 1518ade06c6SDavid S. Miller } 152f83c7790SDavid S. Miller dst_set_neighbour(&rt->dst, n); 153f83c7790SDavid S. Miller 154f83c7790SDavid S. Miller return 0; 155d3aaeb38SDavid S. Miller } 156d3aaeb38SDavid S. Miller 1579a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = { 1581da177e4SLinus Torvalds .family = AF_INET6, 15909640e63SHarvey Harrison .protocol = cpu_to_be16(ETH_P_IPV6), 1601da177e4SLinus Torvalds .gc = ip6_dst_gc, 1611da177e4SLinus Torvalds .gc_thresh = 1024, 1621da177e4SLinus Torvalds .check = ip6_dst_check, 1630dbaee3bSDavid S. Miller .default_advmss = ip6_default_advmss, 164ebb762f2SSteffen Klassert .mtu = ip6_mtu, 16506582540SDavid S. Miller .cow_metrics = ipv6_cow_metrics, 1661da177e4SLinus Torvalds .destroy = ip6_dst_destroy, 1671da177e4SLinus Torvalds .ifdown = ip6_dst_ifdown, 1681da177e4SLinus Torvalds .negative_advice = ip6_negative_advice, 1691da177e4SLinus Torvalds .link_failure = ip6_link_failure, 1701da177e4SLinus Torvalds .update_pmtu = ip6_rt_update_pmtu, 1711ac06e03SHerbert Xu .local_out = __ip6_local_out, 172d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 1731da177e4SLinus Torvalds }; 1741da177e4SLinus Torvalds 175ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst) 176ec831ea7SRoland Dreier { 177618f9bc7SSteffen Klassert unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); 178618f9bc7SSteffen Klassert 179618f9bc7SSteffen Klassert return mtu ? : dst->dev->mtu; 180ec831ea7SRoland Dreier } 181ec831ea7SRoland Dreier 18214e50e57SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu) 18314e50e57SDavid S. Miller { 18414e50e57SDavid S. Miller } 18514e50e57SDavid S. Miller 1860972ddb2SHeld Bernhard static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst, 1870972ddb2SHeld Bernhard unsigned long old) 1880972ddb2SHeld Bernhard { 1890972ddb2SHeld Bernhard return NULL; 1900972ddb2SHeld Bernhard } 1910972ddb2SHeld Bernhard 19214e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = { 19314e50e57SDavid S. Miller .family = AF_INET6, 19409640e63SHarvey Harrison .protocol = cpu_to_be16(ETH_P_IPV6), 19514e50e57SDavid S. Miller .destroy = ip6_dst_destroy, 19614e50e57SDavid S. Miller .check = ip6_dst_check, 197ebb762f2SSteffen Klassert .mtu = ip6_blackhole_mtu, 198214f45c9SEric Dumazet .default_advmss = ip6_default_advmss, 19914e50e57SDavid S. Miller .update_pmtu = ip6_rt_blackhole_update_pmtu, 2000972ddb2SHeld Bernhard .cow_metrics = ip6_rt_blackhole_cow_metrics, 201d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 20214e50e57SDavid S. Miller }; 20314e50e57SDavid S. Miller 20462fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = { 20562fa8a84SDavid S. Miller [RTAX_HOPLIMIT - 1] = 255, 20662fa8a84SDavid S. Miller }; 20762fa8a84SDavid S. Miller 208bdb3289fSDaniel Lezcano static struct rt6_info ip6_null_entry_template = { 2091da177e4SLinus Torvalds .dst = { 2101da177e4SLinus Torvalds .__refcnt = ATOMIC_INIT(1), 2111da177e4SLinus Torvalds .__use = 1, 2121da177e4SLinus Torvalds .obsolete = -1, 2131da177e4SLinus Torvalds .error = -ENETUNREACH, 2141da177e4SLinus Torvalds .input = ip6_pkt_discard, 2151da177e4SLinus Torvalds .output = ip6_pkt_discard_out, 2161da177e4SLinus Torvalds }, 2171da177e4SLinus Torvalds .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 2184f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 2191da177e4SLinus Torvalds .rt6i_metric = ~(u32) 0, 2201da177e4SLinus Torvalds .rt6i_ref = ATOMIC_INIT(1), 2211da177e4SLinus Torvalds }; 2221da177e4SLinus Torvalds 223101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES 224101367c2SThomas Graf 2256723ab54SDavid S. Miller static int ip6_pkt_prohibit(struct sk_buff *skb); 2266723ab54SDavid S. Miller static int ip6_pkt_prohibit_out(struct sk_buff *skb); 2276723ab54SDavid S. Miller 228280a34c8SAdrian Bunk static struct rt6_info ip6_prohibit_entry_template = { 229101367c2SThomas Graf .dst = { 230101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 231101367c2SThomas Graf .__use = 1, 232101367c2SThomas Graf .obsolete = -1, 233101367c2SThomas Graf .error = -EACCES, 2349ce8ade0SThomas Graf .input = ip6_pkt_prohibit, 2359ce8ade0SThomas Graf .output = ip6_pkt_prohibit_out, 236101367c2SThomas Graf }, 237101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 2384f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 239101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 240101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 241101367c2SThomas Graf }; 242101367c2SThomas Graf 243bdb3289fSDaniel Lezcano static struct rt6_info ip6_blk_hole_entry_template = { 244101367c2SThomas Graf .dst = { 245101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 246101367c2SThomas Graf .__use = 1, 247101367c2SThomas Graf .obsolete = -1, 248101367c2SThomas Graf .error = -EINVAL, 249352e512cSHerbert Xu .input = dst_discard, 250352e512cSHerbert Xu .output = dst_discard, 251101367c2SThomas Graf }, 252101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 2534f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 254101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 255101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 256101367c2SThomas Graf }; 257101367c2SThomas Graf 258101367c2SThomas Graf #endif 259101367c2SThomas Graf 2601da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */ 26197bab73fSDavid S. Miller static inline struct rt6_info *ip6_dst_alloc(struct net *net, 262957c665fSDavid S. Miller struct net_device *dev, 2638b96d22dSDavid S. Miller int flags, 2648b96d22dSDavid S. Miller struct fib6_table *table) 2651da177e4SLinus Torvalds { 26697bab73fSDavid S. Miller struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev, 26797bab73fSDavid S. Miller 0, 0, flags); 268cf911662SDavid S. Miller 26997bab73fSDavid S. Miller if (rt) { 270fbe58186SMadalin Bucur memset(&rt->rt6i_table, 0, 271fbe58186SMadalin Bucur sizeof(*rt) - sizeof(struct dst_entry)); 2728b96d22dSDavid S. Miller rt6_init_peer(rt, table ? &table->tb6_peers : net->ipv6.peers); 27397bab73fSDavid S. Miller } 274cf911662SDavid S. Miller return rt; 2751da177e4SLinus Torvalds } 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst) 2781da177e4SLinus Torvalds { 2791da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 2801da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 2811da177e4SLinus Torvalds 2828e2ec639SYan, Zheng if (!(rt->dst.flags & DST_HOST)) 2838e2ec639SYan, Zheng dst_destroy_metrics_generic(dst); 2848e2ec639SYan, Zheng 28538308473SDavid S. Miller if (idev) { 2861da177e4SLinus Torvalds rt->rt6i_idev = NULL; 2871da177e4SLinus Torvalds in6_dev_put(idev); 2881da177e4SLinus Torvalds } 2891716a961SGao feng 2901716a961SGao feng if (!(rt->rt6i_flags & RTF_EXPIRES) && dst->from) 2911716a961SGao feng dst_release(dst->from); 2921716a961SGao feng 29397bab73fSDavid S. Miller if (rt6_has_peer(rt)) { 29497bab73fSDavid S. Miller struct inet_peer *peer = rt6_peer_ptr(rt); 295b3419363SDavid S. Miller inet_putpeer(peer); 296b3419363SDavid S. Miller } 297b3419363SDavid S. Miller } 298b3419363SDavid S. Miller 2996431cbc2SDavid S. Miller static atomic_t __rt6_peer_genid = ATOMIC_INIT(0); 3006431cbc2SDavid S. Miller 3016431cbc2SDavid S. Miller static u32 rt6_peer_genid(void) 3026431cbc2SDavid S. Miller { 3036431cbc2SDavid S. Miller return atomic_read(&__rt6_peer_genid); 3046431cbc2SDavid S. Miller } 3056431cbc2SDavid S. Miller 306b3419363SDavid S. Miller void rt6_bind_peer(struct rt6_info *rt, int create) 307b3419363SDavid S. Miller { 30897bab73fSDavid S. Miller struct inet_peer_base *base; 309b3419363SDavid S. Miller struct inet_peer *peer; 310b3419363SDavid S. Miller 31197bab73fSDavid S. Miller base = inetpeer_base_ptr(rt->_rt6i_peer); 31297bab73fSDavid S. Miller if (!base) 31397bab73fSDavid S. Miller return; 31497bab73fSDavid S. Miller 31597bab73fSDavid S. Miller peer = inet_getpeer_v6(base, &rt->rt6i_dst.addr, create); 3167b34ca2aSDavid S. Miller if (peer) { 31797bab73fSDavid S. Miller if (!rt6_set_peer(rt, peer)) 318b3419363SDavid S. Miller inet_putpeer(peer); 3196431cbc2SDavid S. Miller else 3206431cbc2SDavid S. Miller rt->rt6i_peer_genid = rt6_peer_genid(); 3211da177e4SLinus Torvalds } 3227b34ca2aSDavid S. Miller } 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 3251da177e4SLinus Torvalds int how) 3261da177e4SLinus Torvalds { 3271da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 3281da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 3295a3e55d6SDenis V. Lunev struct net_device *loopback_dev = 330c346dca1SYOSHIFUJI Hideaki dev_net(dev)->loopback_dev; 3311da177e4SLinus Torvalds 33238308473SDavid S. Miller if (dev != loopback_dev && idev && idev->dev == dev) { 3335a3e55d6SDenis V. Lunev struct inet6_dev *loopback_idev = 3345a3e55d6SDenis V. Lunev in6_dev_get(loopback_dev); 33538308473SDavid S. Miller if (loopback_idev) { 3361da177e4SLinus Torvalds rt->rt6i_idev = loopback_idev; 3371da177e4SLinus Torvalds in6_dev_put(idev); 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds } 3411da177e4SLinus Torvalds 342a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt) 3431da177e4SLinus Torvalds { 3441716a961SGao feng struct rt6_info *ort = NULL; 3451716a961SGao feng 3461716a961SGao feng if (rt->rt6i_flags & RTF_EXPIRES) { 3471716a961SGao feng if (time_after(jiffies, rt->dst.expires)) 348a50feda5SEric Dumazet return true; 3491716a961SGao feng } else if (rt->dst.from) { 3501716a961SGao feng ort = (struct rt6_info *) rt->dst.from; 3511716a961SGao feng return (ort->rt6i_flags & RTF_EXPIRES) && 3521716a961SGao feng time_after(jiffies, ort->dst.expires); 3531716a961SGao feng } 354a50feda5SEric Dumazet return false; 3551da177e4SLinus Torvalds } 3561da177e4SLinus Torvalds 357a50feda5SEric Dumazet static bool rt6_need_strict(const struct in6_addr *daddr) 358c71099acSThomas Graf { 359a02cec21SEric Dumazet return ipv6_addr_type(daddr) & 360a02cec21SEric Dumazet (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK); 361c71099acSThomas Graf } 362c71099acSThomas Graf 3631da177e4SLinus Torvalds /* 364c71099acSThomas Graf * Route lookup. Any table->tb6_lock is implied. 3651da177e4SLinus Torvalds */ 3661da177e4SLinus Torvalds 3678ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net, 3688ed67789SDaniel Lezcano struct rt6_info *rt, 369b71d1d42SEric Dumazet const struct in6_addr *saddr, 3701da177e4SLinus Torvalds int oif, 371d420895eSYOSHIFUJI Hideaki int flags) 3721da177e4SLinus Torvalds { 3731da177e4SLinus Torvalds struct rt6_info *local = NULL; 3741da177e4SLinus Torvalds struct rt6_info *sprt; 3751da177e4SLinus Torvalds 376dd3abc4eSYOSHIFUJI Hideaki if (!oif && ipv6_addr_any(saddr)) 377dd3abc4eSYOSHIFUJI Hideaki goto out; 378dd3abc4eSYOSHIFUJI Hideaki 379d8d1f30bSChangli Gao for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) { 380d1918542SDavid S. Miller struct net_device *dev = sprt->dst.dev; 381dd3abc4eSYOSHIFUJI Hideaki 382dd3abc4eSYOSHIFUJI Hideaki if (oif) { 3831da177e4SLinus Torvalds if (dev->ifindex == oif) 3841da177e4SLinus Torvalds return sprt; 3851da177e4SLinus Torvalds if (dev->flags & IFF_LOOPBACK) { 38638308473SDavid S. Miller if (!sprt->rt6i_idev || 3871da177e4SLinus Torvalds sprt->rt6i_idev->dev->ifindex != oif) { 388d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE && oif) 3891da177e4SLinus Torvalds continue; 3901da177e4SLinus Torvalds if (local && (!oif || 3911da177e4SLinus Torvalds local->rt6i_idev->dev->ifindex == oif)) 3921da177e4SLinus Torvalds continue; 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds local = sprt; 3951da177e4SLinus Torvalds } 396dd3abc4eSYOSHIFUJI Hideaki } else { 397dd3abc4eSYOSHIFUJI Hideaki if (ipv6_chk_addr(net, saddr, dev, 398dd3abc4eSYOSHIFUJI Hideaki flags & RT6_LOOKUP_F_IFACE)) 399dd3abc4eSYOSHIFUJI Hideaki return sprt; 400dd3abc4eSYOSHIFUJI Hideaki } 4011da177e4SLinus Torvalds } 4021da177e4SLinus Torvalds 403dd3abc4eSYOSHIFUJI Hideaki if (oif) { 4041da177e4SLinus Torvalds if (local) 4051da177e4SLinus Torvalds return local; 4061da177e4SLinus Torvalds 407d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE) 4088ed67789SDaniel Lezcano return net->ipv6.ip6_null_entry; 4091da177e4SLinus Torvalds } 410dd3abc4eSYOSHIFUJI Hideaki out: 4111da177e4SLinus Torvalds return rt; 4121da177e4SLinus Torvalds } 4131da177e4SLinus Torvalds 41427097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 41527097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt) 41627097255SYOSHIFUJI Hideaki { 417f2c31e32SEric Dumazet struct neighbour *neigh; 41827097255SYOSHIFUJI Hideaki /* 41927097255SYOSHIFUJI Hideaki * Okay, this does not seem to be appropriate 42027097255SYOSHIFUJI Hideaki * for now, however, we need to check if it 42127097255SYOSHIFUJI Hideaki * is really so; aka Router Reachability Probing. 42227097255SYOSHIFUJI Hideaki * 42327097255SYOSHIFUJI Hideaki * Router Reachability Probe MUST be rate-limited 42427097255SYOSHIFUJI Hideaki * to no more than one per minute. 42527097255SYOSHIFUJI Hideaki */ 426f2c31e32SEric Dumazet rcu_read_lock(); 42727217455SDavid Miller neigh = rt ? dst_get_neighbour_noref(&rt->dst) : NULL; 42827097255SYOSHIFUJI Hideaki if (!neigh || (neigh->nud_state & NUD_VALID)) 429f2c31e32SEric Dumazet goto out; 43027097255SYOSHIFUJI Hideaki read_lock_bh(&neigh->lock); 43127097255SYOSHIFUJI Hideaki if (!(neigh->nud_state & NUD_VALID) && 43252e16356SYOSHIFUJI Hideaki time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) { 43327097255SYOSHIFUJI Hideaki struct in6_addr mcaddr; 43427097255SYOSHIFUJI Hideaki struct in6_addr *target; 43527097255SYOSHIFUJI Hideaki 43627097255SYOSHIFUJI Hideaki neigh->updated = jiffies; 43727097255SYOSHIFUJI Hideaki read_unlock_bh(&neigh->lock); 43827097255SYOSHIFUJI Hideaki 43927097255SYOSHIFUJI Hideaki target = (struct in6_addr *)&neigh->primary_key; 44027097255SYOSHIFUJI Hideaki addrconf_addr_solict_mult(target, &mcaddr); 441d1918542SDavid S. Miller ndisc_send_ns(rt->dst.dev, NULL, target, &mcaddr, NULL); 442f2c31e32SEric Dumazet } else { 44327097255SYOSHIFUJI Hideaki read_unlock_bh(&neigh->lock); 44427097255SYOSHIFUJI Hideaki } 445f2c31e32SEric Dumazet out: 446f2c31e32SEric Dumazet rcu_read_unlock(); 447f2c31e32SEric Dumazet } 44827097255SYOSHIFUJI Hideaki #else 44927097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt) 45027097255SYOSHIFUJI Hideaki { 45127097255SYOSHIFUJI Hideaki } 45227097255SYOSHIFUJI Hideaki #endif 45327097255SYOSHIFUJI Hideaki 4541da177e4SLinus Torvalds /* 455554cfb7eSYOSHIFUJI Hideaki * Default Router Selection (RFC 2461 6.3.6) 4561da177e4SLinus Torvalds */ 457b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif) 4581da177e4SLinus Torvalds { 459d1918542SDavid S. Miller struct net_device *dev = rt->dst.dev; 460161980f4SDavid S. Miller if (!oif || dev->ifindex == oif) 461554cfb7eSYOSHIFUJI Hideaki return 2; 462161980f4SDavid S. Miller if ((dev->flags & IFF_LOOPBACK) && 463161980f4SDavid S. Miller rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif) 464161980f4SDavid S. Miller return 1; 465554cfb7eSYOSHIFUJI Hideaki return 0; 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds 468b6f99a21SDave Jones static inline int rt6_check_neigh(struct rt6_info *rt) 4691da177e4SLinus Torvalds { 470f2c31e32SEric Dumazet struct neighbour *neigh; 471398bcbebSYOSHIFUJI Hideaki int m; 472f2c31e32SEric Dumazet 473f2c31e32SEric Dumazet rcu_read_lock(); 47427217455SDavid Miller neigh = dst_get_neighbour_noref(&rt->dst); 4754d0c5911SYOSHIFUJI Hideaki if (rt->rt6i_flags & RTF_NONEXTHOP || 4764d0c5911SYOSHIFUJI Hideaki !(rt->rt6i_flags & RTF_GATEWAY)) 4774d0c5911SYOSHIFUJI Hideaki m = 1; 4784d0c5911SYOSHIFUJI Hideaki else if (neigh) { 4791da177e4SLinus Torvalds read_lock_bh(&neigh->lock); 480554cfb7eSYOSHIFUJI Hideaki if (neigh->nud_state & NUD_VALID) 4814d0c5911SYOSHIFUJI Hideaki m = 2; 482398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 483398bcbebSYOSHIFUJI Hideaki else if (neigh->nud_state & NUD_FAILED) 484398bcbebSYOSHIFUJI Hideaki m = 0; 485398bcbebSYOSHIFUJI Hideaki #endif 486398bcbebSYOSHIFUJI Hideaki else 487ea73ee23SYOSHIFUJI Hideaki m = 1; 4881da177e4SLinus Torvalds read_unlock_bh(&neigh->lock); 489398bcbebSYOSHIFUJI Hideaki } else 490398bcbebSYOSHIFUJI Hideaki m = 0; 491f2c31e32SEric Dumazet rcu_read_unlock(); 492554cfb7eSYOSHIFUJI Hideaki return m; 4931da177e4SLinus Torvalds } 4941da177e4SLinus Torvalds 495554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif, 496554cfb7eSYOSHIFUJI Hideaki int strict) 497554cfb7eSYOSHIFUJI Hideaki { 4984d0c5911SYOSHIFUJI Hideaki int m, n; 4994d0c5911SYOSHIFUJI Hideaki 5004d0c5911SYOSHIFUJI Hideaki m = rt6_check_dev(rt, oif); 50177d16f45SYOSHIFUJI Hideaki if (!m && (strict & RT6_LOOKUP_F_IFACE)) 502554cfb7eSYOSHIFUJI Hideaki return -1; 503ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 504ebacaaa0SYOSHIFUJI Hideaki m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2; 505ebacaaa0SYOSHIFUJI Hideaki #endif 5064d0c5911SYOSHIFUJI Hideaki n = rt6_check_neigh(rt); 507557e92efSYOSHIFUJI Hideaki if (!n && (strict & RT6_LOOKUP_F_REACHABLE)) 508554cfb7eSYOSHIFUJI Hideaki return -1; 509554cfb7eSYOSHIFUJI Hideaki return m; 510554cfb7eSYOSHIFUJI Hideaki } 511554cfb7eSYOSHIFUJI Hideaki 512f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict, 513f11e6659SDavid S. Miller int *mpri, struct rt6_info *match) 514554cfb7eSYOSHIFUJI Hideaki { 515554cfb7eSYOSHIFUJI Hideaki int m; 516554cfb7eSYOSHIFUJI Hideaki 517554cfb7eSYOSHIFUJI Hideaki if (rt6_check_expired(rt)) 518f11e6659SDavid S. Miller goto out; 519554cfb7eSYOSHIFUJI Hideaki 520554cfb7eSYOSHIFUJI Hideaki m = rt6_score_route(rt, oif, strict); 521554cfb7eSYOSHIFUJI Hideaki if (m < 0) 522f11e6659SDavid S. Miller goto out; 523554cfb7eSYOSHIFUJI Hideaki 524f11e6659SDavid S. Miller if (m > *mpri) { 525ea659e07SYOSHIFUJI Hideaki if (strict & RT6_LOOKUP_F_REACHABLE) 52627097255SYOSHIFUJI Hideaki rt6_probe(match); 527f11e6659SDavid S. Miller *mpri = m; 528554cfb7eSYOSHIFUJI Hideaki match = rt; 529ea659e07SYOSHIFUJI Hideaki } else if (strict & RT6_LOOKUP_F_REACHABLE) { 53027097255SYOSHIFUJI Hideaki rt6_probe(rt); 5311da177e4SLinus Torvalds } 532f11e6659SDavid S. Miller 533f11e6659SDavid S. Miller out: 534f11e6659SDavid S. Miller return match; 5351da177e4SLinus Torvalds } 5361da177e4SLinus Torvalds 537f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn, 538f11e6659SDavid S. Miller struct rt6_info *rr_head, 539f11e6659SDavid S. Miller u32 metric, int oif, int strict) 540f11e6659SDavid S. Miller { 541f11e6659SDavid S. Miller struct rt6_info *rt, *match; 542f11e6659SDavid S. Miller int mpri = -1; 543f11e6659SDavid S. Miller 544f11e6659SDavid S. Miller match = NULL; 545f11e6659SDavid S. Miller for (rt = rr_head; rt && rt->rt6i_metric == metric; 546d8d1f30bSChangli Gao rt = rt->dst.rt6_next) 547f11e6659SDavid S. Miller match = find_match(rt, oif, strict, &mpri, match); 548f11e6659SDavid S. Miller for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric; 549d8d1f30bSChangli Gao rt = rt->dst.rt6_next) 550f11e6659SDavid S. Miller match = find_match(rt, oif, strict, &mpri, match); 551f11e6659SDavid S. Miller 552f11e6659SDavid S. Miller return match; 553f11e6659SDavid S. Miller } 554f11e6659SDavid S. Miller 555f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict) 556f11e6659SDavid S. Miller { 557f11e6659SDavid S. Miller struct rt6_info *match, *rt0; 5588ed67789SDaniel Lezcano struct net *net; 559f11e6659SDavid S. Miller 560f11e6659SDavid S. Miller rt0 = fn->rr_ptr; 561f11e6659SDavid S. Miller if (!rt0) 562f11e6659SDavid S. Miller fn->rr_ptr = rt0 = fn->leaf; 563f11e6659SDavid S. Miller 564f11e6659SDavid S. Miller match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict); 565f11e6659SDavid S. Miller 566554cfb7eSYOSHIFUJI Hideaki if (!match && 567f11e6659SDavid S. Miller (strict & RT6_LOOKUP_F_REACHABLE)) { 568d8d1f30bSChangli Gao struct rt6_info *next = rt0->dst.rt6_next; 569f11e6659SDavid S. Miller 570554cfb7eSYOSHIFUJI Hideaki /* no entries matched; do round-robin */ 571f11e6659SDavid S. Miller if (!next || next->rt6i_metric != rt0->rt6i_metric) 572f11e6659SDavid S. Miller next = fn->leaf; 573f11e6659SDavid S. Miller 574f11e6659SDavid S. Miller if (next != rt0) 575f11e6659SDavid S. Miller fn->rr_ptr = next; 576554cfb7eSYOSHIFUJI Hideaki } 577554cfb7eSYOSHIFUJI Hideaki 578d1918542SDavid S. Miller net = dev_net(rt0->dst.dev); 579a02cec21SEric Dumazet return match ? match : net->ipv6.ip6_null_entry; 5801da177e4SLinus Torvalds } 5811da177e4SLinus Torvalds 58270ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 58370ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 584b71d1d42SEric Dumazet const struct in6_addr *gwaddr) 58570ceb4f5SYOSHIFUJI Hideaki { 586c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 58770ceb4f5SYOSHIFUJI Hideaki struct route_info *rinfo = (struct route_info *) opt; 58870ceb4f5SYOSHIFUJI Hideaki struct in6_addr prefix_buf, *prefix; 58970ceb4f5SYOSHIFUJI Hideaki unsigned int pref; 5904bed72e4SYOSHIFUJI Hideaki unsigned long lifetime; 59170ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt; 59270ceb4f5SYOSHIFUJI Hideaki 59370ceb4f5SYOSHIFUJI Hideaki if (len < sizeof(struct route_info)) { 59470ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 59570ceb4f5SYOSHIFUJI Hideaki } 59670ceb4f5SYOSHIFUJI Hideaki 59770ceb4f5SYOSHIFUJI Hideaki /* Sanity check for prefix_len and length */ 59870ceb4f5SYOSHIFUJI Hideaki if (rinfo->length > 3) { 59970ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 60070ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 128) { 60170ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 60270ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 64) { 60370ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 2) { 60470ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 60570ceb4f5SYOSHIFUJI Hideaki } 60670ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 0) { 60770ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 1) { 60870ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 60970ceb4f5SYOSHIFUJI Hideaki } 61070ceb4f5SYOSHIFUJI Hideaki } 61170ceb4f5SYOSHIFUJI Hideaki 61270ceb4f5SYOSHIFUJI Hideaki pref = rinfo->route_pref; 61370ceb4f5SYOSHIFUJI Hideaki if (pref == ICMPV6_ROUTER_PREF_INVALID) 6143933fc95SJens Rosenboom return -EINVAL; 61570ceb4f5SYOSHIFUJI Hideaki 6164bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); 61770ceb4f5SYOSHIFUJI Hideaki 61870ceb4f5SYOSHIFUJI Hideaki if (rinfo->length == 3) 61970ceb4f5SYOSHIFUJI Hideaki prefix = (struct in6_addr *)rinfo->prefix; 62070ceb4f5SYOSHIFUJI Hideaki else { 62170ceb4f5SYOSHIFUJI Hideaki /* this function is safe */ 62270ceb4f5SYOSHIFUJI Hideaki ipv6_addr_prefix(&prefix_buf, 62370ceb4f5SYOSHIFUJI Hideaki (struct in6_addr *)rinfo->prefix, 62470ceb4f5SYOSHIFUJI Hideaki rinfo->prefix_len); 62570ceb4f5SYOSHIFUJI Hideaki prefix = &prefix_buf; 62670ceb4f5SYOSHIFUJI Hideaki } 62770ceb4f5SYOSHIFUJI Hideaki 628efa2cea0SDaniel Lezcano rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr, 629efa2cea0SDaniel Lezcano dev->ifindex); 63070ceb4f5SYOSHIFUJI Hideaki 63170ceb4f5SYOSHIFUJI Hideaki if (rt && !lifetime) { 632e0a1ad73SThomas Graf ip6_del_rt(rt); 63370ceb4f5SYOSHIFUJI Hideaki rt = NULL; 63470ceb4f5SYOSHIFUJI Hideaki } 63570ceb4f5SYOSHIFUJI Hideaki 63670ceb4f5SYOSHIFUJI Hideaki if (!rt && lifetime) 637efa2cea0SDaniel Lezcano rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex, 63870ceb4f5SYOSHIFUJI Hideaki pref); 63970ceb4f5SYOSHIFUJI Hideaki else if (rt) 64070ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags = RTF_ROUTEINFO | 64170ceb4f5SYOSHIFUJI Hideaki (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 64270ceb4f5SYOSHIFUJI Hideaki 64370ceb4f5SYOSHIFUJI Hideaki if (rt) { 6441716a961SGao feng if (!addrconf_finite_timeout(lifetime)) 6451716a961SGao feng rt6_clean_expires(rt); 6461716a961SGao feng else 6471716a961SGao feng rt6_set_expires(rt, jiffies + HZ * lifetime); 6481716a961SGao feng 649d8d1f30bSChangli Gao dst_release(&rt->dst); 65070ceb4f5SYOSHIFUJI Hideaki } 65170ceb4f5SYOSHIFUJI Hideaki return 0; 65270ceb4f5SYOSHIFUJI Hideaki } 65370ceb4f5SYOSHIFUJI Hideaki #endif 65470ceb4f5SYOSHIFUJI Hideaki 6558ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr) \ 656982f56f3SYOSHIFUJI Hideaki do { \ 6578ed67789SDaniel Lezcano if (rt == __net->ipv6.ip6_null_entry) { \ 658982f56f3SYOSHIFUJI Hideaki struct fib6_node *pn; \ 659e0eda7bbSVille Nuorvala while (1) { \ 660982f56f3SYOSHIFUJI Hideaki if (fn->fn_flags & RTN_TL_ROOT) \ 661c71099acSThomas Graf goto out; \ 662982f56f3SYOSHIFUJI Hideaki pn = fn->parent; \ 663982f56f3SYOSHIFUJI Hideaki if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \ 6648bce65b9SKim Nordlund fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \ 665982f56f3SYOSHIFUJI Hideaki else \ 666982f56f3SYOSHIFUJI Hideaki fn = pn; \ 667c71099acSThomas Graf if (fn->fn_flags & RTN_RTINFO) \ 668c71099acSThomas Graf goto restart; \ 669c71099acSThomas Graf } \ 670982f56f3SYOSHIFUJI Hideaki } \ 671982f56f3SYOSHIFUJI Hideaki } while (0) 672c71099acSThomas Graf 6738ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net, 6748ed67789SDaniel Lezcano struct fib6_table *table, 6754c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 6761da177e4SLinus Torvalds { 6771da177e4SLinus Torvalds struct fib6_node *fn; 6781da177e4SLinus Torvalds struct rt6_info *rt; 6791da177e4SLinus Torvalds 680c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 6814c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 682c71099acSThomas Graf restart: 683c71099acSThomas Graf rt = fn->leaf; 6844c9483b2SDavid S. Miller rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags); 6854c9483b2SDavid S. Miller BACKTRACK(net, &fl6->saddr); 686c71099acSThomas Graf out: 687d8d1f30bSChangli Gao dst_use(&rt->dst, jiffies); 688c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 6891da177e4SLinus Torvalds return rt; 690c71099acSThomas Graf 691c71099acSThomas Graf } 692c71099acSThomas Graf 693ea6e574eSFlorian Westphal struct dst_entry * ip6_route_lookup(struct net *net, struct flowi6 *fl6, 694ea6e574eSFlorian Westphal int flags) 695ea6e574eSFlorian Westphal { 696ea6e574eSFlorian Westphal return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup); 697ea6e574eSFlorian Westphal } 698ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup); 699ea6e574eSFlorian Westphal 7009acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, 7019acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *saddr, int oif, int strict) 702c71099acSThomas Graf { 7034c9483b2SDavid S. Miller struct flowi6 fl6 = { 7044c9483b2SDavid S. Miller .flowi6_oif = oif, 7054c9483b2SDavid S. Miller .daddr = *daddr, 706c71099acSThomas Graf }; 707c71099acSThomas Graf struct dst_entry *dst; 70877d16f45SYOSHIFUJI Hideaki int flags = strict ? RT6_LOOKUP_F_IFACE : 0; 709c71099acSThomas Graf 710adaa70bbSThomas Graf if (saddr) { 7114c9483b2SDavid S. Miller memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 712adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 713adaa70bbSThomas Graf } 714adaa70bbSThomas Graf 7154c9483b2SDavid S. Miller dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup); 716c71099acSThomas Graf if (dst->error == 0) 717c71099acSThomas Graf return (struct rt6_info *) dst; 718c71099acSThomas Graf 719c71099acSThomas Graf dst_release(dst); 720c71099acSThomas Graf 7211da177e4SLinus Torvalds return NULL; 7221da177e4SLinus Torvalds } 7231da177e4SLinus Torvalds 7247159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup); 7257159039aSYOSHIFUJI Hideaki 726c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock. 7271da177e4SLinus Torvalds It takes new route entry, the addition fails by any reason the 7281da177e4SLinus Torvalds route is freed. In any case, if caller does not hold it, it may 7291da177e4SLinus Torvalds be destroyed. 7301da177e4SLinus Torvalds */ 7311da177e4SLinus Torvalds 73286872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info) 7331da177e4SLinus Torvalds { 7341da177e4SLinus Torvalds int err; 735c71099acSThomas Graf struct fib6_table *table; 7361da177e4SLinus Torvalds 737c71099acSThomas Graf table = rt->rt6i_table; 738c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 73986872cb5SThomas Graf err = fib6_add(&table->tb6_root, rt, info); 740c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds return err; 7431da177e4SLinus Torvalds } 7441da177e4SLinus Torvalds 74540e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt) 74640e22e8fSThomas Graf { 7474d1169c1SDenis V. Lunev struct nl_info info = { 748d1918542SDavid S. Miller .nl_net = dev_net(rt->dst.dev), 7494d1169c1SDenis V. Lunev }; 750528c4cebSDenis V. Lunev return __ip6_ins_rt(rt, &info); 75140e22e8fSThomas Graf } 75240e22e8fSThomas Graf 7531716a961SGao feng static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, 75421efcfa0SEric Dumazet const struct in6_addr *daddr, 755b71d1d42SEric Dumazet const struct in6_addr *saddr) 7561da177e4SLinus Torvalds { 7571da177e4SLinus Torvalds struct rt6_info *rt; 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds /* 7601da177e4SLinus Torvalds * Clone the route. 7611da177e4SLinus Torvalds */ 7621da177e4SLinus Torvalds 76321efcfa0SEric Dumazet rt = ip6_rt_copy(ort, daddr); 7641da177e4SLinus Torvalds 7651da177e4SLinus Torvalds if (rt) { 76614deae41SDavid S. Miller int attempts = !in_softirq(); 76714deae41SDavid S. Miller 76858c4fb86SYOSHIFUJI Hideaki if (!(rt->rt6i_flags & RTF_GATEWAY)) { 769bb3c3686SDavid S. Miller if (ort->rt6i_dst.plen != 128 && 77021efcfa0SEric Dumazet ipv6_addr_equal(&ort->rt6i_dst.addr, daddr)) 77158c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 7724e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = *daddr; 77358c4fb86SYOSHIFUJI Hideaki } 7741da177e4SLinus Torvalds 7751da177e4SLinus Torvalds rt->rt6i_flags |= RTF_CACHE; 7761da177e4SLinus Torvalds 7771da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 7781da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 7794e3fd7a0SAlexey Dobriyan rt->rt6i_src.addr = *saddr; 7801da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 7811da177e4SLinus Torvalds } 7821da177e4SLinus Torvalds #endif 7831da177e4SLinus Torvalds 78414deae41SDavid S. Miller retry: 7858ade06c6SDavid S. Miller if (rt6_bind_neighbour(rt, rt->dst.dev)) { 786d1918542SDavid S. Miller struct net *net = dev_net(rt->dst.dev); 78714deae41SDavid S. Miller int saved_rt_min_interval = 78814deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_min_interval; 78914deae41SDavid S. Miller int saved_rt_elasticity = 79014deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_elasticity; 79114deae41SDavid S. Miller 79214deae41SDavid S. Miller if (attempts-- > 0) { 79314deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_elasticity = 1; 79414deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_min_interval = 0; 79514deae41SDavid S. Miller 79686393e52SAlexey Dobriyan ip6_dst_gc(&net->ipv6.ip6_dst_ops); 79714deae41SDavid S. Miller 79814deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_elasticity = 79914deae41SDavid S. Miller saved_rt_elasticity; 80014deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_min_interval = 80114deae41SDavid S. Miller saved_rt_min_interval; 80214deae41SDavid S. Miller goto retry; 80314deae41SDavid S. Miller } 80414deae41SDavid S. Miller 805f3213831SJoe Perches net_warn_ratelimited("Neighbour table overflow\n"); 806d8d1f30bSChangli Gao dst_free(&rt->dst); 80714deae41SDavid S. Miller return NULL; 80814deae41SDavid S. Miller } 80995a9a5baSYOSHIFUJI Hideaki } 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds return rt; 8121da177e4SLinus Torvalds } 81395a9a5baSYOSHIFUJI Hideaki 81421efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, 81521efcfa0SEric Dumazet const struct in6_addr *daddr) 816299d9939SYOSHIFUJI Hideaki { 81721efcfa0SEric Dumazet struct rt6_info *rt = ip6_rt_copy(ort, daddr); 81821efcfa0SEric Dumazet 819299d9939SYOSHIFUJI Hideaki if (rt) { 820299d9939SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_CACHE; 82127217455SDavid Miller dst_set_neighbour(&rt->dst, neigh_clone(dst_get_neighbour_noref_raw(&ort->dst))); 822299d9939SYOSHIFUJI Hideaki } 823299d9939SYOSHIFUJI Hideaki return rt; 824299d9939SYOSHIFUJI Hideaki } 825299d9939SYOSHIFUJI Hideaki 8268ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif, 8274c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 8281da177e4SLinus Torvalds { 8291da177e4SLinus Torvalds struct fib6_node *fn; 830519fbd87SYOSHIFUJI Hideaki struct rt6_info *rt, *nrt; 831c71099acSThomas Graf int strict = 0; 8321da177e4SLinus Torvalds int attempts = 3; 833519fbd87SYOSHIFUJI Hideaki int err; 83453b7997fSYOSHIFUJI Hideaki int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE; 8351da177e4SLinus Torvalds 83677d16f45SYOSHIFUJI Hideaki strict |= flags & RT6_LOOKUP_F_IFACE; 8371da177e4SLinus Torvalds 8381da177e4SLinus Torvalds relookup: 839c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 8401da177e4SLinus Torvalds 8418238dd06SYOSHIFUJI Hideaki restart_2: 8424c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds restart: 8454acad72dSPavel Emelyanov rt = rt6_select(fn, oif, strict | reachable); 8468ed67789SDaniel Lezcano 8474c9483b2SDavid S. Miller BACKTRACK(net, &fl6->saddr); 8488ed67789SDaniel Lezcano if (rt == net->ipv6.ip6_null_entry || 8498238dd06SYOSHIFUJI Hideaki rt->rt6i_flags & RTF_CACHE) 8501da177e4SLinus Torvalds goto out; 8511da177e4SLinus Torvalds 852d8d1f30bSChangli Gao dst_hold(&rt->dst); 853c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 8541da177e4SLinus Torvalds 85527217455SDavid Miller if (!dst_get_neighbour_noref_raw(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP)) 8564c9483b2SDavid S. Miller nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr); 8577343ff31SDavid S. Miller else if (!(rt->dst.flags & DST_HOST)) 8584c9483b2SDavid S. Miller nrt = rt6_alloc_clone(rt, &fl6->daddr); 8597343ff31SDavid S. Miller else 8607343ff31SDavid S. Miller goto out2; 8611da177e4SLinus Torvalds 862d8d1f30bSChangli Gao dst_release(&rt->dst); 8638ed67789SDaniel Lezcano rt = nrt ? : net->ipv6.ip6_null_entry; 8641da177e4SLinus Torvalds 865d8d1f30bSChangli Gao dst_hold(&rt->dst); 866e40cf353SYOSHIFUJI Hideaki if (nrt) { 86740e22e8fSThomas Graf err = ip6_ins_rt(nrt); 868e40cf353SYOSHIFUJI Hideaki if (!err) 869e40cf353SYOSHIFUJI Hideaki goto out2; 870e40cf353SYOSHIFUJI Hideaki } 871e40cf353SYOSHIFUJI Hideaki 872e40cf353SYOSHIFUJI Hideaki if (--attempts <= 0) 8731da177e4SLinus Torvalds goto out2; 8741da177e4SLinus Torvalds 875519fbd87SYOSHIFUJI Hideaki /* 876c71099acSThomas Graf * Race condition! In the gap, when table->tb6_lock was 877519fbd87SYOSHIFUJI Hideaki * released someone could insert this route. Relookup. 8781da177e4SLinus Torvalds */ 879d8d1f30bSChangli Gao dst_release(&rt->dst); 8801da177e4SLinus Torvalds goto relookup; 881e40cf353SYOSHIFUJI Hideaki 882519fbd87SYOSHIFUJI Hideaki out: 8838238dd06SYOSHIFUJI Hideaki if (reachable) { 8848238dd06SYOSHIFUJI Hideaki reachable = 0; 8858238dd06SYOSHIFUJI Hideaki goto restart_2; 8868238dd06SYOSHIFUJI Hideaki } 887d8d1f30bSChangli Gao dst_hold(&rt->dst); 888c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 8891da177e4SLinus Torvalds out2: 890d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 891d8d1f30bSChangli Gao rt->dst.__use++; 892c71099acSThomas Graf 893c71099acSThomas Graf return rt; 894c71099acSThomas Graf } 895c71099acSThomas Graf 8968ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table, 8974c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 8984acad72dSPavel Emelyanov { 8994c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags); 9004acad72dSPavel Emelyanov } 9014acad72dSPavel Emelyanov 90272331bc0SShmulik Ladkani static struct dst_entry *ip6_route_input_lookup(struct net *net, 90372331bc0SShmulik Ladkani struct net_device *dev, 90472331bc0SShmulik Ladkani struct flowi6 *fl6, int flags) 90572331bc0SShmulik Ladkani { 90672331bc0SShmulik Ladkani if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) 90772331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_IFACE; 90872331bc0SShmulik Ladkani 90972331bc0SShmulik Ladkani return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input); 91072331bc0SShmulik Ladkani } 91172331bc0SShmulik Ladkani 912c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 913c71099acSThomas Graf { 914b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 915c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 916adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 9174c9483b2SDavid S. Miller struct flowi6 fl6 = { 9184c9483b2SDavid S. Miller .flowi6_iif = skb->dev->ifindex, 9194c9483b2SDavid S. Miller .daddr = iph->daddr, 9204c9483b2SDavid S. Miller .saddr = iph->saddr, 9214c9483b2SDavid S. Miller .flowlabel = (* (__be32 *) iph) & IPV6_FLOWINFO_MASK, 9224c9483b2SDavid S. Miller .flowi6_mark = skb->mark, 9234c9483b2SDavid S. Miller .flowi6_proto = iph->nexthdr, 924c71099acSThomas Graf }; 925adaa70bbSThomas Graf 92672331bc0SShmulik Ladkani skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags)); 927c71099acSThomas Graf } 928c71099acSThomas Graf 9298ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table, 9304c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 931c71099acSThomas Graf { 9324c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags); 933c71099acSThomas Graf } 934c71099acSThomas Graf 9359c7a4f9cSFlorian Westphal struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk, 9364c9483b2SDavid S. Miller struct flowi6 *fl6) 937c71099acSThomas Graf { 938c71099acSThomas Graf int flags = 0; 939c71099acSThomas Graf 9404c9483b2SDavid S. Miller if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr)) 94177d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 942c71099acSThomas Graf 9434c9483b2SDavid S. Miller if (!ipv6_addr_any(&fl6->saddr)) 944adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 9450c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 9460c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 947adaa70bbSThomas Graf 9484c9483b2SDavid S. Miller return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output); 9491da177e4SLinus Torvalds } 9501da177e4SLinus Torvalds 9517159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output); 9521da177e4SLinus Torvalds 9532774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 95414e50e57SDavid S. Miller { 9555c1e6aa3SDavid S. Miller struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; 95614e50e57SDavid S. Miller struct dst_entry *new = NULL; 95714e50e57SDavid S. Miller 9585c1e6aa3SDavid S. Miller rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, 0, 0); 95914e50e57SDavid S. Miller if (rt) { 960cf911662SDavid S. Miller memset(&rt->rt6i_table, 0, sizeof(*rt) - sizeof(struct dst_entry)); 96197bab73fSDavid S. Miller rt6_init_peer(rt, net->ipv6.peers); 962cf911662SDavid S. Miller 963d8d1f30bSChangli Gao new = &rt->dst; 96414e50e57SDavid S. Miller 96514e50e57SDavid S. Miller new->__use = 1; 966352e512cSHerbert Xu new->input = dst_discard; 967352e512cSHerbert Xu new->output = dst_discard; 96814e50e57SDavid S. Miller 96921efcfa0SEric Dumazet if (dst_metrics_read_only(&ort->dst)) 97021efcfa0SEric Dumazet new->_metrics = ort->dst._metrics; 97121efcfa0SEric Dumazet else 972defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 97314e50e57SDavid S. Miller rt->rt6i_idev = ort->rt6i_idev; 97414e50e57SDavid S. Miller if (rt->rt6i_idev) 97514e50e57SDavid S. Miller in6_dev_hold(rt->rt6i_idev); 97614e50e57SDavid S. Miller 9774e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 9781716a961SGao feng rt->rt6i_flags = ort->rt6i_flags; 9791716a961SGao feng rt6_clean_expires(rt); 98014e50e57SDavid S. Miller rt->rt6i_metric = 0; 98114e50e57SDavid S. Miller 98214e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 98314e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 98414e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 98514e50e57SDavid S. Miller #endif 98614e50e57SDavid S. Miller 98714e50e57SDavid S. Miller dst_free(new); 98814e50e57SDavid S. Miller } 98914e50e57SDavid S. Miller 99069ead7afSDavid S. Miller dst_release(dst_orig); 99169ead7afSDavid S. Miller return new ? new : ERR_PTR(-ENOMEM); 99214e50e57SDavid S. Miller } 99314e50e57SDavid S. Miller 9941da177e4SLinus Torvalds /* 9951da177e4SLinus Torvalds * Destination cache support functions 9961da177e4SLinus Torvalds */ 9971da177e4SLinus Torvalds 9981da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 9991da177e4SLinus Torvalds { 10001da177e4SLinus Torvalds struct rt6_info *rt; 10011da177e4SLinus Torvalds 10021da177e4SLinus Torvalds rt = (struct rt6_info *) dst; 10031da177e4SLinus Torvalds 10046431cbc2SDavid S. Miller if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) { 10056431cbc2SDavid S. Miller if (rt->rt6i_peer_genid != rt6_peer_genid()) { 100697bab73fSDavid S. Miller if (!rt6_has_peer(rt)) 10076431cbc2SDavid S. Miller rt6_bind_peer(rt, 0); 10086431cbc2SDavid S. Miller rt->rt6i_peer_genid = rt6_peer_genid(); 10096431cbc2SDavid S. Miller } 10101da177e4SLinus Torvalds return dst; 10116431cbc2SDavid S. Miller } 10121da177e4SLinus Torvalds return NULL; 10131da177e4SLinus Torvalds } 10141da177e4SLinus Torvalds 10151da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 10161da177e4SLinus Torvalds { 10171da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 10181da177e4SLinus Torvalds 10191da177e4SLinus Torvalds if (rt) { 102054c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 102154c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 1022e0a1ad73SThomas Graf ip6_del_rt(rt); 102354c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 10241da177e4SLinus Torvalds } 102554c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 102654c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 102754c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 102854c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 102954c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 103054c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 10311da177e4SLinus Torvalds } 10321da177e4SLinus Torvalds 10331da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 10341da177e4SLinus Torvalds { 10351da177e4SLinus Torvalds struct rt6_info *rt; 10361da177e4SLinus Torvalds 10373ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 10381da177e4SLinus Torvalds 1039adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 10401da177e4SLinus Torvalds if (rt) { 10411716a961SGao feng if (rt->rt6i_flags & RTF_CACHE) 10421716a961SGao feng rt6_update_expires(rt, 0); 10431716a961SGao feng else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) 10441da177e4SLinus Torvalds rt->rt6i_node->fn_sernum = -1; 10451da177e4SLinus Torvalds } 10461da177e4SLinus Torvalds } 10471da177e4SLinus Torvalds 10481da177e4SLinus Torvalds static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu) 10491da177e4SLinus Torvalds { 10501da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info*)dst; 10511da177e4SLinus Torvalds 105281aded24SDavid S. Miller dst_confirm(dst); 10531da177e4SLinus Torvalds if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) { 105481aded24SDavid S. Miller struct net *net = dev_net(dst->dev); 105581aded24SDavid S. Miller 10561da177e4SLinus Torvalds rt6->rt6i_flags |= RTF_MODIFIED; 10571da177e4SLinus Torvalds if (mtu < IPV6_MIN_MTU) { 1058defb3519SDavid S. Miller u32 features = dst_metric(dst, RTAX_FEATURES); 10591da177e4SLinus Torvalds mtu = IPV6_MIN_MTU; 1060defb3519SDavid S. Miller features |= RTAX_FEATURE_ALLFRAG; 1061defb3519SDavid S. Miller dst_metric_set(dst, RTAX_FEATURES, features); 10621da177e4SLinus Torvalds } 1063defb3519SDavid S. Miller dst_metric_set(dst, RTAX_MTU, mtu); 106481aded24SDavid S. Miller rt6_update_expires(rt6, net->ipv6.sysctl.ip6_rt_mtu_expires); 10651da177e4SLinus Torvalds } 10661da177e4SLinus Torvalds } 10671da177e4SLinus Torvalds 1068*42ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, 1069*42ae66c8SDavid S. Miller int oif, u32 mark) 107081aded24SDavid S. Miller { 107181aded24SDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 107281aded24SDavid S. Miller struct dst_entry *dst; 107381aded24SDavid S. Miller struct flowi6 fl6; 107481aded24SDavid S. Miller 107581aded24SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 107681aded24SDavid S. Miller fl6.flowi6_oif = oif; 107781aded24SDavid S. Miller fl6.flowi6_mark = mark; 107881aded24SDavid S. Miller fl6.flowi6_flags = FLOWI_FLAG_PRECOW_METRICS; 107981aded24SDavid S. Miller fl6.daddr = iph->daddr; 108081aded24SDavid S. Miller fl6.saddr = iph->saddr; 108181aded24SDavid S. Miller fl6.flowlabel = (*(__be32 *) iph) & IPV6_FLOWINFO_MASK; 108281aded24SDavid S. Miller 108381aded24SDavid S. Miller dst = ip6_route_output(net, NULL, &fl6); 108481aded24SDavid S. Miller if (!dst->error) 108581aded24SDavid S. Miller ip6_rt_update_pmtu(dst, ntohl(mtu)); 108681aded24SDavid S. Miller dst_release(dst); 108781aded24SDavid S. Miller } 108881aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu); 108981aded24SDavid S. Miller 109081aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) 109181aded24SDavid S. Miller { 109281aded24SDavid S. Miller ip6_update_pmtu(skb, sock_net(sk), mtu, 109381aded24SDavid S. Miller sk->sk_bound_dev_if, sk->sk_mark); 109481aded24SDavid S. Miller } 109581aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); 109681aded24SDavid S. Miller 10970dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 10981da177e4SLinus Torvalds { 10990dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 11000dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 11010dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 11020dbaee3bSDavid S. Miller 11031da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 11041da177e4SLinus Torvalds 11055578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 11065578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 11071da177e4SLinus Torvalds 11081da177e4SLinus Torvalds /* 11091da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 11101da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 11111da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 11121da177e4SLinus Torvalds * rely only on pmtu discovery" 11131da177e4SLinus Torvalds */ 11141da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 11151da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 11161da177e4SLinus Torvalds return mtu; 11171da177e4SLinus Torvalds } 11181da177e4SLinus Torvalds 1119ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst) 1120d33e4553SDavid S. Miller { 1121d33e4553SDavid S. Miller struct inet6_dev *idev; 1122618f9bc7SSteffen Klassert unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); 1123618f9bc7SSteffen Klassert 1124618f9bc7SSteffen Klassert if (mtu) 1125618f9bc7SSteffen Klassert return mtu; 1126618f9bc7SSteffen Klassert 1127618f9bc7SSteffen Klassert mtu = IPV6_MIN_MTU; 1128d33e4553SDavid S. Miller 1129d33e4553SDavid S. Miller rcu_read_lock(); 1130d33e4553SDavid S. Miller idev = __in6_dev_get(dst->dev); 1131d33e4553SDavid S. Miller if (idev) 1132d33e4553SDavid S. Miller mtu = idev->cnf.mtu6; 1133d33e4553SDavid S. Miller rcu_read_unlock(); 1134d33e4553SDavid S. Miller 1135d33e4553SDavid S. Miller return mtu; 1136d33e4553SDavid S. Miller } 1137d33e4553SDavid S. Miller 11383b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list; 11393b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock); 11405d0bbeebSThomas Graf 11413b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 11421da177e4SLinus Torvalds struct neighbour *neigh, 114387a11578SDavid S. Miller struct flowi6 *fl6) 11441da177e4SLinus Torvalds { 114587a11578SDavid S. Miller struct dst_entry *dst; 11461da177e4SLinus Torvalds struct rt6_info *rt; 11471da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 1148c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 11491da177e4SLinus Torvalds 115038308473SDavid S. Miller if (unlikely(!idev)) 1151122bdf67SEric Dumazet return ERR_PTR(-ENODEV); 11521da177e4SLinus Torvalds 11538b96d22dSDavid S. Miller rt = ip6_dst_alloc(net, dev, 0, NULL); 115438308473SDavid S. Miller if (unlikely(!rt)) { 11551da177e4SLinus Torvalds in6_dev_put(idev); 115687a11578SDavid S. Miller dst = ERR_PTR(-ENOMEM); 11571da177e4SLinus Torvalds goto out; 11581da177e4SLinus Torvalds } 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds if (neigh) 11611da177e4SLinus Torvalds neigh_hold(neigh); 116214deae41SDavid S. Miller else { 1163f83c7790SDavid S. Miller neigh = ip6_neigh_lookup(&rt->dst, &fl6->daddr); 1164b43faac6SDavid S. Miller if (IS_ERR(neigh)) { 1165252c3d84SRongQing.Li in6_dev_put(idev); 1166b43faac6SDavid S. Miller dst_free(&rt->dst); 1167b43faac6SDavid S. Miller return ERR_CAST(neigh); 1168b43faac6SDavid S. Miller } 116914deae41SDavid S. Miller } 11701da177e4SLinus Torvalds 11718e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 11728e2ec639SYan, Zheng rt->dst.output = ip6_output; 117369cce1d1SDavid S. Miller dst_set_neighbour(&rt->dst, neigh); 1174d8d1f30bSChangli Gao atomic_set(&rt->dst.__refcnt, 1); 117587a11578SDavid S. Miller rt->rt6i_dst.addr = fl6->daddr; 11768e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 11778e2ec639SYan, Zheng rt->rt6i_idev = idev; 11787011687fSGao feng dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255); 11791da177e4SLinus Torvalds 11803b00944cSYOSHIFUJI Hideaki spin_lock_bh(&icmp6_dst_lock); 1181d8d1f30bSChangli Gao rt->dst.next = icmp6_dst_gc_list; 1182d8d1f30bSChangli Gao icmp6_dst_gc_list = &rt->dst; 11833b00944cSYOSHIFUJI Hideaki spin_unlock_bh(&icmp6_dst_lock); 11841da177e4SLinus Torvalds 11855578689aSDaniel Lezcano fib6_force_start_gc(net); 11861da177e4SLinus Torvalds 118787a11578SDavid S. Miller dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); 118887a11578SDavid S. Miller 11891da177e4SLinus Torvalds out: 119087a11578SDavid S. Miller return dst; 11911da177e4SLinus Torvalds } 11921da177e4SLinus Torvalds 11933d0f24a7SStephen Hemminger int icmp6_dst_gc(void) 11941da177e4SLinus Torvalds { 1195e9476e95SHagen Paul Pfeifer struct dst_entry *dst, **pprev; 11963d0f24a7SStephen Hemminger int more = 0; 11971da177e4SLinus Torvalds 11983b00944cSYOSHIFUJI Hideaki spin_lock_bh(&icmp6_dst_lock); 11993b00944cSYOSHIFUJI Hideaki pprev = &icmp6_dst_gc_list; 12005d0bbeebSThomas Graf 12011da177e4SLinus Torvalds while ((dst = *pprev) != NULL) { 12021da177e4SLinus Torvalds if (!atomic_read(&dst->__refcnt)) { 12031da177e4SLinus Torvalds *pprev = dst->next; 12041da177e4SLinus Torvalds dst_free(dst); 12051da177e4SLinus Torvalds } else { 12061da177e4SLinus Torvalds pprev = &dst->next; 12073d0f24a7SStephen Hemminger ++more; 12081da177e4SLinus Torvalds } 12091da177e4SLinus Torvalds } 12101da177e4SLinus Torvalds 12113b00944cSYOSHIFUJI Hideaki spin_unlock_bh(&icmp6_dst_lock); 12125d0bbeebSThomas Graf 12133d0f24a7SStephen Hemminger return more; 12141da177e4SLinus Torvalds } 12151da177e4SLinus Torvalds 12161e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg), 12171e493d19SDavid S. Miller void *arg) 12181e493d19SDavid S. Miller { 12191e493d19SDavid S. Miller struct dst_entry *dst, **pprev; 12201e493d19SDavid S. Miller 12211e493d19SDavid S. Miller spin_lock_bh(&icmp6_dst_lock); 12221e493d19SDavid S. Miller pprev = &icmp6_dst_gc_list; 12231e493d19SDavid S. Miller while ((dst = *pprev) != NULL) { 12241e493d19SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 12251e493d19SDavid S. Miller if (func(rt, arg)) { 12261e493d19SDavid S. Miller *pprev = dst->next; 12271e493d19SDavid S. Miller dst_free(dst); 12281e493d19SDavid S. Miller } else { 12291e493d19SDavid S. Miller pprev = &dst->next; 12301e493d19SDavid S. Miller } 12311e493d19SDavid S. Miller } 12321e493d19SDavid S. Miller spin_unlock_bh(&icmp6_dst_lock); 12331e493d19SDavid S. Miller } 12341e493d19SDavid S. Miller 1235569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops) 12361da177e4SLinus Torvalds { 12371da177e4SLinus Torvalds unsigned long now = jiffies; 123886393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 12397019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 12407019b78eSDaniel Lezcano int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; 12417019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 12427019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 12437019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 1244fc66f95cSEric Dumazet int entries; 12451da177e4SLinus Torvalds 1246fc66f95cSEric Dumazet entries = dst_entries_get_fast(ops); 12477019b78eSDaniel Lezcano if (time_after(rt_last_gc + rt_min_interval, now) && 1248fc66f95cSEric Dumazet entries <= rt_max_size) 12491da177e4SLinus Torvalds goto out; 12501da177e4SLinus Torvalds 12516891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire++; 12526891a346SBenjamin Thery fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net); 12536891a346SBenjamin Thery net->ipv6.ip6_rt_last_gc = now; 1254fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 1255fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 12567019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; 12571da177e4SLinus Torvalds out: 12587019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; 1259fc66f95cSEric Dumazet return entries > rt_max_size; 12601da177e4SLinus Torvalds } 12611da177e4SLinus Torvalds 12621da177e4SLinus Torvalds /* Clean host part of a prefix. Not necessary in radix tree, 12631da177e4SLinus Torvalds but results in cleaner routing tables. 12641da177e4SLinus Torvalds 12651da177e4SLinus Torvalds Remove it only when all the things will work! 12661da177e4SLinus Torvalds */ 12671da177e4SLinus Torvalds 12686b75d090SYOSHIFUJI Hideaki int ip6_dst_hoplimit(struct dst_entry *dst) 12691da177e4SLinus Torvalds { 12705170ae82SDavid S. Miller int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT); 1271a02e4b7dSDavid S. Miller if (hoplimit == 0) { 12726b75d090SYOSHIFUJI Hideaki struct net_device *dev = dst->dev; 1273c68f24ccSEric Dumazet struct inet6_dev *idev; 1274c68f24ccSEric Dumazet 1275c68f24ccSEric Dumazet rcu_read_lock(); 1276c68f24ccSEric Dumazet idev = __in6_dev_get(dev); 1277c68f24ccSEric Dumazet if (idev) 12781da177e4SLinus Torvalds hoplimit = idev->cnf.hop_limit; 1279c68f24ccSEric Dumazet else 128053b7997fSYOSHIFUJI Hideaki hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit; 1281c68f24ccSEric Dumazet rcu_read_unlock(); 12821da177e4SLinus Torvalds } 12831da177e4SLinus Torvalds return hoplimit; 12841da177e4SLinus Torvalds } 1285abbf46aeSDavid S. Miller EXPORT_SYMBOL(ip6_dst_hoplimit); 12861da177e4SLinus Torvalds 12871da177e4SLinus Torvalds /* 12881da177e4SLinus Torvalds * 12891da177e4SLinus Torvalds */ 12901da177e4SLinus Torvalds 129186872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg) 12921da177e4SLinus Torvalds { 12931da177e4SLinus Torvalds int err; 12945578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 12951da177e4SLinus Torvalds struct rt6_info *rt = NULL; 12961da177e4SLinus Torvalds struct net_device *dev = NULL; 12971da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 1298c71099acSThomas Graf struct fib6_table *table; 12991da177e4SLinus Torvalds int addr_type; 13001da177e4SLinus Torvalds 130186872cb5SThomas Graf if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128) 13021da177e4SLinus Torvalds return -EINVAL; 13031da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 130486872cb5SThomas Graf if (cfg->fc_src_len) 13051da177e4SLinus Torvalds return -EINVAL; 13061da177e4SLinus Torvalds #endif 130786872cb5SThomas Graf if (cfg->fc_ifindex) { 13081da177e4SLinus Torvalds err = -ENODEV; 13095578689aSDaniel Lezcano dev = dev_get_by_index(net, cfg->fc_ifindex); 13101da177e4SLinus Torvalds if (!dev) 13111da177e4SLinus Torvalds goto out; 13121da177e4SLinus Torvalds idev = in6_dev_get(dev); 13131da177e4SLinus Torvalds if (!idev) 13141da177e4SLinus Torvalds goto out; 13151da177e4SLinus Torvalds } 13161da177e4SLinus Torvalds 131786872cb5SThomas Graf if (cfg->fc_metric == 0) 131886872cb5SThomas Graf cfg->fc_metric = IP6_RT_PRIO_USER; 13191da177e4SLinus Torvalds 1320c71099acSThomas Graf err = -ENOBUFS; 132138308473SDavid S. Miller if (cfg->fc_nlinfo.nlh && 1322d71314b4SMatti Vaittinen !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { 1323d71314b4SMatti Vaittinen table = fib6_get_table(net, cfg->fc_table); 132438308473SDavid S. Miller if (!table) { 1325f3213831SJoe Perches pr_warn("NLM_F_CREATE should be specified when creating new route\n"); 1326d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 1327d71314b4SMatti Vaittinen } 1328d71314b4SMatti Vaittinen } else { 1329d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 1330d71314b4SMatti Vaittinen } 133138308473SDavid S. Miller 133238308473SDavid S. Miller if (!table) 1333c71099acSThomas Graf goto out; 1334c71099acSThomas Graf 13358b96d22dSDavid S. Miller rt = ip6_dst_alloc(net, NULL, DST_NOCOUNT, table); 13361da177e4SLinus Torvalds 133738308473SDavid S. Miller if (!rt) { 13381da177e4SLinus Torvalds err = -ENOMEM; 13391da177e4SLinus Torvalds goto out; 13401da177e4SLinus Torvalds } 13411da177e4SLinus Torvalds 1342d8d1f30bSChangli Gao rt->dst.obsolete = -1; 13431716a961SGao feng 13441716a961SGao feng if (cfg->fc_flags & RTF_EXPIRES) 13451716a961SGao feng rt6_set_expires(rt, jiffies + 13461716a961SGao feng clock_t_to_jiffies(cfg->fc_expires)); 13471716a961SGao feng else 13481716a961SGao feng rt6_clean_expires(rt); 13491da177e4SLinus Torvalds 135086872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 135186872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 135286872cb5SThomas Graf rt->rt6i_protocol = cfg->fc_protocol; 135386872cb5SThomas Graf 135486872cb5SThomas Graf addr_type = ipv6_addr_type(&cfg->fc_dst); 13551da177e4SLinus Torvalds 13561da177e4SLinus Torvalds if (addr_type & IPV6_ADDR_MULTICAST) 1357d8d1f30bSChangli Gao rt->dst.input = ip6_mc_input; 1358ab79ad14SMaciej Żenczykowski else if (cfg->fc_flags & RTF_LOCAL) 1359ab79ad14SMaciej Żenczykowski rt->dst.input = ip6_input; 13601da177e4SLinus Torvalds else 1361d8d1f30bSChangli Gao rt->dst.input = ip6_forward; 13621da177e4SLinus Torvalds 1363d8d1f30bSChangli Gao rt->dst.output = ip6_output; 13641da177e4SLinus Torvalds 136586872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 136686872cb5SThomas Graf rt->rt6i_dst.plen = cfg->fc_dst_len; 13671da177e4SLinus Torvalds if (rt->rt6i_dst.plen == 128) 136811d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 13691da177e4SLinus Torvalds 13708e2ec639SYan, Zheng if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) { 13718e2ec639SYan, Zheng u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL); 13728e2ec639SYan, Zheng if (!metrics) { 13738e2ec639SYan, Zheng err = -ENOMEM; 13748e2ec639SYan, Zheng goto out; 13758e2ec639SYan, Zheng } 13768e2ec639SYan, Zheng dst_init_metrics(&rt->dst, metrics, 0); 13778e2ec639SYan, Zheng } 13781da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 137986872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len); 138086872cb5SThomas Graf rt->rt6i_src.plen = cfg->fc_src_len; 13811da177e4SLinus Torvalds #endif 13821da177e4SLinus Torvalds 138386872cb5SThomas Graf rt->rt6i_metric = cfg->fc_metric; 13841da177e4SLinus Torvalds 13851da177e4SLinus Torvalds /* We cannot add true routes via loopback here, 13861da177e4SLinus Torvalds they would result in kernel looping; promote them to reject routes 13871da177e4SLinus Torvalds */ 138886872cb5SThomas Graf if ((cfg->fc_flags & RTF_REJECT) || 138938308473SDavid S. Miller (dev && (dev->flags & IFF_LOOPBACK) && 139038308473SDavid S. Miller !(addr_type & IPV6_ADDR_LOOPBACK) && 139138308473SDavid S. Miller !(cfg->fc_flags & RTF_LOCAL))) { 13921da177e4SLinus Torvalds /* hold loopback dev/idev if we haven't done so. */ 13935578689aSDaniel Lezcano if (dev != net->loopback_dev) { 13941da177e4SLinus Torvalds if (dev) { 13951da177e4SLinus Torvalds dev_put(dev); 13961da177e4SLinus Torvalds in6_dev_put(idev); 13971da177e4SLinus Torvalds } 13985578689aSDaniel Lezcano dev = net->loopback_dev; 13991da177e4SLinus Torvalds dev_hold(dev); 14001da177e4SLinus Torvalds idev = in6_dev_get(dev); 14011da177e4SLinus Torvalds if (!idev) { 14021da177e4SLinus Torvalds err = -ENODEV; 14031da177e4SLinus Torvalds goto out; 14041da177e4SLinus Torvalds } 14051da177e4SLinus Torvalds } 1406d8d1f30bSChangli Gao rt->dst.output = ip6_pkt_discard_out; 1407d8d1f30bSChangli Gao rt->dst.input = ip6_pkt_discard; 1408d8d1f30bSChangli Gao rt->dst.error = -ENETUNREACH; 14091da177e4SLinus Torvalds rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP; 14101da177e4SLinus Torvalds goto install_route; 14111da177e4SLinus Torvalds } 14121da177e4SLinus Torvalds 141386872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY) { 1414b71d1d42SEric Dumazet const struct in6_addr *gw_addr; 14151da177e4SLinus Torvalds int gwa_type; 14161da177e4SLinus Torvalds 141786872cb5SThomas Graf gw_addr = &cfg->fc_gateway; 14184e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = *gw_addr; 14191da177e4SLinus Torvalds gwa_type = ipv6_addr_type(gw_addr); 14201da177e4SLinus Torvalds 14211da177e4SLinus Torvalds if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) { 14221da177e4SLinus Torvalds struct rt6_info *grt; 14231da177e4SLinus Torvalds 14241da177e4SLinus Torvalds /* IPv6 strictly inhibits using not link-local 14251da177e4SLinus Torvalds addresses as nexthop address. 14261da177e4SLinus Torvalds Otherwise, router will not able to send redirects. 14271da177e4SLinus Torvalds It is very good, but in some (rare!) circumstances 14281da177e4SLinus Torvalds (SIT, PtP, NBMA NOARP links) it is handy to allow 14291da177e4SLinus Torvalds some exceptions. --ANK 14301da177e4SLinus Torvalds */ 14311da177e4SLinus Torvalds err = -EINVAL; 14321da177e4SLinus Torvalds if (!(gwa_type & IPV6_ADDR_UNICAST)) 14331da177e4SLinus Torvalds goto out; 14341da177e4SLinus Torvalds 14355578689aSDaniel Lezcano grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1); 14361da177e4SLinus Torvalds 14371da177e4SLinus Torvalds err = -EHOSTUNREACH; 143838308473SDavid S. Miller if (!grt) 14391da177e4SLinus Torvalds goto out; 14401da177e4SLinus Torvalds if (dev) { 1441d1918542SDavid S. Miller if (dev != grt->dst.dev) { 1442d8d1f30bSChangli Gao dst_release(&grt->dst); 14431da177e4SLinus Torvalds goto out; 14441da177e4SLinus Torvalds } 14451da177e4SLinus Torvalds } else { 1446d1918542SDavid S. Miller dev = grt->dst.dev; 14471da177e4SLinus Torvalds idev = grt->rt6i_idev; 14481da177e4SLinus Torvalds dev_hold(dev); 14491da177e4SLinus Torvalds in6_dev_hold(grt->rt6i_idev); 14501da177e4SLinus Torvalds } 14511da177e4SLinus Torvalds if (!(grt->rt6i_flags & RTF_GATEWAY)) 14521da177e4SLinus Torvalds err = 0; 1453d8d1f30bSChangli Gao dst_release(&grt->dst); 14541da177e4SLinus Torvalds 14551da177e4SLinus Torvalds if (err) 14561da177e4SLinus Torvalds goto out; 14571da177e4SLinus Torvalds } 14581da177e4SLinus Torvalds err = -EINVAL; 145938308473SDavid S. Miller if (!dev || (dev->flags & IFF_LOOPBACK)) 14601da177e4SLinus Torvalds goto out; 14611da177e4SLinus Torvalds } 14621da177e4SLinus Torvalds 14631da177e4SLinus Torvalds err = -ENODEV; 146438308473SDavid S. Miller if (!dev) 14651da177e4SLinus Torvalds goto out; 14661da177e4SLinus Torvalds 1467c3968a85SDaniel Walter if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 1468c3968a85SDaniel Walter if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 1469c3968a85SDaniel Walter err = -EINVAL; 1470c3968a85SDaniel Walter goto out; 1471c3968a85SDaniel Walter } 14724e3fd7a0SAlexey Dobriyan rt->rt6i_prefsrc.addr = cfg->fc_prefsrc; 1473c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 128; 1474c3968a85SDaniel Walter } else 1475c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 1476c3968a85SDaniel Walter 147786872cb5SThomas Graf if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) { 14788ade06c6SDavid S. Miller err = rt6_bind_neighbour(rt, dev); 1479f83c7790SDavid S. Miller if (err) 14801da177e4SLinus Torvalds goto out; 14811da177e4SLinus Torvalds } 14821da177e4SLinus Torvalds 148386872cb5SThomas Graf rt->rt6i_flags = cfg->fc_flags; 14841da177e4SLinus Torvalds 14851da177e4SLinus Torvalds install_route: 148686872cb5SThomas Graf if (cfg->fc_mx) { 148786872cb5SThomas Graf struct nlattr *nla; 148886872cb5SThomas Graf int remaining; 14891da177e4SLinus Torvalds 149086872cb5SThomas Graf nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { 14918f4c1f9bSThomas Graf int type = nla_type(nla); 149286872cb5SThomas Graf 149386872cb5SThomas Graf if (type) { 149486872cb5SThomas Graf if (type > RTAX_MAX) { 14951da177e4SLinus Torvalds err = -EINVAL; 14961da177e4SLinus Torvalds goto out; 14971da177e4SLinus Torvalds } 149886872cb5SThomas Graf 1499defb3519SDavid S. Miller dst_metric_set(&rt->dst, type, nla_get_u32(nla)); 15001da177e4SLinus Torvalds } 15011da177e4SLinus Torvalds } 15021da177e4SLinus Torvalds } 15031da177e4SLinus Torvalds 1504d8d1f30bSChangli Gao rt->dst.dev = dev; 15051da177e4SLinus Torvalds rt->rt6i_idev = idev; 1506c71099acSThomas Graf rt->rt6i_table = table; 150763152fc0SDaniel Lezcano 1508c346dca1SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = dev_net(dev); 150963152fc0SDaniel Lezcano 151086872cb5SThomas Graf return __ip6_ins_rt(rt, &cfg->fc_nlinfo); 15111da177e4SLinus Torvalds 15121da177e4SLinus Torvalds out: 15131da177e4SLinus Torvalds if (dev) 15141da177e4SLinus Torvalds dev_put(dev); 15151da177e4SLinus Torvalds if (idev) 15161da177e4SLinus Torvalds in6_dev_put(idev); 15171da177e4SLinus Torvalds if (rt) 1518d8d1f30bSChangli Gao dst_free(&rt->dst); 15191da177e4SLinus Torvalds return err; 15201da177e4SLinus Torvalds } 15211da177e4SLinus Torvalds 152286872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info) 15231da177e4SLinus Torvalds { 15241da177e4SLinus Torvalds int err; 1525c71099acSThomas Graf struct fib6_table *table; 1526d1918542SDavid S. Miller struct net *net = dev_net(rt->dst.dev); 15271da177e4SLinus Torvalds 15288ed67789SDaniel Lezcano if (rt == net->ipv6.ip6_null_entry) 15296c813a72SPatrick McHardy return -ENOENT; 15306c813a72SPatrick McHardy 1531c71099acSThomas Graf table = rt->rt6i_table; 1532c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 15331da177e4SLinus Torvalds 153486872cb5SThomas Graf err = fib6_del(rt, info); 1535d8d1f30bSChangli Gao dst_release(&rt->dst); 15361da177e4SLinus Torvalds 1537c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 15381da177e4SLinus Torvalds 15391da177e4SLinus Torvalds return err; 15401da177e4SLinus Torvalds } 15411da177e4SLinus Torvalds 1542e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt) 1543e0a1ad73SThomas Graf { 15444d1169c1SDenis V. Lunev struct nl_info info = { 1545d1918542SDavid S. Miller .nl_net = dev_net(rt->dst.dev), 15464d1169c1SDenis V. Lunev }; 1547528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 1548e0a1ad73SThomas Graf } 1549e0a1ad73SThomas Graf 155086872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg) 15511da177e4SLinus Torvalds { 1552c71099acSThomas Graf struct fib6_table *table; 15531da177e4SLinus Torvalds struct fib6_node *fn; 15541da177e4SLinus Torvalds struct rt6_info *rt; 15551da177e4SLinus Torvalds int err = -ESRCH; 15561da177e4SLinus Torvalds 15575578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 155838308473SDavid S. Miller if (!table) 1559c71099acSThomas Graf return err; 15601da177e4SLinus Torvalds 1561c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 1562c71099acSThomas Graf 1563c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 156486872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 156586872cb5SThomas Graf &cfg->fc_src, cfg->fc_src_len); 15661da177e4SLinus Torvalds 15671da177e4SLinus Torvalds if (fn) { 1568d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 156986872cb5SThomas Graf if (cfg->fc_ifindex && 1570d1918542SDavid S. Miller (!rt->dst.dev || 1571d1918542SDavid S. Miller rt->dst.dev->ifindex != cfg->fc_ifindex)) 15721da177e4SLinus Torvalds continue; 157386872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 157486872cb5SThomas Graf !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 15751da177e4SLinus Torvalds continue; 157686872cb5SThomas Graf if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric) 15771da177e4SLinus Torvalds continue; 1578d8d1f30bSChangli Gao dst_hold(&rt->dst); 1579c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 15801da177e4SLinus Torvalds 158186872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 15821da177e4SLinus Torvalds } 15831da177e4SLinus Torvalds } 1584c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 15851da177e4SLinus Torvalds 15861da177e4SLinus Torvalds return err; 15871da177e4SLinus Torvalds } 15881da177e4SLinus Torvalds 15891da177e4SLinus Torvalds /* 15901da177e4SLinus Torvalds * Handle redirects 15911da177e4SLinus Torvalds */ 1592a6279458SYOSHIFUJI Hideaki struct ip6rd_flowi { 15934c9483b2SDavid S. Miller struct flowi6 fl6; 1594a6279458SYOSHIFUJI Hideaki struct in6_addr gateway; 1595a6279458SYOSHIFUJI Hideaki }; 15961da177e4SLinus Torvalds 15978ed67789SDaniel Lezcano static struct rt6_info *__ip6_route_redirect(struct net *net, 15988ed67789SDaniel Lezcano struct fib6_table *table, 15994c9483b2SDavid S. Miller struct flowi6 *fl6, 1600a6279458SYOSHIFUJI Hideaki int flags) 1601a6279458SYOSHIFUJI Hideaki { 16024c9483b2SDavid S. Miller struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; 1603a6279458SYOSHIFUJI Hideaki struct rt6_info *rt; 1604a6279458SYOSHIFUJI Hideaki struct fib6_node *fn; 1605c71099acSThomas Graf 1606e843b9e1SYOSHIFUJI Hideaki /* 1607e843b9e1SYOSHIFUJI Hideaki * Get the "current" route for this destination and 1608e843b9e1SYOSHIFUJI Hideaki * check if the redirect has come from approriate router. 1609e843b9e1SYOSHIFUJI Hideaki * 1610e843b9e1SYOSHIFUJI Hideaki * RFC 2461 specifies that redirects should only be 1611e843b9e1SYOSHIFUJI Hideaki * accepted if they come from the nexthop to the target. 1612e843b9e1SYOSHIFUJI Hideaki * Due to the way the routes are chosen, this notion 1613e843b9e1SYOSHIFUJI Hideaki * is a bit fuzzy and one might need to check all possible 1614e843b9e1SYOSHIFUJI Hideaki * routes. 1615e843b9e1SYOSHIFUJI Hideaki */ 16161da177e4SLinus Torvalds 1617c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 16184c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1619e843b9e1SYOSHIFUJI Hideaki restart: 1620d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 16211da177e4SLinus Torvalds /* 16221da177e4SLinus Torvalds * Current route is on-link; redirect is always invalid. 16231da177e4SLinus Torvalds * 16241da177e4SLinus Torvalds * Seems, previous statement is not true. It could 16251da177e4SLinus Torvalds * be node, which looks for us as on-link (f.e. proxy ndisc) 16261da177e4SLinus Torvalds * But then router serving it might decide, that we should 16271da177e4SLinus Torvalds * know truth 8)8) --ANK (980726). 16281da177e4SLinus Torvalds */ 1629e843b9e1SYOSHIFUJI Hideaki if (rt6_check_expired(rt)) 1630e843b9e1SYOSHIFUJI Hideaki continue; 16311da177e4SLinus Torvalds if (!(rt->rt6i_flags & RTF_GATEWAY)) 1632e843b9e1SYOSHIFUJI Hideaki continue; 1633d1918542SDavid S. Miller if (fl6->flowi6_oif != rt->dst.dev->ifindex) 1634e843b9e1SYOSHIFUJI Hideaki continue; 1635a6279458SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway)) 1636e843b9e1SYOSHIFUJI Hideaki continue; 1637e843b9e1SYOSHIFUJI Hideaki break; 1638e843b9e1SYOSHIFUJI Hideaki } 1639a6279458SYOSHIFUJI Hideaki 1640cb15d9c2SYOSHIFUJI Hideaki if (!rt) 16418ed67789SDaniel Lezcano rt = net->ipv6.ip6_null_entry; 16424c9483b2SDavid S. Miller BACKTRACK(net, &fl6->saddr); 1643cb15d9c2SYOSHIFUJI Hideaki out: 1644d8d1f30bSChangli Gao dst_hold(&rt->dst); 1645a6279458SYOSHIFUJI Hideaki 1646c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 16471da177e4SLinus Torvalds 1648a6279458SYOSHIFUJI Hideaki return rt; 1649a6279458SYOSHIFUJI Hideaki }; 1650a6279458SYOSHIFUJI Hideaki 1651b71d1d42SEric Dumazet static struct rt6_info *ip6_route_redirect(const struct in6_addr *dest, 1652b71d1d42SEric Dumazet const struct in6_addr *src, 1653b71d1d42SEric Dumazet const struct in6_addr *gateway, 1654a6279458SYOSHIFUJI Hideaki struct net_device *dev) 1655a6279458SYOSHIFUJI Hideaki { 1656adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 1657c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 1658a6279458SYOSHIFUJI Hideaki struct ip6rd_flowi rdfl = { 16594c9483b2SDavid S. Miller .fl6 = { 16604c9483b2SDavid S. Miller .flowi6_oif = dev->ifindex, 16614c9483b2SDavid S. Miller .daddr = *dest, 16624c9483b2SDavid S. Miller .saddr = *src, 1663a6279458SYOSHIFUJI Hideaki }, 1664a6279458SYOSHIFUJI Hideaki }; 1665adaa70bbSThomas Graf 16664e3fd7a0SAlexey Dobriyan rdfl.gateway = *gateway; 166786c36ce4SBrian Haley 1668adaa70bbSThomas Graf if (rt6_need_strict(dest)) 1669adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_IFACE; 1670a6279458SYOSHIFUJI Hideaki 16714c9483b2SDavid S. Miller return (struct rt6_info *)fib6_rule_lookup(net, &rdfl.fl6, 167258f09b78SDaniel Lezcano flags, __ip6_route_redirect); 1673a6279458SYOSHIFUJI Hideaki } 1674a6279458SYOSHIFUJI Hideaki 1675b71d1d42SEric Dumazet void rt6_redirect(const struct in6_addr *dest, const struct in6_addr *src, 1676b71d1d42SEric Dumazet const struct in6_addr *saddr, 1677a6279458SYOSHIFUJI Hideaki struct neighbour *neigh, u8 *lladdr, int on_link) 1678a6279458SYOSHIFUJI Hideaki { 1679a6279458SYOSHIFUJI Hideaki struct rt6_info *rt, *nrt = NULL; 1680a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 1681c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(neigh->dev); 1682a6279458SYOSHIFUJI Hideaki 1683a6279458SYOSHIFUJI Hideaki rt = ip6_route_redirect(dest, src, saddr, neigh->dev); 1684a6279458SYOSHIFUJI Hideaki 16858ed67789SDaniel Lezcano if (rt == net->ipv6.ip6_null_entry) { 1686e87cc472SJoe Perches net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); 1687a6279458SYOSHIFUJI Hideaki goto out; 16881da177e4SLinus Torvalds } 16891da177e4SLinus Torvalds 16901da177e4SLinus Torvalds /* 16911da177e4SLinus Torvalds * We have finally decided to accept it. 16921da177e4SLinus Torvalds */ 16931da177e4SLinus Torvalds 16941da177e4SLinus Torvalds neigh_update(neigh, lladdr, NUD_STALE, 16951da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 16961da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 16971da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 16981da177e4SLinus Torvalds NEIGH_UPDATE_F_ISROUTER)) 16991da177e4SLinus Torvalds ); 17001da177e4SLinus Torvalds 17011da177e4SLinus Torvalds /* 17021da177e4SLinus Torvalds * Redirect received -> path was valid. 17031da177e4SLinus Torvalds * Look, redirects are sent only in response to data packets, 17041da177e4SLinus Torvalds * so that this nexthop apparently is reachable. --ANK 17051da177e4SLinus Torvalds */ 1706d8d1f30bSChangli Gao dst_confirm(&rt->dst); 17071da177e4SLinus Torvalds 17081da177e4SLinus Torvalds /* Duplicate redirect: silently ignore. */ 170927217455SDavid Miller if (neigh == dst_get_neighbour_noref_raw(&rt->dst)) 17101da177e4SLinus Torvalds goto out; 17111da177e4SLinus Torvalds 171221efcfa0SEric Dumazet nrt = ip6_rt_copy(rt, dest); 171338308473SDavid S. Miller if (!nrt) 17141da177e4SLinus Torvalds goto out; 17151da177e4SLinus Torvalds 17161da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 17171da177e4SLinus Torvalds if (on_link) 17181da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 17191da177e4SLinus Torvalds 17204e3fd7a0SAlexey Dobriyan nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 172169cce1d1SDavid S. Miller dst_set_neighbour(&nrt->dst, neigh_clone(neigh)); 17221da177e4SLinus Torvalds 172340e22e8fSThomas Graf if (ip6_ins_rt(nrt)) 17241da177e4SLinus Torvalds goto out; 17251da177e4SLinus Torvalds 1726d8d1f30bSChangli Gao netevent.old = &rt->dst; 1727d8d1f30bSChangli Gao netevent.new = &nrt->dst; 17288d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 17298d71740cSTom Tucker 17301da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) { 1731e0a1ad73SThomas Graf ip6_del_rt(rt); 17321da177e4SLinus Torvalds return; 17331da177e4SLinus Torvalds } 17341da177e4SLinus Torvalds 17351da177e4SLinus Torvalds out: 1736d8d1f30bSChangli Gao dst_release(&rt->dst); 17371da177e4SLinus Torvalds } 17381da177e4SLinus Torvalds 17391da177e4SLinus Torvalds /* 17401da177e4SLinus Torvalds * Misc support functions 17411da177e4SLinus Torvalds */ 17421da177e4SLinus Torvalds 17431716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort, 174421efcfa0SEric Dumazet const struct in6_addr *dest) 17451da177e4SLinus Torvalds { 1746d1918542SDavid S. Miller struct net *net = dev_net(ort->dst.dev); 17478b96d22dSDavid S. Miller struct rt6_info *rt = ip6_dst_alloc(net, ort->dst.dev, 0, 17488b96d22dSDavid S. Miller ort->rt6i_table); 17491da177e4SLinus Torvalds 17501da177e4SLinus Torvalds if (rt) { 1751d8d1f30bSChangli Gao rt->dst.input = ort->dst.input; 1752d8d1f30bSChangli Gao rt->dst.output = ort->dst.output; 17538e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 17541da177e4SLinus Torvalds 17554e3fd7a0SAlexey Dobriyan rt->rt6i_dst.addr = *dest; 17568e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 1757defb3519SDavid S. Miller dst_copy_metrics(&rt->dst, &ort->dst); 1758d8d1f30bSChangli Gao rt->dst.error = ort->dst.error; 17591da177e4SLinus Torvalds rt->rt6i_idev = ort->rt6i_idev; 17601da177e4SLinus Torvalds if (rt->rt6i_idev) 17611da177e4SLinus Torvalds in6_dev_hold(rt->rt6i_idev); 1762d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 17631da177e4SLinus Torvalds 17644e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 17651716a961SGao feng rt->rt6i_flags = ort->rt6i_flags; 17661716a961SGao feng if ((ort->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) == 17671716a961SGao feng (RTF_DEFAULT | RTF_ADDRCONF)) 17681716a961SGao feng rt6_set_from(rt, ort); 17691716a961SGao feng else 17701716a961SGao feng rt6_clean_expires(rt); 17711da177e4SLinus Torvalds rt->rt6i_metric = 0; 17721da177e4SLinus Torvalds 17731da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 17741da177e4SLinus Torvalds memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 17751da177e4SLinus Torvalds #endif 17760f6c6392SFlorian Westphal memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key)); 1777c71099acSThomas Graf rt->rt6i_table = ort->rt6i_table; 17781da177e4SLinus Torvalds } 17791da177e4SLinus Torvalds return rt; 17801da177e4SLinus Torvalds } 17811da177e4SLinus Torvalds 178270ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 1783efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 1784b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 1785b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex) 178670ceb4f5SYOSHIFUJI Hideaki { 178770ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 178870ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt = NULL; 1789c71099acSThomas Graf struct fib6_table *table; 179070ceb4f5SYOSHIFUJI Hideaki 1791efa2cea0SDaniel Lezcano table = fib6_get_table(net, RT6_TABLE_INFO); 179238308473SDavid S. Miller if (!table) 1793c71099acSThomas Graf return NULL; 1794c71099acSThomas Graf 1795c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 1796c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0); 179770ceb4f5SYOSHIFUJI Hideaki if (!fn) 179870ceb4f5SYOSHIFUJI Hideaki goto out; 179970ceb4f5SYOSHIFUJI Hideaki 1800d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 1801d1918542SDavid S. Miller if (rt->dst.dev->ifindex != ifindex) 180270ceb4f5SYOSHIFUJI Hideaki continue; 180370ceb4f5SYOSHIFUJI Hideaki if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) 180470ceb4f5SYOSHIFUJI Hideaki continue; 180570ceb4f5SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr)) 180670ceb4f5SYOSHIFUJI Hideaki continue; 1807d8d1f30bSChangli Gao dst_hold(&rt->dst); 180870ceb4f5SYOSHIFUJI Hideaki break; 180970ceb4f5SYOSHIFUJI Hideaki } 181070ceb4f5SYOSHIFUJI Hideaki out: 1811c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 181270ceb4f5SYOSHIFUJI Hideaki return rt; 181370ceb4f5SYOSHIFUJI Hideaki } 181470ceb4f5SYOSHIFUJI Hideaki 1815efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 1816b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 1817b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex, 181895c96174SEric Dumazet unsigned int pref) 181970ceb4f5SYOSHIFUJI Hideaki { 182086872cb5SThomas Graf struct fib6_config cfg = { 182186872cb5SThomas Graf .fc_table = RT6_TABLE_INFO, 1822238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 182386872cb5SThomas Graf .fc_ifindex = ifindex, 182486872cb5SThomas Graf .fc_dst_len = prefixlen, 182586872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 182686872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 1827efa2cea0SDaniel Lezcano .fc_nlinfo.pid = 0, 1828efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 1829efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 183086872cb5SThomas Graf }; 183170ceb4f5SYOSHIFUJI Hideaki 18324e3fd7a0SAlexey Dobriyan cfg.fc_dst = *prefix; 18334e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 183486872cb5SThomas Graf 1835e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 1836e317da96SYOSHIFUJI Hideaki if (!prefixlen) 183786872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 183870ceb4f5SYOSHIFUJI Hideaki 183986872cb5SThomas Graf ip6_route_add(&cfg); 184070ceb4f5SYOSHIFUJI Hideaki 1841efa2cea0SDaniel Lezcano return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex); 184270ceb4f5SYOSHIFUJI Hideaki } 184370ceb4f5SYOSHIFUJI Hideaki #endif 184470ceb4f5SYOSHIFUJI Hideaki 1845b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev) 18461da177e4SLinus Torvalds { 18471da177e4SLinus Torvalds struct rt6_info *rt; 1848c71099acSThomas Graf struct fib6_table *table; 18491da177e4SLinus Torvalds 1850c346dca1SYOSHIFUJI Hideaki table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT); 185138308473SDavid S. Miller if (!table) 1852c71099acSThomas Graf return NULL; 18531da177e4SLinus Torvalds 1854c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 1855d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) { 1856d1918542SDavid S. Miller if (dev == rt->dst.dev && 1857045927ffSYOSHIFUJI Hideaki ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 18581da177e4SLinus Torvalds ipv6_addr_equal(&rt->rt6i_gateway, addr)) 18591da177e4SLinus Torvalds break; 18601da177e4SLinus Torvalds } 18611da177e4SLinus Torvalds if (rt) 1862d8d1f30bSChangli Gao dst_hold(&rt->dst); 1863c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 18641da177e4SLinus Torvalds return rt; 18651da177e4SLinus Torvalds } 18661da177e4SLinus Torvalds 1867b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr, 1868ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 1869ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 18701da177e4SLinus Torvalds { 187186872cb5SThomas Graf struct fib6_config cfg = { 187286872cb5SThomas Graf .fc_table = RT6_TABLE_DFLT, 1873238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 187486872cb5SThomas Graf .fc_ifindex = dev->ifindex, 187586872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 187686872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 18775578689aSDaniel Lezcano .fc_nlinfo.pid = 0, 18785578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 1879c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 188086872cb5SThomas Graf }; 18811da177e4SLinus Torvalds 18824e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 18831da177e4SLinus Torvalds 188486872cb5SThomas Graf ip6_route_add(&cfg); 18851da177e4SLinus Torvalds 18861da177e4SLinus Torvalds return rt6_get_dflt_router(gwaddr, dev); 18871da177e4SLinus Torvalds } 18881da177e4SLinus Torvalds 18897b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net) 18901da177e4SLinus Torvalds { 18911da177e4SLinus Torvalds struct rt6_info *rt; 1892c71099acSThomas Graf struct fib6_table *table; 1893c71099acSThomas Graf 1894c71099acSThomas Graf /* NOTE: Keep consistent with rt6_get_dflt_router */ 18957b4da532SDaniel Lezcano table = fib6_get_table(net, RT6_TABLE_DFLT); 189638308473SDavid S. Miller if (!table) 1897c71099acSThomas Graf return; 18981da177e4SLinus Torvalds 18991da177e4SLinus Torvalds restart: 1900c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 1901d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 19021da177e4SLinus Torvalds if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) { 1903d8d1f30bSChangli Gao dst_hold(&rt->dst); 1904c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 1905e0a1ad73SThomas Graf ip6_del_rt(rt); 19061da177e4SLinus Torvalds goto restart; 19071da177e4SLinus Torvalds } 19081da177e4SLinus Torvalds } 1909c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 19101da177e4SLinus Torvalds } 19111da177e4SLinus Torvalds 19125578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 19135578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 191486872cb5SThomas Graf struct fib6_config *cfg) 191586872cb5SThomas Graf { 191686872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 191786872cb5SThomas Graf 191886872cb5SThomas Graf cfg->fc_table = RT6_TABLE_MAIN; 191986872cb5SThomas Graf cfg->fc_ifindex = rtmsg->rtmsg_ifindex; 192086872cb5SThomas Graf cfg->fc_metric = rtmsg->rtmsg_metric; 192186872cb5SThomas Graf cfg->fc_expires = rtmsg->rtmsg_info; 192286872cb5SThomas Graf cfg->fc_dst_len = rtmsg->rtmsg_dst_len; 192386872cb5SThomas Graf cfg->fc_src_len = rtmsg->rtmsg_src_len; 192486872cb5SThomas Graf cfg->fc_flags = rtmsg->rtmsg_flags; 192586872cb5SThomas Graf 19265578689aSDaniel Lezcano cfg->fc_nlinfo.nl_net = net; 1927f1243c2dSBenjamin Thery 19284e3fd7a0SAlexey Dobriyan cfg->fc_dst = rtmsg->rtmsg_dst; 19294e3fd7a0SAlexey Dobriyan cfg->fc_src = rtmsg->rtmsg_src; 19304e3fd7a0SAlexey Dobriyan cfg->fc_gateway = rtmsg->rtmsg_gateway; 193186872cb5SThomas Graf } 193286872cb5SThomas Graf 19335578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) 19341da177e4SLinus Torvalds { 193586872cb5SThomas Graf struct fib6_config cfg; 19361da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 19371da177e4SLinus Torvalds int err; 19381da177e4SLinus Torvalds 19391da177e4SLinus Torvalds switch(cmd) { 19401da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 19411da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 19421da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 19431da177e4SLinus Torvalds return -EPERM; 19441da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 19451da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 19461da177e4SLinus Torvalds if (err) 19471da177e4SLinus Torvalds return -EFAULT; 19481da177e4SLinus Torvalds 19495578689aSDaniel Lezcano rtmsg_to_fib6_config(net, &rtmsg, &cfg); 195086872cb5SThomas Graf 19511da177e4SLinus Torvalds rtnl_lock(); 19521da177e4SLinus Torvalds switch (cmd) { 19531da177e4SLinus Torvalds case SIOCADDRT: 195486872cb5SThomas Graf err = ip6_route_add(&cfg); 19551da177e4SLinus Torvalds break; 19561da177e4SLinus Torvalds case SIOCDELRT: 195786872cb5SThomas Graf err = ip6_route_del(&cfg); 19581da177e4SLinus Torvalds break; 19591da177e4SLinus Torvalds default: 19601da177e4SLinus Torvalds err = -EINVAL; 19611da177e4SLinus Torvalds } 19621da177e4SLinus Torvalds rtnl_unlock(); 19631da177e4SLinus Torvalds 19641da177e4SLinus Torvalds return err; 19653ff50b79SStephen Hemminger } 19661da177e4SLinus Torvalds 19671da177e4SLinus Torvalds return -EINVAL; 19681da177e4SLinus Torvalds } 19691da177e4SLinus Torvalds 19701da177e4SLinus Torvalds /* 19711da177e4SLinus Torvalds * Drop the packet on the floor 19721da177e4SLinus Torvalds */ 19731da177e4SLinus Torvalds 1974d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 19751da177e4SLinus Torvalds { 1976612f09e8SYOSHIFUJI Hideaki int type; 1977adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 1978612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 1979612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 19800660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 198145bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 19823bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 19833bd653c8SDenis V. Lunev IPSTATS_MIB_INADDRERRORS); 1984612f09e8SYOSHIFUJI Hideaki break; 1985612f09e8SYOSHIFUJI Hideaki } 1986612f09e8SYOSHIFUJI Hideaki /* FALLTHROUGH */ 1987612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 19883bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 19893bd653c8SDenis V. Lunev ipstats_mib_noroutes); 1990612f09e8SYOSHIFUJI Hideaki break; 1991612f09e8SYOSHIFUJI Hideaki } 19923ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 19931da177e4SLinus Torvalds kfree_skb(skb); 19941da177e4SLinus Torvalds return 0; 19951da177e4SLinus Torvalds } 19961da177e4SLinus Torvalds 19979ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 19989ce8ade0SThomas Graf { 1999612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 20009ce8ade0SThomas Graf } 20019ce8ade0SThomas Graf 200220380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb) 20031da177e4SLinus Torvalds { 2004adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 2005612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 20061da177e4SLinus Torvalds } 20071da177e4SLinus Torvalds 20086723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES 20096723ab54SDavid S. Miller 20109ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 20119ce8ade0SThomas Graf { 2012612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 20139ce8ade0SThomas Graf } 20149ce8ade0SThomas Graf 20159ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb) 20169ce8ade0SThomas Graf { 2017adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 2018612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 20199ce8ade0SThomas Graf } 20209ce8ade0SThomas Graf 20216723ab54SDavid S. Miller #endif 20226723ab54SDavid S. Miller 20231da177e4SLinus Torvalds /* 20241da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 20251da177e4SLinus Torvalds */ 20261da177e4SLinus Torvalds 20271da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, 20281da177e4SLinus Torvalds const struct in6_addr *addr, 20298f031519SDavid S. Miller bool anycast) 20301da177e4SLinus Torvalds { 2031c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 20328b96d22dSDavid S. Miller struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev, 0, NULL); 2033f83c7790SDavid S. Miller int err; 20341da177e4SLinus Torvalds 203538308473SDavid S. Miller if (!rt) { 2036f3213831SJoe Perches net_warn_ratelimited("Maximum number of routes reached, consider increasing route/max_size\n"); 20371da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 203840385653SBen Greear } 20391da177e4SLinus Torvalds 20401da177e4SLinus Torvalds in6_dev_hold(idev); 20411da177e4SLinus Torvalds 204211d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 2043d8d1f30bSChangli Gao rt->dst.input = ip6_input; 2044d8d1f30bSChangli Gao rt->dst.output = ip6_output; 20451da177e4SLinus Torvalds rt->rt6i_idev = idev; 2046d8d1f30bSChangli Gao rt->dst.obsolete = -1; 20471da177e4SLinus Torvalds 20481da177e4SLinus Torvalds rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP; 204958c4fb86SYOSHIFUJI Hideaki if (anycast) 205058c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 205158c4fb86SYOSHIFUJI Hideaki else 20521da177e4SLinus Torvalds rt->rt6i_flags |= RTF_LOCAL; 20538ade06c6SDavid S. Miller err = rt6_bind_neighbour(rt, rt->dst.dev); 2054f83c7790SDavid S. Miller if (err) { 2055d8d1f30bSChangli Gao dst_free(&rt->dst); 2056f83c7790SDavid S. Miller return ERR_PTR(err); 20571da177e4SLinus Torvalds } 20581da177e4SLinus Torvalds 20594e3fd7a0SAlexey Dobriyan rt->rt6i_dst.addr = *addr; 20601da177e4SLinus Torvalds rt->rt6i_dst.plen = 128; 20615578689aSDaniel Lezcano rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL); 20621da177e4SLinus Torvalds 2063d8d1f30bSChangli Gao atomic_set(&rt->dst.__refcnt, 1); 20641da177e4SLinus Torvalds 20651da177e4SLinus Torvalds return rt; 20661da177e4SLinus Torvalds } 20671da177e4SLinus Torvalds 2068c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net, 2069c3968a85SDaniel Walter struct rt6_info *rt, 2070b71d1d42SEric Dumazet const struct in6_addr *daddr, 2071c3968a85SDaniel Walter unsigned int prefs, 2072c3968a85SDaniel Walter struct in6_addr *saddr) 2073c3968a85SDaniel Walter { 2074c3968a85SDaniel Walter struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt); 2075c3968a85SDaniel Walter int err = 0; 2076c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) 20774e3fd7a0SAlexey Dobriyan *saddr = rt->rt6i_prefsrc.addr; 2078c3968a85SDaniel Walter else 2079c3968a85SDaniel Walter err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL, 2080c3968a85SDaniel Walter daddr, prefs, saddr); 2081c3968a85SDaniel Walter return err; 2082c3968a85SDaniel Walter } 2083c3968a85SDaniel Walter 2084c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 2085c3968a85SDaniel Walter struct arg_dev_net_ip { 2086c3968a85SDaniel Walter struct net_device *dev; 2087c3968a85SDaniel Walter struct net *net; 2088c3968a85SDaniel Walter struct in6_addr *addr; 2089c3968a85SDaniel Walter }; 2090c3968a85SDaniel Walter 2091c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg) 2092c3968a85SDaniel Walter { 2093c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 2094c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 2095c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 2096c3968a85SDaniel Walter 2097d1918542SDavid S. Miller if (((void *)rt->dst.dev == dev || !dev) && 2098c3968a85SDaniel Walter rt != net->ipv6.ip6_null_entry && 2099c3968a85SDaniel Walter ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) { 2100c3968a85SDaniel Walter /* remove prefsrc entry */ 2101c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 2102c3968a85SDaniel Walter } 2103c3968a85SDaniel Walter return 0; 2104c3968a85SDaniel Walter } 2105c3968a85SDaniel Walter 2106c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 2107c3968a85SDaniel Walter { 2108c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 2109c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 2110c3968a85SDaniel Walter .dev = ifp->idev->dev, 2111c3968a85SDaniel Walter .net = net, 2112c3968a85SDaniel Walter .addr = &ifp->addr, 2113c3968a85SDaniel Walter }; 2114c3968a85SDaniel Walter fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni); 2115c3968a85SDaniel Walter } 2116c3968a85SDaniel Walter 21178ed67789SDaniel Lezcano struct arg_dev_net { 21188ed67789SDaniel Lezcano struct net_device *dev; 21198ed67789SDaniel Lezcano struct net *net; 21208ed67789SDaniel Lezcano }; 21218ed67789SDaniel Lezcano 21221da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg) 21231da177e4SLinus Torvalds { 2124bc3ef660Sstephen hemminger const struct arg_dev_net *adn = arg; 2125bc3ef660Sstephen hemminger const struct net_device *dev = adn->dev; 21268ed67789SDaniel Lezcano 2127d1918542SDavid S. Miller if ((rt->dst.dev == dev || !dev) && 2128c159d30cSDavid S. Miller rt != adn->net->ipv6.ip6_null_entry) 21291da177e4SLinus Torvalds return -1; 2130c159d30cSDavid S. Miller 21311da177e4SLinus Torvalds return 0; 21321da177e4SLinus Torvalds } 21331da177e4SLinus Torvalds 2134f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev) 21351da177e4SLinus Torvalds { 21368ed67789SDaniel Lezcano struct arg_dev_net adn = { 21378ed67789SDaniel Lezcano .dev = dev, 21388ed67789SDaniel Lezcano .net = net, 21398ed67789SDaniel Lezcano }; 21408ed67789SDaniel Lezcano 21418ed67789SDaniel Lezcano fib6_clean_all(net, fib6_ifdown, 0, &adn); 21421e493d19SDavid S. Miller icmp6_clean_all(fib6_ifdown, &adn); 21431da177e4SLinus Torvalds } 21441da177e4SLinus Torvalds 214595c96174SEric Dumazet struct rt6_mtu_change_arg { 21461da177e4SLinus Torvalds struct net_device *dev; 214795c96174SEric Dumazet unsigned int mtu; 21481da177e4SLinus Torvalds }; 21491da177e4SLinus Torvalds 21501da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg) 21511da177e4SLinus Torvalds { 21521da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 21531da177e4SLinus Torvalds struct inet6_dev *idev; 21541da177e4SLinus Torvalds 21551da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 21561da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 21571da177e4SLinus Torvalds We still use this lock to block changes 21581da177e4SLinus Torvalds caused by addrconf/ndisc. 21591da177e4SLinus Torvalds */ 21601da177e4SLinus Torvalds 21611da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 216238308473SDavid S. Miller if (!idev) 21631da177e4SLinus Torvalds return 0; 21641da177e4SLinus Torvalds 21651da177e4SLinus Torvalds /* For administrative MTU increase, there is no way to discover 21661da177e4SLinus Torvalds IPv6 PMTU increase, so PMTU increase should be updated here. 21671da177e4SLinus Torvalds Since RFC 1981 doesn't include administrative MTU increase 21681da177e4SLinus Torvalds update PMTU increase is a MUST. (i.e. jumbo frame) 21691da177e4SLinus Torvalds */ 21701da177e4SLinus Torvalds /* 21711da177e4SLinus Torvalds If new MTU is less than route PMTU, this new MTU will be the 21721da177e4SLinus Torvalds lowest MTU in the path, update the route PMTU to reflect PMTU 21731da177e4SLinus Torvalds decreases; if new MTU is greater than route PMTU, and the 21741da177e4SLinus Torvalds old MTU is the lowest MTU in the path, update the route PMTU 21751da177e4SLinus Torvalds to reflect the increase. In this case if the other nodes' MTU 21761da177e4SLinus Torvalds also have the lowest MTU, TOO BIG MESSAGE will be lead to 21771da177e4SLinus Torvalds PMTU discouvery. 21781da177e4SLinus Torvalds */ 2179d1918542SDavid S. Miller if (rt->dst.dev == arg->dev && 2180d8d1f30bSChangli Gao !dst_metric_locked(&rt->dst, RTAX_MTU) && 2181d8d1f30bSChangli Gao (dst_mtu(&rt->dst) >= arg->mtu || 2182d8d1f30bSChangli Gao (dst_mtu(&rt->dst) < arg->mtu && 2183d8d1f30bSChangli Gao dst_mtu(&rt->dst) == idev->cnf.mtu6))) { 2184defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu); 2185566cfd8fSSimon Arlott } 21861da177e4SLinus Torvalds return 0; 21871da177e4SLinus Torvalds } 21881da177e4SLinus Torvalds 218995c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu) 21901da177e4SLinus Torvalds { 2191c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 2192c71099acSThomas Graf .dev = dev, 2193c71099acSThomas Graf .mtu = mtu, 2194c71099acSThomas Graf }; 21951da177e4SLinus Torvalds 2196c346dca1SYOSHIFUJI Hideaki fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg); 21971da177e4SLinus Torvalds } 21981da177e4SLinus Torvalds 2199ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 22005176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 220186872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 2202ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 220386872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 220486872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 220586872cb5SThomas Graf }; 220686872cb5SThomas Graf 220786872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 220886872cb5SThomas Graf struct fib6_config *cfg) 22091da177e4SLinus Torvalds { 221086872cb5SThomas Graf struct rtmsg *rtm; 221186872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 221286872cb5SThomas Graf int err; 22131da177e4SLinus Torvalds 221486872cb5SThomas Graf err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy); 221586872cb5SThomas Graf if (err < 0) 221686872cb5SThomas Graf goto errout; 22171da177e4SLinus Torvalds 221886872cb5SThomas Graf err = -EINVAL; 221986872cb5SThomas Graf rtm = nlmsg_data(nlh); 222086872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 222186872cb5SThomas Graf 222286872cb5SThomas Graf cfg->fc_table = rtm->rtm_table; 222386872cb5SThomas Graf cfg->fc_dst_len = rtm->rtm_dst_len; 222486872cb5SThomas Graf cfg->fc_src_len = rtm->rtm_src_len; 222586872cb5SThomas Graf cfg->fc_flags = RTF_UP; 222686872cb5SThomas Graf cfg->fc_protocol = rtm->rtm_protocol; 222786872cb5SThomas Graf 222886872cb5SThomas Graf if (rtm->rtm_type == RTN_UNREACHABLE) 222986872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 223086872cb5SThomas Graf 2231ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 2232ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 2233ab79ad14SMaciej Żenczykowski 223486872cb5SThomas Graf cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid; 223586872cb5SThomas Graf cfg->fc_nlinfo.nlh = nlh; 22363b1e0a65SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = sock_net(skb->sk); 223786872cb5SThomas Graf 223886872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 223986872cb5SThomas Graf nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16); 224086872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 22411da177e4SLinus Torvalds } 224286872cb5SThomas Graf 224386872cb5SThomas Graf if (tb[RTA_DST]) { 224486872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 224586872cb5SThomas Graf 224686872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 224786872cb5SThomas Graf goto errout; 224886872cb5SThomas Graf 224986872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 22501da177e4SLinus Torvalds } 225186872cb5SThomas Graf 225286872cb5SThomas Graf if (tb[RTA_SRC]) { 225386872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 225486872cb5SThomas Graf 225586872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 225686872cb5SThomas Graf goto errout; 225786872cb5SThomas Graf 225886872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 22591da177e4SLinus Torvalds } 226086872cb5SThomas Graf 2261c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 2262c3968a85SDaniel Walter nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16); 2263c3968a85SDaniel Walter 226486872cb5SThomas Graf if (tb[RTA_OIF]) 226586872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 226686872cb5SThomas Graf 226786872cb5SThomas Graf if (tb[RTA_PRIORITY]) 226886872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 226986872cb5SThomas Graf 227086872cb5SThomas Graf if (tb[RTA_METRICS]) { 227186872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 227286872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 22731da177e4SLinus Torvalds } 227486872cb5SThomas Graf 227586872cb5SThomas Graf if (tb[RTA_TABLE]) 227686872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 227786872cb5SThomas Graf 227886872cb5SThomas Graf err = 0; 227986872cb5SThomas Graf errout: 228086872cb5SThomas Graf return err; 22811da177e4SLinus Torvalds } 22821da177e4SLinus Torvalds 2283c127ea2cSThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 22841da177e4SLinus Torvalds { 228586872cb5SThomas Graf struct fib6_config cfg; 228686872cb5SThomas Graf int err; 22871da177e4SLinus Torvalds 228886872cb5SThomas Graf err = rtm_to_fib6_config(skb, nlh, &cfg); 228986872cb5SThomas Graf if (err < 0) 229086872cb5SThomas Graf return err; 229186872cb5SThomas Graf 229286872cb5SThomas Graf return ip6_route_del(&cfg); 22931da177e4SLinus Torvalds } 22941da177e4SLinus Torvalds 2295c127ea2cSThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 22961da177e4SLinus Torvalds { 229786872cb5SThomas Graf struct fib6_config cfg; 229886872cb5SThomas Graf int err; 22991da177e4SLinus Torvalds 230086872cb5SThomas Graf err = rtm_to_fib6_config(skb, nlh, &cfg); 230186872cb5SThomas Graf if (err < 0) 230286872cb5SThomas Graf return err; 230386872cb5SThomas Graf 230486872cb5SThomas Graf return ip6_route_add(&cfg); 23051da177e4SLinus Torvalds } 23061da177e4SLinus Torvalds 2307339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void) 2308339bf98fSThomas Graf { 2309339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 2310339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 2311339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 2312339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 2313339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 2314339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 2315339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 2316339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 2317339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 23186a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 2319339bf98fSThomas Graf + nla_total_size(sizeof(struct rta_cacheinfo)); 2320339bf98fSThomas Graf } 2321339bf98fSThomas Graf 2322191cd582SBrian Haley static int rt6_fill_node(struct net *net, 2323191cd582SBrian Haley struct sk_buff *skb, struct rt6_info *rt, 23240d51aa80SJamal Hadi Salim struct in6_addr *dst, struct in6_addr *src, 23250d51aa80SJamal Hadi Salim int iif, int type, u32 pid, u32 seq, 23267bc570c8SYOSHIFUJI Hideaki int prefix, int nowait, unsigned int flags) 23271da177e4SLinus Torvalds { 2328346f870bSDavid S. Miller const struct inet_peer *peer; 23291da177e4SLinus Torvalds struct rtmsg *rtm; 23301da177e4SLinus Torvalds struct nlmsghdr *nlh; 2331e3703b3dSThomas Graf long expires; 23329e762a4aSPatrick McHardy u32 table; 2333f2c31e32SEric Dumazet struct neighbour *n; 2334346f870bSDavid S. Miller u32 ts, tsage; 23351da177e4SLinus Torvalds 23361da177e4SLinus Torvalds if (prefix) { /* user wants prefix routes only */ 23371da177e4SLinus Torvalds if (!(rt->rt6i_flags & RTF_PREFIX_RT)) { 23381da177e4SLinus Torvalds /* success since this is not a prefix route */ 23391da177e4SLinus Torvalds return 1; 23401da177e4SLinus Torvalds } 23411da177e4SLinus Torvalds } 23421da177e4SLinus Torvalds 23432d7202bfSThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags); 234438308473SDavid S. Miller if (!nlh) 234526932566SPatrick McHardy return -EMSGSIZE; 23462d7202bfSThomas Graf 23472d7202bfSThomas Graf rtm = nlmsg_data(nlh); 23481da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 23491da177e4SLinus Torvalds rtm->rtm_dst_len = rt->rt6i_dst.plen; 23501da177e4SLinus Torvalds rtm->rtm_src_len = rt->rt6i_src.plen; 23511da177e4SLinus Torvalds rtm->rtm_tos = 0; 2352c71099acSThomas Graf if (rt->rt6i_table) 23539e762a4aSPatrick McHardy table = rt->rt6i_table->tb6_id; 2354c71099acSThomas Graf else 23559e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 23569e762a4aSPatrick McHardy rtm->rtm_table = table; 2357c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, table)) 2358c78679e8SDavid S. Miller goto nla_put_failure; 23591da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_REJECT) 23601da177e4SLinus Torvalds rtm->rtm_type = RTN_UNREACHABLE; 2361ab79ad14SMaciej Żenczykowski else if (rt->rt6i_flags & RTF_LOCAL) 2362ab79ad14SMaciej Żenczykowski rtm->rtm_type = RTN_LOCAL; 2363d1918542SDavid S. Miller else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK)) 23641da177e4SLinus Torvalds rtm->rtm_type = RTN_LOCAL; 23651da177e4SLinus Torvalds else 23661da177e4SLinus Torvalds rtm->rtm_type = RTN_UNICAST; 23671da177e4SLinus Torvalds rtm->rtm_flags = 0; 23681da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 23691da177e4SLinus Torvalds rtm->rtm_protocol = rt->rt6i_protocol; 23701da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_DYNAMIC) 23711da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_REDIRECT; 23721da177e4SLinus Torvalds else if (rt->rt6i_flags & RTF_ADDRCONF) 23731da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_KERNEL; 23741da177e4SLinus Torvalds else if (rt->rt6i_flags & RTF_DEFAULT) 23751da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_RA; 23761da177e4SLinus Torvalds 23771da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) 23781da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 23791da177e4SLinus Torvalds 23801da177e4SLinus Torvalds if (dst) { 2381c78679e8SDavid S. Miller if (nla_put(skb, RTA_DST, 16, dst)) 2382c78679e8SDavid S. Miller goto nla_put_failure; 23831da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 23841da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 2385c78679e8SDavid S. Miller if (nla_put(skb, RTA_DST, 16, &rt->rt6i_dst.addr)) 2386c78679e8SDavid S. Miller goto nla_put_failure; 23871da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 23881da177e4SLinus Torvalds if (src) { 2389c78679e8SDavid S. Miller if (nla_put(skb, RTA_SRC, 16, src)) 2390c78679e8SDavid S. Miller goto nla_put_failure; 23911da177e4SLinus Torvalds rtm->rtm_src_len = 128; 2392c78679e8SDavid S. Miller } else if (rtm->rtm_src_len && 2393c78679e8SDavid S. Miller nla_put(skb, RTA_SRC, 16, &rt->rt6i_src.addr)) 2394c78679e8SDavid S. Miller goto nla_put_failure; 23951da177e4SLinus Torvalds #endif 23967bc570c8SYOSHIFUJI Hideaki if (iif) { 23977bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 23987bc570c8SYOSHIFUJI Hideaki if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) { 23998229efdaSBenjamin Thery int err = ip6mr_get_route(net, skb, rtm, nowait); 24007bc570c8SYOSHIFUJI Hideaki if (err <= 0) { 24017bc570c8SYOSHIFUJI Hideaki if (!nowait) { 24027bc570c8SYOSHIFUJI Hideaki if (err == 0) 24037bc570c8SYOSHIFUJI Hideaki return 0; 24047bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 24057bc570c8SYOSHIFUJI Hideaki } else { 24067bc570c8SYOSHIFUJI Hideaki if (err == -EMSGSIZE) 24077bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 24087bc570c8SYOSHIFUJI Hideaki } 24097bc570c8SYOSHIFUJI Hideaki } 24107bc570c8SYOSHIFUJI Hideaki } else 24117bc570c8SYOSHIFUJI Hideaki #endif 2412c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_IIF, iif)) 2413c78679e8SDavid S. Miller goto nla_put_failure; 24147bc570c8SYOSHIFUJI Hideaki } else if (dst) { 24151da177e4SLinus Torvalds struct in6_addr saddr_buf; 2416c78679e8SDavid S. Miller if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 && 2417c78679e8SDavid S. Miller nla_put(skb, RTA_PREFSRC, 16, &saddr_buf)) 2418c78679e8SDavid S. Miller goto nla_put_failure; 2419c3968a85SDaniel Walter } 2420c3968a85SDaniel Walter 2421c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) { 2422c3968a85SDaniel Walter struct in6_addr saddr_buf; 24234e3fd7a0SAlexey Dobriyan saddr_buf = rt->rt6i_prefsrc.addr; 2424c78679e8SDavid S. Miller if (nla_put(skb, RTA_PREFSRC, 16, &saddr_buf)) 2425c78679e8SDavid S. Miller goto nla_put_failure; 24261da177e4SLinus Torvalds } 24272d7202bfSThomas Graf 2428defb3519SDavid S. Miller if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0) 24292d7202bfSThomas Graf goto nla_put_failure; 24302d7202bfSThomas Graf 2431f2c31e32SEric Dumazet rcu_read_lock(); 243227217455SDavid Miller n = dst_get_neighbour_noref(&rt->dst); 243394f826b8SEric Dumazet if (n) { 243494f826b8SEric Dumazet if (nla_put(skb, RTA_GATEWAY, 16, &n->primary_key) < 0) { 243594f826b8SEric Dumazet rcu_read_unlock(); 243694f826b8SEric Dumazet goto nla_put_failure; 243794f826b8SEric Dumazet } 243894f826b8SEric Dumazet } 2439f2c31e32SEric Dumazet rcu_read_unlock(); 24402d7202bfSThomas Graf 2441c78679e8SDavid S. Miller if (rt->dst.dev && 2442c78679e8SDavid S. Miller nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex)) 2443c78679e8SDavid S. Miller goto nla_put_failure; 2444c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric)) 2445c78679e8SDavid S. Miller goto nla_put_failure; 244636e3deaeSYOSHIFUJI Hideaki if (!(rt->rt6i_flags & RTF_EXPIRES)) 244736e3deaeSYOSHIFUJI Hideaki expires = 0; 2448d1918542SDavid S. Miller else if (rt->dst.expires - jiffies < INT_MAX) 2449d1918542SDavid S. Miller expires = rt->dst.expires - jiffies; 245036e3deaeSYOSHIFUJI Hideaki else 245136e3deaeSYOSHIFUJI Hideaki expires = INT_MAX; 245269cdf8f9SYOSHIFUJI Hideaki 245397bab73fSDavid S. Miller peer = NULL; 245497bab73fSDavid S. Miller if (rt6_has_peer(rt)) 245597bab73fSDavid S. Miller peer = rt6_peer_ptr(rt); 2456346f870bSDavid S. Miller ts = tsage = 0; 2457346f870bSDavid S. Miller if (peer && peer->tcp_ts_stamp) { 2458346f870bSDavid S. Miller ts = peer->tcp_ts; 2459346f870bSDavid S. Miller tsage = get_seconds() - peer->tcp_ts_stamp; 2460346f870bSDavid S. Miller } 2461346f870bSDavid S. Miller 2462346f870bSDavid S. Miller if (rtnl_put_cacheinfo(skb, &rt->dst, 0, ts, tsage, 2463d8d1f30bSChangli Gao expires, rt->dst.error) < 0) 2464e3703b3dSThomas Graf goto nla_put_failure; 24651da177e4SLinus Torvalds 24662d7202bfSThomas Graf return nlmsg_end(skb, nlh); 24672d7202bfSThomas Graf 24682d7202bfSThomas Graf nla_put_failure: 246926932566SPatrick McHardy nlmsg_cancel(skb, nlh); 247026932566SPatrick McHardy return -EMSGSIZE; 24711da177e4SLinus Torvalds } 24721da177e4SLinus Torvalds 24731b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg) 24741da177e4SLinus Torvalds { 24751da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 24761da177e4SLinus Torvalds int prefix; 24771da177e4SLinus Torvalds 24782d7202bfSThomas Graf if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { 24792d7202bfSThomas Graf struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); 24801da177e4SLinus Torvalds prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0; 24811da177e4SLinus Torvalds } else 24821da177e4SLinus Torvalds prefix = 0; 24831da177e4SLinus Torvalds 2484191cd582SBrian Haley return rt6_fill_node(arg->net, 2485191cd582SBrian Haley arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE, 24861da177e4SLinus Torvalds NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq, 24877bc570c8SYOSHIFUJI Hideaki prefix, 0, NLM_F_MULTI); 24881da177e4SLinus Torvalds } 24891da177e4SLinus Torvalds 2490c127ea2cSThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg) 24911da177e4SLinus Torvalds { 24923b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 2493ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 24941da177e4SLinus Torvalds struct rt6_info *rt; 2495ab364a6fSThomas Graf struct sk_buff *skb; 2496ab364a6fSThomas Graf struct rtmsg *rtm; 24974c9483b2SDavid S. Miller struct flowi6 fl6; 249872331bc0SShmulik Ladkani int err, iif = 0, oif = 0; 2499ab364a6fSThomas Graf 2500ab364a6fSThomas Graf err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy); 2501ab364a6fSThomas Graf if (err < 0) 2502ab364a6fSThomas Graf goto errout; 2503ab364a6fSThomas Graf 2504ab364a6fSThomas Graf err = -EINVAL; 25054c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 2506ab364a6fSThomas Graf 2507ab364a6fSThomas Graf if (tb[RTA_SRC]) { 2508ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 2509ab364a6fSThomas Graf goto errout; 2510ab364a6fSThomas Graf 25114e3fd7a0SAlexey Dobriyan fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 2512ab364a6fSThomas Graf } 2513ab364a6fSThomas Graf 2514ab364a6fSThomas Graf if (tb[RTA_DST]) { 2515ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 2516ab364a6fSThomas Graf goto errout; 2517ab364a6fSThomas Graf 25184e3fd7a0SAlexey Dobriyan fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 2519ab364a6fSThomas Graf } 2520ab364a6fSThomas Graf 2521ab364a6fSThomas Graf if (tb[RTA_IIF]) 2522ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 2523ab364a6fSThomas Graf 2524ab364a6fSThomas Graf if (tb[RTA_OIF]) 252572331bc0SShmulik Ladkani oif = nla_get_u32(tb[RTA_OIF]); 2526ab364a6fSThomas Graf 2527ab364a6fSThomas Graf if (iif) { 2528ab364a6fSThomas Graf struct net_device *dev; 252972331bc0SShmulik Ladkani int flags = 0; 253072331bc0SShmulik Ladkani 25315578689aSDaniel Lezcano dev = __dev_get_by_index(net, iif); 2532ab364a6fSThomas Graf if (!dev) { 2533ab364a6fSThomas Graf err = -ENODEV; 2534ab364a6fSThomas Graf goto errout; 2535ab364a6fSThomas Graf } 253672331bc0SShmulik Ladkani 253772331bc0SShmulik Ladkani fl6.flowi6_iif = iif; 253872331bc0SShmulik Ladkani 253972331bc0SShmulik Ladkani if (!ipv6_addr_any(&fl6.saddr)) 254072331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_HAS_SADDR; 254172331bc0SShmulik Ladkani 254272331bc0SShmulik Ladkani rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6, 254372331bc0SShmulik Ladkani flags); 254472331bc0SShmulik Ladkani } else { 254572331bc0SShmulik Ladkani fl6.flowi6_oif = oif; 254672331bc0SShmulik Ladkani 254772331bc0SShmulik Ladkani rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6); 2548ab364a6fSThomas Graf } 25491da177e4SLinus Torvalds 25501da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 255138308473SDavid S. Miller if (!skb) { 25522173bff5SShmulik Ladkani dst_release(&rt->dst); 2553ab364a6fSThomas Graf err = -ENOBUFS; 2554ab364a6fSThomas Graf goto errout; 2555ab364a6fSThomas Graf } 25561da177e4SLinus Torvalds 25571da177e4SLinus Torvalds /* Reserve room for dummy headers, this skb can pass 25581da177e4SLinus Torvalds through good chunk of routing engine. 25591da177e4SLinus Torvalds */ 2560459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 25611da177e4SLinus Torvalds skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr)); 25621da177e4SLinus Torvalds 2563d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 25641da177e4SLinus Torvalds 25654c9483b2SDavid S. Miller err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif, 25661da177e4SLinus Torvalds RTM_NEWROUTE, NETLINK_CB(in_skb).pid, 25677bc570c8SYOSHIFUJI Hideaki nlh->nlmsg_seq, 0, 0, 0); 25681da177e4SLinus Torvalds if (err < 0) { 2569ab364a6fSThomas Graf kfree_skb(skb); 2570ab364a6fSThomas Graf goto errout; 25711da177e4SLinus Torvalds } 25721da177e4SLinus Torvalds 25735578689aSDaniel Lezcano err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid); 2574ab364a6fSThomas Graf errout: 25751da177e4SLinus Torvalds return err; 25761da177e4SLinus Torvalds } 25771da177e4SLinus Torvalds 257886872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info) 25791da177e4SLinus Torvalds { 25801da177e4SLinus Torvalds struct sk_buff *skb; 25815578689aSDaniel Lezcano struct net *net = info->nl_net; 2582528c4cebSDenis V. Lunev u32 seq; 2583528c4cebSDenis V. Lunev int err; 25840d51aa80SJamal Hadi Salim 2585528c4cebSDenis V. Lunev err = -ENOBUFS; 258638308473SDavid S. Miller seq = info->nlh ? info->nlh->nlmsg_seq : 0; 258786872cb5SThomas Graf 2588339bf98fSThomas Graf skb = nlmsg_new(rt6_nlmsg_size(), gfp_any()); 258938308473SDavid S. Miller if (!skb) 259021713ebcSThomas Graf goto errout; 25911da177e4SLinus Torvalds 2592191cd582SBrian Haley err = rt6_fill_node(net, skb, rt, NULL, NULL, 0, 25937bc570c8SYOSHIFUJI Hideaki event, info->pid, seq, 0, 0, 0); 259426932566SPatrick McHardy if (err < 0) { 259526932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 259626932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 259726932566SPatrick McHardy kfree_skb(skb); 259826932566SPatrick McHardy goto errout; 259926932566SPatrick McHardy } 26001ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE, 26015578689aSDaniel Lezcano info->nlh, gfp_any()); 26021ce85fe4SPablo Neira Ayuso return; 260321713ebcSThomas Graf errout: 260421713ebcSThomas Graf if (err < 0) 26055578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 26061da177e4SLinus Torvalds } 26071da177e4SLinus Torvalds 26088ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 26098ed67789SDaniel Lezcano unsigned long event, void *data) 26108ed67789SDaniel Lezcano { 26118ed67789SDaniel Lezcano struct net_device *dev = (struct net_device *)data; 2612c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 26138ed67789SDaniel Lezcano 26148ed67789SDaniel Lezcano if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) { 2615d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 26168ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 26178ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 2618d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 26198ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 2620d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 26218ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 26228ed67789SDaniel Lezcano #endif 26238ed67789SDaniel Lezcano } 26248ed67789SDaniel Lezcano 26258ed67789SDaniel Lezcano return NOTIFY_OK; 26268ed67789SDaniel Lezcano } 26278ed67789SDaniel Lezcano 26281da177e4SLinus Torvalds /* 26291da177e4SLinus Torvalds * /proc 26301da177e4SLinus Torvalds */ 26311da177e4SLinus Torvalds 26321da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 26331da177e4SLinus Torvalds 26341da177e4SLinus Torvalds struct rt6_proc_arg 26351da177e4SLinus Torvalds { 26361da177e4SLinus Torvalds char *buffer; 26371da177e4SLinus Torvalds int offset; 26381da177e4SLinus Torvalds int length; 26391da177e4SLinus Torvalds int skip; 26401da177e4SLinus Torvalds int len; 26411da177e4SLinus Torvalds }; 26421da177e4SLinus Torvalds 26431da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg) 26441da177e4SLinus Torvalds { 264533120b30SAlexey Dobriyan struct seq_file *m = p_arg; 264669cce1d1SDavid S. Miller struct neighbour *n; 26471da177e4SLinus Torvalds 26484b7a4274SHarvey Harrison seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen); 26491da177e4SLinus Torvalds 26501da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 26514b7a4274SHarvey Harrison seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen); 26521da177e4SLinus Torvalds #else 265333120b30SAlexey Dobriyan seq_puts(m, "00000000000000000000000000000000 00 "); 26541da177e4SLinus Torvalds #endif 2655f2c31e32SEric Dumazet rcu_read_lock(); 265627217455SDavid Miller n = dst_get_neighbour_noref(&rt->dst); 265769cce1d1SDavid S. Miller if (n) { 265869cce1d1SDavid S. Miller seq_printf(m, "%pi6", n->primary_key); 26591da177e4SLinus Torvalds } else { 266033120b30SAlexey Dobriyan seq_puts(m, "00000000000000000000000000000000"); 26611da177e4SLinus Torvalds } 2662f2c31e32SEric Dumazet rcu_read_unlock(); 266333120b30SAlexey Dobriyan seq_printf(m, " %08x %08x %08x %08x %8s\n", 2664d8d1f30bSChangli Gao rt->rt6i_metric, atomic_read(&rt->dst.__refcnt), 2665d8d1f30bSChangli Gao rt->dst.__use, rt->rt6i_flags, 2666d1918542SDavid S. Miller rt->dst.dev ? rt->dst.dev->name : ""); 26671da177e4SLinus Torvalds return 0; 26681da177e4SLinus Torvalds } 26691da177e4SLinus Torvalds 267033120b30SAlexey Dobriyan static int ipv6_route_show(struct seq_file *m, void *v) 26711da177e4SLinus Torvalds { 2672f3db4851SDaniel Lezcano struct net *net = (struct net *)m->private; 267332b293a5SJosh Hunt fib6_clean_all_ro(net, rt6_info_route, 0, m); 267433120b30SAlexey Dobriyan return 0; 26751da177e4SLinus Torvalds } 26761da177e4SLinus Torvalds 267733120b30SAlexey Dobriyan static int ipv6_route_open(struct inode *inode, struct file *file) 267833120b30SAlexey Dobriyan { 2679de05c557SPavel Emelyanov return single_open_net(inode, file, ipv6_route_show); 2680f3db4851SDaniel Lezcano } 2681f3db4851SDaniel Lezcano 268233120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = { 268333120b30SAlexey Dobriyan .owner = THIS_MODULE, 268433120b30SAlexey Dobriyan .open = ipv6_route_open, 268533120b30SAlexey Dobriyan .read = seq_read, 268633120b30SAlexey Dobriyan .llseek = seq_lseek, 2687b6fcbdb4SPavel Emelyanov .release = single_release_net, 268833120b30SAlexey Dobriyan }; 268933120b30SAlexey Dobriyan 26901da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 26911da177e4SLinus Torvalds { 269269ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 26931da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 269469ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 269569ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 269669ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_alloc, 269769ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 269869ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 2699fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 270069ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 27011da177e4SLinus Torvalds 27021da177e4SLinus Torvalds return 0; 27031da177e4SLinus Torvalds } 27041da177e4SLinus Torvalds 27051da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file) 27061da177e4SLinus Torvalds { 2707de05c557SPavel Emelyanov return single_open_net(inode, file, rt6_stats_seq_show); 270869ddb805SDaniel Lezcano } 270969ddb805SDaniel Lezcano 27109a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = { 27111da177e4SLinus Torvalds .owner = THIS_MODULE, 27121da177e4SLinus Torvalds .open = rt6_stats_seq_open, 27131da177e4SLinus Torvalds .read = seq_read, 27141da177e4SLinus Torvalds .llseek = seq_lseek, 2715b6fcbdb4SPavel Emelyanov .release = single_release_net, 27161da177e4SLinus Torvalds }; 27171da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 27181da177e4SLinus Torvalds 27191da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 27201da177e4SLinus Torvalds 27211da177e4SLinus Torvalds static 27228d65af78SAlexey Dobriyan int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write, 27231da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 27241da177e4SLinus Torvalds { 2725c486da34SLucian Adrian Grijincu struct net *net; 2726c486da34SLucian Adrian Grijincu int delay; 2727c486da34SLucian Adrian Grijincu if (!write) 2728c486da34SLucian Adrian Grijincu return -EINVAL; 2729c486da34SLucian Adrian Grijincu 2730c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 2731c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 27328d65af78SAlexey Dobriyan proc_dointvec(ctl, write, buffer, lenp, ppos); 27335b7c931dSDaniel Lezcano fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net); 27341da177e4SLinus Torvalds return 0; 27351da177e4SLinus Torvalds } 27361da177e4SLinus Torvalds 2737760f2d01SDaniel Lezcano ctl_table ipv6_route_table_template[] = { 27381da177e4SLinus Torvalds { 27391da177e4SLinus Torvalds .procname = "flush", 27404990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 27411da177e4SLinus Torvalds .maxlen = sizeof(int), 274289c8b3a1SDave Jones .mode = 0200, 27436d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 27441da177e4SLinus Torvalds }, 27451da177e4SLinus Torvalds { 27461da177e4SLinus Torvalds .procname = "gc_thresh", 27479a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 27481da177e4SLinus Torvalds .maxlen = sizeof(int), 27491da177e4SLinus Torvalds .mode = 0644, 27506d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 27511da177e4SLinus Torvalds }, 27521da177e4SLinus Torvalds { 27531da177e4SLinus Torvalds .procname = "max_size", 27544990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 27551da177e4SLinus Torvalds .maxlen = sizeof(int), 27561da177e4SLinus Torvalds .mode = 0644, 27576d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 27581da177e4SLinus Torvalds }, 27591da177e4SLinus Torvalds { 27601da177e4SLinus Torvalds .procname = "gc_min_interval", 27614990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 27621da177e4SLinus Torvalds .maxlen = sizeof(int), 27631da177e4SLinus Torvalds .mode = 0644, 27646d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 27651da177e4SLinus Torvalds }, 27661da177e4SLinus Torvalds { 27671da177e4SLinus Torvalds .procname = "gc_timeout", 27684990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 27691da177e4SLinus Torvalds .maxlen = sizeof(int), 27701da177e4SLinus Torvalds .mode = 0644, 27716d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 27721da177e4SLinus Torvalds }, 27731da177e4SLinus Torvalds { 27741da177e4SLinus Torvalds .procname = "gc_interval", 27754990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 27761da177e4SLinus Torvalds .maxlen = sizeof(int), 27771da177e4SLinus Torvalds .mode = 0644, 27786d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 27791da177e4SLinus Torvalds }, 27801da177e4SLinus Torvalds { 27811da177e4SLinus Torvalds .procname = "gc_elasticity", 27824990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 27831da177e4SLinus Torvalds .maxlen = sizeof(int), 27841da177e4SLinus Torvalds .mode = 0644, 2785f3d3f616SMin Zhang .proc_handler = proc_dointvec, 27861da177e4SLinus Torvalds }, 27871da177e4SLinus Torvalds { 27881da177e4SLinus Torvalds .procname = "mtu_expires", 27894990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 27901da177e4SLinus Torvalds .maxlen = sizeof(int), 27911da177e4SLinus Torvalds .mode = 0644, 27926d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 27931da177e4SLinus Torvalds }, 27941da177e4SLinus Torvalds { 27951da177e4SLinus Torvalds .procname = "min_adv_mss", 27964990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 27971da177e4SLinus Torvalds .maxlen = sizeof(int), 27981da177e4SLinus Torvalds .mode = 0644, 2799f3d3f616SMin Zhang .proc_handler = proc_dointvec, 28001da177e4SLinus Torvalds }, 28011da177e4SLinus Torvalds { 28021da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 28034990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 28041da177e4SLinus Torvalds .maxlen = sizeof(int), 28051da177e4SLinus Torvalds .mode = 0644, 28066d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 28071da177e4SLinus Torvalds }, 2808f8572d8fSEric W. Biederman { } 28091da177e4SLinus Torvalds }; 28101da177e4SLinus Torvalds 28112c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 2812760f2d01SDaniel Lezcano { 2813760f2d01SDaniel Lezcano struct ctl_table *table; 2814760f2d01SDaniel Lezcano 2815760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 2816760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 2817760f2d01SDaniel Lezcano GFP_KERNEL); 28185ee09105SYOSHIFUJI Hideaki 28195ee09105SYOSHIFUJI Hideaki if (table) { 28205ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 2821c486da34SLucian Adrian Grijincu table[0].extra1 = net; 282286393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 28235ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 28245ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 28255ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 28265ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 28275ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 28285ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 28295ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 28309c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 28315ee09105SYOSHIFUJI Hideaki } 28325ee09105SYOSHIFUJI Hideaki 2833760f2d01SDaniel Lezcano return table; 2834760f2d01SDaniel Lezcano } 28351da177e4SLinus Torvalds #endif 28361da177e4SLinus Torvalds 28372c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 2838cdb18761SDaniel Lezcano { 2839633d424bSPavel Emelyanov int ret = -ENOMEM; 28408ed67789SDaniel Lezcano 284186393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 284286393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 2843f2fc6a54SBenjamin Thery 2844fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 2845fc66f95cSEric Dumazet goto out_ip6_dst_ops; 2846fc66f95cSEric Dumazet 28478ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 28488ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 28498ed67789SDaniel Lezcano GFP_KERNEL); 28508ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 2851fc66f95cSEric Dumazet goto out_ip6_dst_entries; 2852d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.path = 28538ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_null_entry; 2854d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 285562fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 285662fa8a84SDavid S. Miller ip6_template_metrics, true); 28578ed67789SDaniel Lezcano 28588ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 28598ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 28608ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 28618ed67789SDaniel Lezcano GFP_KERNEL); 286268fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 286368fffc67SPeter Zijlstra goto out_ip6_null_entry; 2864d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.path = 28658ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_prohibit_entry; 2866d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 286762fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 286862fa8a84SDavid S. Miller ip6_template_metrics, true); 28698ed67789SDaniel Lezcano 28708ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 28718ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 28728ed67789SDaniel Lezcano GFP_KERNEL); 287368fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 287468fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 2875d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.path = 28768ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_blk_hole_entry; 2877d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 287862fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 287962fa8a84SDavid S. Miller ip6_template_metrics, true); 28808ed67789SDaniel Lezcano #endif 28818ed67789SDaniel Lezcano 2882b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 2883b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 2884b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 2885b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 2886b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 2887b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 2888b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 2889b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 2890b339a47cSPeter Zijlstra 2891cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS 2892cdb18761SDaniel Lezcano proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops); 2893cdb18761SDaniel Lezcano proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops); 2894cdb18761SDaniel Lezcano #endif 28956891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 28966891a346SBenjamin Thery 28978ed67789SDaniel Lezcano ret = 0; 28988ed67789SDaniel Lezcano out: 28998ed67789SDaniel Lezcano return ret; 2900f2fc6a54SBenjamin Thery 290168fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 290268fffc67SPeter Zijlstra out_ip6_prohibit_entry: 290368fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 290468fffc67SPeter Zijlstra out_ip6_null_entry: 290568fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 290668fffc67SPeter Zijlstra #endif 2907fc66f95cSEric Dumazet out_ip6_dst_entries: 2908fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 2909f2fc6a54SBenjamin Thery out_ip6_dst_ops: 2910f2fc6a54SBenjamin Thery goto out; 2911cdb18761SDaniel Lezcano } 2912cdb18761SDaniel Lezcano 29132c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 2914cdb18761SDaniel Lezcano { 2915cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS 2916cdb18761SDaniel Lezcano proc_net_remove(net, "ipv6_route"); 2917cdb18761SDaniel Lezcano proc_net_remove(net, "rt6_stats"); 2918cdb18761SDaniel Lezcano #endif 29198ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 29208ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 29218ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 29228ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 29238ed67789SDaniel Lezcano #endif 292441bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 2925cdb18761SDaniel Lezcano } 2926cdb18761SDaniel Lezcano 2927cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 2928cdb18761SDaniel Lezcano .init = ip6_route_net_init, 2929cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 2930cdb18761SDaniel Lezcano }; 2931cdb18761SDaniel Lezcano 2932c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net) 2933c3426b47SDavid S. Miller { 2934c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 2935c3426b47SDavid S. Miller 2936c3426b47SDavid S. Miller if (!bp) 2937c3426b47SDavid S. Miller return -ENOMEM; 2938c3426b47SDavid S. Miller inet_peer_base_init(bp); 2939c3426b47SDavid S. Miller net->ipv6.peers = bp; 2940c3426b47SDavid S. Miller return 0; 2941c3426b47SDavid S. Miller } 2942c3426b47SDavid S. Miller 2943c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net) 2944c3426b47SDavid S. Miller { 2945c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv6.peers; 2946c3426b47SDavid S. Miller 2947c3426b47SDavid S. Miller net->ipv6.peers = NULL; 294856a6b248SDavid S. Miller inetpeer_invalidate_tree(bp); 2949c3426b47SDavid S. Miller kfree(bp); 2950c3426b47SDavid S. Miller } 2951c3426b47SDavid S. Miller 29522b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = { 2953c3426b47SDavid S. Miller .init = ipv6_inetpeer_init, 2954c3426b47SDavid S. Miller .exit = ipv6_inetpeer_exit, 2955c3426b47SDavid S. Miller }; 2956c3426b47SDavid S. Miller 29578ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 29588ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 29598ed67789SDaniel Lezcano .priority = 0, 29608ed67789SDaniel Lezcano }; 29618ed67789SDaniel Lezcano 2962433d49c3SDaniel Lezcano int __init ip6_route_init(void) 29631da177e4SLinus Torvalds { 2964433d49c3SDaniel Lezcano int ret; 2965433d49c3SDaniel Lezcano 29669a7ec3a9SDaniel Lezcano ret = -ENOMEM; 29679a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 29689a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 29699a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 29709a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 2971c19a28e1SFernando Carrijo goto out; 297214e50e57SDavid S. Miller 2973fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 29748ed67789SDaniel Lezcano if (ret) 2975bdb3289fSDaniel Lezcano goto out_kmem_cache; 2976bdb3289fSDaniel Lezcano 29772a0c451aSThomas Graf ret = fib6_init(); 2978fc66f95cSEric Dumazet if (ret) 2979fc66f95cSEric Dumazet goto out_dst_entries; 2980fc66f95cSEric Dumazet 2981c3426b47SDavid S. Miller ret = register_pernet_subsys(&ipv6_inetpeer_ops); 2982c3426b47SDavid S. Miller if (ret) 29832a0c451aSThomas Graf goto out_fib6_init; 29842a0c451aSThomas Graf 29857e52b33bSDavid S. Miller ret = register_pernet_subsys(&ip6_route_net_ops); 29867e52b33bSDavid S. Miller if (ret) 29877e52b33bSDavid S. Miller goto out_register_inetpeer; 2988c3426b47SDavid S. Miller 29895dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 29905dc121e9SArnaud Ebalard 29918ed67789SDaniel Lezcano /* Registering of the loopback is done before this portion of code, 29928ed67789SDaniel Lezcano * the loopback reference in rt6_info will not be taken, do it 29938ed67789SDaniel Lezcano * manually for init_net */ 2994d8d1f30bSChangli Gao init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 29958ed67789SDaniel Lezcano init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 2996bdb3289fSDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 2997d8d1f30bSChangli Gao init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 29988ed67789SDaniel Lezcano init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 2999d8d1f30bSChangli Gao init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 30008ed67789SDaniel Lezcano init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 3001bdb3289fSDaniel Lezcano #endif 30022a0c451aSThomas Graf ret = fib6_init_late(); 3003433d49c3SDaniel Lezcano if (ret) 30048ed67789SDaniel Lezcano goto out_register_subsys; 3005433d49c3SDaniel Lezcano 3006433d49c3SDaniel Lezcano ret = xfrm6_init(); 3007433d49c3SDaniel Lezcano if (ret) 30082a0c451aSThomas Graf goto out_fib6_init_late; 3009c35b7e72SDaniel Lezcano 3010433d49c3SDaniel Lezcano ret = fib6_rules_init(); 3011433d49c3SDaniel Lezcano if (ret) 3012433d49c3SDaniel Lezcano goto xfrm6_init; 30137e5449c2SDaniel Lezcano 3014433d49c3SDaniel Lezcano ret = -ENOBUFS; 3015c7ac8679SGreg Rose if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) || 3016c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) || 3017c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL)) 3018433d49c3SDaniel Lezcano goto fib6_rules_init; 3019433d49c3SDaniel Lezcano 30208ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 3021cdb18761SDaniel Lezcano if (ret) 3022cdb18761SDaniel Lezcano goto fib6_rules_init; 30238ed67789SDaniel Lezcano 3024433d49c3SDaniel Lezcano out: 3025433d49c3SDaniel Lezcano return ret; 3026433d49c3SDaniel Lezcano 3027433d49c3SDaniel Lezcano fib6_rules_init: 3028433d49c3SDaniel Lezcano fib6_rules_cleanup(); 3029433d49c3SDaniel Lezcano xfrm6_init: 3030433d49c3SDaniel Lezcano xfrm6_fini(); 30312a0c451aSThomas Graf out_fib6_init_late: 30322a0c451aSThomas Graf fib6_cleanup_late(); 30338ed67789SDaniel Lezcano out_register_subsys: 30348ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 30357e52b33bSDavid S. Miller out_register_inetpeer: 30367e52b33bSDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 30372a0c451aSThomas Graf out_fib6_init: 30382a0c451aSThomas Graf fib6_gc_cleanup(); 3039fc66f95cSEric Dumazet out_dst_entries: 3040fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 3041433d49c3SDaniel Lezcano out_kmem_cache: 3042f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 3043433d49c3SDaniel Lezcano goto out; 30441da177e4SLinus Torvalds } 30451da177e4SLinus Torvalds 30461da177e4SLinus Torvalds void ip6_route_cleanup(void) 30471da177e4SLinus Torvalds { 30488ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 3049101367c2SThomas Graf fib6_rules_cleanup(); 30501da177e4SLinus Torvalds xfrm6_fini(); 30511da177e4SLinus Torvalds fib6_gc_cleanup(); 3052c3426b47SDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 30538ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 305441bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 3055f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 30561da177e4SLinus Torvalds } 3057