11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Linux INET6 implementation 31da177e4SLinus Torvalds * FIB front-end. 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Authors: 61da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 91da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 101da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 111da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 121da177e4SLinus Torvalds */ 131da177e4SLinus Torvalds 141da177e4SLinus Torvalds /* Changes: 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI 171da177e4SLinus Torvalds * reworked default router selection. 181da177e4SLinus Torvalds * - respect outgoing interface 191da177e4SLinus Torvalds * - select from (probably) reachable routers (i.e. 201da177e4SLinus Torvalds * routers in REACHABLE, STALE, DELAY or PROBE states). 211da177e4SLinus Torvalds * - always select the same router if it is (probably) 221da177e4SLinus Torvalds * reachable. otherwise, round-robin the list. 23c0bece9fSYOSHIFUJI Hideaki * Ville Nuorvala 24c0bece9fSYOSHIFUJI Hideaki * Fixed routing subtrees. 251da177e4SLinus Torvalds */ 261da177e4SLinus Torvalds 27f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt 28f3213831SJoe Perches 294fc268d2SRandy Dunlap #include <linux/capability.h> 301da177e4SLinus Torvalds #include <linux/errno.h> 31bc3b2d7fSPaul Gortmaker #include <linux/export.h> 321da177e4SLinus Torvalds #include <linux/types.h> 331da177e4SLinus Torvalds #include <linux/times.h> 341da177e4SLinus Torvalds #include <linux/socket.h> 351da177e4SLinus Torvalds #include <linux/sockios.h> 361da177e4SLinus Torvalds #include <linux/net.h> 371da177e4SLinus Torvalds #include <linux/route.h> 381da177e4SLinus Torvalds #include <linux/netdevice.h> 391da177e4SLinus Torvalds #include <linux/in6.h> 407bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h> 411da177e4SLinus Torvalds #include <linux/init.h> 421da177e4SLinus Torvalds #include <linux/if_arp.h> 431da177e4SLinus Torvalds #include <linux/proc_fs.h> 441da177e4SLinus Torvalds #include <linux/seq_file.h> 455b7c931dSDaniel Lezcano #include <linux/nsproxy.h> 465a0e3ad6STejun Heo #include <linux/slab.h> 47457c4cbcSEric W. Biederman #include <net/net_namespace.h> 481da177e4SLinus Torvalds #include <net/snmp.h> 491da177e4SLinus Torvalds #include <net/ipv6.h> 501da177e4SLinus Torvalds #include <net/ip6_fib.h> 511da177e4SLinus Torvalds #include <net/ip6_route.h> 521da177e4SLinus Torvalds #include <net/ndisc.h> 531da177e4SLinus Torvalds #include <net/addrconf.h> 541da177e4SLinus Torvalds #include <net/tcp.h> 551da177e4SLinus Torvalds #include <linux/rtnetlink.h> 561da177e4SLinus Torvalds #include <net/dst.h> 571da177e4SLinus Torvalds #include <net/xfrm.h> 588d71740cSTom Tucker #include <net/netevent.h> 5921713ebcSThomas Graf #include <net/netlink.h> 6051ebd318SNicolas Dichtel #include <net/nexthop.h> 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds #include <asm/uaccess.h> 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 651da177e4SLinus Torvalds #include <linux/sysctl.h> 661da177e4SLinus Torvalds #endif 671da177e4SLinus Torvalds 68afc154e9SHannes Frederic Sowa enum rt6_nud_state { 697e980569SJiri Benc RT6_NUD_FAIL_HARD = -3, 707e980569SJiri Benc RT6_NUD_FAIL_PROBE = -2, 717e980569SJiri Benc RT6_NUD_FAIL_DO_RR = -1, 72afc154e9SHannes Frederic Sowa RT6_NUD_SUCCEED = 1 73afc154e9SHannes Frederic Sowa }; 74afc154e9SHannes Frederic Sowa 751716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort, 7621efcfa0SEric Dumazet const struct in6_addr *dest); 771da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie); 780dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst); 79ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst); 801da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *); 811da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *); 821da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *, 831da177e4SLinus Torvalds struct net_device *dev, int how); 84569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops); 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds static int ip6_pkt_discard(struct sk_buff *skb); 87aad88724SEric Dumazet static int ip6_pkt_discard_out(struct sock *sk, struct sk_buff *skb); 887150aedeSKamala R static int ip6_pkt_prohibit(struct sk_buff *skb); 89aad88724SEric Dumazet static int ip6_pkt_prohibit_out(struct sock *sk, struct sk_buff *skb); 901da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb); 916700c270SDavid S. Miller static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 926700c270SDavid S. Miller struct sk_buff *skb, u32 mtu); 936700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, 946700c270SDavid S. Miller struct sk_buff *skb); 9552bd4c0cSNicolas Dichtel static int rt6_score_route(struct rt6_info *rt, int oif, int strict); 961da177e4SLinus Torvalds 9770ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 98efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 99b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 100b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex, 10195c96174SEric Dumazet unsigned int pref); 102efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 103b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 104b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex); 10570ceb4f5SYOSHIFUJI Hideaki #endif 10670ceb4f5SYOSHIFUJI Hideaki 107e8243534Sstephen hemminger static void rt6_bind_peer(struct rt6_info *rt, int create) 108e8243534Sstephen hemminger { 109e8243534Sstephen hemminger struct inet_peer_base *base; 110e8243534Sstephen hemminger struct inet_peer *peer; 111e8243534Sstephen hemminger 112e8243534Sstephen hemminger base = inetpeer_base_ptr(rt->_rt6i_peer); 113e8243534Sstephen hemminger if (!base) 114e8243534Sstephen hemminger return; 115e8243534Sstephen hemminger 116e8243534Sstephen hemminger peer = inet_getpeer_v6(base, &rt->rt6i_dst.addr, create); 117e8243534Sstephen hemminger if (peer) { 118e8243534Sstephen hemminger if (!rt6_set_peer(rt, peer)) 119e8243534Sstephen hemminger inet_putpeer(peer); 120e8243534Sstephen hemminger } 121e8243534Sstephen hemminger } 122e8243534Sstephen hemminger 123e8243534Sstephen hemminger static struct inet_peer *__rt6_get_peer(struct rt6_info *rt, int create) 124e8243534Sstephen hemminger { 125e8243534Sstephen hemminger if (rt6_has_peer(rt)) 126e8243534Sstephen hemminger return rt6_peer_ptr(rt); 127e8243534Sstephen hemminger 128e8243534Sstephen hemminger rt6_bind_peer(rt, create); 129e8243534Sstephen hemminger return (rt6_has_peer(rt) ? rt6_peer_ptr(rt) : NULL); 130e8243534Sstephen hemminger } 131e8243534Sstephen hemminger 132e8243534Sstephen hemminger static struct inet_peer *rt6_get_peer_create(struct rt6_info *rt) 133e8243534Sstephen hemminger { 134e8243534Sstephen hemminger return __rt6_get_peer(rt, 1); 135e8243534Sstephen hemminger } 136e8243534Sstephen hemminger 13706582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old) 13806582540SDavid S. Miller { 13906582540SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 14006582540SDavid S. Miller struct inet_peer *peer; 14106582540SDavid S. Miller u32 *p = NULL; 14206582540SDavid S. Miller 1438e2ec639SYan, Zheng if (!(rt->dst.flags & DST_HOST)) 1448e2ec639SYan, Zheng return NULL; 1458e2ec639SYan, Zheng 146fbfe95a4SDavid S. Miller peer = rt6_get_peer_create(rt); 14706582540SDavid S. Miller if (peer) { 14806582540SDavid S. Miller u32 *old_p = __DST_METRICS_PTR(old); 14906582540SDavid S. Miller unsigned long prev, new; 15006582540SDavid S. Miller 15106582540SDavid S. Miller p = peer->metrics; 152e5fd387aSMichal Kubeček if (inet_metrics_new(peer) || 153e5fd387aSMichal Kubeček (old & DST_METRICS_FORCE_OVERWRITE)) 15406582540SDavid S. Miller memcpy(p, old_p, sizeof(u32) * RTAX_MAX); 15506582540SDavid S. Miller 15606582540SDavid S. Miller new = (unsigned long) p; 15706582540SDavid S. Miller prev = cmpxchg(&dst->_metrics, old, new); 15806582540SDavid S. Miller 15906582540SDavid S. Miller if (prev != old) { 16006582540SDavid S. Miller p = __DST_METRICS_PTR(prev); 16106582540SDavid S. Miller if (prev & DST_METRICS_READ_ONLY) 16206582540SDavid S. Miller p = NULL; 16306582540SDavid S. Miller } 16406582540SDavid S. Miller } 16506582540SDavid S. Miller return p; 16606582540SDavid S. Miller } 16706582540SDavid S. Miller 168f894cbf8SDavid S. Miller static inline const void *choose_neigh_daddr(struct rt6_info *rt, 169f894cbf8SDavid S. Miller struct sk_buff *skb, 170f894cbf8SDavid S. Miller const void *daddr) 17139232973SDavid S. Miller { 17239232973SDavid S. Miller struct in6_addr *p = &rt->rt6i_gateway; 17339232973SDavid S. Miller 174a7563f34SDavid S. Miller if (!ipv6_addr_any(p)) 17539232973SDavid S. Miller return (const void *) p; 176f894cbf8SDavid S. Miller else if (skb) 177f894cbf8SDavid S. Miller return &ipv6_hdr(skb)->daddr; 17839232973SDavid S. Miller return daddr; 17939232973SDavid S. Miller } 18039232973SDavid S. Miller 181f894cbf8SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst, 182f894cbf8SDavid S. Miller struct sk_buff *skb, 183f894cbf8SDavid S. Miller const void *daddr) 184d3aaeb38SDavid S. Miller { 18539232973SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 18639232973SDavid S. Miller struct neighbour *n; 18739232973SDavid S. Miller 188f894cbf8SDavid S. Miller daddr = choose_neigh_daddr(rt, skb, daddr); 1898e022ee6SYOSHIFUJI Hideaki / 吉藤英明 n = __ipv6_neigh_lookup(dst->dev, daddr); 190f83c7790SDavid S. Miller if (n) 191f83c7790SDavid S. Miller return n; 192f83c7790SDavid S. Miller return neigh_create(&nd_tbl, daddr, dst->dev); 193f83c7790SDavid S. Miller } 194f83c7790SDavid S. Miller 1959a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = { 1961da177e4SLinus Torvalds .family = AF_INET6, 19709640e63SHarvey Harrison .protocol = cpu_to_be16(ETH_P_IPV6), 1981da177e4SLinus Torvalds .gc = ip6_dst_gc, 1991da177e4SLinus Torvalds .gc_thresh = 1024, 2001da177e4SLinus Torvalds .check = ip6_dst_check, 2010dbaee3bSDavid S. Miller .default_advmss = ip6_default_advmss, 202ebb762f2SSteffen Klassert .mtu = ip6_mtu, 20306582540SDavid S. Miller .cow_metrics = ipv6_cow_metrics, 2041da177e4SLinus Torvalds .destroy = ip6_dst_destroy, 2051da177e4SLinus Torvalds .ifdown = ip6_dst_ifdown, 2061da177e4SLinus Torvalds .negative_advice = ip6_negative_advice, 2071da177e4SLinus Torvalds .link_failure = ip6_link_failure, 2081da177e4SLinus Torvalds .update_pmtu = ip6_rt_update_pmtu, 2096e157b6aSDavid S. Miller .redirect = rt6_do_redirect, 2101ac06e03SHerbert Xu .local_out = __ip6_local_out, 211d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 2121da177e4SLinus Torvalds }; 2131da177e4SLinus Torvalds 214ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst) 215ec831ea7SRoland Dreier { 216618f9bc7SSteffen Klassert unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); 217618f9bc7SSteffen Klassert 218618f9bc7SSteffen Klassert return mtu ? : dst->dev->mtu; 219ec831ea7SRoland Dreier } 220ec831ea7SRoland Dreier 2216700c270SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk, 2226700c270SDavid S. Miller struct sk_buff *skb, u32 mtu) 22314e50e57SDavid S. Miller { 22414e50e57SDavid S. Miller } 22514e50e57SDavid S. Miller 2266700c270SDavid S. Miller static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk, 2276700c270SDavid S. Miller struct sk_buff *skb) 228b587ee3bSDavid S. Miller { 229b587ee3bSDavid S. Miller } 230b587ee3bSDavid S. Miller 2310972ddb2SHeld Bernhard static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst, 2320972ddb2SHeld Bernhard unsigned long old) 2330972ddb2SHeld Bernhard { 2340972ddb2SHeld Bernhard return NULL; 2350972ddb2SHeld Bernhard } 2360972ddb2SHeld Bernhard 23714e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = { 23814e50e57SDavid S. Miller .family = AF_INET6, 23909640e63SHarvey Harrison .protocol = cpu_to_be16(ETH_P_IPV6), 24014e50e57SDavid S. Miller .destroy = ip6_dst_destroy, 24114e50e57SDavid S. Miller .check = ip6_dst_check, 242ebb762f2SSteffen Klassert .mtu = ip6_blackhole_mtu, 243214f45c9SEric Dumazet .default_advmss = ip6_default_advmss, 24414e50e57SDavid S. Miller .update_pmtu = ip6_rt_blackhole_update_pmtu, 245b587ee3bSDavid S. Miller .redirect = ip6_rt_blackhole_redirect, 2460972ddb2SHeld Bernhard .cow_metrics = ip6_rt_blackhole_cow_metrics, 247d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 24814e50e57SDavid S. Miller }; 24914e50e57SDavid S. Miller 25062fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = { 25114edd87dSLi RongQing [RTAX_HOPLIMIT - 1] = 0, 25262fa8a84SDavid S. Miller }; 25362fa8a84SDavid S. Miller 254fb0af4c7SEric Dumazet static const struct rt6_info ip6_null_entry_template = { 2551da177e4SLinus Torvalds .dst = { 2561da177e4SLinus Torvalds .__refcnt = ATOMIC_INIT(1), 2571da177e4SLinus Torvalds .__use = 1, 2582c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 2591da177e4SLinus Torvalds .error = -ENETUNREACH, 2601da177e4SLinus Torvalds .input = ip6_pkt_discard, 2611da177e4SLinus Torvalds .output = ip6_pkt_discard_out, 2621da177e4SLinus Torvalds }, 2631da177e4SLinus Torvalds .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 2644f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 2651da177e4SLinus Torvalds .rt6i_metric = ~(u32) 0, 2661da177e4SLinus Torvalds .rt6i_ref = ATOMIC_INIT(1), 2671da177e4SLinus Torvalds }; 2681da177e4SLinus Torvalds 269101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES 270101367c2SThomas Graf 271fb0af4c7SEric Dumazet static const struct rt6_info ip6_prohibit_entry_template = { 272101367c2SThomas Graf .dst = { 273101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 274101367c2SThomas Graf .__use = 1, 2752c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 276101367c2SThomas Graf .error = -EACCES, 2779ce8ade0SThomas Graf .input = ip6_pkt_prohibit, 2789ce8ade0SThomas Graf .output = ip6_pkt_prohibit_out, 279101367c2SThomas Graf }, 280101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 2814f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 282101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 283101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 284101367c2SThomas Graf }; 285101367c2SThomas Graf 286fb0af4c7SEric Dumazet static const struct rt6_info ip6_blk_hole_entry_template = { 287101367c2SThomas Graf .dst = { 288101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 289101367c2SThomas Graf .__use = 1, 2902c20cbd7SNicolas Dichtel .obsolete = DST_OBSOLETE_FORCE_CHK, 291101367c2SThomas Graf .error = -EINVAL, 292352e512cSHerbert Xu .input = dst_discard, 293aad88724SEric Dumazet .output = dst_discard_sk, 294101367c2SThomas Graf }, 295101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 2964f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 297101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 298101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 299101367c2SThomas Graf }; 300101367c2SThomas Graf 301101367c2SThomas Graf #endif 302101367c2SThomas Graf 3031da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */ 30497bab73fSDavid S. Miller static inline struct rt6_info *ip6_dst_alloc(struct net *net, 305957c665fSDavid S. Miller struct net_device *dev, 3068b96d22dSDavid S. Miller int flags, 3078b96d22dSDavid S. Miller struct fib6_table *table) 3081da177e4SLinus Torvalds { 30997bab73fSDavid S. Miller struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev, 3106f3118b5SNicolas Dichtel 0, DST_OBSOLETE_FORCE_CHK, flags); 311cf911662SDavid S. Miller 31297bab73fSDavid S. Miller if (rt) { 3138104891bSSteffen Klassert struct dst_entry *dst = &rt->dst; 3148104891bSSteffen Klassert 3158104891bSSteffen Klassert memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst)); 3168b96d22dSDavid S. Miller rt6_init_peer(rt, table ? &table->tb6_peers : net->ipv6.peers); 31751ebd318SNicolas Dichtel INIT_LIST_HEAD(&rt->rt6i_siblings); 31897bab73fSDavid S. Miller } 319cf911662SDavid S. Miller return rt; 3201da177e4SLinus Torvalds } 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst) 3231da177e4SLinus Torvalds { 3241da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 3251da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 326ecd98837SYOSHIFUJI Hideaki / 吉藤英明 struct dst_entry *from = dst->from; 3271da177e4SLinus Torvalds 3288e2ec639SYan, Zheng if (!(rt->dst.flags & DST_HOST)) 3298e2ec639SYan, Zheng dst_destroy_metrics_generic(dst); 3308e2ec639SYan, Zheng 33138308473SDavid S. Miller if (idev) { 3321da177e4SLinus Torvalds rt->rt6i_idev = NULL; 3331da177e4SLinus Torvalds in6_dev_put(idev); 3341da177e4SLinus Torvalds } 3351716a961SGao feng 336ecd98837SYOSHIFUJI Hideaki / 吉藤英明 dst->from = NULL; 337ecd98837SYOSHIFUJI Hideaki / 吉藤英明 dst_release(from); 3381716a961SGao feng 33997bab73fSDavid S. Miller if (rt6_has_peer(rt)) { 34097bab73fSDavid S. Miller struct inet_peer *peer = rt6_peer_ptr(rt); 341b3419363SDavid S. Miller inet_putpeer(peer); 342b3419363SDavid S. Miller } 343b3419363SDavid S. Miller } 344b3419363SDavid S. Miller 3451da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 3461da177e4SLinus Torvalds int how) 3471da177e4SLinus Torvalds { 3481da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 3491da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 3505a3e55d6SDenis V. Lunev struct net_device *loopback_dev = 351c346dca1SYOSHIFUJI Hideaki dev_net(dev)->loopback_dev; 3521da177e4SLinus Torvalds 35397cac082SDavid S. Miller if (dev != loopback_dev) { 35497cac082SDavid S. Miller if (idev && idev->dev == dev) { 3555a3e55d6SDenis V. Lunev struct inet6_dev *loopback_idev = 3565a3e55d6SDenis V. Lunev in6_dev_get(loopback_dev); 35738308473SDavid S. Miller if (loopback_idev) { 3581da177e4SLinus Torvalds rt->rt6i_idev = loopback_idev; 3591da177e4SLinus Torvalds in6_dev_put(idev); 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds } 36297cac082SDavid S. Miller } 3631da177e4SLinus Torvalds } 3641da177e4SLinus Torvalds 365a50feda5SEric Dumazet static bool rt6_check_expired(const struct rt6_info *rt) 3661da177e4SLinus Torvalds { 3671716a961SGao feng if (rt->rt6i_flags & RTF_EXPIRES) { 3681716a961SGao feng if (time_after(jiffies, rt->dst.expires)) 369a50feda5SEric Dumazet return true; 3701716a961SGao feng } else if (rt->dst.from) { 3713fd91fb3SLi RongQing return rt6_check_expired((struct rt6_info *) rt->dst.from); 3721716a961SGao feng } 373a50feda5SEric Dumazet return false; 3741da177e4SLinus Torvalds } 3751da177e4SLinus Torvalds 37651ebd318SNicolas Dichtel /* Multipath route selection: 37751ebd318SNicolas Dichtel * Hash based function using packet header and flowlabel. 37851ebd318SNicolas Dichtel * Adapted from fib_info_hashfn() 37951ebd318SNicolas Dichtel */ 38051ebd318SNicolas Dichtel static int rt6_info_hash_nhsfn(unsigned int candidate_count, 38151ebd318SNicolas Dichtel const struct flowi6 *fl6) 38251ebd318SNicolas Dichtel { 38351ebd318SNicolas Dichtel unsigned int val = fl6->flowi6_proto; 38451ebd318SNicolas Dichtel 385c08977bbSYOSHIFUJI Hideaki / 吉藤英明 val ^= ipv6_addr_hash(&fl6->daddr); 386c08977bbSYOSHIFUJI Hideaki / 吉藤英明 val ^= ipv6_addr_hash(&fl6->saddr); 38751ebd318SNicolas Dichtel 38851ebd318SNicolas Dichtel /* Work only if this not encapsulated */ 38951ebd318SNicolas Dichtel switch (fl6->flowi6_proto) { 39051ebd318SNicolas Dichtel case IPPROTO_UDP: 39151ebd318SNicolas Dichtel case IPPROTO_TCP: 39251ebd318SNicolas Dichtel case IPPROTO_SCTP: 393b3ce5ae1SNicolas Dichtel val ^= (__force u16)fl6->fl6_sport; 394b3ce5ae1SNicolas Dichtel val ^= (__force u16)fl6->fl6_dport; 39551ebd318SNicolas Dichtel break; 39651ebd318SNicolas Dichtel 39751ebd318SNicolas Dichtel case IPPROTO_ICMPV6: 398b3ce5ae1SNicolas Dichtel val ^= (__force u16)fl6->fl6_icmp_type; 399b3ce5ae1SNicolas Dichtel val ^= (__force u16)fl6->fl6_icmp_code; 40051ebd318SNicolas Dichtel break; 40151ebd318SNicolas Dichtel } 40251ebd318SNicolas Dichtel /* RFC6438 recommands to use flowlabel */ 403b3ce5ae1SNicolas Dichtel val ^= (__force u32)fl6->flowlabel; 40451ebd318SNicolas Dichtel 40551ebd318SNicolas Dichtel /* Perhaps, we need to tune, this function? */ 40651ebd318SNicolas Dichtel val = val ^ (val >> 7) ^ (val >> 12); 40751ebd318SNicolas Dichtel return val % candidate_count; 40851ebd318SNicolas Dichtel } 40951ebd318SNicolas Dichtel 41051ebd318SNicolas Dichtel static struct rt6_info *rt6_multipath_select(struct rt6_info *match, 41152bd4c0cSNicolas Dichtel struct flowi6 *fl6, int oif, 41252bd4c0cSNicolas Dichtel int strict) 41351ebd318SNicolas Dichtel { 41451ebd318SNicolas Dichtel struct rt6_info *sibling, *next_sibling; 41551ebd318SNicolas Dichtel int route_choosen; 41651ebd318SNicolas Dichtel 41751ebd318SNicolas Dichtel route_choosen = rt6_info_hash_nhsfn(match->rt6i_nsiblings + 1, fl6); 41851ebd318SNicolas Dichtel /* Don't change the route, if route_choosen == 0 41951ebd318SNicolas Dichtel * (siblings does not include ourself) 42051ebd318SNicolas Dichtel */ 42151ebd318SNicolas Dichtel if (route_choosen) 42251ebd318SNicolas Dichtel list_for_each_entry_safe(sibling, next_sibling, 42351ebd318SNicolas Dichtel &match->rt6i_siblings, rt6i_siblings) { 42451ebd318SNicolas Dichtel route_choosen--; 42551ebd318SNicolas Dichtel if (route_choosen == 0) { 42652bd4c0cSNicolas Dichtel if (rt6_score_route(sibling, oif, strict) < 0) 42752bd4c0cSNicolas Dichtel break; 42851ebd318SNicolas Dichtel match = sibling; 42951ebd318SNicolas Dichtel break; 43051ebd318SNicolas Dichtel } 43151ebd318SNicolas Dichtel } 43251ebd318SNicolas Dichtel return match; 43351ebd318SNicolas Dichtel } 43451ebd318SNicolas Dichtel 4351da177e4SLinus Torvalds /* 436c71099acSThomas Graf * Route lookup. Any table->tb6_lock is implied. 4371da177e4SLinus Torvalds */ 4381da177e4SLinus Torvalds 4398ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net, 4408ed67789SDaniel Lezcano struct rt6_info *rt, 441b71d1d42SEric Dumazet const struct in6_addr *saddr, 4421da177e4SLinus Torvalds int oif, 443d420895eSYOSHIFUJI Hideaki int flags) 4441da177e4SLinus Torvalds { 4451da177e4SLinus Torvalds struct rt6_info *local = NULL; 4461da177e4SLinus Torvalds struct rt6_info *sprt; 4471da177e4SLinus Torvalds 448dd3abc4eSYOSHIFUJI Hideaki if (!oif && ipv6_addr_any(saddr)) 449dd3abc4eSYOSHIFUJI Hideaki goto out; 450dd3abc4eSYOSHIFUJI Hideaki 451d8d1f30bSChangli Gao for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) { 452d1918542SDavid S. Miller struct net_device *dev = sprt->dst.dev; 453dd3abc4eSYOSHIFUJI Hideaki 454dd3abc4eSYOSHIFUJI Hideaki if (oif) { 4551da177e4SLinus Torvalds if (dev->ifindex == oif) 4561da177e4SLinus Torvalds return sprt; 4571da177e4SLinus Torvalds if (dev->flags & IFF_LOOPBACK) { 45838308473SDavid S. Miller if (!sprt->rt6i_idev || 4591da177e4SLinus Torvalds sprt->rt6i_idev->dev->ifindex != oif) { 460d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE && oif) 4611da177e4SLinus Torvalds continue; 4621da177e4SLinus Torvalds if (local && (!oif || 4631da177e4SLinus Torvalds local->rt6i_idev->dev->ifindex == oif)) 4641da177e4SLinus Torvalds continue; 4651da177e4SLinus Torvalds } 4661da177e4SLinus Torvalds local = sprt; 4671da177e4SLinus Torvalds } 468dd3abc4eSYOSHIFUJI Hideaki } else { 469dd3abc4eSYOSHIFUJI Hideaki if (ipv6_chk_addr(net, saddr, dev, 470dd3abc4eSYOSHIFUJI Hideaki flags & RT6_LOOKUP_F_IFACE)) 471dd3abc4eSYOSHIFUJI Hideaki return sprt; 472dd3abc4eSYOSHIFUJI Hideaki } 4731da177e4SLinus Torvalds } 4741da177e4SLinus Torvalds 475dd3abc4eSYOSHIFUJI Hideaki if (oif) { 4761da177e4SLinus Torvalds if (local) 4771da177e4SLinus Torvalds return local; 4781da177e4SLinus Torvalds 479d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE) 4808ed67789SDaniel Lezcano return net->ipv6.ip6_null_entry; 4811da177e4SLinus Torvalds } 482dd3abc4eSYOSHIFUJI Hideaki out: 4831da177e4SLinus Torvalds return rt; 4841da177e4SLinus Torvalds } 4851da177e4SLinus Torvalds 48627097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 487c2f17e82SHannes Frederic Sowa struct __rt6_probe_work { 488c2f17e82SHannes Frederic Sowa struct work_struct work; 489c2f17e82SHannes Frederic Sowa struct in6_addr target; 490c2f17e82SHannes Frederic Sowa struct net_device *dev; 491c2f17e82SHannes Frederic Sowa }; 492c2f17e82SHannes Frederic Sowa 493c2f17e82SHannes Frederic Sowa static void rt6_probe_deferred(struct work_struct *w) 494c2f17e82SHannes Frederic Sowa { 495c2f17e82SHannes Frederic Sowa struct in6_addr mcaddr; 496c2f17e82SHannes Frederic Sowa struct __rt6_probe_work *work = 497c2f17e82SHannes Frederic Sowa container_of(w, struct __rt6_probe_work, work); 498c2f17e82SHannes Frederic Sowa 499c2f17e82SHannes Frederic Sowa addrconf_addr_solict_mult(&work->target, &mcaddr); 500c2f17e82SHannes Frederic Sowa ndisc_send_ns(work->dev, NULL, &work->target, &mcaddr, NULL); 501c2f17e82SHannes Frederic Sowa dev_put(work->dev); 502c2f17e82SHannes Frederic Sowa kfree(w); 503c2f17e82SHannes Frederic Sowa } 504c2f17e82SHannes Frederic Sowa 50527097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt) 50627097255SYOSHIFUJI Hideaki { 507f2c31e32SEric Dumazet struct neighbour *neigh; 50827097255SYOSHIFUJI Hideaki /* 50927097255SYOSHIFUJI Hideaki * Okay, this does not seem to be appropriate 51027097255SYOSHIFUJI Hideaki * for now, however, we need to check if it 51127097255SYOSHIFUJI Hideaki * is really so; aka Router Reachability Probing. 51227097255SYOSHIFUJI Hideaki * 51327097255SYOSHIFUJI Hideaki * Router Reachability Probe MUST be rate-limited 51427097255SYOSHIFUJI Hideaki * to no more than one per minute. 51527097255SYOSHIFUJI Hideaki */ 5162152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (!rt || !(rt->rt6i_flags & RTF_GATEWAY)) 517fdd6681dSAmerigo Wang return; 5182152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 5192152caeaSYOSHIFUJI Hideaki / 吉藤英明 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 5202152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 5212152caeaSYOSHIFUJI Hideaki / 吉藤英明 write_lock(&neigh->lock); 5222152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (neigh->nud_state & NUD_VALID) 5232152caeaSYOSHIFUJI Hideaki / 吉藤英明 goto out; 5247ff74a59SYOSHIFUJI Hideaki / 吉藤英明 } 5252152caeaSYOSHIFUJI Hideaki / 吉藤英明 5262152caeaSYOSHIFUJI Hideaki / 吉藤英明 if (!neigh || 52752e16356SYOSHIFUJI Hideaki time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) { 528c2f17e82SHannes Frederic Sowa struct __rt6_probe_work *work; 52927097255SYOSHIFUJI Hideaki 530c2f17e82SHannes Frederic Sowa work = kmalloc(sizeof(*work), GFP_ATOMIC); 531c2f17e82SHannes Frederic Sowa 532c2f17e82SHannes Frederic Sowa if (neigh && work) 5337e980569SJiri Benc __neigh_set_probe_once(neigh); 5342152caeaSYOSHIFUJI Hideaki / 吉藤英明 535c2f17e82SHannes Frederic Sowa if (neigh) 536c2f17e82SHannes Frederic Sowa write_unlock(&neigh->lock); 537c2f17e82SHannes Frederic Sowa 538c2f17e82SHannes Frederic Sowa if (work) { 539c2f17e82SHannes Frederic Sowa INIT_WORK(&work->work, rt6_probe_deferred); 540c2f17e82SHannes Frederic Sowa work->target = rt->rt6i_gateway; 541c2f17e82SHannes Frederic Sowa dev_hold(rt->dst.dev); 542c2f17e82SHannes Frederic Sowa work->dev = rt->dst.dev; 543c2f17e82SHannes Frederic Sowa schedule_work(&work->work); 544c2f17e82SHannes Frederic Sowa } 545f2c31e32SEric Dumazet } else { 5462152caeaSYOSHIFUJI Hideaki / 吉藤英明 out: 5472152caeaSYOSHIFUJI Hideaki / 吉藤英明 write_unlock(&neigh->lock); 54827097255SYOSHIFUJI Hideaki } 5492152caeaSYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 550f2c31e32SEric Dumazet } 55127097255SYOSHIFUJI Hideaki #else 55227097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt) 55327097255SYOSHIFUJI Hideaki { 55427097255SYOSHIFUJI Hideaki } 55527097255SYOSHIFUJI Hideaki #endif 55627097255SYOSHIFUJI Hideaki 5571da177e4SLinus Torvalds /* 558554cfb7eSYOSHIFUJI Hideaki * Default Router Selection (RFC 2461 6.3.6) 5591da177e4SLinus Torvalds */ 560b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif) 5611da177e4SLinus Torvalds { 562d1918542SDavid S. Miller struct net_device *dev = rt->dst.dev; 563161980f4SDavid S. Miller if (!oif || dev->ifindex == oif) 564554cfb7eSYOSHIFUJI Hideaki return 2; 565161980f4SDavid S. Miller if ((dev->flags & IFF_LOOPBACK) && 566161980f4SDavid S. Miller rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif) 567161980f4SDavid S. Miller return 1; 568554cfb7eSYOSHIFUJI Hideaki return 0; 5691da177e4SLinus Torvalds } 5701da177e4SLinus Torvalds 571afc154e9SHannes Frederic Sowa static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt) 5721da177e4SLinus Torvalds { 573f2c31e32SEric Dumazet struct neighbour *neigh; 574afc154e9SHannes Frederic Sowa enum rt6_nud_state ret = RT6_NUD_FAIL_HARD; 575f2c31e32SEric Dumazet 5764d0c5911SYOSHIFUJI Hideaki if (rt->rt6i_flags & RTF_NONEXTHOP || 5774d0c5911SYOSHIFUJI Hideaki !(rt->rt6i_flags & RTF_GATEWAY)) 578afc154e9SHannes Frederic Sowa return RT6_NUD_SUCCEED; 579145a3621SYOSHIFUJI Hideaki / 吉藤英明 580145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_lock_bh(); 581145a3621SYOSHIFUJI Hideaki / 吉藤英明 neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); 582145a3621SYOSHIFUJI Hideaki / 吉藤英明 if (neigh) { 583145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_lock(&neigh->lock); 584554cfb7eSYOSHIFUJI Hideaki if (neigh->nud_state & NUD_VALID) 585afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 586398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 587a5a81f0bSPaul Marks else if (!(neigh->nud_state & NUD_FAILED)) 588afc154e9SHannes Frederic Sowa ret = RT6_NUD_SUCCEED; 5897e980569SJiri Benc else 5907e980569SJiri Benc ret = RT6_NUD_FAIL_PROBE; 591398bcbebSYOSHIFUJI Hideaki #endif 592145a3621SYOSHIFUJI Hideaki / 吉藤英明 read_unlock(&neigh->lock); 593afc154e9SHannes Frederic Sowa } else { 594afc154e9SHannes Frederic Sowa ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ? 5957e980569SJiri Benc RT6_NUD_SUCCEED : RT6_NUD_FAIL_DO_RR; 596a5a81f0bSPaul Marks } 597145a3621SYOSHIFUJI Hideaki / 吉藤英明 rcu_read_unlock_bh(); 598145a3621SYOSHIFUJI Hideaki / 吉藤英明 599a5a81f0bSPaul Marks return ret; 6001da177e4SLinus Torvalds } 6011da177e4SLinus Torvalds 602554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif, 603554cfb7eSYOSHIFUJI Hideaki int strict) 604554cfb7eSYOSHIFUJI Hideaki { 605a5a81f0bSPaul Marks int m; 6064d0c5911SYOSHIFUJI Hideaki 6074d0c5911SYOSHIFUJI Hideaki m = rt6_check_dev(rt, oif); 60877d16f45SYOSHIFUJI Hideaki if (!m && (strict & RT6_LOOKUP_F_IFACE)) 609afc154e9SHannes Frederic Sowa return RT6_NUD_FAIL_HARD; 610ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 611ebacaaa0SYOSHIFUJI Hideaki m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2; 612ebacaaa0SYOSHIFUJI Hideaki #endif 613afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) { 614afc154e9SHannes Frederic Sowa int n = rt6_check_neigh(rt); 615afc154e9SHannes Frederic Sowa if (n < 0) 616afc154e9SHannes Frederic Sowa return n; 617afc154e9SHannes Frederic Sowa } 618554cfb7eSYOSHIFUJI Hideaki return m; 619554cfb7eSYOSHIFUJI Hideaki } 620554cfb7eSYOSHIFUJI Hideaki 621f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict, 622afc154e9SHannes Frederic Sowa int *mpri, struct rt6_info *match, 623afc154e9SHannes Frederic Sowa bool *do_rr) 624554cfb7eSYOSHIFUJI Hideaki { 625554cfb7eSYOSHIFUJI Hideaki int m; 626afc154e9SHannes Frederic Sowa bool match_do_rr = false; 627554cfb7eSYOSHIFUJI Hideaki 628554cfb7eSYOSHIFUJI Hideaki if (rt6_check_expired(rt)) 629f11e6659SDavid S. Miller goto out; 630554cfb7eSYOSHIFUJI Hideaki 631554cfb7eSYOSHIFUJI Hideaki m = rt6_score_route(rt, oif, strict); 6327e980569SJiri Benc if (m == RT6_NUD_FAIL_DO_RR) { 633afc154e9SHannes Frederic Sowa match_do_rr = true; 634afc154e9SHannes Frederic Sowa m = 0; /* lowest valid score */ 6357e980569SJiri Benc } else if (m == RT6_NUD_FAIL_HARD) { 636f11e6659SDavid S. Miller goto out; 6371da177e4SLinus Torvalds } 638f11e6659SDavid S. Miller 639afc154e9SHannes Frederic Sowa if (strict & RT6_LOOKUP_F_REACHABLE) 640afc154e9SHannes Frederic Sowa rt6_probe(rt); 641afc154e9SHannes Frederic Sowa 6427e980569SJiri Benc /* note that m can be RT6_NUD_FAIL_PROBE at this point */ 643afc154e9SHannes Frederic Sowa if (m > *mpri) { 644afc154e9SHannes Frederic Sowa *do_rr = match_do_rr; 645afc154e9SHannes Frederic Sowa *mpri = m; 646afc154e9SHannes Frederic Sowa match = rt; 647afc154e9SHannes Frederic Sowa } 648f11e6659SDavid S. Miller out: 649f11e6659SDavid S. Miller return match; 6501da177e4SLinus Torvalds } 6511da177e4SLinus Torvalds 652f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn, 653f11e6659SDavid S. Miller struct rt6_info *rr_head, 654afc154e9SHannes Frederic Sowa u32 metric, int oif, int strict, 655afc154e9SHannes Frederic Sowa bool *do_rr) 656f11e6659SDavid S. Miller { 657f11e6659SDavid S. Miller struct rt6_info *rt, *match; 658f11e6659SDavid S. Miller int mpri = -1; 659f11e6659SDavid S. Miller 660f11e6659SDavid S. Miller match = NULL; 661f11e6659SDavid S. Miller for (rt = rr_head; rt && rt->rt6i_metric == metric; 662d8d1f30bSChangli Gao rt = rt->dst.rt6_next) 663afc154e9SHannes Frederic Sowa match = find_match(rt, oif, strict, &mpri, match, do_rr); 664f11e6659SDavid S. Miller for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric; 665d8d1f30bSChangli Gao rt = rt->dst.rt6_next) 666afc154e9SHannes Frederic Sowa match = find_match(rt, oif, strict, &mpri, match, do_rr); 667f11e6659SDavid S. Miller 668f11e6659SDavid S. Miller return match; 669f11e6659SDavid S. Miller } 670f11e6659SDavid S. Miller 671f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict) 672f11e6659SDavid S. Miller { 673f11e6659SDavid S. Miller struct rt6_info *match, *rt0; 6748ed67789SDaniel Lezcano struct net *net; 675afc154e9SHannes Frederic Sowa bool do_rr = false; 676f11e6659SDavid S. Miller 677f11e6659SDavid S. Miller rt0 = fn->rr_ptr; 678f11e6659SDavid S. Miller if (!rt0) 679f11e6659SDavid S. Miller fn->rr_ptr = rt0 = fn->leaf; 680f11e6659SDavid S. Miller 681afc154e9SHannes Frederic Sowa match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict, 682afc154e9SHannes Frederic Sowa &do_rr); 683f11e6659SDavid S. Miller 684afc154e9SHannes Frederic Sowa if (do_rr) { 685d8d1f30bSChangli Gao struct rt6_info *next = rt0->dst.rt6_next; 686f11e6659SDavid S. Miller 687554cfb7eSYOSHIFUJI Hideaki /* no entries matched; do round-robin */ 688f11e6659SDavid S. Miller if (!next || next->rt6i_metric != rt0->rt6i_metric) 689f11e6659SDavid S. Miller next = fn->leaf; 690f11e6659SDavid S. Miller 691f11e6659SDavid S. Miller if (next != rt0) 692f11e6659SDavid S. Miller fn->rr_ptr = next; 693554cfb7eSYOSHIFUJI Hideaki } 694554cfb7eSYOSHIFUJI Hideaki 695d1918542SDavid S. Miller net = dev_net(rt0->dst.dev); 696a02cec21SEric Dumazet return match ? match : net->ipv6.ip6_null_entry; 6971da177e4SLinus Torvalds } 6981da177e4SLinus Torvalds 69970ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 70070ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 701b71d1d42SEric Dumazet const struct in6_addr *gwaddr) 70270ceb4f5SYOSHIFUJI Hideaki { 703c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 70470ceb4f5SYOSHIFUJI Hideaki struct route_info *rinfo = (struct route_info *) opt; 70570ceb4f5SYOSHIFUJI Hideaki struct in6_addr prefix_buf, *prefix; 70670ceb4f5SYOSHIFUJI Hideaki unsigned int pref; 7074bed72e4SYOSHIFUJI Hideaki unsigned long lifetime; 70870ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt; 70970ceb4f5SYOSHIFUJI Hideaki 71070ceb4f5SYOSHIFUJI Hideaki if (len < sizeof(struct route_info)) { 71170ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 71270ceb4f5SYOSHIFUJI Hideaki } 71370ceb4f5SYOSHIFUJI Hideaki 71470ceb4f5SYOSHIFUJI Hideaki /* Sanity check for prefix_len and length */ 71570ceb4f5SYOSHIFUJI Hideaki if (rinfo->length > 3) { 71670ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 71770ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 128) { 71870ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 71970ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 64) { 72070ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 2) { 72170ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 72270ceb4f5SYOSHIFUJI Hideaki } 72370ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 0) { 72470ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 1) { 72570ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 72670ceb4f5SYOSHIFUJI Hideaki } 72770ceb4f5SYOSHIFUJI Hideaki } 72870ceb4f5SYOSHIFUJI Hideaki 72970ceb4f5SYOSHIFUJI Hideaki pref = rinfo->route_pref; 73070ceb4f5SYOSHIFUJI Hideaki if (pref == ICMPV6_ROUTER_PREF_INVALID) 7313933fc95SJens Rosenboom return -EINVAL; 73270ceb4f5SYOSHIFUJI Hideaki 7334bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); 73470ceb4f5SYOSHIFUJI Hideaki 73570ceb4f5SYOSHIFUJI Hideaki if (rinfo->length == 3) 73670ceb4f5SYOSHIFUJI Hideaki prefix = (struct in6_addr *)rinfo->prefix; 73770ceb4f5SYOSHIFUJI Hideaki else { 73870ceb4f5SYOSHIFUJI Hideaki /* this function is safe */ 73970ceb4f5SYOSHIFUJI Hideaki ipv6_addr_prefix(&prefix_buf, 74070ceb4f5SYOSHIFUJI Hideaki (struct in6_addr *)rinfo->prefix, 74170ceb4f5SYOSHIFUJI Hideaki rinfo->prefix_len); 74270ceb4f5SYOSHIFUJI Hideaki prefix = &prefix_buf; 74370ceb4f5SYOSHIFUJI Hideaki } 74470ceb4f5SYOSHIFUJI Hideaki 745f104a567SDuan Jiong if (rinfo->prefix_len == 0) 746f104a567SDuan Jiong rt = rt6_get_dflt_router(gwaddr, dev); 747f104a567SDuan Jiong else 748f104a567SDuan Jiong rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, 749f104a567SDuan Jiong gwaddr, dev->ifindex); 75070ceb4f5SYOSHIFUJI Hideaki 75170ceb4f5SYOSHIFUJI Hideaki if (rt && !lifetime) { 752e0a1ad73SThomas Graf ip6_del_rt(rt); 75370ceb4f5SYOSHIFUJI Hideaki rt = NULL; 75470ceb4f5SYOSHIFUJI Hideaki } 75570ceb4f5SYOSHIFUJI Hideaki 75670ceb4f5SYOSHIFUJI Hideaki if (!rt && lifetime) 757efa2cea0SDaniel Lezcano rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex, 75870ceb4f5SYOSHIFUJI Hideaki pref); 75970ceb4f5SYOSHIFUJI Hideaki else if (rt) 76070ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags = RTF_ROUTEINFO | 76170ceb4f5SYOSHIFUJI Hideaki (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 76270ceb4f5SYOSHIFUJI Hideaki 76370ceb4f5SYOSHIFUJI Hideaki if (rt) { 7641716a961SGao feng if (!addrconf_finite_timeout(lifetime)) 7651716a961SGao feng rt6_clean_expires(rt); 7661716a961SGao feng else 7671716a961SGao feng rt6_set_expires(rt, jiffies + HZ * lifetime); 7681716a961SGao feng 76994e187c0SAmerigo Wang ip6_rt_put(rt); 77070ceb4f5SYOSHIFUJI Hideaki } 77170ceb4f5SYOSHIFUJI Hideaki return 0; 77270ceb4f5SYOSHIFUJI Hideaki } 77370ceb4f5SYOSHIFUJI Hideaki #endif 77470ceb4f5SYOSHIFUJI Hideaki 775a3c00e46SMartin KaFai Lau static struct fib6_node* fib6_backtrack(struct fib6_node *fn, 776a3c00e46SMartin KaFai Lau struct in6_addr *saddr) 777a3c00e46SMartin KaFai Lau { 778a3c00e46SMartin KaFai Lau struct fib6_node *pn; 779a3c00e46SMartin KaFai Lau while (1) { 780a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_TL_ROOT) 781a3c00e46SMartin KaFai Lau return NULL; 782a3c00e46SMartin KaFai Lau pn = fn->parent; 783a3c00e46SMartin KaFai Lau if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) 784a3c00e46SMartin KaFai Lau fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); 785a3c00e46SMartin KaFai Lau else 786a3c00e46SMartin KaFai Lau fn = pn; 787a3c00e46SMartin KaFai Lau if (fn->fn_flags & RTN_RTINFO) 788a3c00e46SMartin KaFai Lau return fn; 789a3c00e46SMartin KaFai Lau } 790a3c00e46SMartin KaFai Lau } 791c71099acSThomas Graf 7928ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net, 7938ed67789SDaniel Lezcano struct fib6_table *table, 7944c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 7951da177e4SLinus Torvalds { 7961da177e4SLinus Torvalds struct fib6_node *fn; 7971da177e4SLinus Torvalds struct rt6_info *rt; 7981da177e4SLinus Torvalds 799c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 8004c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 801c71099acSThomas Graf restart: 802c71099acSThomas Graf rt = fn->leaf; 8034c9483b2SDavid S. Miller rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags); 80451ebd318SNicolas Dichtel if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0) 80552bd4c0cSNicolas Dichtel rt = rt6_multipath_select(rt, fl6, fl6->flowi6_oif, flags); 806a3c00e46SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 807a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 808a3c00e46SMartin KaFai Lau if (fn) 809a3c00e46SMartin KaFai Lau goto restart; 810a3c00e46SMartin KaFai Lau } 811d8d1f30bSChangli Gao dst_use(&rt->dst, jiffies); 812c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 8131da177e4SLinus Torvalds return rt; 814c71099acSThomas Graf 815c71099acSThomas Graf } 816c71099acSThomas Graf 817ea6e574eSFlorian Westphal struct dst_entry *ip6_route_lookup(struct net *net, struct flowi6 *fl6, 818ea6e574eSFlorian Westphal int flags) 819ea6e574eSFlorian Westphal { 820ea6e574eSFlorian Westphal return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup); 821ea6e574eSFlorian Westphal } 822ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup); 823ea6e574eSFlorian Westphal 8249acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, 8259acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *saddr, int oif, int strict) 826c71099acSThomas Graf { 8274c9483b2SDavid S. Miller struct flowi6 fl6 = { 8284c9483b2SDavid S. Miller .flowi6_oif = oif, 8294c9483b2SDavid S. Miller .daddr = *daddr, 830c71099acSThomas Graf }; 831c71099acSThomas Graf struct dst_entry *dst; 83277d16f45SYOSHIFUJI Hideaki int flags = strict ? RT6_LOOKUP_F_IFACE : 0; 833c71099acSThomas Graf 834adaa70bbSThomas Graf if (saddr) { 8354c9483b2SDavid S. Miller memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 836adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 837adaa70bbSThomas Graf } 838adaa70bbSThomas Graf 8394c9483b2SDavid S. Miller dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup); 840c71099acSThomas Graf if (dst->error == 0) 841c71099acSThomas Graf return (struct rt6_info *) dst; 842c71099acSThomas Graf 843c71099acSThomas Graf dst_release(dst); 844c71099acSThomas Graf 8451da177e4SLinus Torvalds return NULL; 8461da177e4SLinus Torvalds } 8477159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup); 8487159039aSYOSHIFUJI Hideaki 849c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock. 8501da177e4SLinus Torvalds It takes new route entry, the addition fails by any reason the 8511da177e4SLinus Torvalds route is freed. In any case, if caller does not hold it, it may 8521da177e4SLinus Torvalds be destroyed. 8531da177e4SLinus Torvalds */ 8541da177e4SLinus Torvalds 855e5fd387aSMichal Kubeček static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info, 856e5fd387aSMichal Kubeček struct nlattr *mx, int mx_len) 8571da177e4SLinus Torvalds { 8581da177e4SLinus Torvalds int err; 859c71099acSThomas Graf struct fib6_table *table; 8601da177e4SLinus Torvalds 861c71099acSThomas Graf table = rt->rt6i_table; 862c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 863e5fd387aSMichal Kubeček err = fib6_add(&table->tb6_root, rt, info, mx, mx_len); 864c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 8651da177e4SLinus Torvalds 8661da177e4SLinus Torvalds return err; 8671da177e4SLinus Torvalds } 8681da177e4SLinus Torvalds 86940e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt) 87040e22e8fSThomas Graf { 8714d1169c1SDenis V. Lunev struct nl_info info = { 872d1918542SDavid S. Miller .nl_net = dev_net(rt->dst.dev), 8734d1169c1SDenis V. Lunev }; 874e5fd387aSMichal Kubeček return __ip6_ins_rt(rt, &info, NULL, 0); 87540e22e8fSThomas Graf } 87640e22e8fSThomas Graf 8771716a961SGao feng static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, 87821efcfa0SEric Dumazet const struct in6_addr *daddr, 879b71d1d42SEric Dumazet const struct in6_addr *saddr) 8801da177e4SLinus Torvalds { 8811da177e4SLinus Torvalds struct rt6_info *rt; 8821da177e4SLinus Torvalds 8831da177e4SLinus Torvalds /* 8841da177e4SLinus Torvalds * Clone the route. 8851da177e4SLinus Torvalds */ 8861da177e4SLinus Torvalds 88721efcfa0SEric Dumazet rt = ip6_rt_copy(ort, daddr); 8881da177e4SLinus Torvalds 8891da177e4SLinus Torvalds if (rt) { 890bb3c3686SDavid S. Miller if (ort->rt6i_dst.plen != 128 && 89121efcfa0SEric Dumazet ipv6_addr_equal(&ort->rt6i_dst.addr, daddr)) 89258c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 8931da177e4SLinus Torvalds 8941da177e4SLinus Torvalds rt->rt6i_flags |= RTF_CACHE; 8951da177e4SLinus Torvalds 8961da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 8971da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 8984e3fd7a0SAlexey Dobriyan rt->rt6i_src.addr = *saddr; 8991da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 9001da177e4SLinus Torvalds } 9011da177e4SLinus Torvalds #endif 90295a9a5baSYOSHIFUJI Hideaki } 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds return rt; 9051da177e4SLinus Torvalds } 90695a9a5baSYOSHIFUJI Hideaki 90721efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, 90821efcfa0SEric Dumazet const struct in6_addr *daddr) 909299d9939SYOSHIFUJI Hideaki { 91021efcfa0SEric Dumazet struct rt6_info *rt = ip6_rt_copy(ort, daddr); 91121efcfa0SEric Dumazet 912887c95ccSYOSHIFUJI Hideaki / 吉藤英明 if (rt) 913299d9939SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_CACHE; 914299d9939SYOSHIFUJI Hideaki return rt; 915299d9939SYOSHIFUJI Hideaki } 916299d9939SYOSHIFUJI Hideaki 9178ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif, 9184c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 9191da177e4SLinus Torvalds { 920*367efcb9SMartin KaFai Lau struct fib6_node *fn, *saved_fn; 921519fbd87SYOSHIFUJI Hideaki struct rt6_info *rt, *nrt; 922c71099acSThomas Graf int strict = 0; 9231da177e4SLinus Torvalds int attempts = 3; 924519fbd87SYOSHIFUJI Hideaki int err; 9251da177e4SLinus Torvalds 92677d16f45SYOSHIFUJI Hideaki strict |= flags & RT6_LOOKUP_F_IFACE; 927*367efcb9SMartin KaFai Lau if (net->ipv6.devconf_all->forwarding == 0) 928*367efcb9SMartin KaFai Lau strict |= RT6_LOOKUP_F_REACHABLE; 9291da177e4SLinus Torvalds 930a3c00e46SMartin KaFai Lau redo_fib6_lookup_lock: 931c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 9321da177e4SLinus Torvalds 9334c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 934*367efcb9SMartin KaFai Lau saved_fn = fn; 9351da177e4SLinus Torvalds 936a3c00e46SMartin KaFai Lau redo_rt6_select: 937*367efcb9SMartin KaFai Lau rt = rt6_select(fn, oif, strict); 93852bd4c0cSNicolas Dichtel if (rt->rt6i_nsiblings) 939*367efcb9SMartin KaFai Lau rt = rt6_multipath_select(rt, fl6, oif, strict); 940a3c00e46SMartin KaFai Lau if (rt == net->ipv6.ip6_null_entry) { 941a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 942a3c00e46SMartin KaFai Lau if (fn) 943a3c00e46SMartin KaFai Lau goto redo_rt6_select; 944*367efcb9SMartin KaFai Lau else if (strict & RT6_LOOKUP_F_REACHABLE) { 945*367efcb9SMartin KaFai Lau /* also consider unreachable route */ 946*367efcb9SMartin KaFai Lau strict &= ~RT6_LOOKUP_F_REACHABLE; 947*367efcb9SMartin KaFai Lau fn = saved_fn; 948*367efcb9SMartin KaFai Lau goto redo_rt6_select; 949*367efcb9SMartin KaFai Lau } else { 950*367efcb9SMartin KaFai Lau dst_hold(&rt->dst); 951*367efcb9SMartin KaFai Lau read_unlock_bh(&table->tb6_lock); 952*367efcb9SMartin KaFai Lau goto out2; 953*367efcb9SMartin KaFai Lau } 954a3c00e46SMartin KaFai Lau } 955a3c00e46SMartin KaFai Lau 956d8d1f30bSChangli Gao dst_hold(&rt->dst); 957c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 9581da177e4SLinus Torvalds 95994c77bb4SMartin KaFai Lau if (rt->rt6i_flags & RTF_CACHE) 96094c77bb4SMartin KaFai Lau goto out2; 96194c77bb4SMartin KaFai Lau 962c440f160SYOSHIFUJI Hideaki / 吉藤英明 if (!(rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY))) 9634c9483b2SDavid S. Miller nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr); 9647343ff31SDavid S. Miller else if (!(rt->dst.flags & DST_HOST)) 9654c9483b2SDavid S. Miller nrt = rt6_alloc_clone(rt, &fl6->daddr); 9667343ff31SDavid S. Miller else 9677343ff31SDavid S. Miller goto out2; 9681da177e4SLinus Torvalds 96994e187c0SAmerigo Wang ip6_rt_put(rt); 9708ed67789SDaniel Lezcano rt = nrt ? : net->ipv6.ip6_null_entry; 9711da177e4SLinus Torvalds 972d8d1f30bSChangli Gao dst_hold(&rt->dst); 973e40cf353SYOSHIFUJI Hideaki if (nrt) { 97440e22e8fSThomas Graf err = ip6_ins_rt(nrt); 975e40cf353SYOSHIFUJI Hideaki if (!err) 976e40cf353SYOSHIFUJI Hideaki goto out2; 977e40cf353SYOSHIFUJI Hideaki } 978e40cf353SYOSHIFUJI Hideaki 979e40cf353SYOSHIFUJI Hideaki if (--attempts <= 0) 9801da177e4SLinus Torvalds goto out2; 9811da177e4SLinus Torvalds 982519fbd87SYOSHIFUJI Hideaki /* 983c71099acSThomas Graf * Race condition! In the gap, when table->tb6_lock was 984519fbd87SYOSHIFUJI Hideaki * released someone could insert this route. Relookup. 9851da177e4SLinus Torvalds */ 98694e187c0SAmerigo Wang ip6_rt_put(rt); 987a3c00e46SMartin KaFai Lau goto redo_fib6_lookup_lock; 988e40cf353SYOSHIFUJI Hideaki 9891da177e4SLinus Torvalds out2: 990d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 991d8d1f30bSChangli Gao rt->dst.__use++; 992c71099acSThomas Graf 993c71099acSThomas Graf return rt; 994c71099acSThomas Graf } 995c71099acSThomas Graf 9968ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table, 9974c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 9984acad72dSPavel Emelyanov { 9994c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags); 10004acad72dSPavel Emelyanov } 10014acad72dSPavel Emelyanov 100272331bc0SShmulik Ladkani static struct dst_entry *ip6_route_input_lookup(struct net *net, 100372331bc0SShmulik Ladkani struct net_device *dev, 100472331bc0SShmulik Ladkani struct flowi6 *fl6, int flags) 100572331bc0SShmulik Ladkani { 100672331bc0SShmulik Ladkani if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) 100772331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_IFACE; 100872331bc0SShmulik Ladkani 100972331bc0SShmulik Ladkani return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input); 101072331bc0SShmulik Ladkani } 101172331bc0SShmulik Ladkani 1012c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 1013c71099acSThomas Graf { 1014b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 1015c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 1016adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 10174c9483b2SDavid S. Miller struct flowi6 fl6 = { 10184c9483b2SDavid S. Miller .flowi6_iif = skb->dev->ifindex, 10194c9483b2SDavid S. Miller .daddr = iph->daddr, 10204c9483b2SDavid S. Miller .saddr = iph->saddr, 10216502ca52SYOSHIFUJI Hideaki / 吉藤英明 .flowlabel = ip6_flowinfo(iph), 10224c9483b2SDavid S. Miller .flowi6_mark = skb->mark, 10234c9483b2SDavid S. Miller .flowi6_proto = iph->nexthdr, 1024c71099acSThomas Graf }; 1025adaa70bbSThomas Graf 102672331bc0SShmulik Ladkani skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags)); 1027c71099acSThomas Graf } 1028c71099acSThomas Graf 10298ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table, 10304c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 1031c71099acSThomas Graf { 10324c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags); 1033c71099acSThomas Graf } 1034c71099acSThomas Graf 10359c7a4f9cSFlorian Westphal struct dst_entry *ip6_route_output(struct net *net, const struct sock *sk, 10364c9483b2SDavid S. Miller struct flowi6 *fl6) 1037c71099acSThomas Graf { 1038c71099acSThomas Graf int flags = 0; 1039c71099acSThomas Graf 10401fb9489bSPavel Emelyanov fl6->flowi6_iif = LOOPBACK_IFINDEX; 10414dc27d1cSDavid McCullough 10424c9483b2SDavid S. Miller if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr)) 104377d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 1044c71099acSThomas Graf 10454c9483b2SDavid S. Miller if (!ipv6_addr_any(&fl6->saddr)) 1046adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 10470c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 10480c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 1049adaa70bbSThomas Graf 10504c9483b2SDavid S. Miller return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output); 10511da177e4SLinus Torvalds } 10527159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output); 10531da177e4SLinus Torvalds 10542774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 105514e50e57SDavid S. Miller { 10565c1e6aa3SDavid S. Miller struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; 105714e50e57SDavid S. Miller struct dst_entry *new = NULL; 105814e50e57SDavid S. Miller 1059f5b0a874SDavid S. Miller rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, DST_OBSOLETE_NONE, 0); 106014e50e57SDavid S. Miller if (rt) { 1061d8d1f30bSChangli Gao new = &rt->dst; 106214e50e57SDavid S. Miller 10638104891bSSteffen Klassert memset(new + 1, 0, sizeof(*rt) - sizeof(*new)); 10648104891bSSteffen Klassert rt6_init_peer(rt, net->ipv6.peers); 10658104891bSSteffen Klassert 106614e50e57SDavid S. Miller new->__use = 1; 1067352e512cSHerbert Xu new->input = dst_discard; 1068aad88724SEric Dumazet new->output = dst_discard_sk; 106914e50e57SDavid S. Miller 107021efcfa0SEric Dumazet if (dst_metrics_read_only(&ort->dst)) 107121efcfa0SEric Dumazet new->_metrics = ort->dst._metrics; 107221efcfa0SEric Dumazet else 1073defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 107414e50e57SDavid S. Miller rt->rt6i_idev = ort->rt6i_idev; 107514e50e57SDavid S. Miller if (rt->rt6i_idev) 107614e50e57SDavid S. Miller in6_dev_hold(rt->rt6i_idev); 107714e50e57SDavid S. Miller 10784e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 10791716a961SGao feng rt->rt6i_flags = ort->rt6i_flags; 108014e50e57SDavid S. Miller rt->rt6i_metric = 0; 108114e50e57SDavid S. Miller 108214e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 108314e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 108414e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 108514e50e57SDavid S. Miller #endif 108614e50e57SDavid S. Miller 108714e50e57SDavid S. Miller dst_free(new); 108814e50e57SDavid S. Miller } 108914e50e57SDavid S. Miller 109069ead7afSDavid S. Miller dst_release(dst_orig); 109169ead7afSDavid S. Miller return new ? new : ERR_PTR(-ENOMEM); 109214e50e57SDavid S. Miller } 109314e50e57SDavid S. Miller 10941da177e4SLinus Torvalds /* 10951da177e4SLinus Torvalds * Destination cache support functions 10961da177e4SLinus Torvalds */ 10971da177e4SLinus Torvalds 10981da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 10991da177e4SLinus Torvalds { 11001da177e4SLinus Torvalds struct rt6_info *rt; 11011da177e4SLinus Torvalds 11021da177e4SLinus Torvalds rt = (struct rt6_info *) dst; 11031da177e4SLinus Torvalds 11046f3118b5SNicolas Dichtel /* All IPV6 dsts are created with ->obsolete set to the value 11056f3118b5SNicolas Dichtel * DST_OBSOLETE_FORCE_CHK which forces validation calls down 11066f3118b5SNicolas Dichtel * into this function always. 11076f3118b5SNicolas Dichtel */ 1108e3bc10bdSHannes Frederic Sowa if (!rt->rt6i_node || (rt->rt6i_node->fn_sernum != cookie)) 11091da177e4SLinus Torvalds return NULL; 1110e3bc10bdSHannes Frederic Sowa 1111e3bc10bdSHannes Frederic Sowa if (rt6_check_expired(rt)) 1112e3bc10bdSHannes Frederic Sowa return NULL; 1113e3bc10bdSHannes Frederic Sowa 1114e3bc10bdSHannes Frederic Sowa return dst; 11151da177e4SLinus Torvalds } 11161da177e4SLinus Torvalds 11171da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 11181da177e4SLinus Torvalds { 11191da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 11201da177e4SLinus Torvalds 11211da177e4SLinus Torvalds if (rt) { 112254c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 112354c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 1124e0a1ad73SThomas Graf ip6_del_rt(rt); 112554c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 11261da177e4SLinus Torvalds } 112754c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 112854c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 112954c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 113054c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 113154c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 113254c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 11331da177e4SLinus Torvalds } 11341da177e4SLinus Torvalds 11351da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 11361da177e4SLinus Torvalds { 11371da177e4SLinus Torvalds struct rt6_info *rt; 11381da177e4SLinus Torvalds 11393ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 11401da177e4SLinus Torvalds 1141adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 11421da177e4SLinus Torvalds if (rt) { 11431eb4f758SHannes Frederic Sowa if (rt->rt6i_flags & RTF_CACHE) { 11441eb4f758SHannes Frederic Sowa dst_hold(&rt->dst); 11451eb4f758SHannes Frederic Sowa if (ip6_del_rt(rt)) 11461eb4f758SHannes Frederic Sowa dst_free(&rt->dst); 11471eb4f758SHannes Frederic Sowa } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) { 11481da177e4SLinus Torvalds rt->rt6i_node->fn_sernum = -1; 11491da177e4SLinus Torvalds } 11501da177e4SLinus Torvalds } 11511eb4f758SHannes Frederic Sowa } 11521da177e4SLinus Torvalds 11536700c270SDavid S. Miller static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 11546700c270SDavid S. Miller struct sk_buff *skb, u32 mtu) 11551da177e4SLinus Torvalds { 11561da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info *)dst; 11571da177e4SLinus Torvalds 115881aded24SDavid S. Miller dst_confirm(dst); 11591da177e4SLinus Torvalds if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) { 116081aded24SDavid S. Miller struct net *net = dev_net(dst->dev); 116181aded24SDavid S. Miller 11621da177e4SLinus Torvalds rt6->rt6i_flags |= RTF_MODIFIED; 11631da177e4SLinus Torvalds if (mtu < IPV6_MIN_MTU) { 1164defb3519SDavid S. Miller u32 features = dst_metric(dst, RTAX_FEATURES); 11651da177e4SLinus Torvalds mtu = IPV6_MIN_MTU; 1166defb3519SDavid S. Miller features |= RTAX_FEATURE_ALLFRAG; 1167defb3519SDavid S. Miller dst_metric_set(dst, RTAX_FEATURES, features); 11681da177e4SLinus Torvalds } 1169defb3519SDavid S. Miller dst_metric_set(dst, RTAX_MTU, mtu); 117081aded24SDavid S. Miller rt6_update_expires(rt6, net->ipv6.sysctl.ip6_rt_mtu_expires); 11711da177e4SLinus Torvalds } 11721da177e4SLinus Torvalds } 11731da177e4SLinus Torvalds 117442ae66c8SDavid S. Miller void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, 117542ae66c8SDavid S. Miller int oif, u32 mark) 117681aded24SDavid S. Miller { 117781aded24SDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 117881aded24SDavid S. Miller struct dst_entry *dst; 117981aded24SDavid S. Miller struct flowi6 fl6; 118081aded24SDavid S. Miller 118181aded24SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 118281aded24SDavid S. Miller fl6.flowi6_oif = oif; 11831b3c61dcSLorenzo Colitti fl6.flowi6_mark = mark ? mark : IP6_REPLY_MARK(net, skb->mark); 118481aded24SDavid S. Miller fl6.daddr = iph->daddr; 118581aded24SDavid S. Miller fl6.saddr = iph->saddr; 11866502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 118781aded24SDavid S. Miller 118881aded24SDavid S. Miller dst = ip6_route_output(net, NULL, &fl6); 118981aded24SDavid S. Miller if (!dst->error) 11906700c270SDavid S. Miller ip6_rt_update_pmtu(dst, NULL, skb, ntohl(mtu)); 119181aded24SDavid S. Miller dst_release(dst); 119281aded24SDavid S. Miller } 119381aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_update_pmtu); 119481aded24SDavid S. Miller 119581aded24SDavid S. Miller void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) 119681aded24SDavid S. Miller { 119781aded24SDavid S. Miller ip6_update_pmtu(skb, sock_net(sk), mtu, 119881aded24SDavid S. Miller sk->sk_bound_dev_if, sk->sk_mark); 119981aded24SDavid S. Miller } 120081aded24SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); 120181aded24SDavid S. Miller 1202b55b76b2SDuan Jiong /* Handle redirects */ 1203b55b76b2SDuan Jiong struct ip6rd_flowi { 1204b55b76b2SDuan Jiong struct flowi6 fl6; 1205b55b76b2SDuan Jiong struct in6_addr gateway; 1206b55b76b2SDuan Jiong }; 1207b55b76b2SDuan Jiong 1208b55b76b2SDuan Jiong static struct rt6_info *__ip6_route_redirect(struct net *net, 1209b55b76b2SDuan Jiong struct fib6_table *table, 1210b55b76b2SDuan Jiong struct flowi6 *fl6, 1211b55b76b2SDuan Jiong int flags) 1212b55b76b2SDuan Jiong { 1213b55b76b2SDuan Jiong struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; 1214b55b76b2SDuan Jiong struct rt6_info *rt; 1215b55b76b2SDuan Jiong struct fib6_node *fn; 1216b55b76b2SDuan Jiong 1217b55b76b2SDuan Jiong /* Get the "current" route for this destination and 1218b55b76b2SDuan Jiong * check if the redirect has come from approriate router. 1219b55b76b2SDuan Jiong * 1220b55b76b2SDuan Jiong * RFC 4861 specifies that redirects should only be 1221b55b76b2SDuan Jiong * accepted if they come from the nexthop to the target. 1222b55b76b2SDuan Jiong * Due to the way the routes are chosen, this notion 1223b55b76b2SDuan Jiong * is a bit fuzzy and one might need to check all possible 1224b55b76b2SDuan Jiong * routes. 1225b55b76b2SDuan Jiong */ 1226b55b76b2SDuan Jiong 1227b55b76b2SDuan Jiong read_lock_bh(&table->tb6_lock); 1228b55b76b2SDuan Jiong fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1229b55b76b2SDuan Jiong restart: 1230b55b76b2SDuan Jiong for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 1231b55b76b2SDuan Jiong if (rt6_check_expired(rt)) 1232b55b76b2SDuan Jiong continue; 1233b55b76b2SDuan Jiong if (rt->dst.error) 1234b55b76b2SDuan Jiong break; 1235b55b76b2SDuan Jiong if (!(rt->rt6i_flags & RTF_GATEWAY)) 1236b55b76b2SDuan Jiong continue; 1237b55b76b2SDuan Jiong if (fl6->flowi6_oif != rt->dst.dev->ifindex) 1238b55b76b2SDuan Jiong continue; 1239b55b76b2SDuan Jiong if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway)) 1240b55b76b2SDuan Jiong continue; 1241b55b76b2SDuan Jiong break; 1242b55b76b2SDuan Jiong } 1243b55b76b2SDuan Jiong 1244b55b76b2SDuan Jiong if (!rt) 1245b55b76b2SDuan Jiong rt = net->ipv6.ip6_null_entry; 1246b55b76b2SDuan Jiong else if (rt->dst.error) { 1247b55b76b2SDuan Jiong rt = net->ipv6.ip6_null_entry; 1248a3c00e46SMartin KaFai Lau } else if (rt == net->ipv6.ip6_null_entry) { 1249a3c00e46SMartin KaFai Lau fn = fib6_backtrack(fn, &fl6->saddr); 1250a3c00e46SMartin KaFai Lau if (fn) 1251a3c00e46SMartin KaFai Lau goto restart; 1252b55b76b2SDuan Jiong } 1253a3c00e46SMartin KaFai Lau 1254b55b76b2SDuan Jiong dst_hold(&rt->dst); 1255b55b76b2SDuan Jiong 1256b55b76b2SDuan Jiong read_unlock_bh(&table->tb6_lock); 1257b55b76b2SDuan Jiong 1258b55b76b2SDuan Jiong return rt; 1259b55b76b2SDuan Jiong }; 1260b55b76b2SDuan Jiong 1261b55b76b2SDuan Jiong static struct dst_entry *ip6_route_redirect(struct net *net, 1262b55b76b2SDuan Jiong const struct flowi6 *fl6, 1263b55b76b2SDuan Jiong const struct in6_addr *gateway) 1264b55b76b2SDuan Jiong { 1265b55b76b2SDuan Jiong int flags = RT6_LOOKUP_F_HAS_SADDR; 1266b55b76b2SDuan Jiong struct ip6rd_flowi rdfl; 1267b55b76b2SDuan Jiong 1268b55b76b2SDuan Jiong rdfl.fl6 = *fl6; 1269b55b76b2SDuan Jiong rdfl.gateway = *gateway; 1270b55b76b2SDuan Jiong 1271b55b76b2SDuan Jiong return fib6_rule_lookup(net, &rdfl.fl6, 1272b55b76b2SDuan Jiong flags, __ip6_route_redirect); 1273b55b76b2SDuan Jiong } 1274b55b76b2SDuan Jiong 12753a5ad2eeSDavid S. Miller void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark) 12763a5ad2eeSDavid S. Miller { 12773a5ad2eeSDavid S. Miller const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; 12783a5ad2eeSDavid S. Miller struct dst_entry *dst; 12793a5ad2eeSDavid S. Miller struct flowi6 fl6; 12803a5ad2eeSDavid S. Miller 12813a5ad2eeSDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 1282e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 12833a5ad2eeSDavid S. Miller fl6.flowi6_oif = oif; 12843a5ad2eeSDavid S. Miller fl6.flowi6_mark = mark; 12853a5ad2eeSDavid S. Miller fl6.daddr = iph->daddr; 12863a5ad2eeSDavid S. Miller fl6.saddr = iph->saddr; 12876502ca52SYOSHIFUJI Hideaki / 吉藤英明 fl6.flowlabel = ip6_flowinfo(iph); 12883a5ad2eeSDavid S. Miller 1289b55b76b2SDuan Jiong dst = ip6_route_redirect(net, &fl6, &ipv6_hdr(skb)->saddr); 12906700c270SDavid S. Miller rt6_do_redirect(dst, NULL, skb); 12913a5ad2eeSDavid S. Miller dst_release(dst); 12923a5ad2eeSDavid S. Miller } 12933a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_redirect); 12943a5ad2eeSDavid S. Miller 1295c92a59ecSDuan Jiong void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif, 1296c92a59ecSDuan Jiong u32 mark) 1297c92a59ecSDuan Jiong { 1298c92a59ecSDuan Jiong const struct ipv6hdr *iph = ipv6_hdr(skb); 1299c92a59ecSDuan Jiong const struct rd_msg *msg = (struct rd_msg *)icmp6_hdr(skb); 1300c92a59ecSDuan Jiong struct dst_entry *dst; 1301c92a59ecSDuan Jiong struct flowi6 fl6; 1302c92a59ecSDuan Jiong 1303c92a59ecSDuan Jiong memset(&fl6, 0, sizeof(fl6)); 1304e374c618SJulian Anastasov fl6.flowi6_iif = LOOPBACK_IFINDEX; 1305c92a59ecSDuan Jiong fl6.flowi6_oif = oif; 1306c92a59ecSDuan Jiong fl6.flowi6_mark = mark; 1307c92a59ecSDuan Jiong fl6.daddr = msg->dest; 1308c92a59ecSDuan Jiong fl6.saddr = iph->daddr; 1309c92a59ecSDuan Jiong 1310b55b76b2SDuan Jiong dst = ip6_route_redirect(net, &fl6, &iph->saddr); 1311c92a59ecSDuan Jiong rt6_do_redirect(dst, NULL, skb); 1312c92a59ecSDuan Jiong dst_release(dst); 1313c92a59ecSDuan Jiong } 1314c92a59ecSDuan Jiong 13153a5ad2eeSDavid S. Miller void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) 13163a5ad2eeSDavid S. Miller { 13173a5ad2eeSDavid S. Miller ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark); 13183a5ad2eeSDavid S. Miller } 13193a5ad2eeSDavid S. Miller EXPORT_SYMBOL_GPL(ip6_sk_redirect); 13203a5ad2eeSDavid S. Miller 13210dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 13221da177e4SLinus Torvalds { 13230dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 13240dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 13250dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 13260dbaee3bSDavid S. Miller 13271da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 13281da177e4SLinus Torvalds 13295578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 13305578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 13311da177e4SLinus Torvalds 13321da177e4SLinus Torvalds /* 13331da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 13341da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 13351da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 13361da177e4SLinus Torvalds * rely only on pmtu discovery" 13371da177e4SLinus Torvalds */ 13381da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 13391da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 13401da177e4SLinus Torvalds return mtu; 13411da177e4SLinus Torvalds } 13421da177e4SLinus Torvalds 1343ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst) 1344d33e4553SDavid S. Miller { 1345d33e4553SDavid S. Miller struct inet6_dev *idev; 1346618f9bc7SSteffen Klassert unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); 1347618f9bc7SSteffen Klassert 1348618f9bc7SSteffen Klassert if (mtu) 134930f78d8eSEric Dumazet goto out; 1350618f9bc7SSteffen Klassert 1351618f9bc7SSteffen Klassert mtu = IPV6_MIN_MTU; 1352d33e4553SDavid S. Miller 1353d33e4553SDavid S. Miller rcu_read_lock(); 1354d33e4553SDavid S. Miller idev = __in6_dev_get(dst->dev); 1355d33e4553SDavid S. Miller if (idev) 1356d33e4553SDavid S. Miller mtu = idev->cnf.mtu6; 1357d33e4553SDavid S. Miller rcu_read_unlock(); 1358d33e4553SDavid S. Miller 135930f78d8eSEric Dumazet out: 136030f78d8eSEric Dumazet return min_t(unsigned int, mtu, IP6_MAX_MTU); 1361d33e4553SDavid S. Miller } 1362d33e4553SDavid S. Miller 13633b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list; 13643b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock); 13655d0bbeebSThomas Graf 13663b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 136787a11578SDavid S. Miller struct flowi6 *fl6) 13681da177e4SLinus Torvalds { 136987a11578SDavid S. Miller struct dst_entry *dst; 13701da177e4SLinus Torvalds struct rt6_info *rt; 13711da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 1372c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 13731da177e4SLinus Torvalds 137438308473SDavid S. Miller if (unlikely(!idev)) 1375122bdf67SEric Dumazet return ERR_PTR(-ENODEV); 13761da177e4SLinus Torvalds 13778b96d22dSDavid S. Miller rt = ip6_dst_alloc(net, dev, 0, NULL); 137838308473SDavid S. Miller if (unlikely(!rt)) { 13791da177e4SLinus Torvalds in6_dev_put(idev); 138087a11578SDavid S. Miller dst = ERR_PTR(-ENOMEM); 13811da177e4SLinus Torvalds goto out; 13821da177e4SLinus Torvalds } 13831da177e4SLinus Torvalds 13848e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 13858e2ec639SYan, Zheng rt->dst.output = ip6_output; 1386d8d1f30bSChangli Gao atomic_set(&rt->dst.__refcnt, 1); 1387550bab42SJulian Anastasov rt->rt6i_gateway = fl6->daddr; 138887a11578SDavid S. Miller rt->rt6i_dst.addr = fl6->daddr; 13898e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 13908e2ec639SYan, Zheng rt->rt6i_idev = idev; 139114edd87dSLi RongQing dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); 13921da177e4SLinus Torvalds 13933b00944cSYOSHIFUJI Hideaki spin_lock_bh(&icmp6_dst_lock); 1394d8d1f30bSChangli Gao rt->dst.next = icmp6_dst_gc_list; 1395d8d1f30bSChangli Gao icmp6_dst_gc_list = &rt->dst; 13963b00944cSYOSHIFUJI Hideaki spin_unlock_bh(&icmp6_dst_lock); 13971da177e4SLinus Torvalds 13985578689aSDaniel Lezcano fib6_force_start_gc(net); 13991da177e4SLinus Torvalds 140087a11578SDavid S. Miller dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); 140187a11578SDavid S. Miller 14021da177e4SLinus Torvalds out: 140387a11578SDavid S. Miller return dst; 14041da177e4SLinus Torvalds } 14051da177e4SLinus Torvalds 14063d0f24a7SStephen Hemminger int icmp6_dst_gc(void) 14071da177e4SLinus Torvalds { 1408e9476e95SHagen Paul Pfeifer struct dst_entry *dst, **pprev; 14093d0f24a7SStephen Hemminger int more = 0; 14101da177e4SLinus Torvalds 14113b00944cSYOSHIFUJI Hideaki spin_lock_bh(&icmp6_dst_lock); 14123b00944cSYOSHIFUJI Hideaki pprev = &icmp6_dst_gc_list; 14135d0bbeebSThomas Graf 14141da177e4SLinus Torvalds while ((dst = *pprev) != NULL) { 14151da177e4SLinus Torvalds if (!atomic_read(&dst->__refcnt)) { 14161da177e4SLinus Torvalds *pprev = dst->next; 14171da177e4SLinus Torvalds dst_free(dst); 14181da177e4SLinus Torvalds } else { 14191da177e4SLinus Torvalds pprev = &dst->next; 14203d0f24a7SStephen Hemminger ++more; 14211da177e4SLinus Torvalds } 14221da177e4SLinus Torvalds } 14231da177e4SLinus Torvalds 14243b00944cSYOSHIFUJI Hideaki spin_unlock_bh(&icmp6_dst_lock); 14255d0bbeebSThomas Graf 14263d0f24a7SStephen Hemminger return more; 14271da177e4SLinus Torvalds } 14281da177e4SLinus Torvalds 14291e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg), 14301e493d19SDavid S. Miller void *arg) 14311e493d19SDavid S. Miller { 14321e493d19SDavid S. Miller struct dst_entry *dst, **pprev; 14331e493d19SDavid S. Miller 14341e493d19SDavid S. Miller spin_lock_bh(&icmp6_dst_lock); 14351e493d19SDavid S. Miller pprev = &icmp6_dst_gc_list; 14361e493d19SDavid S. Miller while ((dst = *pprev) != NULL) { 14371e493d19SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 14381e493d19SDavid S. Miller if (func(rt, arg)) { 14391e493d19SDavid S. Miller *pprev = dst->next; 14401e493d19SDavid S. Miller dst_free(dst); 14411e493d19SDavid S. Miller } else { 14421e493d19SDavid S. Miller pprev = &dst->next; 14431e493d19SDavid S. Miller } 14441e493d19SDavid S. Miller } 14451e493d19SDavid S. Miller spin_unlock_bh(&icmp6_dst_lock); 14461e493d19SDavid S. Miller } 14471e493d19SDavid S. Miller 1448569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops) 14491da177e4SLinus Torvalds { 145086393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 14517019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 14527019b78eSDaniel Lezcano int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; 14537019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 14547019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 14557019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 1456fc66f95cSEric Dumazet int entries; 14571da177e4SLinus Torvalds 1458fc66f95cSEric Dumazet entries = dst_entries_get_fast(ops); 145949a18d86SMichal Kubeček if (time_after(rt_last_gc + rt_min_interval, jiffies) && 1460fc66f95cSEric Dumazet entries <= rt_max_size) 14611da177e4SLinus Torvalds goto out; 14621da177e4SLinus Torvalds 14636891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire++; 146414956643SLi RongQing fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net, true); 1465fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 1466fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 14677019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; 14681da177e4SLinus Torvalds out: 14697019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; 1470fc66f95cSEric Dumazet return entries > rt_max_size; 14711da177e4SLinus Torvalds } 14721da177e4SLinus Torvalds 14731da177e4SLinus Torvalds /* 14741da177e4SLinus Torvalds * 14751da177e4SLinus Torvalds */ 14761da177e4SLinus Torvalds 147786872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg) 14781da177e4SLinus Torvalds { 14791da177e4SLinus Torvalds int err; 14805578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 14811da177e4SLinus Torvalds struct rt6_info *rt = NULL; 14821da177e4SLinus Torvalds struct net_device *dev = NULL; 14831da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 1484c71099acSThomas Graf struct fib6_table *table; 14851da177e4SLinus Torvalds int addr_type; 14861da177e4SLinus Torvalds 148786872cb5SThomas Graf if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128) 14881da177e4SLinus Torvalds return -EINVAL; 14891da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 149086872cb5SThomas Graf if (cfg->fc_src_len) 14911da177e4SLinus Torvalds return -EINVAL; 14921da177e4SLinus Torvalds #endif 149386872cb5SThomas Graf if (cfg->fc_ifindex) { 14941da177e4SLinus Torvalds err = -ENODEV; 14955578689aSDaniel Lezcano dev = dev_get_by_index(net, cfg->fc_ifindex); 14961da177e4SLinus Torvalds if (!dev) 14971da177e4SLinus Torvalds goto out; 14981da177e4SLinus Torvalds idev = in6_dev_get(dev); 14991da177e4SLinus Torvalds if (!idev) 15001da177e4SLinus Torvalds goto out; 15011da177e4SLinus Torvalds } 15021da177e4SLinus Torvalds 150386872cb5SThomas Graf if (cfg->fc_metric == 0) 150486872cb5SThomas Graf cfg->fc_metric = IP6_RT_PRIO_USER; 15051da177e4SLinus Torvalds 1506c71099acSThomas Graf err = -ENOBUFS; 150738308473SDavid S. Miller if (cfg->fc_nlinfo.nlh && 1508d71314b4SMatti Vaittinen !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { 1509d71314b4SMatti Vaittinen table = fib6_get_table(net, cfg->fc_table); 151038308473SDavid S. Miller if (!table) { 1511f3213831SJoe Perches pr_warn("NLM_F_CREATE should be specified when creating new route\n"); 1512d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 1513d71314b4SMatti Vaittinen } 1514d71314b4SMatti Vaittinen } else { 1515d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 1516d71314b4SMatti Vaittinen } 151738308473SDavid S. Miller 151838308473SDavid S. Miller if (!table) 1519c71099acSThomas Graf goto out; 1520c71099acSThomas Graf 1521c88507fbSSabrina Dubroca rt = ip6_dst_alloc(net, NULL, (cfg->fc_flags & RTF_ADDRCONF) ? 0 : DST_NOCOUNT, table); 15221da177e4SLinus Torvalds 152338308473SDavid S. Miller if (!rt) { 15241da177e4SLinus Torvalds err = -ENOMEM; 15251da177e4SLinus Torvalds goto out; 15261da177e4SLinus Torvalds } 15271da177e4SLinus Torvalds 15281716a961SGao feng if (cfg->fc_flags & RTF_EXPIRES) 15291716a961SGao feng rt6_set_expires(rt, jiffies + 15301716a961SGao feng clock_t_to_jiffies(cfg->fc_expires)); 15311716a961SGao feng else 15321716a961SGao feng rt6_clean_expires(rt); 15331da177e4SLinus Torvalds 153486872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 153586872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 153686872cb5SThomas Graf rt->rt6i_protocol = cfg->fc_protocol; 153786872cb5SThomas Graf 153886872cb5SThomas Graf addr_type = ipv6_addr_type(&cfg->fc_dst); 15391da177e4SLinus Torvalds 15401da177e4SLinus Torvalds if (addr_type & IPV6_ADDR_MULTICAST) 1541d8d1f30bSChangli Gao rt->dst.input = ip6_mc_input; 1542ab79ad14SMaciej Żenczykowski else if (cfg->fc_flags & RTF_LOCAL) 1543ab79ad14SMaciej Żenczykowski rt->dst.input = ip6_input; 15441da177e4SLinus Torvalds else 1545d8d1f30bSChangli Gao rt->dst.input = ip6_forward; 15461da177e4SLinus Torvalds 1547d8d1f30bSChangli Gao rt->dst.output = ip6_output; 15481da177e4SLinus Torvalds 154986872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 155086872cb5SThomas Graf rt->rt6i_dst.plen = cfg->fc_dst_len; 1551e5fd387aSMichal Kubeček if (rt->rt6i_dst.plen == 128) { 155211d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 1553e5fd387aSMichal Kubeček dst_metrics_set_force_overwrite(&rt->dst); 1554e5fd387aSMichal Kubeček } 15551da177e4SLinus Torvalds 15561da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 155786872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len); 155886872cb5SThomas Graf rt->rt6i_src.plen = cfg->fc_src_len; 15591da177e4SLinus Torvalds #endif 15601da177e4SLinus Torvalds 156186872cb5SThomas Graf rt->rt6i_metric = cfg->fc_metric; 15621da177e4SLinus Torvalds 15631da177e4SLinus Torvalds /* We cannot add true routes via loopback here, 15641da177e4SLinus Torvalds they would result in kernel looping; promote them to reject routes 15651da177e4SLinus Torvalds */ 156686872cb5SThomas Graf if ((cfg->fc_flags & RTF_REJECT) || 156738308473SDavid S. Miller (dev && (dev->flags & IFF_LOOPBACK) && 156838308473SDavid S. Miller !(addr_type & IPV6_ADDR_LOOPBACK) && 156938308473SDavid S. Miller !(cfg->fc_flags & RTF_LOCAL))) { 15701da177e4SLinus Torvalds /* hold loopback dev/idev if we haven't done so. */ 15715578689aSDaniel Lezcano if (dev != net->loopback_dev) { 15721da177e4SLinus Torvalds if (dev) { 15731da177e4SLinus Torvalds dev_put(dev); 15741da177e4SLinus Torvalds in6_dev_put(idev); 15751da177e4SLinus Torvalds } 15765578689aSDaniel Lezcano dev = net->loopback_dev; 15771da177e4SLinus Torvalds dev_hold(dev); 15781da177e4SLinus Torvalds idev = in6_dev_get(dev); 15791da177e4SLinus Torvalds if (!idev) { 15801da177e4SLinus Torvalds err = -ENODEV; 15811da177e4SLinus Torvalds goto out; 15821da177e4SLinus Torvalds } 15831da177e4SLinus Torvalds } 15841da177e4SLinus Torvalds rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP; 1585ef2c7d7bSNicolas Dichtel switch (cfg->fc_type) { 1586ef2c7d7bSNicolas Dichtel case RTN_BLACKHOLE: 1587ef2c7d7bSNicolas Dichtel rt->dst.error = -EINVAL; 1588aad88724SEric Dumazet rt->dst.output = dst_discard_sk; 15897150aedeSKamala R rt->dst.input = dst_discard; 1590ef2c7d7bSNicolas Dichtel break; 1591ef2c7d7bSNicolas Dichtel case RTN_PROHIBIT: 1592ef2c7d7bSNicolas Dichtel rt->dst.error = -EACCES; 15937150aedeSKamala R rt->dst.output = ip6_pkt_prohibit_out; 15947150aedeSKamala R rt->dst.input = ip6_pkt_prohibit; 1595ef2c7d7bSNicolas Dichtel break; 1596b4949ab2SNicolas Dichtel case RTN_THROW: 1597ef2c7d7bSNicolas Dichtel default: 15987150aedeSKamala R rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN 15997150aedeSKamala R : -ENETUNREACH; 16007150aedeSKamala R rt->dst.output = ip6_pkt_discard_out; 16017150aedeSKamala R rt->dst.input = ip6_pkt_discard; 1602ef2c7d7bSNicolas Dichtel break; 1603ef2c7d7bSNicolas Dichtel } 16041da177e4SLinus Torvalds goto install_route; 16051da177e4SLinus Torvalds } 16061da177e4SLinus Torvalds 160786872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY) { 1608b71d1d42SEric Dumazet const struct in6_addr *gw_addr; 16091da177e4SLinus Torvalds int gwa_type; 16101da177e4SLinus Torvalds 161186872cb5SThomas Graf gw_addr = &cfg->fc_gateway; 16124e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = *gw_addr; 16131da177e4SLinus Torvalds gwa_type = ipv6_addr_type(gw_addr); 16141da177e4SLinus Torvalds 16151da177e4SLinus Torvalds if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) { 16161da177e4SLinus Torvalds struct rt6_info *grt; 16171da177e4SLinus Torvalds 16181da177e4SLinus Torvalds /* IPv6 strictly inhibits using not link-local 16191da177e4SLinus Torvalds addresses as nexthop address. 16201da177e4SLinus Torvalds Otherwise, router will not able to send redirects. 16211da177e4SLinus Torvalds It is very good, but in some (rare!) circumstances 16221da177e4SLinus Torvalds (SIT, PtP, NBMA NOARP links) it is handy to allow 16231da177e4SLinus Torvalds some exceptions. --ANK 16241da177e4SLinus Torvalds */ 16251da177e4SLinus Torvalds err = -EINVAL; 16261da177e4SLinus Torvalds if (!(gwa_type & IPV6_ADDR_UNICAST)) 16271da177e4SLinus Torvalds goto out; 16281da177e4SLinus Torvalds 16295578689aSDaniel Lezcano grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1); 16301da177e4SLinus Torvalds 16311da177e4SLinus Torvalds err = -EHOSTUNREACH; 163238308473SDavid S. Miller if (!grt) 16331da177e4SLinus Torvalds goto out; 16341da177e4SLinus Torvalds if (dev) { 1635d1918542SDavid S. Miller if (dev != grt->dst.dev) { 163694e187c0SAmerigo Wang ip6_rt_put(grt); 16371da177e4SLinus Torvalds goto out; 16381da177e4SLinus Torvalds } 16391da177e4SLinus Torvalds } else { 1640d1918542SDavid S. Miller dev = grt->dst.dev; 16411da177e4SLinus Torvalds idev = grt->rt6i_idev; 16421da177e4SLinus Torvalds dev_hold(dev); 16431da177e4SLinus Torvalds in6_dev_hold(grt->rt6i_idev); 16441da177e4SLinus Torvalds } 16451da177e4SLinus Torvalds if (!(grt->rt6i_flags & RTF_GATEWAY)) 16461da177e4SLinus Torvalds err = 0; 164794e187c0SAmerigo Wang ip6_rt_put(grt); 16481da177e4SLinus Torvalds 16491da177e4SLinus Torvalds if (err) 16501da177e4SLinus Torvalds goto out; 16511da177e4SLinus Torvalds } 16521da177e4SLinus Torvalds err = -EINVAL; 165338308473SDavid S. Miller if (!dev || (dev->flags & IFF_LOOPBACK)) 16541da177e4SLinus Torvalds goto out; 16551da177e4SLinus Torvalds } 16561da177e4SLinus Torvalds 16571da177e4SLinus Torvalds err = -ENODEV; 165838308473SDavid S. Miller if (!dev) 16591da177e4SLinus Torvalds goto out; 16601da177e4SLinus Torvalds 1661c3968a85SDaniel Walter if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 1662c3968a85SDaniel Walter if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 1663c3968a85SDaniel Walter err = -EINVAL; 1664c3968a85SDaniel Walter goto out; 1665c3968a85SDaniel Walter } 16664e3fd7a0SAlexey Dobriyan rt->rt6i_prefsrc.addr = cfg->fc_prefsrc; 1667c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 128; 1668c3968a85SDaniel Walter } else 1669c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 1670c3968a85SDaniel Walter 167186872cb5SThomas Graf rt->rt6i_flags = cfg->fc_flags; 16721da177e4SLinus Torvalds 16731da177e4SLinus Torvalds install_route: 1674d8d1f30bSChangli Gao rt->dst.dev = dev; 16751da177e4SLinus Torvalds rt->rt6i_idev = idev; 1676c71099acSThomas Graf rt->rt6i_table = table; 167763152fc0SDaniel Lezcano 1678c346dca1SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = dev_net(dev); 167963152fc0SDaniel Lezcano 1680e5fd387aSMichal Kubeček return __ip6_ins_rt(rt, &cfg->fc_nlinfo, cfg->fc_mx, cfg->fc_mx_len); 16811da177e4SLinus Torvalds 16821da177e4SLinus Torvalds out: 16831da177e4SLinus Torvalds if (dev) 16841da177e4SLinus Torvalds dev_put(dev); 16851da177e4SLinus Torvalds if (idev) 16861da177e4SLinus Torvalds in6_dev_put(idev); 16871da177e4SLinus Torvalds if (rt) 1688d8d1f30bSChangli Gao dst_free(&rt->dst); 16891da177e4SLinus Torvalds return err; 16901da177e4SLinus Torvalds } 16911da177e4SLinus Torvalds 169286872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info) 16931da177e4SLinus Torvalds { 16941da177e4SLinus Torvalds int err; 1695c71099acSThomas Graf struct fib6_table *table; 1696d1918542SDavid S. Miller struct net *net = dev_net(rt->dst.dev); 16971da177e4SLinus Torvalds 16986825a26cSGao feng if (rt == net->ipv6.ip6_null_entry) { 16996825a26cSGao feng err = -ENOENT; 17006825a26cSGao feng goto out; 17016825a26cSGao feng } 17026c813a72SPatrick McHardy 1703c71099acSThomas Graf table = rt->rt6i_table; 1704c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 170586872cb5SThomas Graf err = fib6_del(rt, info); 1706c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 17071da177e4SLinus Torvalds 17086825a26cSGao feng out: 170994e187c0SAmerigo Wang ip6_rt_put(rt); 17101da177e4SLinus Torvalds return err; 17111da177e4SLinus Torvalds } 17121da177e4SLinus Torvalds 1713e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt) 1714e0a1ad73SThomas Graf { 17154d1169c1SDenis V. Lunev struct nl_info info = { 1716d1918542SDavid S. Miller .nl_net = dev_net(rt->dst.dev), 17174d1169c1SDenis V. Lunev }; 1718528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 1719e0a1ad73SThomas Graf } 1720e0a1ad73SThomas Graf 172186872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg) 17221da177e4SLinus Torvalds { 1723c71099acSThomas Graf struct fib6_table *table; 17241da177e4SLinus Torvalds struct fib6_node *fn; 17251da177e4SLinus Torvalds struct rt6_info *rt; 17261da177e4SLinus Torvalds int err = -ESRCH; 17271da177e4SLinus Torvalds 17285578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 172938308473SDavid S. Miller if (!table) 1730c71099acSThomas Graf return err; 17311da177e4SLinus Torvalds 1732c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 1733c71099acSThomas Graf 1734c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 173586872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 173686872cb5SThomas Graf &cfg->fc_src, cfg->fc_src_len); 17371da177e4SLinus Torvalds 17381da177e4SLinus Torvalds if (fn) { 1739d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 174086872cb5SThomas Graf if (cfg->fc_ifindex && 1741d1918542SDavid S. Miller (!rt->dst.dev || 1742d1918542SDavid S. Miller rt->dst.dev->ifindex != cfg->fc_ifindex)) 17431da177e4SLinus Torvalds continue; 174486872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 174586872cb5SThomas Graf !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 17461da177e4SLinus Torvalds continue; 174786872cb5SThomas Graf if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric) 17481da177e4SLinus Torvalds continue; 1749d8d1f30bSChangli Gao dst_hold(&rt->dst); 1750c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 17511da177e4SLinus Torvalds 175286872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 17531da177e4SLinus Torvalds } 17541da177e4SLinus Torvalds } 1755c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 17561da177e4SLinus Torvalds 17571da177e4SLinus Torvalds return err; 17581da177e4SLinus Torvalds } 17591da177e4SLinus Torvalds 17606700c270SDavid S. Miller static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) 1761a6279458SYOSHIFUJI Hideaki { 1762e8599ff4SDavid S. Miller struct net *net = dev_net(skb->dev); 1763a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 1764e8599ff4SDavid S. Miller struct rt6_info *rt, *nrt = NULL; 1765e8599ff4SDavid S. Miller struct ndisc_options ndopts; 1766e8599ff4SDavid S. Miller struct inet6_dev *in6_dev; 1767e8599ff4SDavid S. Miller struct neighbour *neigh; 176871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 struct rd_msg *msg; 17696e157b6aSDavid S. Miller int optlen, on_link; 17706e157b6aSDavid S. Miller u8 *lladdr; 1771e8599ff4SDavid S. Miller 177229a3cad5SSimon Horman optlen = skb_tail_pointer(skb) - skb_transport_header(skb); 177371bcdba0SYOSHIFUJI Hideaki / 吉藤英明 optlen -= sizeof(*msg); 1774e8599ff4SDavid S. Miller 1775e8599ff4SDavid S. Miller if (optlen < 0) { 17766e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); 1777e8599ff4SDavid S. Miller return; 1778e8599ff4SDavid S. Miller } 1779e8599ff4SDavid S. Miller 178071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 msg = (struct rd_msg *)icmp6_hdr(skb); 1781e8599ff4SDavid S. Miller 178271bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_is_multicast(&msg->dest)) { 17836e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); 1784e8599ff4SDavid S. Miller return; 1785e8599ff4SDavid S. Miller } 1786e8599ff4SDavid S. Miller 17876e157b6aSDavid S. Miller on_link = 0; 178871bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (ipv6_addr_equal(&msg->dest, &msg->target)) { 1789e8599ff4SDavid S. Miller on_link = 1; 179071bcdba0SYOSHIFUJI Hideaki / 吉藤英明 } else if (ipv6_addr_type(&msg->target) != 1791e8599ff4SDavid S. Miller (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { 17926e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); 1793e8599ff4SDavid S. Miller return; 1794e8599ff4SDavid S. Miller } 1795e8599ff4SDavid S. Miller 1796e8599ff4SDavid S. Miller in6_dev = __in6_dev_get(skb->dev); 1797e8599ff4SDavid S. Miller if (!in6_dev) 1798e8599ff4SDavid S. Miller return; 1799e8599ff4SDavid S. Miller if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) 1800e8599ff4SDavid S. Miller return; 1801e8599ff4SDavid S. Miller 1802e8599ff4SDavid S. Miller /* RFC2461 8.1: 1803e8599ff4SDavid S. Miller * The IP source address of the Redirect MUST be the same as the current 1804e8599ff4SDavid S. Miller * first-hop router for the specified ICMP Destination Address. 1805e8599ff4SDavid S. Miller */ 1806e8599ff4SDavid S. Miller 180771bcdba0SYOSHIFUJI Hideaki / 吉藤英明 if (!ndisc_parse_options(msg->opt, optlen, &ndopts)) { 1808e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); 1809e8599ff4SDavid S. Miller return; 1810e8599ff4SDavid S. Miller } 18116e157b6aSDavid S. Miller 18126e157b6aSDavid S. Miller lladdr = NULL; 1813e8599ff4SDavid S. Miller if (ndopts.nd_opts_tgt_lladdr) { 1814e8599ff4SDavid S. Miller lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, 1815e8599ff4SDavid S. Miller skb->dev); 1816e8599ff4SDavid S. Miller if (!lladdr) { 1817e8599ff4SDavid S. Miller net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); 1818e8599ff4SDavid S. Miller return; 1819e8599ff4SDavid S. Miller } 1820e8599ff4SDavid S. Miller } 1821e8599ff4SDavid S. Miller 18226e157b6aSDavid S. Miller rt = (struct rt6_info *) dst; 18236e157b6aSDavid S. Miller if (rt == net->ipv6.ip6_null_entry) { 18246e157b6aSDavid S. Miller net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); 18256e157b6aSDavid S. Miller return; 18266e157b6aSDavid S. Miller } 18276e157b6aSDavid S. Miller 18286e157b6aSDavid S. Miller /* Redirect received -> path was valid. 18296e157b6aSDavid S. Miller * Look, redirects are sent only in response to data packets, 18306e157b6aSDavid S. Miller * so that this nexthop apparently is reachable. --ANK 18316e157b6aSDavid S. Miller */ 18326e157b6aSDavid S. Miller dst_confirm(&rt->dst); 18336e157b6aSDavid S. Miller 183471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); 1835e8599ff4SDavid S. Miller if (!neigh) 1836e8599ff4SDavid S. Miller return; 1837e8599ff4SDavid S. Miller 18381da177e4SLinus Torvalds /* 18391da177e4SLinus Torvalds * We have finally decided to accept it. 18401da177e4SLinus Torvalds */ 18411da177e4SLinus Torvalds 18421da177e4SLinus Torvalds neigh_update(neigh, lladdr, NUD_STALE, 18431da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 18441da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 18451da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 18461da177e4SLinus Torvalds NEIGH_UPDATE_F_ISROUTER)) 18471da177e4SLinus Torvalds ); 18481da177e4SLinus Torvalds 184971bcdba0SYOSHIFUJI Hideaki / 吉藤英明 nrt = ip6_rt_copy(rt, &msg->dest); 185038308473SDavid S. Miller if (!nrt) 18511da177e4SLinus Torvalds goto out; 18521da177e4SLinus Torvalds 18531da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 18541da177e4SLinus Torvalds if (on_link) 18551da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 18561da177e4SLinus Torvalds 18574e3fd7a0SAlexey Dobriyan nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 18581da177e4SLinus Torvalds 185940e22e8fSThomas Graf if (ip6_ins_rt(nrt)) 18601da177e4SLinus Torvalds goto out; 18611da177e4SLinus Torvalds 1862d8d1f30bSChangli Gao netevent.old = &rt->dst; 1863d8d1f30bSChangli Gao netevent.new = &nrt->dst; 186471bcdba0SYOSHIFUJI Hideaki / 吉藤英明 netevent.daddr = &msg->dest; 186560592833SYOSHIFUJI Hideaki / 吉藤英明 netevent.neigh = neigh; 18668d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 18678d71740cSTom Tucker 18681da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) { 18696e157b6aSDavid S. Miller rt = (struct rt6_info *) dst_clone(&rt->dst); 1870e0a1ad73SThomas Graf ip6_del_rt(rt); 18711da177e4SLinus Torvalds } 18721da177e4SLinus Torvalds 18731da177e4SLinus Torvalds out: 1874e8599ff4SDavid S. Miller neigh_release(neigh); 18756e157b6aSDavid S. Miller } 18766e157b6aSDavid S. Miller 18771da177e4SLinus Torvalds /* 18781da177e4SLinus Torvalds * Misc support functions 18791da177e4SLinus Torvalds */ 18801da177e4SLinus Torvalds 18811716a961SGao feng static struct rt6_info *ip6_rt_copy(struct rt6_info *ort, 188221efcfa0SEric Dumazet const struct in6_addr *dest) 18831da177e4SLinus Torvalds { 1884d1918542SDavid S. Miller struct net *net = dev_net(ort->dst.dev); 18858b96d22dSDavid S. Miller struct rt6_info *rt = ip6_dst_alloc(net, ort->dst.dev, 0, 18868b96d22dSDavid S. Miller ort->rt6i_table); 18871da177e4SLinus Torvalds 18881da177e4SLinus Torvalds if (rt) { 1889d8d1f30bSChangli Gao rt->dst.input = ort->dst.input; 1890d8d1f30bSChangli Gao rt->dst.output = ort->dst.output; 18918e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 18921da177e4SLinus Torvalds 18934e3fd7a0SAlexey Dobriyan rt->rt6i_dst.addr = *dest; 18948e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 1895defb3519SDavid S. Miller dst_copy_metrics(&rt->dst, &ort->dst); 1896d8d1f30bSChangli Gao rt->dst.error = ort->dst.error; 18971da177e4SLinus Torvalds rt->rt6i_idev = ort->rt6i_idev; 18981da177e4SLinus Torvalds if (rt->rt6i_idev) 18991da177e4SLinus Torvalds in6_dev_hold(rt->rt6i_idev); 1900d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 19011da177e4SLinus Torvalds 1902550bab42SJulian Anastasov if (ort->rt6i_flags & RTF_GATEWAY) 19034e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 1904550bab42SJulian Anastasov else 1905550bab42SJulian Anastasov rt->rt6i_gateway = *dest; 19061716a961SGao feng rt->rt6i_flags = ort->rt6i_flags; 19071716a961SGao feng rt6_set_from(rt, ort); 19081da177e4SLinus Torvalds rt->rt6i_metric = 0; 19091da177e4SLinus Torvalds 19101da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 19111da177e4SLinus Torvalds memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 19121da177e4SLinus Torvalds #endif 19130f6c6392SFlorian Westphal memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key)); 1914c71099acSThomas Graf rt->rt6i_table = ort->rt6i_table; 19151da177e4SLinus Torvalds } 19161da177e4SLinus Torvalds return rt; 19171da177e4SLinus Torvalds } 19181da177e4SLinus Torvalds 191970ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 1920efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 1921b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 1922b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex) 192370ceb4f5SYOSHIFUJI Hideaki { 192470ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 192570ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt = NULL; 1926c71099acSThomas Graf struct fib6_table *table; 192770ceb4f5SYOSHIFUJI Hideaki 1928efa2cea0SDaniel Lezcano table = fib6_get_table(net, RT6_TABLE_INFO); 192938308473SDavid S. Miller if (!table) 1930c71099acSThomas Graf return NULL; 1931c71099acSThomas Graf 19325744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 1933c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, prefix, prefixlen, NULL, 0); 193470ceb4f5SYOSHIFUJI Hideaki if (!fn) 193570ceb4f5SYOSHIFUJI Hideaki goto out; 193670ceb4f5SYOSHIFUJI Hideaki 1937d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 1938d1918542SDavid S. Miller if (rt->dst.dev->ifindex != ifindex) 193970ceb4f5SYOSHIFUJI Hideaki continue; 194070ceb4f5SYOSHIFUJI Hideaki if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) 194170ceb4f5SYOSHIFUJI Hideaki continue; 194270ceb4f5SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr)) 194370ceb4f5SYOSHIFUJI Hideaki continue; 1944d8d1f30bSChangli Gao dst_hold(&rt->dst); 194570ceb4f5SYOSHIFUJI Hideaki break; 194670ceb4f5SYOSHIFUJI Hideaki } 194770ceb4f5SYOSHIFUJI Hideaki out: 19485744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 194970ceb4f5SYOSHIFUJI Hideaki return rt; 195070ceb4f5SYOSHIFUJI Hideaki } 195170ceb4f5SYOSHIFUJI Hideaki 1952efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 1953b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 1954b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex, 195595c96174SEric Dumazet unsigned int pref) 195670ceb4f5SYOSHIFUJI Hideaki { 195786872cb5SThomas Graf struct fib6_config cfg = { 195886872cb5SThomas Graf .fc_table = RT6_TABLE_INFO, 1959238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 196086872cb5SThomas Graf .fc_ifindex = ifindex, 196186872cb5SThomas Graf .fc_dst_len = prefixlen, 196286872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 196386872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 196415e47304SEric W. Biederman .fc_nlinfo.portid = 0, 1965efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 1966efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 196786872cb5SThomas Graf }; 196870ceb4f5SYOSHIFUJI Hideaki 19694e3fd7a0SAlexey Dobriyan cfg.fc_dst = *prefix; 19704e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 197186872cb5SThomas Graf 1972e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 1973e317da96SYOSHIFUJI Hideaki if (!prefixlen) 197486872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 197570ceb4f5SYOSHIFUJI Hideaki 197686872cb5SThomas Graf ip6_route_add(&cfg); 197770ceb4f5SYOSHIFUJI Hideaki 1978efa2cea0SDaniel Lezcano return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex); 197970ceb4f5SYOSHIFUJI Hideaki } 198070ceb4f5SYOSHIFUJI Hideaki #endif 198170ceb4f5SYOSHIFUJI Hideaki 1982b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev) 19831da177e4SLinus Torvalds { 19841da177e4SLinus Torvalds struct rt6_info *rt; 1985c71099acSThomas Graf struct fib6_table *table; 19861da177e4SLinus Torvalds 1987c346dca1SYOSHIFUJI Hideaki table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT); 198838308473SDavid S. Miller if (!table) 1989c71099acSThomas Graf return NULL; 19901da177e4SLinus Torvalds 19915744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 1992d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 1993d1918542SDavid S. Miller if (dev == rt->dst.dev && 1994045927ffSYOSHIFUJI Hideaki ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 19951da177e4SLinus Torvalds ipv6_addr_equal(&rt->rt6i_gateway, addr)) 19961da177e4SLinus Torvalds break; 19971da177e4SLinus Torvalds } 19981da177e4SLinus Torvalds if (rt) 1999d8d1f30bSChangli Gao dst_hold(&rt->dst); 20005744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 20011da177e4SLinus Torvalds return rt; 20021da177e4SLinus Torvalds } 20031da177e4SLinus Torvalds 2004b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr, 2005ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 2006ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 20071da177e4SLinus Torvalds { 200886872cb5SThomas Graf struct fib6_config cfg = { 200986872cb5SThomas Graf .fc_table = RT6_TABLE_DFLT, 2010238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 201186872cb5SThomas Graf .fc_ifindex = dev->ifindex, 201286872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 201386872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 201415e47304SEric W. Biederman .fc_nlinfo.portid = 0, 20155578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 2016c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 201786872cb5SThomas Graf }; 20181da177e4SLinus Torvalds 20194e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 20201da177e4SLinus Torvalds 202186872cb5SThomas Graf ip6_route_add(&cfg); 20221da177e4SLinus Torvalds 20231da177e4SLinus Torvalds return rt6_get_dflt_router(gwaddr, dev); 20241da177e4SLinus Torvalds } 20251da177e4SLinus Torvalds 20267b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net) 20271da177e4SLinus Torvalds { 20281da177e4SLinus Torvalds struct rt6_info *rt; 2029c71099acSThomas Graf struct fib6_table *table; 2030c71099acSThomas Graf 2031c71099acSThomas Graf /* NOTE: Keep consistent with rt6_get_dflt_router */ 20327b4da532SDaniel Lezcano table = fib6_get_table(net, RT6_TABLE_DFLT); 203338308473SDavid S. Miller if (!table) 2034c71099acSThomas Graf return; 20351da177e4SLinus Torvalds 20361da177e4SLinus Torvalds restart: 2037c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 2038d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 20393e8b0ac3SLorenzo Colitti if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) && 20403e8b0ac3SLorenzo Colitti (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) { 2041d8d1f30bSChangli Gao dst_hold(&rt->dst); 2042c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 2043e0a1ad73SThomas Graf ip6_del_rt(rt); 20441da177e4SLinus Torvalds goto restart; 20451da177e4SLinus Torvalds } 20461da177e4SLinus Torvalds } 2047c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 20481da177e4SLinus Torvalds } 20491da177e4SLinus Torvalds 20505578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 20515578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 205286872cb5SThomas Graf struct fib6_config *cfg) 205386872cb5SThomas Graf { 205486872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 205586872cb5SThomas Graf 205686872cb5SThomas Graf cfg->fc_table = RT6_TABLE_MAIN; 205786872cb5SThomas Graf cfg->fc_ifindex = rtmsg->rtmsg_ifindex; 205886872cb5SThomas Graf cfg->fc_metric = rtmsg->rtmsg_metric; 205986872cb5SThomas Graf cfg->fc_expires = rtmsg->rtmsg_info; 206086872cb5SThomas Graf cfg->fc_dst_len = rtmsg->rtmsg_dst_len; 206186872cb5SThomas Graf cfg->fc_src_len = rtmsg->rtmsg_src_len; 206286872cb5SThomas Graf cfg->fc_flags = rtmsg->rtmsg_flags; 206386872cb5SThomas Graf 20645578689aSDaniel Lezcano cfg->fc_nlinfo.nl_net = net; 2065f1243c2dSBenjamin Thery 20664e3fd7a0SAlexey Dobriyan cfg->fc_dst = rtmsg->rtmsg_dst; 20674e3fd7a0SAlexey Dobriyan cfg->fc_src = rtmsg->rtmsg_src; 20684e3fd7a0SAlexey Dobriyan cfg->fc_gateway = rtmsg->rtmsg_gateway; 206986872cb5SThomas Graf } 207086872cb5SThomas Graf 20715578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) 20721da177e4SLinus Torvalds { 207386872cb5SThomas Graf struct fib6_config cfg; 20741da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 20751da177e4SLinus Torvalds int err; 20761da177e4SLinus Torvalds 20771da177e4SLinus Torvalds switch (cmd) { 20781da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 20791da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 2080af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 20811da177e4SLinus Torvalds return -EPERM; 20821da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 20831da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 20841da177e4SLinus Torvalds if (err) 20851da177e4SLinus Torvalds return -EFAULT; 20861da177e4SLinus Torvalds 20875578689aSDaniel Lezcano rtmsg_to_fib6_config(net, &rtmsg, &cfg); 208886872cb5SThomas Graf 20891da177e4SLinus Torvalds rtnl_lock(); 20901da177e4SLinus Torvalds switch (cmd) { 20911da177e4SLinus Torvalds case SIOCADDRT: 209286872cb5SThomas Graf err = ip6_route_add(&cfg); 20931da177e4SLinus Torvalds break; 20941da177e4SLinus Torvalds case SIOCDELRT: 209586872cb5SThomas Graf err = ip6_route_del(&cfg); 20961da177e4SLinus Torvalds break; 20971da177e4SLinus Torvalds default: 20981da177e4SLinus Torvalds err = -EINVAL; 20991da177e4SLinus Torvalds } 21001da177e4SLinus Torvalds rtnl_unlock(); 21011da177e4SLinus Torvalds 21021da177e4SLinus Torvalds return err; 21033ff50b79SStephen Hemminger } 21041da177e4SLinus Torvalds 21051da177e4SLinus Torvalds return -EINVAL; 21061da177e4SLinus Torvalds } 21071da177e4SLinus Torvalds 21081da177e4SLinus Torvalds /* 21091da177e4SLinus Torvalds * Drop the packet on the floor 21101da177e4SLinus Torvalds */ 21111da177e4SLinus Torvalds 2112d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 21131da177e4SLinus Torvalds { 2114612f09e8SYOSHIFUJI Hideaki int type; 2115adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 2116612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 2117612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 21180660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 211945bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 21203bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 21213bd653c8SDenis V. Lunev IPSTATS_MIB_INADDRERRORS); 2122612f09e8SYOSHIFUJI Hideaki break; 2123612f09e8SYOSHIFUJI Hideaki } 2124612f09e8SYOSHIFUJI Hideaki /* FALLTHROUGH */ 2125612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 21263bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 21273bd653c8SDenis V. Lunev ipstats_mib_noroutes); 2128612f09e8SYOSHIFUJI Hideaki break; 2129612f09e8SYOSHIFUJI Hideaki } 21303ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 21311da177e4SLinus Torvalds kfree_skb(skb); 21321da177e4SLinus Torvalds return 0; 21331da177e4SLinus Torvalds } 21341da177e4SLinus Torvalds 21359ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 21369ce8ade0SThomas Graf { 2137612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 21389ce8ade0SThomas Graf } 21399ce8ade0SThomas Graf 2140aad88724SEric Dumazet static int ip6_pkt_discard_out(struct sock *sk, struct sk_buff *skb) 21411da177e4SLinus Torvalds { 2142adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 2143612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 21441da177e4SLinus Torvalds } 21451da177e4SLinus Torvalds 21469ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 21479ce8ade0SThomas Graf { 2148612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 21499ce8ade0SThomas Graf } 21509ce8ade0SThomas Graf 2151aad88724SEric Dumazet static int ip6_pkt_prohibit_out(struct sock *sk, struct sk_buff *skb) 21529ce8ade0SThomas Graf { 2153adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 2154612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 21559ce8ade0SThomas Graf } 21569ce8ade0SThomas Graf 21571da177e4SLinus Torvalds /* 21581da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 21591da177e4SLinus Torvalds */ 21601da177e4SLinus Torvalds 21611da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, 21621da177e4SLinus Torvalds const struct in6_addr *addr, 21638f031519SDavid S. Miller bool anycast) 21641da177e4SLinus Torvalds { 2165c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 2166a3300ef4SHannes Frederic Sowa struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev, 2167a3300ef4SHannes Frederic Sowa DST_NOCOUNT, NULL); 2168a3300ef4SHannes Frederic Sowa if (!rt) 21691da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 21701da177e4SLinus Torvalds 21711da177e4SLinus Torvalds in6_dev_hold(idev); 21721da177e4SLinus Torvalds 217311d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 2174d8d1f30bSChangli Gao rt->dst.input = ip6_input; 2175d8d1f30bSChangli Gao rt->dst.output = ip6_output; 21761da177e4SLinus Torvalds rt->rt6i_idev = idev; 21771da177e4SLinus Torvalds 21781da177e4SLinus Torvalds rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP; 217958c4fb86SYOSHIFUJI Hideaki if (anycast) 218058c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 218158c4fb86SYOSHIFUJI Hideaki else 21821da177e4SLinus Torvalds rt->rt6i_flags |= RTF_LOCAL; 21831da177e4SLinus Torvalds 2184550bab42SJulian Anastasov rt->rt6i_gateway = *addr; 21854e3fd7a0SAlexey Dobriyan rt->rt6i_dst.addr = *addr; 21861da177e4SLinus Torvalds rt->rt6i_dst.plen = 128; 21875578689aSDaniel Lezcano rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL); 21881da177e4SLinus Torvalds 2189d8d1f30bSChangli Gao atomic_set(&rt->dst.__refcnt, 1); 21901da177e4SLinus Torvalds 21911da177e4SLinus Torvalds return rt; 21921da177e4SLinus Torvalds } 21931da177e4SLinus Torvalds 2194c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net, 2195c3968a85SDaniel Walter struct rt6_info *rt, 2196b71d1d42SEric Dumazet const struct in6_addr *daddr, 2197c3968a85SDaniel Walter unsigned int prefs, 2198c3968a85SDaniel Walter struct in6_addr *saddr) 2199c3968a85SDaniel Walter { 2200c3968a85SDaniel Walter struct inet6_dev *idev = ip6_dst_idev((struct dst_entry *)rt); 2201c3968a85SDaniel Walter int err = 0; 2202c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) 22034e3fd7a0SAlexey Dobriyan *saddr = rt->rt6i_prefsrc.addr; 2204c3968a85SDaniel Walter else 2205c3968a85SDaniel Walter err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL, 2206c3968a85SDaniel Walter daddr, prefs, saddr); 2207c3968a85SDaniel Walter return err; 2208c3968a85SDaniel Walter } 2209c3968a85SDaniel Walter 2210c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 2211c3968a85SDaniel Walter struct arg_dev_net_ip { 2212c3968a85SDaniel Walter struct net_device *dev; 2213c3968a85SDaniel Walter struct net *net; 2214c3968a85SDaniel Walter struct in6_addr *addr; 2215c3968a85SDaniel Walter }; 2216c3968a85SDaniel Walter 2217c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg) 2218c3968a85SDaniel Walter { 2219c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 2220c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 2221c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 2222c3968a85SDaniel Walter 2223d1918542SDavid S. Miller if (((void *)rt->dst.dev == dev || !dev) && 2224c3968a85SDaniel Walter rt != net->ipv6.ip6_null_entry && 2225c3968a85SDaniel Walter ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) { 2226c3968a85SDaniel Walter /* remove prefsrc entry */ 2227c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 2228c3968a85SDaniel Walter } 2229c3968a85SDaniel Walter return 0; 2230c3968a85SDaniel Walter } 2231c3968a85SDaniel Walter 2232c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 2233c3968a85SDaniel Walter { 2234c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 2235c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 2236c3968a85SDaniel Walter .dev = ifp->idev->dev, 2237c3968a85SDaniel Walter .net = net, 2238c3968a85SDaniel Walter .addr = &ifp->addr, 2239c3968a85SDaniel Walter }; 22400c3584d5SLi RongQing fib6_clean_all(net, fib6_remove_prefsrc, &adni); 2241c3968a85SDaniel Walter } 2242c3968a85SDaniel Walter 2243be7a010dSDuan Jiong #define RTF_RA_ROUTER (RTF_ADDRCONF | RTF_DEFAULT | RTF_GATEWAY) 2244be7a010dSDuan Jiong #define RTF_CACHE_GATEWAY (RTF_GATEWAY | RTF_CACHE) 2245be7a010dSDuan Jiong 2246be7a010dSDuan Jiong /* Remove routers and update dst entries when gateway turn into host. */ 2247be7a010dSDuan Jiong static int fib6_clean_tohost(struct rt6_info *rt, void *arg) 2248be7a010dSDuan Jiong { 2249be7a010dSDuan Jiong struct in6_addr *gateway = (struct in6_addr *)arg; 2250be7a010dSDuan Jiong 2251be7a010dSDuan Jiong if ((((rt->rt6i_flags & RTF_RA_ROUTER) == RTF_RA_ROUTER) || 2252be7a010dSDuan Jiong ((rt->rt6i_flags & RTF_CACHE_GATEWAY) == RTF_CACHE_GATEWAY)) && 2253be7a010dSDuan Jiong ipv6_addr_equal(gateway, &rt->rt6i_gateway)) { 2254be7a010dSDuan Jiong return -1; 2255be7a010dSDuan Jiong } 2256be7a010dSDuan Jiong return 0; 2257be7a010dSDuan Jiong } 2258be7a010dSDuan Jiong 2259be7a010dSDuan Jiong void rt6_clean_tohost(struct net *net, struct in6_addr *gateway) 2260be7a010dSDuan Jiong { 2261be7a010dSDuan Jiong fib6_clean_all(net, fib6_clean_tohost, gateway); 2262be7a010dSDuan Jiong } 2263be7a010dSDuan Jiong 22648ed67789SDaniel Lezcano struct arg_dev_net { 22658ed67789SDaniel Lezcano struct net_device *dev; 22668ed67789SDaniel Lezcano struct net *net; 22678ed67789SDaniel Lezcano }; 22688ed67789SDaniel Lezcano 22691da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg) 22701da177e4SLinus Torvalds { 2271bc3ef660Sstephen hemminger const struct arg_dev_net *adn = arg; 2272bc3ef660Sstephen hemminger const struct net_device *dev = adn->dev; 22738ed67789SDaniel Lezcano 2274d1918542SDavid S. Miller if ((rt->dst.dev == dev || !dev) && 2275c159d30cSDavid S. Miller rt != adn->net->ipv6.ip6_null_entry) 22761da177e4SLinus Torvalds return -1; 2277c159d30cSDavid S. Miller 22781da177e4SLinus Torvalds return 0; 22791da177e4SLinus Torvalds } 22801da177e4SLinus Torvalds 2281f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev) 22821da177e4SLinus Torvalds { 22838ed67789SDaniel Lezcano struct arg_dev_net adn = { 22848ed67789SDaniel Lezcano .dev = dev, 22858ed67789SDaniel Lezcano .net = net, 22868ed67789SDaniel Lezcano }; 22878ed67789SDaniel Lezcano 22880c3584d5SLi RongQing fib6_clean_all(net, fib6_ifdown, &adn); 22891e493d19SDavid S. Miller icmp6_clean_all(fib6_ifdown, &adn); 22901da177e4SLinus Torvalds } 22911da177e4SLinus Torvalds 229295c96174SEric Dumazet struct rt6_mtu_change_arg { 22931da177e4SLinus Torvalds struct net_device *dev; 229495c96174SEric Dumazet unsigned int mtu; 22951da177e4SLinus Torvalds }; 22961da177e4SLinus Torvalds 22971da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg) 22981da177e4SLinus Torvalds { 22991da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 23001da177e4SLinus Torvalds struct inet6_dev *idev; 23011da177e4SLinus Torvalds 23021da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 23031da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 23041da177e4SLinus Torvalds We still use this lock to block changes 23051da177e4SLinus Torvalds caused by addrconf/ndisc. 23061da177e4SLinus Torvalds */ 23071da177e4SLinus Torvalds 23081da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 230938308473SDavid S. Miller if (!idev) 23101da177e4SLinus Torvalds return 0; 23111da177e4SLinus Torvalds 23121da177e4SLinus Torvalds /* For administrative MTU increase, there is no way to discover 23131da177e4SLinus Torvalds IPv6 PMTU increase, so PMTU increase should be updated here. 23141da177e4SLinus Torvalds Since RFC 1981 doesn't include administrative MTU increase 23151da177e4SLinus Torvalds update PMTU increase is a MUST. (i.e. jumbo frame) 23161da177e4SLinus Torvalds */ 23171da177e4SLinus Torvalds /* 23181da177e4SLinus Torvalds If new MTU is less than route PMTU, this new MTU will be the 23191da177e4SLinus Torvalds lowest MTU in the path, update the route PMTU to reflect PMTU 23201da177e4SLinus Torvalds decreases; if new MTU is greater than route PMTU, and the 23211da177e4SLinus Torvalds old MTU is the lowest MTU in the path, update the route PMTU 23221da177e4SLinus Torvalds to reflect the increase. In this case if the other nodes' MTU 23231da177e4SLinus Torvalds also have the lowest MTU, TOO BIG MESSAGE will be lead to 23241da177e4SLinus Torvalds PMTU discouvery. 23251da177e4SLinus Torvalds */ 2326d1918542SDavid S. Miller if (rt->dst.dev == arg->dev && 2327d8d1f30bSChangli Gao !dst_metric_locked(&rt->dst, RTAX_MTU) && 2328d8d1f30bSChangli Gao (dst_mtu(&rt->dst) >= arg->mtu || 2329d8d1f30bSChangli Gao (dst_mtu(&rt->dst) < arg->mtu && 2330d8d1f30bSChangli Gao dst_mtu(&rt->dst) == idev->cnf.mtu6))) { 2331defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu); 2332566cfd8fSSimon Arlott } 23331da177e4SLinus Torvalds return 0; 23341da177e4SLinus Torvalds } 23351da177e4SLinus Torvalds 233695c96174SEric Dumazet void rt6_mtu_change(struct net_device *dev, unsigned int mtu) 23371da177e4SLinus Torvalds { 2338c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 2339c71099acSThomas Graf .dev = dev, 2340c71099acSThomas Graf .mtu = mtu, 2341c71099acSThomas Graf }; 23421da177e4SLinus Torvalds 23430c3584d5SLi RongQing fib6_clean_all(dev_net(dev), rt6_mtu_change_route, &arg); 23441da177e4SLinus Torvalds } 23451da177e4SLinus Torvalds 2346ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 23475176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 234886872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 2349ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 235086872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 235186872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 235251ebd318SNicolas Dichtel [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 235386872cb5SThomas Graf }; 235486872cb5SThomas Graf 235586872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 235686872cb5SThomas Graf struct fib6_config *cfg) 23571da177e4SLinus Torvalds { 235886872cb5SThomas Graf struct rtmsg *rtm; 235986872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 236086872cb5SThomas Graf int err; 23611da177e4SLinus Torvalds 236286872cb5SThomas Graf err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy); 236386872cb5SThomas Graf if (err < 0) 236486872cb5SThomas Graf goto errout; 23651da177e4SLinus Torvalds 236686872cb5SThomas Graf err = -EINVAL; 236786872cb5SThomas Graf rtm = nlmsg_data(nlh); 236886872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 236986872cb5SThomas Graf 237086872cb5SThomas Graf cfg->fc_table = rtm->rtm_table; 237186872cb5SThomas Graf cfg->fc_dst_len = rtm->rtm_dst_len; 237286872cb5SThomas Graf cfg->fc_src_len = rtm->rtm_src_len; 237386872cb5SThomas Graf cfg->fc_flags = RTF_UP; 237486872cb5SThomas Graf cfg->fc_protocol = rtm->rtm_protocol; 2375ef2c7d7bSNicolas Dichtel cfg->fc_type = rtm->rtm_type; 237686872cb5SThomas Graf 2377ef2c7d7bSNicolas Dichtel if (rtm->rtm_type == RTN_UNREACHABLE || 2378ef2c7d7bSNicolas Dichtel rtm->rtm_type == RTN_BLACKHOLE || 2379b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_PROHIBIT || 2380b4949ab2SNicolas Dichtel rtm->rtm_type == RTN_THROW) 238186872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 238286872cb5SThomas Graf 2383ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 2384ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 2385ab79ad14SMaciej Żenczykowski 238615e47304SEric W. Biederman cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid; 238786872cb5SThomas Graf cfg->fc_nlinfo.nlh = nlh; 23883b1e0a65SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = sock_net(skb->sk); 238986872cb5SThomas Graf 239086872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 239186872cb5SThomas Graf nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16); 239286872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 23931da177e4SLinus Torvalds } 239486872cb5SThomas Graf 239586872cb5SThomas Graf if (tb[RTA_DST]) { 239686872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 239786872cb5SThomas Graf 239886872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 239986872cb5SThomas Graf goto errout; 240086872cb5SThomas Graf 240186872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 24021da177e4SLinus Torvalds } 240386872cb5SThomas Graf 240486872cb5SThomas Graf if (tb[RTA_SRC]) { 240586872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 240686872cb5SThomas Graf 240786872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 240886872cb5SThomas Graf goto errout; 240986872cb5SThomas Graf 241086872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 24111da177e4SLinus Torvalds } 241286872cb5SThomas Graf 2413c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 2414c3968a85SDaniel Walter nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16); 2415c3968a85SDaniel Walter 241686872cb5SThomas Graf if (tb[RTA_OIF]) 241786872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 241886872cb5SThomas Graf 241986872cb5SThomas Graf if (tb[RTA_PRIORITY]) 242086872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 242186872cb5SThomas Graf 242286872cb5SThomas Graf if (tb[RTA_METRICS]) { 242386872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 242486872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 24251da177e4SLinus Torvalds } 242686872cb5SThomas Graf 242786872cb5SThomas Graf if (tb[RTA_TABLE]) 242886872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 242986872cb5SThomas Graf 243051ebd318SNicolas Dichtel if (tb[RTA_MULTIPATH]) { 243151ebd318SNicolas Dichtel cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); 243251ebd318SNicolas Dichtel cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); 243351ebd318SNicolas Dichtel } 243451ebd318SNicolas Dichtel 243586872cb5SThomas Graf err = 0; 243686872cb5SThomas Graf errout: 243786872cb5SThomas Graf return err; 24381da177e4SLinus Torvalds } 24391da177e4SLinus Torvalds 244051ebd318SNicolas Dichtel static int ip6_route_multipath(struct fib6_config *cfg, int add) 244151ebd318SNicolas Dichtel { 244251ebd318SNicolas Dichtel struct fib6_config r_cfg; 244351ebd318SNicolas Dichtel struct rtnexthop *rtnh; 244451ebd318SNicolas Dichtel int remaining; 244551ebd318SNicolas Dichtel int attrlen; 244651ebd318SNicolas Dichtel int err = 0, last_err = 0; 244751ebd318SNicolas Dichtel 244851ebd318SNicolas Dichtel beginning: 244951ebd318SNicolas Dichtel rtnh = (struct rtnexthop *)cfg->fc_mp; 245051ebd318SNicolas Dichtel remaining = cfg->fc_mp_len; 245151ebd318SNicolas Dichtel 245251ebd318SNicolas Dichtel /* Parse a Multipath Entry */ 245351ebd318SNicolas Dichtel while (rtnh_ok(rtnh, remaining)) { 245451ebd318SNicolas Dichtel memcpy(&r_cfg, cfg, sizeof(*cfg)); 245551ebd318SNicolas Dichtel if (rtnh->rtnh_ifindex) 245651ebd318SNicolas Dichtel r_cfg.fc_ifindex = rtnh->rtnh_ifindex; 245751ebd318SNicolas Dichtel 245851ebd318SNicolas Dichtel attrlen = rtnh_attrlen(rtnh); 245951ebd318SNicolas Dichtel if (attrlen > 0) { 246051ebd318SNicolas Dichtel struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 246151ebd318SNicolas Dichtel 246251ebd318SNicolas Dichtel nla = nla_find(attrs, attrlen, RTA_GATEWAY); 246351ebd318SNicolas Dichtel if (nla) { 246451ebd318SNicolas Dichtel nla_memcpy(&r_cfg.fc_gateway, nla, 16); 246551ebd318SNicolas Dichtel r_cfg.fc_flags |= RTF_GATEWAY; 246651ebd318SNicolas Dichtel } 246751ebd318SNicolas Dichtel } 246851ebd318SNicolas Dichtel err = add ? ip6_route_add(&r_cfg) : ip6_route_del(&r_cfg); 246951ebd318SNicolas Dichtel if (err) { 247051ebd318SNicolas Dichtel last_err = err; 247151ebd318SNicolas Dichtel /* If we are trying to remove a route, do not stop the 247251ebd318SNicolas Dichtel * loop when ip6_route_del() fails (because next hop is 247351ebd318SNicolas Dichtel * already gone), we should try to remove all next hops. 247451ebd318SNicolas Dichtel */ 247551ebd318SNicolas Dichtel if (add) { 247651ebd318SNicolas Dichtel /* If add fails, we should try to delete all 247751ebd318SNicolas Dichtel * next hops that have been already added. 247851ebd318SNicolas Dichtel */ 247951ebd318SNicolas Dichtel add = 0; 248051ebd318SNicolas Dichtel goto beginning; 248151ebd318SNicolas Dichtel } 248251ebd318SNicolas Dichtel } 24831a72418bSNicolas Dichtel /* Because each route is added like a single route we remove 24841a72418bSNicolas Dichtel * this flag after the first nexthop (if there is a collision, 24851a72418bSNicolas Dichtel * we have already fail to add the first nexthop: 24861a72418bSNicolas Dichtel * fib6_add_rt2node() has reject it). 24871a72418bSNicolas Dichtel */ 24881a72418bSNicolas Dichtel cfg->fc_nlinfo.nlh->nlmsg_flags &= ~NLM_F_EXCL; 248951ebd318SNicolas Dichtel rtnh = rtnh_next(rtnh, &remaining); 249051ebd318SNicolas Dichtel } 249151ebd318SNicolas Dichtel 249251ebd318SNicolas Dichtel return last_err; 249351ebd318SNicolas Dichtel } 249451ebd318SNicolas Dichtel 2495661d2967SThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh) 24961da177e4SLinus Torvalds { 249786872cb5SThomas Graf struct fib6_config cfg; 249886872cb5SThomas Graf int err; 24991da177e4SLinus Torvalds 250086872cb5SThomas Graf err = rtm_to_fib6_config(skb, nlh, &cfg); 250186872cb5SThomas Graf if (err < 0) 250286872cb5SThomas Graf return err; 250386872cb5SThomas Graf 250451ebd318SNicolas Dichtel if (cfg.fc_mp) 250551ebd318SNicolas Dichtel return ip6_route_multipath(&cfg, 0); 250651ebd318SNicolas Dichtel else 250786872cb5SThomas Graf return ip6_route_del(&cfg); 25081da177e4SLinus Torvalds } 25091da177e4SLinus Torvalds 2510661d2967SThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh) 25111da177e4SLinus Torvalds { 251286872cb5SThomas Graf struct fib6_config cfg; 251386872cb5SThomas Graf int err; 25141da177e4SLinus Torvalds 251586872cb5SThomas Graf err = rtm_to_fib6_config(skb, nlh, &cfg); 251686872cb5SThomas Graf if (err < 0) 251786872cb5SThomas Graf return err; 251886872cb5SThomas Graf 251951ebd318SNicolas Dichtel if (cfg.fc_mp) 252051ebd318SNicolas Dichtel return ip6_route_multipath(&cfg, 1); 252151ebd318SNicolas Dichtel else 252286872cb5SThomas Graf return ip6_route_add(&cfg); 25231da177e4SLinus Torvalds } 25241da177e4SLinus Torvalds 2525339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void) 2526339bf98fSThomas Graf { 2527339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 2528339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 2529339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 2530339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 2531339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 2532339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 2533339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 2534339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 2535339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 25366a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 2537339bf98fSThomas Graf + nla_total_size(sizeof(struct rta_cacheinfo)); 2538339bf98fSThomas Graf } 2539339bf98fSThomas Graf 2540191cd582SBrian Haley static int rt6_fill_node(struct net *net, 2541191cd582SBrian Haley struct sk_buff *skb, struct rt6_info *rt, 25420d51aa80SJamal Hadi Salim struct in6_addr *dst, struct in6_addr *src, 254315e47304SEric W. Biederman int iif, int type, u32 portid, u32 seq, 25447bc570c8SYOSHIFUJI Hideaki int prefix, int nowait, unsigned int flags) 25451da177e4SLinus Torvalds { 25461da177e4SLinus Torvalds struct rtmsg *rtm; 25471da177e4SLinus Torvalds struct nlmsghdr *nlh; 2548e3703b3dSThomas Graf long expires; 25499e762a4aSPatrick McHardy u32 table; 25501da177e4SLinus Torvalds 25511da177e4SLinus Torvalds if (prefix) { /* user wants prefix routes only */ 25521da177e4SLinus Torvalds if (!(rt->rt6i_flags & RTF_PREFIX_RT)) { 25531da177e4SLinus Torvalds /* success since this is not a prefix route */ 25541da177e4SLinus Torvalds return 1; 25551da177e4SLinus Torvalds } 25561da177e4SLinus Torvalds } 25571da177e4SLinus Torvalds 255815e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); 255938308473SDavid S. Miller if (!nlh) 256026932566SPatrick McHardy return -EMSGSIZE; 25612d7202bfSThomas Graf 25622d7202bfSThomas Graf rtm = nlmsg_data(nlh); 25631da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 25641da177e4SLinus Torvalds rtm->rtm_dst_len = rt->rt6i_dst.plen; 25651da177e4SLinus Torvalds rtm->rtm_src_len = rt->rt6i_src.plen; 25661da177e4SLinus Torvalds rtm->rtm_tos = 0; 2567c71099acSThomas Graf if (rt->rt6i_table) 25689e762a4aSPatrick McHardy table = rt->rt6i_table->tb6_id; 2569c71099acSThomas Graf else 25709e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 25719e762a4aSPatrick McHardy rtm->rtm_table = table; 2572c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_TABLE, table)) 2573c78679e8SDavid S. Miller goto nla_put_failure; 2574ef2c7d7bSNicolas Dichtel if (rt->rt6i_flags & RTF_REJECT) { 2575ef2c7d7bSNicolas Dichtel switch (rt->dst.error) { 2576ef2c7d7bSNicolas Dichtel case -EINVAL: 2577ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_BLACKHOLE; 2578ef2c7d7bSNicolas Dichtel break; 2579ef2c7d7bSNicolas Dichtel case -EACCES: 2580ef2c7d7bSNicolas Dichtel rtm->rtm_type = RTN_PROHIBIT; 2581ef2c7d7bSNicolas Dichtel break; 2582b4949ab2SNicolas Dichtel case -EAGAIN: 2583b4949ab2SNicolas Dichtel rtm->rtm_type = RTN_THROW; 2584b4949ab2SNicolas Dichtel break; 2585ef2c7d7bSNicolas Dichtel default: 25861da177e4SLinus Torvalds rtm->rtm_type = RTN_UNREACHABLE; 2587ef2c7d7bSNicolas Dichtel break; 2588ef2c7d7bSNicolas Dichtel } 2589ef2c7d7bSNicolas Dichtel } 2590ab79ad14SMaciej Żenczykowski else if (rt->rt6i_flags & RTF_LOCAL) 2591ab79ad14SMaciej Żenczykowski rtm->rtm_type = RTN_LOCAL; 2592d1918542SDavid S. Miller else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK)) 25931da177e4SLinus Torvalds rtm->rtm_type = RTN_LOCAL; 25941da177e4SLinus Torvalds else 25951da177e4SLinus Torvalds rtm->rtm_type = RTN_UNICAST; 25961da177e4SLinus Torvalds rtm->rtm_flags = 0; 25971da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 25981da177e4SLinus Torvalds rtm->rtm_protocol = rt->rt6i_protocol; 25991da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_DYNAMIC) 26001da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_REDIRECT; 2601f0396f60SDenis Ovsienko else if (rt->rt6i_flags & RTF_ADDRCONF) { 2602f0396f60SDenis Ovsienko if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO)) 26031da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_RA; 2604f0396f60SDenis Ovsienko else 2605f0396f60SDenis Ovsienko rtm->rtm_protocol = RTPROT_KERNEL; 2606f0396f60SDenis Ovsienko } 26071da177e4SLinus Torvalds 26081da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) 26091da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 26101da177e4SLinus Torvalds 26111da177e4SLinus Torvalds if (dst) { 2612c78679e8SDavid S. Miller if (nla_put(skb, RTA_DST, 16, dst)) 2613c78679e8SDavid S. Miller goto nla_put_failure; 26141da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 26151da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 2616c78679e8SDavid S. Miller if (nla_put(skb, RTA_DST, 16, &rt->rt6i_dst.addr)) 2617c78679e8SDavid S. Miller goto nla_put_failure; 26181da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 26191da177e4SLinus Torvalds if (src) { 2620c78679e8SDavid S. Miller if (nla_put(skb, RTA_SRC, 16, src)) 2621c78679e8SDavid S. Miller goto nla_put_failure; 26221da177e4SLinus Torvalds rtm->rtm_src_len = 128; 2623c78679e8SDavid S. Miller } else if (rtm->rtm_src_len && 2624c78679e8SDavid S. Miller nla_put(skb, RTA_SRC, 16, &rt->rt6i_src.addr)) 2625c78679e8SDavid S. Miller goto nla_put_failure; 26261da177e4SLinus Torvalds #endif 26277bc570c8SYOSHIFUJI Hideaki if (iif) { 26287bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 26297bc570c8SYOSHIFUJI Hideaki if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) { 26308229efdaSBenjamin Thery int err = ip6mr_get_route(net, skb, rtm, nowait); 26317bc570c8SYOSHIFUJI Hideaki if (err <= 0) { 26327bc570c8SYOSHIFUJI Hideaki if (!nowait) { 26337bc570c8SYOSHIFUJI Hideaki if (err == 0) 26347bc570c8SYOSHIFUJI Hideaki return 0; 26357bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 26367bc570c8SYOSHIFUJI Hideaki } else { 26377bc570c8SYOSHIFUJI Hideaki if (err == -EMSGSIZE) 26387bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 26397bc570c8SYOSHIFUJI Hideaki } 26407bc570c8SYOSHIFUJI Hideaki } 26417bc570c8SYOSHIFUJI Hideaki } else 26427bc570c8SYOSHIFUJI Hideaki #endif 2643c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_IIF, iif)) 2644c78679e8SDavid S. Miller goto nla_put_failure; 26457bc570c8SYOSHIFUJI Hideaki } else if (dst) { 26461da177e4SLinus Torvalds struct in6_addr saddr_buf; 2647c78679e8SDavid S. Miller if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 && 2648c78679e8SDavid S. Miller nla_put(skb, RTA_PREFSRC, 16, &saddr_buf)) 2649c78679e8SDavid S. Miller goto nla_put_failure; 2650c3968a85SDaniel Walter } 2651c3968a85SDaniel Walter 2652c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) { 2653c3968a85SDaniel Walter struct in6_addr saddr_buf; 26544e3fd7a0SAlexey Dobriyan saddr_buf = rt->rt6i_prefsrc.addr; 2655c78679e8SDavid S. Miller if (nla_put(skb, RTA_PREFSRC, 16, &saddr_buf)) 2656c78679e8SDavid S. Miller goto nla_put_failure; 26571da177e4SLinus Torvalds } 26582d7202bfSThomas Graf 2659defb3519SDavid S. Miller if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0) 26602d7202bfSThomas Graf goto nla_put_failure; 26612d7202bfSThomas Graf 2662dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_GATEWAY) { 2663dd0cbf29SYOSHIFUJI Hideaki / 吉藤英明 if (nla_put(skb, RTA_GATEWAY, 16, &rt->rt6i_gateway) < 0) 266494f826b8SEric Dumazet goto nla_put_failure; 266594f826b8SEric Dumazet } 26662d7202bfSThomas Graf 2667c78679e8SDavid S. Miller if (rt->dst.dev && 2668c78679e8SDavid S. Miller nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex)) 2669c78679e8SDavid S. Miller goto nla_put_failure; 2670c78679e8SDavid S. Miller if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric)) 2671c78679e8SDavid S. Miller goto nla_put_failure; 26728253947eSLi Wei 26738253947eSLi Wei expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0; 267469cdf8f9SYOSHIFUJI Hideaki 267587a50699SDavid S. Miller if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0) 2676e3703b3dSThomas Graf goto nla_put_failure; 26771da177e4SLinus Torvalds 26782d7202bfSThomas Graf return nlmsg_end(skb, nlh); 26792d7202bfSThomas Graf 26802d7202bfSThomas Graf nla_put_failure: 268126932566SPatrick McHardy nlmsg_cancel(skb, nlh); 268226932566SPatrick McHardy return -EMSGSIZE; 26831da177e4SLinus Torvalds } 26841da177e4SLinus Torvalds 26851b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg) 26861da177e4SLinus Torvalds { 26871da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 26881da177e4SLinus Torvalds int prefix; 26891da177e4SLinus Torvalds 26902d7202bfSThomas Graf if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { 26912d7202bfSThomas Graf struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); 26921da177e4SLinus Torvalds prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0; 26931da177e4SLinus Torvalds } else 26941da177e4SLinus Torvalds prefix = 0; 26951da177e4SLinus Torvalds 2696191cd582SBrian Haley return rt6_fill_node(arg->net, 2697191cd582SBrian Haley arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE, 269815e47304SEric W. Biederman NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq, 26997bc570c8SYOSHIFUJI Hideaki prefix, 0, NLM_F_MULTI); 27001da177e4SLinus Torvalds } 27011da177e4SLinus Torvalds 2702661d2967SThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh) 27031da177e4SLinus Torvalds { 27043b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 2705ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 27061da177e4SLinus Torvalds struct rt6_info *rt; 2707ab364a6fSThomas Graf struct sk_buff *skb; 2708ab364a6fSThomas Graf struct rtmsg *rtm; 27094c9483b2SDavid S. Miller struct flowi6 fl6; 271072331bc0SShmulik Ladkani int err, iif = 0, oif = 0; 2711ab364a6fSThomas Graf 2712ab364a6fSThomas Graf err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy); 2713ab364a6fSThomas Graf if (err < 0) 2714ab364a6fSThomas Graf goto errout; 2715ab364a6fSThomas Graf 2716ab364a6fSThomas Graf err = -EINVAL; 27174c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 2718ab364a6fSThomas Graf 2719ab364a6fSThomas Graf if (tb[RTA_SRC]) { 2720ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 2721ab364a6fSThomas Graf goto errout; 2722ab364a6fSThomas Graf 27234e3fd7a0SAlexey Dobriyan fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 2724ab364a6fSThomas Graf } 2725ab364a6fSThomas Graf 2726ab364a6fSThomas Graf if (tb[RTA_DST]) { 2727ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 2728ab364a6fSThomas Graf goto errout; 2729ab364a6fSThomas Graf 27304e3fd7a0SAlexey Dobriyan fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 2731ab364a6fSThomas Graf } 2732ab364a6fSThomas Graf 2733ab364a6fSThomas Graf if (tb[RTA_IIF]) 2734ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 2735ab364a6fSThomas Graf 2736ab364a6fSThomas Graf if (tb[RTA_OIF]) 273772331bc0SShmulik Ladkani oif = nla_get_u32(tb[RTA_OIF]); 2738ab364a6fSThomas Graf 27392e47b291SLorenzo Colitti if (tb[RTA_MARK]) 27402e47b291SLorenzo Colitti fl6.flowi6_mark = nla_get_u32(tb[RTA_MARK]); 27412e47b291SLorenzo Colitti 2742ab364a6fSThomas Graf if (iif) { 2743ab364a6fSThomas Graf struct net_device *dev; 274472331bc0SShmulik Ladkani int flags = 0; 274572331bc0SShmulik Ladkani 27465578689aSDaniel Lezcano dev = __dev_get_by_index(net, iif); 2747ab364a6fSThomas Graf if (!dev) { 2748ab364a6fSThomas Graf err = -ENODEV; 2749ab364a6fSThomas Graf goto errout; 2750ab364a6fSThomas Graf } 275172331bc0SShmulik Ladkani 275272331bc0SShmulik Ladkani fl6.flowi6_iif = iif; 275372331bc0SShmulik Ladkani 275472331bc0SShmulik Ladkani if (!ipv6_addr_any(&fl6.saddr)) 275572331bc0SShmulik Ladkani flags |= RT6_LOOKUP_F_HAS_SADDR; 275672331bc0SShmulik Ladkani 275772331bc0SShmulik Ladkani rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6, 275872331bc0SShmulik Ladkani flags); 275972331bc0SShmulik Ladkani } else { 276072331bc0SShmulik Ladkani fl6.flowi6_oif = oif; 276172331bc0SShmulik Ladkani 276272331bc0SShmulik Ladkani rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6); 2763ab364a6fSThomas Graf } 27641da177e4SLinus Torvalds 27651da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 276638308473SDavid S. Miller if (!skb) { 276794e187c0SAmerigo Wang ip6_rt_put(rt); 2768ab364a6fSThomas Graf err = -ENOBUFS; 2769ab364a6fSThomas Graf goto errout; 2770ab364a6fSThomas Graf } 27711da177e4SLinus Torvalds 27721da177e4SLinus Torvalds /* Reserve room for dummy headers, this skb can pass 27731da177e4SLinus Torvalds through good chunk of routing engine. 27741da177e4SLinus Torvalds */ 2775459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 27761da177e4SLinus Torvalds skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr)); 27771da177e4SLinus Torvalds 2778d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 27791da177e4SLinus Torvalds 27804c9483b2SDavid S. Miller err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif, 278115e47304SEric W. Biederman RTM_NEWROUTE, NETLINK_CB(in_skb).portid, 27827bc570c8SYOSHIFUJI Hideaki nlh->nlmsg_seq, 0, 0, 0); 27831da177e4SLinus Torvalds if (err < 0) { 2784ab364a6fSThomas Graf kfree_skb(skb); 2785ab364a6fSThomas Graf goto errout; 27861da177e4SLinus Torvalds } 27871da177e4SLinus Torvalds 278815e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 2789ab364a6fSThomas Graf errout: 27901da177e4SLinus Torvalds return err; 27911da177e4SLinus Torvalds } 27921da177e4SLinus Torvalds 279386872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info) 27941da177e4SLinus Torvalds { 27951da177e4SLinus Torvalds struct sk_buff *skb; 27965578689aSDaniel Lezcano struct net *net = info->nl_net; 2797528c4cebSDenis V. Lunev u32 seq; 2798528c4cebSDenis V. Lunev int err; 27990d51aa80SJamal Hadi Salim 2800528c4cebSDenis V. Lunev err = -ENOBUFS; 280138308473SDavid S. Miller seq = info->nlh ? info->nlh->nlmsg_seq : 0; 280286872cb5SThomas Graf 2803339bf98fSThomas Graf skb = nlmsg_new(rt6_nlmsg_size(), gfp_any()); 280438308473SDavid S. Miller if (!skb) 280521713ebcSThomas Graf goto errout; 28061da177e4SLinus Torvalds 2807191cd582SBrian Haley err = rt6_fill_node(net, skb, rt, NULL, NULL, 0, 280815e47304SEric W. Biederman event, info->portid, seq, 0, 0, 0); 280926932566SPatrick McHardy if (err < 0) { 281026932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 281126932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 281226932566SPatrick McHardy kfree_skb(skb); 281326932566SPatrick McHardy goto errout; 281426932566SPatrick McHardy } 281515e47304SEric W. Biederman rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, 28165578689aSDaniel Lezcano info->nlh, gfp_any()); 28171ce85fe4SPablo Neira Ayuso return; 281821713ebcSThomas Graf errout: 281921713ebcSThomas Graf if (err < 0) 28205578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 28211da177e4SLinus Torvalds } 28221da177e4SLinus Torvalds 28238ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 2824351638e7SJiri Pirko unsigned long event, void *ptr) 28258ed67789SDaniel Lezcano { 2826351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 2827c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 28288ed67789SDaniel Lezcano 28298ed67789SDaniel Lezcano if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) { 2830d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 28318ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 28328ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 2833d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 28348ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 2835d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 28368ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 28378ed67789SDaniel Lezcano #endif 28388ed67789SDaniel Lezcano } 28398ed67789SDaniel Lezcano 28408ed67789SDaniel Lezcano return NOTIFY_OK; 28418ed67789SDaniel Lezcano } 28428ed67789SDaniel Lezcano 28431da177e4SLinus Torvalds /* 28441da177e4SLinus Torvalds * /proc 28451da177e4SLinus Torvalds */ 28461da177e4SLinus Torvalds 28471da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 28481da177e4SLinus Torvalds 284933120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = { 285033120b30SAlexey Dobriyan .owner = THIS_MODULE, 285133120b30SAlexey Dobriyan .open = ipv6_route_open, 285233120b30SAlexey Dobriyan .read = seq_read, 285333120b30SAlexey Dobriyan .llseek = seq_lseek, 28548d2ca1d7SHannes Frederic Sowa .release = seq_release_net, 285533120b30SAlexey Dobriyan }; 285633120b30SAlexey Dobriyan 28571da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 28581da177e4SLinus Torvalds { 285969ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 28601da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 286169ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 286269ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 286369ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_alloc, 286469ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 286569ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 2866fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 286769ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 28681da177e4SLinus Torvalds 28691da177e4SLinus Torvalds return 0; 28701da177e4SLinus Torvalds } 28711da177e4SLinus Torvalds 28721da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file) 28731da177e4SLinus Torvalds { 2874de05c557SPavel Emelyanov return single_open_net(inode, file, rt6_stats_seq_show); 287569ddb805SDaniel Lezcano } 287669ddb805SDaniel Lezcano 28779a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = { 28781da177e4SLinus Torvalds .owner = THIS_MODULE, 28791da177e4SLinus Torvalds .open = rt6_stats_seq_open, 28801da177e4SLinus Torvalds .read = seq_read, 28811da177e4SLinus Torvalds .llseek = seq_lseek, 2882b6fcbdb4SPavel Emelyanov .release = single_release_net, 28831da177e4SLinus Torvalds }; 28841da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 28851da177e4SLinus Torvalds 28861da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 28871da177e4SLinus Torvalds 28881da177e4SLinus Torvalds static 2889fe2c6338SJoe Perches int ipv6_sysctl_rtcache_flush(struct ctl_table *ctl, int write, 28901da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 28911da177e4SLinus Torvalds { 2892c486da34SLucian Adrian Grijincu struct net *net; 2893c486da34SLucian Adrian Grijincu int delay; 2894c486da34SLucian Adrian Grijincu if (!write) 2895c486da34SLucian Adrian Grijincu return -EINVAL; 2896c486da34SLucian Adrian Grijincu 2897c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 2898c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 28998d65af78SAlexey Dobriyan proc_dointvec(ctl, write, buffer, lenp, ppos); 29002ac3ac8fSMichal Kubeček fib6_run_gc(delay <= 0 ? 0 : (unsigned long)delay, net, delay > 0); 29011da177e4SLinus Torvalds return 0; 29021da177e4SLinus Torvalds } 29031da177e4SLinus Torvalds 2904fe2c6338SJoe Perches struct ctl_table ipv6_route_table_template[] = { 29051da177e4SLinus Torvalds { 29061da177e4SLinus Torvalds .procname = "flush", 29074990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 29081da177e4SLinus Torvalds .maxlen = sizeof(int), 290989c8b3a1SDave Jones .mode = 0200, 29106d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 29111da177e4SLinus Torvalds }, 29121da177e4SLinus Torvalds { 29131da177e4SLinus Torvalds .procname = "gc_thresh", 29149a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 29151da177e4SLinus Torvalds .maxlen = sizeof(int), 29161da177e4SLinus Torvalds .mode = 0644, 29176d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 29181da177e4SLinus Torvalds }, 29191da177e4SLinus Torvalds { 29201da177e4SLinus Torvalds .procname = "max_size", 29214990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 29221da177e4SLinus Torvalds .maxlen = sizeof(int), 29231da177e4SLinus Torvalds .mode = 0644, 29246d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 29251da177e4SLinus Torvalds }, 29261da177e4SLinus Torvalds { 29271da177e4SLinus Torvalds .procname = "gc_min_interval", 29284990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 29291da177e4SLinus Torvalds .maxlen = sizeof(int), 29301da177e4SLinus Torvalds .mode = 0644, 29316d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 29321da177e4SLinus Torvalds }, 29331da177e4SLinus Torvalds { 29341da177e4SLinus Torvalds .procname = "gc_timeout", 29354990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 29361da177e4SLinus Torvalds .maxlen = sizeof(int), 29371da177e4SLinus Torvalds .mode = 0644, 29386d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 29391da177e4SLinus Torvalds }, 29401da177e4SLinus Torvalds { 29411da177e4SLinus Torvalds .procname = "gc_interval", 29424990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 29431da177e4SLinus Torvalds .maxlen = sizeof(int), 29441da177e4SLinus Torvalds .mode = 0644, 29456d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 29461da177e4SLinus Torvalds }, 29471da177e4SLinus Torvalds { 29481da177e4SLinus Torvalds .procname = "gc_elasticity", 29494990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 29501da177e4SLinus Torvalds .maxlen = sizeof(int), 29511da177e4SLinus Torvalds .mode = 0644, 2952f3d3f616SMin Zhang .proc_handler = proc_dointvec, 29531da177e4SLinus Torvalds }, 29541da177e4SLinus Torvalds { 29551da177e4SLinus Torvalds .procname = "mtu_expires", 29564990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 29571da177e4SLinus Torvalds .maxlen = sizeof(int), 29581da177e4SLinus Torvalds .mode = 0644, 29596d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 29601da177e4SLinus Torvalds }, 29611da177e4SLinus Torvalds { 29621da177e4SLinus Torvalds .procname = "min_adv_mss", 29634990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 29641da177e4SLinus Torvalds .maxlen = sizeof(int), 29651da177e4SLinus Torvalds .mode = 0644, 2966f3d3f616SMin Zhang .proc_handler = proc_dointvec, 29671da177e4SLinus Torvalds }, 29681da177e4SLinus Torvalds { 29691da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 29704990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 29711da177e4SLinus Torvalds .maxlen = sizeof(int), 29721da177e4SLinus Torvalds .mode = 0644, 29736d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 29741da177e4SLinus Torvalds }, 2975f8572d8fSEric W. Biederman { } 29761da177e4SLinus Torvalds }; 29771da177e4SLinus Torvalds 29782c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 2979760f2d01SDaniel Lezcano { 2980760f2d01SDaniel Lezcano struct ctl_table *table; 2981760f2d01SDaniel Lezcano 2982760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 2983760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 2984760f2d01SDaniel Lezcano GFP_KERNEL); 29855ee09105SYOSHIFUJI Hideaki 29865ee09105SYOSHIFUJI Hideaki if (table) { 29875ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 2988c486da34SLucian Adrian Grijincu table[0].extra1 = net; 298986393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 29905ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 29915ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 29925ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 29935ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 29945ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 29955ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 29965ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 29979c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 2998464dc801SEric W. Biederman 2999464dc801SEric W. Biederman /* Don't export sysctls to unprivileged users */ 3000464dc801SEric W. Biederman if (net->user_ns != &init_user_ns) 3001464dc801SEric W. Biederman table[0].procname = NULL; 30025ee09105SYOSHIFUJI Hideaki } 30035ee09105SYOSHIFUJI Hideaki 3004760f2d01SDaniel Lezcano return table; 3005760f2d01SDaniel Lezcano } 30061da177e4SLinus Torvalds #endif 30071da177e4SLinus Torvalds 30082c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 3009cdb18761SDaniel Lezcano { 3010633d424bSPavel Emelyanov int ret = -ENOMEM; 30118ed67789SDaniel Lezcano 301286393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 301386393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 3014f2fc6a54SBenjamin Thery 3015fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 3016fc66f95cSEric Dumazet goto out_ip6_dst_ops; 3017fc66f95cSEric Dumazet 30188ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 30198ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 30208ed67789SDaniel Lezcano GFP_KERNEL); 30218ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 3022fc66f95cSEric Dumazet goto out_ip6_dst_entries; 3023d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.path = 30248ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_null_entry; 3025d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 302662fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 302762fa8a84SDavid S. Miller ip6_template_metrics, true); 30288ed67789SDaniel Lezcano 30298ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 30308ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 30318ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 30328ed67789SDaniel Lezcano GFP_KERNEL); 303368fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 303468fffc67SPeter Zijlstra goto out_ip6_null_entry; 3035d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.path = 30368ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_prohibit_entry; 3037d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 303862fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 303962fa8a84SDavid S. Miller ip6_template_metrics, true); 30408ed67789SDaniel Lezcano 30418ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 30428ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 30438ed67789SDaniel Lezcano GFP_KERNEL); 304468fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 304568fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 3046d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.path = 30478ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_blk_hole_entry; 3048d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 304962fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 305062fa8a84SDavid S. Miller ip6_template_metrics, true); 30518ed67789SDaniel Lezcano #endif 30528ed67789SDaniel Lezcano 3053b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 3054b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 3055b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 3056b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 3057b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 3058b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 3059b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 3060b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 3061b339a47cSPeter Zijlstra 30626891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 30636891a346SBenjamin Thery 30648ed67789SDaniel Lezcano ret = 0; 30658ed67789SDaniel Lezcano out: 30668ed67789SDaniel Lezcano return ret; 3067f2fc6a54SBenjamin Thery 306868fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 306968fffc67SPeter Zijlstra out_ip6_prohibit_entry: 307068fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 307168fffc67SPeter Zijlstra out_ip6_null_entry: 307268fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 307368fffc67SPeter Zijlstra #endif 3074fc66f95cSEric Dumazet out_ip6_dst_entries: 3075fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 3076f2fc6a54SBenjamin Thery out_ip6_dst_ops: 3077f2fc6a54SBenjamin Thery goto out; 3078cdb18761SDaniel Lezcano } 3079cdb18761SDaniel Lezcano 30802c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 3081cdb18761SDaniel Lezcano { 30828ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 30838ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 30848ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 30858ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 30868ed67789SDaniel Lezcano #endif 308741bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 3088cdb18761SDaniel Lezcano } 3089cdb18761SDaniel Lezcano 3090d189634eSThomas Graf static int __net_init ip6_route_net_init_late(struct net *net) 3091d189634eSThomas Graf { 3092d189634eSThomas Graf #ifdef CONFIG_PROC_FS 3093d4beaa66SGao feng proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops); 3094d4beaa66SGao feng proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops); 3095d189634eSThomas Graf #endif 3096d189634eSThomas Graf return 0; 3097d189634eSThomas Graf } 3098d189634eSThomas Graf 3099d189634eSThomas Graf static void __net_exit ip6_route_net_exit_late(struct net *net) 3100d189634eSThomas Graf { 3101d189634eSThomas Graf #ifdef CONFIG_PROC_FS 3102ece31ffdSGao feng remove_proc_entry("ipv6_route", net->proc_net); 3103ece31ffdSGao feng remove_proc_entry("rt6_stats", net->proc_net); 3104d189634eSThomas Graf #endif 3105d189634eSThomas Graf } 3106d189634eSThomas Graf 3107cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 3108cdb18761SDaniel Lezcano .init = ip6_route_net_init, 3109cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 3110cdb18761SDaniel Lezcano }; 3111cdb18761SDaniel Lezcano 3112c3426b47SDavid S. Miller static int __net_init ipv6_inetpeer_init(struct net *net) 3113c3426b47SDavid S. Miller { 3114c3426b47SDavid S. Miller struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); 3115c3426b47SDavid S. Miller 3116c3426b47SDavid S. Miller if (!bp) 3117c3426b47SDavid S. Miller return -ENOMEM; 3118c3426b47SDavid S. Miller inet_peer_base_init(bp); 3119c3426b47SDavid S. Miller net->ipv6.peers = bp; 3120c3426b47SDavid S. Miller return 0; 3121c3426b47SDavid S. Miller } 3122c3426b47SDavid S. Miller 3123c3426b47SDavid S. Miller static void __net_exit ipv6_inetpeer_exit(struct net *net) 3124c3426b47SDavid S. Miller { 3125c3426b47SDavid S. Miller struct inet_peer_base *bp = net->ipv6.peers; 3126c3426b47SDavid S. Miller 3127c3426b47SDavid S. Miller net->ipv6.peers = NULL; 312856a6b248SDavid S. Miller inetpeer_invalidate_tree(bp); 3129c3426b47SDavid S. Miller kfree(bp); 3130c3426b47SDavid S. Miller } 3131c3426b47SDavid S. Miller 31322b823f72SDavid S. Miller static struct pernet_operations ipv6_inetpeer_ops = { 3133c3426b47SDavid S. Miller .init = ipv6_inetpeer_init, 3134c3426b47SDavid S. Miller .exit = ipv6_inetpeer_exit, 3135c3426b47SDavid S. Miller }; 3136c3426b47SDavid S. Miller 3137d189634eSThomas Graf static struct pernet_operations ip6_route_net_late_ops = { 3138d189634eSThomas Graf .init = ip6_route_net_init_late, 3139d189634eSThomas Graf .exit = ip6_route_net_exit_late, 3140d189634eSThomas Graf }; 3141d189634eSThomas Graf 31428ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 31438ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 31448ed67789SDaniel Lezcano .priority = 0, 31458ed67789SDaniel Lezcano }; 31468ed67789SDaniel Lezcano 3147433d49c3SDaniel Lezcano int __init ip6_route_init(void) 31481da177e4SLinus Torvalds { 3149433d49c3SDaniel Lezcano int ret; 3150433d49c3SDaniel Lezcano 31519a7ec3a9SDaniel Lezcano ret = -ENOMEM; 31529a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 31539a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 31549a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 31559a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 3156c19a28e1SFernando Carrijo goto out; 315714e50e57SDavid S. Miller 3158fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 31598ed67789SDaniel Lezcano if (ret) 3160bdb3289fSDaniel Lezcano goto out_kmem_cache; 3161bdb3289fSDaniel Lezcano 3162c3426b47SDavid S. Miller ret = register_pernet_subsys(&ipv6_inetpeer_ops); 3163c3426b47SDavid S. Miller if (ret) 3164e8803b6cSDavid S. Miller goto out_dst_entries; 31652a0c451aSThomas Graf 31667e52b33bSDavid S. Miller ret = register_pernet_subsys(&ip6_route_net_ops); 31677e52b33bSDavid S. Miller if (ret) 31687e52b33bSDavid S. Miller goto out_register_inetpeer; 3169c3426b47SDavid S. Miller 31705dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 31715dc121e9SArnaud Ebalard 31728ed67789SDaniel Lezcano /* Registering of the loopback is done before this portion of code, 31738ed67789SDaniel Lezcano * the loopback reference in rt6_info will not be taken, do it 31748ed67789SDaniel Lezcano * manually for init_net */ 3175d8d1f30bSChangli Gao init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 31768ed67789SDaniel Lezcano init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 3177bdb3289fSDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 3178d8d1f30bSChangli Gao init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 31798ed67789SDaniel Lezcano init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 3180d8d1f30bSChangli Gao init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 31818ed67789SDaniel Lezcano init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 3182bdb3289fSDaniel Lezcano #endif 3183e8803b6cSDavid S. Miller ret = fib6_init(); 3184433d49c3SDaniel Lezcano if (ret) 31858ed67789SDaniel Lezcano goto out_register_subsys; 3186433d49c3SDaniel Lezcano 3187433d49c3SDaniel Lezcano ret = xfrm6_init(); 3188433d49c3SDaniel Lezcano if (ret) 3189e8803b6cSDavid S. Miller goto out_fib6_init; 3190c35b7e72SDaniel Lezcano 3191433d49c3SDaniel Lezcano ret = fib6_rules_init(); 3192433d49c3SDaniel Lezcano if (ret) 3193433d49c3SDaniel Lezcano goto xfrm6_init; 31947e5449c2SDaniel Lezcano 3195d189634eSThomas Graf ret = register_pernet_subsys(&ip6_route_net_late_ops); 3196d189634eSThomas Graf if (ret) 3197d189634eSThomas Graf goto fib6_rules_init; 3198d189634eSThomas Graf 3199433d49c3SDaniel Lezcano ret = -ENOBUFS; 3200c7ac8679SGreg Rose if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) || 3201c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) || 3202c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL)) 3203d189634eSThomas Graf goto out_register_late_subsys; 3204433d49c3SDaniel Lezcano 32058ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 3206cdb18761SDaniel Lezcano if (ret) 3207d189634eSThomas Graf goto out_register_late_subsys; 32088ed67789SDaniel Lezcano 3209433d49c3SDaniel Lezcano out: 3210433d49c3SDaniel Lezcano return ret; 3211433d49c3SDaniel Lezcano 3212d189634eSThomas Graf out_register_late_subsys: 3213d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 3214433d49c3SDaniel Lezcano fib6_rules_init: 3215433d49c3SDaniel Lezcano fib6_rules_cleanup(); 3216433d49c3SDaniel Lezcano xfrm6_init: 3217433d49c3SDaniel Lezcano xfrm6_fini(); 32182a0c451aSThomas Graf out_fib6_init: 32192a0c451aSThomas Graf fib6_gc_cleanup(); 32208ed67789SDaniel Lezcano out_register_subsys: 32218ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 32227e52b33bSDavid S. Miller out_register_inetpeer: 32237e52b33bSDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 3224fc66f95cSEric Dumazet out_dst_entries: 3225fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 3226433d49c3SDaniel Lezcano out_kmem_cache: 3227f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 3228433d49c3SDaniel Lezcano goto out; 32291da177e4SLinus Torvalds } 32301da177e4SLinus Torvalds 32311da177e4SLinus Torvalds void ip6_route_cleanup(void) 32321da177e4SLinus Torvalds { 32338ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 3234d189634eSThomas Graf unregister_pernet_subsys(&ip6_route_net_late_ops); 3235101367c2SThomas Graf fib6_rules_cleanup(); 32361da177e4SLinus Torvalds xfrm6_fini(); 32371da177e4SLinus Torvalds fib6_gc_cleanup(); 3238c3426b47SDavid S. Miller unregister_pernet_subsys(&ipv6_inetpeer_ops); 32398ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 324041bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 3241f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 32421da177e4SLinus Torvalds } 3243