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 274fc268d2SRandy Dunlap #include <linux/capability.h> 281da177e4SLinus Torvalds #include <linux/errno.h> 29bc3b2d7fSPaul Gortmaker #include <linux/export.h> 301da177e4SLinus Torvalds #include <linux/types.h> 311da177e4SLinus Torvalds #include <linux/times.h> 321da177e4SLinus Torvalds #include <linux/socket.h> 331da177e4SLinus Torvalds #include <linux/sockios.h> 341da177e4SLinus Torvalds #include <linux/net.h> 351da177e4SLinus Torvalds #include <linux/route.h> 361da177e4SLinus Torvalds #include <linux/netdevice.h> 371da177e4SLinus Torvalds #include <linux/in6.h> 387bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h> 391da177e4SLinus Torvalds #include <linux/init.h> 401da177e4SLinus Torvalds #include <linux/if_arp.h> 411da177e4SLinus Torvalds #include <linux/proc_fs.h> 421da177e4SLinus Torvalds #include <linux/seq_file.h> 435b7c931dSDaniel Lezcano #include <linux/nsproxy.h> 445a0e3ad6STejun Heo #include <linux/slab.h> 45457c4cbcSEric W. Biederman #include <net/net_namespace.h> 461da177e4SLinus Torvalds #include <net/snmp.h> 471da177e4SLinus Torvalds #include <net/ipv6.h> 481da177e4SLinus Torvalds #include <net/ip6_fib.h> 491da177e4SLinus Torvalds #include <net/ip6_route.h> 501da177e4SLinus Torvalds #include <net/ndisc.h> 511da177e4SLinus Torvalds #include <net/addrconf.h> 521da177e4SLinus Torvalds #include <net/tcp.h> 531da177e4SLinus Torvalds #include <linux/rtnetlink.h> 541da177e4SLinus Torvalds #include <net/dst.h> 551da177e4SLinus Torvalds #include <net/xfrm.h> 568d71740cSTom Tucker #include <net/netevent.h> 5721713ebcSThomas Graf #include <net/netlink.h> 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds #include <asm/uaccess.h> 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 621da177e4SLinus Torvalds #include <linux/sysctl.h> 631da177e4SLinus Torvalds #endif 641da177e4SLinus Torvalds 6521efcfa0SEric Dumazet static struct rt6_info *ip6_rt_copy(const struct rt6_info *ort, 6621efcfa0SEric Dumazet const struct in6_addr *dest); 671da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie); 680dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst); 69ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst); 701da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *); 711da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *); 721da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *, 731da177e4SLinus Torvalds struct net_device *dev, int how); 74569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops); 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds static int ip6_pkt_discard(struct sk_buff *skb); 771da177e4SLinus Torvalds static int ip6_pkt_discard_out(struct sk_buff *skb); 781da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb); 791da177e4SLinus Torvalds static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu); 801da177e4SLinus Torvalds 8170ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 82efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 83b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 84b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex, 8570ceb4f5SYOSHIFUJI Hideaki unsigned pref); 86efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 87b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 88b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex); 8970ceb4f5SYOSHIFUJI Hideaki #endif 9070ceb4f5SYOSHIFUJI Hideaki 9106582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old) 9206582540SDavid S. Miller { 9306582540SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 9406582540SDavid S. Miller struct inet_peer *peer; 9506582540SDavid S. Miller u32 *p = NULL; 9606582540SDavid S. Miller 978e2ec639SYan, Zheng if (!(rt->dst.flags & DST_HOST)) 988e2ec639SYan, Zheng return NULL; 998e2ec639SYan, Zheng 10006582540SDavid S. Miller if (!rt->rt6i_peer) 10106582540SDavid S. Miller rt6_bind_peer(rt, 1); 10206582540SDavid S. Miller 10306582540SDavid S. Miller peer = rt->rt6i_peer; 10406582540SDavid S. Miller if (peer) { 10506582540SDavid S. Miller u32 *old_p = __DST_METRICS_PTR(old); 10606582540SDavid S. Miller unsigned long prev, new; 10706582540SDavid S. Miller 10806582540SDavid S. Miller p = peer->metrics; 10906582540SDavid S. Miller if (inet_metrics_new(peer)) 11006582540SDavid S. Miller memcpy(p, old_p, sizeof(u32) * RTAX_MAX); 11106582540SDavid S. Miller 11206582540SDavid S. Miller new = (unsigned long) p; 11306582540SDavid S. Miller prev = cmpxchg(&dst->_metrics, old, new); 11406582540SDavid S. Miller 11506582540SDavid S. Miller if (prev != old) { 11606582540SDavid S. Miller p = __DST_METRICS_PTR(prev); 11706582540SDavid S. Miller if (prev & DST_METRICS_READ_ONLY) 11806582540SDavid S. Miller p = NULL; 11906582540SDavid S. Miller } 12006582540SDavid S. Miller } 12106582540SDavid S. Miller return p; 12206582540SDavid S. Miller } 12306582540SDavid S. Miller 124d3aaeb38SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst, const void *daddr) 125d3aaeb38SDavid S. Miller { 126f83c7790SDavid S. Miller struct neighbour *n = __ipv6_neigh_lookup(&nd_tbl, dst->dev, daddr); 127f83c7790SDavid S. Miller if (n) 128f83c7790SDavid S. Miller return n; 129f83c7790SDavid S. Miller return neigh_create(&nd_tbl, daddr, dst->dev); 130f83c7790SDavid S. Miller } 131f83c7790SDavid S. Miller 132*8ade06c6SDavid S. Miller static int rt6_bind_neighbour(struct rt6_info *rt, struct net_device *dev) 133f83c7790SDavid S. Miller { 134*8ade06c6SDavid S. Miller struct neighbour *n = __ipv6_neigh_lookup(&nd_tbl, dev, &rt->rt6i_gateway); 135*8ade06c6SDavid S. Miller if (!n) { 136*8ade06c6SDavid S. Miller n = neigh_create(&nd_tbl, &rt->rt6i_gateway, dev); 137f83c7790SDavid S. Miller if (IS_ERR(n)) 138f83c7790SDavid S. Miller return PTR_ERR(n); 139*8ade06c6SDavid S. Miller } 140f83c7790SDavid S. Miller dst_set_neighbour(&rt->dst, n); 141f83c7790SDavid S. Miller 142f83c7790SDavid S. Miller return 0; 143d3aaeb38SDavid S. Miller } 144d3aaeb38SDavid S. Miller 1459a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = { 1461da177e4SLinus Torvalds .family = AF_INET6, 14709640e63SHarvey Harrison .protocol = cpu_to_be16(ETH_P_IPV6), 1481da177e4SLinus Torvalds .gc = ip6_dst_gc, 1491da177e4SLinus Torvalds .gc_thresh = 1024, 1501da177e4SLinus Torvalds .check = ip6_dst_check, 1510dbaee3bSDavid S. Miller .default_advmss = ip6_default_advmss, 152ebb762f2SSteffen Klassert .mtu = ip6_mtu, 15306582540SDavid S. Miller .cow_metrics = ipv6_cow_metrics, 1541da177e4SLinus Torvalds .destroy = ip6_dst_destroy, 1551da177e4SLinus Torvalds .ifdown = ip6_dst_ifdown, 1561da177e4SLinus Torvalds .negative_advice = ip6_negative_advice, 1571da177e4SLinus Torvalds .link_failure = ip6_link_failure, 1581da177e4SLinus Torvalds .update_pmtu = ip6_rt_update_pmtu, 1591ac06e03SHerbert Xu .local_out = __ip6_local_out, 160d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 1611da177e4SLinus Torvalds }; 1621da177e4SLinus Torvalds 163ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst) 164ec831ea7SRoland Dreier { 165618f9bc7SSteffen Klassert unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); 166618f9bc7SSteffen Klassert 167618f9bc7SSteffen Klassert return mtu ? : dst->dev->mtu; 168ec831ea7SRoland Dreier } 169ec831ea7SRoland Dreier 17014e50e57SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu) 17114e50e57SDavid S. Miller { 17214e50e57SDavid S. Miller } 17314e50e57SDavid S. Miller 1740972ddb2SHeld Bernhard static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst, 1750972ddb2SHeld Bernhard unsigned long old) 1760972ddb2SHeld Bernhard { 1770972ddb2SHeld Bernhard return NULL; 1780972ddb2SHeld Bernhard } 1790972ddb2SHeld Bernhard 18014e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = { 18114e50e57SDavid S. Miller .family = AF_INET6, 18209640e63SHarvey Harrison .protocol = cpu_to_be16(ETH_P_IPV6), 18314e50e57SDavid S. Miller .destroy = ip6_dst_destroy, 18414e50e57SDavid S. Miller .check = ip6_dst_check, 185ebb762f2SSteffen Klassert .mtu = ip6_blackhole_mtu, 186214f45c9SEric Dumazet .default_advmss = ip6_default_advmss, 18714e50e57SDavid S. Miller .update_pmtu = ip6_rt_blackhole_update_pmtu, 1880972ddb2SHeld Bernhard .cow_metrics = ip6_rt_blackhole_cow_metrics, 189d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 19014e50e57SDavid S. Miller }; 19114e50e57SDavid S. Miller 19262fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = { 19362fa8a84SDavid S. Miller [RTAX_HOPLIMIT - 1] = 255, 19462fa8a84SDavid S. Miller }; 19562fa8a84SDavid S. Miller 196bdb3289fSDaniel Lezcano static struct rt6_info ip6_null_entry_template = { 1971da177e4SLinus Torvalds .dst = { 1981da177e4SLinus Torvalds .__refcnt = ATOMIC_INIT(1), 1991da177e4SLinus Torvalds .__use = 1, 2001da177e4SLinus Torvalds .obsolete = -1, 2011da177e4SLinus Torvalds .error = -ENETUNREACH, 2021da177e4SLinus Torvalds .input = ip6_pkt_discard, 2031da177e4SLinus Torvalds .output = ip6_pkt_discard_out, 2041da177e4SLinus Torvalds }, 2051da177e4SLinus Torvalds .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 2064f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 2071da177e4SLinus Torvalds .rt6i_metric = ~(u32) 0, 2081da177e4SLinus Torvalds .rt6i_ref = ATOMIC_INIT(1), 2091da177e4SLinus Torvalds }; 2101da177e4SLinus Torvalds 211101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES 212101367c2SThomas Graf 2136723ab54SDavid S. Miller static int ip6_pkt_prohibit(struct sk_buff *skb); 2146723ab54SDavid S. Miller static int ip6_pkt_prohibit_out(struct sk_buff *skb); 2156723ab54SDavid S. Miller 216280a34c8SAdrian Bunk static struct rt6_info ip6_prohibit_entry_template = { 217101367c2SThomas Graf .dst = { 218101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 219101367c2SThomas Graf .__use = 1, 220101367c2SThomas Graf .obsolete = -1, 221101367c2SThomas Graf .error = -EACCES, 2229ce8ade0SThomas Graf .input = ip6_pkt_prohibit, 2239ce8ade0SThomas Graf .output = ip6_pkt_prohibit_out, 224101367c2SThomas Graf }, 225101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 2264f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 227101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 228101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 229101367c2SThomas Graf }; 230101367c2SThomas Graf 231bdb3289fSDaniel Lezcano static struct rt6_info ip6_blk_hole_entry_template = { 232101367c2SThomas Graf .dst = { 233101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 234101367c2SThomas Graf .__use = 1, 235101367c2SThomas Graf .obsolete = -1, 236101367c2SThomas Graf .error = -EINVAL, 237352e512cSHerbert Xu .input = dst_discard, 238352e512cSHerbert Xu .output = dst_discard, 239101367c2SThomas Graf }, 240101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 2414f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 242101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 243101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 244101367c2SThomas Graf }; 245101367c2SThomas Graf 246101367c2SThomas Graf #endif 247101367c2SThomas Graf 2481da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */ 2495c1e6aa3SDavid S. Miller static inline struct rt6_info *ip6_dst_alloc(struct dst_ops *ops, 250957c665fSDavid S. Miller struct net_device *dev, 251957c665fSDavid S. Miller int flags) 2521da177e4SLinus Torvalds { 253957c665fSDavid S. Miller struct rt6_info *rt = dst_alloc(ops, dev, 0, 0, flags); 254cf911662SDavid S. Miller 25538308473SDavid S. Miller if (rt) 256fbe58186SMadalin Bucur memset(&rt->rt6i_table, 0, 257fbe58186SMadalin Bucur sizeof(*rt) - sizeof(struct dst_entry)); 258cf911662SDavid S. Miller 259cf911662SDavid S. Miller return rt; 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst) 2631da177e4SLinus Torvalds { 2641da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 2651da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 266b3419363SDavid S. Miller struct inet_peer *peer = rt->rt6i_peer; 2671da177e4SLinus Torvalds 2688e2ec639SYan, Zheng if (!(rt->dst.flags & DST_HOST)) 2698e2ec639SYan, Zheng dst_destroy_metrics_generic(dst); 2708e2ec639SYan, Zheng 27138308473SDavid S. Miller if (idev) { 2721da177e4SLinus Torvalds rt->rt6i_idev = NULL; 2731da177e4SLinus Torvalds in6_dev_put(idev); 2741da177e4SLinus Torvalds } 275b3419363SDavid S. Miller if (peer) { 276b3419363SDavid S. Miller rt->rt6i_peer = NULL; 277b3419363SDavid S. Miller inet_putpeer(peer); 278b3419363SDavid S. Miller } 279b3419363SDavid S. Miller } 280b3419363SDavid S. Miller 2816431cbc2SDavid S. Miller static atomic_t __rt6_peer_genid = ATOMIC_INIT(0); 2826431cbc2SDavid S. Miller 2836431cbc2SDavid S. Miller static u32 rt6_peer_genid(void) 2846431cbc2SDavid S. Miller { 2856431cbc2SDavid S. Miller return atomic_read(&__rt6_peer_genid); 2866431cbc2SDavid S. Miller } 2876431cbc2SDavid S. Miller 288b3419363SDavid S. Miller void rt6_bind_peer(struct rt6_info *rt, int create) 289b3419363SDavid S. Miller { 290b3419363SDavid S. Miller struct inet_peer *peer; 291b3419363SDavid S. Miller 292b3419363SDavid S. Miller peer = inet_getpeer_v6(&rt->rt6i_dst.addr, create); 293b3419363SDavid S. Miller if (peer && cmpxchg(&rt->rt6i_peer, NULL, peer) != NULL) 294b3419363SDavid S. Miller inet_putpeer(peer); 2956431cbc2SDavid S. Miller else 2966431cbc2SDavid S. Miller rt->rt6i_peer_genid = rt6_peer_genid(); 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 3001da177e4SLinus Torvalds int how) 3011da177e4SLinus Torvalds { 3021da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 3031da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 3045a3e55d6SDenis V. Lunev struct net_device *loopback_dev = 305c346dca1SYOSHIFUJI Hideaki dev_net(dev)->loopback_dev; 3061da177e4SLinus Torvalds 30738308473SDavid S. Miller if (dev != loopback_dev && idev && idev->dev == dev) { 3085a3e55d6SDenis V. Lunev struct inet6_dev *loopback_idev = 3095a3e55d6SDenis V. Lunev in6_dev_get(loopback_dev); 31038308473SDavid S. Miller if (loopback_idev) { 3111da177e4SLinus Torvalds rt->rt6i_idev = loopback_idev; 3121da177e4SLinus Torvalds in6_dev_put(idev); 3131da177e4SLinus Torvalds } 3141da177e4SLinus Torvalds } 3151da177e4SLinus Torvalds } 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds static __inline__ int rt6_check_expired(const struct rt6_info *rt) 3181da177e4SLinus Torvalds { 319a02cec21SEric Dumazet return (rt->rt6i_flags & RTF_EXPIRES) && 320d1918542SDavid S. Miller time_after(jiffies, rt->dst.expires); 3211da177e4SLinus Torvalds } 3221da177e4SLinus Torvalds 323b71d1d42SEric Dumazet static inline int rt6_need_strict(const struct in6_addr *daddr) 324c71099acSThomas Graf { 325a02cec21SEric Dumazet return ipv6_addr_type(daddr) & 326a02cec21SEric Dumazet (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK); 327c71099acSThomas Graf } 328c71099acSThomas Graf 3291da177e4SLinus Torvalds /* 330c71099acSThomas Graf * Route lookup. Any table->tb6_lock is implied. 3311da177e4SLinus Torvalds */ 3321da177e4SLinus Torvalds 3338ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net, 3348ed67789SDaniel Lezcano struct rt6_info *rt, 335b71d1d42SEric Dumazet const struct in6_addr *saddr, 3361da177e4SLinus Torvalds int oif, 337d420895eSYOSHIFUJI Hideaki int flags) 3381da177e4SLinus Torvalds { 3391da177e4SLinus Torvalds struct rt6_info *local = NULL; 3401da177e4SLinus Torvalds struct rt6_info *sprt; 3411da177e4SLinus Torvalds 342dd3abc4eSYOSHIFUJI Hideaki if (!oif && ipv6_addr_any(saddr)) 343dd3abc4eSYOSHIFUJI Hideaki goto out; 344dd3abc4eSYOSHIFUJI Hideaki 345d8d1f30bSChangli Gao for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) { 346d1918542SDavid S. Miller struct net_device *dev = sprt->dst.dev; 347dd3abc4eSYOSHIFUJI Hideaki 348dd3abc4eSYOSHIFUJI Hideaki if (oif) { 3491da177e4SLinus Torvalds if (dev->ifindex == oif) 3501da177e4SLinus Torvalds return sprt; 3511da177e4SLinus Torvalds if (dev->flags & IFF_LOOPBACK) { 35238308473SDavid S. Miller if (!sprt->rt6i_idev || 3531da177e4SLinus Torvalds sprt->rt6i_idev->dev->ifindex != oif) { 354d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE && oif) 3551da177e4SLinus Torvalds continue; 3561da177e4SLinus Torvalds if (local && (!oif || 3571da177e4SLinus Torvalds local->rt6i_idev->dev->ifindex == oif)) 3581da177e4SLinus Torvalds continue; 3591da177e4SLinus Torvalds } 3601da177e4SLinus Torvalds local = sprt; 3611da177e4SLinus Torvalds } 362dd3abc4eSYOSHIFUJI Hideaki } else { 363dd3abc4eSYOSHIFUJI Hideaki if (ipv6_chk_addr(net, saddr, dev, 364dd3abc4eSYOSHIFUJI Hideaki flags & RT6_LOOKUP_F_IFACE)) 365dd3abc4eSYOSHIFUJI Hideaki return sprt; 366dd3abc4eSYOSHIFUJI Hideaki } 3671da177e4SLinus Torvalds } 3681da177e4SLinus Torvalds 369dd3abc4eSYOSHIFUJI Hideaki if (oif) { 3701da177e4SLinus Torvalds if (local) 3711da177e4SLinus Torvalds return local; 3721da177e4SLinus Torvalds 373d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE) 3748ed67789SDaniel Lezcano return net->ipv6.ip6_null_entry; 3751da177e4SLinus Torvalds } 376dd3abc4eSYOSHIFUJI Hideaki out: 3771da177e4SLinus Torvalds return rt; 3781da177e4SLinus Torvalds } 3791da177e4SLinus Torvalds 38027097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 38127097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt) 38227097255SYOSHIFUJI Hideaki { 383f2c31e32SEric Dumazet struct neighbour *neigh; 38427097255SYOSHIFUJI Hideaki /* 38527097255SYOSHIFUJI Hideaki * Okay, this does not seem to be appropriate 38627097255SYOSHIFUJI Hideaki * for now, however, we need to check if it 38727097255SYOSHIFUJI Hideaki * is really so; aka Router Reachability Probing. 38827097255SYOSHIFUJI Hideaki * 38927097255SYOSHIFUJI Hideaki * Router Reachability Probe MUST be rate-limited 39027097255SYOSHIFUJI Hideaki * to no more than one per minute. 39127097255SYOSHIFUJI Hideaki */ 392f2c31e32SEric Dumazet rcu_read_lock(); 39327217455SDavid Miller neigh = rt ? dst_get_neighbour_noref(&rt->dst) : NULL; 39427097255SYOSHIFUJI Hideaki if (!neigh || (neigh->nud_state & NUD_VALID)) 395f2c31e32SEric Dumazet goto out; 39627097255SYOSHIFUJI Hideaki read_lock_bh(&neigh->lock); 39727097255SYOSHIFUJI Hideaki if (!(neigh->nud_state & NUD_VALID) && 39852e16356SYOSHIFUJI Hideaki time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) { 39927097255SYOSHIFUJI Hideaki struct in6_addr mcaddr; 40027097255SYOSHIFUJI Hideaki struct in6_addr *target; 40127097255SYOSHIFUJI Hideaki 40227097255SYOSHIFUJI Hideaki neigh->updated = jiffies; 40327097255SYOSHIFUJI Hideaki read_unlock_bh(&neigh->lock); 40427097255SYOSHIFUJI Hideaki 40527097255SYOSHIFUJI Hideaki target = (struct in6_addr *)&neigh->primary_key; 40627097255SYOSHIFUJI Hideaki addrconf_addr_solict_mult(target, &mcaddr); 407d1918542SDavid S. Miller ndisc_send_ns(rt->dst.dev, NULL, target, &mcaddr, NULL); 408f2c31e32SEric Dumazet } else { 40927097255SYOSHIFUJI Hideaki read_unlock_bh(&neigh->lock); 41027097255SYOSHIFUJI Hideaki } 411f2c31e32SEric Dumazet out: 412f2c31e32SEric Dumazet rcu_read_unlock(); 413f2c31e32SEric Dumazet } 41427097255SYOSHIFUJI Hideaki #else 41527097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt) 41627097255SYOSHIFUJI Hideaki { 41727097255SYOSHIFUJI Hideaki } 41827097255SYOSHIFUJI Hideaki #endif 41927097255SYOSHIFUJI Hideaki 4201da177e4SLinus Torvalds /* 421554cfb7eSYOSHIFUJI Hideaki * Default Router Selection (RFC 2461 6.3.6) 4221da177e4SLinus Torvalds */ 423b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif) 4241da177e4SLinus Torvalds { 425d1918542SDavid S. Miller struct net_device *dev = rt->dst.dev; 426161980f4SDavid S. Miller if (!oif || dev->ifindex == oif) 427554cfb7eSYOSHIFUJI Hideaki return 2; 428161980f4SDavid S. Miller if ((dev->flags & IFF_LOOPBACK) && 429161980f4SDavid S. Miller rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif) 430161980f4SDavid S. Miller return 1; 431554cfb7eSYOSHIFUJI Hideaki return 0; 4321da177e4SLinus Torvalds } 4331da177e4SLinus Torvalds 434b6f99a21SDave Jones static inline int rt6_check_neigh(struct rt6_info *rt) 4351da177e4SLinus Torvalds { 436f2c31e32SEric Dumazet struct neighbour *neigh; 437398bcbebSYOSHIFUJI Hideaki int m; 438f2c31e32SEric Dumazet 439f2c31e32SEric Dumazet rcu_read_lock(); 44027217455SDavid Miller neigh = dst_get_neighbour_noref(&rt->dst); 4414d0c5911SYOSHIFUJI Hideaki if (rt->rt6i_flags & RTF_NONEXTHOP || 4424d0c5911SYOSHIFUJI Hideaki !(rt->rt6i_flags & RTF_GATEWAY)) 4434d0c5911SYOSHIFUJI Hideaki m = 1; 4444d0c5911SYOSHIFUJI Hideaki else if (neigh) { 4451da177e4SLinus Torvalds read_lock_bh(&neigh->lock); 446554cfb7eSYOSHIFUJI Hideaki if (neigh->nud_state & NUD_VALID) 4474d0c5911SYOSHIFUJI Hideaki m = 2; 448398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 449398bcbebSYOSHIFUJI Hideaki else if (neigh->nud_state & NUD_FAILED) 450398bcbebSYOSHIFUJI Hideaki m = 0; 451398bcbebSYOSHIFUJI Hideaki #endif 452398bcbebSYOSHIFUJI Hideaki else 453ea73ee23SYOSHIFUJI Hideaki m = 1; 4541da177e4SLinus Torvalds read_unlock_bh(&neigh->lock); 455398bcbebSYOSHIFUJI Hideaki } else 456398bcbebSYOSHIFUJI Hideaki m = 0; 457f2c31e32SEric Dumazet rcu_read_unlock(); 458554cfb7eSYOSHIFUJI Hideaki return m; 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds 461554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif, 462554cfb7eSYOSHIFUJI Hideaki int strict) 463554cfb7eSYOSHIFUJI Hideaki { 4644d0c5911SYOSHIFUJI Hideaki int m, n; 4654d0c5911SYOSHIFUJI Hideaki 4664d0c5911SYOSHIFUJI Hideaki m = rt6_check_dev(rt, oif); 46777d16f45SYOSHIFUJI Hideaki if (!m && (strict & RT6_LOOKUP_F_IFACE)) 468554cfb7eSYOSHIFUJI Hideaki return -1; 469ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 470ebacaaa0SYOSHIFUJI Hideaki m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2; 471ebacaaa0SYOSHIFUJI Hideaki #endif 4724d0c5911SYOSHIFUJI Hideaki n = rt6_check_neigh(rt); 473557e92efSYOSHIFUJI Hideaki if (!n && (strict & RT6_LOOKUP_F_REACHABLE)) 474554cfb7eSYOSHIFUJI Hideaki return -1; 475554cfb7eSYOSHIFUJI Hideaki return m; 476554cfb7eSYOSHIFUJI Hideaki } 477554cfb7eSYOSHIFUJI Hideaki 478f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict, 479f11e6659SDavid S. Miller int *mpri, struct rt6_info *match) 480554cfb7eSYOSHIFUJI Hideaki { 481554cfb7eSYOSHIFUJI Hideaki int m; 482554cfb7eSYOSHIFUJI Hideaki 483554cfb7eSYOSHIFUJI Hideaki if (rt6_check_expired(rt)) 484f11e6659SDavid S. Miller goto out; 485554cfb7eSYOSHIFUJI Hideaki 486554cfb7eSYOSHIFUJI Hideaki m = rt6_score_route(rt, oif, strict); 487554cfb7eSYOSHIFUJI Hideaki if (m < 0) 488f11e6659SDavid S. Miller goto out; 489554cfb7eSYOSHIFUJI Hideaki 490f11e6659SDavid S. Miller if (m > *mpri) { 491ea659e07SYOSHIFUJI Hideaki if (strict & RT6_LOOKUP_F_REACHABLE) 49227097255SYOSHIFUJI Hideaki rt6_probe(match); 493f11e6659SDavid S. Miller *mpri = m; 494554cfb7eSYOSHIFUJI Hideaki match = rt; 495ea659e07SYOSHIFUJI Hideaki } else if (strict & RT6_LOOKUP_F_REACHABLE) { 49627097255SYOSHIFUJI Hideaki rt6_probe(rt); 4971da177e4SLinus Torvalds } 498f11e6659SDavid S. Miller 499f11e6659SDavid S. Miller out: 500f11e6659SDavid S. Miller return match; 5011da177e4SLinus Torvalds } 5021da177e4SLinus Torvalds 503f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn, 504f11e6659SDavid S. Miller struct rt6_info *rr_head, 505f11e6659SDavid S. Miller u32 metric, int oif, int strict) 506f11e6659SDavid S. Miller { 507f11e6659SDavid S. Miller struct rt6_info *rt, *match; 508f11e6659SDavid S. Miller int mpri = -1; 509f11e6659SDavid S. Miller 510f11e6659SDavid S. Miller match = NULL; 511f11e6659SDavid S. Miller for (rt = rr_head; rt && rt->rt6i_metric == metric; 512d8d1f30bSChangli Gao rt = rt->dst.rt6_next) 513f11e6659SDavid S. Miller match = find_match(rt, oif, strict, &mpri, match); 514f11e6659SDavid S. Miller for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric; 515d8d1f30bSChangli Gao rt = rt->dst.rt6_next) 516f11e6659SDavid S. Miller match = find_match(rt, oif, strict, &mpri, match); 517f11e6659SDavid S. Miller 518f11e6659SDavid S. Miller return match; 519f11e6659SDavid S. Miller } 520f11e6659SDavid S. Miller 521f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict) 522f11e6659SDavid S. Miller { 523f11e6659SDavid S. Miller struct rt6_info *match, *rt0; 5248ed67789SDaniel Lezcano struct net *net; 525f11e6659SDavid S. Miller 526f11e6659SDavid S. Miller rt0 = fn->rr_ptr; 527f11e6659SDavid S. Miller if (!rt0) 528f11e6659SDavid S. Miller fn->rr_ptr = rt0 = fn->leaf; 529f11e6659SDavid S. Miller 530f11e6659SDavid S. Miller match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict); 531f11e6659SDavid S. Miller 532554cfb7eSYOSHIFUJI Hideaki if (!match && 533f11e6659SDavid S. Miller (strict & RT6_LOOKUP_F_REACHABLE)) { 534d8d1f30bSChangli Gao struct rt6_info *next = rt0->dst.rt6_next; 535f11e6659SDavid S. Miller 536554cfb7eSYOSHIFUJI Hideaki /* no entries matched; do round-robin */ 537f11e6659SDavid S. Miller if (!next || next->rt6i_metric != rt0->rt6i_metric) 538f11e6659SDavid S. Miller next = fn->leaf; 539f11e6659SDavid S. Miller 540f11e6659SDavid S. Miller if (next != rt0) 541f11e6659SDavid S. Miller fn->rr_ptr = next; 542554cfb7eSYOSHIFUJI Hideaki } 543554cfb7eSYOSHIFUJI Hideaki 544d1918542SDavid S. Miller net = dev_net(rt0->dst.dev); 545a02cec21SEric Dumazet return match ? match : net->ipv6.ip6_null_entry; 5461da177e4SLinus Torvalds } 5471da177e4SLinus Torvalds 54870ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 54970ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 550b71d1d42SEric Dumazet const struct in6_addr *gwaddr) 55170ceb4f5SYOSHIFUJI Hideaki { 552c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 55370ceb4f5SYOSHIFUJI Hideaki struct route_info *rinfo = (struct route_info *) opt; 55470ceb4f5SYOSHIFUJI Hideaki struct in6_addr prefix_buf, *prefix; 55570ceb4f5SYOSHIFUJI Hideaki unsigned int pref; 5564bed72e4SYOSHIFUJI Hideaki unsigned long lifetime; 55770ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt; 55870ceb4f5SYOSHIFUJI Hideaki 55970ceb4f5SYOSHIFUJI Hideaki if (len < sizeof(struct route_info)) { 56070ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 56170ceb4f5SYOSHIFUJI Hideaki } 56270ceb4f5SYOSHIFUJI Hideaki 56370ceb4f5SYOSHIFUJI Hideaki /* Sanity check for prefix_len and length */ 56470ceb4f5SYOSHIFUJI Hideaki if (rinfo->length > 3) { 56570ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 56670ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 128) { 56770ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 56870ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 64) { 56970ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 2) { 57070ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 57170ceb4f5SYOSHIFUJI Hideaki } 57270ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 0) { 57370ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 1) { 57470ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 57570ceb4f5SYOSHIFUJI Hideaki } 57670ceb4f5SYOSHIFUJI Hideaki } 57770ceb4f5SYOSHIFUJI Hideaki 57870ceb4f5SYOSHIFUJI Hideaki pref = rinfo->route_pref; 57970ceb4f5SYOSHIFUJI Hideaki if (pref == ICMPV6_ROUTER_PREF_INVALID) 5803933fc95SJens Rosenboom return -EINVAL; 58170ceb4f5SYOSHIFUJI Hideaki 5824bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); 58370ceb4f5SYOSHIFUJI Hideaki 58470ceb4f5SYOSHIFUJI Hideaki if (rinfo->length == 3) 58570ceb4f5SYOSHIFUJI Hideaki prefix = (struct in6_addr *)rinfo->prefix; 58670ceb4f5SYOSHIFUJI Hideaki else { 58770ceb4f5SYOSHIFUJI Hideaki /* this function is safe */ 58870ceb4f5SYOSHIFUJI Hideaki ipv6_addr_prefix(&prefix_buf, 58970ceb4f5SYOSHIFUJI Hideaki (struct in6_addr *)rinfo->prefix, 59070ceb4f5SYOSHIFUJI Hideaki rinfo->prefix_len); 59170ceb4f5SYOSHIFUJI Hideaki prefix = &prefix_buf; 59270ceb4f5SYOSHIFUJI Hideaki } 59370ceb4f5SYOSHIFUJI Hideaki 594efa2cea0SDaniel Lezcano rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr, 595efa2cea0SDaniel Lezcano dev->ifindex); 59670ceb4f5SYOSHIFUJI Hideaki 59770ceb4f5SYOSHIFUJI Hideaki if (rt && !lifetime) { 598e0a1ad73SThomas Graf ip6_del_rt(rt); 59970ceb4f5SYOSHIFUJI Hideaki rt = NULL; 60070ceb4f5SYOSHIFUJI Hideaki } 60170ceb4f5SYOSHIFUJI Hideaki 60270ceb4f5SYOSHIFUJI Hideaki if (!rt && lifetime) 603efa2cea0SDaniel Lezcano rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex, 60470ceb4f5SYOSHIFUJI Hideaki pref); 60570ceb4f5SYOSHIFUJI Hideaki else if (rt) 60670ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags = RTF_ROUTEINFO | 60770ceb4f5SYOSHIFUJI Hideaki (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 60870ceb4f5SYOSHIFUJI Hideaki 60970ceb4f5SYOSHIFUJI Hideaki if (rt) { 6104bed72e4SYOSHIFUJI Hideaki if (!addrconf_finite_timeout(lifetime)) { 61170ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags &= ~RTF_EXPIRES; 61270ceb4f5SYOSHIFUJI Hideaki } else { 613d1918542SDavid S. Miller rt->dst.expires = jiffies + HZ * lifetime; 61470ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_EXPIRES; 61570ceb4f5SYOSHIFUJI Hideaki } 616d8d1f30bSChangli Gao dst_release(&rt->dst); 61770ceb4f5SYOSHIFUJI Hideaki } 61870ceb4f5SYOSHIFUJI Hideaki return 0; 61970ceb4f5SYOSHIFUJI Hideaki } 62070ceb4f5SYOSHIFUJI Hideaki #endif 62170ceb4f5SYOSHIFUJI Hideaki 6228ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr) \ 623982f56f3SYOSHIFUJI Hideaki do { \ 6248ed67789SDaniel Lezcano if (rt == __net->ipv6.ip6_null_entry) { \ 625982f56f3SYOSHIFUJI Hideaki struct fib6_node *pn; \ 626e0eda7bbSVille Nuorvala while (1) { \ 627982f56f3SYOSHIFUJI Hideaki if (fn->fn_flags & RTN_TL_ROOT) \ 628c71099acSThomas Graf goto out; \ 629982f56f3SYOSHIFUJI Hideaki pn = fn->parent; \ 630982f56f3SYOSHIFUJI Hideaki if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \ 6318bce65b9SKim Nordlund fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \ 632982f56f3SYOSHIFUJI Hideaki else \ 633982f56f3SYOSHIFUJI Hideaki fn = pn; \ 634c71099acSThomas Graf if (fn->fn_flags & RTN_RTINFO) \ 635c71099acSThomas Graf goto restart; \ 636c71099acSThomas Graf } \ 637982f56f3SYOSHIFUJI Hideaki } \ 638982f56f3SYOSHIFUJI Hideaki } while (0) 639c71099acSThomas Graf 6408ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net, 6418ed67789SDaniel Lezcano struct fib6_table *table, 6424c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 6431da177e4SLinus Torvalds { 6441da177e4SLinus Torvalds struct fib6_node *fn; 6451da177e4SLinus Torvalds struct rt6_info *rt; 6461da177e4SLinus Torvalds 647c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 6484c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 649c71099acSThomas Graf restart: 650c71099acSThomas Graf rt = fn->leaf; 6514c9483b2SDavid S. Miller rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags); 6524c9483b2SDavid S. Miller BACKTRACK(net, &fl6->saddr); 653c71099acSThomas Graf out: 654d8d1f30bSChangli Gao dst_use(&rt->dst, jiffies); 655c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 6561da177e4SLinus Torvalds return rt; 657c71099acSThomas Graf 658c71099acSThomas Graf } 659c71099acSThomas Graf 660ea6e574eSFlorian Westphal struct dst_entry * ip6_route_lookup(struct net *net, struct flowi6 *fl6, 661ea6e574eSFlorian Westphal int flags) 662ea6e574eSFlorian Westphal { 663ea6e574eSFlorian Westphal return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup); 664ea6e574eSFlorian Westphal } 665ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup); 666ea6e574eSFlorian Westphal 6679acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, 6689acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *saddr, int oif, int strict) 669c71099acSThomas Graf { 6704c9483b2SDavid S. Miller struct flowi6 fl6 = { 6714c9483b2SDavid S. Miller .flowi6_oif = oif, 6724c9483b2SDavid S. Miller .daddr = *daddr, 673c71099acSThomas Graf }; 674c71099acSThomas Graf struct dst_entry *dst; 67577d16f45SYOSHIFUJI Hideaki int flags = strict ? RT6_LOOKUP_F_IFACE : 0; 676c71099acSThomas Graf 677adaa70bbSThomas Graf if (saddr) { 6784c9483b2SDavid S. Miller memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 679adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 680adaa70bbSThomas Graf } 681adaa70bbSThomas Graf 6824c9483b2SDavid S. Miller dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup); 683c71099acSThomas Graf if (dst->error == 0) 684c71099acSThomas Graf return (struct rt6_info *) dst; 685c71099acSThomas Graf 686c71099acSThomas Graf dst_release(dst); 687c71099acSThomas Graf 6881da177e4SLinus Torvalds return NULL; 6891da177e4SLinus Torvalds } 6901da177e4SLinus Torvalds 6917159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup); 6927159039aSYOSHIFUJI Hideaki 693c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock. 6941da177e4SLinus Torvalds It takes new route entry, the addition fails by any reason the 6951da177e4SLinus Torvalds route is freed. In any case, if caller does not hold it, it may 6961da177e4SLinus Torvalds be destroyed. 6971da177e4SLinus Torvalds */ 6981da177e4SLinus Torvalds 69986872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info) 7001da177e4SLinus Torvalds { 7011da177e4SLinus Torvalds int err; 702c71099acSThomas Graf struct fib6_table *table; 7031da177e4SLinus Torvalds 704c71099acSThomas Graf table = rt->rt6i_table; 705c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 70686872cb5SThomas Graf err = fib6_add(&table->tb6_root, rt, info); 707c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds return err; 7101da177e4SLinus Torvalds } 7111da177e4SLinus Torvalds 71240e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt) 71340e22e8fSThomas Graf { 7144d1169c1SDenis V. Lunev struct nl_info info = { 715d1918542SDavid S. Miller .nl_net = dev_net(rt->dst.dev), 7164d1169c1SDenis V. Lunev }; 717528c4cebSDenis V. Lunev return __ip6_ins_rt(rt, &info); 71840e22e8fSThomas Graf } 71940e22e8fSThomas Graf 72021efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_cow(const struct rt6_info *ort, 72121efcfa0SEric Dumazet const struct in6_addr *daddr, 722b71d1d42SEric Dumazet const struct in6_addr *saddr) 7231da177e4SLinus Torvalds { 7241da177e4SLinus Torvalds struct rt6_info *rt; 7251da177e4SLinus Torvalds 7261da177e4SLinus Torvalds /* 7271da177e4SLinus Torvalds * Clone the route. 7281da177e4SLinus Torvalds */ 7291da177e4SLinus Torvalds 73021efcfa0SEric Dumazet rt = ip6_rt_copy(ort, daddr); 7311da177e4SLinus Torvalds 7321da177e4SLinus Torvalds if (rt) { 73314deae41SDavid S. Miller int attempts = !in_softirq(); 73414deae41SDavid S. Miller 73558c4fb86SYOSHIFUJI Hideaki if (!(rt->rt6i_flags & RTF_GATEWAY)) { 736bb3c3686SDavid S. Miller if (ort->rt6i_dst.plen != 128 && 73721efcfa0SEric Dumazet ipv6_addr_equal(&ort->rt6i_dst.addr, daddr)) 73858c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 7394e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = *daddr; 74058c4fb86SYOSHIFUJI Hideaki } 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds rt->rt6i_flags |= RTF_CACHE; 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 7451da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 7464e3fd7a0SAlexey Dobriyan rt->rt6i_src.addr = *saddr; 7471da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 7481da177e4SLinus Torvalds } 7491da177e4SLinus Torvalds #endif 7501da177e4SLinus Torvalds 75114deae41SDavid S. Miller retry: 752*8ade06c6SDavid S. Miller if (rt6_bind_neighbour(rt, rt->dst.dev)) { 753d1918542SDavid S. Miller struct net *net = dev_net(rt->dst.dev); 75414deae41SDavid S. Miller int saved_rt_min_interval = 75514deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_min_interval; 75614deae41SDavid S. Miller int saved_rt_elasticity = 75714deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_elasticity; 75814deae41SDavid S. Miller 75914deae41SDavid S. Miller if (attempts-- > 0) { 76014deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_elasticity = 1; 76114deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_min_interval = 0; 76214deae41SDavid S. Miller 76386393e52SAlexey Dobriyan ip6_dst_gc(&net->ipv6.ip6_dst_ops); 76414deae41SDavid S. Miller 76514deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_elasticity = 76614deae41SDavid S. Miller saved_rt_elasticity; 76714deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_min_interval = 76814deae41SDavid S. Miller saved_rt_min_interval; 76914deae41SDavid S. Miller goto retry; 77014deae41SDavid S. Miller } 77114deae41SDavid S. Miller 77214deae41SDavid S. Miller if (net_ratelimit()) 77314deae41SDavid S. Miller printk(KERN_WARNING 7747e1b33e5SUlrich Weber "ipv6: Neighbour table overflow.\n"); 775d8d1f30bSChangli Gao dst_free(&rt->dst); 77614deae41SDavid S. Miller return NULL; 77714deae41SDavid S. Miller } 77895a9a5baSYOSHIFUJI Hideaki } 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds return rt; 7811da177e4SLinus Torvalds } 78295a9a5baSYOSHIFUJI Hideaki 78321efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, 78421efcfa0SEric Dumazet const struct in6_addr *daddr) 785299d9939SYOSHIFUJI Hideaki { 78621efcfa0SEric Dumazet struct rt6_info *rt = ip6_rt_copy(ort, daddr); 78721efcfa0SEric Dumazet 788299d9939SYOSHIFUJI Hideaki if (rt) { 789299d9939SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_CACHE; 79027217455SDavid Miller dst_set_neighbour(&rt->dst, neigh_clone(dst_get_neighbour_noref_raw(&ort->dst))); 791299d9939SYOSHIFUJI Hideaki } 792299d9939SYOSHIFUJI Hideaki return rt; 793299d9939SYOSHIFUJI Hideaki } 794299d9939SYOSHIFUJI Hideaki 7958ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif, 7964c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 7971da177e4SLinus Torvalds { 7981da177e4SLinus Torvalds struct fib6_node *fn; 799519fbd87SYOSHIFUJI Hideaki struct rt6_info *rt, *nrt; 800c71099acSThomas Graf int strict = 0; 8011da177e4SLinus Torvalds int attempts = 3; 802519fbd87SYOSHIFUJI Hideaki int err; 80353b7997fSYOSHIFUJI Hideaki int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE; 8041da177e4SLinus Torvalds 80577d16f45SYOSHIFUJI Hideaki strict |= flags & RT6_LOOKUP_F_IFACE; 8061da177e4SLinus Torvalds 8071da177e4SLinus Torvalds relookup: 808c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 8091da177e4SLinus Torvalds 8108238dd06SYOSHIFUJI Hideaki restart_2: 8114c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds restart: 8144acad72dSPavel Emelyanov rt = rt6_select(fn, oif, strict | reachable); 8158ed67789SDaniel Lezcano 8164c9483b2SDavid S. Miller BACKTRACK(net, &fl6->saddr); 8178ed67789SDaniel Lezcano if (rt == net->ipv6.ip6_null_entry || 8188238dd06SYOSHIFUJI Hideaki rt->rt6i_flags & RTF_CACHE) 8191da177e4SLinus Torvalds goto out; 8201da177e4SLinus Torvalds 821d8d1f30bSChangli Gao dst_hold(&rt->dst); 822c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 8231da177e4SLinus Torvalds 82427217455SDavid Miller if (!dst_get_neighbour_noref_raw(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP)) 8254c9483b2SDavid S. Miller nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr); 8267343ff31SDavid S. Miller else if (!(rt->dst.flags & DST_HOST)) 8274c9483b2SDavid S. Miller nrt = rt6_alloc_clone(rt, &fl6->daddr); 8287343ff31SDavid S. Miller else 8297343ff31SDavid S. Miller goto out2; 8301da177e4SLinus Torvalds 831d8d1f30bSChangli Gao dst_release(&rt->dst); 8328ed67789SDaniel Lezcano rt = nrt ? : net->ipv6.ip6_null_entry; 8331da177e4SLinus Torvalds 834d8d1f30bSChangli Gao dst_hold(&rt->dst); 835e40cf353SYOSHIFUJI Hideaki if (nrt) { 83640e22e8fSThomas Graf err = ip6_ins_rt(nrt); 837e40cf353SYOSHIFUJI Hideaki if (!err) 838e40cf353SYOSHIFUJI Hideaki goto out2; 839e40cf353SYOSHIFUJI Hideaki } 840e40cf353SYOSHIFUJI Hideaki 841e40cf353SYOSHIFUJI Hideaki if (--attempts <= 0) 8421da177e4SLinus Torvalds goto out2; 8431da177e4SLinus Torvalds 844519fbd87SYOSHIFUJI Hideaki /* 845c71099acSThomas Graf * Race condition! In the gap, when table->tb6_lock was 846519fbd87SYOSHIFUJI Hideaki * released someone could insert this route. Relookup. 8471da177e4SLinus Torvalds */ 848d8d1f30bSChangli Gao dst_release(&rt->dst); 8491da177e4SLinus Torvalds goto relookup; 850e40cf353SYOSHIFUJI Hideaki 851519fbd87SYOSHIFUJI Hideaki out: 8528238dd06SYOSHIFUJI Hideaki if (reachable) { 8538238dd06SYOSHIFUJI Hideaki reachable = 0; 8548238dd06SYOSHIFUJI Hideaki goto restart_2; 8558238dd06SYOSHIFUJI Hideaki } 856d8d1f30bSChangli Gao dst_hold(&rt->dst); 857c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 8581da177e4SLinus Torvalds out2: 859d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 860d8d1f30bSChangli Gao rt->dst.__use++; 861c71099acSThomas Graf 862c71099acSThomas Graf return rt; 863c71099acSThomas Graf } 864c71099acSThomas Graf 8658ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table, 8664c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 8674acad72dSPavel Emelyanov { 8684c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags); 8694acad72dSPavel Emelyanov } 8704acad72dSPavel Emelyanov 871c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 872c71099acSThomas Graf { 873b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 874c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 875adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 8764c9483b2SDavid S. Miller struct flowi6 fl6 = { 8774c9483b2SDavid S. Miller .flowi6_iif = skb->dev->ifindex, 8784c9483b2SDavid S. Miller .daddr = iph->daddr, 8794c9483b2SDavid S. Miller .saddr = iph->saddr, 8804c9483b2SDavid S. Miller .flowlabel = (* (__be32 *) iph) & IPV6_FLOWINFO_MASK, 8814c9483b2SDavid S. Miller .flowi6_mark = skb->mark, 8824c9483b2SDavid S. Miller .flowi6_proto = iph->nexthdr, 883c71099acSThomas Graf }; 884adaa70bbSThomas Graf 8851d6e55f1SThomas Goff if (rt6_need_strict(&iph->daddr) && skb->dev->type != ARPHRD_PIMREG) 886adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_IFACE; 887c71099acSThomas Graf 8884c9483b2SDavid S. Miller skb_dst_set(skb, fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_input)); 889c71099acSThomas Graf } 890c71099acSThomas Graf 8918ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table, 8924c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 893c71099acSThomas Graf { 8944c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags); 895c71099acSThomas Graf } 896c71099acSThomas Graf 8979c7a4f9cSFlorian Westphal struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk, 8984c9483b2SDavid S. Miller struct flowi6 *fl6) 899c71099acSThomas Graf { 900c71099acSThomas Graf int flags = 0; 901c71099acSThomas Graf 9024c9483b2SDavid S. Miller if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr)) 90377d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 904c71099acSThomas Graf 9054c9483b2SDavid S. Miller if (!ipv6_addr_any(&fl6->saddr)) 906adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 9070c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 9080c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 909adaa70bbSThomas Graf 9104c9483b2SDavid S. Miller return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output); 9111da177e4SLinus Torvalds } 9121da177e4SLinus Torvalds 9137159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output); 9141da177e4SLinus Torvalds 9152774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 91614e50e57SDavid S. Miller { 9175c1e6aa3SDavid S. Miller struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; 91814e50e57SDavid S. Miller struct dst_entry *new = NULL; 91914e50e57SDavid S. Miller 9205c1e6aa3SDavid S. Miller rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, 0, 0); 92114e50e57SDavid S. Miller if (rt) { 922cf911662SDavid S. Miller memset(&rt->rt6i_table, 0, sizeof(*rt) - sizeof(struct dst_entry)); 923cf911662SDavid S. Miller 924d8d1f30bSChangli Gao new = &rt->dst; 92514e50e57SDavid S. Miller 92614e50e57SDavid S. Miller new->__use = 1; 927352e512cSHerbert Xu new->input = dst_discard; 928352e512cSHerbert Xu new->output = dst_discard; 92914e50e57SDavid S. Miller 93021efcfa0SEric Dumazet if (dst_metrics_read_only(&ort->dst)) 93121efcfa0SEric Dumazet new->_metrics = ort->dst._metrics; 93221efcfa0SEric Dumazet else 933defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 93414e50e57SDavid S. Miller rt->rt6i_idev = ort->rt6i_idev; 93514e50e57SDavid S. Miller if (rt->rt6i_idev) 93614e50e57SDavid S. Miller in6_dev_hold(rt->rt6i_idev); 937d1918542SDavid S. Miller rt->dst.expires = 0; 93814e50e57SDavid S. Miller 9394e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 94014e50e57SDavid S. Miller rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES; 94114e50e57SDavid S. Miller rt->rt6i_metric = 0; 94214e50e57SDavid S. Miller 94314e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 94414e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 94514e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 94614e50e57SDavid S. Miller #endif 94714e50e57SDavid S. Miller 94814e50e57SDavid S. Miller dst_free(new); 94914e50e57SDavid S. Miller } 95014e50e57SDavid S. Miller 95169ead7afSDavid S. Miller dst_release(dst_orig); 95269ead7afSDavid S. Miller return new ? new : ERR_PTR(-ENOMEM); 95314e50e57SDavid S. Miller } 95414e50e57SDavid S. Miller 9551da177e4SLinus Torvalds /* 9561da177e4SLinus Torvalds * Destination cache support functions 9571da177e4SLinus Torvalds */ 9581da177e4SLinus Torvalds 9591da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 9601da177e4SLinus Torvalds { 9611da177e4SLinus Torvalds struct rt6_info *rt; 9621da177e4SLinus Torvalds 9631da177e4SLinus Torvalds rt = (struct rt6_info *) dst; 9641da177e4SLinus Torvalds 9656431cbc2SDavid S. Miller if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) { 9666431cbc2SDavid S. Miller if (rt->rt6i_peer_genid != rt6_peer_genid()) { 9676431cbc2SDavid S. Miller if (!rt->rt6i_peer) 9686431cbc2SDavid S. Miller rt6_bind_peer(rt, 0); 9696431cbc2SDavid S. Miller rt->rt6i_peer_genid = rt6_peer_genid(); 9706431cbc2SDavid S. Miller } 9711da177e4SLinus Torvalds return dst; 9726431cbc2SDavid S. Miller } 9731da177e4SLinus Torvalds return NULL; 9741da177e4SLinus Torvalds } 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 9771da177e4SLinus Torvalds { 9781da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds if (rt) { 98154c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 98254c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 983e0a1ad73SThomas Graf ip6_del_rt(rt); 98454c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 9851da177e4SLinus Torvalds } 98654c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 98754c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 98854c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 98954c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 99054c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 99154c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 9921da177e4SLinus Torvalds } 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 9951da177e4SLinus Torvalds { 9961da177e4SLinus Torvalds struct rt6_info *rt; 9971da177e4SLinus Torvalds 9983ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 9991da177e4SLinus Torvalds 1000adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 10011da177e4SLinus Torvalds if (rt) { 10021da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) { 1003d8d1f30bSChangli Gao dst_set_expires(&rt->dst, 0); 10041da177e4SLinus Torvalds rt->rt6i_flags |= RTF_EXPIRES; 10051da177e4SLinus Torvalds } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) 10061da177e4SLinus Torvalds rt->rt6i_node->fn_sernum = -1; 10071da177e4SLinus Torvalds } 10081da177e4SLinus Torvalds } 10091da177e4SLinus Torvalds 10101da177e4SLinus Torvalds static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu) 10111da177e4SLinus Torvalds { 10121da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info*)dst; 10131da177e4SLinus Torvalds 10141da177e4SLinus Torvalds if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) { 10151da177e4SLinus Torvalds rt6->rt6i_flags |= RTF_MODIFIED; 10161da177e4SLinus Torvalds if (mtu < IPV6_MIN_MTU) { 1017defb3519SDavid S. Miller u32 features = dst_metric(dst, RTAX_FEATURES); 10181da177e4SLinus Torvalds mtu = IPV6_MIN_MTU; 1019defb3519SDavid S. Miller features |= RTAX_FEATURE_ALLFRAG; 1020defb3519SDavid S. Miller dst_metric_set(dst, RTAX_FEATURES, features); 10211da177e4SLinus Torvalds } 1022defb3519SDavid S. Miller dst_metric_set(dst, RTAX_MTU, mtu); 10231da177e4SLinus Torvalds } 10241da177e4SLinus Torvalds } 10251da177e4SLinus Torvalds 10260dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 10271da177e4SLinus Torvalds { 10280dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 10290dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 10300dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 10310dbaee3bSDavid S. Miller 10321da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 10331da177e4SLinus Torvalds 10345578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 10355578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 10361da177e4SLinus Torvalds 10371da177e4SLinus Torvalds /* 10381da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 10391da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 10401da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 10411da177e4SLinus Torvalds * rely only on pmtu discovery" 10421da177e4SLinus Torvalds */ 10431da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 10441da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 10451da177e4SLinus Torvalds return mtu; 10461da177e4SLinus Torvalds } 10471da177e4SLinus Torvalds 1048ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst) 1049d33e4553SDavid S. Miller { 1050d33e4553SDavid S. Miller struct inet6_dev *idev; 1051618f9bc7SSteffen Klassert unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); 1052618f9bc7SSteffen Klassert 1053618f9bc7SSteffen Klassert if (mtu) 1054618f9bc7SSteffen Klassert return mtu; 1055618f9bc7SSteffen Klassert 1056618f9bc7SSteffen Klassert mtu = IPV6_MIN_MTU; 1057d33e4553SDavid S. Miller 1058d33e4553SDavid S. Miller rcu_read_lock(); 1059d33e4553SDavid S. Miller idev = __in6_dev_get(dst->dev); 1060d33e4553SDavid S. Miller if (idev) 1061d33e4553SDavid S. Miller mtu = idev->cnf.mtu6; 1062d33e4553SDavid S. Miller rcu_read_unlock(); 1063d33e4553SDavid S. Miller 1064d33e4553SDavid S. Miller return mtu; 1065d33e4553SDavid S. Miller } 1066d33e4553SDavid S. Miller 10673b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list; 10683b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock); 10695d0bbeebSThomas Graf 10703b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 10711da177e4SLinus Torvalds struct neighbour *neigh, 107287a11578SDavid S. Miller struct flowi6 *fl6) 10731da177e4SLinus Torvalds { 107487a11578SDavid S. Miller struct dst_entry *dst; 10751da177e4SLinus Torvalds struct rt6_info *rt; 10761da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 1077c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 10781da177e4SLinus Torvalds 107938308473SDavid S. Miller if (unlikely(!idev)) 10801da177e4SLinus Torvalds return NULL; 10811da177e4SLinus Torvalds 1082957c665fSDavid S. Miller rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, dev, 0); 108338308473SDavid S. Miller if (unlikely(!rt)) { 10841da177e4SLinus Torvalds in6_dev_put(idev); 108587a11578SDavid S. Miller dst = ERR_PTR(-ENOMEM); 10861da177e4SLinus Torvalds goto out; 10871da177e4SLinus Torvalds } 10881da177e4SLinus Torvalds 10891da177e4SLinus Torvalds if (neigh) 10901da177e4SLinus Torvalds neigh_hold(neigh); 109114deae41SDavid S. Miller else { 1092f83c7790SDavid S. Miller neigh = ip6_neigh_lookup(&rt->dst, &fl6->daddr); 1093b43faac6SDavid S. Miller if (IS_ERR(neigh)) { 1094b43faac6SDavid S. Miller dst_free(&rt->dst); 1095b43faac6SDavid S. Miller return ERR_CAST(neigh); 1096b43faac6SDavid S. Miller } 109714deae41SDavid S. Miller } 10981da177e4SLinus Torvalds 10998e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 11008e2ec639SYan, Zheng rt->dst.output = ip6_output; 110169cce1d1SDavid S. Miller dst_set_neighbour(&rt->dst, neigh); 1102d8d1f30bSChangli Gao atomic_set(&rt->dst.__refcnt, 1); 110387a11578SDavid S. Miller rt->rt6i_dst.addr = fl6->daddr; 11048e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 11058e2ec639SYan, Zheng rt->rt6i_idev = idev; 11067011687fSGao feng dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255); 11071da177e4SLinus Torvalds 11083b00944cSYOSHIFUJI Hideaki spin_lock_bh(&icmp6_dst_lock); 1109d8d1f30bSChangli Gao rt->dst.next = icmp6_dst_gc_list; 1110d8d1f30bSChangli Gao icmp6_dst_gc_list = &rt->dst; 11113b00944cSYOSHIFUJI Hideaki spin_unlock_bh(&icmp6_dst_lock); 11121da177e4SLinus Torvalds 11135578689aSDaniel Lezcano fib6_force_start_gc(net); 11141da177e4SLinus Torvalds 111587a11578SDavid S. Miller dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); 111687a11578SDavid S. Miller 11171da177e4SLinus Torvalds out: 111887a11578SDavid S. Miller return dst; 11191da177e4SLinus Torvalds } 11201da177e4SLinus Torvalds 11213d0f24a7SStephen Hemminger int icmp6_dst_gc(void) 11221da177e4SLinus Torvalds { 1123e9476e95SHagen Paul Pfeifer struct dst_entry *dst, **pprev; 11243d0f24a7SStephen Hemminger int more = 0; 11251da177e4SLinus Torvalds 11263b00944cSYOSHIFUJI Hideaki spin_lock_bh(&icmp6_dst_lock); 11273b00944cSYOSHIFUJI Hideaki pprev = &icmp6_dst_gc_list; 11285d0bbeebSThomas Graf 11291da177e4SLinus Torvalds while ((dst = *pprev) != NULL) { 11301da177e4SLinus Torvalds if (!atomic_read(&dst->__refcnt)) { 11311da177e4SLinus Torvalds *pprev = dst->next; 11321da177e4SLinus Torvalds dst_free(dst); 11331da177e4SLinus Torvalds } else { 11341da177e4SLinus Torvalds pprev = &dst->next; 11353d0f24a7SStephen Hemminger ++more; 11361da177e4SLinus Torvalds } 11371da177e4SLinus Torvalds } 11381da177e4SLinus Torvalds 11393b00944cSYOSHIFUJI Hideaki spin_unlock_bh(&icmp6_dst_lock); 11405d0bbeebSThomas Graf 11413d0f24a7SStephen Hemminger return more; 11421da177e4SLinus Torvalds } 11431da177e4SLinus Torvalds 11441e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg), 11451e493d19SDavid S. Miller void *arg) 11461e493d19SDavid S. Miller { 11471e493d19SDavid S. Miller struct dst_entry *dst, **pprev; 11481e493d19SDavid S. Miller 11491e493d19SDavid S. Miller spin_lock_bh(&icmp6_dst_lock); 11501e493d19SDavid S. Miller pprev = &icmp6_dst_gc_list; 11511e493d19SDavid S. Miller while ((dst = *pprev) != NULL) { 11521e493d19SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 11531e493d19SDavid S. Miller if (func(rt, arg)) { 11541e493d19SDavid S. Miller *pprev = dst->next; 11551e493d19SDavid S. Miller dst_free(dst); 11561e493d19SDavid S. Miller } else { 11571e493d19SDavid S. Miller pprev = &dst->next; 11581e493d19SDavid S. Miller } 11591e493d19SDavid S. Miller } 11601e493d19SDavid S. Miller spin_unlock_bh(&icmp6_dst_lock); 11611e493d19SDavid S. Miller } 11621e493d19SDavid S. Miller 1163569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops) 11641da177e4SLinus Torvalds { 11651da177e4SLinus Torvalds unsigned long now = jiffies; 116686393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 11677019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 11687019b78eSDaniel Lezcano int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; 11697019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 11707019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 11717019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 1172fc66f95cSEric Dumazet int entries; 11731da177e4SLinus Torvalds 1174fc66f95cSEric Dumazet entries = dst_entries_get_fast(ops); 11757019b78eSDaniel Lezcano if (time_after(rt_last_gc + rt_min_interval, now) && 1176fc66f95cSEric Dumazet entries <= rt_max_size) 11771da177e4SLinus Torvalds goto out; 11781da177e4SLinus Torvalds 11796891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire++; 11806891a346SBenjamin Thery fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net); 11816891a346SBenjamin Thery net->ipv6.ip6_rt_last_gc = now; 1182fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 1183fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 11847019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; 11851da177e4SLinus Torvalds out: 11867019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; 1187fc66f95cSEric Dumazet return entries > rt_max_size; 11881da177e4SLinus Torvalds } 11891da177e4SLinus Torvalds 11901da177e4SLinus Torvalds /* Clean host part of a prefix. Not necessary in radix tree, 11911da177e4SLinus Torvalds but results in cleaner routing tables. 11921da177e4SLinus Torvalds 11931da177e4SLinus Torvalds Remove it only when all the things will work! 11941da177e4SLinus Torvalds */ 11951da177e4SLinus Torvalds 11966b75d090SYOSHIFUJI Hideaki int ip6_dst_hoplimit(struct dst_entry *dst) 11971da177e4SLinus Torvalds { 11985170ae82SDavid S. Miller int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT); 1199a02e4b7dSDavid S. Miller if (hoplimit == 0) { 12006b75d090SYOSHIFUJI Hideaki struct net_device *dev = dst->dev; 1201c68f24ccSEric Dumazet struct inet6_dev *idev; 1202c68f24ccSEric Dumazet 1203c68f24ccSEric Dumazet rcu_read_lock(); 1204c68f24ccSEric Dumazet idev = __in6_dev_get(dev); 1205c68f24ccSEric Dumazet if (idev) 12061da177e4SLinus Torvalds hoplimit = idev->cnf.hop_limit; 1207c68f24ccSEric Dumazet else 120853b7997fSYOSHIFUJI Hideaki hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit; 1209c68f24ccSEric Dumazet rcu_read_unlock(); 12101da177e4SLinus Torvalds } 12111da177e4SLinus Torvalds return hoplimit; 12121da177e4SLinus Torvalds } 1213abbf46aeSDavid S. Miller EXPORT_SYMBOL(ip6_dst_hoplimit); 12141da177e4SLinus Torvalds 12151da177e4SLinus Torvalds /* 12161da177e4SLinus Torvalds * 12171da177e4SLinus Torvalds */ 12181da177e4SLinus Torvalds 121986872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg) 12201da177e4SLinus Torvalds { 12211da177e4SLinus Torvalds int err; 12225578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 12231da177e4SLinus Torvalds struct rt6_info *rt = NULL; 12241da177e4SLinus Torvalds struct net_device *dev = NULL; 12251da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 1226c71099acSThomas Graf struct fib6_table *table; 12271da177e4SLinus Torvalds int addr_type; 12281da177e4SLinus Torvalds 122986872cb5SThomas Graf if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128) 12301da177e4SLinus Torvalds return -EINVAL; 12311da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 123286872cb5SThomas Graf if (cfg->fc_src_len) 12331da177e4SLinus Torvalds return -EINVAL; 12341da177e4SLinus Torvalds #endif 123586872cb5SThomas Graf if (cfg->fc_ifindex) { 12361da177e4SLinus Torvalds err = -ENODEV; 12375578689aSDaniel Lezcano dev = dev_get_by_index(net, cfg->fc_ifindex); 12381da177e4SLinus Torvalds if (!dev) 12391da177e4SLinus Torvalds goto out; 12401da177e4SLinus Torvalds idev = in6_dev_get(dev); 12411da177e4SLinus Torvalds if (!idev) 12421da177e4SLinus Torvalds goto out; 12431da177e4SLinus Torvalds } 12441da177e4SLinus Torvalds 124586872cb5SThomas Graf if (cfg->fc_metric == 0) 124686872cb5SThomas Graf cfg->fc_metric = IP6_RT_PRIO_USER; 12471da177e4SLinus Torvalds 1248c71099acSThomas Graf err = -ENOBUFS; 124938308473SDavid S. Miller if (cfg->fc_nlinfo.nlh && 1250d71314b4SMatti Vaittinen !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { 1251d71314b4SMatti Vaittinen table = fib6_get_table(net, cfg->fc_table); 125238308473SDavid S. Miller if (!table) { 1253d71314b4SMatti Vaittinen printk(KERN_WARNING "IPv6: NLM_F_CREATE should be specified when creating new route\n"); 1254d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 1255d71314b4SMatti Vaittinen } 1256d71314b4SMatti Vaittinen } else { 1257d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 1258d71314b4SMatti Vaittinen } 125938308473SDavid S. Miller 126038308473SDavid S. Miller if (!table) 1261c71099acSThomas Graf goto out; 1262c71099acSThomas Graf 1263957c665fSDavid S. Miller rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, NULL, DST_NOCOUNT); 12641da177e4SLinus Torvalds 126538308473SDavid S. Miller if (!rt) { 12661da177e4SLinus Torvalds err = -ENOMEM; 12671da177e4SLinus Torvalds goto out; 12681da177e4SLinus Torvalds } 12691da177e4SLinus Torvalds 1270d8d1f30bSChangli Gao rt->dst.obsolete = -1; 1271d1918542SDavid S. Miller rt->dst.expires = (cfg->fc_flags & RTF_EXPIRES) ? 12726f704992SYOSHIFUJI Hideaki jiffies + clock_t_to_jiffies(cfg->fc_expires) : 12736f704992SYOSHIFUJI Hideaki 0; 12741da177e4SLinus Torvalds 127586872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 127686872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 127786872cb5SThomas Graf rt->rt6i_protocol = cfg->fc_protocol; 127886872cb5SThomas Graf 127986872cb5SThomas Graf addr_type = ipv6_addr_type(&cfg->fc_dst); 12801da177e4SLinus Torvalds 12811da177e4SLinus Torvalds if (addr_type & IPV6_ADDR_MULTICAST) 1282d8d1f30bSChangli Gao rt->dst.input = ip6_mc_input; 1283ab79ad14SMaciej Żenczykowski else if (cfg->fc_flags & RTF_LOCAL) 1284ab79ad14SMaciej Żenczykowski rt->dst.input = ip6_input; 12851da177e4SLinus Torvalds else 1286d8d1f30bSChangli Gao rt->dst.input = ip6_forward; 12871da177e4SLinus Torvalds 1288d8d1f30bSChangli Gao rt->dst.output = ip6_output; 12891da177e4SLinus Torvalds 129086872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 129186872cb5SThomas Graf rt->rt6i_dst.plen = cfg->fc_dst_len; 12921da177e4SLinus Torvalds if (rt->rt6i_dst.plen == 128) 129311d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 12941da177e4SLinus Torvalds 12958e2ec639SYan, Zheng if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) { 12968e2ec639SYan, Zheng u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL); 12978e2ec639SYan, Zheng if (!metrics) { 12988e2ec639SYan, Zheng err = -ENOMEM; 12998e2ec639SYan, Zheng goto out; 13008e2ec639SYan, Zheng } 13018e2ec639SYan, Zheng dst_init_metrics(&rt->dst, metrics, 0); 13028e2ec639SYan, Zheng } 13031da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 130486872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len); 130586872cb5SThomas Graf rt->rt6i_src.plen = cfg->fc_src_len; 13061da177e4SLinus Torvalds #endif 13071da177e4SLinus Torvalds 130886872cb5SThomas Graf rt->rt6i_metric = cfg->fc_metric; 13091da177e4SLinus Torvalds 13101da177e4SLinus Torvalds /* We cannot add true routes via loopback here, 13111da177e4SLinus Torvalds they would result in kernel looping; promote them to reject routes 13121da177e4SLinus Torvalds */ 131386872cb5SThomas Graf if ((cfg->fc_flags & RTF_REJECT) || 131438308473SDavid S. Miller (dev && (dev->flags & IFF_LOOPBACK) && 131538308473SDavid S. Miller !(addr_type & IPV6_ADDR_LOOPBACK) && 131638308473SDavid S. Miller !(cfg->fc_flags & RTF_LOCAL))) { 13171da177e4SLinus Torvalds /* hold loopback dev/idev if we haven't done so. */ 13185578689aSDaniel Lezcano if (dev != net->loopback_dev) { 13191da177e4SLinus Torvalds if (dev) { 13201da177e4SLinus Torvalds dev_put(dev); 13211da177e4SLinus Torvalds in6_dev_put(idev); 13221da177e4SLinus Torvalds } 13235578689aSDaniel Lezcano dev = net->loopback_dev; 13241da177e4SLinus Torvalds dev_hold(dev); 13251da177e4SLinus Torvalds idev = in6_dev_get(dev); 13261da177e4SLinus Torvalds if (!idev) { 13271da177e4SLinus Torvalds err = -ENODEV; 13281da177e4SLinus Torvalds goto out; 13291da177e4SLinus Torvalds } 13301da177e4SLinus Torvalds } 1331d8d1f30bSChangli Gao rt->dst.output = ip6_pkt_discard_out; 1332d8d1f30bSChangli Gao rt->dst.input = ip6_pkt_discard; 1333d8d1f30bSChangli Gao rt->dst.error = -ENETUNREACH; 13341da177e4SLinus Torvalds rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP; 13351da177e4SLinus Torvalds goto install_route; 13361da177e4SLinus Torvalds } 13371da177e4SLinus Torvalds 133886872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY) { 1339b71d1d42SEric Dumazet const struct in6_addr *gw_addr; 13401da177e4SLinus Torvalds int gwa_type; 13411da177e4SLinus Torvalds 134286872cb5SThomas Graf gw_addr = &cfg->fc_gateway; 13434e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = *gw_addr; 13441da177e4SLinus Torvalds gwa_type = ipv6_addr_type(gw_addr); 13451da177e4SLinus Torvalds 13461da177e4SLinus Torvalds if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) { 13471da177e4SLinus Torvalds struct rt6_info *grt; 13481da177e4SLinus Torvalds 13491da177e4SLinus Torvalds /* IPv6 strictly inhibits using not link-local 13501da177e4SLinus Torvalds addresses as nexthop address. 13511da177e4SLinus Torvalds Otherwise, router will not able to send redirects. 13521da177e4SLinus Torvalds It is very good, but in some (rare!) circumstances 13531da177e4SLinus Torvalds (SIT, PtP, NBMA NOARP links) it is handy to allow 13541da177e4SLinus Torvalds some exceptions. --ANK 13551da177e4SLinus Torvalds */ 13561da177e4SLinus Torvalds err = -EINVAL; 13571da177e4SLinus Torvalds if (!(gwa_type & IPV6_ADDR_UNICAST)) 13581da177e4SLinus Torvalds goto out; 13591da177e4SLinus Torvalds 13605578689aSDaniel Lezcano grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1); 13611da177e4SLinus Torvalds 13621da177e4SLinus Torvalds err = -EHOSTUNREACH; 136338308473SDavid S. Miller if (!grt) 13641da177e4SLinus Torvalds goto out; 13651da177e4SLinus Torvalds if (dev) { 1366d1918542SDavid S. Miller if (dev != grt->dst.dev) { 1367d8d1f30bSChangli Gao dst_release(&grt->dst); 13681da177e4SLinus Torvalds goto out; 13691da177e4SLinus Torvalds } 13701da177e4SLinus Torvalds } else { 1371d1918542SDavid S. Miller dev = grt->dst.dev; 13721da177e4SLinus Torvalds idev = grt->rt6i_idev; 13731da177e4SLinus Torvalds dev_hold(dev); 13741da177e4SLinus Torvalds in6_dev_hold(grt->rt6i_idev); 13751da177e4SLinus Torvalds } 13761da177e4SLinus Torvalds if (!(grt->rt6i_flags & RTF_GATEWAY)) 13771da177e4SLinus Torvalds err = 0; 1378d8d1f30bSChangli Gao dst_release(&grt->dst); 13791da177e4SLinus Torvalds 13801da177e4SLinus Torvalds if (err) 13811da177e4SLinus Torvalds goto out; 13821da177e4SLinus Torvalds } 13831da177e4SLinus Torvalds err = -EINVAL; 138438308473SDavid S. Miller if (!dev || (dev->flags & IFF_LOOPBACK)) 13851da177e4SLinus Torvalds goto out; 13861da177e4SLinus Torvalds } 13871da177e4SLinus Torvalds 13881da177e4SLinus Torvalds err = -ENODEV; 138938308473SDavid S. Miller if (!dev) 13901da177e4SLinus Torvalds goto out; 13911da177e4SLinus Torvalds 1392c3968a85SDaniel Walter if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 1393c3968a85SDaniel Walter if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 1394c3968a85SDaniel Walter err = -EINVAL; 1395c3968a85SDaniel Walter goto out; 1396c3968a85SDaniel Walter } 13974e3fd7a0SAlexey Dobriyan rt->rt6i_prefsrc.addr = cfg->fc_prefsrc; 1398c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 128; 1399c3968a85SDaniel Walter } else 1400c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 1401c3968a85SDaniel Walter 140286872cb5SThomas Graf if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) { 1403*8ade06c6SDavid S. Miller err = rt6_bind_neighbour(rt, dev); 1404f83c7790SDavid S. Miller if (err) 14051da177e4SLinus Torvalds goto out; 14061da177e4SLinus Torvalds } 14071da177e4SLinus Torvalds 140886872cb5SThomas Graf rt->rt6i_flags = cfg->fc_flags; 14091da177e4SLinus Torvalds 14101da177e4SLinus Torvalds install_route: 141186872cb5SThomas Graf if (cfg->fc_mx) { 141286872cb5SThomas Graf struct nlattr *nla; 141386872cb5SThomas Graf int remaining; 14141da177e4SLinus Torvalds 141586872cb5SThomas Graf nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { 14168f4c1f9bSThomas Graf int type = nla_type(nla); 141786872cb5SThomas Graf 141886872cb5SThomas Graf if (type) { 141986872cb5SThomas Graf if (type > RTAX_MAX) { 14201da177e4SLinus Torvalds err = -EINVAL; 14211da177e4SLinus Torvalds goto out; 14221da177e4SLinus Torvalds } 142386872cb5SThomas Graf 1424defb3519SDavid S. Miller dst_metric_set(&rt->dst, type, nla_get_u32(nla)); 14251da177e4SLinus Torvalds } 14261da177e4SLinus Torvalds } 14271da177e4SLinus Torvalds } 14281da177e4SLinus Torvalds 1429d8d1f30bSChangli Gao rt->dst.dev = dev; 14301da177e4SLinus Torvalds rt->rt6i_idev = idev; 1431c71099acSThomas Graf rt->rt6i_table = table; 143263152fc0SDaniel Lezcano 1433c346dca1SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = dev_net(dev); 143463152fc0SDaniel Lezcano 143586872cb5SThomas Graf return __ip6_ins_rt(rt, &cfg->fc_nlinfo); 14361da177e4SLinus Torvalds 14371da177e4SLinus Torvalds out: 14381da177e4SLinus Torvalds if (dev) 14391da177e4SLinus Torvalds dev_put(dev); 14401da177e4SLinus Torvalds if (idev) 14411da177e4SLinus Torvalds in6_dev_put(idev); 14421da177e4SLinus Torvalds if (rt) 1443d8d1f30bSChangli Gao dst_free(&rt->dst); 14441da177e4SLinus Torvalds return err; 14451da177e4SLinus Torvalds } 14461da177e4SLinus Torvalds 144786872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info) 14481da177e4SLinus Torvalds { 14491da177e4SLinus Torvalds int err; 1450c71099acSThomas Graf struct fib6_table *table; 1451d1918542SDavid S. Miller struct net *net = dev_net(rt->dst.dev); 14521da177e4SLinus Torvalds 14538ed67789SDaniel Lezcano if (rt == net->ipv6.ip6_null_entry) 14546c813a72SPatrick McHardy return -ENOENT; 14556c813a72SPatrick McHardy 1456c71099acSThomas Graf table = rt->rt6i_table; 1457c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 14581da177e4SLinus Torvalds 145986872cb5SThomas Graf err = fib6_del(rt, info); 1460d8d1f30bSChangli Gao dst_release(&rt->dst); 14611da177e4SLinus Torvalds 1462c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 14631da177e4SLinus Torvalds 14641da177e4SLinus Torvalds return err; 14651da177e4SLinus Torvalds } 14661da177e4SLinus Torvalds 1467e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt) 1468e0a1ad73SThomas Graf { 14694d1169c1SDenis V. Lunev struct nl_info info = { 1470d1918542SDavid S. Miller .nl_net = dev_net(rt->dst.dev), 14714d1169c1SDenis V. Lunev }; 1472528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 1473e0a1ad73SThomas Graf } 1474e0a1ad73SThomas Graf 147586872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg) 14761da177e4SLinus Torvalds { 1477c71099acSThomas Graf struct fib6_table *table; 14781da177e4SLinus Torvalds struct fib6_node *fn; 14791da177e4SLinus Torvalds struct rt6_info *rt; 14801da177e4SLinus Torvalds int err = -ESRCH; 14811da177e4SLinus Torvalds 14825578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 148338308473SDavid S. Miller if (!table) 1484c71099acSThomas Graf return err; 14851da177e4SLinus Torvalds 1486c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 1487c71099acSThomas Graf 1488c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 148986872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 149086872cb5SThomas Graf &cfg->fc_src, cfg->fc_src_len); 14911da177e4SLinus Torvalds 14921da177e4SLinus Torvalds if (fn) { 1493d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 149486872cb5SThomas Graf if (cfg->fc_ifindex && 1495d1918542SDavid S. Miller (!rt->dst.dev || 1496d1918542SDavid S. Miller rt->dst.dev->ifindex != cfg->fc_ifindex)) 14971da177e4SLinus Torvalds continue; 149886872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 149986872cb5SThomas Graf !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 15001da177e4SLinus Torvalds continue; 150186872cb5SThomas Graf if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric) 15021da177e4SLinus Torvalds continue; 1503d8d1f30bSChangli Gao dst_hold(&rt->dst); 1504c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 15051da177e4SLinus Torvalds 150686872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 15071da177e4SLinus Torvalds } 15081da177e4SLinus Torvalds } 1509c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 15101da177e4SLinus Torvalds 15111da177e4SLinus Torvalds return err; 15121da177e4SLinus Torvalds } 15131da177e4SLinus Torvalds 15141da177e4SLinus Torvalds /* 15151da177e4SLinus Torvalds * Handle redirects 15161da177e4SLinus Torvalds */ 1517a6279458SYOSHIFUJI Hideaki struct ip6rd_flowi { 15184c9483b2SDavid S. Miller struct flowi6 fl6; 1519a6279458SYOSHIFUJI Hideaki struct in6_addr gateway; 1520a6279458SYOSHIFUJI Hideaki }; 15211da177e4SLinus Torvalds 15228ed67789SDaniel Lezcano static struct rt6_info *__ip6_route_redirect(struct net *net, 15238ed67789SDaniel Lezcano struct fib6_table *table, 15244c9483b2SDavid S. Miller struct flowi6 *fl6, 1525a6279458SYOSHIFUJI Hideaki int flags) 1526a6279458SYOSHIFUJI Hideaki { 15274c9483b2SDavid S. Miller struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; 1528a6279458SYOSHIFUJI Hideaki struct rt6_info *rt; 1529a6279458SYOSHIFUJI Hideaki struct fib6_node *fn; 1530c71099acSThomas Graf 1531e843b9e1SYOSHIFUJI Hideaki /* 1532e843b9e1SYOSHIFUJI Hideaki * Get the "current" route for this destination and 1533e843b9e1SYOSHIFUJI Hideaki * check if the redirect has come from approriate router. 1534e843b9e1SYOSHIFUJI Hideaki * 1535e843b9e1SYOSHIFUJI Hideaki * RFC 2461 specifies that redirects should only be 1536e843b9e1SYOSHIFUJI Hideaki * accepted if they come from the nexthop to the target. 1537e843b9e1SYOSHIFUJI Hideaki * Due to the way the routes are chosen, this notion 1538e843b9e1SYOSHIFUJI Hideaki * is a bit fuzzy and one might need to check all possible 1539e843b9e1SYOSHIFUJI Hideaki * routes. 1540e843b9e1SYOSHIFUJI Hideaki */ 15411da177e4SLinus Torvalds 1542c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 15434c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1544e843b9e1SYOSHIFUJI Hideaki restart: 1545d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 15461da177e4SLinus Torvalds /* 15471da177e4SLinus Torvalds * Current route is on-link; redirect is always invalid. 15481da177e4SLinus Torvalds * 15491da177e4SLinus Torvalds * Seems, previous statement is not true. It could 15501da177e4SLinus Torvalds * be node, which looks for us as on-link (f.e. proxy ndisc) 15511da177e4SLinus Torvalds * But then router serving it might decide, that we should 15521da177e4SLinus Torvalds * know truth 8)8) --ANK (980726). 15531da177e4SLinus Torvalds */ 1554e843b9e1SYOSHIFUJI Hideaki if (rt6_check_expired(rt)) 1555e843b9e1SYOSHIFUJI Hideaki continue; 15561da177e4SLinus Torvalds if (!(rt->rt6i_flags & RTF_GATEWAY)) 1557e843b9e1SYOSHIFUJI Hideaki continue; 1558d1918542SDavid S. Miller if (fl6->flowi6_oif != rt->dst.dev->ifindex) 1559e843b9e1SYOSHIFUJI Hideaki continue; 1560a6279458SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway)) 1561e843b9e1SYOSHIFUJI Hideaki continue; 1562e843b9e1SYOSHIFUJI Hideaki break; 1563e843b9e1SYOSHIFUJI Hideaki } 1564a6279458SYOSHIFUJI Hideaki 1565cb15d9c2SYOSHIFUJI Hideaki if (!rt) 15668ed67789SDaniel Lezcano rt = net->ipv6.ip6_null_entry; 15674c9483b2SDavid S. Miller BACKTRACK(net, &fl6->saddr); 1568cb15d9c2SYOSHIFUJI Hideaki out: 1569d8d1f30bSChangli Gao dst_hold(&rt->dst); 1570a6279458SYOSHIFUJI Hideaki 1571c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 15721da177e4SLinus Torvalds 1573a6279458SYOSHIFUJI Hideaki return rt; 1574a6279458SYOSHIFUJI Hideaki }; 1575a6279458SYOSHIFUJI Hideaki 1576b71d1d42SEric Dumazet static struct rt6_info *ip6_route_redirect(const struct in6_addr *dest, 1577b71d1d42SEric Dumazet const struct in6_addr *src, 1578b71d1d42SEric Dumazet const struct in6_addr *gateway, 1579a6279458SYOSHIFUJI Hideaki struct net_device *dev) 1580a6279458SYOSHIFUJI Hideaki { 1581adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 1582c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 1583a6279458SYOSHIFUJI Hideaki struct ip6rd_flowi rdfl = { 15844c9483b2SDavid S. Miller .fl6 = { 15854c9483b2SDavid S. Miller .flowi6_oif = dev->ifindex, 15864c9483b2SDavid S. Miller .daddr = *dest, 15874c9483b2SDavid S. Miller .saddr = *src, 1588a6279458SYOSHIFUJI Hideaki }, 1589a6279458SYOSHIFUJI Hideaki }; 1590adaa70bbSThomas Graf 15914e3fd7a0SAlexey Dobriyan rdfl.gateway = *gateway; 159286c36ce4SBrian Haley 1593adaa70bbSThomas Graf if (rt6_need_strict(dest)) 1594adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_IFACE; 1595a6279458SYOSHIFUJI Hideaki 15964c9483b2SDavid S. Miller return (struct rt6_info *)fib6_rule_lookup(net, &rdfl.fl6, 159758f09b78SDaniel Lezcano flags, __ip6_route_redirect); 1598a6279458SYOSHIFUJI Hideaki } 1599a6279458SYOSHIFUJI Hideaki 1600b71d1d42SEric Dumazet void rt6_redirect(const struct in6_addr *dest, const struct in6_addr *src, 1601b71d1d42SEric Dumazet const struct in6_addr *saddr, 1602a6279458SYOSHIFUJI Hideaki struct neighbour *neigh, u8 *lladdr, int on_link) 1603a6279458SYOSHIFUJI Hideaki { 1604a6279458SYOSHIFUJI Hideaki struct rt6_info *rt, *nrt = NULL; 1605a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 1606c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(neigh->dev); 1607a6279458SYOSHIFUJI Hideaki 1608a6279458SYOSHIFUJI Hideaki rt = ip6_route_redirect(dest, src, saddr, neigh->dev); 1609a6279458SYOSHIFUJI Hideaki 16108ed67789SDaniel Lezcano if (rt == net->ipv6.ip6_null_entry) { 16111da177e4SLinus Torvalds if (net_ratelimit()) 16121da177e4SLinus Torvalds printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop " 16131da177e4SLinus Torvalds "for redirect target\n"); 1614a6279458SYOSHIFUJI Hideaki goto out; 16151da177e4SLinus Torvalds } 16161da177e4SLinus Torvalds 16171da177e4SLinus Torvalds /* 16181da177e4SLinus Torvalds * We have finally decided to accept it. 16191da177e4SLinus Torvalds */ 16201da177e4SLinus Torvalds 16211da177e4SLinus Torvalds neigh_update(neigh, lladdr, NUD_STALE, 16221da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 16231da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 16241da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 16251da177e4SLinus Torvalds NEIGH_UPDATE_F_ISROUTER)) 16261da177e4SLinus Torvalds ); 16271da177e4SLinus Torvalds 16281da177e4SLinus Torvalds /* 16291da177e4SLinus Torvalds * Redirect received -> path was valid. 16301da177e4SLinus Torvalds * Look, redirects are sent only in response to data packets, 16311da177e4SLinus Torvalds * so that this nexthop apparently is reachable. --ANK 16321da177e4SLinus Torvalds */ 1633d8d1f30bSChangli Gao dst_confirm(&rt->dst); 16341da177e4SLinus Torvalds 16351da177e4SLinus Torvalds /* Duplicate redirect: silently ignore. */ 163627217455SDavid Miller if (neigh == dst_get_neighbour_noref_raw(&rt->dst)) 16371da177e4SLinus Torvalds goto out; 16381da177e4SLinus Torvalds 163921efcfa0SEric Dumazet nrt = ip6_rt_copy(rt, dest); 164038308473SDavid S. Miller if (!nrt) 16411da177e4SLinus Torvalds goto out; 16421da177e4SLinus Torvalds 16431da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 16441da177e4SLinus Torvalds if (on_link) 16451da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 16461da177e4SLinus Torvalds 16474e3fd7a0SAlexey Dobriyan nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 164869cce1d1SDavid S. Miller dst_set_neighbour(&nrt->dst, neigh_clone(neigh)); 16491da177e4SLinus Torvalds 165040e22e8fSThomas Graf if (ip6_ins_rt(nrt)) 16511da177e4SLinus Torvalds goto out; 16521da177e4SLinus Torvalds 1653d8d1f30bSChangli Gao netevent.old = &rt->dst; 1654d8d1f30bSChangli Gao netevent.new = &nrt->dst; 16558d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 16568d71740cSTom Tucker 16571da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) { 1658e0a1ad73SThomas Graf ip6_del_rt(rt); 16591da177e4SLinus Torvalds return; 16601da177e4SLinus Torvalds } 16611da177e4SLinus Torvalds 16621da177e4SLinus Torvalds out: 1663d8d1f30bSChangli Gao dst_release(&rt->dst); 16641da177e4SLinus Torvalds } 16651da177e4SLinus Torvalds 16661da177e4SLinus Torvalds /* 16671da177e4SLinus Torvalds * Handle ICMP "packet too big" messages 16681da177e4SLinus Torvalds * i.e. Path MTU discovery 16691da177e4SLinus Torvalds */ 16701da177e4SLinus Torvalds 1671b71d1d42SEric Dumazet static void rt6_do_pmtu_disc(const struct in6_addr *daddr, const struct in6_addr *saddr, 1672ae878ae2SMaciej Żenczykowski struct net *net, u32 pmtu, int ifindex) 16731da177e4SLinus Torvalds { 16741da177e4SLinus Torvalds struct rt6_info *rt, *nrt; 16751da177e4SLinus Torvalds int allfrag = 0; 1676d3052b55SAndrey Vagin again: 1677ae878ae2SMaciej Żenczykowski rt = rt6_lookup(net, daddr, saddr, ifindex, 0); 167838308473SDavid S. Miller if (!rt) 16791da177e4SLinus Torvalds return; 16801da177e4SLinus Torvalds 1681d3052b55SAndrey Vagin if (rt6_check_expired(rt)) { 1682d3052b55SAndrey Vagin ip6_del_rt(rt); 1683d3052b55SAndrey Vagin goto again; 1684d3052b55SAndrey Vagin } 1685d3052b55SAndrey Vagin 1686d8d1f30bSChangli Gao if (pmtu >= dst_mtu(&rt->dst)) 16871da177e4SLinus Torvalds goto out; 16881da177e4SLinus Torvalds 16891da177e4SLinus Torvalds if (pmtu < IPV6_MIN_MTU) { 16901da177e4SLinus Torvalds /* 16911da177e4SLinus Torvalds * According to RFC2460, PMTU is set to the IPv6 Minimum Link 16921da177e4SLinus Torvalds * MTU (1280) and a fragment header should always be included 16931da177e4SLinus Torvalds * after a node receiving Too Big message reporting PMTU is 16941da177e4SLinus Torvalds * less than the IPv6 Minimum Link MTU. 16951da177e4SLinus Torvalds */ 16961da177e4SLinus Torvalds pmtu = IPV6_MIN_MTU; 16971da177e4SLinus Torvalds allfrag = 1; 16981da177e4SLinus Torvalds } 16991da177e4SLinus Torvalds 17001da177e4SLinus Torvalds /* New mtu received -> path was valid. 17011da177e4SLinus Torvalds They are sent only in response to data packets, 17021da177e4SLinus Torvalds so that this nexthop apparently is reachable. --ANK 17031da177e4SLinus Torvalds */ 1704d8d1f30bSChangli Gao dst_confirm(&rt->dst); 17051da177e4SLinus Torvalds 17061da177e4SLinus Torvalds /* Host route. If it is static, it would be better 17071da177e4SLinus Torvalds not to override it, but add new one, so that 17081da177e4SLinus Torvalds when cache entry will expire old pmtu 17091da177e4SLinus Torvalds would return automatically. 17101da177e4SLinus Torvalds */ 17111da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) { 1712defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_MTU, pmtu); 1713defb3519SDavid S. Miller if (allfrag) { 1714defb3519SDavid S. Miller u32 features = dst_metric(&rt->dst, RTAX_FEATURES); 1715defb3519SDavid S. Miller features |= RTAX_FEATURE_ALLFRAG; 1716defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_FEATURES, features); 1717defb3519SDavid S. Miller } 1718d8d1f30bSChangli Gao dst_set_expires(&rt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires); 17191da177e4SLinus Torvalds rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES; 17201da177e4SLinus Torvalds goto out; 17211da177e4SLinus Torvalds } 17221da177e4SLinus Torvalds 17231da177e4SLinus Torvalds /* Network route. 17241da177e4SLinus Torvalds Two cases are possible: 17251da177e4SLinus Torvalds 1. It is connected route. Action: COW 17261da177e4SLinus Torvalds 2. It is gatewayed route or NONEXTHOP route. Action: clone it. 17271da177e4SLinus Torvalds */ 172827217455SDavid Miller if (!dst_get_neighbour_noref_raw(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP)) 1729a1e78363SYOSHIFUJI Hideaki nrt = rt6_alloc_cow(rt, daddr, saddr); 1730d5315b50SYOSHIFUJI Hideaki else 1731d5315b50SYOSHIFUJI Hideaki nrt = rt6_alloc_clone(rt, daddr); 1732a1e78363SYOSHIFUJI Hideaki 1733d5315b50SYOSHIFUJI Hideaki if (nrt) { 1734defb3519SDavid S. Miller dst_metric_set(&nrt->dst, RTAX_MTU, pmtu); 1735defb3519SDavid S. Miller if (allfrag) { 1736defb3519SDavid S. Miller u32 features = dst_metric(&nrt->dst, RTAX_FEATURES); 1737defb3519SDavid S. Miller features |= RTAX_FEATURE_ALLFRAG; 1738defb3519SDavid S. Miller dst_metric_set(&nrt->dst, RTAX_FEATURES, features); 1739defb3519SDavid S. Miller } 1740a1e78363SYOSHIFUJI Hideaki 17411da177e4SLinus Torvalds /* According to RFC 1981, detecting PMTU increase shouldn't be 1742a1e78363SYOSHIFUJI Hideaki * happened within 5 mins, the recommended timer is 10 mins. 1743a1e78363SYOSHIFUJI Hideaki * Here this route expiration time is set to ip6_rt_mtu_expires 1744a1e78363SYOSHIFUJI Hideaki * which is 10 mins. After 10 mins the decreased pmtu is expired 1745a1e78363SYOSHIFUJI Hideaki * and detecting PMTU increase will be automatically happened. 17461da177e4SLinus Torvalds */ 1747d8d1f30bSChangli Gao dst_set_expires(&nrt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires); 17481da177e4SLinus Torvalds nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES; 1749a1e78363SYOSHIFUJI Hideaki 175040e22e8fSThomas Graf ip6_ins_rt(nrt); 17511da177e4SLinus Torvalds } 17521da177e4SLinus Torvalds out: 1753d8d1f30bSChangli Gao dst_release(&rt->dst); 17541da177e4SLinus Torvalds } 17551da177e4SLinus Torvalds 1756b71d1d42SEric Dumazet void rt6_pmtu_discovery(const struct in6_addr *daddr, const struct in6_addr *saddr, 1757ae878ae2SMaciej Żenczykowski struct net_device *dev, u32 pmtu) 1758ae878ae2SMaciej Żenczykowski { 1759ae878ae2SMaciej Żenczykowski struct net *net = dev_net(dev); 1760ae878ae2SMaciej Żenczykowski 1761ae878ae2SMaciej Żenczykowski /* 1762ae878ae2SMaciej Żenczykowski * RFC 1981 states that a node "MUST reduce the size of the packets it 1763ae878ae2SMaciej Żenczykowski * is sending along the path" that caused the Packet Too Big message. 1764ae878ae2SMaciej Żenczykowski * Since it's not possible in the general case to determine which 1765ae878ae2SMaciej Żenczykowski * interface was used to send the original packet, we update the MTU 1766ae878ae2SMaciej Żenczykowski * on the interface that will be used to send future packets. We also 1767ae878ae2SMaciej Żenczykowski * update the MTU on the interface that received the Packet Too Big in 1768ae878ae2SMaciej Żenczykowski * case the original packet was forced out that interface with 1769ae878ae2SMaciej Żenczykowski * SO_BINDTODEVICE or similar. This is the next best thing to the 1770ae878ae2SMaciej Żenczykowski * correct behaviour, which would be to update the MTU on all 1771ae878ae2SMaciej Żenczykowski * interfaces. 1772ae878ae2SMaciej Żenczykowski */ 1773ae878ae2SMaciej Żenczykowski rt6_do_pmtu_disc(daddr, saddr, net, pmtu, 0); 1774ae878ae2SMaciej Żenczykowski rt6_do_pmtu_disc(daddr, saddr, net, pmtu, dev->ifindex); 1775ae878ae2SMaciej Żenczykowski } 1776ae878ae2SMaciej Żenczykowski 17771da177e4SLinus Torvalds /* 17781da177e4SLinus Torvalds * Misc support functions 17791da177e4SLinus Torvalds */ 17801da177e4SLinus Torvalds 178121efcfa0SEric Dumazet static struct rt6_info *ip6_rt_copy(const struct rt6_info *ort, 178221efcfa0SEric Dumazet const struct in6_addr *dest) 17831da177e4SLinus Torvalds { 1784d1918542SDavid S. Miller struct net *net = dev_net(ort->dst.dev); 17855c1e6aa3SDavid S. Miller struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, 1786957c665fSDavid S. Miller ort->dst.dev, 0); 17871da177e4SLinus Torvalds 17881da177e4SLinus Torvalds if (rt) { 1789d8d1f30bSChangli Gao rt->dst.input = ort->dst.input; 1790d8d1f30bSChangli Gao rt->dst.output = ort->dst.output; 17918e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 17921da177e4SLinus Torvalds 17934e3fd7a0SAlexey Dobriyan rt->rt6i_dst.addr = *dest; 17948e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 1795defb3519SDavid S. Miller dst_copy_metrics(&rt->dst, &ort->dst); 1796d8d1f30bSChangli Gao rt->dst.error = ort->dst.error; 17971da177e4SLinus Torvalds rt->rt6i_idev = ort->rt6i_idev; 17981da177e4SLinus Torvalds if (rt->rt6i_idev) 17991da177e4SLinus Torvalds in6_dev_hold(rt->rt6i_idev); 1800d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 1801d1918542SDavid S. Miller rt->dst.expires = 0; 18021da177e4SLinus Torvalds 18034e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 18041da177e4SLinus Torvalds rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES; 18051da177e4SLinus Torvalds rt->rt6i_metric = 0; 18061da177e4SLinus Torvalds 18071da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 18081da177e4SLinus Torvalds memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 18091da177e4SLinus Torvalds #endif 18100f6c6392SFlorian Westphal memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key)); 1811c71099acSThomas Graf rt->rt6i_table = ort->rt6i_table; 18121da177e4SLinus Torvalds } 18131da177e4SLinus Torvalds return rt; 18141da177e4SLinus Torvalds } 18151da177e4SLinus Torvalds 181670ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 1817efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 1818b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 1819b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex) 182070ceb4f5SYOSHIFUJI Hideaki { 182170ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 182270ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt = NULL; 1823c71099acSThomas Graf struct fib6_table *table; 182470ceb4f5SYOSHIFUJI Hideaki 1825efa2cea0SDaniel Lezcano table = fib6_get_table(net, RT6_TABLE_INFO); 182638308473SDavid S. Miller if (!table) 1827c71099acSThomas Graf return NULL; 1828c71099acSThomas Graf 1829c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 1830c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0); 183170ceb4f5SYOSHIFUJI Hideaki if (!fn) 183270ceb4f5SYOSHIFUJI Hideaki goto out; 183370ceb4f5SYOSHIFUJI Hideaki 1834d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 1835d1918542SDavid S. Miller if (rt->dst.dev->ifindex != ifindex) 183670ceb4f5SYOSHIFUJI Hideaki continue; 183770ceb4f5SYOSHIFUJI Hideaki if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) 183870ceb4f5SYOSHIFUJI Hideaki continue; 183970ceb4f5SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr)) 184070ceb4f5SYOSHIFUJI Hideaki continue; 1841d8d1f30bSChangli Gao dst_hold(&rt->dst); 184270ceb4f5SYOSHIFUJI Hideaki break; 184370ceb4f5SYOSHIFUJI Hideaki } 184470ceb4f5SYOSHIFUJI Hideaki out: 1845c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 184670ceb4f5SYOSHIFUJI Hideaki return rt; 184770ceb4f5SYOSHIFUJI Hideaki } 184870ceb4f5SYOSHIFUJI Hideaki 1849efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 1850b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 1851b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex, 185270ceb4f5SYOSHIFUJI Hideaki unsigned pref) 185370ceb4f5SYOSHIFUJI Hideaki { 185486872cb5SThomas Graf struct fib6_config cfg = { 185586872cb5SThomas Graf .fc_table = RT6_TABLE_INFO, 1856238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 185786872cb5SThomas Graf .fc_ifindex = ifindex, 185886872cb5SThomas Graf .fc_dst_len = prefixlen, 185986872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 186086872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 1861efa2cea0SDaniel Lezcano .fc_nlinfo.pid = 0, 1862efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 1863efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 186486872cb5SThomas Graf }; 186570ceb4f5SYOSHIFUJI Hideaki 18664e3fd7a0SAlexey Dobriyan cfg.fc_dst = *prefix; 18674e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 186886872cb5SThomas Graf 1869e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 1870e317da96SYOSHIFUJI Hideaki if (!prefixlen) 187186872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 187270ceb4f5SYOSHIFUJI Hideaki 187386872cb5SThomas Graf ip6_route_add(&cfg); 187470ceb4f5SYOSHIFUJI Hideaki 1875efa2cea0SDaniel Lezcano return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex); 187670ceb4f5SYOSHIFUJI Hideaki } 187770ceb4f5SYOSHIFUJI Hideaki #endif 187870ceb4f5SYOSHIFUJI Hideaki 1879b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev) 18801da177e4SLinus Torvalds { 18811da177e4SLinus Torvalds struct rt6_info *rt; 1882c71099acSThomas Graf struct fib6_table *table; 18831da177e4SLinus Torvalds 1884c346dca1SYOSHIFUJI Hideaki table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT); 188538308473SDavid S. Miller if (!table) 1886c71099acSThomas Graf return NULL; 18871da177e4SLinus Torvalds 1888c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 1889d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) { 1890d1918542SDavid S. Miller if (dev == rt->dst.dev && 1891045927ffSYOSHIFUJI Hideaki ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 18921da177e4SLinus Torvalds ipv6_addr_equal(&rt->rt6i_gateway, addr)) 18931da177e4SLinus Torvalds break; 18941da177e4SLinus Torvalds } 18951da177e4SLinus Torvalds if (rt) 1896d8d1f30bSChangli Gao dst_hold(&rt->dst); 1897c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 18981da177e4SLinus Torvalds return rt; 18991da177e4SLinus Torvalds } 19001da177e4SLinus Torvalds 1901b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr, 1902ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 1903ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 19041da177e4SLinus Torvalds { 190586872cb5SThomas Graf struct fib6_config cfg = { 190686872cb5SThomas Graf .fc_table = RT6_TABLE_DFLT, 1907238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 190886872cb5SThomas Graf .fc_ifindex = dev->ifindex, 190986872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 191086872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 19115578689aSDaniel Lezcano .fc_nlinfo.pid = 0, 19125578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 1913c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 191486872cb5SThomas Graf }; 19151da177e4SLinus Torvalds 19164e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 19171da177e4SLinus Torvalds 191886872cb5SThomas Graf ip6_route_add(&cfg); 19191da177e4SLinus Torvalds 19201da177e4SLinus Torvalds return rt6_get_dflt_router(gwaddr, dev); 19211da177e4SLinus Torvalds } 19221da177e4SLinus Torvalds 19237b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net) 19241da177e4SLinus Torvalds { 19251da177e4SLinus Torvalds struct rt6_info *rt; 1926c71099acSThomas Graf struct fib6_table *table; 1927c71099acSThomas Graf 1928c71099acSThomas Graf /* NOTE: Keep consistent with rt6_get_dflt_router */ 19297b4da532SDaniel Lezcano table = fib6_get_table(net, RT6_TABLE_DFLT); 193038308473SDavid S. Miller if (!table) 1931c71099acSThomas Graf return; 19321da177e4SLinus Torvalds 19331da177e4SLinus Torvalds restart: 1934c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 1935d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 19361da177e4SLinus Torvalds if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) { 1937d8d1f30bSChangli Gao dst_hold(&rt->dst); 1938c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 1939e0a1ad73SThomas Graf ip6_del_rt(rt); 19401da177e4SLinus Torvalds goto restart; 19411da177e4SLinus Torvalds } 19421da177e4SLinus Torvalds } 1943c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 19441da177e4SLinus Torvalds } 19451da177e4SLinus Torvalds 19465578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 19475578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 194886872cb5SThomas Graf struct fib6_config *cfg) 194986872cb5SThomas Graf { 195086872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 195186872cb5SThomas Graf 195286872cb5SThomas Graf cfg->fc_table = RT6_TABLE_MAIN; 195386872cb5SThomas Graf cfg->fc_ifindex = rtmsg->rtmsg_ifindex; 195486872cb5SThomas Graf cfg->fc_metric = rtmsg->rtmsg_metric; 195586872cb5SThomas Graf cfg->fc_expires = rtmsg->rtmsg_info; 195686872cb5SThomas Graf cfg->fc_dst_len = rtmsg->rtmsg_dst_len; 195786872cb5SThomas Graf cfg->fc_src_len = rtmsg->rtmsg_src_len; 195886872cb5SThomas Graf cfg->fc_flags = rtmsg->rtmsg_flags; 195986872cb5SThomas Graf 19605578689aSDaniel Lezcano cfg->fc_nlinfo.nl_net = net; 1961f1243c2dSBenjamin Thery 19624e3fd7a0SAlexey Dobriyan cfg->fc_dst = rtmsg->rtmsg_dst; 19634e3fd7a0SAlexey Dobriyan cfg->fc_src = rtmsg->rtmsg_src; 19644e3fd7a0SAlexey Dobriyan cfg->fc_gateway = rtmsg->rtmsg_gateway; 196586872cb5SThomas Graf } 196686872cb5SThomas Graf 19675578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) 19681da177e4SLinus Torvalds { 196986872cb5SThomas Graf struct fib6_config cfg; 19701da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 19711da177e4SLinus Torvalds int err; 19721da177e4SLinus Torvalds 19731da177e4SLinus Torvalds switch(cmd) { 19741da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 19751da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 19761da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 19771da177e4SLinus Torvalds return -EPERM; 19781da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 19791da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 19801da177e4SLinus Torvalds if (err) 19811da177e4SLinus Torvalds return -EFAULT; 19821da177e4SLinus Torvalds 19835578689aSDaniel Lezcano rtmsg_to_fib6_config(net, &rtmsg, &cfg); 198486872cb5SThomas Graf 19851da177e4SLinus Torvalds rtnl_lock(); 19861da177e4SLinus Torvalds switch (cmd) { 19871da177e4SLinus Torvalds case SIOCADDRT: 198886872cb5SThomas Graf err = ip6_route_add(&cfg); 19891da177e4SLinus Torvalds break; 19901da177e4SLinus Torvalds case SIOCDELRT: 199186872cb5SThomas Graf err = ip6_route_del(&cfg); 19921da177e4SLinus Torvalds break; 19931da177e4SLinus Torvalds default: 19941da177e4SLinus Torvalds err = -EINVAL; 19951da177e4SLinus Torvalds } 19961da177e4SLinus Torvalds rtnl_unlock(); 19971da177e4SLinus Torvalds 19981da177e4SLinus Torvalds return err; 19993ff50b79SStephen Hemminger } 20001da177e4SLinus Torvalds 20011da177e4SLinus Torvalds return -EINVAL; 20021da177e4SLinus Torvalds } 20031da177e4SLinus Torvalds 20041da177e4SLinus Torvalds /* 20051da177e4SLinus Torvalds * Drop the packet on the floor 20061da177e4SLinus Torvalds */ 20071da177e4SLinus Torvalds 2008d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 20091da177e4SLinus Torvalds { 2010612f09e8SYOSHIFUJI Hideaki int type; 2011adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 2012612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 2013612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 20140660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 201545bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 20163bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 20173bd653c8SDenis V. Lunev IPSTATS_MIB_INADDRERRORS); 2018612f09e8SYOSHIFUJI Hideaki break; 2019612f09e8SYOSHIFUJI Hideaki } 2020612f09e8SYOSHIFUJI Hideaki /* FALLTHROUGH */ 2021612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 20223bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 20233bd653c8SDenis V. Lunev ipstats_mib_noroutes); 2024612f09e8SYOSHIFUJI Hideaki break; 2025612f09e8SYOSHIFUJI Hideaki } 20263ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 20271da177e4SLinus Torvalds kfree_skb(skb); 20281da177e4SLinus Torvalds return 0; 20291da177e4SLinus Torvalds } 20301da177e4SLinus Torvalds 20319ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 20329ce8ade0SThomas Graf { 2033612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 20349ce8ade0SThomas Graf } 20359ce8ade0SThomas Graf 203620380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb) 20371da177e4SLinus Torvalds { 2038adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 2039612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 20401da177e4SLinus Torvalds } 20411da177e4SLinus Torvalds 20426723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES 20436723ab54SDavid S. Miller 20449ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 20459ce8ade0SThomas Graf { 2046612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 20479ce8ade0SThomas Graf } 20489ce8ade0SThomas Graf 20499ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb) 20509ce8ade0SThomas Graf { 2051adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 2052612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 20539ce8ade0SThomas Graf } 20549ce8ade0SThomas Graf 20556723ab54SDavid S. Miller #endif 20566723ab54SDavid S. Miller 20571da177e4SLinus Torvalds /* 20581da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 20591da177e4SLinus Torvalds */ 20601da177e4SLinus Torvalds 20611da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, 20621da177e4SLinus Torvalds const struct in6_addr *addr, 20638f031519SDavid S. Miller bool anycast) 20641da177e4SLinus Torvalds { 2065c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 20665c1e6aa3SDavid S. Miller struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, 2067957c665fSDavid S. Miller net->loopback_dev, 0); 2068f83c7790SDavid S. Miller int err; 20691da177e4SLinus Torvalds 207038308473SDavid S. Miller if (!rt) { 207140385653SBen Greear if (net_ratelimit()) 207240385653SBen Greear pr_warning("IPv6: Maximum number of routes reached," 207340385653SBen Greear " consider increasing route/max_size.\n"); 20741da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 207540385653SBen Greear } 20761da177e4SLinus Torvalds 20771da177e4SLinus Torvalds in6_dev_hold(idev); 20781da177e4SLinus Torvalds 207911d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 2080d8d1f30bSChangli Gao rt->dst.input = ip6_input; 2081d8d1f30bSChangli Gao rt->dst.output = ip6_output; 20821da177e4SLinus Torvalds rt->rt6i_idev = idev; 2083d8d1f30bSChangli Gao rt->dst.obsolete = -1; 20841da177e4SLinus Torvalds 20851da177e4SLinus Torvalds rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP; 208658c4fb86SYOSHIFUJI Hideaki if (anycast) 208758c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 208858c4fb86SYOSHIFUJI Hideaki else 20891da177e4SLinus Torvalds rt->rt6i_flags |= RTF_LOCAL; 2090*8ade06c6SDavid S. Miller err = rt6_bind_neighbour(rt, rt->dst.dev); 2091f83c7790SDavid S. Miller if (err) { 2092d8d1f30bSChangli Gao dst_free(&rt->dst); 2093f83c7790SDavid S. Miller return ERR_PTR(err); 20941da177e4SLinus Torvalds } 20951da177e4SLinus Torvalds 20964e3fd7a0SAlexey Dobriyan rt->rt6i_dst.addr = *addr; 20971da177e4SLinus Torvalds rt->rt6i_dst.plen = 128; 20985578689aSDaniel Lezcano rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL); 20991da177e4SLinus Torvalds 2100d8d1f30bSChangli Gao atomic_set(&rt->dst.__refcnt, 1); 21011da177e4SLinus Torvalds 21021da177e4SLinus Torvalds return rt; 21031da177e4SLinus Torvalds } 21041da177e4SLinus Torvalds 2105c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net, 2106c3968a85SDaniel Walter struct rt6_info *rt, 2107b71d1d42SEric Dumazet const struct in6_addr *daddr, 2108c3968a85SDaniel Walter unsigned int prefs, 2109c3968a85SDaniel Walter struct in6_addr *saddr) 2110c3968a85SDaniel Walter { 2111c3968a85SDaniel Walter struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt); 2112c3968a85SDaniel Walter int err = 0; 2113c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) 21144e3fd7a0SAlexey Dobriyan *saddr = rt->rt6i_prefsrc.addr; 2115c3968a85SDaniel Walter else 2116c3968a85SDaniel Walter err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL, 2117c3968a85SDaniel Walter daddr, prefs, saddr); 2118c3968a85SDaniel Walter return err; 2119c3968a85SDaniel Walter } 2120c3968a85SDaniel Walter 2121c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 2122c3968a85SDaniel Walter struct arg_dev_net_ip { 2123c3968a85SDaniel Walter struct net_device *dev; 2124c3968a85SDaniel Walter struct net *net; 2125c3968a85SDaniel Walter struct in6_addr *addr; 2126c3968a85SDaniel Walter }; 2127c3968a85SDaniel Walter 2128c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg) 2129c3968a85SDaniel Walter { 2130c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 2131c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 2132c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 2133c3968a85SDaniel Walter 2134d1918542SDavid S. Miller if (((void *)rt->dst.dev == dev || !dev) && 2135c3968a85SDaniel Walter rt != net->ipv6.ip6_null_entry && 2136c3968a85SDaniel Walter ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) { 2137c3968a85SDaniel Walter /* remove prefsrc entry */ 2138c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 2139c3968a85SDaniel Walter } 2140c3968a85SDaniel Walter return 0; 2141c3968a85SDaniel Walter } 2142c3968a85SDaniel Walter 2143c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 2144c3968a85SDaniel Walter { 2145c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 2146c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 2147c3968a85SDaniel Walter .dev = ifp->idev->dev, 2148c3968a85SDaniel Walter .net = net, 2149c3968a85SDaniel Walter .addr = &ifp->addr, 2150c3968a85SDaniel Walter }; 2151c3968a85SDaniel Walter fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni); 2152c3968a85SDaniel Walter } 2153c3968a85SDaniel Walter 21548ed67789SDaniel Lezcano struct arg_dev_net { 21558ed67789SDaniel Lezcano struct net_device *dev; 21568ed67789SDaniel Lezcano struct net *net; 21578ed67789SDaniel Lezcano }; 21588ed67789SDaniel Lezcano 21591da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg) 21601da177e4SLinus Torvalds { 2161bc3ef660Sstephen hemminger const struct arg_dev_net *adn = arg; 2162bc3ef660Sstephen hemminger const struct net_device *dev = adn->dev; 21638ed67789SDaniel Lezcano 2164d1918542SDavid S. Miller if ((rt->dst.dev == dev || !dev) && 2165c159d30cSDavid S. Miller rt != adn->net->ipv6.ip6_null_entry) 21661da177e4SLinus Torvalds return -1; 2167c159d30cSDavid S. Miller 21681da177e4SLinus Torvalds return 0; 21691da177e4SLinus Torvalds } 21701da177e4SLinus Torvalds 2171f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev) 21721da177e4SLinus Torvalds { 21738ed67789SDaniel Lezcano struct arg_dev_net adn = { 21748ed67789SDaniel Lezcano .dev = dev, 21758ed67789SDaniel Lezcano .net = net, 21768ed67789SDaniel Lezcano }; 21778ed67789SDaniel Lezcano 21788ed67789SDaniel Lezcano fib6_clean_all(net, fib6_ifdown, 0, &adn); 21791e493d19SDavid S. Miller icmp6_clean_all(fib6_ifdown, &adn); 21801da177e4SLinus Torvalds } 21811da177e4SLinus Torvalds 21821da177e4SLinus Torvalds struct rt6_mtu_change_arg 21831da177e4SLinus Torvalds { 21841da177e4SLinus Torvalds struct net_device *dev; 21851da177e4SLinus Torvalds unsigned mtu; 21861da177e4SLinus Torvalds }; 21871da177e4SLinus Torvalds 21881da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg) 21891da177e4SLinus Torvalds { 21901da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 21911da177e4SLinus Torvalds struct inet6_dev *idev; 21921da177e4SLinus Torvalds 21931da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 21941da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 21951da177e4SLinus Torvalds We still use this lock to block changes 21961da177e4SLinus Torvalds caused by addrconf/ndisc. 21971da177e4SLinus Torvalds */ 21981da177e4SLinus Torvalds 21991da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 220038308473SDavid S. Miller if (!idev) 22011da177e4SLinus Torvalds return 0; 22021da177e4SLinus Torvalds 22031da177e4SLinus Torvalds /* For administrative MTU increase, there is no way to discover 22041da177e4SLinus Torvalds IPv6 PMTU increase, so PMTU increase should be updated here. 22051da177e4SLinus Torvalds Since RFC 1981 doesn't include administrative MTU increase 22061da177e4SLinus Torvalds update PMTU increase is a MUST. (i.e. jumbo frame) 22071da177e4SLinus Torvalds */ 22081da177e4SLinus Torvalds /* 22091da177e4SLinus Torvalds If new MTU is less than route PMTU, this new MTU will be the 22101da177e4SLinus Torvalds lowest MTU in the path, update the route PMTU to reflect PMTU 22111da177e4SLinus Torvalds decreases; if new MTU is greater than route PMTU, and the 22121da177e4SLinus Torvalds old MTU is the lowest MTU in the path, update the route PMTU 22131da177e4SLinus Torvalds to reflect the increase. In this case if the other nodes' MTU 22141da177e4SLinus Torvalds also have the lowest MTU, TOO BIG MESSAGE will be lead to 22151da177e4SLinus Torvalds PMTU discouvery. 22161da177e4SLinus Torvalds */ 2217d1918542SDavid S. Miller if (rt->dst.dev == arg->dev && 2218d8d1f30bSChangli Gao !dst_metric_locked(&rt->dst, RTAX_MTU) && 2219d8d1f30bSChangli Gao (dst_mtu(&rt->dst) >= arg->mtu || 2220d8d1f30bSChangli Gao (dst_mtu(&rt->dst) < arg->mtu && 2221d8d1f30bSChangli Gao dst_mtu(&rt->dst) == idev->cnf.mtu6))) { 2222defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu); 2223566cfd8fSSimon Arlott } 22241da177e4SLinus Torvalds return 0; 22251da177e4SLinus Torvalds } 22261da177e4SLinus Torvalds 22271da177e4SLinus Torvalds void rt6_mtu_change(struct net_device *dev, unsigned mtu) 22281da177e4SLinus Torvalds { 2229c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 2230c71099acSThomas Graf .dev = dev, 2231c71099acSThomas Graf .mtu = mtu, 2232c71099acSThomas Graf }; 22331da177e4SLinus Torvalds 2234c346dca1SYOSHIFUJI Hideaki fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg); 22351da177e4SLinus Torvalds } 22361da177e4SLinus Torvalds 2237ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 22385176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 223986872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 2240ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 224186872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 224286872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 224386872cb5SThomas Graf }; 224486872cb5SThomas Graf 224586872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 224686872cb5SThomas Graf struct fib6_config *cfg) 22471da177e4SLinus Torvalds { 224886872cb5SThomas Graf struct rtmsg *rtm; 224986872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 225086872cb5SThomas Graf int err; 22511da177e4SLinus Torvalds 225286872cb5SThomas Graf err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy); 225386872cb5SThomas Graf if (err < 0) 225486872cb5SThomas Graf goto errout; 22551da177e4SLinus Torvalds 225686872cb5SThomas Graf err = -EINVAL; 225786872cb5SThomas Graf rtm = nlmsg_data(nlh); 225886872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 225986872cb5SThomas Graf 226086872cb5SThomas Graf cfg->fc_table = rtm->rtm_table; 226186872cb5SThomas Graf cfg->fc_dst_len = rtm->rtm_dst_len; 226286872cb5SThomas Graf cfg->fc_src_len = rtm->rtm_src_len; 226386872cb5SThomas Graf cfg->fc_flags = RTF_UP; 226486872cb5SThomas Graf cfg->fc_protocol = rtm->rtm_protocol; 226586872cb5SThomas Graf 226686872cb5SThomas Graf if (rtm->rtm_type == RTN_UNREACHABLE) 226786872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 226886872cb5SThomas Graf 2269ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 2270ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 2271ab79ad14SMaciej Żenczykowski 227286872cb5SThomas Graf cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid; 227386872cb5SThomas Graf cfg->fc_nlinfo.nlh = nlh; 22743b1e0a65SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = sock_net(skb->sk); 227586872cb5SThomas Graf 227686872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 227786872cb5SThomas Graf nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16); 227886872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 22791da177e4SLinus Torvalds } 228086872cb5SThomas Graf 228186872cb5SThomas Graf if (tb[RTA_DST]) { 228286872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 228386872cb5SThomas Graf 228486872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 228586872cb5SThomas Graf goto errout; 228686872cb5SThomas Graf 228786872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 22881da177e4SLinus Torvalds } 228986872cb5SThomas Graf 229086872cb5SThomas Graf if (tb[RTA_SRC]) { 229186872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 229286872cb5SThomas Graf 229386872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 229486872cb5SThomas Graf goto errout; 229586872cb5SThomas Graf 229686872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 22971da177e4SLinus Torvalds } 229886872cb5SThomas Graf 2299c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 2300c3968a85SDaniel Walter nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16); 2301c3968a85SDaniel Walter 230286872cb5SThomas Graf if (tb[RTA_OIF]) 230386872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 230486872cb5SThomas Graf 230586872cb5SThomas Graf if (tb[RTA_PRIORITY]) 230686872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 230786872cb5SThomas Graf 230886872cb5SThomas Graf if (tb[RTA_METRICS]) { 230986872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 231086872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 23111da177e4SLinus Torvalds } 231286872cb5SThomas Graf 231386872cb5SThomas Graf if (tb[RTA_TABLE]) 231486872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 231586872cb5SThomas Graf 231686872cb5SThomas Graf err = 0; 231786872cb5SThomas Graf errout: 231886872cb5SThomas Graf return err; 23191da177e4SLinus Torvalds } 23201da177e4SLinus Torvalds 2321c127ea2cSThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 23221da177e4SLinus Torvalds { 232386872cb5SThomas Graf struct fib6_config cfg; 232486872cb5SThomas Graf int err; 23251da177e4SLinus Torvalds 232686872cb5SThomas Graf err = rtm_to_fib6_config(skb, nlh, &cfg); 232786872cb5SThomas Graf if (err < 0) 232886872cb5SThomas Graf return err; 232986872cb5SThomas Graf 233086872cb5SThomas Graf return ip6_route_del(&cfg); 23311da177e4SLinus Torvalds } 23321da177e4SLinus Torvalds 2333c127ea2cSThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 23341da177e4SLinus Torvalds { 233586872cb5SThomas Graf struct fib6_config cfg; 233686872cb5SThomas Graf int err; 23371da177e4SLinus Torvalds 233886872cb5SThomas Graf err = rtm_to_fib6_config(skb, nlh, &cfg); 233986872cb5SThomas Graf if (err < 0) 234086872cb5SThomas Graf return err; 234186872cb5SThomas Graf 234286872cb5SThomas Graf return ip6_route_add(&cfg); 23431da177e4SLinus Torvalds } 23441da177e4SLinus Torvalds 2345339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void) 2346339bf98fSThomas Graf { 2347339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 2348339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 2349339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 2350339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 2351339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 2352339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 2353339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 2354339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 2355339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 23566a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 2357339bf98fSThomas Graf + nla_total_size(sizeof(struct rta_cacheinfo)); 2358339bf98fSThomas Graf } 2359339bf98fSThomas Graf 2360191cd582SBrian Haley static int rt6_fill_node(struct net *net, 2361191cd582SBrian Haley struct sk_buff *skb, struct rt6_info *rt, 23620d51aa80SJamal Hadi Salim struct in6_addr *dst, struct in6_addr *src, 23630d51aa80SJamal Hadi Salim int iif, int type, u32 pid, u32 seq, 23647bc570c8SYOSHIFUJI Hideaki int prefix, int nowait, unsigned int flags) 23651da177e4SLinus Torvalds { 2366346f870bSDavid S. Miller const struct inet_peer *peer; 23671da177e4SLinus Torvalds struct rtmsg *rtm; 23681da177e4SLinus Torvalds struct nlmsghdr *nlh; 2369e3703b3dSThomas Graf long expires; 23709e762a4aSPatrick McHardy u32 table; 2371f2c31e32SEric Dumazet struct neighbour *n; 2372346f870bSDavid S. Miller u32 ts, tsage; 23731da177e4SLinus Torvalds 23741da177e4SLinus Torvalds if (prefix) { /* user wants prefix routes only */ 23751da177e4SLinus Torvalds if (!(rt->rt6i_flags & RTF_PREFIX_RT)) { 23761da177e4SLinus Torvalds /* success since this is not a prefix route */ 23771da177e4SLinus Torvalds return 1; 23781da177e4SLinus Torvalds } 23791da177e4SLinus Torvalds } 23801da177e4SLinus Torvalds 23812d7202bfSThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags); 238238308473SDavid S. Miller if (!nlh) 238326932566SPatrick McHardy return -EMSGSIZE; 23842d7202bfSThomas Graf 23852d7202bfSThomas Graf rtm = nlmsg_data(nlh); 23861da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 23871da177e4SLinus Torvalds rtm->rtm_dst_len = rt->rt6i_dst.plen; 23881da177e4SLinus Torvalds rtm->rtm_src_len = rt->rt6i_src.plen; 23891da177e4SLinus Torvalds rtm->rtm_tos = 0; 2390c71099acSThomas Graf if (rt->rt6i_table) 23919e762a4aSPatrick McHardy table = rt->rt6i_table->tb6_id; 2392c71099acSThomas Graf else 23939e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 23949e762a4aSPatrick McHardy rtm->rtm_table = table; 23952d7202bfSThomas Graf NLA_PUT_U32(skb, RTA_TABLE, table); 23961da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_REJECT) 23971da177e4SLinus Torvalds rtm->rtm_type = RTN_UNREACHABLE; 2398ab79ad14SMaciej Żenczykowski else if (rt->rt6i_flags & RTF_LOCAL) 2399ab79ad14SMaciej Żenczykowski rtm->rtm_type = RTN_LOCAL; 2400d1918542SDavid S. Miller else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK)) 24011da177e4SLinus Torvalds rtm->rtm_type = RTN_LOCAL; 24021da177e4SLinus Torvalds else 24031da177e4SLinus Torvalds rtm->rtm_type = RTN_UNICAST; 24041da177e4SLinus Torvalds rtm->rtm_flags = 0; 24051da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 24061da177e4SLinus Torvalds rtm->rtm_protocol = rt->rt6i_protocol; 24071da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_DYNAMIC) 24081da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_REDIRECT; 24091da177e4SLinus Torvalds else if (rt->rt6i_flags & RTF_ADDRCONF) 24101da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_KERNEL; 24111da177e4SLinus Torvalds else if (rt->rt6i_flags & RTF_DEFAULT) 24121da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_RA; 24131da177e4SLinus Torvalds 24141da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) 24151da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 24161da177e4SLinus Torvalds 24171da177e4SLinus Torvalds if (dst) { 24182d7202bfSThomas Graf NLA_PUT(skb, RTA_DST, 16, dst); 24191da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 24201da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 24212d7202bfSThomas Graf NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr); 24221da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 24231da177e4SLinus Torvalds if (src) { 24242d7202bfSThomas Graf NLA_PUT(skb, RTA_SRC, 16, src); 24251da177e4SLinus Torvalds rtm->rtm_src_len = 128; 24261da177e4SLinus Torvalds } else if (rtm->rtm_src_len) 24272d7202bfSThomas Graf NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr); 24281da177e4SLinus Torvalds #endif 24297bc570c8SYOSHIFUJI Hideaki if (iif) { 24307bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 24317bc570c8SYOSHIFUJI Hideaki if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) { 24328229efdaSBenjamin Thery int err = ip6mr_get_route(net, skb, rtm, nowait); 24337bc570c8SYOSHIFUJI Hideaki if (err <= 0) { 24347bc570c8SYOSHIFUJI Hideaki if (!nowait) { 24357bc570c8SYOSHIFUJI Hideaki if (err == 0) 24367bc570c8SYOSHIFUJI Hideaki return 0; 24377bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 24387bc570c8SYOSHIFUJI Hideaki } else { 24397bc570c8SYOSHIFUJI Hideaki if (err == -EMSGSIZE) 24407bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 24417bc570c8SYOSHIFUJI Hideaki } 24427bc570c8SYOSHIFUJI Hideaki } 24437bc570c8SYOSHIFUJI Hideaki } else 24447bc570c8SYOSHIFUJI Hideaki #endif 24452d7202bfSThomas Graf NLA_PUT_U32(skb, RTA_IIF, iif); 24467bc570c8SYOSHIFUJI Hideaki } else if (dst) { 24471da177e4SLinus Torvalds struct in6_addr saddr_buf; 2448c3968a85SDaniel Walter if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0) 2449c3968a85SDaniel Walter NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf); 2450c3968a85SDaniel Walter } 2451c3968a85SDaniel Walter 2452c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) { 2453c3968a85SDaniel Walter struct in6_addr saddr_buf; 24544e3fd7a0SAlexey Dobriyan saddr_buf = rt->rt6i_prefsrc.addr; 24552d7202bfSThomas Graf NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf); 24561da177e4SLinus Torvalds } 24572d7202bfSThomas Graf 2458defb3519SDavid S. Miller if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0) 24592d7202bfSThomas Graf goto nla_put_failure; 24602d7202bfSThomas Graf 2461f2c31e32SEric Dumazet rcu_read_lock(); 246227217455SDavid Miller n = dst_get_neighbour_noref(&rt->dst); 2463f2c31e32SEric Dumazet if (n) 2464f2c31e32SEric Dumazet NLA_PUT(skb, RTA_GATEWAY, 16, &n->primary_key); 2465f2c31e32SEric Dumazet rcu_read_unlock(); 24662d7202bfSThomas Graf 2467d8d1f30bSChangli Gao if (rt->dst.dev) 2468d1918542SDavid S. Miller NLA_PUT_U32(skb, RTA_OIF, rt->dst.dev->ifindex); 24692d7202bfSThomas Graf 24702d7202bfSThomas Graf NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric); 2471e3703b3dSThomas Graf 247236e3deaeSYOSHIFUJI Hideaki if (!(rt->rt6i_flags & RTF_EXPIRES)) 247336e3deaeSYOSHIFUJI Hideaki expires = 0; 2474d1918542SDavid S. Miller else if (rt->dst.expires - jiffies < INT_MAX) 2475d1918542SDavid S. Miller expires = rt->dst.expires - jiffies; 247636e3deaeSYOSHIFUJI Hideaki else 247736e3deaeSYOSHIFUJI Hideaki expires = INT_MAX; 247869cdf8f9SYOSHIFUJI Hideaki 2479346f870bSDavid S. Miller peer = rt->rt6i_peer; 2480346f870bSDavid S. Miller ts = tsage = 0; 2481346f870bSDavid S. Miller if (peer && peer->tcp_ts_stamp) { 2482346f870bSDavid S. Miller ts = peer->tcp_ts; 2483346f870bSDavid S. Miller tsage = get_seconds() - peer->tcp_ts_stamp; 2484346f870bSDavid S. Miller } 2485346f870bSDavid S. Miller 2486346f870bSDavid S. Miller if (rtnl_put_cacheinfo(skb, &rt->dst, 0, ts, tsage, 2487d8d1f30bSChangli Gao expires, rt->dst.error) < 0) 2488e3703b3dSThomas Graf goto nla_put_failure; 24891da177e4SLinus Torvalds 24902d7202bfSThomas Graf return nlmsg_end(skb, nlh); 24912d7202bfSThomas Graf 24922d7202bfSThomas Graf nla_put_failure: 249326932566SPatrick McHardy nlmsg_cancel(skb, nlh); 249426932566SPatrick McHardy return -EMSGSIZE; 24951da177e4SLinus Torvalds } 24961da177e4SLinus Torvalds 24971b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg) 24981da177e4SLinus Torvalds { 24991da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 25001da177e4SLinus Torvalds int prefix; 25011da177e4SLinus Torvalds 25022d7202bfSThomas Graf if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { 25032d7202bfSThomas Graf struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); 25041da177e4SLinus Torvalds prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0; 25051da177e4SLinus Torvalds } else 25061da177e4SLinus Torvalds prefix = 0; 25071da177e4SLinus Torvalds 2508191cd582SBrian Haley return rt6_fill_node(arg->net, 2509191cd582SBrian Haley arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE, 25101da177e4SLinus Torvalds NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq, 25117bc570c8SYOSHIFUJI Hideaki prefix, 0, NLM_F_MULTI); 25121da177e4SLinus Torvalds } 25131da177e4SLinus Torvalds 2514c127ea2cSThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg) 25151da177e4SLinus Torvalds { 25163b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 2517ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 25181da177e4SLinus Torvalds struct rt6_info *rt; 2519ab364a6fSThomas Graf struct sk_buff *skb; 2520ab364a6fSThomas Graf struct rtmsg *rtm; 25214c9483b2SDavid S. Miller struct flowi6 fl6; 2522ab364a6fSThomas Graf int err, iif = 0; 2523ab364a6fSThomas Graf 2524ab364a6fSThomas Graf err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy); 2525ab364a6fSThomas Graf if (err < 0) 2526ab364a6fSThomas Graf goto errout; 2527ab364a6fSThomas Graf 2528ab364a6fSThomas Graf err = -EINVAL; 25294c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 2530ab364a6fSThomas Graf 2531ab364a6fSThomas Graf if (tb[RTA_SRC]) { 2532ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 2533ab364a6fSThomas Graf goto errout; 2534ab364a6fSThomas Graf 25354e3fd7a0SAlexey Dobriyan fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 2536ab364a6fSThomas Graf } 2537ab364a6fSThomas Graf 2538ab364a6fSThomas Graf if (tb[RTA_DST]) { 2539ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 2540ab364a6fSThomas Graf goto errout; 2541ab364a6fSThomas Graf 25424e3fd7a0SAlexey Dobriyan fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 2543ab364a6fSThomas Graf } 2544ab364a6fSThomas Graf 2545ab364a6fSThomas Graf if (tb[RTA_IIF]) 2546ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 2547ab364a6fSThomas Graf 2548ab364a6fSThomas Graf if (tb[RTA_OIF]) 25494c9483b2SDavid S. Miller fl6.flowi6_oif = nla_get_u32(tb[RTA_OIF]); 2550ab364a6fSThomas Graf 2551ab364a6fSThomas Graf if (iif) { 2552ab364a6fSThomas Graf struct net_device *dev; 25535578689aSDaniel Lezcano dev = __dev_get_by_index(net, iif); 2554ab364a6fSThomas Graf if (!dev) { 2555ab364a6fSThomas Graf err = -ENODEV; 2556ab364a6fSThomas Graf goto errout; 2557ab364a6fSThomas Graf } 2558ab364a6fSThomas Graf } 25591da177e4SLinus Torvalds 25601da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 256138308473SDavid S. Miller if (!skb) { 2562ab364a6fSThomas Graf err = -ENOBUFS; 2563ab364a6fSThomas Graf goto errout; 2564ab364a6fSThomas Graf } 25651da177e4SLinus Torvalds 25661da177e4SLinus Torvalds /* Reserve room for dummy headers, this skb can pass 25671da177e4SLinus Torvalds through good chunk of routing engine. 25681da177e4SLinus Torvalds */ 2569459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 25701da177e4SLinus Torvalds skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr)); 25711da177e4SLinus Torvalds 25724c9483b2SDavid S. Miller rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl6); 2573d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 25741da177e4SLinus Torvalds 25754c9483b2SDavid S. Miller err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif, 25761da177e4SLinus Torvalds RTM_NEWROUTE, NETLINK_CB(in_skb).pid, 25777bc570c8SYOSHIFUJI Hideaki nlh->nlmsg_seq, 0, 0, 0); 25781da177e4SLinus Torvalds if (err < 0) { 2579ab364a6fSThomas Graf kfree_skb(skb); 2580ab364a6fSThomas Graf goto errout; 25811da177e4SLinus Torvalds } 25821da177e4SLinus Torvalds 25835578689aSDaniel Lezcano err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid); 2584ab364a6fSThomas Graf errout: 25851da177e4SLinus Torvalds return err; 25861da177e4SLinus Torvalds } 25871da177e4SLinus Torvalds 258886872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info) 25891da177e4SLinus Torvalds { 25901da177e4SLinus Torvalds struct sk_buff *skb; 25915578689aSDaniel Lezcano struct net *net = info->nl_net; 2592528c4cebSDenis V. Lunev u32 seq; 2593528c4cebSDenis V. Lunev int err; 25940d51aa80SJamal Hadi Salim 2595528c4cebSDenis V. Lunev err = -ENOBUFS; 259638308473SDavid S. Miller seq = info->nlh ? info->nlh->nlmsg_seq : 0; 259786872cb5SThomas Graf 2598339bf98fSThomas Graf skb = nlmsg_new(rt6_nlmsg_size(), gfp_any()); 259938308473SDavid S. Miller if (!skb) 260021713ebcSThomas Graf goto errout; 26011da177e4SLinus Torvalds 2602191cd582SBrian Haley err = rt6_fill_node(net, skb, rt, NULL, NULL, 0, 26037bc570c8SYOSHIFUJI Hideaki event, info->pid, seq, 0, 0, 0); 260426932566SPatrick McHardy if (err < 0) { 260526932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 260626932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 260726932566SPatrick McHardy kfree_skb(skb); 260826932566SPatrick McHardy goto errout; 260926932566SPatrick McHardy } 26101ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE, 26115578689aSDaniel Lezcano info->nlh, gfp_any()); 26121ce85fe4SPablo Neira Ayuso return; 261321713ebcSThomas Graf errout: 261421713ebcSThomas Graf if (err < 0) 26155578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 26161da177e4SLinus Torvalds } 26171da177e4SLinus Torvalds 26188ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 26198ed67789SDaniel Lezcano unsigned long event, void *data) 26208ed67789SDaniel Lezcano { 26218ed67789SDaniel Lezcano struct net_device *dev = (struct net_device *)data; 2622c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 26238ed67789SDaniel Lezcano 26248ed67789SDaniel Lezcano if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) { 2625d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 26268ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 26278ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 2628d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 26298ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 2630d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 26318ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 26328ed67789SDaniel Lezcano #endif 26338ed67789SDaniel Lezcano } 26348ed67789SDaniel Lezcano 26358ed67789SDaniel Lezcano return NOTIFY_OK; 26368ed67789SDaniel Lezcano } 26378ed67789SDaniel Lezcano 26381da177e4SLinus Torvalds /* 26391da177e4SLinus Torvalds * /proc 26401da177e4SLinus Torvalds */ 26411da177e4SLinus Torvalds 26421da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 26431da177e4SLinus Torvalds 26441da177e4SLinus Torvalds struct rt6_proc_arg 26451da177e4SLinus Torvalds { 26461da177e4SLinus Torvalds char *buffer; 26471da177e4SLinus Torvalds int offset; 26481da177e4SLinus Torvalds int length; 26491da177e4SLinus Torvalds int skip; 26501da177e4SLinus Torvalds int len; 26511da177e4SLinus Torvalds }; 26521da177e4SLinus Torvalds 26531da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg) 26541da177e4SLinus Torvalds { 265533120b30SAlexey Dobriyan struct seq_file *m = p_arg; 265669cce1d1SDavid S. Miller struct neighbour *n; 26571da177e4SLinus Torvalds 26584b7a4274SHarvey Harrison seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen); 26591da177e4SLinus Torvalds 26601da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 26614b7a4274SHarvey Harrison seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen); 26621da177e4SLinus Torvalds #else 266333120b30SAlexey Dobriyan seq_puts(m, "00000000000000000000000000000000 00 "); 26641da177e4SLinus Torvalds #endif 2665f2c31e32SEric Dumazet rcu_read_lock(); 266627217455SDavid Miller n = dst_get_neighbour_noref(&rt->dst); 266769cce1d1SDavid S. Miller if (n) { 266869cce1d1SDavid S. Miller seq_printf(m, "%pi6", n->primary_key); 26691da177e4SLinus Torvalds } else { 267033120b30SAlexey Dobriyan seq_puts(m, "00000000000000000000000000000000"); 26711da177e4SLinus Torvalds } 2672f2c31e32SEric Dumazet rcu_read_unlock(); 267333120b30SAlexey Dobriyan seq_printf(m, " %08x %08x %08x %08x %8s\n", 2674d8d1f30bSChangli Gao rt->rt6i_metric, atomic_read(&rt->dst.__refcnt), 2675d8d1f30bSChangli Gao rt->dst.__use, rt->rt6i_flags, 2676d1918542SDavid S. Miller rt->dst.dev ? rt->dst.dev->name : ""); 26771da177e4SLinus Torvalds return 0; 26781da177e4SLinus Torvalds } 26791da177e4SLinus Torvalds 268033120b30SAlexey Dobriyan static int ipv6_route_show(struct seq_file *m, void *v) 26811da177e4SLinus Torvalds { 2682f3db4851SDaniel Lezcano struct net *net = (struct net *)m->private; 2683f3db4851SDaniel Lezcano fib6_clean_all(net, rt6_info_route, 0, m); 268433120b30SAlexey Dobriyan return 0; 26851da177e4SLinus Torvalds } 26861da177e4SLinus Torvalds 268733120b30SAlexey Dobriyan static int ipv6_route_open(struct inode *inode, struct file *file) 268833120b30SAlexey Dobriyan { 2689de05c557SPavel Emelyanov return single_open_net(inode, file, ipv6_route_show); 2690f3db4851SDaniel Lezcano } 2691f3db4851SDaniel Lezcano 269233120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = { 269333120b30SAlexey Dobriyan .owner = THIS_MODULE, 269433120b30SAlexey Dobriyan .open = ipv6_route_open, 269533120b30SAlexey Dobriyan .read = seq_read, 269633120b30SAlexey Dobriyan .llseek = seq_lseek, 2697b6fcbdb4SPavel Emelyanov .release = single_release_net, 269833120b30SAlexey Dobriyan }; 269933120b30SAlexey Dobriyan 27001da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 27011da177e4SLinus Torvalds { 270269ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 27031da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 270469ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 270569ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 270669ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_alloc, 270769ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 270869ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 2709fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 271069ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 27111da177e4SLinus Torvalds 27121da177e4SLinus Torvalds return 0; 27131da177e4SLinus Torvalds } 27141da177e4SLinus Torvalds 27151da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file) 27161da177e4SLinus Torvalds { 2717de05c557SPavel Emelyanov return single_open_net(inode, file, rt6_stats_seq_show); 271869ddb805SDaniel Lezcano } 271969ddb805SDaniel Lezcano 27209a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = { 27211da177e4SLinus Torvalds .owner = THIS_MODULE, 27221da177e4SLinus Torvalds .open = rt6_stats_seq_open, 27231da177e4SLinus Torvalds .read = seq_read, 27241da177e4SLinus Torvalds .llseek = seq_lseek, 2725b6fcbdb4SPavel Emelyanov .release = single_release_net, 27261da177e4SLinus Torvalds }; 27271da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 27281da177e4SLinus Torvalds 27291da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 27301da177e4SLinus Torvalds 27311da177e4SLinus Torvalds static 27328d65af78SAlexey Dobriyan int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write, 27331da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 27341da177e4SLinus Torvalds { 2735c486da34SLucian Adrian Grijincu struct net *net; 2736c486da34SLucian Adrian Grijincu int delay; 2737c486da34SLucian Adrian Grijincu if (!write) 2738c486da34SLucian Adrian Grijincu return -EINVAL; 2739c486da34SLucian Adrian Grijincu 2740c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 2741c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 27428d65af78SAlexey Dobriyan proc_dointvec(ctl, write, buffer, lenp, ppos); 27435b7c931dSDaniel Lezcano fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net); 27441da177e4SLinus Torvalds return 0; 27451da177e4SLinus Torvalds } 27461da177e4SLinus Torvalds 2747760f2d01SDaniel Lezcano ctl_table ipv6_route_table_template[] = { 27481da177e4SLinus Torvalds { 27491da177e4SLinus Torvalds .procname = "flush", 27504990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 27511da177e4SLinus Torvalds .maxlen = sizeof(int), 275289c8b3a1SDave Jones .mode = 0200, 27536d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 27541da177e4SLinus Torvalds }, 27551da177e4SLinus Torvalds { 27561da177e4SLinus Torvalds .procname = "gc_thresh", 27579a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 27581da177e4SLinus Torvalds .maxlen = sizeof(int), 27591da177e4SLinus Torvalds .mode = 0644, 27606d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 27611da177e4SLinus Torvalds }, 27621da177e4SLinus Torvalds { 27631da177e4SLinus Torvalds .procname = "max_size", 27644990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 27651da177e4SLinus Torvalds .maxlen = sizeof(int), 27661da177e4SLinus Torvalds .mode = 0644, 27676d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 27681da177e4SLinus Torvalds }, 27691da177e4SLinus Torvalds { 27701da177e4SLinus Torvalds .procname = "gc_min_interval", 27714990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 27721da177e4SLinus Torvalds .maxlen = sizeof(int), 27731da177e4SLinus Torvalds .mode = 0644, 27746d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 27751da177e4SLinus Torvalds }, 27761da177e4SLinus Torvalds { 27771da177e4SLinus Torvalds .procname = "gc_timeout", 27784990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 27791da177e4SLinus Torvalds .maxlen = sizeof(int), 27801da177e4SLinus Torvalds .mode = 0644, 27816d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 27821da177e4SLinus Torvalds }, 27831da177e4SLinus Torvalds { 27841da177e4SLinus Torvalds .procname = "gc_interval", 27854990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 27861da177e4SLinus Torvalds .maxlen = sizeof(int), 27871da177e4SLinus Torvalds .mode = 0644, 27886d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 27891da177e4SLinus Torvalds }, 27901da177e4SLinus Torvalds { 27911da177e4SLinus Torvalds .procname = "gc_elasticity", 27924990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 27931da177e4SLinus Torvalds .maxlen = sizeof(int), 27941da177e4SLinus Torvalds .mode = 0644, 2795f3d3f616SMin Zhang .proc_handler = proc_dointvec, 27961da177e4SLinus Torvalds }, 27971da177e4SLinus Torvalds { 27981da177e4SLinus Torvalds .procname = "mtu_expires", 27994990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 28001da177e4SLinus Torvalds .maxlen = sizeof(int), 28011da177e4SLinus Torvalds .mode = 0644, 28026d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 28031da177e4SLinus Torvalds }, 28041da177e4SLinus Torvalds { 28051da177e4SLinus Torvalds .procname = "min_adv_mss", 28064990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 28071da177e4SLinus Torvalds .maxlen = sizeof(int), 28081da177e4SLinus Torvalds .mode = 0644, 2809f3d3f616SMin Zhang .proc_handler = proc_dointvec, 28101da177e4SLinus Torvalds }, 28111da177e4SLinus Torvalds { 28121da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 28134990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 28141da177e4SLinus Torvalds .maxlen = sizeof(int), 28151da177e4SLinus Torvalds .mode = 0644, 28166d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 28171da177e4SLinus Torvalds }, 2818f8572d8fSEric W. Biederman { } 28191da177e4SLinus Torvalds }; 28201da177e4SLinus Torvalds 28212c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 2822760f2d01SDaniel Lezcano { 2823760f2d01SDaniel Lezcano struct ctl_table *table; 2824760f2d01SDaniel Lezcano 2825760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 2826760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 2827760f2d01SDaniel Lezcano GFP_KERNEL); 28285ee09105SYOSHIFUJI Hideaki 28295ee09105SYOSHIFUJI Hideaki if (table) { 28305ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 2831c486da34SLucian Adrian Grijincu table[0].extra1 = net; 283286393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 28335ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 28345ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 28355ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 28365ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 28375ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 28385ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 28395ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 28409c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 28415ee09105SYOSHIFUJI Hideaki } 28425ee09105SYOSHIFUJI Hideaki 2843760f2d01SDaniel Lezcano return table; 2844760f2d01SDaniel Lezcano } 28451da177e4SLinus Torvalds #endif 28461da177e4SLinus Torvalds 28472c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 2848cdb18761SDaniel Lezcano { 2849633d424bSPavel Emelyanov int ret = -ENOMEM; 28508ed67789SDaniel Lezcano 285186393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 285286393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 2853f2fc6a54SBenjamin Thery 2854fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 2855fc66f95cSEric Dumazet goto out_ip6_dst_ops; 2856fc66f95cSEric Dumazet 28578ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 28588ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 28598ed67789SDaniel Lezcano GFP_KERNEL); 28608ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 2861fc66f95cSEric Dumazet goto out_ip6_dst_entries; 2862d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.path = 28638ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_null_entry; 2864d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 286562fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 286662fa8a84SDavid S. Miller ip6_template_metrics, true); 28678ed67789SDaniel Lezcano 28688ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 28698ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 28708ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 28718ed67789SDaniel Lezcano GFP_KERNEL); 287268fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 287368fffc67SPeter Zijlstra goto out_ip6_null_entry; 2874d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.path = 28758ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_prohibit_entry; 2876d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 287762fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 287862fa8a84SDavid S. Miller ip6_template_metrics, true); 28798ed67789SDaniel Lezcano 28808ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 28818ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 28828ed67789SDaniel Lezcano GFP_KERNEL); 288368fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 288468fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 2885d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.path = 28868ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_blk_hole_entry; 2887d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 288862fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 288962fa8a84SDavid S. Miller ip6_template_metrics, true); 28908ed67789SDaniel Lezcano #endif 28918ed67789SDaniel Lezcano 2892b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 2893b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 2894b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 2895b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 2896b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 2897b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 2898b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 2899b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 2900b339a47cSPeter Zijlstra 2901cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS 2902cdb18761SDaniel Lezcano proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops); 2903cdb18761SDaniel Lezcano proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops); 2904cdb18761SDaniel Lezcano #endif 29056891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 29066891a346SBenjamin Thery 29078ed67789SDaniel Lezcano ret = 0; 29088ed67789SDaniel Lezcano out: 29098ed67789SDaniel Lezcano return ret; 2910f2fc6a54SBenjamin Thery 291168fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 291268fffc67SPeter Zijlstra out_ip6_prohibit_entry: 291368fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 291468fffc67SPeter Zijlstra out_ip6_null_entry: 291568fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 291668fffc67SPeter Zijlstra #endif 2917fc66f95cSEric Dumazet out_ip6_dst_entries: 2918fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 2919f2fc6a54SBenjamin Thery out_ip6_dst_ops: 2920f2fc6a54SBenjamin Thery goto out; 2921cdb18761SDaniel Lezcano } 2922cdb18761SDaniel Lezcano 29232c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 2924cdb18761SDaniel Lezcano { 2925cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS 2926cdb18761SDaniel Lezcano proc_net_remove(net, "ipv6_route"); 2927cdb18761SDaniel Lezcano proc_net_remove(net, "rt6_stats"); 2928cdb18761SDaniel Lezcano #endif 29298ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 29308ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 29318ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 29328ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 29338ed67789SDaniel Lezcano #endif 293441bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 2935cdb18761SDaniel Lezcano } 2936cdb18761SDaniel Lezcano 2937cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 2938cdb18761SDaniel Lezcano .init = ip6_route_net_init, 2939cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 2940cdb18761SDaniel Lezcano }; 2941cdb18761SDaniel Lezcano 29428ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 29438ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 29448ed67789SDaniel Lezcano .priority = 0, 29458ed67789SDaniel Lezcano }; 29468ed67789SDaniel Lezcano 2947433d49c3SDaniel Lezcano int __init ip6_route_init(void) 29481da177e4SLinus Torvalds { 2949433d49c3SDaniel Lezcano int ret; 2950433d49c3SDaniel Lezcano 29519a7ec3a9SDaniel Lezcano ret = -ENOMEM; 29529a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 29539a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 29549a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 29559a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 2956c19a28e1SFernando Carrijo goto out; 295714e50e57SDavid S. Miller 2958fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 29598ed67789SDaniel Lezcano if (ret) 2960bdb3289fSDaniel Lezcano goto out_kmem_cache; 2961bdb3289fSDaniel Lezcano 2962fc66f95cSEric Dumazet ret = register_pernet_subsys(&ip6_route_net_ops); 2963fc66f95cSEric Dumazet if (ret) 2964fc66f95cSEric Dumazet goto out_dst_entries; 2965fc66f95cSEric Dumazet 29665dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 29675dc121e9SArnaud Ebalard 29688ed67789SDaniel Lezcano /* Registering of the loopback is done before this portion of code, 29698ed67789SDaniel Lezcano * the loopback reference in rt6_info will not be taken, do it 29708ed67789SDaniel Lezcano * manually for init_net */ 2971d8d1f30bSChangli Gao init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 29728ed67789SDaniel Lezcano init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 2973bdb3289fSDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 2974d8d1f30bSChangli Gao init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 29758ed67789SDaniel Lezcano init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 2976d8d1f30bSChangli Gao init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 29778ed67789SDaniel Lezcano init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 2978bdb3289fSDaniel Lezcano #endif 2979433d49c3SDaniel Lezcano ret = fib6_init(); 2980433d49c3SDaniel Lezcano if (ret) 29818ed67789SDaniel Lezcano goto out_register_subsys; 2982433d49c3SDaniel Lezcano 2983433d49c3SDaniel Lezcano ret = xfrm6_init(); 2984433d49c3SDaniel Lezcano if (ret) 2985cdb18761SDaniel Lezcano goto out_fib6_init; 2986c35b7e72SDaniel Lezcano 2987433d49c3SDaniel Lezcano ret = fib6_rules_init(); 2988433d49c3SDaniel Lezcano if (ret) 2989433d49c3SDaniel Lezcano goto xfrm6_init; 29907e5449c2SDaniel Lezcano 2991433d49c3SDaniel Lezcano ret = -ENOBUFS; 2992c7ac8679SGreg Rose if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) || 2993c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) || 2994c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL)) 2995433d49c3SDaniel Lezcano goto fib6_rules_init; 2996433d49c3SDaniel Lezcano 29978ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 2998cdb18761SDaniel Lezcano if (ret) 2999cdb18761SDaniel Lezcano goto fib6_rules_init; 30008ed67789SDaniel Lezcano 3001433d49c3SDaniel Lezcano out: 3002433d49c3SDaniel Lezcano return ret; 3003433d49c3SDaniel Lezcano 3004433d49c3SDaniel Lezcano fib6_rules_init: 3005433d49c3SDaniel Lezcano fib6_rules_cleanup(); 3006433d49c3SDaniel Lezcano xfrm6_init: 3007433d49c3SDaniel Lezcano xfrm6_fini(); 3008433d49c3SDaniel Lezcano out_fib6_init: 3009433d49c3SDaniel Lezcano fib6_gc_cleanup(); 30108ed67789SDaniel Lezcano out_register_subsys: 30118ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 3012fc66f95cSEric Dumazet out_dst_entries: 3013fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 3014433d49c3SDaniel Lezcano out_kmem_cache: 3015f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 3016433d49c3SDaniel Lezcano goto out; 30171da177e4SLinus Torvalds } 30181da177e4SLinus Torvalds 30191da177e4SLinus Torvalds void ip6_route_cleanup(void) 30201da177e4SLinus Torvalds { 30218ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 3022101367c2SThomas Graf fib6_rules_cleanup(); 30231da177e4SLinus Torvalds xfrm6_fini(); 30241da177e4SLinus Torvalds fib6_gc_cleanup(); 30258ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 302641bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 3027f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 30281da177e4SLinus Torvalds } 3029