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 */ 2615c1e6aa3SDavid S. Miller static inline struct rt6_info *ip6_dst_alloc(struct dst_ops *ops, 262957c665fSDavid S. Miller struct net_device *dev, 263957c665fSDavid S. Miller int flags) 2641da177e4SLinus Torvalds { 265957c665fSDavid S. Miller struct rt6_info *rt = dst_alloc(ops, dev, 0, 0, flags); 266cf911662SDavid S. Miller 26738308473SDavid S. Miller if (rt) 268fbe58186SMadalin Bucur memset(&rt->rt6i_table, 0, 269fbe58186SMadalin Bucur sizeof(*rt) - sizeof(struct dst_entry)); 270cf911662SDavid S. Miller 271cf911662SDavid S. Miller return rt; 2721da177e4SLinus Torvalds } 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst) 2751da177e4SLinus Torvalds { 2761da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 2771da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 278b3419363SDavid S. Miller struct inet_peer *peer = rt->rt6i_peer; 2791da177e4SLinus Torvalds 2808e2ec639SYan, Zheng if (!(rt->dst.flags & DST_HOST)) 2818e2ec639SYan, Zheng dst_destroy_metrics_generic(dst); 2828e2ec639SYan, Zheng 28338308473SDavid S. Miller if (idev) { 2841da177e4SLinus Torvalds rt->rt6i_idev = NULL; 2851da177e4SLinus Torvalds in6_dev_put(idev); 2861da177e4SLinus Torvalds } 2871716a961SGao feng 2881716a961SGao feng if (!(rt->rt6i_flags & RTF_EXPIRES) && dst->from) 2891716a961SGao feng dst_release(dst->from); 2901716a961SGao feng 291b3419363SDavid S. Miller if (peer) { 292b3419363SDavid S. Miller rt->rt6i_peer = NULL; 293b3419363SDavid S. Miller inet_putpeer(peer); 294b3419363SDavid S. Miller } 295b3419363SDavid S. Miller } 296b3419363SDavid S. Miller 2976431cbc2SDavid S. Miller static atomic_t __rt6_peer_genid = ATOMIC_INIT(0); 2986431cbc2SDavid S. Miller 2996431cbc2SDavid S. Miller static u32 rt6_peer_genid(void) 3006431cbc2SDavid S. Miller { 3016431cbc2SDavid S. Miller return atomic_read(&__rt6_peer_genid); 3026431cbc2SDavid S. Miller } 3036431cbc2SDavid S. Miller 304b3419363SDavid S. Miller void rt6_bind_peer(struct rt6_info *rt, int create) 305b3419363SDavid S. Miller { 30654db0cc2SGao feng struct net *net = dev_net(rt->dst.dev); 307b3419363SDavid S. Miller struct inet_peer *peer; 308b3419363SDavid S. Miller 30954db0cc2SGao feng peer = inet_getpeer_v6(net, &rt->rt6i_dst.addr, create); 310b3419363SDavid S. Miller if (peer && cmpxchg(&rt->rt6i_peer, NULL, peer) != NULL) 311b3419363SDavid S. Miller inet_putpeer(peer); 3126431cbc2SDavid S. Miller else 3136431cbc2SDavid S. Miller rt->rt6i_peer_genid = rt6_peer_genid(); 3141da177e4SLinus Torvalds } 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 3171da177e4SLinus Torvalds int how) 3181da177e4SLinus Torvalds { 3191da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 3201da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 3215a3e55d6SDenis V. Lunev struct net_device *loopback_dev = 322c346dca1SYOSHIFUJI Hideaki dev_net(dev)->loopback_dev; 3231da177e4SLinus Torvalds 32438308473SDavid S. Miller if (dev != loopback_dev && idev && idev->dev == dev) { 3255a3e55d6SDenis V. Lunev struct inet6_dev *loopback_idev = 3265a3e55d6SDenis V. Lunev in6_dev_get(loopback_dev); 32738308473SDavid S. Miller if (loopback_idev) { 3281da177e4SLinus Torvalds rt->rt6i_idev = loopback_idev; 3291da177e4SLinus Torvalds in6_dev_put(idev); 3301da177e4SLinus Torvalds } 3311da177e4SLinus Torvalds } 3321da177e4SLinus Torvalds } 3331da177e4SLinus Torvalds 334a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt) 3351da177e4SLinus Torvalds { 3361716a961SGao feng struct rt6_info *ort = NULL; 3371716a961SGao feng 3381716a961SGao feng if (rt->rt6i_flags & RTF_EXPIRES) { 3391716a961SGao feng if (time_after(jiffies, rt->dst.expires)) 340a50feda5SEric Dumazet return true; 3411716a961SGao feng } else if (rt->dst.from) { 3421716a961SGao feng ort = (struct rt6_info *) rt->dst.from; 3431716a961SGao feng return (ort->rt6i_flags & RTF_EXPIRES) && 3441716a961SGao feng time_after(jiffies, ort->dst.expires); 3451716a961SGao feng } 346a50feda5SEric Dumazet return false; 3471da177e4SLinus Torvalds } 3481da177e4SLinus Torvalds 349a50feda5SEric Dumazet static bool rt6_need_strict(const struct in6_addr *daddr) 350c71099acSThomas Graf { 351a02cec21SEric Dumazet return ipv6_addr_type(daddr) & 352a02cec21SEric Dumazet (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK); 353c71099acSThomas Graf } 354c71099acSThomas Graf 3551da177e4SLinus Torvalds /* 356c71099acSThomas Graf * Route lookup. Any table->tb6_lock is implied. 3571da177e4SLinus Torvalds */ 3581da177e4SLinus Torvalds 3598ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net, 3608ed67789SDaniel Lezcano struct rt6_info *rt, 361b71d1d42SEric Dumazet const struct in6_addr *saddr, 3621da177e4SLinus Torvalds int oif, 363d420895eSYOSHIFUJI Hideaki int flags) 3641da177e4SLinus Torvalds { 3651da177e4SLinus Torvalds struct rt6_info *local = NULL; 3661da177e4SLinus Torvalds struct rt6_info *sprt; 3671da177e4SLinus Torvalds 368dd3abc4eSYOSHIFUJI Hideaki if (!oif && ipv6_addr_any(saddr)) 369dd3abc4eSYOSHIFUJI Hideaki goto out; 370dd3abc4eSYOSHIFUJI Hideaki 371d8d1f30bSChangli Gao for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) { 372d1918542SDavid S. Miller struct net_device *dev = sprt->dst.dev; 373dd3abc4eSYOSHIFUJI Hideaki 374dd3abc4eSYOSHIFUJI Hideaki if (oif) { 3751da177e4SLinus Torvalds if (dev->ifindex == oif) 3761da177e4SLinus Torvalds return sprt; 3771da177e4SLinus Torvalds if (dev->flags & IFF_LOOPBACK) { 37838308473SDavid S. Miller if (!sprt->rt6i_idev || 3791da177e4SLinus Torvalds sprt->rt6i_idev->dev->ifindex != oif) { 380d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE && oif) 3811da177e4SLinus Torvalds continue; 3821da177e4SLinus Torvalds if (local && (!oif || 3831da177e4SLinus Torvalds local->rt6i_idev->dev->ifindex == oif)) 3841da177e4SLinus Torvalds continue; 3851da177e4SLinus Torvalds } 3861da177e4SLinus Torvalds local = sprt; 3871da177e4SLinus Torvalds } 388dd3abc4eSYOSHIFUJI Hideaki } else { 389dd3abc4eSYOSHIFUJI Hideaki if (ipv6_chk_addr(net, saddr, dev, 390dd3abc4eSYOSHIFUJI Hideaki flags & RT6_LOOKUP_F_IFACE)) 391dd3abc4eSYOSHIFUJI Hideaki return sprt; 392dd3abc4eSYOSHIFUJI Hideaki } 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds 395dd3abc4eSYOSHIFUJI Hideaki if (oif) { 3961da177e4SLinus Torvalds if (local) 3971da177e4SLinus Torvalds return local; 3981da177e4SLinus Torvalds 399d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE) 4008ed67789SDaniel Lezcano return net->ipv6.ip6_null_entry; 4011da177e4SLinus Torvalds } 402dd3abc4eSYOSHIFUJI Hideaki out: 4031da177e4SLinus Torvalds return rt; 4041da177e4SLinus Torvalds } 4051da177e4SLinus Torvalds 40627097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 40727097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt) 40827097255SYOSHIFUJI Hideaki { 409f2c31e32SEric Dumazet struct neighbour *neigh; 41027097255SYOSHIFUJI Hideaki /* 41127097255SYOSHIFUJI Hideaki * Okay, this does not seem to be appropriate 41227097255SYOSHIFUJI Hideaki * for now, however, we need to check if it 41327097255SYOSHIFUJI Hideaki * is really so; aka Router Reachability Probing. 41427097255SYOSHIFUJI Hideaki * 41527097255SYOSHIFUJI Hideaki * Router Reachability Probe MUST be rate-limited 41627097255SYOSHIFUJI Hideaki * to no more than one per minute. 41727097255SYOSHIFUJI Hideaki */ 418f2c31e32SEric Dumazet rcu_read_lock(); 41927217455SDavid Miller neigh = rt ? dst_get_neighbour_noref(&rt->dst) : NULL; 42027097255SYOSHIFUJI Hideaki if (!neigh || (neigh->nud_state & NUD_VALID)) 421f2c31e32SEric Dumazet goto out; 42227097255SYOSHIFUJI Hideaki read_lock_bh(&neigh->lock); 42327097255SYOSHIFUJI Hideaki if (!(neigh->nud_state & NUD_VALID) && 42452e16356SYOSHIFUJI Hideaki time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) { 42527097255SYOSHIFUJI Hideaki struct in6_addr mcaddr; 42627097255SYOSHIFUJI Hideaki struct in6_addr *target; 42727097255SYOSHIFUJI Hideaki 42827097255SYOSHIFUJI Hideaki neigh->updated = jiffies; 42927097255SYOSHIFUJI Hideaki read_unlock_bh(&neigh->lock); 43027097255SYOSHIFUJI Hideaki 43127097255SYOSHIFUJI Hideaki target = (struct in6_addr *)&neigh->primary_key; 43227097255SYOSHIFUJI Hideaki addrconf_addr_solict_mult(target, &mcaddr); 433d1918542SDavid S. Miller ndisc_send_ns(rt->dst.dev, NULL, target, &mcaddr, NULL); 434f2c31e32SEric Dumazet } else { 43527097255SYOSHIFUJI Hideaki read_unlock_bh(&neigh->lock); 43627097255SYOSHIFUJI Hideaki } 437f2c31e32SEric Dumazet out: 438f2c31e32SEric Dumazet rcu_read_unlock(); 439f2c31e32SEric Dumazet } 44027097255SYOSHIFUJI Hideaki #else 44127097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt) 44227097255SYOSHIFUJI Hideaki { 44327097255SYOSHIFUJI Hideaki } 44427097255SYOSHIFUJI Hideaki #endif 44527097255SYOSHIFUJI Hideaki 4461da177e4SLinus Torvalds /* 447554cfb7eSYOSHIFUJI Hideaki * Default Router Selection (RFC 2461 6.3.6) 4481da177e4SLinus Torvalds */ 449b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif) 4501da177e4SLinus Torvalds { 451d1918542SDavid S. Miller struct net_device *dev = rt->dst.dev; 452161980f4SDavid S. Miller if (!oif || dev->ifindex == oif) 453554cfb7eSYOSHIFUJI Hideaki return 2; 454161980f4SDavid S. Miller if ((dev->flags & IFF_LOOPBACK) && 455161980f4SDavid S. Miller rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif) 456161980f4SDavid S. Miller return 1; 457554cfb7eSYOSHIFUJI Hideaki return 0; 4581da177e4SLinus Torvalds } 4591da177e4SLinus Torvalds 460b6f99a21SDave Jones static inline int rt6_check_neigh(struct rt6_info *rt) 4611da177e4SLinus Torvalds { 462f2c31e32SEric Dumazet struct neighbour *neigh; 463398bcbebSYOSHIFUJI Hideaki int m; 464f2c31e32SEric Dumazet 465f2c31e32SEric Dumazet rcu_read_lock(); 46627217455SDavid Miller neigh = dst_get_neighbour_noref(&rt->dst); 4674d0c5911SYOSHIFUJI Hideaki if (rt->rt6i_flags & RTF_NONEXTHOP || 4684d0c5911SYOSHIFUJI Hideaki !(rt->rt6i_flags & RTF_GATEWAY)) 4694d0c5911SYOSHIFUJI Hideaki m = 1; 4704d0c5911SYOSHIFUJI Hideaki else if (neigh) { 4711da177e4SLinus Torvalds read_lock_bh(&neigh->lock); 472554cfb7eSYOSHIFUJI Hideaki if (neigh->nud_state & NUD_VALID) 4734d0c5911SYOSHIFUJI Hideaki m = 2; 474398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 475398bcbebSYOSHIFUJI Hideaki else if (neigh->nud_state & NUD_FAILED) 476398bcbebSYOSHIFUJI Hideaki m = 0; 477398bcbebSYOSHIFUJI Hideaki #endif 478398bcbebSYOSHIFUJI Hideaki else 479ea73ee23SYOSHIFUJI Hideaki m = 1; 4801da177e4SLinus Torvalds read_unlock_bh(&neigh->lock); 481398bcbebSYOSHIFUJI Hideaki } else 482398bcbebSYOSHIFUJI Hideaki m = 0; 483f2c31e32SEric Dumazet rcu_read_unlock(); 484554cfb7eSYOSHIFUJI Hideaki return m; 4851da177e4SLinus Torvalds } 4861da177e4SLinus Torvalds 487554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif, 488554cfb7eSYOSHIFUJI Hideaki int strict) 489554cfb7eSYOSHIFUJI Hideaki { 4904d0c5911SYOSHIFUJI Hideaki int m, n; 4914d0c5911SYOSHIFUJI Hideaki 4924d0c5911SYOSHIFUJI Hideaki m = rt6_check_dev(rt, oif); 49377d16f45SYOSHIFUJI Hideaki if (!m && (strict & RT6_LOOKUP_F_IFACE)) 494554cfb7eSYOSHIFUJI Hideaki return -1; 495ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 496ebacaaa0SYOSHIFUJI Hideaki m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2; 497ebacaaa0SYOSHIFUJI Hideaki #endif 4984d0c5911SYOSHIFUJI Hideaki n = rt6_check_neigh(rt); 499557e92efSYOSHIFUJI Hideaki if (!n && (strict & RT6_LOOKUP_F_REACHABLE)) 500554cfb7eSYOSHIFUJI Hideaki return -1; 501554cfb7eSYOSHIFUJI Hideaki return m; 502554cfb7eSYOSHIFUJI Hideaki } 503554cfb7eSYOSHIFUJI Hideaki 504f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict, 505f11e6659SDavid S. Miller int *mpri, struct rt6_info *match) 506554cfb7eSYOSHIFUJI Hideaki { 507554cfb7eSYOSHIFUJI Hideaki int m; 508554cfb7eSYOSHIFUJI Hideaki 509554cfb7eSYOSHIFUJI Hideaki if (rt6_check_expired(rt)) 510f11e6659SDavid S. Miller goto out; 511554cfb7eSYOSHIFUJI Hideaki 512554cfb7eSYOSHIFUJI Hideaki m = rt6_score_route(rt, oif, strict); 513554cfb7eSYOSHIFUJI Hideaki if (m < 0) 514f11e6659SDavid S. Miller goto out; 515554cfb7eSYOSHIFUJI Hideaki 516f11e6659SDavid S. Miller if (m > *mpri) { 517ea659e07SYOSHIFUJI Hideaki if (strict & RT6_LOOKUP_F_REACHABLE) 51827097255SYOSHIFUJI Hideaki rt6_probe(match); 519f11e6659SDavid S. Miller *mpri = m; 520554cfb7eSYOSHIFUJI Hideaki match = rt; 521ea659e07SYOSHIFUJI Hideaki } else if (strict & RT6_LOOKUP_F_REACHABLE) { 52227097255SYOSHIFUJI Hideaki rt6_probe(rt); 5231da177e4SLinus Torvalds } 524f11e6659SDavid S. Miller 525f11e6659SDavid S. Miller out: 526f11e6659SDavid S. Miller return match; 5271da177e4SLinus Torvalds } 5281da177e4SLinus Torvalds 529f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn, 530f11e6659SDavid S. Miller struct rt6_info *rr_head, 531f11e6659SDavid S. Miller u32 metric, int oif, int strict) 532f11e6659SDavid S. Miller { 533f11e6659SDavid S. Miller struct rt6_info *rt, *match; 534f11e6659SDavid S. Miller int mpri = -1; 535f11e6659SDavid S. Miller 536f11e6659SDavid S. Miller match = NULL; 537f11e6659SDavid S. Miller for (rt = rr_head; rt && rt->rt6i_metric == metric; 538d8d1f30bSChangli Gao rt = rt->dst.rt6_next) 539f11e6659SDavid S. Miller match = find_match(rt, oif, strict, &mpri, match); 540f11e6659SDavid S. Miller for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric; 541d8d1f30bSChangli Gao rt = rt->dst.rt6_next) 542f11e6659SDavid S. Miller match = find_match(rt, oif, strict, &mpri, match); 543f11e6659SDavid S. Miller 544f11e6659SDavid S. Miller return match; 545f11e6659SDavid S. Miller } 546f11e6659SDavid S. Miller 547f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict) 548f11e6659SDavid S. Miller { 549f11e6659SDavid S. Miller struct rt6_info *match, *rt0; 5508ed67789SDaniel Lezcano struct net *net; 551f11e6659SDavid S. Miller 552f11e6659SDavid S. Miller rt0 = fn->rr_ptr; 553f11e6659SDavid S. Miller if (!rt0) 554f11e6659SDavid S. Miller fn->rr_ptr = rt0 = fn->leaf; 555f11e6659SDavid S. Miller 556f11e6659SDavid S. Miller match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict); 557f11e6659SDavid S. Miller 558554cfb7eSYOSHIFUJI Hideaki if (!match && 559f11e6659SDavid S. Miller (strict & RT6_LOOKUP_F_REACHABLE)) { 560d8d1f30bSChangli Gao struct rt6_info *next = rt0->dst.rt6_next; 561f11e6659SDavid S. Miller 562554cfb7eSYOSHIFUJI Hideaki /* no entries matched; do round-robin */ 563f11e6659SDavid S. Miller if (!next || next->rt6i_metric != rt0->rt6i_metric) 564f11e6659SDavid S. Miller next = fn->leaf; 565f11e6659SDavid S. Miller 566f11e6659SDavid S. Miller if (next != rt0) 567f11e6659SDavid S. Miller fn->rr_ptr = next; 568554cfb7eSYOSHIFUJI Hideaki } 569554cfb7eSYOSHIFUJI Hideaki 570d1918542SDavid S. Miller net = dev_net(rt0->dst.dev); 571a02cec21SEric Dumazet return match ? match : net->ipv6.ip6_null_entry; 5721da177e4SLinus Torvalds } 5731da177e4SLinus Torvalds 57470ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 57570ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 576b71d1d42SEric Dumazet const struct in6_addr *gwaddr) 57770ceb4f5SYOSHIFUJI Hideaki { 578c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 57970ceb4f5SYOSHIFUJI Hideaki struct route_info *rinfo = (struct route_info *) opt; 58070ceb4f5SYOSHIFUJI Hideaki struct in6_addr prefix_buf, *prefix; 58170ceb4f5SYOSHIFUJI Hideaki unsigned int pref; 5824bed72e4SYOSHIFUJI Hideaki unsigned long lifetime; 58370ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt; 58470ceb4f5SYOSHIFUJI Hideaki 58570ceb4f5SYOSHIFUJI Hideaki if (len < sizeof(struct route_info)) { 58670ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 58770ceb4f5SYOSHIFUJI Hideaki } 58870ceb4f5SYOSHIFUJI Hideaki 58970ceb4f5SYOSHIFUJI Hideaki /* Sanity check for prefix_len and length */ 59070ceb4f5SYOSHIFUJI Hideaki if (rinfo->length > 3) { 59170ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 59270ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 128) { 59370ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 59470ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 64) { 59570ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 2) { 59670ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 59770ceb4f5SYOSHIFUJI Hideaki } 59870ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 0) { 59970ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 1) { 60070ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 60170ceb4f5SYOSHIFUJI Hideaki } 60270ceb4f5SYOSHIFUJI Hideaki } 60370ceb4f5SYOSHIFUJI Hideaki 60470ceb4f5SYOSHIFUJI Hideaki pref = rinfo->route_pref; 60570ceb4f5SYOSHIFUJI Hideaki if (pref == ICMPV6_ROUTER_PREF_INVALID) 6063933fc95SJens Rosenboom return -EINVAL; 60770ceb4f5SYOSHIFUJI Hideaki 6084bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); 60970ceb4f5SYOSHIFUJI Hideaki 61070ceb4f5SYOSHIFUJI Hideaki if (rinfo->length == 3) 61170ceb4f5SYOSHIFUJI Hideaki prefix = (struct in6_addr *)rinfo->prefix; 61270ceb4f5SYOSHIFUJI Hideaki else { 61370ceb4f5SYOSHIFUJI Hideaki /* this function is safe */ 61470ceb4f5SYOSHIFUJI Hideaki ipv6_addr_prefix(&prefix_buf, 61570ceb4f5SYOSHIFUJI Hideaki (struct in6_addr *)rinfo->prefix, 61670ceb4f5SYOSHIFUJI Hideaki rinfo->prefix_len); 61770ceb4f5SYOSHIFUJI Hideaki prefix = &prefix_buf; 61870ceb4f5SYOSHIFUJI Hideaki } 61970ceb4f5SYOSHIFUJI Hideaki 620efa2cea0SDaniel Lezcano rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr, 621efa2cea0SDaniel Lezcano dev->ifindex); 62270ceb4f5SYOSHIFUJI Hideaki 62370ceb4f5SYOSHIFUJI Hideaki if (rt && !lifetime) { 624e0a1ad73SThomas Graf ip6_del_rt(rt); 62570ceb4f5SYOSHIFUJI Hideaki rt = NULL; 62670ceb4f5SYOSHIFUJI Hideaki } 62770ceb4f5SYOSHIFUJI Hideaki 62870ceb4f5SYOSHIFUJI Hideaki if (!rt && lifetime) 629efa2cea0SDaniel Lezcano rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex, 63070ceb4f5SYOSHIFUJI Hideaki pref); 63170ceb4f5SYOSHIFUJI Hideaki else if (rt) 63270ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags = RTF_ROUTEINFO | 63370ceb4f5SYOSHIFUJI Hideaki (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 63470ceb4f5SYOSHIFUJI Hideaki 63570ceb4f5SYOSHIFUJI Hideaki if (rt) { 6361716a961SGao feng if (!addrconf_finite_timeout(lifetime)) 6371716a961SGao feng rt6_clean_expires(rt); 6381716a961SGao feng else 6391716a961SGao feng rt6_set_expires(rt, jiffies + HZ * lifetime); 6401716a961SGao feng 641d8d1f30bSChangli Gao dst_release(&rt->dst); 64270ceb4f5SYOSHIFUJI Hideaki } 64370ceb4f5SYOSHIFUJI Hideaki return 0; 64470ceb4f5SYOSHIFUJI Hideaki } 64570ceb4f5SYOSHIFUJI Hideaki #endif 64670ceb4f5SYOSHIFUJI Hideaki 6478ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr) \ 648982f56f3SYOSHIFUJI Hideaki do { \ 6498ed67789SDaniel Lezcano if (rt == __net->ipv6.ip6_null_entry) { \ 650982f56f3SYOSHIFUJI Hideaki struct fib6_node *pn; \ 651e0eda7bbSVille Nuorvala while (1) { \ 652982f56f3SYOSHIFUJI Hideaki if (fn->fn_flags & RTN_TL_ROOT) \ 653c71099acSThomas Graf goto out; \ 654982f56f3SYOSHIFUJI Hideaki pn = fn->parent; \ 655982f56f3SYOSHIFUJI Hideaki if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \ 6568bce65b9SKim Nordlund fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \ 657982f56f3SYOSHIFUJI Hideaki else \ 658982f56f3SYOSHIFUJI Hideaki fn = pn; \ 659c71099acSThomas Graf if (fn->fn_flags & RTN_RTINFO) \ 660c71099acSThomas Graf goto restart; \ 661c71099acSThomas Graf } \ 662982f56f3SYOSHIFUJI Hideaki } \ 663982f56f3SYOSHIFUJI Hideaki } while (0) 664c71099acSThomas Graf 6658ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net, 6668ed67789SDaniel Lezcano struct fib6_table *table, 6674c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 6681da177e4SLinus Torvalds { 6691da177e4SLinus Torvalds struct fib6_node *fn; 6701da177e4SLinus Torvalds struct rt6_info *rt; 6711da177e4SLinus Torvalds 672c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 6734c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 674c71099acSThomas Graf restart: 675c71099acSThomas Graf rt = fn->leaf; 6764c9483b2SDavid S. Miller rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags); 6774c9483b2SDavid S. Miller BACKTRACK(net, &fl6->saddr); 678c71099acSThomas Graf out: 679d8d1f30bSChangli Gao dst_use(&rt->dst, jiffies); 680c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 6811da177e4SLinus Torvalds return rt; 682c71099acSThomas Graf 683c71099acSThomas Graf } 684c71099acSThomas Graf 685ea6e574eSFlorian Westphal struct dst_entry * ip6_route_lookup(struct net *net, struct flowi6 *fl6, 686ea6e574eSFlorian Westphal int flags) 687ea6e574eSFlorian Westphal { 688ea6e574eSFlorian Westphal return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup); 689ea6e574eSFlorian Westphal } 690ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup); 691ea6e574eSFlorian Westphal 6929acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, 6939acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *saddr, int oif, int strict) 694c71099acSThomas Graf { 6954c9483b2SDavid S. Miller struct flowi6 fl6 = { 6964c9483b2SDavid S. Miller .flowi6_oif = oif, 6974c9483b2SDavid S. Miller .daddr = *daddr, 698c71099acSThomas Graf }; 699c71099acSThomas Graf struct dst_entry *dst; 70077d16f45SYOSHIFUJI Hideaki int flags = strict ? RT6_LOOKUP_F_IFACE : 0; 701c71099acSThomas Graf 702adaa70bbSThomas Graf if (saddr) { 7034c9483b2SDavid S. Miller memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 704adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 705adaa70bbSThomas Graf } 706adaa70bbSThomas Graf 7074c9483b2SDavid S. Miller dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup); 708c71099acSThomas Graf if (dst->error == 0) 709c71099acSThomas Graf return (struct rt6_info *) dst; 710c71099acSThomas Graf 711c71099acSThomas Graf dst_release(dst); 712c71099acSThomas Graf 7131da177e4SLinus Torvalds return NULL; 7141da177e4SLinus Torvalds } 7151da177e4SLinus Torvalds 7167159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup); 7177159039aSYOSHIFUJI Hideaki 718c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock. 7191da177e4SLinus Torvalds It takes new route entry, the addition fails by any reason the 7201da177e4SLinus Torvalds route is freed. In any case, if caller does not hold it, it may 7211da177e4SLinus Torvalds be destroyed. 7221da177e4SLinus Torvalds */ 7231da177e4SLinus Torvalds 72486872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info) 7251da177e4SLinus Torvalds { 7261da177e4SLinus Torvalds int err; 727c71099acSThomas Graf struct fib6_table *table; 7281da177e4SLinus Torvalds 729c71099acSThomas Graf table = rt->rt6i_table; 730c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 73186872cb5SThomas Graf err = fib6_add(&table->tb6_root, rt, info); 732c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 7331da177e4SLinus Torvalds 7341da177e4SLinus Torvalds return err; 7351da177e4SLinus Torvalds } 7361da177e4SLinus Torvalds 73740e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt) 73840e22e8fSThomas Graf { 7394d1169c1SDenis V. Lunev struct nl_info info = { 740d1918542SDavid S. Miller .nl_net = dev_net(rt->dst.dev), 7414d1169c1SDenis V. Lunev }; 742528c4cebSDenis V. Lunev return __ip6_ins_rt(rt, &info); 74340e22e8fSThomas Graf } 74440e22e8fSThomas Graf 7451716a961SGao feng static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, 74621efcfa0SEric Dumazet const struct in6_addr *daddr, 747b71d1d42SEric Dumazet const struct in6_addr *saddr) 7481da177e4SLinus Torvalds { 7491da177e4SLinus Torvalds struct rt6_info *rt; 7501da177e4SLinus Torvalds 7511da177e4SLinus Torvalds /* 7521da177e4SLinus Torvalds * Clone the route. 7531da177e4SLinus Torvalds */ 7541da177e4SLinus Torvalds 75521efcfa0SEric Dumazet rt = ip6_rt_copy(ort, daddr); 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds if (rt) { 75814deae41SDavid S. Miller int attempts = !in_softirq(); 75914deae41SDavid S. Miller 76058c4fb86SYOSHIFUJI Hideaki if (!(rt->rt6i_flags & RTF_GATEWAY)) { 761bb3c3686SDavid S. Miller if (ort->rt6i_dst.plen != 128 && 76221efcfa0SEric Dumazet ipv6_addr_equal(&ort->rt6i_dst.addr, daddr)) 76358c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 7644e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = *daddr; 76558c4fb86SYOSHIFUJI Hideaki } 7661da177e4SLinus Torvalds 7671da177e4SLinus Torvalds rt->rt6i_flags |= RTF_CACHE; 7681da177e4SLinus Torvalds 7691da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 7701da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 7714e3fd7a0SAlexey Dobriyan rt->rt6i_src.addr = *saddr; 7721da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 7731da177e4SLinus Torvalds } 7741da177e4SLinus Torvalds #endif 7751da177e4SLinus Torvalds 77614deae41SDavid S. Miller retry: 7778ade06c6SDavid S. Miller if (rt6_bind_neighbour(rt, rt->dst.dev)) { 778d1918542SDavid S. Miller struct net *net = dev_net(rt->dst.dev); 77914deae41SDavid S. Miller int saved_rt_min_interval = 78014deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_min_interval; 78114deae41SDavid S. Miller int saved_rt_elasticity = 78214deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_elasticity; 78314deae41SDavid S. Miller 78414deae41SDavid S. Miller if (attempts-- > 0) { 78514deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_elasticity = 1; 78614deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_min_interval = 0; 78714deae41SDavid S. Miller 78886393e52SAlexey Dobriyan ip6_dst_gc(&net->ipv6.ip6_dst_ops); 78914deae41SDavid S. Miller 79014deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_elasticity = 79114deae41SDavid S. Miller saved_rt_elasticity; 79214deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_min_interval = 79314deae41SDavid S. Miller saved_rt_min_interval; 79414deae41SDavid S. Miller goto retry; 79514deae41SDavid S. Miller } 79614deae41SDavid S. Miller 797f3213831SJoe Perches net_warn_ratelimited("Neighbour table overflow\n"); 798d8d1f30bSChangli Gao dst_free(&rt->dst); 79914deae41SDavid S. Miller return NULL; 80014deae41SDavid S. Miller } 80195a9a5baSYOSHIFUJI Hideaki } 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds return rt; 8041da177e4SLinus Torvalds } 80595a9a5baSYOSHIFUJI Hideaki 80621efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, 80721efcfa0SEric Dumazet const struct in6_addr *daddr) 808299d9939SYOSHIFUJI Hideaki { 80921efcfa0SEric Dumazet struct rt6_info *rt = ip6_rt_copy(ort, daddr); 81021efcfa0SEric Dumazet 811299d9939SYOSHIFUJI Hideaki if (rt) { 812299d9939SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_CACHE; 81327217455SDavid Miller dst_set_neighbour(&rt->dst, neigh_clone(dst_get_neighbour_noref_raw(&ort->dst))); 814299d9939SYOSHIFUJI Hideaki } 815299d9939SYOSHIFUJI Hideaki return rt; 816299d9939SYOSHIFUJI Hideaki } 817299d9939SYOSHIFUJI Hideaki 8188ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif, 8194c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 8201da177e4SLinus Torvalds { 8211da177e4SLinus Torvalds struct fib6_node *fn; 822519fbd87SYOSHIFUJI Hideaki struct rt6_info *rt, *nrt; 823c71099acSThomas Graf int strict = 0; 8241da177e4SLinus Torvalds int attempts = 3; 825519fbd87SYOSHIFUJI Hideaki int err; 82653b7997fSYOSHIFUJI Hideaki int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE; 8271da177e4SLinus Torvalds 82877d16f45SYOSHIFUJI Hideaki strict |= flags & RT6_LOOKUP_F_IFACE; 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds relookup: 831c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 8321da177e4SLinus Torvalds 8338238dd06SYOSHIFUJI Hideaki restart_2: 8344c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds restart: 8374acad72dSPavel Emelyanov rt = rt6_select(fn, oif, strict | reachable); 8388ed67789SDaniel Lezcano 8394c9483b2SDavid S. Miller BACKTRACK(net, &fl6->saddr); 8408ed67789SDaniel Lezcano if (rt == net->ipv6.ip6_null_entry || 8418238dd06SYOSHIFUJI Hideaki rt->rt6i_flags & RTF_CACHE) 8421da177e4SLinus Torvalds goto out; 8431da177e4SLinus Torvalds 844d8d1f30bSChangli Gao dst_hold(&rt->dst); 845c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 8461da177e4SLinus Torvalds 84727217455SDavid Miller if (!dst_get_neighbour_noref_raw(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP)) 8484c9483b2SDavid S. Miller nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr); 8497343ff31SDavid S. Miller else if (!(rt->dst.flags & DST_HOST)) 8504c9483b2SDavid S. Miller nrt = rt6_alloc_clone(rt, &fl6->daddr); 8517343ff31SDavid S. Miller else 8527343ff31SDavid S. Miller goto out2; 8531da177e4SLinus Torvalds 854d8d1f30bSChangli Gao dst_release(&rt->dst); 8558ed67789SDaniel Lezcano rt = nrt ? : net->ipv6.ip6_null_entry; 8561da177e4SLinus Torvalds 857d8d1f30bSChangli Gao dst_hold(&rt->dst); 858e40cf353SYOSHIFUJI Hideaki if (nrt) { 85940e22e8fSThomas Graf err = ip6_ins_rt(nrt); 860e40cf353SYOSHIFUJI Hideaki if (!err) 861e40cf353SYOSHIFUJI Hideaki goto out2; 862e40cf353SYOSHIFUJI Hideaki } 863e40cf353SYOSHIFUJI Hideaki 864e40cf353SYOSHIFUJI Hideaki if (--attempts <= 0) 8651da177e4SLinus Torvalds goto out2; 8661da177e4SLinus Torvalds 867519fbd87SYOSHIFUJI Hideaki /* 868c71099acSThomas Graf * Race condition! In the gap, when table->tb6_lock was 869519fbd87SYOSHIFUJI Hideaki * released someone could insert this route. Relookup. 8701da177e4SLinus Torvalds */ 871d8d1f30bSChangli Gao dst_release(&rt->dst); 8721da177e4SLinus Torvalds goto relookup; 873e40cf353SYOSHIFUJI Hideaki 874519fbd87SYOSHIFUJI Hideaki out: 8758238dd06SYOSHIFUJI Hideaki if (reachable) { 8768238dd06SYOSHIFUJI Hideaki reachable = 0; 8778238dd06SYOSHIFUJI Hideaki goto restart_2; 8788238dd06SYOSHIFUJI Hideaki } 879d8d1f30bSChangli Gao dst_hold(&rt->dst); 880c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 8811da177e4SLinus Torvalds out2: 882d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 883d8d1f30bSChangli Gao rt->dst.__use++; 884c71099acSThomas Graf 885c71099acSThomas Graf return rt; 886c71099acSThomas Graf } 887c71099acSThomas Graf 8888ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table, 8894c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 8904acad72dSPavel Emelyanov { 8914c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags); 8924acad72dSPavel Emelyanov } 8934acad72dSPavel Emelyanov 89472331bc0SShmulik Ladkani static struct dst_entry *ip6_route_input_lookup(struct net *net, 89572331bc0SShmulik Ladkani struct net_device *dev, 89672331bc0SShmulik Ladkani struct flowi6 *fl6, int flags) 89772331bc0SShmulik Ladkani { 89872331bc0SShmulik Ladkani if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) 89972331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_IFACE; 90072331bc0SShmulik Ladkani 90172331bc0SShmulik Ladkani return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input); 90272331bc0SShmulik Ladkani } 90372331bc0SShmulik Ladkani 904c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 905c71099acSThomas Graf { 906b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 907c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 908adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 9094c9483b2SDavid S. Miller struct flowi6 fl6 = { 9104c9483b2SDavid S. Miller .flowi6_iif = skb->dev->ifindex, 9114c9483b2SDavid S. Miller .daddr = iph->daddr, 9124c9483b2SDavid S. Miller .saddr = iph->saddr, 9134c9483b2SDavid S. Miller .flowlabel = (* (__be32 *) iph) & IPV6_FLOWINFO_MASK, 9144c9483b2SDavid S. Miller .flowi6_mark = skb->mark, 9154c9483b2SDavid S. Miller .flowi6_proto = iph->nexthdr, 916c71099acSThomas Graf }; 917adaa70bbSThomas Graf 91872331bc0SShmulik Ladkani skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags)); 919c71099acSThomas Graf } 920c71099acSThomas Graf 9218ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table, 9224c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 923c71099acSThomas Graf { 9244c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags); 925c71099acSThomas Graf } 926c71099acSThomas Graf 9279c7a4f9cSFlorian Westphal struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk, 9284c9483b2SDavid S. Miller struct flowi6 *fl6) 929c71099acSThomas Graf { 930c71099acSThomas Graf int flags = 0; 931c71099acSThomas Graf 9324c9483b2SDavid S. Miller if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr)) 93377d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 934c71099acSThomas Graf 9354c9483b2SDavid S. Miller if (!ipv6_addr_any(&fl6->saddr)) 936adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 9370c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 9380c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 939adaa70bbSThomas Graf 9404c9483b2SDavid S. Miller return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output); 9411da177e4SLinus Torvalds } 9421da177e4SLinus Torvalds 9437159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output); 9441da177e4SLinus Torvalds 9452774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 94614e50e57SDavid S. Miller { 9475c1e6aa3SDavid S. Miller struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; 94814e50e57SDavid S. Miller struct dst_entry *new = NULL; 94914e50e57SDavid S. Miller 9505c1e6aa3SDavid S. Miller rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, 0, 0); 95114e50e57SDavid S. Miller if (rt) { 952cf911662SDavid S. Miller memset(&rt->rt6i_table, 0, sizeof(*rt) - sizeof(struct dst_entry)); 953cf911662SDavid S. Miller 954d8d1f30bSChangli Gao new = &rt->dst; 95514e50e57SDavid S. Miller 95614e50e57SDavid S. Miller new->__use = 1; 957352e512cSHerbert Xu new->input = dst_discard; 958352e512cSHerbert Xu new->output = dst_discard; 95914e50e57SDavid S. Miller 96021efcfa0SEric Dumazet if (dst_metrics_read_only(&ort->dst)) 96121efcfa0SEric Dumazet new->_metrics = ort->dst._metrics; 96221efcfa0SEric Dumazet else 963defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 96414e50e57SDavid S. Miller rt->rt6i_idev = ort->rt6i_idev; 96514e50e57SDavid S. Miller if (rt->rt6i_idev) 96614e50e57SDavid S. Miller in6_dev_hold(rt->rt6i_idev); 96714e50e57SDavid S. Miller 9684e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 9691716a961SGao feng rt->rt6i_flags = ort->rt6i_flags; 9701716a961SGao feng rt6_clean_expires(rt); 97114e50e57SDavid S. Miller rt->rt6i_metric = 0; 97214e50e57SDavid S. Miller 97314e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 97414e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 97514e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 97614e50e57SDavid S. Miller #endif 97714e50e57SDavid S. Miller 97814e50e57SDavid S. Miller dst_free(new); 97914e50e57SDavid S. Miller } 98014e50e57SDavid S. Miller 98169ead7afSDavid S. Miller dst_release(dst_orig); 98269ead7afSDavid S. Miller return new ? new : ERR_PTR(-ENOMEM); 98314e50e57SDavid S. Miller } 98414e50e57SDavid S. Miller 9851da177e4SLinus Torvalds /* 9861da177e4SLinus Torvalds * Destination cache support functions 9871da177e4SLinus Torvalds */ 9881da177e4SLinus Torvalds 9891da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 9901da177e4SLinus Torvalds { 9911da177e4SLinus Torvalds struct rt6_info *rt; 9921da177e4SLinus Torvalds 9931da177e4SLinus Torvalds rt = (struct rt6_info *) dst; 9941da177e4SLinus Torvalds 9956431cbc2SDavid S. Miller if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) { 9966431cbc2SDavid S. Miller if (rt->rt6i_peer_genid != rt6_peer_genid()) { 9976431cbc2SDavid S. Miller if (!rt->rt6i_peer) 9986431cbc2SDavid S. Miller rt6_bind_peer(rt, 0); 9996431cbc2SDavid S. Miller rt->rt6i_peer_genid = rt6_peer_genid(); 10006431cbc2SDavid S. Miller } 10011da177e4SLinus Torvalds return dst; 10026431cbc2SDavid S. Miller } 10031da177e4SLinus Torvalds return NULL; 10041da177e4SLinus Torvalds } 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 10071da177e4SLinus Torvalds { 10081da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 10091da177e4SLinus Torvalds 10101da177e4SLinus Torvalds if (rt) { 101154c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 101254c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 1013e0a1ad73SThomas Graf ip6_del_rt(rt); 101454c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 10151da177e4SLinus Torvalds } 101654c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 101754c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 101854c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 101954c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 102054c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 102154c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 10221da177e4SLinus Torvalds } 10231da177e4SLinus Torvalds 10241da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 10251da177e4SLinus Torvalds { 10261da177e4SLinus Torvalds struct rt6_info *rt; 10271da177e4SLinus Torvalds 10283ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 10291da177e4SLinus Torvalds 1030adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 10311da177e4SLinus Torvalds if (rt) { 10321716a961SGao feng if (rt->rt6i_flags & RTF_CACHE) 10331716a961SGao feng rt6_update_expires(rt, 0); 10341716a961SGao feng else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) 10351da177e4SLinus Torvalds rt->rt6i_node->fn_sernum = -1; 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds } 10381da177e4SLinus Torvalds 10391da177e4SLinus Torvalds static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu) 10401da177e4SLinus Torvalds { 10411da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info*)dst; 10421da177e4SLinus Torvalds 10431da177e4SLinus Torvalds if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) { 10441da177e4SLinus Torvalds rt6->rt6i_flags |= RTF_MODIFIED; 10451da177e4SLinus Torvalds if (mtu < IPV6_MIN_MTU) { 1046defb3519SDavid S. Miller u32 features = dst_metric(dst, RTAX_FEATURES); 10471da177e4SLinus Torvalds mtu = IPV6_MIN_MTU; 1048defb3519SDavid S. Miller features |= RTAX_FEATURE_ALLFRAG; 1049defb3519SDavid S. Miller dst_metric_set(dst, RTAX_FEATURES, features); 10501da177e4SLinus Torvalds } 1051defb3519SDavid S. Miller dst_metric_set(dst, RTAX_MTU, mtu); 10521da177e4SLinus Torvalds } 10531da177e4SLinus Torvalds } 10541da177e4SLinus Torvalds 10550dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 10561da177e4SLinus Torvalds { 10570dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 10580dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 10590dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 10600dbaee3bSDavid S. Miller 10611da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 10621da177e4SLinus Torvalds 10635578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 10645578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 10651da177e4SLinus Torvalds 10661da177e4SLinus Torvalds /* 10671da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 10681da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 10691da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 10701da177e4SLinus Torvalds * rely only on pmtu discovery" 10711da177e4SLinus Torvalds */ 10721da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 10731da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 10741da177e4SLinus Torvalds return mtu; 10751da177e4SLinus Torvalds } 10761da177e4SLinus Torvalds 1077ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst) 1078d33e4553SDavid S. Miller { 1079d33e4553SDavid S. Miller struct inet6_dev *idev; 1080618f9bc7SSteffen Klassert unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); 1081618f9bc7SSteffen Klassert 1082618f9bc7SSteffen Klassert if (mtu) 1083618f9bc7SSteffen Klassert return mtu; 1084618f9bc7SSteffen Klassert 1085618f9bc7SSteffen Klassert mtu = IPV6_MIN_MTU; 1086d33e4553SDavid S. Miller 1087d33e4553SDavid S. Miller rcu_read_lock(); 1088d33e4553SDavid S. Miller idev = __in6_dev_get(dst->dev); 1089d33e4553SDavid S. Miller if (idev) 1090d33e4553SDavid S. Miller mtu = idev->cnf.mtu6; 1091d33e4553SDavid S. Miller rcu_read_unlock(); 1092d33e4553SDavid S. Miller 1093d33e4553SDavid S. Miller return mtu; 1094d33e4553SDavid S. Miller } 1095d33e4553SDavid S. Miller 10963b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list; 10973b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock); 10985d0bbeebSThomas Graf 10993b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 11001da177e4SLinus Torvalds struct neighbour *neigh, 110187a11578SDavid S. Miller struct flowi6 *fl6) 11021da177e4SLinus Torvalds { 110387a11578SDavid S. Miller struct dst_entry *dst; 11041da177e4SLinus Torvalds struct rt6_info *rt; 11051da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 1106c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 11071da177e4SLinus Torvalds 110838308473SDavid S. Miller if (unlikely(!idev)) 1109122bdf67SEric Dumazet return ERR_PTR(-ENODEV); 11101da177e4SLinus Torvalds 1111957c665fSDavid S. Miller rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, dev, 0); 111238308473SDavid S. Miller if (unlikely(!rt)) { 11131da177e4SLinus Torvalds in6_dev_put(idev); 111487a11578SDavid S. Miller dst = ERR_PTR(-ENOMEM); 11151da177e4SLinus Torvalds goto out; 11161da177e4SLinus Torvalds } 11171da177e4SLinus Torvalds 11181da177e4SLinus Torvalds if (neigh) 11191da177e4SLinus Torvalds neigh_hold(neigh); 112014deae41SDavid S. Miller else { 1121f83c7790SDavid S. Miller neigh = ip6_neigh_lookup(&rt->dst, &fl6->daddr); 1122b43faac6SDavid S. Miller if (IS_ERR(neigh)) { 1123252c3d84SRongQing.Li in6_dev_put(idev); 1124b43faac6SDavid S. Miller dst_free(&rt->dst); 1125b43faac6SDavid S. Miller return ERR_CAST(neigh); 1126b43faac6SDavid S. Miller } 112714deae41SDavid S. Miller } 11281da177e4SLinus Torvalds 11298e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 11308e2ec639SYan, Zheng rt->dst.output = ip6_output; 113169cce1d1SDavid S. Miller dst_set_neighbour(&rt->dst, neigh); 1132d8d1f30bSChangli Gao atomic_set(&rt->dst.__refcnt, 1); 113387a11578SDavid S. Miller rt->rt6i_dst.addr = fl6->daddr; 11348e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 11358e2ec639SYan, Zheng rt->rt6i_idev = idev; 11367011687fSGao feng dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255); 11371da177e4SLinus Torvalds 11383b00944cSYOSHIFUJI Hideaki spin_lock_bh(&icmp6_dst_lock); 1139d8d1f30bSChangli Gao rt->dst.next = icmp6_dst_gc_list; 1140d8d1f30bSChangli Gao icmp6_dst_gc_list = &rt->dst; 11413b00944cSYOSHIFUJI Hideaki spin_unlock_bh(&icmp6_dst_lock); 11421da177e4SLinus Torvalds 11435578689aSDaniel Lezcano fib6_force_start_gc(net); 11441da177e4SLinus Torvalds 114587a11578SDavid S. Miller dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); 114687a11578SDavid S. Miller 11471da177e4SLinus Torvalds out: 114887a11578SDavid S. Miller return dst; 11491da177e4SLinus Torvalds } 11501da177e4SLinus Torvalds 11513d0f24a7SStephen Hemminger int icmp6_dst_gc(void) 11521da177e4SLinus Torvalds { 1153e9476e95SHagen Paul Pfeifer struct dst_entry *dst, **pprev; 11543d0f24a7SStephen Hemminger int more = 0; 11551da177e4SLinus Torvalds 11563b00944cSYOSHIFUJI Hideaki spin_lock_bh(&icmp6_dst_lock); 11573b00944cSYOSHIFUJI Hideaki pprev = &icmp6_dst_gc_list; 11585d0bbeebSThomas Graf 11591da177e4SLinus Torvalds while ((dst = *pprev) != NULL) { 11601da177e4SLinus Torvalds if (!atomic_read(&dst->__refcnt)) { 11611da177e4SLinus Torvalds *pprev = dst->next; 11621da177e4SLinus Torvalds dst_free(dst); 11631da177e4SLinus Torvalds } else { 11641da177e4SLinus Torvalds pprev = &dst->next; 11653d0f24a7SStephen Hemminger ++more; 11661da177e4SLinus Torvalds } 11671da177e4SLinus Torvalds } 11681da177e4SLinus Torvalds 11693b00944cSYOSHIFUJI Hideaki spin_unlock_bh(&icmp6_dst_lock); 11705d0bbeebSThomas Graf 11713d0f24a7SStephen Hemminger return more; 11721da177e4SLinus Torvalds } 11731da177e4SLinus Torvalds 11741e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg), 11751e493d19SDavid S. Miller void *arg) 11761e493d19SDavid S. Miller { 11771e493d19SDavid S. Miller struct dst_entry *dst, **pprev; 11781e493d19SDavid S. Miller 11791e493d19SDavid S. Miller spin_lock_bh(&icmp6_dst_lock); 11801e493d19SDavid S. Miller pprev = &icmp6_dst_gc_list; 11811e493d19SDavid S. Miller while ((dst = *pprev) != NULL) { 11821e493d19SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 11831e493d19SDavid S. Miller if (func(rt, arg)) { 11841e493d19SDavid S. Miller *pprev = dst->next; 11851e493d19SDavid S. Miller dst_free(dst); 11861e493d19SDavid S. Miller } else { 11871e493d19SDavid S. Miller pprev = &dst->next; 11881e493d19SDavid S. Miller } 11891e493d19SDavid S. Miller } 11901e493d19SDavid S. Miller spin_unlock_bh(&icmp6_dst_lock); 11911e493d19SDavid S. Miller } 11921e493d19SDavid S. Miller 1193569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops) 11941da177e4SLinus Torvalds { 11951da177e4SLinus Torvalds unsigned long now = jiffies; 119686393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 11977019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 11987019b78eSDaniel Lezcano int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; 11997019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 12007019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 12017019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 1202fc66f95cSEric Dumazet int entries; 12031da177e4SLinus Torvalds 1204fc66f95cSEric Dumazet entries = dst_entries_get_fast(ops); 12057019b78eSDaniel Lezcano if (time_after(rt_last_gc + rt_min_interval, now) && 1206fc66f95cSEric Dumazet entries <= rt_max_size) 12071da177e4SLinus Torvalds goto out; 12081da177e4SLinus Torvalds 12096891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire++; 12106891a346SBenjamin Thery fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net); 12116891a346SBenjamin Thery net->ipv6.ip6_rt_last_gc = now; 1212fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 1213fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 12147019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; 12151da177e4SLinus Torvalds out: 12167019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; 1217fc66f95cSEric Dumazet return entries > rt_max_size; 12181da177e4SLinus Torvalds } 12191da177e4SLinus Torvalds 12201da177e4SLinus Torvalds /* Clean host part of a prefix. Not necessary in radix tree, 12211da177e4SLinus Torvalds but results in cleaner routing tables. 12221da177e4SLinus Torvalds 12231da177e4SLinus Torvalds Remove it only when all the things will work! 12241da177e4SLinus Torvalds */ 12251da177e4SLinus Torvalds 12266b75d090SYOSHIFUJI Hideaki int ip6_dst_hoplimit(struct dst_entry *dst) 12271da177e4SLinus Torvalds { 12285170ae82SDavid S. Miller int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT); 1229a02e4b7dSDavid S. Miller if (hoplimit == 0) { 12306b75d090SYOSHIFUJI Hideaki struct net_device *dev = dst->dev; 1231c68f24ccSEric Dumazet struct inet6_dev *idev; 1232c68f24ccSEric Dumazet 1233c68f24ccSEric Dumazet rcu_read_lock(); 1234c68f24ccSEric Dumazet idev = __in6_dev_get(dev); 1235c68f24ccSEric Dumazet if (idev) 12361da177e4SLinus Torvalds hoplimit = idev->cnf.hop_limit; 1237c68f24ccSEric Dumazet else 123853b7997fSYOSHIFUJI Hideaki hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit; 1239c68f24ccSEric Dumazet rcu_read_unlock(); 12401da177e4SLinus Torvalds } 12411da177e4SLinus Torvalds return hoplimit; 12421da177e4SLinus Torvalds } 1243abbf46aeSDavid S. Miller EXPORT_SYMBOL(ip6_dst_hoplimit); 12441da177e4SLinus Torvalds 12451da177e4SLinus Torvalds /* 12461da177e4SLinus Torvalds * 12471da177e4SLinus Torvalds */ 12481da177e4SLinus Torvalds 124986872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg) 12501da177e4SLinus Torvalds { 12511da177e4SLinus Torvalds int err; 12525578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 12531da177e4SLinus Torvalds struct rt6_info *rt = NULL; 12541da177e4SLinus Torvalds struct net_device *dev = NULL; 12551da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 1256c71099acSThomas Graf struct fib6_table *table; 12571da177e4SLinus Torvalds int addr_type; 12581da177e4SLinus Torvalds 125986872cb5SThomas Graf if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128) 12601da177e4SLinus Torvalds return -EINVAL; 12611da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 126286872cb5SThomas Graf if (cfg->fc_src_len) 12631da177e4SLinus Torvalds return -EINVAL; 12641da177e4SLinus Torvalds #endif 126586872cb5SThomas Graf if (cfg->fc_ifindex) { 12661da177e4SLinus Torvalds err = -ENODEV; 12675578689aSDaniel Lezcano dev = dev_get_by_index(net, cfg->fc_ifindex); 12681da177e4SLinus Torvalds if (!dev) 12691da177e4SLinus Torvalds goto out; 12701da177e4SLinus Torvalds idev = in6_dev_get(dev); 12711da177e4SLinus Torvalds if (!idev) 12721da177e4SLinus Torvalds goto out; 12731da177e4SLinus Torvalds } 12741da177e4SLinus Torvalds 127586872cb5SThomas Graf if (cfg->fc_metric == 0) 127686872cb5SThomas Graf cfg->fc_metric = IP6_RT_PRIO_USER; 12771da177e4SLinus Torvalds 1278c71099acSThomas Graf err = -ENOBUFS; 127938308473SDavid S. Miller if (cfg->fc_nlinfo.nlh && 1280d71314b4SMatti Vaittinen !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { 1281d71314b4SMatti Vaittinen table = fib6_get_table(net, cfg->fc_table); 128238308473SDavid S. Miller if (!table) { 1283f3213831SJoe Perches pr_warn("NLM_F_CREATE should be specified when creating new route\n"); 1284d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 1285d71314b4SMatti Vaittinen } 1286d71314b4SMatti Vaittinen } else { 1287d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 1288d71314b4SMatti Vaittinen } 128938308473SDavid S. Miller 129038308473SDavid S. Miller if (!table) 1291c71099acSThomas Graf goto out; 1292c71099acSThomas Graf 1293957c665fSDavid S. Miller rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, NULL, DST_NOCOUNT); 12941da177e4SLinus Torvalds 129538308473SDavid S. Miller if (!rt) { 12961da177e4SLinus Torvalds err = -ENOMEM; 12971da177e4SLinus Torvalds goto out; 12981da177e4SLinus Torvalds } 12991da177e4SLinus Torvalds 1300d8d1f30bSChangli Gao rt->dst.obsolete = -1; 13011716a961SGao feng 13021716a961SGao feng if (cfg->fc_flags & RTF_EXPIRES) 13031716a961SGao feng rt6_set_expires(rt, jiffies + 13041716a961SGao feng clock_t_to_jiffies(cfg->fc_expires)); 13051716a961SGao feng else 13061716a961SGao feng rt6_clean_expires(rt); 13071da177e4SLinus Torvalds 130886872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 130986872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 131086872cb5SThomas Graf rt->rt6i_protocol = cfg->fc_protocol; 131186872cb5SThomas Graf 131286872cb5SThomas Graf addr_type = ipv6_addr_type(&cfg->fc_dst); 13131da177e4SLinus Torvalds 13141da177e4SLinus Torvalds if (addr_type & IPV6_ADDR_MULTICAST) 1315d8d1f30bSChangli Gao rt->dst.input = ip6_mc_input; 1316ab79ad14SMaciej Żenczykowski else if (cfg->fc_flags & RTF_LOCAL) 1317ab79ad14SMaciej Żenczykowski rt->dst.input = ip6_input; 13181da177e4SLinus Torvalds else 1319d8d1f30bSChangli Gao rt->dst.input = ip6_forward; 13201da177e4SLinus Torvalds 1321d8d1f30bSChangli Gao rt->dst.output = ip6_output; 13221da177e4SLinus Torvalds 132386872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 132486872cb5SThomas Graf rt->rt6i_dst.plen = cfg->fc_dst_len; 13251da177e4SLinus Torvalds if (rt->rt6i_dst.plen == 128) 132611d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 13271da177e4SLinus Torvalds 13288e2ec639SYan, Zheng if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) { 13298e2ec639SYan, Zheng u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL); 13308e2ec639SYan, Zheng if (!metrics) { 13318e2ec639SYan, Zheng err = -ENOMEM; 13328e2ec639SYan, Zheng goto out; 13338e2ec639SYan, Zheng } 13348e2ec639SYan, Zheng dst_init_metrics(&rt->dst, metrics, 0); 13358e2ec639SYan, Zheng } 13361da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 133786872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len); 133886872cb5SThomas Graf rt->rt6i_src.plen = cfg->fc_src_len; 13391da177e4SLinus Torvalds #endif 13401da177e4SLinus Torvalds 134186872cb5SThomas Graf rt->rt6i_metric = cfg->fc_metric; 13421da177e4SLinus Torvalds 13431da177e4SLinus Torvalds /* We cannot add true routes via loopback here, 13441da177e4SLinus Torvalds they would result in kernel looping; promote them to reject routes 13451da177e4SLinus Torvalds */ 134686872cb5SThomas Graf if ((cfg->fc_flags & RTF_REJECT) || 134738308473SDavid S. Miller (dev && (dev->flags & IFF_LOOPBACK) && 134838308473SDavid S. Miller !(addr_type & IPV6_ADDR_LOOPBACK) && 134938308473SDavid S. Miller !(cfg->fc_flags & RTF_LOCAL))) { 13501da177e4SLinus Torvalds /* hold loopback dev/idev if we haven't done so. */ 13515578689aSDaniel Lezcano if (dev != net->loopback_dev) { 13521da177e4SLinus Torvalds if (dev) { 13531da177e4SLinus Torvalds dev_put(dev); 13541da177e4SLinus Torvalds in6_dev_put(idev); 13551da177e4SLinus Torvalds } 13565578689aSDaniel Lezcano dev = net->loopback_dev; 13571da177e4SLinus Torvalds dev_hold(dev); 13581da177e4SLinus Torvalds idev = in6_dev_get(dev); 13591da177e4SLinus Torvalds if (!idev) { 13601da177e4SLinus Torvalds err = -ENODEV; 13611da177e4SLinus Torvalds goto out; 13621da177e4SLinus Torvalds } 13631da177e4SLinus Torvalds } 1364d8d1f30bSChangli Gao rt->dst.output = ip6_pkt_discard_out; 1365d8d1f30bSChangli Gao rt->dst.input = ip6_pkt_discard; 1366d8d1f30bSChangli Gao rt->dst.error = -ENETUNREACH; 13671da177e4SLinus Torvalds rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP; 13681da177e4SLinus Torvalds goto install_route; 13691da177e4SLinus Torvalds } 13701da177e4SLinus Torvalds 137186872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY) { 1372b71d1d42SEric Dumazet const struct in6_addr *gw_addr; 13731da177e4SLinus Torvalds int gwa_type; 13741da177e4SLinus Torvalds 137586872cb5SThomas Graf gw_addr = &cfg->fc_gateway; 13764e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = *gw_addr; 13771da177e4SLinus Torvalds gwa_type = ipv6_addr_type(gw_addr); 13781da177e4SLinus Torvalds 13791da177e4SLinus Torvalds if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) { 13801da177e4SLinus Torvalds struct rt6_info *grt; 13811da177e4SLinus Torvalds 13821da177e4SLinus Torvalds /* IPv6 strictly inhibits using not link-local 13831da177e4SLinus Torvalds addresses as nexthop address. 13841da177e4SLinus Torvalds Otherwise, router will not able to send redirects. 13851da177e4SLinus Torvalds It is very good, but in some (rare!) circumstances 13861da177e4SLinus Torvalds (SIT, PtP, NBMA NOARP links) it is handy to allow 13871da177e4SLinus Torvalds some exceptions. --ANK 13881da177e4SLinus Torvalds */ 13891da177e4SLinus Torvalds err = -EINVAL; 13901da177e4SLinus Torvalds if (!(gwa_type & IPV6_ADDR_UNICAST)) 13911da177e4SLinus Torvalds goto out; 13921da177e4SLinus Torvalds 13935578689aSDaniel Lezcano grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1); 13941da177e4SLinus Torvalds 13951da177e4SLinus Torvalds err = -EHOSTUNREACH; 139638308473SDavid S. Miller if (!grt) 13971da177e4SLinus Torvalds goto out; 13981da177e4SLinus Torvalds if (dev) { 1399d1918542SDavid S. Miller if (dev != grt->dst.dev) { 1400d8d1f30bSChangli Gao dst_release(&grt->dst); 14011da177e4SLinus Torvalds goto out; 14021da177e4SLinus Torvalds } 14031da177e4SLinus Torvalds } else { 1404d1918542SDavid S. Miller dev = grt->dst.dev; 14051da177e4SLinus Torvalds idev = grt->rt6i_idev; 14061da177e4SLinus Torvalds dev_hold(dev); 14071da177e4SLinus Torvalds in6_dev_hold(grt->rt6i_idev); 14081da177e4SLinus Torvalds } 14091da177e4SLinus Torvalds if (!(grt->rt6i_flags & RTF_GATEWAY)) 14101da177e4SLinus Torvalds err = 0; 1411d8d1f30bSChangli Gao dst_release(&grt->dst); 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds if (err) 14141da177e4SLinus Torvalds goto out; 14151da177e4SLinus Torvalds } 14161da177e4SLinus Torvalds err = -EINVAL; 141738308473SDavid S. Miller if (!dev || (dev->flags & IFF_LOOPBACK)) 14181da177e4SLinus Torvalds goto out; 14191da177e4SLinus Torvalds } 14201da177e4SLinus Torvalds 14211da177e4SLinus Torvalds err = -ENODEV; 142238308473SDavid S. Miller if (!dev) 14231da177e4SLinus Torvalds goto out; 14241da177e4SLinus Torvalds 1425c3968a85SDaniel Walter if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 1426c3968a85SDaniel Walter if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 1427c3968a85SDaniel Walter err = -EINVAL; 1428c3968a85SDaniel Walter goto out; 1429c3968a85SDaniel Walter } 14304e3fd7a0SAlexey Dobriyan rt->rt6i_prefsrc.addr = cfg->fc_prefsrc; 1431c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 128; 1432c3968a85SDaniel Walter } else 1433c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 1434c3968a85SDaniel Walter 143586872cb5SThomas Graf if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) { 14368ade06c6SDavid S. Miller err = rt6_bind_neighbour(rt, dev); 1437f83c7790SDavid S. Miller if (err) 14381da177e4SLinus Torvalds goto out; 14391da177e4SLinus Torvalds } 14401da177e4SLinus Torvalds 144186872cb5SThomas Graf rt->rt6i_flags = cfg->fc_flags; 14421da177e4SLinus Torvalds 14431da177e4SLinus Torvalds install_route: 144486872cb5SThomas Graf if (cfg->fc_mx) { 144586872cb5SThomas Graf struct nlattr *nla; 144686872cb5SThomas Graf int remaining; 14471da177e4SLinus Torvalds 144886872cb5SThomas Graf nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { 14498f4c1f9bSThomas Graf int type = nla_type(nla); 145086872cb5SThomas Graf 145186872cb5SThomas Graf if (type) { 145286872cb5SThomas Graf if (type > RTAX_MAX) { 14531da177e4SLinus Torvalds err = -EINVAL; 14541da177e4SLinus Torvalds goto out; 14551da177e4SLinus Torvalds } 145686872cb5SThomas Graf 1457defb3519SDavid S. Miller dst_metric_set(&rt->dst, type, nla_get_u32(nla)); 14581da177e4SLinus Torvalds } 14591da177e4SLinus Torvalds } 14601da177e4SLinus Torvalds } 14611da177e4SLinus Torvalds 1462d8d1f30bSChangli Gao rt->dst.dev = dev; 14631da177e4SLinus Torvalds rt->rt6i_idev = idev; 1464c71099acSThomas Graf rt->rt6i_table = table; 146563152fc0SDaniel Lezcano 1466c346dca1SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = dev_net(dev); 146763152fc0SDaniel Lezcano 146886872cb5SThomas Graf return __ip6_ins_rt(rt, &cfg->fc_nlinfo); 14691da177e4SLinus Torvalds 14701da177e4SLinus Torvalds out: 14711da177e4SLinus Torvalds if (dev) 14721da177e4SLinus Torvalds dev_put(dev); 14731da177e4SLinus Torvalds if (idev) 14741da177e4SLinus Torvalds in6_dev_put(idev); 14751da177e4SLinus Torvalds if (rt) 1476d8d1f30bSChangli Gao dst_free(&rt->dst); 14771da177e4SLinus Torvalds return err; 14781da177e4SLinus Torvalds } 14791da177e4SLinus Torvalds 148086872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info) 14811da177e4SLinus Torvalds { 14821da177e4SLinus Torvalds int err; 1483c71099acSThomas Graf struct fib6_table *table; 1484d1918542SDavid S. Miller struct net *net = dev_net(rt->dst.dev); 14851da177e4SLinus Torvalds 14868ed67789SDaniel Lezcano if (rt == net->ipv6.ip6_null_entry) 14876c813a72SPatrick McHardy return -ENOENT; 14886c813a72SPatrick McHardy 1489c71099acSThomas Graf table = rt->rt6i_table; 1490c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 14911da177e4SLinus Torvalds 149286872cb5SThomas Graf err = fib6_del(rt, info); 1493d8d1f30bSChangli Gao dst_release(&rt->dst); 14941da177e4SLinus Torvalds 1495c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 14961da177e4SLinus Torvalds 14971da177e4SLinus Torvalds return err; 14981da177e4SLinus Torvalds } 14991da177e4SLinus Torvalds 1500e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt) 1501e0a1ad73SThomas Graf { 15024d1169c1SDenis V. Lunev struct nl_info info = { 1503d1918542SDavid S. Miller .nl_net = dev_net(rt->dst.dev), 15044d1169c1SDenis V. Lunev }; 1505528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 1506e0a1ad73SThomas Graf } 1507e0a1ad73SThomas Graf 150886872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg) 15091da177e4SLinus Torvalds { 1510c71099acSThomas Graf struct fib6_table *table; 15111da177e4SLinus Torvalds struct fib6_node *fn; 15121da177e4SLinus Torvalds struct rt6_info *rt; 15131da177e4SLinus Torvalds int err = -ESRCH; 15141da177e4SLinus Torvalds 15155578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 151638308473SDavid S. Miller if (!table) 1517c71099acSThomas Graf return err; 15181da177e4SLinus Torvalds 1519c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 1520c71099acSThomas Graf 1521c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 152286872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 152386872cb5SThomas Graf &cfg->fc_src, cfg->fc_src_len); 15241da177e4SLinus Torvalds 15251da177e4SLinus Torvalds if (fn) { 1526d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 152786872cb5SThomas Graf if (cfg->fc_ifindex && 1528d1918542SDavid S. Miller (!rt->dst.dev || 1529d1918542SDavid S. Miller rt->dst.dev->ifindex != cfg->fc_ifindex)) 15301da177e4SLinus Torvalds continue; 153186872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 153286872cb5SThomas Graf !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 15331da177e4SLinus Torvalds continue; 153486872cb5SThomas Graf if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric) 15351da177e4SLinus Torvalds continue; 1536d8d1f30bSChangli Gao dst_hold(&rt->dst); 1537c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 15381da177e4SLinus Torvalds 153986872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 15401da177e4SLinus Torvalds } 15411da177e4SLinus Torvalds } 1542c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 15431da177e4SLinus Torvalds 15441da177e4SLinus Torvalds return err; 15451da177e4SLinus Torvalds } 15461da177e4SLinus Torvalds 15471da177e4SLinus Torvalds /* 15481da177e4SLinus Torvalds * Handle redirects 15491da177e4SLinus Torvalds */ 1550a6279458SYOSHIFUJI Hideaki struct ip6rd_flowi { 15514c9483b2SDavid S. Miller struct flowi6 fl6; 1552a6279458SYOSHIFUJI Hideaki struct in6_addr gateway; 1553a6279458SYOSHIFUJI Hideaki }; 15541da177e4SLinus Torvalds 15558ed67789SDaniel Lezcano static struct rt6_info *__ip6_route_redirect(struct net *net, 15568ed67789SDaniel Lezcano struct fib6_table *table, 15574c9483b2SDavid S. Miller struct flowi6 *fl6, 1558a6279458SYOSHIFUJI Hideaki int flags) 1559a6279458SYOSHIFUJI Hideaki { 15604c9483b2SDavid S. Miller struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; 1561a6279458SYOSHIFUJI Hideaki struct rt6_info *rt; 1562a6279458SYOSHIFUJI Hideaki struct fib6_node *fn; 1563c71099acSThomas Graf 1564e843b9e1SYOSHIFUJI Hideaki /* 1565e843b9e1SYOSHIFUJI Hideaki * Get the "current" route for this destination and 1566e843b9e1SYOSHIFUJI Hideaki * check if the redirect has come from approriate router. 1567e843b9e1SYOSHIFUJI Hideaki * 1568e843b9e1SYOSHIFUJI Hideaki * RFC 2461 specifies that redirects should only be 1569e843b9e1SYOSHIFUJI Hideaki * accepted if they come from the nexthop to the target. 1570e843b9e1SYOSHIFUJI Hideaki * Due to the way the routes are chosen, this notion 1571e843b9e1SYOSHIFUJI Hideaki * is a bit fuzzy and one might need to check all possible 1572e843b9e1SYOSHIFUJI Hideaki * routes. 1573e843b9e1SYOSHIFUJI Hideaki */ 15741da177e4SLinus Torvalds 1575c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 15764c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1577e843b9e1SYOSHIFUJI Hideaki restart: 1578d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 15791da177e4SLinus Torvalds /* 15801da177e4SLinus Torvalds * Current route is on-link; redirect is always invalid. 15811da177e4SLinus Torvalds * 15821da177e4SLinus Torvalds * Seems, previous statement is not true. It could 15831da177e4SLinus Torvalds * be node, which looks for us as on-link (f.e. proxy ndisc) 15841da177e4SLinus Torvalds * But then router serving it might decide, that we should 15851da177e4SLinus Torvalds * know truth 8)8) --ANK (980726). 15861da177e4SLinus Torvalds */ 1587e843b9e1SYOSHIFUJI Hideaki if (rt6_check_expired(rt)) 1588e843b9e1SYOSHIFUJI Hideaki continue; 15891da177e4SLinus Torvalds if (!(rt->rt6i_flags & RTF_GATEWAY)) 1590e843b9e1SYOSHIFUJI Hideaki continue; 1591d1918542SDavid S. Miller if (fl6->flowi6_oif != rt->dst.dev->ifindex) 1592e843b9e1SYOSHIFUJI Hideaki continue; 1593a6279458SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway)) 1594e843b9e1SYOSHIFUJI Hideaki continue; 1595e843b9e1SYOSHIFUJI Hideaki break; 1596e843b9e1SYOSHIFUJI Hideaki } 1597a6279458SYOSHIFUJI Hideaki 1598cb15d9c2SYOSHIFUJI Hideaki if (!rt) 15998ed67789SDaniel Lezcano rt = net->ipv6.ip6_null_entry; 16004c9483b2SDavid S. Miller BACKTRACK(net, &fl6->saddr); 1601cb15d9c2SYOSHIFUJI Hideaki out: 1602d8d1f30bSChangli Gao dst_hold(&rt->dst); 1603a6279458SYOSHIFUJI Hideaki 1604c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 16051da177e4SLinus Torvalds 1606a6279458SYOSHIFUJI Hideaki return rt; 1607a6279458SYOSHIFUJI Hideaki }; 1608a6279458SYOSHIFUJI Hideaki 1609b71d1d42SEric Dumazet static struct rt6_info *ip6_route_redirect(const struct in6_addr *dest, 1610b71d1d42SEric Dumazet const struct in6_addr *src, 1611b71d1d42SEric Dumazet const struct in6_addr *gateway, 1612a6279458SYOSHIFUJI Hideaki struct net_device *dev) 1613a6279458SYOSHIFUJI Hideaki { 1614adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 1615c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 1616a6279458SYOSHIFUJI Hideaki struct ip6rd_flowi rdfl = { 16174c9483b2SDavid S. Miller .fl6 = { 16184c9483b2SDavid S. Miller .flowi6_oif = dev->ifindex, 16194c9483b2SDavid S. Miller .daddr = *dest, 16204c9483b2SDavid S. Miller .saddr = *src, 1621a6279458SYOSHIFUJI Hideaki }, 1622a6279458SYOSHIFUJI Hideaki }; 1623adaa70bbSThomas Graf 16244e3fd7a0SAlexey Dobriyan rdfl.gateway = *gateway; 162586c36ce4SBrian Haley 1626adaa70bbSThomas Graf if (rt6_need_strict(dest)) 1627adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_IFACE; 1628a6279458SYOSHIFUJI Hideaki 16294c9483b2SDavid S. Miller return (struct rt6_info *)fib6_rule_lookup(net, &rdfl.fl6, 163058f09b78SDaniel Lezcano flags, __ip6_route_redirect); 1631a6279458SYOSHIFUJI Hideaki } 1632a6279458SYOSHIFUJI Hideaki 1633b71d1d42SEric Dumazet void rt6_redirect(const struct in6_addr *dest, const struct in6_addr *src, 1634b71d1d42SEric Dumazet const struct in6_addr *saddr, 1635a6279458SYOSHIFUJI Hideaki struct neighbour *neigh, u8 *lladdr, int on_link) 1636a6279458SYOSHIFUJI Hideaki { 1637a6279458SYOSHIFUJI Hideaki struct rt6_info *rt, *nrt = NULL; 1638a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 1639c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(neigh->dev); 1640a6279458SYOSHIFUJI Hideaki 1641a6279458SYOSHIFUJI Hideaki rt = ip6_route_redirect(dest, src, saddr, neigh->dev); 1642a6279458SYOSHIFUJI Hideaki 16438ed67789SDaniel Lezcano if (rt == net->ipv6.ip6_null_entry) { 1644e87cc472SJoe Perches net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); 1645a6279458SYOSHIFUJI Hideaki goto out; 16461da177e4SLinus Torvalds } 16471da177e4SLinus Torvalds 16481da177e4SLinus Torvalds /* 16491da177e4SLinus Torvalds * We have finally decided to accept it. 16501da177e4SLinus Torvalds */ 16511da177e4SLinus Torvalds 16521da177e4SLinus Torvalds neigh_update(neigh, lladdr, NUD_STALE, 16531da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 16541da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 16551da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 16561da177e4SLinus Torvalds NEIGH_UPDATE_F_ISROUTER)) 16571da177e4SLinus Torvalds ); 16581da177e4SLinus Torvalds 16591da177e4SLinus Torvalds /* 16601da177e4SLinus Torvalds * Redirect received -> path was valid. 16611da177e4SLinus Torvalds * Look, redirects are sent only in response to data packets, 16621da177e4SLinus Torvalds * so that this nexthop apparently is reachable. --ANK 16631da177e4SLinus Torvalds */ 1664d8d1f30bSChangli Gao dst_confirm(&rt->dst); 16651da177e4SLinus Torvalds 16661da177e4SLinus Torvalds /* Duplicate redirect: silently ignore. */ 166727217455SDavid Miller if (neigh == dst_get_neighbour_noref_raw(&rt->dst)) 16681da177e4SLinus Torvalds goto out; 16691da177e4SLinus Torvalds 167021efcfa0SEric Dumazet nrt = ip6_rt_copy(rt, dest); 167138308473SDavid S. Miller if (!nrt) 16721da177e4SLinus Torvalds goto out; 16731da177e4SLinus Torvalds 16741da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 16751da177e4SLinus Torvalds if (on_link) 16761da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 16771da177e4SLinus Torvalds 16784e3fd7a0SAlexey Dobriyan nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 167969cce1d1SDavid S. Miller dst_set_neighbour(&nrt->dst, neigh_clone(neigh)); 16801da177e4SLinus Torvalds 168140e22e8fSThomas Graf if (ip6_ins_rt(nrt)) 16821da177e4SLinus Torvalds goto out; 16831da177e4SLinus Torvalds 1684d8d1f30bSChangli Gao netevent.old = &rt->dst; 1685d8d1f30bSChangli Gao netevent.new = &nrt->dst; 16868d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 16878d71740cSTom Tucker 16881da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) { 1689e0a1ad73SThomas Graf ip6_del_rt(rt); 16901da177e4SLinus Torvalds return; 16911da177e4SLinus Torvalds } 16921da177e4SLinus Torvalds 16931da177e4SLinus Torvalds out: 1694d8d1f30bSChangli Gao dst_release(&rt->dst); 16951da177e4SLinus Torvalds } 16961da177e4SLinus Torvalds 16971da177e4SLinus Torvalds /* 16981da177e4SLinus Torvalds * Handle ICMP "packet too big" messages 16991da177e4SLinus Torvalds * i.e. Path MTU discovery 17001da177e4SLinus Torvalds */ 17011da177e4SLinus Torvalds 1702b71d1d42SEric Dumazet static void rt6_do_pmtu_disc(const struct in6_addr *daddr, const struct in6_addr *saddr, 1703ae878ae2SMaciej Żenczykowski struct net *net, u32 pmtu, int ifindex) 17041da177e4SLinus Torvalds { 17051da177e4SLinus Torvalds struct rt6_info *rt, *nrt; 17061da177e4SLinus Torvalds int allfrag = 0; 1707d3052b55SAndrey Vagin again: 1708ae878ae2SMaciej Żenczykowski rt = rt6_lookup(net, daddr, saddr, ifindex, 0); 170938308473SDavid S. Miller if (!rt) 17101da177e4SLinus Torvalds return; 17111da177e4SLinus Torvalds 1712d3052b55SAndrey Vagin if (rt6_check_expired(rt)) { 1713d3052b55SAndrey Vagin ip6_del_rt(rt); 1714d3052b55SAndrey Vagin goto again; 1715d3052b55SAndrey Vagin } 1716d3052b55SAndrey Vagin 1717d8d1f30bSChangli Gao if (pmtu >= dst_mtu(&rt->dst)) 17181da177e4SLinus Torvalds goto out; 17191da177e4SLinus Torvalds 17201da177e4SLinus Torvalds if (pmtu < IPV6_MIN_MTU) { 17211da177e4SLinus Torvalds /* 17221da177e4SLinus Torvalds * According to RFC2460, PMTU is set to the IPv6 Minimum Link 17231da177e4SLinus Torvalds * MTU (1280) and a fragment header should always be included 17241da177e4SLinus Torvalds * after a node receiving Too Big message reporting PMTU is 17251da177e4SLinus Torvalds * less than the IPv6 Minimum Link MTU. 17261da177e4SLinus Torvalds */ 17271da177e4SLinus Torvalds pmtu = IPV6_MIN_MTU; 17281da177e4SLinus Torvalds allfrag = 1; 17291da177e4SLinus Torvalds } 17301da177e4SLinus Torvalds 17311da177e4SLinus Torvalds /* New mtu received -> path was valid. 17321da177e4SLinus Torvalds They are sent only in response to data packets, 17331da177e4SLinus Torvalds so that this nexthop apparently is reachable. --ANK 17341da177e4SLinus Torvalds */ 1735d8d1f30bSChangli Gao dst_confirm(&rt->dst); 17361da177e4SLinus Torvalds 17371da177e4SLinus Torvalds /* Host route. If it is static, it would be better 17381da177e4SLinus Torvalds not to override it, but add new one, so that 17391da177e4SLinus Torvalds when cache entry will expire old pmtu 17401da177e4SLinus Torvalds would return automatically. 17411da177e4SLinus Torvalds */ 17421da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) { 1743defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_MTU, pmtu); 1744defb3519SDavid S. Miller if (allfrag) { 1745defb3519SDavid S. Miller u32 features = dst_metric(&rt->dst, RTAX_FEATURES); 1746defb3519SDavid S. Miller features |= RTAX_FEATURE_ALLFRAG; 1747defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_FEATURES, features); 1748defb3519SDavid S. Miller } 17491716a961SGao feng rt6_update_expires(rt, net->ipv6.sysctl.ip6_rt_mtu_expires); 17501716a961SGao feng rt->rt6i_flags |= RTF_MODIFIED; 17511da177e4SLinus Torvalds goto out; 17521da177e4SLinus Torvalds } 17531da177e4SLinus Torvalds 17541da177e4SLinus Torvalds /* Network route. 17551da177e4SLinus Torvalds Two cases are possible: 17561da177e4SLinus Torvalds 1. It is connected route. Action: COW 17571da177e4SLinus Torvalds 2. It is gatewayed route or NONEXTHOP route. Action: clone it. 17581da177e4SLinus Torvalds */ 175927217455SDavid Miller if (!dst_get_neighbour_noref_raw(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP)) 1760a1e78363SYOSHIFUJI Hideaki nrt = rt6_alloc_cow(rt, daddr, saddr); 1761d5315b50SYOSHIFUJI Hideaki else 1762d5315b50SYOSHIFUJI Hideaki nrt = rt6_alloc_clone(rt, daddr); 1763a1e78363SYOSHIFUJI Hideaki 1764d5315b50SYOSHIFUJI Hideaki if (nrt) { 1765defb3519SDavid S. Miller dst_metric_set(&nrt->dst, RTAX_MTU, pmtu); 1766defb3519SDavid S. Miller if (allfrag) { 1767defb3519SDavid S. Miller u32 features = dst_metric(&nrt->dst, RTAX_FEATURES); 1768defb3519SDavid S. Miller features |= RTAX_FEATURE_ALLFRAG; 1769defb3519SDavid S. Miller dst_metric_set(&nrt->dst, RTAX_FEATURES, features); 1770defb3519SDavid S. Miller } 1771a1e78363SYOSHIFUJI Hideaki 17721da177e4SLinus Torvalds /* According to RFC 1981, detecting PMTU increase shouldn't be 1773a1e78363SYOSHIFUJI Hideaki * happened within 5 mins, the recommended timer is 10 mins. 1774a1e78363SYOSHIFUJI Hideaki * Here this route expiration time is set to ip6_rt_mtu_expires 1775a1e78363SYOSHIFUJI Hideaki * which is 10 mins. After 10 mins the decreased pmtu is expired 1776a1e78363SYOSHIFUJI Hideaki * and detecting PMTU increase will be automatically happened. 17771da177e4SLinus Torvalds */ 17781716a961SGao feng rt6_update_expires(nrt, net->ipv6.sysctl.ip6_rt_mtu_expires); 17791716a961SGao feng nrt->rt6i_flags |= RTF_DYNAMIC; 178040e22e8fSThomas Graf ip6_ins_rt(nrt); 17811da177e4SLinus Torvalds } 17821da177e4SLinus Torvalds out: 1783d8d1f30bSChangli Gao dst_release(&rt->dst); 17841da177e4SLinus Torvalds } 17851da177e4SLinus Torvalds 1786b71d1d42SEric Dumazet void rt6_pmtu_discovery(const struct in6_addr *daddr, const struct in6_addr *saddr, 1787ae878ae2SMaciej Żenczykowski struct net_device *dev, u32 pmtu) 1788ae878ae2SMaciej Żenczykowski { 1789ae878ae2SMaciej Żenczykowski struct net *net = dev_net(dev); 1790ae878ae2SMaciej Żenczykowski 1791ae878ae2SMaciej Żenczykowski /* 1792ae878ae2SMaciej Żenczykowski * RFC 1981 states that a node "MUST reduce the size of the packets it 1793ae878ae2SMaciej Żenczykowski * is sending along the path" that caused the Packet Too Big message. 1794ae878ae2SMaciej Żenczykowski * Since it's not possible in the general case to determine which 1795ae878ae2SMaciej Żenczykowski * interface was used to send the original packet, we update the MTU 1796ae878ae2SMaciej Żenczykowski * on the interface that will be used to send future packets. We also 1797ae878ae2SMaciej Żenczykowski * update the MTU on the interface that received the Packet Too Big in 1798ae878ae2SMaciej Żenczykowski * case the original packet was forced out that interface with 1799ae878ae2SMaciej Żenczykowski * SO_BINDTODEVICE or similar. This is the next best thing to the 1800ae878ae2SMaciej Żenczykowski * correct behaviour, which would be to update the MTU on all 1801ae878ae2SMaciej Żenczykowski * interfaces. 1802ae878ae2SMaciej Żenczykowski */ 1803ae878ae2SMaciej Żenczykowski rt6_do_pmtu_disc(daddr, saddr, net, pmtu, 0); 1804ae878ae2SMaciej Żenczykowski rt6_do_pmtu_disc(daddr, saddr, net, pmtu, dev->ifindex); 1805ae878ae2SMaciej Żenczykowski } 1806ae878ae2SMaciej Żenczykowski 18071da177e4SLinus Torvalds /* 18081da177e4SLinus Torvalds * Misc support functions 18091da177e4SLinus Torvalds */ 18101da177e4SLinus Torvalds 18111716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort, 181221efcfa0SEric Dumazet const struct in6_addr *dest) 18131da177e4SLinus Torvalds { 1814d1918542SDavid S. Miller struct net *net = dev_net(ort->dst.dev); 18155c1e6aa3SDavid S. Miller struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, 1816957c665fSDavid S. Miller ort->dst.dev, 0); 18171da177e4SLinus Torvalds 18181da177e4SLinus Torvalds if (rt) { 1819d8d1f30bSChangli Gao rt->dst.input = ort->dst.input; 1820d8d1f30bSChangli Gao rt->dst.output = ort->dst.output; 18218e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 18221da177e4SLinus Torvalds 18234e3fd7a0SAlexey Dobriyan rt->rt6i_dst.addr = *dest; 18248e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 1825defb3519SDavid S. Miller dst_copy_metrics(&rt->dst, &ort->dst); 1826d8d1f30bSChangli Gao rt->dst.error = ort->dst.error; 18271da177e4SLinus Torvalds rt->rt6i_idev = ort->rt6i_idev; 18281da177e4SLinus Torvalds if (rt->rt6i_idev) 18291da177e4SLinus Torvalds in6_dev_hold(rt->rt6i_idev); 1830d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 18311da177e4SLinus Torvalds 18324e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 18331716a961SGao feng rt->rt6i_flags = ort->rt6i_flags; 18341716a961SGao feng if ((ort->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) == 18351716a961SGao feng (RTF_DEFAULT | RTF_ADDRCONF)) 18361716a961SGao feng rt6_set_from(rt, ort); 18371716a961SGao feng else 18381716a961SGao feng rt6_clean_expires(rt); 18391da177e4SLinus Torvalds rt->rt6i_metric = 0; 18401da177e4SLinus Torvalds 18411da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 18421da177e4SLinus Torvalds memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 18431da177e4SLinus Torvalds #endif 18440f6c6392SFlorian Westphal memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key)); 1845c71099acSThomas Graf rt->rt6i_table = ort->rt6i_table; 18461da177e4SLinus Torvalds } 18471da177e4SLinus Torvalds return rt; 18481da177e4SLinus Torvalds } 18491da177e4SLinus Torvalds 185070ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 1851efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 1852b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 1853b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex) 185470ceb4f5SYOSHIFUJI Hideaki { 185570ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 185670ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt = NULL; 1857c71099acSThomas Graf struct fib6_table *table; 185870ceb4f5SYOSHIFUJI Hideaki 1859efa2cea0SDaniel Lezcano table = fib6_get_table(net, RT6_TABLE_INFO); 186038308473SDavid S. Miller if (!table) 1861c71099acSThomas Graf return NULL; 1862c71099acSThomas Graf 1863c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 1864c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0); 186570ceb4f5SYOSHIFUJI Hideaki if (!fn) 186670ceb4f5SYOSHIFUJI Hideaki goto out; 186770ceb4f5SYOSHIFUJI Hideaki 1868d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 1869d1918542SDavid S. Miller if (rt->dst.dev->ifindex != ifindex) 187070ceb4f5SYOSHIFUJI Hideaki continue; 187170ceb4f5SYOSHIFUJI Hideaki if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) 187270ceb4f5SYOSHIFUJI Hideaki continue; 187370ceb4f5SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr)) 187470ceb4f5SYOSHIFUJI Hideaki continue; 1875d8d1f30bSChangli Gao dst_hold(&rt->dst); 187670ceb4f5SYOSHIFUJI Hideaki break; 187770ceb4f5SYOSHIFUJI Hideaki } 187870ceb4f5SYOSHIFUJI Hideaki out: 1879c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 188070ceb4f5SYOSHIFUJI Hideaki return rt; 188170ceb4f5SYOSHIFUJI Hideaki } 188270ceb4f5SYOSHIFUJI Hideaki 1883efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 1884b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 1885b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex, 188695c96174SEric Dumazet unsigned int pref) 188770ceb4f5SYOSHIFUJI Hideaki { 188886872cb5SThomas Graf struct fib6_config cfg = { 188986872cb5SThomas Graf .fc_table = RT6_TABLE_INFO, 1890238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 189186872cb5SThomas Graf .fc_ifindex = ifindex, 189286872cb5SThomas Graf .fc_dst_len = prefixlen, 189386872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 189486872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 1895efa2cea0SDaniel Lezcano .fc_nlinfo.pid = 0, 1896efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 1897efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 189886872cb5SThomas Graf }; 189970ceb4f5SYOSHIFUJI Hideaki 19004e3fd7a0SAlexey Dobriyan cfg.fc_dst = *prefix; 19014e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 190286872cb5SThomas Graf 1903e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 1904e317da96SYOSHIFUJI Hideaki if (!prefixlen) 190586872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 190670ceb4f5SYOSHIFUJI Hideaki 190786872cb5SThomas Graf ip6_route_add(&cfg); 190870ceb4f5SYOSHIFUJI Hideaki 1909efa2cea0SDaniel Lezcano return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex); 191070ceb4f5SYOSHIFUJI Hideaki } 191170ceb4f5SYOSHIFUJI Hideaki #endif 191270ceb4f5SYOSHIFUJI Hideaki 1913b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev) 19141da177e4SLinus Torvalds { 19151da177e4SLinus Torvalds struct rt6_info *rt; 1916c71099acSThomas Graf struct fib6_table *table; 19171da177e4SLinus Torvalds 1918c346dca1SYOSHIFUJI Hideaki table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT); 191938308473SDavid S. Miller if (!table) 1920c71099acSThomas Graf return NULL; 19211da177e4SLinus Torvalds 1922c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 1923d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) { 1924d1918542SDavid S. Miller if (dev == rt->dst.dev && 1925045927ffSYOSHIFUJI Hideaki ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 19261da177e4SLinus Torvalds ipv6_addr_equal(&rt->rt6i_gateway, addr)) 19271da177e4SLinus Torvalds break; 19281da177e4SLinus Torvalds } 19291da177e4SLinus Torvalds if (rt) 1930d8d1f30bSChangli Gao dst_hold(&rt->dst); 1931c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 19321da177e4SLinus Torvalds return rt; 19331da177e4SLinus Torvalds } 19341da177e4SLinus Torvalds 1935b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr, 1936ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 1937ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 19381da177e4SLinus Torvalds { 193986872cb5SThomas Graf struct fib6_config cfg = { 194086872cb5SThomas Graf .fc_table = RT6_TABLE_DFLT, 1941238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 194286872cb5SThomas Graf .fc_ifindex = dev->ifindex, 194386872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 194486872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 19455578689aSDaniel Lezcano .fc_nlinfo.pid = 0, 19465578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 1947c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 194886872cb5SThomas Graf }; 19491da177e4SLinus Torvalds 19504e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 19511da177e4SLinus Torvalds 195286872cb5SThomas Graf ip6_route_add(&cfg); 19531da177e4SLinus Torvalds 19541da177e4SLinus Torvalds return rt6_get_dflt_router(gwaddr, dev); 19551da177e4SLinus Torvalds } 19561da177e4SLinus Torvalds 19577b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net) 19581da177e4SLinus Torvalds { 19591da177e4SLinus Torvalds struct rt6_info *rt; 1960c71099acSThomas Graf struct fib6_table *table; 1961c71099acSThomas Graf 1962c71099acSThomas Graf /* NOTE: Keep consistent with rt6_get_dflt_router */ 19637b4da532SDaniel Lezcano table = fib6_get_table(net, RT6_TABLE_DFLT); 196438308473SDavid S. Miller if (!table) 1965c71099acSThomas Graf return; 19661da177e4SLinus Torvalds 19671da177e4SLinus Torvalds restart: 1968c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 1969d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 19701da177e4SLinus Torvalds if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) { 1971d8d1f30bSChangli Gao dst_hold(&rt->dst); 1972c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 1973e0a1ad73SThomas Graf ip6_del_rt(rt); 19741da177e4SLinus Torvalds goto restart; 19751da177e4SLinus Torvalds } 19761da177e4SLinus Torvalds } 1977c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 19781da177e4SLinus Torvalds } 19791da177e4SLinus Torvalds 19805578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 19815578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 198286872cb5SThomas Graf struct fib6_config *cfg) 198386872cb5SThomas Graf { 198486872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 198586872cb5SThomas Graf 198686872cb5SThomas Graf cfg->fc_table = RT6_TABLE_MAIN; 198786872cb5SThomas Graf cfg->fc_ifindex = rtmsg->rtmsg_ifindex; 198886872cb5SThomas Graf cfg->fc_metric = rtmsg->rtmsg_metric; 198986872cb5SThomas Graf cfg->fc_expires = rtmsg->rtmsg_info; 199086872cb5SThomas Graf cfg->fc_dst_len = rtmsg->rtmsg_dst_len; 199186872cb5SThomas Graf cfg->fc_src_len = rtmsg->rtmsg_src_len; 199286872cb5SThomas Graf cfg->fc_flags = rtmsg->rtmsg_flags; 199386872cb5SThomas Graf 19945578689aSDaniel Lezcano cfg->fc_nlinfo.nl_net = net; 1995f1243c2dSBenjamin Thery 19964e3fd7a0SAlexey Dobriyan cfg->fc_dst = rtmsg->rtmsg_dst; 19974e3fd7a0SAlexey Dobriyan cfg->fc_src = rtmsg->rtmsg_src; 19984e3fd7a0SAlexey Dobriyan cfg->fc_gateway = rtmsg->rtmsg_gateway; 199986872cb5SThomas Graf } 200086872cb5SThomas Graf 20015578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) 20021da177e4SLinus Torvalds { 200386872cb5SThomas Graf struct fib6_config cfg; 20041da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 20051da177e4SLinus Torvalds int err; 20061da177e4SLinus Torvalds 20071da177e4SLinus Torvalds switch(cmd) { 20081da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 20091da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 20101da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 20111da177e4SLinus Torvalds return -EPERM; 20121da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 20131da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 20141da177e4SLinus Torvalds if (err) 20151da177e4SLinus Torvalds return -EFAULT; 20161da177e4SLinus Torvalds 20175578689aSDaniel Lezcano rtmsg_to_fib6_config(net, &rtmsg, &cfg); 201886872cb5SThomas Graf 20191da177e4SLinus Torvalds rtnl_lock(); 20201da177e4SLinus Torvalds switch (cmd) { 20211da177e4SLinus Torvalds case SIOCADDRT: 202286872cb5SThomas Graf err = ip6_route_add(&cfg); 20231da177e4SLinus Torvalds break; 20241da177e4SLinus Torvalds case SIOCDELRT: 202586872cb5SThomas Graf err = ip6_route_del(&cfg); 20261da177e4SLinus Torvalds break; 20271da177e4SLinus Torvalds default: 20281da177e4SLinus Torvalds err = -EINVAL; 20291da177e4SLinus Torvalds } 20301da177e4SLinus Torvalds rtnl_unlock(); 20311da177e4SLinus Torvalds 20321da177e4SLinus Torvalds return err; 20333ff50b79SStephen Hemminger } 20341da177e4SLinus Torvalds 20351da177e4SLinus Torvalds return -EINVAL; 20361da177e4SLinus Torvalds } 20371da177e4SLinus Torvalds 20381da177e4SLinus Torvalds /* 20391da177e4SLinus Torvalds * Drop the packet on the floor 20401da177e4SLinus Torvalds */ 20411da177e4SLinus Torvalds 2042d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 20431da177e4SLinus Torvalds { 2044612f09e8SYOSHIFUJI Hideaki int type; 2045adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 2046612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 2047612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 20480660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 204945bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 20503bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 20513bd653c8SDenis V. Lunev IPSTATS_MIB_INADDRERRORS); 2052612f09e8SYOSHIFUJI Hideaki break; 2053612f09e8SYOSHIFUJI Hideaki } 2054612f09e8SYOSHIFUJI Hideaki /* FALLTHROUGH */ 2055612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 20563bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 20573bd653c8SDenis V. Lunev ipstats_mib_noroutes); 2058612f09e8SYOSHIFUJI Hideaki break; 2059612f09e8SYOSHIFUJI Hideaki } 20603ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 20611da177e4SLinus Torvalds kfree_skb(skb); 20621da177e4SLinus Torvalds return 0; 20631da177e4SLinus Torvalds } 20641da177e4SLinus Torvalds 20659ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 20669ce8ade0SThomas Graf { 2067612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 20689ce8ade0SThomas Graf } 20699ce8ade0SThomas Graf 207020380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb) 20711da177e4SLinus Torvalds { 2072adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 2073612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 20741da177e4SLinus Torvalds } 20751da177e4SLinus Torvalds 20766723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES 20776723ab54SDavid S. Miller 20789ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 20799ce8ade0SThomas Graf { 2080612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 20819ce8ade0SThomas Graf } 20829ce8ade0SThomas Graf 20839ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb) 20849ce8ade0SThomas Graf { 2085adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 2086612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 20879ce8ade0SThomas Graf } 20889ce8ade0SThomas Graf 20896723ab54SDavid S. Miller #endif 20906723ab54SDavid S. Miller 20911da177e4SLinus Torvalds /* 20921da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 20931da177e4SLinus Torvalds */ 20941da177e4SLinus Torvalds 20951da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, 20961da177e4SLinus Torvalds const struct in6_addr *addr, 20978f031519SDavid S. Miller bool anycast) 20981da177e4SLinus Torvalds { 2099c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 21005c1e6aa3SDavid S. Miller struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, 2101957c665fSDavid S. Miller net->loopback_dev, 0); 2102f83c7790SDavid S. Miller int err; 21031da177e4SLinus Torvalds 210438308473SDavid S. Miller if (!rt) { 2105f3213831SJoe Perches net_warn_ratelimited("Maximum number of routes reached, consider increasing route/max_size\n"); 21061da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 210740385653SBen Greear } 21081da177e4SLinus Torvalds 21091da177e4SLinus Torvalds in6_dev_hold(idev); 21101da177e4SLinus Torvalds 211111d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 2112d8d1f30bSChangli Gao rt->dst.input = ip6_input; 2113d8d1f30bSChangli Gao rt->dst.output = ip6_output; 21141da177e4SLinus Torvalds rt->rt6i_idev = idev; 2115d8d1f30bSChangli Gao rt->dst.obsolete = -1; 21161da177e4SLinus Torvalds 21171da177e4SLinus Torvalds rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP; 211858c4fb86SYOSHIFUJI Hideaki if (anycast) 211958c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 212058c4fb86SYOSHIFUJI Hideaki else 21211da177e4SLinus Torvalds rt->rt6i_flags |= RTF_LOCAL; 21228ade06c6SDavid S. Miller err = rt6_bind_neighbour(rt, rt->dst.dev); 2123f83c7790SDavid S. Miller if (err) { 2124d8d1f30bSChangli Gao dst_free(&rt->dst); 2125f83c7790SDavid S. Miller return ERR_PTR(err); 21261da177e4SLinus Torvalds } 21271da177e4SLinus Torvalds 21284e3fd7a0SAlexey Dobriyan rt->rt6i_dst.addr = *addr; 21291da177e4SLinus Torvalds rt->rt6i_dst.plen = 128; 21305578689aSDaniel Lezcano rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL); 21311da177e4SLinus Torvalds 2132d8d1f30bSChangli Gao atomic_set(&rt->dst.__refcnt, 1); 21331da177e4SLinus Torvalds 21341da177e4SLinus Torvalds return rt; 21351da177e4SLinus Torvalds } 21361da177e4SLinus Torvalds 2137c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net, 2138c3968a85SDaniel Walter struct rt6_info *rt, 2139b71d1d42SEric Dumazet const struct in6_addr *daddr, 2140c3968a85SDaniel Walter unsigned int prefs, 2141c3968a85SDaniel Walter struct in6_addr *saddr) 2142c3968a85SDaniel Walter { 2143c3968a85SDaniel Walter struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt); 2144c3968a85SDaniel Walter int err = 0; 2145c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) 21464e3fd7a0SAlexey Dobriyan *saddr = rt->rt6i_prefsrc.addr; 2147c3968a85SDaniel Walter else 2148c3968a85SDaniel Walter err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL, 2149c3968a85SDaniel Walter daddr, prefs, saddr); 2150c3968a85SDaniel Walter return err; 2151c3968a85SDaniel Walter } 2152c3968a85SDaniel Walter 2153c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 2154c3968a85SDaniel Walter struct arg_dev_net_ip { 2155c3968a85SDaniel Walter struct net_device *dev; 2156c3968a85SDaniel Walter struct net *net; 2157c3968a85SDaniel Walter struct in6_addr *addr; 2158c3968a85SDaniel Walter }; 2159c3968a85SDaniel Walter 2160c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg) 2161c3968a85SDaniel Walter { 2162c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 2163c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 2164c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 2165c3968a85SDaniel Walter 2166d1918542SDavid S. Miller if (((void *)rt->dst.dev == dev || !dev) && 2167c3968a85SDaniel Walter rt != net->ipv6.ip6_null_entry && 2168c3968a85SDaniel Walter ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) { 2169c3968a85SDaniel Walter /* remove prefsrc entry */ 2170c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 2171c3968a85SDaniel Walter } 2172c3968a85SDaniel Walter return 0; 2173c3968a85SDaniel Walter } 2174c3968a85SDaniel Walter 2175c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 2176c3968a85SDaniel Walter { 2177c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 2178c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 2179c3968a85SDaniel Walter .dev = ifp->idev->dev, 2180c3968a85SDaniel Walter .net = net, 2181c3968a85SDaniel Walter .addr = &ifp->addr, 2182c3968a85SDaniel Walter }; 2183c3968a85SDaniel Walter fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni); 2184c3968a85SDaniel Walter } 2185c3968a85SDaniel Walter 21868ed67789SDaniel Lezcano struct arg_dev_net { 21878ed67789SDaniel Lezcano struct net_device *dev; 21888ed67789SDaniel Lezcano struct net *net; 21898ed67789SDaniel Lezcano }; 21908ed67789SDaniel Lezcano 21911da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg) 21921da177e4SLinus Torvalds { 2193bc3ef660Sstephen hemminger const struct arg_dev_net *adn = arg; 2194bc3ef660Sstephen hemminger const struct net_device *dev = adn->dev; 21958ed67789SDaniel Lezcano 2196d1918542SDavid S. Miller if ((rt->dst.dev == dev || !dev) && 2197c159d30cSDavid S. Miller rt != adn->net->ipv6.ip6_null_entry) 21981da177e4SLinus Torvalds return -1; 2199c159d30cSDavid S. Miller 22001da177e4SLinus Torvalds return 0; 22011da177e4SLinus Torvalds } 22021da177e4SLinus Torvalds 2203f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev) 22041da177e4SLinus Torvalds { 22058ed67789SDaniel Lezcano struct arg_dev_net adn = { 22068ed67789SDaniel Lezcano .dev = dev, 22078ed67789SDaniel Lezcano .net = net, 22088ed67789SDaniel Lezcano }; 22098ed67789SDaniel Lezcano 22108ed67789SDaniel Lezcano fib6_clean_all(net, fib6_ifdown, 0, &adn); 22111e493d19SDavid S. Miller icmp6_clean_all(fib6_ifdown, &adn); 22121da177e4SLinus Torvalds } 22131da177e4SLinus Torvalds 221495c96174SEric Dumazet struct rt6_mtu_change_arg { 22151da177e4SLinus Torvalds struct net_device *dev; 221695c96174SEric Dumazet unsigned int mtu; 22171da177e4SLinus Torvalds }; 22181da177e4SLinus Torvalds 22191da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg) 22201da177e4SLinus Torvalds { 22211da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 22221da177e4SLinus Torvalds struct inet6_dev *idev; 22231da177e4SLinus Torvalds 22241da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 22251da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 22261da177e4SLinus Torvalds We still use this lock to block changes 22271da177e4SLinus Torvalds caused by addrconf/ndisc. 22281da177e4SLinus Torvalds */ 22291da177e4SLinus Torvalds 22301da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 223138308473SDavid S. Miller if (!idev) 22321da177e4SLinus Torvalds return 0; 22331da177e4SLinus Torvalds 22341da177e4SLinus Torvalds /* For administrative MTU increase, there is no way to discover 22351da177e4SLinus Torvalds IPv6 PMTU increase, so PMTU increase should be updated here. 22361da177e4SLinus Torvalds Since RFC 1981 doesn't include administrative MTU increase 22371da177e4SLinus Torvalds update PMTU increase is a MUST. (i.e. jumbo frame) 22381da177e4SLinus Torvalds */ 22391da177e4SLinus Torvalds /* 22401da177e4SLinus Torvalds If new MTU is less than route PMTU, this new MTU will be the 22411da177e4SLinus Torvalds lowest MTU in the path, update the route PMTU to reflect PMTU 22421da177e4SLinus Torvalds decreases; if new MTU is greater than route PMTU, and the 22431da177e4SLinus Torvalds old MTU is the lowest MTU in the path, update the route PMTU 22441da177e4SLinus Torvalds to reflect the increase. In this case if the other nodes' MTU 22451da177e4SLinus Torvalds also have the lowest MTU, TOO BIG MESSAGE will be lead to 22461da177e4SLinus Torvalds PMTU discouvery. 22471da177e4SLinus Torvalds */ 2248d1918542SDavid S. Miller if (rt->dst.dev == arg->dev && 2249d8d1f30bSChangli Gao !dst_metric_locked(&rt->dst, RTAX_MTU) && 2250d8d1f30bSChangli Gao (dst_mtu(&rt->dst) >= arg->mtu || 2251d8d1f30bSChangli Gao (dst_mtu(&rt->dst) < arg->mtu && 2252d8d1f30bSChangli Gao dst_mtu(&rt->dst) == idev->cnf.mtu6))) { 2253defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu); 2254566cfd8fSSimon Arlott } 22551da177e4SLinus Torvalds return 0; 22561da177e4SLinus Torvalds } 22571da177e4SLinus Torvalds 225895c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu) 22591da177e4SLinus Torvalds { 2260c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 2261c71099acSThomas Graf .dev = dev, 2262c71099acSThomas Graf .mtu = mtu, 2263c71099acSThomas Graf }; 22641da177e4SLinus Torvalds 2265c346dca1SYOSHIFUJI Hideaki fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg); 22661da177e4SLinus Torvalds } 22671da177e4SLinus Torvalds 2268ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 22695176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 227086872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 2271ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 227286872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 227386872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 227486872cb5SThomas Graf }; 227586872cb5SThomas Graf 227686872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 227786872cb5SThomas Graf struct fib6_config *cfg) 22781da177e4SLinus Torvalds { 227986872cb5SThomas Graf struct rtmsg *rtm; 228086872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 228186872cb5SThomas Graf int err; 22821da177e4SLinus Torvalds 228386872cb5SThomas Graf err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy); 228486872cb5SThomas Graf if (err < 0) 228586872cb5SThomas Graf goto errout; 22861da177e4SLinus Torvalds 228786872cb5SThomas Graf err = -EINVAL; 228886872cb5SThomas Graf rtm = nlmsg_data(nlh); 228986872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 229086872cb5SThomas Graf 229186872cb5SThomas Graf cfg->fc_table = rtm->rtm_table; 229286872cb5SThomas Graf cfg->fc_dst_len = rtm->rtm_dst_len; 229386872cb5SThomas Graf cfg->fc_src_len = rtm->rtm_src_len; 229486872cb5SThomas Graf cfg->fc_flags = RTF_UP; 229586872cb5SThomas Graf cfg->fc_protocol = rtm->rtm_protocol; 229686872cb5SThomas Graf 229786872cb5SThomas Graf if (rtm->rtm_type == RTN_UNREACHABLE) 229886872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 229986872cb5SThomas Graf 2300ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 2301ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 2302ab79ad14SMaciej Żenczykowski 230386872cb5SThomas Graf cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid; 230486872cb5SThomas Graf cfg->fc_nlinfo.nlh = nlh; 23053b1e0a65SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = sock_net(skb->sk); 230686872cb5SThomas Graf 230786872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 230886872cb5SThomas Graf nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16); 230986872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 23101da177e4SLinus Torvalds } 231186872cb5SThomas Graf 231286872cb5SThomas Graf if (tb[RTA_DST]) { 231386872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 231486872cb5SThomas Graf 231586872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 231686872cb5SThomas Graf goto errout; 231786872cb5SThomas Graf 231886872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 23191da177e4SLinus Torvalds } 232086872cb5SThomas Graf 232186872cb5SThomas Graf if (tb[RTA_SRC]) { 232286872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 232386872cb5SThomas Graf 232486872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 232586872cb5SThomas Graf goto errout; 232686872cb5SThomas Graf 232786872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 23281da177e4SLinus Torvalds } 232986872cb5SThomas Graf 2330c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 2331c3968a85SDaniel Walter nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16); 2332c3968a85SDaniel Walter 233386872cb5SThomas Graf if (tb[RTA_OIF]) 233486872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 233586872cb5SThomas Graf 233686872cb5SThomas Graf if (tb[RTA_PRIORITY]) 233786872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 233886872cb5SThomas Graf 233986872cb5SThomas Graf if (tb[RTA_METRICS]) { 234086872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 234186872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 23421da177e4SLinus Torvalds } 234386872cb5SThomas Graf 234486872cb5SThomas Graf if (tb[RTA_TABLE]) 234586872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 234686872cb5SThomas Graf 234786872cb5SThomas Graf err = 0; 234886872cb5SThomas Graf errout: 234986872cb5SThomas Graf return err; 23501da177e4SLinus Torvalds } 23511da177e4SLinus Torvalds 2352c127ea2cSThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 23531da177e4SLinus Torvalds { 235486872cb5SThomas Graf struct fib6_config cfg; 235586872cb5SThomas Graf int err; 23561da177e4SLinus Torvalds 235786872cb5SThomas Graf err = rtm_to_fib6_config(skb, nlh, &cfg); 235886872cb5SThomas Graf if (err < 0) 235986872cb5SThomas Graf return err; 236086872cb5SThomas Graf 236186872cb5SThomas Graf return ip6_route_del(&cfg); 23621da177e4SLinus Torvalds } 23631da177e4SLinus Torvalds 2364c127ea2cSThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 23651da177e4SLinus Torvalds { 236686872cb5SThomas Graf struct fib6_config cfg; 236786872cb5SThomas Graf int err; 23681da177e4SLinus Torvalds 236986872cb5SThomas Graf err = rtm_to_fib6_config(skb, nlh, &cfg); 237086872cb5SThomas Graf if (err < 0) 237186872cb5SThomas Graf return err; 237286872cb5SThomas Graf 237386872cb5SThomas Graf return ip6_route_add(&cfg); 23741da177e4SLinus Torvalds } 23751da177e4SLinus Torvalds 2376339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void) 2377339bf98fSThomas Graf { 2378339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 2379339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 2380339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 2381339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 2382339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 2383339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 2384339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 2385339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 2386339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 23876a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 2388339bf98fSThomas Graf + nla_total_size(sizeof(struct rta_cacheinfo)); 2389339bf98fSThomas Graf } 2390339bf98fSThomas Graf 2391191cd582SBrian Haley static int rt6_fill_node(struct net *net, 2392191cd582SBrian Haley struct sk_buff *skb, struct rt6_info *rt, 23930d51aa80SJamal Hadi Salim struct in6_addr *dst, struct in6_addr *src, 23940d51aa80SJamal Hadi Salim int iif, int type, u32 pid, u32 seq, 23957bc570c8SYOSHIFUJI Hideaki int prefix, int nowait, unsigned int flags) 23961da177e4SLinus Torvalds { 2397346f870bSDavid S. Miller const struct inet_peer *peer; 23981da177e4SLinus Torvalds struct rtmsg *rtm; 23991da177e4SLinus Torvalds struct nlmsghdr *nlh; 2400e3703b3dSThomas Graf long expires; 24019e762a4aSPatrick McHardy u32 table; 2402f2c31e32SEric Dumazet struct neighbour *n; 2403346f870bSDavid S. Miller u32 ts, tsage; 24041da177e4SLinus Torvalds 24051da177e4SLinus Torvalds if (prefix) { /* user wants prefix routes only */ 24061da177e4SLinus Torvalds if (!(rt->rt6i_flags & RTF_PREFIX_RT)) { 24071da177e4SLinus Torvalds /* success since this is not a prefix route */ 24081da177e4SLinus Torvalds return 1; 24091da177e4SLinus Torvalds } 24101da177e4SLinus Torvalds } 24111da177e4SLinus Torvalds 24122d7202bfSThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags); 241338308473SDavid S. Miller if (!nlh) 241426932566SPatrick McHardy return -EMSGSIZE; 24152d7202bfSThomas Graf 24162d7202bfSThomas Graf rtm = nlmsg_data(nlh); 24171da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 24181da177e4SLinus Torvalds rtm->rtm_dst_len = rt->rt6i_dst.plen; 24191da177e4SLinus Torvalds rtm->rtm_src_len = rt->rt6i_src.plen; 24201da177e4SLinus Torvalds rtm->rtm_tos = 0; 2421c71099acSThomas Graf if (rt->rt6i_table) 24229e762a4aSPatrick McHardy table = rt->rt6i_table->tb6_id; 2423c71099acSThomas Graf else 24249e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 24259e762a4aSPatrick McHardy rtm->rtm_table = table; 2426c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, table)) 2427c78679e8SDavid S. Miller goto nla_put_failure; 24281da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_REJECT) 24291da177e4SLinus Torvalds rtm->rtm_type = RTN_UNREACHABLE; 2430ab79ad14SMaciej Żenczykowski else if (rt->rt6i_flags & RTF_LOCAL) 2431ab79ad14SMaciej Żenczykowski rtm->rtm_type = RTN_LOCAL; 2432d1918542SDavid S. Miller else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK)) 24331da177e4SLinus Torvalds rtm->rtm_type = RTN_LOCAL; 24341da177e4SLinus Torvalds else 24351da177e4SLinus Torvalds rtm->rtm_type = RTN_UNICAST; 24361da177e4SLinus Torvalds rtm->rtm_flags = 0; 24371da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 24381da177e4SLinus Torvalds rtm->rtm_protocol = rt->rt6i_protocol; 24391da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_DYNAMIC) 24401da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_REDIRECT; 24411da177e4SLinus Torvalds else if (rt->rt6i_flags & RTF_ADDRCONF) 24421da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_KERNEL; 24431da177e4SLinus Torvalds else if (rt->rt6i_flags & RTF_DEFAULT) 24441da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_RA; 24451da177e4SLinus Torvalds 24461da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) 24471da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 24481da177e4SLinus Torvalds 24491da177e4SLinus Torvalds if (dst) { 2450c78679e8SDavid S. Miller if (nla_put(skb, RTA_DST, 16, dst)) 2451c78679e8SDavid S. Miller goto nla_put_failure; 24521da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 24531da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 2454c78679e8SDavid S. Miller if (nla_put(skb, RTA_DST, 16, &rt->rt6i_dst.addr)) 2455c78679e8SDavid S. Miller goto nla_put_failure; 24561da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 24571da177e4SLinus Torvalds if (src) { 2458c78679e8SDavid S. Miller if (nla_put(skb, RTA_SRC, 16, src)) 2459c78679e8SDavid S. Miller goto nla_put_failure; 24601da177e4SLinus Torvalds rtm->rtm_src_len = 128; 2461c78679e8SDavid S. Miller } else if (rtm->rtm_src_len && 2462c78679e8SDavid S. Miller nla_put(skb, RTA_SRC, 16, &rt->rt6i_src.addr)) 2463c78679e8SDavid S. Miller goto nla_put_failure; 24641da177e4SLinus Torvalds #endif 24657bc570c8SYOSHIFUJI Hideaki if (iif) { 24667bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 24677bc570c8SYOSHIFUJI Hideaki if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) { 24688229efdaSBenjamin Thery int err = ip6mr_get_route(net, skb, rtm, nowait); 24697bc570c8SYOSHIFUJI Hideaki if (err <= 0) { 24707bc570c8SYOSHIFUJI Hideaki if (!nowait) { 24717bc570c8SYOSHIFUJI Hideaki if (err == 0) 24727bc570c8SYOSHIFUJI Hideaki return 0; 24737bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 24747bc570c8SYOSHIFUJI Hideaki } else { 24757bc570c8SYOSHIFUJI Hideaki if (err == -EMSGSIZE) 24767bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 24777bc570c8SYOSHIFUJI Hideaki } 24787bc570c8SYOSHIFUJI Hideaki } 24797bc570c8SYOSHIFUJI Hideaki } else 24807bc570c8SYOSHIFUJI Hideaki #endif 2481c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_IIF, iif)) 2482c78679e8SDavid S. Miller goto nla_put_failure; 24837bc570c8SYOSHIFUJI Hideaki } else if (dst) { 24841da177e4SLinus Torvalds struct in6_addr saddr_buf; 2485c78679e8SDavid S. Miller if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 && 2486c78679e8SDavid S. Miller nla_put(skb, RTA_PREFSRC, 16, &saddr_buf)) 2487c78679e8SDavid S. Miller goto nla_put_failure; 2488c3968a85SDaniel Walter } 2489c3968a85SDaniel Walter 2490c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) { 2491c3968a85SDaniel Walter struct in6_addr saddr_buf; 24924e3fd7a0SAlexey Dobriyan saddr_buf = rt->rt6i_prefsrc.addr; 2493c78679e8SDavid S. Miller if (nla_put(skb, RTA_PREFSRC, 16, &saddr_buf)) 2494c78679e8SDavid S. Miller goto nla_put_failure; 24951da177e4SLinus Torvalds } 24962d7202bfSThomas Graf 2497defb3519SDavid S. Miller if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0) 24982d7202bfSThomas Graf goto nla_put_failure; 24992d7202bfSThomas Graf 2500f2c31e32SEric Dumazet rcu_read_lock(); 250127217455SDavid Miller n = dst_get_neighbour_noref(&rt->dst); 250294f826b8SEric Dumazet if (n) { 250394f826b8SEric Dumazet if (nla_put(skb, RTA_GATEWAY, 16, &n->primary_key) < 0) { 250494f826b8SEric Dumazet rcu_read_unlock(); 250594f826b8SEric Dumazet goto nla_put_failure; 250694f826b8SEric Dumazet } 250794f826b8SEric Dumazet } 2508f2c31e32SEric Dumazet rcu_read_unlock(); 25092d7202bfSThomas Graf 2510c78679e8SDavid S. Miller if (rt->dst.dev && 2511c78679e8SDavid S. Miller nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex)) 2512c78679e8SDavid S. Miller goto nla_put_failure; 2513c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric)) 2514c78679e8SDavid S. Miller goto nla_put_failure; 251536e3deaeSYOSHIFUJI Hideaki if (!(rt->rt6i_flags & RTF_EXPIRES)) 251636e3deaeSYOSHIFUJI Hideaki expires = 0; 2517d1918542SDavid S. Miller else if (rt->dst.expires - jiffies < INT_MAX) 2518d1918542SDavid S. Miller expires = rt->dst.expires - jiffies; 251936e3deaeSYOSHIFUJI Hideaki else 252036e3deaeSYOSHIFUJI Hideaki expires = INT_MAX; 252169cdf8f9SYOSHIFUJI Hideaki 2522346f870bSDavid S. Miller peer = rt->rt6i_peer; 2523346f870bSDavid S. Miller ts = tsage = 0; 2524346f870bSDavid S. Miller if (peer && peer->tcp_ts_stamp) { 2525346f870bSDavid S. Miller ts = peer->tcp_ts; 2526346f870bSDavid S. Miller tsage = get_seconds() - peer->tcp_ts_stamp; 2527346f870bSDavid S. Miller } 2528346f870bSDavid S. Miller 2529346f870bSDavid S. Miller if (rtnl_put_cacheinfo(skb, &rt->dst, 0, ts, tsage, 2530d8d1f30bSChangli Gao expires, rt->dst.error) < 0) 2531e3703b3dSThomas Graf goto nla_put_failure; 25321da177e4SLinus Torvalds 25332d7202bfSThomas Graf return nlmsg_end(skb, nlh); 25342d7202bfSThomas Graf 25352d7202bfSThomas Graf nla_put_failure: 253626932566SPatrick McHardy nlmsg_cancel(skb, nlh); 253726932566SPatrick McHardy return -EMSGSIZE; 25381da177e4SLinus Torvalds } 25391da177e4SLinus Torvalds 25401b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg) 25411da177e4SLinus Torvalds { 25421da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 25431da177e4SLinus Torvalds int prefix; 25441da177e4SLinus Torvalds 25452d7202bfSThomas Graf if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { 25462d7202bfSThomas Graf struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); 25471da177e4SLinus Torvalds prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0; 25481da177e4SLinus Torvalds } else 25491da177e4SLinus Torvalds prefix = 0; 25501da177e4SLinus Torvalds 2551191cd582SBrian Haley return rt6_fill_node(arg->net, 2552191cd582SBrian Haley arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE, 25531da177e4SLinus Torvalds NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq, 25547bc570c8SYOSHIFUJI Hideaki prefix, 0, NLM_F_MULTI); 25551da177e4SLinus Torvalds } 25561da177e4SLinus Torvalds 2557c127ea2cSThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg) 25581da177e4SLinus Torvalds { 25593b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 2560ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 25611da177e4SLinus Torvalds struct rt6_info *rt; 2562ab364a6fSThomas Graf struct sk_buff *skb; 2563ab364a6fSThomas Graf struct rtmsg *rtm; 25644c9483b2SDavid S. Miller struct flowi6 fl6; 256572331bc0SShmulik Ladkani int err, iif = 0, oif = 0; 2566ab364a6fSThomas Graf 2567ab364a6fSThomas Graf err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy); 2568ab364a6fSThomas Graf if (err < 0) 2569ab364a6fSThomas Graf goto errout; 2570ab364a6fSThomas Graf 2571ab364a6fSThomas Graf err = -EINVAL; 25724c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 2573ab364a6fSThomas Graf 2574ab364a6fSThomas Graf if (tb[RTA_SRC]) { 2575ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 2576ab364a6fSThomas Graf goto errout; 2577ab364a6fSThomas Graf 25784e3fd7a0SAlexey Dobriyan fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 2579ab364a6fSThomas Graf } 2580ab364a6fSThomas Graf 2581ab364a6fSThomas Graf if (tb[RTA_DST]) { 2582ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 2583ab364a6fSThomas Graf goto errout; 2584ab364a6fSThomas Graf 25854e3fd7a0SAlexey Dobriyan fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 2586ab364a6fSThomas Graf } 2587ab364a6fSThomas Graf 2588ab364a6fSThomas Graf if (tb[RTA_IIF]) 2589ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 2590ab364a6fSThomas Graf 2591ab364a6fSThomas Graf if (tb[RTA_OIF]) 259272331bc0SShmulik Ladkani oif = nla_get_u32(tb[RTA_OIF]); 2593ab364a6fSThomas Graf 2594ab364a6fSThomas Graf if (iif) { 2595ab364a6fSThomas Graf struct net_device *dev; 259672331bc0SShmulik Ladkani int flags = 0; 259772331bc0SShmulik Ladkani 25985578689aSDaniel Lezcano dev = __dev_get_by_index(net, iif); 2599ab364a6fSThomas Graf if (!dev) { 2600ab364a6fSThomas Graf err = -ENODEV; 2601ab364a6fSThomas Graf goto errout; 2602ab364a6fSThomas Graf } 260372331bc0SShmulik Ladkani 260472331bc0SShmulik Ladkani fl6.flowi6_iif = iif; 260572331bc0SShmulik Ladkani 260672331bc0SShmulik Ladkani if (!ipv6_addr_any(&fl6.saddr)) 260772331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_HAS_SADDR; 260872331bc0SShmulik Ladkani 260972331bc0SShmulik Ladkani rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6, 261072331bc0SShmulik Ladkani flags); 261172331bc0SShmulik Ladkani } else { 261272331bc0SShmulik Ladkani fl6.flowi6_oif = oif; 261372331bc0SShmulik Ladkani 261472331bc0SShmulik Ladkani rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6); 2615ab364a6fSThomas Graf } 26161da177e4SLinus Torvalds 26171da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 261838308473SDavid S. Miller if (!skb) { 26192173bff5SShmulik Ladkani dst_release(&rt->dst); 2620ab364a6fSThomas Graf err = -ENOBUFS; 2621ab364a6fSThomas Graf goto errout; 2622ab364a6fSThomas Graf } 26231da177e4SLinus Torvalds 26241da177e4SLinus Torvalds /* Reserve room for dummy headers, this skb can pass 26251da177e4SLinus Torvalds through good chunk of routing engine. 26261da177e4SLinus Torvalds */ 2627459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 26281da177e4SLinus Torvalds skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr)); 26291da177e4SLinus Torvalds 2630d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 26311da177e4SLinus Torvalds 26324c9483b2SDavid S. Miller err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif, 26331da177e4SLinus Torvalds RTM_NEWROUTE, NETLINK_CB(in_skb).pid, 26347bc570c8SYOSHIFUJI Hideaki nlh->nlmsg_seq, 0, 0, 0); 26351da177e4SLinus Torvalds if (err < 0) { 2636ab364a6fSThomas Graf kfree_skb(skb); 2637ab364a6fSThomas Graf goto errout; 26381da177e4SLinus Torvalds } 26391da177e4SLinus Torvalds 26405578689aSDaniel Lezcano err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid); 2641ab364a6fSThomas Graf errout: 26421da177e4SLinus Torvalds return err; 26431da177e4SLinus Torvalds } 26441da177e4SLinus Torvalds 264586872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info) 26461da177e4SLinus Torvalds { 26471da177e4SLinus Torvalds struct sk_buff *skb; 26485578689aSDaniel Lezcano struct net *net = info->nl_net; 2649528c4cebSDenis V. Lunev u32 seq; 2650528c4cebSDenis V. Lunev int err; 26510d51aa80SJamal Hadi Salim 2652528c4cebSDenis V. Lunev err = -ENOBUFS; 265338308473SDavid S. Miller seq = info->nlh ? info->nlh->nlmsg_seq : 0; 265486872cb5SThomas Graf 2655339bf98fSThomas Graf skb = nlmsg_new(rt6_nlmsg_size(), gfp_any()); 265638308473SDavid S. Miller if (!skb) 265721713ebcSThomas Graf goto errout; 26581da177e4SLinus Torvalds 2659191cd582SBrian Haley err = rt6_fill_node(net, skb, rt, NULL, NULL, 0, 26607bc570c8SYOSHIFUJI Hideaki event, info->pid, seq, 0, 0, 0); 266126932566SPatrick McHardy if (err < 0) { 266226932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 266326932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 266426932566SPatrick McHardy kfree_skb(skb); 266526932566SPatrick McHardy goto errout; 266626932566SPatrick McHardy } 26671ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE, 26685578689aSDaniel Lezcano info->nlh, gfp_any()); 26691ce85fe4SPablo Neira Ayuso return; 267021713ebcSThomas Graf errout: 267121713ebcSThomas Graf if (err < 0) 26725578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 26731da177e4SLinus Torvalds } 26741da177e4SLinus Torvalds 26758ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 26768ed67789SDaniel Lezcano unsigned long event, void *data) 26778ed67789SDaniel Lezcano { 26788ed67789SDaniel Lezcano struct net_device *dev = (struct net_device *)data; 2679c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 26808ed67789SDaniel Lezcano 26818ed67789SDaniel Lezcano if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) { 2682d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 26838ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 26848ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 2685d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 26868ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 2687d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 26888ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 26898ed67789SDaniel Lezcano #endif 26908ed67789SDaniel Lezcano } 26918ed67789SDaniel Lezcano 26928ed67789SDaniel Lezcano return NOTIFY_OK; 26938ed67789SDaniel Lezcano } 26948ed67789SDaniel Lezcano 26951da177e4SLinus Torvalds /* 26961da177e4SLinus Torvalds * /proc 26971da177e4SLinus Torvalds */ 26981da177e4SLinus Torvalds 26991da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 27001da177e4SLinus Torvalds 27011da177e4SLinus Torvalds struct rt6_proc_arg 27021da177e4SLinus Torvalds { 27031da177e4SLinus Torvalds char *buffer; 27041da177e4SLinus Torvalds int offset; 27051da177e4SLinus Torvalds int length; 27061da177e4SLinus Torvalds int skip; 27071da177e4SLinus Torvalds int len; 27081da177e4SLinus Torvalds }; 27091da177e4SLinus Torvalds 27101da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg) 27111da177e4SLinus Torvalds { 271233120b30SAlexey Dobriyan struct seq_file *m = p_arg; 271369cce1d1SDavid S. Miller struct neighbour *n; 27141da177e4SLinus Torvalds 27154b7a4274SHarvey Harrison seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen); 27161da177e4SLinus Torvalds 27171da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 27184b7a4274SHarvey Harrison seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen); 27191da177e4SLinus Torvalds #else 272033120b30SAlexey Dobriyan seq_puts(m, "00000000000000000000000000000000 00 "); 27211da177e4SLinus Torvalds #endif 2722f2c31e32SEric Dumazet rcu_read_lock(); 272327217455SDavid Miller n = dst_get_neighbour_noref(&rt->dst); 272469cce1d1SDavid S. Miller if (n) { 272569cce1d1SDavid S. Miller seq_printf(m, "%pi6", n->primary_key); 27261da177e4SLinus Torvalds } else { 272733120b30SAlexey Dobriyan seq_puts(m, "00000000000000000000000000000000"); 27281da177e4SLinus Torvalds } 2729f2c31e32SEric Dumazet rcu_read_unlock(); 273033120b30SAlexey Dobriyan seq_printf(m, " %08x %08x %08x %08x %8s\n", 2731d8d1f30bSChangli Gao rt->rt6i_metric, atomic_read(&rt->dst.__refcnt), 2732d8d1f30bSChangli Gao rt->dst.__use, rt->rt6i_flags, 2733d1918542SDavid S. Miller rt->dst.dev ? rt->dst.dev->name : ""); 27341da177e4SLinus Torvalds return 0; 27351da177e4SLinus Torvalds } 27361da177e4SLinus Torvalds 273733120b30SAlexey Dobriyan static int ipv6_route_show(struct seq_file *m, void *v) 27381da177e4SLinus Torvalds { 2739f3db4851SDaniel Lezcano struct net *net = (struct net *)m->private; 274032b293a5SJosh Hunt fib6_clean_all_ro(net, rt6_info_route, 0, m); 274133120b30SAlexey Dobriyan return 0; 27421da177e4SLinus Torvalds } 27431da177e4SLinus Torvalds 274433120b30SAlexey Dobriyan static int ipv6_route_open(struct inode *inode, struct file *file) 274533120b30SAlexey Dobriyan { 2746de05c557SPavel Emelyanov return single_open_net(inode, file, ipv6_route_show); 2747f3db4851SDaniel Lezcano } 2748f3db4851SDaniel Lezcano 274933120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = { 275033120b30SAlexey Dobriyan .owner = THIS_MODULE, 275133120b30SAlexey Dobriyan .open = ipv6_route_open, 275233120b30SAlexey Dobriyan .read = seq_read, 275333120b30SAlexey Dobriyan .llseek = seq_lseek, 2754b6fcbdb4SPavel Emelyanov .release = single_release_net, 275533120b30SAlexey Dobriyan }; 275633120b30SAlexey Dobriyan 27571da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 27581da177e4SLinus Torvalds { 275969ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 27601da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 276169ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 276269ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 276369ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_alloc, 276469ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 276569ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 2766fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 276769ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 27681da177e4SLinus Torvalds 27691da177e4SLinus Torvalds return 0; 27701da177e4SLinus Torvalds } 27711da177e4SLinus Torvalds 27721da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file) 27731da177e4SLinus Torvalds { 2774de05c557SPavel Emelyanov return single_open_net(inode, file, rt6_stats_seq_show); 277569ddb805SDaniel Lezcano } 277669ddb805SDaniel Lezcano 27779a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = { 27781da177e4SLinus Torvalds .owner = THIS_MODULE, 27791da177e4SLinus Torvalds .open = rt6_stats_seq_open, 27801da177e4SLinus Torvalds .read = seq_read, 27811da177e4SLinus Torvalds .llseek = seq_lseek, 2782b6fcbdb4SPavel Emelyanov .release = single_release_net, 27831da177e4SLinus Torvalds }; 27841da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 27851da177e4SLinus Torvalds 27861da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 27871da177e4SLinus Torvalds 27881da177e4SLinus Torvalds static 27898d65af78SAlexey Dobriyan int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write, 27901da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 27911da177e4SLinus Torvalds { 2792c486da34SLucian Adrian Grijincu struct net *net; 2793c486da34SLucian Adrian Grijincu int delay; 2794c486da34SLucian Adrian Grijincu if (!write) 2795c486da34SLucian Adrian Grijincu return -EINVAL; 2796c486da34SLucian Adrian Grijincu 2797c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 2798c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 27998d65af78SAlexey Dobriyan proc_dointvec(ctl, write, buffer, lenp, ppos); 28005b7c931dSDaniel Lezcano fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net); 28011da177e4SLinus Torvalds return 0; 28021da177e4SLinus Torvalds } 28031da177e4SLinus Torvalds 2804760f2d01SDaniel Lezcano ctl_table ipv6_route_table_template[] = { 28051da177e4SLinus Torvalds { 28061da177e4SLinus Torvalds .procname = "flush", 28074990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 28081da177e4SLinus Torvalds .maxlen = sizeof(int), 280989c8b3a1SDave Jones .mode = 0200, 28106d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 28111da177e4SLinus Torvalds }, 28121da177e4SLinus Torvalds { 28131da177e4SLinus Torvalds .procname = "gc_thresh", 28149a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 28151da177e4SLinus Torvalds .maxlen = sizeof(int), 28161da177e4SLinus Torvalds .mode = 0644, 28176d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 28181da177e4SLinus Torvalds }, 28191da177e4SLinus Torvalds { 28201da177e4SLinus Torvalds .procname = "max_size", 28214990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 28221da177e4SLinus Torvalds .maxlen = sizeof(int), 28231da177e4SLinus Torvalds .mode = 0644, 28246d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 28251da177e4SLinus Torvalds }, 28261da177e4SLinus Torvalds { 28271da177e4SLinus Torvalds .procname = "gc_min_interval", 28284990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 28291da177e4SLinus Torvalds .maxlen = sizeof(int), 28301da177e4SLinus Torvalds .mode = 0644, 28316d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 28321da177e4SLinus Torvalds }, 28331da177e4SLinus Torvalds { 28341da177e4SLinus Torvalds .procname = "gc_timeout", 28354990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 28361da177e4SLinus Torvalds .maxlen = sizeof(int), 28371da177e4SLinus Torvalds .mode = 0644, 28386d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 28391da177e4SLinus Torvalds }, 28401da177e4SLinus Torvalds { 28411da177e4SLinus Torvalds .procname = "gc_interval", 28424990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 28431da177e4SLinus Torvalds .maxlen = sizeof(int), 28441da177e4SLinus Torvalds .mode = 0644, 28456d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 28461da177e4SLinus Torvalds }, 28471da177e4SLinus Torvalds { 28481da177e4SLinus Torvalds .procname = "gc_elasticity", 28494990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 28501da177e4SLinus Torvalds .maxlen = sizeof(int), 28511da177e4SLinus Torvalds .mode = 0644, 2852f3d3f616SMin Zhang .proc_handler = proc_dointvec, 28531da177e4SLinus Torvalds }, 28541da177e4SLinus Torvalds { 28551da177e4SLinus Torvalds .procname = "mtu_expires", 28564990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 28571da177e4SLinus Torvalds .maxlen = sizeof(int), 28581da177e4SLinus Torvalds .mode = 0644, 28596d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 28601da177e4SLinus Torvalds }, 28611da177e4SLinus Torvalds { 28621da177e4SLinus Torvalds .procname = "min_adv_mss", 28634990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 28641da177e4SLinus Torvalds .maxlen = sizeof(int), 28651da177e4SLinus Torvalds .mode = 0644, 2866f3d3f616SMin Zhang .proc_handler = proc_dointvec, 28671da177e4SLinus Torvalds }, 28681da177e4SLinus Torvalds { 28691da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 28704990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 28711da177e4SLinus Torvalds .maxlen = sizeof(int), 28721da177e4SLinus Torvalds .mode = 0644, 28736d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 28741da177e4SLinus Torvalds }, 2875f8572d8fSEric W. Biederman { } 28761da177e4SLinus Torvalds }; 28771da177e4SLinus Torvalds 28782c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 2879760f2d01SDaniel Lezcano { 2880760f2d01SDaniel Lezcano struct ctl_table *table; 2881760f2d01SDaniel Lezcano 2882760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 2883760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 2884760f2d01SDaniel Lezcano GFP_KERNEL); 28855ee09105SYOSHIFUJI Hideaki 28865ee09105SYOSHIFUJI Hideaki if (table) { 28875ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 2888c486da34SLucian Adrian Grijincu table[0].extra1 = net; 288986393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 28905ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 28915ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 28925ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 28935ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 28945ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 28955ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 28965ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 28979c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 28985ee09105SYOSHIFUJI Hideaki } 28995ee09105SYOSHIFUJI Hideaki 2900760f2d01SDaniel Lezcano return table; 2901760f2d01SDaniel Lezcano } 29021da177e4SLinus Torvalds #endif 29031da177e4SLinus Torvalds 29042c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 2905cdb18761SDaniel Lezcano { 2906633d424bSPavel Emelyanov int ret = -ENOMEM; 29078ed67789SDaniel Lezcano 290886393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 290986393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 2910f2fc6a54SBenjamin Thery 2911fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 2912fc66f95cSEric Dumazet goto out_ip6_dst_ops; 2913fc66f95cSEric Dumazet 29148ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 29158ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 29168ed67789SDaniel Lezcano GFP_KERNEL); 29178ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 2918fc66f95cSEric Dumazet goto out_ip6_dst_entries; 2919d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.path = 29208ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_null_entry; 2921d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 292262fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 292362fa8a84SDavid S. Miller ip6_template_metrics, true); 29248ed67789SDaniel Lezcano 29258ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 29268ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 29278ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 29288ed67789SDaniel Lezcano GFP_KERNEL); 292968fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 293068fffc67SPeter Zijlstra goto out_ip6_null_entry; 2931d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.path = 29328ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_prohibit_entry; 2933d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 293462fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 293562fa8a84SDavid S. Miller ip6_template_metrics, true); 29368ed67789SDaniel Lezcano 29378ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 29388ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 29398ed67789SDaniel Lezcano GFP_KERNEL); 294068fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 294168fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 2942d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.path = 29438ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_blk_hole_entry; 2944d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 294562fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 294662fa8a84SDavid S. Miller ip6_template_metrics, true); 29478ed67789SDaniel Lezcano #endif 29488ed67789SDaniel Lezcano 2949b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 2950b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 2951b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 2952b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 2953b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 2954b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 2955b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 2956b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 2957b339a47cSPeter Zijlstra 2958cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS 2959cdb18761SDaniel Lezcano proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops); 2960cdb18761SDaniel Lezcano proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops); 2961cdb18761SDaniel Lezcano #endif 29626891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 29636891a346SBenjamin Thery 29648ed67789SDaniel Lezcano ret = 0; 29658ed67789SDaniel Lezcano out: 29668ed67789SDaniel Lezcano return ret; 2967f2fc6a54SBenjamin Thery 296868fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 296968fffc67SPeter Zijlstra out_ip6_prohibit_entry: 297068fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 297168fffc67SPeter Zijlstra out_ip6_null_entry: 297268fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 297368fffc67SPeter Zijlstra #endif 2974fc66f95cSEric Dumazet out_ip6_dst_entries: 2975fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 2976f2fc6a54SBenjamin Thery out_ip6_dst_ops: 2977f2fc6a54SBenjamin Thery goto out; 2978cdb18761SDaniel Lezcano } 2979cdb18761SDaniel Lezcano 29802c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 2981cdb18761SDaniel Lezcano { 2982cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS 2983cdb18761SDaniel Lezcano proc_net_remove(net, "ipv6_route"); 2984cdb18761SDaniel Lezcano proc_net_remove(net, "rt6_stats"); 2985cdb18761SDaniel Lezcano #endif 29868ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 29878ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 29888ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 29898ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 29908ed67789SDaniel Lezcano #endif 299141bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 2992cdb18761SDaniel Lezcano } 2993cdb18761SDaniel Lezcano 2994cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 2995cdb18761SDaniel Lezcano .init = ip6_route_net_init, 2996cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 2997cdb18761SDaniel Lezcano }; 2998cdb18761SDaniel Lezcano 2999*c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net) 3000*c3426b47SDavid S. Miller { 3001*c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 3002*c3426b47SDavid S. Miller 3003*c3426b47SDavid S. Miller if (!bp) 3004*c3426b47SDavid S. Miller return -ENOMEM; 3005*c3426b47SDavid S. Miller inet_peer_base_init(bp); 3006*c3426b47SDavid S. Miller net->ipv6.peers = bp; 3007*c3426b47SDavid S. Miller return 0; 3008*c3426b47SDavid S. Miller } 3009*c3426b47SDavid S. Miller 3010*c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net) 3011*c3426b47SDavid S. Miller { 3012*c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv6.peers; 3013*c3426b47SDavid S. Miller 3014*c3426b47SDavid S. Miller net->ipv6.peers = NULL; 3015*c3426b47SDavid S. Miller __inetpeer_invalidate_tree(bp); 3016*c3426b47SDavid S. Miller kfree(bp); 3017*c3426b47SDavid S. Miller } 3018*c3426b47SDavid S. Miller 3019*c3426b47SDavid S. Miller static __net_initdata struct pernet_operations ipv6_inetpeer_ops = { 3020*c3426b47SDavid S. Miller .init = ipv6_inetpeer_init, 3021*c3426b47SDavid S. Miller .exit = ipv6_inetpeer_exit, 3022*c3426b47SDavid S. Miller }; 3023*c3426b47SDavid S. Miller 30248ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 30258ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 30268ed67789SDaniel Lezcano .priority = 0, 30278ed67789SDaniel Lezcano }; 30288ed67789SDaniel Lezcano 3029433d49c3SDaniel Lezcano int __init ip6_route_init(void) 30301da177e4SLinus Torvalds { 3031433d49c3SDaniel Lezcano int ret; 3032433d49c3SDaniel Lezcano 30339a7ec3a9SDaniel Lezcano ret = -ENOMEM; 30349a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 30359a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 30369a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 30379a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 3038c19a28e1SFernando Carrijo goto out; 303914e50e57SDavid S. Miller 3040fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 30418ed67789SDaniel Lezcano if (ret) 3042bdb3289fSDaniel Lezcano goto out_kmem_cache; 3043bdb3289fSDaniel Lezcano 3044fc66f95cSEric Dumazet ret = register_pernet_subsys(&ip6_route_net_ops); 3045fc66f95cSEric Dumazet if (ret) 3046fc66f95cSEric Dumazet goto out_dst_entries; 3047fc66f95cSEric Dumazet 3048*c3426b47SDavid S. Miller ret = register_pernet_subsys(&ipv6_inetpeer_ops); 3049*c3426b47SDavid S. Miller if (ret) 3050*c3426b47SDavid S. Miller goto out_register_subsys; 3051*c3426b47SDavid S. Miller 30525dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 30535dc121e9SArnaud Ebalard 30548ed67789SDaniel Lezcano /* Registering of the loopback is done before this portion of code, 30558ed67789SDaniel Lezcano * the loopback reference in rt6_info will not be taken, do it 30568ed67789SDaniel Lezcano * manually for init_net */ 3057d8d1f30bSChangli Gao init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 30588ed67789SDaniel Lezcano init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 3059bdb3289fSDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 3060d8d1f30bSChangli Gao init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 30618ed67789SDaniel Lezcano init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 3062d8d1f30bSChangli Gao init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 30638ed67789SDaniel Lezcano init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 3064bdb3289fSDaniel Lezcano #endif 3065433d49c3SDaniel Lezcano ret = fib6_init(); 3066433d49c3SDaniel Lezcano if (ret) 3067*c3426b47SDavid S. Miller goto out_register_inetpeer; 3068433d49c3SDaniel Lezcano 3069433d49c3SDaniel Lezcano ret = xfrm6_init(); 3070433d49c3SDaniel Lezcano if (ret) 3071cdb18761SDaniel Lezcano goto out_fib6_init; 3072c35b7e72SDaniel Lezcano 3073433d49c3SDaniel Lezcano ret = fib6_rules_init(); 3074433d49c3SDaniel Lezcano if (ret) 3075433d49c3SDaniel Lezcano goto xfrm6_init; 30767e5449c2SDaniel Lezcano 3077433d49c3SDaniel Lezcano ret = -ENOBUFS; 3078c7ac8679SGreg Rose if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) || 3079c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) || 3080c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL)) 3081433d49c3SDaniel Lezcano goto fib6_rules_init; 3082433d49c3SDaniel Lezcano 30838ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 3084cdb18761SDaniel Lezcano if (ret) 3085cdb18761SDaniel Lezcano goto fib6_rules_init; 30868ed67789SDaniel Lezcano 3087433d49c3SDaniel Lezcano out: 3088433d49c3SDaniel Lezcano return ret; 3089433d49c3SDaniel Lezcano 3090433d49c3SDaniel Lezcano fib6_rules_init: 3091433d49c3SDaniel Lezcano fib6_rules_cleanup(); 3092433d49c3SDaniel Lezcano xfrm6_init: 3093433d49c3SDaniel Lezcano xfrm6_fini(); 3094433d49c3SDaniel Lezcano out_fib6_init: 3095433d49c3SDaniel Lezcano fib6_gc_cleanup(); 3096*c3426b47SDavid S. Miller out_register_inetpeer: 3097*c3426b47SDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 30988ed67789SDaniel Lezcano out_register_subsys: 30998ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 3100fc66f95cSEric Dumazet out_dst_entries: 3101fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 3102433d49c3SDaniel Lezcano out_kmem_cache: 3103f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 3104433d49c3SDaniel Lezcano goto out; 31051da177e4SLinus Torvalds } 31061da177e4SLinus Torvalds 31071da177e4SLinus Torvalds void ip6_route_cleanup(void) 31081da177e4SLinus Torvalds { 31098ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 3110101367c2SThomas Graf fib6_rules_cleanup(); 31111da177e4SLinus Torvalds xfrm6_fini(); 31121da177e4SLinus Torvalds fib6_gc_cleanup(); 3113*c3426b47SDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 31148ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 311541bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 3116f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 31171da177e4SLinus Torvalds } 3118