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); 816700c270SDavid S. Miller static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 826700c270SDavid S. Miller struct sk_buff *skb, u32 mtu); 836700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, 846700c270SDavid S. Miller struct sk_buff *skb); 851da177e4SLinus Torvalds 8670ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 87efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 88b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 89b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex, 9095c96174SEric Dumazet unsigned int pref); 91efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 92b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 93b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex); 9470ceb4f5SYOSHIFUJI Hideaki #endif 9570ceb4f5SYOSHIFUJI Hideaki 9606582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old) 9706582540SDavid S. Miller { 9806582540SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 9906582540SDavid S. Miller struct inet_peer *peer; 10006582540SDavid S. Miller u32 *p = NULL; 10106582540SDavid S. Miller 1028e2ec639SYan, Zheng if (!(rt->dst.flags & DST_HOST)) 1038e2ec639SYan, Zheng return NULL; 1048e2ec639SYan, Zheng 105fbfe95a4SDavid S. Miller peer = rt6_get_peer_create(rt); 10606582540SDavid S. Miller if (peer) { 10706582540SDavid S. Miller u32 *old_p = __DST_METRICS_PTR(old); 10806582540SDavid S. Miller unsigned long prev, new; 10906582540SDavid S. Miller 11006582540SDavid S. Miller p = peer->metrics; 11106582540SDavid S. Miller if (inet_metrics_new(peer)) 11206582540SDavid S. Miller memcpy(p, old_p, sizeof(u32) * RTAX_MAX); 11306582540SDavid S. Miller 11406582540SDavid S. Miller new = (unsigned long) p; 11506582540SDavid S. Miller prev = cmpxchg(&dst->_metrics, old, new); 11606582540SDavid S. Miller 11706582540SDavid S. Miller if (prev != old) { 11806582540SDavid S. Miller p = __DST_METRICS_PTR(prev); 11906582540SDavid S. Miller if (prev & DST_METRICS_READ_ONLY) 12006582540SDavid S. Miller p = NULL; 12106582540SDavid S. Miller } 12206582540SDavid S. Miller } 12306582540SDavid S. Miller return p; 12406582540SDavid S. Miller } 12506582540SDavid S. Miller 126f894cbf8SDavid S. Miller static inline const void *choose_neigh_daddr(struct rt6_info *rt, 127f894cbf8SDavid S. Miller struct sk_buff *skb, 128f894cbf8SDavid S. Miller const void *daddr) 12939232973SDavid S. Miller { 13039232973SDavid S. Miller struct in6_addr *p = &rt->rt6i_gateway; 13139232973SDavid S. Miller 132a7563f34SDavid S. Miller if (!ipv6_addr_any(p)) 13339232973SDavid S. Miller return (const void *) p; 134f894cbf8SDavid S. Miller else if (skb) 135f894cbf8SDavid S. Miller return &ipv6_hdr(skb)->daddr; 13639232973SDavid S. Miller return daddr; 13739232973SDavid S. Miller } 13839232973SDavid S. Miller 139f894cbf8SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst, 140f894cbf8SDavid S. Miller struct sk_buff *skb, 141f894cbf8SDavid S. Miller const void *daddr) 142d3aaeb38SDavid S. Miller { 14339232973SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 14439232973SDavid S. Miller struct neighbour *n; 14539232973SDavid S. Miller 146f894cbf8SDavid S. Miller daddr = choose_neigh_daddr(rt, skb, daddr); 14739232973SDavid S. Miller n = __ipv6_neigh_lookup(&nd_tbl, dst->dev, daddr); 148f83c7790SDavid S. Miller if (n) 149f83c7790SDavid S. Miller return n; 150f83c7790SDavid S. Miller return neigh_create(&nd_tbl, daddr, dst->dev); 151f83c7790SDavid S. Miller } 152f83c7790SDavid S. Miller 1538ade06c6SDavid S. Miller static int rt6_bind_neighbour(struct rt6_info *rt, struct net_device *dev) 154f83c7790SDavid S. Miller { 1558ade06c6SDavid S. Miller struct neighbour *n = __ipv6_neigh_lookup(&nd_tbl, dev, &rt->rt6i_gateway); 1568ade06c6SDavid S. Miller if (!n) { 1578ade06c6SDavid S. Miller n = neigh_create(&nd_tbl, &rt->rt6i_gateway, dev); 158f83c7790SDavid S. Miller if (IS_ERR(n)) 159f83c7790SDavid S. Miller return PTR_ERR(n); 1608ade06c6SDavid S. Miller } 16197cac082SDavid S. Miller rt->n = n; 162f83c7790SDavid S. Miller 163f83c7790SDavid S. Miller return 0; 164d3aaeb38SDavid S. Miller } 165d3aaeb38SDavid S. Miller 1669a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = { 1671da177e4SLinus Torvalds .family = AF_INET6, 16809640e63SHarvey Harrison .protocol = cpu_to_be16(ETH_P_IPV6), 1691da177e4SLinus Torvalds .gc = ip6_dst_gc, 1701da177e4SLinus Torvalds .gc_thresh = 1024, 1711da177e4SLinus Torvalds .check = ip6_dst_check, 1720dbaee3bSDavid S. Miller .default_advmss = ip6_default_advmss, 173ebb762f2SSteffen Klassert .mtu = ip6_mtu, 17406582540SDavid S. Miller .cow_metrics = ipv6_cow_metrics, 1751da177e4SLinus Torvalds .destroy = ip6_dst_destroy, 1761da177e4SLinus Torvalds .ifdown = ip6_dst_ifdown, 1771da177e4SLinus Torvalds .negative_advice = ip6_negative_advice, 1781da177e4SLinus Torvalds .link_failure = ip6_link_failure, 1791da177e4SLinus Torvalds .update_pmtu = ip6_rt_update_pmtu, 1806e157b6aSDavid S. Miller .redirect = rt6_do_redirect, 1811ac06e03SHerbert Xu .local_out = __ip6_local_out, 182d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 1831da177e4SLinus Torvalds }; 1841da177e4SLinus Torvalds 185ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst) 186ec831ea7SRoland Dreier { 187618f9bc7SSteffen Klassert unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); 188618f9bc7SSteffen Klassert 189618f9bc7SSteffen Klassert return mtu ? : dst->dev->mtu; 190ec831ea7SRoland Dreier } 191ec831ea7SRoland Dreier 1926700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk, 1936700c270SDavid S. Miller struct sk_buff *skb, u32 mtu) 19414e50e57SDavid S. Miller { 19514e50e57SDavid S. Miller } 19614e50e57SDavid S. Miller 1976700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk, 1986700c270SDavid S. Miller struct sk_buff *skb) 199b587ee3bSDavid S. Miller { 200b587ee3bSDavid S. Miller } 201b587ee3bSDavid S. Miller 2020972ddb2SHeld Bernhard static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst, 2030972ddb2SHeld Bernhard unsigned long old) 2040972ddb2SHeld Bernhard { 2050972ddb2SHeld Bernhard return NULL; 2060972ddb2SHeld Bernhard } 2070972ddb2SHeld Bernhard 20814e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = { 20914e50e57SDavid S. Miller .family = AF_INET6, 21009640e63SHarvey Harrison .protocol = cpu_to_be16(ETH_P_IPV6), 21114e50e57SDavid S. Miller .destroy = ip6_dst_destroy, 21214e50e57SDavid S. Miller .check = ip6_dst_check, 213ebb762f2SSteffen Klassert .mtu = ip6_blackhole_mtu, 214214f45c9SEric Dumazet .default_advmss = ip6_default_advmss, 21514e50e57SDavid S. Miller .update_pmtu = ip6_rt_blackhole_update_pmtu, 216b587ee3bSDavid S. Miller .redirect = ip6_rt_blackhole_redirect, 2170972ddb2SHeld Bernhard .cow_metrics = ip6_rt_blackhole_cow_metrics, 218d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 21914e50e57SDavid S. Miller }; 22014e50e57SDavid S. Miller 22162fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = { 22262fa8a84SDavid S. Miller [RTAX_HOPLIMIT - 1] = 255, 22362fa8a84SDavid S. Miller }; 22462fa8a84SDavid S. Miller 225bdb3289fSDaniel Lezcano static struct rt6_info ip6_null_entry_template = { 2261da177e4SLinus Torvalds .dst = { 2271da177e4SLinus Torvalds .__refcnt = ATOMIC_INIT(1), 2281da177e4SLinus Torvalds .__use = 1, 2291da177e4SLinus Torvalds .obsolete = -1, 2301da177e4SLinus Torvalds .error = -ENETUNREACH, 2311da177e4SLinus Torvalds .input = ip6_pkt_discard, 2321da177e4SLinus Torvalds .output = ip6_pkt_discard_out, 2331da177e4SLinus Torvalds }, 2341da177e4SLinus Torvalds .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 2354f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 2361da177e4SLinus Torvalds .rt6i_metric = ~(u32) 0, 2371da177e4SLinus Torvalds .rt6i_ref = ATOMIC_INIT(1), 2381da177e4SLinus Torvalds }; 2391da177e4SLinus Torvalds 240101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES 241101367c2SThomas Graf 2426723ab54SDavid S. Miller static int ip6_pkt_prohibit(struct sk_buff *skb); 2436723ab54SDavid S. Miller static int ip6_pkt_prohibit_out(struct sk_buff *skb); 2446723ab54SDavid S. Miller 245280a34c8SAdrian Bunk static struct rt6_info ip6_prohibit_entry_template = { 246101367c2SThomas Graf .dst = { 247101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 248101367c2SThomas Graf .__use = 1, 249101367c2SThomas Graf .obsolete = -1, 250101367c2SThomas Graf .error = -EACCES, 2519ce8ade0SThomas Graf .input = ip6_pkt_prohibit, 2529ce8ade0SThomas Graf .output = ip6_pkt_prohibit_out, 253101367c2SThomas Graf }, 254101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 2554f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 256101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 257101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 258101367c2SThomas Graf }; 259101367c2SThomas Graf 260bdb3289fSDaniel Lezcano static struct rt6_info ip6_blk_hole_entry_template = { 261101367c2SThomas Graf .dst = { 262101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 263101367c2SThomas Graf .__use = 1, 264101367c2SThomas Graf .obsolete = -1, 265101367c2SThomas Graf .error = -EINVAL, 266352e512cSHerbert Xu .input = dst_discard, 267352e512cSHerbert Xu .output = dst_discard, 268101367c2SThomas Graf }, 269101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 2704f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 271101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 272101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 273101367c2SThomas Graf }; 274101367c2SThomas Graf 275101367c2SThomas Graf #endif 276101367c2SThomas Graf 2771da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */ 27897bab73fSDavid S. Miller static inline struct rt6_info *ip6_dst_alloc(struct net *net, 279957c665fSDavid S. Miller struct net_device *dev, 2808b96d22dSDavid S. Miller int flags, 2818b96d22dSDavid S. Miller struct fib6_table *table) 2821da177e4SLinus Torvalds { 28397bab73fSDavid S. Miller struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev, 284f5b0a874SDavid S. Miller 0, DST_OBSOLETE_NONE, flags); 285cf911662SDavid S. Miller 28697bab73fSDavid S. Miller if (rt) { 2878104891bSSteffen Klassert struct dst_entry *dst = &rt->dst; 2888104891bSSteffen Klassert 2898104891bSSteffen Klassert memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst)); 2908b96d22dSDavid S. Miller rt6_init_peer(rt, table ? &table->tb6_peers : net->ipv6.peers); 29197bab73fSDavid S. Miller } 292cf911662SDavid S. Miller return rt; 2931da177e4SLinus Torvalds } 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst) 2961da177e4SLinus Torvalds { 2971da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 2981da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 2991da177e4SLinus Torvalds 30097cac082SDavid S. Miller if (rt->n) 30197cac082SDavid S. Miller neigh_release(rt->n); 30297cac082SDavid S. Miller 3038e2ec639SYan, Zheng if (!(rt->dst.flags & DST_HOST)) 3048e2ec639SYan, Zheng dst_destroy_metrics_generic(dst); 3058e2ec639SYan, Zheng 30638308473SDavid S. Miller if (idev) { 3071da177e4SLinus Torvalds rt->rt6i_idev = NULL; 3081da177e4SLinus Torvalds in6_dev_put(idev); 3091da177e4SLinus Torvalds } 3101716a961SGao feng 3111716a961SGao feng if (!(rt->rt6i_flags & RTF_EXPIRES) && dst->from) 3121716a961SGao feng dst_release(dst->from); 3131716a961SGao feng 31497bab73fSDavid S. Miller if (rt6_has_peer(rt)) { 31597bab73fSDavid S. Miller struct inet_peer *peer = rt6_peer_ptr(rt); 316b3419363SDavid S. Miller inet_putpeer(peer); 317b3419363SDavid S. Miller } 318b3419363SDavid S. Miller } 319b3419363SDavid S. Miller 3206431cbc2SDavid S. Miller static atomic_t __rt6_peer_genid = ATOMIC_INIT(0); 3216431cbc2SDavid S. Miller 3226431cbc2SDavid S. Miller static u32 rt6_peer_genid(void) 3236431cbc2SDavid S. Miller { 3246431cbc2SDavid S. Miller return atomic_read(&__rt6_peer_genid); 3256431cbc2SDavid S. Miller } 3266431cbc2SDavid S. Miller 327b3419363SDavid S. Miller void rt6_bind_peer(struct rt6_info *rt, int create) 328b3419363SDavid S. Miller { 32997bab73fSDavid S. Miller struct inet_peer_base *base; 330b3419363SDavid S. Miller struct inet_peer *peer; 331b3419363SDavid S. Miller 33297bab73fSDavid S. Miller base = inetpeer_base_ptr(rt->_rt6i_peer); 33397bab73fSDavid S. Miller if (!base) 33497bab73fSDavid S. Miller return; 33597bab73fSDavid S. Miller 33697bab73fSDavid S. Miller peer = inet_getpeer_v6(base, &rt->rt6i_dst.addr, create); 3377b34ca2aSDavid S. Miller if (peer) { 33897bab73fSDavid S. Miller if (!rt6_set_peer(rt, peer)) 339b3419363SDavid S. Miller inet_putpeer(peer); 3406431cbc2SDavid S. Miller else 3416431cbc2SDavid S. Miller rt->rt6i_peer_genid = rt6_peer_genid(); 3421da177e4SLinus Torvalds } 3437b34ca2aSDavid S. Miller } 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 3461da177e4SLinus Torvalds int how) 3471da177e4SLinus Torvalds { 3481da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 3491da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 3505a3e55d6SDenis V. Lunev struct net_device *loopback_dev = 351c346dca1SYOSHIFUJI Hideaki dev_net(dev)->loopback_dev; 3521da177e4SLinus Torvalds 35397cac082SDavid S. Miller if (dev != loopback_dev) { 35497cac082SDavid S. Miller if (idev && idev->dev == dev) { 3555a3e55d6SDenis V. Lunev struct inet6_dev *loopback_idev = 3565a3e55d6SDenis V. Lunev in6_dev_get(loopback_dev); 35738308473SDavid S. Miller if (loopback_idev) { 3581da177e4SLinus Torvalds rt->rt6i_idev = loopback_idev; 3591da177e4SLinus Torvalds in6_dev_put(idev); 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds } 36297cac082SDavid S. Miller if (rt->n && rt->n->dev == dev) { 36397cac082SDavid S. Miller rt->n->dev = loopback_dev; 36497cac082SDavid S. Miller dev_hold(loopback_dev); 36597cac082SDavid S. Miller dev_put(dev); 36697cac082SDavid S. Miller } 36797cac082SDavid S. Miller } 3681da177e4SLinus Torvalds } 3691da177e4SLinus Torvalds 370a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt) 3711da177e4SLinus Torvalds { 3721716a961SGao feng struct rt6_info *ort = NULL; 3731716a961SGao feng 3741716a961SGao feng if (rt->rt6i_flags & RTF_EXPIRES) { 3751716a961SGao feng if (time_after(jiffies, rt->dst.expires)) 376a50feda5SEric Dumazet return true; 3771716a961SGao feng } else if (rt->dst.from) { 3781716a961SGao feng ort = (struct rt6_info *) rt->dst.from; 3791716a961SGao feng return (ort->rt6i_flags & RTF_EXPIRES) && 3801716a961SGao feng time_after(jiffies, ort->dst.expires); 3811716a961SGao feng } 382a50feda5SEric Dumazet return false; 3831da177e4SLinus Torvalds } 3841da177e4SLinus Torvalds 385a50feda5SEric Dumazet static bool rt6_need_strict(const struct in6_addr *daddr) 386c71099acSThomas Graf { 387a02cec21SEric Dumazet return ipv6_addr_type(daddr) & 388a02cec21SEric Dumazet (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK); 389c71099acSThomas Graf } 390c71099acSThomas Graf 3911da177e4SLinus Torvalds /* 392c71099acSThomas Graf * Route lookup. Any table->tb6_lock is implied. 3931da177e4SLinus Torvalds */ 3941da177e4SLinus Torvalds 3958ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net, 3968ed67789SDaniel Lezcano struct rt6_info *rt, 397b71d1d42SEric Dumazet const struct in6_addr *saddr, 3981da177e4SLinus Torvalds int oif, 399d420895eSYOSHIFUJI Hideaki int flags) 4001da177e4SLinus Torvalds { 4011da177e4SLinus Torvalds struct rt6_info *local = NULL; 4021da177e4SLinus Torvalds struct rt6_info *sprt; 4031da177e4SLinus Torvalds 404dd3abc4eSYOSHIFUJI Hideaki if (!oif && ipv6_addr_any(saddr)) 405dd3abc4eSYOSHIFUJI Hideaki goto out; 406dd3abc4eSYOSHIFUJI Hideaki 407d8d1f30bSChangli Gao for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) { 408d1918542SDavid S. Miller struct net_device *dev = sprt->dst.dev; 409dd3abc4eSYOSHIFUJI Hideaki 410dd3abc4eSYOSHIFUJI Hideaki if (oif) { 4111da177e4SLinus Torvalds if (dev->ifindex == oif) 4121da177e4SLinus Torvalds return sprt; 4131da177e4SLinus Torvalds if (dev->flags & IFF_LOOPBACK) { 41438308473SDavid S. Miller if (!sprt->rt6i_idev || 4151da177e4SLinus Torvalds sprt->rt6i_idev->dev->ifindex != oif) { 416d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE && oif) 4171da177e4SLinus Torvalds continue; 4181da177e4SLinus Torvalds if (local && (!oif || 4191da177e4SLinus Torvalds local->rt6i_idev->dev->ifindex == oif)) 4201da177e4SLinus Torvalds continue; 4211da177e4SLinus Torvalds } 4221da177e4SLinus Torvalds local = sprt; 4231da177e4SLinus Torvalds } 424dd3abc4eSYOSHIFUJI Hideaki } else { 425dd3abc4eSYOSHIFUJI Hideaki if (ipv6_chk_addr(net, saddr, dev, 426dd3abc4eSYOSHIFUJI Hideaki flags & RT6_LOOKUP_F_IFACE)) 427dd3abc4eSYOSHIFUJI Hideaki return sprt; 428dd3abc4eSYOSHIFUJI Hideaki } 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds 431dd3abc4eSYOSHIFUJI Hideaki if (oif) { 4321da177e4SLinus Torvalds if (local) 4331da177e4SLinus Torvalds return local; 4341da177e4SLinus Torvalds 435d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE) 4368ed67789SDaniel Lezcano return net->ipv6.ip6_null_entry; 4371da177e4SLinus Torvalds } 438dd3abc4eSYOSHIFUJI Hideaki out: 4391da177e4SLinus Torvalds return rt; 4401da177e4SLinus Torvalds } 4411da177e4SLinus Torvalds 44227097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 44327097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt) 44427097255SYOSHIFUJI Hideaki { 445f2c31e32SEric Dumazet struct neighbour *neigh; 44627097255SYOSHIFUJI Hideaki /* 44727097255SYOSHIFUJI Hideaki * Okay, this does not seem to be appropriate 44827097255SYOSHIFUJI Hideaki * for now, however, we need to check if it 44927097255SYOSHIFUJI Hideaki * is really so; aka Router Reachability Probing. 45027097255SYOSHIFUJI Hideaki * 45127097255SYOSHIFUJI Hideaki * Router Reachability Probe MUST be rate-limited 45227097255SYOSHIFUJI Hideaki * to no more than one per minute. 45327097255SYOSHIFUJI Hideaki */ 454f2c31e32SEric Dumazet rcu_read_lock(); 45597cac082SDavid S. Miller neigh = rt ? rt->n : NULL; 45627097255SYOSHIFUJI Hideaki if (!neigh || (neigh->nud_state & NUD_VALID)) 457f2c31e32SEric Dumazet goto out; 45827097255SYOSHIFUJI Hideaki read_lock_bh(&neigh->lock); 45927097255SYOSHIFUJI Hideaki if (!(neigh->nud_state & NUD_VALID) && 46052e16356SYOSHIFUJI Hideaki time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) { 46127097255SYOSHIFUJI Hideaki struct in6_addr mcaddr; 46227097255SYOSHIFUJI Hideaki struct in6_addr *target; 46327097255SYOSHIFUJI Hideaki 46427097255SYOSHIFUJI Hideaki neigh->updated = jiffies; 46527097255SYOSHIFUJI Hideaki read_unlock_bh(&neigh->lock); 46627097255SYOSHIFUJI Hideaki 46727097255SYOSHIFUJI Hideaki target = (struct in6_addr *)&neigh->primary_key; 46827097255SYOSHIFUJI Hideaki addrconf_addr_solict_mult(target, &mcaddr); 469d1918542SDavid S. Miller ndisc_send_ns(rt->dst.dev, NULL, target, &mcaddr, NULL); 470f2c31e32SEric Dumazet } else { 47127097255SYOSHIFUJI Hideaki read_unlock_bh(&neigh->lock); 47227097255SYOSHIFUJI Hideaki } 473f2c31e32SEric Dumazet out: 474f2c31e32SEric Dumazet rcu_read_unlock(); 475f2c31e32SEric Dumazet } 47627097255SYOSHIFUJI Hideaki #else 47727097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt) 47827097255SYOSHIFUJI Hideaki { 47927097255SYOSHIFUJI Hideaki } 48027097255SYOSHIFUJI Hideaki #endif 48127097255SYOSHIFUJI Hideaki 4821da177e4SLinus Torvalds /* 483554cfb7eSYOSHIFUJI Hideaki * Default Router Selection (RFC 2461 6.3.6) 4841da177e4SLinus Torvalds */ 485b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif) 4861da177e4SLinus Torvalds { 487d1918542SDavid S. Miller struct net_device *dev = rt->dst.dev; 488161980f4SDavid S. Miller if (!oif || dev->ifindex == oif) 489554cfb7eSYOSHIFUJI Hideaki return 2; 490161980f4SDavid S. Miller if ((dev->flags & IFF_LOOPBACK) && 491161980f4SDavid S. Miller rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif) 492161980f4SDavid S. Miller return 1; 493554cfb7eSYOSHIFUJI Hideaki return 0; 4941da177e4SLinus Torvalds } 4951da177e4SLinus Torvalds 496b6f99a21SDave Jones static inline int rt6_check_neigh(struct rt6_info *rt) 4971da177e4SLinus Torvalds { 498f2c31e32SEric Dumazet struct neighbour *neigh; 499398bcbebSYOSHIFUJI Hideaki int m; 500f2c31e32SEric Dumazet 501f2c31e32SEric Dumazet rcu_read_lock(); 50297cac082SDavid S. Miller neigh = rt->n; 5034d0c5911SYOSHIFUJI Hideaki if (rt->rt6i_flags & RTF_NONEXTHOP || 5044d0c5911SYOSHIFUJI Hideaki !(rt->rt6i_flags & RTF_GATEWAY)) 5054d0c5911SYOSHIFUJI Hideaki m = 1; 5064d0c5911SYOSHIFUJI Hideaki else if (neigh) { 5071da177e4SLinus Torvalds read_lock_bh(&neigh->lock); 508554cfb7eSYOSHIFUJI Hideaki if (neigh->nud_state & NUD_VALID) 5094d0c5911SYOSHIFUJI Hideaki m = 2; 510398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 511398bcbebSYOSHIFUJI Hideaki else if (neigh->nud_state & NUD_FAILED) 512398bcbebSYOSHIFUJI Hideaki m = 0; 513398bcbebSYOSHIFUJI Hideaki #endif 514398bcbebSYOSHIFUJI Hideaki else 515ea73ee23SYOSHIFUJI Hideaki m = 1; 5161da177e4SLinus Torvalds read_unlock_bh(&neigh->lock); 517398bcbebSYOSHIFUJI Hideaki } else 518398bcbebSYOSHIFUJI Hideaki m = 0; 519f2c31e32SEric Dumazet rcu_read_unlock(); 520554cfb7eSYOSHIFUJI Hideaki return m; 5211da177e4SLinus Torvalds } 5221da177e4SLinus Torvalds 523554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif, 524554cfb7eSYOSHIFUJI Hideaki int strict) 525554cfb7eSYOSHIFUJI Hideaki { 5264d0c5911SYOSHIFUJI Hideaki int m, n; 5274d0c5911SYOSHIFUJI Hideaki 5284d0c5911SYOSHIFUJI Hideaki m = rt6_check_dev(rt, oif); 52977d16f45SYOSHIFUJI Hideaki if (!m && (strict & RT6_LOOKUP_F_IFACE)) 530554cfb7eSYOSHIFUJI Hideaki return -1; 531ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 532ebacaaa0SYOSHIFUJI Hideaki m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2; 533ebacaaa0SYOSHIFUJI Hideaki #endif 5344d0c5911SYOSHIFUJI Hideaki n = rt6_check_neigh(rt); 535557e92efSYOSHIFUJI Hideaki if (!n && (strict & RT6_LOOKUP_F_REACHABLE)) 536554cfb7eSYOSHIFUJI Hideaki return -1; 537554cfb7eSYOSHIFUJI Hideaki return m; 538554cfb7eSYOSHIFUJI Hideaki } 539554cfb7eSYOSHIFUJI Hideaki 540f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict, 541f11e6659SDavid S. Miller int *mpri, struct rt6_info *match) 542554cfb7eSYOSHIFUJI Hideaki { 543554cfb7eSYOSHIFUJI Hideaki int m; 544554cfb7eSYOSHIFUJI Hideaki 545554cfb7eSYOSHIFUJI Hideaki if (rt6_check_expired(rt)) 546f11e6659SDavid S. Miller goto out; 547554cfb7eSYOSHIFUJI Hideaki 548554cfb7eSYOSHIFUJI Hideaki m = rt6_score_route(rt, oif, strict); 549554cfb7eSYOSHIFUJI Hideaki if (m < 0) 550f11e6659SDavid S. Miller goto out; 551554cfb7eSYOSHIFUJI Hideaki 552f11e6659SDavid S. Miller if (m > *mpri) { 553ea659e07SYOSHIFUJI Hideaki if (strict & RT6_LOOKUP_F_REACHABLE) 55427097255SYOSHIFUJI Hideaki rt6_probe(match); 555f11e6659SDavid S. Miller *mpri = m; 556554cfb7eSYOSHIFUJI Hideaki match = rt; 557ea659e07SYOSHIFUJI Hideaki } else if (strict & RT6_LOOKUP_F_REACHABLE) { 55827097255SYOSHIFUJI Hideaki rt6_probe(rt); 5591da177e4SLinus Torvalds } 560f11e6659SDavid S. Miller 561f11e6659SDavid S. Miller out: 562f11e6659SDavid S. Miller return match; 5631da177e4SLinus Torvalds } 5641da177e4SLinus Torvalds 565f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn, 566f11e6659SDavid S. Miller struct rt6_info *rr_head, 567f11e6659SDavid S. Miller u32 metric, int oif, int strict) 568f11e6659SDavid S. Miller { 569f11e6659SDavid S. Miller struct rt6_info *rt, *match; 570f11e6659SDavid S. Miller int mpri = -1; 571f11e6659SDavid S. Miller 572f11e6659SDavid S. Miller match = NULL; 573f11e6659SDavid S. Miller for (rt = rr_head; rt && rt->rt6i_metric == metric; 574d8d1f30bSChangli Gao rt = rt->dst.rt6_next) 575f11e6659SDavid S. Miller match = find_match(rt, oif, strict, &mpri, match); 576f11e6659SDavid S. Miller for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric; 577d8d1f30bSChangli Gao rt = rt->dst.rt6_next) 578f11e6659SDavid S. Miller match = find_match(rt, oif, strict, &mpri, match); 579f11e6659SDavid S. Miller 580f11e6659SDavid S. Miller return match; 581f11e6659SDavid S. Miller } 582f11e6659SDavid S. Miller 583f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict) 584f11e6659SDavid S. Miller { 585f11e6659SDavid S. Miller struct rt6_info *match, *rt0; 5868ed67789SDaniel Lezcano struct net *net; 587f11e6659SDavid S. Miller 588f11e6659SDavid S. Miller rt0 = fn->rr_ptr; 589f11e6659SDavid S. Miller if (!rt0) 590f11e6659SDavid S. Miller fn->rr_ptr = rt0 = fn->leaf; 591f11e6659SDavid S. Miller 592f11e6659SDavid S. Miller match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict); 593f11e6659SDavid S. Miller 594554cfb7eSYOSHIFUJI Hideaki if (!match && 595f11e6659SDavid S. Miller (strict & RT6_LOOKUP_F_REACHABLE)) { 596d8d1f30bSChangli Gao struct rt6_info *next = rt0->dst.rt6_next; 597f11e6659SDavid S. Miller 598554cfb7eSYOSHIFUJI Hideaki /* no entries matched; do round-robin */ 599f11e6659SDavid S. Miller if (!next || next->rt6i_metric != rt0->rt6i_metric) 600f11e6659SDavid S. Miller next = fn->leaf; 601f11e6659SDavid S. Miller 602f11e6659SDavid S. Miller if (next != rt0) 603f11e6659SDavid S. Miller fn->rr_ptr = next; 604554cfb7eSYOSHIFUJI Hideaki } 605554cfb7eSYOSHIFUJI Hideaki 606d1918542SDavid S. Miller net = dev_net(rt0->dst.dev); 607a02cec21SEric Dumazet return match ? match : net->ipv6.ip6_null_entry; 6081da177e4SLinus Torvalds } 6091da177e4SLinus Torvalds 61070ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 61170ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 612b71d1d42SEric Dumazet const struct in6_addr *gwaddr) 61370ceb4f5SYOSHIFUJI Hideaki { 614c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 61570ceb4f5SYOSHIFUJI Hideaki struct route_info *rinfo = (struct route_info *) opt; 61670ceb4f5SYOSHIFUJI Hideaki struct in6_addr prefix_buf, *prefix; 61770ceb4f5SYOSHIFUJI Hideaki unsigned int pref; 6184bed72e4SYOSHIFUJI Hideaki unsigned long lifetime; 61970ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt; 62070ceb4f5SYOSHIFUJI Hideaki 62170ceb4f5SYOSHIFUJI Hideaki if (len < sizeof(struct route_info)) { 62270ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 62370ceb4f5SYOSHIFUJI Hideaki } 62470ceb4f5SYOSHIFUJI Hideaki 62570ceb4f5SYOSHIFUJI Hideaki /* Sanity check for prefix_len and length */ 62670ceb4f5SYOSHIFUJI Hideaki if (rinfo->length > 3) { 62770ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 62870ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 128) { 62970ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 63070ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 64) { 63170ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 2) { 63270ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 63370ceb4f5SYOSHIFUJI Hideaki } 63470ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 0) { 63570ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 1) { 63670ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 63770ceb4f5SYOSHIFUJI Hideaki } 63870ceb4f5SYOSHIFUJI Hideaki } 63970ceb4f5SYOSHIFUJI Hideaki 64070ceb4f5SYOSHIFUJI Hideaki pref = rinfo->route_pref; 64170ceb4f5SYOSHIFUJI Hideaki if (pref == ICMPV6_ROUTER_PREF_INVALID) 6423933fc95SJens Rosenboom return -EINVAL; 64370ceb4f5SYOSHIFUJI Hideaki 6444bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); 64570ceb4f5SYOSHIFUJI Hideaki 64670ceb4f5SYOSHIFUJI Hideaki if (rinfo->length == 3) 64770ceb4f5SYOSHIFUJI Hideaki prefix = (struct in6_addr *)rinfo->prefix; 64870ceb4f5SYOSHIFUJI Hideaki else { 64970ceb4f5SYOSHIFUJI Hideaki /* this function is safe */ 65070ceb4f5SYOSHIFUJI Hideaki ipv6_addr_prefix(&prefix_buf, 65170ceb4f5SYOSHIFUJI Hideaki (struct in6_addr *)rinfo->prefix, 65270ceb4f5SYOSHIFUJI Hideaki rinfo->prefix_len); 65370ceb4f5SYOSHIFUJI Hideaki prefix = &prefix_buf; 65470ceb4f5SYOSHIFUJI Hideaki } 65570ceb4f5SYOSHIFUJI Hideaki 656efa2cea0SDaniel Lezcano rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr, 657efa2cea0SDaniel Lezcano dev->ifindex); 65870ceb4f5SYOSHIFUJI Hideaki 65970ceb4f5SYOSHIFUJI Hideaki if (rt && !lifetime) { 660e0a1ad73SThomas Graf ip6_del_rt(rt); 66170ceb4f5SYOSHIFUJI Hideaki rt = NULL; 66270ceb4f5SYOSHIFUJI Hideaki } 66370ceb4f5SYOSHIFUJI Hideaki 66470ceb4f5SYOSHIFUJI Hideaki if (!rt && lifetime) 665efa2cea0SDaniel Lezcano rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex, 66670ceb4f5SYOSHIFUJI Hideaki pref); 66770ceb4f5SYOSHIFUJI Hideaki else if (rt) 66870ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags = RTF_ROUTEINFO | 66970ceb4f5SYOSHIFUJI Hideaki (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 67070ceb4f5SYOSHIFUJI Hideaki 67170ceb4f5SYOSHIFUJI Hideaki if (rt) { 6721716a961SGao feng if (!addrconf_finite_timeout(lifetime)) 6731716a961SGao feng rt6_clean_expires(rt); 6741716a961SGao feng else 6751716a961SGao feng rt6_set_expires(rt, jiffies + HZ * lifetime); 6761716a961SGao feng 677d8d1f30bSChangli Gao dst_release(&rt->dst); 67870ceb4f5SYOSHIFUJI Hideaki } 67970ceb4f5SYOSHIFUJI Hideaki return 0; 68070ceb4f5SYOSHIFUJI Hideaki } 68170ceb4f5SYOSHIFUJI Hideaki #endif 68270ceb4f5SYOSHIFUJI Hideaki 6838ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr) \ 684982f56f3SYOSHIFUJI Hideaki do { \ 6858ed67789SDaniel Lezcano if (rt == __net->ipv6.ip6_null_entry) { \ 686982f56f3SYOSHIFUJI Hideaki struct fib6_node *pn; \ 687e0eda7bbSVille Nuorvala while (1) { \ 688982f56f3SYOSHIFUJI Hideaki if (fn->fn_flags & RTN_TL_ROOT) \ 689c71099acSThomas Graf goto out; \ 690982f56f3SYOSHIFUJI Hideaki pn = fn->parent; \ 691982f56f3SYOSHIFUJI Hideaki if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \ 6928bce65b9SKim Nordlund fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \ 693982f56f3SYOSHIFUJI Hideaki else \ 694982f56f3SYOSHIFUJI Hideaki fn = pn; \ 695c71099acSThomas Graf if (fn->fn_flags & RTN_RTINFO) \ 696c71099acSThomas Graf goto restart; \ 697c71099acSThomas Graf } \ 698982f56f3SYOSHIFUJI Hideaki } \ 699982f56f3SYOSHIFUJI Hideaki } while (0) 700c71099acSThomas Graf 7018ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net, 7028ed67789SDaniel Lezcano struct fib6_table *table, 7034c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 7041da177e4SLinus Torvalds { 7051da177e4SLinus Torvalds struct fib6_node *fn; 7061da177e4SLinus Torvalds struct rt6_info *rt; 7071da177e4SLinus Torvalds 708c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 7094c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 710c71099acSThomas Graf restart: 711c71099acSThomas Graf rt = fn->leaf; 7124c9483b2SDavid S. Miller rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags); 7134c9483b2SDavid S. Miller BACKTRACK(net, &fl6->saddr); 714c71099acSThomas Graf out: 715d8d1f30bSChangli Gao dst_use(&rt->dst, jiffies); 716c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 7171da177e4SLinus Torvalds return rt; 718c71099acSThomas Graf 719c71099acSThomas Graf } 720c71099acSThomas Graf 721ea6e574eSFlorian Westphal struct dst_entry * ip6_route_lookup(struct net *net, struct flowi6 *fl6, 722ea6e574eSFlorian Westphal int flags) 723ea6e574eSFlorian Westphal { 724ea6e574eSFlorian Westphal return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup); 725ea6e574eSFlorian Westphal } 726ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup); 727ea6e574eSFlorian Westphal 7289acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, 7299acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *saddr, int oif, int strict) 730c71099acSThomas Graf { 7314c9483b2SDavid S. Miller struct flowi6 fl6 = { 7324c9483b2SDavid S. Miller .flowi6_oif = oif, 7334c9483b2SDavid S. Miller .daddr = *daddr, 734c71099acSThomas Graf }; 735c71099acSThomas Graf struct dst_entry *dst; 73677d16f45SYOSHIFUJI Hideaki int flags = strict ? RT6_LOOKUP_F_IFACE : 0; 737c71099acSThomas Graf 738adaa70bbSThomas Graf if (saddr) { 7394c9483b2SDavid S. Miller memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 740adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 741adaa70bbSThomas Graf } 742adaa70bbSThomas Graf 7434c9483b2SDavid S. Miller dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup); 744c71099acSThomas Graf if (dst->error == 0) 745c71099acSThomas Graf return (struct rt6_info *) dst; 746c71099acSThomas Graf 747c71099acSThomas Graf dst_release(dst); 748c71099acSThomas Graf 7491da177e4SLinus Torvalds return NULL; 7501da177e4SLinus Torvalds } 7511da177e4SLinus Torvalds 7527159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup); 7537159039aSYOSHIFUJI Hideaki 754c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock. 7551da177e4SLinus Torvalds It takes new route entry, the addition fails by any reason the 7561da177e4SLinus Torvalds route is freed. In any case, if caller does not hold it, it may 7571da177e4SLinus Torvalds be destroyed. 7581da177e4SLinus Torvalds */ 7591da177e4SLinus Torvalds 76086872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info) 7611da177e4SLinus Torvalds { 7621da177e4SLinus Torvalds int err; 763c71099acSThomas Graf struct fib6_table *table; 7641da177e4SLinus Torvalds 765c71099acSThomas Graf table = rt->rt6i_table; 766c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 76786872cb5SThomas Graf err = fib6_add(&table->tb6_root, rt, info); 768c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 7691da177e4SLinus Torvalds 7701da177e4SLinus Torvalds return err; 7711da177e4SLinus Torvalds } 7721da177e4SLinus Torvalds 77340e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt) 77440e22e8fSThomas Graf { 7754d1169c1SDenis V. Lunev struct nl_info info = { 776d1918542SDavid S. Miller .nl_net = dev_net(rt->dst.dev), 7774d1169c1SDenis V. Lunev }; 778528c4cebSDenis V. Lunev return __ip6_ins_rt(rt, &info); 77940e22e8fSThomas Graf } 78040e22e8fSThomas Graf 7811716a961SGao feng static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, 78221efcfa0SEric Dumazet const struct in6_addr *daddr, 783b71d1d42SEric Dumazet const struct in6_addr *saddr) 7841da177e4SLinus Torvalds { 7851da177e4SLinus Torvalds struct rt6_info *rt; 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds /* 7881da177e4SLinus Torvalds * Clone the route. 7891da177e4SLinus Torvalds */ 7901da177e4SLinus Torvalds 79121efcfa0SEric Dumazet rt = ip6_rt_copy(ort, daddr); 7921da177e4SLinus Torvalds 7931da177e4SLinus Torvalds if (rt) { 79414deae41SDavid S. Miller int attempts = !in_softirq(); 79514deae41SDavid S. Miller 79658c4fb86SYOSHIFUJI Hideaki if (!(rt->rt6i_flags & RTF_GATEWAY)) { 797bb3c3686SDavid S. Miller if (ort->rt6i_dst.plen != 128 && 79821efcfa0SEric Dumazet ipv6_addr_equal(&ort->rt6i_dst.addr, daddr)) 79958c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 8004e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = *daddr; 80158c4fb86SYOSHIFUJI Hideaki } 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds rt->rt6i_flags |= RTF_CACHE; 8041da177e4SLinus Torvalds 8051da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 8061da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 8074e3fd7a0SAlexey Dobriyan rt->rt6i_src.addr = *saddr; 8081da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 8091da177e4SLinus Torvalds } 8101da177e4SLinus Torvalds #endif 8111da177e4SLinus Torvalds 81214deae41SDavid S. Miller retry: 8138ade06c6SDavid S. Miller if (rt6_bind_neighbour(rt, rt->dst.dev)) { 814d1918542SDavid S. Miller struct net *net = dev_net(rt->dst.dev); 81514deae41SDavid S. Miller int saved_rt_min_interval = 81614deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_min_interval; 81714deae41SDavid S. Miller int saved_rt_elasticity = 81814deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_elasticity; 81914deae41SDavid S. Miller 82014deae41SDavid S. Miller if (attempts-- > 0) { 82114deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_elasticity = 1; 82214deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_min_interval = 0; 82314deae41SDavid S. Miller 82486393e52SAlexey Dobriyan ip6_dst_gc(&net->ipv6.ip6_dst_ops); 82514deae41SDavid S. Miller 82614deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_elasticity = 82714deae41SDavid S. Miller saved_rt_elasticity; 82814deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_min_interval = 82914deae41SDavid S. Miller saved_rt_min_interval; 83014deae41SDavid S. Miller goto retry; 83114deae41SDavid S. Miller } 83214deae41SDavid S. Miller 833f3213831SJoe Perches net_warn_ratelimited("Neighbour table overflow\n"); 834d8d1f30bSChangli Gao dst_free(&rt->dst); 83514deae41SDavid S. Miller return NULL; 83614deae41SDavid S. Miller } 83795a9a5baSYOSHIFUJI Hideaki } 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds return rt; 8401da177e4SLinus Torvalds } 84195a9a5baSYOSHIFUJI Hideaki 84221efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, 84321efcfa0SEric Dumazet const struct in6_addr *daddr) 844299d9939SYOSHIFUJI Hideaki { 84521efcfa0SEric Dumazet struct rt6_info *rt = ip6_rt_copy(ort, daddr); 84621efcfa0SEric Dumazet 847299d9939SYOSHIFUJI Hideaki if (rt) { 848299d9939SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_CACHE; 84997cac082SDavid S. Miller rt->n = neigh_clone(ort->n); 850299d9939SYOSHIFUJI Hideaki } 851299d9939SYOSHIFUJI Hideaki return rt; 852299d9939SYOSHIFUJI Hideaki } 853299d9939SYOSHIFUJI Hideaki 8548ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif, 8554c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 8561da177e4SLinus Torvalds { 8571da177e4SLinus Torvalds struct fib6_node *fn; 858519fbd87SYOSHIFUJI Hideaki struct rt6_info *rt, *nrt; 859c71099acSThomas Graf int strict = 0; 8601da177e4SLinus Torvalds int attempts = 3; 861519fbd87SYOSHIFUJI Hideaki int err; 86253b7997fSYOSHIFUJI Hideaki int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE; 8631da177e4SLinus Torvalds 86477d16f45SYOSHIFUJI Hideaki strict |= flags & RT6_LOOKUP_F_IFACE; 8651da177e4SLinus Torvalds 8661da177e4SLinus Torvalds relookup: 867c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 8681da177e4SLinus Torvalds 8698238dd06SYOSHIFUJI Hideaki restart_2: 8704c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 8711da177e4SLinus Torvalds 8721da177e4SLinus Torvalds restart: 8734acad72dSPavel Emelyanov rt = rt6_select(fn, oif, strict | reachable); 8748ed67789SDaniel Lezcano 8754c9483b2SDavid S. Miller BACKTRACK(net, &fl6->saddr); 8768ed67789SDaniel Lezcano if (rt == net->ipv6.ip6_null_entry || 8778238dd06SYOSHIFUJI Hideaki rt->rt6i_flags & RTF_CACHE) 8781da177e4SLinus Torvalds goto out; 8791da177e4SLinus Torvalds 880d8d1f30bSChangli Gao dst_hold(&rt->dst); 881c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 8821da177e4SLinus Torvalds 88397cac082SDavid S. Miller if (!rt->n && !(rt->rt6i_flags & RTF_NONEXTHOP)) 8844c9483b2SDavid S. Miller nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr); 8857343ff31SDavid S. Miller else if (!(rt->dst.flags & DST_HOST)) 8864c9483b2SDavid S. Miller nrt = rt6_alloc_clone(rt, &fl6->daddr); 8877343ff31SDavid S. Miller else 8887343ff31SDavid S. Miller goto out2; 8891da177e4SLinus Torvalds 890d8d1f30bSChangli Gao dst_release(&rt->dst); 8918ed67789SDaniel Lezcano rt = nrt ? : net->ipv6.ip6_null_entry; 8921da177e4SLinus Torvalds 893d8d1f30bSChangli Gao dst_hold(&rt->dst); 894e40cf353SYOSHIFUJI Hideaki if (nrt) { 89540e22e8fSThomas Graf err = ip6_ins_rt(nrt); 896e40cf353SYOSHIFUJI Hideaki if (!err) 897e40cf353SYOSHIFUJI Hideaki goto out2; 898e40cf353SYOSHIFUJI Hideaki } 899e40cf353SYOSHIFUJI Hideaki 900e40cf353SYOSHIFUJI Hideaki if (--attempts <= 0) 9011da177e4SLinus Torvalds goto out2; 9021da177e4SLinus Torvalds 903519fbd87SYOSHIFUJI Hideaki /* 904c71099acSThomas Graf * Race condition! In the gap, when table->tb6_lock was 905519fbd87SYOSHIFUJI Hideaki * released someone could insert this route. Relookup. 9061da177e4SLinus Torvalds */ 907d8d1f30bSChangli Gao dst_release(&rt->dst); 9081da177e4SLinus Torvalds goto relookup; 909e40cf353SYOSHIFUJI Hideaki 910519fbd87SYOSHIFUJI Hideaki out: 9118238dd06SYOSHIFUJI Hideaki if (reachable) { 9128238dd06SYOSHIFUJI Hideaki reachable = 0; 9138238dd06SYOSHIFUJI Hideaki goto restart_2; 9148238dd06SYOSHIFUJI Hideaki } 915d8d1f30bSChangli Gao dst_hold(&rt->dst); 916c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 9171da177e4SLinus Torvalds out2: 918d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 919d8d1f30bSChangli Gao rt->dst.__use++; 920c71099acSThomas Graf 921c71099acSThomas Graf return rt; 922c71099acSThomas Graf } 923c71099acSThomas Graf 9248ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table, 9254c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 9264acad72dSPavel Emelyanov { 9274c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags); 9284acad72dSPavel Emelyanov } 9294acad72dSPavel Emelyanov 93072331bc0SShmulik Ladkani static struct dst_entry *ip6_route_input_lookup(struct net *net, 93172331bc0SShmulik Ladkani struct net_device *dev, 93272331bc0SShmulik Ladkani struct flowi6 *fl6, int flags) 93372331bc0SShmulik Ladkani { 93472331bc0SShmulik Ladkani if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) 93572331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_IFACE; 93672331bc0SShmulik Ladkani 93772331bc0SShmulik Ladkani return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input); 93872331bc0SShmulik Ladkani } 93972331bc0SShmulik Ladkani 940c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 941c71099acSThomas Graf { 942b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 943c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 944adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 9454c9483b2SDavid S. Miller struct flowi6 fl6 = { 9464c9483b2SDavid S. Miller .flowi6_iif = skb->dev->ifindex, 9474c9483b2SDavid S. Miller .daddr = iph->daddr, 9484c9483b2SDavid S. Miller .saddr = iph->saddr, 9494c9483b2SDavid S. Miller .flowlabel = (* (__be32 *) iph) & IPV6_FLOWINFO_MASK, 9504c9483b2SDavid S. Miller .flowi6_mark = skb->mark, 9514c9483b2SDavid S. Miller .flowi6_proto = iph->nexthdr, 952c71099acSThomas Graf }; 953adaa70bbSThomas Graf 95472331bc0SShmulik Ladkani skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags)); 955c71099acSThomas Graf } 956c71099acSThomas Graf 9578ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table, 9584c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 959c71099acSThomas Graf { 9604c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags); 961c71099acSThomas Graf } 962c71099acSThomas Graf 9639c7a4f9cSFlorian Westphal struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk, 9644c9483b2SDavid S. Miller struct flowi6 *fl6) 965c71099acSThomas Graf { 966c71099acSThomas Graf int flags = 0; 967c71099acSThomas Graf 9681fb9489bSPavel Emelyanov fl6->flowi6_iif = LOOPBACK_IFINDEX; 9694dc27d1cSDavid McCullough 9704c9483b2SDavid S. Miller if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr)) 97177d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 972c71099acSThomas Graf 9734c9483b2SDavid S. Miller if (!ipv6_addr_any(&fl6->saddr)) 974adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 9750c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 9760c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 977adaa70bbSThomas Graf 9784c9483b2SDavid S. Miller return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output); 9791da177e4SLinus Torvalds } 9801da177e4SLinus Torvalds 9817159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output); 9821da177e4SLinus Torvalds 9832774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 98414e50e57SDavid S. Miller { 9855c1e6aa3SDavid S. Miller struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; 98614e50e57SDavid S. Miller struct dst_entry *new = NULL; 98714e50e57SDavid S. Miller 988f5b0a874SDavid S. Miller rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, DST_OBSOLETE_NONE, 0); 98914e50e57SDavid S. Miller if (rt) { 990d8d1f30bSChangli Gao new = &rt->dst; 99114e50e57SDavid S. Miller 9928104891bSSteffen Klassert memset(new + 1, 0, sizeof(*rt) - sizeof(*new)); 9938104891bSSteffen Klassert rt6_init_peer(rt, net->ipv6.peers); 9948104891bSSteffen Klassert 99514e50e57SDavid S. Miller new->__use = 1; 996352e512cSHerbert Xu new->input = dst_discard; 997352e512cSHerbert Xu new->output = dst_discard; 99814e50e57SDavid S. Miller 99921efcfa0SEric Dumazet if (dst_metrics_read_only(&ort->dst)) 100021efcfa0SEric Dumazet new->_metrics = ort->dst._metrics; 100121efcfa0SEric Dumazet else 1002defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 100314e50e57SDavid S. Miller rt->rt6i_idev = ort->rt6i_idev; 100414e50e57SDavid S. Miller if (rt->rt6i_idev) 100514e50e57SDavid S. Miller in6_dev_hold(rt->rt6i_idev); 100614e50e57SDavid S. Miller 10074e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 10081716a961SGao feng rt->rt6i_flags = ort->rt6i_flags; 10091716a961SGao feng rt6_clean_expires(rt); 101014e50e57SDavid S. Miller rt->rt6i_metric = 0; 101114e50e57SDavid S. Miller 101214e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 101314e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 101414e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 101514e50e57SDavid S. Miller #endif 101614e50e57SDavid S. Miller 101714e50e57SDavid S. Miller dst_free(new); 101814e50e57SDavid S. Miller } 101914e50e57SDavid S. Miller 102069ead7afSDavid S. Miller dst_release(dst_orig); 102169ead7afSDavid S. Miller return new ? new : ERR_PTR(-ENOMEM); 102214e50e57SDavid S. Miller } 102314e50e57SDavid S. Miller 10241da177e4SLinus Torvalds /* 10251da177e4SLinus Torvalds * Destination cache support functions 10261da177e4SLinus Torvalds */ 10271da177e4SLinus Torvalds 10281da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 10291da177e4SLinus Torvalds { 10301da177e4SLinus Torvalds struct rt6_info *rt; 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds rt = (struct rt6_info *) dst; 10331da177e4SLinus Torvalds 10346431cbc2SDavid S. Miller if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) { 10356431cbc2SDavid S. Miller if (rt->rt6i_peer_genid != rt6_peer_genid()) { 103697bab73fSDavid S. Miller if (!rt6_has_peer(rt)) 10376431cbc2SDavid S. Miller rt6_bind_peer(rt, 0); 10386431cbc2SDavid S. Miller rt->rt6i_peer_genid = rt6_peer_genid(); 10396431cbc2SDavid S. Miller } 10401da177e4SLinus Torvalds return dst; 10416431cbc2SDavid S. Miller } 10421da177e4SLinus Torvalds return NULL; 10431da177e4SLinus Torvalds } 10441da177e4SLinus Torvalds 10451da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 10461da177e4SLinus Torvalds { 10471da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 10481da177e4SLinus Torvalds 10491da177e4SLinus Torvalds if (rt) { 105054c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 105154c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 1052e0a1ad73SThomas Graf ip6_del_rt(rt); 105354c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 10541da177e4SLinus Torvalds } 105554c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 105654c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 105754c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 105854c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 105954c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 106054c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 10611da177e4SLinus Torvalds } 10621da177e4SLinus Torvalds 10631da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 10641da177e4SLinus Torvalds { 10651da177e4SLinus Torvalds struct rt6_info *rt; 10661da177e4SLinus Torvalds 10673ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 10681da177e4SLinus Torvalds 1069adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 10701da177e4SLinus Torvalds if (rt) { 10711716a961SGao feng if (rt->rt6i_flags & RTF_CACHE) 10721716a961SGao feng rt6_update_expires(rt, 0); 10731716a961SGao feng else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) 10741da177e4SLinus Torvalds rt->rt6i_node->fn_sernum = -1; 10751da177e4SLinus Torvalds } 10761da177e4SLinus Torvalds } 10771da177e4SLinus Torvalds 10786700c270SDavid S. Miller static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 10796700c270SDavid S. Miller struct sk_buff *skb, u32 mtu) 10801da177e4SLinus Torvalds { 10811da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info*)dst; 10821da177e4SLinus Torvalds 108381aded24SDavid S. Miller dst_confirm(dst); 10841da177e4SLinus Torvalds if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) { 108581aded24SDavid S. Miller struct net *net = dev_net(dst->dev); 108681aded24SDavid S. Miller 10871da177e4SLinus Torvalds rt6->rt6i_flags |= RTF_MODIFIED; 10881da177e4SLinus Torvalds if (mtu < IPV6_MIN_MTU) { 1089defb3519SDavid S. Miller u32 features = dst_metric(dst, RTAX_FEATURES); 10901da177e4SLinus Torvalds mtu = IPV6_MIN_MTU; 1091defb3519SDavid S. Miller features |= RTAX_FEATURE_ALLFRAG; 1092defb3519SDavid S. Miller dst_metric_set(dst, RTAX_FEATURES, features); 10931da177e4SLinus Torvalds } 1094defb3519SDavid S. Miller dst_metric_set(dst, RTAX_MTU, mtu); 109581aded24SDavid S. Miller rt6_update_expires(rt6, net->ipv6.sysctl.ip6_rt_mtu_expires); 10961da177e4SLinus Torvalds } 10971da177e4SLinus Torvalds } 10981da177e4SLinus Torvalds 109942ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, 110042ae66c8SDavid S. Miller int oif, u32 mark) 110181aded24SDavid S. Miller { 110281aded24SDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 110381aded24SDavid S. Miller struct dst_entry *dst; 110481aded24SDavid S. Miller struct flowi6 fl6; 110581aded24SDavid S. Miller 110681aded24SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 110781aded24SDavid S. Miller fl6.flowi6_oif = oif; 110881aded24SDavid S. Miller fl6.flowi6_mark = mark; 11093e12939aSDavid S. Miller fl6.flowi6_flags = 0; 111081aded24SDavid S. Miller fl6.daddr = iph->daddr; 111181aded24SDavid S. Miller fl6.saddr = iph->saddr; 111281aded24SDavid S. Miller fl6.flowlabel = (*(__be32 *) iph) & IPV6_FLOWINFO_MASK; 111381aded24SDavid S. Miller 111481aded24SDavid S. Miller dst = ip6_route_output(net, NULL, &fl6); 111581aded24SDavid S. Miller if (!dst->error) 11166700c270SDavid S. Miller ip6_rt_update_pmtu(dst, NULL, skb, ntohl(mtu)); 111781aded24SDavid S. Miller dst_release(dst); 111881aded24SDavid S. Miller } 111981aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu); 112081aded24SDavid S. Miller 112181aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) 112281aded24SDavid S. Miller { 112381aded24SDavid S. Miller ip6_update_pmtu(skb, sock_net(sk), mtu, 112481aded24SDavid S. Miller sk->sk_bound_dev_if, sk->sk_mark); 112581aded24SDavid S. Miller } 112681aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); 112781aded24SDavid S. Miller 11283a5ad2eeSDavid S. Miller void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark) 11293a5ad2eeSDavid S. Miller { 11303a5ad2eeSDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 11313a5ad2eeSDavid S. Miller struct dst_entry *dst; 11323a5ad2eeSDavid S. Miller struct flowi6 fl6; 11333a5ad2eeSDavid S. Miller 11343a5ad2eeSDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 11353a5ad2eeSDavid S. Miller fl6.flowi6_oif = oif; 11363a5ad2eeSDavid S. Miller fl6.flowi6_mark = mark; 11373a5ad2eeSDavid S. Miller fl6.flowi6_flags = 0; 11383a5ad2eeSDavid S. Miller fl6.daddr = iph->daddr; 11393a5ad2eeSDavid S. Miller fl6.saddr = iph->saddr; 11403a5ad2eeSDavid S. Miller fl6.flowlabel = (*(__be32 *) iph) & IPV6_FLOWINFO_MASK; 11413a5ad2eeSDavid S. Miller 11423a5ad2eeSDavid S. Miller dst = ip6_route_output(net, NULL, &fl6); 11433a5ad2eeSDavid S. Miller if (!dst->error) 11446700c270SDavid S. Miller rt6_do_redirect(dst, NULL, skb); 11453a5ad2eeSDavid S. Miller dst_release(dst); 11463a5ad2eeSDavid S. Miller } 11473a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect); 11483a5ad2eeSDavid S. Miller 11493a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) 11503a5ad2eeSDavid S. Miller { 11513a5ad2eeSDavid S. Miller ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark); 11523a5ad2eeSDavid S. Miller } 11533a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect); 11543a5ad2eeSDavid S. Miller 11550dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 11561da177e4SLinus Torvalds { 11570dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 11580dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 11590dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 11600dbaee3bSDavid S. Miller 11611da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 11621da177e4SLinus Torvalds 11635578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 11645578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 11651da177e4SLinus Torvalds 11661da177e4SLinus Torvalds /* 11671da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 11681da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 11691da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 11701da177e4SLinus Torvalds * rely only on pmtu discovery" 11711da177e4SLinus Torvalds */ 11721da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 11731da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 11741da177e4SLinus Torvalds return mtu; 11751da177e4SLinus Torvalds } 11761da177e4SLinus Torvalds 1177ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst) 1178d33e4553SDavid S. Miller { 1179d33e4553SDavid S. Miller struct inet6_dev *idev; 1180618f9bc7SSteffen Klassert unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); 1181618f9bc7SSteffen Klassert 1182618f9bc7SSteffen Klassert if (mtu) 1183618f9bc7SSteffen Klassert return mtu; 1184618f9bc7SSteffen Klassert 1185618f9bc7SSteffen Klassert mtu = IPV6_MIN_MTU; 1186d33e4553SDavid S. Miller 1187d33e4553SDavid S. Miller rcu_read_lock(); 1188d33e4553SDavid S. Miller idev = __in6_dev_get(dst->dev); 1189d33e4553SDavid S. Miller if (idev) 1190d33e4553SDavid S. Miller mtu = idev->cnf.mtu6; 1191d33e4553SDavid S. Miller rcu_read_unlock(); 1192d33e4553SDavid S. Miller 1193d33e4553SDavid S. Miller return mtu; 1194d33e4553SDavid S. Miller } 1195d33e4553SDavid S. Miller 11963b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list; 11973b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock); 11985d0bbeebSThomas Graf 11993b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 12001da177e4SLinus Torvalds struct neighbour *neigh, 120187a11578SDavid S. Miller struct flowi6 *fl6) 12021da177e4SLinus Torvalds { 120387a11578SDavid S. Miller struct dst_entry *dst; 12041da177e4SLinus Torvalds struct rt6_info *rt; 12051da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 1206c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 12071da177e4SLinus Torvalds 120838308473SDavid S. Miller if (unlikely(!idev)) 1209122bdf67SEric Dumazet return ERR_PTR(-ENODEV); 12101da177e4SLinus Torvalds 12118b96d22dSDavid S. Miller rt = ip6_dst_alloc(net, dev, 0, NULL); 121238308473SDavid S. Miller if (unlikely(!rt)) { 12131da177e4SLinus Torvalds in6_dev_put(idev); 121487a11578SDavid S. Miller dst = ERR_PTR(-ENOMEM); 12151da177e4SLinus Torvalds goto out; 12161da177e4SLinus Torvalds } 12171da177e4SLinus Torvalds 12181da177e4SLinus Torvalds if (neigh) 12191da177e4SLinus Torvalds neigh_hold(neigh); 122014deae41SDavid S. Miller else { 1221f894cbf8SDavid S. Miller neigh = ip6_neigh_lookup(&rt->dst, NULL, &fl6->daddr); 1222b43faac6SDavid S. Miller if (IS_ERR(neigh)) { 1223252c3d84SRongQing.Li in6_dev_put(idev); 1224b43faac6SDavid S. Miller dst_free(&rt->dst); 1225b43faac6SDavid S. Miller return ERR_CAST(neigh); 1226b43faac6SDavid S. Miller } 122714deae41SDavid S. Miller } 12281da177e4SLinus Torvalds 12298e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 12308e2ec639SYan, Zheng rt->dst.output = ip6_output; 123197cac082SDavid S. Miller rt->n = neigh; 1232d8d1f30bSChangli Gao atomic_set(&rt->dst.__refcnt, 1); 123387a11578SDavid S. Miller rt->rt6i_dst.addr = fl6->daddr; 12348e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 12358e2ec639SYan, Zheng rt->rt6i_idev = idev; 12367011687fSGao feng dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255); 12371da177e4SLinus Torvalds 12383b00944cSYOSHIFUJI Hideaki spin_lock_bh(&icmp6_dst_lock); 1239d8d1f30bSChangli Gao rt->dst.next = icmp6_dst_gc_list; 1240d8d1f30bSChangli Gao icmp6_dst_gc_list = &rt->dst; 12413b00944cSYOSHIFUJI Hideaki spin_unlock_bh(&icmp6_dst_lock); 12421da177e4SLinus Torvalds 12435578689aSDaniel Lezcano fib6_force_start_gc(net); 12441da177e4SLinus Torvalds 124587a11578SDavid S. Miller dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); 124687a11578SDavid S. Miller 12471da177e4SLinus Torvalds out: 124887a11578SDavid S. Miller return dst; 12491da177e4SLinus Torvalds } 12501da177e4SLinus Torvalds 12513d0f24a7SStephen Hemminger int icmp6_dst_gc(void) 12521da177e4SLinus Torvalds { 1253e9476e95SHagen Paul Pfeifer struct dst_entry *dst, **pprev; 12543d0f24a7SStephen Hemminger int more = 0; 12551da177e4SLinus Torvalds 12563b00944cSYOSHIFUJI Hideaki spin_lock_bh(&icmp6_dst_lock); 12573b00944cSYOSHIFUJI Hideaki pprev = &icmp6_dst_gc_list; 12585d0bbeebSThomas Graf 12591da177e4SLinus Torvalds while ((dst = *pprev) != NULL) { 12601da177e4SLinus Torvalds if (!atomic_read(&dst->__refcnt)) { 12611da177e4SLinus Torvalds *pprev = dst->next; 12621da177e4SLinus Torvalds dst_free(dst); 12631da177e4SLinus Torvalds } else { 12641da177e4SLinus Torvalds pprev = &dst->next; 12653d0f24a7SStephen Hemminger ++more; 12661da177e4SLinus Torvalds } 12671da177e4SLinus Torvalds } 12681da177e4SLinus Torvalds 12693b00944cSYOSHIFUJI Hideaki spin_unlock_bh(&icmp6_dst_lock); 12705d0bbeebSThomas Graf 12713d0f24a7SStephen Hemminger return more; 12721da177e4SLinus Torvalds } 12731da177e4SLinus Torvalds 12741e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg), 12751e493d19SDavid S. Miller void *arg) 12761e493d19SDavid S. Miller { 12771e493d19SDavid S. Miller struct dst_entry *dst, **pprev; 12781e493d19SDavid S. Miller 12791e493d19SDavid S. Miller spin_lock_bh(&icmp6_dst_lock); 12801e493d19SDavid S. Miller pprev = &icmp6_dst_gc_list; 12811e493d19SDavid S. Miller while ((dst = *pprev) != NULL) { 12821e493d19SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 12831e493d19SDavid S. Miller if (func(rt, arg)) { 12841e493d19SDavid S. Miller *pprev = dst->next; 12851e493d19SDavid S. Miller dst_free(dst); 12861e493d19SDavid S. Miller } else { 12871e493d19SDavid S. Miller pprev = &dst->next; 12881e493d19SDavid S. Miller } 12891e493d19SDavid S. Miller } 12901e493d19SDavid S. Miller spin_unlock_bh(&icmp6_dst_lock); 12911e493d19SDavid S. Miller } 12921e493d19SDavid S. Miller 1293569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops) 12941da177e4SLinus Torvalds { 12951da177e4SLinus Torvalds unsigned long now = jiffies; 129686393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 12977019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 12987019b78eSDaniel Lezcano int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; 12997019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 13007019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 13017019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 1302fc66f95cSEric Dumazet int entries; 13031da177e4SLinus Torvalds 1304fc66f95cSEric Dumazet entries = dst_entries_get_fast(ops); 13057019b78eSDaniel Lezcano if (time_after(rt_last_gc + rt_min_interval, now) && 1306fc66f95cSEric Dumazet entries <= rt_max_size) 13071da177e4SLinus Torvalds goto out; 13081da177e4SLinus Torvalds 13096891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire++; 13106891a346SBenjamin Thery fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net); 13116891a346SBenjamin Thery net->ipv6.ip6_rt_last_gc = now; 1312fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 1313fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 13147019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; 13151da177e4SLinus Torvalds out: 13167019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; 1317fc66f95cSEric Dumazet return entries > rt_max_size; 13181da177e4SLinus Torvalds } 13191da177e4SLinus Torvalds 13201da177e4SLinus Torvalds /* Clean host part of a prefix. Not necessary in radix tree, 13211da177e4SLinus Torvalds but results in cleaner routing tables. 13221da177e4SLinus Torvalds 13231da177e4SLinus Torvalds Remove it only when all the things will work! 13241da177e4SLinus Torvalds */ 13251da177e4SLinus Torvalds 13266b75d090SYOSHIFUJI Hideaki int ip6_dst_hoplimit(struct dst_entry *dst) 13271da177e4SLinus Torvalds { 13285170ae82SDavid S. Miller int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT); 1329a02e4b7dSDavid S. Miller if (hoplimit == 0) { 13306b75d090SYOSHIFUJI Hideaki struct net_device *dev = dst->dev; 1331c68f24ccSEric Dumazet struct inet6_dev *idev; 1332c68f24ccSEric Dumazet 1333c68f24ccSEric Dumazet rcu_read_lock(); 1334c68f24ccSEric Dumazet idev = __in6_dev_get(dev); 1335c68f24ccSEric Dumazet if (idev) 13361da177e4SLinus Torvalds hoplimit = idev->cnf.hop_limit; 1337c68f24ccSEric Dumazet else 133853b7997fSYOSHIFUJI Hideaki hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit; 1339c68f24ccSEric Dumazet rcu_read_unlock(); 13401da177e4SLinus Torvalds } 13411da177e4SLinus Torvalds return hoplimit; 13421da177e4SLinus Torvalds } 1343abbf46aeSDavid S. Miller EXPORT_SYMBOL(ip6_dst_hoplimit); 13441da177e4SLinus Torvalds 13451da177e4SLinus Torvalds /* 13461da177e4SLinus Torvalds * 13471da177e4SLinus Torvalds */ 13481da177e4SLinus Torvalds 134986872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg) 13501da177e4SLinus Torvalds { 13511da177e4SLinus Torvalds int err; 13525578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 13531da177e4SLinus Torvalds struct rt6_info *rt = NULL; 13541da177e4SLinus Torvalds struct net_device *dev = NULL; 13551da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 1356c71099acSThomas Graf struct fib6_table *table; 13571da177e4SLinus Torvalds int addr_type; 13581da177e4SLinus Torvalds 135986872cb5SThomas Graf if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128) 13601da177e4SLinus Torvalds return -EINVAL; 13611da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 136286872cb5SThomas Graf if (cfg->fc_src_len) 13631da177e4SLinus Torvalds return -EINVAL; 13641da177e4SLinus Torvalds #endif 136586872cb5SThomas Graf if (cfg->fc_ifindex) { 13661da177e4SLinus Torvalds err = -ENODEV; 13675578689aSDaniel Lezcano dev = dev_get_by_index(net, cfg->fc_ifindex); 13681da177e4SLinus Torvalds if (!dev) 13691da177e4SLinus Torvalds goto out; 13701da177e4SLinus Torvalds idev = in6_dev_get(dev); 13711da177e4SLinus Torvalds if (!idev) 13721da177e4SLinus Torvalds goto out; 13731da177e4SLinus Torvalds } 13741da177e4SLinus Torvalds 137586872cb5SThomas Graf if (cfg->fc_metric == 0) 137686872cb5SThomas Graf cfg->fc_metric = IP6_RT_PRIO_USER; 13771da177e4SLinus Torvalds 1378c71099acSThomas Graf err = -ENOBUFS; 137938308473SDavid S. Miller if (cfg->fc_nlinfo.nlh && 1380d71314b4SMatti Vaittinen !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { 1381d71314b4SMatti Vaittinen table = fib6_get_table(net, cfg->fc_table); 138238308473SDavid S. Miller if (!table) { 1383f3213831SJoe Perches pr_warn("NLM_F_CREATE should be specified when creating new route\n"); 1384d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 1385d71314b4SMatti Vaittinen } 1386d71314b4SMatti Vaittinen } else { 1387d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 1388d71314b4SMatti Vaittinen } 138938308473SDavid S. Miller 139038308473SDavid S. Miller if (!table) 1391c71099acSThomas Graf goto out; 1392c71099acSThomas Graf 13938b96d22dSDavid S. Miller rt = ip6_dst_alloc(net, NULL, DST_NOCOUNT, table); 13941da177e4SLinus Torvalds 139538308473SDavid S. Miller if (!rt) { 13961da177e4SLinus Torvalds err = -ENOMEM; 13971da177e4SLinus Torvalds goto out; 13981da177e4SLinus Torvalds } 13991da177e4SLinus Torvalds 1400d8d1f30bSChangli Gao rt->dst.obsolete = -1; 14011716a961SGao feng 14021716a961SGao feng if (cfg->fc_flags & RTF_EXPIRES) 14031716a961SGao feng rt6_set_expires(rt, jiffies + 14041716a961SGao feng clock_t_to_jiffies(cfg->fc_expires)); 14051716a961SGao feng else 14061716a961SGao feng rt6_clean_expires(rt); 14071da177e4SLinus Torvalds 140886872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 140986872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 141086872cb5SThomas Graf rt->rt6i_protocol = cfg->fc_protocol; 141186872cb5SThomas Graf 141286872cb5SThomas Graf addr_type = ipv6_addr_type(&cfg->fc_dst); 14131da177e4SLinus Torvalds 14141da177e4SLinus Torvalds if (addr_type & IPV6_ADDR_MULTICAST) 1415d8d1f30bSChangli Gao rt->dst.input = ip6_mc_input; 1416ab79ad14SMaciej Żenczykowski else if (cfg->fc_flags & RTF_LOCAL) 1417ab79ad14SMaciej Żenczykowski rt->dst.input = ip6_input; 14181da177e4SLinus Torvalds else 1419d8d1f30bSChangli Gao rt->dst.input = ip6_forward; 14201da177e4SLinus Torvalds 1421d8d1f30bSChangli Gao rt->dst.output = ip6_output; 14221da177e4SLinus Torvalds 142386872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 142486872cb5SThomas Graf rt->rt6i_dst.plen = cfg->fc_dst_len; 14251da177e4SLinus Torvalds if (rt->rt6i_dst.plen == 128) 142611d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 14271da177e4SLinus Torvalds 14288e2ec639SYan, Zheng if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) { 14298e2ec639SYan, Zheng u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL); 14308e2ec639SYan, Zheng if (!metrics) { 14318e2ec639SYan, Zheng err = -ENOMEM; 14328e2ec639SYan, Zheng goto out; 14338e2ec639SYan, Zheng } 14348e2ec639SYan, Zheng dst_init_metrics(&rt->dst, metrics, 0); 14358e2ec639SYan, Zheng } 14361da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 143786872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len); 143886872cb5SThomas Graf rt->rt6i_src.plen = cfg->fc_src_len; 14391da177e4SLinus Torvalds #endif 14401da177e4SLinus Torvalds 144186872cb5SThomas Graf rt->rt6i_metric = cfg->fc_metric; 14421da177e4SLinus Torvalds 14431da177e4SLinus Torvalds /* We cannot add true routes via loopback here, 14441da177e4SLinus Torvalds they would result in kernel looping; promote them to reject routes 14451da177e4SLinus Torvalds */ 144686872cb5SThomas Graf if ((cfg->fc_flags & RTF_REJECT) || 144738308473SDavid S. Miller (dev && (dev->flags & IFF_LOOPBACK) && 144838308473SDavid S. Miller !(addr_type & IPV6_ADDR_LOOPBACK) && 144938308473SDavid S. Miller !(cfg->fc_flags & RTF_LOCAL))) { 14501da177e4SLinus Torvalds /* hold loopback dev/idev if we haven't done so. */ 14515578689aSDaniel Lezcano if (dev != net->loopback_dev) { 14521da177e4SLinus Torvalds if (dev) { 14531da177e4SLinus Torvalds dev_put(dev); 14541da177e4SLinus Torvalds in6_dev_put(idev); 14551da177e4SLinus Torvalds } 14565578689aSDaniel Lezcano dev = net->loopback_dev; 14571da177e4SLinus Torvalds dev_hold(dev); 14581da177e4SLinus Torvalds idev = in6_dev_get(dev); 14591da177e4SLinus Torvalds if (!idev) { 14601da177e4SLinus Torvalds err = -ENODEV; 14611da177e4SLinus Torvalds goto out; 14621da177e4SLinus Torvalds } 14631da177e4SLinus Torvalds } 1464d8d1f30bSChangli Gao rt->dst.output = ip6_pkt_discard_out; 1465d8d1f30bSChangli Gao rt->dst.input = ip6_pkt_discard; 14661da177e4SLinus Torvalds rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP; 1467*ef2c7d7bSNicolas Dichtel switch (cfg->fc_type) { 1468*ef2c7d7bSNicolas Dichtel case RTN_BLACKHOLE: 1469*ef2c7d7bSNicolas Dichtel rt->dst.error = -EINVAL; 1470*ef2c7d7bSNicolas Dichtel break; 1471*ef2c7d7bSNicolas Dichtel case RTN_PROHIBIT: 1472*ef2c7d7bSNicolas Dichtel rt->dst.error = -EACCES; 1473*ef2c7d7bSNicolas Dichtel break; 1474*ef2c7d7bSNicolas Dichtel default: 1475*ef2c7d7bSNicolas Dichtel rt->dst.error = -ENETUNREACH; 1476*ef2c7d7bSNicolas Dichtel break; 1477*ef2c7d7bSNicolas Dichtel } 14781da177e4SLinus Torvalds goto install_route; 14791da177e4SLinus Torvalds } 14801da177e4SLinus Torvalds 148186872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY) { 1482b71d1d42SEric Dumazet const struct in6_addr *gw_addr; 14831da177e4SLinus Torvalds int gwa_type; 14841da177e4SLinus Torvalds 148586872cb5SThomas Graf gw_addr = &cfg->fc_gateway; 14864e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = *gw_addr; 14871da177e4SLinus Torvalds gwa_type = ipv6_addr_type(gw_addr); 14881da177e4SLinus Torvalds 14891da177e4SLinus Torvalds if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) { 14901da177e4SLinus Torvalds struct rt6_info *grt; 14911da177e4SLinus Torvalds 14921da177e4SLinus Torvalds /* IPv6 strictly inhibits using not link-local 14931da177e4SLinus Torvalds addresses as nexthop address. 14941da177e4SLinus Torvalds Otherwise, router will not able to send redirects. 14951da177e4SLinus Torvalds It is very good, but in some (rare!) circumstances 14961da177e4SLinus Torvalds (SIT, PtP, NBMA NOARP links) it is handy to allow 14971da177e4SLinus Torvalds some exceptions. --ANK 14981da177e4SLinus Torvalds */ 14991da177e4SLinus Torvalds err = -EINVAL; 15001da177e4SLinus Torvalds if (!(gwa_type & IPV6_ADDR_UNICAST)) 15011da177e4SLinus Torvalds goto out; 15021da177e4SLinus Torvalds 15035578689aSDaniel Lezcano grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1); 15041da177e4SLinus Torvalds 15051da177e4SLinus Torvalds err = -EHOSTUNREACH; 150638308473SDavid S. Miller if (!grt) 15071da177e4SLinus Torvalds goto out; 15081da177e4SLinus Torvalds if (dev) { 1509d1918542SDavid S. Miller if (dev != grt->dst.dev) { 1510d8d1f30bSChangli Gao dst_release(&grt->dst); 15111da177e4SLinus Torvalds goto out; 15121da177e4SLinus Torvalds } 15131da177e4SLinus Torvalds } else { 1514d1918542SDavid S. Miller dev = grt->dst.dev; 15151da177e4SLinus Torvalds idev = grt->rt6i_idev; 15161da177e4SLinus Torvalds dev_hold(dev); 15171da177e4SLinus Torvalds in6_dev_hold(grt->rt6i_idev); 15181da177e4SLinus Torvalds } 15191da177e4SLinus Torvalds if (!(grt->rt6i_flags & RTF_GATEWAY)) 15201da177e4SLinus Torvalds err = 0; 1521d8d1f30bSChangli Gao dst_release(&grt->dst); 15221da177e4SLinus Torvalds 15231da177e4SLinus Torvalds if (err) 15241da177e4SLinus Torvalds goto out; 15251da177e4SLinus Torvalds } 15261da177e4SLinus Torvalds err = -EINVAL; 152738308473SDavid S. Miller if (!dev || (dev->flags & IFF_LOOPBACK)) 15281da177e4SLinus Torvalds goto out; 15291da177e4SLinus Torvalds } 15301da177e4SLinus Torvalds 15311da177e4SLinus Torvalds err = -ENODEV; 153238308473SDavid S. Miller if (!dev) 15331da177e4SLinus Torvalds goto out; 15341da177e4SLinus Torvalds 1535c3968a85SDaniel Walter if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 1536c3968a85SDaniel Walter if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 1537c3968a85SDaniel Walter err = -EINVAL; 1538c3968a85SDaniel Walter goto out; 1539c3968a85SDaniel Walter } 15404e3fd7a0SAlexey Dobriyan rt->rt6i_prefsrc.addr = cfg->fc_prefsrc; 1541c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 128; 1542c3968a85SDaniel Walter } else 1543c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 1544c3968a85SDaniel Walter 154586872cb5SThomas Graf if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) { 15468ade06c6SDavid S. Miller err = rt6_bind_neighbour(rt, dev); 1547f83c7790SDavid S. Miller if (err) 15481da177e4SLinus Torvalds goto out; 15491da177e4SLinus Torvalds } 15501da177e4SLinus Torvalds 155186872cb5SThomas Graf rt->rt6i_flags = cfg->fc_flags; 15521da177e4SLinus Torvalds 15531da177e4SLinus Torvalds install_route: 155486872cb5SThomas Graf if (cfg->fc_mx) { 155586872cb5SThomas Graf struct nlattr *nla; 155686872cb5SThomas Graf int remaining; 15571da177e4SLinus Torvalds 155886872cb5SThomas Graf nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { 15598f4c1f9bSThomas Graf int type = nla_type(nla); 156086872cb5SThomas Graf 156186872cb5SThomas Graf if (type) { 156286872cb5SThomas Graf if (type > RTAX_MAX) { 15631da177e4SLinus Torvalds err = -EINVAL; 15641da177e4SLinus Torvalds goto out; 15651da177e4SLinus Torvalds } 156686872cb5SThomas Graf 1567defb3519SDavid S. Miller dst_metric_set(&rt->dst, type, nla_get_u32(nla)); 15681da177e4SLinus Torvalds } 15691da177e4SLinus Torvalds } 15701da177e4SLinus Torvalds } 15711da177e4SLinus Torvalds 1572d8d1f30bSChangli Gao rt->dst.dev = dev; 15731da177e4SLinus Torvalds rt->rt6i_idev = idev; 1574c71099acSThomas Graf rt->rt6i_table = table; 157563152fc0SDaniel Lezcano 1576c346dca1SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = dev_net(dev); 157763152fc0SDaniel Lezcano 157886872cb5SThomas Graf return __ip6_ins_rt(rt, &cfg->fc_nlinfo); 15791da177e4SLinus Torvalds 15801da177e4SLinus Torvalds out: 15811da177e4SLinus Torvalds if (dev) 15821da177e4SLinus Torvalds dev_put(dev); 15831da177e4SLinus Torvalds if (idev) 15841da177e4SLinus Torvalds in6_dev_put(idev); 15851da177e4SLinus Torvalds if (rt) 1586d8d1f30bSChangli Gao dst_free(&rt->dst); 15871da177e4SLinus Torvalds return err; 15881da177e4SLinus Torvalds } 15891da177e4SLinus Torvalds 159086872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info) 15911da177e4SLinus Torvalds { 15921da177e4SLinus Torvalds int err; 1593c71099acSThomas Graf struct fib6_table *table; 1594d1918542SDavid S. Miller struct net *net = dev_net(rt->dst.dev); 15951da177e4SLinus Torvalds 15968ed67789SDaniel Lezcano if (rt == net->ipv6.ip6_null_entry) 15976c813a72SPatrick McHardy return -ENOENT; 15986c813a72SPatrick McHardy 1599c71099acSThomas Graf table = rt->rt6i_table; 1600c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 16011da177e4SLinus Torvalds 160286872cb5SThomas Graf err = fib6_del(rt, info); 1603d8d1f30bSChangli Gao dst_release(&rt->dst); 16041da177e4SLinus Torvalds 1605c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 16061da177e4SLinus Torvalds 16071da177e4SLinus Torvalds return err; 16081da177e4SLinus Torvalds } 16091da177e4SLinus Torvalds 1610e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt) 1611e0a1ad73SThomas Graf { 16124d1169c1SDenis V. Lunev struct nl_info info = { 1613d1918542SDavid S. Miller .nl_net = dev_net(rt->dst.dev), 16144d1169c1SDenis V. Lunev }; 1615528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 1616e0a1ad73SThomas Graf } 1617e0a1ad73SThomas Graf 161886872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg) 16191da177e4SLinus Torvalds { 1620c71099acSThomas Graf struct fib6_table *table; 16211da177e4SLinus Torvalds struct fib6_node *fn; 16221da177e4SLinus Torvalds struct rt6_info *rt; 16231da177e4SLinus Torvalds int err = -ESRCH; 16241da177e4SLinus Torvalds 16255578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 162638308473SDavid S. Miller if (!table) 1627c71099acSThomas Graf return err; 16281da177e4SLinus Torvalds 1629c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 1630c71099acSThomas Graf 1631c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 163286872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 163386872cb5SThomas Graf &cfg->fc_src, cfg->fc_src_len); 16341da177e4SLinus Torvalds 16351da177e4SLinus Torvalds if (fn) { 1636d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 163786872cb5SThomas Graf if (cfg->fc_ifindex && 1638d1918542SDavid S. Miller (!rt->dst.dev || 1639d1918542SDavid S. Miller rt->dst.dev->ifindex != cfg->fc_ifindex)) 16401da177e4SLinus Torvalds continue; 164186872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 164286872cb5SThomas Graf !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 16431da177e4SLinus Torvalds continue; 164486872cb5SThomas Graf if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric) 16451da177e4SLinus Torvalds continue; 1646d8d1f30bSChangli Gao dst_hold(&rt->dst); 1647c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 16481da177e4SLinus Torvalds 164986872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 16501da177e4SLinus Torvalds } 16511da177e4SLinus Torvalds } 1652c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 16531da177e4SLinus Torvalds 16541da177e4SLinus Torvalds return err; 16551da177e4SLinus Torvalds } 16561da177e4SLinus Torvalds 16576700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 1658a6279458SYOSHIFUJI Hideaki { 1659e8599ff4SDavid S. Miller struct net *net = dev_net(skb->dev); 1660a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 1661e8599ff4SDavid S. Miller struct rt6_info *rt, *nrt = NULL; 1662e8599ff4SDavid S. Miller const struct in6_addr *target; 1663e8599ff4SDavid S. Miller struct ndisc_options ndopts; 16646e157b6aSDavid S. Miller const struct in6_addr *dest; 16656e157b6aSDavid S. Miller struct neighbour *old_neigh; 1666e8599ff4SDavid S. Miller struct inet6_dev *in6_dev; 1667e8599ff4SDavid S. Miller struct neighbour *neigh; 1668e8599ff4SDavid S. Miller struct icmp6hdr *icmph; 16696e157b6aSDavid S. Miller int optlen, on_link; 16706e157b6aSDavid S. Miller u8 *lladdr; 1671e8599ff4SDavid S. Miller 1672e8599ff4SDavid S. Miller optlen = skb->tail - skb->transport_header; 1673e8599ff4SDavid S. Miller optlen -= sizeof(struct icmp6hdr) + 2 * sizeof(struct in6_addr); 1674e8599ff4SDavid S. Miller 1675e8599ff4SDavid S. Miller if (optlen < 0) { 16766e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); 1677e8599ff4SDavid S. Miller return; 1678e8599ff4SDavid S. Miller } 1679e8599ff4SDavid S. Miller 1680e8599ff4SDavid S. Miller icmph = icmp6_hdr(skb); 1681e8599ff4SDavid S. Miller target = (const struct in6_addr *) (icmph + 1); 1682e8599ff4SDavid S. Miller dest = target + 1; 1683e8599ff4SDavid S. Miller 1684e8599ff4SDavid S. Miller if (ipv6_addr_is_multicast(dest)) { 16856e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); 1686e8599ff4SDavid S. Miller return; 1687e8599ff4SDavid S. Miller } 1688e8599ff4SDavid S. Miller 16896e157b6aSDavid S. Miller on_link = 0; 1690e8599ff4SDavid S. Miller if (ipv6_addr_equal(dest, target)) { 1691e8599ff4SDavid S. Miller on_link = 1; 1692e8599ff4SDavid S. Miller } else if (ipv6_addr_type(target) != 1693e8599ff4SDavid S. Miller (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { 16946e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); 1695e8599ff4SDavid S. Miller return; 1696e8599ff4SDavid S. Miller } 1697e8599ff4SDavid S. Miller 1698e8599ff4SDavid S. Miller in6_dev = __in6_dev_get(skb->dev); 1699e8599ff4SDavid S. Miller if (!in6_dev) 1700e8599ff4SDavid S. Miller return; 1701e8599ff4SDavid S. Miller if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) 1702e8599ff4SDavid S. Miller return; 1703e8599ff4SDavid S. Miller 1704e8599ff4SDavid S. Miller /* RFC2461 8.1: 1705e8599ff4SDavid S. Miller * The IP source address of the Redirect MUST be the same as the current 1706e8599ff4SDavid S. Miller * first-hop router for the specified ICMP Destination Address. 1707e8599ff4SDavid S. Miller */ 1708e8599ff4SDavid S. Miller 1709e8599ff4SDavid S. Miller if (!ndisc_parse_options((u8*)(dest + 1), optlen, &ndopts)) { 1710e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); 1711e8599ff4SDavid S. Miller return; 1712e8599ff4SDavid S. Miller } 17136e157b6aSDavid S. Miller 17146e157b6aSDavid S. Miller lladdr = NULL; 1715e8599ff4SDavid S. Miller if (ndopts.nd_opts_tgt_lladdr) { 1716e8599ff4SDavid S. Miller lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, 1717e8599ff4SDavid S. Miller skb->dev); 1718e8599ff4SDavid S. Miller if (!lladdr) { 1719e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); 1720e8599ff4SDavid S. Miller return; 1721e8599ff4SDavid S. Miller } 1722e8599ff4SDavid S. Miller } 1723e8599ff4SDavid S. Miller 17246e157b6aSDavid S. Miller rt = (struct rt6_info *) dst; 17256e157b6aSDavid S. Miller if (rt == net->ipv6.ip6_null_entry) { 17266e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); 17276e157b6aSDavid S. Miller return; 17286e157b6aSDavid S. Miller } 17296e157b6aSDavid S. Miller 17306e157b6aSDavid S. Miller /* Redirect received -> path was valid. 17316e157b6aSDavid S. Miller * Look, redirects are sent only in response to data packets, 17326e157b6aSDavid S. Miller * so that this nexthop apparently is reachable. --ANK 17336e157b6aSDavid S. Miller */ 17346e157b6aSDavid S. Miller dst_confirm(&rt->dst); 17356e157b6aSDavid S. Miller 1736e8599ff4SDavid S. Miller neigh = __neigh_lookup(&nd_tbl, target, skb->dev, 1); 1737e8599ff4SDavid S. Miller if (!neigh) 1738e8599ff4SDavid S. Miller return; 1739e8599ff4SDavid S. Miller 17406e157b6aSDavid S. Miller /* Duplicate redirect: silently ignore. */ 17416e157b6aSDavid S. Miller old_neigh = rt->n; 17426e157b6aSDavid S. Miller if (neigh == old_neigh) 1743a6279458SYOSHIFUJI Hideaki goto out; 17441da177e4SLinus Torvalds 17451da177e4SLinus Torvalds /* 17461da177e4SLinus Torvalds * We have finally decided to accept it. 17471da177e4SLinus Torvalds */ 17481da177e4SLinus Torvalds 17491da177e4SLinus Torvalds neigh_update(neigh, lladdr, NUD_STALE, 17501da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 17511da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 17521da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 17531da177e4SLinus Torvalds NEIGH_UPDATE_F_ISROUTER)) 17541da177e4SLinus Torvalds ); 17551da177e4SLinus Torvalds 175621efcfa0SEric Dumazet nrt = ip6_rt_copy(rt, dest); 175738308473SDavid S. Miller if (!nrt) 17581da177e4SLinus Torvalds goto out; 17591da177e4SLinus Torvalds 17601da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 17611da177e4SLinus Torvalds if (on_link) 17621da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 17631da177e4SLinus Torvalds 17644e3fd7a0SAlexey Dobriyan nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 176597cac082SDavid S. Miller nrt->n = neigh_clone(neigh); 17661da177e4SLinus Torvalds 176740e22e8fSThomas Graf if (ip6_ins_rt(nrt)) 17681da177e4SLinus Torvalds goto out; 17691da177e4SLinus Torvalds 1770d8d1f30bSChangli Gao netevent.old = &rt->dst; 17711d248b1cSDavid S. Miller netevent.old_neigh = old_neigh; 1772d8d1f30bSChangli Gao netevent.new = &nrt->dst; 17731d248b1cSDavid S. Miller netevent.new_neigh = neigh; 17741d248b1cSDavid S. Miller netevent.daddr = dest; 17758d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 17768d71740cSTom Tucker 17771da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) { 17786e157b6aSDavid S. Miller rt = (struct rt6_info *) dst_clone(&rt->dst); 1779e0a1ad73SThomas Graf ip6_del_rt(rt); 17801da177e4SLinus Torvalds } 17811da177e4SLinus Torvalds 17821da177e4SLinus Torvalds out: 1783e8599ff4SDavid S. Miller neigh_release(neigh); 17846e157b6aSDavid S. Miller } 17856e157b6aSDavid S. Miller 17861da177e4SLinus Torvalds /* 17871da177e4SLinus Torvalds * Misc support functions 17881da177e4SLinus Torvalds */ 17891da177e4SLinus Torvalds 17901716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort, 179121efcfa0SEric Dumazet const struct in6_addr *dest) 17921da177e4SLinus Torvalds { 1793d1918542SDavid S. Miller struct net *net = dev_net(ort->dst.dev); 17948b96d22dSDavid S. Miller struct rt6_info *rt = ip6_dst_alloc(net, ort->dst.dev, 0, 17958b96d22dSDavid S. Miller ort->rt6i_table); 17961da177e4SLinus Torvalds 17971da177e4SLinus Torvalds if (rt) { 1798d8d1f30bSChangli Gao rt->dst.input = ort->dst.input; 1799d8d1f30bSChangli Gao rt->dst.output = ort->dst.output; 18008e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 18011da177e4SLinus Torvalds 18024e3fd7a0SAlexey Dobriyan rt->rt6i_dst.addr = *dest; 18038e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 1804defb3519SDavid S. Miller dst_copy_metrics(&rt->dst, &ort->dst); 1805d8d1f30bSChangli Gao rt->dst.error = ort->dst.error; 18061da177e4SLinus Torvalds rt->rt6i_idev = ort->rt6i_idev; 18071da177e4SLinus Torvalds if (rt->rt6i_idev) 18081da177e4SLinus Torvalds in6_dev_hold(rt->rt6i_idev); 1809d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 18101da177e4SLinus Torvalds 18114e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 18121716a961SGao feng rt->rt6i_flags = ort->rt6i_flags; 18131716a961SGao feng if ((ort->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) == 18141716a961SGao feng (RTF_DEFAULT | RTF_ADDRCONF)) 18151716a961SGao feng rt6_set_from(rt, ort); 18161716a961SGao feng else 18171716a961SGao feng rt6_clean_expires(rt); 18181da177e4SLinus Torvalds rt->rt6i_metric = 0; 18191da177e4SLinus Torvalds 18201da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 18211da177e4SLinus Torvalds memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 18221da177e4SLinus Torvalds #endif 18230f6c6392SFlorian Westphal memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key)); 1824c71099acSThomas Graf rt->rt6i_table = ort->rt6i_table; 18251da177e4SLinus Torvalds } 18261da177e4SLinus Torvalds return rt; 18271da177e4SLinus Torvalds } 18281da177e4SLinus Torvalds 182970ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 1830efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 1831b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 1832b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex) 183370ceb4f5SYOSHIFUJI Hideaki { 183470ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 183570ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt = NULL; 1836c71099acSThomas Graf struct fib6_table *table; 183770ceb4f5SYOSHIFUJI Hideaki 1838efa2cea0SDaniel Lezcano table = fib6_get_table(net, RT6_TABLE_INFO); 183938308473SDavid S. Miller if (!table) 1840c71099acSThomas Graf return NULL; 1841c71099acSThomas Graf 1842c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 1843c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0); 184470ceb4f5SYOSHIFUJI Hideaki if (!fn) 184570ceb4f5SYOSHIFUJI Hideaki goto out; 184670ceb4f5SYOSHIFUJI Hideaki 1847d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 1848d1918542SDavid S. Miller if (rt->dst.dev->ifindex != ifindex) 184970ceb4f5SYOSHIFUJI Hideaki continue; 185070ceb4f5SYOSHIFUJI Hideaki if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) 185170ceb4f5SYOSHIFUJI Hideaki continue; 185270ceb4f5SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr)) 185370ceb4f5SYOSHIFUJI Hideaki continue; 1854d8d1f30bSChangli Gao dst_hold(&rt->dst); 185570ceb4f5SYOSHIFUJI Hideaki break; 185670ceb4f5SYOSHIFUJI Hideaki } 185770ceb4f5SYOSHIFUJI Hideaki out: 1858c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 185970ceb4f5SYOSHIFUJI Hideaki return rt; 186070ceb4f5SYOSHIFUJI Hideaki } 186170ceb4f5SYOSHIFUJI Hideaki 1862efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 1863b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 1864b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex, 186595c96174SEric Dumazet unsigned int pref) 186670ceb4f5SYOSHIFUJI Hideaki { 186786872cb5SThomas Graf struct fib6_config cfg = { 186886872cb5SThomas Graf .fc_table = RT6_TABLE_INFO, 1869238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 187086872cb5SThomas Graf .fc_ifindex = ifindex, 187186872cb5SThomas Graf .fc_dst_len = prefixlen, 187286872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 187386872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 1874efa2cea0SDaniel Lezcano .fc_nlinfo.pid = 0, 1875efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 1876efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 187786872cb5SThomas Graf }; 187870ceb4f5SYOSHIFUJI Hideaki 18794e3fd7a0SAlexey Dobriyan cfg.fc_dst = *prefix; 18804e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 188186872cb5SThomas Graf 1882e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 1883e317da96SYOSHIFUJI Hideaki if (!prefixlen) 188486872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 188570ceb4f5SYOSHIFUJI Hideaki 188686872cb5SThomas Graf ip6_route_add(&cfg); 188770ceb4f5SYOSHIFUJI Hideaki 1888efa2cea0SDaniel Lezcano return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex); 188970ceb4f5SYOSHIFUJI Hideaki } 189070ceb4f5SYOSHIFUJI Hideaki #endif 189170ceb4f5SYOSHIFUJI Hideaki 1892b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev) 18931da177e4SLinus Torvalds { 18941da177e4SLinus Torvalds struct rt6_info *rt; 1895c71099acSThomas Graf struct fib6_table *table; 18961da177e4SLinus Torvalds 1897c346dca1SYOSHIFUJI Hideaki table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT); 189838308473SDavid S. Miller if (!table) 1899c71099acSThomas Graf return NULL; 19001da177e4SLinus Torvalds 1901c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 1902d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) { 1903d1918542SDavid S. Miller if (dev == rt->dst.dev && 1904045927ffSYOSHIFUJI Hideaki ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 19051da177e4SLinus Torvalds ipv6_addr_equal(&rt->rt6i_gateway, addr)) 19061da177e4SLinus Torvalds break; 19071da177e4SLinus Torvalds } 19081da177e4SLinus Torvalds if (rt) 1909d8d1f30bSChangli Gao dst_hold(&rt->dst); 1910c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 19111da177e4SLinus Torvalds return rt; 19121da177e4SLinus Torvalds } 19131da177e4SLinus Torvalds 1914b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr, 1915ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 1916ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 19171da177e4SLinus Torvalds { 191886872cb5SThomas Graf struct fib6_config cfg = { 191986872cb5SThomas Graf .fc_table = RT6_TABLE_DFLT, 1920238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 192186872cb5SThomas Graf .fc_ifindex = dev->ifindex, 192286872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 192386872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 19245578689aSDaniel Lezcano .fc_nlinfo.pid = 0, 19255578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 1926c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 192786872cb5SThomas Graf }; 19281da177e4SLinus Torvalds 19294e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 19301da177e4SLinus Torvalds 193186872cb5SThomas Graf ip6_route_add(&cfg); 19321da177e4SLinus Torvalds 19331da177e4SLinus Torvalds return rt6_get_dflt_router(gwaddr, dev); 19341da177e4SLinus Torvalds } 19351da177e4SLinus Torvalds 19367b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net) 19371da177e4SLinus Torvalds { 19381da177e4SLinus Torvalds struct rt6_info *rt; 1939c71099acSThomas Graf struct fib6_table *table; 1940c71099acSThomas Graf 1941c71099acSThomas Graf /* NOTE: Keep consistent with rt6_get_dflt_router */ 19427b4da532SDaniel Lezcano table = fib6_get_table(net, RT6_TABLE_DFLT); 194338308473SDavid S. Miller if (!table) 1944c71099acSThomas Graf return; 19451da177e4SLinus Torvalds 19461da177e4SLinus Torvalds restart: 1947c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 1948d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 19491da177e4SLinus Torvalds if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) { 1950d8d1f30bSChangli Gao dst_hold(&rt->dst); 1951c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 1952e0a1ad73SThomas Graf ip6_del_rt(rt); 19531da177e4SLinus Torvalds goto restart; 19541da177e4SLinus Torvalds } 19551da177e4SLinus Torvalds } 1956c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 19571da177e4SLinus Torvalds } 19581da177e4SLinus Torvalds 19595578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 19605578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 196186872cb5SThomas Graf struct fib6_config *cfg) 196286872cb5SThomas Graf { 196386872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 196486872cb5SThomas Graf 196586872cb5SThomas Graf cfg->fc_table = RT6_TABLE_MAIN; 196686872cb5SThomas Graf cfg->fc_ifindex = rtmsg->rtmsg_ifindex; 196786872cb5SThomas Graf cfg->fc_metric = rtmsg->rtmsg_metric; 196886872cb5SThomas Graf cfg->fc_expires = rtmsg->rtmsg_info; 196986872cb5SThomas Graf cfg->fc_dst_len = rtmsg->rtmsg_dst_len; 197086872cb5SThomas Graf cfg->fc_src_len = rtmsg->rtmsg_src_len; 197186872cb5SThomas Graf cfg->fc_flags = rtmsg->rtmsg_flags; 197286872cb5SThomas Graf 19735578689aSDaniel Lezcano cfg->fc_nlinfo.nl_net = net; 1974f1243c2dSBenjamin Thery 19754e3fd7a0SAlexey Dobriyan cfg->fc_dst = rtmsg->rtmsg_dst; 19764e3fd7a0SAlexey Dobriyan cfg->fc_src = rtmsg->rtmsg_src; 19774e3fd7a0SAlexey Dobriyan cfg->fc_gateway = rtmsg->rtmsg_gateway; 197886872cb5SThomas Graf } 197986872cb5SThomas Graf 19805578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) 19811da177e4SLinus Torvalds { 198286872cb5SThomas Graf struct fib6_config cfg; 19831da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 19841da177e4SLinus Torvalds int err; 19851da177e4SLinus Torvalds 19861da177e4SLinus Torvalds switch(cmd) { 19871da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 19881da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 19891da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 19901da177e4SLinus Torvalds return -EPERM; 19911da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 19921da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 19931da177e4SLinus Torvalds if (err) 19941da177e4SLinus Torvalds return -EFAULT; 19951da177e4SLinus Torvalds 19965578689aSDaniel Lezcano rtmsg_to_fib6_config(net, &rtmsg, &cfg); 199786872cb5SThomas Graf 19981da177e4SLinus Torvalds rtnl_lock(); 19991da177e4SLinus Torvalds switch (cmd) { 20001da177e4SLinus Torvalds case SIOCADDRT: 200186872cb5SThomas Graf err = ip6_route_add(&cfg); 20021da177e4SLinus Torvalds break; 20031da177e4SLinus Torvalds case SIOCDELRT: 200486872cb5SThomas Graf err = ip6_route_del(&cfg); 20051da177e4SLinus Torvalds break; 20061da177e4SLinus Torvalds default: 20071da177e4SLinus Torvalds err = -EINVAL; 20081da177e4SLinus Torvalds } 20091da177e4SLinus Torvalds rtnl_unlock(); 20101da177e4SLinus Torvalds 20111da177e4SLinus Torvalds return err; 20123ff50b79SStephen Hemminger } 20131da177e4SLinus Torvalds 20141da177e4SLinus Torvalds return -EINVAL; 20151da177e4SLinus Torvalds } 20161da177e4SLinus Torvalds 20171da177e4SLinus Torvalds /* 20181da177e4SLinus Torvalds * Drop the packet on the floor 20191da177e4SLinus Torvalds */ 20201da177e4SLinus Torvalds 2021d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 20221da177e4SLinus Torvalds { 2023612f09e8SYOSHIFUJI Hideaki int type; 2024adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 2025612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 2026612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 20270660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 202845bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 20293bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 20303bd653c8SDenis V. Lunev IPSTATS_MIB_INADDRERRORS); 2031612f09e8SYOSHIFUJI Hideaki break; 2032612f09e8SYOSHIFUJI Hideaki } 2033612f09e8SYOSHIFUJI Hideaki /* FALLTHROUGH */ 2034612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 20353bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 20363bd653c8SDenis V. Lunev ipstats_mib_noroutes); 2037612f09e8SYOSHIFUJI Hideaki break; 2038612f09e8SYOSHIFUJI Hideaki } 20393ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 20401da177e4SLinus Torvalds kfree_skb(skb); 20411da177e4SLinus Torvalds return 0; 20421da177e4SLinus Torvalds } 20431da177e4SLinus Torvalds 20449ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 20459ce8ade0SThomas Graf { 2046612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 20479ce8ade0SThomas Graf } 20489ce8ade0SThomas Graf 204920380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb) 20501da177e4SLinus Torvalds { 2051adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 2052612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 20531da177e4SLinus Torvalds } 20541da177e4SLinus Torvalds 20556723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES 20566723ab54SDavid S. Miller 20579ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 20589ce8ade0SThomas Graf { 2059612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 20609ce8ade0SThomas Graf } 20619ce8ade0SThomas Graf 20629ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb) 20639ce8ade0SThomas Graf { 2064adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 2065612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 20669ce8ade0SThomas Graf } 20679ce8ade0SThomas Graf 20686723ab54SDavid S. Miller #endif 20696723ab54SDavid S. Miller 20701da177e4SLinus Torvalds /* 20711da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 20721da177e4SLinus Torvalds */ 20731da177e4SLinus Torvalds 20741da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, 20751da177e4SLinus Torvalds const struct in6_addr *addr, 20768f031519SDavid S. Miller bool anycast) 20771da177e4SLinus Torvalds { 2078c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 20798b96d22dSDavid S. Miller struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev, 0, NULL); 2080f83c7790SDavid S. Miller int err; 20811da177e4SLinus Torvalds 208238308473SDavid S. Miller if (!rt) { 2083f3213831SJoe Perches net_warn_ratelimited("Maximum number of routes reached, consider increasing route/max_size\n"); 20841da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 208540385653SBen Greear } 20861da177e4SLinus Torvalds 20871da177e4SLinus Torvalds in6_dev_hold(idev); 20881da177e4SLinus Torvalds 208911d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 2090d8d1f30bSChangli Gao rt->dst.input = ip6_input; 2091d8d1f30bSChangli Gao rt->dst.output = ip6_output; 20921da177e4SLinus Torvalds rt->rt6i_idev = idev; 2093d8d1f30bSChangli Gao rt->dst.obsolete = -1; 20941da177e4SLinus Torvalds 20951da177e4SLinus Torvalds rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP; 209658c4fb86SYOSHIFUJI Hideaki if (anycast) 209758c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 209858c4fb86SYOSHIFUJI Hideaki else 20991da177e4SLinus Torvalds rt->rt6i_flags |= RTF_LOCAL; 21008ade06c6SDavid S. Miller err = rt6_bind_neighbour(rt, rt->dst.dev); 2101f83c7790SDavid S. Miller if (err) { 2102d8d1f30bSChangli Gao dst_free(&rt->dst); 2103f83c7790SDavid S. Miller return ERR_PTR(err); 21041da177e4SLinus Torvalds } 21051da177e4SLinus Torvalds 21064e3fd7a0SAlexey Dobriyan rt->rt6i_dst.addr = *addr; 21071da177e4SLinus Torvalds rt->rt6i_dst.plen = 128; 21085578689aSDaniel Lezcano rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL); 21091da177e4SLinus Torvalds 2110d8d1f30bSChangli Gao atomic_set(&rt->dst.__refcnt, 1); 21111da177e4SLinus Torvalds 21121da177e4SLinus Torvalds return rt; 21131da177e4SLinus Torvalds } 21141da177e4SLinus Torvalds 2115c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net, 2116c3968a85SDaniel Walter struct rt6_info *rt, 2117b71d1d42SEric Dumazet const struct in6_addr *daddr, 2118c3968a85SDaniel Walter unsigned int prefs, 2119c3968a85SDaniel Walter struct in6_addr *saddr) 2120c3968a85SDaniel Walter { 2121c3968a85SDaniel Walter struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt); 2122c3968a85SDaniel Walter int err = 0; 2123c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) 21244e3fd7a0SAlexey Dobriyan *saddr = rt->rt6i_prefsrc.addr; 2125c3968a85SDaniel Walter else 2126c3968a85SDaniel Walter err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL, 2127c3968a85SDaniel Walter daddr, prefs, saddr); 2128c3968a85SDaniel Walter return err; 2129c3968a85SDaniel Walter } 2130c3968a85SDaniel Walter 2131c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 2132c3968a85SDaniel Walter struct arg_dev_net_ip { 2133c3968a85SDaniel Walter struct net_device *dev; 2134c3968a85SDaniel Walter struct net *net; 2135c3968a85SDaniel Walter struct in6_addr *addr; 2136c3968a85SDaniel Walter }; 2137c3968a85SDaniel Walter 2138c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg) 2139c3968a85SDaniel Walter { 2140c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 2141c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 2142c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 2143c3968a85SDaniel Walter 2144d1918542SDavid S. Miller if (((void *)rt->dst.dev == dev || !dev) && 2145c3968a85SDaniel Walter rt != net->ipv6.ip6_null_entry && 2146c3968a85SDaniel Walter ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) { 2147c3968a85SDaniel Walter /* remove prefsrc entry */ 2148c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 2149c3968a85SDaniel Walter } 2150c3968a85SDaniel Walter return 0; 2151c3968a85SDaniel Walter } 2152c3968a85SDaniel Walter 2153c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 2154c3968a85SDaniel Walter { 2155c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 2156c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 2157c3968a85SDaniel Walter .dev = ifp->idev->dev, 2158c3968a85SDaniel Walter .net = net, 2159c3968a85SDaniel Walter .addr = &ifp->addr, 2160c3968a85SDaniel Walter }; 2161c3968a85SDaniel Walter fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni); 2162c3968a85SDaniel Walter } 2163c3968a85SDaniel Walter 21648ed67789SDaniel Lezcano struct arg_dev_net { 21658ed67789SDaniel Lezcano struct net_device *dev; 21668ed67789SDaniel Lezcano struct net *net; 21678ed67789SDaniel Lezcano }; 21688ed67789SDaniel Lezcano 21691da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg) 21701da177e4SLinus Torvalds { 2171bc3ef660Sstephen hemminger const struct arg_dev_net *adn = arg; 2172bc3ef660Sstephen hemminger const struct net_device *dev = adn->dev; 21738ed67789SDaniel Lezcano 2174d1918542SDavid S. Miller if ((rt->dst.dev == dev || !dev) && 2175c159d30cSDavid S. Miller rt != adn->net->ipv6.ip6_null_entry) 21761da177e4SLinus Torvalds return -1; 2177c159d30cSDavid S. Miller 21781da177e4SLinus Torvalds return 0; 21791da177e4SLinus Torvalds } 21801da177e4SLinus Torvalds 2181f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev) 21821da177e4SLinus Torvalds { 21838ed67789SDaniel Lezcano struct arg_dev_net adn = { 21848ed67789SDaniel Lezcano .dev = dev, 21858ed67789SDaniel Lezcano .net = net, 21868ed67789SDaniel Lezcano }; 21878ed67789SDaniel Lezcano 21888ed67789SDaniel Lezcano fib6_clean_all(net, fib6_ifdown, 0, &adn); 21891e493d19SDavid S. Miller icmp6_clean_all(fib6_ifdown, &adn); 21901da177e4SLinus Torvalds } 21911da177e4SLinus Torvalds 219295c96174SEric Dumazet struct rt6_mtu_change_arg { 21931da177e4SLinus Torvalds struct net_device *dev; 219495c96174SEric Dumazet unsigned int mtu; 21951da177e4SLinus Torvalds }; 21961da177e4SLinus Torvalds 21971da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg) 21981da177e4SLinus Torvalds { 21991da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 22001da177e4SLinus Torvalds struct inet6_dev *idev; 22011da177e4SLinus Torvalds 22021da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 22031da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 22041da177e4SLinus Torvalds We still use this lock to block changes 22051da177e4SLinus Torvalds caused by addrconf/ndisc. 22061da177e4SLinus Torvalds */ 22071da177e4SLinus Torvalds 22081da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 220938308473SDavid S. Miller if (!idev) 22101da177e4SLinus Torvalds return 0; 22111da177e4SLinus Torvalds 22121da177e4SLinus Torvalds /* For administrative MTU increase, there is no way to discover 22131da177e4SLinus Torvalds IPv6 PMTU increase, so PMTU increase should be updated here. 22141da177e4SLinus Torvalds Since RFC 1981 doesn't include administrative MTU increase 22151da177e4SLinus Torvalds update PMTU increase is a MUST. (i.e. jumbo frame) 22161da177e4SLinus Torvalds */ 22171da177e4SLinus Torvalds /* 22181da177e4SLinus Torvalds If new MTU is less than route PMTU, this new MTU will be the 22191da177e4SLinus Torvalds lowest MTU in the path, update the route PMTU to reflect PMTU 22201da177e4SLinus Torvalds decreases; if new MTU is greater than route PMTU, and the 22211da177e4SLinus Torvalds old MTU is the lowest MTU in the path, update the route PMTU 22221da177e4SLinus Torvalds to reflect the increase. In this case if the other nodes' MTU 22231da177e4SLinus Torvalds also have the lowest MTU, TOO BIG MESSAGE will be lead to 22241da177e4SLinus Torvalds PMTU discouvery. 22251da177e4SLinus Torvalds */ 2226d1918542SDavid S. Miller if (rt->dst.dev == arg->dev && 2227d8d1f30bSChangli Gao !dst_metric_locked(&rt->dst, RTAX_MTU) && 2228d8d1f30bSChangli Gao (dst_mtu(&rt->dst) >= arg->mtu || 2229d8d1f30bSChangli Gao (dst_mtu(&rt->dst) < arg->mtu && 2230d8d1f30bSChangli Gao dst_mtu(&rt->dst) == idev->cnf.mtu6))) { 2231defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu); 2232566cfd8fSSimon Arlott } 22331da177e4SLinus Torvalds return 0; 22341da177e4SLinus Torvalds } 22351da177e4SLinus Torvalds 223695c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu) 22371da177e4SLinus Torvalds { 2238c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 2239c71099acSThomas Graf .dev = dev, 2240c71099acSThomas Graf .mtu = mtu, 2241c71099acSThomas Graf }; 22421da177e4SLinus Torvalds 2243c346dca1SYOSHIFUJI Hideaki fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg); 22441da177e4SLinus Torvalds } 22451da177e4SLinus Torvalds 2246ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 22475176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 224886872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 2249ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 225086872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 225186872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 225286872cb5SThomas Graf }; 225386872cb5SThomas Graf 225486872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 225586872cb5SThomas Graf struct fib6_config *cfg) 22561da177e4SLinus Torvalds { 225786872cb5SThomas Graf struct rtmsg *rtm; 225886872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 225986872cb5SThomas Graf int err; 22601da177e4SLinus Torvalds 226186872cb5SThomas Graf err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy); 226286872cb5SThomas Graf if (err < 0) 226386872cb5SThomas Graf goto errout; 22641da177e4SLinus Torvalds 226586872cb5SThomas Graf err = -EINVAL; 226686872cb5SThomas Graf rtm = nlmsg_data(nlh); 226786872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 226886872cb5SThomas Graf 226986872cb5SThomas Graf cfg->fc_table = rtm->rtm_table; 227086872cb5SThomas Graf cfg->fc_dst_len = rtm->rtm_dst_len; 227186872cb5SThomas Graf cfg->fc_src_len = rtm->rtm_src_len; 227286872cb5SThomas Graf cfg->fc_flags = RTF_UP; 227386872cb5SThomas Graf cfg->fc_protocol = rtm->rtm_protocol; 2274*ef2c7d7bSNicolas Dichtel cfg->fc_type = rtm->rtm_type; 227586872cb5SThomas Graf 2276*ef2c7d7bSNicolas Dichtel if (rtm->rtm_type == RTN_UNREACHABLE || 2277*ef2c7d7bSNicolas Dichtel rtm->rtm_type == RTN_BLACKHOLE || 2278*ef2c7d7bSNicolas Dichtel rtm->rtm_type == RTN_PROHIBIT) 227986872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 228086872cb5SThomas Graf 2281ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 2282ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 2283ab79ad14SMaciej Żenczykowski 228486872cb5SThomas Graf cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid; 228586872cb5SThomas Graf cfg->fc_nlinfo.nlh = nlh; 22863b1e0a65SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = sock_net(skb->sk); 228786872cb5SThomas Graf 228886872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 228986872cb5SThomas Graf nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16); 229086872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 22911da177e4SLinus Torvalds } 229286872cb5SThomas Graf 229386872cb5SThomas Graf if (tb[RTA_DST]) { 229486872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 229586872cb5SThomas Graf 229686872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 229786872cb5SThomas Graf goto errout; 229886872cb5SThomas Graf 229986872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 23001da177e4SLinus Torvalds } 230186872cb5SThomas Graf 230286872cb5SThomas Graf if (tb[RTA_SRC]) { 230386872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 230486872cb5SThomas Graf 230586872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 230686872cb5SThomas Graf goto errout; 230786872cb5SThomas Graf 230886872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 23091da177e4SLinus Torvalds } 231086872cb5SThomas Graf 2311c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 2312c3968a85SDaniel Walter nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16); 2313c3968a85SDaniel Walter 231486872cb5SThomas Graf if (tb[RTA_OIF]) 231586872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 231686872cb5SThomas Graf 231786872cb5SThomas Graf if (tb[RTA_PRIORITY]) 231886872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 231986872cb5SThomas Graf 232086872cb5SThomas Graf if (tb[RTA_METRICS]) { 232186872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 232286872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 23231da177e4SLinus Torvalds } 232486872cb5SThomas Graf 232586872cb5SThomas Graf if (tb[RTA_TABLE]) 232686872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 232786872cb5SThomas Graf 232886872cb5SThomas Graf err = 0; 232986872cb5SThomas Graf errout: 233086872cb5SThomas Graf return err; 23311da177e4SLinus Torvalds } 23321da177e4SLinus Torvalds 2333c127ea2cSThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 23341da177e4SLinus Torvalds { 233586872cb5SThomas Graf struct fib6_config cfg; 233686872cb5SThomas Graf int err; 23371da177e4SLinus Torvalds 233886872cb5SThomas Graf err = rtm_to_fib6_config(skb, nlh, &cfg); 233986872cb5SThomas Graf if (err < 0) 234086872cb5SThomas Graf return err; 234186872cb5SThomas Graf 234286872cb5SThomas Graf return ip6_route_del(&cfg); 23431da177e4SLinus Torvalds } 23441da177e4SLinus Torvalds 2345c127ea2cSThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 23461da177e4SLinus Torvalds { 234786872cb5SThomas Graf struct fib6_config cfg; 234886872cb5SThomas Graf int err; 23491da177e4SLinus Torvalds 235086872cb5SThomas Graf err = rtm_to_fib6_config(skb, nlh, &cfg); 235186872cb5SThomas Graf if (err < 0) 235286872cb5SThomas Graf return err; 235386872cb5SThomas Graf 235486872cb5SThomas Graf return ip6_route_add(&cfg); 23551da177e4SLinus Torvalds } 23561da177e4SLinus Torvalds 2357339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void) 2358339bf98fSThomas Graf { 2359339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 2360339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 2361339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 2362339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 2363339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 2364339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 2365339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 2366339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 2367339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 23686a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 2369339bf98fSThomas Graf + nla_total_size(sizeof(struct rta_cacheinfo)); 2370339bf98fSThomas Graf } 2371339bf98fSThomas Graf 2372191cd582SBrian Haley static int rt6_fill_node(struct net *net, 2373191cd582SBrian Haley struct sk_buff *skb, struct rt6_info *rt, 23740d51aa80SJamal Hadi Salim struct in6_addr *dst, struct in6_addr *src, 23750d51aa80SJamal Hadi Salim int iif, int type, u32 pid, u32 seq, 23767bc570c8SYOSHIFUJI Hideaki int prefix, int nowait, unsigned int flags) 23771da177e4SLinus Torvalds { 23781da177e4SLinus Torvalds struct rtmsg *rtm; 23791da177e4SLinus Torvalds struct nlmsghdr *nlh; 2380e3703b3dSThomas Graf long expires; 23819e762a4aSPatrick McHardy u32 table; 2382f2c31e32SEric Dumazet struct neighbour *n; 23831da177e4SLinus Torvalds 23841da177e4SLinus Torvalds if (prefix) { /* user wants prefix routes only */ 23851da177e4SLinus Torvalds if (!(rt->rt6i_flags & RTF_PREFIX_RT)) { 23861da177e4SLinus Torvalds /* success since this is not a prefix route */ 23871da177e4SLinus Torvalds return 1; 23881da177e4SLinus Torvalds } 23891da177e4SLinus Torvalds } 23901da177e4SLinus Torvalds 23912d7202bfSThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags); 239238308473SDavid S. Miller if (!nlh) 239326932566SPatrick McHardy return -EMSGSIZE; 23942d7202bfSThomas Graf 23952d7202bfSThomas Graf rtm = nlmsg_data(nlh); 23961da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 23971da177e4SLinus Torvalds rtm->rtm_dst_len = rt->rt6i_dst.plen; 23981da177e4SLinus Torvalds rtm->rtm_src_len = rt->rt6i_src.plen; 23991da177e4SLinus Torvalds rtm->rtm_tos = 0; 2400c71099acSThomas Graf if (rt->rt6i_table) 24019e762a4aSPatrick McHardy table = rt->rt6i_table->tb6_id; 2402c71099acSThomas Graf else 24039e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 24049e762a4aSPatrick McHardy rtm->rtm_table = table; 2405c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, table)) 2406c78679e8SDavid S. Miller goto nla_put_failure; 2407*ef2c7d7bSNicolas Dichtel if (rt->rt6i_flags & RTF_REJECT) { 2408*ef2c7d7bSNicolas Dichtel switch (rt->dst.error) { 2409*ef2c7d7bSNicolas Dichtel case -EINVAL: 2410*ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_BLACKHOLE; 2411*ef2c7d7bSNicolas Dichtel break; 2412*ef2c7d7bSNicolas Dichtel case -EACCES: 2413*ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_PROHIBIT; 2414*ef2c7d7bSNicolas Dichtel break; 2415*ef2c7d7bSNicolas Dichtel default: 24161da177e4SLinus Torvalds rtm->rtm_type = RTN_UNREACHABLE; 2417*ef2c7d7bSNicolas Dichtel break; 2418*ef2c7d7bSNicolas Dichtel } 2419*ef2c7d7bSNicolas Dichtel } 2420ab79ad14SMaciej Żenczykowski else if (rt->rt6i_flags & RTF_LOCAL) 2421ab79ad14SMaciej Żenczykowski rtm->rtm_type = RTN_LOCAL; 2422d1918542SDavid S. Miller else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK)) 24231da177e4SLinus Torvalds rtm->rtm_type = RTN_LOCAL; 24241da177e4SLinus Torvalds else 24251da177e4SLinus Torvalds rtm->rtm_type = RTN_UNICAST; 24261da177e4SLinus Torvalds rtm->rtm_flags = 0; 24271da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 24281da177e4SLinus Torvalds rtm->rtm_protocol = rt->rt6i_protocol; 24291da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_DYNAMIC) 24301da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_REDIRECT; 2431f0396f60SDenis Ovsienko else if (rt->rt6i_flags & RTF_ADDRCONF) { 2432f0396f60SDenis Ovsienko if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO)) 24331da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_RA; 2434f0396f60SDenis Ovsienko else 2435f0396f60SDenis Ovsienko rtm->rtm_protocol = RTPROT_KERNEL; 2436f0396f60SDenis Ovsienko } 24371da177e4SLinus Torvalds 24381da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) 24391da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 24401da177e4SLinus Torvalds 24411da177e4SLinus Torvalds if (dst) { 2442c78679e8SDavid S. Miller if (nla_put(skb, RTA_DST, 16, dst)) 2443c78679e8SDavid S. Miller goto nla_put_failure; 24441da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 24451da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 2446c78679e8SDavid S. Miller if (nla_put(skb, RTA_DST, 16, &rt->rt6i_dst.addr)) 2447c78679e8SDavid S. Miller goto nla_put_failure; 24481da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 24491da177e4SLinus Torvalds if (src) { 2450c78679e8SDavid S. Miller if (nla_put(skb, RTA_SRC, 16, src)) 2451c78679e8SDavid S. Miller goto nla_put_failure; 24521da177e4SLinus Torvalds rtm->rtm_src_len = 128; 2453c78679e8SDavid S. Miller } else if (rtm->rtm_src_len && 2454c78679e8SDavid S. Miller nla_put(skb, RTA_SRC, 16, &rt->rt6i_src.addr)) 2455c78679e8SDavid S. Miller goto nla_put_failure; 24561da177e4SLinus Torvalds #endif 24577bc570c8SYOSHIFUJI Hideaki if (iif) { 24587bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 24597bc570c8SYOSHIFUJI Hideaki if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) { 24608229efdaSBenjamin Thery int err = ip6mr_get_route(net, skb, rtm, nowait); 24617bc570c8SYOSHIFUJI Hideaki if (err <= 0) { 24627bc570c8SYOSHIFUJI Hideaki if (!nowait) { 24637bc570c8SYOSHIFUJI Hideaki if (err == 0) 24647bc570c8SYOSHIFUJI Hideaki return 0; 24657bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 24667bc570c8SYOSHIFUJI Hideaki } else { 24677bc570c8SYOSHIFUJI Hideaki if (err == -EMSGSIZE) 24687bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 24697bc570c8SYOSHIFUJI Hideaki } 24707bc570c8SYOSHIFUJI Hideaki } 24717bc570c8SYOSHIFUJI Hideaki } else 24727bc570c8SYOSHIFUJI Hideaki #endif 2473c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_IIF, iif)) 2474c78679e8SDavid S. Miller goto nla_put_failure; 24757bc570c8SYOSHIFUJI Hideaki } else if (dst) { 24761da177e4SLinus Torvalds struct in6_addr saddr_buf; 2477c78679e8SDavid S. Miller if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 && 2478c78679e8SDavid S. Miller nla_put(skb, RTA_PREFSRC, 16, &saddr_buf)) 2479c78679e8SDavid S. Miller goto nla_put_failure; 2480c3968a85SDaniel Walter } 2481c3968a85SDaniel Walter 2482c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) { 2483c3968a85SDaniel Walter struct in6_addr saddr_buf; 24844e3fd7a0SAlexey Dobriyan saddr_buf = rt->rt6i_prefsrc.addr; 2485c78679e8SDavid S. Miller if (nla_put(skb, RTA_PREFSRC, 16, &saddr_buf)) 2486c78679e8SDavid S. Miller goto nla_put_failure; 24871da177e4SLinus Torvalds } 24882d7202bfSThomas Graf 2489defb3519SDavid S. Miller if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0) 24902d7202bfSThomas Graf goto nla_put_failure; 24912d7202bfSThomas Graf 2492f2c31e32SEric Dumazet rcu_read_lock(); 249397cac082SDavid S. Miller n = rt->n; 249494f826b8SEric Dumazet if (n) { 249594f826b8SEric Dumazet if (nla_put(skb, RTA_GATEWAY, 16, &n->primary_key) < 0) { 249694f826b8SEric Dumazet rcu_read_unlock(); 249794f826b8SEric Dumazet goto nla_put_failure; 249894f826b8SEric Dumazet } 249994f826b8SEric Dumazet } 2500f2c31e32SEric Dumazet rcu_read_unlock(); 25012d7202bfSThomas Graf 2502c78679e8SDavid S. Miller if (rt->dst.dev && 2503c78679e8SDavid S. Miller nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex)) 2504c78679e8SDavid S. Miller goto nla_put_failure; 2505c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric)) 2506c78679e8SDavid S. Miller goto nla_put_failure; 25078253947eSLi Wei 25088253947eSLi Wei expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0; 250969cdf8f9SYOSHIFUJI Hideaki 251087a50699SDavid S. Miller if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0) 2511e3703b3dSThomas Graf goto nla_put_failure; 25121da177e4SLinus Torvalds 25132d7202bfSThomas Graf return nlmsg_end(skb, nlh); 25142d7202bfSThomas Graf 25152d7202bfSThomas Graf nla_put_failure: 251626932566SPatrick McHardy nlmsg_cancel(skb, nlh); 251726932566SPatrick McHardy return -EMSGSIZE; 25181da177e4SLinus Torvalds } 25191da177e4SLinus Torvalds 25201b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg) 25211da177e4SLinus Torvalds { 25221da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 25231da177e4SLinus Torvalds int prefix; 25241da177e4SLinus Torvalds 25252d7202bfSThomas Graf if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { 25262d7202bfSThomas Graf struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); 25271da177e4SLinus Torvalds prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0; 25281da177e4SLinus Torvalds } else 25291da177e4SLinus Torvalds prefix = 0; 25301da177e4SLinus Torvalds 2531191cd582SBrian Haley return rt6_fill_node(arg->net, 2532191cd582SBrian Haley arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE, 25331da177e4SLinus Torvalds NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq, 25347bc570c8SYOSHIFUJI Hideaki prefix, 0, NLM_F_MULTI); 25351da177e4SLinus Torvalds } 25361da177e4SLinus Torvalds 2537c127ea2cSThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg) 25381da177e4SLinus Torvalds { 25393b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 2540ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 25411da177e4SLinus Torvalds struct rt6_info *rt; 2542ab364a6fSThomas Graf struct sk_buff *skb; 2543ab364a6fSThomas Graf struct rtmsg *rtm; 25444c9483b2SDavid S. Miller struct flowi6 fl6; 254572331bc0SShmulik Ladkani int err, iif = 0, oif = 0; 2546ab364a6fSThomas Graf 2547ab364a6fSThomas Graf err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy); 2548ab364a6fSThomas Graf if (err < 0) 2549ab364a6fSThomas Graf goto errout; 2550ab364a6fSThomas Graf 2551ab364a6fSThomas Graf err = -EINVAL; 25524c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 2553ab364a6fSThomas Graf 2554ab364a6fSThomas Graf if (tb[RTA_SRC]) { 2555ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 2556ab364a6fSThomas Graf goto errout; 2557ab364a6fSThomas Graf 25584e3fd7a0SAlexey Dobriyan fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 2559ab364a6fSThomas Graf } 2560ab364a6fSThomas Graf 2561ab364a6fSThomas Graf if (tb[RTA_DST]) { 2562ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 2563ab364a6fSThomas Graf goto errout; 2564ab364a6fSThomas Graf 25654e3fd7a0SAlexey Dobriyan fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 2566ab364a6fSThomas Graf } 2567ab364a6fSThomas Graf 2568ab364a6fSThomas Graf if (tb[RTA_IIF]) 2569ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 2570ab364a6fSThomas Graf 2571ab364a6fSThomas Graf if (tb[RTA_OIF]) 257272331bc0SShmulik Ladkani oif = nla_get_u32(tb[RTA_OIF]); 2573ab364a6fSThomas Graf 2574ab364a6fSThomas Graf if (iif) { 2575ab364a6fSThomas Graf struct net_device *dev; 257672331bc0SShmulik Ladkani int flags = 0; 257772331bc0SShmulik Ladkani 25785578689aSDaniel Lezcano dev = __dev_get_by_index(net, iif); 2579ab364a6fSThomas Graf if (!dev) { 2580ab364a6fSThomas Graf err = -ENODEV; 2581ab364a6fSThomas Graf goto errout; 2582ab364a6fSThomas Graf } 258372331bc0SShmulik Ladkani 258472331bc0SShmulik Ladkani fl6.flowi6_iif = iif; 258572331bc0SShmulik Ladkani 258672331bc0SShmulik Ladkani if (!ipv6_addr_any(&fl6.saddr)) 258772331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_HAS_SADDR; 258872331bc0SShmulik Ladkani 258972331bc0SShmulik Ladkani rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6, 259072331bc0SShmulik Ladkani flags); 259172331bc0SShmulik Ladkani } else { 259272331bc0SShmulik Ladkani fl6.flowi6_oif = oif; 259372331bc0SShmulik Ladkani 259472331bc0SShmulik Ladkani rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6); 2595ab364a6fSThomas Graf } 25961da177e4SLinus Torvalds 25971da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 259838308473SDavid S. Miller if (!skb) { 25992173bff5SShmulik Ladkani dst_release(&rt->dst); 2600ab364a6fSThomas Graf err = -ENOBUFS; 2601ab364a6fSThomas Graf goto errout; 2602ab364a6fSThomas Graf } 26031da177e4SLinus Torvalds 26041da177e4SLinus Torvalds /* Reserve room for dummy headers, this skb can pass 26051da177e4SLinus Torvalds through good chunk of routing engine. 26061da177e4SLinus Torvalds */ 2607459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 26081da177e4SLinus Torvalds skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr)); 26091da177e4SLinus Torvalds 2610d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 26111da177e4SLinus Torvalds 26124c9483b2SDavid S. Miller err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif, 26131da177e4SLinus Torvalds RTM_NEWROUTE, NETLINK_CB(in_skb).pid, 26147bc570c8SYOSHIFUJI Hideaki nlh->nlmsg_seq, 0, 0, 0); 26151da177e4SLinus Torvalds if (err < 0) { 2616ab364a6fSThomas Graf kfree_skb(skb); 2617ab364a6fSThomas Graf goto errout; 26181da177e4SLinus Torvalds } 26191da177e4SLinus Torvalds 26205578689aSDaniel Lezcano err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid); 2621ab364a6fSThomas Graf errout: 26221da177e4SLinus Torvalds return err; 26231da177e4SLinus Torvalds } 26241da177e4SLinus Torvalds 262586872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info) 26261da177e4SLinus Torvalds { 26271da177e4SLinus Torvalds struct sk_buff *skb; 26285578689aSDaniel Lezcano struct net *net = info->nl_net; 2629528c4cebSDenis V. Lunev u32 seq; 2630528c4cebSDenis V. Lunev int err; 26310d51aa80SJamal Hadi Salim 2632528c4cebSDenis V. Lunev err = -ENOBUFS; 263338308473SDavid S. Miller seq = info->nlh ? info->nlh->nlmsg_seq : 0; 263486872cb5SThomas Graf 2635339bf98fSThomas Graf skb = nlmsg_new(rt6_nlmsg_size(), gfp_any()); 263638308473SDavid S. Miller if (!skb) 263721713ebcSThomas Graf goto errout; 26381da177e4SLinus Torvalds 2639191cd582SBrian Haley err = rt6_fill_node(net, skb, rt, NULL, NULL, 0, 26407bc570c8SYOSHIFUJI Hideaki event, info->pid, seq, 0, 0, 0); 264126932566SPatrick McHardy if (err < 0) { 264226932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 264326932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 264426932566SPatrick McHardy kfree_skb(skb); 264526932566SPatrick McHardy goto errout; 264626932566SPatrick McHardy } 26471ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE, 26485578689aSDaniel Lezcano info->nlh, gfp_any()); 26491ce85fe4SPablo Neira Ayuso return; 265021713ebcSThomas Graf errout: 265121713ebcSThomas Graf if (err < 0) 26525578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 26531da177e4SLinus Torvalds } 26541da177e4SLinus Torvalds 26558ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 26568ed67789SDaniel Lezcano unsigned long event, void *data) 26578ed67789SDaniel Lezcano { 26588ed67789SDaniel Lezcano struct net_device *dev = (struct net_device *)data; 2659c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 26608ed67789SDaniel Lezcano 26618ed67789SDaniel Lezcano if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) { 2662d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 26638ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 26648ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 2665d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 26668ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 2667d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 26688ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 26698ed67789SDaniel Lezcano #endif 26708ed67789SDaniel Lezcano } 26718ed67789SDaniel Lezcano 26728ed67789SDaniel Lezcano return NOTIFY_OK; 26738ed67789SDaniel Lezcano } 26748ed67789SDaniel Lezcano 26751da177e4SLinus Torvalds /* 26761da177e4SLinus Torvalds * /proc 26771da177e4SLinus Torvalds */ 26781da177e4SLinus Torvalds 26791da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 26801da177e4SLinus Torvalds 26811da177e4SLinus Torvalds struct rt6_proc_arg 26821da177e4SLinus Torvalds { 26831da177e4SLinus Torvalds char *buffer; 26841da177e4SLinus Torvalds int offset; 26851da177e4SLinus Torvalds int length; 26861da177e4SLinus Torvalds int skip; 26871da177e4SLinus Torvalds int len; 26881da177e4SLinus Torvalds }; 26891da177e4SLinus Torvalds 26901da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg) 26911da177e4SLinus Torvalds { 269233120b30SAlexey Dobriyan struct seq_file *m = p_arg; 269369cce1d1SDavid S. Miller struct neighbour *n; 26941da177e4SLinus Torvalds 26954b7a4274SHarvey Harrison seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen); 26961da177e4SLinus Torvalds 26971da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 26984b7a4274SHarvey Harrison seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen); 26991da177e4SLinus Torvalds #else 270033120b30SAlexey Dobriyan seq_puts(m, "00000000000000000000000000000000 00 "); 27011da177e4SLinus Torvalds #endif 2702f2c31e32SEric Dumazet rcu_read_lock(); 270397cac082SDavid S. Miller n = rt->n; 270469cce1d1SDavid S. Miller if (n) { 270569cce1d1SDavid S. Miller seq_printf(m, "%pi6", n->primary_key); 27061da177e4SLinus Torvalds } else { 270733120b30SAlexey Dobriyan seq_puts(m, "00000000000000000000000000000000"); 27081da177e4SLinus Torvalds } 2709f2c31e32SEric Dumazet rcu_read_unlock(); 271033120b30SAlexey Dobriyan seq_printf(m, " %08x %08x %08x %08x %8s\n", 2711d8d1f30bSChangli Gao rt->rt6i_metric, atomic_read(&rt->dst.__refcnt), 2712d8d1f30bSChangli Gao rt->dst.__use, rt->rt6i_flags, 2713d1918542SDavid S. Miller rt->dst.dev ? rt->dst.dev->name : ""); 27141da177e4SLinus Torvalds return 0; 27151da177e4SLinus Torvalds } 27161da177e4SLinus Torvalds 271733120b30SAlexey Dobriyan static int ipv6_route_show(struct seq_file *m, void *v) 27181da177e4SLinus Torvalds { 2719f3db4851SDaniel Lezcano struct net *net = (struct net *)m->private; 272032b293a5SJosh Hunt fib6_clean_all_ro(net, rt6_info_route, 0, m); 272133120b30SAlexey Dobriyan return 0; 27221da177e4SLinus Torvalds } 27231da177e4SLinus Torvalds 272433120b30SAlexey Dobriyan static int ipv6_route_open(struct inode *inode, struct file *file) 272533120b30SAlexey Dobriyan { 2726de05c557SPavel Emelyanov return single_open_net(inode, file, ipv6_route_show); 2727f3db4851SDaniel Lezcano } 2728f3db4851SDaniel Lezcano 272933120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = { 273033120b30SAlexey Dobriyan .owner = THIS_MODULE, 273133120b30SAlexey Dobriyan .open = ipv6_route_open, 273233120b30SAlexey Dobriyan .read = seq_read, 273333120b30SAlexey Dobriyan .llseek = seq_lseek, 2734b6fcbdb4SPavel Emelyanov .release = single_release_net, 273533120b30SAlexey Dobriyan }; 273633120b30SAlexey Dobriyan 27371da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 27381da177e4SLinus Torvalds { 273969ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 27401da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 274169ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 274269ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 274369ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_alloc, 274469ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 274569ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 2746fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 274769ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 27481da177e4SLinus Torvalds 27491da177e4SLinus Torvalds return 0; 27501da177e4SLinus Torvalds } 27511da177e4SLinus Torvalds 27521da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file) 27531da177e4SLinus Torvalds { 2754de05c557SPavel Emelyanov return single_open_net(inode, file, rt6_stats_seq_show); 275569ddb805SDaniel Lezcano } 275669ddb805SDaniel Lezcano 27579a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = { 27581da177e4SLinus Torvalds .owner = THIS_MODULE, 27591da177e4SLinus Torvalds .open = rt6_stats_seq_open, 27601da177e4SLinus Torvalds .read = seq_read, 27611da177e4SLinus Torvalds .llseek = seq_lseek, 2762b6fcbdb4SPavel Emelyanov .release = single_release_net, 27631da177e4SLinus Torvalds }; 27641da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 27651da177e4SLinus Torvalds 27661da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 27671da177e4SLinus Torvalds 27681da177e4SLinus Torvalds static 27698d65af78SAlexey Dobriyan int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write, 27701da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 27711da177e4SLinus Torvalds { 2772c486da34SLucian Adrian Grijincu struct net *net; 2773c486da34SLucian Adrian Grijincu int delay; 2774c486da34SLucian Adrian Grijincu if (!write) 2775c486da34SLucian Adrian Grijincu return -EINVAL; 2776c486da34SLucian Adrian Grijincu 2777c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 2778c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 27798d65af78SAlexey Dobriyan proc_dointvec(ctl, write, buffer, lenp, ppos); 27805b7c931dSDaniel Lezcano fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net); 27811da177e4SLinus Torvalds return 0; 27821da177e4SLinus Torvalds } 27831da177e4SLinus Torvalds 2784760f2d01SDaniel Lezcano ctl_table ipv6_route_table_template[] = { 27851da177e4SLinus Torvalds { 27861da177e4SLinus Torvalds .procname = "flush", 27874990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 27881da177e4SLinus Torvalds .maxlen = sizeof(int), 278989c8b3a1SDave Jones .mode = 0200, 27906d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 27911da177e4SLinus Torvalds }, 27921da177e4SLinus Torvalds { 27931da177e4SLinus Torvalds .procname = "gc_thresh", 27949a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 27951da177e4SLinus Torvalds .maxlen = sizeof(int), 27961da177e4SLinus Torvalds .mode = 0644, 27976d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 27981da177e4SLinus Torvalds }, 27991da177e4SLinus Torvalds { 28001da177e4SLinus Torvalds .procname = "max_size", 28014990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 28021da177e4SLinus Torvalds .maxlen = sizeof(int), 28031da177e4SLinus Torvalds .mode = 0644, 28046d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 28051da177e4SLinus Torvalds }, 28061da177e4SLinus Torvalds { 28071da177e4SLinus Torvalds .procname = "gc_min_interval", 28084990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 28091da177e4SLinus Torvalds .maxlen = sizeof(int), 28101da177e4SLinus Torvalds .mode = 0644, 28116d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 28121da177e4SLinus Torvalds }, 28131da177e4SLinus Torvalds { 28141da177e4SLinus Torvalds .procname = "gc_timeout", 28154990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 28161da177e4SLinus Torvalds .maxlen = sizeof(int), 28171da177e4SLinus Torvalds .mode = 0644, 28186d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 28191da177e4SLinus Torvalds }, 28201da177e4SLinus Torvalds { 28211da177e4SLinus Torvalds .procname = "gc_interval", 28224990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 28231da177e4SLinus Torvalds .maxlen = sizeof(int), 28241da177e4SLinus Torvalds .mode = 0644, 28256d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 28261da177e4SLinus Torvalds }, 28271da177e4SLinus Torvalds { 28281da177e4SLinus Torvalds .procname = "gc_elasticity", 28294990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 28301da177e4SLinus Torvalds .maxlen = sizeof(int), 28311da177e4SLinus Torvalds .mode = 0644, 2832f3d3f616SMin Zhang .proc_handler = proc_dointvec, 28331da177e4SLinus Torvalds }, 28341da177e4SLinus Torvalds { 28351da177e4SLinus Torvalds .procname = "mtu_expires", 28364990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 28371da177e4SLinus Torvalds .maxlen = sizeof(int), 28381da177e4SLinus Torvalds .mode = 0644, 28396d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 28401da177e4SLinus Torvalds }, 28411da177e4SLinus Torvalds { 28421da177e4SLinus Torvalds .procname = "min_adv_mss", 28434990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 28441da177e4SLinus Torvalds .maxlen = sizeof(int), 28451da177e4SLinus Torvalds .mode = 0644, 2846f3d3f616SMin Zhang .proc_handler = proc_dointvec, 28471da177e4SLinus Torvalds }, 28481da177e4SLinus Torvalds { 28491da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 28504990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 28511da177e4SLinus Torvalds .maxlen = sizeof(int), 28521da177e4SLinus Torvalds .mode = 0644, 28536d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 28541da177e4SLinus Torvalds }, 2855f8572d8fSEric W. Biederman { } 28561da177e4SLinus Torvalds }; 28571da177e4SLinus Torvalds 28582c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 2859760f2d01SDaniel Lezcano { 2860760f2d01SDaniel Lezcano struct ctl_table *table; 2861760f2d01SDaniel Lezcano 2862760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 2863760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 2864760f2d01SDaniel Lezcano GFP_KERNEL); 28655ee09105SYOSHIFUJI Hideaki 28665ee09105SYOSHIFUJI Hideaki if (table) { 28675ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 2868c486da34SLucian Adrian Grijincu table[0].extra1 = net; 286986393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 28705ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 28715ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 28725ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 28735ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 28745ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 28755ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 28765ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 28779c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 28785ee09105SYOSHIFUJI Hideaki } 28795ee09105SYOSHIFUJI Hideaki 2880760f2d01SDaniel Lezcano return table; 2881760f2d01SDaniel Lezcano } 28821da177e4SLinus Torvalds #endif 28831da177e4SLinus Torvalds 28842c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 2885cdb18761SDaniel Lezcano { 2886633d424bSPavel Emelyanov int ret = -ENOMEM; 28878ed67789SDaniel Lezcano 288886393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 288986393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 2890f2fc6a54SBenjamin Thery 2891fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 2892fc66f95cSEric Dumazet goto out_ip6_dst_ops; 2893fc66f95cSEric Dumazet 28948ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 28958ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 28968ed67789SDaniel Lezcano GFP_KERNEL); 28978ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 2898fc66f95cSEric Dumazet goto out_ip6_dst_entries; 2899d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.path = 29008ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_null_entry; 2901d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 290262fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 290362fa8a84SDavid S. Miller ip6_template_metrics, true); 29048ed67789SDaniel Lezcano 29058ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 29068ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 29078ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 29088ed67789SDaniel Lezcano GFP_KERNEL); 290968fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 291068fffc67SPeter Zijlstra goto out_ip6_null_entry; 2911d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.path = 29128ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_prohibit_entry; 2913d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 291462fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 291562fa8a84SDavid S. Miller ip6_template_metrics, true); 29168ed67789SDaniel Lezcano 29178ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 29188ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 29198ed67789SDaniel Lezcano GFP_KERNEL); 292068fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 292168fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 2922d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.path = 29238ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_blk_hole_entry; 2924d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 292562fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 292662fa8a84SDavid S. Miller ip6_template_metrics, true); 29278ed67789SDaniel Lezcano #endif 29288ed67789SDaniel Lezcano 2929b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 2930b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 2931b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 2932b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 2933b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 2934b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 2935b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 2936b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 2937b339a47cSPeter Zijlstra 29386891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 29396891a346SBenjamin Thery 29408ed67789SDaniel Lezcano ret = 0; 29418ed67789SDaniel Lezcano out: 29428ed67789SDaniel Lezcano return ret; 2943f2fc6a54SBenjamin Thery 294468fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 294568fffc67SPeter Zijlstra out_ip6_prohibit_entry: 294668fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 294768fffc67SPeter Zijlstra out_ip6_null_entry: 294868fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 294968fffc67SPeter Zijlstra #endif 2950fc66f95cSEric Dumazet out_ip6_dst_entries: 2951fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 2952f2fc6a54SBenjamin Thery out_ip6_dst_ops: 2953f2fc6a54SBenjamin Thery goto out; 2954cdb18761SDaniel Lezcano } 2955cdb18761SDaniel Lezcano 29562c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 2957cdb18761SDaniel Lezcano { 29588ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 29598ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 29608ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 29618ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 29628ed67789SDaniel Lezcano #endif 296341bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 2964cdb18761SDaniel Lezcano } 2965cdb18761SDaniel Lezcano 2966d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net) 2967d189634eSThomas Graf { 2968d189634eSThomas Graf #ifdef CONFIG_PROC_FS 2969d189634eSThomas Graf proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops); 2970d189634eSThomas Graf proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops); 2971d189634eSThomas Graf #endif 2972d189634eSThomas Graf return 0; 2973d189634eSThomas Graf } 2974d189634eSThomas Graf 2975d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net) 2976d189634eSThomas Graf { 2977d189634eSThomas Graf #ifdef CONFIG_PROC_FS 2978d189634eSThomas Graf proc_net_remove(net, "ipv6_route"); 2979d189634eSThomas Graf proc_net_remove(net, "rt6_stats"); 2980d189634eSThomas Graf #endif 2981d189634eSThomas Graf } 2982d189634eSThomas Graf 2983cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 2984cdb18761SDaniel Lezcano .init = ip6_route_net_init, 2985cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 2986cdb18761SDaniel Lezcano }; 2987cdb18761SDaniel Lezcano 2988c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net) 2989c3426b47SDavid S. Miller { 2990c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 2991c3426b47SDavid S. Miller 2992c3426b47SDavid S. Miller if (!bp) 2993c3426b47SDavid S. Miller return -ENOMEM; 2994c3426b47SDavid S. Miller inet_peer_base_init(bp); 2995c3426b47SDavid S. Miller net->ipv6.peers = bp; 2996c3426b47SDavid S. Miller return 0; 2997c3426b47SDavid S. Miller } 2998c3426b47SDavid S. Miller 2999c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net) 3000c3426b47SDavid S. Miller { 3001c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv6.peers; 3002c3426b47SDavid S. Miller 3003c3426b47SDavid S. Miller net->ipv6.peers = NULL; 300456a6b248SDavid S. Miller inetpeer_invalidate_tree(bp); 3005c3426b47SDavid S. Miller kfree(bp); 3006c3426b47SDavid S. Miller } 3007c3426b47SDavid S. Miller 30082b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = { 3009c3426b47SDavid S. Miller .init = ipv6_inetpeer_init, 3010c3426b47SDavid S. Miller .exit = ipv6_inetpeer_exit, 3011c3426b47SDavid S. Miller }; 3012c3426b47SDavid S. Miller 3013d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = { 3014d189634eSThomas Graf .init = ip6_route_net_init_late, 3015d189634eSThomas Graf .exit = ip6_route_net_exit_late, 3016d189634eSThomas Graf }; 3017d189634eSThomas Graf 30188ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 30198ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 30208ed67789SDaniel Lezcano .priority = 0, 30218ed67789SDaniel Lezcano }; 30228ed67789SDaniel Lezcano 3023433d49c3SDaniel Lezcano int __init ip6_route_init(void) 30241da177e4SLinus Torvalds { 3025433d49c3SDaniel Lezcano int ret; 3026433d49c3SDaniel Lezcano 30279a7ec3a9SDaniel Lezcano ret = -ENOMEM; 30289a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 30299a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 30309a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 30319a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 3032c19a28e1SFernando Carrijo goto out; 303314e50e57SDavid S. Miller 3034fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 30358ed67789SDaniel Lezcano if (ret) 3036bdb3289fSDaniel Lezcano goto out_kmem_cache; 3037bdb3289fSDaniel Lezcano 3038c3426b47SDavid S. Miller ret = register_pernet_subsys(&ipv6_inetpeer_ops); 3039c3426b47SDavid S. Miller if (ret) 3040e8803b6cSDavid S. Miller goto out_dst_entries; 30412a0c451aSThomas Graf 30427e52b33bSDavid S. Miller ret = register_pernet_subsys(&ip6_route_net_ops); 30437e52b33bSDavid S. Miller if (ret) 30447e52b33bSDavid S. Miller goto out_register_inetpeer; 3045c3426b47SDavid S. Miller 30465dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 30475dc121e9SArnaud Ebalard 30488ed67789SDaniel Lezcano /* Registering of the loopback is done before this portion of code, 30498ed67789SDaniel Lezcano * the loopback reference in rt6_info will not be taken, do it 30508ed67789SDaniel Lezcano * manually for init_net */ 3051d8d1f30bSChangli Gao init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 30528ed67789SDaniel Lezcano init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 3053bdb3289fSDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 3054d8d1f30bSChangli Gao init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 30558ed67789SDaniel Lezcano init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 3056d8d1f30bSChangli Gao init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 30578ed67789SDaniel Lezcano init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 3058bdb3289fSDaniel Lezcano #endif 3059e8803b6cSDavid S. Miller ret = fib6_init(); 3060433d49c3SDaniel Lezcano if (ret) 30618ed67789SDaniel Lezcano goto out_register_subsys; 3062433d49c3SDaniel Lezcano 3063433d49c3SDaniel Lezcano ret = xfrm6_init(); 3064433d49c3SDaniel Lezcano if (ret) 3065e8803b6cSDavid S. Miller goto out_fib6_init; 3066c35b7e72SDaniel Lezcano 3067433d49c3SDaniel Lezcano ret = fib6_rules_init(); 3068433d49c3SDaniel Lezcano if (ret) 3069433d49c3SDaniel Lezcano goto xfrm6_init; 30707e5449c2SDaniel Lezcano 3071d189634eSThomas Graf ret = register_pernet_subsys(&ip6_route_net_late_ops); 3072d189634eSThomas Graf if (ret) 3073d189634eSThomas Graf goto fib6_rules_init; 3074d189634eSThomas Graf 3075433d49c3SDaniel Lezcano ret = -ENOBUFS; 3076c7ac8679SGreg Rose if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) || 3077c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) || 3078c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL)) 3079d189634eSThomas Graf goto out_register_late_subsys; 3080433d49c3SDaniel Lezcano 30818ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 3082cdb18761SDaniel Lezcano if (ret) 3083d189634eSThomas Graf goto out_register_late_subsys; 30848ed67789SDaniel Lezcano 3085433d49c3SDaniel Lezcano out: 3086433d49c3SDaniel Lezcano return ret; 3087433d49c3SDaniel Lezcano 3088d189634eSThomas Graf out_register_late_subsys: 3089d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 3090433d49c3SDaniel Lezcano fib6_rules_init: 3091433d49c3SDaniel Lezcano fib6_rules_cleanup(); 3092433d49c3SDaniel Lezcano xfrm6_init: 3093433d49c3SDaniel Lezcano xfrm6_fini(); 30942a0c451aSThomas Graf out_fib6_init: 30952a0c451aSThomas Graf fib6_gc_cleanup(); 30968ed67789SDaniel Lezcano out_register_subsys: 30978ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 30987e52b33bSDavid S. Miller out_register_inetpeer: 30997e52b33bSDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_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); 3110d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 3111101367c2SThomas Graf fib6_rules_cleanup(); 31121da177e4SLinus Torvalds xfrm6_fini(); 31131da177e4SLinus Torvalds fib6_gc_cleanup(); 3114c3426b47SDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 31158ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 311641bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 3117f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 31181da177e4SLinus Torvalds } 3119