11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Linux INET6 implementation 31da177e4SLinus Torvalds * FIB front-end. 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Authors: 61da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 91da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 101da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 111da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 121da177e4SLinus Torvalds */ 131da177e4SLinus Torvalds 141da177e4SLinus Torvalds /* Changes: 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI 171da177e4SLinus Torvalds * reworked default router selection. 181da177e4SLinus Torvalds * - respect outgoing interface 191da177e4SLinus Torvalds * - select from (probably) reachable routers (i.e. 201da177e4SLinus Torvalds * routers in REACHABLE, STALE, DELAY or PROBE states). 211da177e4SLinus Torvalds * - always select the same router if it is (probably) 221da177e4SLinus Torvalds * reachable. otherwise, round-robin the list. 23c0bece9fSYOSHIFUJI Hideaki * Ville Nuorvala 24c0bece9fSYOSHIFUJI Hideaki * Fixed routing subtrees. 251da177e4SLinus Torvalds */ 261da177e4SLinus Torvalds 27f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt 28f3213831SJoe Perches 294fc268d2SRandy Dunlap #include <linux/capability.h> 301da177e4SLinus Torvalds #include <linux/errno.h> 31bc3b2d7fSPaul Gortmaker #include <linux/export.h> 321da177e4SLinus Torvalds #include <linux/types.h> 331da177e4SLinus Torvalds #include <linux/times.h> 341da177e4SLinus Torvalds #include <linux/socket.h> 351da177e4SLinus Torvalds #include <linux/sockios.h> 361da177e4SLinus Torvalds #include <linux/net.h> 371da177e4SLinus Torvalds #include <linux/route.h> 381da177e4SLinus Torvalds #include <linux/netdevice.h> 391da177e4SLinus Torvalds #include <linux/in6.h> 407bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h> 411da177e4SLinus Torvalds #include <linux/init.h> 421da177e4SLinus Torvalds #include <linux/if_arp.h> 431da177e4SLinus Torvalds #include <linux/proc_fs.h> 441da177e4SLinus Torvalds #include <linux/seq_file.h> 455b7c931dSDaniel Lezcano #include <linux/nsproxy.h> 465a0e3ad6STejun Heo #include <linux/slab.h> 47457c4cbcSEric W. Biederman #include <net/net_namespace.h> 481da177e4SLinus Torvalds #include <net/snmp.h> 491da177e4SLinus Torvalds #include <net/ipv6.h> 501da177e4SLinus Torvalds #include <net/ip6_fib.h> 511da177e4SLinus Torvalds #include <net/ip6_route.h> 521da177e4SLinus Torvalds #include <net/ndisc.h> 531da177e4SLinus Torvalds #include <net/addrconf.h> 541da177e4SLinus Torvalds #include <net/tcp.h> 551da177e4SLinus Torvalds #include <linux/rtnetlink.h> 561da177e4SLinus Torvalds #include <net/dst.h> 571da177e4SLinus Torvalds #include <net/xfrm.h> 588d71740cSTom Tucker #include <net/netevent.h> 5921713ebcSThomas Graf #include <net/netlink.h> 6051ebd318SNicolas Dichtel #include <net/nexthop.h> 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds #include <asm/uaccess.h> 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 651da177e4SLinus Torvalds #include <linux/sysctl.h> 661da177e4SLinus Torvalds #endif 671da177e4SLinus Torvalds 681716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort, 6921efcfa0SEric Dumazet const struct in6_addr *dest); 701da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie); 710dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst); 72ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst); 731da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *); 741da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *); 751da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *, 761da177e4SLinus Torvalds struct net_device *dev, int how); 77569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops); 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds static int ip6_pkt_discard(struct sk_buff *skb); 801da177e4SLinus Torvalds static int ip6_pkt_discard_out(struct sk_buff *skb); 811da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb); 826700c270SDavid S. Miller static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 836700c270SDavid S. Miller struct sk_buff *skb, u32 mtu); 846700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, 856700c270SDavid S. Miller struct sk_buff *skb); 861da177e4SLinus Torvalds 8770ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 88efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 89b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 90b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex, 9195c96174SEric Dumazet unsigned int pref); 92efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 93b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 94b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex); 9570ceb4f5SYOSHIFUJI Hideaki #endif 9670ceb4f5SYOSHIFUJI Hideaki 9706582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old) 9806582540SDavid S. Miller { 9906582540SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 10006582540SDavid S. Miller struct inet_peer *peer; 10106582540SDavid S. Miller u32 *p = NULL; 10206582540SDavid S. Miller 1038e2ec639SYan, Zheng if (!(rt->dst.flags & DST_HOST)) 1048e2ec639SYan, Zheng return NULL; 1058e2ec639SYan, Zheng 106fbfe95a4SDavid S. Miller peer = rt6_get_peer_create(rt); 10706582540SDavid S. Miller if (peer) { 10806582540SDavid S. Miller u32 *old_p = __DST_METRICS_PTR(old); 10906582540SDavid S. Miller unsigned long prev, new; 11006582540SDavid S. Miller 11106582540SDavid S. Miller p = peer->metrics; 11206582540SDavid S. Miller if (inet_metrics_new(peer)) 11306582540SDavid S. Miller memcpy(p, old_p, sizeof(u32) * RTAX_MAX); 11406582540SDavid S. Miller 11506582540SDavid S. Miller new = (unsigned long) p; 11606582540SDavid S. Miller prev = cmpxchg(&dst->_metrics, old, new); 11706582540SDavid S. Miller 11806582540SDavid S. Miller if (prev != old) { 11906582540SDavid S. Miller p = __DST_METRICS_PTR(prev); 12006582540SDavid S. Miller if (prev & DST_METRICS_READ_ONLY) 12106582540SDavid S. Miller p = NULL; 12206582540SDavid S. Miller } 12306582540SDavid S. Miller } 12406582540SDavid S. Miller return p; 12506582540SDavid S. Miller } 12606582540SDavid S. Miller 127f894cbf8SDavid S. Miller static inline const void *choose_neigh_daddr(struct rt6_info *rt, 128f894cbf8SDavid S. Miller struct sk_buff *skb, 129f894cbf8SDavid S. Miller const void *daddr) 13039232973SDavid S. Miller { 13139232973SDavid S. Miller struct in6_addr *p = &rt->rt6i_gateway; 13239232973SDavid S. Miller 133a7563f34SDavid S. Miller if (!ipv6_addr_any(p)) 13439232973SDavid S. Miller return (const void *) p; 135f894cbf8SDavid S. Miller else if (skb) 136f894cbf8SDavid S. Miller return &ipv6_hdr(skb)->daddr; 13739232973SDavid S. Miller return daddr; 13839232973SDavid S. Miller } 13939232973SDavid S. Miller 140f894cbf8SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst, 141f894cbf8SDavid S. Miller struct sk_buff *skb, 142f894cbf8SDavid S. Miller const void *daddr) 143d3aaeb38SDavid S. Miller { 14439232973SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 14539232973SDavid S. Miller struct neighbour *n; 14639232973SDavid S. Miller 147f894cbf8SDavid S. Miller daddr = choose_neigh_daddr(rt, skb, daddr); 1488e022ee6SYOSHIFUJI Hideaki / 吉藤英明 n = __ipv6_neigh_lookup(dst->dev, daddr); 149f83c7790SDavid S. Miller if (n) 150f83c7790SDavid S. Miller return n; 151f83c7790SDavid S. Miller return neigh_create(&nd_tbl, daddr, dst->dev); 152f83c7790SDavid S. Miller } 153f83c7790SDavid S. Miller 1549a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = { 1551da177e4SLinus Torvalds .family = AF_INET6, 15609640e63SHarvey Harrison .protocol = cpu_to_be16(ETH_P_IPV6), 1571da177e4SLinus Torvalds .gc = ip6_dst_gc, 1581da177e4SLinus Torvalds .gc_thresh = 1024, 1591da177e4SLinus Torvalds .check = ip6_dst_check, 1600dbaee3bSDavid S. Miller .default_advmss = ip6_default_advmss, 161ebb762f2SSteffen Klassert .mtu = ip6_mtu, 16206582540SDavid S. Miller .cow_metrics = ipv6_cow_metrics, 1631da177e4SLinus Torvalds .destroy = ip6_dst_destroy, 1641da177e4SLinus Torvalds .ifdown = ip6_dst_ifdown, 1651da177e4SLinus Torvalds .negative_advice = ip6_negative_advice, 1661da177e4SLinus Torvalds .link_failure = ip6_link_failure, 1671da177e4SLinus Torvalds .update_pmtu = ip6_rt_update_pmtu, 1686e157b6aSDavid S. Miller .redirect = rt6_do_redirect, 1691ac06e03SHerbert Xu .local_out = __ip6_local_out, 170d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 1711da177e4SLinus Torvalds }; 1721da177e4SLinus Torvalds 173ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst) 174ec831ea7SRoland Dreier { 175618f9bc7SSteffen Klassert unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); 176618f9bc7SSteffen Klassert 177618f9bc7SSteffen Klassert return mtu ? : dst->dev->mtu; 178ec831ea7SRoland Dreier } 179ec831ea7SRoland Dreier 1806700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk, 1816700c270SDavid S. Miller struct sk_buff *skb, u32 mtu) 18214e50e57SDavid S. Miller { 18314e50e57SDavid S. Miller } 18414e50e57SDavid S. Miller 1856700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk, 1866700c270SDavid S. Miller struct sk_buff *skb) 187b587ee3bSDavid S. Miller { 188b587ee3bSDavid S. Miller } 189b587ee3bSDavid S. Miller 1900972ddb2SHeld Bernhard static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst, 1910972ddb2SHeld Bernhard unsigned long old) 1920972ddb2SHeld Bernhard { 1930972ddb2SHeld Bernhard return NULL; 1940972ddb2SHeld Bernhard } 1950972ddb2SHeld Bernhard 19614e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = { 19714e50e57SDavid S. Miller .family = AF_INET6, 19809640e63SHarvey Harrison .protocol = cpu_to_be16(ETH_P_IPV6), 19914e50e57SDavid S. Miller .destroy = ip6_dst_destroy, 20014e50e57SDavid S. Miller .check = ip6_dst_check, 201ebb762f2SSteffen Klassert .mtu = ip6_blackhole_mtu, 202214f45c9SEric Dumazet .default_advmss = ip6_default_advmss, 20314e50e57SDavid S. Miller .update_pmtu = ip6_rt_blackhole_update_pmtu, 204b587ee3bSDavid S. Miller .redirect = ip6_rt_blackhole_redirect, 2050972ddb2SHeld Bernhard .cow_metrics = ip6_rt_blackhole_cow_metrics, 206d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 20714e50e57SDavid S. Miller }; 20814e50e57SDavid S. Miller 20962fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = { 21014edd87dSLi RongQing [RTAX_HOPLIMIT - 1] = 0, 21162fa8a84SDavid S. Miller }; 21262fa8a84SDavid S. Miller 213fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = { 2141da177e4SLinus Torvalds .dst = { 2151da177e4SLinus Torvalds .__refcnt = ATOMIC_INIT(1), 2161da177e4SLinus Torvalds .__use = 1, 2172c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 2181da177e4SLinus Torvalds .error = -ENETUNREACH, 2191da177e4SLinus Torvalds .input = ip6_pkt_discard, 2201da177e4SLinus Torvalds .output = ip6_pkt_discard_out, 2211da177e4SLinus Torvalds }, 2221da177e4SLinus Torvalds .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 2234f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 2241da177e4SLinus Torvalds .rt6i_metric = ~(u32) 0, 2251da177e4SLinus Torvalds .rt6i_ref = ATOMIC_INIT(1), 2261da177e4SLinus Torvalds }; 2271da177e4SLinus Torvalds 228101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES 229101367c2SThomas Graf 2306723ab54SDavid S. Miller static int ip6_pkt_prohibit(struct sk_buff *skb); 2316723ab54SDavid S. Miller static int ip6_pkt_prohibit_out(struct sk_buff *skb); 2326723ab54SDavid S. Miller 233fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = { 234101367c2SThomas Graf .dst = { 235101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 236101367c2SThomas Graf .__use = 1, 2372c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 238101367c2SThomas Graf .error = -EACCES, 2399ce8ade0SThomas Graf .input = ip6_pkt_prohibit, 2409ce8ade0SThomas Graf .output = ip6_pkt_prohibit_out, 241101367c2SThomas Graf }, 242101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 2434f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 244101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 245101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 246101367c2SThomas Graf }; 247101367c2SThomas Graf 248fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = { 249101367c2SThomas Graf .dst = { 250101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 251101367c2SThomas Graf .__use = 1, 2522c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 253101367c2SThomas Graf .error = -EINVAL, 254352e512cSHerbert Xu .input = dst_discard, 255352e512cSHerbert Xu .output = dst_discard, 256101367c2SThomas Graf }, 257101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 2584f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 259101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 260101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 261101367c2SThomas Graf }; 262101367c2SThomas Graf 263101367c2SThomas Graf #endif 264101367c2SThomas Graf 2651da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */ 26697bab73fSDavid S. Miller static inline struct rt6_info *ip6_dst_alloc(struct net *net, 267957c665fSDavid S. Miller struct net_device *dev, 2688b96d22dSDavid S. Miller int flags, 2698b96d22dSDavid S. Miller struct fib6_table *table) 2701da177e4SLinus Torvalds { 27197bab73fSDavid S. Miller struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev, 2726f3118b5SNicolas Dichtel 0, DST_OBSOLETE_FORCE_CHK, flags); 273cf911662SDavid S. Miller 27497bab73fSDavid S. Miller if (rt) { 2758104891bSSteffen Klassert struct dst_entry *dst = &rt->dst; 2768104891bSSteffen Klassert 2778104891bSSteffen Klassert memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst)); 2788b96d22dSDavid S. Miller rt6_init_peer(rt, table ? &table->tb6_peers : net->ipv6.peers); 2796f3118b5SNicolas Dichtel rt->rt6i_genid = rt_genid(net); 28051ebd318SNicolas Dichtel INIT_LIST_HEAD(&rt->rt6i_siblings); 28151ebd318SNicolas Dichtel rt->rt6i_nsiblings = 0; 28297bab73fSDavid S. Miller } 283cf911662SDavid S. Miller return rt; 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst) 2871da177e4SLinus Torvalds { 2881da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 2891da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 2901da177e4SLinus Torvalds 2918e2ec639SYan, Zheng if (!(rt->dst.flags & DST_HOST)) 2928e2ec639SYan, Zheng dst_destroy_metrics_generic(dst); 2938e2ec639SYan, Zheng 29438308473SDavid S. Miller if (idev) { 2951da177e4SLinus Torvalds rt->rt6i_idev = NULL; 2961da177e4SLinus Torvalds in6_dev_put(idev); 2971da177e4SLinus Torvalds } 2981716a961SGao feng 2991716a961SGao feng if (!(rt->rt6i_flags & RTF_EXPIRES) && dst->from) 3001716a961SGao feng dst_release(dst->from); 3011716a961SGao feng 30297bab73fSDavid S. Miller if (rt6_has_peer(rt)) { 30397bab73fSDavid S. Miller struct inet_peer *peer = rt6_peer_ptr(rt); 304b3419363SDavid S. Miller inet_putpeer(peer); 305b3419363SDavid S. Miller } 306b3419363SDavid S. Miller } 307b3419363SDavid S. Miller 308b3419363SDavid S. Miller void rt6_bind_peer(struct rt6_info *rt, int create) 309b3419363SDavid S. Miller { 31097bab73fSDavid S. Miller struct inet_peer_base *base; 311b3419363SDavid S. Miller struct inet_peer *peer; 312b3419363SDavid S. Miller 31397bab73fSDavid S. Miller base = inetpeer_base_ptr(rt->_rt6i_peer); 31497bab73fSDavid S. Miller if (!base) 31597bab73fSDavid S. Miller return; 31697bab73fSDavid S. Miller 31797bab73fSDavid S. Miller peer = inet_getpeer_v6(base, &rt->rt6i_dst.addr, create); 3187b34ca2aSDavid S. Miller if (peer) { 31997bab73fSDavid S. Miller if (!rt6_set_peer(rt, peer)) 320b3419363SDavid S. Miller inet_putpeer(peer); 3211da177e4SLinus Torvalds } 3227b34ca2aSDavid S. Miller } 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 3251da177e4SLinus Torvalds int how) 3261da177e4SLinus Torvalds { 3271da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 3281da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 3295a3e55d6SDenis V. Lunev struct net_device *loopback_dev = 330c346dca1SYOSHIFUJI Hideaki dev_net(dev)->loopback_dev; 3311da177e4SLinus Torvalds 33297cac082SDavid S. Miller if (dev != loopback_dev) { 33397cac082SDavid S. Miller if (idev && idev->dev == dev) { 3345a3e55d6SDenis V. Lunev struct inet6_dev *loopback_idev = 3355a3e55d6SDenis V. Lunev in6_dev_get(loopback_dev); 33638308473SDavid S. Miller if (loopback_idev) { 3371da177e4SLinus Torvalds rt->rt6i_idev = loopback_idev; 3381da177e4SLinus Torvalds in6_dev_put(idev); 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds } 34197cac082SDavid S. Miller } 3421da177e4SLinus Torvalds } 3431da177e4SLinus Torvalds 344a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt) 3451da177e4SLinus Torvalds { 3461716a961SGao feng if (rt->rt6i_flags & RTF_EXPIRES) { 3471716a961SGao feng if (time_after(jiffies, rt->dst.expires)) 348a50feda5SEric Dumazet return true; 3491716a961SGao feng } else if (rt->dst.from) { 3503fd91fb3SLi RongQing return rt6_check_expired((struct rt6_info *) rt->dst.from); 3511716a961SGao feng } 352a50feda5SEric Dumazet return false; 3531da177e4SLinus Torvalds } 3541da177e4SLinus Torvalds 355a50feda5SEric Dumazet static bool rt6_need_strict(const struct in6_addr *daddr) 356c71099acSThomas Graf { 357a02cec21SEric Dumazet return ipv6_addr_type(daddr) & 358a02cec21SEric Dumazet (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK); 359c71099acSThomas Graf } 360c71099acSThomas Graf 36151ebd318SNicolas Dichtel /* Multipath route selection: 36251ebd318SNicolas Dichtel * Hash based function using packet header and flowlabel. 36351ebd318SNicolas Dichtel * Adapted from fib_info_hashfn() 36451ebd318SNicolas Dichtel */ 36551ebd318SNicolas Dichtel static int rt6_info_hash_nhsfn(unsigned int candidate_count, 36651ebd318SNicolas Dichtel const struct flowi6 *fl6) 36751ebd318SNicolas Dichtel { 36851ebd318SNicolas Dichtel unsigned int val = fl6->flowi6_proto; 36951ebd318SNicolas Dichtel 370c08977bbSYOSHIFUJI Hideaki / 吉藤英明 val ^= ipv6_addr_hash(&fl6->daddr); 371c08977bbSYOSHIFUJI Hideaki / 吉藤英明 val ^= ipv6_addr_hash(&fl6->saddr); 37251ebd318SNicolas Dichtel 37351ebd318SNicolas Dichtel /* Work only if this not encapsulated */ 37451ebd318SNicolas Dichtel switch (fl6->flowi6_proto) { 37551ebd318SNicolas Dichtel case IPPROTO_UDP: 37651ebd318SNicolas Dichtel case IPPROTO_TCP: 37751ebd318SNicolas Dichtel case IPPROTO_SCTP: 378b3ce5ae1SNicolas Dichtel val ^= (__force u16)fl6->fl6_sport; 379b3ce5ae1SNicolas Dichtel val ^= (__force u16)fl6->fl6_dport; 38051ebd318SNicolas Dichtel break; 38151ebd318SNicolas Dichtel 38251ebd318SNicolas Dichtel case IPPROTO_ICMPV6: 383b3ce5ae1SNicolas Dichtel val ^= (__force u16)fl6->fl6_icmp_type; 384b3ce5ae1SNicolas Dichtel val ^= (__force u16)fl6->fl6_icmp_code; 38551ebd318SNicolas Dichtel break; 38651ebd318SNicolas Dichtel } 38751ebd318SNicolas Dichtel /* RFC6438 recommands to use flowlabel */ 388b3ce5ae1SNicolas Dichtel val ^= (__force u32)fl6->flowlabel; 38951ebd318SNicolas Dichtel 39051ebd318SNicolas Dichtel /* Perhaps, we need to tune, this function? */ 39151ebd318SNicolas Dichtel val = val ^ (val >> 7) ^ (val >> 12); 39251ebd318SNicolas Dichtel return val % candidate_count; 39351ebd318SNicolas Dichtel } 39451ebd318SNicolas Dichtel 39551ebd318SNicolas Dichtel static struct rt6_info *rt6_multipath_select(struct rt6_info *match, 39651ebd318SNicolas Dichtel struct flowi6 *fl6) 39751ebd318SNicolas Dichtel { 39851ebd318SNicolas Dichtel struct rt6_info *sibling, *next_sibling; 39951ebd318SNicolas Dichtel int route_choosen; 40051ebd318SNicolas Dichtel 40151ebd318SNicolas Dichtel route_choosen = rt6_info_hash_nhsfn(match->rt6i_nsiblings + 1, fl6); 40251ebd318SNicolas Dichtel /* Don't change the route, if route_choosen == 0 40351ebd318SNicolas Dichtel * (siblings does not include ourself) 40451ebd318SNicolas Dichtel */ 40551ebd318SNicolas Dichtel if (route_choosen) 40651ebd318SNicolas Dichtel list_for_each_entry_safe(sibling, next_sibling, 40751ebd318SNicolas Dichtel &match->rt6i_siblings, rt6i_siblings) { 40851ebd318SNicolas Dichtel route_choosen--; 40951ebd318SNicolas Dichtel if (route_choosen == 0) { 41051ebd318SNicolas Dichtel match = sibling; 41151ebd318SNicolas Dichtel break; 41251ebd318SNicolas Dichtel } 41351ebd318SNicolas Dichtel } 41451ebd318SNicolas Dichtel return match; 41551ebd318SNicolas Dichtel } 41651ebd318SNicolas Dichtel 4171da177e4SLinus Torvalds /* 418c71099acSThomas Graf * Route lookup. Any table->tb6_lock is implied. 4191da177e4SLinus Torvalds */ 4201da177e4SLinus Torvalds 4218ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net, 4228ed67789SDaniel Lezcano struct rt6_info *rt, 423b71d1d42SEric Dumazet const struct in6_addr *saddr, 4241da177e4SLinus Torvalds int oif, 425d420895eSYOSHIFUJI Hideaki int flags) 4261da177e4SLinus Torvalds { 4271da177e4SLinus Torvalds struct rt6_info *local = NULL; 4281da177e4SLinus Torvalds struct rt6_info *sprt; 4291da177e4SLinus Torvalds 430dd3abc4eSYOSHIFUJI Hideaki if (!oif && ipv6_addr_any(saddr)) 431dd3abc4eSYOSHIFUJI Hideaki goto out; 432dd3abc4eSYOSHIFUJI Hideaki 433d8d1f30bSChangli Gao for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) { 434d1918542SDavid S. Miller struct net_device *dev = sprt->dst.dev; 435dd3abc4eSYOSHIFUJI Hideaki 436dd3abc4eSYOSHIFUJI Hideaki if (oif) { 4371da177e4SLinus Torvalds if (dev->ifindex == oif) 4381da177e4SLinus Torvalds return sprt; 4391da177e4SLinus Torvalds if (dev->flags & IFF_LOOPBACK) { 44038308473SDavid S. Miller if (!sprt->rt6i_idev || 4411da177e4SLinus Torvalds sprt->rt6i_idev->dev->ifindex != oif) { 442d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE && oif) 4431da177e4SLinus Torvalds continue; 4441da177e4SLinus Torvalds if (local && (!oif || 4451da177e4SLinus Torvalds local->rt6i_idev->dev->ifindex == oif)) 4461da177e4SLinus Torvalds continue; 4471da177e4SLinus Torvalds } 4481da177e4SLinus Torvalds local = sprt; 4491da177e4SLinus Torvalds } 450dd3abc4eSYOSHIFUJI Hideaki } else { 451dd3abc4eSYOSHIFUJI Hideaki if (ipv6_chk_addr(net, saddr, dev, 452dd3abc4eSYOSHIFUJI Hideaki flags & RT6_LOOKUP_F_IFACE)) 453dd3abc4eSYOSHIFUJI Hideaki return sprt; 454dd3abc4eSYOSHIFUJI Hideaki } 4551da177e4SLinus Torvalds } 4561da177e4SLinus Torvalds 457dd3abc4eSYOSHIFUJI Hideaki if (oif) { 4581da177e4SLinus Torvalds if (local) 4591da177e4SLinus Torvalds return local; 4601da177e4SLinus Torvalds 461d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE) 4628ed67789SDaniel Lezcano return net->ipv6.ip6_null_entry; 4631da177e4SLinus Torvalds } 464dd3abc4eSYOSHIFUJI Hideaki out: 4651da177e4SLinus Torvalds return rt; 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds 46827097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 46927097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt) 47027097255SYOSHIFUJI Hideaki { 471f2c31e32SEric Dumazet struct neighbour *neigh; 47227097255SYOSHIFUJI Hideaki /* 47327097255SYOSHIFUJI Hideaki * Okay, this does not seem to be appropriate 47427097255SYOSHIFUJI Hideaki * for now, however, we need to check if it 47527097255SYOSHIFUJI Hideaki * is really so; aka Router Reachability Probing. 47627097255SYOSHIFUJI Hideaki * 47727097255SYOSHIFUJI Hideaki * Router Reachability Probe MUST be rate-limited 47827097255SYOSHIFUJI Hideaki * to no more than one per minute. 47927097255SYOSHIFUJI Hideaki */ 4802152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (!rt || !(rt->rt6i_flags & RTF_GATEWAY)) 481fdd6681dSAmerigo Wang return; 4822152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 4832152caeaSYOSHIFUJI Hideaki / 吉藤英明 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 4842152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 4852152caeaSYOSHIFUJI Hideaki / 吉藤英明 write_lock(&neigh->lock); 4862152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (neigh->nud_state & NUD_VALID) 4872152caeaSYOSHIFUJI Hideaki / 吉藤英明 goto out; 4887ff74a59SYOSHIFUJI Hideaki / 吉藤英明 } 4892152caeaSYOSHIFUJI Hideaki / 吉藤英明 4902152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (!neigh || 49152e16356SYOSHIFUJI Hideaki time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) { 49227097255SYOSHIFUJI Hideaki struct in6_addr mcaddr; 49327097255SYOSHIFUJI Hideaki struct in6_addr *target; 49427097255SYOSHIFUJI Hideaki 49527097255SYOSHIFUJI Hideaki neigh->updated = jiffies; 49627097255SYOSHIFUJI Hideaki 4972152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (neigh) 4982152caeaSYOSHIFUJI Hideaki / 吉藤英明 write_unlock(&neigh->lock); 4992152caeaSYOSHIFUJI Hideaki / 吉藤英明 5002152caeaSYOSHIFUJI Hideaki / 吉藤英明 target = (struct in6_addr *)&rt->rt6i_gateway; 50127097255SYOSHIFUJI Hideaki addrconf_addr_solict_mult(target, &mcaddr); 502d1918542SDavid S. Miller ndisc_send_ns(rt->dst.dev, NULL, target, &mcaddr, NULL); 503f2c31e32SEric Dumazet } else { 5042152caeaSYOSHIFUJI Hideaki / 吉藤英明 out: 5052152caeaSYOSHIFUJI Hideaki / 吉藤英明 write_unlock(&neigh->lock); 50627097255SYOSHIFUJI Hideaki } 5072152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 508f2c31e32SEric Dumazet } 50927097255SYOSHIFUJI Hideaki #else 51027097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt) 51127097255SYOSHIFUJI Hideaki { 51227097255SYOSHIFUJI Hideaki } 51327097255SYOSHIFUJI Hideaki #endif 51427097255SYOSHIFUJI Hideaki 5151da177e4SLinus Torvalds /* 516554cfb7eSYOSHIFUJI Hideaki * Default Router Selection (RFC 2461 6.3.6) 5171da177e4SLinus Torvalds */ 518b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif) 5191da177e4SLinus Torvalds { 520d1918542SDavid S. Miller struct net_device *dev = rt->dst.dev; 521161980f4SDavid S. Miller if (!oif || dev->ifindex == oif) 522554cfb7eSYOSHIFUJI Hideaki return 2; 523161980f4SDavid S. Miller if ((dev->flags & IFF_LOOPBACK) && 524161980f4SDavid S. Miller rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif) 525161980f4SDavid S. Miller return 1; 526554cfb7eSYOSHIFUJI Hideaki return 0; 5271da177e4SLinus Torvalds } 5281da177e4SLinus Torvalds 529a5a81f0bSPaul Marks static inline bool rt6_check_neigh(struct rt6_info *rt) 5301da177e4SLinus Torvalds { 531f2c31e32SEric Dumazet struct neighbour *neigh; 532a5a81f0bSPaul Marks bool ret = false; 533f2c31e32SEric Dumazet 5344d0c5911SYOSHIFUJI Hideaki if (rt->rt6i_flags & RTF_NONEXTHOP || 5354d0c5911SYOSHIFUJI Hideaki !(rt->rt6i_flags & RTF_GATEWAY)) 536145a3621SYOSHIFUJI Hideaki / 吉藤英明 return true; 537145a3621SYOSHIFUJI Hideaki / 吉藤英明 538145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 539145a3621SYOSHIFUJI Hideaki / 吉藤英明 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 540145a3621SYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 541145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_lock(&neigh->lock); 542554cfb7eSYOSHIFUJI Hideaki if (neigh->nud_state & NUD_VALID) 543a5a81f0bSPaul Marks ret = true; 544398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 545a5a81f0bSPaul Marks else if (!(neigh->nud_state & NUD_FAILED)) 546a5a81f0bSPaul Marks ret = true; 547398bcbebSYOSHIFUJI Hideaki #endif 548145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_unlock(&neigh->lock); 549a5a81f0bSPaul Marks } 550145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 551145a3621SYOSHIFUJI Hideaki / 吉藤英明 552a5a81f0bSPaul Marks return ret; 5531da177e4SLinus Torvalds } 5541da177e4SLinus Torvalds 555554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif, 556554cfb7eSYOSHIFUJI Hideaki int strict) 557554cfb7eSYOSHIFUJI Hideaki { 558a5a81f0bSPaul Marks int m; 5594d0c5911SYOSHIFUJI Hideaki 5604d0c5911SYOSHIFUJI Hideaki m = rt6_check_dev(rt, oif); 56177d16f45SYOSHIFUJI Hideaki if (!m && (strict & RT6_LOOKUP_F_IFACE)) 562554cfb7eSYOSHIFUJI Hideaki return -1; 563ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 564ebacaaa0SYOSHIFUJI Hideaki m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2; 565ebacaaa0SYOSHIFUJI Hideaki #endif 566a5a81f0bSPaul Marks if (!rt6_check_neigh(rt) && (strict & RT6_LOOKUP_F_REACHABLE)) 567554cfb7eSYOSHIFUJI Hideaki return -1; 568554cfb7eSYOSHIFUJI Hideaki return m; 569554cfb7eSYOSHIFUJI Hideaki } 570554cfb7eSYOSHIFUJI Hideaki 571f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict, 572f11e6659SDavid S. Miller int *mpri, struct rt6_info *match) 573554cfb7eSYOSHIFUJI Hideaki { 574554cfb7eSYOSHIFUJI Hideaki int m; 575554cfb7eSYOSHIFUJI Hideaki 576554cfb7eSYOSHIFUJI Hideaki if (rt6_check_expired(rt)) 577f11e6659SDavid S. Miller goto out; 578554cfb7eSYOSHIFUJI Hideaki 579554cfb7eSYOSHIFUJI Hideaki m = rt6_score_route(rt, oif, strict); 580554cfb7eSYOSHIFUJI Hideaki if (m < 0) 581f11e6659SDavid S. Miller goto out; 582554cfb7eSYOSHIFUJI Hideaki 583f11e6659SDavid S. Miller if (m > *mpri) { 584ea659e07SYOSHIFUJI Hideaki if (strict & RT6_LOOKUP_F_REACHABLE) 58527097255SYOSHIFUJI Hideaki rt6_probe(match); 586f11e6659SDavid S. Miller *mpri = m; 587554cfb7eSYOSHIFUJI Hideaki match = rt; 588ea659e07SYOSHIFUJI Hideaki } else if (strict & RT6_LOOKUP_F_REACHABLE) { 58927097255SYOSHIFUJI Hideaki rt6_probe(rt); 5901da177e4SLinus Torvalds } 591f11e6659SDavid S. Miller 592f11e6659SDavid S. Miller out: 593f11e6659SDavid S. Miller return match; 5941da177e4SLinus Torvalds } 5951da177e4SLinus Torvalds 596f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn, 597f11e6659SDavid S. Miller struct rt6_info *rr_head, 598f11e6659SDavid S. Miller u32 metric, int oif, int strict) 599f11e6659SDavid S. Miller { 600f11e6659SDavid S. Miller struct rt6_info *rt, *match; 601f11e6659SDavid S. Miller int mpri = -1; 602f11e6659SDavid S. Miller 603f11e6659SDavid S. Miller match = NULL; 604f11e6659SDavid S. Miller for (rt = rr_head; rt && rt->rt6i_metric == metric; 605d8d1f30bSChangli Gao rt = rt->dst.rt6_next) 606f11e6659SDavid S. Miller match = find_match(rt, oif, strict, &mpri, match); 607f11e6659SDavid S. Miller for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric; 608d8d1f30bSChangli Gao rt = rt->dst.rt6_next) 609f11e6659SDavid S. Miller match = find_match(rt, oif, strict, &mpri, match); 610f11e6659SDavid S. Miller 611f11e6659SDavid S. Miller return match; 612f11e6659SDavid S. Miller } 613f11e6659SDavid S. Miller 614f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict) 615f11e6659SDavid S. Miller { 616f11e6659SDavid S. Miller struct rt6_info *match, *rt0; 6178ed67789SDaniel Lezcano struct net *net; 618f11e6659SDavid S. Miller 619f11e6659SDavid S. Miller rt0 = fn->rr_ptr; 620f11e6659SDavid S. Miller if (!rt0) 621f11e6659SDavid S. Miller fn->rr_ptr = rt0 = fn->leaf; 622f11e6659SDavid S. Miller 623f11e6659SDavid S. Miller match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict); 624f11e6659SDavid S. Miller 625554cfb7eSYOSHIFUJI Hideaki if (!match && 626f11e6659SDavid S. Miller (strict & RT6_LOOKUP_F_REACHABLE)) { 627d8d1f30bSChangli Gao struct rt6_info *next = rt0->dst.rt6_next; 628f11e6659SDavid S. Miller 629554cfb7eSYOSHIFUJI Hideaki /* no entries matched; do round-robin */ 630f11e6659SDavid S. Miller if (!next || next->rt6i_metric != rt0->rt6i_metric) 631f11e6659SDavid S. Miller next = fn->leaf; 632f11e6659SDavid S. Miller 633f11e6659SDavid S. Miller if (next != rt0) 634f11e6659SDavid S. Miller fn->rr_ptr = next; 635554cfb7eSYOSHIFUJI Hideaki } 636554cfb7eSYOSHIFUJI Hideaki 637d1918542SDavid S. Miller net = dev_net(rt0->dst.dev); 638a02cec21SEric Dumazet return match ? match : net->ipv6.ip6_null_entry; 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds 64170ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 64270ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 643b71d1d42SEric Dumazet const struct in6_addr *gwaddr) 64470ceb4f5SYOSHIFUJI Hideaki { 645c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 64670ceb4f5SYOSHIFUJI Hideaki struct route_info *rinfo = (struct route_info *) opt; 64770ceb4f5SYOSHIFUJI Hideaki struct in6_addr prefix_buf, *prefix; 64870ceb4f5SYOSHIFUJI Hideaki unsigned int pref; 6494bed72e4SYOSHIFUJI Hideaki unsigned long lifetime; 65070ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt; 65170ceb4f5SYOSHIFUJI Hideaki 65270ceb4f5SYOSHIFUJI Hideaki if (len < sizeof(struct route_info)) { 65370ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 65470ceb4f5SYOSHIFUJI Hideaki } 65570ceb4f5SYOSHIFUJI Hideaki 65670ceb4f5SYOSHIFUJI Hideaki /* Sanity check for prefix_len and length */ 65770ceb4f5SYOSHIFUJI Hideaki if (rinfo->length > 3) { 65870ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 65970ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 128) { 66070ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 66170ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 64) { 66270ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 2) { 66370ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 66470ceb4f5SYOSHIFUJI Hideaki } 66570ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 0) { 66670ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 1) { 66770ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 66870ceb4f5SYOSHIFUJI Hideaki } 66970ceb4f5SYOSHIFUJI Hideaki } 67070ceb4f5SYOSHIFUJI Hideaki 67170ceb4f5SYOSHIFUJI Hideaki pref = rinfo->route_pref; 67270ceb4f5SYOSHIFUJI Hideaki if (pref == ICMPV6_ROUTER_PREF_INVALID) 6733933fc95SJens Rosenboom return -EINVAL; 67470ceb4f5SYOSHIFUJI Hideaki 6754bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); 67670ceb4f5SYOSHIFUJI Hideaki 67770ceb4f5SYOSHIFUJI Hideaki if (rinfo->length == 3) 67870ceb4f5SYOSHIFUJI Hideaki prefix = (struct in6_addr *)rinfo->prefix; 67970ceb4f5SYOSHIFUJI Hideaki else { 68070ceb4f5SYOSHIFUJI Hideaki /* this function is safe */ 68170ceb4f5SYOSHIFUJI Hideaki ipv6_addr_prefix(&prefix_buf, 68270ceb4f5SYOSHIFUJI Hideaki (struct in6_addr *)rinfo->prefix, 68370ceb4f5SYOSHIFUJI Hideaki rinfo->prefix_len); 68470ceb4f5SYOSHIFUJI Hideaki prefix = &prefix_buf; 68570ceb4f5SYOSHIFUJI Hideaki } 68670ceb4f5SYOSHIFUJI Hideaki 687efa2cea0SDaniel Lezcano rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr, 688efa2cea0SDaniel Lezcano dev->ifindex); 68970ceb4f5SYOSHIFUJI Hideaki 69070ceb4f5SYOSHIFUJI Hideaki if (rt && !lifetime) { 691e0a1ad73SThomas Graf ip6_del_rt(rt); 69270ceb4f5SYOSHIFUJI Hideaki rt = NULL; 69370ceb4f5SYOSHIFUJI Hideaki } 69470ceb4f5SYOSHIFUJI Hideaki 69570ceb4f5SYOSHIFUJI Hideaki if (!rt && lifetime) 696efa2cea0SDaniel Lezcano rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex, 69770ceb4f5SYOSHIFUJI Hideaki pref); 69870ceb4f5SYOSHIFUJI Hideaki else if (rt) 69970ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags = RTF_ROUTEINFO | 70070ceb4f5SYOSHIFUJI Hideaki (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 70170ceb4f5SYOSHIFUJI Hideaki 70270ceb4f5SYOSHIFUJI Hideaki if (rt) { 7031716a961SGao feng if (!addrconf_finite_timeout(lifetime)) 7041716a961SGao feng rt6_clean_expires(rt); 7051716a961SGao feng else 7061716a961SGao feng rt6_set_expires(rt, jiffies + HZ * lifetime); 7071716a961SGao feng 70894e187c0SAmerigo Wang ip6_rt_put(rt); 70970ceb4f5SYOSHIFUJI Hideaki } 71070ceb4f5SYOSHIFUJI Hideaki return 0; 71170ceb4f5SYOSHIFUJI Hideaki } 71270ceb4f5SYOSHIFUJI Hideaki #endif 71370ceb4f5SYOSHIFUJI Hideaki 7148ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr) \ 715982f56f3SYOSHIFUJI Hideaki do { \ 7168ed67789SDaniel Lezcano if (rt == __net->ipv6.ip6_null_entry) { \ 717982f56f3SYOSHIFUJI Hideaki struct fib6_node *pn; \ 718e0eda7bbSVille Nuorvala while (1) { \ 719982f56f3SYOSHIFUJI Hideaki if (fn->fn_flags & RTN_TL_ROOT) \ 720c71099acSThomas Graf goto out; \ 721982f56f3SYOSHIFUJI Hideaki pn = fn->parent; \ 722982f56f3SYOSHIFUJI Hideaki if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \ 7238bce65b9SKim Nordlund fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \ 724982f56f3SYOSHIFUJI Hideaki else \ 725982f56f3SYOSHIFUJI Hideaki fn = pn; \ 726c71099acSThomas Graf if (fn->fn_flags & RTN_RTINFO) \ 727c71099acSThomas Graf goto restart; \ 728c71099acSThomas Graf } \ 729982f56f3SYOSHIFUJI Hideaki } \ 730982f56f3SYOSHIFUJI Hideaki } while (0) 731c71099acSThomas Graf 7328ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net, 7338ed67789SDaniel Lezcano struct fib6_table *table, 7344c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 7351da177e4SLinus Torvalds { 7361da177e4SLinus Torvalds struct fib6_node *fn; 7371da177e4SLinus Torvalds struct rt6_info *rt; 7381da177e4SLinus Torvalds 739c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 7404c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 741c71099acSThomas Graf restart: 742c71099acSThomas Graf rt = fn->leaf; 7434c9483b2SDavid S. Miller rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags); 74451ebd318SNicolas Dichtel if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0) 74551ebd318SNicolas Dichtel rt = rt6_multipath_select(rt, fl6); 7464c9483b2SDavid S. Miller BACKTRACK(net, &fl6->saddr); 747c71099acSThomas Graf out: 748d8d1f30bSChangli Gao dst_use(&rt->dst, jiffies); 749c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 7501da177e4SLinus Torvalds return rt; 751c71099acSThomas Graf 752c71099acSThomas Graf } 753c71099acSThomas Graf 754ea6e574eSFlorian Westphal struct dst_entry * ip6_route_lookup(struct net *net, struct flowi6 *fl6, 755ea6e574eSFlorian Westphal int flags) 756ea6e574eSFlorian Westphal { 757ea6e574eSFlorian Westphal return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup); 758ea6e574eSFlorian Westphal } 759ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup); 760ea6e574eSFlorian Westphal 7619acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, 7629acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *saddr, int oif, int strict) 763c71099acSThomas Graf { 7644c9483b2SDavid S. Miller struct flowi6 fl6 = { 7654c9483b2SDavid S. Miller .flowi6_oif = oif, 7664c9483b2SDavid S. Miller .daddr = *daddr, 767c71099acSThomas Graf }; 768c71099acSThomas Graf struct dst_entry *dst; 76977d16f45SYOSHIFUJI Hideaki int flags = strict ? RT6_LOOKUP_F_IFACE : 0; 770c71099acSThomas Graf 771adaa70bbSThomas Graf if (saddr) { 7724c9483b2SDavid S. Miller memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 773adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 774adaa70bbSThomas Graf } 775adaa70bbSThomas Graf 7764c9483b2SDavid S. Miller dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup); 777c71099acSThomas Graf if (dst->error == 0) 778c71099acSThomas Graf return (struct rt6_info *) dst; 779c71099acSThomas Graf 780c71099acSThomas Graf dst_release(dst); 781c71099acSThomas Graf 7821da177e4SLinus Torvalds return NULL; 7831da177e4SLinus Torvalds } 7841da177e4SLinus Torvalds 7857159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup); 7867159039aSYOSHIFUJI Hideaki 787c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock. 7881da177e4SLinus Torvalds It takes new route entry, the addition fails by any reason the 7891da177e4SLinus Torvalds route is freed. In any case, if caller does not hold it, it may 7901da177e4SLinus Torvalds be destroyed. 7911da177e4SLinus Torvalds */ 7921da177e4SLinus Torvalds 79386872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info) 7941da177e4SLinus Torvalds { 7951da177e4SLinus Torvalds int err; 796c71099acSThomas Graf struct fib6_table *table; 7971da177e4SLinus Torvalds 798c71099acSThomas Graf table = rt->rt6i_table; 799c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 80086872cb5SThomas Graf err = fib6_add(&table->tb6_root, rt, info); 801c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds return err; 8041da177e4SLinus Torvalds } 8051da177e4SLinus Torvalds 80640e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt) 80740e22e8fSThomas Graf { 8084d1169c1SDenis V. Lunev struct nl_info info = { 809d1918542SDavid S. Miller .nl_net = dev_net(rt->dst.dev), 8104d1169c1SDenis V. Lunev }; 811528c4cebSDenis V. Lunev return __ip6_ins_rt(rt, &info); 81240e22e8fSThomas Graf } 81340e22e8fSThomas Graf 8141716a961SGao feng static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, 81521efcfa0SEric Dumazet const struct in6_addr *daddr, 816b71d1d42SEric Dumazet const struct in6_addr *saddr) 8171da177e4SLinus Torvalds { 8181da177e4SLinus Torvalds struct rt6_info *rt; 8191da177e4SLinus Torvalds 8201da177e4SLinus Torvalds /* 8211da177e4SLinus Torvalds * Clone the route. 8221da177e4SLinus Torvalds */ 8231da177e4SLinus Torvalds 82421efcfa0SEric Dumazet rt = ip6_rt_copy(ort, daddr); 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds if (rt) { 82758c4fb86SYOSHIFUJI Hideaki if (!(rt->rt6i_flags & RTF_GATEWAY)) { 828bb3c3686SDavid S. Miller if (ort->rt6i_dst.plen != 128 && 82921efcfa0SEric Dumazet ipv6_addr_equal(&ort->rt6i_dst.addr, daddr)) 83058c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 8314e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = *daddr; 83258c4fb86SYOSHIFUJI Hideaki } 8331da177e4SLinus Torvalds 8341da177e4SLinus Torvalds rt->rt6i_flags |= RTF_CACHE; 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 8371da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 8384e3fd7a0SAlexey Dobriyan rt->rt6i_src.addr = *saddr; 8391da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 8401da177e4SLinus Torvalds } 8411da177e4SLinus Torvalds #endif 84295a9a5baSYOSHIFUJI Hideaki } 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds return rt; 8451da177e4SLinus Torvalds } 84695a9a5baSYOSHIFUJI Hideaki 84721efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, 84821efcfa0SEric Dumazet const struct in6_addr *daddr) 849299d9939SYOSHIFUJI Hideaki { 85021efcfa0SEric Dumazet struct rt6_info *rt = ip6_rt_copy(ort, daddr); 85121efcfa0SEric Dumazet 852*887c95ccSYOSHIFUJI Hideaki / 吉藤英明 if (rt) 853299d9939SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_CACHE; 854299d9939SYOSHIFUJI Hideaki return rt; 855299d9939SYOSHIFUJI Hideaki } 856299d9939SYOSHIFUJI Hideaki 8578ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif, 8584c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 8591da177e4SLinus Torvalds { 8601da177e4SLinus Torvalds struct fib6_node *fn; 861519fbd87SYOSHIFUJI Hideaki struct rt6_info *rt, *nrt; 862c71099acSThomas Graf int strict = 0; 8631da177e4SLinus Torvalds int attempts = 3; 864519fbd87SYOSHIFUJI Hideaki int err; 86553b7997fSYOSHIFUJI Hideaki int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE; 8661da177e4SLinus Torvalds 86777d16f45SYOSHIFUJI Hideaki strict |= flags & RT6_LOOKUP_F_IFACE; 8681da177e4SLinus Torvalds 8691da177e4SLinus Torvalds relookup: 870c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 8711da177e4SLinus Torvalds 8728238dd06SYOSHIFUJI Hideaki restart_2: 8734c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 8741da177e4SLinus Torvalds 8751da177e4SLinus Torvalds restart: 8764acad72dSPavel Emelyanov rt = rt6_select(fn, oif, strict | reachable); 87751ebd318SNicolas Dichtel if (rt->rt6i_nsiblings && oif == 0) 87851ebd318SNicolas Dichtel rt = rt6_multipath_select(rt, fl6); 8794c9483b2SDavid S. Miller BACKTRACK(net, &fl6->saddr); 8808ed67789SDaniel Lezcano if (rt == net->ipv6.ip6_null_entry || 8818238dd06SYOSHIFUJI Hideaki rt->rt6i_flags & RTF_CACHE) 8821da177e4SLinus Torvalds goto out; 8831da177e4SLinus Torvalds 884d8d1f30bSChangli Gao dst_hold(&rt->dst); 885c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 8861da177e4SLinus Torvalds 887c440f160SYOSHIFUJI Hideaki / 吉藤英明 if (!(rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY))) 8884c9483b2SDavid S. Miller nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr); 8897343ff31SDavid S. Miller else if (!(rt->dst.flags & DST_HOST)) 8904c9483b2SDavid S. Miller nrt = rt6_alloc_clone(rt, &fl6->daddr); 8917343ff31SDavid S. Miller else 8927343ff31SDavid S. Miller goto out2; 8931da177e4SLinus Torvalds 89494e187c0SAmerigo Wang ip6_rt_put(rt); 8958ed67789SDaniel Lezcano rt = nrt ? : net->ipv6.ip6_null_entry; 8961da177e4SLinus Torvalds 897d8d1f30bSChangli Gao dst_hold(&rt->dst); 898e40cf353SYOSHIFUJI Hideaki if (nrt) { 89940e22e8fSThomas Graf err = ip6_ins_rt(nrt); 900e40cf353SYOSHIFUJI Hideaki if (!err) 901e40cf353SYOSHIFUJI Hideaki goto out2; 902e40cf353SYOSHIFUJI Hideaki } 903e40cf353SYOSHIFUJI Hideaki 904e40cf353SYOSHIFUJI Hideaki if (--attempts <= 0) 9051da177e4SLinus Torvalds goto out2; 9061da177e4SLinus Torvalds 907519fbd87SYOSHIFUJI Hideaki /* 908c71099acSThomas Graf * Race condition! In the gap, when table->tb6_lock was 909519fbd87SYOSHIFUJI Hideaki * released someone could insert this route. Relookup. 9101da177e4SLinus Torvalds */ 91194e187c0SAmerigo Wang ip6_rt_put(rt); 9121da177e4SLinus Torvalds goto relookup; 913e40cf353SYOSHIFUJI Hideaki 914519fbd87SYOSHIFUJI Hideaki out: 9158238dd06SYOSHIFUJI Hideaki if (reachable) { 9168238dd06SYOSHIFUJI Hideaki reachable = 0; 9178238dd06SYOSHIFUJI Hideaki goto restart_2; 9188238dd06SYOSHIFUJI Hideaki } 919d8d1f30bSChangli Gao dst_hold(&rt->dst); 920c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 9211da177e4SLinus Torvalds out2: 922d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 923d8d1f30bSChangli Gao rt->dst.__use++; 924c71099acSThomas Graf 925c71099acSThomas Graf return rt; 926c71099acSThomas Graf } 927c71099acSThomas Graf 9288ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table, 9294c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 9304acad72dSPavel Emelyanov { 9314c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags); 9324acad72dSPavel Emelyanov } 9334acad72dSPavel Emelyanov 93472331bc0SShmulik Ladkani static struct dst_entry *ip6_route_input_lookup(struct net *net, 93572331bc0SShmulik Ladkani struct net_device *dev, 93672331bc0SShmulik Ladkani struct flowi6 *fl6, int flags) 93772331bc0SShmulik Ladkani { 93872331bc0SShmulik Ladkani if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) 93972331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_IFACE; 94072331bc0SShmulik Ladkani 94172331bc0SShmulik Ladkani return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input); 94272331bc0SShmulik Ladkani } 94372331bc0SShmulik Ladkani 944c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 945c71099acSThomas Graf { 946b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 947c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 948adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 9494c9483b2SDavid S. Miller struct flowi6 fl6 = { 9504c9483b2SDavid S. Miller .flowi6_iif = skb->dev->ifindex, 9514c9483b2SDavid S. Miller .daddr = iph->daddr, 9524c9483b2SDavid S. Miller .saddr = iph->saddr, 9536502ca52SYOSHIFUJI Hideaki / 吉藤英明 .flowlabel = ip6_flowinfo(iph), 9544c9483b2SDavid S. Miller .flowi6_mark = skb->mark, 9554c9483b2SDavid S. Miller .flowi6_proto = iph->nexthdr, 956c71099acSThomas Graf }; 957adaa70bbSThomas Graf 95872331bc0SShmulik Ladkani skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags)); 959c71099acSThomas Graf } 960c71099acSThomas Graf 9618ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table, 9624c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 963c71099acSThomas Graf { 9644c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags); 965c71099acSThomas Graf } 966c71099acSThomas Graf 9679c7a4f9cSFlorian Westphal struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk, 9684c9483b2SDavid S. Miller struct flowi6 *fl6) 969c71099acSThomas Graf { 970c71099acSThomas Graf int flags = 0; 971c71099acSThomas Graf 9721fb9489bSPavel Emelyanov fl6->flowi6_iif = LOOPBACK_IFINDEX; 9734dc27d1cSDavid McCullough 9744c9483b2SDavid S. Miller if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr)) 97577d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 976c71099acSThomas Graf 9774c9483b2SDavid S. Miller if (!ipv6_addr_any(&fl6->saddr)) 978adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 9790c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 9800c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 981adaa70bbSThomas Graf 9824c9483b2SDavid S. Miller return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output); 9831da177e4SLinus Torvalds } 9841da177e4SLinus Torvalds 9857159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output); 9861da177e4SLinus Torvalds 9872774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 98814e50e57SDavid S. Miller { 9895c1e6aa3SDavid S. Miller struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; 99014e50e57SDavid S. Miller struct dst_entry *new = NULL; 99114e50e57SDavid S. Miller 992f5b0a874SDavid S. Miller rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, DST_OBSOLETE_NONE, 0); 99314e50e57SDavid S. Miller if (rt) { 994d8d1f30bSChangli Gao new = &rt->dst; 99514e50e57SDavid S. Miller 9968104891bSSteffen Klassert memset(new + 1, 0, sizeof(*rt) - sizeof(*new)); 9978104891bSSteffen Klassert rt6_init_peer(rt, net->ipv6.peers); 9988104891bSSteffen Klassert 99914e50e57SDavid S. Miller new->__use = 1; 1000352e512cSHerbert Xu new->input = dst_discard; 1001352e512cSHerbert Xu new->output = dst_discard; 100214e50e57SDavid S. Miller 100321efcfa0SEric Dumazet if (dst_metrics_read_only(&ort->dst)) 100421efcfa0SEric Dumazet new->_metrics = ort->dst._metrics; 100521efcfa0SEric Dumazet else 1006defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 100714e50e57SDavid S. Miller rt->rt6i_idev = ort->rt6i_idev; 100814e50e57SDavid S. Miller if (rt->rt6i_idev) 100914e50e57SDavid S. Miller in6_dev_hold(rt->rt6i_idev); 101014e50e57SDavid S. Miller 10114e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 10121716a961SGao feng rt->rt6i_flags = ort->rt6i_flags; 10131716a961SGao feng rt6_clean_expires(rt); 101414e50e57SDavid S. Miller rt->rt6i_metric = 0; 101514e50e57SDavid S. Miller 101614e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 101714e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 101814e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 101914e50e57SDavid S. Miller #endif 102014e50e57SDavid S. Miller 102114e50e57SDavid S. Miller dst_free(new); 102214e50e57SDavid S. Miller } 102314e50e57SDavid S. Miller 102469ead7afSDavid S. Miller dst_release(dst_orig); 102569ead7afSDavid S. Miller return new ? new : ERR_PTR(-ENOMEM); 102614e50e57SDavid S. Miller } 102714e50e57SDavid S. Miller 10281da177e4SLinus Torvalds /* 10291da177e4SLinus Torvalds * Destination cache support functions 10301da177e4SLinus Torvalds */ 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 10331da177e4SLinus Torvalds { 10341da177e4SLinus Torvalds struct rt6_info *rt; 10351da177e4SLinus Torvalds 10361da177e4SLinus Torvalds rt = (struct rt6_info *) dst; 10371da177e4SLinus Torvalds 10386f3118b5SNicolas Dichtel /* All IPV6 dsts are created with ->obsolete set to the value 10396f3118b5SNicolas Dichtel * DST_OBSOLETE_FORCE_CHK which forces validation calls down 10406f3118b5SNicolas Dichtel * into this function always. 10416f3118b5SNicolas Dichtel */ 10426f3118b5SNicolas Dichtel if (rt->rt6i_genid != rt_genid(dev_net(rt->dst.dev))) 10436f3118b5SNicolas Dichtel return NULL; 10446f3118b5SNicolas Dichtel 1045a4477c4dSLi RongQing if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) 10461da177e4SLinus Torvalds return dst; 1047a4477c4dSLi RongQing 10481da177e4SLinus Torvalds return NULL; 10491da177e4SLinus Torvalds } 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 10521da177e4SLinus Torvalds { 10531da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 10541da177e4SLinus Torvalds 10551da177e4SLinus Torvalds if (rt) { 105654c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 105754c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 1058e0a1ad73SThomas Graf ip6_del_rt(rt); 105954c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 10601da177e4SLinus Torvalds } 106154c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 106254c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 106354c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 106454c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 106554c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 106654c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 10671da177e4SLinus Torvalds } 10681da177e4SLinus Torvalds 10691da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 10701da177e4SLinus Torvalds { 10711da177e4SLinus Torvalds struct rt6_info *rt; 10721da177e4SLinus Torvalds 10733ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 10741da177e4SLinus Torvalds 1075adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 10761da177e4SLinus Torvalds if (rt) { 10771716a961SGao feng if (rt->rt6i_flags & RTF_CACHE) 10781716a961SGao feng rt6_update_expires(rt, 0); 10791716a961SGao feng else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) 10801da177e4SLinus Torvalds rt->rt6i_node->fn_sernum = -1; 10811da177e4SLinus Torvalds } 10821da177e4SLinus Torvalds } 10831da177e4SLinus Torvalds 10846700c270SDavid S. Miller static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 10856700c270SDavid S. Miller struct sk_buff *skb, u32 mtu) 10861da177e4SLinus Torvalds { 10871da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info*)dst; 10881da177e4SLinus Torvalds 108981aded24SDavid S. Miller dst_confirm(dst); 10901da177e4SLinus Torvalds if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) { 109181aded24SDavid S. Miller struct net *net = dev_net(dst->dev); 109281aded24SDavid S. Miller 10931da177e4SLinus Torvalds rt6->rt6i_flags |= RTF_MODIFIED; 10941da177e4SLinus Torvalds if (mtu < IPV6_MIN_MTU) { 1095defb3519SDavid S. Miller u32 features = dst_metric(dst, RTAX_FEATURES); 10961da177e4SLinus Torvalds mtu = IPV6_MIN_MTU; 1097defb3519SDavid S. Miller features |= RTAX_FEATURE_ALLFRAG; 1098defb3519SDavid S. Miller dst_metric_set(dst, RTAX_FEATURES, features); 10991da177e4SLinus Torvalds } 1100defb3519SDavid S. Miller dst_metric_set(dst, RTAX_MTU, mtu); 110181aded24SDavid S. Miller rt6_update_expires(rt6, net->ipv6.sysctl.ip6_rt_mtu_expires); 11021da177e4SLinus Torvalds } 11031da177e4SLinus Torvalds } 11041da177e4SLinus Torvalds 110542ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, 110642ae66c8SDavid S. Miller int oif, u32 mark) 110781aded24SDavid S. Miller { 110881aded24SDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 110981aded24SDavid S. Miller struct dst_entry *dst; 111081aded24SDavid S. Miller struct flowi6 fl6; 111181aded24SDavid S. Miller 111281aded24SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 111381aded24SDavid S. Miller fl6.flowi6_oif = oif; 111481aded24SDavid S. Miller fl6.flowi6_mark = mark; 11153e12939aSDavid S. Miller fl6.flowi6_flags = 0; 111681aded24SDavid S. Miller fl6.daddr = iph->daddr; 111781aded24SDavid S. Miller fl6.saddr = iph->saddr; 11186502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 111981aded24SDavid S. Miller 112081aded24SDavid S. Miller dst = ip6_route_output(net, NULL, &fl6); 112181aded24SDavid S. Miller if (!dst->error) 11226700c270SDavid S. Miller ip6_rt_update_pmtu(dst, NULL, skb, ntohl(mtu)); 112381aded24SDavid S. Miller dst_release(dst); 112481aded24SDavid S. Miller } 112581aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu); 112681aded24SDavid S. Miller 112781aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) 112881aded24SDavid S. Miller { 112981aded24SDavid S. Miller ip6_update_pmtu(skb, sock_net(sk), mtu, 113081aded24SDavid S. Miller sk->sk_bound_dev_if, sk->sk_mark); 113181aded24SDavid S. Miller } 113281aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); 113381aded24SDavid S. Miller 11343a5ad2eeSDavid S. Miller void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark) 11353a5ad2eeSDavid S. Miller { 11363a5ad2eeSDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 11373a5ad2eeSDavid S. Miller struct dst_entry *dst; 11383a5ad2eeSDavid S. Miller struct flowi6 fl6; 11393a5ad2eeSDavid S. Miller 11403a5ad2eeSDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 11413a5ad2eeSDavid S. Miller fl6.flowi6_oif = oif; 11423a5ad2eeSDavid S. Miller fl6.flowi6_mark = mark; 11433a5ad2eeSDavid S. Miller fl6.flowi6_flags = 0; 11443a5ad2eeSDavid S. Miller fl6.daddr = iph->daddr; 11453a5ad2eeSDavid S. Miller fl6.saddr = iph->saddr; 11466502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 11473a5ad2eeSDavid S. Miller 11483a5ad2eeSDavid S. Miller dst = ip6_route_output(net, NULL, &fl6); 11493a5ad2eeSDavid S. Miller if (!dst->error) 11506700c270SDavid S. Miller rt6_do_redirect(dst, NULL, skb); 11513a5ad2eeSDavid S. Miller dst_release(dst); 11523a5ad2eeSDavid S. Miller } 11533a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect); 11543a5ad2eeSDavid S. Miller 11553a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) 11563a5ad2eeSDavid S. Miller { 11573a5ad2eeSDavid S. Miller ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark); 11583a5ad2eeSDavid S. Miller } 11593a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect); 11603a5ad2eeSDavid S. Miller 11610dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 11621da177e4SLinus Torvalds { 11630dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 11640dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 11650dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 11660dbaee3bSDavid S. Miller 11671da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 11681da177e4SLinus Torvalds 11695578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 11705578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 11711da177e4SLinus Torvalds 11721da177e4SLinus Torvalds /* 11731da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 11741da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 11751da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 11761da177e4SLinus Torvalds * rely only on pmtu discovery" 11771da177e4SLinus Torvalds */ 11781da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 11791da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 11801da177e4SLinus Torvalds return mtu; 11811da177e4SLinus Torvalds } 11821da177e4SLinus Torvalds 1183ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst) 1184d33e4553SDavid S. Miller { 1185d33e4553SDavid S. Miller struct inet6_dev *idev; 1186618f9bc7SSteffen Klassert unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); 1187618f9bc7SSteffen Klassert 1188618f9bc7SSteffen Klassert if (mtu) 1189618f9bc7SSteffen Klassert return mtu; 1190618f9bc7SSteffen Klassert 1191618f9bc7SSteffen Klassert mtu = IPV6_MIN_MTU; 1192d33e4553SDavid S. Miller 1193d33e4553SDavid S. Miller rcu_read_lock(); 1194d33e4553SDavid S. Miller idev = __in6_dev_get(dst->dev); 1195d33e4553SDavid S. Miller if (idev) 1196d33e4553SDavid S. Miller mtu = idev->cnf.mtu6; 1197d33e4553SDavid S. Miller rcu_read_unlock(); 1198d33e4553SDavid S. Miller 1199d33e4553SDavid S. Miller return mtu; 1200d33e4553SDavid S. Miller } 1201d33e4553SDavid S. Miller 12023b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list; 12033b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock); 12045d0bbeebSThomas Graf 12053b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 12061da177e4SLinus Torvalds struct neighbour *neigh, 120787a11578SDavid S. Miller struct flowi6 *fl6) 12081da177e4SLinus Torvalds { 120987a11578SDavid S. Miller struct dst_entry *dst; 12101da177e4SLinus Torvalds struct rt6_info *rt; 12111da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 1212c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 12131da177e4SLinus Torvalds 121438308473SDavid S. Miller if (unlikely(!idev)) 1215122bdf67SEric Dumazet return ERR_PTR(-ENODEV); 12161da177e4SLinus Torvalds 12178b96d22dSDavid S. Miller rt = ip6_dst_alloc(net, dev, 0, NULL); 121838308473SDavid S. Miller if (unlikely(!rt)) { 12191da177e4SLinus Torvalds in6_dev_put(idev); 122087a11578SDavid S. Miller dst = ERR_PTR(-ENOMEM); 12211da177e4SLinus Torvalds goto out; 12221da177e4SLinus Torvalds } 12231da177e4SLinus Torvalds 12248e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 12258e2ec639SYan, Zheng rt->dst.output = ip6_output; 1226d8d1f30bSChangli Gao atomic_set(&rt->dst.__refcnt, 1); 122787a11578SDavid S. Miller rt->rt6i_dst.addr = fl6->daddr; 12288e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 12298e2ec639SYan, Zheng rt->rt6i_idev = idev; 123014edd87dSLi RongQing dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); 12311da177e4SLinus Torvalds 12323b00944cSYOSHIFUJI Hideaki spin_lock_bh(&icmp6_dst_lock); 1233d8d1f30bSChangli Gao rt->dst.next = icmp6_dst_gc_list; 1234d8d1f30bSChangli Gao icmp6_dst_gc_list = &rt->dst; 12353b00944cSYOSHIFUJI Hideaki spin_unlock_bh(&icmp6_dst_lock); 12361da177e4SLinus Torvalds 12375578689aSDaniel Lezcano fib6_force_start_gc(net); 12381da177e4SLinus Torvalds 123987a11578SDavid S. Miller dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); 124087a11578SDavid S. Miller 12411da177e4SLinus Torvalds out: 124287a11578SDavid S. Miller return dst; 12431da177e4SLinus Torvalds } 12441da177e4SLinus Torvalds 12453d0f24a7SStephen Hemminger int icmp6_dst_gc(void) 12461da177e4SLinus Torvalds { 1247e9476e95SHagen Paul Pfeifer struct dst_entry *dst, **pprev; 12483d0f24a7SStephen Hemminger int more = 0; 12491da177e4SLinus Torvalds 12503b00944cSYOSHIFUJI Hideaki spin_lock_bh(&icmp6_dst_lock); 12513b00944cSYOSHIFUJI Hideaki pprev = &icmp6_dst_gc_list; 12525d0bbeebSThomas Graf 12531da177e4SLinus Torvalds while ((dst = *pprev) != NULL) { 12541da177e4SLinus Torvalds if (!atomic_read(&dst->__refcnt)) { 12551da177e4SLinus Torvalds *pprev = dst->next; 12561da177e4SLinus Torvalds dst_free(dst); 12571da177e4SLinus Torvalds } else { 12581da177e4SLinus Torvalds pprev = &dst->next; 12593d0f24a7SStephen Hemminger ++more; 12601da177e4SLinus Torvalds } 12611da177e4SLinus Torvalds } 12621da177e4SLinus Torvalds 12633b00944cSYOSHIFUJI Hideaki spin_unlock_bh(&icmp6_dst_lock); 12645d0bbeebSThomas Graf 12653d0f24a7SStephen Hemminger return more; 12661da177e4SLinus Torvalds } 12671da177e4SLinus Torvalds 12681e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg), 12691e493d19SDavid S. Miller void *arg) 12701e493d19SDavid S. Miller { 12711e493d19SDavid S. Miller struct dst_entry *dst, **pprev; 12721e493d19SDavid S. Miller 12731e493d19SDavid S. Miller spin_lock_bh(&icmp6_dst_lock); 12741e493d19SDavid S. Miller pprev = &icmp6_dst_gc_list; 12751e493d19SDavid S. Miller while ((dst = *pprev) != NULL) { 12761e493d19SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 12771e493d19SDavid S. Miller if (func(rt, arg)) { 12781e493d19SDavid S. Miller *pprev = dst->next; 12791e493d19SDavid S. Miller dst_free(dst); 12801e493d19SDavid S. Miller } else { 12811e493d19SDavid S. Miller pprev = &dst->next; 12821e493d19SDavid S. Miller } 12831e493d19SDavid S. Miller } 12841e493d19SDavid S. Miller spin_unlock_bh(&icmp6_dst_lock); 12851e493d19SDavid S. Miller } 12861e493d19SDavid S. Miller 1287569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops) 12881da177e4SLinus Torvalds { 12891da177e4SLinus Torvalds unsigned long now = jiffies; 129086393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 12917019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 12927019b78eSDaniel Lezcano int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; 12937019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 12947019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 12957019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 1296fc66f95cSEric Dumazet int entries; 12971da177e4SLinus Torvalds 1298fc66f95cSEric Dumazet entries = dst_entries_get_fast(ops); 12997019b78eSDaniel Lezcano if (time_after(rt_last_gc + rt_min_interval, now) && 1300fc66f95cSEric Dumazet entries <= rt_max_size) 13011da177e4SLinus Torvalds goto out; 13021da177e4SLinus Torvalds 13036891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire++; 13046891a346SBenjamin Thery fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net); 13056891a346SBenjamin Thery net->ipv6.ip6_rt_last_gc = now; 1306fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 1307fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 13087019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; 13091da177e4SLinus Torvalds out: 13107019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; 1311fc66f95cSEric Dumazet return entries > rt_max_size; 13121da177e4SLinus Torvalds } 13131da177e4SLinus Torvalds 13146b75d090SYOSHIFUJI Hideaki int ip6_dst_hoplimit(struct dst_entry *dst) 13151da177e4SLinus Torvalds { 13165170ae82SDavid S. Miller int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT); 1317a02e4b7dSDavid S. Miller if (hoplimit == 0) { 13186b75d090SYOSHIFUJI Hideaki struct net_device *dev = dst->dev; 1319c68f24ccSEric Dumazet struct inet6_dev *idev; 1320c68f24ccSEric Dumazet 1321c68f24ccSEric Dumazet rcu_read_lock(); 1322c68f24ccSEric Dumazet idev = __in6_dev_get(dev); 1323c68f24ccSEric Dumazet if (idev) 13241da177e4SLinus Torvalds hoplimit = idev->cnf.hop_limit; 1325c68f24ccSEric Dumazet else 132653b7997fSYOSHIFUJI Hideaki hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit; 1327c68f24ccSEric Dumazet rcu_read_unlock(); 13281da177e4SLinus Torvalds } 13291da177e4SLinus Torvalds return hoplimit; 13301da177e4SLinus Torvalds } 1331abbf46aeSDavid S. Miller EXPORT_SYMBOL(ip6_dst_hoplimit); 13321da177e4SLinus Torvalds 13331da177e4SLinus Torvalds /* 13341da177e4SLinus Torvalds * 13351da177e4SLinus Torvalds */ 13361da177e4SLinus Torvalds 133786872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg) 13381da177e4SLinus Torvalds { 13391da177e4SLinus Torvalds int err; 13405578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 13411da177e4SLinus Torvalds struct rt6_info *rt = NULL; 13421da177e4SLinus Torvalds struct net_device *dev = NULL; 13431da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 1344c71099acSThomas Graf struct fib6_table *table; 13451da177e4SLinus Torvalds int addr_type; 13461da177e4SLinus Torvalds 134786872cb5SThomas Graf if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128) 13481da177e4SLinus Torvalds return -EINVAL; 13491da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 135086872cb5SThomas Graf if (cfg->fc_src_len) 13511da177e4SLinus Torvalds return -EINVAL; 13521da177e4SLinus Torvalds #endif 135386872cb5SThomas Graf if (cfg->fc_ifindex) { 13541da177e4SLinus Torvalds err = -ENODEV; 13555578689aSDaniel Lezcano dev = dev_get_by_index(net, cfg->fc_ifindex); 13561da177e4SLinus Torvalds if (!dev) 13571da177e4SLinus Torvalds goto out; 13581da177e4SLinus Torvalds idev = in6_dev_get(dev); 13591da177e4SLinus Torvalds if (!idev) 13601da177e4SLinus Torvalds goto out; 13611da177e4SLinus Torvalds } 13621da177e4SLinus Torvalds 136386872cb5SThomas Graf if (cfg->fc_metric == 0) 136486872cb5SThomas Graf cfg->fc_metric = IP6_RT_PRIO_USER; 13651da177e4SLinus Torvalds 1366c71099acSThomas Graf err = -ENOBUFS; 136738308473SDavid S. Miller if (cfg->fc_nlinfo.nlh && 1368d71314b4SMatti Vaittinen !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { 1369d71314b4SMatti Vaittinen table = fib6_get_table(net, cfg->fc_table); 137038308473SDavid S. Miller if (!table) { 1371f3213831SJoe Perches pr_warn("NLM_F_CREATE should be specified when creating new route\n"); 1372d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 1373d71314b4SMatti Vaittinen } 1374d71314b4SMatti Vaittinen } else { 1375d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 1376d71314b4SMatti Vaittinen } 137738308473SDavid S. Miller 137838308473SDavid S. Miller if (!table) 1379c71099acSThomas Graf goto out; 1380c71099acSThomas Graf 13818b96d22dSDavid S. Miller rt = ip6_dst_alloc(net, NULL, DST_NOCOUNT, table); 13821da177e4SLinus Torvalds 138338308473SDavid S. Miller if (!rt) { 13841da177e4SLinus Torvalds err = -ENOMEM; 13851da177e4SLinus Torvalds goto out; 13861da177e4SLinus Torvalds } 13871da177e4SLinus Torvalds 13881716a961SGao feng if (cfg->fc_flags & RTF_EXPIRES) 13891716a961SGao feng rt6_set_expires(rt, jiffies + 13901716a961SGao feng clock_t_to_jiffies(cfg->fc_expires)); 13911716a961SGao feng else 13921716a961SGao feng rt6_clean_expires(rt); 13931da177e4SLinus Torvalds 139486872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 139586872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 139686872cb5SThomas Graf rt->rt6i_protocol = cfg->fc_protocol; 139786872cb5SThomas Graf 139886872cb5SThomas Graf addr_type = ipv6_addr_type(&cfg->fc_dst); 13991da177e4SLinus Torvalds 14001da177e4SLinus Torvalds if (addr_type & IPV6_ADDR_MULTICAST) 1401d8d1f30bSChangli Gao rt->dst.input = ip6_mc_input; 1402ab79ad14SMaciej Żenczykowski else if (cfg->fc_flags & RTF_LOCAL) 1403ab79ad14SMaciej Żenczykowski rt->dst.input = ip6_input; 14041da177e4SLinus Torvalds else 1405d8d1f30bSChangli Gao rt->dst.input = ip6_forward; 14061da177e4SLinus Torvalds 1407d8d1f30bSChangli Gao rt->dst.output = ip6_output; 14081da177e4SLinus Torvalds 140986872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 141086872cb5SThomas Graf rt->rt6i_dst.plen = cfg->fc_dst_len; 14111da177e4SLinus Torvalds if (rt->rt6i_dst.plen == 128) 141211d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 14131da177e4SLinus Torvalds 14148e2ec639SYan, Zheng if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) { 14158e2ec639SYan, Zheng u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL); 14168e2ec639SYan, Zheng if (!metrics) { 14178e2ec639SYan, Zheng err = -ENOMEM; 14188e2ec639SYan, Zheng goto out; 14198e2ec639SYan, Zheng } 14208e2ec639SYan, Zheng dst_init_metrics(&rt->dst, metrics, 0); 14218e2ec639SYan, Zheng } 14221da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 142386872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len); 142486872cb5SThomas Graf rt->rt6i_src.plen = cfg->fc_src_len; 14251da177e4SLinus Torvalds #endif 14261da177e4SLinus Torvalds 142786872cb5SThomas Graf rt->rt6i_metric = cfg->fc_metric; 14281da177e4SLinus Torvalds 14291da177e4SLinus Torvalds /* We cannot add true routes via loopback here, 14301da177e4SLinus Torvalds they would result in kernel looping; promote them to reject routes 14311da177e4SLinus Torvalds */ 143286872cb5SThomas Graf if ((cfg->fc_flags & RTF_REJECT) || 143338308473SDavid S. Miller (dev && (dev->flags & IFF_LOOPBACK) && 143438308473SDavid S. Miller !(addr_type & IPV6_ADDR_LOOPBACK) && 143538308473SDavid S. Miller !(cfg->fc_flags & RTF_LOCAL))) { 14361da177e4SLinus Torvalds /* hold loopback dev/idev if we haven't done so. */ 14375578689aSDaniel Lezcano if (dev != net->loopback_dev) { 14381da177e4SLinus Torvalds if (dev) { 14391da177e4SLinus Torvalds dev_put(dev); 14401da177e4SLinus Torvalds in6_dev_put(idev); 14411da177e4SLinus Torvalds } 14425578689aSDaniel Lezcano dev = net->loopback_dev; 14431da177e4SLinus Torvalds dev_hold(dev); 14441da177e4SLinus Torvalds idev = in6_dev_get(dev); 14451da177e4SLinus Torvalds if (!idev) { 14461da177e4SLinus Torvalds err = -ENODEV; 14471da177e4SLinus Torvalds goto out; 14481da177e4SLinus Torvalds } 14491da177e4SLinus Torvalds } 1450d8d1f30bSChangli Gao rt->dst.output = ip6_pkt_discard_out; 1451d8d1f30bSChangli Gao rt->dst.input = ip6_pkt_discard; 14521da177e4SLinus Torvalds rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP; 1453ef2c7d7bSNicolas Dichtel switch (cfg->fc_type) { 1454ef2c7d7bSNicolas Dichtel case RTN_BLACKHOLE: 1455ef2c7d7bSNicolas Dichtel rt->dst.error = -EINVAL; 1456ef2c7d7bSNicolas Dichtel break; 1457ef2c7d7bSNicolas Dichtel case RTN_PROHIBIT: 1458ef2c7d7bSNicolas Dichtel rt->dst.error = -EACCES; 1459ef2c7d7bSNicolas Dichtel break; 1460b4949ab2SNicolas Dichtel case RTN_THROW: 1461b4949ab2SNicolas Dichtel rt->dst.error = -EAGAIN; 1462b4949ab2SNicolas Dichtel break; 1463ef2c7d7bSNicolas Dichtel default: 1464ef2c7d7bSNicolas Dichtel rt->dst.error = -ENETUNREACH; 1465ef2c7d7bSNicolas Dichtel break; 1466ef2c7d7bSNicolas Dichtel } 14671da177e4SLinus Torvalds goto install_route; 14681da177e4SLinus Torvalds } 14691da177e4SLinus Torvalds 147086872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY) { 1471b71d1d42SEric Dumazet const struct in6_addr *gw_addr; 14721da177e4SLinus Torvalds int gwa_type; 14731da177e4SLinus Torvalds 147486872cb5SThomas Graf gw_addr = &cfg->fc_gateway; 14754e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = *gw_addr; 14761da177e4SLinus Torvalds gwa_type = ipv6_addr_type(gw_addr); 14771da177e4SLinus Torvalds 14781da177e4SLinus Torvalds if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) { 14791da177e4SLinus Torvalds struct rt6_info *grt; 14801da177e4SLinus Torvalds 14811da177e4SLinus Torvalds /* IPv6 strictly inhibits using not link-local 14821da177e4SLinus Torvalds addresses as nexthop address. 14831da177e4SLinus Torvalds Otherwise, router will not able to send redirects. 14841da177e4SLinus Torvalds It is very good, but in some (rare!) circumstances 14851da177e4SLinus Torvalds (SIT, PtP, NBMA NOARP links) it is handy to allow 14861da177e4SLinus Torvalds some exceptions. --ANK 14871da177e4SLinus Torvalds */ 14881da177e4SLinus Torvalds err = -EINVAL; 14891da177e4SLinus Torvalds if (!(gwa_type & IPV6_ADDR_UNICAST)) 14901da177e4SLinus Torvalds goto out; 14911da177e4SLinus Torvalds 14925578689aSDaniel Lezcano grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1); 14931da177e4SLinus Torvalds 14941da177e4SLinus Torvalds err = -EHOSTUNREACH; 149538308473SDavid S. Miller if (!grt) 14961da177e4SLinus Torvalds goto out; 14971da177e4SLinus Torvalds if (dev) { 1498d1918542SDavid S. Miller if (dev != grt->dst.dev) { 149994e187c0SAmerigo Wang ip6_rt_put(grt); 15001da177e4SLinus Torvalds goto out; 15011da177e4SLinus Torvalds } 15021da177e4SLinus Torvalds } else { 1503d1918542SDavid S. Miller dev = grt->dst.dev; 15041da177e4SLinus Torvalds idev = grt->rt6i_idev; 15051da177e4SLinus Torvalds dev_hold(dev); 15061da177e4SLinus Torvalds in6_dev_hold(grt->rt6i_idev); 15071da177e4SLinus Torvalds } 15081da177e4SLinus Torvalds if (!(grt->rt6i_flags & RTF_GATEWAY)) 15091da177e4SLinus Torvalds err = 0; 151094e187c0SAmerigo Wang ip6_rt_put(grt); 15111da177e4SLinus Torvalds 15121da177e4SLinus Torvalds if (err) 15131da177e4SLinus Torvalds goto out; 15141da177e4SLinus Torvalds } 15151da177e4SLinus Torvalds err = -EINVAL; 151638308473SDavid S. Miller if (!dev || (dev->flags & IFF_LOOPBACK)) 15171da177e4SLinus Torvalds goto out; 15181da177e4SLinus Torvalds } 15191da177e4SLinus Torvalds 15201da177e4SLinus Torvalds err = -ENODEV; 152138308473SDavid S. Miller if (!dev) 15221da177e4SLinus Torvalds goto out; 15231da177e4SLinus Torvalds 1524c3968a85SDaniel Walter if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 1525c3968a85SDaniel Walter if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 1526c3968a85SDaniel Walter err = -EINVAL; 1527c3968a85SDaniel Walter goto out; 1528c3968a85SDaniel Walter } 15294e3fd7a0SAlexey Dobriyan rt->rt6i_prefsrc.addr = cfg->fc_prefsrc; 1530c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 128; 1531c3968a85SDaniel Walter } else 1532c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 1533c3968a85SDaniel Walter 153486872cb5SThomas Graf rt->rt6i_flags = cfg->fc_flags; 15351da177e4SLinus Torvalds 15361da177e4SLinus Torvalds install_route: 153786872cb5SThomas Graf if (cfg->fc_mx) { 153886872cb5SThomas Graf struct nlattr *nla; 153986872cb5SThomas Graf int remaining; 15401da177e4SLinus Torvalds 154186872cb5SThomas Graf nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { 15428f4c1f9bSThomas Graf int type = nla_type(nla); 154386872cb5SThomas Graf 154486872cb5SThomas Graf if (type) { 154586872cb5SThomas Graf if (type > RTAX_MAX) { 15461da177e4SLinus Torvalds err = -EINVAL; 15471da177e4SLinus Torvalds goto out; 15481da177e4SLinus Torvalds } 154986872cb5SThomas Graf 1550defb3519SDavid S. Miller dst_metric_set(&rt->dst, type, nla_get_u32(nla)); 15511da177e4SLinus Torvalds } 15521da177e4SLinus Torvalds } 15531da177e4SLinus Torvalds } 15541da177e4SLinus Torvalds 1555d8d1f30bSChangli Gao rt->dst.dev = dev; 15561da177e4SLinus Torvalds rt->rt6i_idev = idev; 1557c71099acSThomas Graf rt->rt6i_table = table; 155863152fc0SDaniel Lezcano 1559c346dca1SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = dev_net(dev); 156063152fc0SDaniel Lezcano 156186872cb5SThomas Graf return __ip6_ins_rt(rt, &cfg->fc_nlinfo); 15621da177e4SLinus Torvalds 15631da177e4SLinus Torvalds out: 15641da177e4SLinus Torvalds if (dev) 15651da177e4SLinus Torvalds dev_put(dev); 15661da177e4SLinus Torvalds if (idev) 15671da177e4SLinus Torvalds in6_dev_put(idev); 15681da177e4SLinus Torvalds if (rt) 1569d8d1f30bSChangli Gao dst_free(&rt->dst); 15701da177e4SLinus Torvalds return err; 15711da177e4SLinus Torvalds } 15721da177e4SLinus Torvalds 157386872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info) 15741da177e4SLinus Torvalds { 15751da177e4SLinus Torvalds int err; 1576c71099acSThomas Graf struct fib6_table *table; 1577d1918542SDavid S. Miller struct net *net = dev_net(rt->dst.dev); 15781da177e4SLinus Torvalds 15796825a26cSGao feng if (rt == net->ipv6.ip6_null_entry) { 15806825a26cSGao feng err = -ENOENT; 15816825a26cSGao feng goto out; 15826825a26cSGao feng } 15836c813a72SPatrick McHardy 1584c71099acSThomas Graf table = rt->rt6i_table; 1585c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 158686872cb5SThomas Graf err = fib6_del(rt, info); 1587c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 15881da177e4SLinus Torvalds 15896825a26cSGao feng out: 159094e187c0SAmerigo Wang ip6_rt_put(rt); 15911da177e4SLinus Torvalds return err; 15921da177e4SLinus Torvalds } 15931da177e4SLinus Torvalds 1594e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt) 1595e0a1ad73SThomas Graf { 15964d1169c1SDenis V. Lunev struct nl_info info = { 1597d1918542SDavid S. Miller .nl_net = dev_net(rt->dst.dev), 15984d1169c1SDenis V. Lunev }; 1599528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 1600e0a1ad73SThomas Graf } 1601e0a1ad73SThomas Graf 160286872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg) 16031da177e4SLinus Torvalds { 1604c71099acSThomas Graf struct fib6_table *table; 16051da177e4SLinus Torvalds struct fib6_node *fn; 16061da177e4SLinus Torvalds struct rt6_info *rt; 16071da177e4SLinus Torvalds int err = -ESRCH; 16081da177e4SLinus Torvalds 16095578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 161038308473SDavid S. Miller if (!table) 1611c71099acSThomas Graf return err; 16121da177e4SLinus Torvalds 1613c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 1614c71099acSThomas Graf 1615c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 161686872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 161786872cb5SThomas Graf &cfg->fc_src, cfg->fc_src_len); 16181da177e4SLinus Torvalds 16191da177e4SLinus Torvalds if (fn) { 1620d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 162186872cb5SThomas Graf if (cfg->fc_ifindex && 1622d1918542SDavid S. Miller (!rt->dst.dev || 1623d1918542SDavid S. Miller rt->dst.dev->ifindex != cfg->fc_ifindex)) 16241da177e4SLinus Torvalds continue; 162586872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 162686872cb5SThomas Graf !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 16271da177e4SLinus Torvalds continue; 162886872cb5SThomas Graf if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric) 16291da177e4SLinus Torvalds continue; 1630d8d1f30bSChangli Gao dst_hold(&rt->dst); 1631c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 16321da177e4SLinus Torvalds 163386872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 16341da177e4SLinus Torvalds } 16351da177e4SLinus Torvalds } 1636c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 16371da177e4SLinus Torvalds 16381da177e4SLinus Torvalds return err; 16391da177e4SLinus Torvalds } 16401da177e4SLinus Torvalds 16416700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 1642a6279458SYOSHIFUJI Hideaki { 1643e8599ff4SDavid S. Miller struct net *net = dev_net(skb->dev); 1644a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 1645e8599ff4SDavid S. Miller struct rt6_info *rt, *nrt = NULL; 1646e8599ff4SDavid S. Miller struct ndisc_options ndopts; 1647e8599ff4SDavid S. Miller struct inet6_dev *in6_dev; 1648e8599ff4SDavid S. Miller struct neighbour *neigh; 164971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 struct rd_msg *msg; 16506e157b6aSDavid S. Miller int optlen, on_link; 16516e157b6aSDavid S. Miller u8 *lladdr; 1652e8599ff4SDavid S. Miller 1653e8599ff4SDavid S. Miller optlen = skb->tail - skb->transport_header; 165471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 optlen -= sizeof(*msg); 1655e8599ff4SDavid S. Miller 1656e8599ff4SDavid S. Miller if (optlen < 0) { 16576e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); 1658e8599ff4SDavid S. Miller return; 1659e8599ff4SDavid S. Miller } 1660e8599ff4SDavid S. Miller 166171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 msg = (struct rd_msg *)icmp6_hdr(skb); 1662e8599ff4SDavid S. Miller 166371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_is_multicast(&msg->dest)) { 16646e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); 1665e8599ff4SDavid S. Miller return; 1666e8599ff4SDavid S. Miller } 1667e8599ff4SDavid S. Miller 16686e157b6aSDavid S. Miller on_link = 0; 166971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_equal(&msg->dest, &msg->target)) { 1670e8599ff4SDavid S. Miller on_link = 1; 167171bcdba0SYOSHIFUJI Hideaki / 吉藤英明 } else if (ipv6_addr_type(&msg->target) != 1672e8599ff4SDavid S. Miller (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { 16736e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); 1674e8599ff4SDavid S. Miller return; 1675e8599ff4SDavid S. Miller } 1676e8599ff4SDavid S. Miller 1677e8599ff4SDavid S. Miller in6_dev = __in6_dev_get(skb->dev); 1678e8599ff4SDavid S. Miller if (!in6_dev) 1679e8599ff4SDavid S. Miller return; 1680e8599ff4SDavid S. Miller if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) 1681e8599ff4SDavid S. Miller return; 1682e8599ff4SDavid S. Miller 1683e8599ff4SDavid S. Miller /* RFC2461 8.1: 1684e8599ff4SDavid S. Miller * The IP source address of the Redirect MUST be the same as the current 1685e8599ff4SDavid S. Miller * first-hop router for the specified ICMP Destination Address. 1686e8599ff4SDavid S. Miller */ 1687e8599ff4SDavid S. Miller 168871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (!ndisc_parse_options(msg->opt, optlen, &ndopts)) { 1689e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); 1690e8599ff4SDavid S. Miller return; 1691e8599ff4SDavid S. Miller } 16926e157b6aSDavid S. Miller 16936e157b6aSDavid S. Miller lladdr = NULL; 1694e8599ff4SDavid S. Miller if (ndopts.nd_opts_tgt_lladdr) { 1695e8599ff4SDavid S. Miller lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, 1696e8599ff4SDavid S. Miller skb->dev); 1697e8599ff4SDavid S. Miller if (!lladdr) { 1698e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); 1699e8599ff4SDavid S. Miller return; 1700e8599ff4SDavid S. Miller } 1701e8599ff4SDavid S. Miller } 1702e8599ff4SDavid S. Miller 17036e157b6aSDavid S. Miller rt = (struct rt6_info *) dst; 17046e157b6aSDavid S. Miller if (rt == net->ipv6.ip6_null_entry) { 17056e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); 17066e157b6aSDavid S. Miller return; 17076e157b6aSDavid S. Miller } 17086e157b6aSDavid S. Miller 17096e157b6aSDavid S. Miller /* Redirect received -> path was valid. 17106e157b6aSDavid S. Miller * Look, redirects are sent only in response to data packets, 17116e157b6aSDavid S. Miller * so that this nexthop apparently is reachable. --ANK 17126e157b6aSDavid S. Miller */ 17136e157b6aSDavid S. Miller dst_confirm(&rt->dst); 17146e157b6aSDavid S. Miller 171571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); 1716e8599ff4SDavid S. Miller if (!neigh) 1717e8599ff4SDavid S. Miller return; 1718e8599ff4SDavid S. Miller 17191da177e4SLinus Torvalds /* 17201da177e4SLinus Torvalds * We have finally decided to accept it. 17211da177e4SLinus Torvalds */ 17221da177e4SLinus Torvalds 17231da177e4SLinus Torvalds neigh_update(neigh, lladdr, NUD_STALE, 17241da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 17251da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 17261da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 17271da177e4SLinus Torvalds NEIGH_UPDATE_F_ISROUTER)) 17281da177e4SLinus Torvalds ); 17291da177e4SLinus Torvalds 173071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 nrt = ip6_rt_copy(rt, &msg->dest); 173138308473SDavid S. Miller if (!nrt) 17321da177e4SLinus Torvalds goto out; 17331da177e4SLinus Torvalds 17341da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 17351da177e4SLinus Torvalds if (on_link) 17361da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 17371da177e4SLinus Torvalds 17384e3fd7a0SAlexey Dobriyan nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 17391da177e4SLinus Torvalds 174040e22e8fSThomas Graf if (ip6_ins_rt(nrt)) 17411da177e4SLinus Torvalds goto out; 17421da177e4SLinus Torvalds 1743d8d1f30bSChangli Gao netevent.old = &rt->dst; 1744d8d1f30bSChangli Gao netevent.new = &nrt->dst; 174571bcdba0SYOSHIFUJI Hideaki / 吉藤英明 netevent.daddr = &msg->dest; 174660592833SYOSHIFUJI Hideaki / 吉藤英明 netevent.neigh = neigh; 17478d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 17488d71740cSTom Tucker 17491da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) { 17506e157b6aSDavid S. Miller rt = (struct rt6_info *) dst_clone(&rt->dst); 1751e0a1ad73SThomas Graf ip6_del_rt(rt); 17521da177e4SLinus Torvalds } 17531da177e4SLinus Torvalds 17541da177e4SLinus Torvalds out: 1755e8599ff4SDavid S. Miller neigh_release(neigh); 17566e157b6aSDavid S. Miller } 17576e157b6aSDavid S. Miller 17581da177e4SLinus Torvalds /* 17591da177e4SLinus Torvalds * Misc support functions 17601da177e4SLinus Torvalds */ 17611da177e4SLinus Torvalds 17621716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort, 176321efcfa0SEric Dumazet const struct in6_addr *dest) 17641da177e4SLinus Torvalds { 1765d1918542SDavid S. Miller struct net *net = dev_net(ort->dst.dev); 17668b96d22dSDavid S. Miller struct rt6_info *rt = ip6_dst_alloc(net, ort->dst.dev, 0, 17678b96d22dSDavid S. Miller ort->rt6i_table); 17681da177e4SLinus Torvalds 17691da177e4SLinus Torvalds if (rt) { 1770d8d1f30bSChangli Gao rt->dst.input = ort->dst.input; 1771d8d1f30bSChangli Gao rt->dst.output = ort->dst.output; 17728e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 17731da177e4SLinus Torvalds 17744e3fd7a0SAlexey Dobriyan rt->rt6i_dst.addr = *dest; 17758e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 1776defb3519SDavid S. Miller dst_copy_metrics(&rt->dst, &ort->dst); 1777d8d1f30bSChangli Gao rt->dst.error = ort->dst.error; 17781da177e4SLinus Torvalds rt->rt6i_idev = ort->rt6i_idev; 17791da177e4SLinus Torvalds if (rt->rt6i_idev) 17801da177e4SLinus Torvalds in6_dev_hold(rt->rt6i_idev); 1781d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 17821da177e4SLinus Torvalds 17834e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 17841716a961SGao feng rt->rt6i_flags = ort->rt6i_flags; 17851716a961SGao feng if ((ort->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) == 17861716a961SGao feng (RTF_DEFAULT | RTF_ADDRCONF)) 17871716a961SGao feng rt6_set_from(rt, ort); 17881716a961SGao feng else 17891716a961SGao feng rt6_clean_expires(rt); 17901da177e4SLinus Torvalds rt->rt6i_metric = 0; 17911da177e4SLinus Torvalds 17921da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 17931da177e4SLinus Torvalds memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 17941da177e4SLinus Torvalds #endif 17950f6c6392SFlorian Westphal memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key)); 1796c71099acSThomas Graf rt->rt6i_table = ort->rt6i_table; 17971da177e4SLinus Torvalds } 17981da177e4SLinus Torvalds return rt; 17991da177e4SLinus Torvalds } 18001da177e4SLinus Torvalds 180170ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 1802efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 1803b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 1804b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex) 180570ceb4f5SYOSHIFUJI Hideaki { 180670ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 180770ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt = NULL; 1808c71099acSThomas Graf struct fib6_table *table; 180970ceb4f5SYOSHIFUJI Hideaki 1810efa2cea0SDaniel Lezcano table = fib6_get_table(net, RT6_TABLE_INFO); 181138308473SDavid S. Miller if (!table) 1812c71099acSThomas Graf return NULL; 1813c71099acSThomas Graf 18145744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 1815c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0); 181670ceb4f5SYOSHIFUJI Hideaki if (!fn) 181770ceb4f5SYOSHIFUJI Hideaki goto out; 181870ceb4f5SYOSHIFUJI Hideaki 1819d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 1820d1918542SDavid S. Miller if (rt->dst.dev->ifindex != ifindex) 182170ceb4f5SYOSHIFUJI Hideaki continue; 182270ceb4f5SYOSHIFUJI Hideaki if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) 182370ceb4f5SYOSHIFUJI Hideaki continue; 182470ceb4f5SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr)) 182570ceb4f5SYOSHIFUJI Hideaki continue; 1826d8d1f30bSChangli Gao dst_hold(&rt->dst); 182770ceb4f5SYOSHIFUJI Hideaki break; 182870ceb4f5SYOSHIFUJI Hideaki } 182970ceb4f5SYOSHIFUJI Hideaki out: 18305744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 183170ceb4f5SYOSHIFUJI Hideaki return rt; 183270ceb4f5SYOSHIFUJI Hideaki } 183370ceb4f5SYOSHIFUJI Hideaki 1834efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 1835b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 1836b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex, 183795c96174SEric Dumazet unsigned int pref) 183870ceb4f5SYOSHIFUJI Hideaki { 183986872cb5SThomas Graf struct fib6_config cfg = { 184086872cb5SThomas Graf .fc_table = RT6_TABLE_INFO, 1841238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 184286872cb5SThomas Graf .fc_ifindex = ifindex, 184386872cb5SThomas Graf .fc_dst_len = prefixlen, 184486872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 184586872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 184615e47304SEric W. Biederman .fc_nlinfo.portid = 0, 1847efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 1848efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 184986872cb5SThomas Graf }; 185070ceb4f5SYOSHIFUJI Hideaki 18514e3fd7a0SAlexey Dobriyan cfg.fc_dst = *prefix; 18524e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 185386872cb5SThomas Graf 1854e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 1855e317da96SYOSHIFUJI Hideaki if (!prefixlen) 185686872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 185770ceb4f5SYOSHIFUJI Hideaki 185886872cb5SThomas Graf ip6_route_add(&cfg); 185970ceb4f5SYOSHIFUJI Hideaki 1860efa2cea0SDaniel Lezcano return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex); 186170ceb4f5SYOSHIFUJI Hideaki } 186270ceb4f5SYOSHIFUJI Hideaki #endif 186370ceb4f5SYOSHIFUJI Hideaki 1864b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev) 18651da177e4SLinus Torvalds { 18661da177e4SLinus Torvalds struct rt6_info *rt; 1867c71099acSThomas Graf struct fib6_table *table; 18681da177e4SLinus Torvalds 1869c346dca1SYOSHIFUJI Hideaki table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT); 187038308473SDavid S. Miller if (!table) 1871c71099acSThomas Graf return NULL; 18721da177e4SLinus Torvalds 18735744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 1874d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) { 1875d1918542SDavid S. Miller if (dev == rt->dst.dev && 1876045927ffSYOSHIFUJI Hideaki ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 18771da177e4SLinus Torvalds ipv6_addr_equal(&rt->rt6i_gateway, addr)) 18781da177e4SLinus Torvalds break; 18791da177e4SLinus Torvalds } 18801da177e4SLinus Torvalds if (rt) 1881d8d1f30bSChangli Gao dst_hold(&rt->dst); 18825744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 18831da177e4SLinus Torvalds return rt; 18841da177e4SLinus Torvalds } 18851da177e4SLinus Torvalds 1886b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr, 1887ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 1888ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 18891da177e4SLinus Torvalds { 189086872cb5SThomas Graf struct fib6_config cfg = { 189186872cb5SThomas Graf .fc_table = RT6_TABLE_DFLT, 1892238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 189386872cb5SThomas Graf .fc_ifindex = dev->ifindex, 189486872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 189586872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 189615e47304SEric W. Biederman .fc_nlinfo.portid = 0, 18975578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 1898c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 189986872cb5SThomas Graf }; 19001da177e4SLinus Torvalds 19014e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 19021da177e4SLinus Torvalds 190386872cb5SThomas Graf ip6_route_add(&cfg); 19041da177e4SLinus Torvalds 19051da177e4SLinus Torvalds return rt6_get_dflt_router(gwaddr, dev); 19061da177e4SLinus Torvalds } 19071da177e4SLinus Torvalds 19087b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net) 19091da177e4SLinus Torvalds { 19101da177e4SLinus Torvalds struct rt6_info *rt; 1911c71099acSThomas Graf struct fib6_table *table; 1912c71099acSThomas Graf 1913c71099acSThomas Graf /* NOTE: Keep consistent with rt6_get_dflt_router */ 19147b4da532SDaniel Lezcano table = fib6_get_table(net, RT6_TABLE_DFLT); 191538308473SDavid S. Miller if (!table) 1916c71099acSThomas Graf return; 19171da177e4SLinus Torvalds 19181da177e4SLinus Torvalds restart: 1919c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 1920d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 19211da177e4SLinus Torvalds if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) { 1922d8d1f30bSChangli Gao dst_hold(&rt->dst); 1923c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 1924e0a1ad73SThomas Graf ip6_del_rt(rt); 19251da177e4SLinus Torvalds goto restart; 19261da177e4SLinus Torvalds } 19271da177e4SLinus Torvalds } 1928c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 19291da177e4SLinus Torvalds } 19301da177e4SLinus Torvalds 19315578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 19325578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 193386872cb5SThomas Graf struct fib6_config *cfg) 193486872cb5SThomas Graf { 193586872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 193686872cb5SThomas Graf 193786872cb5SThomas Graf cfg->fc_table = RT6_TABLE_MAIN; 193886872cb5SThomas Graf cfg->fc_ifindex = rtmsg->rtmsg_ifindex; 193986872cb5SThomas Graf cfg->fc_metric = rtmsg->rtmsg_metric; 194086872cb5SThomas Graf cfg->fc_expires = rtmsg->rtmsg_info; 194186872cb5SThomas Graf cfg->fc_dst_len = rtmsg->rtmsg_dst_len; 194286872cb5SThomas Graf cfg->fc_src_len = rtmsg->rtmsg_src_len; 194386872cb5SThomas Graf cfg->fc_flags = rtmsg->rtmsg_flags; 194486872cb5SThomas Graf 19455578689aSDaniel Lezcano cfg->fc_nlinfo.nl_net = net; 1946f1243c2dSBenjamin Thery 19474e3fd7a0SAlexey Dobriyan cfg->fc_dst = rtmsg->rtmsg_dst; 19484e3fd7a0SAlexey Dobriyan cfg->fc_src = rtmsg->rtmsg_src; 19494e3fd7a0SAlexey Dobriyan cfg->fc_gateway = rtmsg->rtmsg_gateway; 195086872cb5SThomas Graf } 195186872cb5SThomas Graf 19525578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) 19531da177e4SLinus Torvalds { 195486872cb5SThomas Graf struct fib6_config cfg; 19551da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 19561da177e4SLinus Torvalds int err; 19571da177e4SLinus Torvalds 19581da177e4SLinus Torvalds switch(cmd) { 19591da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 19601da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 1961af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 19621da177e4SLinus Torvalds return -EPERM; 19631da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 19641da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 19651da177e4SLinus Torvalds if (err) 19661da177e4SLinus Torvalds return -EFAULT; 19671da177e4SLinus Torvalds 19685578689aSDaniel Lezcano rtmsg_to_fib6_config(net, &rtmsg, &cfg); 196986872cb5SThomas Graf 19701da177e4SLinus Torvalds rtnl_lock(); 19711da177e4SLinus Torvalds switch (cmd) { 19721da177e4SLinus Torvalds case SIOCADDRT: 197386872cb5SThomas Graf err = ip6_route_add(&cfg); 19741da177e4SLinus Torvalds break; 19751da177e4SLinus Torvalds case SIOCDELRT: 197686872cb5SThomas Graf err = ip6_route_del(&cfg); 19771da177e4SLinus Torvalds break; 19781da177e4SLinus Torvalds default: 19791da177e4SLinus Torvalds err = -EINVAL; 19801da177e4SLinus Torvalds } 19811da177e4SLinus Torvalds rtnl_unlock(); 19821da177e4SLinus Torvalds 19831da177e4SLinus Torvalds return err; 19843ff50b79SStephen Hemminger } 19851da177e4SLinus Torvalds 19861da177e4SLinus Torvalds return -EINVAL; 19871da177e4SLinus Torvalds } 19881da177e4SLinus Torvalds 19891da177e4SLinus Torvalds /* 19901da177e4SLinus Torvalds * Drop the packet on the floor 19911da177e4SLinus Torvalds */ 19921da177e4SLinus Torvalds 1993d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 19941da177e4SLinus Torvalds { 1995612f09e8SYOSHIFUJI Hideaki int type; 1996adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 1997612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 1998612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 19990660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 200045bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 20013bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 20023bd653c8SDenis V. Lunev IPSTATS_MIB_INADDRERRORS); 2003612f09e8SYOSHIFUJI Hideaki break; 2004612f09e8SYOSHIFUJI Hideaki } 2005612f09e8SYOSHIFUJI Hideaki /* FALLTHROUGH */ 2006612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 20073bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 20083bd653c8SDenis V. Lunev ipstats_mib_noroutes); 2009612f09e8SYOSHIFUJI Hideaki break; 2010612f09e8SYOSHIFUJI Hideaki } 20113ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 20121da177e4SLinus Torvalds kfree_skb(skb); 20131da177e4SLinus Torvalds return 0; 20141da177e4SLinus Torvalds } 20151da177e4SLinus Torvalds 20169ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 20179ce8ade0SThomas Graf { 2018612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 20199ce8ade0SThomas Graf } 20209ce8ade0SThomas Graf 202120380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb) 20221da177e4SLinus Torvalds { 2023adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 2024612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 20251da177e4SLinus Torvalds } 20261da177e4SLinus Torvalds 20276723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES 20286723ab54SDavid S. Miller 20299ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 20309ce8ade0SThomas Graf { 2031612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 20329ce8ade0SThomas Graf } 20339ce8ade0SThomas Graf 20349ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb) 20359ce8ade0SThomas Graf { 2036adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 2037612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 20389ce8ade0SThomas Graf } 20399ce8ade0SThomas Graf 20406723ab54SDavid S. Miller #endif 20416723ab54SDavid S. Miller 20421da177e4SLinus Torvalds /* 20431da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 20441da177e4SLinus Torvalds */ 20451da177e4SLinus Torvalds 20461da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, 20471da177e4SLinus Torvalds const struct in6_addr *addr, 20488f031519SDavid S. Miller bool anycast) 20491da177e4SLinus Torvalds { 2050c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 20518b96d22dSDavid S. Miller struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev, 0, NULL); 20521da177e4SLinus Torvalds 205338308473SDavid S. Miller if (!rt) { 2054f3213831SJoe Perches net_warn_ratelimited("Maximum number of routes reached, consider increasing route/max_size\n"); 20551da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 205640385653SBen Greear } 20571da177e4SLinus Torvalds 20581da177e4SLinus Torvalds in6_dev_hold(idev); 20591da177e4SLinus Torvalds 206011d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 2061d8d1f30bSChangli Gao rt->dst.input = ip6_input; 2062d8d1f30bSChangli Gao rt->dst.output = ip6_output; 20631da177e4SLinus Torvalds rt->rt6i_idev = idev; 20641da177e4SLinus Torvalds 20651da177e4SLinus Torvalds rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP; 206658c4fb86SYOSHIFUJI Hideaki if (anycast) 206758c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 206858c4fb86SYOSHIFUJI Hideaki else 20691da177e4SLinus Torvalds rt->rt6i_flags |= RTF_LOCAL; 20701da177e4SLinus Torvalds 20714e3fd7a0SAlexey Dobriyan rt->rt6i_dst.addr = *addr; 20721da177e4SLinus Torvalds rt->rt6i_dst.plen = 128; 20735578689aSDaniel Lezcano rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL); 20741da177e4SLinus Torvalds 2075d8d1f30bSChangli Gao atomic_set(&rt->dst.__refcnt, 1); 20761da177e4SLinus Torvalds 20771da177e4SLinus Torvalds return rt; 20781da177e4SLinus Torvalds } 20791da177e4SLinus Torvalds 2080c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net, 2081c3968a85SDaniel Walter struct rt6_info *rt, 2082b71d1d42SEric Dumazet const struct in6_addr *daddr, 2083c3968a85SDaniel Walter unsigned int prefs, 2084c3968a85SDaniel Walter struct in6_addr *saddr) 2085c3968a85SDaniel Walter { 2086c3968a85SDaniel Walter struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt); 2087c3968a85SDaniel Walter int err = 0; 2088c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) 20894e3fd7a0SAlexey Dobriyan *saddr = rt->rt6i_prefsrc.addr; 2090c3968a85SDaniel Walter else 2091c3968a85SDaniel Walter err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL, 2092c3968a85SDaniel Walter daddr, prefs, saddr); 2093c3968a85SDaniel Walter return err; 2094c3968a85SDaniel Walter } 2095c3968a85SDaniel Walter 2096c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 2097c3968a85SDaniel Walter struct arg_dev_net_ip { 2098c3968a85SDaniel Walter struct net_device *dev; 2099c3968a85SDaniel Walter struct net *net; 2100c3968a85SDaniel Walter struct in6_addr *addr; 2101c3968a85SDaniel Walter }; 2102c3968a85SDaniel Walter 2103c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg) 2104c3968a85SDaniel Walter { 2105c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 2106c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 2107c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 2108c3968a85SDaniel Walter 2109d1918542SDavid S. Miller if (((void *)rt->dst.dev == dev || !dev) && 2110c3968a85SDaniel Walter rt != net->ipv6.ip6_null_entry && 2111c3968a85SDaniel Walter ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) { 2112c3968a85SDaniel Walter /* remove prefsrc entry */ 2113c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 2114c3968a85SDaniel Walter } 2115c3968a85SDaniel Walter return 0; 2116c3968a85SDaniel Walter } 2117c3968a85SDaniel Walter 2118c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 2119c3968a85SDaniel Walter { 2120c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 2121c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 2122c3968a85SDaniel Walter .dev = ifp->idev->dev, 2123c3968a85SDaniel Walter .net = net, 2124c3968a85SDaniel Walter .addr = &ifp->addr, 2125c3968a85SDaniel Walter }; 2126c3968a85SDaniel Walter fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni); 2127c3968a85SDaniel Walter } 2128c3968a85SDaniel Walter 21298ed67789SDaniel Lezcano struct arg_dev_net { 21308ed67789SDaniel Lezcano struct net_device *dev; 21318ed67789SDaniel Lezcano struct net *net; 21328ed67789SDaniel Lezcano }; 21338ed67789SDaniel Lezcano 21341da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg) 21351da177e4SLinus Torvalds { 2136bc3ef660Sstephen hemminger const struct arg_dev_net *adn = arg; 2137bc3ef660Sstephen hemminger const struct net_device *dev = adn->dev; 21388ed67789SDaniel Lezcano 2139d1918542SDavid S. Miller if ((rt->dst.dev == dev || !dev) && 2140c159d30cSDavid S. Miller rt != adn->net->ipv6.ip6_null_entry) 21411da177e4SLinus Torvalds return -1; 2142c159d30cSDavid S. Miller 21431da177e4SLinus Torvalds return 0; 21441da177e4SLinus Torvalds } 21451da177e4SLinus Torvalds 2146f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev) 21471da177e4SLinus Torvalds { 21488ed67789SDaniel Lezcano struct arg_dev_net adn = { 21498ed67789SDaniel Lezcano .dev = dev, 21508ed67789SDaniel Lezcano .net = net, 21518ed67789SDaniel Lezcano }; 21528ed67789SDaniel Lezcano 21538ed67789SDaniel Lezcano fib6_clean_all(net, fib6_ifdown, 0, &adn); 21541e493d19SDavid S. Miller icmp6_clean_all(fib6_ifdown, &adn); 21551da177e4SLinus Torvalds } 21561da177e4SLinus Torvalds 215795c96174SEric Dumazet struct rt6_mtu_change_arg { 21581da177e4SLinus Torvalds struct net_device *dev; 215995c96174SEric Dumazet unsigned int mtu; 21601da177e4SLinus Torvalds }; 21611da177e4SLinus Torvalds 21621da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg) 21631da177e4SLinus Torvalds { 21641da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 21651da177e4SLinus Torvalds struct inet6_dev *idev; 21661da177e4SLinus Torvalds 21671da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 21681da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 21691da177e4SLinus Torvalds We still use this lock to block changes 21701da177e4SLinus Torvalds caused by addrconf/ndisc. 21711da177e4SLinus Torvalds */ 21721da177e4SLinus Torvalds 21731da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 217438308473SDavid S. Miller if (!idev) 21751da177e4SLinus Torvalds return 0; 21761da177e4SLinus Torvalds 21771da177e4SLinus Torvalds /* For administrative MTU increase, there is no way to discover 21781da177e4SLinus Torvalds IPv6 PMTU increase, so PMTU increase should be updated here. 21791da177e4SLinus Torvalds Since RFC 1981 doesn't include administrative MTU increase 21801da177e4SLinus Torvalds update PMTU increase is a MUST. (i.e. jumbo frame) 21811da177e4SLinus Torvalds */ 21821da177e4SLinus Torvalds /* 21831da177e4SLinus Torvalds If new MTU is less than route PMTU, this new MTU will be the 21841da177e4SLinus Torvalds lowest MTU in the path, update the route PMTU to reflect PMTU 21851da177e4SLinus Torvalds decreases; if new MTU is greater than route PMTU, and the 21861da177e4SLinus Torvalds old MTU is the lowest MTU in the path, update the route PMTU 21871da177e4SLinus Torvalds to reflect the increase. In this case if the other nodes' MTU 21881da177e4SLinus Torvalds also have the lowest MTU, TOO BIG MESSAGE will be lead to 21891da177e4SLinus Torvalds PMTU discouvery. 21901da177e4SLinus Torvalds */ 2191d1918542SDavid S. Miller if (rt->dst.dev == arg->dev && 2192d8d1f30bSChangli Gao !dst_metric_locked(&rt->dst, RTAX_MTU) && 2193d8d1f30bSChangli Gao (dst_mtu(&rt->dst) >= arg->mtu || 2194d8d1f30bSChangli Gao (dst_mtu(&rt->dst) < arg->mtu && 2195d8d1f30bSChangli Gao dst_mtu(&rt->dst) == idev->cnf.mtu6))) { 2196defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu); 2197566cfd8fSSimon Arlott } 21981da177e4SLinus Torvalds return 0; 21991da177e4SLinus Torvalds } 22001da177e4SLinus Torvalds 220195c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu) 22021da177e4SLinus Torvalds { 2203c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 2204c71099acSThomas Graf .dev = dev, 2205c71099acSThomas Graf .mtu = mtu, 2206c71099acSThomas Graf }; 22071da177e4SLinus Torvalds 2208c346dca1SYOSHIFUJI Hideaki fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg); 22091da177e4SLinus Torvalds } 22101da177e4SLinus Torvalds 2211ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 22125176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 221386872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 2214ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 221586872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 221686872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 221751ebd318SNicolas Dichtel [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 221886872cb5SThomas Graf }; 221986872cb5SThomas Graf 222086872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 222186872cb5SThomas Graf struct fib6_config *cfg) 22221da177e4SLinus Torvalds { 222386872cb5SThomas Graf struct rtmsg *rtm; 222486872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 222586872cb5SThomas Graf int err; 22261da177e4SLinus Torvalds 222786872cb5SThomas Graf err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy); 222886872cb5SThomas Graf if (err < 0) 222986872cb5SThomas Graf goto errout; 22301da177e4SLinus Torvalds 223186872cb5SThomas Graf err = -EINVAL; 223286872cb5SThomas Graf rtm = nlmsg_data(nlh); 223386872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 223486872cb5SThomas Graf 223586872cb5SThomas Graf cfg->fc_table = rtm->rtm_table; 223686872cb5SThomas Graf cfg->fc_dst_len = rtm->rtm_dst_len; 223786872cb5SThomas Graf cfg->fc_src_len = rtm->rtm_src_len; 223886872cb5SThomas Graf cfg->fc_flags = RTF_UP; 223986872cb5SThomas Graf cfg->fc_protocol = rtm->rtm_protocol; 2240ef2c7d7bSNicolas Dichtel cfg->fc_type = rtm->rtm_type; 224186872cb5SThomas Graf 2242ef2c7d7bSNicolas Dichtel if (rtm->rtm_type == RTN_UNREACHABLE || 2243ef2c7d7bSNicolas Dichtel rtm->rtm_type == RTN_BLACKHOLE || 2244b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_PROHIBIT || 2245b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_THROW) 224686872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 224786872cb5SThomas Graf 2248ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 2249ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 2250ab79ad14SMaciej Żenczykowski 225115e47304SEric W. Biederman cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid; 225286872cb5SThomas Graf cfg->fc_nlinfo.nlh = nlh; 22533b1e0a65SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = sock_net(skb->sk); 225486872cb5SThomas Graf 225586872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 225686872cb5SThomas Graf nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16); 225786872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 22581da177e4SLinus Torvalds } 225986872cb5SThomas Graf 226086872cb5SThomas Graf if (tb[RTA_DST]) { 226186872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 226286872cb5SThomas Graf 226386872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 226486872cb5SThomas Graf goto errout; 226586872cb5SThomas Graf 226686872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 22671da177e4SLinus Torvalds } 226886872cb5SThomas Graf 226986872cb5SThomas Graf if (tb[RTA_SRC]) { 227086872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 227186872cb5SThomas Graf 227286872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 227386872cb5SThomas Graf goto errout; 227486872cb5SThomas Graf 227586872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 22761da177e4SLinus Torvalds } 227786872cb5SThomas Graf 2278c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 2279c3968a85SDaniel Walter nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16); 2280c3968a85SDaniel Walter 228186872cb5SThomas Graf if (tb[RTA_OIF]) 228286872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 228386872cb5SThomas Graf 228486872cb5SThomas Graf if (tb[RTA_PRIORITY]) 228586872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 228686872cb5SThomas Graf 228786872cb5SThomas Graf if (tb[RTA_METRICS]) { 228886872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 228986872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 22901da177e4SLinus Torvalds } 229186872cb5SThomas Graf 229286872cb5SThomas Graf if (tb[RTA_TABLE]) 229386872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 229486872cb5SThomas Graf 229551ebd318SNicolas Dichtel if (tb[RTA_MULTIPATH]) { 229651ebd318SNicolas Dichtel cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); 229751ebd318SNicolas Dichtel cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); 229851ebd318SNicolas Dichtel } 229951ebd318SNicolas Dichtel 230086872cb5SThomas Graf err = 0; 230186872cb5SThomas Graf errout: 230286872cb5SThomas Graf return err; 23031da177e4SLinus Torvalds } 23041da177e4SLinus Torvalds 230551ebd318SNicolas Dichtel static int ip6_route_multipath(struct fib6_config *cfg, int add) 230651ebd318SNicolas Dichtel { 230751ebd318SNicolas Dichtel struct fib6_config r_cfg; 230851ebd318SNicolas Dichtel struct rtnexthop *rtnh; 230951ebd318SNicolas Dichtel int remaining; 231051ebd318SNicolas Dichtel int attrlen; 231151ebd318SNicolas Dichtel int err = 0, last_err = 0; 231251ebd318SNicolas Dichtel 231351ebd318SNicolas Dichtel beginning: 231451ebd318SNicolas Dichtel rtnh = (struct rtnexthop *)cfg->fc_mp; 231551ebd318SNicolas Dichtel remaining = cfg->fc_mp_len; 231651ebd318SNicolas Dichtel 231751ebd318SNicolas Dichtel /* Parse a Multipath Entry */ 231851ebd318SNicolas Dichtel while (rtnh_ok(rtnh, remaining)) { 231951ebd318SNicolas Dichtel memcpy(&r_cfg, cfg, sizeof(*cfg)); 232051ebd318SNicolas Dichtel if (rtnh->rtnh_ifindex) 232151ebd318SNicolas Dichtel r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 232251ebd318SNicolas Dichtel 232351ebd318SNicolas Dichtel attrlen = rtnh_attrlen(rtnh); 232451ebd318SNicolas Dichtel if (attrlen > 0) { 232551ebd318SNicolas Dichtel struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 232651ebd318SNicolas Dichtel 232751ebd318SNicolas Dichtel nla = nla_find(attrs, attrlen, RTA_GATEWAY); 232851ebd318SNicolas Dichtel if (nla) { 232951ebd318SNicolas Dichtel nla_memcpy(&r_cfg.fc_gateway, nla, 16); 233051ebd318SNicolas Dichtel r_cfg.fc_flags |= RTF_GATEWAY; 233151ebd318SNicolas Dichtel } 233251ebd318SNicolas Dichtel } 233351ebd318SNicolas Dichtel err = add ? ip6_route_add(&r_cfg) : ip6_route_del(&r_cfg); 233451ebd318SNicolas Dichtel if (err) { 233551ebd318SNicolas Dichtel last_err = err; 233651ebd318SNicolas Dichtel /* If we are trying to remove a route, do not stop the 233751ebd318SNicolas Dichtel * loop when ip6_route_del() fails (because next hop is 233851ebd318SNicolas Dichtel * already gone), we should try to remove all next hops. 233951ebd318SNicolas Dichtel */ 234051ebd318SNicolas Dichtel if (add) { 234151ebd318SNicolas Dichtel /* If add fails, we should try to delete all 234251ebd318SNicolas Dichtel * next hops that have been already added. 234351ebd318SNicolas Dichtel */ 234451ebd318SNicolas Dichtel add = 0; 234551ebd318SNicolas Dichtel goto beginning; 234651ebd318SNicolas Dichtel } 234751ebd318SNicolas Dichtel } 23481a72418bSNicolas Dichtel /* Because each route is added like a single route we remove 23491a72418bSNicolas Dichtel * this flag after the first nexthop (if there is a collision, 23501a72418bSNicolas Dichtel * we have already fail to add the first nexthop: 23511a72418bSNicolas Dichtel * fib6_add_rt2node() has reject it). 23521a72418bSNicolas Dichtel */ 23531a72418bSNicolas Dichtel cfg->fc_nlinfo.nlh->nlmsg_flags &= ~NLM_F_EXCL; 235451ebd318SNicolas Dichtel rtnh = rtnh_next(rtnh, &remaining); 235551ebd318SNicolas Dichtel } 235651ebd318SNicolas Dichtel 235751ebd318SNicolas Dichtel return last_err; 235851ebd318SNicolas Dichtel } 235951ebd318SNicolas Dichtel 2360c127ea2cSThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 23611da177e4SLinus Torvalds { 236286872cb5SThomas Graf struct fib6_config cfg; 236386872cb5SThomas Graf int err; 23641da177e4SLinus Torvalds 236586872cb5SThomas Graf err = rtm_to_fib6_config(skb, nlh, &cfg); 236686872cb5SThomas Graf if (err < 0) 236786872cb5SThomas Graf return err; 236886872cb5SThomas Graf 236951ebd318SNicolas Dichtel if (cfg.fc_mp) 237051ebd318SNicolas Dichtel return ip6_route_multipath(&cfg, 0); 237151ebd318SNicolas Dichtel else 237286872cb5SThomas Graf return ip6_route_del(&cfg); 23731da177e4SLinus Torvalds } 23741da177e4SLinus Torvalds 2375c127ea2cSThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 23761da177e4SLinus Torvalds { 237786872cb5SThomas Graf struct fib6_config cfg; 237886872cb5SThomas Graf int err; 23791da177e4SLinus Torvalds 238086872cb5SThomas Graf err = rtm_to_fib6_config(skb, nlh, &cfg); 238186872cb5SThomas Graf if (err < 0) 238286872cb5SThomas Graf return err; 238386872cb5SThomas Graf 238451ebd318SNicolas Dichtel if (cfg.fc_mp) 238551ebd318SNicolas Dichtel return ip6_route_multipath(&cfg, 1); 238651ebd318SNicolas Dichtel else 238786872cb5SThomas Graf return ip6_route_add(&cfg); 23881da177e4SLinus Torvalds } 23891da177e4SLinus Torvalds 2390339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void) 2391339bf98fSThomas Graf { 2392339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 2393339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 2394339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 2395339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 2396339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 2397339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 2398339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 2399339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 2400339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 24016a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 2402339bf98fSThomas Graf + nla_total_size(sizeof(struct rta_cacheinfo)); 2403339bf98fSThomas Graf } 2404339bf98fSThomas Graf 2405191cd582SBrian Haley static int rt6_fill_node(struct net *net, 2406191cd582SBrian Haley struct sk_buff *skb, struct rt6_info *rt, 24070d51aa80SJamal Hadi Salim struct in6_addr *dst, struct in6_addr *src, 240815e47304SEric W. Biederman int iif, int type, u32 portid, u32 seq, 24097bc570c8SYOSHIFUJI Hideaki int prefix, int nowait, unsigned int flags) 24101da177e4SLinus Torvalds { 24111da177e4SLinus Torvalds struct rtmsg *rtm; 24121da177e4SLinus Torvalds struct nlmsghdr *nlh; 2413e3703b3dSThomas Graf long expires; 24149e762a4aSPatrick McHardy u32 table; 24151da177e4SLinus Torvalds 24161da177e4SLinus Torvalds if (prefix) { /* user wants prefix routes only */ 24171da177e4SLinus Torvalds if (!(rt->rt6i_flags & RTF_PREFIX_RT)) { 24181da177e4SLinus Torvalds /* success since this is not a prefix route */ 24191da177e4SLinus Torvalds return 1; 24201da177e4SLinus Torvalds } 24211da177e4SLinus Torvalds } 24221da177e4SLinus Torvalds 242315e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); 242438308473SDavid S. Miller if (!nlh) 242526932566SPatrick McHardy return -EMSGSIZE; 24262d7202bfSThomas Graf 24272d7202bfSThomas Graf rtm = nlmsg_data(nlh); 24281da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 24291da177e4SLinus Torvalds rtm->rtm_dst_len = rt->rt6i_dst.plen; 24301da177e4SLinus Torvalds rtm->rtm_src_len = rt->rt6i_src.plen; 24311da177e4SLinus Torvalds rtm->rtm_tos = 0; 2432c71099acSThomas Graf if (rt->rt6i_table) 24339e762a4aSPatrick McHardy table = rt->rt6i_table->tb6_id; 2434c71099acSThomas Graf else 24359e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 24369e762a4aSPatrick McHardy rtm->rtm_table = table; 2437c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, table)) 2438c78679e8SDavid S. Miller goto nla_put_failure; 2439ef2c7d7bSNicolas Dichtel if (rt->rt6i_flags & RTF_REJECT) { 2440ef2c7d7bSNicolas Dichtel switch (rt->dst.error) { 2441ef2c7d7bSNicolas Dichtel case -EINVAL: 2442ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_BLACKHOLE; 2443ef2c7d7bSNicolas Dichtel break; 2444ef2c7d7bSNicolas Dichtel case -EACCES: 2445ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_PROHIBIT; 2446ef2c7d7bSNicolas Dichtel break; 2447b4949ab2SNicolas Dichtel case -EAGAIN: 2448b4949ab2SNicolas Dichtel rtm->rtm_type = RTN_THROW; 2449b4949ab2SNicolas Dichtel break; 2450ef2c7d7bSNicolas Dichtel default: 24511da177e4SLinus Torvalds rtm->rtm_type = RTN_UNREACHABLE; 2452ef2c7d7bSNicolas Dichtel break; 2453ef2c7d7bSNicolas Dichtel } 2454ef2c7d7bSNicolas Dichtel } 2455ab79ad14SMaciej Żenczykowski else if (rt->rt6i_flags & RTF_LOCAL) 2456ab79ad14SMaciej Żenczykowski rtm->rtm_type = RTN_LOCAL; 2457d1918542SDavid S. Miller else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK)) 24581da177e4SLinus Torvalds rtm->rtm_type = RTN_LOCAL; 24591da177e4SLinus Torvalds else 24601da177e4SLinus Torvalds rtm->rtm_type = RTN_UNICAST; 24611da177e4SLinus Torvalds rtm->rtm_flags = 0; 24621da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 24631da177e4SLinus Torvalds rtm->rtm_protocol = rt->rt6i_protocol; 24641da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_DYNAMIC) 24651da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_REDIRECT; 2466f0396f60SDenis Ovsienko else if (rt->rt6i_flags & RTF_ADDRCONF) { 2467f0396f60SDenis Ovsienko if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO)) 24681da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_RA; 2469f0396f60SDenis Ovsienko else 2470f0396f60SDenis Ovsienko rtm->rtm_protocol = RTPROT_KERNEL; 2471f0396f60SDenis Ovsienko } 24721da177e4SLinus Torvalds 24731da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) 24741da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 24751da177e4SLinus Torvalds 24761da177e4SLinus Torvalds if (dst) { 2477c78679e8SDavid S. Miller if (nla_put(skb, RTA_DST, 16, dst)) 2478c78679e8SDavid S. Miller goto nla_put_failure; 24791da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 24801da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 2481c78679e8SDavid S. Miller if (nla_put(skb, RTA_DST, 16, &rt->rt6i_dst.addr)) 2482c78679e8SDavid S. Miller goto nla_put_failure; 24831da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 24841da177e4SLinus Torvalds if (src) { 2485c78679e8SDavid S. Miller if (nla_put(skb, RTA_SRC, 16, src)) 2486c78679e8SDavid S. Miller goto nla_put_failure; 24871da177e4SLinus Torvalds rtm->rtm_src_len = 128; 2488c78679e8SDavid S. Miller } else if (rtm->rtm_src_len && 2489c78679e8SDavid S. Miller nla_put(skb, RTA_SRC, 16, &rt->rt6i_src.addr)) 2490c78679e8SDavid S. Miller goto nla_put_failure; 24911da177e4SLinus Torvalds #endif 24927bc570c8SYOSHIFUJI Hideaki if (iif) { 24937bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 24947bc570c8SYOSHIFUJI Hideaki if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) { 24958229efdaSBenjamin Thery int err = ip6mr_get_route(net, skb, rtm, nowait); 24967bc570c8SYOSHIFUJI Hideaki if (err <= 0) { 24977bc570c8SYOSHIFUJI Hideaki if (!nowait) { 24987bc570c8SYOSHIFUJI Hideaki if (err == 0) 24997bc570c8SYOSHIFUJI Hideaki return 0; 25007bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 25017bc570c8SYOSHIFUJI Hideaki } else { 25027bc570c8SYOSHIFUJI Hideaki if (err == -EMSGSIZE) 25037bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 25047bc570c8SYOSHIFUJI Hideaki } 25057bc570c8SYOSHIFUJI Hideaki } 25067bc570c8SYOSHIFUJI Hideaki } else 25077bc570c8SYOSHIFUJI Hideaki #endif 2508c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_IIF, iif)) 2509c78679e8SDavid S. Miller goto nla_put_failure; 25107bc570c8SYOSHIFUJI Hideaki } else if (dst) { 25111da177e4SLinus Torvalds struct in6_addr saddr_buf; 2512c78679e8SDavid S. Miller if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 && 2513c78679e8SDavid S. Miller nla_put(skb, RTA_PREFSRC, 16, &saddr_buf)) 2514c78679e8SDavid S. Miller goto nla_put_failure; 2515c3968a85SDaniel Walter } 2516c3968a85SDaniel Walter 2517c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) { 2518c3968a85SDaniel Walter struct in6_addr saddr_buf; 25194e3fd7a0SAlexey Dobriyan saddr_buf = rt->rt6i_prefsrc.addr; 2520c78679e8SDavid S. Miller if (nla_put(skb, RTA_PREFSRC, 16, &saddr_buf)) 2521c78679e8SDavid S. Miller goto nla_put_failure; 25221da177e4SLinus Torvalds } 25232d7202bfSThomas Graf 2524defb3519SDavid S. Miller if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0) 25252d7202bfSThomas Graf goto nla_put_failure; 25262d7202bfSThomas Graf 2527dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_GATEWAY) { 2528dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 if (nla_put(skb, RTA_GATEWAY, 16, &rt->rt6i_gateway) < 0) 252994f826b8SEric Dumazet goto nla_put_failure; 253094f826b8SEric Dumazet } 25312d7202bfSThomas Graf 2532c78679e8SDavid S. Miller if (rt->dst.dev && 2533c78679e8SDavid S. Miller nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex)) 2534c78679e8SDavid S. Miller goto nla_put_failure; 2535c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric)) 2536c78679e8SDavid S. Miller goto nla_put_failure; 25378253947eSLi Wei 25388253947eSLi Wei expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0; 253969cdf8f9SYOSHIFUJI Hideaki 254087a50699SDavid S. Miller if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0) 2541e3703b3dSThomas Graf goto nla_put_failure; 25421da177e4SLinus Torvalds 25432d7202bfSThomas Graf return nlmsg_end(skb, nlh); 25442d7202bfSThomas Graf 25452d7202bfSThomas Graf nla_put_failure: 254626932566SPatrick McHardy nlmsg_cancel(skb, nlh); 254726932566SPatrick McHardy return -EMSGSIZE; 25481da177e4SLinus Torvalds } 25491da177e4SLinus Torvalds 25501b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg) 25511da177e4SLinus Torvalds { 25521da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 25531da177e4SLinus Torvalds int prefix; 25541da177e4SLinus Torvalds 25552d7202bfSThomas Graf if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { 25562d7202bfSThomas Graf struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); 25571da177e4SLinus Torvalds prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0; 25581da177e4SLinus Torvalds } else 25591da177e4SLinus Torvalds prefix = 0; 25601da177e4SLinus Torvalds 2561191cd582SBrian Haley return rt6_fill_node(arg->net, 2562191cd582SBrian Haley arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE, 256315e47304SEric W. Biederman NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq, 25647bc570c8SYOSHIFUJI Hideaki prefix, 0, NLM_F_MULTI); 25651da177e4SLinus Torvalds } 25661da177e4SLinus Torvalds 2567c127ea2cSThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg) 25681da177e4SLinus Torvalds { 25693b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 2570ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 25711da177e4SLinus Torvalds struct rt6_info *rt; 2572ab364a6fSThomas Graf struct sk_buff *skb; 2573ab364a6fSThomas Graf struct rtmsg *rtm; 25744c9483b2SDavid S. Miller struct flowi6 fl6; 257572331bc0SShmulik Ladkani int err, iif = 0, oif = 0; 2576ab364a6fSThomas Graf 2577ab364a6fSThomas Graf err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy); 2578ab364a6fSThomas Graf if (err < 0) 2579ab364a6fSThomas Graf goto errout; 2580ab364a6fSThomas Graf 2581ab364a6fSThomas Graf err = -EINVAL; 25824c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 2583ab364a6fSThomas Graf 2584ab364a6fSThomas Graf if (tb[RTA_SRC]) { 2585ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 2586ab364a6fSThomas Graf goto errout; 2587ab364a6fSThomas Graf 25884e3fd7a0SAlexey Dobriyan fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 2589ab364a6fSThomas Graf } 2590ab364a6fSThomas Graf 2591ab364a6fSThomas Graf if (tb[RTA_DST]) { 2592ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 2593ab364a6fSThomas Graf goto errout; 2594ab364a6fSThomas Graf 25954e3fd7a0SAlexey Dobriyan fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 2596ab364a6fSThomas Graf } 2597ab364a6fSThomas Graf 2598ab364a6fSThomas Graf if (tb[RTA_IIF]) 2599ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 2600ab364a6fSThomas Graf 2601ab364a6fSThomas Graf if (tb[RTA_OIF]) 260272331bc0SShmulik Ladkani oif = nla_get_u32(tb[RTA_OIF]); 2603ab364a6fSThomas Graf 2604ab364a6fSThomas Graf if (iif) { 2605ab364a6fSThomas Graf struct net_device *dev; 260672331bc0SShmulik Ladkani int flags = 0; 260772331bc0SShmulik Ladkani 26085578689aSDaniel Lezcano dev = __dev_get_by_index(net, iif); 2609ab364a6fSThomas Graf if (!dev) { 2610ab364a6fSThomas Graf err = -ENODEV; 2611ab364a6fSThomas Graf goto errout; 2612ab364a6fSThomas Graf } 261372331bc0SShmulik Ladkani 261472331bc0SShmulik Ladkani fl6.flowi6_iif = iif; 261572331bc0SShmulik Ladkani 261672331bc0SShmulik Ladkani if (!ipv6_addr_any(&fl6.saddr)) 261772331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_HAS_SADDR; 261872331bc0SShmulik Ladkani 261972331bc0SShmulik Ladkani rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6, 262072331bc0SShmulik Ladkani flags); 262172331bc0SShmulik Ladkani } else { 262272331bc0SShmulik Ladkani fl6.flowi6_oif = oif; 262372331bc0SShmulik Ladkani 262472331bc0SShmulik Ladkani rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6); 2625ab364a6fSThomas Graf } 26261da177e4SLinus Torvalds 26271da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 262838308473SDavid S. Miller if (!skb) { 262994e187c0SAmerigo Wang ip6_rt_put(rt); 2630ab364a6fSThomas Graf err = -ENOBUFS; 2631ab364a6fSThomas Graf goto errout; 2632ab364a6fSThomas Graf } 26331da177e4SLinus Torvalds 26341da177e4SLinus Torvalds /* Reserve room for dummy headers, this skb can pass 26351da177e4SLinus Torvalds through good chunk of routing engine. 26361da177e4SLinus Torvalds */ 2637459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 26381da177e4SLinus Torvalds skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr)); 26391da177e4SLinus Torvalds 2640d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 26411da177e4SLinus Torvalds 26424c9483b2SDavid S. Miller err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif, 264315e47304SEric W. Biederman RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 26447bc570c8SYOSHIFUJI Hideaki nlh->nlmsg_seq, 0, 0, 0); 26451da177e4SLinus Torvalds if (err < 0) { 2646ab364a6fSThomas Graf kfree_skb(skb); 2647ab364a6fSThomas Graf goto errout; 26481da177e4SLinus Torvalds } 26491da177e4SLinus Torvalds 265015e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 2651ab364a6fSThomas Graf errout: 26521da177e4SLinus Torvalds return err; 26531da177e4SLinus Torvalds } 26541da177e4SLinus Torvalds 265586872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info) 26561da177e4SLinus Torvalds { 26571da177e4SLinus Torvalds struct sk_buff *skb; 26585578689aSDaniel Lezcano struct net *net = info->nl_net; 2659528c4cebSDenis V. Lunev u32 seq; 2660528c4cebSDenis V. Lunev int err; 26610d51aa80SJamal Hadi Salim 2662528c4cebSDenis V. Lunev err = -ENOBUFS; 266338308473SDavid S. Miller seq = info->nlh ? info->nlh->nlmsg_seq : 0; 266486872cb5SThomas Graf 2665339bf98fSThomas Graf skb = nlmsg_new(rt6_nlmsg_size(), gfp_any()); 266638308473SDavid S. Miller if (!skb) 266721713ebcSThomas Graf goto errout; 26681da177e4SLinus Torvalds 2669191cd582SBrian Haley err = rt6_fill_node(net, skb, rt, NULL, NULL, 0, 267015e47304SEric W. Biederman event, info->portid, seq, 0, 0, 0); 267126932566SPatrick McHardy if (err < 0) { 267226932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 267326932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 267426932566SPatrick McHardy kfree_skb(skb); 267526932566SPatrick McHardy goto errout; 267626932566SPatrick McHardy } 267715e47304SEric W. Biederman rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 26785578689aSDaniel Lezcano info->nlh, gfp_any()); 26791ce85fe4SPablo Neira Ayuso return; 268021713ebcSThomas Graf errout: 268121713ebcSThomas Graf if (err < 0) 26825578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 26831da177e4SLinus Torvalds } 26841da177e4SLinus Torvalds 26858ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 26868ed67789SDaniel Lezcano unsigned long event, void *data) 26878ed67789SDaniel Lezcano { 26888ed67789SDaniel Lezcano struct net_device *dev = (struct net_device *)data; 2689c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 26908ed67789SDaniel Lezcano 26918ed67789SDaniel Lezcano if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) { 2692d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 26938ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 26948ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 2695d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 26968ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 2697d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 26988ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 26998ed67789SDaniel Lezcano #endif 27008ed67789SDaniel Lezcano } 27018ed67789SDaniel Lezcano 27028ed67789SDaniel Lezcano return NOTIFY_OK; 27038ed67789SDaniel Lezcano } 27048ed67789SDaniel Lezcano 27051da177e4SLinus Torvalds /* 27061da177e4SLinus Torvalds * /proc 27071da177e4SLinus Torvalds */ 27081da177e4SLinus Torvalds 27091da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 27101da177e4SLinus Torvalds 27111da177e4SLinus Torvalds struct rt6_proc_arg 27121da177e4SLinus Torvalds { 27131da177e4SLinus Torvalds char *buffer; 27141da177e4SLinus Torvalds int offset; 27151da177e4SLinus Torvalds int length; 27161da177e4SLinus Torvalds int skip; 27171da177e4SLinus Torvalds int len; 27181da177e4SLinus Torvalds }; 27191da177e4SLinus Torvalds 27201da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg) 27211da177e4SLinus Torvalds { 272233120b30SAlexey Dobriyan struct seq_file *m = p_arg; 27231da177e4SLinus Torvalds 27244b7a4274SHarvey Harrison seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen); 27251da177e4SLinus Torvalds 27261da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 27274b7a4274SHarvey Harrison seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen); 27281da177e4SLinus Torvalds #else 272933120b30SAlexey Dobriyan seq_puts(m, "00000000000000000000000000000000 00 "); 27301da177e4SLinus Torvalds #endif 2731dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_GATEWAY) { 2732dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 seq_printf(m, "%pi6", &rt->rt6i_gateway); 27331da177e4SLinus Torvalds } else { 273433120b30SAlexey Dobriyan seq_puts(m, "00000000000000000000000000000000"); 27351da177e4SLinus Torvalds } 273633120b30SAlexey Dobriyan seq_printf(m, " %08x %08x %08x %08x %8s\n", 2737d8d1f30bSChangli Gao rt->rt6i_metric, atomic_read(&rt->dst.__refcnt), 2738d8d1f30bSChangli Gao rt->dst.__use, rt->rt6i_flags, 2739d1918542SDavid S. Miller rt->dst.dev ? rt->dst.dev->name : ""); 27401da177e4SLinus Torvalds return 0; 27411da177e4SLinus Torvalds } 27421da177e4SLinus Torvalds 274333120b30SAlexey Dobriyan static int ipv6_route_show(struct seq_file *m, void *v) 27441da177e4SLinus Torvalds { 2745f3db4851SDaniel Lezcano struct net *net = (struct net *)m->private; 274632b293a5SJosh Hunt fib6_clean_all_ro(net, rt6_info_route, 0, m); 274733120b30SAlexey Dobriyan return 0; 27481da177e4SLinus Torvalds } 27491da177e4SLinus Torvalds 275033120b30SAlexey Dobriyan static int ipv6_route_open(struct inode *inode, struct file *file) 275133120b30SAlexey Dobriyan { 2752de05c557SPavel Emelyanov return single_open_net(inode, file, ipv6_route_show); 2753f3db4851SDaniel Lezcano } 2754f3db4851SDaniel Lezcano 275533120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = { 275633120b30SAlexey Dobriyan .owner = THIS_MODULE, 275733120b30SAlexey Dobriyan .open = ipv6_route_open, 275833120b30SAlexey Dobriyan .read = seq_read, 275933120b30SAlexey Dobriyan .llseek = seq_lseek, 2760b6fcbdb4SPavel Emelyanov .release = single_release_net, 276133120b30SAlexey Dobriyan }; 276233120b30SAlexey Dobriyan 27631da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 27641da177e4SLinus Torvalds { 276569ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 27661da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 276769ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 276869ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 276969ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_alloc, 277069ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 277169ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 2772fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 277369ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 27741da177e4SLinus Torvalds 27751da177e4SLinus Torvalds return 0; 27761da177e4SLinus Torvalds } 27771da177e4SLinus Torvalds 27781da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file) 27791da177e4SLinus Torvalds { 2780de05c557SPavel Emelyanov return single_open_net(inode, file, rt6_stats_seq_show); 278169ddb805SDaniel Lezcano } 278269ddb805SDaniel Lezcano 27839a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = { 27841da177e4SLinus Torvalds .owner = THIS_MODULE, 27851da177e4SLinus Torvalds .open = rt6_stats_seq_open, 27861da177e4SLinus Torvalds .read = seq_read, 27871da177e4SLinus Torvalds .llseek = seq_lseek, 2788b6fcbdb4SPavel Emelyanov .release = single_release_net, 27891da177e4SLinus Torvalds }; 27901da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 27911da177e4SLinus Torvalds 27921da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 27931da177e4SLinus Torvalds 27941da177e4SLinus Torvalds static 27958d65af78SAlexey Dobriyan int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write, 27961da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 27971da177e4SLinus Torvalds { 2798c486da34SLucian Adrian Grijincu struct net *net; 2799c486da34SLucian Adrian Grijincu int delay; 2800c486da34SLucian Adrian Grijincu if (!write) 2801c486da34SLucian Adrian Grijincu return -EINVAL; 2802c486da34SLucian Adrian Grijincu 2803c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 2804c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 28058d65af78SAlexey Dobriyan proc_dointvec(ctl, write, buffer, lenp, ppos); 28065b7c931dSDaniel Lezcano fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net); 28071da177e4SLinus Torvalds return 0; 28081da177e4SLinus Torvalds } 28091da177e4SLinus Torvalds 2810760f2d01SDaniel Lezcano ctl_table ipv6_route_table_template[] = { 28111da177e4SLinus Torvalds { 28121da177e4SLinus Torvalds .procname = "flush", 28134990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 28141da177e4SLinus Torvalds .maxlen = sizeof(int), 281589c8b3a1SDave Jones .mode = 0200, 28166d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 28171da177e4SLinus Torvalds }, 28181da177e4SLinus Torvalds { 28191da177e4SLinus Torvalds .procname = "gc_thresh", 28209a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 28211da177e4SLinus Torvalds .maxlen = sizeof(int), 28221da177e4SLinus Torvalds .mode = 0644, 28236d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 28241da177e4SLinus Torvalds }, 28251da177e4SLinus Torvalds { 28261da177e4SLinus Torvalds .procname = "max_size", 28274990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 28281da177e4SLinus Torvalds .maxlen = sizeof(int), 28291da177e4SLinus Torvalds .mode = 0644, 28306d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 28311da177e4SLinus Torvalds }, 28321da177e4SLinus Torvalds { 28331da177e4SLinus Torvalds .procname = "gc_min_interval", 28344990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 28351da177e4SLinus Torvalds .maxlen = sizeof(int), 28361da177e4SLinus Torvalds .mode = 0644, 28376d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 28381da177e4SLinus Torvalds }, 28391da177e4SLinus Torvalds { 28401da177e4SLinus Torvalds .procname = "gc_timeout", 28414990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 28421da177e4SLinus Torvalds .maxlen = sizeof(int), 28431da177e4SLinus Torvalds .mode = 0644, 28446d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 28451da177e4SLinus Torvalds }, 28461da177e4SLinus Torvalds { 28471da177e4SLinus Torvalds .procname = "gc_interval", 28484990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 28491da177e4SLinus Torvalds .maxlen = sizeof(int), 28501da177e4SLinus Torvalds .mode = 0644, 28516d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 28521da177e4SLinus Torvalds }, 28531da177e4SLinus Torvalds { 28541da177e4SLinus Torvalds .procname = "gc_elasticity", 28554990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 28561da177e4SLinus Torvalds .maxlen = sizeof(int), 28571da177e4SLinus Torvalds .mode = 0644, 2858f3d3f616SMin Zhang .proc_handler = proc_dointvec, 28591da177e4SLinus Torvalds }, 28601da177e4SLinus Torvalds { 28611da177e4SLinus Torvalds .procname = "mtu_expires", 28624990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 28631da177e4SLinus Torvalds .maxlen = sizeof(int), 28641da177e4SLinus Torvalds .mode = 0644, 28656d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 28661da177e4SLinus Torvalds }, 28671da177e4SLinus Torvalds { 28681da177e4SLinus Torvalds .procname = "min_adv_mss", 28694990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 28701da177e4SLinus Torvalds .maxlen = sizeof(int), 28711da177e4SLinus Torvalds .mode = 0644, 2872f3d3f616SMin Zhang .proc_handler = proc_dointvec, 28731da177e4SLinus Torvalds }, 28741da177e4SLinus Torvalds { 28751da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 28764990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 28771da177e4SLinus Torvalds .maxlen = sizeof(int), 28781da177e4SLinus Torvalds .mode = 0644, 28796d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 28801da177e4SLinus Torvalds }, 2881f8572d8fSEric W. Biederman { } 28821da177e4SLinus Torvalds }; 28831da177e4SLinus Torvalds 28842c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 2885760f2d01SDaniel Lezcano { 2886760f2d01SDaniel Lezcano struct ctl_table *table; 2887760f2d01SDaniel Lezcano 2888760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 2889760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 2890760f2d01SDaniel Lezcano GFP_KERNEL); 28915ee09105SYOSHIFUJI Hideaki 28925ee09105SYOSHIFUJI Hideaki if (table) { 28935ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 2894c486da34SLucian Adrian Grijincu table[0].extra1 = net; 289586393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 28965ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 28975ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 28985ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 28995ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 29005ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 29015ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 29025ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 29039c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 2904464dc801SEric W. Biederman 2905464dc801SEric W. Biederman /* Don't export sysctls to unprivileged users */ 2906464dc801SEric W. Biederman if (net->user_ns != &init_user_ns) 2907464dc801SEric W. Biederman table[0].procname = NULL; 29085ee09105SYOSHIFUJI Hideaki } 29095ee09105SYOSHIFUJI Hideaki 2910760f2d01SDaniel Lezcano return table; 2911760f2d01SDaniel Lezcano } 29121da177e4SLinus Torvalds #endif 29131da177e4SLinus Torvalds 29142c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 2915cdb18761SDaniel Lezcano { 2916633d424bSPavel Emelyanov int ret = -ENOMEM; 29178ed67789SDaniel Lezcano 291886393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 291986393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 2920f2fc6a54SBenjamin Thery 2921fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 2922fc66f95cSEric Dumazet goto out_ip6_dst_ops; 2923fc66f95cSEric Dumazet 29248ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 29258ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 29268ed67789SDaniel Lezcano GFP_KERNEL); 29278ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 2928fc66f95cSEric Dumazet goto out_ip6_dst_entries; 2929d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.path = 29308ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_null_entry; 2931d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 293262fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 293362fa8a84SDavid S. Miller ip6_template_metrics, true); 29348ed67789SDaniel Lezcano 29358ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 29368ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 29378ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 29388ed67789SDaniel Lezcano GFP_KERNEL); 293968fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 294068fffc67SPeter Zijlstra goto out_ip6_null_entry; 2941d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.path = 29428ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_prohibit_entry; 2943d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 294462fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 294562fa8a84SDavid S. Miller ip6_template_metrics, true); 29468ed67789SDaniel Lezcano 29478ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 29488ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 29498ed67789SDaniel Lezcano GFP_KERNEL); 295068fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 295168fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 2952d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.path = 29538ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_blk_hole_entry; 2954d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 295562fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 295662fa8a84SDavid S. Miller ip6_template_metrics, true); 29578ed67789SDaniel Lezcano #endif 29588ed67789SDaniel Lezcano 2959b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 2960b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 2961b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 2962b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 2963b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 2964b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 2965b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 2966b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 2967b339a47cSPeter Zijlstra 29686891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 29696891a346SBenjamin Thery 29708ed67789SDaniel Lezcano ret = 0; 29718ed67789SDaniel Lezcano out: 29728ed67789SDaniel Lezcano return ret; 2973f2fc6a54SBenjamin Thery 297468fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 297568fffc67SPeter Zijlstra out_ip6_prohibit_entry: 297668fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 297768fffc67SPeter Zijlstra out_ip6_null_entry: 297868fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 297968fffc67SPeter Zijlstra #endif 2980fc66f95cSEric Dumazet out_ip6_dst_entries: 2981fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 2982f2fc6a54SBenjamin Thery out_ip6_dst_ops: 2983f2fc6a54SBenjamin Thery goto out; 2984cdb18761SDaniel Lezcano } 2985cdb18761SDaniel Lezcano 29862c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 2987cdb18761SDaniel Lezcano { 29888ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 29898ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 29908ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 29918ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 29928ed67789SDaniel Lezcano #endif 299341bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 2994cdb18761SDaniel Lezcano } 2995cdb18761SDaniel Lezcano 2996d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net) 2997d189634eSThomas Graf { 2998d189634eSThomas Graf #ifdef CONFIG_PROC_FS 2999d189634eSThomas Graf proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops); 3000d189634eSThomas Graf proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops); 3001d189634eSThomas Graf #endif 3002d189634eSThomas Graf return 0; 3003d189634eSThomas Graf } 3004d189634eSThomas Graf 3005d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net) 3006d189634eSThomas Graf { 3007d189634eSThomas Graf #ifdef CONFIG_PROC_FS 3008d189634eSThomas Graf proc_net_remove(net, "ipv6_route"); 3009d189634eSThomas Graf proc_net_remove(net, "rt6_stats"); 3010d189634eSThomas Graf #endif 3011d189634eSThomas Graf } 3012d189634eSThomas Graf 3013cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 3014cdb18761SDaniel Lezcano .init = ip6_route_net_init, 3015cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 3016cdb18761SDaniel Lezcano }; 3017cdb18761SDaniel Lezcano 3018c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net) 3019c3426b47SDavid S. Miller { 3020c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 3021c3426b47SDavid S. Miller 3022c3426b47SDavid S. Miller if (!bp) 3023c3426b47SDavid S. Miller return -ENOMEM; 3024c3426b47SDavid S. Miller inet_peer_base_init(bp); 3025c3426b47SDavid S. Miller net->ipv6.peers = bp; 3026c3426b47SDavid S. Miller return 0; 3027c3426b47SDavid S. Miller } 3028c3426b47SDavid S. Miller 3029c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net) 3030c3426b47SDavid S. Miller { 3031c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv6.peers; 3032c3426b47SDavid S. Miller 3033c3426b47SDavid S. Miller net->ipv6.peers = NULL; 303456a6b248SDavid S. Miller inetpeer_invalidate_tree(bp); 3035c3426b47SDavid S. Miller kfree(bp); 3036c3426b47SDavid S. Miller } 3037c3426b47SDavid S. Miller 30382b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = { 3039c3426b47SDavid S. Miller .init = ipv6_inetpeer_init, 3040c3426b47SDavid S. Miller .exit = ipv6_inetpeer_exit, 3041c3426b47SDavid S. Miller }; 3042c3426b47SDavid S. Miller 3043d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = { 3044d189634eSThomas Graf .init = ip6_route_net_init_late, 3045d189634eSThomas Graf .exit = ip6_route_net_exit_late, 3046d189634eSThomas Graf }; 3047d189634eSThomas Graf 30488ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 30498ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 30508ed67789SDaniel Lezcano .priority = 0, 30518ed67789SDaniel Lezcano }; 30528ed67789SDaniel Lezcano 3053433d49c3SDaniel Lezcano int __init ip6_route_init(void) 30541da177e4SLinus Torvalds { 3055433d49c3SDaniel Lezcano int ret; 3056433d49c3SDaniel Lezcano 30579a7ec3a9SDaniel Lezcano ret = -ENOMEM; 30589a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 30599a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 30609a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 30619a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 3062c19a28e1SFernando Carrijo goto out; 306314e50e57SDavid S. Miller 3064fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 30658ed67789SDaniel Lezcano if (ret) 3066bdb3289fSDaniel Lezcano goto out_kmem_cache; 3067bdb3289fSDaniel Lezcano 3068c3426b47SDavid S. Miller ret = register_pernet_subsys(&ipv6_inetpeer_ops); 3069c3426b47SDavid S. Miller if (ret) 3070e8803b6cSDavid S. Miller goto out_dst_entries; 30712a0c451aSThomas Graf 30727e52b33bSDavid S. Miller ret = register_pernet_subsys(&ip6_route_net_ops); 30737e52b33bSDavid S. Miller if (ret) 30747e52b33bSDavid S. Miller goto out_register_inetpeer; 3075c3426b47SDavid S. Miller 30765dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 30775dc121e9SArnaud Ebalard 30788ed67789SDaniel Lezcano /* Registering of the loopback is done before this portion of code, 30798ed67789SDaniel Lezcano * the loopback reference in rt6_info will not be taken, do it 30808ed67789SDaniel Lezcano * manually for init_net */ 3081d8d1f30bSChangli Gao init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 30828ed67789SDaniel Lezcano init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 3083bdb3289fSDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 3084d8d1f30bSChangli Gao init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 30858ed67789SDaniel Lezcano init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 3086d8d1f30bSChangli Gao init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 30878ed67789SDaniel Lezcano init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 3088bdb3289fSDaniel Lezcano #endif 3089e8803b6cSDavid S. Miller ret = fib6_init(); 3090433d49c3SDaniel Lezcano if (ret) 30918ed67789SDaniel Lezcano goto out_register_subsys; 3092433d49c3SDaniel Lezcano 3093433d49c3SDaniel Lezcano ret = xfrm6_init(); 3094433d49c3SDaniel Lezcano if (ret) 3095e8803b6cSDavid S. Miller goto out_fib6_init; 3096c35b7e72SDaniel Lezcano 3097433d49c3SDaniel Lezcano ret = fib6_rules_init(); 3098433d49c3SDaniel Lezcano if (ret) 3099433d49c3SDaniel Lezcano goto xfrm6_init; 31007e5449c2SDaniel Lezcano 3101d189634eSThomas Graf ret = register_pernet_subsys(&ip6_route_net_late_ops); 3102d189634eSThomas Graf if (ret) 3103d189634eSThomas Graf goto fib6_rules_init; 3104d189634eSThomas Graf 3105433d49c3SDaniel Lezcano ret = -ENOBUFS; 3106c7ac8679SGreg Rose if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) || 3107c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) || 3108c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL)) 3109d189634eSThomas Graf goto out_register_late_subsys; 3110433d49c3SDaniel Lezcano 31118ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 3112cdb18761SDaniel Lezcano if (ret) 3113d189634eSThomas Graf goto out_register_late_subsys; 31148ed67789SDaniel Lezcano 3115433d49c3SDaniel Lezcano out: 3116433d49c3SDaniel Lezcano return ret; 3117433d49c3SDaniel Lezcano 3118d189634eSThomas Graf out_register_late_subsys: 3119d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 3120433d49c3SDaniel Lezcano fib6_rules_init: 3121433d49c3SDaniel Lezcano fib6_rules_cleanup(); 3122433d49c3SDaniel Lezcano xfrm6_init: 3123433d49c3SDaniel Lezcano xfrm6_fini(); 31242a0c451aSThomas Graf out_fib6_init: 31252a0c451aSThomas Graf fib6_gc_cleanup(); 31268ed67789SDaniel Lezcano out_register_subsys: 31278ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 31287e52b33bSDavid S. Miller out_register_inetpeer: 31297e52b33bSDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 3130fc66f95cSEric Dumazet out_dst_entries: 3131fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 3132433d49c3SDaniel Lezcano out_kmem_cache: 3133f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 3134433d49c3SDaniel Lezcano goto out; 31351da177e4SLinus Torvalds } 31361da177e4SLinus Torvalds 31371da177e4SLinus Torvalds void ip6_route_cleanup(void) 31381da177e4SLinus Torvalds { 31398ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 3140d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 3141101367c2SThomas Graf fib6_rules_cleanup(); 31421da177e4SLinus Torvalds xfrm6_fini(); 31431da177e4SLinus Torvalds fib6_gc_cleanup(); 3144c3426b47SDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 31458ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 314641bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 3147f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 31481da177e4SLinus Torvalds } 3149