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 751da177e4SLinus Torvalds static struct rt6_info * ip6_rt_copy(struct rt6_info *ort); 761da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie); 770dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst); 78d33e4553SDavid S. Miller static unsigned int ip6_default_mtu(const struct dst_entry *dst); 791da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *); 801da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *); 811da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *, 821da177e4SLinus Torvalds struct net_device *dev, int how); 83569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops); 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds static int ip6_pkt_discard(struct sk_buff *skb); 861da177e4SLinus Torvalds static int ip6_pkt_discard_out(struct sk_buff *skb); 871da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb); 881da177e4SLinus Torvalds static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu); 891da177e4SLinus Torvalds 9070ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 91efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 92efa2cea0SDaniel Lezcano struct in6_addr *prefix, int prefixlen, 9370ceb4f5SYOSHIFUJI Hideaki struct in6_addr *gwaddr, int ifindex, 9470ceb4f5SYOSHIFUJI Hideaki unsigned pref); 95efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 96efa2cea0SDaniel Lezcano struct in6_addr *prefix, int prefixlen, 9770ceb4f5SYOSHIFUJI Hideaki struct in6_addr *gwaddr, int ifindex); 9870ceb4f5SYOSHIFUJI Hideaki #endif 9970ceb4f5SYOSHIFUJI Hideaki 1009a7ec3a9SDaniel Lezcano static struct dst_ops ip6_dst_ops_template = { 1011da177e4SLinus Torvalds .family = AF_INET6, 10209640e63SHarvey Harrison .protocol = cpu_to_be16(ETH_P_IPV6), 1031da177e4SLinus Torvalds .gc = ip6_dst_gc, 1041da177e4SLinus Torvalds .gc_thresh = 1024, 1051da177e4SLinus Torvalds .check = ip6_dst_check, 1060dbaee3bSDavid S. Miller .default_advmss = ip6_default_advmss, 107d33e4553SDavid S. Miller .default_mtu = ip6_default_mtu, 1081da177e4SLinus Torvalds .destroy = ip6_dst_destroy, 1091da177e4SLinus Torvalds .ifdown = ip6_dst_ifdown, 1101da177e4SLinus Torvalds .negative_advice = ip6_negative_advice, 1111da177e4SLinus Torvalds .link_failure = ip6_link_failure, 1121da177e4SLinus Torvalds .update_pmtu = ip6_rt_update_pmtu, 1131ac06e03SHerbert Xu .local_out = __ip6_local_out, 1141da177e4SLinus Torvalds }; 1151da177e4SLinus Torvalds 116ec831ea7SRoland Dreier static unsigned int ip6_blackhole_default_mtu(const struct dst_entry *dst) 117ec831ea7SRoland Dreier { 118ec831ea7SRoland Dreier return 0; 119ec831ea7SRoland Dreier } 120ec831ea7SRoland Dreier 12114e50e57SDavid S. Miller static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu) 12214e50e57SDavid S. Miller { 12314e50e57SDavid S. Miller } 12414e50e57SDavid S. Miller 12514e50e57SDavid S. Miller static struct dst_ops ip6_dst_blackhole_ops = { 12614e50e57SDavid S. Miller .family = AF_INET6, 12709640e63SHarvey Harrison .protocol = cpu_to_be16(ETH_P_IPV6), 12814e50e57SDavid S. Miller .destroy = ip6_dst_destroy, 12914e50e57SDavid S. Miller .check = ip6_dst_check, 130ec831ea7SRoland Dreier .default_mtu = ip6_blackhole_default_mtu, 131214f45c9SEric Dumazet .default_advmss = ip6_default_advmss, 13214e50e57SDavid S. Miller .update_pmtu = ip6_rt_blackhole_update_pmtu, 13314e50e57SDavid S. Miller }; 13414e50e57SDavid S. Miller 135bdb3289fSDaniel Lezcano static struct rt6_info ip6_null_entry_template = { 1361da177e4SLinus Torvalds .dst = { 1371da177e4SLinus Torvalds .__refcnt = ATOMIC_INIT(1), 1381da177e4SLinus Torvalds .__use = 1, 1391da177e4SLinus Torvalds .obsolete = -1, 1401da177e4SLinus Torvalds .error = -ENETUNREACH, 1411da177e4SLinus Torvalds .input = ip6_pkt_discard, 1421da177e4SLinus Torvalds .output = ip6_pkt_discard_out, 1431da177e4SLinus Torvalds }, 1441da177e4SLinus Torvalds .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 1454f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 1461da177e4SLinus Torvalds .rt6i_metric = ~(u32) 0, 1471da177e4SLinus Torvalds .rt6i_ref = ATOMIC_INIT(1), 1481da177e4SLinus Torvalds }; 1491da177e4SLinus Torvalds 150101367c2SThomas Graf #ifdef CONFIG_IPV6_MULTIPLE_TABLES 151101367c2SThomas Graf 1526723ab54SDavid S. Miller static int ip6_pkt_prohibit(struct sk_buff *skb); 1536723ab54SDavid S. Miller static int ip6_pkt_prohibit_out(struct sk_buff *skb); 1546723ab54SDavid S. Miller 155280a34c8SAdrian Bunk static struct rt6_info ip6_prohibit_entry_template = { 156101367c2SThomas Graf .dst = { 157101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 158101367c2SThomas Graf .__use = 1, 159101367c2SThomas Graf .obsolete = -1, 160101367c2SThomas Graf .error = -EACCES, 1619ce8ade0SThomas Graf .input = ip6_pkt_prohibit, 1629ce8ade0SThomas Graf .output = ip6_pkt_prohibit_out, 163101367c2SThomas Graf }, 164101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 1654f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 166101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 167101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 168101367c2SThomas Graf }; 169101367c2SThomas Graf 170bdb3289fSDaniel Lezcano static struct rt6_info ip6_blk_hole_entry_template = { 171101367c2SThomas Graf .dst = { 172101367c2SThomas Graf .__refcnt = ATOMIC_INIT(1), 173101367c2SThomas Graf .__use = 1, 174101367c2SThomas Graf .obsolete = -1, 175101367c2SThomas Graf .error = -EINVAL, 176352e512cSHerbert Xu .input = dst_discard, 177352e512cSHerbert Xu .output = dst_discard, 178101367c2SThomas Graf }, 179101367c2SThomas Graf .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 1804f724279SJean-Mickael Guerin .rt6i_protocol = RTPROT_KERNEL, 181101367c2SThomas Graf .rt6i_metric = ~(u32) 0, 182101367c2SThomas Graf .rt6i_ref = ATOMIC_INIT(1), 183101367c2SThomas Graf }; 184101367c2SThomas Graf 185101367c2SThomas Graf #endif 186101367c2SThomas Graf 1871da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */ 188f2fc6a54SBenjamin Thery static inline struct rt6_info *ip6_dst_alloc(struct dst_ops *ops) 1891da177e4SLinus Torvalds { 190f2fc6a54SBenjamin Thery return (struct rt6_info *)dst_alloc(ops); 1911da177e4SLinus Torvalds } 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst) 1941da177e4SLinus Torvalds { 1951da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 1961da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 197b3419363SDavid S. Miller struct inet_peer *peer = rt->rt6i_peer; 1981da177e4SLinus Torvalds 1991da177e4SLinus Torvalds if (idev != NULL) { 2001da177e4SLinus Torvalds rt->rt6i_idev = NULL; 2011da177e4SLinus Torvalds in6_dev_put(idev); 2021da177e4SLinus Torvalds } 203b3419363SDavid S. Miller if (peer) { 204b3419363SDavid S. Miller rt->rt6i_peer = NULL; 205b3419363SDavid S. Miller inet_putpeer(peer); 206b3419363SDavid S. Miller } 207b3419363SDavid S. Miller } 208b3419363SDavid S. Miller 209b3419363SDavid S. Miller void rt6_bind_peer(struct rt6_info *rt, int create) 210b3419363SDavid S. Miller { 211b3419363SDavid S. Miller struct inet_peer *peer; 212b3419363SDavid S. Miller 213b3419363SDavid S. Miller peer = inet_getpeer_v6(&rt->rt6i_dst.addr, create); 214b3419363SDavid S. Miller if (peer && cmpxchg(&rt->rt6i_peer, NULL, peer) != NULL) 215b3419363SDavid S. Miller inet_putpeer(peer); 2161da177e4SLinus Torvalds } 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 2191da177e4SLinus Torvalds int how) 2201da177e4SLinus Torvalds { 2211da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 2221da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 2235a3e55d6SDenis V. Lunev struct net_device *loopback_dev = 224c346dca1SYOSHIFUJI Hideaki dev_net(dev)->loopback_dev; 2251da177e4SLinus Torvalds 2265a3e55d6SDenis V. Lunev if (dev != loopback_dev && idev != NULL && idev->dev == dev) { 2275a3e55d6SDenis V. Lunev struct inet6_dev *loopback_idev = 2285a3e55d6SDenis V. Lunev in6_dev_get(loopback_dev); 2291da177e4SLinus Torvalds if (loopback_idev != NULL) { 2301da177e4SLinus Torvalds rt->rt6i_idev = loopback_idev; 2311da177e4SLinus Torvalds in6_dev_put(idev); 2321da177e4SLinus Torvalds } 2331da177e4SLinus Torvalds } 2341da177e4SLinus Torvalds } 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds static __inline__ int rt6_check_expired(const struct rt6_info *rt) 2371da177e4SLinus Torvalds { 238a02cec21SEric Dumazet return (rt->rt6i_flags & RTF_EXPIRES) && 239a02cec21SEric Dumazet time_after(jiffies, rt->rt6i_expires); 2401da177e4SLinus Torvalds } 2411da177e4SLinus Torvalds 242c71099acSThomas Graf static inline int rt6_need_strict(struct in6_addr *daddr) 243c71099acSThomas Graf { 244a02cec21SEric Dumazet return ipv6_addr_type(daddr) & 245a02cec21SEric Dumazet (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK); 246c71099acSThomas Graf } 247c71099acSThomas Graf 2481da177e4SLinus Torvalds /* 249c71099acSThomas Graf * Route lookup. Any table->tb6_lock is implied. 2501da177e4SLinus Torvalds */ 2511da177e4SLinus Torvalds 2528ed67789SDaniel Lezcano static inline struct rt6_info *rt6_device_match(struct net *net, 2538ed67789SDaniel Lezcano struct rt6_info *rt, 254dd3abc4eSYOSHIFUJI Hideaki struct in6_addr *saddr, 2551da177e4SLinus Torvalds int oif, 256d420895eSYOSHIFUJI Hideaki int flags) 2571da177e4SLinus Torvalds { 2581da177e4SLinus Torvalds struct rt6_info *local = NULL; 2591da177e4SLinus Torvalds struct rt6_info *sprt; 2601da177e4SLinus Torvalds 261dd3abc4eSYOSHIFUJI Hideaki if (!oif && ipv6_addr_any(saddr)) 262dd3abc4eSYOSHIFUJI Hideaki goto out; 263dd3abc4eSYOSHIFUJI Hideaki 264d8d1f30bSChangli Gao for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) { 2651da177e4SLinus Torvalds struct net_device *dev = sprt->rt6i_dev; 266dd3abc4eSYOSHIFUJI Hideaki 267dd3abc4eSYOSHIFUJI Hideaki if (oif) { 2681da177e4SLinus Torvalds if (dev->ifindex == oif) 2691da177e4SLinus Torvalds return sprt; 2701da177e4SLinus Torvalds if (dev->flags & IFF_LOOPBACK) { 2711da177e4SLinus Torvalds if (sprt->rt6i_idev == NULL || 2721da177e4SLinus Torvalds sprt->rt6i_idev->dev->ifindex != oif) { 273d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE && oif) 2741da177e4SLinus Torvalds continue; 2751da177e4SLinus Torvalds if (local && (!oif || 2761da177e4SLinus Torvalds local->rt6i_idev->dev->ifindex == oif)) 2771da177e4SLinus Torvalds continue; 2781da177e4SLinus Torvalds } 2791da177e4SLinus Torvalds local = sprt; 2801da177e4SLinus Torvalds } 281dd3abc4eSYOSHIFUJI Hideaki } else { 282dd3abc4eSYOSHIFUJI Hideaki if (ipv6_chk_addr(net, saddr, dev, 283dd3abc4eSYOSHIFUJI Hideaki flags & RT6_LOOKUP_F_IFACE)) 284dd3abc4eSYOSHIFUJI Hideaki return sprt; 285dd3abc4eSYOSHIFUJI Hideaki } 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds 288dd3abc4eSYOSHIFUJI Hideaki if (oif) { 2891da177e4SLinus Torvalds if (local) 2901da177e4SLinus Torvalds return local; 2911da177e4SLinus Torvalds 292d420895eSYOSHIFUJI Hideaki if (flags & RT6_LOOKUP_F_IFACE) 2938ed67789SDaniel Lezcano return net->ipv6.ip6_null_entry; 2941da177e4SLinus Torvalds } 295dd3abc4eSYOSHIFUJI Hideaki out: 2961da177e4SLinus Torvalds return rt; 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds 29927097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 30027097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt) 30127097255SYOSHIFUJI Hideaki { 30227097255SYOSHIFUJI Hideaki struct neighbour *neigh = rt ? rt->rt6i_nexthop : NULL; 30327097255SYOSHIFUJI Hideaki /* 30427097255SYOSHIFUJI Hideaki * Okay, this does not seem to be appropriate 30527097255SYOSHIFUJI Hideaki * for now, however, we need to check if it 30627097255SYOSHIFUJI Hideaki * is really so; aka Router Reachability Probing. 30727097255SYOSHIFUJI Hideaki * 30827097255SYOSHIFUJI Hideaki * Router Reachability Probe MUST be rate-limited 30927097255SYOSHIFUJI Hideaki * to no more than one per minute. 31027097255SYOSHIFUJI Hideaki */ 31127097255SYOSHIFUJI Hideaki if (!neigh || (neigh->nud_state & NUD_VALID)) 31227097255SYOSHIFUJI Hideaki return; 31327097255SYOSHIFUJI Hideaki read_lock_bh(&neigh->lock); 31427097255SYOSHIFUJI Hideaki if (!(neigh->nud_state & NUD_VALID) && 31552e16356SYOSHIFUJI Hideaki time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) { 31627097255SYOSHIFUJI Hideaki struct in6_addr mcaddr; 31727097255SYOSHIFUJI Hideaki struct in6_addr *target; 31827097255SYOSHIFUJI Hideaki 31927097255SYOSHIFUJI Hideaki neigh->updated = jiffies; 32027097255SYOSHIFUJI Hideaki read_unlock_bh(&neigh->lock); 32127097255SYOSHIFUJI Hideaki 32227097255SYOSHIFUJI Hideaki target = (struct in6_addr *)&neigh->primary_key; 32327097255SYOSHIFUJI Hideaki addrconf_addr_solict_mult(target, &mcaddr); 32427097255SYOSHIFUJI Hideaki ndisc_send_ns(rt->rt6i_dev, NULL, target, &mcaddr, NULL); 32527097255SYOSHIFUJI Hideaki } else 32627097255SYOSHIFUJI Hideaki read_unlock_bh(&neigh->lock); 32727097255SYOSHIFUJI Hideaki } 32827097255SYOSHIFUJI Hideaki #else 32927097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt) 33027097255SYOSHIFUJI Hideaki { 33127097255SYOSHIFUJI Hideaki } 33227097255SYOSHIFUJI Hideaki #endif 33327097255SYOSHIFUJI Hideaki 3341da177e4SLinus Torvalds /* 335554cfb7eSYOSHIFUJI Hideaki * Default Router Selection (RFC 2461 6.3.6) 3361da177e4SLinus Torvalds */ 337b6f99a21SDave Jones static inline int rt6_check_dev(struct rt6_info *rt, int oif) 3381da177e4SLinus Torvalds { 339554cfb7eSYOSHIFUJI Hideaki struct net_device *dev = rt->rt6i_dev; 340161980f4SDavid S. Miller if (!oif || dev->ifindex == oif) 341554cfb7eSYOSHIFUJI Hideaki return 2; 342161980f4SDavid S. Miller if ((dev->flags & IFF_LOOPBACK) && 343161980f4SDavid S. Miller rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif) 344161980f4SDavid S. Miller return 1; 345554cfb7eSYOSHIFUJI Hideaki return 0; 3461da177e4SLinus Torvalds } 3471da177e4SLinus Torvalds 348b6f99a21SDave Jones static inline int rt6_check_neigh(struct rt6_info *rt) 3491da177e4SLinus Torvalds { 350554cfb7eSYOSHIFUJI Hideaki struct neighbour *neigh = rt->rt6i_nexthop; 351398bcbebSYOSHIFUJI Hideaki int m; 3524d0c5911SYOSHIFUJI Hideaki if (rt->rt6i_flags & RTF_NONEXTHOP || 3534d0c5911SYOSHIFUJI Hideaki !(rt->rt6i_flags & RTF_GATEWAY)) 3544d0c5911SYOSHIFUJI Hideaki m = 1; 3554d0c5911SYOSHIFUJI Hideaki else if (neigh) { 3561da177e4SLinus Torvalds read_lock_bh(&neigh->lock); 357554cfb7eSYOSHIFUJI Hideaki if (neigh->nud_state & NUD_VALID) 3584d0c5911SYOSHIFUJI Hideaki m = 2; 359398bcbebSYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 360398bcbebSYOSHIFUJI Hideaki else if (neigh->nud_state & NUD_FAILED) 361398bcbebSYOSHIFUJI Hideaki m = 0; 362398bcbebSYOSHIFUJI Hideaki #endif 363398bcbebSYOSHIFUJI Hideaki else 364ea73ee23SYOSHIFUJI Hideaki m = 1; 3651da177e4SLinus Torvalds read_unlock_bh(&neigh->lock); 366398bcbebSYOSHIFUJI Hideaki } else 367398bcbebSYOSHIFUJI Hideaki m = 0; 368554cfb7eSYOSHIFUJI Hideaki return m; 3691da177e4SLinus Torvalds } 3701da177e4SLinus Torvalds 371554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif, 372554cfb7eSYOSHIFUJI Hideaki int strict) 373554cfb7eSYOSHIFUJI Hideaki { 3744d0c5911SYOSHIFUJI Hideaki int m, n; 3754d0c5911SYOSHIFUJI Hideaki 3764d0c5911SYOSHIFUJI Hideaki m = rt6_check_dev(rt, oif); 37777d16f45SYOSHIFUJI Hideaki if (!m && (strict & RT6_LOOKUP_F_IFACE)) 378554cfb7eSYOSHIFUJI Hideaki return -1; 379ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 380ebacaaa0SYOSHIFUJI Hideaki m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2; 381ebacaaa0SYOSHIFUJI Hideaki #endif 3824d0c5911SYOSHIFUJI Hideaki n = rt6_check_neigh(rt); 383557e92efSYOSHIFUJI Hideaki if (!n && (strict & RT6_LOOKUP_F_REACHABLE)) 384554cfb7eSYOSHIFUJI Hideaki return -1; 385554cfb7eSYOSHIFUJI Hideaki return m; 386554cfb7eSYOSHIFUJI Hideaki } 387554cfb7eSYOSHIFUJI Hideaki 388f11e6659SDavid S. Miller static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict, 389f11e6659SDavid S. Miller int *mpri, struct rt6_info *match) 390554cfb7eSYOSHIFUJI Hideaki { 391554cfb7eSYOSHIFUJI Hideaki int m; 392554cfb7eSYOSHIFUJI Hideaki 393554cfb7eSYOSHIFUJI Hideaki if (rt6_check_expired(rt)) 394f11e6659SDavid S. Miller goto out; 395554cfb7eSYOSHIFUJI Hideaki 396554cfb7eSYOSHIFUJI Hideaki m = rt6_score_route(rt, oif, strict); 397554cfb7eSYOSHIFUJI Hideaki if (m < 0) 398f11e6659SDavid S. Miller goto out; 399554cfb7eSYOSHIFUJI Hideaki 400f11e6659SDavid S. Miller if (m > *mpri) { 401ea659e07SYOSHIFUJI Hideaki if (strict & RT6_LOOKUP_F_REACHABLE) 40227097255SYOSHIFUJI Hideaki rt6_probe(match); 403f11e6659SDavid S. Miller *mpri = m; 404554cfb7eSYOSHIFUJI Hideaki match = rt; 405ea659e07SYOSHIFUJI Hideaki } else if (strict & RT6_LOOKUP_F_REACHABLE) { 40627097255SYOSHIFUJI Hideaki rt6_probe(rt); 4071da177e4SLinus Torvalds } 408f11e6659SDavid S. Miller 409f11e6659SDavid S. Miller out: 410f11e6659SDavid S. Miller return match; 4111da177e4SLinus Torvalds } 4121da177e4SLinus Torvalds 413f11e6659SDavid S. Miller static struct rt6_info *find_rr_leaf(struct fib6_node *fn, 414f11e6659SDavid S. Miller struct rt6_info *rr_head, 415f11e6659SDavid S. Miller u32 metric, int oif, int strict) 416f11e6659SDavid S. Miller { 417f11e6659SDavid S. Miller struct rt6_info *rt, *match; 418f11e6659SDavid S. Miller int mpri = -1; 419f11e6659SDavid S. Miller 420f11e6659SDavid S. Miller match = NULL; 421f11e6659SDavid S. Miller for (rt = rr_head; rt && rt->rt6i_metric == metric; 422d8d1f30bSChangli Gao rt = rt->dst.rt6_next) 423f11e6659SDavid S. Miller match = find_match(rt, oif, strict, &mpri, match); 424f11e6659SDavid S. Miller for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric; 425d8d1f30bSChangli Gao rt = rt->dst.rt6_next) 426f11e6659SDavid S. Miller match = find_match(rt, oif, strict, &mpri, match); 427f11e6659SDavid S. Miller 428f11e6659SDavid S. Miller return match; 429f11e6659SDavid S. Miller } 430f11e6659SDavid S. Miller 431f11e6659SDavid S. Miller static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict) 432f11e6659SDavid S. Miller { 433f11e6659SDavid S. Miller struct rt6_info *match, *rt0; 4348ed67789SDaniel Lezcano struct net *net; 435f11e6659SDavid S. Miller 436f11e6659SDavid S. Miller RT6_TRACE("%s(fn->leaf=%p, oif=%d)\n", 4370dc47877SHarvey Harrison __func__, fn->leaf, oif); 438f11e6659SDavid S. Miller 439f11e6659SDavid S. Miller rt0 = fn->rr_ptr; 440f11e6659SDavid S. Miller if (!rt0) 441f11e6659SDavid S. Miller fn->rr_ptr = rt0 = fn->leaf; 442f11e6659SDavid S. Miller 443f11e6659SDavid S. Miller match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict); 444f11e6659SDavid S. Miller 445554cfb7eSYOSHIFUJI Hideaki if (!match && 446f11e6659SDavid S. Miller (strict & RT6_LOOKUP_F_REACHABLE)) { 447d8d1f30bSChangli Gao struct rt6_info *next = rt0->dst.rt6_next; 448f11e6659SDavid S. Miller 449554cfb7eSYOSHIFUJI Hideaki /* no entries matched; do round-robin */ 450f11e6659SDavid S. Miller if (!next || next->rt6i_metric != rt0->rt6i_metric) 451f11e6659SDavid S. Miller next = fn->leaf; 452f11e6659SDavid S. Miller 453f11e6659SDavid S. Miller if (next != rt0) 454f11e6659SDavid S. Miller fn->rr_ptr = next; 455554cfb7eSYOSHIFUJI Hideaki } 456554cfb7eSYOSHIFUJI Hideaki 457f11e6659SDavid S. Miller RT6_TRACE("%s() => %p\n", 4580dc47877SHarvey Harrison __func__, match); 459554cfb7eSYOSHIFUJI Hideaki 460c346dca1SYOSHIFUJI Hideaki net = dev_net(rt0->rt6i_dev); 461a02cec21SEric Dumazet return match ? match : net->ipv6.ip6_null_entry; 4621da177e4SLinus Torvalds } 4631da177e4SLinus Torvalds 46470ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 46570ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 46670ceb4f5SYOSHIFUJI Hideaki struct in6_addr *gwaddr) 46770ceb4f5SYOSHIFUJI Hideaki { 468c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 46970ceb4f5SYOSHIFUJI Hideaki struct route_info *rinfo = (struct route_info *) opt; 47070ceb4f5SYOSHIFUJI Hideaki struct in6_addr prefix_buf, *prefix; 47170ceb4f5SYOSHIFUJI Hideaki unsigned int pref; 4724bed72e4SYOSHIFUJI Hideaki unsigned long lifetime; 47370ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt; 47470ceb4f5SYOSHIFUJI Hideaki 47570ceb4f5SYOSHIFUJI Hideaki if (len < sizeof(struct route_info)) { 47670ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 47770ceb4f5SYOSHIFUJI Hideaki } 47870ceb4f5SYOSHIFUJI Hideaki 47970ceb4f5SYOSHIFUJI Hideaki /* Sanity check for prefix_len and length */ 48070ceb4f5SYOSHIFUJI Hideaki if (rinfo->length > 3) { 48170ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 48270ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 128) { 48370ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 48470ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 64) { 48570ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 2) { 48670ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 48770ceb4f5SYOSHIFUJI Hideaki } 48870ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 0) { 48970ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 1) { 49070ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 49170ceb4f5SYOSHIFUJI Hideaki } 49270ceb4f5SYOSHIFUJI Hideaki } 49370ceb4f5SYOSHIFUJI Hideaki 49470ceb4f5SYOSHIFUJI Hideaki pref = rinfo->route_pref; 49570ceb4f5SYOSHIFUJI Hideaki if (pref == ICMPV6_ROUTER_PREF_INVALID) 4963933fc95SJens Rosenboom return -EINVAL; 49770ceb4f5SYOSHIFUJI Hideaki 4984bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); 49970ceb4f5SYOSHIFUJI Hideaki 50070ceb4f5SYOSHIFUJI Hideaki if (rinfo->length == 3) 50170ceb4f5SYOSHIFUJI Hideaki prefix = (struct in6_addr *)rinfo->prefix; 50270ceb4f5SYOSHIFUJI Hideaki else { 50370ceb4f5SYOSHIFUJI Hideaki /* this function is safe */ 50470ceb4f5SYOSHIFUJI Hideaki ipv6_addr_prefix(&prefix_buf, 50570ceb4f5SYOSHIFUJI Hideaki (struct in6_addr *)rinfo->prefix, 50670ceb4f5SYOSHIFUJI Hideaki rinfo->prefix_len); 50770ceb4f5SYOSHIFUJI Hideaki prefix = &prefix_buf; 50870ceb4f5SYOSHIFUJI Hideaki } 50970ceb4f5SYOSHIFUJI Hideaki 510efa2cea0SDaniel Lezcano rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr, 511efa2cea0SDaniel Lezcano dev->ifindex); 51270ceb4f5SYOSHIFUJI Hideaki 51370ceb4f5SYOSHIFUJI Hideaki if (rt && !lifetime) { 514e0a1ad73SThomas Graf ip6_del_rt(rt); 51570ceb4f5SYOSHIFUJI Hideaki rt = NULL; 51670ceb4f5SYOSHIFUJI Hideaki } 51770ceb4f5SYOSHIFUJI Hideaki 51870ceb4f5SYOSHIFUJI Hideaki if (!rt && lifetime) 519efa2cea0SDaniel Lezcano rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex, 52070ceb4f5SYOSHIFUJI Hideaki pref); 52170ceb4f5SYOSHIFUJI Hideaki else if (rt) 52270ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags = RTF_ROUTEINFO | 52370ceb4f5SYOSHIFUJI Hideaki (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 52470ceb4f5SYOSHIFUJI Hideaki 52570ceb4f5SYOSHIFUJI Hideaki if (rt) { 5264bed72e4SYOSHIFUJI Hideaki if (!addrconf_finite_timeout(lifetime)) { 52770ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags &= ~RTF_EXPIRES; 52870ceb4f5SYOSHIFUJI Hideaki } else { 52970ceb4f5SYOSHIFUJI Hideaki rt->rt6i_expires = jiffies + HZ * lifetime; 53070ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_EXPIRES; 53170ceb4f5SYOSHIFUJI Hideaki } 532d8d1f30bSChangli Gao dst_release(&rt->dst); 53370ceb4f5SYOSHIFUJI Hideaki } 53470ceb4f5SYOSHIFUJI Hideaki return 0; 53570ceb4f5SYOSHIFUJI Hideaki } 53670ceb4f5SYOSHIFUJI Hideaki #endif 53770ceb4f5SYOSHIFUJI Hideaki 5388ed67789SDaniel Lezcano #define BACKTRACK(__net, saddr) \ 539982f56f3SYOSHIFUJI Hideaki do { \ 5408ed67789SDaniel Lezcano if (rt == __net->ipv6.ip6_null_entry) { \ 541982f56f3SYOSHIFUJI Hideaki struct fib6_node *pn; \ 542e0eda7bbSVille Nuorvala while (1) { \ 543982f56f3SYOSHIFUJI Hideaki if (fn->fn_flags & RTN_TL_ROOT) \ 544c71099acSThomas Graf goto out; \ 545982f56f3SYOSHIFUJI Hideaki pn = fn->parent; \ 546982f56f3SYOSHIFUJI Hideaki if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \ 5478bce65b9SKim Nordlund fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \ 548982f56f3SYOSHIFUJI Hideaki else \ 549982f56f3SYOSHIFUJI Hideaki fn = pn; \ 550c71099acSThomas Graf if (fn->fn_flags & RTN_RTINFO) \ 551c71099acSThomas Graf goto restart; \ 552c71099acSThomas Graf } \ 553982f56f3SYOSHIFUJI Hideaki } \ 554982f56f3SYOSHIFUJI Hideaki } while(0) 555c71099acSThomas Graf 5568ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_lookup(struct net *net, 5578ed67789SDaniel Lezcano struct fib6_table *table, 558c71099acSThomas Graf struct flowi *fl, int flags) 5591da177e4SLinus Torvalds { 5601da177e4SLinus Torvalds struct fib6_node *fn; 5611da177e4SLinus Torvalds struct rt6_info *rt; 5621da177e4SLinus Torvalds 563c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 564c71099acSThomas Graf fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src); 565c71099acSThomas Graf restart: 566c71099acSThomas Graf rt = fn->leaf; 567dd3abc4eSYOSHIFUJI Hideaki rt = rt6_device_match(net, rt, &fl->fl6_src, fl->oif, flags); 5688ed67789SDaniel Lezcano BACKTRACK(net, &fl->fl6_src); 569c71099acSThomas Graf out: 570d8d1f30bSChangli Gao dst_use(&rt->dst, jiffies); 571c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 5721da177e4SLinus Torvalds return rt; 573c71099acSThomas Graf 574c71099acSThomas Graf } 575c71099acSThomas Graf 5769acd9f3aSYOSHIFUJI Hideaki struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, 5779acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *saddr, int oif, int strict) 578c71099acSThomas Graf { 579c71099acSThomas Graf struct flowi fl = { 580c71099acSThomas Graf .oif = oif, 5815811662bSChangli Gao .fl6_dst = *daddr, 582c71099acSThomas Graf }; 583c71099acSThomas Graf struct dst_entry *dst; 58477d16f45SYOSHIFUJI Hideaki int flags = strict ? RT6_LOOKUP_F_IFACE : 0; 585c71099acSThomas Graf 586adaa70bbSThomas Graf if (saddr) { 587adaa70bbSThomas Graf memcpy(&fl.fl6_src, saddr, sizeof(*saddr)); 588adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 589adaa70bbSThomas Graf } 590adaa70bbSThomas Graf 591606a2b48SDaniel Lezcano dst = fib6_rule_lookup(net, &fl, flags, ip6_pol_route_lookup); 592c71099acSThomas Graf if (dst->error == 0) 593c71099acSThomas Graf return (struct rt6_info *) dst; 594c71099acSThomas Graf 595c71099acSThomas Graf dst_release(dst); 596c71099acSThomas Graf 5971da177e4SLinus Torvalds return NULL; 5981da177e4SLinus Torvalds } 5991da177e4SLinus Torvalds 6007159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(rt6_lookup); 6017159039aSYOSHIFUJI Hideaki 602c71099acSThomas Graf /* ip6_ins_rt is called with FREE table->tb6_lock. 6031da177e4SLinus Torvalds It takes new route entry, the addition fails by any reason the 6041da177e4SLinus Torvalds route is freed. In any case, if caller does not hold it, it may 6051da177e4SLinus Torvalds be destroyed. 6061da177e4SLinus Torvalds */ 6071da177e4SLinus Torvalds 60886872cb5SThomas Graf static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info) 6091da177e4SLinus Torvalds { 6101da177e4SLinus Torvalds int err; 611c71099acSThomas Graf struct fib6_table *table; 6121da177e4SLinus Torvalds 613c71099acSThomas Graf table = rt->rt6i_table; 614c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 61586872cb5SThomas Graf err = fib6_add(&table->tb6_root, rt, info); 616c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 6171da177e4SLinus Torvalds 6181da177e4SLinus Torvalds return err; 6191da177e4SLinus Torvalds } 6201da177e4SLinus Torvalds 62140e22e8fSThomas Graf int ip6_ins_rt(struct rt6_info *rt) 62240e22e8fSThomas Graf { 6234d1169c1SDenis V. Lunev struct nl_info info = { 624c346dca1SYOSHIFUJI Hideaki .nl_net = dev_net(rt->rt6i_dev), 6254d1169c1SDenis V. Lunev }; 626528c4cebSDenis V. Lunev return __ip6_ins_rt(rt, &info); 62740e22e8fSThomas Graf } 62840e22e8fSThomas Graf 62995a9a5baSYOSHIFUJI Hideaki static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, struct in6_addr *daddr, 63095a9a5baSYOSHIFUJI Hideaki struct in6_addr *saddr) 6311da177e4SLinus Torvalds { 6321da177e4SLinus Torvalds struct rt6_info *rt; 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds /* 6351da177e4SLinus Torvalds * Clone the route. 6361da177e4SLinus Torvalds */ 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds rt = ip6_rt_copy(ort); 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds if (rt) { 64114deae41SDavid S. Miller struct neighbour *neigh; 64214deae41SDavid S. Miller int attempts = !in_softirq(); 64314deae41SDavid S. Miller 64458c4fb86SYOSHIFUJI Hideaki if (!(rt->rt6i_flags&RTF_GATEWAY)) { 64558c4fb86SYOSHIFUJI Hideaki if (rt->rt6i_dst.plen != 128 && 64658c4fb86SYOSHIFUJI Hideaki ipv6_addr_equal(&rt->rt6i_dst.addr, daddr)) 64758c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 6481da177e4SLinus Torvalds ipv6_addr_copy(&rt->rt6i_gateway, daddr); 64958c4fb86SYOSHIFUJI Hideaki } 6501da177e4SLinus Torvalds 65158c4fb86SYOSHIFUJI Hideaki ipv6_addr_copy(&rt->rt6i_dst.addr, daddr); 6521da177e4SLinus Torvalds rt->rt6i_dst.plen = 128; 6531da177e4SLinus Torvalds rt->rt6i_flags |= RTF_CACHE; 654d8d1f30bSChangli Gao rt->dst.flags |= DST_HOST; 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 6571da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 6581da177e4SLinus Torvalds ipv6_addr_copy(&rt->rt6i_src.addr, saddr); 6591da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 6601da177e4SLinus Torvalds } 6611da177e4SLinus Torvalds #endif 6621da177e4SLinus Torvalds 66314deae41SDavid S. Miller retry: 66414deae41SDavid S. Miller neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway); 66514deae41SDavid S. Miller if (IS_ERR(neigh)) { 66614deae41SDavid S. Miller struct net *net = dev_net(rt->rt6i_dev); 66714deae41SDavid S. Miller int saved_rt_min_interval = 66814deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_min_interval; 66914deae41SDavid S. Miller int saved_rt_elasticity = 67014deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_elasticity; 67114deae41SDavid S. Miller 67214deae41SDavid S. Miller if (attempts-- > 0) { 67314deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_elasticity = 1; 67414deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_min_interval = 0; 67514deae41SDavid S. Miller 67686393e52SAlexey Dobriyan ip6_dst_gc(&net->ipv6.ip6_dst_ops); 67714deae41SDavid S. Miller 67814deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_elasticity = 67914deae41SDavid S. Miller saved_rt_elasticity; 68014deae41SDavid S. Miller net->ipv6.sysctl.ip6_rt_gc_min_interval = 68114deae41SDavid S. Miller saved_rt_min_interval; 68214deae41SDavid S. Miller goto retry; 68314deae41SDavid S. Miller } 68414deae41SDavid S. Miller 68514deae41SDavid S. Miller if (net_ratelimit()) 68614deae41SDavid S. Miller printk(KERN_WARNING 6877e1b33e5SUlrich Weber "ipv6: Neighbour table overflow.\n"); 688d8d1f30bSChangli Gao dst_free(&rt->dst); 68914deae41SDavid S. Miller return NULL; 69014deae41SDavid S. Miller } 69114deae41SDavid S. Miller rt->rt6i_nexthop = neigh; 6921da177e4SLinus Torvalds 69395a9a5baSYOSHIFUJI Hideaki } 6941da177e4SLinus Torvalds 6951da177e4SLinus Torvalds return rt; 6961da177e4SLinus Torvalds } 69795a9a5baSYOSHIFUJI Hideaki 698299d9939SYOSHIFUJI Hideaki static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, struct in6_addr *daddr) 699299d9939SYOSHIFUJI Hideaki { 700299d9939SYOSHIFUJI Hideaki struct rt6_info *rt = ip6_rt_copy(ort); 701299d9939SYOSHIFUJI Hideaki if (rt) { 702299d9939SYOSHIFUJI Hideaki ipv6_addr_copy(&rt->rt6i_dst.addr, daddr); 703299d9939SYOSHIFUJI Hideaki rt->rt6i_dst.plen = 128; 704299d9939SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_CACHE; 705d8d1f30bSChangli Gao rt->dst.flags |= DST_HOST; 706299d9939SYOSHIFUJI Hideaki rt->rt6i_nexthop = neigh_clone(ort->rt6i_nexthop); 707299d9939SYOSHIFUJI Hideaki } 708299d9939SYOSHIFUJI Hideaki return rt; 709299d9939SYOSHIFUJI Hideaki } 710299d9939SYOSHIFUJI Hideaki 7118ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif, 7128ce11e6aSAdrian Bunk struct flowi *fl, int flags) 7131da177e4SLinus Torvalds { 7141da177e4SLinus Torvalds struct fib6_node *fn; 715519fbd87SYOSHIFUJI Hideaki struct rt6_info *rt, *nrt; 716c71099acSThomas Graf int strict = 0; 7171da177e4SLinus Torvalds int attempts = 3; 718519fbd87SYOSHIFUJI Hideaki int err; 71953b7997fSYOSHIFUJI Hideaki int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE; 7201da177e4SLinus Torvalds 72177d16f45SYOSHIFUJI Hideaki strict |= flags & RT6_LOOKUP_F_IFACE; 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds relookup: 724c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 7251da177e4SLinus Torvalds 7268238dd06SYOSHIFUJI Hideaki restart_2: 727c71099acSThomas Graf fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src); 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds restart: 7304acad72dSPavel Emelyanov rt = rt6_select(fn, oif, strict | reachable); 7318ed67789SDaniel Lezcano 7328ed67789SDaniel Lezcano BACKTRACK(net, &fl->fl6_src); 7338ed67789SDaniel Lezcano if (rt == net->ipv6.ip6_null_entry || 7348238dd06SYOSHIFUJI Hideaki rt->rt6i_flags & RTF_CACHE) 7351da177e4SLinus Torvalds goto out; 7361da177e4SLinus Torvalds 737d8d1f30bSChangli Gao dst_hold(&rt->dst); 738c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 7391da177e4SLinus Torvalds 740519fbd87SYOSHIFUJI Hideaki if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP)) 741e40cf353SYOSHIFUJI Hideaki nrt = rt6_alloc_cow(rt, &fl->fl6_dst, &fl->fl6_src); 742*7343ff31SDavid S. Miller else if (!(rt->dst.flags & DST_HOST)) 743519fbd87SYOSHIFUJI Hideaki nrt = rt6_alloc_clone(rt, &fl->fl6_dst); 744*7343ff31SDavid S. Miller else 745*7343ff31SDavid S. Miller goto out2; 7461da177e4SLinus Torvalds 747d8d1f30bSChangli Gao dst_release(&rt->dst); 7488ed67789SDaniel Lezcano rt = nrt ? : net->ipv6.ip6_null_entry; 7491da177e4SLinus Torvalds 750d8d1f30bSChangli Gao dst_hold(&rt->dst); 751e40cf353SYOSHIFUJI Hideaki if (nrt) { 75240e22e8fSThomas Graf err = ip6_ins_rt(nrt); 753e40cf353SYOSHIFUJI Hideaki if (!err) 754e40cf353SYOSHIFUJI Hideaki goto out2; 755e40cf353SYOSHIFUJI Hideaki } 756e40cf353SYOSHIFUJI Hideaki 757e40cf353SYOSHIFUJI Hideaki if (--attempts <= 0) 7581da177e4SLinus Torvalds goto out2; 7591da177e4SLinus Torvalds 760519fbd87SYOSHIFUJI Hideaki /* 761c71099acSThomas Graf * Race condition! In the gap, when table->tb6_lock was 762519fbd87SYOSHIFUJI Hideaki * released someone could insert this route. Relookup. 7631da177e4SLinus Torvalds */ 764d8d1f30bSChangli Gao dst_release(&rt->dst); 7651da177e4SLinus Torvalds goto relookup; 766e40cf353SYOSHIFUJI Hideaki 767519fbd87SYOSHIFUJI Hideaki out: 7688238dd06SYOSHIFUJI Hideaki if (reachable) { 7698238dd06SYOSHIFUJI Hideaki reachable = 0; 7708238dd06SYOSHIFUJI Hideaki goto restart_2; 7718238dd06SYOSHIFUJI Hideaki } 772d8d1f30bSChangli Gao dst_hold(&rt->dst); 773c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 7741da177e4SLinus Torvalds out2: 775d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 776d8d1f30bSChangli Gao rt->dst.__use++; 777c71099acSThomas Graf 778c71099acSThomas Graf return rt; 779c71099acSThomas Graf } 780c71099acSThomas Graf 7818ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table, 7824acad72dSPavel Emelyanov struct flowi *fl, int flags) 7834acad72dSPavel Emelyanov { 7848ed67789SDaniel Lezcano return ip6_pol_route(net, table, fl->iif, fl, flags); 7854acad72dSPavel Emelyanov } 7864acad72dSPavel Emelyanov 787c71099acSThomas Graf void ip6_route_input(struct sk_buff *skb) 788c71099acSThomas Graf { 7890660e03fSArnaldo Carvalho de Melo struct ipv6hdr *iph = ipv6_hdr(skb); 790c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(skb->dev); 791adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 792c71099acSThomas Graf struct flowi fl = { 793c71099acSThomas Graf .iif = skb->dev->ifindex, 7945811662bSChangli Gao .fl6_dst = iph->daddr, 7955811662bSChangli Gao .fl6_src = iph->saddr, 7965811662bSChangli Gao .fl6_flowlabel = (* (__be32 *) iph)&IPV6_FLOWINFO_MASK, 79747dcf0cbSThomas Graf .mark = skb->mark, 798c71099acSThomas Graf .proto = iph->nexthdr, 799c71099acSThomas Graf }; 800adaa70bbSThomas Graf 8011d6e55f1SThomas Goff if (rt6_need_strict(&iph->daddr) && skb->dev->type != ARPHRD_PIMREG) 802adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_IFACE; 803c71099acSThomas Graf 804adf30907SEric Dumazet skb_dst_set(skb, fib6_rule_lookup(net, &fl, flags, ip6_pol_route_input)); 805c71099acSThomas Graf } 806c71099acSThomas Graf 8078ed67789SDaniel Lezcano static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table, 808c71099acSThomas Graf struct flowi *fl, int flags) 809c71099acSThomas Graf { 8108ed67789SDaniel Lezcano return ip6_pol_route(net, table, fl->oif, fl, flags); 811c71099acSThomas Graf } 812c71099acSThomas Graf 8134591db4fSDaniel Lezcano struct dst_entry * ip6_route_output(struct net *net, struct sock *sk, 8144591db4fSDaniel Lezcano struct flowi *fl) 815c71099acSThomas Graf { 816c71099acSThomas Graf int flags = 0; 817c71099acSThomas Graf 8186057fd78SBrian Haley if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl->fl6_dst)) 81977d16f45SYOSHIFUJI Hideaki flags |= RT6_LOOKUP_F_IFACE; 820c71099acSThomas Graf 821adaa70bbSThomas Graf if (!ipv6_addr_any(&fl->fl6_src)) 822adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_HAS_SADDR; 8230c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 else if (sk) 8240c9a2ac1SYOSHIFUJI Hideaki / 吉藤英明 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); 825adaa70bbSThomas Graf 8264591db4fSDaniel Lezcano return fib6_rule_lookup(net, fl, flags, ip6_pol_route_output); 8271da177e4SLinus Torvalds } 8281da177e4SLinus Torvalds 8297159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ip6_route_output); 8301da177e4SLinus Torvalds 83114e50e57SDavid S. Miller int ip6_dst_blackhole(struct sock *sk, struct dst_entry **dstp, struct flowi *fl) 83214e50e57SDavid S. Miller { 83314e50e57SDavid S. Miller struct rt6_info *ort = (struct rt6_info *) *dstp; 83414e50e57SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) 83514e50e57SDavid S. Miller dst_alloc(&ip6_dst_blackhole_ops); 83614e50e57SDavid S. Miller struct dst_entry *new = NULL; 83714e50e57SDavid S. Miller 83814e50e57SDavid S. Miller if (rt) { 839d8d1f30bSChangli Gao new = &rt->dst; 84014e50e57SDavid S. Miller 84114e50e57SDavid S. Miller atomic_set(&new->__refcnt, 1); 84214e50e57SDavid S. Miller new->__use = 1; 843352e512cSHerbert Xu new->input = dst_discard; 844352e512cSHerbert Xu new->output = dst_discard; 84514e50e57SDavid S. Miller 846defb3519SDavid S. Miller dst_copy_metrics(new, &ort->dst); 847d8d1f30bSChangli Gao new->dev = ort->dst.dev; 84814e50e57SDavid S. Miller if (new->dev) 84914e50e57SDavid S. Miller dev_hold(new->dev); 85014e50e57SDavid S. Miller rt->rt6i_idev = ort->rt6i_idev; 85114e50e57SDavid S. Miller if (rt->rt6i_idev) 85214e50e57SDavid S. Miller in6_dev_hold(rt->rt6i_idev); 85314e50e57SDavid S. Miller rt->rt6i_expires = 0; 85414e50e57SDavid S. Miller 85514e50e57SDavid S. Miller ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway); 85614e50e57SDavid S. Miller rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES; 85714e50e57SDavid S. Miller rt->rt6i_metric = 0; 85814e50e57SDavid S. Miller 85914e50e57SDavid S. Miller memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 86014e50e57SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES 86114e50e57SDavid S. Miller memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 86214e50e57SDavid S. Miller #endif 86314e50e57SDavid S. Miller 86414e50e57SDavid S. Miller dst_free(new); 86514e50e57SDavid S. Miller } 86614e50e57SDavid S. Miller 86714e50e57SDavid S. Miller dst_release(*dstp); 86814e50e57SDavid S. Miller *dstp = new; 869a02cec21SEric Dumazet return new ? 0 : -ENOMEM; 87014e50e57SDavid S. Miller } 87114e50e57SDavid S. Miller EXPORT_SYMBOL_GPL(ip6_dst_blackhole); 87214e50e57SDavid S. Miller 8731da177e4SLinus Torvalds /* 8741da177e4SLinus Torvalds * Destination cache support functions 8751da177e4SLinus Torvalds */ 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 8781da177e4SLinus Torvalds { 8791da177e4SLinus Torvalds struct rt6_info *rt; 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds rt = (struct rt6_info *) dst; 8821da177e4SLinus Torvalds 88310414444SHerbert Xu if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) 8841da177e4SLinus Torvalds return dst; 8851da177e4SLinus Torvalds 8861da177e4SLinus Torvalds return NULL; 8871da177e4SLinus Torvalds } 8881da177e4SLinus Torvalds 8891da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 8901da177e4SLinus Torvalds { 8911da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 8921da177e4SLinus Torvalds 8931da177e4SLinus Torvalds if (rt) { 89454c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt->rt6i_flags & RTF_CACHE) { 89554c1a859SYOSHIFUJI Hideaki / 吉藤英明 if (rt6_check_expired(rt)) { 896e0a1ad73SThomas Graf ip6_del_rt(rt); 89754c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 8981da177e4SLinus Torvalds } 89954c1a859SYOSHIFUJI Hideaki / 吉藤英明 } else { 90054c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst_release(dst); 90154c1a859SYOSHIFUJI Hideaki / 吉藤英明 dst = NULL; 90254c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 90354c1a859SYOSHIFUJI Hideaki / 吉藤英明 } 90454c1a859SYOSHIFUJI Hideaki / 吉藤英明 return dst; 9051da177e4SLinus Torvalds } 9061da177e4SLinus Torvalds 9071da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 9081da177e4SLinus Torvalds { 9091da177e4SLinus Torvalds struct rt6_info *rt; 9101da177e4SLinus Torvalds 9113ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); 9121da177e4SLinus Torvalds 913adf30907SEric Dumazet rt = (struct rt6_info *) skb_dst(skb); 9141da177e4SLinus Torvalds if (rt) { 9151da177e4SLinus Torvalds if (rt->rt6i_flags&RTF_CACHE) { 916d8d1f30bSChangli Gao dst_set_expires(&rt->dst, 0); 9171da177e4SLinus Torvalds rt->rt6i_flags |= RTF_EXPIRES; 9181da177e4SLinus Torvalds } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) 9191da177e4SLinus Torvalds rt->rt6i_node->fn_sernum = -1; 9201da177e4SLinus Torvalds } 9211da177e4SLinus Torvalds } 9221da177e4SLinus Torvalds 9231da177e4SLinus Torvalds static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu) 9241da177e4SLinus Torvalds { 9251da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info*)dst; 9261da177e4SLinus Torvalds 9271da177e4SLinus Torvalds if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) { 9281da177e4SLinus Torvalds rt6->rt6i_flags |= RTF_MODIFIED; 9291da177e4SLinus Torvalds if (mtu < IPV6_MIN_MTU) { 930defb3519SDavid S. Miller u32 features = dst_metric(dst, RTAX_FEATURES); 9311da177e4SLinus Torvalds mtu = IPV6_MIN_MTU; 932defb3519SDavid S. Miller features |= RTAX_FEATURE_ALLFRAG; 933defb3519SDavid S. Miller dst_metric_set(dst, RTAX_FEATURES, features); 9341da177e4SLinus Torvalds } 935defb3519SDavid S. Miller dst_metric_set(dst, RTAX_MTU, mtu); 9368d71740cSTom Tucker call_netevent_notifiers(NETEVENT_PMTU_UPDATE, dst); 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds } 9391da177e4SLinus Torvalds 9400dbaee3bSDavid S. Miller static unsigned int ip6_default_advmss(const struct dst_entry *dst) 9411da177e4SLinus Torvalds { 9420dbaee3bSDavid S. Miller struct net_device *dev = dst->dev; 9430dbaee3bSDavid S. Miller unsigned int mtu = dst_mtu(dst); 9440dbaee3bSDavid S. Miller struct net *net = dev_net(dev); 9450dbaee3bSDavid S. Miller 9461da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 9471da177e4SLinus Torvalds 9485578689aSDaniel Lezcano if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) 9495578689aSDaniel Lezcano mtu = net->ipv6.sysctl.ip6_rt_min_advmss; 9501da177e4SLinus Torvalds 9511da177e4SLinus Torvalds /* 9521da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 9531da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 9541da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 9551da177e4SLinus Torvalds * rely only on pmtu discovery" 9561da177e4SLinus Torvalds */ 9571da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 9581da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 9591da177e4SLinus Torvalds return mtu; 9601da177e4SLinus Torvalds } 9611da177e4SLinus Torvalds 962d33e4553SDavid S. Miller static unsigned int ip6_default_mtu(const struct dst_entry *dst) 963d33e4553SDavid S. Miller { 964d33e4553SDavid S. Miller unsigned int mtu = IPV6_MIN_MTU; 965d33e4553SDavid S. Miller struct inet6_dev *idev; 966d33e4553SDavid S. Miller 967d33e4553SDavid S. Miller rcu_read_lock(); 968d33e4553SDavid S. Miller idev = __in6_dev_get(dst->dev); 969d33e4553SDavid S. Miller if (idev) 970d33e4553SDavid S. Miller mtu = idev->cnf.mtu6; 971d33e4553SDavid S. Miller rcu_read_unlock(); 972d33e4553SDavid S. Miller 973d33e4553SDavid S. Miller return mtu; 974d33e4553SDavid S. Miller } 975d33e4553SDavid S. Miller 9763b00944cSYOSHIFUJI Hideaki static struct dst_entry *icmp6_dst_gc_list; 9773b00944cSYOSHIFUJI Hideaki static DEFINE_SPINLOCK(icmp6_dst_lock); 9785d0bbeebSThomas Graf 9793b00944cSYOSHIFUJI Hideaki struct dst_entry *icmp6_dst_alloc(struct net_device *dev, 9801da177e4SLinus Torvalds struct neighbour *neigh, 9819acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *addr) 9821da177e4SLinus Torvalds { 9831da177e4SLinus Torvalds struct rt6_info *rt; 9841da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 985c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 9861da177e4SLinus Torvalds 9871da177e4SLinus Torvalds if (unlikely(idev == NULL)) 9881da177e4SLinus Torvalds return NULL; 9891da177e4SLinus Torvalds 99086393e52SAlexey Dobriyan rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops); 9911da177e4SLinus Torvalds if (unlikely(rt == NULL)) { 9921da177e4SLinus Torvalds in6_dev_put(idev); 9931da177e4SLinus Torvalds goto out; 9941da177e4SLinus Torvalds } 9951da177e4SLinus Torvalds 9961da177e4SLinus Torvalds dev_hold(dev); 9971da177e4SLinus Torvalds if (neigh) 9981da177e4SLinus Torvalds neigh_hold(neigh); 99914deae41SDavid S. Miller else { 10001da177e4SLinus Torvalds neigh = ndisc_get_neigh(dev, addr); 100114deae41SDavid S. Miller if (IS_ERR(neigh)) 100214deae41SDavid S. Miller neigh = NULL; 100314deae41SDavid S. Miller } 10041da177e4SLinus Torvalds 10051da177e4SLinus Torvalds rt->rt6i_dev = dev; 10061da177e4SLinus Torvalds rt->rt6i_idev = idev; 10071da177e4SLinus Torvalds rt->rt6i_nexthop = neigh; 1008d8d1f30bSChangli Gao atomic_set(&rt->dst.__refcnt, 1); 1009defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255); 1010d8d1f30bSChangli Gao rt->dst.output = ip6_output; 10111da177e4SLinus Torvalds 10121da177e4SLinus Torvalds #if 0 /* there's no chance to use these for ndisc */ 1013d8d1f30bSChangli Gao rt->dst.flags = ipv6_addr_type(addr) & IPV6_ADDR_UNICAST 10141da177e4SLinus Torvalds ? DST_HOST 10151da177e4SLinus Torvalds : 0; 10161da177e4SLinus Torvalds ipv6_addr_copy(&rt->rt6i_dst.addr, addr); 10171da177e4SLinus Torvalds rt->rt6i_dst.plen = 128; 10181da177e4SLinus Torvalds #endif 10191da177e4SLinus Torvalds 10203b00944cSYOSHIFUJI Hideaki spin_lock_bh(&icmp6_dst_lock); 1021d8d1f30bSChangli Gao rt->dst.next = icmp6_dst_gc_list; 1022d8d1f30bSChangli Gao icmp6_dst_gc_list = &rt->dst; 10233b00944cSYOSHIFUJI Hideaki spin_unlock_bh(&icmp6_dst_lock); 10241da177e4SLinus Torvalds 10255578689aSDaniel Lezcano fib6_force_start_gc(net); 10261da177e4SLinus Torvalds 10271da177e4SLinus Torvalds out: 1028d8d1f30bSChangli Gao return &rt->dst; 10291da177e4SLinus Torvalds } 10301da177e4SLinus Torvalds 10313d0f24a7SStephen Hemminger int icmp6_dst_gc(void) 10321da177e4SLinus Torvalds { 10331da177e4SLinus Torvalds struct dst_entry *dst, *next, **pprev; 10343d0f24a7SStephen Hemminger int more = 0; 10351da177e4SLinus Torvalds 10361da177e4SLinus Torvalds next = NULL; 10375d0bbeebSThomas Graf 10383b00944cSYOSHIFUJI Hideaki spin_lock_bh(&icmp6_dst_lock); 10393b00944cSYOSHIFUJI Hideaki pprev = &icmp6_dst_gc_list; 10405d0bbeebSThomas Graf 10411da177e4SLinus Torvalds while ((dst = *pprev) != NULL) { 10421da177e4SLinus Torvalds if (!atomic_read(&dst->__refcnt)) { 10431da177e4SLinus Torvalds *pprev = dst->next; 10441da177e4SLinus Torvalds dst_free(dst); 10451da177e4SLinus Torvalds } else { 10461da177e4SLinus Torvalds pprev = &dst->next; 10473d0f24a7SStephen Hemminger ++more; 10481da177e4SLinus Torvalds } 10491da177e4SLinus Torvalds } 10501da177e4SLinus Torvalds 10513b00944cSYOSHIFUJI Hideaki spin_unlock_bh(&icmp6_dst_lock); 10525d0bbeebSThomas Graf 10533d0f24a7SStephen Hemminger return more; 10541da177e4SLinus Torvalds } 10551da177e4SLinus Torvalds 10561e493d19SDavid S. Miller static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg), 10571e493d19SDavid S. Miller void *arg) 10581e493d19SDavid S. Miller { 10591e493d19SDavid S. Miller struct dst_entry *dst, **pprev; 10601e493d19SDavid S. Miller 10611e493d19SDavid S. Miller spin_lock_bh(&icmp6_dst_lock); 10621e493d19SDavid S. Miller pprev = &icmp6_dst_gc_list; 10631e493d19SDavid S. Miller while ((dst = *pprev) != NULL) { 10641e493d19SDavid S. Miller struct rt6_info *rt = (struct rt6_info *) dst; 10651e493d19SDavid S. Miller if (func(rt, arg)) { 10661e493d19SDavid S. Miller *pprev = dst->next; 10671e493d19SDavid S. Miller dst_free(dst); 10681e493d19SDavid S. Miller } else { 10691e493d19SDavid S. Miller pprev = &dst->next; 10701e493d19SDavid S. Miller } 10711e493d19SDavid S. Miller } 10721e493d19SDavid S. Miller spin_unlock_bh(&icmp6_dst_lock); 10731e493d19SDavid S. Miller } 10741e493d19SDavid S. Miller 1075569d3645SDaniel Lezcano static int ip6_dst_gc(struct dst_ops *ops) 10761da177e4SLinus Torvalds { 10771da177e4SLinus Torvalds unsigned long now = jiffies; 107886393e52SAlexey Dobriyan struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); 10797019b78eSDaniel Lezcano int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; 10807019b78eSDaniel Lezcano int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; 10817019b78eSDaniel Lezcano int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; 10827019b78eSDaniel Lezcano int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; 10837019b78eSDaniel Lezcano unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; 1084fc66f95cSEric Dumazet int entries; 10851da177e4SLinus Torvalds 1086fc66f95cSEric Dumazet entries = dst_entries_get_fast(ops); 10877019b78eSDaniel Lezcano if (time_after(rt_last_gc + rt_min_interval, now) && 1088fc66f95cSEric Dumazet entries <= rt_max_size) 10891da177e4SLinus Torvalds goto out; 10901da177e4SLinus Torvalds 10916891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire++; 10926891a346SBenjamin Thery fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net); 10936891a346SBenjamin Thery net->ipv6.ip6_rt_last_gc = now; 1094fc66f95cSEric Dumazet entries = dst_entries_get_slow(ops); 1095fc66f95cSEric Dumazet if (entries < ops->gc_thresh) 10967019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; 10971da177e4SLinus Torvalds out: 10987019b78eSDaniel Lezcano net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; 1099fc66f95cSEric Dumazet return entries > rt_max_size; 11001da177e4SLinus Torvalds } 11011da177e4SLinus Torvalds 11021da177e4SLinus Torvalds /* Clean host part of a prefix. Not necessary in radix tree, 11031da177e4SLinus Torvalds but results in cleaner routing tables. 11041da177e4SLinus Torvalds 11051da177e4SLinus Torvalds Remove it only when all the things will work! 11061da177e4SLinus Torvalds */ 11071da177e4SLinus Torvalds 11086b75d090SYOSHIFUJI Hideaki int ip6_dst_hoplimit(struct dst_entry *dst) 11091da177e4SLinus Torvalds { 11105170ae82SDavid S. Miller int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT); 1111a02e4b7dSDavid S. Miller if (hoplimit == 0) { 11126b75d090SYOSHIFUJI Hideaki struct net_device *dev = dst->dev; 1113c68f24ccSEric Dumazet struct inet6_dev *idev; 1114c68f24ccSEric Dumazet 1115c68f24ccSEric Dumazet rcu_read_lock(); 1116c68f24ccSEric Dumazet idev = __in6_dev_get(dev); 1117c68f24ccSEric Dumazet if (idev) 11181da177e4SLinus Torvalds hoplimit = idev->cnf.hop_limit; 1119c68f24ccSEric Dumazet else 112053b7997fSYOSHIFUJI Hideaki hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit; 1121c68f24ccSEric Dumazet rcu_read_unlock(); 11221da177e4SLinus Torvalds } 11231da177e4SLinus Torvalds return hoplimit; 11241da177e4SLinus Torvalds } 1125abbf46aeSDavid S. Miller EXPORT_SYMBOL(ip6_dst_hoplimit); 11261da177e4SLinus Torvalds 11271da177e4SLinus Torvalds /* 11281da177e4SLinus Torvalds * 11291da177e4SLinus Torvalds */ 11301da177e4SLinus Torvalds 113186872cb5SThomas Graf int ip6_route_add(struct fib6_config *cfg) 11321da177e4SLinus Torvalds { 11331da177e4SLinus Torvalds int err; 11345578689aSDaniel Lezcano struct net *net = cfg->fc_nlinfo.nl_net; 11351da177e4SLinus Torvalds struct rt6_info *rt = NULL; 11361da177e4SLinus Torvalds struct net_device *dev = NULL; 11371da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 1138c71099acSThomas Graf struct fib6_table *table; 11391da177e4SLinus Torvalds int addr_type; 11401da177e4SLinus Torvalds 114186872cb5SThomas Graf if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128) 11421da177e4SLinus Torvalds return -EINVAL; 11431da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 114486872cb5SThomas Graf if (cfg->fc_src_len) 11451da177e4SLinus Torvalds return -EINVAL; 11461da177e4SLinus Torvalds #endif 114786872cb5SThomas Graf if (cfg->fc_ifindex) { 11481da177e4SLinus Torvalds err = -ENODEV; 11495578689aSDaniel Lezcano dev = dev_get_by_index(net, cfg->fc_ifindex); 11501da177e4SLinus Torvalds if (!dev) 11511da177e4SLinus Torvalds goto out; 11521da177e4SLinus Torvalds idev = in6_dev_get(dev); 11531da177e4SLinus Torvalds if (!idev) 11541da177e4SLinus Torvalds goto out; 11551da177e4SLinus Torvalds } 11561da177e4SLinus Torvalds 115786872cb5SThomas Graf if (cfg->fc_metric == 0) 115886872cb5SThomas Graf cfg->fc_metric = IP6_RT_PRIO_USER; 11591da177e4SLinus Torvalds 11605578689aSDaniel Lezcano table = fib6_new_table(net, cfg->fc_table); 1161c71099acSThomas Graf if (table == NULL) { 1162c71099acSThomas Graf err = -ENOBUFS; 1163c71099acSThomas Graf goto out; 1164c71099acSThomas Graf } 1165c71099acSThomas Graf 116686393e52SAlexey Dobriyan rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops); 11671da177e4SLinus Torvalds 11681da177e4SLinus Torvalds if (rt == NULL) { 11691da177e4SLinus Torvalds err = -ENOMEM; 11701da177e4SLinus Torvalds goto out; 11711da177e4SLinus Torvalds } 11721da177e4SLinus Torvalds 1173d8d1f30bSChangli Gao rt->dst.obsolete = -1; 11746f704992SYOSHIFUJI Hideaki rt->rt6i_expires = (cfg->fc_flags & RTF_EXPIRES) ? 11756f704992SYOSHIFUJI Hideaki jiffies + clock_t_to_jiffies(cfg->fc_expires) : 11766f704992SYOSHIFUJI Hideaki 0; 11771da177e4SLinus Torvalds 117886872cb5SThomas Graf if (cfg->fc_protocol == RTPROT_UNSPEC) 117986872cb5SThomas Graf cfg->fc_protocol = RTPROT_BOOT; 118086872cb5SThomas Graf rt->rt6i_protocol = cfg->fc_protocol; 118186872cb5SThomas Graf 118286872cb5SThomas Graf addr_type = ipv6_addr_type(&cfg->fc_dst); 11831da177e4SLinus Torvalds 11841da177e4SLinus Torvalds if (addr_type & IPV6_ADDR_MULTICAST) 1185d8d1f30bSChangli Gao rt->dst.input = ip6_mc_input; 1186ab79ad14SMaciej Żenczykowski else if (cfg->fc_flags & RTF_LOCAL) 1187ab79ad14SMaciej Żenczykowski rt->dst.input = ip6_input; 11881da177e4SLinus Torvalds else 1189d8d1f30bSChangli Gao rt->dst.input = ip6_forward; 11901da177e4SLinus Torvalds 1191d8d1f30bSChangli Gao rt->dst.output = ip6_output; 11921da177e4SLinus Torvalds 119386872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); 119486872cb5SThomas Graf rt->rt6i_dst.plen = cfg->fc_dst_len; 11951da177e4SLinus Torvalds if (rt->rt6i_dst.plen == 128) 1196d8d1f30bSChangli Gao rt->dst.flags = DST_HOST; 11971da177e4SLinus Torvalds 11981da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 119986872cb5SThomas Graf ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len); 120086872cb5SThomas Graf rt->rt6i_src.plen = cfg->fc_src_len; 12011da177e4SLinus Torvalds #endif 12021da177e4SLinus Torvalds 120386872cb5SThomas Graf rt->rt6i_metric = cfg->fc_metric; 12041da177e4SLinus Torvalds 12051da177e4SLinus Torvalds /* We cannot add true routes via loopback here, 12061da177e4SLinus Torvalds they would result in kernel looping; promote them to reject routes 12071da177e4SLinus Torvalds */ 120886872cb5SThomas Graf if ((cfg->fc_flags & RTF_REJECT) || 1209ab79ad14SMaciej Żenczykowski (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK) 1210ab79ad14SMaciej Żenczykowski && !(cfg->fc_flags&RTF_LOCAL))) { 12111da177e4SLinus Torvalds /* hold loopback dev/idev if we haven't done so. */ 12125578689aSDaniel Lezcano if (dev != net->loopback_dev) { 12131da177e4SLinus Torvalds if (dev) { 12141da177e4SLinus Torvalds dev_put(dev); 12151da177e4SLinus Torvalds in6_dev_put(idev); 12161da177e4SLinus Torvalds } 12175578689aSDaniel Lezcano dev = net->loopback_dev; 12181da177e4SLinus Torvalds dev_hold(dev); 12191da177e4SLinus Torvalds idev = in6_dev_get(dev); 12201da177e4SLinus Torvalds if (!idev) { 12211da177e4SLinus Torvalds err = -ENODEV; 12221da177e4SLinus Torvalds goto out; 12231da177e4SLinus Torvalds } 12241da177e4SLinus Torvalds } 1225d8d1f30bSChangli Gao rt->dst.output = ip6_pkt_discard_out; 1226d8d1f30bSChangli Gao rt->dst.input = ip6_pkt_discard; 1227d8d1f30bSChangli Gao rt->dst.error = -ENETUNREACH; 12281da177e4SLinus Torvalds rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP; 12291da177e4SLinus Torvalds goto install_route; 12301da177e4SLinus Torvalds } 12311da177e4SLinus Torvalds 123286872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY) { 12331da177e4SLinus Torvalds struct in6_addr *gw_addr; 12341da177e4SLinus Torvalds int gwa_type; 12351da177e4SLinus Torvalds 123686872cb5SThomas Graf gw_addr = &cfg->fc_gateway; 123786872cb5SThomas Graf ipv6_addr_copy(&rt->rt6i_gateway, gw_addr); 12381da177e4SLinus Torvalds gwa_type = ipv6_addr_type(gw_addr); 12391da177e4SLinus Torvalds 12401da177e4SLinus Torvalds if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) { 12411da177e4SLinus Torvalds struct rt6_info *grt; 12421da177e4SLinus Torvalds 12431da177e4SLinus Torvalds /* IPv6 strictly inhibits using not link-local 12441da177e4SLinus Torvalds addresses as nexthop address. 12451da177e4SLinus Torvalds Otherwise, router will not able to send redirects. 12461da177e4SLinus Torvalds It is very good, but in some (rare!) circumstances 12471da177e4SLinus Torvalds (SIT, PtP, NBMA NOARP links) it is handy to allow 12481da177e4SLinus Torvalds some exceptions. --ANK 12491da177e4SLinus Torvalds */ 12501da177e4SLinus Torvalds err = -EINVAL; 12511da177e4SLinus Torvalds if (!(gwa_type&IPV6_ADDR_UNICAST)) 12521da177e4SLinus Torvalds goto out; 12531da177e4SLinus Torvalds 12545578689aSDaniel Lezcano grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1); 12551da177e4SLinus Torvalds 12561da177e4SLinus Torvalds err = -EHOSTUNREACH; 12571da177e4SLinus Torvalds if (grt == NULL) 12581da177e4SLinus Torvalds goto out; 12591da177e4SLinus Torvalds if (dev) { 12601da177e4SLinus Torvalds if (dev != grt->rt6i_dev) { 1261d8d1f30bSChangli Gao dst_release(&grt->dst); 12621da177e4SLinus Torvalds goto out; 12631da177e4SLinus Torvalds } 12641da177e4SLinus Torvalds } else { 12651da177e4SLinus Torvalds dev = grt->rt6i_dev; 12661da177e4SLinus Torvalds idev = grt->rt6i_idev; 12671da177e4SLinus Torvalds dev_hold(dev); 12681da177e4SLinus Torvalds in6_dev_hold(grt->rt6i_idev); 12691da177e4SLinus Torvalds } 12701da177e4SLinus Torvalds if (!(grt->rt6i_flags&RTF_GATEWAY)) 12711da177e4SLinus Torvalds err = 0; 1272d8d1f30bSChangli Gao dst_release(&grt->dst); 12731da177e4SLinus Torvalds 12741da177e4SLinus Torvalds if (err) 12751da177e4SLinus Torvalds goto out; 12761da177e4SLinus Torvalds } 12771da177e4SLinus Torvalds err = -EINVAL; 12781da177e4SLinus Torvalds if (dev == NULL || (dev->flags&IFF_LOOPBACK)) 12791da177e4SLinus Torvalds goto out; 12801da177e4SLinus Torvalds } 12811da177e4SLinus Torvalds 12821da177e4SLinus Torvalds err = -ENODEV; 12831da177e4SLinus Torvalds if (dev == NULL) 12841da177e4SLinus Torvalds goto out; 12851da177e4SLinus Torvalds 128686872cb5SThomas Graf if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) { 12871da177e4SLinus Torvalds rt->rt6i_nexthop = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev); 12881da177e4SLinus Torvalds if (IS_ERR(rt->rt6i_nexthop)) { 12891da177e4SLinus Torvalds err = PTR_ERR(rt->rt6i_nexthop); 12901da177e4SLinus Torvalds rt->rt6i_nexthop = NULL; 12911da177e4SLinus Torvalds goto out; 12921da177e4SLinus Torvalds } 12931da177e4SLinus Torvalds } 12941da177e4SLinus Torvalds 129586872cb5SThomas Graf rt->rt6i_flags = cfg->fc_flags; 12961da177e4SLinus Torvalds 12971da177e4SLinus Torvalds install_route: 129886872cb5SThomas Graf if (cfg->fc_mx) { 129986872cb5SThomas Graf struct nlattr *nla; 130086872cb5SThomas Graf int remaining; 13011da177e4SLinus Torvalds 130286872cb5SThomas Graf nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { 13038f4c1f9bSThomas Graf int type = nla_type(nla); 130486872cb5SThomas Graf 130586872cb5SThomas Graf if (type) { 130686872cb5SThomas Graf if (type > RTAX_MAX) { 13071da177e4SLinus Torvalds err = -EINVAL; 13081da177e4SLinus Torvalds goto out; 13091da177e4SLinus Torvalds } 131086872cb5SThomas Graf 1311defb3519SDavid S. Miller dst_metric_set(&rt->dst, type, nla_get_u32(nla)); 13121da177e4SLinus Torvalds } 13131da177e4SLinus Torvalds } 13141da177e4SLinus Torvalds } 13151da177e4SLinus Torvalds 1316d8d1f30bSChangli Gao rt->dst.dev = dev; 13171da177e4SLinus Torvalds rt->rt6i_idev = idev; 1318c71099acSThomas Graf rt->rt6i_table = table; 131963152fc0SDaniel Lezcano 1320c346dca1SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = dev_net(dev); 132163152fc0SDaniel Lezcano 132286872cb5SThomas Graf return __ip6_ins_rt(rt, &cfg->fc_nlinfo); 13231da177e4SLinus Torvalds 13241da177e4SLinus Torvalds out: 13251da177e4SLinus Torvalds if (dev) 13261da177e4SLinus Torvalds dev_put(dev); 13271da177e4SLinus Torvalds if (idev) 13281da177e4SLinus Torvalds in6_dev_put(idev); 13291da177e4SLinus Torvalds if (rt) 1330d8d1f30bSChangli Gao dst_free(&rt->dst); 13311da177e4SLinus Torvalds return err; 13321da177e4SLinus Torvalds } 13331da177e4SLinus Torvalds 133486872cb5SThomas Graf static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info) 13351da177e4SLinus Torvalds { 13361da177e4SLinus Torvalds int err; 1337c71099acSThomas Graf struct fib6_table *table; 1338c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(rt->rt6i_dev); 13391da177e4SLinus Torvalds 13408ed67789SDaniel Lezcano if (rt == net->ipv6.ip6_null_entry) 13416c813a72SPatrick McHardy return -ENOENT; 13426c813a72SPatrick McHardy 1343c71099acSThomas Graf table = rt->rt6i_table; 1344c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 13451da177e4SLinus Torvalds 134686872cb5SThomas Graf err = fib6_del(rt, info); 1347d8d1f30bSChangli Gao dst_release(&rt->dst); 13481da177e4SLinus Torvalds 1349c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds return err; 13521da177e4SLinus Torvalds } 13531da177e4SLinus Torvalds 1354e0a1ad73SThomas Graf int ip6_del_rt(struct rt6_info *rt) 1355e0a1ad73SThomas Graf { 13564d1169c1SDenis V. Lunev struct nl_info info = { 1357c346dca1SYOSHIFUJI Hideaki .nl_net = dev_net(rt->rt6i_dev), 13584d1169c1SDenis V. Lunev }; 1359528c4cebSDenis V. Lunev return __ip6_del_rt(rt, &info); 1360e0a1ad73SThomas Graf } 1361e0a1ad73SThomas Graf 136286872cb5SThomas Graf static int ip6_route_del(struct fib6_config *cfg) 13631da177e4SLinus Torvalds { 1364c71099acSThomas Graf struct fib6_table *table; 13651da177e4SLinus Torvalds struct fib6_node *fn; 13661da177e4SLinus Torvalds struct rt6_info *rt; 13671da177e4SLinus Torvalds int err = -ESRCH; 13681da177e4SLinus Torvalds 13695578689aSDaniel Lezcano table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); 1370c71099acSThomas Graf if (table == NULL) 1371c71099acSThomas Graf return err; 13721da177e4SLinus Torvalds 1373c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 1374c71099acSThomas Graf 1375c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, 137686872cb5SThomas Graf &cfg->fc_dst, cfg->fc_dst_len, 137786872cb5SThomas Graf &cfg->fc_src, cfg->fc_src_len); 13781da177e4SLinus Torvalds 13791da177e4SLinus Torvalds if (fn) { 1380d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 138186872cb5SThomas Graf if (cfg->fc_ifindex && 13821da177e4SLinus Torvalds (rt->rt6i_dev == NULL || 138386872cb5SThomas Graf rt->rt6i_dev->ifindex != cfg->fc_ifindex)) 13841da177e4SLinus Torvalds continue; 138586872cb5SThomas Graf if (cfg->fc_flags & RTF_GATEWAY && 138686872cb5SThomas Graf !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) 13871da177e4SLinus Torvalds continue; 138886872cb5SThomas Graf if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric) 13891da177e4SLinus Torvalds continue; 1390d8d1f30bSChangli Gao dst_hold(&rt->dst); 1391c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 13921da177e4SLinus Torvalds 139386872cb5SThomas Graf return __ip6_del_rt(rt, &cfg->fc_nlinfo); 13941da177e4SLinus Torvalds } 13951da177e4SLinus Torvalds } 1396c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 13971da177e4SLinus Torvalds 13981da177e4SLinus Torvalds return err; 13991da177e4SLinus Torvalds } 14001da177e4SLinus Torvalds 14011da177e4SLinus Torvalds /* 14021da177e4SLinus Torvalds * Handle redirects 14031da177e4SLinus Torvalds */ 1404a6279458SYOSHIFUJI Hideaki struct ip6rd_flowi { 1405a6279458SYOSHIFUJI Hideaki struct flowi fl; 1406a6279458SYOSHIFUJI Hideaki struct in6_addr gateway; 1407a6279458SYOSHIFUJI Hideaki }; 14081da177e4SLinus Torvalds 14098ed67789SDaniel Lezcano static struct rt6_info *__ip6_route_redirect(struct net *net, 14108ed67789SDaniel Lezcano struct fib6_table *table, 1411a6279458SYOSHIFUJI Hideaki struct flowi *fl, 1412a6279458SYOSHIFUJI Hideaki int flags) 1413a6279458SYOSHIFUJI Hideaki { 1414a6279458SYOSHIFUJI Hideaki struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl; 1415a6279458SYOSHIFUJI Hideaki struct rt6_info *rt; 1416a6279458SYOSHIFUJI Hideaki struct fib6_node *fn; 1417c71099acSThomas Graf 1418e843b9e1SYOSHIFUJI Hideaki /* 1419e843b9e1SYOSHIFUJI Hideaki * Get the "current" route for this destination and 1420e843b9e1SYOSHIFUJI Hideaki * check if the redirect has come from approriate router. 1421e843b9e1SYOSHIFUJI Hideaki * 1422e843b9e1SYOSHIFUJI Hideaki * RFC 2461 specifies that redirects should only be 1423e843b9e1SYOSHIFUJI Hideaki * accepted if they come from the nexthop to the target. 1424e843b9e1SYOSHIFUJI Hideaki * Due to the way the routes are chosen, this notion 1425e843b9e1SYOSHIFUJI Hideaki * is a bit fuzzy and one might need to check all possible 1426e843b9e1SYOSHIFUJI Hideaki * routes. 1427e843b9e1SYOSHIFUJI Hideaki */ 14281da177e4SLinus Torvalds 1429c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 1430a6279458SYOSHIFUJI Hideaki fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src); 1431e843b9e1SYOSHIFUJI Hideaki restart: 1432d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 14331da177e4SLinus Torvalds /* 14341da177e4SLinus Torvalds * Current route is on-link; redirect is always invalid. 14351da177e4SLinus Torvalds * 14361da177e4SLinus Torvalds * Seems, previous statement is not true. It could 14371da177e4SLinus Torvalds * be node, which looks for us as on-link (f.e. proxy ndisc) 14381da177e4SLinus Torvalds * But then router serving it might decide, that we should 14391da177e4SLinus Torvalds * know truth 8)8) --ANK (980726). 14401da177e4SLinus Torvalds */ 1441e843b9e1SYOSHIFUJI Hideaki if (rt6_check_expired(rt)) 1442e843b9e1SYOSHIFUJI Hideaki continue; 14431da177e4SLinus Torvalds if (!(rt->rt6i_flags & RTF_GATEWAY)) 1444e843b9e1SYOSHIFUJI Hideaki continue; 1445a6279458SYOSHIFUJI Hideaki if (fl->oif != rt->rt6i_dev->ifindex) 1446e843b9e1SYOSHIFUJI Hideaki continue; 1447a6279458SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway)) 1448e843b9e1SYOSHIFUJI Hideaki continue; 1449e843b9e1SYOSHIFUJI Hideaki break; 1450e843b9e1SYOSHIFUJI Hideaki } 1451a6279458SYOSHIFUJI Hideaki 1452cb15d9c2SYOSHIFUJI Hideaki if (!rt) 14538ed67789SDaniel Lezcano rt = net->ipv6.ip6_null_entry; 14548ed67789SDaniel Lezcano BACKTRACK(net, &fl->fl6_src); 1455cb15d9c2SYOSHIFUJI Hideaki out: 1456d8d1f30bSChangli Gao dst_hold(&rt->dst); 1457a6279458SYOSHIFUJI Hideaki 1458c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 14591da177e4SLinus Torvalds 1460a6279458SYOSHIFUJI Hideaki return rt; 1461a6279458SYOSHIFUJI Hideaki }; 1462a6279458SYOSHIFUJI Hideaki 1463a6279458SYOSHIFUJI Hideaki static struct rt6_info *ip6_route_redirect(struct in6_addr *dest, 1464a6279458SYOSHIFUJI Hideaki struct in6_addr *src, 1465a6279458SYOSHIFUJI Hideaki struct in6_addr *gateway, 1466a6279458SYOSHIFUJI Hideaki struct net_device *dev) 1467a6279458SYOSHIFUJI Hideaki { 1468adaa70bbSThomas Graf int flags = RT6_LOOKUP_F_HAS_SADDR; 1469c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 1470a6279458SYOSHIFUJI Hideaki struct ip6rd_flowi rdfl = { 1471a6279458SYOSHIFUJI Hideaki .fl = { 1472a6279458SYOSHIFUJI Hideaki .oif = dev->ifindex, 14735811662bSChangli Gao .fl6_dst = *dest, 14745811662bSChangli Gao .fl6_src = *src, 1475a6279458SYOSHIFUJI Hideaki }, 1476a6279458SYOSHIFUJI Hideaki }; 1477adaa70bbSThomas Graf 147886c36ce4SBrian Haley ipv6_addr_copy(&rdfl.gateway, gateway); 147986c36ce4SBrian Haley 1480adaa70bbSThomas Graf if (rt6_need_strict(dest)) 1481adaa70bbSThomas Graf flags |= RT6_LOOKUP_F_IFACE; 1482a6279458SYOSHIFUJI Hideaki 14835578689aSDaniel Lezcano return (struct rt6_info *)fib6_rule_lookup(net, (struct flowi *)&rdfl, 148458f09b78SDaniel Lezcano flags, __ip6_route_redirect); 1485a6279458SYOSHIFUJI Hideaki } 1486a6279458SYOSHIFUJI Hideaki 1487a6279458SYOSHIFUJI Hideaki void rt6_redirect(struct in6_addr *dest, struct in6_addr *src, 1488a6279458SYOSHIFUJI Hideaki struct in6_addr *saddr, 1489a6279458SYOSHIFUJI Hideaki struct neighbour *neigh, u8 *lladdr, int on_link) 1490a6279458SYOSHIFUJI Hideaki { 1491a6279458SYOSHIFUJI Hideaki struct rt6_info *rt, *nrt = NULL; 1492a6279458SYOSHIFUJI Hideaki struct netevent_redirect netevent; 1493c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(neigh->dev); 1494a6279458SYOSHIFUJI Hideaki 1495a6279458SYOSHIFUJI Hideaki rt = ip6_route_redirect(dest, src, saddr, neigh->dev); 1496a6279458SYOSHIFUJI Hideaki 14978ed67789SDaniel Lezcano if (rt == net->ipv6.ip6_null_entry) { 14981da177e4SLinus Torvalds if (net_ratelimit()) 14991da177e4SLinus Torvalds printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop " 15001da177e4SLinus Torvalds "for redirect target\n"); 1501a6279458SYOSHIFUJI Hideaki goto out; 15021da177e4SLinus Torvalds } 15031da177e4SLinus Torvalds 15041da177e4SLinus Torvalds /* 15051da177e4SLinus Torvalds * We have finally decided to accept it. 15061da177e4SLinus Torvalds */ 15071da177e4SLinus Torvalds 15081da177e4SLinus Torvalds neigh_update(neigh, lladdr, NUD_STALE, 15091da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 15101da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 15111da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 15121da177e4SLinus Torvalds NEIGH_UPDATE_F_ISROUTER)) 15131da177e4SLinus Torvalds ); 15141da177e4SLinus Torvalds 15151da177e4SLinus Torvalds /* 15161da177e4SLinus Torvalds * Redirect received -> path was valid. 15171da177e4SLinus Torvalds * Look, redirects are sent only in response to data packets, 15181da177e4SLinus Torvalds * so that this nexthop apparently is reachable. --ANK 15191da177e4SLinus Torvalds */ 1520d8d1f30bSChangli Gao dst_confirm(&rt->dst); 15211da177e4SLinus Torvalds 15221da177e4SLinus Torvalds /* Duplicate redirect: silently ignore. */ 1523d8d1f30bSChangli Gao if (neigh == rt->dst.neighbour) 15241da177e4SLinus Torvalds goto out; 15251da177e4SLinus Torvalds 15261da177e4SLinus Torvalds nrt = ip6_rt_copy(rt); 15271da177e4SLinus Torvalds if (nrt == NULL) 15281da177e4SLinus Torvalds goto out; 15291da177e4SLinus Torvalds 15301da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 15311da177e4SLinus Torvalds if (on_link) 15321da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 15331da177e4SLinus Torvalds 15341da177e4SLinus Torvalds ipv6_addr_copy(&nrt->rt6i_dst.addr, dest); 15351da177e4SLinus Torvalds nrt->rt6i_dst.plen = 128; 1536d8d1f30bSChangli Gao nrt->dst.flags |= DST_HOST; 15371da177e4SLinus Torvalds 15381da177e4SLinus Torvalds ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key); 15391da177e4SLinus Torvalds nrt->rt6i_nexthop = neigh_clone(neigh); 15401da177e4SLinus Torvalds 154140e22e8fSThomas Graf if (ip6_ins_rt(nrt)) 15421da177e4SLinus Torvalds goto out; 15431da177e4SLinus Torvalds 1544d8d1f30bSChangli Gao netevent.old = &rt->dst; 1545d8d1f30bSChangli Gao netevent.new = &nrt->dst; 15468d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 15478d71740cSTom Tucker 15481da177e4SLinus Torvalds if (rt->rt6i_flags&RTF_CACHE) { 1549e0a1ad73SThomas Graf ip6_del_rt(rt); 15501da177e4SLinus Torvalds return; 15511da177e4SLinus Torvalds } 15521da177e4SLinus Torvalds 15531da177e4SLinus Torvalds out: 1554d8d1f30bSChangli Gao dst_release(&rt->dst); 15551da177e4SLinus Torvalds } 15561da177e4SLinus Torvalds 15571da177e4SLinus Torvalds /* 15581da177e4SLinus Torvalds * Handle ICMP "packet too big" messages 15591da177e4SLinus Torvalds * i.e. Path MTU discovery 15601da177e4SLinus Torvalds */ 15611da177e4SLinus Torvalds 1562ae878ae2SMaciej Żenczykowski static void rt6_do_pmtu_disc(struct in6_addr *daddr, struct in6_addr *saddr, 1563ae878ae2SMaciej Żenczykowski struct net *net, u32 pmtu, int ifindex) 15641da177e4SLinus Torvalds { 15651da177e4SLinus Torvalds struct rt6_info *rt, *nrt; 15661da177e4SLinus Torvalds int allfrag = 0; 1567d3052b55SAndrey Vagin again: 1568ae878ae2SMaciej Żenczykowski rt = rt6_lookup(net, daddr, saddr, ifindex, 0); 15691da177e4SLinus Torvalds if (rt == NULL) 15701da177e4SLinus Torvalds return; 15711da177e4SLinus Torvalds 1572d3052b55SAndrey Vagin if (rt6_check_expired(rt)) { 1573d3052b55SAndrey Vagin ip6_del_rt(rt); 1574d3052b55SAndrey Vagin goto again; 1575d3052b55SAndrey Vagin } 1576d3052b55SAndrey Vagin 1577d8d1f30bSChangli Gao if (pmtu >= dst_mtu(&rt->dst)) 15781da177e4SLinus Torvalds goto out; 15791da177e4SLinus Torvalds 15801da177e4SLinus Torvalds if (pmtu < IPV6_MIN_MTU) { 15811da177e4SLinus Torvalds /* 15821da177e4SLinus Torvalds * According to RFC2460, PMTU is set to the IPv6 Minimum Link 15831da177e4SLinus Torvalds * MTU (1280) and a fragment header should always be included 15841da177e4SLinus Torvalds * after a node receiving Too Big message reporting PMTU is 15851da177e4SLinus Torvalds * less than the IPv6 Minimum Link MTU. 15861da177e4SLinus Torvalds */ 15871da177e4SLinus Torvalds pmtu = IPV6_MIN_MTU; 15881da177e4SLinus Torvalds allfrag = 1; 15891da177e4SLinus Torvalds } 15901da177e4SLinus Torvalds 15911da177e4SLinus Torvalds /* New mtu received -> path was valid. 15921da177e4SLinus Torvalds They are sent only in response to data packets, 15931da177e4SLinus Torvalds so that this nexthop apparently is reachable. --ANK 15941da177e4SLinus Torvalds */ 1595d8d1f30bSChangli Gao dst_confirm(&rt->dst); 15961da177e4SLinus Torvalds 15971da177e4SLinus Torvalds /* Host route. If it is static, it would be better 15981da177e4SLinus Torvalds not to override it, but add new one, so that 15991da177e4SLinus Torvalds when cache entry will expire old pmtu 16001da177e4SLinus Torvalds would return automatically. 16011da177e4SLinus Torvalds */ 16021da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) { 1603defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_MTU, pmtu); 1604defb3519SDavid S. Miller if (allfrag) { 1605defb3519SDavid S. Miller u32 features = dst_metric(&rt->dst, RTAX_FEATURES); 1606defb3519SDavid S. Miller features |= RTAX_FEATURE_ALLFRAG; 1607defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_FEATURES, features); 1608defb3519SDavid S. Miller } 1609d8d1f30bSChangli Gao dst_set_expires(&rt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires); 16101da177e4SLinus Torvalds rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES; 16111da177e4SLinus Torvalds goto out; 16121da177e4SLinus Torvalds } 16131da177e4SLinus Torvalds 16141da177e4SLinus Torvalds /* Network route. 16151da177e4SLinus Torvalds Two cases are possible: 16161da177e4SLinus Torvalds 1. It is connected route. Action: COW 16171da177e4SLinus Torvalds 2. It is gatewayed route or NONEXTHOP route. Action: clone it. 16181da177e4SLinus Torvalds */ 1619d5315b50SYOSHIFUJI Hideaki if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP)) 1620a1e78363SYOSHIFUJI Hideaki nrt = rt6_alloc_cow(rt, daddr, saddr); 1621d5315b50SYOSHIFUJI Hideaki else 1622d5315b50SYOSHIFUJI Hideaki nrt = rt6_alloc_clone(rt, daddr); 1623a1e78363SYOSHIFUJI Hideaki 1624d5315b50SYOSHIFUJI Hideaki if (nrt) { 1625defb3519SDavid S. Miller dst_metric_set(&nrt->dst, RTAX_MTU, pmtu); 1626defb3519SDavid S. Miller if (allfrag) { 1627defb3519SDavid S. Miller u32 features = dst_metric(&nrt->dst, RTAX_FEATURES); 1628defb3519SDavid S. Miller features |= RTAX_FEATURE_ALLFRAG; 1629defb3519SDavid S. Miller dst_metric_set(&nrt->dst, RTAX_FEATURES, features); 1630defb3519SDavid S. Miller } 1631a1e78363SYOSHIFUJI Hideaki 16321da177e4SLinus Torvalds /* According to RFC 1981, detecting PMTU increase shouldn't be 1633a1e78363SYOSHIFUJI Hideaki * happened within 5 mins, the recommended timer is 10 mins. 1634a1e78363SYOSHIFUJI Hideaki * Here this route expiration time is set to ip6_rt_mtu_expires 1635a1e78363SYOSHIFUJI Hideaki * which is 10 mins. After 10 mins the decreased pmtu is expired 1636a1e78363SYOSHIFUJI Hideaki * and detecting PMTU increase will be automatically happened. 16371da177e4SLinus Torvalds */ 1638d8d1f30bSChangli Gao dst_set_expires(&nrt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires); 16391da177e4SLinus Torvalds nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES; 1640a1e78363SYOSHIFUJI Hideaki 164140e22e8fSThomas Graf ip6_ins_rt(nrt); 16421da177e4SLinus Torvalds } 16431da177e4SLinus Torvalds out: 1644d8d1f30bSChangli Gao dst_release(&rt->dst); 16451da177e4SLinus Torvalds } 16461da177e4SLinus Torvalds 1647ae878ae2SMaciej Żenczykowski void rt6_pmtu_discovery(struct in6_addr *daddr, struct in6_addr *saddr, 1648ae878ae2SMaciej Żenczykowski struct net_device *dev, u32 pmtu) 1649ae878ae2SMaciej Żenczykowski { 1650ae878ae2SMaciej Żenczykowski struct net *net = dev_net(dev); 1651ae878ae2SMaciej Żenczykowski 1652ae878ae2SMaciej Żenczykowski /* 1653ae878ae2SMaciej Żenczykowski * RFC 1981 states that a node "MUST reduce the size of the packets it 1654ae878ae2SMaciej Żenczykowski * is sending along the path" that caused the Packet Too Big message. 1655ae878ae2SMaciej Żenczykowski * Since it's not possible in the general case to determine which 1656ae878ae2SMaciej Żenczykowski * interface was used to send the original packet, we update the MTU 1657ae878ae2SMaciej Żenczykowski * on the interface that will be used to send future packets. We also 1658ae878ae2SMaciej Żenczykowski * update the MTU on the interface that received the Packet Too Big in 1659ae878ae2SMaciej Żenczykowski * case the original packet was forced out that interface with 1660ae878ae2SMaciej Żenczykowski * SO_BINDTODEVICE or similar. This is the next best thing to the 1661ae878ae2SMaciej Żenczykowski * correct behaviour, which would be to update the MTU on all 1662ae878ae2SMaciej Żenczykowski * interfaces. 1663ae878ae2SMaciej Żenczykowski */ 1664ae878ae2SMaciej Żenczykowski rt6_do_pmtu_disc(daddr, saddr, net, pmtu, 0); 1665ae878ae2SMaciej Żenczykowski rt6_do_pmtu_disc(daddr, saddr, net, pmtu, dev->ifindex); 1666ae878ae2SMaciej Żenczykowski } 1667ae878ae2SMaciej Żenczykowski 16681da177e4SLinus Torvalds /* 16691da177e4SLinus Torvalds * Misc support functions 16701da177e4SLinus Torvalds */ 16711da177e4SLinus Torvalds 16721da177e4SLinus Torvalds static struct rt6_info * ip6_rt_copy(struct rt6_info *ort) 16731da177e4SLinus Torvalds { 1674c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(ort->rt6i_dev); 167586393e52SAlexey Dobriyan struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops); 16761da177e4SLinus Torvalds 16771da177e4SLinus Torvalds if (rt) { 1678d8d1f30bSChangli Gao rt->dst.input = ort->dst.input; 1679d8d1f30bSChangli Gao rt->dst.output = ort->dst.output; 16801da177e4SLinus Torvalds 1681defb3519SDavid S. Miller dst_copy_metrics(&rt->dst, &ort->dst); 1682d8d1f30bSChangli Gao rt->dst.error = ort->dst.error; 1683d8d1f30bSChangli Gao rt->dst.dev = ort->dst.dev; 1684d8d1f30bSChangli Gao if (rt->dst.dev) 1685d8d1f30bSChangli Gao dev_hold(rt->dst.dev); 16861da177e4SLinus Torvalds rt->rt6i_idev = ort->rt6i_idev; 16871da177e4SLinus Torvalds if (rt->rt6i_idev) 16881da177e4SLinus Torvalds in6_dev_hold(rt->rt6i_idev); 1689d8d1f30bSChangli Gao rt->dst.lastuse = jiffies; 16901da177e4SLinus Torvalds rt->rt6i_expires = 0; 16911da177e4SLinus Torvalds 16921da177e4SLinus Torvalds ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway); 16931da177e4SLinus Torvalds rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES; 16941da177e4SLinus Torvalds rt->rt6i_metric = 0; 16951da177e4SLinus Torvalds 16961da177e4SLinus Torvalds memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 16971da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 16981da177e4SLinus Torvalds memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 16991da177e4SLinus Torvalds #endif 1700c71099acSThomas Graf rt->rt6i_table = ort->rt6i_table; 17011da177e4SLinus Torvalds } 17021da177e4SLinus Torvalds return rt; 17031da177e4SLinus Torvalds } 17041da177e4SLinus Torvalds 170570ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 1706efa2cea0SDaniel Lezcano static struct rt6_info *rt6_get_route_info(struct net *net, 1707efa2cea0SDaniel Lezcano struct in6_addr *prefix, int prefixlen, 170870ceb4f5SYOSHIFUJI Hideaki struct in6_addr *gwaddr, int ifindex) 170970ceb4f5SYOSHIFUJI Hideaki { 171070ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 171170ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt = NULL; 1712c71099acSThomas Graf struct fib6_table *table; 171370ceb4f5SYOSHIFUJI Hideaki 1714efa2cea0SDaniel Lezcano table = fib6_get_table(net, RT6_TABLE_INFO); 1715c71099acSThomas Graf if (table == NULL) 1716c71099acSThomas Graf return NULL; 1717c71099acSThomas Graf 1718c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 1719c71099acSThomas Graf fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0); 172070ceb4f5SYOSHIFUJI Hideaki if (!fn) 172170ceb4f5SYOSHIFUJI Hideaki goto out; 172270ceb4f5SYOSHIFUJI Hideaki 1723d8d1f30bSChangli Gao for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 172470ceb4f5SYOSHIFUJI Hideaki if (rt->rt6i_dev->ifindex != ifindex) 172570ceb4f5SYOSHIFUJI Hideaki continue; 172670ceb4f5SYOSHIFUJI Hideaki if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) 172770ceb4f5SYOSHIFUJI Hideaki continue; 172870ceb4f5SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr)) 172970ceb4f5SYOSHIFUJI Hideaki continue; 1730d8d1f30bSChangli Gao dst_hold(&rt->dst); 173170ceb4f5SYOSHIFUJI Hideaki break; 173270ceb4f5SYOSHIFUJI Hideaki } 173370ceb4f5SYOSHIFUJI Hideaki out: 1734c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 173570ceb4f5SYOSHIFUJI Hideaki return rt; 173670ceb4f5SYOSHIFUJI Hideaki } 173770ceb4f5SYOSHIFUJI Hideaki 1738efa2cea0SDaniel Lezcano static struct rt6_info *rt6_add_route_info(struct net *net, 1739efa2cea0SDaniel Lezcano struct in6_addr *prefix, int prefixlen, 174070ceb4f5SYOSHIFUJI Hideaki struct in6_addr *gwaddr, int ifindex, 174170ceb4f5SYOSHIFUJI Hideaki unsigned pref) 174270ceb4f5SYOSHIFUJI Hideaki { 174386872cb5SThomas Graf struct fib6_config cfg = { 174486872cb5SThomas Graf .fc_table = RT6_TABLE_INFO, 1745238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 174686872cb5SThomas Graf .fc_ifindex = ifindex, 174786872cb5SThomas Graf .fc_dst_len = prefixlen, 174886872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | 174986872cb5SThomas Graf RTF_UP | RTF_PREF(pref), 1750efa2cea0SDaniel Lezcano .fc_nlinfo.pid = 0, 1751efa2cea0SDaniel Lezcano .fc_nlinfo.nlh = NULL, 1752efa2cea0SDaniel Lezcano .fc_nlinfo.nl_net = net, 175386872cb5SThomas Graf }; 175470ceb4f5SYOSHIFUJI Hideaki 175586872cb5SThomas Graf ipv6_addr_copy(&cfg.fc_dst, prefix); 175686872cb5SThomas Graf ipv6_addr_copy(&cfg.fc_gateway, gwaddr); 175786872cb5SThomas Graf 1758e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 1759e317da96SYOSHIFUJI Hideaki if (!prefixlen) 176086872cb5SThomas Graf cfg.fc_flags |= RTF_DEFAULT; 176170ceb4f5SYOSHIFUJI Hideaki 176286872cb5SThomas Graf ip6_route_add(&cfg); 176370ceb4f5SYOSHIFUJI Hideaki 1764efa2cea0SDaniel Lezcano return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex); 176570ceb4f5SYOSHIFUJI Hideaki } 176670ceb4f5SYOSHIFUJI Hideaki #endif 176770ceb4f5SYOSHIFUJI Hideaki 17681da177e4SLinus Torvalds struct rt6_info *rt6_get_dflt_router(struct in6_addr *addr, struct net_device *dev) 17691da177e4SLinus Torvalds { 17701da177e4SLinus Torvalds struct rt6_info *rt; 1771c71099acSThomas Graf struct fib6_table *table; 17721da177e4SLinus Torvalds 1773c346dca1SYOSHIFUJI Hideaki table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT); 1774c71099acSThomas Graf if (table == NULL) 1775c71099acSThomas Graf return NULL; 17761da177e4SLinus Torvalds 1777c71099acSThomas Graf write_lock_bh(&table->tb6_lock); 1778d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) { 17791da177e4SLinus Torvalds if (dev == rt->rt6i_dev && 1780045927ffSYOSHIFUJI Hideaki ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 17811da177e4SLinus Torvalds ipv6_addr_equal(&rt->rt6i_gateway, addr)) 17821da177e4SLinus Torvalds break; 17831da177e4SLinus Torvalds } 17841da177e4SLinus Torvalds if (rt) 1785d8d1f30bSChangli Gao dst_hold(&rt->dst); 1786c71099acSThomas Graf write_unlock_bh(&table->tb6_lock); 17871da177e4SLinus Torvalds return rt; 17881da177e4SLinus Torvalds } 17891da177e4SLinus Torvalds 17901da177e4SLinus Torvalds struct rt6_info *rt6_add_dflt_router(struct in6_addr *gwaddr, 1791ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 1792ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 17931da177e4SLinus Torvalds { 179486872cb5SThomas Graf struct fib6_config cfg = { 179586872cb5SThomas Graf .fc_table = RT6_TABLE_DFLT, 1796238fc7eaSRami Rosen .fc_metric = IP6_RT_PRIO_USER, 179786872cb5SThomas Graf .fc_ifindex = dev->ifindex, 179886872cb5SThomas Graf .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | 179986872cb5SThomas Graf RTF_UP | RTF_EXPIRES | RTF_PREF(pref), 18005578689aSDaniel Lezcano .fc_nlinfo.pid = 0, 18015578689aSDaniel Lezcano .fc_nlinfo.nlh = NULL, 1802c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 180386872cb5SThomas Graf }; 18041da177e4SLinus Torvalds 180586872cb5SThomas Graf ipv6_addr_copy(&cfg.fc_gateway, gwaddr); 18061da177e4SLinus Torvalds 180786872cb5SThomas Graf ip6_route_add(&cfg); 18081da177e4SLinus Torvalds 18091da177e4SLinus Torvalds return rt6_get_dflt_router(gwaddr, dev); 18101da177e4SLinus Torvalds } 18111da177e4SLinus Torvalds 18127b4da532SDaniel Lezcano void rt6_purge_dflt_routers(struct net *net) 18131da177e4SLinus Torvalds { 18141da177e4SLinus Torvalds struct rt6_info *rt; 1815c71099acSThomas Graf struct fib6_table *table; 1816c71099acSThomas Graf 1817c71099acSThomas Graf /* NOTE: Keep consistent with rt6_get_dflt_router */ 18187b4da532SDaniel Lezcano table = fib6_get_table(net, RT6_TABLE_DFLT); 1819c71099acSThomas Graf if (table == NULL) 1820c71099acSThomas Graf return; 18211da177e4SLinus Torvalds 18221da177e4SLinus Torvalds restart: 1823c71099acSThomas Graf read_lock_bh(&table->tb6_lock); 1824d8d1f30bSChangli Gao for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { 18251da177e4SLinus Torvalds if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) { 1826d8d1f30bSChangli Gao dst_hold(&rt->dst); 1827c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 1828e0a1ad73SThomas Graf ip6_del_rt(rt); 18291da177e4SLinus Torvalds goto restart; 18301da177e4SLinus Torvalds } 18311da177e4SLinus Torvalds } 1832c71099acSThomas Graf read_unlock_bh(&table->tb6_lock); 18331da177e4SLinus Torvalds } 18341da177e4SLinus Torvalds 18355578689aSDaniel Lezcano static void rtmsg_to_fib6_config(struct net *net, 18365578689aSDaniel Lezcano struct in6_rtmsg *rtmsg, 183786872cb5SThomas Graf struct fib6_config *cfg) 183886872cb5SThomas Graf { 183986872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 184086872cb5SThomas Graf 184186872cb5SThomas Graf cfg->fc_table = RT6_TABLE_MAIN; 184286872cb5SThomas Graf cfg->fc_ifindex = rtmsg->rtmsg_ifindex; 184386872cb5SThomas Graf cfg->fc_metric = rtmsg->rtmsg_metric; 184486872cb5SThomas Graf cfg->fc_expires = rtmsg->rtmsg_info; 184586872cb5SThomas Graf cfg->fc_dst_len = rtmsg->rtmsg_dst_len; 184686872cb5SThomas Graf cfg->fc_src_len = rtmsg->rtmsg_src_len; 184786872cb5SThomas Graf cfg->fc_flags = rtmsg->rtmsg_flags; 184886872cb5SThomas Graf 18495578689aSDaniel Lezcano cfg->fc_nlinfo.nl_net = net; 1850f1243c2dSBenjamin Thery 185186872cb5SThomas Graf ipv6_addr_copy(&cfg->fc_dst, &rtmsg->rtmsg_dst); 185286872cb5SThomas Graf ipv6_addr_copy(&cfg->fc_src, &rtmsg->rtmsg_src); 185386872cb5SThomas Graf ipv6_addr_copy(&cfg->fc_gateway, &rtmsg->rtmsg_gateway); 185486872cb5SThomas Graf } 185586872cb5SThomas Graf 18565578689aSDaniel Lezcano int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) 18571da177e4SLinus Torvalds { 185886872cb5SThomas Graf struct fib6_config cfg; 18591da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 18601da177e4SLinus Torvalds int err; 18611da177e4SLinus Torvalds 18621da177e4SLinus Torvalds switch(cmd) { 18631da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 18641da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 18651da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 18661da177e4SLinus Torvalds return -EPERM; 18671da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 18681da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 18691da177e4SLinus Torvalds if (err) 18701da177e4SLinus Torvalds return -EFAULT; 18711da177e4SLinus Torvalds 18725578689aSDaniel Lezcano rtmsg_to_fib6_config(net, &rtmsg, &cfg); 187386872cb5SThomas Graf 18741da177e4SLinus Torvalds rtnl_lock(); 18751da177e4SLinus Torvalds switch (cmd) { 18761da177e4SLinus Torvalds case SIOCADDRT: 187786872cb5SThomas Graf err = ip6_route_add(&cfg); 18781da177e4SLinus Torvalds break; 18791da177e4SLinus Torvalds case SIOCDELRT: 188086872cb5SThomas Graf err = ip6_route_del(&cfg); 18811da177e4SLinus Torvalds break; 18821da177e4SLinus Torvalds default: 18831da177e4SLinus Torvalds err = -EINVAL; 18841da177e4SLinus Torvalds } 18851da177e4SLinus Torvalds rtnl_unlock(); 18861da177e4SLinus Torvalds 18871da177e4SLinus Torvalds return err; 18883ff50b79SStephen Hemminger } 18891da177e4SLinus Torvalds 18901da177e4SLinus Torvalds return -EINVAL; 18911da177e4SLinus Torvalds } 18921da177e4SLinus Torvalds 18931da177e4SLinus Torvalds /* 18941da177e4SLinus Torvalds * Drop the packet on the floor 18951da177e4SLinus Torvalds */ 18961da177e4SLinus Torvalds 1897d5fdd6baSBrian Haley static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) 18981da177e4SLinus Torvalds { 1899612f09e8SYOSHIFUJI Hideaki int type; 1900adf30907SEric Dumazet struct dst_entry *dst = skb_dst(skb); 1901612f09e8SYOSHIFUJI Hideaki switch (ipstats_mib_noroutes) { 1902612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_INNOROUTES: 19030660e03fSArnaldo Carvalho de Melo type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); 190445bb0060SUlrich Weber if (type == IPV6_ADDR_ANY) { 19053bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 19063bd653c8SDenis V. Lunev IPSTATS_MIB_INADDRERRORS); 1907612f09e8SYOSHIFUJI Hideaki break; 1908612f09e8SYOSHIFUJI Hideaki } 1909612f09e8SYOSHIFUJI Hideaki /* FALLTHROUGH */ 1910612f09e8SYOSHIFUJI Hideaki case IPSTATS_MIB_OUTNOROUTES: 19113bd653c8SDenis V. Lunev IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), 19123bd653c8SDenis V. Lunev ipstats_mib_noroutes); 1913612f09e8SYOSHIFUJI Hideaki break; 1914612f09e8SYOSHIFUJI Hideaki } 19153ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); 19161da177e4SLinus Torvalds kfree_skb(skb); 19171da177e4SLinus Torvalds return 0; 19181da177e4SLinus Torvalds } 19191da177e4SLinus Torvalds 19209ce8ade0SThomas Graf static int ip6_pkt_discard(struct sk_buff *skb) 19219ce8ade0SThomas Graf { 1922612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); 19239ce8ade0SThomas Graf } 19249ce8ade0SThomas Graf 192520380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb) 19261da177e4SLinus Torvalds { 1927adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 1928612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 19291da177e4SLinus Torvalds } 19301da177e4SLinus Torvalds 19316723ab54SDavid S. Miller #ifdef CONFIG_IPV6_MULTIPLE_TABLES 19326723ab54SDavid S. Miller 19339ce8ade0SThomas Graf static int ip6_pkt_prohibit(struct sk_buff *skb) 19349ce8ade0SThomas Graf { 1935612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 19369ce8ade0SThomas Graf } 19379ce8ade0SThomas Graf 19389ce8ade0SThomas Graf static int ip6_pkt_prohibit_out(struct sk_buff *skb) 19399ce8ade0SThomas Graf { 1940adf30907SEric Dumazet skb->dev = skb_dst(skb)->dev; 1941612f09e8SYOSHIFUJI Hideaki return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 19429ce8ade0SThomas Graf } 19439ce8ade0SThomas Graf 19446723ab54SDavid S. Miller #endif 19456723ab54SDavid S. Miller 19461da177e4SLinus Torvalds /* 19471da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 19481da177e4SLinus Torvalds */ 19491da177e4SLinus Torvalds 19501da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, 19511da177e4SLinus Torvalds const struct in6_addr *addr, 19521da177e4SLinus Torvalds int anycast) 19531da177e4SLinus Torvalds { 1954c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 195586393e52SAlexey Dobriyan struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops); 195614deae41SDavid S. Miller struct neighbour *neigh; 19571da177e4SLinus Torvalds 195840385653SBen Greear if (rt == NULL) { 195940385653SBen Greear if (net_ratelimit()) 196040385653SBen Greear pr_warning("IPv6: Maximum number of routes reached," 196140385653SBen Greear " consider increasing route/max_size.\n"); 19621da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 196340385653SBen Greear } 19641da177e4SLinus Torvalds 19655578689aSDaniel Lezcano dev_hold(net->loopback_dev); 19661da177e4SLinus Torvalds in6_dev_hold(idev); 19671da177e4SLinus Torvalds 1968d8d1f30bSChangli Gao rt->dst.flags = DST_HOST; 1969d8d1f30bSChangli Gao rt->dst.input = ip6_input; 1970d8d1f30bSChangli Gao rt->dst.output = ip6_output; 19715578689aSDaniel Lezcano rt->rt6i_dev = net->loopback_dev; 19721da177e4SLinus Torvalds rt->rt6i_idev = idev; 1973defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_HOPLIMIT, -1); 1974d8d1f30bSChangli Gao rt->dst.obsolete = -1; 19751da177e4SLinus Torvalds 19761da177e4SLinus Torvalds rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP; 197758c4fb86SYOSHIFUJI Hideaki if (anycast) 197858c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 197958c4fb86SYOSHIFUJI Hideaki else 19801da177e4SLinus Torvalds rt->rt6i_flags |= RTF_LOCAL; 198114deae41SDavid S. Miller neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway); 198214deae41SDavid S. Miller if (IS_ERR(neigh)) { 1983d8d1f30bSChangli Gao dst_free(&rt->dst); 198414deae41SDavid S. Miller 198514deae41SDavid S. Miller /* We are casting this because that is the return 198614deae41SDavid S. Miller * value type. But an errno encoded pointer is the 198714deae41SDavid S. Miller * same regardless of the underlying pointer type, 198814deae41SDavid S. Miller * and that's what we are returning. So this is OK. 198914deae41SDavid S. Miller */ 199014deae41SDavid S. Miller return (struct rt6_info *) neigh; 19911da177e4SLinus Torvalds } 199214deae41SDavid S. Miller rt->rt6i_nexthop = neigh; 19931da177e4SLinus Torvalds 19941da177e4SLinus Torvalds ipv6_addr_copy(&rt->rt6i_dst.addr, addr); 19951da177e4SLinus Torvalds rt->rt6i_dst.plen = 128; 19965578689aSDaniel Lezcano rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL); 19971da177e4SLinus Torvalds 1998d8d1f30bSChangli Gao atomic_set(&rt->dst.__refcnt, 1); 19991da177e4SLinus Torvalds 20001da177e4SLinus Torvalds return rt; 20011da177e4SLinus Torvalds } 20021da177e4SLinus Torvalds 20038ed67789SDaniel Lezcano struct arg_dev_net { 20048ed67789SDaniel Lezcano struct net_device *dev; 20058ed67789SDaniel Lezcano struct net *net; 20068ed67789SDaniel Lezcano }; 20078ed67789SDaniel Lezcano 20081da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg) 20091da177e4SLinus Torvalds { 2010bc3ef660Sstephen hemminger const struct arg_dev_net *adn = arg; 2011bc3ef660Sstephen hemminger const struct net_device *dev = adn->dev; 20128ed67789SDaniel Lezcano 2013bc3ef660Sstephen hemminger if ((rt->rt6i_dev == dev || dev == NULL) && 2014bc3ef660Sstephen hemminger rt != adn->net->ipv6.ip6_null_entry) { 20151da177e4SLinus Torvalds RT6_TRACE("deleted by ifdown %p\n", rt); 20161da177e4SLinus Torvalds return -1; 20171da177e4SLinus Torvalds } 20181da177e4SLinus Torvalds return 0; 20191da177e4SLinus Torvalds } 20201da177e4SLinus Torvalds 2021f3db4851SDaniel Lezcano void rt6_ifdown(struct net *net, struct net_device *dev) 20221da177e4SLinus Torvalds { 20238ed67789SDaniel Lezcano struct arg_dev_net adn = { 20248ed67789SDaniel Lezcano .dev = dev, 20258ed67789SDaniel Lezcano .net = net, 20268ed67789SDaniel Lezcano }; 20278ed67789SDaniel Lezcano 20288ed67789SDaniel Lezcano fib6_clean_all(net, fib6_ifdown, 0, &adn); 20291e493d19SDavid S. Miller icmp6_clean_all(fib6_ifdown, &adn); 20301da177e4SLinus Torvalds } 20311da177e4SLinus Torvalds 20321da177e4SLinus Torvalds struct rt6_mtu_change_arg 20331da177e4SLinus Torvalds { 20341da177e4SLinus Torvalds struct net_device *dev; 20351da177e4SLinus Torvalds unsigned mtu; 20361da177e4SLinus Torvalds }; 20371da177e4SLinus Torvalds 20381da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg) 20391da177e4SLinus Torvalds { 20401da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 20411da177e4SLinus Torvalds struct inet6_dev *idev; 20421da177e4SLinus Torvalds 20431da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 20441da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 20451da177e4SLinus Torvalds We still use this lock to block changes 20461da177e4SLinus Torvalds caused by addrconf/ndisc. 20471da177e4SLinus Torvalds */ 20481da177e4SLinus Torvalds 20491da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 20501da177e4SLinus Torvalds if (idev == NULL) 20511da177e4SLinus Torvalds return 0; 20521da177e4SLinus Torvalds 20531da177e4SLinus Torvalds /* For administrative MTU increase, there is no way to discover 20541da177e4SLinus Torvalds IPv6 PMTU increase, so PMTU increase should be updated here. 20551da177e4SLinus Torvalds Since RFC 1981 doesn't include administrative MTU increase 20561da177e4SLinus Torvalds update PMTU increase is a MUST. (i.e. jumbo frame) 20571da177e4SLinus Torvalds */ 20581da177e4SLinus Torvalds /* 20591da177e4SLinus Torvalds If new MTU is less than route PMTU, this new MTU will be the 20601da177e4SLinus Torvalds lowest MTU in the path, update the route PMTU to reflect PMTU 20611da177e4SLinus Torvalds decreases; if new MTU is greater than route PMTU, and the 20621da177e4SLinus Torvalds old MTU is the lowest MTU in the path, update the route PMTU 20631da177e4SLinus Torvalds to reflect the increase. In this case if the other nodes' MTU 20641da177e4SLinus Torvalds also have the lowest MTU, TOO BIG MESSAGE will be lead to 20651da177e4SLinus Torvalds PMTU discouvery. 20661da177e4SLinus Torvalds */ 20671da177e4SLinus Torvalds if (rt->rt6i_dev == arg->dev && 2068d8d1f30bSChangli Gao !dst_metric_locked(&rt->dst, RTAX_MTU) && 2069d8d1f30bSChangli Gao (dst_mtu(&rt->dst) >= arg->mtu || 2070d8d1f30bSChangli Gao (dst_mtu(&rt->dst) < arg->mtu && 2071d8d1f30bSChangli Gao dst_mtu(&rt->dst) == idev->cnf.mtu6))) { 2072defb3519SDavid S. Miller dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu); 2073566cfd8fSSimon Arlott } 20741da177e4SLinus Torvalds return 0; 20751da177e4SLinus Torvalds } 20761da177e4SLinus Torvalds 20771da177e4SLinus Torvalds void rt6_mtu_change(struct net_device *dev, unsigned mtu) 20781da177e4SLinus Torvalds { 2079c71099acSThomas Graf struct rt6_mtu_change_arg arg = { 2080c71099acSThomas Graf .dev = dev, 2081c71099acSThomas Graf .mtu = mtu, 2082c71099acSThomas Graf }; 20831da177e4SLinus Torvalds 2084c346dca1SYOSHIFUJI Hideaki fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg); 20851da177e4SLinus Torvalds } 20861da177e4SLinus Torvalds 2087ef7c79edSPatrick McHardy static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { 20885176f91eSThomas Graf [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, 208986872cb5SThomas Graf [RTA_OIF] = { .type = NLA_U32 }, 2090ab364a6fSThomas Graf [RTA_IIF] = { .type = NLA_U32 }, 209186872cb5SThomas Graf [RTA_PRIORITY] = { .type = NLA_U32 }, 209286872cb5SThomas Graf [RTA_METRICS] = { .type = NLA_NESTED }, 209386872cb5SThomas Graf }; 209486872cb5SThomas Graf 209586872cb5SThomas Graf static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 209686872cb5SThomas Graf struct fib6_config *cfg) 20971da177e4SLinus Torvalds { 209886872cb5SThomas Graf struct rtmsg *rtm; 209986872cb5SThomas Graf struct nlattr *tb[RTA_MAX+1]; 210086872cb5SThomas Graf int err; 21011da177e4SLinus Torvalds 210286872cb5SThomas Graf err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy); 210386872cb5SThomas Graf if (err < 0) 210486872cb5SThomas Graf goto errout; 21051da177e4SLinus Torvalds 210686872cb5SThomas Graf err = -EINVAL; 210786872cb5SThomas Graf rtm = nlmsg_data(nlh); 210886872cb5SThomas Graf memset(cfg, 0, sizeof(*cfg)); 210986872cb5SThomas Graf 211086872cb5SThomas Graf cfg->fc_table = rtm->rtm_table; 211186872cb5SThomas Graf cfg->fc_dst_len = rtm->rtm_dst_len; 211286872cb5SThomas Graf cfg->fc_src_len = rtm->rtm_src_len; 211386872cb5SThomas Graf cfg->fc_flags = RTF_UP; 211486872cb5SThomas Graf cfg->fc_protocol = rtm->rtm_protocol; 211586872cb5SThomas Graf 211686872cb5SThomas Graf if (rtm->rtm_type == RTN_UNREACHABLE) 211786872cb5SThomas Graf cfg->fc_flags |= RTF_REJECT; 211886872cb5SThomas Graf 2119ab79ad14SMaciej Żenczykowski if (rtm->rtm_type == RTN_LOCAL) 2120ab79ad14SMaciej Żenczykowski cfg->fc_flags |= RTF_LOCAL; 2121ab79ad14SMaciej Żenczykowski 212286872cb5SThomas Graf cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid; 212386872cb5SThomas Graf cfg->fc_nlinfo.nlh = nlh; 21243b1e0a65SYOSHIFUJI Hideaki cfg->fc_nlinfo.nl_net = sock_net(skb->sk); 212586872cb5SThomas Graf 212686872cb5SThomas Graf if (tb[RTA_GATEWAY]) { 212786872cb5SThomas Graf nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16); 212886872cb5SThomas Graf cfg->fc_flags |= RTF_GATEWAY; 21291da177e4SLinus Torvalds } 213086872cb5SThomas Graf 213186872cb5SThomas Graf if (tb[RTA_DST]) { 213286872cb5SThomas Graf int plen = (rtm->rtm_dst_len + 7) >> 3; 213386872cb5SThomas Graf 213486872cb5SThomas Graf if (nla_len(tb[RTA_DST]) < plen) 213586872cb5SThomas Graf goto errout; 213686872cb5SThomas Graf 213786872cb5SThomas Graf nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); 21381da177e4SLinus Torvalds } 213986872cb5SThomas Graf 214086872cb5SThomas Graf if (tb[RTA_SRC]) { 214186872cb5SThomas Graf int plen = (rtm->rtm_src_len + 7) >> 3; 214286872cb5SThomas Graf 214386872cb5SThomas Graf if (nla_len(tb[RTA_SRC]) < plen) 214486872cb5SThomas Graf goto errout; 214586872cb5SThomas Graf 214686872cb5SThomas Graf nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); 21471da177e4SLinus Torvalds } 214886872cb5SThomas Graf 214986872cb5SThomas Graf if (tb[RTA_OIF]) 215086872cb5SThomas Graf cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 215186872cb5SThomas Graf 215286872cb5SThomas Graf if (tb[RTA_PRIORITY]) 215386872cb5SThomas Graf cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); 215486872cb5SThomas Graf 215586872cb5SThomas Graf if (tb[RTA_METRICS]) { 215686872cb5SThomas Graf cfg->fc_mx = nla_data(tb[RTA_METRICS]); 215786872cb5SThomas Graf cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); 21581da177e4SLinus Torvalds } 215986872cb5SThomas Graf 216086872cb5SThomas Graf if (tb[RTA_TABLE]) 216186872cb5SThomas Graf cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); 216286872cb5SThomas Graf 216386872cb5SThomas Graf err = 0; 216486872cb5SThomas Graf errout: 216586872cb5SThomas Graf return err; 21661da177e4SLinus Torvalds } 21671da177e4SLinus Torvalds 2168c127ea2cSThomas Graf static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 21691da177e4SLinus Torvalds { 217086872cb5SThomas Graf struct fib6_config cfg; 217186872cb5SThomas Graf int err; 21721da177e4SLinus Torvalds 217386872cb5SThomas Graf err = rtm_to_fib6_config(skb, nlh, &cfg); 217486872cb5SThomas Graf if (err < 0) 217586872cb5SThomas Graf return err; 217686872cb5SThomas Graf 217786872cb5SThomas Graf return ip6_route_del(&cfg); 21781da177e4SLinus Torvalds } 21791da177e4SLinus Torvalds 2180c127ea2cSThomas Graf static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 21811da177e4SLinus Torvalds { 218286872cb5SThomas Graf struct fib6_config cfg; 218386872cb5SThomas Graf int err; 21841da177e4SLinus Torvalds 218586872cb5SThomas Graf err = rtm_to_fib6_config(skb, nlh, &cfg); 218686872cb5SThomas Graf if (err < 0) 218786872cb5SThomas Graf return err; 218886872cb5SThomas Graf 218986872cb5SThomas Graf return ip6_route_add(&cfg); 21901da177e4SLinus Torvalds } 21911da177e4SLinus Torvalds 2192339bf98fSThomas Graf static inline size_t rt6_nlmsg_size(void) 2193339bf98fSThomas Graf { 2194339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct rtmsg)) 2195339bf98fSThomas Graf + nla_total_size(16) /* RTA_SRC */ 2196339bf98fSThomas Graf + nla_total_size(16) /* RTA_DST */ 2197339bf98fSThomas Graf + nla_total_size(16) /* RTA_GATEWAY */ 2198339bf98fSThomas Graf + nla_total_size(16) /* RTA_PREFSRC */ 2199339bf98fSThomas Graf + nla_total_size(4) /* RTA_TABLE */ 2200339bf98fSThomas Graf + nla_total_size(4) /* RTA_IIF */ 2201339bf98fSThomas Graf + nla_total_size(4) /* RTA_OIF */ 2202339bf98fSThomas Graf + nla_total_size(4) /* RTA_PRIORITY */ 22036a2b9ce0SNoriaki TAKAMIYA + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 2204339bf98fSThomas Graf + nla_total_size(sizeof(struct rta_cacheinfo)); 2205339bf98fSThomas Graf } 2206339bf98fSThomas Graf 2207191cd582SBrian Haley static int rt6_fill_node(struct net *net, 2208191cd582SBrian Haley struct sk_buff *skb, struct rt6_info *rt, 22090d51aa80SJamal Hadi Salim struct in6_addr *dst, struct in6_addr *src, 22100d51aa80SJamal Hadi Salim int iif, int type, u32 pid, u32 seq, 22117bc570c8SYOSHIFUJI Hideaki int prefix, int nowait, unsigned int flags) 22121da177e4SLinus Torvalds { 22131da177e4SLinus Torvalds struct rtmsg *rtm; 22141da177e4SLinus Torvalds struct nlmsghdr *nlh; 2215e3703b3dSThomas Graf long expires; 22169e762a4aSPatrick McHardy u32 table; 22171da177e4SLinus Torvalds 22181da177e4SLinus Torvalds if (prefix) { /* user wants prefix routes only */ 22191da177e4SLinus Torvalds if (!(rt->rt6i_flags & RTF_PREFIX_RT)) { 22201da177e4SLinus Torvalds /* success since this is not a prefix route */ 22211da177e4SLinus Torvalds return 1; 22221da177e4SLinus Torvalds } 22231da177e4SLinus Torvalds } 22241da177e4SLinus Torvalds 22252d7202bfSThomas Graf nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags); 22262d7202bfSThomas Graf if (nlh == NULL) 222726932566SPatrick McHardy return -EMSGSIZE; 22282d7202bfSThomas Graf 22292d7202bfSThomas Graf rtm = nlmsg_data(nlh); 22301da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 22311da177e4SLinus Torvalds rtm->rtm_dst_len = rt->rt6i_dst.plen; 22321da177e4SLinus Torvalds rtm->rtm_src_len = rt->rt6i_src.plen; 22331da177e4SLinus Torvalds rtm->rtm_tos = 0; 2234c71099acSThomas Graf if (rt->rt6i_table) 22359e762a4aSPatrick McHardy table = rt->rt6i_table->tb6_id; 2236c71099acSThomas Graf else 22379e762a4aSPatrick McHardy table = RT6_TABLE_UNSPEC; 22389e762a4aSPatrick McHardy rtm->rtm_table = table; 22392d7202bfSThomas Graf NLA_PUT_U32(skb, RTA_TABLE, table); 22401da177e4SLinus Torvalds if (rt->rt6i_flags&RTF_REJECT) 22411da177e4SLinus Torvalds rtm->rtm_type = RTN_UNREACHABLE; 2242ab79ad14SMaciej Żenczykowski else if (rt->rt6i_flags&RTF_LOCAL) 2243ab79ad14SMaciej Żenczykowski rtm->rtm_type = RTN_LOCAL; 22441da177e4SLinus Torvalds else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK)) 22451da177e4SLinus Torvalds rtm->rtm_type = RTN_LOCAL; 22461da177e4SLinus Torvalds else 22471da177e4SLinus Torvalds rtm->rtm_type = RTN_UNICAST; 22481da177e4SLinus Torvalds rtm->rtm_flags = 0; 22491da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 22501da177e4SLinus Torvalds rtm->rtm_protocol = rt->rt6i_protocol; 22511da177e4SLinus Torvalds if (rt->rt6i_flags&RTF_DYNAMIC) 22521da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_REDIRECT; 22531da177e4SLinus Torvalds else if (rt->rt6i_flags & RTF_ADDRCONF) 22541da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_KERNEL; 22551da177e4SLinus Torvalds else if (rt->rt6i_flags&RTF_DEFAULT) 22561da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_RA; 22571da177e4SLinus Torvalds 22581da177e4SLinus Torvalds if (rt->rt6i_flags&RTF_CACHE) 22591da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 22601da177e4SLinus Torvalds 22611da177e4SLinus Torvalds if (dst) { 22622d7202bfSThomas Graf NLA_PUT(skb, RTA_DST, 16, dst); 22631da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 22641da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 22652d7202bfSThomas Graf NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr); 22661da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 22671da177e4SLinus Torvalds if (src) { 22682d7202bfSThomas Graf NLA_PUT(skb, RTA_SRC, 16, src); 22691da177e4SLinus Torvalds rtm->rtm_src_len = 128; 22701da177e4SLinus Torvalds } else if (rtm->rtm_src_len) 22712d7202bfSThomas Graf NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr); 22721da177e4SLinus Torvalds #endif 22737bc570c8SYOSHIFUJI Hideaki if (iif) { 22747bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 22757bc570c8SYOSHIFUJI Hideaki if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) { 22768229efdaSBenjamin Thery int err = ip6mr_get_route(net, skb, rtm, nowait); 22777bc570c8SYOSHIFUJI Hideaki if (err <= 0) { 22787bc570c8SYOSHIFUJI Hideaki if (!nowait) { 22797bc570c8SYOSHIFUJI Hideaki if (err == 0) 22807bc570c8SYOSHIFUJI Hideaki return 0; 22817bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 22827bc570c8SYOSHIFUJI Hideaki } else { 22837bc570c8SYOSHIFUJI Hideaki if (err == -EMSGSIZE) 22847bc570c8SYOSHIFUJI Hideaki goto nla_put_failure; 22857bc570c8SYOSHIFUJI Hideaki } 22867bc570c8SYOSHIFUJI Hideaki } 22877bc570c8SYOSHIFUJI Hideaki } else 22887bc570c8SYOSHIFUJI Hideaki #endif 22892d7202bfSThomas Graf NLA_PUT_U32(skb, RTA_IIF, iif); 22907bc570c8SYOSHIFUJI Hideaki } else if (dst) { 2291d8d1f30bSChangli Gao struct inet6_dev *idev = ip6_dst_idev(&rt->dst); 22921da177e4SLinus Torvalds struct in6_addr saddr_buf; 2293191cd582SBrian Haley if (ipv6_dev_get_saddr(net, idev ? idev->dev : NULL, 22947cbca67cSYOSHIFUJI Hideaki dst, 0, &saddr_buf) == 0) 22952d7202bfSThomas Graf NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf); 22961da177e4SLinus Torvalds } 22972d7202bfSThomas Graf 2298defb3519SDavid S. Miller if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0) 22992d7202bfSThomas Graf goto nla_put_failure; 23002d7202bfSThomas Graf 2301d8d1f30bSChangli Gao if (rt->dst.neighbour) 2302d8d1f30bSChangli Gao NLA_PUT(skb, RTA_GATEWAY, 16, &rt->dst.neighbour->primary_key); 23032d7202bfSThomas Graf 2304d8d1f30bSChangli Gao if (rt->dst.dev) 23052d7202bfSThomas Graf NLA_PUT_U32(skb, RTA_OIF, rt->rt6i_dev->ifindex); 23062d7202bfSThomas Graf 23072d7202bfSThomas Graf NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric); 2308e3703b3dSThomas Graf 230936e3deaeSYOSHIFUJI Hideaki if (!(rt->rt6i_flags & RTF_EXPIRES)) 231036e3deaeSYOSHIFUJI Hideaki expires = 0; 231136e3deaeSYOSHIFUJI Hideaki else if (rt->rt6i_expires - jiffies < INT_MAX) 231236e3deaeSYOSHIFUJI Hideaki expires = rt->rt6i_expires - jiffies; 231336e3deaeSYOSHIFUJI Hideaki else 231436e3deaeSYOSHIFUJI Hideaki expires = INT_MAX; 231569cdf8f9SYOSHIFUJI Hideaki 2316d8d1f30bSChangli Gao if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0, 2317d8d1f30bSChangli Gao expires, rt->dst.error) < 0) 2318e3703b3dSThomas Graf goto nla_put_failure; 23191da177e4SLinus Torvalds 23202d7202bfSThomas Graf return nlmsg_end(skb, nlh); 23212d7202bfSThomas Graf 23222d7202bfSThomas Graf nla_put_failure: 232326932566SPatrick McHardy nlmsg_cancel(skb, nlh); 232426932566SPatrick McHardy return -EMSGSIZE; 23251da177e4SLinus Torvalds } 23261da177e4SLinus Torvalds 23271b43af54SPatrick McHardy int rt6_dump_route(struct rt6_info *rt, void *p_arg) 23281da177e4SLinus Torvalds { 23291da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 23301da177e4SLinus Torvalds int prefix; 23311da177e4SLinus Torvalds 23322d7202bfSThomas Graf if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { 23332d7202bfSThomas Graf struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); 23341da177e4SLinus Torvalds prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0; 23351da177e4SLinus Torvalds } else 23361da177e4SLinus Torvalds prefix = 0; 23371da177e4SLinus Torvalds 2338191cd582SBrian Haley return rt6_fill_node(arg->net, 2339191cd582SBrian Haley arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE, 23401da177e4SLinus Torvalds NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq, 23417bc570c8SYOSHIFUJI Hideaki prefix, 0, NLM_F_MULTI); 23421da177e4SLinus Torvalds } 23431da177e4SLinus Torvalds 2344c127ea2cSThomas Graf static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg) 23451da177e4SLinus Torvalds { 23463b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 2347ab364a6fSThomas Graf struct nlattr *tb[RTA_MAX+1]; 23481da177e4SLinus Torvalds struct rt6_info *rt; 2349ab364a6fSThomas Graf struct sk_buff *skb; 2350ab364a6fSThomas Graf struct rtmsg *rtm; 2351ab364a6fSThomas Graf struct flowi fl; 2352ab364a6fSThomas Graf int err, iif = 0; 2353ab364a6fSThomas Graf 2354ab364a6fSThomas Graf err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy); 2355ab364a6fSThomas Graf if (err < 0) 2356ab364a6fSThomas Graf goto errout; 2357ab364a6fSThomas Graf 2358ab364a6fSThomas Graf err = -EINVAL; 2359ab364a6fSThomas Graf memset(&fl, 0, sizeof(fl)); 2360ab364a6fSThomas Graf 2361ab364a6fSThomas Graf if (tb[RTA_SRC]) { 2362ab364a6fSThomas Graf if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) 2363ab364a6fSThomas Graf goto errout; 2364ab364a6fSThomas Graf 2365ab364a6fSThomas Graf ipv6_addr_copy(&fl.fl6_src, nla_data(tb[RTA_SRC])); 2366ab364a6fSThomas Graf } 2367ab364a6fSThomas Graf 2368ab364a6fSThomas Graf if (tb[RTA_DST]) { 2369ab364a6fSThomas Graf if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) 2370ab364a6fSThomas Graf goto errout; 2371ab364a6fSThomas Graf 2372ab364a6fSThomas Graf ipv6_addr_copy(&fl.fl6_dst, nla_data(tb[RTA_DST])); 2373ab364a6fSThomas Graf } 2374ab364a6fSThomas Graf 2375ab364a6fSThomas Graf if (tb[RTA_IIF]) 2376ab364a6fSThomas Graf iif = nla_get_u32(tb[RTA_IIF]); 2377ab364a6fSThomas Graf 2378ab364a6fSThomas Graf if (tb[RTA_OIF]) 2379ab364a6fSThomas Graf fl.oif = nla_get_u32(tb[RTA_OIF]); 2380ab364a6fSThomas Graf 2381ab364a6fSThomas Graf if (iif) { 2382ab364a6fSThomas Graf struct net_device *dev; 23835578689aSDaniel Lezcano dev = __dev_get_by_index(net, iif); 2384ab364a6fSThomas Graf if (!dev) { 2385ab364a6fSThomas Graf err = -ENODEV; 2386ab364a6fSThomas Graf goto errout; 2387ab364a6fSThomas Graf } 2388ab364a6fSThomas Graf } 23891da177e4SLinus Torvalds 23901da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 2391ab364a6fSThomas Graf if (skb == NULL) { 2392ab364a6fSThomas Graf err = -ENOBUFS; 2393ab364a6fSThomas Graf goto errout; 2394ab364a6fSThomas Graf } 23951da177e4SLinus Torvalds 23961da177e4SLinus Torvalds /* Reserve room for dummy headers, this skb can pass 23971da177e4SLinus Torvalds through good chunk of routing engine. 23981da177e4SLinus Torvalds */ 2399459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 24001da177e4SLinus Torvalds skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr)); 24011da177e4SLinus Torvalds 24028a3edd80SDaniel Lezcano rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl); 2403d8d1f30bSChangli Gao skb_dst_set(skb, &rt->dst); 24041da177e4SLinus Torvalds 2405191cd582SBrian Haley err = rt6_fill_node(net, skb, rt, &fl.fl6_dst, &fl.fl6_src, iif, 24061da177e4SLinus Torvalds RTM_NEWROUTE, NETLINK_CB(in_skb).pid, 24077bc570c8SYOSHIFUJI Hideaki nlh->nlmsg_seq, 0, 0, 0); 24081da177e4SLinus Torvalds if (err < 0) { 2409ab364a6fSThomas Graf kfree_skb(skb); 2410ab364a6fSThomas Graf goto errout; 24111da177e4SLinus Torvalds } 24121da177e4SLinus Torvalds 24135578689aSDaniel Lezcano err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid); 2414ab364a6fSThomas Graf errout: 24151da177e4SLinus Torvalds return err; 24161da177e4SLinus Torvalds } 24171da177e4SLinus Torvalds 241886872cb5SThomas Graf void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info) 24191da177e4SLinus Torvalds { 24201da177e4SLinus Torvalds struct sk_buff *skb; 24215578689aSDaniel Lezcano struct net *net = info->nl_net; 2422528c4cebSDenis V. Lunev u32 seq; 2423528c4cebSDenis V. Lunev int err; 24240d51aa80SJamal Hadi Salim 2425528c4cebSDenis V. Lunev err = -ENOBUFS; 2426528c4cebSDenis V. Lunev seq = info->nlh != NULL ? info->nlh->nlmsg_seq : 0; 242786872cb5SThomas Graf 2428339bf98fSThomas Graf skb = nlmsg_new(rt6_nlmsg_size(), gfp_any()); 242921713ebcSThomas Graf if (skb == NULL) 243021713ebcSThomas Graf goto errout; 24311da177e4SLinus Torvalds 2432191cd582SBrian Haley err = rt6_fill_node(net, skb, rt, NULL, NULL, 0, 24337bc570c8SYOSHIFUJI Hideaki event, info->pid, seq, 0, 0, 0); 243426932566SPatrick McHardy if (err < 0) { 243526932566SPatrick McHardy /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ 243626932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 243726932566SPatrick McHardy kfree_skb(skb); 243826932566SPatrick McHardy goto errout; 243926932566SPatrick McHardy } 24401ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE, 24415578689aSDaniel Lezcano info->nlh, gfp_any()); 24421ce85fe4SPablo Neira Ayuso return; 244321713ebcSThomas Graf errout: 244421713ebcSThomas Graf if (err < 0) 24455578689aSDaniel Lezcano rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); 24461da177e4SLinus Torvalds } 24471da177e4SLinus Torvalds 24488ed67789SDaniel Lezcano static int ip6_route_dev_notify(struct notifier_block *this, 24498ed67789SDaniel Lezcano unsigned long event, void *data) 24508ed67789SDaniel Lezcano { 24518ed67789SDaniel Lezcano struct net_device *dev = (struct net_device *)data; 2452c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 24538ed67789SDaniel Lezcano 24548ed67789SDaniel Lezcano if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) { 2455d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.dev = dev; 24568ed67789SDaniel Lezcano net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); 24578ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 2458d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.dev = dev; 24598ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); 2460d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.dev = dev; 24618ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); 24628ed67789SDaniel Lezcano #endif 24638ed67789SDaniel Lezcano } 24648ed67789SDaniel Lezcano 24658ed67789SDaniel Lezcano return NOTIFY_OK; 24668ed67789SDaniel Lezcano } 24678ed67789SDaniel Lezcano 24681da177e4SLinus Torvalds /* 24691da177e4SLinus Torvalds * /proc 24701da177e4SLinus Torvalds */ 24711da177e4SLinus Torvalds 24721da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 24731da177e4SLinus Torvalds 24741da177e4SLinus Torvalds struct rt6_proc_arg 24751da177e4SLinus Torvalds { 24761da177e4SLinus Torvalds char *buffer; 24771da177e4SLinus Torvalds int offset; 24781da177e4SLinus Torvalds int length; 24791da177e4SLinus Torvalds int skip; 24801da177e4SLinus Torvalds int len; 24811da177e4SLinus Torvalds }; 24821da177e4SLinus Torvalds 24831da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg) 24841da177e4SLinus Torvalds { 248533120b30SAlexey Dobriyan struct seq_file *m = p_arg; 24861da177e4SLinus Torvalds 24874b7a4274SHarvey Harrison seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen); 24881da177e4SLinus Torvalds 24891da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 24904b7a4274SHarvey Harrison seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen); 24911da177e4SLinus Torvalds #else 249233120b30SAlexey Dobriyan seq_puts(m, "00000000000000000000000000000000 00 "); 24931da177e4SLinus Torvalds #endif 24941da177e4SLinus Torvalds 24951da177e4SLinus Torvalds if (rt->rt6i_nexthop) { 24964b7a4274SHarvey Harrison seq_printf(m, "%pi6", rt->rt6i_nexthop->primary_key); 24971da177e4SLinus Torvalds } else { 249833120b30SAlexey Dobriyan seq_puts(m, "00000000000000000000000000000000"); 24991da177e4SLinus Torvalds } 250033120b30SAlexey Dobriyan seq_printf(m, " %08x %08x %08x %08x %8s\n", 2501d8d1f30bSChangli Gao rt->rt6i_metric, atomic_read(&rt->dst.__refcnt), 2502d8d1f30bSChangli Gao rt->dst.__use, rt->rt6i_flags, 25031da177e4SLinus Torvalds rt->rt6i_dev ? rt->rt6i_dev->name : ""); 25041da177e4SLinus Torvalds return 0; 25051da177e4SLinus Torvalds } 25061da177e4SLinus Torvalds 250733120b30SAlexey Dobriyan static int ipv6_route_show(struct seq_file *m, void *v) 25081da177e4SLinus Torvalds { 2509f3db4851SDaniel Lezcano struct net *net = (struct net *)m->private; 2510f3db4851SDaniel Lezcano fib6_clean_all(net, rt6_info_route, 0, m); 251133120b30SAlexey Dobriyan return 0; 25121da177e4SLinus Torvalds } 25131da177e4SLinus Torvalds 251433120b30SAlexey Dobriyan static int ipv6_route_open(struct inode *inode, struct file *file) 251533120b30SAlexey Dobriyan { 2516de05c557SPavel Emelyanov return single_open_net(inode, file, ipv6_route_show); 2517f3db4851SDaniel Lezcano } 2518f3db4851SDaniel Lezcano 251933120b30SAlexey Dobriyan static const struct file_operations ipv6_route_proc_fops = { 252033120b30SAlexey Dobriyan .owner = THIS_MODULE, 252133120b30SAlexey Dobriyan .open = ipv6_route_open, 252233120b30SAlexey Dobriyan .read = seq_read, 252333120b30SAlexey Dobriyan .llseek = seq_lseek, 2524b6fcbdb4SPavel Emelyanov .release = single_release_net, 252533120b30SAlexey Dobriyan }; 252633120b30SAlexey Dobriyan 25271da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 25281da177e4SLinus Torvalds { 252969ddb805SDaniel Lezcano struct net *net = (struct net *)seq->private; 25301da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 253169ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_nodes, 253269ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_route_nodes, 253369ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_alloc, 253469ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_entries, 253569ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_rt_cache, 2536fc66f95cSEric Dumazet dst_entries_get_slow(&net->ipv6.ip6_dst_ops), 253769ddb805SDaniel Lezcano net->ipv6.rt6_stats->fib_discarded_routes); 25381da177e4SLinus Torvalds 25391da177e4SLinus Torvalds return 0; 25401da177e4SLinus Torvalds } 25411da177e4SLinus Torvalds 25421da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file) 25431da177e4SLinus Torvalds { 2544de05c557SPavel Emelyanov return single_open_net(inode, file, rt6_stats_seq_show); 254569ddb805SDaniel Lezcano } 254669ddb805SDaniel Lezcano 25479a32144eSArjan van de Ven static const struct file_operations rt6_stats_seq_fops = { 25481da177e4SLinus Torvalds .owner = THIS_MODULE, 25491da177e4SLinus Torvalds .open = rt6_stats_seq_open, 25501da177e4SLinus Torvalds .read = seq_read, 25511da177e4SLinus Torvalds .llseek = seq_lseek, 2552b6fcbdb4SPavel Emelyanov .release = single_release_net, 25531da177e4SLinus Torvalds }; 25541da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 25551da177e4SLinus Torvalds 25561da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 25571da177e4SLinus Torvalds 25581da177e4SLinus Torvalds static 25598d65af78SAlexey Dobriyan int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write, 25601da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 25611da177e4SLinus Torvalds { 2562c486da34SLucian Adrian Grijincu struct net *net; 2563c486da34SLucian Adrian Grijincu int delay; 2564c486da34SLucian Adrian Grijincu if (!write) 2565c486da34SLucian Adrian Grijincu return -EINVAL; 2566c486da34SLucian Adrian Grijincu 2567c486da34SLucian Adrian Grijincu net = (struct net *)ctl->extra1; 2568c486da34SLucian Adrian Grijincu delay = net->ipv6.sysctl.flush_delay; 25698d65af78SAlexey Dobriyan proc_dointvec(ctl, write, buffer, lenp, ppos); 25705b7c931dSDaniel Lezcano fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net); 25711da177e4SLinus Torvalds return 0; 25721da177e4SLinus Torvalds } 25731da177e4SLinus Torvalds 2574760f2d01SDaniel Lezcano ctl_table ipv6_route_table_template[] = { 25751da177e4SLinus Torvalds { 25761da177e4SLinus Torvalds .procname = "flush", 25774990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.flush_delay, 25781da177e4SLinus Torvalds .maxlen = sizeof(int), 257989c8b3a1SDave Jones .mode = 0200, 25806d9f239aSAlexey Dobriyan .proc_handler = ipv6_sysctl_rtcache_flush 25811da177e4SLinus Torvalds }, 25821da177e4SLinus Torvalds { 25831da177e4SLinus Torvalds .procname = "gc_thresh", 25849a7ec3a9SDaniel Lezcano .data = &ip6_dst_ops_template.gc_thresh, 25851da177e4SLinus Torvalds .maxlen = sizeof(int), 25861da177e4SLinus Torvalds .mode = 0644, 25876d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 25881da177e4SLinus Torvalds }, 25891da177e4SLinus Torvalds { 25901da177e4SLinus Torvalds .procname = "max_size", 25914990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_max_size, 25921da177e4SLinus Torvalds .maxlen = sizeof(int), 25931da177e4SLinus Torvalds .mode = 0644, 25946d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 25951da177e4SLinus Torvalds }, 25961da177e4SLinus Torvalds { 25971da177e4SLinus Torvalds .procname = "gc_min_interval", 25984990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 25991da177e4SLinus Torvalds .maxlen = sizeof(int), 26001da177e4SLinus Torvalds .mode = 0644, 26016d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 26021da177e4SLinus Torvalds }, 26031da177e4SLinus Torvalds { 26041da177e4SLinus Torvalds .procname = "gc_timeout", 26054990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, 26061da177e4SLinus Torvalds .maxlen = sizeof(int), 26071da177e4SLinus Torvalds .mode = 0644, 26086d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 26091da177e4SLinus Torvalds }, 26101da177e4SLinus Torvalds { 26111da177e4SLinus Torvalds .procname = "gc_interval", 26124990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, 26131da177e4SLinus Torvalds .maxlen = sizeof(int), 26141da177e4SLinus Torvalds .mode = 0644, 26156d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 26161da177e4SLinus Torvalds }, 26171da177e4SLinus Torvalds { 26181da177e4SLinus Torvalds .procname = "gc_elasticity", 26194990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, 26201da177e4SLinus Torvalds .maxlen = sizeof(int), 26211da177e4SLinus Torvalds .mode = 0644, 2622f3d3f616SMin Zhang .proc_handler = proc_dointvec, 26231da177e4SLinus Torvalds }, 26241da177e4SLinus Torvalds { 26251da177e4SLinus Torvalds .procname = "mtu_expires", 26264990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, 26271da177e4SLinus Torvalds .maxlen = sizeof(int), 26281da177e4SLinus Torvalds .mode = 0644, 26296d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 26301da177e4SLinus Torvalds }, 26311da177e4SLinus Torvalds { 26321da177e4SLinus Torvalds .procname = "min_adv_mss", 26334990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, 26341da177e4SLinus Torvalds .maxlen = sizeof(int), 26351da177e4SLinus Torvalds .mode = 0644, 2636f3d3f616SMin Zhang .proc_handler = proc_dointvec, 26371da177e4SLinus Torvalds }, 26381da177e4SLinus Torvalds { 26391da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 26404990509fSDaniel Lezcano .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, 26411da177e4SLinus Torvalds .maxlen = sizeof(int), 26421da177e4SLinus Torvalds .mode = 0644, 26436d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_ms_jiffies, 26441da177e4SLinus Torvalds }, 2645f8572d8fSEric W. Biederman { } 26461da177e4SLinus Torvalds }; 26471da177e4SLinus Torvalds 26482c8c1e72SAlexey Dobriyan struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) 2649760f2d01SDaniel Lezcano { 2650760f2d01SDaniel Lezcano struct ctl_table *table; 2651760f2d01SDaniel Lezcano 2652760f2d01SDaniel Lezcano table = kmemdup(ipv6_route_table_template, 2653760f2d01SDaniel Lezcano sizeof(ipv6_route_table_template), 2654760f2d01SDaniel Lezcano GFP_KERNEL); 26555ee09105SYOSHIFUJI Hideaki 26565ee09105SYOSHIFUJI Hideaki if (table) { 26575ee09105SYOSHIFUJI Hideaki table[0].data = &net->ipv6.sysctl.flush_delay; 2658c486da34SLucian Adrian Grijincu table[0].extra1 = net; 265986393e52SAlexey Dobriyan table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; 26605ee09105SYOSHIFUJI Hideaki table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; 26615ee09105SYOSHIFUJI Hideaki table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 26625ee09105SYOSHIFUJI Hideaki table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; 26635ee09105SYOSHIFUJI Hideaki table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; 26645ee09105SYOSHIFUJI Hideaki table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; 26655ee09105SYOSHIFUJI Hideaki table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; 26665ee09105SYOSHIFUJI Hideaki table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; 26679c69fabeSAlexey Dobriyan table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; 26685ee09105SYOSHIFUJI Hideaki } 26695ee09105SYOSHIFUJI Hideaki 2670760f2d01SDaniel Lezcano return table; 2671760f2d01SDaniel Lezcano } 26721da177e4SLinus Torvalds #endif 26731da177e4SLinus Torvalds 26742c8c1e72SAlexey Dobriyan static int __net_init ip6_route_net_init(struct net *net) 2675cdb18761SDaniel Lezcano { 2676633d424bSPavel Emelyanov int ret = -ENOMEM; 26778ed67789SDaniel Lezcano 267886393e52SAlexey Dobriyan memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, 267986393e52SAlexey Dobriyan sizeof(net->ipv6.ip6_dst_ops)); 2680f2fc6a54SBenjamin Thery 2681fc66f95cSEric Dumazet if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) 2682fc66f95cSEric Dumazet goto out_ip6_dst_ops; 2683fc66f95cSEric Dumazet 26848ed67789SDaniel Lezcano net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, 26858ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_null_entry), 26868ed67789SDaniel Lezcano GFP_KERNEL); 26878ed67789SDaniel Lezcano if (!net->ipv6.ip6_null_entry) 2688fc66f95cSEric Dumazet goto out_ip6_dst_entries; 2689d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.path = 26908ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_null_entry; 2691d8d1f30bSChangli Gao net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; 2692defb3519SDavid S. Miller dst_metric_set(&net->ipv6.ip6_null_entry->dst, RTAX_HOPLIMIT, 255); 26938ed67789SDaniel Lezcano 26948ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 26958ed67789SDaniel Lezcano net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, 26968ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_prohibit_entry), 26978ed67789SDaniel Lezcano GFP_KERNEL); 269868fffc67SPeter Zijlstra if (!net->ipv6.ip6_prohibit_entry) 269968fffc67SPeter Zijlstra goto out_ip6_null_entry; 2700d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.path = 27018ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_prohibit_entry; 2702d8d1f30bSChangli Gao net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; 2703defb3519SDavid S. Miller dst_metric_set(&net->ipv6.ip6_prohibit_entry->dst, RTAX_HOPLIMIT, 255); 27048ed67789SDaniel Lezcano 27058ed67789SDaniel Lezcano net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, 27068ed67789SDaniel Lezcano sizeof(*net->ipv6.ip6_blk_hole_entry), 27078ed67789SDaniel Lezcano GFP_KERNEL); 270868fffc67SPeter Zijlstra if (!net->ipv6.ip6_blk_hole_entry) 270968fffc67SPeter Zijlstra goto out_ip6_prohibit_entry; 2710d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.path = 27118ed67789SDaniel Lezcano (struct dst_entry *)net->ipv6.ip6_blk_hole_entry; 2712d8d1f30bSChangli Gao net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; 2713defb3519SDavid S. Miller dst_metric_set(&net->ipv6.ip6_blk_hole_entry->dst, RTAX_HOPLIMIT, 255); 27148ed67789SDaniel Lezcano #endif 27158ed67789SDaniel Lezcano 2716b339a47cSPeter Zijlstra net->ipv6.sysctl.flush_delay = 0; 2717b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_max_size = 4096; 2718b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; 2719b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; 2720b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; 2721b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; 2722b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; 2723b339a47cSPeter Zijlstra net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 2724b339a47cSPeter Zijlstra 2725cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS 2726cdb18761SDaniel Lezcano proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops); 2727cdb18761SDaniel Lezcano proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops); 2728cdb18761SDaniel Lezcano #endif 27296891a346SBenjamin Thery net->ipv6.ip6_rt_gc_expire = 30*HZ; 27306891a346SBenjamin Thery 27318ed67789SDaniel Lezcano ret = 0; 27328ed67789SDaniel Lezcano out: 27338ed67789SDaniel Lezcano return ret; 2734f2fc6a54SBenjamin Thery 273568fffc67SPeter Zijlstra #ifdef CONFIG_IPV6_MULTIPLE_TABLES 273668fffc67SPeter Zijlstra out_ip6_prohibit_entry: 273768fffc67SPeter Zijlstra kfree(net->ipv6.ip6_prohibit_entry); 273868fffc67SPeter Zijlstra out_ip6_null_entry: 273968fffc67SPeter Zijlstra kfree(net->ipv6.ip6_null_entry); 274068fffc67SPeter Zijlstra #endif 2741fc66f95cSEric Dumazet out_ip6_dst_entries: 2742fc66f95cSEric Dumazet dst_entries_destroy(&net->ipv6.ip6_dst_ops); 2743f2fc6a54SBenjamin Thery out_ip6_dst_ops: 2744f2fc6a54SBenjamin Thery goto out; 2745cdb18761SDaniel Lezcano } 2746cdb18761SDaniel Lezcano 27472c8c1e72SAlexey Dobriyan static void __net_exit ip6_route_net_exit(struct net *net) 2748cdb18761SDaniel Lezcano { 2749cdb18761SDaniel Lezcano #ifdef CONFIG_PROC_FS 2750cdb18761SDaniel Lezcano proc_net_remove(net, "ipv6_route"); 2751cdb18761SDaniel Lezcano proc_net_remove(net, "rt6_stats"); 2752cdb18761SDaniel Lezcano #endif 27538ed67789SDaniel Lezcano kfree(net->ipv6.ip6_null_entry); 27548ed67789SDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 27558ed67789SDaniel Lezcano kfree(net->ipv6.ip6_prohibit_entry); 27568ed67789SDaniel Lezcano kfree(net->ipv6.ip6_blk_hole_entry); 27578ed67789SDaniel Lezcano #endif 275841bb78b4SXiaotian Feng dst_entries_destroy(&net->ipv6.ip6_dst_ops); 2759cdb18761SDaniel Lezcano } 2760cdb18761SDaniel Lezcano 2761cdb18761SDaniel Lezcano static struct pernet_operations ip6_route_net_ops = { 2762cdb18761SDaniel Lezcano .init = ip6_route_net_init, 2763cdb18761SDaniel Lezcano .exit = ip6_route_net_exit, 2764cdb18761SDaniel Lezcano }; 2765cdb18761SDaniel Lezcano 27668ed67789SDaniel Lezcano static struct notifier_block ip6_route_dev_notifier = { 27678ed67789SDaniel Lezcano .notifier_call = ip6_route_dev_notify, 27688ed67789SDaniel Lezcano .priority = 0, 27698ed67789SDaniel Lezcano }; 27708ed67789SDaniel Lezcano 2771433d49c3SDaniel Lezcano int __init ip6_route_init(void) 27721da177e4SLinus Torvalds { 2773433d49c3SDaniel Lezcano int ret; 2774433d49c3SDaniel Lezcano 27759a7ec3a9SDaniel Lezcano ret = -ENOMEM; 27769a7ec3a9SDaniel Lezcano ip6_dst_ops_template.kmem_cachep = 27779a7ec3a9SDaniel Lezcano kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, 27789a7ec3a9SDaniel Lezcano SLAB_HWCACHE_ALIGN, NULL); 27799a7ec3a9SDaniel Lezcano if (!ip6_dst_ops_template.kmem_cachep) 2780c19a28e1SFernando Carrijo goto out; 278114e50e57SDavid S. Miller 2782fc66f95cSEric Dumazet ret = dst_entries_init(&ip6_dst_blackhole_ops); 27838ed67789SDaniel Lezcano if (ret) 2784bdb3289fSDaniel Lezcano goto out_kmem_cache; 2785bdb3289fSDaniel Lezcano 2786fc66f95cSEric Dumazet ret = register_pernet_subsys(&ip6_route_net_ops); 2787fc66f95cSEric Dumazet if (ret) 2788fc66f95cSEric Dumazet goto out_dst_entries; 2789fc66f95cSEric Dumazet 27905dc121e9SArnaud Ebalard ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; 27915dc121e9SArnaud Ebalard 27928ed67789SDaniel Lezcano /* Registering of the loopback is done before this portion of code, 27938ed67789SDaniel Lezcano * the loopback reference in rt6_info will not be taken, do it 27948ed67789SDaniel Lezcano * manually for init_net */ 2795d8d1f30bSChangli Gao init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; 27968ed67789SDaniel Lezcano init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 2797bdb3289fSDaniel Lezcano #ifdef CONFIG_IPV6_MULTIPLE_TABLES 2798d8d1f30bSChangli Gao init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; 27998ed67789SDaniel Lezcano init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 2800d8d1f30bSChangli Gao init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; 28018ed67789SDaniel Lezcano init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); 2802bdb3289fSDaniel Lezcano #endif 2803433d49c3SDaniel Lezcano ret = fib6_init(); 2804433d49c3SDaniel Lezcano if (ret) 28058ed67789SDaniel Lezcano goto out_register_subsys; 2806433d49c3SDaniel Lezcano 2807433d49c3SDaniel Lezcano ret = xfrm6_init(); 2808433d49c3SDaniel Lezcano if (ret) 2809cdb18761SDaniel Lezcano goto out_fib6_init; 2810c35b7e72SDaniel Lezcano 2811433d49c3SDaniel Lezcano ret = fib6_rules_init(); 2812433d49c3SDaniel Lezcano if (ret) 2813433d49c3SDaniel Lezcano goto xfrm6_init; 28147e5449c2SDaniel Lezcano 2815433d49c3SDaniel Lezcano ret = -ENOBUFS; 2816433d49c3SDaniel Lezcano if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL) || 2817433d49c3SDaniel Lezcano __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL) || 2818433d49c3SDaniel Lezcano __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL)) 2819433d49c3SDaniel Lezcano goto fib6_rules_init; 2820433d49c3SDaniel Lezcano 28218ed67789SDaniel Lezcano ret = register_netdevice_notifier(&ip6_route_dev_notifier); 2822cdb18761SDaniel Lezcano if (ret) 2823cdb18761SDaniel Lezcano goto fib6_rules_init; 28248ed67789SDaniel Lezcano 2825433d49c3SDaniel Lezcano out: 2826433d49c3SDaniel Lezcano return ret; 2827433d49c3SDaniel Lezcano 2828433d49c3SDaniel Lezcano fib6_rules_init: 2829433d49c3SDaniel Lezcano fib6_rules_cleanup(); 2830433d49c3SDaniel Lezcano xfrm6_init: 2831433d49c3SDaniel Lezcano xfrm6_fini(); 2832433d49c3SDaniel Lezcano out_fib6_init: 2833433d49c3SDaniel Lezcano fib6_gc_cleanup(); 28348ed67789SDaniel Lezcano out_register_subsys: 28358ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 2836fc66f95cSEric Dumazet out_dst_entries: 2837fc66f95cSEric Dumazet dst_entries_destroy(&ip6_dst_blackhole_ops); 2838433d49c3SDaniel Lezcano out_kmem_cache: 2839f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 2840433d49c3SDaniel Lezcano goto out; 28411da177e4SLinus Torvalds } 28421da177e4SLinus Torvalds 28431da177e4SLinus Torvalds void ip6_route_cleanup(void) 28441da177e4SLinus Torvalds { 28458ed67789SDaniel Lezcano unregister_netdevice_notifier(&ip6_route_dev_notifier); 2846101367c2SThomas Graf fib6_rules_cleanup(); 28471da177e4SLinus Torvalds xfrm6_fini(); 28481da177e4SLinus Torvalds fib6_gc_cleanup(); 28498ed67789SDaniel Lezcano unregister_pernet_subsys(&ip6_route_net_ops); 285041bb78b4SXiaotian Feng dst_entries_destroy(&ip6_dst_blackhole_ops); 2851f2fc6a54SBenjamin Thery kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); 28521da177e4SLinus Torvalds } 2853