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 132f83c7790SDavid S. Miller static int rt6_bind_neighbour(struct rt6_info *rt) 133f83c7790SDavid S. Miller { 134f83c7790SDavid S. Miller struct neighbour *n = ip6_neigh_lookup(&rt->dst, &rt->rt6i_gateway); 135f83c7790SDavid S. Miller if (IS_ERR(n)) 136f83c7790SDavid S. Miller return PTR_ERR(n); 137f83c7790SDavid S. Miller dst_set_neighbour(&rt->dst, n); 138f83c7790SDavid S. Miller 139f83c7790SDavid S. Miller return 0; 140d3aaeb38SDavid S. Miller } 141d3aaeb38SDavid S. Miller 1429a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = { 1431da177e4SLinus Torvalds .family = AF_INET6, 14409640e63SHarvey Harrison .protocol = cpu_to_be16(ETH_P_IPV6), 1451da177e4SLinus Torvalds .gc = ip6_dst_gc, 1461da177e4SLinus Torvalds .gc_thresh = 1024, 1471da177e4SLinus Torvalds .check = ip6_dst_check, 1480dbaee3bSDavid S. Miller .default_advmss = ip6_default_advmss, 149ebb762f2SSteffen Klassert .mtu = ip6_mtu, 15006582540SDavid S. Miller .cow_metrics = ipv6_cow_metrics, 1511da177e4SLinus Torvalds .destroy = ip6_dst_destroy, 1521da177e4SLinus Torvalds .ifdown = ip6_dst_ifdown, 1531da177e4SLinus Torvalds .negative_advice = ip6_negative_advice, 1541da177e4SLinus Torvalds .link_failure = ip6_link_failure, 1551da177e4SLinus Torvalds .update_pmtu = ip6_rt_update_pmtu, 1561ac06e03SHerbert Xu .local_out = __ip6_local_out, 157d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 1581da177e4SLinus Torvalds }; 1591da177e4SLinus Torvalds 160ebb762f2SSteffen Klassert static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst) 161ec831ea7SRoland Dreier { 162618f9bc7SSteffen Klassert unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); 163618f9bc7SSteffen Klassert 164618f9bc7SSteffen Klassert return mtu ? : dst->dev->mtu; 165ec831ea7SRoland Dreier } 166ec831ea7SRoland Dreier 16714e50e57SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu) 16814e50e57SDavid S. Miller { 16914e50e57SDavid S. Miller } 17014e50e57SDavid S. Miller 1710972ddb2SHeld Bernhard static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst, 1720972ddb2SHeld Bernhard unsigned long old) 1730972ddb2SHeld Bernhard { 1740972ddb2SHeld Bernhard return NULL; 1750972ddb2SHeld Bernhard } 1760972ddb2SHeld Bernhard 17714e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = { 17814e50e57SDavid S. Miller .family = AF_INET6, 17909640e63SHarvey Harrison .protocol = cpu_to_be16(ETH_P_IPV6), 18014e50e57SDavid S. Miller .destroy = ip6_dst_destroy, 18114e50e57SDavid S. Miller .check = ip6_dst_check, 182ebb762f2SSteffen Klassert .mtu = ip6_blackhole_mtu, 183214f45c9SEric Dumazet .default_advmss = ip6_default_advmss, 18414e50e57SDavid S. Miller .update_pmtu = ip6_rt_blackhole_update_pmtu, 1850972ddb2SHeld Bernhard .cow_metrics = ip6_rt_blackhole_cow_metrics, 186d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 18714e50e57SDavid S. Miller }; 18814e50e57SDavid S. Miller 18962fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = { 19062fa8a84SDavid S. Miller [RTAX_HOPLIMIT - 1] = 255, 19162fa8a84SDavid S. Miller }; 19262fa8a84SDavid S. Miller 193bdb3289fSDaniel Lezcano static struct rt6_info ip6_null_entry_template = { 1941da177e4SLinus Torvalds .dst = { 1951da177e4SLinus Torvalds .__refcnt = ATOMIC_INIT(1), 1961da177e4SLinus Torvalds .__use = 1, 1971da177e4SLinus Torvalds .obsolete = -1, 1981da177e4SLinus Torvalds .error = -ENETUNREACH, 1991da177e4SLinus Torvalds .input = ip6_pkt_discard, 2001da177e4SLinus Torvalds .output = ip6_pkt_discard_out, 2011da177e4SLinus Torvalds }, 2021da177e4SLinus Torvalds .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 2034f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 2041da177e4SLinus Torvalds .rt6i_metric = ~(u32) 0, 2051da177e4SLinus Torvalds .rt6i_ref = ATOMIC_INIT(1), 2061da177e4SLinus Torvalds }; 2071da177e4SLinus Torvalds 208101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES 209101367c2SThomas Graf 2106723ab54SDavid S. Miller static int ip6_pkt_prohibit(struct sk_buff *skb); 2116723ab54SDavid S. Miller static int ip6_pkt_prohibit_out(struct sk_buff *skb); 2126723ab54SDavid S. Miller 213280a34c8SAdrian Bunk static struct rt6_info ip6_prohibit_entry_template = { 214101367c2SThomas Graf .dst = { 215101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 216101367c2SThomas Graf .__use = 1, 217101367c2SThomas Graf .obsolete = -1, 218101367c2SThomas Graf .error = -EACCES, 2199ce8ade0SThomas Graf .input = ip6_pkt_prohibit, 2209ce8ade0SThomas Graf .output = ip6_pkt_prohibit_out, 221101367c2SThomas Graf }, 222101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 2234f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 224101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 225101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 226101367c2SThomas Graf }; 227101367c2SThomas Graf 228bdb3289fSDaniel Lezcano static struct rt6_info ip6_blk_hole_entry_template = { 229101367c2SThomas Graf .dst = { 230101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 231101367c2SThomas Graf .__use = 1, 232101367c2SThomas Graf .obsolete = -1, 233101367c2SThomas Graf .error = -EINVAL, 234352e512cSHerbert Xu .input = dst_discard, 235352e512cSHerbert Xu .output = dst_discard, 236101367c2SThomas Graf }, 237101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 2384f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 239101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 240101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 241101367c2SThomas Graf }; 242101367c2SThomas Graf 243101367c2SThomas Graf #endif 244101367c2SThomas Graf 2451da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */ 2465c1e6aa3SDavid S. Miller static inline struct rt6_info *ip6_dst_alloc(struct dst_ops *ops, 247957c665fSDavid S. Miller struct net_device *dev, 248957c665fSDavid S. Miller int flags) 2491da177e4SLinus Torvalds { 250957c665fSDavid S. Miller struct rt6_info *rt = dst_alloc(ops, dev, 0, 0, flags); 251cf911662SDavid S. Miller 25238308473SDavid S. Miller if (rt) 253fbe58186SMadalin Bucur memset(&rt->rt6i_table, 0, 254fbe58186SMadalin Bucur sizeof(*rt) - sizeof(struct dst_entry)); 255cf911662SDavid S. Miller 256cf911662SDavid S. Miller return rt; 2571da177e4SLinus Torvalds } 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst) 2601da177e4SLinus Torvalds { 2611da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 2621da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 263b3419363SDavid S. Miller struct inet_peer *peer = rt->rt6i_peer; 2641da177e4SLinus Torvalds 2658e2ec639SYan, Zheng if (!(rt->dst.flags & DST_HOST)) 2668e2ec639SYan, Zheng dst_destroy_metrics_generic(dst); 2678e2ec639SYan, Zheng 26838308473SDavid S. Miller if (idev) { 2691da177e4SLinus Torvalds rt->rt6i_idev = NULL; 2701da177e4SLinus Torvalds in6_dev_put(idev); 2711da177e4SLinus Torvalds } 272b3419363SDavid S. Miller if (peer) { 273b3419363SDavid S. Miller rt->rt6i_peer = NULL; 274b3419363SDavid S. Miller inet_putpeer(peer); 275b3419363SDavid S. Miller } 276b3419363SDavid S. Miller } 277b3419363SDavid S. Miller 2786431cbc2SDavid S. Miller static atomic_t __rt6_peer_genid = ATOMIC_INIT(0); 2796431cbc2SDavid S. Miller 2806431cbc2SDavid S. Miller static u32 rt6_peer_genid(void) 2816431cbc2SDavid S. Miller { 2826431cbc2SDavid S. Miller return atomic_read(&__rt6_peer_genid); 2836431cbc2SDavid S. Miller } 2846431cbc2SDavid S. Miller 285b3419363SDavid S. Miller void rt6_bind_peer(struct rt6_info *rt, int create) 286b3419363SDavid S. Miller { 287b3419363SDavid S. Miller struct inet_peer *peer; 288b3419363SDavid S. Miller 289b3419363SDavid S. Miller peer = inet_getpeer_v6(&rt->rt6i_dst.addr, create); 290b3419363SDavid S. Miller if (peer && cmpxchg(&rt->rt6i_peer, NULL, peer) != NULL) 291b3419363SDavid S. Miller inet_putpeer(peer); 2926431cbc2SDavid S. Miller else 2936431cbc2SDavid S. Miller rt->rt6i_peer_genid = rt6_peer_genid(); 2941da177e4SLinus Torvalds } 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 2971da177e4SLinus Torvalds int how) 2981da177e4SLinus Torvalds { 2991da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 3001da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 3015a3e55d6SDenis V. Lunev struct net_device *loopback_dev = 302c346dca1SYOSHIFUJI Hideaki dev_net(dev)->loopback_dev; 3031da177e4SLinus Torvalds 30438308473SDavid S. Miller if (dev != loopback_dev && idev && idev->dev == dev) { 3055a3e55d6SDenis V. Lunev struct inet6_dev *loopback_idev = 3065a3e55d6SDenis V. Lunev in6_dev_get(loopback_dev); 30738308473SDavid S. Miller if (loopback_idev) { 3081da177e4SLinus Torvalds rt->rt6i_idev = loopback_idev; 3091da177e4SLinus Torvalds in6_dev_put(idev); 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds } 3121da177e4SLinus Torvalds } 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds static __inline__ int rt6_check_expired(const struct rt6_info *rt) 3151da177e4SLinus Torvalds { 316a02cec21SEric Dumazet return (rt->rt6i_flags & RTF_EXPIRES) && 317*d1918542SDavid S. Miller time_after(jiffies, rt->dst.expires); 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds 320b71d1d42SEric Dumazet static inline int rt6_need_strict(const struct in6_addr *daddr) 321c71099acSThomas Graf { 322a02cec21SEric Dumazet return ipv6_addr_type(daddr) & 323a02cec21SEric Dumazet (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK); 324c71099acSThomas Graf } 325c71099acSThomas Graf 3261da177e4SLinus Torvalds /* 327c71099acSThomas Graf * Route lookup. Any table->tb6_lock is implied. 3281da177e4SLinus Torvalds */ 3291da177e4SLinus Torvalds 3308ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net, 3318ed67789SDaniel Lezcano struct rt6_info *rt, 332b71d1d42SEric Dumazet const struct in6_addr *saddr, 3331da177e4SLinus Torvalds int oif, 334d420895eSYOSHIFUJI Hideaki int flags) 3351da177e4SLinus Torvalds { 3361da177e4SLinus Torvalds struct rt6_info *local = NULL; 3371da177e4SLinus Torvalds struct rt6_info *sprt; 3381da177e4SLinus Torvalds 339dd3abc4eSYOSHIFUJI Hideaki if (!oif && ipv6_addr_any(saddr)) 340dd3abc4eSYOSHIFUJI Hideaki goto out; 341dd3abc4eSYOSHIFUJI Hideaki 342d8d1f30bSChangli Gao for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) { 343*d1918542SDavid S. Miller struct net_device *dev = sprt->dst.dev; 344dd3abc4eSYOSHIFUJI Hideaki 345dd3abc4eSYOSHIFUJI Hideaki if (oif) { 3461da177e4SLinus Torvalds if (dev->ifindex == oif) 3471da177e4SLinus Torvalds return sprt; 3481da177e4SLinus Torvalds if (dev->flags & IFF_LOOPBACK) { 34938308473SDavid S. Miller if (!sprt->rt6i_idev || 3501da177e4SLinus Torvalds sprt->rt6i_idev->dev->ifindex != oif) { 351d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE && oif) 3521da177e4SLinus Torvalds continue; 3531da177e4SLinus Torvalds if (local && (!oif || 3541da177e4SLinus Torvalds local->rt6i_idev->dev->ifindex == oif)) 3551da177e4SLinus Torvalds continue; 3561da177e4SLinus Torvalds } 3571da177e4SLinus Torvalds local = sprt; 3581da177e4SLinus Torvalds } 359dd3abc4eSYOSHIFUJI Hideaki } else { 360dd3abc4eSYOSHIFUJI Hideaki if (ipv6_chk_addr(net, saddr, dev, 361dd3abc4eSYOSHIFUJI Hideaki flags & RT6_LOOKUP_F_IFACE)) 362dd3abc4eSYOSHIFUJI Hideaki return sprt; 363dd3abc4eSYOSHIFUJI Hideaki } 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds 366dd3abc4eSYOSHIFUJI Hideaki if (oif) { 3671da177e4SLinus Torvalds if (local) 3681da177e4SLinus Torvalds return local; 3691da177e4SLinus Torvalds 370d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE) 3718ed67789SDaniel Lezcano return net->ipv6.ip6_null_entry; 3721da177e4SLinus Torvalds } 373dd3abc4eSYOSHIFUJI Hideaki out: 3741da177e4SLinus Torvalds return rt; 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds 37727097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 37827097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt) 37927097255SYOSHIFUJI Hideaki { 380f2c31e32SEric Dumazet struct neighbour *neigh; 38127097255SYOSHIFUJI Hideaki /* 38227097255SYOSHIFUJI Hideaki * Okay, this does not seem to be appropriate 38327097255SYOSHIFUJI Hideaki * for now, however, we need to check if it 38427097255SYOSHIFUJI Hideaki * is really so; aka Router Reachability Probing. 38527097255SYOSHIFUJI Hideaki * 38627097255SYOSHIFUJI Hideaki * Router Reachability Probe MUST be rate-limited 38727097255SYOSHIFUJI Hideaki * to no more than one per minute. 38827097255SYOSHIFUJI Hideaki */ 389f2c31e32SEric Dumazet rcu_read_lock(); 39027217455SDavid Miller neigh = rt ? dst_get_neighbour_noref(&rt->dst) : NULL; 39127097255SYOSHIFUJI Hideaki if (!neigh || (neigh->nud_state & NUD_VALID)) 392f2c31e32SEric Dumazet goto out; 39327097255SYOSHIFUJI Hideaki read_lock_bh(&neigh->lock); 39427097255SYOSHIFUJI Hideaki if (!(neigh->nud_state & NUD_VALID) && 39552e16356SYOSHIFUJI Hideaki time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) { 39627097255SYOSHIFUJI Hideaki struct in6_addr mcaddr; 39727097255SYOSHIFUJI Hideaki struct in6_addr *target; 39827097255SYOSHIFUJI Hideaki 39927097255SYOSHIFUJI Hideaki neigh->updated = jiffies; 40027097255SYOSHIFUJI Hideaki read_unlock_bh(&neigh->lock); 40127097255SYOSHIFUJI Hideaki 40227097255SYOSHIFUJI Hideaki target = (struct in6_addr *)&neigh->primary_key; 40327097255SYOSHIFUJI Hideaki addrconf_addr_solict_mult(target, &mcaddr); 404*d1918542SDavid S. Miller ndisc_send_ns(rt->dst.dev, NULL, target, &mcaddr, NULL); 405f2c31e32SEric Dumazet } else { 40627097255SYOSHIFUJI Hideaki read_unlock_bh(&neigh->lock); 40727097255SYOSHIFUJI Hideaki } 408f2c31e32SEric Dumazet out: 409f2c31e32SEric Dumazet rcu_read_unlock(); 410f2c31e32SEric Dumazet } 41127097255SYOSHIFUJI Hideaki #else 41227097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt) 41327097255SYOSHIFUJI Hideaki { 41427097255SYOSHIFUJI Hideaki } 41527097255SYOSHIFUJI Hideaki #endif 41627097255SYOSHIFUJI Hideaki 4171da177e4SLinus Torvalds /* 418554cfb7eSYOSHIFUJI Hideaki * Default Router Selection (RFC 2461 6.3.6) 4191da177e4SLinus Torvalds */ 420b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif) 4211da177e4SLinus Torvalds { 422*d1918542SDavid S. Miller struct net_device *dev = rt->dst.dev; 423161980f4SDavid S. Miller if (!oif || dev->ifindex == oif) 424554cfb7eSYOSHIFUJI Hideaki return 2; 425161980f4SDavid S. Miller if ((dev->flags & IFF_LOOPBACK) && 426161980f4SDavid S. Miller rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif) 427161980f4SDavid S. Miller return 1; 428554cfb7eSYOSHIFUJI Hideaki return 0; 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds 431b6f99a21SDave Jones static inline int rt6_check_neigh(struct rt6_info *rt) 4321da177e4SLinus Torvalds { 433f2c31e32SEric Dumazet struct neighbour *neigh; 434398bcbebSYOSHIFUJI Hideaki int m; 435f2c31e32SEric Dumazet 436f2c31e32SEric Dumazet rcu_read_lock(); 43727217455SDavid Miller neigh = dst_get_neighbour_noref(&rt->dst); 4384d0c5911SYOSHIFUJI Hideaki if (rt->rt6i_flags & RTF_NONEXTHOP || 4394d0c5911SYOSHIFUJI Hideaki !(rt->rt6i_flags & RTF_GATEWAY)) 4404d0c5911SYOSHIFUJI Hideaki m = 1; 4414d0c5911SYOSHIFUJI Hideaki else if (neigh) { 4421da177e4SLinus Torvalds read_lock_bh(&neigh->lock); 443554cfb7eSYOSHIFUJI Hideaki if (neigh->nud_state & NUD_VALID) 4444d0c5911SYOSHIFUJI Hideaki m = 2; 445398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 446398bcbebSYOSHIFUJI Hideaki else if (neigh->nud_state & NUD_FAILED) 447398bcbebSYOSHIFUJI Hideaki m = 0; 448398bcbebSYOSHIFUJI Hideaki #endif 449398bcbebSYOSHIFUJI Hideaki else 450ea73ee23SYOSHIFUJI Hideaki m = 1; 4511da177e4SLinus Torvalds read_unlock_bh(&neigh->lock); 452398bcbebSYOSHIFUJI Hideaki } else 453398bcbebSYOSHIFUJI Hideaki m = 0; 454f2c31e32SEric Dumazet rcu_read_unlock(); 455554cfb7eSYOSHIFUJI Hideaki return m; 4561da177e4SLinus Torvalds } 4571da177e4SLinus Torvalds 458554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif, 459554cfb7eSYOSHIFUJI Hideaki int strict) 460554cfb7eSYOSHIFUJI Hideaki { 4614d0c5911SYOSHIFUJI Hideaki int m, n; 4624d0c5911SYOSHIFUJI Hideaki 4634d0c5911SYOSHIFUJI Hideaki m = rt6_check_dev(rt, oif); 46477d16f45SYOSHIFUJI Hideaki if (!m && (strict & RT6_LOOKUP_F_IFACE)) 465554cfb7eSYOSHIFUJI Hideaki return -1; 466ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 467ebacaaa0SYOSHIFUJI Hideaki m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2; 468ebacaaa0SYOSHIFUJI Hideaki #endif 4694d0c5911SYOSHIFUJI Hideaki n = rt6_check_neigh(rt); 470557e92efSYOSHIFUJI Hideaki if (!n && (strict & RT6_LOOKUP_F_REACHABLE)) 471554cfb7eSYOSHIFUJI Hideaki return -1; 472554cfb7eSYOSHIFUJI Hideaki return m; 473554cfb7eSYOSHIFUJI Hideaki } 474554cfb7eSYOSHIFUJI Hideaki 475f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict, 476f11e6659SDavid S. Miller int *mpri, struct rt6_info *match) 477554cfb7eSYOSHIFUJI Hideaki { 478554cfb7eSYOSHIFUJI Hideaki int m; 479554cfb7eSYOSHIFUJI Hideaki 480554cfb7eSYOSHIFUJI Hideaki if (rt6_check_expired(rt)) 481f11e6659SDavid S. Miller goto out; 482554cfb7eSYOSHIFUJI Hideaki 483554cfb7eSYOSHIFUJI Hideaki m = rt6_score_route(rt, oif, strict); 484554cfb7eSYOSHIFUJI Hideaki if (m < 0) 485f11e6659SDavid S. Miller goto out; 486554cfb7eSYOSHIFUJI Hideaki 487f11e6659SDavid S. Miller if (m > *mpri) { 488ea659e07SYOSHIFUJI Hideaki if (strict & RT6_LOOKUP_F_REACHABLE) 48927097255SYOSHIFUJI Hideaki rt6_probe(match); 490f11e6659SDavid S. Miller *mpri = m; 491554cfb7eSYOSHIFUJI Hideaki match = rt; 492ea659e07SYOSHIFUJI Hideaki } else if (strict & RT6_LOOKUP_F_REACHABLE) { 49327097255SYOSHIFUJI Hideaki rt6_probe(rt); 4941da177e4SLinus Torvalds } 495f11e6659SDavid S. Miller 496f11e6659SDavid S. Miller out: 497f11e6659SDavid S. Miller return match; 4981da177e4SLinus Torvalds } 4991da177e4SLinus Torvalds 500f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn, 501f11e6659SDavid S. Miller struct rt6_info *rr_head, 502f11e6659SDavid S. Miller u32 metric, int oif, int strict) 503f11e6659SDavid S. Miller { 504f11e6659SDavid S. Miller struct rt6_info *rt, *match; 505f11e6659SDavid S. Miller int mpri = -1; 506f11e6659SDavid S. Miller 507f11e6659SDavid S. Miller match = NULL; 508f11e6659SDavid S. Miller for (rt = rr_head; rt && rt->rt6i_metric == metric; 509d8d1f30bSChangli Gao rt = rt->dst.rt6_next) 510f11e6659SDavid S. Miller match = find_match(rt, oif, strict, &mpri, match); 511f11e6659SDavid S. Miller for (rt = fn->leaf; rt && rt != rr_head && 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 515f11e6659SDavid S. Miller return match; 516f11e6659SDavid S. Miller } 517f11e6659SDavid S. Miller 518f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict) 519f11e6659SDavid S. Miller { 520f11e6659SDavid S. Miller struct rt6_info *match, *rt0; 5218ed67789SDaniel Lezcano struct net *net; 522f11e6659SDavid S. Miller 523f11e6659SDavid S. Miller rt0 = fn->rr_ptr; 524f11e6659SDavid S. Miller if (!rt0) 525f11e6659SDavid S. Miller fn->rr_ptr = rt0 = fn->leaf; 526f11e6659SDavid S. Miller 527f11e6659SDavid S. Miller match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict); 528f11e6659SDavid S. Miller 529554cfb7eSYOSHIFUJI Hideaki if (!match && 530f11e6659SDavid S. Miller (strict & RT6_LOOKUP_F_REACHABLE)) { 531d8d1f30bSChangli Gao struct rt6_info *next = rt0->dst.rt6_next; 532f11e6659SDavid S. Miller 533554cfb7eSYOSHIFUJI Hideaki /* no entries matched; do round-robin */ 534f11e6659SDavid S. Miller if (!next || next->rt6i_metric != rt0->rt6i_metric) 535f11e6659SDavid S. Miller next = fn->leaf; 536f11e6659SDavid S. Miller 537f11e6659SDavid S. Miller if (next != rt0) 538f11e6659SDavid S. Miller fn->rr_ptr = next; 539554cfb7eSYOSHIFUJI Hideaki } 540554cfb7eSYOSHIFUJI Hideaki 541*d1918542SDavid S. Miller net = dev_net(rt0->dst.dev); 542a02cec21SEric Dumazet return match ? match : net->ipv6.ip6_null_entry; 5431da177e4SLinus Torvalds } 5441da177e4SLinus Torvalds 54570ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 54670ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 547b71d1d42SEric Dumazet const struct in6_addr *gwaddr) 54870ceb4f5SYOSHIFUJI Hideaki { 549c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 55070ceb4f5SYOSHIFUJI Hideaki struct route_info *rinfo = (struct route_info *) opt; 55170ceb4f5SYOSHIFUJI Hideaki struct in6_addr prefix_buf, *prefix; 55270ceb4f5SYOSHIFUJI Hideaki unsigned int pref; 5534bed72e4SYOSHIFUJI Hideaki unsigned long lifetime; 55470ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt; 55570ceb4f5SYOSHIFUJI Hideaki 55670ceb4f5SYOSHIFUJI Hideaki if (len < sizeof(struct route_info)) { 55770ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 55870ceb4f5SYOSHIFUJI Hideaki } 55970ceb4f5SYOSHIFUJI Hideaki 56070ceb4f5SYOSHIFUJI Hideaki /* Sanity check for prefix_len and length */ 56170ceb4f5SYOSHIFUJI Hideaki if (rinfo->length > 3) { 56270ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 56370ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 128) { 56470ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 56570ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 64) { 56670ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 2) { 56770ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 56870ceb4f5SYOSHIFUJI Hideaki } 56970ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 0) { 57070ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 1) { 57170ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 57270ceb4f5SYOSHIFUJI Hideaki } 57370ceb4f5SYOSHIFUJI Hideaki } 57470ceb4f5SYOSHIFUJI Hideaki 57570ceb4f5SYOSHIFUJI Hideaki pref = rinfo->route_pref; 57670ceb4f5SYOSHIFUJI Hideaki if (pref == ICMPV6_ROUTER_PREF_INVALID) 5773933fc95SJens Rosenboom return -EINVAL; 57870ceb4f5SYOSHIFUJI Hideaki 5794bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); 58070ceb4f5SYOSHIFUJI Hideaki 58170ceb4f5SYOSHIFUJI Hideaki if (rinfo->length == 3) 58270ceb4f5SYOSHIFUJI Hideaki prefix = (struct in6_addr *)rinfo->prefix; 58370ceb4f5SYOSHIFUJI Hideaki else { 58470ceb4f5SYOSHIFUJI Hideaki /* this function is safe */ 58570ceb4f5SYOSHIFUJI Hideaki ipv6_addr_prefix(&prefix_buf, 58670ceb4f5SYOSHIFUJI Hideaki (struct in6_addr *)rinfo->prefix, 58770ceb4f5SYOSHIFUJI Hideaki rinfo->prefix_len); 58870ceb4f5SYOSHIFUJI Hideaki prefix = &prefix_buf; 58970ceb4f5SYOSHIFUJI Hideaki } 59070ceb4f5SYOSHIFUJI Hideaki 591efa2cea0SDaniel Lezcano rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr, 592efa2cea0SDaniel Lezcano dev->ifindex); 59370ceb4f5SYOSHIFUJI Hideaki 59470ceb4f5SYOSHIFUJI Hideaki if (rt && !lifetime) { 595e0a1ad73SThomas Graf ip6_del_rt(rt); 59670ceb4f5SYOSHIFUJI Hideaki rt = NULL; 59770ceb4f5SYOSHIFUJI Hideaki } 59870ceb4f5SYOSHIFUJI Hideaki 59970ceb4f5SYOSHIFUJI Hideaki if (!rt && lifetime) 600efa2cea0SDaniel Lezcano rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex, 60170ceb4f5SYOSHIFUJI Hideaki pref); 60270ceb4f5SYOSHIFUJI Hideaki else if (rt) 60370ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags = RTF_ROUTEINFO | 60470ceb4f5SYOSHIFUJI Hideaki (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 60570ceb4f5SYOSHIFUJI Hideaki 60670ceb4f5SYOSHIFUJI Hideaki if (rt) { 6074bed72e4SYOSHIFUJI Hideaki if (!addrconf_finite_timeout(lifetime)) { 60870ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags &= ~RTF_EXPIRES; 60970ceb4f5SYOSHIFUJI Hideaki } else { 610*d1918542SDavid S. Miller rt->dst.expires = jiffies + HZ * lifetime; 61170ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_EXPIRES; 61270ceb4f5SYOSHIFUJI Hideaki } 613d8d1f30bSChangli Gao dst_release(&rt->dst); 61470ceb4f5SYOSHIFUJI Hideaki } 61570ceb4f5SYOSHIFUJI Hideaki return 0; 61670ceb4f5SYOSHIFUJI Hideaki } 61770ceb4f5SYOSHIFUJI Hideaki #endif 61870ceb4f5SYOSHIFUJI Hideaki 6198ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr) \ 620982f56f3SYOSHIFUJI Hideaki do { \ 6218ed67789SDaniel Lezcano if (rt == __net->ipv6.ip6_null_entry) { \ 622982f56f3SYOSHIFUJI Hideaki struct fib6_node *pn; \ 623e0eda7bbSVille Nuorvala while (1) { \ 624982f56f3SYOSHIFUJI Hideaki if (fn->fn_flags & RTN_TL_ROOT) \ 625c71099acSThomas Graf goto out; \ 626982f56f3SYOSHIFUJI Hideaki pn = fn->parent; \ 627982f56f3SYOSHIFUJI Hideaki if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \ 6288bce65b9SKim Nordlund fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \ 629982f56f3SYOSHIFUJI Hideaki else \ 630982f56f3SYOSHIFUJI Hideaki fn = pn; \ 631c71099acSThomas Graf if (fn->fn_flags & RTN_RTINFO) \ 632c71099acSThomas Graf goto restart; \ 633c71099acSThomas Graf } \ 634982f56f3SYOSHIFUJI Hideaki } \ 635982f56f3SYOSHIFUJI Hideaki } while (0) 636c71099acSThomas Graf 6378ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net, 6388ed67789SDaniel Lezcano struct fib6_table *table, 6394c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 6401da177e4SLinus Torvalds { 6411da177e4SLinus Torvalds struct fib6_node *fn; 6421da177e4SLinus Torvalds struct rt6_info *rt; 6431da177e4SLinus Torvalds 644c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 6454c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 646c71099acSThomas Graf restart: 647c71099acSThomas Graf rt = fn->leaf; 6484c9483b2SDavid S. Miller rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags); 6494c9483b2SDavid S. Miller BACKTRACK(net, &fl6->saddr); 650c71099acSThomas Graf out: 651d8d1f30bSChangli Gao dst_use(&rt->dst, jiffies); 652c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 6531da177e4SLinus Torvalds return rt; 654c71099acSThomas Graf 655c71099acSThomas Graf } 656c71099acSThomas Graf 657ea6e574eSFlorian Westphal struct dst_entry * ip6_route_lookup(struct net *net, struct flowi6 *fl6, 658ea6e574eSFlorian Westphal int flags) 659ea6e574eSFlorian Westphal { 660ea6e574eSFlorian Westphal return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup); 661ea6e574eSFlorian Westphal } 662ea6e574eSFlorian Westphal EXPORT_SYMBOL_GPL(ip6_route_lookup); 663ea6e574eSFlorian Westphal 6649acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, 6659acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *saddr, int oif, int strict) 666c71099acSThomas Graf { 6674c9483b2SDavid S. Miller struct flowi6 fl6 = { 6684c9483b2SDavid S. Miller .flowi6_oif = oif, 6694c9483b2SDavid S. Miller .daddr = *daddr, 670c71099acSThomas Graf }; 671c71099acSThomas Graf struct dst_entry *dst; 67277d16f45SYOSHIFUJI Hideaki int flags = strict ? RT6_LOOKUP_F_IFACE : 0; 673c71099acSThomas Graf 674adaa70bbSThomas Graf if (saddr) { 6754c9483b2SDavid S. Miller memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 676adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 677adaa70bbSThomas Graf } 678adaa70bbSThomas Graf 6794c9483b2SDavid S. Miller dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup); 680c71099acSThomas Graf if (dst->error == 0) 681c71099acSThomas Graf return (struct rt6_info *) dst; 682c71099acSThomas Graf 683c71099acSThomas Graf dst_release(dst); 684c71099acSThomas Graf 6851da177e4SLinus Torvalds return NULL; 6861da177e4SLinus Torvalds } 6871da177e4SLinus Torvalds 6887159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup); 6897159039aSYOSHIFUJI Hideaki 690c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock. 6911da177e4SLinus Torvalds It takes new route entry, the addition fails by any reason the 6921da177e4SLinus Torvalds route is freed. In any case, if caller does not hold it, it may 6931da177e4SLinus Torvalds be destroyed. 6941da177e4SLinus Torvalds */ 6951da177e4SLinus Torvalds 69686872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info) 6971da177e4SLinus Torvalds { 6981da177e4SLinus Torvalds int err; 699c71099acSThomas Graf struct fib6_table *table; 7001da177e4SLinus Torvalds 701c71099acSThomas Graf table = rt->rt6i_table; 702c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 70386872cb5SThomas Graf err = fib6_add(&table->tb6_root, rt, info); 704c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 7051da177e4SLinus Torvalds 7061da177e4SLinus Torvalds return err; 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds 70940e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt) 71040e22e8fSThomas Graf { 7114d1169c1SDenis V. Lunev struct nl_info info = { 712*d1918542SDavid S. Miller .nl_net = dev_net(rt->dst.dev), 7134d1169c1SDenis V. Lunev }; 714528c4cebSDenis V. Lunev return __ip6_ins_rt(rt, &info); 71540e22e8fSThomas Graf } 71640e22e8fSThomas Graf 71721efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_cow(const struct rt6_info *ort, 71821efcfa0SEric Dumazet const struct in6_addr *daddr, 719b71d1d42SEric Dumazet const struct in6_addr *saddr) 7201da177e4SLinus Torvalds { 7211da177e4SLinus Torvalds struct rt6_info *rt; 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds /* 7241da177e4SLinus Torvalds * Clone the route. 7251da177e4SLinus Torvalds */ 7261da177e4SLinus Torvalds 72721efcfa0SEric Dumazet rt = ip6_rt_copy(ort, daddr); 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds if (rt) { 73014deae41SDavid S. Miller int attempts = !in_softirq(); 73114deae41SDavid S. Miller 73258c4fb86SYOSHIFUJI Hideaki if (!(rt->rt6i_flags & RTF_GATEWAY)) { 733bb3c3686SDavid S. Miller if (ort->rt6i_dst.plen != 128 && 73421efcfa0SEric Dumazet ipv6_addr_equal(&ort->rt6i_dst.addr, daddr)) 73558c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 7364e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = *daddr; 73758c4fb86SYOSHIFUJI Hideaki } 7381da177e4SLinus Torvalds 7391da177e4SLinus Torvalds rt->rt6i_flags |= RTF_CACHE; 7401da177e4SLinus Torvalds 7411da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 7421da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 7434e3fd7a0SAlexey Dobriyan rt->rt6i_src.addr = *saddr; 7441da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 7451da177e4SLinus Torvalds } 7461da177e4SLinus Torvalds #endif 7471da177e4SLinus Torvalds 74814deae41SDavid S. Miller retry: 749f83c7790SDavid S. Miller if (rt6_bind_neighbour(rt)) { 750*d1918542SDavid S. Miller struct net *net = dev_net(rt->dst.dev); 75114deae41SDavid S. Miller int saved_rt_min_interval = 75214deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_min_interval; 75314deae41SDavid S. Miller int saved_rt_elasticity = 75414deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_elasticity; 75514deae41SDavid S. Miller 75614deae41SDavid S. Miller if (attempts-- > 0) { 75714deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_elasticity = 1; 75814deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_min_interval = 0; 75914deae41SDavid S. Miller 76086393e52SAlexey Dobriyan ip6_dst_gc(&net->ipv6.ip6_dst_ops); 76114deae41SDavid S. Miller 76214deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_elasticity = 76314deae41SDavid S. Miller saved_rt_elasticity; 76414deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_min_interval = 76514deae41SDavid S. Miller saved_rt_min_interval; 76614deae41SDavid S. Miller goto retry; 76714deae41SDavid S. Miller } 76814deae41SDavid S. Miller 76914deae41SDavid S. Miller if (net_ratelimit()) 77014deae41SDavid S. Miller printk(KERN_WARNING 7717e1b33e5SUlrich Weber "ipv6: Neighbour table overflow.\n"); 772d8d1f30bSChangli Gao dst_free(&rt->dst); 77314deae41SDavid S. Miller return NULL; 77414deae41SDavid S. Miller } 77595a9a5baSYOSHIFUJI Hideaki } 7761da177e4SLinus Torvalds 7771da177e4SLinus Torvalds return rt; 7781da177e4SLinus Torvalds } 77995a9a5baSYOSHIFUJI Hideaki 78021efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, 78121efcfa0SEric Dumazet const struct in6_addr *daddr) 782299d9939SYOSHIFUJI Hideaki { 78321efcfa0SEric Dumazet struct rt6_info *rt = ip6_rt_copy(ort, daddr); 78421efcfa0SEric Dumazet 785299d9939SYOSHIFUJI Hideaki if (rt) { 786299d9939SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_CACHE; 78727217455SDavid Miller dst_set_neighbour(&rt->dst, neigh_clone(dst_get_neighbour_noref_raw(&ort->dst))); 788299d9939SYOSHIFUJI Hideaki } 789299d9939SYOSHIFUJI Hideaki return rt; 790299d9939SYOSHIFUJI Hideaki } 791299d9939SYOSHIFUJI Hideaki 7928ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif, 7934c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 7941da177e4SLinus Torvalds { 7951da177e4SLinus Torvalds struct fib6_node *fn; 796519fbd87SYOSHIFUJI Hideaki struct rt6_info *rt, *nrt; 797c71099acSThomas Graf int strict = 0; 7981da177e4SLinus Torvalds int attempts = 3; 799519fbd87SYOSHIFUJI Hideaki int err; 80053b7997fSYOSHIFUJI Hideaki int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE; 8011da177e4SLinus Torvalds 80277d16f45SYOSHIFUJI Hideaki strict |= flags & RT6_LOOKUP_F_IFACE; 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds relookup: 805c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 8061da177e4SLinus Torvalds 8078238dd06SYOSHIFUJI Hideaki restart_2: 8084c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 8091da177e4SLinus Torvalds 8101da177e4SLinus Torvalds restart: 8114acad72dSPavel Emelyanov rt = rt6_select(fn, oif, strict | reachable); 8128ed67789SDaniel Lezcano 8134c9483b2SDavid S. Miller BACKTRACK(net, &fl6->saddr); 8148ed67789SDaniel Lezcano if (rt == net->ipv6.ip6_null_entry || 8158238dd06SYOSHIFUJI Hideaki rt->rt6i_flags & RTF_CACHE) 8161da177e4SLinus Torvalds goto out; 8171da177e4SLinus Torvalds 818d8d1f30bSChangli Gao dst_hold(&rt->dst); 819c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 8201da177e4SLinus Torvalds 82127217455SDavid Miller if (!dst_get_neighbour_noref_raw(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP)) 8224c9483b2SDavid S. Miller nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr); 8237343ff31SDavid S. Miller else if (!(rt->dst.flags & DST_HOST)) 8244c9483b2SDavid S. Miller nrt = rt6_alloc_clone(rt, &fl6->daddr); 8257343ff31SDavid S. Miller else 8267343ff31SDavid S. Miller goto out2; 8271da177e4SLinus Torvalds 828d8d1f30bSChangli Gao dst_release(&rt->dst); 8298ed67789SDaniel Lezcano rt = nrt ? : net->ipv6.ip6_null_entry; 8301da177e4SLinus Torvalds 831d8d1f30bSChangli Gao dst_hold(&rt->dst); 832e40cf353SYOSHIFUJI Hideaki if (nrt) { 83340e22e8fSThomas Graf err = ip6_ins_rt(nrt); 834e40cf353SYOSHIFUJI Hideaki if (!err) 835e40cf353SYOSHIFUJI Hideaki goto out2; 836e40cf353SYOSHIFUJI Hideaki } 837e40cf353SYOSHIFUJI Hideaki 838e40cf353SYOSHIFUJI Hideaki if (--attempts <= 0) 8391da177e4SLinus Torvalds goto out2; 8401da177e4SLinus Torvalds 841519fbd87SYOSHIFUJI Hideaki /* 842c71099acSThomas Graf * Race condition! In the gap, when table->tb6_lock was 843519fbd87SYOSHIFUJI Hideaki * released someone could insert this route. Relookup. 8441da177e4SLinus Torvalds */ 845d8d1f30bSChangli Gao dst_release(&rt->dst); 8461da177e4SLinus Torvalds goto relookup; 847e40cf353SYOSHIFUJI Hideaki 848519fbd87SYOSHIFUJI Hideaki out: 8498238dd06SYOSHIFUJI Hideaki if (reachable) { 8508238dd06SYOSHIFUJI Hideaki reachable = 0; 8518238dd06SYOSHIFUJI Hideaki goto restart_2; 8528238dd06SYOSHIFUJI Hideaki } 853d8d1f30bSChangli Gao dst_hold(&rt->dst); 854c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 8551da177e4SLinus Torvalds out2: 856d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 857d8d1f30bSChangli Gao rt->dst.__use++; 858c71099acSThomas Graf 859c71099acSThomas Graf return rt; 860c71099acSThomas Graf } 861c71099acSThomas Graf 8628ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table, 8634c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 8644acad72dSPavel Emelyanov { 8654c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags); 8664acad72dSPavel Emelyanov } 8674acad72dSPavel Emelyanov 868c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 869c71099acSThomas Graf { 870b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 871c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 872adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 8734c9483b2SDavid S. Miller struct flowi6 fl6 = { 8744c9483b2SDavid S. Miller .flowi6_iif = skb->dev->ifindex, 8754c9483b2SDavid S. Miller .daddr = iph->daddr, 8764c9483b2SDavid S. Miller .saddr = iph->saddr, 8774c9483b2SDavid S. Miller .flowlabel = (* (__be32 *) iph) & IPV6_FLOWINFO_MASK, 8784c9483b2SDavid S. Miller .flowi6_mark = skb->mark, 8794c9483b2SDavid S. Miller .flowi6_proto = iph->nexthdr, 880c71099acSThomas Graf }; 881adaa70bbSThomas Graf 8821d6e55f1SThomas Goff if (rt6_need_strict(&iph->daddr) && skb->dev->type != ARPHRD_PIMREG) 883adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_IFACE; 884c71099acSThomas Graf 8854c9483b2SDavid S. Miller skb_dst_set(skb, fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_input)); 886c71099acSThomas Graf } 887c71099acSThomas Graf 8888ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table, 8894c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 890c71099acSThomas Graf { 8914c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags); 892c71099acSThomas Graf } 893c71099acSThomas Graf 8949c7a4f9cSFlorian Westphal struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk, 8954c9483b2SDavid S. Miller struct flowi6 *fl6) 896c71099acSThomas Graf { 897c71099acSThomas Graf int flags = 0; 898c71099acSThomas Graf 8994c9483b2SDavid S. Miller if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr)) 90077d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 901c71099acSThomas Graf 9024c9483b2SDavid S. Miller if (!ipv6_addr_any(&fl6->saddr)) 903adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 9040c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 9050c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 906adaa70bbSThomas Graf 9074c9483b2SDavid S. Miller return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output); 9081da177e4SLinus Torvalds } 9091da177e4SLinus Torvalds 9107159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output); 9111da177e4SLinus Torvalds 9122774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 91314e50e57SDavid S. Miller { 9145c1e6aa3SDavid S. Miller struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; 91514e50e57SDavid S. Miller struct dst_entry *new = NULL; 91614e50e57SDavid S. Miller 9175c1e6aa3SDavid S. Miller rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, 0, 0); 91814e50e57SDavid S. Miller if (rt) { 919cf911662SDavid S. Miller memset(&rt->rt6i_table, 0, sizeof(*rt) - sizeof(struct dst_entry)); 920cf911662SDavid S. Miller 921d8d1f30bSChangli Gao new = &rt->dst; 92214e50e57SDavid S. Miller 92314e50e57SDavid S. Miller new->__use = 1; 924352e512cSHerbert Xu new->input = dst_discard; 925352e512cSHerbert Xu new->output = dst_discard; 92614e50e57SDavid S. Miller 92721efcfa0SEric Dumazet if (dst_metrics_read_only(&ort->dst)) 92821efcfa0SEric Dumazet new->_metrics = ort->dst._metrics; 92921efcfa0SEric Dumazet else 930defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 93114e50e57SDavid S. Miller rt->rt6i_idev = ort->rt6i_idev; 93214e50e57SDavid S. Miller if (rt->rt6i_idev) 93314e50e57SDavid S. Miller in6_dev_hold(rt->rt6i_idev); 934*d1918542SDavid S. Miller rt->dst.expires = 0; 93514e50e57SDavid S. Miller 9364e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 93714e50e57SDavid S. Miller rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES; 93814e50e57SDavid S. Miller rt->rt6i_metric = 0; 93914e50e57SDavid S. Miller 94014e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 94114e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 94214e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 94314e50e57SDavid S. Miller #endif 94414e50e57SDavid S. Miller 94514e50e57SDavid S. Miller dst_free(new); 94614e50e57SDavid S. Miller } 94714e50e57SDavid S. Miller 94869ead7afSDavid S. Miller dst_release(dst_orig); 94969ead7afSDavid S. Miller return new ? new : ERR_PTR(-ENOMEM); 95014e50e57SDavid S. Miller } 95114e50e57SDavid S. Miller 9521da177e4SLinus Torvalds /* 9531da177e4SLinus Torvalds * Destination cache support functions 9541da177e4SLinus Torvalds */ 9551da177e4SLinus Torvalds 9561da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 9571da177e4SLinus Torvalds { 9581da177e4SLinus Torvalds struct rt6_info *rt; 9591da177e4SLinus Torvalds 9601da177e4SLinus Torvalds rt = (struct rt6_info *) dst; 9611da177e4SLinus Torvalds 9626431cbc2SDavid S. Miller if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) { 9636431cbc2SDavid S. Miller if (rt->rt6i_peer_genid != rt6_peer_genid()) { 9646431cbc2SDavid S. Miller if (!rt->rt6i_peer) 9656431cbc2SDavid S. Miller rt6_bind_peer(rt, 0); 9666431cbc2SDavid S. Miller rt->rt6i_peer_genid = rt6_peer_genid(); 9676431cbc2SDavid S. Miller } 9681da177e4SLinus Torvalds return dst; 9696431cbc2SDavid S. Miller } 9701da177e4SLinus Torvalds return NULL; 9711da177e4SLinus Torvalds } 9721da177e4SLinus Torvalds 9731da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 9741da177e4SLinus Torvalds { 9751da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 9761da177e4SLinus Torvalds 9771da177e4SLinus Torvalds if (rt) { 97854c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 97954c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 980e0a1ad73SThomas Graf ip6_del_rt(rt); 98154c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 9821da177e4SLinus Torvalds } 98354c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 98454c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 98554c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 98654c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 98754c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 98854c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 9891da177e4SLinus Torvalds } 9901da177e4SLinus Torvalds 9911da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 9921da177e4SLinus Torvalds { 9931da177e4SLinus Torvalds struct rt6_info *rt; 9941da177e4SLinus Torvalds 9953ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 9961da177e4SLinus Torvalds 997adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 9981da177e4SLinus Torvalds if (rt) { 9991da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) { 1000d8d1f30bSChangli Gao dst_set_expires(&rt->dst, 0); 10011da177e4SLinus Torvalds rt->rt6i_flags |= RTF_EXPIRES; 10021da177e4SLinus Torvalds } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) 10031da177e4SLinus Torvalds rt->rt6i_node->fn_sernum = -1; 10041da177e4SLinus Torvalds } 10051da177e4SLinus Torvalds } 10061da177e4SLinus Torvalds 10071da177e4SLinus Torvalds static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu) 10081da177e4SLinus Torvalds { 10091da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info*)dst; 10101da177e4SLinus Torvalds 10111da177e4SLinus Torvalds if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) { 10121da177e4SLinus Torvalds rt6->rt6i_flags |= RTF_MODIFIED; 10131da177e4SLinus Torvalds if (mtu < IPV6_MIN_MTU) { 1014defb3519SDavid S. Miller u32 features = dst_metric(dst, RTAX_FEATURES); 10151da177e4SLinus Torvalds mtu = IPV6_MIN_MTU; 1016defb3519SDavid S. Miller features |= RTAX_FEATURE_ALLFRAG; 1017defb3519SDavid S. Miller dst_metric_set(dst, RTAX_FEATURES, features); 10181da177e4SLinus Torvalds } 1019defb3519SDavid S. Miller dst_metric_set(dst, RTAX_MTU, mtu); 10201da177e4SLinus Torvalds } 10211da177e4SLinus Torvalds } 10221da177e4SLinus Torvalds 10230dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 10241da177e4SLinus Torvalds { 10250dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 10260dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 10270dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 10280dbaee3bSDavid S. Miller 10291da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 10301da177e4SLinus Torvalds 10315578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 10325578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 10331da177e4SLinus Torvalds 10341da177e4SLinus Torvalds /* 10351da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 10361da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 10371da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 10381da177e4SLinus Torvalds * rely only on pmtu discovery" 10391da177e4SLinus Torvalds */ 10401da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 10411da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 10421da177e4SLinus Torvalds return mtu; 10431da177e4SLinus Torvalds } 10441da177e4SLinus Torvalds 1045ebb762f2SSteffen Klassert static unsigned int ip6_mtu(const struct dst_entry *dst) 1046d33e4553SDavid S. Miller { 1047d33e4553SDavid S. Miller struct inet6_dev *idev; 1048618f9bc7SSteffen Klassert unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); 1049618f9bc7SSteffen Klassert 1050618f9bc7SSteffen Klassert if (mtu) 1051618f9bc7SSteffen Klassert return mtu; 1052618f9bc7SSteffen Klassert 1053618f9bc7SSteffen Klassert mtu = IPV6_MIN_MTU; 1054d33e4553SDavid S. Miller 1055d33e4553SDavid S. Miller rcu_read_lock(); 1056d33e4553SDavid S. Miller idev = __in6_dev_get(dst->dev); 1057d33e4553SDavid S. Miller if (idev) 1058d33e4553SDavid S. Miller mtu = idev->cnf.mtu6; 1059d33e4553SDavid S. Miller rcu_read_unlock(); 1060d33e4553SDavid S. Miller 1061d33e4553SDavid S. Miller return mtu; 1062d33e4553SDavid S. Miller } 1063d33e4553SDavid S. Miller 10643b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list; 10653b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock); 10665d0bbeebSThomas Graf 10673b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 10681da177e4SLinus Torvalds struct neighbour *neigh, 106987a11578SDavid S. Miller struct flowi6 *fl6) 10701da177e4SLinus Torvalds { 107187a11578SDavid S. Miller struct dst_entry *dst; 10721da177e4SLinus Torvalds struct rt6_info *rt; 10731da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 1074c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 10751da177e4SLinus Torvalds 107638308473SDavid S. Miller if (unlikely(!idev)) 10771da177e4SLinus Torvalds return NULL; 10781da177e4SLinus Torvalds 1079957c665fSDavid S. Miller rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, dev, 0); 108038308473SDavid S. Miller if (unlikely(!rt)) { 10811da177e4SLinus Torvalds in6_dev_put(idev); 108287a11578SDavid S. Miller dst = ERR_PTR(-ENOMEM); 10831da177e4SLinus Torvalds goto out; 10841da177e4SLinus Torvalds } 10851da177e4SLinus Torvalds 10861da177e4SLinus Torvalds if (neigh) 10871da177e4SLinus Torvalds neigh_hold(neigh); 108814deae41SDavid S. Miller else { 1089f83c7790SDavid S. Miller neigh = ip6_neigh_lookup(&rt->dst, &fl6->daddr); 1090b43faac6SDavid S. Miller if (IS_ERR(neigh)) { 1091b43faac6SDavid S. Miller dst_free(&rt->dst); 1092b43faac6SDavid S. Miller return ERR_CAST(neigh); 1093b43faac6SDavid S. Miller } 109414deae41SDavid S. Miller } 10951da177e4SLinus Torvalds 10968e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 10978e2ec639SYan, Zheng rt->dst.output = ip6_output; 109869cce1d1SDavid S. Miller dst_set_neighbour(&rt->dst, neigh); 1099d8d1f30bSChangli Gao atomic_set(&rt->dst.__refcnt, 1); 110087a11578SDavid S. Miller rt->rt6i_dst.addr = fl6->daddr; 11018e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 11028e2ec639SYan, Zheng rt->rt6i_idev = idev; 11037011687fSGao feng dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255); 11041da177e4SLinus Torvalds 11053b00944cSYOSHIFUJI Hideaki spin_lock_bh(&icmp6_dst_lock); 1106d8d1f30bSChangli Gao rt->dst.next = icmp6_dst_gc_list; 1107d8d1f30bSChangli Gao icmp6_dst_gc_list = &rt->dst; 11083b00944cSYOSHIFUJI Hideaki spin_unlock_bh(&icmp6_dst_lock); 11091da177e4SLinus Torvalds 11105578689aSDaniel Lezcano fib6_force_start_gc(net); 11111da177e4SLinus Torvalds 111287a11578SDavid S. Miller dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); 111387a11578SDavid S. Miller 11141da177e4SLinus Torvalds out: 111587a11578SDavid S. Miller return dst; 11161da177e4SLinus Torvalds } 11171da177e4SLinus Torvalds 11183d0f24a7SStephen Hemminger int icmp6_dst_gc(void) 11191da177e4SLinus Torvalds { 1120e9476e95SHagen Paul Pfeifer struct dst_entry *dst, **pprev; 11213d0f24a7SStephen Hemminger int more = 0; 11221da177e4SLinus Torvalds 11233b00944cSYOSHIFUJI Hideaki spin_lock_bh(&icmp6_dst_lock); 11243b00944cSYOSHIFUJI Hideaki pprev = &icmp6_dst_gc_list; 11255d0bbeebSThomas Graf 11261da177e4SLinus Torvalds while ((dst = *pprev) != NULL) { 11271da177e4SLinus Torvalds if (!atomic_read(&dst->__refcnt)) { 11281da177e4SLinus Torvalds *pprev = dst->next; 11291da177e4SLinus Torvalds dst_free(dst); 11301da177e4SLinus Torvalds } else { 11311da177e4SLinus Torvalds pprev = &dst->next; 11323d0f24a7SStephen Hemminger ++more; 11331da177e4SLinus Torvalds } 11341da177e4SLinus Torvalds } 11351da177e4SLinus Torvalds 11363b00944cSYOSHIFUJI Hideaki spin_unlock_bh(&icmp6_dst_lock); 11375d0bbeebSThomas Graf 11383d0f24a7SStephen Hemminger return more; 11391da177e4SLinus Torvalds } 11401da177e4SLinus Torvalds 11411e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg), 11421e493d19SDavid S. Miller void *arg) 11431e493d19SDavid S. Miller { 11441e493d19SDavid S. Miller struct dst_entry *dst, **pprev; 11451e493d19SDavid S. Miller 11461e493d19SDavid S. Miller spin_lock_bh(&icmp6_dst_lock); 11471e493d19SDavid S. Miller pprev = &icmp6_dst_gc_list; 11481e493d19SDavid S. Miller while ((dst = *pprev) != NULL) { 11491e493d19SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 11501e493d19SDavid S. Miller if (func(rt, arg)) { 11511e493d19SDavid S. Miller *pprev = dst->next; 11521e493d19SDavid S. Miller dst_free(dst); 11531e493d19SDavid S. Miller } else { 11541e493d19SDavid S. Miller pprev = &dst->next; 11551e493d19SDavid S. Miller } 11561e493d19SDavid S. Miller } 11571e493d19SDavid S. Miller spin_unlock_bh(&icmp6_dst_lock); 11581e493d19SDavid S. Miller } 11591e493d19SDavid S. Miller 1160569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops) 11611da177e4SLinus Torvalds { 11621da177e4SLinus Torvalds unsigned long now = jiffies; 116386393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 11647019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 11657019b78eSDaniel Lezcano int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; 11667019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 11677019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 11687019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 1169fc66f95cSEric Dumazet int entries; 11701da177e4SLinus Torvalds 1171fc66f95cSEric Dumazet entries = dst_entries_get_fast(ops); 11727019b78eSDaniel Lezcano if (time_after(rt_last_gc + rt_min_interval, now) && 1173fc66f95cSEric Dumazet entries <= rt_max_size) 11741da177e4SLinus Torvalds goto out; 11751da177e4SLinus Torvalds 11766891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire++; 11776891a346SBenjamin Thery fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net); 11786891a346SBenjamin Thery net->ipv6.ip6_rt_last_gc = now; 1179fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 1180fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 11817019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; 11821da177e4SLinus Torvalds out: 11837019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; 1184fc66f95cSEric Dumazet return entries > rt_max_size; 11851da177e4SLinus Torvalds } 11861da177e4SLinus Torvalds 11871da177e4SLinus Torvalds /* Clean host part of a prefix. Not necessary in radix tree, 11881da177e4SLinus Torvalds but results in cleaner routing tables. 11891da177e4SLinus Torvalds 11901da177e4SLinus Torvalds Remove it only when all the things will work! 11911da177e4SLinus Torvalds */ 11921da177e4SLinus Torvalds 11936b75d090SYOSHIFUJI Hideaki int ip6_dst_hoplimit(struct dst_entry *dst) 11941da177e4SLinus Torvalds { 11955170ae82SDavid S. Miller int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT); 1196a02e4b7dSDavid S. Miller if (hoplimit == 0) { 11976b75d090SYOSHIFUJI Hideaki struct net_device *dev = dst->dev; 1198c68f24ccSEric Dumazet struct inet6_dev *idev; 1199c68f24ccSEric Dumazet 1200c68f24ccSEric Dumazet rcu_read_lock(); 1201c68f24ccSEric Dumazet idev = __in6_dev_get(dev); 1202c68f24ccSEric Dumazet if (idev) 12031da177e4SLinus Torvalds hoplimit = idev->cnf.hop_limit; 1204c68f24ccSEric Dumazet else 120553b7997fSYOSHIFUJI Hideaki hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit; 1206c68f24ccSEric Dumazet rcu_read_unlock(); 12071da177e4SLinus Torvalds } 12081da177e4SLinus Torvalds return hoplimit; 12091da177e4SLinus Torvalds } 1210abbf46aeSDavid S. Miller EXPORT_SYMBOL(ip6_dst_hoplimit); 12111da177e4SLinus Torvalds 12121da177e4SLinus Torvalds /* 12131da177e4SLinus Torvalds * 12141da177e4SLinus Torvalds */ 12151da177e4SLinus Torvalds 121686872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg) 12171da177e4SLinus Torvalds { 12181da177e4SLinus Torvalds int err; 12195578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 12201da177e4SLinus Torvalds struct rt6_info *rt = NULL; 12211da177e4SLinus Torvalds struct net_device *dev = NULL; 12221da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 1223c71099acSThomas Graf struct fib6_table *table; 12241da177e4SLinus Torvalds int addr_type; 12251da177e4SLinus Torvalds 122686872cb5SThomas Graf if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128) 12271da177e4SLinus Torvalds return -EINVAL; 12281da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 122986872cb5SThomas Graf if (cfg->fc_src_len) 12301da177e4SLinus Torvalds return -EINVAL; 12311da177e4SLinus Torvalds #endif 123286872cb5SThomas Graf if (cfg->fc_ifindex) { 12331da177e4SLinus Torvalds err = -ENODEV; 12345578689aSDaniel Lezcano dev = dev_get_by_index(net, cfg->fc_ifindex); 12351da177e4SLinus Torvalds if (!dev) 12361da177e4SLinus Torvalds goto out; 12371da177e4SLinus Torvalds idev = in6_dev_get(dev); 12381da177e4SLinus Torvalds if (!idev) 12391da177e4SLinus Torvalds goto out; 12401da177e4SLinus Torvalds } 12411da177e4SLinus Torvalds 124286872cb5SThomas Graf if (cfg->fc_metric == 0) 124386872cb5SThomas Graf cfg->fc_metric = IP6_RT_PRIO_USER; 12441da177e4SLinus Torvalds 1245c71099acSThomas Graf err = -ENOBUFS; 124638308473SDavid S. Miller if (cfg->fc_nlinfo.nlh && 1247d71314b4SMatti Vaittinen !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { 1248d71314b4SMatti Vaittinen table = fib6_get_table(net, cfg->fc_table); 124938308473SDavid S. Miller if (!table) { 1250d71314b4SMatti Vaittinen printk(KERN_WARNING "IPv6: NLM_F_CREATE should be specified when creating new route\n"); 1251d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 1252d71314b4SMatti Vaittinen } 1253d71314b4SMatti Vaittinen } else { 1254d71314b4SMatti Vaittinen table = fib6_new_table(net, cfg->fc_table); 1255d71314b4SMatti Vaittinen } 125638308473SDavid S. Miller 125738308473SDavid S. Miller if (!table) 1258c71099acSThomas Graf goto out; 1259c71099acSThomas Graf 1260957c665fSDavid S. Miller rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, NULL, DST_NOCOUNT); 12611da177e4SLinus Torvalds 126238308473SDavid S. Miller if (!rt) { 12631da177e4SLinus Torvalds err = -ENOMEM; 12641da177e4SLinus Torvalds goto out; 12651da177e4SLinus Torvalds } 12661da177e4SLinus Torvalds 1267d8d1f30bSChangli Gao rt->dst.obsolete = -1; 1268*d1918542SDavid S. Miller rt->dst.expires = (cfg->fc_flags & RTF_EXPIRES) ? 12696f704992SYOSHIFUJI Hideaki jiffies + clock_t_to_jiffies(cfg->fc_expires) : 12706f704992SYOSHIFUJI Hideaki 0; 12711da177e4SLinus Torvalds 127286872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 127386872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 127486872cb5SThomas Graf rt->rt6i_protocol = cfg->fc_protocol; 127586872cb5SThomas Graf 127686872cb5SThomas Graf addr_type = ipv6_addr_type(&cfg->fc_dst); 12771da177e4SLinus Torvalds 12781da177e4SLinus Torvalds if (addr_type & IPV6_ADDR_MULTICAST) 1279d8d1f30bSChangli Gao rt->dst.input = ip6_mc_input; 1280ab79ad14SMaciej Żenczykowski else if (cfg->fc_flags & RTF_LOCAL) 1281ab79ad14SMaciej Żenczykowski rt->dst.input = ip6_input; 12821da177e4SLinus Torvalds else 1283d8d1f30bSChangli Gao rt->dst.input = ip6_forward; 12841da177e4SLinus Torvalds 1285d8d1f30bSChangli Gao rt->dst.output = ip6_output; 12861da177e4SLinus Torvalds 128786872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 128886872cb5SThomas Graf rt->rt6i_dst.plen = cfg->fc_dst_len; 12891da177e4SLinus Torvalds if (rt->rt6i_dst.plen == 128) 129011d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 12911da177e4SLinus Torvalds 12928e2ec639SYan, Zheng if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) { 12938e2ec639SYan, Zheng u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL); 12948e2ec639SYan, Zheng if (!metrics) { 12958e2ec639SYan, Zheng err = -ENOMEM; 12968e2ec639SYan, Zheng goto out; 12978e2ec639SYan, Zheng } 12988e2ec639SYan, Zheng dst_init_metrics(&rt->dst, metrics, 0); 12998e2ec639SYan, Zheng } 13001da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 130186872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len); 130286872cb5SThomas Graf rt->rt6i_src.plen = cfg->fc_src_len; 13031da177e4SLinus Torvalds #endif 13041da177e4SLinus Torvalds 130586872cb5SThomas Graf rt->rt6i_metric = cfg->fc_metric; 13061da177e4SLinus Torvalds 13071da177e4SLinus Torvalds /* We cannot add true routes via loopback here, 13081da177e4SLinus Torvalds they would result in kernel looping; promote them to reject routes 13091da177e4SLinus Torvalds */ 131086872cb5SThomas Graf if ((cfg->fc_flags & RTF_REJECT) || 131138308473SDavid S. Miller (dev && (dev->flags & IFF_LOOPBACK) && 131238308473SDavid S. Miller !(addr_type & IPV6_ADDR_LOOPBACK) && 131338308473SDavid S. Miller !(cfg->fc_flags & RTF_LOCAL))) { 13141da177e4SLinus Torvalds /* hold loopback dev/idev if we haven't done so. */ 13155578689aSDaniel Lezcano if (dev != net->loopback_dev) { 13161da177e4SLinus Torvalds if (dev) { 13171da177e4SLinus Torvalds dev_put(dev); 13181da177e4SLinus Torvalds in6_dev_put(idev); 13191da177e4SLinus Torvalds } 13205578689aSDaniel Lezcano dev = net->loopback_dev; 13211da177e4SLinus Torvalds dev_hold(dev); 13221da177e4SLinus Torvalds idev = in6_dev_get(dev); 13231da177e4SLinus Torvalds if (!idev) { 13241da177e4SLinus Torvalds err = -ENODEV; 13251da177e4SLinus Torvalds goto out; 13261da177e4SLinus Torvalds } 13271da177e4SLinus Torvalds } 1328d8d1f30bSChangli Gao rt->dst.output = ip6_pkt_discard_out; 1329d8d1f30bSChangli Gao rt->dst.input = ip6_pkt_discard; 1330d8d1f30bSChangli Gao rt->dst.error = -ENETUNREACH; 13311da177e4SLinus Torvalds rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP; 13321da177e4SLinus Torvalds goto install_route; 13331da177e4SLinus Torvalds } 13341da177e4SLinus Torvalds 133586872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY) { 1336b71d1d42SEric Dumazet const struct in6_addr *gw_addr; 13371da177e4SLinus Torvalds int gwa_type; 13381da177e4SLinus Torvalds 133986872cb5SThomas Graf gw_addr = &cfg->fc_gateway; 13404e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = *gw_addr; 13411da177e4SLinus Torvalds gwa_type = ipv6_addr_type(gw_addr); 13421da177e4SLinus Torvalds 13431da177e4SLinus Torvalds if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) { 13441da177e4SLinus Torvalds struct rt6_info *grt; 13451da177e4SLinus Torvalds 13461da177e4SLinus Torvalds /* IPv6 strictly inhibits using not link-local 13471da177e4SLinus Torvalds addresses as nexthop address. 13481da177e4SLinus Torvalds Otherwise, router will not able to send redirects. 13491da177e4SLinus Torvalds It is very good, but in some (rare!) circumstances 13501da177e4SLinus Torvalds (SIT, PtP, NBMA NOARP links) it is handy to allow 13511da177e4SLinus Torvalds some exceptions. --ANK 13521da177e4SLinus Torvalds */ 13531da177e4SLinus Torvalds err = -EINVAL; 13541da177e4SLinus Torvalds if (!(gwa_type & IPV6_ADDR_UNICAST)) 13551da177e4SLinus Torvalds goto out; 13561da177e4SLinus Torvalds 13575578689aSDaniel Lezcano grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1); 13581da177e4SLinus Torvalds 13591da177e4SLinus Torvalds err = -EHOSTUNREACH; 136038308473SDavid S. Miller if (!grt) 13611da177e4SLinus Torvalds goto out; 13621da177e4SLinus Torvalds if (dev) { 1363*d1918542SDavid S. Miller if (dev != grt->dst.dev) { 1364d8d1f30bSChangli Gao dst_release(&grt->dst); 13651da177e4SLinus Torvalds goto out; 13661da177e4SLinus Torvalds } 13671da177e4SLinus Torvalds } else { 1368*d1918542SDavid S. Miller dev = grt->dst.dev; 13691da177e4SLinus Torvalds idev = grt->rt6i_idev; 13701da177e4SLinus Torvalds dev_hold(dev); 13711da177e4SLinus Torvalds in6_dev_hold(grt->rt6i_idev); 13721da177e4SLinus Torvalds } 13731da177e4SLinus Torvalds if (!(grt->rt6i_flags & RTF_GATEWAY)) 13741da177e4SLinus Torvalds err = 0; 1375d8d1f30bSChangli Gao dst_release(&grt->dst); 13761da177e4SLinus Torvalds 13771da177e4SLinus Torvalds if (err) 13781da177e4SLinus Torvalds goto out; 13791da177e4SLinus Torvalds } 13801da177e4SLinus Torvalds err = -EINVAL; 138138308473SDavid S. Miller if (!dev || (dev->flags & IFF_LOOPBACK)) 13821da177e4SLinus Torvalds goto out; 13831da177e4SLinus Torvalds } 13841da177e4SLinus Torvalds 13851da177e4SLinus Torvalds err = -ENODEV; 138638308473SDavid S. Miller if (!dev) 13871da177e4SLinus Torvalds goto out; 13881da177e4SLinus Torvalds 1389c3968a85SDaniel Walter if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 1390c3968a85SDaniel Walter if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 1391c3968a85SDaniel Walter err = -EINVAL; 1392c3968a85SDaniel Walter goto out; 1393c3968a85SDaniel Walter } 13944e3fd7a0SAlexey Dobriyan rt->rt6i_prefsrc.addr = cfg->fc_prefsrc; 1395c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 128; 1396c3968a85SDaniel Walter } else 1397c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 1398c3968a85SDaniel Walter 139986872cb5SThomas Graf if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) { 1400f83c7790SDavid S. Miller err = rt6_bind_neighbour(rt); 1401f83c7790SDavid S. Miller if (err) 14021da177e4SLinus Torvalds goto out; 14031da177e4SLinus Torvalds } 14041da177e4SLinus Torvalds 140586872cb5SThomas Graf rt->rt6i_flags = cfg->fc_flags; 14061da177e4SLinus Torvalds 14071da177e4SLinus Torvalds install_route: 140886872cb5SThomas Graf if (cfg->fc_mx) { 140986872cb5SThomas Graf struct nlattr *nla; 141086872cb5SThomas Graf int remaining; 14111da177e4SLinus Torvalds 141286872cb5SThomas Graf nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { 14138f4c1f9bSThomas Graf int type = nla_type(nla); 141486872cb5SThomas Graf 141586872cb5SThomas Graf if (type) { 141686872cb5SThomas Graf if (type > RTAX_MAX) { 14171da177e4SLinus Torvalds err = -EINVAL; 14181da177e4SLinus Torvalds goto out; 14191da177e4SLinus Torvalds } 142086872cb5SThomas Graf 1421defb3519SDavid S. Miller dst_metric_set(&rt->dst, type, nla_get_u32(nla)); 14221da177e4SLinus Torvalds } 14231da177e4SLinus Torvalds } 14241da177e4SLinus Torvalds } 14251da177e4SLinus Torvalds 1426d8d1f30bSChangli Gao rt->dst.dev = dev; 14271da177e4SLinus Torvalds rt->rt6i_idev = idev; 1428c71099acSThomas Graf rt->rt6i_table = table; 142963152fc0SDaniel Lezcano 1430c346dca1SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = dev_net(dev); 143163152fc0SDaniel Lezcano 143286872cb5SThomas Graf return __ip6_ins_rt(rt, &cfg->fc_nlinfo); 14331da177e4SLinus Torvalds 14341da177e4SLinus Torvalds out: 14351da177e4SLinus Torvalds if (dev) 14361da177e4SLinus Torvalds dev_put(dev); 14371da177e4SLinus Torvalds if (idev) 14381da177e4SLinus Torvalds in6_dev_put(idev); 14391da177e4SLinus Torvalds if (rt) 1440d8d1f30bSChangli Gao dst_free(&rt->dst); 14411da177e4SLinus Torvalds return err; 14421da177e4SLinus Torvalds } 14431da177e4SLinus Torvalds 144486872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info) 14451da177e4SLinus Torvalds { 14461da177e4SLinus Torvalds int err; 1447c71099acSThomas Graf struct fib6_table *table; 1448*d1918542SDavid S. Miller struct net *net = dev_net(rt->dst.dev); 14491da177e4SLinus Torvalds 14508ed67789SDaniel Lezcano if (rt == net->ipv6.ip6_null_entry) 14516c813a72SPatrick McHardy return -ENOENT; 14526c813a72SPatrick McHardy 1453c71099acSThomas Graf table = rt->rt6i_table; 1454c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 14551da177e4SLinus Torvalds 145686872cb5SThomas Graf err = fib6_del(rt, info); 1457d8d1f30bSChangli Gao dst_release(&rt->dst); 14581da177e4SLinus Torvalds 1459c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 14601da177e4SLinus Torvalds 14611da177e4SLinus Torvalds return err; 14621da177e4SLinus Torvalds } 14631da177e4SLinus Torvalds 1464e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt) 1465e0a1ad73SThomas Graf { 14664d1169c1SDenis V. Lunev struct nl_info info = { 1467*d1918542SDavid S. Miller .nl_net = dev_net(rt->dst.dev), 14684d1169c1SDenis V. Lunev }; 1469528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 1470e0a1ad73SThomas Graf } 1471e0a1ad73SThomas Graf 147286872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg) 14731da177e4SLinus Torvalds { 1474c71099acSThomas Graf struct fib6_table *table; 14751da177e4SLinus Torvalds struct fib6_node *fn; 14761da177e4SLinus Torvalds struct rt6_info *rt; 14771da177e4SLinus Torvalds int err = -ESRCH; 14781da177e4SLinus Torvalds 14795578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 148038308473SDavid S. Miller if (!table) 1481c71099acSThomas Graf return err; 14821da177e4SLinus Torvalds 1483c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 1484c71099acSThomas Graf 1485c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 148686872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 148786872cb5SThomas Graf &cfg->fc_src, cfg->fc_src_len); 14881da177e4SLinus Torvalds 14891da177e4SLinus Torvalds if (fn) { 1490d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 149186872cb5SThomas Graf if (cfg->fc_ifindex && 1492*d1918542SDavid S. Miller (!rt->dst.dev || 1493*d1918542SDavid S. Miller rt->dst.dev->ifindex != cfg->fc_ifindex)) 14941da177e4SLinus Torvalds continue; 149586872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 149686872cb5SThomas Graf !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 14971da177e4SLinus Torvalds continue; 149886872cb5SThomas Graf if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric) 14991da177e4SLinus Torvalds continue; 1500d8d1f30bSChangli Gao dst_hold(&rt->dst); 1501c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 15021da177e4SLinus Torvalds 150386872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 15041da177e4SLinus Torvalds } 15051da177e4SLinus Torvalds } 1506c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 15071da177e4SLinus Torvalds 15081da177e4SLinus Torvalds return err; 15091da177e4SLinus Torvalds } 15101da177e4SLinus Torvalds 15111da177e4SLinus Torvalds /* 15121da177e4SLinus Torvalds * Handle redirects 15131da177e4SLinus Torvalds */ 1514a6279458SYOSHIFUJI Hideaki struct ip6rd_flowi { 15154c9483b2SDavid S. Miller struct flowi6 fl6; 1516a6279458SYOSHIFUJI Hideaki struct in6_addr gateway; 1517a6279458SYOSHIFUJI Hideaki }; 15181da177e4SLinus Torvalds 15198ed67789SDaniel Lezcano static struct rt6_info *__ip6_route_redirect(struct net *net, 15208ed67789SDaniel Lezcano struct fib6_table *table, 15214c9483b2SDavid S. Miller struct flowi6 *fl6, 1522a6279458SYOSHIFUJI Hideaki int flags) 1523a6279458SYOSHIFUJI Hideaki { 15244c9483b2SDavid S. Miller struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; 1525a6279458SYOSHIFUJI Hideaki struct rt6_info *rt; 1526a6279458SYOSHIFUJI Hideaki struct fib6_node *fn; 1527c71099acSThomas Graf 1528e843b9e1SYOSHIFUJI Hideaki /* 1529e843b9e1SYOSHIFUJI Hideaki * Get the "current" route for this destination and 1530e843b9e1SYOSHIFUJI Hideaki * check if the redirect has come from approriate router. 1531e843b9e1SYOSHIFUJI Hideaki * 1532e843b9e1SYOSHIFUJI Hideaki * RFC 2461 specifies that redirects should only be 1533e843b9e1SYOSHIFUJI Hideaki * accepted if they come from the nexthop to the target. 1534e843b9e1SYOSHIFUJI Hideaki * Due to the way the routes are chosen, this notion 1535e843b9e1SYOSHIFUJI Hideaki * is a bit fuzzy and one might need to check all possible 1536e843b9e1SYOSHIFUJI Hideaki * routes. 1537e843b9e1SYOSHIFUJI Hideaki */ 15381da177e4SLinus Torvalds 1539c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 15404c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1541e843b9e1SYOSHIFUJI Hideaki restart: 1542d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 15431da177e4SLinus Torvalds /* 15441da177e4SLinus Torvalds * Current route is on-link; redirect is always invalid. 15451da177e4SLinus Torvalds * 15461da177e4SLinus Torvalds * Seems, previous statement is not true. It could 15471da177e4SLinus Torvalds * be node, which looks for us as on-link (f.e. proxy ndisc) 15481da177e4SLinus Torvalds * But then router serving it might decide, that we should 15491da177e4SLinus Torvalds * know truth 8)8) --ANK (980726). 15501da177e4SLinus Torvalds */ 1551e843b9e1SYOSHIFUJI Hideaki if (rt6_check_expired(rt)) 1552e843b9e1SYOSHIFUJI Hideaki continue; 15531da177e4SLinus Torvalds if (!(rt->rt6i_flags & RTF_GATEWAY)) 1554e843b9e1SYOSHIFUJI Hideaki continue; 1555*d1918542SDavid S. Miller if (fl6->flowi6_oif != rt->dst.dev->ifindex) 1556e843b9e1SYOSHIFUJI Hideaki continue; 1557a6279458SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway)) 1558e843b9e1SYOSHIFUJI Hideaki continue; 1559e843b9e1SYOSHIFUJI Hideaki break; 1560e843b9e1SYOSHIFUJI Hideaki } 1561a6279458SYOSHIFUJI Hideaki 1562cb15d9c2SYOSHIFUJI Hideaki if (!rt) 15638ed67789SDaniel Lezcano rt = net->ipv6.ip6_null_entry; 15644c9483b2SDavid S. Miller BACKTRACK(net, &fl6->saddr); 1565cb15d9c2SYOSHIFUJI Hideaki out: 1566d8d1f30bSChangli Gao dst_hold(&rt->dst); 1567a6279458SYOSHIFUJI Hideaki 1568c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 15691da177e4SLinus Torvalds 1570a6279458SYOSHIFUJI Hideaki return rt; 1571a6279458SYOSHIFUJI Hideaki }; 1572a6279458SYOSHIFUJI Hideaki 1573b71d1d42SEric Dumazet static struct rt6_info *ip6_route_redirect(const struct in6_addr *dest, 1574b71d1d42SEric Dumazet const struct in6_addr *src, 1575b71d1d42SEric Dumazet const struct in6_addr *gateway, 1576a6279458SYOSHIFUJI Hideaki struct net_device *dev) 1577a6279458SYOSHIFUJI Hideaki { 1578adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 1579c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 1580a6279458SYOSHIFUJI Hideaki struct ip6rd_flowi rdfl = { 15814c9483b2SDavid S. Miller .fl6 = { 15824c9483b2SDavid S. Miller .flowi6_oif = dev->ifindex, 15834c9483b2SDavid S. Miller .daddr = *dest, 15844c9483b2SDavid S. Miller .saddr = *src, 1585a6279458SYOSHIFUJI Hideaki }, 1586a6279458SYOSHIFUJI Hideaki }; 1587adaa70bbSThomas Graf 15884e3fd7a0SAlexey Dobriyan rdfl.gateway = *gateway; 158986c36ce4SBrian Haley 1590adaa70bbSThomas Graf if (rt6_need_strict(dest)) 1591adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_IFACE; 1592a6279458SYOSHIFUJI Hideaki 15934c9483b2SDavid S. Miller return (struct rt6_info *)fib6_rule_lookup(net, &rdfl.fl6, 159458f09b78SDaniel Lezcano flags, __ip6_route_redirect); 1595a6279458SYOSHIFUJI Hideaki } 1596a6279458SYOSHIFUJI Hideaki 1597b71d1d42SEric Dumazet void rt6_redirect(const struct in6_addr *dest, const struct in6_addr *src, 1598b71d1d42SEric Dumazet const struct in6_addr *saddr, 1599a6279458SYOSHIFUJI Hideaki struct neighbour *neigh, u8 *lladdr, int on_link) 1600a6279458SYOSHIFUJI Hideaki { 1601a6279458SYOSHIFUJI Hideaki struct rt6_info *rt, *nrt = NULL; 1602a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 1603c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(neigh->dev); 1604a6279458SYOSHIFUJI Hideaki 1605a6279458SYOSHIFUJI Hideaki rt = ip6_route_redirect(dest, src, saddr, neigh->dev); 1606a6279458SYOSHIFUJI Hideaki 16078ed67789SDaniel Lezcano if (rt == net->ipv6.ip6_null_entry) { 16081da177e4SLinus Torvalds if (net_ratelimit()) 16091da177e4SLinus Torvalds printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop " 16101da177e4SLinus Torvalds "for redirect target\n"); 1611a6279458SYOSHIFUJI Hideaki goto out; 16121da177e4SLinus Torvalds } 16131da177e4SLinus Torvalds 16141da177e4SLinus Torvalds /* 16151da177e4SLinus Torvalds * We have finally decided to accept it. 16161da177e4SLinus Torvalds */ 16171da177e4SLinus Torvalds 16181da177e4SLinus Torvalds neigh_update(neigh, lladdr, NUD_STALE, 16191da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 16201da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 16211da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 16221da177e4SLinus Torvalds NEIGH_UPDATE_F_ISROUTER)) 16231da177e4SLinus Torvalds ); 16241da177e4SLinus Torvalds 16251da177e4SLinus Torvalds /* 16261da177e4SLinus Torvalds * Redirect received -> path was valid. 16271da177e4SLinus Torvalds * Look, redirects are sent only in response to data packets, 16281da177e4SLinus Torvalds * so that this nexthop apparently is reachable. --ANK 16291da177e4SLinus Torvalds */ 1630d8d1f30bSChangli Gao dst_confirm(&rt->dst); 16311da177e4SLinus Torvalds 16321da177e4SLinus Torvalds /* Duplicate redirect: silently ignore. */ 163327217455SDavid Miller if (neigh == dst_get_neighbour_noref_raw(&rt->dst)) 16341da177e4SLinus Torvalds goto out; 16351da177e4SLinus Torvalds 163621efcfa0SEric Dumazet nrt = ip6_rt_copy(rt, dest); 163738308473SDavid S. Miller if (!nrt) 16381da177e4SLinus Torvalds goto out; 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 16411da177e4SLinus Torvalds if (on_link) 16421da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 16431da177e4SLinus Torvalds 16444e3fd7a0SAlexey Dobriyan nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; 164569cce1d1SDavid S. Miller dst_set_neighbour(&nrt->dst, neigh_clone(neigh)); 16461da177e4SLinus Torvalds 164740e22e8fSThomas Graf if (ip6_ins_rt(nrt)) 16481da177e4SLinus Torvalds goto out; 16491da177e4SLinus Torvalds 1650d8d1f30bSChangli Gao netevent.old = &rt->dst; 1651d8d1f30bSChangli Gao netevent.new = &nrt->dst; 16528d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 16538d71740cSTom Tucker 16541da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) { 1655e0a1ad73SThomas Graf ip6_del_rt(rt); 16561da177e4SLinus Torvalds return; 16571da177e4SLinus Torvalds } 16581da177e4SLinus Torvalds 16591da177e4SLinus Torvalds out: 1660d8d1f30bSChangli Gao dst_release(&rt->dst); 16611da177e4SLinus Torvalds } 16621da177e4SLinus Torvalds 16631da177e4SLinus Torvalds /* 16641da177e4SLinus Torvalds * Handle ICMP "packet too big" messages 16651da177e4SLinus Torvalds * i.e. Path MTU discovery 16661da177e4SLinus Torvalds */ 16671da177e4SLinus Torvalds 1668b71d1d42SEric Dumazet static void rt6_do_pmtu_disc(const struct in6_addr *daddr, const struct in6_addr *saddr, 1669ae878ae2SMaciej Żenczykowski struct net *net, u32 pmtu, int ifindex) 16701da177e4SLinus Torvalds { 16711da177e4SLinus Torvalds struct rt6_info *rt, *nrt; 16721da177e4SLinus Torvalds int allfrag = 0; 1673d3052b55SAndrey Vagin again: 1674ae878ae2SMaciej Żenczykowski rt = rt6_lookup(net, daddr, saddr, ifindex, 0); 167538308473SDavid S. Miller if (!rt) 16761da177e4SLinus Torvalds return; 16771da177e4SLinus Torvalds 1678d3052b55SAndrey Vagin if (rt6_check_expired(rt)) { 1679d3052b55SAndrey Vagin ip6_del_rt(rt); 1680d3052b55SAndrey Vagin goto again; 1681d3052b55SAndrey Vagin } 1682d3052b55SAndrey Vagin 1683d8d1f30bSChangli Gao if (pmtu >= dst_mtu(&rt->dst)) 16841da177e4SLinus Torvalds goto out; 16851da177e4SLinus Torvalds 16861da177e4SLinus Torvalds if (pmtu < IPV6_MIN_MTU) { 16871da177e4SLinus Torvalds /* 16881da177e4SLinus Torvalds * According to RFC2460, PMTU is set to the IPv6 Minimum Link 16891da177e4SLinus Torvalds * MTU (1280) and a fragment header should always be included 16901da177e4SLinus Torvalds * after a node receiving Too Big message reporting PMTU is 16911da177e4SLinus Torvalds * less than the IPv6 Minimum Link MTU. 16921da177e4SLinus Torvalds */ 16931da177e4SLinus Torvalds pmtu = IPV6_MIN_MTU; 16941da177e4SLinus Torvalds allfrag = 1; 16951da177e4SLinus Torvalds } 16961da177e4SLinus Torvalds 16971da177e4SLinus Torvalds /* New mtu received -> path was valid. 16981da177e4SLinus Torvalds They are sent only in response to data packets, 16991da177e4SLinus Torvalds so that this nexthop apparently is reachable. --ANK 17001da177e4SLinus Torvalds */ 1701d8d1f30bSChangli Gao dst_confirm(&rt->dst); 17021da177e4SLinus Torvalds 17031da177e4SLinus Torvalds /* Host route. If it is static, it would be better 17041da177e4SLinus Torvalds not to override it, but add new one, so that 17051da177e4SLinus Torvalds when cache entry will expire old pmtu 17061da177e4SLinus Torvalds would return automatically. 17071da177e4SLinus Torvalds */ 17081da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) { 1709defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_MTU, pmtu); 1710defb3519SDavid S. Miller if (allfrag) { 1711defb3519SDavid S. Miller u32 features = dst_metric(&rt->dst, RTAX_FEATURES); 1712defb3519SDavid S. Miller features |= RTAX_FEATURE_ALLFRAG; 1713defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_FEATURES, features); 1714defb3519SDavid S. Miller } 1715d8d1f30bSChangli Gao dst_set_expires(&rt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires); 17161da177e4SLinus Torvalds rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES; 17171da177e4SLinus Torvalds goto out; 17181da177e4SLinus Torvalds } 17191da177e4SLinus Torvalds 17201da177e4SLinus Torvalds /* Network route. 17211da177e4SLinus Torvalds Two cases are possible: 17221da177e4SLinus Torvalds 1. It is connected route. Action: COW 17231da177e4SLinus Torvalds 2. It is gatewayed route or NONEXTHOP route. Action: clone it. 17241da177e4SLinus Torvalds */ 172527217455SDavid Miller if (!dst_get_neighbour_noref_raw(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP)) 1726a1e78363SYOSHIFUJI Hideaki nrt = rt6_alloc_cow(rt, daddr, saddr); 1727d5315b50SYOSHIFUJI Hideaki else 1728d5315b50SYOSHIFUJI Hideaki nrt = rt6_alloc_clone(rt, daddr); 1729a1e78363SYOSHIFUJI Hideaki 1730d5315b50SYOSHIFUJI Hideaki if (nrt) { 1731defb3519SDavid S. Miller dst_metric_set(&nrt->dst, RTAX_MTU, pmtu); 1732defb3519SDavid S. Miller if (allfrag) { 1733defb3519SDavid S. Miller u32 features = dst_metric(&nrt->dst, RTAX_FEATURES); 1734defb3519SDavid S. Miller features |= RTAX_FEATURE_ALLFRAG; 1735defb3519SDavid S. Miller dst_metric_set(&nrt->dst, RTAX_FEATURES, features); 1736defb3519SDavid S. Miller } 1737a1e78363SYOSHIFUJI Hideaki 17381da177e4SLinus Torvalds /* According to RFC 1981, detecting PMTU increase shouldn't be 1739a1e78363SYOSHIFUJI Hideaki * happened within 5 mins, the recommended timer is 10 mins. 1740a1e78363SYOSHIFUJI Hideaki * Here this route expiration time is set to ip6_rt_mtu_expires 1741a1e78363SYOSHIFUJI Hideaki * which is 10 mins. After 10 mins the decreased pmtu is expired 1742a1e78363SYOSHIFUJI Hideaki * and detecting PMTU increase will be automatically happened. 17431da177e4SLinus Torvalds */ 1744d8d1f30bSChangli Gao dst_set_expires(&nrt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires); 17451da177e4SLinus Torvalds nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES; 1746a1e78363SYOSHIFUJI Hideaki 174740e22e8fSThomas Graf ip6_ins_rt(nrt); 17481da177e4SLinus Torvalds } 17491da177e4SLinus Torvalds out: 1750d8d1f30bSChangli Gao dst_release(&rt->dst); 17511da177e4SLinus Torvalds } 17521da177e4SLinus Torvalds 1753b71d1d42SEric Dumazet void rt6_pmtu_discovery(const struct in6_addr *daddr, const struct in6_addr *saddr, 1754ae878ae2SMaciej Żenczykowski struct net_device *dev, u32 pmtu) 1755ae878ae2SMaciej Żenczykowski { 1756ae878ae2SMaciej Żenczykowski struct net *net = dev_net(dev); 1757ae878ae2SMaciej Żenczykowski 1758ae878ae2SMaciej Żenczykowski /* 1759ae878ae2SMaciej Żenczykowski * RFC 1981 states that a node "MUST reduce the size of the packets it 1760ae878ae2SMaciej Żenczykowski * is sending along the path" that caused the Packet Too Big message. 1761ae878ae2SMaciej Żenczykowski * Since it's not possible in the general case to determine which 1762ae878ae2SMaciej Żenczykowski * interface was used to send the original packet, we update the MTU 1763ae878ae2SMaciej Żenczykowski * on the interface that will be used to send future packets. We also 1764ae878ae2SMaciej Żenczykowski * update the MTU on the interface that received the Packet Too Big in 1765ae878ae2SMaciej Żenczykowski * case the original packet was forced out that interface with 1766ae878ae2SMaciej Żenczykowski * SO_BINDTODEVICE or similar. This is the next best thing to the 1767ae878ae2SMaciej Żenczykowski * correct behaviour, which would be to update the MTU on all 1768ae878ae2SMaciej Żenczykowski * interfaces. 1769ae878ae2SMaciej Żenczykowski */ 1770ae878ae2SMaciej Żenczykowski rt6_do_pmtu_disc(daddr, saddr, net, pmtu, 0); 1771ae878ae2SMaciej Żenczykowski rt6_do_pmtu_disc(daddr, saddr, net, pmtu, dev->ifindex); 1772ae878ae2SMaciej Żenczykowski } 1773ae878ae2SMaciej Żenczykowski 17741da177e4SLinus Torvalds /* 17751da177e4SLinus Torvalds * Misc support functions 17761da177e4SLinus Torvalds */ 17771da177e4SLinus Torvalds 177821efcfa0SEric Dumazet static struct rt6_info *ip6_rt_copy(const struct rt6_info *ort, 177921efcfa0SEric Dumazet const struct in6_addr *dest) 17801da177e4SLinus Torvalds { 1781*d1918542SDavid S. Miller struct net *net = dev_net(ort->dst.dev); 17825c1e6aa3SDavid S. Miller struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, 1783957c665fSDavid S. Miller ort->dst.dev, 0); 17841da177e4SLinus Torvalds 17851da177e4SLinus Torvalds if (rt) { 1786d8d1f30bSChangli Gao rt->dst.input = ort->dst.input; 1787d8d1f30bSChangli Gao rt->dst.output = ort->dst.output; 17888e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 17891da177e4SLinus Torvalds 17904e3fd7a0SAlexey Dobriyan rt->rt6i_dst.addr = *dest; 17918e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 1792defb3519SDavid S. Miller dst_copy_metrics(&rt->dst, &ort->dst); 1793d8d1f30bSChangli Gao rt->dst.error = ort->dst.error; 17941da177e4SLinus Torvalds rt->rt6i_idev = ort->rt6i_idev; 17951da177e4SLinus Torvalds if (rt->rt6i_idev) 17961da177e4SLinus Torvalds in6_dev_hold(rt->rt6i_idev); 1797d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 1798*d1918542SDavid S. Miller rt->dst.expires = 0; 17991da177e4SLinus Torvalds 18004e3fd7a0SAlexey Dobriyan rt->rt6i_gateway = ort->rt6i_gateway; 18011da177e4SLinus Torvalds rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES; 18021da177e4SLinus Torvalds rt->rt6i_metric = 0; 18031da177e4SLinus Torvalds 18041da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 18051da177e4SLinus Torvalds memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 18061da177e4SLinus Torvalds #endif 18070f6c6392SFlorian Westphal memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key)); 1808c71099acSThomas Graf rt->rt6i_table = ort->rt6i_table; 18091da177e4SLinus Torvalds } 18101da177e4SLinus Torvalds return rt; 18111da177e4SLinus Torvalds } 18121da177e4SLinus Torvalds 181370ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 1814efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 1815b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 1816b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex) 181770ceb4f5SYOSHIFUJI Hideaki { 181870ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 181970ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt = NULL; 1820c71099acSThomas Graf struct fib6_table *table; 182170ceb4f5SYOSHIFUJI Hideaki 1822efa2cea0SDaniel Lezcano table = fib6_get_table(net, RT6_TABLE_INFO); 182338308473SDavid S. Miller if (!table) 1824c71099acSThomas Graf return NULL; 1825c71099acSThomas Graf 1826c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 1827c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0); 182870ceb4f5SYOSHIFUJI Hideaki if (!fn) 182970ceb4f5SYOSHIFUJI Hideaki goto out; 183070ceb4f5SYOSHIFUJI Hideaki 1831d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 1832*d1918542SDavid S. Miller if (rt->dst.dev->ifindex != ifindex) 183370ceb4f5SYOSHIFUJI Hideaki continue; 183470ceb4f5SYOSHIFUJI Hideaki if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) 183570ceb4f5SYOSHIFUJI Hideaki continue; 183670ceb4f5SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr)) 183770ceb4f5SYOSHIFUJI Hideaki continue; 1838d8d1f30bSChangli Gao dst_hold(&rt->dst); 183970ceb4f5SYOSHIFUJI Hideaki break; 184070ceb4f5SYOSHIFUJI Hideaki } 184170ceb4f5SYOSHIFUJI Hideaki out: 1842c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 184370ceb4f5SYOSHIFUJI Hideaki return rt; 184470ceb4f5SYOSHIFUJI Hideaki } 184570ceb4f5SYOSHIFUJI Hideaki 1846efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 1847b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 1848b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex, 184970ceb4f5SYOSHIFUJI Hideaki unsigned pref) 185070ceb4f5SYOSHIFUJI Hideaki { 185186872cb5SThomas Graf struct fib6_config cfg = { 185286872cb5SThomas Graf .fc_table = RT6_TABLE_INFO, 1853238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 185486872cb5SThomas Graf .fc_ifindex = ifindex, 185586872cb5SThomas Graf .fc_dst_len = prefixlen, 185686872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 185786872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 1858efa2cea0SDaniel Lezcano .fc_nlinfo.pid = 0, 1859efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 1860efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 186186872cb5SThomas Graf }; 186270ceb4f5SYOSHIFUJI Hideaki 18634e3fd7a0SAlexey Dobriyan cfg.fc_dst = *prefix; 18644e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 186586872cb5SThomas Graf 1866e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 1867e317da96SYOSHIFUJI Hideaki if (!prefixlen) 186886872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 186970ceb4f5SYOSHIFUJI Hideaki 187086872cb5SThomas Graf ip6_route_add(&cfg); 187170ceb4f5SYOSHIFUJI Hideaki 1872efa2cea0SDaniel Lezcano return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex); 187370ceb4f5SYOSHIFUJI Hideaki } 187470ceb4f5SYOSHIFUJI Hideaki #endif 187570ceb4f5SYOSHIFUJI Hideaki 1876b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev) 18771da177e4SLinus Torvalds { 18781da177e4SLinus Torvalds struct rt6_info *rt; 1879c71099acSThomas Graf struct fib6_table *table; 18801da177e4SLinus Torvalds 1881c346dca1SYOSHIFUJI Hideaki table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT); 188238308473SDavid S. Miller if (!table) 1883c71099acSThomas Graf return NULL; 18841da177e4SLinus Torvalds 1885c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 1886d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) { 1887*d1918542SDavid S. Miller if (dev == rt->dst.dev && 1888045927ffSYOSHIFUJI Hideaki ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 18891da177e4SLinus Torvalds ipv6_addr_equal(&rt->rt6i_gateway, addr)) 18901da177e4SLinus Torvalds break; 18911da177e4SLinus Torvalds } 18921da177e4SLinus Torvalds if (rt) 1893d8d1f30bSChangli Gao dst_hold(&rt->dst); 1894c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 18951da177e4SLinus Torvalds return rt; 18961da177e4SLinus Torvalds } 18971da177e4SLinus Torvalds 1898b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr, 1899ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 1900ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 19011da177e4SLinus Torvalds { 190286872cb5SThomas Graf struct fib6_config cfg = { 190386872cb5SThomas Graf .fc_table = RT6_TABLE_DFLT, 1904238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 190586872cb5SThomas Graf .fc_ifindex = dev->ifindex, 190686872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 190786872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 19085578689aSDaniel Lezcano .fc_nlinfo.pid = 0, 19095578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 1910c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 191186872cb5SThomas Graf }; 19121da177e4SLinus Torvalds 19134e3fd7a0SAlexey Dobriyan cfg.fc_gateway = *gwaddr; 19141da177e4SLinus Torvalds 191586872cb5SThomas Graf ip6_route_add(&cfg); 19161da177e4SLinus Torvalds 19171da177e4SLinus Torvalds return rt6_get_dflt_router(gwaddr, dev); 19181da177e4SLinus Torvalds } 19191da177e4SLinus Torvalds 19207b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net) 19211da177e4SLinus Torvalds { 19221da177e4SLinus Torvalds struct rt6_info *rt; 1923c71099acSThomas Graf struct fib6_table *table; 1924c71099acSThomas Graf 1925c71099acSThomas Graf /* NOTE: Keep consistent with rt6_get_dflt_router */ 19267b4da532SDaniel Lezcano table = fib6_get_table(net, RT6_TABLE_DFLT); 192738308473SDavid S. Miller if (!table) 1928c71099acSThomas Graf return; 19291da177e4SLinus Torvalds 19301da177e4SLinus Torvalds restart: 1931c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 1932d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 19331da177e4SLinus Torvalds if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) { 1934d8d1f30bSChangli Gao dst_hold(&rt->dst); 1935c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 1936e0a1ad73SThomas Graf ip6_del_rt(rt); 19371da177e4SLinus Torvalds goto restart; 19381da177e4SLinus Torvalds } 19391da177e4SLinus Torvalds } 1940c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 19411da177e4SLinus Torvalds } 19421da177e4SLinus Torvalds 19435578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 19445578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 194586872cb5SThomas Graf struct fib6_config *cfg) 194686872cb5SThomas Graf { 194786872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 194886872cb5SThomas Graf 194986872cb5SThomas Graf cfg->fc_table = RT6_TABLE_MAIN; 195086872cb5SThomas Graf cfg->fc_ifindex = rtmsg->rtmsg_ifindex; 195186872cb5SThomas Graf cfg->fc_metric = rtmsg->rtmsg_metric; 195286872cb5SThomas Graf cfg->fc_expires = rtmsg->rtmsg_info; 195386872cb5SThomas Graf cfg->fc_dst_len = rtmsg->rtmsg_dst_len; 195486872cb5SThomas Graf cfg->fc_src_len = rtmsg->rtmsg_src_len; 195586872cb5SThomas Graf cfg->fc_flags = rtmsg->rtmsg_flags; 195686872cb5SThomas Graf 19575578689aSDaniel Lezcano cfg->fc_nlinfo.nl_net = net; 1958f1243c2dSBenjamin Thery 19594e3fd7a0SAlexey Dobriyan cfg->fc_dst = rtmsg->rtmsg_dst; 19604e3fd7a0SAlexey Dobriyan cfg->fc_src = rtmsg->rtmsg_src; 19614e3fd7a0SAlexey Dobriyan cfg->fc_gateway = rtmsg->rtmsg_gateway; 196286872cb5SThomas Graf } 196386872cb5SThomas Graf 19645578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) 19651da177e4SLinus Torvalds { 196686872cb5SThomas Graf struct fib6_config cfg; 19671da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 19681da177e4SLinus Torvalds int err; 19691da177e4SLinus Torvalds 19701da177e4SLinus Torvalds switch(cmd) { 19711da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 19721da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 19731da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 19741da177e4SLinus Torvalds return -EPERM; 19751da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 19761da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 19771da177e4SLinus Torvalds if (err) 19781da177e4SLinus Torvalds return -EFAULT; 19791da177e4SLinus Torvalds 19805578689aSDaniel Lezcano rtmsg_to_fib6_config(net, &rtmsg, &cfg); 198186872cb5SThomas Graf 19821da177e4SLinus Torvalds rtnl_lock(); 19831da177e4SLinus Torvalds switch (cmd) { 19841da177e4SLinus Torvalds case SIOCADDRT: 198586872cb5SThomas Graf err = ip6_route_add(&cfg); 19861da177e4SLinus Torvalds break; 19871da177e4SLinus Torvalds case SIOCDELRT: 198886872cb5SThomas Graf err = ip6_route_del(&cfg); 19891da177e4SLinus Torvalds break; 19901da177e4SLinus Torvalds default: 19911da177e4SLinus Torvalds err = -EINVAL; 19921da177e4SLinus Torvalds } 19931da177e4SLinus Torvalds rtnl_unlock(); 19941da177e4SLinus Torvalds 19951da177e4SLinus Torvalds return err; 19963ff50b79SStephen Hemminger } 19971da177e4SLinus Torvalds 19981da177e4SLinus Torvalds return -EINVAL; 19991da177e4SLinus Torvalds } 20001da177e4SLinus Torvalds 20011da177e4SLinus Torvalds /* 20021da177e4SLinus Torvalds * Drop the packet on the floor 20031da177e4SLinus Torvalds */ 20041da177e4SLinus Torvalds 2005d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 20061da177e4SLinus Torvalds { 2007612f09e8SYOSHIFUJI Hideaki int type; 2008adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 2009612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 2010612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 20110660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 201245bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 20133bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 20143bd653c8SDenis V. Lunev IPSTATS_MIB_INADDRERRORS); 2015612f09e8SYOSHIFUJI Hideaki break; 2016612f09e8SYOSHIFUJI Hideaki } 2017612f09e8SYOSHIFUJI Hideaki /* FALLTHROUGH */ 2018612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 20193bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 20203bd653c8SDenis V. Lunev ipstats_mib_noroutes); 2021612f09e8SYOSHIFUJI Hideaki break; 2022612f09e8SYOSHIFUJI Hideaki } 20233ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 20241da177e4SLinus Torvalds kfree_skb(skb); 20251da177e4SLinus Torvalds return 0; 20261da177e4SLinus Torvalds } 20271da177e4SLinus Torvalds 20289ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 20299ce8ade0SThomas Graf { 2030612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 20319ce8ade0SThomas Graf } 20329ce8ade0SThomas Graf 203320380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb) 20341da177e4SLinus Torvalds { 2035adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 2036612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 20371da177e4SLinus Torvalds } 20381da177e4SLinus Torvalds 20396723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES 20406723ab54SDavid S. Miller 20419ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 20429ce8ade0SThomas Graf { 2043612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 20449ce8ade0SThomas Graf } 20459ce8ade0SThomas Graf 20469ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb) 20479ce8ade0SThomas Graf { 2048adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 2049612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 20509ce8ade0SThomas Graf } 20519ce8ade0SThomas Graf 20526723ab54SDavid S. Miller #endif 20536723ab54SDavid S. Miller 20541da177e4SLinus Torvalds /* 20551da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 20561da177e4SLinus Torvalds */ 20571da177e4SLinus Torvalds 20581da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, 20591da177e4SLinus Torvalds const struct in6_addr *addr, 20608f031519SDavid S. Miller bool anycast) 20611da177e4SLinus Torvalds { 2062c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 20635c1e6aa3SDavid S. Miller struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, 2064957c665fSDavid S. Miller net->loopback_dev, 0); 2065f83c7790SDavid S. Miller int err; 20661da177e4SLinus Torvalds 206738308473SDavid S. Miller if (!rt) { 206840385653SBen Greear if (net_ratelimit()) 206940385653SBen Greear pr_warning("IPv6: Maximum number of routes reached," 207040385653SBen Greear " consider increasing route/max_size.\n"); 20711da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 207240385653SBen Greear } 20731da177e4SLinus Torvalds 20741da177e4SLinus Torvalds in6_dev_hold(idev); 20751da177e4SLinus Torvalds 207611d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 2077d8d1f30bSChangli Gao rt->dst.input = ip6_input; 2078d8d1f30bSChangli Gao rt->dst.output = ip6_output; 20791da177e4SLinus Torvalds rt->rt6i_idev = idev; 2080d8d1f30bSChangli Gao rt->dst.obsolete = -1; 20811da177e4SLinus Torvalds 20821da177e4SLinus Torvalds rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP; 208358c4fb86SYOSHIFUJI Hideaki if (anycast) 208458c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 208558c4fb86SYOSHIFUJI Hideaki else 20861da177e4SLinus Torvalds rt->rt6i_flags |= RTF_LOCAL; 2087f83c7790SDavid S. Miller err = rt6_bind_neighbour(rt); 2088f83c7790SDavid S. Miller if (err) { 2089d8d1f30bSChangli Gao dst_free(&rt->dst); 2090f83c7790SDavid S. Miller return ERR_PTR(err); 20911da177e4SLinus Torvalds } 20921da177e4SLinus Torvalds 20934e3fd7a0SAlexey Dobriyan rt->rt6i_dst.addr = *addr; 20941da177e4SLinus Torvalds rt->rt6i_dst.plen = 128; 20955578689aSDaniel Lezcano rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL); 20961da177e4SLinus Torvalds 2097d8d1f30bSChangli Gao atomic_set(&rt->dst.__refcnt, 1); 20981da177e4SLinus Torvalds 20991da177e4SLinus Torvalds return rt; 21001da177e4SLinus Torvalds } 21011da177e4SLinus Torvalds 2102c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net, 2103c3968a85SDaniel Walter struct rt6_info *rt, 2104b71d1d42SEric Dumazet const struct in6_addr *daddr, 2105c3968a85SDaniel Walter unsigned int prefs, 2106c3968a85SDaniel Walter struct in6_addr *saddr) 2107c3968a85SDaniel Walter { 2108c3968a85SDaniel Walter struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt); 2109c3968a85SDaniel Walter int err = 0; 2110c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) 21114e3fd7a0SAlexey Dobriyan *saddr = rt->rt6i_prefsrc.addr; 2112c3968a85SDaniel Walter else 2113c3968a85SDaniel Walter err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL, 2114c3968a85SDaniel Walter daddr, prefs, saddr); 2115c3968a85SDaniel Walter return err; 2116c3968a85SDaniel Walter } 2117c3968a85SDaniel Walter 2118c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 2119c3968a85SDaniel Walter struct arg_dev_net_ip { 2120c3968a85SDaniel Walter struct net_device *dev; 2121c3968a85SDaniel Walter struct net *net; 2122c3968a85SDaniel Walter struct in6_addr *addr; 2123c3968a85SDaniel Walter }; 2124c3968a85SDaniel Walter 2125c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg) 2126c3968a85SDaniel Walter { 2127c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 2128c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 2129c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 2130c3968a85SDaniel Walter 2131*d1918542SDavid S. Miller if (((void *)rt->dst.dev == dev || !dev) && 2132c3968a85SDaniel Walter rt != net->ipv6.ip6_null_entry && 2133c3968a85SDaniel Walter ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) { 2134c3968a85SDaniel Walter /* remove prefsrc entry */ 2135c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 2136c3968a85SDaniel Walter } 2137c3968a85SDaniel Walter return 0; 2138c3968a85SDaniel Walter } 2139c3968a85SDaniel Walter 2140c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 2141c3968a85SDaniel Walter { 2142c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 2143c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 2144c3968a85SDaniel Walter .dev = ifp->idev->dev, 2145c3968a85SDaniel Walter .net = net, 2146c3968a85SDaniel Walter .addr = &ifp->addr, 2147c3968a85SDaniel Walter }; 2148c3968a85SDaniel Walter fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni); 2149c3968a85SDaniel Walter } 2150c3968a85SDaniel Walter 21518ed67789SDaniel Lezcano struct arg_dev_net { 21528ed67789SDaniel Lezcano struct net_device *dev; 21538ed67789SDaniel Lezcano struct net *net; 21548ed67789SDaniel Lezcano }; 21558ed67789SDaniel Lezcano 21561da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg) 21571da177e4SLinus Torvalds { 2158bc3ef660Sstephen hemminger const struct arg_dev_net *adn = arg; 2159bc3ef660Sstephen hemminger const struct net_device *dev = adn->dev; 21608ed67789SDaniel Lezcano 2161*d1918542SDavid S. Miller if ((rt->dst.dev == dev || !dev) && 2162c159d30cSDavid S. Miller rt != adn->net->ipv6.ip6_null_entry) 21631da177e4SLinus Torvalds return -1; 2164c159d30cSDavid S. Miller 21651da177e4SLinus Torvalds return 0; 21661da177e4SLinus Torvalds } 21671da177e4SLinus Torvalds 2168f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev) 21691da177e4SLinus Torvalds { 21708ed67789SDaniel Lezcano struct arg_dev_net adn = { 21718ed67789SDaniel Lezcano .dev = dev, 21728ed67789SDaniel Lezcano .net = net, 21738ed67789SDaniel Lezcano }; 21748ed67789SDaniel Lezcano 21758ed67789SDaniel Lezcano fib6_clean_all(net, fib6_ifdown, 0, &adn); 21761e493d19SDavid S. Miller icmp6_clean_all(fib6_ifdown, &adn); 21771da177e4SLinus Torvalds } 21781da177e4SLinus Torvalds 21791da177e4SLinus Torvalds struct rt6_mtu_change_arg 21801da177e4SLinus Torvalds { 21811da177e4SLinus Torvalds struct net_device *dev; 21821da177e4SLinus Torvalds unsigned mtu; 21831da177e4SLinus Torvalds }; 21841da177e4SLinus Torvalds 21851da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg) 21861da177e4SLinus Torvalds { 21871da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 21881da177e4SLinus Torvalds struct inet6_dev *idev; 21891da177e4SLinus Torvalds 21901da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 21911da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 21921da177e4SLinus Torvalds We still use this lock to block changes 21931da177e4SLinus Torvalds caused by addrconf/ndisc. 21941da177e4SLinus Torvalds */ 21951da177e4SLinus Torvalds 21961da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 219738308473SDavid S. Miller if (!idev) 21981da177e4SLinus Torvalds return 0; 21991da177e4SLinus Torvalds 22001da177e4SLinus Torvalds /* For administrative MTU increase, there is no way to discover 22011da177e4SLinus Torvalds IPv6 PMTU increase, so PMTU increase should be updated here. 22021da177e4SLinus Torvalds Since RFC 1981 doesn't include administrative MTU increase 22031da177e4SLinus Torvalds update PMTU increase is a MUST. (i.e. jumbo frame) 22041da177e4SLinus Torvalds */ 22051da177e4SLinus Torvalds /* 22061da177e4SLinus Torvalds If new MTU is less than route PMTU, this new MTU will be the 22071da177e4SLinus Torvalds lowest MTU in the path, update the route PMTU to reflect PMTU 22081da177e4SLinus Torvalds decreases; if new MTU is greater than route PMTU, and the 22091da177e4SLinus Torvalds old MTU is the lowest MTU in the path, update the route PMTU 22101da177e4SLinus Torvalds to reflect the increase. In this case if the other nodes' MTU 22111da177e4SLinus Torvalds also have the lowest MTU, TOO BIG MESSAGE will be lead to 22121da177e4SLinus Torvalds PMTU discouvery. 22131da177e4SLinus Torvalds */ 2214*d1918542SDavid S. Miller if (rt->dst.dev == arg->dev && 2215d8d1f30bSChangli Gao !dst_metric_locked(&rt->dst, RTAX_MTU) && 2216d8d1f30bSChangli Gao (dst_mtu(&rt->dst) >= arg->mtu || 2217d8d1f30bSChangli Gao (dst_mtu(&rt->dst) < arg->mtu && 2218d8d1f30bSChangli Gao dst_mtu(&rt->dst) == idev->cnf.mtu6))) { 2219defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu); 2220566cfd8fSSimon Arlott } 22211da177e4SLinus Torvalds return 0; 22221da177e4SLinus Torvalds } 22231da177e4SLinus Torvalds 22241da177e4SLinus Torvalds void rt6_mtu_change(struct net_device *dev, unsigned mtu) 22251da177e4SLinus Torvalds { 2226c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 2227c71099acSThomas Graf .dev = dev, 2228c71099acSThomas Graf .mtu = mtu, 2229c71099acSThomas Graf }; 22301da177e4SLinus Torvalds 2231c346dca1SYOSHIFUJI Hideaki fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg); 22321da177e4SLinus Torvalds } 22331da177e4SLinus Torvalds 2234ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 22355176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 223686872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 2237ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 223886872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 223986872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 224086872cb5SThomas Graf }; 224186872cb5SThomas Graf 224286872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 224386872cb5SThomas Graf struct fib6_config *cfg) 22441da177e4SLinus Torvalds { 224586872cb5SThomas Graf struct rtmsg *rtm; 224686872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 224786872cb5SThomas Graf int err; 22481da177e4SLinus Torvalds 224986872cb5SThomas Graf err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy); 225086872cb5SThomas Graf if (err < 0) 225186872cb5SThomas Graf goto errout; 22521da177e4SLinus Torvalds 225386872cb5SThomas Graf err = -EINVAL; 225486872cb5SThomas Graf rtm = nlmsg_data(nlh); 225586872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 225686872cb5SThomas Graf 225786872cb5SThomas Graf cfg->fc_table = rtm->rtm_table; 225886872cb5SThomas Graf cfg->fc_dst_len = rtm->rtm_dst_len; 225986872cb5SThomas Graf cfg->fc_src_len = rtm->rtm_src_len; 226086872cb5SThomas Graf cfg->fc_flags = RTF_UP; 226186872cb5SThomas Graf cfg->fc_protocol = rtm->rtm_protocol; 226286872cb5SThomas Graf 226386872cb5SThomas Graf if (rtm->rtm_type == RTN_UNREACHABLE) 226486872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 226586872cb5SThomas Graf 2266ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 2267ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 2268ab79ad14SMaciej Żenczykowski 226986872cb5SThomas Graf cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid; 227086872cb5SThomas Graf cfg->fc_nlinfo.nlh = nlh; 22713b1e0a65SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = sock_net(skb->sk); 227286872cb5SThomas Graf 227386872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 227486872cb5SThomas Graf nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16); 227586872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 22761da177e4SLinus Torvalds } 227786872cb5SThomas Graf 227886872cb5SThomas Graf if (tb[RTA_DST]) { 227986872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 228086872cb5SThomas Graf 228186872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 228286872cb5SThomas Graf goto errout; 228386872cb5SThomas Graf 228486872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 22851da177e4SLinus Torvalds } 228686872cb5SThomas Graf 228786872cb5SThomas Graf if (tb[RTA_SRC]) { 228886872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 228986872cb5SThomas Graf 229086872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 229186872cb5SThomas Graf goto errout; 229286872cb5SThomas Graf 229386872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 22941da177e4SLinus Torvalds } 229586872cb5SThomas Graf 2296c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 2297c3968a85SDaniel Walter nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16); 2298c3968a85SDaniel Walter 229986872cb5SThomas Graf if (tb[RTA_OIF]) 230086872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 230186872cb5SThomas Graf 230286872cb5SThomas Graf if (tb[RTA_PRIORITY]) 230386872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 230486872cb5SThomas Graf 230586872cb5SThomas Graf if (tb[RTA_METRICS]) { 230686872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 230786872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 23081da177e4SLinus Torvalds } 230986872cb5SThomas Graf 231086872cb5SThomas Graf if (tb[RTA_TABLE]) 231186872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 231286872cb5SThomas Graf 231386872cb5SThomas Graf err = 0; 231486872cb5SThomas Graf errout: 231586872cb5SThomas Graf return err; 23161da177e4SLinus Torvalds } 23171da177e4SLinus Torvalds 2318c127ea2cSThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 23191da177e4SLinus Torvalds { 232086872cb5SThomas Graf struct fib6_config cfg; 232186872cb5SThomas Graf int err; 23221da177e4SLinus Torvalds 232386872cb5SThomas Graf err = rtm_to_fib6_config(skb, nlh, &cfg); 232486872cb5SThomas Graf if (err < 0) 232586872cb5SThomas Graf return err; 232686872cb5SThomas Graf 232786872cb5SThomas Graf return ip6_route_del(&cfg); 23281da177e4SLinus Torvalds } 23291da177e4SLinus Torvalds 2330c127ea2cSThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 23311da177e4SLinus Torvalds { 233286872cb5SThomas Graf struct fib6_config cfg; 233386872cb5SThomas Graf int err; 23341da177e4SLinus Torvalds 233586872cb5SThomas Graf err = rtm_to_fib6_config(skb, nlh, &cfg); 233686872cb5SThomas Graf if (err < 0) 233786872cb5SThomas Graf return err; 233886872cb5SThomas Graf 233986872cb5SThomas Graf return ip6_route_add(&cfg); 23401da177e4SLinus Torvalds } 23411da177e4SLinus Torvalds 2342339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void) 2343339bf98fSThomas Graf { 2344339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 2345339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 2346339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 2347339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 2348339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 2349339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 2350339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 2351339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 2352339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 23536a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 2354339bf98fSThomas Graf + nla_total_size(sizeof(struct rta_cacheinfo)); 2355339bf98fSThomas Graf } 2356339bf98fSThomas Graf 2357191cd582SBrian Haley static int rt6_fill_node(struct net *net, 2358191cd582SBrian Haley struct sk_buff *skb, struct rt6_info *rt, 23590d51aa80SJamal Hadi Salim struct in6_addr *dst, struct in6_addr *src, 23600d51aa80SJamal Hadi Salim int iif, int type, u32 pid, u32 seq, 23617bc570c8SYOSHIFUJI Hideaki int prefix, int nowait, unsigned int flags) 23621da177e4SLinus Torvalds { 23631da177e4SLinus Torvalds struct rtmsg *rtm; 23641da177e4SLinus Torvalds struct nlmsghdr *nlh; 2365e3703b3dSThomas Graf long expires; 23669e762a4aSPatrick McHardy u32 table; 2367f2c31e32SEric Dumazet struct neighbour *n; 23681da177e4SLinus Torvalds 23691da177e4SLinus Torvalds if (prefix) { /* user wants prefix routes only */ 23701da177e4SLinus Torvalds if (!(rt->rt6i_flags & RTF_PREFIX_RT)) { 23711da177e4SLinus Torvalds /* success since this is not a prefix route */ 23721da177e4SLinus Torvalds return 1; 23731da177e4SLinus Torvalds } 23741da177e4SLinus Torvalds } 23751da177e4SLinus Torvalds 23762d7202bfSThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags); 237738308473SDavid S. Miller if (!nlh) 237826932566SPatrick McHardy return -EMSGSIZE; 23792d7202bfSThomas Graf 23802d7202bfSThomas Graf rtm = nlmsg_data(nlh); 23811da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 23821da177e4SLinus Torvalds rtm->rtm_dst_len = rt->rt6i_dst.plen; 23831da177e4SLinus Torvalds rtm->rtm_src_len = rt->rt6i_src.plen; 23841da177e4SLinus Torvalds rtm->rtm_tos = 0; 2385c71099acSThomas Graf if (rt->rt6i_table) 23869e762a4aSPatrick McHardy table = rt->rt6i_table->tb6_id; 2387c71099acSThomas Graf else 23889e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 23899e762a4aSPatrick McHardy rtm->rtm_table = table; 23902d7202bfSThomas Graf NLA_PUT_U32(skb, RTA_TABLE, table); 23911da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_REJECT) 23921da177e4SLinus Torvalds rtm->rtm_type = RTN_UNREACHABLE; 2393ab79ad14SMaciej Żenczykowski else if (rt->rt6i_flags & RTF_LOCAL) 2394ab79ad14SMaciej Żenczykowski rtm->rtm_type = RTN_LOCAL; 2395*d1918542SDavid S. Miller else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK)) 23961da177e4SLinus Torvalds rtm->rtm_type = RTN_LOCAL; 23971da177e4SLinus Torvalds else 23981da177e4SLinus Torvalds rtm->rtm_type = RTN_UNICAST; 23991da177e4SLinus Torvalds rtm->rtm_flags = 0; 24001da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 24011da177e4SLinus Torvalds rtm->rtm_protocol = rt->rt6i_protocol; 24021da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_DYNAMIC) 24031da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_REDIRECT; 24041da177e4SLinus Torvalds else if (rt->rt6i_flags & RTF_ADDRCONF) 24051da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_KERNEL; 24061da177e4SLinus Torvalds else if (rt->rt6i_flags & RTF_DEFAULT) 24071da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_RA; 24081da177e4SLinus Torvalds 24091da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) 24101da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 24111da177e4SLinus Torvalds 24121da177e4SLinus Torvalds if (dst) { 24132d7202bfSThomas Graf NLA_PUT(skb, RTA_DST, 16, dst); 24141da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 24151da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 24162d7202bfSThomas Graf NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr); 24171da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 24181da177e4SLinus Torvalds if (src) { 24192d7202bfSThomas Graf NLA_PUT(skb, RTA_SRC, 16, src); 24201da177e4SLinus Torvalds rtm->rtm_src_len = 128; 24211da177e4SLinus Torvalds } else if (rtm->rtm_src_len) 24222d7202bfSThomas Graf NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr); 24231da177e4SLinus Torvalds #endif 24247bc570c8SYOSHIFUJI Hideaki if (iif) { 24257bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 24267bc570c8SYOSHIFUJI Hideaki if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) { 24278229efdaSBenjamin Thery int err = ip6mr_get_route(net, skb, rtm, nowait); 24287bc570c8SYOSHIFUJI Hideaki if (err <= 0) { 24297bc570c8SYOSHIFUJI Hideaki if (!nowait) { 24307bc570c8SYOSHIFUJI Hideaki if (err == 0) 24317bc570c8SYOSHIFUJI Hideaki return 0; 24327bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 24337bc570c8SYOSHIFUJI Hideaki } else { 24347bc570c8SYOSHIFUJI Hideaki if (err == -EMSGSIZE) 24357bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 24367bc570c8SYOSHIFUJI Hideaki } 24377bc570c8SYOSHIFUJI Hideaki } 24387bc570c8SYOSHIFUJI Hideaki } else 24397bc570c8SYOSHIFUJI Hideaki #endif 24402d7202bfSThomas Graf NLA_PUT_U32(skb, RTA_IIF, iif); 24417bc570c8SYOSHIFUJI Hideaki } else if (dst) { 24421da177e4SLinus Torvalds struct in6_addr saddr_buf; 2443c3968a85SDaniel Walter if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0) 2444c3968a85SDaniel Walter NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf); 2445c3968a85SDaniel Walter } 2446c3968a85SDaniel Walter 2447c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) { 2448c3968a85SDaniel Walter struct in6_addr saddr_buf; 24494e3fd7a0SAlexey Dobriyan saddr_buf = rt->rt6i_prefsrc.addr; 24502d7202bfSThomas Graf NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf); 24511da177e4SLinus Torvalds } 24522d7202bfSThomas Graf 2453defb3519SDavid S. Miller if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0) 24542d7202bfSThomas Graf goto nla_put_failure; 24552d7202bfSThomas Graf 2456f2c31e32SEric Dumazet rcu_read_lock(); 245727217455SDavid Miller n = dst_get_neighbour_noref(&rt->dst); 2458f2c31e32SEric Dumazet if (n) 2459f2c31e32SEric Dumazet NLA_PUT(skb, RTA_GATEWAY, 16, &n->primary_key); 2460f2c31e32SEric Dumazet rcu_read_unlock(); 24612d7202bfSThomas Graf 2462d8d1f30bSChangli Gao if (rt->dst.dev) 2463*d1918542SDavid S. Miller NLA_PUT_U32(skb, RTA_OIF, rt->dst.dev->ifindex); 24642d7202bfSThomas Graf 24652d7202bfSThomas Graf NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric); 2466e3703b3dSThomas Graf 246736e3deaeSYOSHIFUJI Hideaki if (!(rt->rt6i_flags & RTF_EXPIRES)) 246836e3deaeSYOSHIFUJI Hideaki expires = 0; 2469*d1918542SDavid S. Miller else if (rt->dst.expires - jiffies < INT_MAX) 2470*d1918542SDavid S. Miller expires = rt->dst.expires - jiffies; 247136e3deaeSYOSHIFUJI Hideaki else 247236e3deaeSYOSHIFUJI Hideaki expires = INT_MAX; 247369cdf8f9SYOSHIFUJI Hideaki 2474d8d1f30bSChangli Gao if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0, 2475d8d1f30bSChangli Gao expires, rt->dst.error) < 0) 2476e3703b3dSThomas Graf goto nla_put_failure; 24771da177e4SLinus Torvalds 24782d7202bfSThomas Graf return nlmsg_end(skb, nlh); 24792d7202bfSThomas Graf 24802d7202bfSThomas Graf nla_put_failure: 248126932566SPatrick McHardy nlmsg_cancel(skb, nlh); 248226932566SPatrick McHardy return -EMSGSIZE; 24831da177e4SLinus Torvalds } 24841da177e4SLinus Torvalds 24851b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg) 24861da177e4SLinus Torvalds { 24871da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 24881da177e4SLinus Torvalds int prefix; 24891da177e4SLinus Torvalds 24902d7202bfSThomas Graf if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { 24912d7202bfSThomas Graf struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); 24921da177e4SLinus Torvalds prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0; 24931da177e4SLinus Torvalds } else 24941da177e4SLinus Torvalds prefix = 0; 24951da177e4SLinus Torvalds 2496191cd582SBrian Haley return rt6_fill_node(arg->net, 2497191cd582SBrian Haley arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE, 24981da177e4SLinus Torvalds NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq, 24997bc570c8SYOSHIFUJI Hideaki prefix, 0, NLM_F_MULTI); 25001da177e4SLinus Torvalds } 25011da177e4SLinus Torvalds 2502c127ea2cSThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg) 25031da177e4SLinus Torvalds { 25043b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 2505ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 25061da177e4SLinus Torvalds struct rt6_info *rt; 2507ab364a6fSThomas Graf struct sk_buff *skb; 2508ab364a6fSThomas Graf struct rtmsg *rtm; 25094c9483b2SDavid S. Miller struct flowi6 fl6; 2510ab364a6fSThomas Graf int err, iif = 0; 2511ab364a6fSThomas Graf 2512ab364a6fSThomas Graf err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy); 2513ab364a6fSThomas Graf if (err < 0) 2514ab364a6fSThomas Graf goto errout; 2515ab364a6fSThomas Graf 2516ab364a6fSThomas Graf err = -EINVAL; 25174c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 2518ab364a6fSThomas Graf 2519ab364a6fSThomas Graf if (tb[RTA_SRC]) { 2520ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 2521ab364a6fSThomas Graf goto errout; 2522ab364a6fSThomas Graf 25234e3fd7a0SAlexey Dobriyan fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); 2524ab364a6fSThomas Graf } 2525ab364a6fSThomas Graf 2526ab364a6fSThomas Graf if (tb[RTA_DST]) { 2527ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 2528ab364a6fSThomas Graf goto errout; 2529ab364a6fSThomas Graf 25304e3fd7a0SAlexey Dobriyan fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); 2531ab364a6fSThomas Graf } 2532ab364a6fSThomas Graf 2533ab364a6fSThomas Graf if (tb[RTA_IIF]) 2534ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 2535ab364a6fSThomas Graf 2536ab364a6fSThomas Graf if (tb[RTA_OIF]) 25374c9483b2SDavid S. Miller fl6.flowi6_oif = nla_get_u32(tb[RTA_OIF]); 2538ab364a6fSThomas Graf 2539ab364a6fSThomas Graf if (iif) { 2540ab364a6fSThomas Graf struct net_device *dev; 25415578689aSDaniel Lezcano dev = __dev_get_by_index(net, iif); 2542ab364a6fSThomas Graf if (!dev) { 2543ab364a6fSThomas Graf err = -ENODEV; 2544ab364a6fSThomas Graf goto errout; 2545ab364a6fSThomas Graf } 2546ab364a6fSThomas Graf } 25471da177e4SLinus Torvalds 25481da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 254938308473SDavid S. Miller if (!skb) { 2550ab364a6fSThomas Graf err = -ENOBUFS; 2551ab364a6fSThomas Graf goto errout; 2552ab364a6fSThomas Graf } 25531da177e4SLinus Torvalds 25541da177e4SLinus Torvalds /* Reserve room for dummy headers, this skb can pass 25551da177e4SLinus Torvalds through good chunk of routing engine. 25561da177e4SLinus Torvalds */ 2557459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 25581da177e4SLinus Torvalds skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr)); 25591da177e4SLinus Torvalds 25604c9483b2SDavid S. Miller rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl6); 2561d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 25621da177e4SLinus Torvalds 25634c9483b2SDavid S. Miller err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif, 25641da177e4SLinus Torvalds RTM_NEWROUTE, NETLINK_CB(in_skb).pid, 25657bc570c8SYOSHIFUJI Hideaki nlh->nlmsg_seq, 0, 0, 0); 25661da177e4SLinus Torvalds if (err < 0) { 2567ab364a6fSThomas Graf kfree_skb(skb); 2568ab364a6fSThomas Graf goto errout; 25691da177e4SLinus Torvalds } 25701da177e4SLinus Torvalds 25715578689aSDaniel Lezcano err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid); 2572ab364a6fSThomas Graf errout: 25731da177e4SLinus Torvalds return err; 25741da177e4SLinus Torvalds } 25751da177e4SLinus Torvalds 257686872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info) 25771da177e4SLinus Torvalds { 25781da177e4SLinus Torvalds struct sk_buff *skb; 25795578689aSDaniel Lezcano struct net *net = info->nl_net; 2580528c4cebSDenis V. Lunev u32 seq; 2581528c4cebSDenis V. Lunev int err; 25820d51aa80SJamal Hadi Salim 2583528c4cebSDenis V. Lunev err = -ENOBUFS; 258438308473SDavid S. Miller seq = info->nlh ? info->nlh->nlmsg_seq : 0; 258586872cb5SThomas Graf 2586339bf98fSThomas Graf skb = nlmsg_new(rt6_nlmsg_size(), gfp_any()); 258738308473SDavid S. Miller if (!skb) 258821713ebcSThomas Graf goto errout; 25891da177e4SLinus Torvalds 2590191cd582SBrian Haley err = rt6_fill_node(net, skb, rt, NULL, NULL, 0, 25917bc570c8SYOSHIFUJI Hideaki event, info->pid, seq, 0, 0, 0); 259226932566SPatrick McHardy if (err < 0) { 259326932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 259426932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 259526932566SPatrick McHardy kfree_skb(skb); 259626932566SPatrick McHardy goto errout; 259726932566SPatrick McHardy } 25981ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE, 25995578689aSDaniel Lezcano info->nlh, gfp_any()); 26001ce85fe4SPablo Neira Ayuso return; 260121713ebcSThomas Graf errout: 260221713ebcSThomas Graf if (err < 0) 26035578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 26041da177e4SLinus Torvalds } 26051da177e4SLinus Torvalds 26068ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 26078ed67789SDaniel Lezcano unsigned long event, void *data) 26088ed67789SDaniel Lezcano { 26098ed67789SDaniel Lezcano struct net_device *dev = (struct net_device *)data; 2610c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 26118ed67789SDaniel Lezcano 26128ed67789SDaniel Lezcano if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) { 2613d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 26148ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 26158ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 2616d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 26178ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 2618d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 26198ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 26208ed67789SDaniel Lezcano #endif 26218ed67789SDaniel Lezcano } 26228ed67789SDaniel Lezcano 26238ed67789SDaniel Lezcano return NOTIFY_OK; 26248ed67789SDaniel Lezcano } 26258ed67789SDaniel Lezcano 26261da177e4SLinus Torvalds /* 26271da177e4SLinus Torvalds * /proc 26281da177e4SLinus Torvalds */ 26291da177e4SLinus Torvalds 26301da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 26311da177e4SLinus Torvalds 26321da177e4SLinus Torvalds struct rt6_proc_arg 26331da177e4SLinus Torvalds { 26341da177e4SLinus Torvalds char *buffer; 26351da177e4SLinus Torvalds int offset; 26361da177e4SLinus Torvalds int length; 26371da177e4SLinus Torvalds int skip; 26381da177e4SLinus Torvalds int len; 26391da177e4SLinus Torvalds }; 26401da177e4SLinus Torvalds 26411da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg) 26421da177e4SLinus Torvalds { 264333120b30SAlexey Dobriyan struct seq_file *m = p_arg; 264469cce1d1SDavid S. Miller struct neighbour *n; 26451da177e4SLinus Torvalds 26464b7a4274SHarvey Harrison seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen); 26471da177e4SLinus Torvalds 26481da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 26494b7a4274SHarvey Harrison seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen); 26501da177e4SLinus Torvalds #else 265133120b30SAlexey Dobriyan seq_puts(m, "00000000000000000000000000000000 00 "); 26521da177e4SLinus Torvalds #endif 2653f2c31e32SEric Dumazet rcu_read_lock(); 265427217455SDavid Miller n = dst_get_neighbour_noref(&rt->dst); 265569cce1d1SDavid S. Miller if (n) { 265669cce1d1SDavid S. Miller seq_printf(m, "%pi6", n->primary_key); 26571da177e4SLinus Torvalds } else { 265833120b30SAlexey Dobriyan seq_puts(m, "00000000000000000000000000000000"); 26591da177e4SLinus Torvalds } 2660f2c31e32SEric Dumazet rcu_read_unlock(); 266133120b30SAlexey Dobriyan seq_printf(m, " %08x %08x %08x %08x %8s\n", 2662d8d1f30bSChangli Gao rt->rt6i_metric, atomic_read(&rt->dst.__refcnt), 2663d8d1f30bSChangli Gao rt->dst.__use, rt->rt6i_flags, 2664*d1918542SDavid S. Miller rt->dst.dev ? rt->dst.dev->name : ""); 26651da177e4SLinus Torvalds return 0; 26661da177e4SLinus Torvalds } 26671da177e4SLinus Torvalds 266833120b30SAlexey Dobriyan static int ipv6_route_show(struct seq_file *m, void *v) 26691da177e4SLinus Torvalds { 2670f3db4851SDaniel Lezcano struct net *net = (struct net *)m->private; 2671f3db4851SDaniel Lezcano fib6_clean_all(net, rt6_info_route, 0, m); 267233120b30SAlexey Dobriyan return 0; 26731da177e4SLinus Torvalds } 26741da177e4SLinus Torvalds 267533120b30SAlexey Dobriyan static int ipv6_route_open(struct inode *inode, struct file *file) 267633120b30SAlexey Dobriyan { 2677de05c557SPavel Emelyanov return single_open_net(inode, file, ipv6_route_show); 2678f3db4851SDaniel Lezcano } 2679f3db4851SDaniel Lezcano 268033120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = { 268133120b30SAlexey Dobriyan .owner = THIS_MODULE, 268233120b30SAlexey Dobriyan .open = ipv6_route_open, 268333120b30SAlexey Dobriyan .read = seq_read, 268433120b30SAlexey Dobriyan .llseek = seq_lseek, 2685b6fcbdb4SPavel Emelyanov .release = single_release_net, 268633120b30SAlexey Dobriyan }; 268733120b30SAlexey Dobriyan 26881da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 26891da177e4SLinus Torvalds { 269069ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 26911da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 269269ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 269369ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 269469ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_alloc, 269569ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 269669ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 2697fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 269869ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 26991da177e4SLinus Torvalds 27001da177e4SLinus Torvalds return 0; 27011da177e4SLinus Torvalds } 27021da177e4SLinus Torvalds 27031da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file) 27041da177e4SLinus Torvalds { 2705de05c557SPavel Emelyanov return single_open_net(inode, file, rt6_stats_seq_show); 270669ddb805SDaniel Lezcano } 270769ddb805SDaniel Lezcano 27089a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = { 27091da177e4SLinus Torvalds .owner = THIS_MODULE, 27101da177e4SLinus Torvalds .open = rt6_stats_seq_open, 27111da177e4SLinus Torvalds .read = seq_read, 27121da177e4SLinus Torvalds .llseek = seq_lseek, 2713b6fcbdb4SPavel Emelyanov .release = single_release_net, 27141da177e4SLinus Torvalds }; 27151da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 27161da177e4SLinus Torvalds 27171da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 27181da177e4SLinus Torvalds 27191da177e4SLinus Torvalds static 27208d65af78SAlexey Dobriyan int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write, 27211da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 27221da177e4SLinus Torvalds { 2723c486da34SLucian Adrian Grijincu struct net *net; 2724c486da34SLucian Adrian Grijincu int delay; 2725c486da34SLucian Adrian Grijincu if (!write) 2726c486da34SLucian Adrian Grijincu return -EINVAL; 2727c486da34SLucian Adrian Grijincu 2728c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 2729c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 27308d65af78SAlexey Dobriyan proc_dointvec(ctl, write, buffer, lenp, ppos); 27315b7c931dSDaniel Lezcano fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net); 27321da177e4SLinus Torvalds return 0; 27331da177e4SLinus Torvalds } 27341da177e4SLinus Torvalds 2735760f2d01SDaniel Lezcano ctl_table ipv6_route_table_template[] = { 27361da177e4SLinus Torvalds { 27371da177e4SLinus Torvalds .procname = "flush", 27384990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 27391da177e4SLinus Torvalds .maxlen = sizeof(int), 274089c8b3a1SDave Jones .mode = 0200, 27416d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 27421da177e4SLinus Torvalds }, 27431da177e4SLinus Torvalds { 27441da177e4SLinus Torvalds .procname = "gc_thresh", 27459a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 27461da177e4SLinus Torvalds .maxlen = sizeof(int), 27471da177e4SLinus Torvalds .mode = 0644, 27486d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 27491da177e4SLinus Torvalds }, 27501da177e4SLinus Torvalds { 27511da177e4SLinus Torvalds .procname = "max_size", 27524990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 27531da177e4SLinus Torvalds .maxlen = sizeof(int), 27541da177e4SLinus Torvalds .mode = 0644, 27556d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 27561da177e4SLinus Torvalds }, 27571da177e4SLinus Torvalds { 27581da177e4SLinus Torvalds .procname = "gc_min_interval", 27594990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 27601da177e4SLinus Torvalds .maxlen = sizeof(int), 27611da177e4SLinus Torvalds .mode = 0644, 27626d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 27631da177e4SLinus Torvalds }, 27641da177e4SLinus Torvalds { 27651da177e4SLinus Torvalds .procname = "gc_timeout", 27664990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 27671da177e4SLinus Torvalds .maxlen = sizeof(int), 27681da177e4SLinus Torvalds .mode = 0644, 27696d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 27701da177e4SLinus Torvalds }, 27711da177e4SLinus Torvalds { 27721da177e4SLinus Torvalds .procname = "gc_interval", 27734990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 27741da177e4SLinus Torvalds .maxlen = sizeof(int), 27751da177e4SLinus Torvalds .mode = 0644, 27766d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 27771da177e4SLinus Torvalds }, 27781da177e4SLinus Torvalds { 27791da177e4SLinus Torvalds .procname = "gc_elasticity", 27804990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 27811da177e4SLinus Torvalds .maxlen = sizeof(int), 27821da177e4SLinus Torvalds .mode = 0644, 2783f3d3f616SMin Zhang .proc_handler = proc_dointvec, 27841da177e4SLinus Torvalds }, 27851da177e4SLinus Torvalds { 27861da177e4SLinus Torvalds .procname = "mtu_expires", 27874990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 27881da177e4SLinus Torvalds .maxlen = sizeof(int), 27891da177e4SLinus Torvalds .mode = 0644, 27906d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 27911da177e4SLinus Torvalds }, 27921da177e4SLinus Torvalds { 27931da177e4SLinus Torvalds .procname = "min_adv_mss", 27944990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 27951da177e4SLinus Torvalds .maxlen = sizeof(int), 27961da177e4SLinus Torvalds .mode = 0644, 2797f3d3f616SMin Zhang .proc_handler = proc_dointvec, 27981da177e4SLinus Torvalds }, 27991da177e4SLinus Torvalds { 28001da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 28014990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 28021da177e4SLinus Torvalds .maxlen = sizeof(int), 28031da177e4SLinus Torvalds .mode = 0644, 28046d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 28051da177e4SLinus Torvalds }, 2806f8572d8fSEric W. Biederman { } 28071da177e4SLinus Torvalds }; 28081da177e4SLinus Torvalds 28092c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 2810760f2d01SDaniel Lezcano { 2811760f2d01SDaniel Lezcano struct ctl_table *table; 2812760f2d01SDaniel Lezcano 2813760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 2814760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 2815760f2d01SDaniel Lezcano GFP_KERNEL); 28165ee09105SYOSHIFUJI Hideaki 28175ee09105SYOSHIFUJI Hideaki if (table) { 28185ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 2819c486da34SLucian Adrian Grijincu table[0].extra1 = net; 282086393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 28215ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 28225ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 28235ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 28245ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 28255ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 28265ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 28275ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 28289c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 28295ee09105SYOSHIFUJI Hideaki } 28305ee09105SYOSHIFUJI Hideaki 2831760f2d01SDaniel Lezcano return table; 2832760f2d01SDaniel Lezcano } 28331da177e4SLinus Torvalds #endif 28341da177e4SLinus Torvalds 28352c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 2836cdb18761SDaniel Lezcano { 2837633d424bSPavel Emelyanov int ret = -ENOMEM; 28388ed67789SDaniel Lezcano 283986393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 284086393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 2841f2fc6a54SBenjamin Thery 2842fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 2843fc66f95cSEric Dumazet goto out_ip6_dst_ops; 2844fc66f95cSEric Dumazet 28458ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 28468ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 28478ed67789SDaniel Lezcano GFP_KERNEL); 28488ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 2849fc66f95cSEric Dumazet goto out_ip6_dst_entries; 2850d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.path = 28518ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_null_entry; 2852d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 285362fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 285462fa8a84SDavid S. Miller ip6_template_metrics, true); 28558ed67789SDaniel Lezcano 28568ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 28578ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 28588ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 28598ed67789SDaniel Lezcano GFP_KERNEL); 286068fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 286168fffc67SPeter Zijlstra goto out_ip6_null_entry; 2862d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.path = 28638ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_prohibit_entry; 2864d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 286562fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 286662fa8a84SDavid S. Miller ip6_template_metrics, true); 28678ed67789SDaniel Lezcano 28688ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 28698ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 28708ed67789SDaniel Lezcano GFP_KERNEL); 287168fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 287268fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 2873d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.path = 28748ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_blk_hole_entry; 2875d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 287662fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 287762fa8a84SDavid S. Miller ip6_template_metrics, true); 28788ed67789SDaniel Lezcano #endif 28798ed67789SDaniel Lezcano 2880b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 2881b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 2882b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 2883b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 2884b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 2885b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 2886b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 2887b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 2888b339a47cSPeter Zijlstra 2889cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS 2890cdb18761SDaniel Lezcano proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops); 2891cdb18761SDaniel Lezcano proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops); 2892cdb18761SDaniel Lezcano #endif 28936891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 28946891a346SBenjamin Thery 28958ed67789SDaniel Lezcano ret = 0; 28968ed67789SDaniel Lezcano out: 28978ed67789SDaniel Lezcano return ret; 2898f2fc6a54SBenjamin Thery 289968fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 290068fffc67SPeter Zijlstra out_ip6_prohibit_entry: 290168fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 290268fffc67SPeter Zijlstra out_ip6_null_entry: 290368fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 290468fffc67SPeter Zijlstra #endif 2905fc66f95cSEric Dumazet out_ip6_dst_entries: 2906fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 2907f2fc6a54SBenjamin Thery out_ip6_dst_ops: 2908f2fc6a54SBenjamin Thery goto out; 2909cdb18761SDaniel Lezcano } 2910cdb18761SDaniel Lezcano 29112c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 2912cdb18761SDaniel Lezcano { 2913cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS 2914cdb18761SDaniel Lezcano proc_net_remove(net, "ipv6_route"); 2915cdb18761SDaniel Lezcano proc_net_remove(net, "rt6_stats"); 2916cdb18761SDaniel Lezcano #endif 29178ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 29188ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 29198ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 29208ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 29218ed67789SDaniel Lezcano #endif 292241bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 2923cdb18761SDaniel Lezcano } 2924cdb18761SDaniel Lezcano 2925cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 2926cdb18761SDaniel Lezcano .init = ip6_route_net_init, 2927cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 2928cdb18761SDaniel Lezcano }; 2929cdb18761SDaniel Lezcano 29308ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 29318ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 29328ed67789SDaniel Lezcano .priority = 0, 29338ed67789SDaniel Lezcano }; 29348ed67789SDaniel Lezcano 2935433d49c3SDaniel Lezcano int __init ip6_route_init(void) 29361da177e4SLinus Torvalds { 2937433d49c3SDaniel Lezcano int ret; 2938433d49c3SDaniel Lezcano 29399a7ec3a9SDaniel Lezcano ret = -ENOMEM; 29409a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 29419a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 29429a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 29439a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 2944c19a28e1SFernando Carrijo goto out; 294514e50e57SDavid S. Miller 2946fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 29478ed67789SDaniel Lezcano if (ret) 2948bdb3289fSDaniel Lezcano goto out_kmem_cache; 2949bdb3289fSDaniel Lezcano 2950fc66f95cSEric Dumazet ret = register_pernet_subsys(&ip6_route_net_ops); 2951fc66f95cSEric Dumazet if (ret) 2952fc66f95cSEric Dumazet goto out_dst_entries; 2953fc66f95cSEric Dumazet 29545dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 29555dc121e9SArnaud Ebalard 29568ed67789SDaniel Lezcano /* Registering of the loopback is done before this portion of code, 29578ed67789SDaniel Lezcano * the loopback reference in rt6_info will not be taken, do it 29588ed67789SDaniel Lezcano * manually for init_net */ 2959d8d1f30bSChangli Gao init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 29608ed67789SDaniel Lezcano init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 2961bdb3289fSDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 2962d8d1f30bSChangli Gao init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 29638ed67789SDaniel Lezcano init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 2964d8d1f30bSChangli Gao init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 29658ed67789SDaniel Lezcano init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 2966bdb3289fSDaniel Lezcano #endif 2967433d49c3SDaniel Lezcano ret = fib6_init(); 2968433d49c3SDaniel Lezcano if (ret) 29698ed67789SDaniel Lezcano goto out_register_subsys; 2970433d49c3SDaniel Lezcano 2971433d49c3SDaniel Lezcano ret = xfrm6_init(); 2972433d49c3SDaniel Lezcano if (ret) 2973cdb18761SDaniel Lezcano goto out_fib6_init; 2974c35b7e72SDaniel Lezcano 2975433d49c3SDaniel Lezcano ret = fib6_rules_init(); 2976433d49c3SDaniel Lezcano if (ret) 2977433d49c3SDaniel Lezcano goto xfrm6_init; 29787e5449c2SDaniel Lezcano 2979433d49c3SDaniel Lezcano ret = -ENOBUFS; 2980c7ac8679SGreg Rose if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) || 2981c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) || 2982c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL)) 2983433d49c3SDaniel Lezcano goto fib6_rules_init; 2984433d49c3SDaniel Lezcano 29858ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 2986cdb18761SDaniel Lezcano if (ret) 2987cdb18761SDaniel Lezcano goto fib6_rules_init; 29888ed67789SDaniel Lezcano 2989433d49c3SDaniel Lezcano out: 2990433d49c3SDaniel Lezcano return ret; 2991433d49c3SDaniel Lezcano 2992433d49c3SDaniel Lezcano fib6_rules_init: 2993433d49c3SDaniel Lezcano fib6_rules_cleanup(); 2994433d49c3SDaniel Lezcano xfrm6_init: 2995433d49c3SDaniel Lezcano xfrm6_fini(); 2996433d49c3SDaniel Lezcano out_fib6_init: 2997433d49c3SDaniel Lezcano fib6_gc_cleanup(); 29988ed67789SDaniel Lezcano out_register_subsys: 29998ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 3000fc66f95cSEric Dumazet out_dst_entries: 3001fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 3002433d49c3SDaniel Lezcano out_kmem_cache: 3003f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 3004433d49c3SDaniel Lezcano goto out; 30051da177e4SLinus Torvalds } 30061da177e4SLinus Torvalds 30071da177e4SLinus Torvalds void ip6_route_cleanup(void) 30081da177e4SLinus Torvalds { 30098ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 3010101367c2SThomas Graf fib6_rules_cleanup(); 30111da177e4SLinus Torvalds xfrm6_fini(); 30121da177e4SLinus Torvalds fib6_gc_cleanup(); 30138ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 301441bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 3015f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 30161da177e4SLinus Torvalds } 3017