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> 291da177e4SLinus Torvalds #include <linux/types.h> 301da177e4SLinus Torvalds #include <linux/times.h> 311da177e4SLinus Torvalds #include <linux/socket.h> 321da177e4SLinus Torvalds #include <linux/sockios.h> 331da177e4SLinus Torvalds #include <linux/net.h> 341da177e4SLinus Torvalds #include <linux/route.h> 351da177e4SLinus Torvalds #include <linux/netdevice.h> 361da177e4SLinus Torvalds #include <linux/in6.h> 377bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h> 381da177e4SLinus Torvalds #include <linux/init.h> 391da177e4SLinus Torvalds #include <linux/if_arp.h> 401da177e4SLinus Torvalds #include <linux/proc_fs.h> 411da177e4SLinus Torvalds #include <linux/seq_file.h> 425b7c931dSDaniel Lezcano #include <linux/nsproxy.h> 435a0e3ad6STejun Heo #include <linux/slab.h> 44457c4cbcSEric W. Biederman #include <net/net_namespace.h> 451da177e4SLinus Torvalds #include <net/snmp.h> 461da177e4SLinus Torvalds #include <net/ipv6.h> 471da177e4SLinus Torvalds #include <net/ip6_fib.h> 481da177e4SLinus Torvalds #include <net/ip6_route.h> 491da177e4SLinus Torvalds #include <net/ndisc.h> 501da177e4SLinus Torvalds #include <net/addrconf.h> 511da177e4SLinus Torvalds #include <net/tcp.h> 521da177e4SLinus Torvalds #include <linux/rtnetlink.h> 531da177e4SLinus Torvalds #include <net/dst.h> 541da177e4SLinus Torvalds #include <net/xfrm.h> 558d71740cSTom Tucker #include <net/netevent.h> 5621713ebcSThomas Graf #include <net/netlink.h> 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds #include <asm/uaccess.h> 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 611da177e4SLinus Torvalds #include <linux/sysctl.h> 621da177e4SLinus Torvalds #endif 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds /* Set to 3 to get tracing. */ 651da177e4SLinus Torvalds #define RT6_DEBUG 2 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds #if RT6_DEBUG >= 3 681da177e4SLinus Torvalds #define RDBG(x) printk x 691da177e4SLinus Torvalds #define RT6_TRACE(x...) printk(KERN_DEBUG x) 701da177e4SLinus Torvalds #else 711da177e4SLinus Torvalds #define RDBG(x) 721da177e4SLinus Torvalds #define RT6_TRACE(x...) do { ; } while (0) 731da177e4SLinus Torvalds #endif 741da177e4SLinus Torvalds 7521efcfa0SEric Dumazet static struct rt6_info *ip6_rt_copy(const struct rt6_info *ort, 7621efcfa0SEric Dumazet const struct in6_addr *dest); 771da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie); 780dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst); 79d33e4553SDavid S. Miller static unsigned int ip6_default_mtu(const struct dst_entry *dst); 801da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *); 811da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *); 821da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *, 831da177e4SLinus Torvalds struct net_device *dev, int how); 84569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops); 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds static int ip6_pkt_discard(struct sk_buff *skb); 871da177e4SLinus Torvalds static int ip6_pkt_discard_out(struct sk_buff *skb); 881da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb); 891da177e4SLinus Torvalds static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu); 901da177e4SLinus Torvalds 9170ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 92efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 93b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 94b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex, 9570ceb4f5SYOSHIFUJI Hideaki unsigned pref); 96efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 97b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 98b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex); 9970ceb4f5SYOSHIFUJI Hideaki #endif 10070ceb4f5SYOSHIFUJI Hideaki 10106582540SDavid S. Miller static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old) 10206582540SDavid S. Miller { 10306582540SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 10406582540SDavid S. Miller struct inet_peer *peer; 10506582540SDavid S. Miller u32 *p = NULL; 10606582540SDavid S. Miller 107*8e2ec639SYan, Zheng if (!(rt->dst.flags & DST_HOST)) 108*8e2ec639SYan, Zheng return NULL; 109*8e2ec639SYan, Zheng 11006582540SDavid S. Miller if (!rt->rt6i_peer) 11106582540SDavid S. Miller rt6_bind_peer(rt, 1); 11206582540SDavid S. Miller 11306582540SDavid S. Miller peer = rt->rt6i_peer; 11406582540SDavid S. Miller if (peer) { 11506582540SDavid S. Miller u32 *old_p = __DST_METRICS_PTR(old); 11606582540SDavid S. Miller unsigned long prev, new; 11706582540SDavid S. Miller 11806582540SDavid S. Miller p = peer->metrics; 11906582540SDavid S. Miller if (inet_metrics_new(peer)) 12006582540SDavid S. Miller memcpy(p, old_p, sizeof(u32) * RTAX_MAX); 12106582540SDavid S. Miller 12206582540SDavid S. Miller new = (unsigned long) p; 12306582540SDavid S. Miller prev = cmpxchg(&dst->_metrics, old, new); 12406582540SDavid S. Miller 12506582540SDavid S. Miller if (prev != old) { 12606582540SDavid S. Miller p = __DST_METRICS_PTR(prev); 12706582540SDavid S. Miller if (prev & DST_METRICS_READ_ONLY) 12806582540SDavid S. Miller p = NULL; 12906582540SDavid S. Miller } 13006582540SDavid S. Miller } 13106582540SDavid S. Miller return p; 13206582540SDavid S. Miller } 13306582540SDavid S. Miller 134d3aaeb38SDavid S. Miller static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst, const void *daddr) 135d3aaeb38SDavid S. Miller { 136d3aaeb38SDavid S. Miller return __neigh_lookup_errno(&nd_tbl, daddr, dst->dev); 137d3aaeb38SDavid S. Miller } 138d3aaeb38SDavid S. Miller 1399a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = { 1401da177e4SLinus Torvalds .family = AF_INET6, 14109640e63SHarvey Harrison .protocol = cpu_to_be16(ETH_P_IPV6), 1421da177e4SLinus Torvalds .gc = ip6_dst_gc, 1431da177e4SLinus Torvalds .gc_thresh = 1024, 1441da177e4SLinus Torvalds .check = ip6_dst_check, 1450dbaee3bSDavid S. Miller .default_advmss = ip6_default_advmss, 146d33e4553SDavid S. Miller .default_mtu = ip6_default_mtu, 14706582540SDavid S. Miller .cow_metrics = ipv6_cow_metrics, 1481da177e4SLinus Torvalds .destroy = ip6_dst_destroy, 1491da177e4SLinus Torvalds .ifdown = ip6_dst_ifdown, 1501da177e4SLinus Torvalds .negative_advice = ip6_negative_advice, 1511da177e4SLinus Torvalds .link_failure = ip6_link_failure, 1521da177e4SLinus Torvalds .update_pmtu = ip6_rt_update_pmtu, 1531ac06e03SHerbert Xu .local_out = __ip6_local_out, 154d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 1551da177e4SLinus Torvalds }; 1561da177e4SLinus Torvalds 157ec831ea7SRoland Dreier static unsigned int ip6_blackhole_default_mtu(const struct dst_entry *dst) 158ec831ea7SRoland Dreier { 159ec831ea7SRoland Dreier return 0; 160ec831ea7SRoland Dreier } 161ec831ea7SRoland Dreier 16214e50e57SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu) 16314e50e57SDavid S. Miller { 16414e50e57SDavid S. Miller } 16514e50e57SDavid S. Miller 1660972ddb2SHeld Bernhard static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst, 1670972ddb2SHeld Bernhard unsigned long old) 1680972ddb2SHeld Bernhard { 1690972ddb2SHeld Bernhard return NULL; 1700972ddb2SHeld Bernhard } 1710972ddb2SHeld Bernhard 17214e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = { 17314e50e57SDavid S. Miller .family = AF_INET6, 17409640e63SHarvey Harrison .protocol = cpu_to_be16(ETH_P_IPV6), 17514e50e57SDavid S. Miller .destroy = ip6_dst_destroy, 17614e50e57SDavid S. Miller .check = ip6_dst_check, 177ec831ea7SRoland Dreier .default_mtu = ip6_blackhole_default_mtu, 178214f45c9SEric Dumazet .default_advmss = ip6_default_advmss, 17914e50e57SDavid S. Miller .update_pmtu = ip6_rt_blackhole_update_pmtu, 1800972ddb2SHeld Bernhard .cow_metrics = ip6_rt_blackhole_cow_metrics, 181d3aaeb38SDavid S. Miller .neigh_lookup = ip6_neigh_lookup, 18214e50e57SDavid S. Miller }; 18314e50e57SDavid S. Miller 18462fa8a84SDavid S. Miller static const u32 ip6_template_metrics[RTAX_MAX] = { 18562fa8a84SDavid S. Miller [RTAX_HOPLIMIT - 1] = 255, 18662fa8a84SDavid S. Miller }; 18762fa8a84SDavid S. Miller 188bdb3289fSDaniel Lezcano static struct rt6_info ip6_null_entry_template = { 1891da177e4SLinus Torvalds .dst = { 1901da177e4SLinus Torvalds .__refcnt = ATOMIC_INIT(1), 1911da177e4SLinus Torvalds .__use = 1, 1921da177e4SLinus Torvalds .obsolete = -1, 1931da177e4SLinus Torvalds .error = -ENETUNREACH, 1941da177e4SLinus Torvalds .input = ip6_pkt_discard, 1951da177e4SLinus Torvalds .output = ip6_pkt_discard_out, 1961da177e4SLinus Torvalds }, 1971da177e4SLinus Torvalds .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 1984f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 1991da177e4SLinus Torvalds .rt6i_metric = ~(u32) 0, 2001da177e4SLinus Torvalds .rt6i_ref = ATOMIC_INIT(1), 2011da177e4SLinus Torvalds }; 2021da177e4SLinus Torvalds 203101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES 204101367c2SThomas Graf 2056723ab54SDavid S. Miller static int ip6_pkt_prohibit(struct sk_buff *skb); 2066723ab54SDavid S. Miller static int ip6_pkt_prohibit_out(struct sk_buff *skb); 2076723ab54SDavid S. Miller 208280a34c8SAdrian Bunk static struct rt6_info ip6_prohibit_entry_template = { 209101367c2SThomas Graf .dst = { 210101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 211101367c2SThomas Graf .__use = 1, 212101367c2SThomas Graf .obsolete = -1, 213101367c2SThomas Graf .error = -EACCES, 2149ce8ade0SThomas Graf .input = ip6_pkt_prohibit, 2159ce8ade0SThomas Graf .output = ip6_pkt_prohibit_out, 216101367c2SThomas Graf }, 217101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 2184f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 219101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 220101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 221101367c2SThomas Graf }; 222101367c2SThomas Graf 223bdb3289fSDaniel Lezcano static struct rt6_info ip6_blk_hole_entry_template = { 224101367c2SThomas Graf .dst = { 225101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 226101367c2SThomas Graf .__use = 1, 227101367c2SThomas Graf .obsolete = -1, 228101367c2SThomas Graf .error = -EINVAL, 229352e512cSHerbert Xu .input = dst_discard, 230352e512cSHerbert Xu .output = dst_discard, 231101367c2SThomas Graf }, 232101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 2334f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 234101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 235101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 236101367c2SThomas Graf }; 237101367c2SThomas Graf 238101367c2SThomas Graf #endif 239101367c2SThomas Graf 2401da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */ 2415c1e6aa3SDavid S. Miller static inline struct rt6_info *ip6_dst_alloc(struct dst_ops *ops, 242957c665fSDavid S. Miller struct net_device *dev, 243957c665fSDavid S. Miller int flags) 2441da177e4SLinus Torvalds { 245957c665fSDavid S. Miller struct rt6_info *rt = dst_alloc(ops, dev, 0, 0, flags); 246cf911662SDavid S. Miller 247cf911662SDavid S. Miller memset(&rt->rt6i_table, 0, sizeof(*rt) - sizeof(struct dst_entry)); 248cf911662SDavid S. Miller 249cf911662SDavid S. Miller return rt; 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst) 2531da177e4SLinus Torvalds { 2541da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 2551da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 256b3419363SDavid S. Miller struct inet_peer *peer = rt->rt6i_peer; 2571da177e4SLinus Torvalds 258*8e2ec639SYan, Zheng if (!(rt->dst.flags & DST_HOST)) 259*8e2ec639SYan, Zheng dst_destroy_metrics_generic(dst); 260*8e2ec639SYan, Zheng 2611da177e4SLinus Torvalds if (idev != NULL) { 2621da177e4SLinus Torvalds rt->rt6i_idev = NULL; 2631da177e4SLinus Torvalds in6_dev_put(idev); 2641da177e4SLinus Torvalds } 265b3419363SDavid S. Miller if (peer) { 266b3419363SDavid S. Miller rt->rt6i_peer = NULL; 267b3419363SDavid S. Miller inet_putpeer(peer); 268b3419363SDavid S. Miller } 269b3419363SDavid S. Miller } 270b3419363SDavid S. Miller 2716431cbc2SDavid S. Miller static atomic_t __rt6_peer_genid = ATOMIC_INIT(0); 2726431cbc2SDavid S. Miller 2736431cbc2SDavid S. Miller static u32 rt6_peer_genid(void) 2746431cbc2SDavid S. Miller { 2756431cbc2SDavid S. Miller return atomic_read(&__rt6_peer_genid); 2766431cbc2SDavid S. Miller } 2776431cbc2SDavid S. Miller 278b3419363SDavid S. Miller void rt6_bind_peer(struct rt6_info *rt, int create) 279b3419363SDavid S. Miller { 280b3419363SDavid S. Miller struct inet_peer *peer; 281b3419363SDavid S. Miller 282b3419363SDavid S. Miller peer = inet_getpeer_v6(&rt->rt6i_dst.addr, create); 283b3419363SDavid S. Miller if (peer && cmpxchg(&rt->rt6i_peer, NULL, peer) != NULL) 284b3419363SDavid S. Miller inet_putpeer(peer); 2856431cbc2SDavid S. Miller else 2866431cbc2SDavid S. Miller rt->rt6i_peer_genid = rt6_peer_genid(); 2871da177e4SLinus Torvalds } 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 2901da177e4SLinus Torvalds int how) 2911da177e4SLinus Torvalds { 2921da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 2931da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 2945a3e55d6SDenis V. Lunev struct net_device *loopback_dev = 295c346dca1SYOSHIFUJI Hideaki dev_net(dev)->loopback_dev; 2961da177e4SLinus Torvalds 2975a3e55d6SDenis V. Lunev if (dev != loopback_dev && idev != NULL && idev->dev == dev) { 2985a3e55d6SDenis V. Lunev struct inet6_dev *loopback_idev = 2995a3e55d6SDenis V. Lunev in6_dev_get(loopback_dev); 3001da177e4SLinus Torvalds if (loopback_idev != NULL) { 3011da177e4SLinus Torvalds rt->rt6i_idev = loopback_idev; 3021da177e4SLinus Torvalds in6_dev_put(idev); 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds } 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds 3071da177e4SLinus Torvalds static __inline__ int rt6_check_expired(const struct rt6_info *rt) 3081da177e4SLinus Torvalds { 309a02cec21SEric Dumazet return (rt->rt6i_flags & RTF_EXPIRES) && 310a02cec21SEric Dumazet time_after(jiffies, rt->rt6i_expires); 3111da177e4SLinus Torvalds } 3121da177e4SLinus Torvalds 313b71d1d42SEric Dumazet static inline int rt6_need_strict(const struct in6_addr *daddr) 314c71099acSThomas Graf { 315a02cec21SEric Dumazet return ipv6_addr_type(daddr) & 316a02cec21SEric Dumazet (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK); 317c71099acSThomas Graf } 318c71099acSThomas Graf 3191da177e4SLinus Torvalds /* 320c71099acSThomas Graf * Route lookup. Any table->tb6_lock is implied. 3211da177e4SLinus Torvalds */ 3221da177e4SLinus Torvalds 3238ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net, 3248ed67789SDaniel Lezcano struct rt6_info *rt, 325b71d1d42SEric Dumazet const struct in6_addr *saddr, 3261da177e4SLinus Torvalds int oif, 327d420895eSYOSHIFUJI Hideaki int flags) 3281da177e4SLinus Torvalds { 3291da177e4SLinus Torvalds struct rt6_info *local = NULL; 3301da177e4SLinus Torvalds struct rt6_info *sprt; 3311da177e4SLinus Torvalds 332dd3abc4eSYOSHIFUJI Hideaki if (!oif && ipv6_addr_any(saddr)) 333dd3abc4eSYOSHIFUJI Hideaki goto out; 334dd3abc4eSYOSHIFUJI Hideaki 335d8d1f30bSChangli Gao for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) { 3361da177e4SLinus Torvalds struct net_device *dev = sprt->rt6i_dev; 337dd3abc4eSYOSHIFUJI Hideaki 338dd3abc4eSYOSHIFUJI Hideaki if (oif) { 3391da177e4SLinus Torvalds if (dev->ifindex == oif) 3401da177e4SLinus Torvalds return sprt; 3411da177e4SLinus Torvalds if (dev->flags & IFF_LOOPBACK) { 3421da177e4SLinus Torvalds if (sprt->rt6i_idev == NULL || 3431da177e4SLinus Torvalds sprt->rt6i_idev->dev->ifindex != oif) { 344d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE && oif) 3451da177e4SLinus Torvalds continue; 3461da177e4SLinus Torvalds if (local && (!oif || 3471da177e4SLinus Torvalds local->rt6i_idev->dev->ifindex == oif)) 3481da177e4SLinus Torvalds continue; 3491da177e4SLinus Torvalds } 3501da177e4SLinus Torvalds local = sprt; 3511da177e4SLinus Torvalds } 352dd3abc4eSYOSHIFUJI Hideaki } else { 353dd3abc4eSYOSHIFUJI Hideaki if (ipv6_chk_addr(net, saddr, dev, 354dd3abc4eSYOSHIFUJI Hideaki flags & RT6_LOOKUP_F_IFACE)) 355dd3abc4eSYOSHIFUJI Hideaki return sprt; 356dd3abc4eSYOSHIFUJI Hideaki } 3571da177e4SLinus Torvalds } 3581da177e4SLinus Torvalds 359dd3abc4eSYOSHIFUJI Hideaki if (oif) { 3601da177e4SLinus Torvalds if (local) 3611da177e4SLinus Torvalds return local; 3621da177e4SLinus Torvalds 363d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE) 3648ed67789SDaniel Lezcano return net->ipv6.ip6_null_entry; 3651da177e4SLinus Torvalds } 366dd3abc4eSYOSHIFUJI Hideaki out: 3671da177e4SLinus Torvalds return rt; 3681da177e4SLinus Torvalds } 3691da177e4SLinus Torvalds 37027097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 37127097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt) 37227097255SYOSHIFUJI Hideaki { 373f2c31e32SEric Dumazet struct neighbour *neigh; 37427097255SYOSHIFUJI Hideaki /* 37527097255SYOSHIFUJI Hideaki * Okay, this does not seem to be appropriate 37627097255SYOSHIFUJI Hideaki * for now, however, we need to check if it 37727097255SYOSHIFUJI Hideaki * is really so; aka Router Reachability Probing. 37827097255SYOSHIFUJI Hideaki * 37927097255SYOSHIFUJI Hideaki * Router Reachability Probe MUST be rate-limited 38027097255SYOSHIFUJI Hideaki * to no more than one per minute. 38127097255SYOSHIFUJI Hideaki */ 382f2c31e32SEric Dumazet rcu_read_lock(); 383f2c31e32SEric Dumazet neigh = rt ? dst_get_neighbour(&rt->dst) : NULL; 38427097255SYOSHIFUJI Hideaki if (!neigh || (neigh->nud_state & NUD_VALID)) 385f2c31e32SEric Dumazet goto out; 38627097255SYOSHIFUJI Hideaki read_lock_bh(&neigh->lock); 38727097255SYOSHIFUJI Hideaki if (!(neigh->nud_state & NUD_VALID) && 38852e16356SYOSHIFUJI Hideaki time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) { 38927097255SYOSHIFUJI Hideaki struct in6_addr mcaddr; 39027097255SYOSHIFUJI Hideaki struct in6_addr *target; 39127097255SYOSHIFUJI Hideaki 39227097255SYOSHIFUJI Hideaki neigh->updated = jiffies; 39327097255SYOSHIFUJI Hideaki read_unlock_bh(&neigh->lock); 39427097255SYOSHIFUJI Hideaki 39527097255SYOSHIFUJI Hideaki target = (struct in6_addr *)&neigh->primary_key; 39627097255SYOSHIFUJI Hideaki addrconf_addr_solict_mult(target, &mcaddr); 39727097255SYOSHIFUJI Hideaki ndisc_send_ns(rt->rt6i_dev, NULL, target, &mcaddr, NULL); 398f2c31e32SEric Dumazet } else { 39927097255SYOSHIFUJI Hideaki read_unlock_bh(&neigh->lock); 40027097255SYOSHIFUJI Hideaki } 401f2c31e32SEric Dumazet out: 402f2c31e32SEric Dumazet rcu_read_unlock(); 403f2c31e32SEric Dumazet } 40427097255SYOSHIFUJI Hideaki #else 40527097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt) 40627097255SYOSHIFUJI Hideaki { 40727097255SYOSHIFUJI Hideaki } 40827097255SYOSHIFUJI Hideaki #endif 40927097255SYOSHIFUJI Hideaki 4101da177e4SLinus Torvalds /* 411554cfb7eSYOSHIFUJI Hideaki * Default Router Selection (RFC 2461 6.3.6) 4121da177e4SLinus Torvalds */ 413b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif) 4141da177e4SLinus Torvalds { 415554cfb7eSYOSHIFUJI Hideaki struct net_device *dev = rt->rt6i_dev; 416161980f4SDavid S. Miller if (!oif || dev->ifindex == oif) 417554cfb7eSYOSHIFUJI Hideaki return 2; 418161980f4SDavid S. Miller if ((dev->flags & IFF_LOOPBACK) && 419161980f4SDavid S. Miller rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif) 420161980f4SDavid S. Miller return 1; 421554cfb7eSYOSHIFUJI Hideaki return 0; 4221da177e4SLinus Torvalds } 4231da177e4SLinus Torvalds 424b6f99a21SDave Jones static inline int rt6_check_neigh(struct rt6_info *rt) 4251da177e4SLinus Torvalds { 426f2c31e32SEric Dumazet struct neighbour *neigh; 427398bcbebSYOSHIFUJI Hideaki int m; 428f2c31e32SEric Dumazet 429f2c31e32SEric Dumazet rcu_read_lock(); 430f2c31e32SEric Dumazet neigh = dst_get_neighbour(&rt->dst); 4314d0c5911SYOSHIFUJI Hideaki if (rt->rt6i_flags & RTF_NONEXTHOP || 4324d0c5911SYOSHIFUJI Hideaki !(rt->rt6i_flags & RTF_GATEWAY)) 4334d0c5911SYOSHIFUJI Hideaki m = 1; 4344d0c5911SYOSHIFUJI Hideaki else if (neigh) { 4351da177e4SLinus Torvalds read_lock_bh(&neigh->lock); 436554cfb7eSYOSHIFUJI Hideaki if (neigh->nud_state & NUD_VALID) 4374d0c5911SYOSHIFUJI Hideaki m = 2; 438398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 439398bcbebSYOSHIFUJI Hideaki else if (neigh->nud_state & NUD_FAILED) 440398bcbebSYOSHIFUJI Hideaki m = 0; 441398bcbebSYOSHIFUJI Hideaki #endif 442398bcbebSYOSHIFUJI Hideaki else 443ea73ee23SYOSHIFUJI Hideaki m = 1; 4441da177e4SLinus Torvalds read_unlock_bh(&neigh->lock); 445398bcbebSYOSHIFUJI Hideaki } else 446398bcbebSYOSHIFUJI Hideaki m = 0; 447f2c31e32SEric Dumazet rcu_read_unlock(); 448554cfb7eSYOSHIFUJI Hideaki return m; 4491da177e4SLinus Torvalds } 4501da177e4SLinus Torvalds 451554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif, 452554cfb7eSYOSHIFUJI Hideaki int strict) 453554cfb7eSYOSHIFUJI Hideaki { 4544d0c5911SYOSHIFUJI Hideaki int m, n; 4554d0c5911SYOSHIFUJI Hideaki 4564d0c5911SYOSHIFUJI Hideaki m = rt6_check_dev(rt, oif); 45777d16f45SYOSHIFUJI Hideaki if (!m && (strict & RT6_LOOKUP_F_IFACE)) 458554cfb7eSYOSHIFUJI Hideaki return -1; 459ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 460ebacaaa0SYOSHIFUJI Hideaki m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2; 461ebacaaa0SYOSHIFUJI Hideaki #endif 4624d0c5911SYOSHIFUJI Hideaki n = rt6_check_neigh(rt); 463557e92efSYOSHIFUJI Hideaki if (!n && (strict & RT6_LOOKUP_F_REACHABLE)) 464554cfb7eSYOSHIFUJI Hideaki return -1; 465554cfb7eSYOSHIFUJI Hideaki return m; 466554cfb7eSYOSHIFUJI Hideaki } 467554cfb7eSYOSHIFUJI Hideaki 468f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict, 469f11e6659SDavid S. Miller int *mpri, struct rt6_info *match) 470554cfb7eSYOSHIFUJI Hideaki { 471554cfb7eSYOSHIFUJI Hideaki int m; 472554cfb7eSYOSHIFUJI Hideaki 473554cfb7eSYOSHIFUJI Hideaki if (rt6_check_expired(rt)) 474f11e6659SDavid S. Miller goto out; 475554cfb7eSYOSHIFUJI Hideaki 476554cfb7eSYOSHIFUJI Hideaki m = rt6_score_route(rt, oif, strict); 477554cfb7eSYOSHIFUJI Hideaki if (m < 0) 478f11e6659SDavid S. Miller goto out; 479554cfb7eSYOSHIFUJI Hideaki 480f11e6659SDavid S. Miller if (m > *mpri) { 481ea659e07SYOSHIFUJI Hideaki if (strict & RT6_LOOKUP_F_REACHABLE) 48227097255SYOSHIFUJI Hideaki rt6_probe(match); 483f11e6659SDavid S. Miller *mpri = m; 484554cfb7eSYOSHIFUJI Hideaki match = rt; 485ea659e07SYOSHIFUJI Hideaki } else if (strict & RT6_LOOKUP_F_REACHABLE) { 48627097255SYOSHIFUJI Hideaki rt6_probe(rt); 4871da177e4SLinus Torvalds } 488f11e6659SDavid S. Miller 489f11e6659SDavid S. Miller out: 490f11e6659SDavid S. Miller return match; 4911da177e4SLinus Torvalds } 4921da177e4SLinus Torvalds 493f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn, 494f11e6659SDavid S. Miller struct rt6_info *rr_head, 495f11e6659SDavid S. Miller u32 metric, int oif, int strict) 496f11e6659SDavid S. Miller { 497f11e6659SDavid S. Miller struct rt6_info *rt, *match; 498f11e6659SDavid S. Miller int mpri = -1; 499f11e6659SDavid S. Miller 500f11e6659SDavid S. Miller match = NULL; 501f11e6659SDavid S. Miller for (rt = rr_head; rt && rt->rt6i_metric == metric; 502d8d1f30bSChangli Gao rt = rt->dst.rt6_next) 503f11e6659SDavid S. Miller match = find_match(rt, oif, strict, &mpri, match); 504f11e6659SDavid S. Miller for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric; 505d8d1f30bSChangli Gao rt = rt->dst.rt6_next) 506f11e6659SDavid S. Miller match = find_match(rt, oif, strict, &mpri, match); 507f11e6659SDavid S. Miller 508f11e6659SDavid S. Miller return match; 509f11e6659SDavid S. Miller } 510f11e6659SDavid S. Miller 511f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict) 512f11e6659SDavid S. Miller { 513f11e6659SDavid S. Miller struct rt6_info *match, *rt0; 5148ed67789SDaniel Lezcano struct net *net; 515f11e6659SDavid S. Miller 516f11e6659SDavid S. Miller RT6_TRACE("%s(fn->leaf=%p, oif=%d)\n", 5170dc47877SHarvey Harrison __func__, fn->leaf, oif); 518f11e6659SDavid S. Miller 519f11e6659SDavid S. Miller rt0 = fn->rr_ptr; 520f11e6659SDavid S. Miller if (!rt0) 521f11e6659SDavid S. Miller fn->rr_ptr = rt0 = fn->leaf; 522f11e6659SDavid S. Miller 523f11e6659SDavid S. Miller match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict); 524f11e6659SDavid S. Miller 525554cfb7eSYOSHIFUJI Hideaki if (!match && 526f11e6659SDavid S. Miller (strict & RT6_LOOKUP_F_REACHABLE)) { 527d8d1f30bSChangli Gao struct rt6_info *next = rt0->dst.rt6_next; 528f11e6659SDavid S. Miller 529554cfb7eSYOSHIFUJI Hideaki /* no entries matched; do round-robin */ 530f11e6659SDavid S. Miller if (!next || next->rt6i_metric != rt0->rt6i_metric) 531f11e6659SDavid S. Miller next = fn->leaf; 532f11e6659SDavid S. Miller 533f11e6659SDavid S. Miller if (next != rt0) 534f11e6659SDavid S. Miller fn->rr_ptr = next; 535554cfb7eSYOSHIFUJI Hideaki } 536554cfb7eSYOSHIFUJI Hideaki 537f11e6659SDavid S. Miller RT6_TRACE("%s() => %p\n", 5380dc47877SHarvey Harrison __func__, match); 539554cfb7eSYOSHIFUJI Hideaki 540c346dca1SYOSHIFUJI Hideaki net = dev_net(rt0->rt6i_dev); 541a02cec21SEric Dumazet return match ? match : net->ipv6.ip6_null_entry; 5421da177e4SLinus Torvalds } 5431da177e4SLinus Torvalds 54470ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 54570ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 546b71d1d42SEric Dumazet const struct in6_addr *gwaddr) 54770ceb4f5SYOSHIFUJI Hideaki { 548c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 54970ceb4f5SYOSHIFUJI Hideaki struct route_info *rinfo = (struct route_info *) opt; 55070ceb4f5SYOSHIFUJI Hideaki struct in6_addr prefix_buf, *prefix; 55170ceb4f5SYOSHIFUJI Hideaki unsigned int pref; 5524bed72e4SYOSHIFUJI Hideaki unsigned long lifetime; 55370ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt; 55470ceb4f5SYOSHIFUJI Hideaki 55570ceb4f5SYOSHIFUJI Hideaki if (len < sizeof(struct route_info)) { 55670ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 55770ceb4f5SYOSHIFUJI Hideaki } 55870ceb4f5SYOSHIFUJI Hideaki 55970ceb4f5SYOSHIFUJI Hideaki /* Sanity check for prefix_len and length */ 56070ceb4f5SYOSHIFUJI Hideaki if (rinfo->length > 3) { 56170ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 56270ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 128) { 56370ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 56470ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 64) { 56570ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 2) { 56670ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 56770ceb4f5SYOSHIFUJI Hideaki } 56870ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 0) { 56970ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 1) { 57070ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 57170ceb4f5SYOSHIFUJI Hideaki } 57270ceb4f5SYOSHIFUJI Hideaki } 57370ceb4f5SYOSHIFUJI Hideaki 57470ceb4f5SYOSHIFUJI Hideaki pref = rinfo->route_pref; 57570ceb4f5SYOSHIFUJI Hideaki if (pref == ICMPV6_ROUTER_PREF_INVALID) 5763933fc95SJens Rosenboom return -EINVAL; 57770ceb4f5SYOSHIFUJI Hideaki 5784bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); 57970ceb4f5SYOSHIFUJI Hideaki 58070ceb4f5SYOSHIFUJI Hideaki if (rinfo->length == 3) 58170ceb4f5SYOSHIFUJI Hideaki prefix = (struct in6_addr *)rinfo->prefix; 58270ceb4f5SYOSHIFUJI Hideaki else { 58370ceb4f5SYOSHIFUJI Hideaki /* this function is safe */ 58470ceb4f5SYOSHIFUJI Hideaki ipv6_addr_prefix(&prefix_buf, 58570ceb4f5SYOSHIFUJI Hideaki (struct in6_addr *)rinfo->prefix, 58670ceb4f5SYOSHIFUJI Hideaki rinfo->prefix_len); 58770ceb4f5SYOSHIFUJI Hideaki prefix = &prefix_buf; 58870ceb4f5SYOSHIFUJI Hideaki } 58970ceb4f5SYOSHIFUJI Hideaki 590efa2cea0SDaniel Lezcano rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr, 591efa2cea0SDaniel Lezcano dev->ifindex); 59270ceb4f5SYOSHIFUJI Hideaki 59370ceb4f5SYOSHIFUJI Hideaki if (rt && !lifetime) { 594e0a1ad73SThomas Graf ip6_del_rt(rt); 59570ceb4f5SYOSHIFUJI Hideaki rt = NULL; 59670ceb4f5SYOSHIFUJI Hideaki } 59770ceb4f5SYOSHIFUJI Hideaki 59870ceb4f5SYOSHIFUJI Hideaki if (!rt && lifetime) 599efa2cea0SDaniel Lezcano rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex, 60070ceb4f5SYOSHIFUJI Hideaki pref); 60170ceb4f5SYOSHIFUJI Hideaki else if (rt) 60270ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags = RTF_ROUTEINFO | 60370ceb4f5SYOSHIFUJI Hideaki (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 60470ceb4f5SYOSHIFUJI Hideaki 60570ceb4f5SYOSHIFUJI Hideaki if (rt) { 6064bed72e4SYOSHIFUJI Hideaki if (!addrconf_finite_timeout(lifetime)) { 60770ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags &= ~RTF_EXPIRES; 60870ceb4f5SYOSHIFUJI Hideaki } else { 60970ceb4f5SYOSHIFUJI Hideaki rt->rt6i_expires = jiffies + HZ * lifetime; 61070ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_EXPIRES; 61170ceb4f5SYOSHIFUJI Hideaki } 612d8d1f30bSChangli Gao dst_release(&rt->dst); 61370ceb4f5SYOSHIFUJI Hideaki } 61470ceb4f5SYOSHIFUJI Hideaki return 0; 61570ceb4f5SYOSHIFUJI Hideaki } 61670ceb4f5SYOSHIFUJI Hideaki #endif 61770ceb4f5SYOSHIFUJI Hideaki 6188ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr) \ 619982f56f3SYOSHIFUJI Hideaki do { \ 6208ed67789SDaniel Lezcano if (rt == __net->ipv6.ip6_null_entry) { \ 621982f56f3SYOSHIFUJI Hideaki struct fib6_node *pn; \ 622e0eda7bbSVille Nuorvala while (1) { \ 623982f56f3SYOSHIFUJI Hideaki if (fn->fn_flags & RTN_TL_ROOT) \ 624c71099acSThomas Graf goto out; \ 625982f56f3SYOSHIFUJI Hideaki pn = fn->parent; \ 626982f56f3SYOSHIFUJI Hideaki if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \ 6278bce65b9SKim Nordlund fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \ 628982f56f3SYOSHIFUJI Hideaki else \ 629982f56f3SYOSHIFUJI Hideaki fn = pn; \ 630c71099acSThomas Graf if (fn->fn_flags & RTN_RTINFO) \ 631c71099acSThomas Graf goto restart; \ 632c71099acSThomas Graf } \ 633982f56f3SYOSHIFUJI Hideaki } \ 634982f56f3SYOSHIFUJI Hideaki } while(0) 635c71099acSThomas Graf 6368ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net, 6378ed67789SDaniel Lezcano struct fib6_table *table, 6384c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 6391da177e4SLinus Torvalds { 6401da177e4SLinus Torvalds struct fib6_node *fn; 6411da177e4SLinus Torvalds struct rt6_info *rt; 6421da177e4SLinus Torvalds 643c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 6444c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 645c71099acSThomas Graf restart: 646c71099acSThomas Graf rt = fn->leaf; 6474c9483b2SDavid S. Miller rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags); 6484c9483b2SDavid S. Miller BACKTRACK(net, &fl6->saddr); 649c71099acSThomas Graf out: 650d8d1f30bSChangli Gao dst_use(&rt->dst, jiffies); 651c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 6521da177e4SLinus Torvalds return rt; 653c71099acSThomas Graf 654c71099acSThomas Graf } 655c71099acSThomas Graf 6569acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, 6579acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *saddr, int oif, int strict) 658c71099acSThomas Graf { 6594c9483b2SDavid S. Miller struct flowi6 fl6 = { 6604c9483b2SDavid S. Miller .flowi6_oif = oif, 6614c9483b2SDavid S. Miller .daddr = *daddr, 662c71099acSThomas Graf }; 663c71099acSThomas Graf struct dst_entry *dst; 66477d16f45SYOSHIFUJI Hideaki int flags = strict ? RT6_LOOKUP_F_IFACE : 0; 665c71099acSThomas Graf 666adaa70bbSThomas Graf if (saddr) { 6674c9483b2SDavid S. Miller memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 668adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 669adaa70bbSThomas Graf } 670adaa70bbSThomas Graf 6714c9483b2SDavid S. Miller dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup); 672c71099acSThomas Graf if (dst->error == 0) 673c71099acSThomas Graf return (struct rt6_info *) dst; 674c71099acSThomas Graf 675c71099acSThomas Graf dst_release(dst); 676c71099acSThomas Graf 6771da177e4SLinus Torvalds return NULL; 6781da177e4SLinus Torvalds } 6791da177e4SLinus Torvalds 6807159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup); 6817159039aSYOSHIFUJI Hideaki 682c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock. 6831da177e4SLinus Torvalds It takes new route entry, the addition fails by any reason the 6841da177e4SLinus Torvalds route is freed. In any case, if caller does not hold it, it may 6851da177e4SLinus Torvalds be destroyed. 6861da177e4SLinus Torvalds */ 6871da177e4SLinus Torvalds 68886872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info) 6891da177e4SLinus Torvalds { 6901da177e4SLinus Torvalds int err; 691c71099acSThomas Graf struct fib6_table *table; 6921da177e4SLinus Torvalds 693c71099acSThomas Graf table = rt->rt6i_table; 694c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 69586872cb5SThomas Graf err = fib6_add(&table->tb6_root, rt, info); 696c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 6971da177e4SLinus Torvalds 6981da177e4SLinus Torvalds return err; 6991da177e4SLinus Torvalds } 7001da177e4SLinus Torvalds 70140e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt) 70240e22e8fSThomas Graf { 7034d1169c1SDenis V. Lunev struct nl_info info = { 704c346dca1SYOSHIFUJI Hideaki .nl_net = dev_net(rt->rt6i_dev), 7054d1169c1SDenis V. Lunev }; 706528c4cebSDenis V. Lunev return __ip6_ins_rt(rt, &info); 70740e22e8fSThomas Graf } 70840e22e8fSThomas Graf 70921efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_cow(const struct rt6_info *ort, 71021efcfa0SEric Dumazet const struct in6_addr *daddr, 711b71d1d42SEric Dumazet const struct in6_addr *saddr) 7121da177e4SLinus Torvalds { 7131da177e4SLinus Torvalds struct rt6_info *rt; 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds /* 7161da177e4SLinus Torvalds * Clone the route. 7171da177e4SLinus Torvalds */ 7181da177e4SLinus Torvalds 71921efcfa0SEric Dumazet rt = ip6_rt_copy(ort, daddr); 7201da177e4SLinus Torvalds 7211da177e4SLinus Torvalds if (rt) { 72214deae41SDavid S. Miller struct neighbour *neigh; 72314deae41SDavid S. Miller int attempts = !in_softirq(); 72414deae41SDavid S. Miller 72558c4fb86SYOSHIFUJI Hideaki if (!(rt->rt6i_flags&RTF_GATEWAY)) { 72658c4fb86SYOSHIFUJI Hideaki if (rt->rt6i_dst.plen != 128 && 72721efcfa0SEric Dumazet ipv6_addr_equal(&ort->rt6i_dst.addr, daddr)) 72858c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 7291da177e4SLinus Torvalds ipv6_addr_copy(&rt->rt6i_gateway, daddr); 73058c4fb86SYOSHIFUJI Hideaki } 7311da177e4SLinus Torvalds 7321da177e4SLinus Torvalds rt->rt6i_flags |= RTF_CACHE; 7331da177e4SLinus Torvalds 7341da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 7351da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 7361da177e4SLinus Torvalds ipv6_addr_copy(&rt->rt6i_src.addr, saddr); 7371da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 7381da177e4SLinus Torvalds } 7391da177e4SLinus Torvalds #endif 7401da177e4SLinus Torvalds 74114deae41SDavid S. Miller retry: 74214deae41SDavid S. Miller neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway); 74314deae41SDavid S. Miller if (IS_ERR(neigh)) { 74414deae41SDavid S. Miller struct net *net = dev_net(rt->rt6i_dev); 74514deae41SDavid S. Miller int saved_rt_min_interval = 74614deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_min_interval; 74714deae41SDavid S. Miller int saved_rt_elasticity = 74814deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_elasticity; 74914deae41SDavid S. Miller 75014deae41SDavid S. Miller if (attempts-- > 0) { 75114deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_elasticity = 1; 75214deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_min_interval = 0; 75314deae41SDavid S. Miller 75486393e52SAlexey Dobriyan ip6_dst_gc(&net->ipv6.ip6_dst_ops); 75514deae41SDavid S. Miller 75614deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_elasticity = 75714deae41SDavid S. Miller saved_rt_elasticity; 75814deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_min_interval = 75914deae41SDavid S. Miller saved_rt_min_interval; 76014deae41SDavid S. Miller goto retry; 76114deae41SDavid S. Miller } 76214deae41SDavid S. Miller 76314deae41SDavid S. Miller if (net_ratelimit()) 76414deae41SDavid S. Miller printk(KERN_WARNING 7657e1b33e5SUlrich Weber "ipv6: Neighbour table overflow.\n"); 766d8d1f30bSChangli Gao dst_free(&rt->dst); 76714deae41SDavid S. Miller return NULL; 76814deae41SDavid S. Miller } 76969cce1d1SDavid S. Miller dst_set_neighbour(&rt->dst, neigh); 7701da177e4SLinus Torvalds 77195a9a5baSYOSHIFUJI Hideaki } 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds return rt; 7741da177e4SLinus Torvalds } 77595a9a5baSYOSHIFUJI Hideaki 77621efcfa0SEric Dumazet static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, 77721efcfa0SEric Dumazet const struct in6_addr *daddr) 778299d9939SYOSHIFUJI Hideaki { 77921efcfa0SEric Dumazet struct rt6_info *rt = ip6_rt_copy(ort, daddr); 78021efcfa0SEric Dumazet 781299d9939SYOSHIFUJI Hideaki if (rt) { 782299d9939SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_CACHE; 783f2c31e32SEric Dumazet dst_set_neighbour(&rt->dst, neigh_clone(dst_get_neighbour_raw(&ort->dst))); 784299d9939SYOSHIFUJI Hideaki } 785299d9939SYOSHIFUJI Hideaki return rt; 786299d9939SYOSHIFUJI Hideaki } 787299d9939SYOSHIFUJI Hideaki 7888ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif, 7894c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 7901da177e4SLinus Torvalds { 7911da177e4SLinus Torvalds struct fib6_node *fn; 792519fbd87SYOSHIFUJI Hideaki struct rt6_info *rt, *nrt; 793c71099acSThomas Graf int strict = 0; 7941da177e4SLinus Torvalds int attempts = 3; 795519fbd87SYOSHIFUJI Hideaki int err; 79653b7997fSYOSHIFUJI Hideaki int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE; 7971da177e4SLinus Torvalds 79877d16f45SYOSHIFUJI Hideaki strict |= flags & RT6_LOOKUP_F_IFACE; 7991da177e4SLinus Torvalds 8001da177e4SLinus Torvalds relookup: 801c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 8021da177e4SLinus Torvalds 8038238dd06SYOSHIFUJI Hideaki restart_2: 8044c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds restart: 8074acad72dSPavel Emelyanov rt = rt6_select(fn, oif, strict | reachable); 8088ed67789SDaniel Lezcano 8094c9483b2SDavid S. Miller BACKTRACK(net, &fl6->saddr); 8108ed67789SDaniel Lezcano if (rt == net->ipv6.ip6_null_entry || 8118238dd06SYOSHIFUJI Hideaki rt->rt6i_flags & RTF_CACHE) 8121da177e4SLinus Torvalds goto out; 8131da177e4SLinus Torvalds 814d8d1f30bSChangli Gao dst_hold(&rt->dst); 815c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 8161da177e4SLinus Torvalds 817f2c31e32SEric Dumazet if (!dst_get_neighbour_raw(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP)) 8184c9483b2SDavid S. Miller nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr); 8197343ff31SDavid S. Miller else if (!(rt->dst.flags & DST_HOST)) 8204c9483b2SDavid S. Miller nrt = rt6_alloc_clone(rt, &fl6->daddr); 8217343ff31SDavid S. Miller else 8227343ff31SDavid S. Miller goto out2; 8231da177e4SLinus Torvalds 824d8d1f30bSChangli Gao dst_release(&rt->dst); 8258ed67789SDaniel Lezcano rt = nrt ? : net->ipv6.ip6_null_entry; 8261da177e4SLinus Torvalds 827d8d1f30bSChangli Gao dst_hold(&rt->dst); 828e40cf353SYOSHIFUJI Hideaki if (nrt) { 82940e22e8fSThomas Graf err = ip6_ins_rt(nrt); 830e40cf353SYOSHIFUJI Hideaki if (!err) 831e40cf353SYOSHIFUJI Hideaki goto out2; 832e40cf353SYOSHIFUJI Hideaki } 833e40cf353SYOSHIFUJI Hideaki 834e40cf353SYOSHIFUJI Hideaki if (--attempts <= 0) 8351da177e4SLinus Torvalds goto out2; 8361da177e4SLinus Torvalds 837519fbd87SYOSHIFUJI Hideaki /* 838c71099acSThomas Graf * Race condition! In the gap, when table->tb6_lock was 839519fbd87SYOSHIFUJI Hideaki * released someone could insert this route. Relookup. 8401da177e4SLinus Torvalds */ 841d8d1f30bSChangli Gao dst_release(&rt->dst); 8421da177e4SLinus Torvalds goto relookup; 843e40cf353SYOSHIFUJI Hideaki 844519fbd87SYOSHIFUJI Hideaki out: 8458238dd06SYOSHIFUJI Hideaki if (reachable) { 8468238dd06SYOSHIFUJI Hideaki reachable = 0; 8478238dd06SYOSHIFUJI Hideaki goto restart_2; 8488238dd06SYOSHIFUJI Hideaki } 849d8d1f30bSChangli Gao dst_hold(&rt->dst); 850c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 8511da177e4SLinus Torvalds out2: 852d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 853d8d1f30bSChangli Gao rt->dst.__use++; 854c71099acSThomas Graf 855c71099acSThomas Graf return rt; 856c71099acSThomas Graf } 857c71099acSThomas Graf 8588ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table, 8594c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 8604acad72dSPavel Emelyanov { 8614c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags); 8624acad72dSPavel Emelyanov } 8634acad72dSPavel Emelyanov 864c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 865c71099acSThomas Graf { 866b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb); 867c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 868adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 8694c9483b2SDavid S. Miller struct flowi6 fl6 = { 8704c9483b2SDavid S. Miller .flowi6_iif = skb->dev->ifindex, 8714c9483b2SDavid S. Miller .daddr = iph->daddr, 8724c9483b2SDavid S. Miller .saddr = iph->saddr, 8734c9483b2SDavid S. Miller .flowlabel = (* (__be32 *) iph)&IPV6_FLOWINFO_MASK, 8744c9483b2SDavid S. Miller .flowi6_mark = skb->mark, 8754c9483b2SDavid S. Miller .flowi6_proto = iph->nexthdr, 876c71099acSThomas Graf }; 877adaa70bbSThomas Graf 8781d6e55f1SThomas Goff if (rt6_need_strict(&iph->daddr) && skb->dev->type != ARPHRD_PIMREG) 879adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_IFACE; 880c71099acSThomas Graf 8814c9483b2SDavid S. Miller skb_dst_set(skb, fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_input)); 882c71099acSThomas Graf } 883c71099acSThomas Graf 8848ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table, 8854c9483b2SDavid S. Miller struct flowi6 *fl6, int flags) 886c71099acSThomas Graf { 8874c9483b2SDavid S. Miller return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags); 888c71099acSThomas Graf } 889c71099acSThomas Graf 8909c7a4f9cSFlorian Westphal struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk, 8914c9483b2SDavid S. Miller struct flowi6 *fl6) 892c71099acSThomas Graf { 893c71099acSThomas Graf int flags = 0; 894c71099acSThomas Graf 8954c9483b2SDavid S. Miller if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr)) 89677d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 897c71099acSThomas Graf 8984c9483b2SDavid S. Miller if (!ipv6_addr_any(&fl6->saddr)) 899adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 9000c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 9010c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 902adaa70bbSThomas Graf 9034c9483b2SDavid S. Miller return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output); 9041da177e4SLinus Torvalds } 9051da177e4SLinus Torvalds 9067159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output); 9071da177e4SLinus Torvalds 9082774c131SDavid S. Miller struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) 90914e50e57SDavid S. Miller { 9105c1e6aa3SDavid S. Miller struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; 91114e50e57SDavid S. Miller struct dst_entry *new = NULL; 91214e50e57SDavid S. Miller 9135c1e6aa3SDavid S. Miller rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, 0, 0); 91414e50e57SDavid S. Miller if (rt) { 915cf911662SDavid S. Miller memset(&rt->rt6i_table, 0, sizeof(*rt) - sizeof(struct dst_entry)); 916cf911662SDavid S. Miller 917d8d1f30bSChangli Gao new = &rt->dst; 91814e50e57SDavid S. Miller 91914e50e57SDavid S. Miller new->__use = 1; 920352e512cSHerbert Xu new->input = dst_discard; 921352e512cSHerbert Xu new->output = dst_discard; 92214e50e57SDavid S. Miller 92321efcfa0SEric Dumazet if (dst_metrics_read_only(&ort->dst)) 92421efcfa0SEric Dumazet new->_metrics = ort->dst._metrics; 92521efcfa0SEric Dumazet else 926defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 92714e50e57SDavid S. Miller rt->rt6i_idev = ort->rt6i_idev; 92814e50e57SDavid S. Miller if (rt->rt6i_idev) 92914e50e57SDavid S. Miller in6_dev_hold(rt->rt6i_idev); 93014e50e57SDavid S. Miller rt->rt6i_expires = 0; 93114e50e57SDavid S. Miller 93214e50e57SDavid S. Miller ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway); 93314e50e57SDavid S. Miller rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES; 93414e50e57SDavid S. Miller rt->rt6i_metric = 0; 93514e50e57SDavid S. Miller 93614e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 93714e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 93814e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 93914e50e57SDavid S. Miller #endif 94014e50e57SDavid S. Miller 94114e50e57SDavid S. Miller dst_free(new); 94214e50e57SDavid S. Miller } 94314e50e57SDavid S. Miller 94469ead7afSDavid S. Miller dst_release(dst_orig); 94569ead7afSDavid S. Miller return new ? new : ERR_PTR(-ENOMEM); 94614e50e57SDavid S. Miller } 94714e50e57SDavid S. Miller 9481da177e4SLinus Torvalds /* 9491da177e4SLinus Torvalds * Destination cache support functions 9501da177e4SLinus Torvalds */ 9511da177e4SLinus Torvalds 9521da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 9531da177e4SLinus Torvalds { 9541da177e4SLinus Torvalds struct rt6_info *rt; 9551da177e4SLinus Torvalds 9561da177e4SLinus Torvalds rt = (struct rt6_info *) dst; 9571da177e4SLinus Torvalds 9586431cbc2SDavid S. Miller if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) { 9596431cbc2SDavid S. Miller if (rt->rt6i_peer_genid != rt6_peer_genid()) { 9606431cbc2SDavid S. Miller if (!rt->rt6i_peer) 9616431cbc2SDavid S. Miller rt6_bind_peer(rt, 0); 9626431cbc2SDavid S. Miller rt->rt6i_peer_genid = rt6_peer_genid(); 9636431cbc2SDavid S. Miller } 9641da177e4SLinus Torvalds return dst; 9656431cbc2SDavid S. Miller } 9661da177e4SLinus Torvalds return NULL; 9671da177e4SLinus Torvalds } 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 9701da177e4SLinus Torvalds { 9711da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 9721da177e4SLinus Torvalds 9731da177e4SLinus Torvalds if (rt) { 97454c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 97554c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 976e0a1ad73SThomas Graf ip6_del_rt(rt); 97754c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 9781da177e4SLinus Torvalds } 97954c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 98054c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 98154c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 98254c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 98354c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 98454c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 9851da177e4SLinus Torvalds } 9861da177e4SLinus Torvalds 9871da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 9881da177e4SLinus Torvalds { 9891da177e4SLinus Torvalds struct rt6_info *rt; 9901da177e4SLinus Torvalds 9913ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 9921da177e4SLinus Torvalds 993adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 9941da177e4SLinus Torvalds if (rt) { 9951da177e4SLinus Torvalds if (rt->rt6i_flags&RTF_CACHE) { 996d8d1f30bSChangli Gao dst_set_expires(&rt->dst, 0); 9971da177e4SLinus Torvalds rt->rt6i_flags |= RTF_EXPIRES; 9981da177e4SLinus Torvalds } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) 9991da177e4SLinus Torvalds rt->rt6i_node->fn_sernum = -1; 10001da177e4SLinus Torvalds } 10011da177e4SLinus Torvalds } 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu) 10041da177e4SLinus Torvalds { 10051da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info*)dst; 10061da177e4SLinus Torvalds 10071da177e4SLinus Torvalds if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) { 10081da177e4SLinus Torvalds rt6->rt6i_flags |= RTF_MODIFIED; 10091da177e4SLinus Torvalds if (mtu < IPV6_MIN_MTU) { 1010defb3519SDavid S. Miller u32 features = dst_metric(dst, RTAX_FEATURES); 10111da177e4SLinus Torvalds mtu = IPV6_MIN_MTU; 1012defb3519SDavid S. Miller features |= RTAX_FEATURE_ALLFRAG; 1013defb3519SDavid S. Miller dst_metric_set(dst, RTAX_FEATURES, features); 10141da177e4SLinus Torvalds } 1015defb3519SDavid S. Miller dst_metric_set(dst, RTAX_MTU, mtu); 10161da177e4SLinus Torvalds } 10171da177e4SLinus Torvalds } 10181da177e4SLinus Torvalds 10190dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 10201da177e4SLinus Torvalds { 10210dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 10220dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 10230dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 10240dbaee3bSDavid S. Miller 10251da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 10261da177e4SLinus Torvalds 10275578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 10285578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 10291da177e4SLinus Torvalds 10301da177e4SLinus Torvalds /* 10311da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 10321da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 10331da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 10341da177e4SLinus Torvalds * rely only on pmtu discovery" 10351da177e4SLinus Torvalds */ 10361da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 10371da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 10381da177e4SLinus Torvalds return mtu; 10391da177e4SLinus Torvalds } 10401da177e4SLinus Torvalds 1041d33e4553SDavid S. Miller static unsigned int ip6_default_mtu(const struct dst_entry *dst) 1042d33e4553SDavid S. Miller { 1043d33e4553SDavid S. Miller unsigned int mtu = IPV6_MIN_MTU; 1044d33e4553SDavid S. Miller struct inet6_dev *idev; 1045d33e4553SDavid S. Miller 1046d33e4553SDavid S. Miller rcu_read_lock(); 1047d33e4553SDavid S. Miller idev = __in6_dev_get(dst->dev); 1048d33e4553SDavid S. Miller if (idev) 1049d33e4553SDavid S. Miller mtu = idev->cnf.mtu6; 1050d33e4553SDavid S. Miller rcu_read_unlock(); 1051d33e4553SDavid S. Miller 1052d33e4553SDavid S. Miller return mtu; 1053d33e4553SDavid S. Miller } 1054d33e4553SDavid S. Miller 10553b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list; 10563b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock); 10575d0bbeebSThomas Graf 10583b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 10591da177e4SLinus Torvalds struct neighbour *neigh, 10609acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *addr) 10611da177e4SLinus Torvalds { 10621da177e4SLinus Torvalds struct rt6_info *rt; 10631da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 1064c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 10651da177e4SLinus Torvalds 10661da177e4SLinus Torvalds if (unlikely(idev == NULL)) 10671da177e4SLinus Torvalds return NULL; 10681da177e4SLinus Torvalds 1069957c665fSDavid S. Miller rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, dev, 0); 10701da177e4SLinus Torvalds if (unlikely(rt == NULL)) { 10711da177e4SLinus Torvalds in6_dev_put(idev); 10721da177e4SLinus Torvalds goto out; 10731da177e4SLinus Torvalds } 10741da177e4SLinus Torvalds 10751da177e4SLinus Torvalds if (neigh) 10761da177e4SLinus Torvalds neigh_hold(neigh); 107714deae41SDavid S. Miller else { 10781da177e4SLinus Torvalds neigh = ndisc_get_neigh(dev, addr); 107914deae41SDavid S. Miller if (IS_ERR(neigh)) 108014deae41SDavid S. Miller neigh = NULL; 108114deae41SDavid S. Miller } 10821da177e4SLinus Torvalds 1083*8e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 1084*8e2ec639SYan, Zheng rt->dst.output = ip6_output; 108569cce1d1SDavid S. Miller dst_set_neighbour(&rt->dst, neigh); 1086d8d1f30bSChangli Gao atomic_set(&rt->dst.__refcnt, 1); 1087defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255); 1088*8e2ec639SYan, Zheng 1089*8e2ec639SYan, Zheng ipv6_addr_copy(&rt->rt6i_dst.addr, addr); 1090*8e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 1091*8e2ec639SYan, Zheng rt->rt6i_idev = idev; 10921da177e4SLinus Torvalds 10933b00944cSYOSHIFUJI Hideaki spin_lock_bh(&icmp6_dst_lock); 1094d8d1f30bSChangli Gao rt->dst.next = icmp6_dst_gc_list; 1095d8d1f30bSChangli Gao icmp6_dst_gc_list = &rt->dst; 10963b00944cSYOSHIFUJI Hideaki spin_unlock_bh(&icmp6_dst_lock); 10971da177e4SLinus Torvalds 10985578689aSDaniel Lezcano fib6_force_start_gc(net); 10991da177e4SLinus Torvalds 11001da177e4SLinus Torvalds out: 1101d8d1f30bSChangli Gao return &rt->dst; 11021da177e4SLinus Torvalds } 11031da177e4SLinus Torvalds 11043d0f24a7SStephen Hemminger int icmp6_dst_gc(void) 11051da177e4SLinus Torvalds { 1106e9476e95SHagen Paul Pfeifer struct dst_entry *dst, **pprev; 11073d0f24a7SStephen Hemminger int more = 0; 11081da177e4SLinus Torvalds 11093b00944cSYOSHIFUJI Hideaki spin_lock_bh(&icmp6_dst_lock); 11103b00944cSYOSHIFUJI Hideaki pprev = &icmp6_dst_gc_list; 11115d0bbeebSThomas Graf 11121da177e4SLinus Torvalds while ((dst = *pprev) != NULL) { 11131da177e4SLinus Torvalds if (!atomic_read(&dst->__refcnt)) { 11141da177e4SLinus Torvalds *pprev = dst->next; 11151da177e4SLinus Torvalds dst_free(dst); 11161da177e4SLinus Torvalds } else { 11171da177e4SLinus Torvalds pprev = &dst->next; 11183d0f24a7SStephen Hemminger ++more; 11191da177e4SLinus Torvalds } 11201da177e4SLinus Torvalds } 11211da177e4SLinus Torvalds 11223b00944cSYOSHIFUJI Hideaki spin_unlock_bh(&icmp6_dst_lock); 11235d0bbeebSThomas Graf 11243d0f24a7SStephen Hemminger return more; 11251da177e4SLinus Torvalds } 11261da177e4SLinus Torvalds 11271e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg), 11281e493d19SDavid S. Miller void *arg) 11291e493d19SDavid S. Miller { 11301e493d19SDavid S. Miller struct dst_entry *dst, **pprev; 11311e493d19SDavid S. Miller 11321e493d19SDavid S. Miller spin_lock_bh(&icmp6_dst_lock); 11331e493d19SDavid S. Miller pprev = &icmp6_dst_gc_list; 11341e493d19SDavid S. Miller while ((dst = *pprev) != NULL) { 11351e493d19SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 11361e493d19SDavid S. Miller if (func(rt, arg)) { 11371e493d19SDavid S. Miller *pprev = dst->next; 11381e493d19SDavid S. Miller dst_free(dst); 11391e493d19SDavid S. Miller } else { 11401e493d19SDavid S. Miller pprev = &dst->next; 11411e493d19SDavid S. Miller } 11421e493d19SDavid S. Miller } 11431e493d19SDavid S. Miller spin_unlock_bh(&icmp6_dst_lock); 11441e493d19SDavid S. Miller } 11451e493d19SDavid S. Miller 1146569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops) 11471da177e4SLinus Torvalds { 11481da177e4SLinus Torvalds unsigned long now = jiffies; 114986393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 11507019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 11517019b78eSDaniel Lezcano int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; 11527019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 11537019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 11547019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 1155fc66f95cSEric Dumazet int entries; 11561da177e4SLinus Torvalds 1157fc66f95cSEric Dumazet entries = dst_entries_get_fast(ops); 11587019b78eSDaniel Lezcano if (time_after(rt_last_gc + rt_min_interval, now) && 1159fc66f95cSEric Dumazet entries <= rt_max_size) 11601da177e4SLinus Torvalds goto out; 11611da177e4SLinus Torvalds 11626891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire++; 11636891a346SBenjamin Thery fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net); 11646891a346SBenjamin Thery net->ipv6.ip6_rt_last_gc = now; 1165fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 1166fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 11677019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; 11681da177e4SLinus Torvalds out: 11697019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; 1170fc66f95cSEric Dumazet return entries > rt_max_size; 11711da177e4SLinus Torvalds } 11721da177e4SLinus Torvalds 11731da177e4SLinus Torvalds /* Clean host part of a prefix. Not necessary in radix tree, 11741da177e4SLinus Torvalds but results in cleaner routing tables. 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds Remove it only when all the things will work! 11771da177e4SLinus Torvalds */ 11781da177e4SLinus Torvalds 11796b75d090SYOSHIFUJI Hideaki int ip6_dst_hoplimit(struct dst_entry *dst) 11801da177e4SLinus Torvalds { 11815170ae82SDavid S. Miller int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT); 1182a02e4b7dSDavid S. Miller if (hoplimit == 0) { 11836b75d090SYOSHIFUJI Hideaki struct net_device *dev = dst->dev; 1184c68f24ccSEric Dumazet struct inet6_dev *idev; 1185c68f24ccSEric Dumazet 1186c68f24ccSEric Dumazet rcu_read_lock(); 1187c68f24ccSEric Dumazet idev = __in6_dev_get(dev); 1188c68f24ccSEric Dumazet if (idev) 11891da177e4SLinus Torvalds hoplimit = idev->cnf.hop_limit; 1190c68f24ccSEric Dumazet else 119153b7997fSYOSHIFUJI Hideaki hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit; 1192c68f24ccSEric Dumazet rcu_read_unlock(); 11931da177e4SLinus Torvalds } 11941da177e4SLinus Torvalds return hoplimit; 11951da177e4SLinus Torvalds } 1196abbf46aeSDavid S. Miller EXPORT_SYMBOL(ip6_dst_hoplimit); 11971da177e4SLinus Torvalds 11981da177e4SLinus Torvalds /* 11991da177e4SLinus Torvalds * 12001da177e4SLinus Torvalds */ 12011da177e4SLinus Torvalds 120286872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg) 12031da177e4SLinus Torvalds { 12041da177e4SLinus Torvalds int err; 12055578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 12061da177e4SLinus Torvalds struct rt6_info *rt = NULL; 12071da177e4SLinus Torvalds struct net_device *dev = NULL; 12081da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 1209c71099acSThomas Graf struct fib6_table *table; 12101da177e4SLinus Torvalds int addr_type; 12111da177e4SLinus Torvalds 121286872cb5SThomas Graf if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128) 12131da177e4SLinus Torvalds return -EINVAL; 12141da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 121586872cb5SThomas Graf if (cfg->fc_src_len) 12161da177e4SLinus Torvalds return -EINVAL; 12171da177e4SLinus Torvalds #endif 121886872cb5SThomas Graf if (cfg->fc_ifindex) { 12191da177e4SLinus Torvalds err = -ENODEV; 12205578689aSDaniel Lezcano dev = dev_get_by_index(net, cfg->fc_ifindex); 12211da177e4SLinus Torvalds if (!dev) 12221da177e4SLinus Torvalds goto out; 12231da177e4SLinus Torvalds idev = in6_dev_get(dev); 12241da177e4SLinus Torvalds if (!idev) 12251da177e4SLinus Torvalds goto out; 12261da177e4SLinus Torvalds } 12271da177e4SLinus Torvalds 122886872cb5SThomas Graf if (cfg->fc_metric == 0) 122986872cb5SThomas Graf cfg->fc_metric = IP6_RT_PRIO_USER; 12301da177e4SLinus Torvalds 12315578689aSDaniel Lezcano table = fib6_new_table(net, cfg->fc_table); 1232c71099acSThomas Graf if (table == NULL) { 1233c71099acSThomas Graf err = -ENOBUFS; 1234c71099acSThomas Graf goto out; 1235c71099acSThomas Graf } 1236c71099acSThomas Graf 1237957c665fSDavid S. Miller rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, NULL, DST_NOCOUNT); 12381da177e4SLinus Torvalds 12391da177e4SLinus Torvalds if (rt == NULL) { 12401da177e4SLinus Torvalds err = -ENOMEM; 12411da177e4SLinus Torvalds goto out; 12421da177e4SLinus Torvalds } 12431da177e4SLinus Torvalds 1244d8d1f30bSChangli Gao rt->dst.obsolete = -1; 12456f704992SYOSHIFUJI Hideaki rt->rt6i_expires = (cfg->fc_flags & RTF_EXPIRES) ? 12466f704992SYOSHIFUJI Hideaki jiffies + clock_t_to_jiffies(cfg->fc_expires) : 12476f704992SYOSHIFUJI Hideaki 0; 12481da177e4SLinus Torvalds 124986872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 125086872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 125186872cb5SThomas Graf rt->rt6i_protocol = cfg->fc_protocol; 125286872cb5SThomas Graf 125386872cb5SThomas Graf addr_type = ipv6_addr_type(&cfg->fc_dst); 12541da177e4SLinus Torvalds 12551da177e4SLinus Torvalds if (addr_type & IPV6_ADDR_MULTICAST) 1256d8d1f30bSChangli Gao rt->dst.input = ip6_mc_input; 1257ab79ad14SMaciej Żenczykowski else if (cfg->fc_flags & RTF_LOCAL) 1258ab79ad14SMaciej Żenczykowski rt->dst.input = ip6_input; 12591da177e4SLinus Torvalds else 1260d8d1f30bSChangli Gao rt->dst.input = ip6_forward; 12611da177e4SLinus Torvalds 1262d8d1f30bSChangli Gao rt->dst.output = ip6_output; 12631da177e4SLinus Torvalds 126486872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 126586872cb5SThomas Graf rt->rt6i_dst.plen = cfg->fc_dst_len; 12661da177e4SLinus Torvalds if (rt->rt6i_dst.plen == 128) 126711d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 12681da177e4SLinus Torvalds 1269*8e2ec639SYan, Zheng if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) { 1270*8e2ec639SYan, Zheng u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL); 1271*8e2ec639SYan, Zheng if (!metrics) { 1272*8e2ec639SYan, Zheng err = -ENOMEM; 1273*8e2ec639SYan, Zheng goto out; 1274*8e2ec639SYan, Zheng } 1275*8e2ec639SYan, Zheng dst_init_metrics(&rt->dst, metrics, 0); 1276*8e2ec639SYan, Zheng } 12771da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 127886872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len); 127986872cb5SThomas Graf rt->rt6i_src.plen = cfg->fc_src_len; 12801da177e4SLinus Torvalds #endif 12811da177e4SLinus Torvalds 128286872cb5SThomas Graf rt->rt6i_metric = cfg->fc_metric; 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds /* We cannot add true routes via loopback here, 12851da177e4SLinus Torvalds they would result in kernel looping; promote them to reject routes 12861da177e4SLinus Torvalds */ 128786872cb5SThomas Graf if ((cfg->fc_flags & RTF_REJECT) || 1288ab79ad14SMaciej Żenczykowski (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK) 1289ab79ad14SMaciej Żenczykowski && !(cfg->fc_flags&RTF_LOCAL))) { 12901da177e4SLinus Torvalds /* hold loopback dev/idev if we haven't done so. */ 12915578689aSDaniel Lezcano if (dev != net->loopback_dev) { 12921da177e4SLinus Torvalds if (dev) { 12931da177e4SLinus Torvalds dev_put(dev); 12941da177e4SLinus Torvalds in6_dev_put(idev); 12951da177e4SLinus Torvalds } 12965578689aSDaniel Lezcano dev = net->loopback_dev; 12971da177e4SLinus Torvalds dev_hold(dev); 12981da177e4SLinus Torvalds idev = in6_dev_get(dev); 12991da177e4SLinus Torvalds if (!idev) { 13001da177e4SLinus Torvalds err = -ENODEV; 13011da177e4SLinus Torvalds goto out; 13021da177e4SLinus Torvalds } 13031da177e4SLinus Torvalds } 1304d8d1f30bSChangli Gao rt->dst.output = ip6_pkt_discard_out; 1305d8d1f30bSChangli Gao rt->dst.input = ip6_pkt_discard; 1306d8d1f30bSChangli Gao rt->dst.error = -ENETUNREACH; 13071da177e4SLinus Torvalds rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP; 13081da177e4SLinus Torvalds goto install_route; 13091da177e4SLinus Torvalds } 13101da177e4SLinus Torvalds 131186872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY) { 1312b71d1d42SEric Dumazet const struct in6_addr *gw_addr; 13131da177e4SLinus Torvalds int gwa_type; 13141da177e4SLinus Torvalds 131586872cb5SThomas Graf gw_addr = &cfg->fc_gateway; 131686872cb5SThomas Graf ipv6_addr_copy(&rt->rt6i_gateway, gw_addr); 13171da177e4SLinus Torvalds gwa_type = ipv6_addr_type(gw_addr); 13181da177e4SLinus Torvalds 13191da177e4SLinus Torvalds if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) { 13201da177e4SLinus Torvalds struct rt6_info *grt; 13211da177e4SLinus Torvalds 13221da177e4SLinus Torvalds /* IPv6 strictly inhibits using not link-local 13231da177e4SLinus Torvalds addresses as nexthop address. 13241da177e4SLinus Torvalds Otherwise, router will not able to send redirects. 13251da177e4SLinus Torvalds It is very good, but in some (rare!) circumstances 13261da177e4SLinus Torvalds (SIT, PtP, NBMA NOARP links) it is handy to allow 13271da177e4SLinus Torvalds some exceptions. --ANK 13281da177e4SLinus Torvalds */ 13291da177e4SLinus Torvalds err = -EINVAL; 13301da177e4SLinus Torvalds if (!(gwa_type&IPV6_ADDR_UNICAST)) 13311da177e4SLinus Torvalds goto out; 13321da177e4SLinus Torvalds 13335578689aSDaniel Lezcano grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1); 13341da177e4SLinus Torvalds 13351da177e4SLinus Torvalds err = -EHOSTUNREACH; 13361da177e4SLinus Torvalds if (grt == NULL) 13371da177e4SLinus Torvalds goto out; 13381da177e4SLinus Torvalds if (dev) { 13391da177e4SLinus Torvalds if (dev != grt->rt6i_dev) { 1340d8d1f30bSChangli Gao dst_release(&grt->dst); 13411da177e4SLinus Torvalds goto out; 13421da177e4SLinus Torvalds } 13431da177e4SLinus Torvalds } else { 13441da177e4SLinus Torvalds dev = grt->rt6i_dev; 13451da177e4SLinus Torvalds idev = grt->rt6i_idev; 13461da177e4SLinus Torvalds dev_hold(dev); 13471da177e4SLinus Torvalds in6_dev_hold(grt->rt6i_idev); 13481da177e4SLinus Torvalds } 13491da177e4SLinus Torvalds if (!(grt->rt6i_flags&RTF_GATEWAY)) 13501da177e4SLinus Torvalds err = 0; 1351d8d1f30bSChangli Gao dst_release(&grt->dst); 13521da177e4SLinus Torvalds 13531da177e4SLinus Torvalds if (err) 13541da177e4SLinus Torvalds goto out; 13551da177e4SLinus Torvalds } 13561da177e4SLinus Torvalds err = -EINVAL; 13571da177e4SLinus Torvalds if (dev == NULL || (dev->flags&IFF_LOOPBACK)) 13581da177e4SLinus Torvalds goto out; 13591da177e4SLinus Torvalds } 13601da177e4SLinus Torvalds 13611da177e4SLinus Torvalds err = -ENODEV; 13621da177e4SLinus Torvalds if (dev == NULL) 13631da177e4SLinus Torvalds goto out; 13641da177e4SLinus Torvalds 1365c3968a85SDaniel Walter if (!ipv6_addr_any(&cfg->fc_prefsrc)) { 1366c3968a85SDaniel Walter if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { 1367c3968a85SDaniel Walter err = -EINVAL; 1368c3968a85SDaniel Walter goto out; 1369c3968a85SDaniel Walter } 1370c3968a85SDaniel Walter ipv6_addr_copy(&rt->rt6i_prefsrc.addr, &cfg->fc_prefsrc); 1371c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 128; 1372c3968a85SDaniel Walter } else 1373c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 1374c3968a85SDaniel Walter 137586872cb5SThomas Graf if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) { 137669cce1d1SDavid S. Miller struct neighbour *n = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev); 137769cce1d1SDavid S. Miller if (IS_ERR(n)) { 137869cce1d1SDavid S. Miller err = PTR_ERR(n); 13791da177e4SLinus Torvalds goto out; 13801da177e4SLinus Torvalds } 138169cce1d1SDavid S. Miller dst_set_neighbour(&rt->dst, n); 13821da177e4SLinus Torvalds } 13831da177e4SLinus Torvalds 138486872cb5SThomas Graf rt->rt6i_flags = cfg->fc_flags; 13851da177e4SLinus Torvalds 13861da177e4SLinus Torvalds install_route: 138786872cb5SThomas Graf if (cfg->fc_mx) { 138886872cb5SThomas Graf struct nlattr *nla; 138986872cb5SThomas Graf int remaining; 13901da177e4SLinus Torvalds 139186872cb5SThomas Graf nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { 13928f4c1f9bSThomas Graf int type = nla_type(nla); 139386872cb5SThomas Graf 139486872cb5SThomas Graf if (type) { 139586872cb5SThomas Graf if (type > RTAX_MAX) { 13961da177e4SLinus Torvalds err = -EINVAL; 13971da177e4SLinus Torvalds goto out; 13981da177e4SLinus Torvalds } 139986872cb5SThomas Graf 1400defb3519SDavid S. Miller dst_metric_set(&rt->dst, type, nla_get_u32(nla)); 14011da177e4SLinus Torvalds } 14021da177e4SLinus Torvalds } 14031da177e4SLinus Torvalds } 14041da177e4SLinus Torvalds 1405d8d1f30bSChangli Gao rt->dst.dev = dev; 14061da177e4SLinus Torvalds rt->rt6i_idev = idev; 1407c71099acSThomas Graf rt->rt6i_table = table; 140863152fc0SDaniel Lezcano 1409c346dca1SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = dev_net(dev); 141063152fc0SDaniel Lezcano 141186872cb5SThomas Graf return __ip6_ins_rt(rt, &cfg->fc_nlinfo); 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds out: 14141da177e4SLinus Torvalds if (dev) 14151da177e4SLinus Torvalds dev_put(dev); 14161da177e4SLinus Torvalds if (idev) 14171da177e4SLinus Torvalds in6_dev_put(idev); 14181da177e4SLinus Torvalds if (rt) 1419d8d1f30bSChangli Gao dst_free(&rt->dst); 14201da177e4SLinus Torvalds return err; 14211da177e4SLinus Torvalds } 14221da177e4SLinus Torvalds 142386872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info) 14241da177e4SLinus Torvalds { 14251da177e4SLinus Torvalds int err; 1426c71099acSThomas Graf struct fib6_table *table; 1427c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(rt->rt6i_dev); 14281da177e4SLinus Torvalds 14298ed67789SDaniel Lezcano if (rt == net->ipv6.ip6_null_entry) 14306c813a72SPatrick McHardy return -ENOENT; 14316c813a72SPatrick McHardy 1432c71099acSThomas Graf table = rt->rt6i_table; 1433c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 14341da177e4SLinus Torvalds 143586872cb5SThomas Graf err = fib6_del(rt, info); 1436d8d1f30bSChangli Gao dst_release(&rt->dst); 14371da177e4SLinus Torvalds 1438c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 14391da177e4SLinus Torvalds 14401da177e4SLinus Torvalds return err; 14411da177e4SLinus Torvalds } 14421da177e4SLinus Torvalds 1443e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt) 1444e0a1ad73SThomas Graf { 14454d1169c1SDenis V. Lunev struct nl_info info = { 1446c346dca1SYOSHIFUJI Hideaki .nl_net = dev_net(rt->rt6i_dev), 14474d1169c1SDenis V. Lunev }; 1448528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 1449e0a1ad73SThomas Graf } 1450e0a1ad73SThomas Graf 145186872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg) 14521da177e4SLinus Torvalds { 1453c71099acSThomas Graf struct fib6_table *table; 14541da177e4SLinus Torvalds struct fib6_node *fn; 14551da177e4SLinus Torvalds struct rt6_info *rt; 14561da177e4SLinus Torvalds int err = -ESRCH; 14571da177e4SLinus Torvalds 14585578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 1459c71099acSThomas Graf if (table == NULL) 1460c71099acSThomas Graf return err; 14611da177e4SLinus Torvalds 1462c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 1463c71099acSThomas Graf 1464c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 146586872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 146686872cb5SThomas Graf &cfg->fc_src, cfg->fc_src_len); 14671da177e4SLinus Torvalds 14681da177e4SLinus Torvalds if (fn) { 1469d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 147086872cb5SThomas Graf if (cfg->fc_ifindex && 14711da177e4SLinus Torvalds (rt->rt6i_dev == NULL || 147286872cb5SThomas Graf rt->rt6i_dev->ifindex != cfg->fc_ifindex)) 14731da177e4SLinus Torvalds continue; 147486872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 147586872cb5SThomas Graf !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 14761da177e4SLinus Torvalds continue; 147786872cb5SThomas Graf if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric) 14781da177e4SLinus Torvalds continue; 1479d8d1f30bSChangli Gao dst_hold(&rt->dst); 1480c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 14811da177e4SLinus Torvalds 148286872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 14831da177e4SLinus Torvalds } 14841da177e4SLinus Torvalds } 1485c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 14861da177e4SLinus Torvalds 14871da177e4SLinus Torvalds return err; 14881da177e4SLinus Torvalds } 14891da177e4SLinus Torvalds 14901da177e4SLinus Torvalds /* 14911da177e4SLinus Torvalds * Handle redirects 14921da177e4SLinus Torvalds */ 1493a6279458SYOSHIFUJI Hideaki struct ip6rd_flowi { 14944c9483b2SDavid S. Miller struct flowi6 fl6; 1495a6279458SYOSHIFUJI Hideaki struct in6_addr gateway; 1496a6279458SYOSHIFUJI Hideaki }; 14971da177e4SLinus Torvalds 14988ed67789SDaniel Lezcano static struct rt6_info *__ip6_route_redirect(struct net *net, 14998ed67789SDaniel Lezcano struct fib6_table *table, 15004c9483b2SDavid S. Miller struct flowi6 *fl6, 1501a6279458SYOSHIFUJI Hideaki int flags) 1502a6279458SYOSHIFUJI Hideaki { 15034c9483b2SDavid S. Miller struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; 1504a6279458SYOSHIFUJI Hideaki struct rt6_info *rt; 1505a6279458SYOSHIFUJI Hideaki struct fib6_node *fn; 1506c71099acSThomas Graf 1507e843b9e1SYOSHIFUJI Hideaki /* 1508e843b9e1SYOSHIFUJI Hideaki * Get the "current" route for this destination and 1509e843b9e1SYOSHIFUJI Hideaki * check if the redirect has come from approriate router. 1510e843b9e1SYOSHIFUJI Hideaki * 1511e843b9e1SYOSHIFUJI Hideaki * RFC 2461 specifies that redirects should only be 1512e843b9e1SYOSHIFUJI Hideaki * accepted if they come from the nexthop to the target. 1513e843b9e1SYOSHIFUJI Hideaki * Due to the way the routes are chosen, this notion 1514e843b9e1SYOSHIFUJI Hideaki * is a bit fuzzy and one might need to check all possible 1515e843b9e1SYOSHIFUJI Hideaki * routes. 1516e843b9e1SYOSHIFUJI Hideaki */ 15171da177e4SLinus Torvalds 1518c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 15194c9483b2SDavid S. Miller fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); 1520e843b9e1SYOSHIFUJI Hideaki restart: 1521d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 15221da177e4SLinus Torvalds /* 15231da177e4SLinus Torvalds * Current route is on-link; redirect is always invalid. 15241da177e4SLinus Torvalds * 15251da177e4SLinus Torvalds * Seems, previous statement is not true. It could 15261da177e4SLinus Torvalds * be node, which looks for us as on-link (f.e. proxy ndisc) 15271da177e4SLinus Torvalds * But then router serving it might decide, that we should 15281da177e4SLinus Torvalds * know truth 8)8) --ANK (980726). 15291da177e4SLinus Torvalds */ 1530e843b9e1SYOSHIFUJI Hideaki if (rt6_check_expired(rt)) 1531e843b9e1SYOSHIFUJI Hideaki continue; 15321da177e4SLinus Torvalds if (!(rt->rt6i_flags & RTF_GATEWAY)) 1533e843b9e1SYOSHIFUJI Hideaki continue; 15344c9483b2SDavid S. Miller if (fl6->flowi6_oif != rt->rt6i_dev->ifindex) 1535e843b9e1SYOSHIFUJI Hideaki continue; 1536a6279458SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway)) 1537e843b9e1SYOSHIFUJI Hideaki continue; 1538e843b9e1SYOSHIFUJI Hideaki break; 1539e843b9e1SYOSHIFUJI Hideaki } 1540a6279458SYOSHIFUJI Hideaki 1541cb15d9c2SYOSHIFUJI Hideaki if (!rt) 15428ed67789SDaniel Lezcano rt = net->ipv6.ip6_null_entry; 15434c9483b2SDavid S. Miller BACKTRACK(net, &fl6->saddr); 1544cb15d9c2SYOSHIFUJI Hideaki out: 1545d8d1f30bSChangli Gao dst_hold(&rt->dst); 1546a6279458SYOSHIFUJI Hideaki 1547c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 15481da177e4SLinus Torvalds 1549a6279458SYOSHIFUJI Hideaki return rt; 1550a6279458SYOSHIFUJI Hideaki }; 1551a6279458SYOSHIFUJI Hideaki 1552b71d1d42SEric Dumazet static struct rt6_info *ip6_route_redirect(const struct in6_addr *dest, 1553b71d1d42SEric Dumazet const struct in6_addr *src, 1554b71d1d42SEric Dumazet const struct in6_addr *gateway, 1555a6279458SYOSHIFUJI Hideaki struct net_device *dev) 1556a6279458SYOSHIFUJI Hideaki { 1557adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 1558c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 1559a6279458SYOSHIFUJI Hideaki struct ip6rd_flowi rdfl = { 15604c9483b2SDavid S. Miller .fl6 = { 15614c9483b2SDavid S. Miller .flowi6_oif = dev->ifindex, 15624c9483b2SDavid S. Miller .daddr = *dest, 15634c9483b2SDavid S. Miller .saddr = *src, 1564a6279458SYOSHIFUJI Hideaki }, 1565a6279458SYOSHIFUJI Hideaki }; 1566adaa70bbSThomas Graf 156786c36ce4SBrian Haley ipv6_addr_copy(&rdfl.gateway, gateway); 156886c36ce4SBrian Haley 1569adaa70bbSThomas Graf if (rt6_need_strict(dest)) 1570adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_IFACE; 1571a6279458SYOSHIFUJI Hideaki 15724c9483b2SDavid S. Miller return (struct rt6_info *)fib6_rule_lookup(net, &rdfl.fl6, 157358f09b78SDaniel Lezcano flags, __ip6_route_redirect); 1574a6279458SYOSHIFUJI Hideaki } 1575a6279458SYOSHIFUJI Hideaki 1576b71d1d42SEric Dumazet void rt6_redirect(const struct in6_addr *dest, const struct in6_addr *src, 1577b71d1d42SEric Dumazet const struct in6_addr *saddr, 1578a6279458SYOSHIFUJI Hideaki struct neighbour *neigh, u8 *lladdr, int on_link) 1579a6279458SYOSHIFUJI Hideaki { 1580a6279458SYOSHIFUJI Hideaki struct rt6_info *rt, *nrt = NULL; 1581a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 1582c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(neigh->dev); 1583a6279458SYOSHIFUJI Hideaki 1584a6279458SYOSHIFUJI Hideaki rt = ip6_route_redirect(dest, src, saddr, neigh->dev); 1585a6279458SYOSHIFUJI Hideaki 15868ed67789SDaniel Lezcano if (rt == net->ipv6.ip6_null_entry) { 15871da177e4SLinus Torvalds if (net_ratelimit()) 15881da177e4SLinus Torvalds printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop " 15891da177e4SLinus Torvalds "for redirect target\n"); 1590a6279458SYOSHIFUJI Hideaki goto out; 15911da177e4SLinus Torvalds } 15921da177e4SLinus Torvalds 15931da177e4SLinus Torvalds /* 15941da177e4SLinus Torvalds * We have finally decided to accept it. 15951da177e4SLinus Torvalds */ 15961da177e4SLinus Torvalds 15971da177e4SLinus Torvalds neigh_update(neigh, lladdr, NUD_STALE, 15981da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 15991da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 16001da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 16011da177e4SLinus Torvalds NEIGH_UPDATE_F_ISROUTER)) 16021da177e4SLinus Torvalds ); 16031da177e4SLinus Torvalds 16041da177e4SLinus Torvalds /* 16051da177e4SLinus Torvalds * Redirect received -> path was valid. 16061da177e4SLinus Torvalds * Look, redirects are sent only in response to data packets, 16071da177e4SLinus Torvalds * so that this nexthop apparently is reachable. --ANK 16081da177e4SLinus Torvalds */ 1609d8d1f30bSChangli Gao dst_confirm(&rt->dst); 16101da177e4SLinus Torvalds 16111da177e4SLinus Torvalds /* Duplicate redirect: silently ignore. */ 1612f2c31e32SEric Dumazet if (neigh == dst_get_neighbour_raw(&rt->dst)) 16131da177e4SLinus Torvalds goto out; 16141da177e4SLinus Torvalds 161521efcfa0SEric Dumazet nrt = ip6_rt_copy(rt, dest); 16161da177e4SLinus Torvalds if (nrt == NULL) 16171da177e4SLinus Torvalds goto out; 16181da177e4SLinus Torvalds 16191da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 16201da177e4SLinus Torvalds if (on_link) 16211da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 16221da177e4SLinus Torvalds 16231da177e4SLinus Torvalds ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key); 162469cce1d1SDavid S. Miller dst_set_neighbour(&nrt->dst, neigh_clone(neigh)); 16251da177e4SLinus Torvalds 162640e22e8fSThomas Graf if (ip6_ins_rt(nrt)) 16271da177e4SLinus Torvalds goto out; 16281da177e4SLinus Torvalds 1629d8d1f30bSChangli Gao netevent.old = &rt->dst; 1630d8d1f30bSChangli Gao netevent.new = &nrt->dst; 16318d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 16328d71740cSTom Tucker 16331da177e4SLinus Torvalds if (rt->rt6i_flags&RTF_CACHE) { 1634e0a1ad73SThomas Graf ip6_del_rt(rt); 16351da177e4SLinus Torvalds return; 16361da177e4SLinus Torvalds } 16371da177e4SLinus Torvalds 16381da177e4SLinus Torvalds out: 1639d8d1f30bSChangli Gao dst_release(&rt->dst); 16401da177e4SLinus Torvalds } 16411da177e4SLinus Torvalds 16421da177e4SLinus Torvalds /* 16431da177e4SLinus Torvalds * Handle ICMP "packet too big" messages 16441da177e4SLinus Torvalds * i.e. Path MTU discovery 16451da177e4SLinus Torvalds */ 16461da177e4SLinus Torvalds 1647b71d1d42SEric Dumazet static void rt6_do_pmtu_disc(const struct in6_addr *daddr, const struct in6_addr *saddr, 1648ae878ae2SMaciej Żenczykowski struct net *net, u32 pmtu, int ifindex) 16491da177e4SLinus Torvalds { 16501da177e4SLinus Torvalds struct rt6_info *rt, *nrt; 16511da177e4SLinus Torvalds int allfrag = 0; 1652d3052b55SAndrey Vagin again: 1653ae878ae2SMaciej Żenczykowski rt = rt6_lookup(net, daddr, saddr, ifindex, 0); 16541da177e4SLinus Torvalds if (rt == NULL) 16551da177e4SLinus Torvalds return; 16561da177e4SLinus Torvalds 1657d3052b55SAndrey Vagin if (rt6_check_expired(rt)) { 1658d3052b55SAndrey Vagin ip6_del_rt(rt); 1659d3052b55SAndrey Vagin goto again; 1660d3052b55SAndrey Vagin } 1661d3052b55SAndrey Vagin 1662d8d1f30bSChangli Gao if (pmtu >= dst_mtu(&rt->dst)) 16631da177e4SLinus Torvalds goto out; 16641da177e4SLinus Torvalds 16651da177e4SLinus Torvalds if (pmtu < IPV6_MIN_MTU) { 16661da177e4SLinus Torvalds /* 16671da177e4SLinus Torvalds * According to RFC2460, PMTU is set to the IPv6 Minimum Link 16681da177e4SLinus Torvalds * MTU (1280) and a fragment header should always be included 16691da177e4SLinus Torvalds * after a node receiving Too Big message reporting PMTU is 16701da177e4SLinus Torvalds * less than the IPv6 Minimum Link MTU. 16711da177e4SLinus Torvalds */ 16721da177e4SLinus Torvalds pmtu = IPV6_MIN_MTU; 16731da177e4SLinus Torvalds allfrag = 1; 16741da177e4SLinus Torvalds } 16751da177e4SLinus Torvalds 16761da177e4SLinus Torvalds /* New mtu received -> path was valid. 16771da177e4SLinus Torvalds They are sent only in response to data packets, 16781da177e4SLinus Torvalds so that this nexthop apparently is reachable. --ANK 16791da177e4SLinus Torvalds */ 1680d8d1f30bSChangli Gao dst_confirm(&rt->dst); 16811da177e4SLinus Torvalds 16821da177e4SLinus Torvalds /* Host route. If it is static, it would be better 16831da177e4SLinus Torvalds not to override it, but add new one, so that 16841da177e4SLinus Torvalds when cache entry will expire old pmtu 16851da177e4SLinus Torvalds would return automatically. 16861da177e4SLinus Torvalds */ 16871da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) { 1688defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_MTU, pmtu); 1689defb3519SDavid S. Miller if (allfrag) { 1690defb3519SDavid S. Miller u32 features = dst_metric(&rt->dst, RTAX_FEATURES); 1691defb3519SDavid S. Miller features |= RTAX_FEATURE_ALLFRAG; 1692defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_FEATURES, features); 1693defb3519SDavid S. Miller } 1694d8d1f30bSChangli Gao dst_set_expires(&rt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires); 16951da177e4SLinus Torvalds rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES; 16961da177e4SLinus Torvalds goto out; 16971da177e4SLinus Torvalds } 16981da177e4SLinus Torvalds 16991da177e4SLinus Torvalds /* Network route. 17001da177e4SLinus Torvalds Two cases are possible: 17011da177e4SLinus Torvalds 1. It is connected route. Action: COW 17021da177e4SLinus Torvalds 2. It is gatewayed route or NONEXTHOP route. Action: clone it. 17031da177e4SLinus Torvalds */ 1704f2c31e32SEric Dumazet if (!dst_get_neighbour_raw(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP)) 1705a1e78363SYOSHIFUJI Hideaki nrt = rt6_alloc_cow(rt, daddr, saddr); 1706d5315b50SYOSHIFUJI Hideaki else 1707d5315b50SYOSHIFUJI Hideaki nrt = rt6_alloc_clone(rt, daddr); 1708a1e78363SYOSHIFUJI Hideaki 1709d5315b50SYOSHIFUJI Hideaki if (nrt) { 1710defb3519SDavid S. Miller dst_metric_set(&nrt->dst, RTAX_MTU, pmtu); 1711defb3519SDavid S. Miller if (allfrag) { 1712defb3519SDavid S. Miller u32 features = dst_metric(&nrt->dst, RTAX_FEATURES); 1713defb3519SDavid S. Miller features |= RTAX_FEATURE_ALLFRAG; 1714defb3519SDavid S. Miller dst_metric_set(&nrt->dst, RTAX_FEATURES, features); 1715defb3519SDavid S. Miller } 1716a1e78363SYOSHIFUJI Hideaki 17171da177e4SLinus Torvalds /* According to RFC 1981, detecting PMTU increase shouldn't be 1718a1e78363SYOSHIFUJI Hideaki * happened within 5 mins, the recommended timer is 10 mins. 1719a1e78363SYOSHIFUJI Hideaki * Here this route expiration time is set to ip6_rt_mtu_expires 1720a1e78363SYOSHIFUJI Hideaki * which is 10 mins. After 10 mins the decreased pmtu is expired 1721a1e78363SYOSHIFUJI Hideaki * and detecting PMTU increase will be automatically happened. 17221da177e4SLinus Torvalds */ 1723d8d1f30bSChangli Gao dst_set_expires(&nrt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires); 17241da177e4SLinus Torvalds nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES; 1725a1e78363SYOSHIFUJI Hideaki 172640e22e8fSThomas Graf ip6_ins_rt(nrt); 17271da177e4SLinus Torvalds } 17281da177e4SLinus Torvalds out: 1729d8d1f30bSChangli Gao dst_release(&rt->dst); 17301da177e4SLinus Torvalds } 17311da177e4SLinus Torvalds 1732b71d1d42SEric Dumazet void rt6_pmtu_discovery(const struct in6_addr *daddr, const struct in6_addr *saddr, 1733ae878ae2SMaciej Żenczykowski struct net_device *dev, u32 pmtu) 1734ae878ae2SMaciej Żenczykowski { 1735ae878ae2SMaciej Żenczykowski struct net *net = dev_net(dev); 1736ae878ae2SMaciej Żenczykowski 1737ae878ae2SMaciej Żenczykowski /* 1738ae878ae2SMaciej Żenczykowski * RFC 1981 states that a node "MUST reduce the size of the packets it 1739ae878ae2SMaciej Żenczykowski * is sending along the path" that caused the Packet Too Big message. 1740ae878ae2SMaciej Żenczykowski * Since it's not possible in the general case to determine which 1741ae878ae2SMaciej Żenczykowski * interface was used to send the original packet, we update the MTU 1742ae878ae2SMaciej Żenczykowski * on the interface that will be used to send future packets. We also 1743ae878ae2SMaciej Żenczykowski * update the MTU on the interface that received the Packet Too Big in 1744ae878ae2SMaciej Żenczykowski * case the original packet was forced out that interface with 1745ae878ae2SMaciej Żenczykowski * SO_BINDTODEVICE or similar. This is the next best thing to the 1746ae878ae2SMaciej Żenczykowski * correct behaviour, which would be to update the MTU on all 1747ae878ae2SMaciej Żenczykowski * interfaces. 1748ae878ae2SMaciej Żenczykowski */ 1749ae878ae2SMaciej Żenczykowski rt6_do_pmtu_disc(daddr, saddr, net, pmtu, 0); 1750ae878ae2SMaciej Żenczykowski rt6_do_pmtu_disc(daddr, saddr, net, pmtu, dev->ifindex); 1751ae878ae2SMaciej Żenczykowski } 1752ae878ae2SMaciej Żenczykowski 17531da177e4SLinus Torvalds /* 17541da177e4SLinus Torvalds * Misc support functions 17551da177e4SLinus Torvalds */ 17561da177e4SLinus Torvalds 175721efcfa0SEric Dumazet static struct rt6_info *ip6_rt_copy(const struct rt6_info *ort, 175821efcfa0SEric Dumazet const struct in6_addr *dest) 17591da177e4SLinus Torvalds { 1760c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(ort->rt6i_dev); 17615c1e6aa3SDavid S. Miller struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, 1762957c665fSDavid S. Miller ort->dst.dev, 0); 17631da177e4SLinus Torvalds 17641da177e4SLinus Torvalds if (rt) { 1765d8d1f30bSChangli Gao rt->dst.input = ort->dst.input; 1766d8d1f30bSChangli Gao rt->dst.output = ort->dst.output; 1767*8e2ec639SYan, Zheng rt->dst.flags |= DST_HOST; 17681da177e4SLinus Torvalds 176921efcfa0SEric Dumazet ipv6_addr_copy(&rt->rt6i_dst.addr, dest); 1770*8e2ec639SYan, Zheng rt->rt6i_dst.plen = 128; 1771defb3519SDavid S. Miller dst_copy_metrics(&rt->dst, &ort->dst); 1772d8d1f30bSChangli Gao rt->dst.error = ort->dst.error; 17731da177e4SLinus Torvalds rt->rt6i_idev = ort->rt6i_idev; 17741da177e4SLinus Torvalds if (rt->rt6i_idev) 17751da177e4SLinus Torvalds in6_dev_hold(rt->rt6i_idev); 1776d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 17771da177e4SLinus Torvalds rt->rt6i_expires = 0; 17781da177e4SLinus Torvalds 17791da177e4SLinus Torvalds ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway); 17801da177e4SLinus Torvalds rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES; 17811da177e4SLinus Torvalds rt->rt6i_metric = 0; 17821da177e4SLinus Torvalds 17831da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 17841da177e4SLinus Torvalds memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 17851da177e4SLinus Torvalds #endif 17860f6c6392SFlorian Westphal memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key)); 1787c71099acSThomas Graf rt->rt6i_table = ort->rt6i_table; 17881da177e4SLinus Torvalds } 17891da177e4SLinus Torvalds return rt; 17901da177e4SLinus Torvalds } 17911da177e4SLinus Torvalds 179270ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 1793efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 1794b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 1795b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex) 179670ceb4f5SYOSHIFUJI Hideaki { 179770ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 179870ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt = NULL; 1799c71099acSThomas Graf struct fib6_table *table; 180070ceb4f5SYOSHIFUJI Hideaki 1801efa2cea0SDaniel Lezcano table = fib6_get_table(net, RT6_TABLE_INFO); 1802c71099acSThomas Graf if (table == NULL) 1803c71099acSThomas Graf return NULL; 1804c71099acSThomas Graf 1805c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 1806c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0); 180770ceb4f5SYOSHIFUJI Hideaki if (!fn) 180870ceb4f5SYOSHIFUJI Hideaki goto out; 180970ceb4f5SYOSHIFUJI Hideaki 1810d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 181170ceb4f5SYOSHIFUJI Hideaki if (rt->rt6i_dev->ifindex != ifindex) 181270ceb4f5SYOSHIFUJI Hideaki continue; 181370ceb4f5SYOSHIFUJI Hideaki if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) 181470ceb4f5SYOSHIFUJI Hideaki continue; 181570ceb4f5SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr)) 181670ceb4f5SYOSHIFUJI Hideaki continue; 1817d8d1f30bSChangli Gao dst_hold(&rt->dst); 181870ceb4f5SYOSHIFUJI Hideaki break; 181970ceb4f5SYOSHIFUJI Hideaki } 182070ceb4f5SYOSHIFUJI Hideaki out: 1821c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 182270ceb4f5SYOSHIFUJI Hideaki return rt; 182370ceb4f5SYOSHIFUJI Hideaki } 182470ceb4f5SYOSHIFUJI Hideaki 1825efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 1826b71d1d42SEric Dumazet const struct in6_addr *prefix, int prefixlen, 1827b71d1d42SEric Dumazet const struct in6_addr *gwaddr, int ifindex, 182870ceb4f5SYOSHIFUJI Hideaki unsigned pref) 182970ceb4f5SYOSHIFUJI Hideaki { 183086872cb5SThomas Graf struct fib6_config cfg = { 183186872cb5SThomas Graf .fc_table = RT6_TABLE_INFO, 1832238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 183386872cb5SThomas Graf .fc_ifindex = ifindex, 183486872cb5SThomas Graf .fc_dst_len = prefixlen, 183586872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 183686872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 1837efa2cea0SDaniel Lezcano .fc_nlinfo.pid = 0, 1838efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 1839efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 184086872cb5SThomas Graf }; 184170ceb4f5SYOSHIFUJI Hideaki 184286872cb5SThomas Graf ipv6_addr_copy(&cfg.fc_dst, prefix); 184386872cb5SThomas Graf ipv6_addr_copy(&cfg.fc_gateway, gwaddr); 184486872cb5SThomas Graf 1845e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 1846e317da96SYOSHIFUJI Hideaki if (!prefixlen) 184786872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 184870ceb4f5SYOSHIFUJI Hideaki 184986872cb5SThomas Graf ip6_route_add(&cfg); 185070ceb4f5SYOSHIFUJI Hideaki 1851efa2cea0SDaniel Lezcano return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex); 185270ceb4f5SYOSHIFUJI Hideaki } 185370ceb4f5SYOSHIFUJI Hideaki #endif 185470ceb4f5SYOSHIFUJI Hideaki 1855b71d1d42SEric Dumazet struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev) 18561da177e4SLinus Torvalds { 18571da177e4SLinus Torvalds struct rt6_info *rt; 1858c71099acSThomas Graf struct fib6_table *table; 18591da177e4SLinus Torvalds 1860c346dca1SYOSHIFUJI Hideaki table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT); 1861c71099acSThomas Graf if (table == NULL) 1862c71099acSThomas Graf return NULL; 18631da177e4SLinus Torvalds 1864c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 1865d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) { 18661da177e4SLinus Torvalds if (dev == rt->rt6i_dev && 1867045927ffSYOSHIFUJI Hideaki ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 18681da177e4SLinus Torvalds ipv6_addr_equal(&rt->rt6i_gateway, addr)) 18691da177e4SLinus Torvalds break; 18701da177e4SLinus Torvalds } 18711da177e4SLinus Torvalds if (rt) 1872d8d1f30bSChangli Gao dst_hold(&rt->dst); 1873c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 18741da177e4SLinus Torvalds return rt; 18751da177e4SLinus Torvalds } 18761da177e4SLinus Torvalds 1877b71d1d42SEric Dumazet struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr, 1878ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 1879ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 18801da177e4SLinus Torvalds { 188186872cb5SThomas Graf struct fib6_config cfg = { 188286872cb5SThomas Graf .fc_table = RT6_TABLE_DFLT, 1883238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 188486872cb5SThomas Graf .fc_ifindex = dev->ifindex, 188586872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 188686872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 18875578689aSDaniel Lezcano .fc_nlinfo.pid = 0, 18885578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 1889c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 189086872cb5SThomas Graf }; 18911da177e4SLinus Torvalds 189286872cb5SThomas Graf ipv6_addr_copy(&cfg.fc_gateway, gwaddr); 18931da177e4SLinus Torvalds 189486872cb5SThomas Graf ip6_route_add(&cfg); 18951da177e4SLinus Torvalds 18961da177e4SLinus Torvalds return rt6_get_dflt_router(gwaddr, dev); 18971da177e4SLinus Torvalds } 18981da177e4SLinus Torvalds 18997b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net) 19001da177e4SLinus Torvalds { 19011da177e4SLinus Torvalds struct rt6_info *rt; 1902c71099acSThomas Graf struct fib6_table *table; 1903c71099acSThomas Graf 1904c71099acSThomas Graf /* NOTE: Keep consistent with rt6_get_dflt_router */ 19057b4da532SDaniel Lezcano table = fib6_get_table(net, RT6_TABLE_DFLT); 1906c71099acSThomas Graf if (table == NULL) 1907c71099acSThomas Graf return; 19081da177e4SLinus Torvalds 19091da177e4SLinus Torvalds restart: 1910c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 1911d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 19121da177e4SLinus Torvalds if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) { 1913d8d1f30bSChangli Gao dst_hold(&rt->dst); 1914c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 1915e0a1ad73SThomas Graf ip6_del_rt(rt); 19161da177e4SLinus Torvalds goto restart; 19171da177e4SLinus Torvalds } 19181da177e4SLinus Torvalds } 1919c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 19201da177e4SLinus Torvalds } 19211da177e4SLinus Torvalds 19225578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 19235578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 192486872cb5SThomas Graf struct fib6_config *cfg) 192586872cb5SThomas Graf { 192686872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 192786872cb5SThomas Graf 192886872cb5SThomas Graf cfg->fc_table = RT6_TABLE_MAIN; 192986872cb5SThomas Graf cfg->fc_ifindex = rtmsg->rtmsg_ifindex; 193086872cb5SThomas Graf cfg->fc_metric = rtmsg->rtmsg_metric; 193186872cb5SThomas Graf cfg->fc_expires = rtmsg->rtmsg_info; 193286872cb5SThomas Graf cfg->fc_dst_len = rtmsg->rtmsg_dst_len; 193386872cb5SThomas Graf cfg->fc_src_len = rtmsg->rtmsg_src_len; 193486872cb5SThomas Graf cfg->fc_flags = rtmsg->rtmsg_flags; 193586872cb5SThomas Graf 19365578689aSDaniel Lezcano cfg->fc_nlinfo.nl_net = net; 1937f1243c2dSBenjamin Thery 193886872cb5SThomas Graf ipv6_addr_copy(&cfg->fc_dst, &rtmsg->rtmsg_dst); 193986872cb5SThomas Graf ipv6_addr_copy(&cfg->fc_src, &rtmsg->rtmsg_src); 194086872cb5SThomas Graf ipv6_addr_copy(&cfg->fc_gateway, &rtmsg->rtmsg_gateway); 194186872cb5SThomas Graf } 194286872cb5SThomas Graf 19435578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) 19441da177e4SLinus Torvalds { 194586872cb5SThomas Graf struct fib6_config cfg; 19461da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 19471da177e4SLinus Torvalds int err; 19481da177e4SLinus Torvalds 19491da177e4SLinus Torvalds switch(cmd) { 19501da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 19511da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 19521da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 19531da177e4SLinus Torvalds return -EPERM; 19541da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 19551da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 19561da177e4SLinus Torvalds if (err) 19571da177e4SLinus Torvalds return -EFAULT; 19581da177e4SLinus Torvalds 19595578689aSDaniel Lezcano rtmsg_to_fib6_config(net, &rtmsg, &cfg); 196086872cb5SThomas Graf 19611da177e4SLinus Torvalds rtnl_lock(); 19621da177e4SLinus Torvalds switch (cmd) { 19631da177e4SLinus Torvalds case SIOCADDRT: 196486872cb5SThomas Graf err = ip6_route_add(&cfg); 19651da177e4SLinus Torvalds break; 19661da177e4SLinus Torvalds case SIOCDELRT: 196786872cb5SThomas Graf err = ip6_route_del(&cfg); 19681da177e4SLinus Torvalds break; 19691da177e4SLinus Torvalds default: 19701da177e4SLinus Torvalds err = -EINVAL; 19711da177e4SLinus Torvalds } 19721da177e4SLinus Torvalds rtnl_unlock(); 19731da177e4SLinus Torvalds 19741da177e4SLinus Torvalds return err; 19753ff50b79SStephen Hemminger } 19761da177e4SLinus Torvalds 19771da177e4SLinus Torvalds return -EINVAL; 19781da177e4SLinus Torvalds } 19791da177e4SLinus Torvalds 19801da177e4SLinus Torvalds /* 19811da177e4SLinus Torvalds * Drop the packet on the floor 19821da177e4SLinus Torvalds */ 19831da177e4SLinus Torvalds 1984d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 19851da177e4SLinus Torvalds { 1986612f09e8SYOSHIFUJI Hideaki int type; 1987adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 1988612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 1989612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 19900660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 199145bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 19923bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 19933bd653c8SDenis V. Lunev IPSTATS_MIB_INADDRERRORS); 1994612f09e8SYOSHIFUJI Hideaki break; 1995612f09e8SYOSHIFUJI Hideaki } 1996612f09e8SYOSHIFUJI Hideaki /* FALLTHROUGH */ 1997612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 19983bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 19993bd653c8SDenis V. Lunev ipstats_mib_noroutes); 2000612f09e8SYOSHIFUJI Hideaki break; 2001612f09e8SYOSHIFUJI Hideaki } 20023ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 20031da177e4SLinus Torvalds kfree_skb(skb); 20041da177e4SLinus Torvalds return 0; 20051da177e4SLinus Torvalds } 20061da177e4SLinus Torvalds 20079ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 20089ce8ade0SThomas Graf { 2009612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 20109ce8ade0SThomas Graf } 20119ce8ade0SThomas Graf 201220380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb) 20131da177e4SLinus Torvalds { 2014adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 2015612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 20161da177e4SLinus Torvalds } 20171da177e4SLinus Torvalds 20186723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES 20196723ab54SDavid S. Miller 20209ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 20219ce8ade0SThomas Graf { 2022612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 20239ce8ade0SThomas Graf } 20249ce8ade0SThomas Graf 20259ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb) 20269ce8ade0SThomas Graf { 2027adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 2028612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 20299ce8ade0SThomas Graf } 20309ce8ade0SThomas Graf 20316723ab54SDavid S. Miller #endif 20326723ab54SDavid S. Miller 20331da177e4SLinus Torvalds /* 20341da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 20351da177e4SLinus Torvalds */ 20361da177e4SLinus Torvalds 20371da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, 20381da177e4SLinus Torvalds const struct in6_addr *addr, 20391da177e4SLinus Torvalds int anycast) 20401da177e4SLinus Torvalds { 2041c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 20425c1e6aa3SDavid S. Miller struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, 2043957c665fSDavid S. Miller net->loopback_dev, 0); 204414deae41SDavid S. Miller struct neighbour *neigh; 20451da177e4SLinus Torvalds 204640385653SBen Greear if (rt == NULL) { 204740385653SBen Greear if (net_ratelimit()) 204840385653SBen Greear pr_warning("IPv6: Maximum number of routes reached," 204940385653SBen Greear " consider increasing route/max_size.\n"); 20501da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 205140385653SBen Greear } 20521da177e4SLinus Torvalds 20531da177e4SLinus Torvalds in6_dev_hold(idev); 20541da177e4SLinus Torvalds 205511d53b49SDavid S. Miller rt->dst.flags |= DST_HOST; 2056d8d1f30bSChangli Gao rt->dst.input = ip6_input; 2057d8d1f30bSChangli Gao rt->dst.output = ip6_output; 20581da177e4SLinus Torvalds rt->rt6i_idev = idev; 2059d8d1f30bSChangli Gao rt->dst.obsolete = -1; 20601da177e4SLinus Torvalds 20611da177e4SLinus Torvalds rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP; 206258c4fb86SYOSHIFUJI Hideaki if (anycast) 206358c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 206458c4fb86SYOSHIFUJI Hideaki else 20651da177e4SLinus Torvalds rt->rt6i_flags |= RTF_LOCAL; 206614deae41SDavid S. Miller neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway); 206714deae41SDavid S. Miller if (IS_ERR(neigh)) { 2068d8d1f30bSChangli Gao dst_free(&rt->dst); 206914deae41SDavid S. Miller 207029546a64SDavid S. Miller return ERR_CAST(neigh); 20711da177e4SLinus Torvalds } 207269cce1d1SDavid S. Miller dst_set_neighbour(&rt->dst, neigh); 20731da177e4SLinus Torvalds 20741da177e4SLinus Torvalds ipv6_addr_copy(&rt->rt6i_dst.addr, addr); 20751da177e4SLinus Torvalds rt->rt6i_dst.plen = 128; 20765578689aSDaniel Lezcano rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL); 20771da177e4SLinus Torvalds 2078d8d1f30bSChangli Gao atomic_set(&rt->dst.__refcnt, 1); 20791da177e4SLinus Torvalds 20801da177e4SLinus Torvalds return rt; 20811da177e4SLinus Torvalds } 20821da177e4SLinus Torvalds 2083c3968a85SDaniel Walter int ip6_route_get_saddr(struct net *net, 2084c3968a85SDaniel Walter struct rt6_info *rt, 2085b71d1d42SEric Dumazet const struct in6_addr *daddr, 2086c3968a85SDaniel Walter unsigned int prefs, 2087c3968a85SDaniel Walter struct in6_addr *saddr) 2088c3968a85SDaniel Walter { 2089c3968a85SDaniel Walter struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt); 2090c3968a85SDaniel Walter int err = 0; 2091c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) 2092c3968a85SDaniel Walter ipv6_addr_copy(saddr, &rt->rt6i_prefsrc.addr); 2093c3968a85SDaniel Walter else 2094c3968a85SDaniel Walter err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL, 2095c3968a85SDaniel Walter daddr, prefs, saddr); 2096c3968a85SDaniel Walter return err; 2097c3968a85SDaniel Walter } 2098c3968a85SDaniel Walter 2099c3968a85SDaniel Walter /* remove deleted ip from prefsrc entries */ 2100c3968a85SDaniel Walter struct arg_dev_net_ip { 2101c3968a85SDaniel Walter struct net_device *dev; 2102c3968a85SDaniel Walter struct net *net; 2103c3968a85SDaniel Walter struct in6_addr *addr; 2104c3968a85SDaniel Walter }; 2105c3968a85SDaniel Walter 2106c3968a85SDaniel Walter static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg) 2107c3968a85SDaniel Walter { 2108c3968a85SDaniel Walter struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; 2109c3968a85SDaniel Walter struct net *net = ((struct arg_dev_net_ip *)arg)->net; 2110c3968a85SDaniel Walter struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; 2111c3968a85SDaniel Walter 2112c3968a85SDaniel Walter if (((void *)rt->rt6i_dev == dev || dev == NULL) && 2113c3968a85SDaniel Walter rt != net->ipv6.ip6_null_entry && 2114c3968a85SDaniel Walter ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) { 2115c3968a85SDaniel Walter /* remove prefsrc entry */ 2116c3968a85SDaniel Walter rt->rt6i_prefsrc.plen = 0; 2117c3968a85SDaniel Walter } 2118c3968a85SDaniel Walter return 0; 2119c3968a85SDaniel Walter } 2120c3968a85SDaniel Walter 2121c3968a85SDaniel Walter void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) 2122c3968a85SDaniel Walter { 2123c3968a85SDaniel Walter struct net *net = dev_net(ifp->idev->dev); 2124c3968a85SDaniel Walter struct arg_dev_net_ip adni = { 2125c3968a85SDaniel Walter .dev = ifp->idev->dev, 2126c3968a85SDaniel Walter .net = net, 2127c3968a85SDaniel Walter .addr = &ifp->addr, 2128c3968a85SDaniel Walter }; 2129c3968a85SDaniel Walter fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni); 2130c3968a85SDaniel Walter } 2131c3968a85SDaniel Walter 21328ed67789SDaniel Lezcano struct arg_dev_net { 21338ed67789SDaniel Lezcano struct net_device *dev; 21348ed67789SDaniel Lezcano struct net *net; 21358ed67789SDaniel Lezcano }; 21368ed67789SDaniel Lezcano 21371da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg) 21381da177e4SLinus Torvalds { 2139bc3ef660Sstephen hemminger const struct arg_dev_net *adn = arg; 2140bc3ef660Sstephen hemminger const struct net_device *dev = adn->dev; 21418ed67789SDaniel Lezcano 2142bc3ef660Sstephen hemminger if ((rt->rt6i_dev == dev || dev == NULL) && 2143bc3ef660Sstephen hemminger rt != adn->net->ipv6.ip6_null_entry) { 21441da177e4SLinus Torvalds RT6_TRACE("deleted by ifdown %p\n", rt); 21451da177e4SLinus Torvalds return -1; 21461da177e4SLinus Torvalds } 21471da177e4SLinus Torvalds return 0; 21481da177e4SLinus Torvalds } 21491da177e4SLinus Torvalds 2150f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev) 21511da177e4SLinus Torvalds { 21528ed67789SDaniel Lezcano struct arg_dev_net adn = { 21538ed67789SDaniel Lezcano .dev = dev, 21548ed67789SDaniel Lezcano .net = net, 21558ed67789SDaniel Lezcano }; 21568ed67789SDaniel Lezcano 21578ed67789SDaniel Lezcano fib6_clean_all(net, fib6_ifdown, 0, &adn); 21581e493d19SDavid S. Miller icmp6_clean_all(fib6_ifdown, &adn); 21591da177e4SLinus Torvalds } 21601da177e4SLinus Torvalds 21611da177e4SLinus Torvalds struct rt6_mtu_change_arg 21621da177e4SLinus Torvalds { 21631da177e4SLinus Torvalds struct net_device *dev; 21641da177e4SLinus Torvalds unsigned mtu; 21651da177e4SLinus Torvalds }; 21661da177e4SLinus Torvalds 21671da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg) 21681da177e4SLinus Torvalds { 21691da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 21701da177e4SLinus Torvalds struct inet6_dev *idev; 21711da177e4SLinus Torvalds 21721da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 21731da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 21741da177e4SLinus Torvalds We still use this lock to block changes 21751da177e4SLinus Torvalds caused by addrconf/ndisc. 21761da177e4SLinus Torvalds */ 21771da177e4SLinus Torvalds 21781da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 21791da177e4SLinus Torvalds if (idev == NULL) 21801da177e4SLinus Torvalds return 0; 21811da177e4SLinus Torvalds 21821da177e4SLinus Torvalds /* For administrative MTU increase, there is no way to discover 21831da177e4SLinus Torvalds IPv6 PMTU increase, so PMTU increase should be updated here. 21841da177e4SLinus Torvalds Since RFC 1981 doesn't include administrative MTU increase 21851da177e4SLinus Torvalds update PMTU increase is a MUST. (i.e. jumbo frame) 21861da177e4SLinus Torvalds */ 21871da177e4SLinus Torvalds /* 21881da177e4SLinus Torvalds If new MTU is less than route PMTU, this new MTU will be the 21891da177e4SLinus Torvalds lowest MTU in the path, update the route PMTU to reflect PMTU 21901da177e4SLinus Torvalds decreases; if new MTU is greater than route PMTU, and the 21911da177e4SLinus Torvalds old MTU is the lowest MTU in the path, update the route PMTU 21921da177e4SLinus Torvalds to reflect the increase. In this case if the other nodes' MTU 21931da177e4SLinus Torvalds also have the lowest MTU, TOO BIG MESSAGE will be lead to 21941da177e4SLinus Torvalds PMTU discouvery. 21951da177e4SLinus Torvalds */ 21961da177e4SLinus Torvalds if (rt->rt6i_dev == arg->dev && 2197d8d1f30bSChangli Gao !dst_metric_locked(&rt->dst, RTAX_MTU) && 2198d8d1f30bSChangli Gao (dst_mtu(&rt->dst) >= arg->mtu || 2199d8d1f30bSChangli Gao (dst_mtu(&rt->dst) < arg->mtu && 2200d8d1f30bSChangli Gao dst_mtu(&rt->dst) == idev->cnf.mtu6))) { 2201defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu); 2202566cfd8fSSimon Arlott } 22031da177e4SLinus Torvalds return 0; 22041da177e4SLinus Torvalds } 22051da177e4SLinus Torvalds 22061da177e4SLinus Torvalds void rt6_mtu_change(struct net_device *dev, unsigned mtu) 22071da177e4SLinus Torvalds { 2208c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 2209c71099acSThomas Graf .dev = dev, 2210c71099acSThomas Graf .mtu = mtu, 2211c71099acSThomas Graf }; 22121da177e4SLinus Torvalds 2213c346dca1SYOSHIFUJI Hideaki fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg); 22141da177e4SLinus Torvalds } 22151da177e4SLinus Torvalds 2216ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 22175176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 221886872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 2219ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 222086872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 222186872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 222286872cb5SThomas Graf }; 222386872cb5SThomas Graf 222486872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 222586872cb5SThomas Graf struct fib6_config *cfg) 22261da177e4SLinus Torvalds { 222786872cb5SThomas Graf struct rtmsg *rtm; 222886872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 222986872cb5SThomas Graf int err; 22301da177e4SLinus Torvalds 223186872cb5SThomas Graf err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy); 223286872cb5SThomas Graf if (err < 0) 223386872cb5SThomas Graf goto errout; 22341da177e4SLinus Torvalds 223586872cb5SThomas Graf err = -EINVAL; 223686872cb5SThomas Graf rtm = nlmsg_data(nlh); 223786872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 223886872cb5SThomas Graf 223986872cb5SThomas Graf cfg->fc_table = rtm->rtm_table; 224086872cb5SThomas Graf cfg->fc_dst_len = rtm->rtm_dst_len; 224186872cb5SThomas Graf cfg->fc_src_len = rtm->rtm_src_len; 224286872cb5SThomas Graf cfg->fc_flags = RTF_UP; 224386872cb5SThomas Graf cfg->fc_protocol = rtm->rtm_protocol; 224486872cb5SThomas Graf 224586872cb5SThomas Graf if (rtm->rtm_type == RTN_UNREACHABLE) 224686872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 224786872cb5SThomas Graf 2248ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 2249ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 2250ab79ad14SMaciej Żenczykowski 225186872cb5SThomas Graf cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid; 225286872cb5SThomas Graf cfg->fc_nlinfo.nlh = nlh; 22533b1e0a65SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = sock_net(skb->sk); 225486872cb5SThomas Graf 225586872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 225686872cb5SThomas Graf nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16); 225786872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 22581da177e4SLinus Torvalds } 225986872cb5SThomas Graf 226086872cb5SThomas Graf if (tb[RTA_DST]) { 226186872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 226286872cb5SThomas Graf 226386872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 226486872cb5SThomas Graf goto errout; 226586872cb5SThomas Graf 226686872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 22671da177e4SLinus Torvalds } 226886872cb5SThomas Graf 226986872cb5SThomas Graf if (tb[RTA_SRC]) { 227086872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 227186872cb5SThomas Graf 227286872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 227386872cb5SThomas Graf goto errout; 227486872cb5SThomas Graf 227586872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 22761da177e4SLinus Torvalds } 227786872cb5SThomas Graf 2278c3968a85SDaniel Walter if (tb[RTA_PREFSRC]) 2279c3968a85SDaniel Walter nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16); 2280c3968a85SDaniel Walter 228186872cb5SThomas Graf if (tb[RTA_OIF]) 228286872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 228386872cb5SThomas Graf 228486872cb5SThomas Graf if (tb[RTA_PRIORITY]) 228586872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 228686872cb5SThomas Graf 228786872cb5SThomas Graf if (tb[RTA_METRICS]) { 228886872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 228986872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 22901da177e4SLinus Torvalds } 229186872cb5SThomas Graf 229286872cb5SThomas Graf if (tb[RTA_TABLE]) 229386872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 229486872cb5SThomas Graf 229586872cb5SThomas Graf err = 0; 229686872cb5SThomas Graf errout: 229786872cb5SThomas Graf return err; 22981da177e4SLinus Torvalds } 22991da177e4SLinus Torvalds 2300c127ea2cSThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 23011da177e4SLinus Torvalds { 230286872cb5SThomas Graf struct fib6_config cfg; 230386872cb5SThomas Graf int err; 23041da177e4SLinus Torvalds 230586872cb5SThomas Graf err = rtm_to_fib6_config(skb, nlh, &cfg); 230686872cb5SThomas Graf if (err < 0) 230786872cb5SThomas Graf return err; 230886872cb5SThomas Graf 230986872cb5SThomas Graf return ip6_route_del(&cfg); 23101da177e4SLinus Torvalds } 23111da177e4SLinus Torvalds 2312c127ea2cSThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 23131da177e4SLinus Torvalds { 231486872cb5SThomas Graf struct fib6_config cfg; 231586872cb5SThomas Graf int err; 23161da177e4SLinus Torvalds 231786872cb5SThomas Graf err = rtm_to_fib6_config(skb, nlh, &cfg); 231886872cb5SThomas Graf if (err < 0) 231986872cb5SThomas Graf return err; 232086872cb5SThomas Graf 232186872cb5SThomas Graf return ip6_route_add(&cfg); 23221da177e4SLinus Torvalds } 23231da177e4SLinus Torvalds 2324339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void) 2325339bf98fSThomas Graf { 2326339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 2327339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 2328339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 2329339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 2330339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 2331339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 2332339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 2333339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 2334339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 23356a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 2336339bf98fSThomas Graf + nla_total_size(sizeof(struct rta_cacheinfo)); 2337339bf98fSThomas Graf } 2338339bf98fSThomas Graf 2339191cd582SBrian Haley static int rt6_fill_node(struct net *net, 2340191cd582SBrian Haley struct sk_buff *skb, struct rt6_info *rt, 23410d51aa80SJamal Hadi Salim struct in6_addr *dst, struct in6_addr *src, 23420d51aa80SJamal Hadi Salim int iif, int type, u32 pid, u32 seq, 23437bc570c8SYOSHIFUJI Hideaki int prefix, int nowait, unsigned int flags) 23441da177e4SLinus Torvalds { 23451da177e4SLinus Torvalds struct rtmsg *rtm; 23461da177e4SLinus Torvalds struct nlmsghdr *nlh; 2347e3703b3dSThomas Graf long expires; 23489e762a4aSPatrick McHardy u32 table; 2349f2c31e32SEric Dumazet struct neighbour *n; 23501da177e4SLinus Torvalds 23511da177e4SLinus Torvalds if (prefix) { /* user wants prefix routes only */ 23521da177e4SLinus Torvalds if (!(rt->rt6i_flags & RTF_PREFIX_RT)) { 23531da177e4SLinus Torvalds /* success since this is not a prefix route */ 23541da177e4SLinus Torvalds return 1; 23551da177e4SLinus Torvalds } 23561da177e4SLinus Torvalds } 23571da177e4SLinus Torvalds 23582d7202bfSThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags); 23592d7202bfSThomas Graf if (nlh == NULL) 236026932566SPatrick McHardy return -EMSGSIZE; 23612d7202bfSThomas Graf 23622d7202bfSThomas Graf rtm = nlmsg_data(nlh); 23631da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 23641da177e4SLinus Torvalds rtm->rtm_dst_len = rt->rt6i_dst.plen; 23651da177e4SLinus Torvalds rtm->rtm_src_len = rt->rt6i_src.plen; 23661da177e4SLinus Torvalds rtm->rtm_tos = 0; 2367c71099acSThomas Graf if (rt->rt6i_table) 23689e762a4aSPatrick McHardy table = rt->rt6i_table->tb6_id; 2369c71099acSThomas Graf else 23709e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 23719e762a4aSPatrick McHardy rtm->rtm_table = table; 23722d7202bfSThomas Graf NLA_PUT_U32(skb, RTA_TABLE, table); 23731da177e4SLinus Torvalds if (rt->rt6i_flags&RTF_REJECT) 23741da177e4SLinus Torvalds rtm->rtm_type = RTN_UNREACHABLE; 2375ab79ad14SMaciej Żenczykowski else if (rt->rt6i_flags&RTF_LOCAL) 2376ab79ad14SMaciej Żenczykowski rtm->rtm_type = RTN_LOCAL; 23771da177e4SLinus Torvalds else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK)) 23781da177e4SLinus Torvalds rtm->rtm_type = RTN_LOCAL; 23791da177e4SLinus Torvalds else 23801da177e4SLinus Torvalds rtm->rtm_type = RTN_UNICAST; 23811da177e4SLinus Torvalds rtm->rtm_flags = 0; 23821da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 23831da177e4SLinus Torvalds rtm->rtm_protocol = rt->rt6i_protocol; 23841da177e4SLinus Torvalds if (rt->rt6i_flags&RTF_DYNAMIC) 23851da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_REDIRECT; 23861da177e4SLinus Torvalds else if (rt->rt6i_flags & RTF_ADDRCONF) 23871da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_KERNEL; 23881da177e4SLinus Torvalds else if (rt->rt6i_flags&RTF_DEFAULT) 23891da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_RA; 23901da177e4SLinus Torvalds 23911da177e4SLinus Torvalds if (rt->rt6i_flags&RTF_CACHE) 23921da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 23931da177e4SLinus Torvalds 23941da177e4SLinus Torvalds if (dst) { 23952d7202bfSThomas Graf NLA_PUT(skb, RTA_DST, 16, dst); 23961da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 23971da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 23982d7202bfSThomas Graf NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr); 23991da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 24001da177e4SLinus Torvalds if (src) { 24012d7202bfSThomas Graf NLA_PUT(skb, RTA_SRC, 16, src); 24021da177e4SLinus Torvalds rtm->rtm_src_len = 128; 24031da177e4SLinus Torvalds } else if (rtm->rtm_src_len) 24042d7202bfSThomas Graf NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr); 24051da177e4SLinus Torvalds #endif 24067bc570c8SYOSHIFUJI Hideaki if (iif) { 24077bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 24087bc570c8SYOSHIFUJI Hideaki if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) { 24098229efdaSBenjamin Thery int err = ip6mr_get_route(net, skb, rtm, nowait); 24107bc570c8SYOSHIFUJI Hideaki if (err <= 0) { 24117bc570c8SYOSHIFUJI Hideaki if (!nowait) { 24127bc570c8SYOSHIFUJI Hideaki if (err == 0) 24137bc570c8SYOSHIFUJI Hideaki return 0; 24147bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 24157bc570c8SYOSHIFUJI Hideaki } else { 24167bc570c8SYOSHIFUJI Hideaki if (err == -EMSGSIZE) 24177bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 24187bc570c8SYOSHIFUJI Hideaki } 24197bc570c8SYOSHIFUJI Hideaki } 24207bc570c8SYOSHIFUJI Hideaki } else 24217bc570c8SYOSHIFUJI Hideaki #endif 24222d7202bfSThomas Graf NLA_PUT_U32(skb, RTA_IIF, iif); 24237bc570c8SYOSHIFUJI Hideaki } else if (dst) { 24241da177e4SLinus Torvalds struct in6_addr saddr_buf; 2425c3968a85SDaniel Walter if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0) 2426c3968a85SDaniel Walter NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf); 2427c3968a85SDaniel Walter } 2428c3968a85SDaniel Walter 2429c3968a85SDaniel Walter if (rt->rt6i_prefsrc.plen) { 2430c3968a85SDaniel Walter struct in6_addr saddr_buf; 2431c3968a85SDaniel Walter ipv6_addr_copy(&saddr_buf, &rt->rt6i_prefsrc.addr); 24322d7202bfSThomas Graf NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf); 24331da177e4SLinus Torvalds } 24342d7202bfSThomas Graf 2435defb3519SDavid S. Miller if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0) 24362d7202bfSThomas Graf goto nla_put_failure; 24372d7202bfSThomas Graf 2438f2c31e32SEric Dumazet rcu_read_lock(); 2439f2c31e32SEric Dumazet n = dst_get_neighbour(&rt->dst); 2440f2c31e32SEric Dumazet if (n) 2441f2c31e32SEric Dumazet NLA_PUT(skb, RTA_GATEWAY, 16, &n->primary_key); 2442f2c31e32SEric Dumazet rcu_read_unlock(); 24432d7202bfSThomas Graf 2444d8d1f30bSChangli Gao if (rt->dst.dev) 24452d7202bfSThomas Graf NLA_PUT_U32(skb, RTA_OIF, rt->rt6i_dev->ifindex); 24462d7202bfSThomas Graf 24472d7202bfSThomas Graf NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric); 2448e3703b3dSThomas Graf 244936e3deaeSYOSHIFUJI Hideaki if (!(rt->rt6i_flags & RTF_EXPIRES)) 245036e3deaeSYOSHIFUJI Hideaki expires = 0; 245136e3deaeSYOSHIFUJI Hideaki else if (rt->rt6i_expires - jiffies < INT_MAX) 245236e3deaeSYOSHIFUJI Hideaki expires = rt->rt6i_expires - jiffies; 245336e3deaeSYOSHIFUJI Hideaki else 245436e3deaeSYOSHIFUJI Hideaki expires = INT_MAX; 245569cdf8f9SYOSHIFUJI Hideaki 2456d8d1f30bSChangli Gao if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0, 2457d8d1f30bSChangli Gao expires, rt->dst.error) < 0) 2458e3703b3dSThomas Graf goto nla_put_failure; 24591da177e4SLinus Torvalds 24602d7202bfSThomas Graf return nlmsg_end(skb, nlh); 24612d7202bfSThomas Graf 24622d7202bfSThomas Graf nla_put_failure: 246326932566SPatrick McHardy nlmsg_cancel(skb, nlh); 246426932566SPatrick McHardy return -EMSGSIZE; 24651da177e4SLinus Torvalds } 24661da177e4SLinus Torvalds 24671b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg) 24681da177e4SLinus Torvalds { 24691da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 24701da177e4SLinus Torvalds int prefix; 24711da177e4SLinus Torvalds 24722d7202bfSThomas Graf if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { 24732d7202bfSThomas Graf struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); 24741da177e4SLinus Torvalds prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0; 24751da177e4SLinus Torvalds } else 24761da177e4SLinus Torvalds prefix = 0; 24771da177e4SLinus Torvalds 2478191cd582SBrian Haley return rt6_fill_node(arg->net, 2479191cd582SBrian Haley arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE, 24801da177e4SLinus Torvalds NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq, 24817bc570c8SYOSHIFUJI Hideaki prefix, 0, NLM_F_MULTI); 24821da177e4SLinus Torvalds } 24831da177e4SLinus Torvalds 2484c127ea2cSThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg) 24851da177e4SLinus Torvalds { 24863b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 2487ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 24881da177e4SLinus Torvalds struct rt6_info *rt; 2489ab364a6fSThomas Graf struct sk_buff *skb; 2490ab364a6fSThomas Graf struct rtmsg *rtm; 24914c9483b2SDavid S. Miller struct flowi6 fl6; 2492ab364a6fSThomas Graf int err, iif = 0; 2493ab364a6fSThomas Graf 2494ab364a6fSThomas Graf err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy); 2495ab364a6fSThomas Graf if (err < 0) 2496ab364a6fSThomas Graf goto errout; 2497ab364a6fSThomas Graf 2498ab364a6fSThomas Graf err = -EINVAL; 24994c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 2500ab364a6fSThomas Graf 2501ab364a6fSThomas Graf if (tb[RTA_SRC]) { 2502ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 2503ab364a6fSThomas Graf goto errout; 2504ab364a6fSThomas Graf 25054c9483b2SDavid S. Miller ipv6_addr_copy(&fl6.saddr, nla_data(tb[RTA_SRC])); 2506ab364a6fSThomas Graf } 2507ab364a6fSThomas Graf 2508ab364a6fSThomas Graf if (tb[RTA_DST]) { 2509ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 2510ab364a6fSThomas Graf goto errout; 2511ab364a6fSThomas Graf 25124c9483b2SDavid S. Miller ipv6_addr_copy(&fl6.daddr, nla_data(tb[RTA_DST])); 2513ab364a6fSThomas Graf } 2514ab364a6fSThomas Graf 2515ab364a6fSThomas Graf if (tb[RTA_IIF]) 2516ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 2517ab364a6fSThomas Graf 2518ab364a6fSThomas Graf if (tb[RTA_OIF]) 25194c9483b2SDavid S. Miller fl6.flowi6_oif = nla_get_u32(tb[RTA_OIF]); 2520ab364a6fSThomas Graf 2521ab364a6fSThomas Graf if (iif) { 2522ab364a6fSThomas Graf struct net_device *dev; 25235578689aSDaniel Lezcano dev = __dev_get_by_index(net, iif); 2524ab364a6fSThomas Graf if (!dev) { 2525ab364a6fSThomas Graf err = -ENODEV; 2526ab364a6fSThomas Graf goto errout; 2527ab364a6fSThomas Graf } 2528ab364a6fSThomas Graf } 25291da177e4SLinus Torvalds 25301da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 2531ab364a6fSThomas Graf if (skb == NULL) { 2532ab364a6fSThomas Graf err = -ENOBUFS; 2533ab364a6fSThomas Graf goto errout; 2534ab364a6fSThomas Graf } 25351da177e4SLinus Torvalds 25361da177e4SLinus Torvalds /* Reserve room for dummy headers, this skb can pass 25371da177e4SLinus Torvalds through good chunk of routing engine. 25381da177e4SLinus Torvalds */ 2539459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 25401da177e4SLinus Torvalds skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr)); 25411da177e4SLinus Torvalds 25424c9483b2SDavid S. Miller rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl6); 2543d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 25441da177e4SLinus Torvalds 25454c9483b2SDavid S. Miller err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif, 25461da177e4SLinus Torvalds RTM_NEWROUTE, NETLINK_CB(in_skb).pid, 25477bc570c8SYOSHIFUJI Hideaki nlh->nlmsg_seq, 0, 0, 0); 25481da177e4SLinus Torvalds if (err < 0) { 2549ab364a6fSThomas Graf kfree_skb(skb); 2550ab364a6fSThomas Graf goto errout; 25511da177e4SLinus Torvalds } 25521da177e4SLinus Torvalds 25535578689aSDaniel Lezcano err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid); 2554ab364a6fSThomas Graf errout: 25551da177e4SLinus Torvalds return err; 25561da177e4SLinus Torvalds } 25571da177e4SLinus Torvalds 255886872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info) 25591da177e4SLinus Torvalds { 25601da177e4SLinus Torvalds struct sk_buff *skb; 25615578689aSDaniel Lezcano struct net *net = info->nl_net; 2562528c4cebSDenis V. Lunev u32 seq; 2563528c4cebSDenis V. Lunev int err; 25640d51aa80SJamal Hadi Salim 2565528c4cebSDenis V. Lunev err = -ENOBUFS; 2566528c4cebSDenis V. Lunev seq = info->nlh != NULL ? info->nlh->nlmsg_seq : 0; 256786872cb5SThomas Graf 2568339bf98fSThomas Graf skb = nlmsg_new(rt6_nlmsg_size(), gfp_any()); 256921713ebcSThomas Graf if (skb == NULL) 257021713ebcSThomas Graf goto errout; 25711da177e4SLinus Torvalds 2572191cd582SBrian Haley err = rt6_fill_node(net, skb, rt, NULL, NULL, 0, 25737bc570c8SYOSHIFUJI Hideaki event, info->pid, seq, 0, 0, 0); 257426932566SPatrick McHardy if (err < 0) { 257526932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 257626932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 257726932566SPatrick McHardy kfree_skb(skb); 257826932566SPatrick McHardy goto errout; 257926932566SPatrick McHardy } 25801ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE, 25815578689aSDaniel Lezcano info->nlh, gfp_any()); 25821ce85fe4SPablo Neira Ayuso return; 258321713ebcSThomas Graf errout: 258421713ebcSThomas Graf if (err < 0) 25855578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 25861da177e4SLinus Torvalds } 25871da177e4SLinus Torvalds 25888ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 25898ed67789SDaniel Lezcano unsigned long event, void *data) 25908ed67789SDaniel Lezcano { 25918ed67789SDaniel Lezcano struct net_device *dev = (struct net_device *)data; 2592c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 25938ed67789SDaniel Lezcano 25948ed67789SDaniel Lezcano if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) { 2595d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 25968ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 25978ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 2598d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 25998ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 2600d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 26018ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 26028ed67789SDaniel Lezcano #endif 26038ed67789SDaniel Lezcano } 26048ed67789SDaniel Lezcano 26058ed67789SDaniel Lezcano return NOTIFY_OK; 26068ed67789SDaniel Lezcano } 26078ed67789SDaniel Lezcano 26081da177e4SLinus Torvalds /* 26091da177e4SLinus Torvalds * /proc 26101da177e4SLinus Torvalds */ 26111da177e4SLinus Torvalds 26121da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 26131da177e4SLinus Torvalds 26141da177e4SLinus Torvalds struct rt6_proc_arg 26151da177e4SLinus Torvalds { 26161da177e4SLinus Torvalds char *buffer; 26171da177e4SLinus Torvalds int offset; 26181da177e4SLinus Torvalds int length; 26191da177e4SLinus Torvalds int skip; 26201da177e4SLinus Torvalds int len; 26211da177e4SLinus Torvalds }; 26221da177e4SLinus Torvalds 26231da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg) 26241da177e4SLinus Torvalds { 262533120b30SAlexey Dobriyan struct seq_file *m = p_arg; 262669cce1d1SDavid S. Miller struct neighbour *n; 26271da177e4SLinus Torvalds 26284b7a4274SHarvey Harrison seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen); 26291da177e4SLinus Torvalds 26301da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 26314b7a4274SHarvey Harrison seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen); 26321da177e4SLinus Torvalds #else 263333120b30SAlexey Dobriyan seq_puts(m, "00000000000000000000000000000000 00 "); 26341da177e4SLinus Torvalds #endif 2635f2c31e32SEric Dumazet rcu_read_lock(); 263669cce1d1SDavid S. Miller n = dst_get_neighbour(&rt->dst); 263769cce1d1SDavid S. Miller if (n) { 263869cce1d1SDavid S. Miller seq_printf(m, "%pi6", n->primary_key); 26391da177e4SLinus Torvalds } else { 264033120b30SAlexey Dobriyan seq_puts(m, "00000000000000000000000000000000"); 26411da177e4SLinus Torvalds } 2642f2c31e32SEric Dumazet rcu_read_unlock(); 264333120b30SAlexey Dobriyan seq_printf(m, " %08x %08x %08x %08x %8s\n", 2644d8d1f30bSChangli Gao rt->rt6i_metric, atomic_read(&rt->dst.__refcnt), 2645d8d1f30bSChangli Gao rt->dst.__use, rt->rt6i_flags, 26461da177e4SLinus Torvalds rt->rt6i_dev ? rt->rt6i_dev->name : ""); 26471da177e4SLinus Torvalds return 0; 26481da177e4SLinus Torvalds } 26491da177e4SLinus Torvalds 265033120b30SAlexey Dobriyan static int ipv6_route_show(struct seq_file *m, void *v) 26511da177e4SLinus Torvalds { 2652f3db4851SDaniel Lezcano struct net *net = (struct net *)m->private; 2653f3db4851SDaniel Lezcano fib6_clean_all(net, rt6_info_route, 0, m); 265433120b30SAlexey Dobriyan return 0; 26551da177e4SLinus Torvalds } 26561da177e4SLinus Torvalds 265733120b30SAlexey Dobriyan static int ipv6_route_open(struct inode *inode, struct file *file) 265833120b30SAlexey Dobriyan { 2659de05c557SPavel Emelyanov return single_open_net(inode, file, ipv6_route_show); 2660f3db4851SDaniel Lezcano } 2661f3db4851SDaniel Lezcano 266233120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = { 266333120b30SAlexey Dobriyan .owner = THIS_MODULE, 266433120b30SAlexey Dobriyan .open = ipv6_route_open, 266533120b30SAlexey Dobriyan .read = seq_read, 266633120b30SAlexey Dobriyan .llseek = seq_lseek, 2667b6fcbdb4SPavel Emelyanov .release = single_release_net, 266833120b30SAlexey Dobriyan }; 266933120b30SAlexey Dobriyan 26701da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 26711da177e4SLinus Torvalds { 267269ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 26731da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 267469ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 267569ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 267669ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_alloc, 267769ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 267869ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 2679fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 268069ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 26811da177e4SLinus Torvalds 26821da177e4SLinus Torvalds return 0; 26831da177e4SLinus Torvalds } 26841da177e4SLinus Torvalds 26851da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file) 26861da177e4SLinus Torvalds { 2687de05c557SPavel Emelyanov return single_open_net(inode, file, rt6_stats_seq_show); 268869ddb805SDaniel Lezcano } 268969ddb805SDaniel Lezcano 26909a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = { 26911da177e4SLinus Torvalds .owner = THIS_MODULE, 26921da177e4SLinus Torvalds .open = rt6_stats_seq_open, 26931da177e4SLinus Torvalds .read = seq_read, 26941da177e4SLinus Torvalds .llseek = seq_lseek, 2695b6fcbdb4SPavel Emelyanov .release = single_release_net, 26961da177e4SLinus Torvalds }; 26971da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 26981da177e4SLinus Torvalds 26991da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 27001da177e4SLinus Torvalds 27011da177e4SLinus Torvalds static 27028d65af78SAlexey Dobriyan int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write, 27031da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 27041da177e4SLinus Torvalds { 2705c486da34SLucian Adrian Grijincu struct net *net; 2706c486da34SLucian Adrian Grijincu int delay; 2707c486da34SLucian Adrian Grijincu if (!write) 2708c486da34SLucian Adrian Grijincu return -EINVAL; 2709c486da34SLucian Adrian Grijincu 2710c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 2711c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 27128d65af78SAlexey Dobriyan proc_dointvec(ctl, write, buffer, lenp, ppos); 27135b7c931dSDaniel Lezcano fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net); 27141da177e4SLinus Torvalds return 0; 27151da177e4SLinus Torvalds } 27161da177e4SLinus Torvalds 2717760f2d01SDaniel Lezcano ctl_table ipv6_route_table_template[] = { 27181da177e4SLinus Torvalds { 27191da177e4SLinus Torvalds .procname = "flush", 27204990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 27211da177e4SLinus Torvalds .maxlen = sizeof(int), 272289c8b3a1SDave Jones .mode = 0200, 27236d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 27241da177e4SLinus Torvalds }, 27251da177e4SLinus Torvalds { 27261da177e4SLinus Torvalds .procname = "gc_thresh", 27279a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 27281da177e4SLinus Torvalds .maxlen = sizeof(int), 27291da177e4SLinus Torvalds .mode = 0644, 27306d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 27311da177e4SLinus Torvalds }, 27321da177e4SLinus Torvalds { 27331da177e4SLinus Torvalds .procname = "max_size", 27344990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 27351da177e4SLinus Torvalds .maxlen = sizeof(int), 27361da177e4SLinus Torvalds .mode = 0644, 27376d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 27381da177e4SLinus Torvalds }, 27391da177e4SLinus Torvalds { 27401da177e4SLinus Torvalds .procname = "gc_min_interval", 27414990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 27421da177e4SLinus Torvalds .maxlen = sizeof(int), 27431da177e4SLinus Torvalds .mode = 0644, 27446d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 27451da177e4SLinus Torvalds }, 27461da177e4SLinus Torvalds { 27471da177e4SLinus Torvalds .procname = "gc_timeout", 27484990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 27491da177e4SLinus Torvalds .maxlen = sizeof(int), 27501da177e4SLinus Torvalds .mode = 0644, 27516d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 27521da177e4SLinus Torvalds }, 27531da177e4SLinus Torvalds { 27541da177e4SLinus Torvalds .procname = "gc_interval", 27554990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 27561da177e4SLinus Torvalds .maxlen = sizeof(int), 27571da177e4SLinus Torvalds .mode = 0644, 27586d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 27591da177e4SLinus Torvalds }, 27601da177e4SLinus Torvalds { 27611da177e4SLinus Torvalds .procname = "gc_elasticity", 27624990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 27631da177e4SLinus Torvalds .maxlen = sizeof(int), 27641da177e4SLinus Torvalds .mode = 0644, 2765f3d3f616SMin Zhang .proc_handler = proc_dointvec, 27661da177e4SLinus Torvalds }, 27671da177e4SLinus Torvalds { 27681da177e4SLinus Torvalds .procname = "mtu_expires", 27694990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 27701da177e4SLinus Torvalds .maxlen = sizeof(int), 27711da177e4SLinus Torvalds .mode = 0644, 27726d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 27731da177e4SLinus Torvalds }, 27741da177e4SLinus Torvalds { 27751da177e4SLinus Torvalds .procname = "min_adv_mss", 27764990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 27771da177e4SLinus Torvalds .maxlen = sizeof(int), 27781da177e4SLinus Torvalds .mode = 0644, 2779f3d3f616SMin Zhang .proc_handler = proc_dointvec, 27801da177e4SLinus Torvalds }, 27811da177e4SLinus Torvalds { 27821da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 27834990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 27841da177e4SLinus Torvalds .maxlen = sizeof(int), 27851da177e4SLinus Torvalds .mode = 0644, 27866d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 27871da177e4SLinus Torvalds }, 2788f8572d8fSEric W. Biederman { } 27891da177e4SLinus Torvalds }; 27901da177e4SLinus Torvalds 27912c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 2792760f2d01SDaniel Lezcano { 2793760f2d01SDaniel Lezcano struct ctl_table *table; 2794760f2d01SDaniel Lezcano 2795760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 2796760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 2797760f2d01SDaniel Lezcano GFP_KERNEL); 27985ee09105SYOSHIFUJI Hideaki 27995ee09105SYOSHIFUJI Hideaki if (table) { 28005ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 2801c486da34SLucian Adrian Grijincu table[0].extra1 = net; 280286393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 28035ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 28045ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 28055ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 28065ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 28075ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 28085ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 28095ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 28109c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 28115ee09105SYOSHIFUJI Hideaki } 28125ee09105SYOSHIFUJI Hideaki 2813760f2d01SDaniel Lezcano return table; 2814760f2d01SDaniel Lezcano } 28151da177e4SLinus Torvalds #endif 28161da177e4SLinus Torvalds 28172c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 2818cdb18761SDaniel Lezcano { 2819633d424bSPavel Emelyanov int ret = -ENOMEM; 28208ed67789SDaniel Lezcano 282186393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 282286393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 2823f2fc6a54SBenjamin Thery 2824fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 2825fc66f95cSEric Dumazet goto out_ip6_dst_ops; 2826fc66f95cSEric Dumazet 28278ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 28288ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 28298ed67789SDaniel Lezcano GFP_KERNEL); 28308ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 2831fc66f95cSEric Dumazet goto out_ip6_dst_entries; 2832d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.path = 28338ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_null_entry; 2834d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 283562fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_null_entry->dst, 283662fa8a84SDavid S. Miller ip6_template_metrics, true); 28378ed67789SDaniel Lezcano 28388ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 28398ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 28408ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 28418ed67789SDaniel Lezcano GFP_KERNEL); 284268fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 284368fffc67SPeter Zijlstra goto out_ip6_null_entry; 2844d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.path = 28458ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_prohibit_entry; 2846d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 284762fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, 284862fa8a84SDavid S. Miller ip6_template_metrics, true); 28498ed67789SDaniel Lezcano 28508ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 28518ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 28528ed67789SDaniel Lezcano GFP_KERNEL); 285368fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 285468fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 2855d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.path = 28568ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_blk_hole_entry; 2857d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 285862fa8a84SDavid S. Miller dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, 285962fa8a84SDavid S. Miller ip6_template_metrics, true); 28608ed67789SDaniel Lezcano #endif 28618ed67789SDaniel Lezcano 2862b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 2863b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 2864b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 2865b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 2866b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 2867b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 2868b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 2869b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 2870b339a47cSPeter Zijlstra 2871cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS 2872cdb18761SDaniel Lezcano proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops); 2873cdb18761SDaniel Lezcano proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops); 2874cdb18761SDaniel Lezcano #endif 28756891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 28766891a346SBenjamin Thery 28778ed67789SDaniel Lezcano ret = 0; 28788ed67789SDaniel Lezcano out: 28798ed67789SDaniel Lezcano return ret; 2880f2fc6a54SBenjamin Thery 288168fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 288268fffc67SPeter Zijlstra out_ip6_prohibit_entry: 288368fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 288468fffc67SPeter Zijlstra out_ip6_null_entry: 288568fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 288668fffc67SPeter Zijlstra #endif 2887fc66f95cSEric Dumazet out_ip6_dst_entries: 2888fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 2889f2fc6a54SBenjamin Thery out_ip6_dst_ops: 2890f2fc6a54SBenjamin Thery goto out; 2891cdb18761SDaniel Lezcano } 2892cdb18761SDaniel Lezcano 28932c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 2894cdb18761SDaniel Lezcano { 2895cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS 2896cdb18761SDaniel Lezcano proc_net_remove(net, "ipv6_route"); 2897cdb18761SDaniel Lezcano proc_net_remove(net, "rt6_stats"); 2898cdb18761SDaniel Lezcano #endif 28998ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 29008ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 29018ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 29028ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 29038ed67789SDaniel Lezcano #endif 290441bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 2905cdb18761SDaniel Lezcano } 2906cdb18761SDaniel Lezcano 2907cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 2908cdb18761SDaniel Lezcano .init = ip6_route_net_init, 2909cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 2910cdb18761SDaniel Lezcano }; 2911cdb18761SDaniel Lezcano 29128ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 29138ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 29148ed67789SDaniel Lezcano .priority = 0, 29158ed67789SDaniel Lezcano }; 29168ed67789SDaniel Lezcano 2917433d49c3SDaniel Lezcano int __init ip6_route_init(void) 29181da177e4SLinus Torvalds { 2919433d49c3SDaniel Lezcano int ret; 2920433d49c3SDaniel Lezcano 29219a7ec3a9SDaniel Lezcano ret = -ENOMEM; 29229a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 29239a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 29249a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 29259a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 2926c19a28e1SFernando Carrijo goto out; 292714e50e57SDavid S. Miller 2928fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 29298ed67789SDaniel Lezcano if (ret) 2930bdb3289fSDaniel Lezcano goto out_kmem_cache; 2931bdb3289fSDaniel Lezcano 2932fc66f95cSEric Dumazet ret = register_pernet_subsys(&ip6_route_net_ops); 2933fc66f95cSEric Dumazet if (ret) 2934fc66f95cSEric Dumazet goto out_dst_entries; 2935fc66f95cSEric Dumazet 29365dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 29375dc121e9SArnaud Ebalard 29388ed67789SDaniel Lezcano /* Registering of the loopback is done before this portion of code, 29398ed67789SDaniel Lezcano * the loopback reference in rt6_info will not be taken, do it 29408ed67789SDaniel Lezcano * manually for init_net */ 2941d8d1f30bSChangli Gao init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 29428ed67789SDaniel Lezcano init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 2943bdb3289fSDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 2944d8d1f30bSChangli Gao init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 29458ed67789SDaniel Lezcano init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 2946d8d1f30bSChangli Gao init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 29478ed67789SDaniel Lezcano init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 2948bdb3289fSDaniel Lezcano #endif 2949433d49c3SDaniel Lezcano ret = fib6_init(); 2950433d49c3SDaniel Lezcano if (ret) 29518ed67789SDaniel Lezcano goto out_register_subsys; 2952433d49c3SDaniel Lezcano 2953433d49c3SDaniel Lezcano ret = xfrm6_init(); 2954433d49c3SDaniel Lezcano if (ret) 2955cdb18761SDaniel Lezcano goto out_fib6_init; 2956c35b7e72SDaniel Lezcano 2957433d49c3SDaniel Lezcano ret = fib6_rules_init(); 2958433d49c3SDaniel Lezcano if (ret) 2959433d49c3SDaniel Lezcano goto xfrm6_init; 29607e5449c2SDaniel Lezcano 2961433d49c3SDaniel Lezcano ret = -ENOBUFS; 2962c7ac8679SGreg Rose if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) || 2963c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) || 2964c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL)) 2965433d49c3SDaniel Lezcano goto fib6_rules_init; 2966433d49c3SDaniel Lezcano 29678ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 2968cdb18761SDaniel Lezcano if (ret) 2969cdb18761SDaniel Lezcano goto fib6_rules_init; 29708ed67789SDaniel Lezcano 2971433d49c3SDaniel Lezcano out: 2972433d49c3SDaniel Lezcano return ret; 2973433d49c3SDaniel Lezcano 2974433d49c3SDaniel Lezcano fib6_rules_init: 2975433d49c3SDaniel Lezcano fib6_rules_cleanup(); 2976433d49c3SDaniel Lezcano xfrm6_init: 2977433d49c3SDaniel Lezcano xfrm6_fini(); 2978433d49c3SDaniel Lezcano out_fib6_init: 2979433d49c3SDaniel Lezcano fib6_gc_cleanup(); 29808ed67789SDaniel Lezcano out_register_subsys: 29818ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 2982fc66f95cSEric Dumazet out_dst_entries: 2983fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 2984433d49c3SDaniel Lezcano out_kmem_cache: 2985f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 2986433d49c3SDaniel Lezcano goto out; 29871da177e4SLinus Torvalds } 29881da177e4SLinus Torvalds 29891da177e4SLinus Torvalds void ip6_route_cleanup(void) 29901da177e4SLinus Torvalds { 29918ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 2992101367c2SThomas Graf fib6_rules_cleanup(); 29931da177e4SLinus Torvalds xfrm6_fini(); 29941da177e4SLinus Torvalds fib6_gc_cleanup(); 29958ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 299641bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 2997f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 29981da177e4SLinus Torvalds } 2999