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 * $Id: route.c,v 1.56 2001/10/31 21:55:55 davem Exp $ 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 111da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 121da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 131da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 141da177e4SLinus Torvalds */ 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds /* Changes: 171da177e4SLinus Torvalds * 181da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI 191da177e4SLinus Torvalds * reworked default router selection. 201da177e4SLinus Torvalds * - respect outgoing interface 211da177e4SLinus Torvalds * - select from (probably) reachable routers (i.e. 221da177e4SLinus Torvalds * routers in REACHABLE, STALE, DELAY or PROBE states). 231da177e4SLinus Torvalds * - always select the same router if it is (probably) 241da177e4SLinus Torvalds * reachable. otherwise, round-robin the list. 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> 371da177e4SLinus Torvalds #include <linux/init.h> 381da177e4SLinus Torvalds #include <linux/netlink.h> 391da177e4SLinus Torvalds #include <linux/if_arp.h> 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 421da177e4SLinus Torvalds #include <linux/proc_fs.h> 431da177e4SLinus Torvalds #include <linux/seq_file.h> 441da177e4SLinus Torvalds #endif 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds #include <net/snmp.h> 471da177e4SLinus Torvalds #include <net/ipv6.h> 481da177e4SLinus Torvalds #include <net/ip6_fib.h> 491da177e4SLinus Torvalds #include <net/ip6_route.h> 501da177e4SLinus Torvalds #include <net/ndisc.h> 511da177e4SLinus Torvalds #include <net/addrconf.h> 521da177e4SLinus Torvalds #include <net/tcp.h> 531da177e4SLinus Torvalds #include <linux/rtnetlink.h> 541da177e4SLinus Torvalds #include <net/dst.h> 551da177e4SLinus Torvalds #include <net/xfrm.h> 568d71740cSTom Tucker #include <net/netevent.h> 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 75519fbd87SYOSHIFUJI Hideaki #define CLONE_OFFLINK_ROUTE 0 761da177e4SLinus Torvalds 77554cfb7eSYOSHIFUJI Hideaki #define RT6_SELECT_F_IFACE 0x1 78554cfb7eSYOSHIFUJI Hideaki #define RT6_SELECT_F_REACHABLE 0x2 79554cfb7eSYOSHIFUJI Hideaki 801da177e4SLinus Torvalds static int ip6_rt_max_size = 4096; 811da177e4SLinus Torvalds static int ip6_rt_gc_min_interval = HZ / 2; 821da177e4SLinus Torvalds static int ip6_rt_gc_timeout = 60*HZ; 831da177e4SLinus Torvalds int ip6_rt_gc_interval = 30*HZ; 841da177e4SLinus Torvalds static int ip6_rt_gc_elasticity = 9; 851da177e4SLinus Torvalds static int ip6_rt_mtu_expires = 10*60*HZ; 861da177e4SLinus Torvalds static int ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds static struct rt6_info * ip6_rt_copy(struct rt6_info *ort); 891da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie); 901da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *); 911da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *); 921da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *, 931da177e4SLinus Torvalds struct net_device *dev, int how); 941da177e4SLinus Torvalds static int ip6_dst_gc(void); 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds static int ip6_pkt_discard(struct sk_buff *skb); 971da177e4SLinus Torvalds static int ip6_pkt_discard_out(struct sk_buff *skb); 981da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb); 991da177e4SLinus Torvalds static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu); 1001da177e4SLinus Torvalds 10170ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 10270ceb4f5SYOSHIFUJI Hideaki static struct rt6_info *rt6_add_route_info(struct in6_addr *prefix, int prefixlen, 10370ceb4f5SYOSHIFUJI Hideaki struct in6_addr *gwaddr, int ifindex, 10470ceb4f5SYOSHIFUJI Hideaki unsigned pref); 10570ceb4f5SYOSHIFUJI Hideaki static struct rt6_info *rt6_get_route_info(struct in6_addr *prefix, int prefixlen, 10670ceb4f5SYOSHIFUJI Hideaki struct in6_addr *gwaddr, int ifindex); 10770ceb4f5SYOSHIFUJI Hideaki #endif 10870ceb4f5SYOSHIFUJI Hideaki 1091da177e4SLinus Torvalds static struct dst_ops ip6_dst_ops = { 1101da177e4SLinus Torvalds .family = AF_INET6, 1111da177e4SLinus Torvalds .protocol = __constant_htons(ETH_P_IPV6), 1121da177e4SLinus Torvalds .gc = ip6_dst_gc, 1131da177e4SLinus Torvalds .gc_thresh = 1024, 1141da177e4SLinus Torvalds .check = ip6_dst_check, 1151da177e4SLinus Torvalds .destroy = ip6_dst_destroy, 1161da177e4SLinus Torvalds .ifdown = ip6_dst_ifdown, 1171da177e4SLinus Torvalds .negative_advice = ip6_negative_advice, 1181da177e4SLinus Torvalds .link_failure = ip6_link_failure, 1191da177e4SLinus Torvalds .update_pmtu = ip6_rt_update_pmtu, 1201da177e4SLinus Torvalds .entry_size = sizeof(struct rt6_info), 1211da177e4SLinus Torvalds }; 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds struct rt6_info ip6_null_entry = { 1241da177e4SLinus Torvalds .u = { 1251da177e4SLinus Torvalds .dst = { 1261da177e4SLinus Torvalds .__refcnt = ATOMIC_INIT(1), 1271da177e4SLinus Torvalds .__use = 1, 1281da177e4SLinus Torvalds .dev = &loopback_dev, 1291da177e4SLinus Torvalds .obsolete = -1, 1301da177e4SLinus Torvalds .error = -ENETUNREACH, 1311da177e4SLinus Torvalds .metrics = { [RTAX_HOPLIMIT - 1] = 255, }, 1321da177e4SLinus Torvalds .input = ip6_pkt_discard, 1331da177e4SLinus Torvalds .output = ip6_pkt_discard_out, 1341da177e4SLinus Torvalds .ops = &ip6_dst_ops, 1351da177e4SLinus Torvalds .path = (struct dst_entry*)&ip6_null_entry, 1361da177e4SLinus Torvalds } 1371da177e4SLinus Torvalds }, 1381da177e4SLinus Torvalds .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), 1391da177e4SLinus Torvalds .rt6i_metric = ~(u32) 0, 1401da177e4SLinus Torvalds .rt6i_ref = ATOMIC_INIT(1), 1411da177e4SLinus Torvalds }; 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds struct fib6_node ip6_routing_table = { 1441da177e4SLinus Torvalds .leaf = &ip6_null_entry, 1451da177e4SLinus Torvalds .fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO, 1461da177e4SLinus Torvalds }; 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds /* Protects all the ip6 fib */ 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds DEFINE_RWLOCK(rt6_lock); 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds /* allocate dst with ip6_dst_ops */ 1541da177e4SLinus Torvalds static __inline__ struct rt6_info *ip6_dst_alloc(void) 1551da177e4SLinus Torvalds { 1561da177e4SLinus Torvalds return (struct rt6_info *)dst_alloc(&ip6_dst_ops); 1571da177e4SLinus Torvalds } 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds static void ip6_dst_destroy(struct dst_entry *dst) 1601da177e4SLinus Torvalds { 1611da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 1621da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds if (idev != NULL) { 1651da177e4SLinus Torvalds rt->rt6i_idev = NULL; 1661da177e4SLinus Torvalds in6_dev_put(idev); 1671da177e4SLinus Torvalds } 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 1711da177e4SLinus Torvalds int how) 1721da177e4SLinus Torvalds { 1731da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *)dst; 1741da177e4SLinus Torvalds struct inet6_dev *idev = rt->rt6i_idev; 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds if (dev != &loopback_dev && idev != NULL && idev->dev == dev) { 1771da177e4SLinus Torvalds struct inet6_dev *loopback_idev = in6_dev_get(&loopback_dev); 1781da177e4SLinus Torvalds if (loopback_idev != NULL) { 1791da177e4SLinus Torvalds rt->rt6i_idev = loopback_idev; 1801da177e4SLinus Torvalds in6_dev_put(idev); 1811da177e4SLinus Torvalds } 1821da177e4SLinus Torvalds } 1831da177e4SLinus Torvalds } 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds static __inline__ int rt6_check_expired(const struct rt6_info *rt) 1861da177e4SLinus Torvalds { 1871da177e4SLinus Torvalds return (rt->rt6i_flags & RTF_EXPIRES && 1881da177e4SLinus Torvalds time_after(jiffies, rt->rt6i_expires)); 1891da177e4SLinus Torvalds } 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds /* 1921da177e4SLinus Torvalds * Route lookup. Any rt6_lock is implied. 1931da177e4SLinus Torvalds */ 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds static __inline__ struct rt6_info *rt6_device_match(struct rt6_info *rt, 1961da177e4SLinus Torvalds int oif, 1971da177e4SLinus Torvalds int strict) 1981da177e4SLinus Torvalds { 1991da177e4SLinus Torvalds struct rt6_info *local = NULL; 2001da177e4SLinus Torvalds struct rt6_info *sprt; 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds if (oif) { 2031da177e4SLinus Torvalds for (sprt = rt; sprt; sprt = sprt->u.next) { 2041da177e4SLinus Torvalds struct net_device *dev = sprt->rt6i_dev; 2051da177e4SLinus Torvalds if (dev->ifindex == oif) 2061da177e4SLinus Torvalds return sprt; 2071da177e4SLinus Torvalds if (dev->flags & IFF_LOOPBACK) { 2081da177e4SLinus Torvalds if (sprt->rt6i_idev == NULL || 2091da177e4SLinus Torvalds sprt->rt6i_idev->dev->ifindex != oif) { 2101da177e4SLinus Torvalds if (strict && oif) 2111da177e4SLinus Torvalds continue; 2121da177e4SLinus Torvalds if (local && (!oif || 2131da177e4SLinus Torvalds local->rt6i_idev->dev->ifindex == oif)) 2141da177e4SLinus Torvalds continue; 2151da177e4SLinus Torvalds } 2161da177e4SLinus Torvalds local = sprt; 2171da177e4SLinus Torvalds } 2181da177e4SLinus Torvalds } 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds if (local) 2211da177e4SLinus Torvalds return local; 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds if (strict) 2241da177e4SLinus Torvalds return &ip6_null_entry; 2251da177e4SLinus Torvalds } 2261da177e4SLinus Torvalds return rt; 2271da177e4SLinus Torvalds } 2281da177e4SLinus Torvalds 22927097255SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 23027097255SYOSHIFUJI Hideaki static void rt6_probe(struct rt6_info *rt) 23127097255SYOSHIFUJI Hideaki { 23227097255SYOSHIFUJI Hideaki struct neighbour *neigh = rt ? rt->rt6i_nexthop : NULL; 23327097255SYOSHIFUJI Hideaki /* 23427097255SYOSHIFUJI Hideaki * Okay, this does not seem to be appropriate 23527097255SYOSHIFUJI Hideaki * for now, however, we need to check if it 23627097255SYOSHIFUJI Hideaki * is really so; aka Router Reachability Probing. 23727097255SYOSHIFUJI Hideaki * 23827097255SYOSHIFUJI Hideaki * Router Reachability Probe MUST be rate-limited 23927097255SYOSHIFUJI Hideaki * to no more than one per minute. 24027097255SYOSHIFUJI Hideaki */ 24127097255SYOSHIFUJI Hideaki if (!neigh || (neigh->nud_state & NUD_VALID)) 24227097255SYOSHIFUJI Hideaki return; 24327097255SYOSHIFUJI Hideaki read_lock_bh(&neigh->lock); 24427097255SYOSHIFUJI Hideaki if (!(neigh->nud_state & NUD_VALID) && 24552e16356SYOSHIFUJI Hideaki time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) { 24627097255SYOSHIFUJI Hideaki struct in6_addr mcaddr; 24727097255SYOSHIFUJI Hideaki struct in6_addr *target; 24827097255SYOSHIFUJI Hideaki 24927097255SYOSHIFUJI Hideaki neigh->updated = jiffies; 25027097255SYOSHIFUJI Hideaki read_unlock_bh(&neigh->lock); 25127097255SYOSHIFUJI Hideaki 25227097255SYOSHIFUJI Hideaki target = (struct in6_addr *)&neigh->primary_key; 25327097255SYOSHIFUJI Hideaki addrconf_addr_solict_mult(target, &mcaddr); 25427097255SYOSHIFUJI Hideaki ndisc_send_ns(rt->rt6i_dev, NULL, target, &mcaddr, NULL); 25527097255SYOSHIFUJI Hideaki } else 25627097255SYOSHIFUJI Hideaki read_unlock_bh(&neigh->lock); 25727097255SYOSHIFUJI Hideaki } 25827097255SYOSHIFUJI Hideaki #else 25927097255SYOSHIFUJI Hideaki static inline void rt6_probe(struct rt6_info *rt) 26027097255SYOSHIFUJI Hideaki { 26127097255SYOSHIFUJI Hideaki return; 26227097255SYOSHIFUJI Hideaki } 26327097255SYOSHIFUJI Hideaki #endif 26427097255SYOSHIFUJI Hideaki 2651da177e4SLinus Torvalds /* 266554cfb7eSYOSHIFUJI Hideaki * Default Router Selection (RFC 2461 6.3.6) 2671da177e4SLinus Torvalds */ 268554cfb7eSYOSHIFUJI Hideaki static int inline rt6_check_dev(struct rt6_info *rt, int oif) 2691da177e4SLinus Torvalds { 270554cfb7eSYOSHIFUJI Hideaki struct net_device *dev = rt->rt6i_dev; 271554cfb7eSYOSHIFUJI Hideaki if (!oif || dev->ifindex == oif) 272554cfb7eSYOSHIFUJI Hideaki return 2; 273554cfb7eSYOSHIFUJI Hideaki if ((dev->flags & IFF_LOOPBACK) && 274554cfb7eSYOSHIFUJI Hideaki rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif) 275554cfb7eSYOSHIFUJI Hideaki return 1; 276554cfb7eSYOSHIFUJI Hideaki return 0; 2771da177e4SLinus Torvalds } 2781da177e4SLinus Torvalds 279554cfb7eSYOSHIFUJI Hideaki static int inline rt6_check_neigh(struct rt6_info *rt) 2801da177e4SLinus Torvalds { 281554cfb7eSYOSHIFUJI Hideaki struct neighbour *neigh = rt->rt6i_nexthop; 2821da177e4SLinus Torvalds int m = 0; 2834d0c5911SYOSHIFUJI Hideaki if (rt->rt6i_flags & RTF_NONEXTHOP || 2844d0c5911SYOSHIFUJI Hideaki !(rt->rt6i_flags & RTF_GATEWAY)) 2854d0c5911SYOSHIFUJI Hideaki m = 1; 2864d0c5911SYOSHIFUJI Hideaki else if (neigh) { 2871da177e4SLinus Torvalds read_lock_bh(&neigh->lock); 288554cfb7eSYOSHIFUJI Hideaki if (neigh->nud_state & NUD_VALID) 2894d0c5911SYOSHIFUJI Hideaki m = 2; 2901da177e4SLinus Torvalds read_unlock_bh(&neigh->lock); 2911da177e4SLinus Torvalds } 292554cfb7eSYOSHIFUJI Hideaki return m; 2931da177e4SLinus Torvalds } 2941da177e4SLinus Torvalds 295554cfb7eSYOSHIFUJI Hideaki static int rt6_score_route(struct rt6_info *rt, int oif, 296554cfb7eSYOSHIFUJI Hideaki int strict) 297554cfb7eSYOSHIFUJI Hideaki { 2984d0c5911SYOSHIFUJI Hideaki int m, n; 2994d0c5911SYOSHIFUJI Hideaki 3004d0c5911SYOSHIFUJI Hideaki m = rt6_check_dev(rt, oif); 301554cfb7eSYOSHIFUJI Hideaki if (!m && (strict & RT6_SELECT_F_IFACE)) 302554cfb7eSYOSHIFUJI Hideaki return -1; 303ebacaaa0SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 304ebacaaa0SYOSHIFUJI Hideaki m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2; 305ebacaaa0SYOSHIFUJI Hideaki #endif 3064d0c5911SYOSHIFUJI Hideaki n = rt6_check_neigh(rt); 3074d0c5911SYOSHIFUJI Hideaki if (n > 1) 308ebacaaa0SYOSHIFUJI Hideaki m |= 16; 3094d0c5911SYOSHIFUJI Hideaki else if (!n && strict & RT6_SELECT_F_REACHABLE) 310554cfb7eSYOSHIFUJI Hideaki return -1; 311554cfb7eSYOSHIFUJI Hideaki return m; 312554cfb7eSYOSHIFUJI Hideaki } 313554cfb7eSYOSHIFUJI Hideaki 314554cfb7eSYOSHIFUJI Hideaki static struct rt6_info *rt6_select(struct rt6_info **head, int oif, 315554cfb7eSYOSHIFUJI Hideaki int strict) 316554cfb7eSYOSHIFUJI Hideaki { 317554cfb7eSYOSHIFUJI Hideaki struct rt6_info *match = NULL, *last = NULL; 318554cfb7eSYOSHIFUJI Hideaki struct rt6_info *rt, *rt0 = *head; 319554cfb7eSYOSHIFUJI Hideaki u32 metric; 320554cfb7eSYOSHIFUJI Hideaki int mpri = -1; 321554cfb7eSYOSHIFUJI Hideaki 322554cfb7eSYOSHIFUJI Hideaki RT6_TRACE("%s(head=%p(*head=%p), oif=%d)\n", 323554cfb7eSYOSHIFUJI Hideaki __FUNCTION__, head, head ? *head : NULL, oif); 324554cfb7eSYOSHIFUJI Hideaki 325554cfb7eSYOSHIFUJI Hideaki for (rt = rt0, metric = rt0->rt6i_metric; 326c302e6d5SYOSHIFUJI Hideaki rt && rt->rt6i_metric == metric && (!last || rt != rt0); 327554cfb7eSYOSHIFUJI Hideaki rt = rt->u.next) { 328554cfb7eSYOSHIFUJI Hideaki int m; 329554cfb7eSYOSHIFUJI Hideaki 330554cfb7eSYOSHIFUJI Hideaki if (rt6_check_expired(rt)) 331554cfb7eSYOSHIFUJI Hideaki continue; 332554cfb7eSYOSHIFUJI Hideaki 333554cfb7eSYOSHIFUJI Hideaki last = rt; 334554cfb7eSYOSHIFUJI Hideaki 335554cfb7eSYOSHIFUJI Hideaki m = rt6_score_route(rt, oif, strict); 336554cfb7eSYOSHIFUJI Hideaki if (m < 0) 337554cfb7eSYOSHIFUJI Hideaki continue; 338554cfb7eSYOSHIFUJI Hideaki 339554cfb7eSYOSHIFUJI Hideaki if (m > mpri) { 34027097255SYOSHIFUJI Hideaki rt6_probe(match); 341554cfb7eSYOSHIFUJI Hideaki match = rt; 3421da177e4SLinus Torvalds mpri = m; 34327097255SYOSHIFUJI Hideaki } else { 34427097255SYOSHIFUJI Hideaki rt6_probe(rt); 3451da177e4SLinus Torvalds } 3461da177e4SLinus Torvalds } 3471da177e4SLinus Torvalds 348554cfb7eSYOSHIFUJI Hideaki if (!match && 349554cfb7eSYOSHIFUJI Hideaki (strict & RT6_SELECT_F_REACHABLE) && 350554cfb7eSYOSHIFUJI Hideaki last && last != rt0) { 351554cfb7eSYOSHIFUJI Hideaki /* no entries matched; do round-robin */ 35234af946aSIngo Molnar static DEFINE_SPINLOCK(lock); 353c302e6d5SYOSHIFUJI Hideaki spin_lock(&lock); 354554cfb7eSYOSHIFUJI Hideaki *head = rt0->u.next; 355554cfb7eSYOSHIFUJI Hideaki rt0->u.next = last->u.next; 356554cfb7eSYOSHIFUJI Hideaki last->u.next = rt0; 357c302e6d5SYOSHIFUJI Hideaki spin_unlock(&lock); 358554cfb7eSYOSHIFUJI Hideaki } 359554cfb7eSYOSHIFUJI Hideaki 360554cfb7eSYOSHIFUJI Hideaki RT6_TRACE("%s() => %p, score=%d\n", 361554cfb7eSYOSHIFUJI Hideaki __FUNCTION__, match, mpri); 362554cfb7eSYOSHIFUJI Hideaki 363554cfb7eSYOSHIFUJI Hideaki return (match ? match : &ip6_null_entry); 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds 36670ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 36770ceb4f5SYOSHIFUJI Hideaki int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, 36870ceb4f5SYOSHIFUJI Hideaki struct in6_addr *gwaddr) 36970ceb4f5SYOSHIFUJI Hideaki { 37070ceb4f5SYOSHIFUJI Hideaki struct route_info *rinfo = (struct route_info *) opt; 37170ceb4f5SYOSHIFUJI Hideaki struct in6_addr prefix_buf, *prefix; 37270ceb4f5SYOSHIFUJI Hideaki unsigned int pref; 37370ceb4f5SYOSHIFUJI Hideaki u32 lifetime; 37470ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt; 37570ceb4f5SYOSHIFUJI Hideaki 37670ceb4f5SYOSHIFUJI Hideaki if (len < sizeof(struct route_info)) { 37770ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 37870ceb4f5SYOSHIFUJI Hideaki } 37970ceb4f5SYOSHIFUJI Hideaki 38070ceb4f5SYOSHIFUJI Hideaki /* Sanity check for prefix_len and length */ 38170ceb4f5SYOSHIFUJI Hideaki if (rinfo->length > 3) { 38270ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 38370ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 128) { 38470ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 38570ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 64) { 38670ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 2) { 38770ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 38870ceb4f5SYOSHIFUJI Hideaki } 38970ceb4f5SYOSHIFUJI Hideaki } else if (rinfo->prefix_len > 0) { 39070ceb4f5SYOSHIFUJI Hideaki if (rinfo->length < 1) { 39170ceb4f5SYOSHIFUJI Hideaki return -EINVAL; 39270ceb4f5SYOSHIFUJI Hideaki } 39370ceb4f5SYOSHIFUJI Hideaki } 39470ceb4f5SYOSHIFUJI Hideaki 39570ceb4f5SYOSHIFUJI Hideaki pref = rinfo->route_pref; 39670ceb4f5SYOSHIFUJI Hideaki if (pref == ICMPV6_ROUTER_PREF_INVALID) 39770ceb4f5SYOSHIFUJI Hideaki pref = ICMPV6_ROUTER_PREF_MEDIUM; 39870ceb4f5SYOSHIFUJI Hideaki 39970ceb4f5SYOSHIFUJI Hideaki lifetime = htonl(rinfo->lifetime); 40070ceb4f5SYOSHIFUJI Hideaki if (lifetime == 0xffffffff) { 40170ceb4f5SYOSHIFUJI Hideaki /* infinity */ 40270ceb4f5SYOSHIFUJI Hideaki } else if (lifetime > 0x7fffffff/HZ) { 40370ceb4f5SYOSHIFUJI Hideaki /* Avoid arithmetic overflow */ 40470ceb4f5SYOSHIFUJI Hideaki lifetime = 0x7fffffff/HZ - 1; 40570ceb4f5SYOSHIFUJI Hideaki } 40670ceb4f5SYOSHIFUJI Hideaki 40770ceb4f5SYOSHIFUJI Hideaki if (rinfo->length == 3) 40870ceb4f5SYOSHIFUJI Hideaki prefix = (struct in6_addr *)rinfo->prefix; 40970ceb4f5SYOSHIFUJI Hideaki else { 41070ceb4f5SYOSHIFUJI Hideaki /* this function is safe */ 41170ceb4f5SYOSHIFUJI Hideaki ipv6_addr_prefix(&prefix_buf, 41270ceb4f5SYOSHIFUJI Hideaki (struct in6_addr *)rinfo->prefix, 41370ceb4f5SYOSHIFUJI Hideaki rinfo->prefix_len); 41470ceb4f5SYOSHIFUJI Hideaki prefix = &prefix_buf; 41570ceb4f5SYOSHIFUJI Hideaki } 41670ceb4f5SYOSHIFUJI Hideaki 41770ceb4f5SYOSHIFUJI Hideaki rt = rt6_get_route_info(prefix, rinfo->prefix_len, gwaddr, dev->ifindex); 41870ceb4f5SYOSHIFUJI Hideaki 41970ceb4f5SYOSHIFUJI Hideaki if (rt && !lifetime) { 42070ceb4f5SYOSHIFUJI Hideaki ip6_del_rt(rt, NULL, NULL, NULL); 42170ceb4f5SYOSHIFUJI Hideaki rt = NULL; 42270ceb4f5SYOSHIFUJI Hideaki } 42370ceb4f5SYOSHIFUJI Hideaki 42470ceb4f5SYOSHIFUJI Hideaki if (!rt && lifetime) 42570ceb4f5SYOSHIFUJI Hideaki rt = rt6_add_route_info(prefix, rinfo->prefix_len, gwaddr, dev->ifindex, 42670ceb4f5SYOSHIFUJI Hideaki pref); 42770ceb4f5SYOSHIFUJI Hideaki else if (rt) 42870ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags = RTF_ROUTEINFO | 42970ceb4f5SYOSHIFUJI Hideaki (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); 43070ceb4f5SYOSHIFUJI Hideaki 43170ceb4f5SYOSHIFUJI Hideaki if (rt) { 43270ceb4f5SYOSHIFUJI Hideaki if (lifetime == 0xffffffff) { 43370ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags &= ~RTF_EXPIRES; 43470ceb4f5SYOSHIFUJI Hideaki } else { 43570ceb4f5SYOSHIFUJI Hideaki rt->rt6i_expires = jiffies + HZ * lifetime; 43670ceb4f5SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_EXPIRES; 43770ceb4f5SYOSHIFUJI Hideaki } 43870ceb4f5SYOSHIFUJI Hideaki dst_release(&rt->u.dst); 43970ceb4f5SYOSHIFUJI Hideaki } 44070ceb4f5SYOSHIFUJI Hideaki return 0; 44170ceb4f5SYOSHIFUJI Hideaki } 44270ceb4f5SYOSHIFUJI Hideaki #endif 44370ceb4f5SYOSHIFUJI Hideaki 4441da177e4SLinus Torvalds struct rt6_info *rt6_lookup(struct in6_addr *daddr, struct in6_addr *saddr, 4451da177e4SLinus Torvalds int oif, int strict) 4461da177e4SLinus Torvalds { 4471da177e4SLinus Torvalds struct fib6_node *fn; 4481da177e4SLinus Torvalds struct rt6_info *rt; 4491da177e4SLinus Torvalds 4501da177e4SLinus Torvalds read_lock_bh(&rt6_lock); 4511da177e4SLinus Torvalds fn = fib6_lookup(&ip6_routing_table, daddr, saddr); 4521da177e4SLinus Torvalds rt = rt6_device_match(fn->leaf, oif, strict); 4531da177e4SLinus Torvalds dst_hold(&rt->u.dst); 4541da177e4SLinus Torvalds rt->u.dst.__use++; 4551da177e4SLinus Torvalds read_unlock_bh(&rt6_lock); 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds rt->u.dst.lastuse = jiffies; 4581da177e4SLinus Torvalds if (rt->u.dst.error == 0) 4591da177e4SLinus Torvalds return rt; 4601da177e4SLinus Torvalds dst_release(&rt->u.dst); 4611da177e4SLinus Torvalds return NULL; 4621da177e4SLinus Torvalds } 4631da177e4SLinus Torvalds 4641da177e4SLinus Torvalds /* ip6_ins_rt is called with FREE rt6_lock. 4651da177e4SLinus Torvalds It takes new route entry, the addition fails by any reason the 4661da177e4SLinus Torvalds route is freed. In any case, if caller does not hold it, it may 4671da177e4SLinus Torvalds be destroyed. 4681da177e4SLinus Torvalds */ 4691da177e4SLinus Torvalds 4700d51aa80SJamal Hadi Salim int ip6_ins_rt(struct rt6_info *rt, struct nlmsghdr *nlh, 4710d51aa80SJamal Hadi Salim void *_rtattr, struct netlink_skb_parms *req) 4721da177e4SLinus Torvalds { 4731da177e4SLinus Torvalds int err; 4741da177e4SLinus Torvalds 4751da177e4SLinus Torvalds write_lock_bh(&rt6_lock); 4760d51aa80SJamal Hadi Salim err = fib6_add(&ip6_routing_table, rt, nlh, _rtattr, req); 4771da177e4SLinus Torvalds write_unlock_bh(&rt6_lock); 4781da177e4SLinus Torvalds 4791da177e4SLinus Torvalds return err; 4801da177e4SLinus Torvalds } 4811da177e4SLinus Torvalds 48295a9a5baSYOSHIFUJI Hideaki static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, struct in6_addr *daddr, 48395a9a5baSYOSHIFUJI Hideaki struct in6_addr *saddr) 4841da177e4SLinus Torvalds { 4851da177e4SLinus Torvalds struct rt6_info *rt; 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds /* 4881da177e4SLinus Torvalds * Clone the route. 4891da177e4SLinus Torvalds */ 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds rt = ip6_rt_copy(ort); 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds if (rt) { 49458c4fb86SYOSHIFUJI Hideaki if (!(rt->rt6i_flags&RTF_GATEWAY)) { 49558c4fb86SYOSHIFUJI Hideaki if (rt->rt6i_dst.plen != 128 && 49658c4fb86SYOSHIFUJI Hideaki ipv6_addr_equal(&rt->rt6i_dst.addr, daddr)) 49758c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 4981da177e4SLinus Torvalds ipv6_addr_copy(&rt->rt6i_gateway, daddr); 49958c4fb86SYOSHIFUJI Hideaki } 5001da177e4SLinus Torvalds 50158c4fb86SYOSHIFUJI Hideaki ipv6_addr_copy(&rt->rt6i_dst.addr, daddr); 5021da177e4SLinus Torvalds rt->rt6i_dst.plen = 128; 5031da177e4SLinus Torvalds rt->rt6i_flags |= RTF_CACHE; 5041da177e4SLinus Torvalds rt->u.dst.flags |= DST_HOST; 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 5071da177e4SLinus Torvalds if (rt->rt6i_src.plen && saddr) { 5081da177e4SLinus Torvalds ipv6_addr_copy(&rt->rt6i_src.addr, saddr); 5091da177e4SLinus Torvalds rt->rt6i_src.plen = 128; 5101da177e4SLinus Torvalds } 5111da177e4SLinus Torvalds #endif 5121da177e4SLinus Torvalds 5131da177e4SLinus Torvalds rt->rt6i_nexthop = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway); 5141da177e4SLinus Torvalds 51595a9a5baSYOSHIFUJI Hideaki } 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds return rt; 5181da177e4SLinus Torvalds } 51995a9a5baSYOSHIFUJI Hideaki 520299d9939SYOSHIFUJI Hideaki static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, struct in6_addr *daddr) 521299d9939SYOSHIFUJI Hideaki { 522299d9939SYOSHIFUJI Hideaki struct rt6_info *rt = ip6_rt_copy(ort); 523299d9939SYOSHIFUJI Hideaki if (rt) { 524299d9939SYOSHIFUJI Hideaki ipv6_addr_copy(&rt->rt6i_dst.addr, daddr); 525299d9939SYOSHIFUJI Hideaki rt->rt6i_dst.plen = 128; 526299d9939SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_CACHE; 527299d9939SYOSHIFUJI Hideaki if (rt->rt6i_flags & RTF_REJECT) 528299d9939SYOSHIFUJI Hideaki rt->u.dst.error = ort->u.dst.error; 529299d9939SYOSHIFUJI Hideaki rt->u.dst.flags |= DST_HOST; 530299d9939SYOSHIFUJI Hideaki rt->rt6i_nexthop = neigh_clone(ort->rt6i_nexthop); 531299d9939SYOSHIFUJI Hideaki } 532299d9939SYOSHIFUJI Hideaki return rt; 533299d9939SYOSHIFUJI Hideaki } 534299d9939SYOSHIFUJI Hideaki 5351da177e4SLinus Torvalds #define BACKTRACK() \ 536bb133964SYOSHIFUJI Hideaki if (rt == &ip6_null_entry) { \ 5371da177e4SLinus Torvalds while ((fn = fn->parent) != NULL) { \ 5381da177e4SLinus Torvalds if (fn->fn_flags & RTN_ROOT) { \ 5391da177e4SLinus Torvalds goto out; \ 5401da177e4SLinus Torvalds } \ 5411da177e4SLinus Torvalds if (fn->fn_flags & RTN_RTINFO) \ 5421da177e4SLinus Torvalds goto restart; \ 5431da177e4SLinus Torvalds } \ 5441da177e4SLinus Torvalds } 5451da177e4SLinus Torvalds 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds void ip6_route_input(struct sk_buff *skb) 5481da177e4SLinus Torvalds { 5491da177e4SLinus Torvalds struct fib6_node *fn; 550519fbd87SYOSHIFUJI Hideaki struct rt6_info *rt, *nrt; 5511da177e4SLinus Torvalds int strict; 5521da177e4SLinus Torvalds int attempts = 3; 553519fbd87SYOSHIFUJI Hideaki int err; 5548238dd06SYOSHIFUJI Hideaki int reachable = RT6_SELECT_F_REACHABLE; 5551da177e4SLinus Torvalds 556118f8c16SYOSHIFUJI Hideaki strict = ipv6_addr_type(&skb->nh.ipv6h->daddr) & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL) ? RT6_SELECT_F_IFACE : 0; 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds relookup: 5591da177e4SLinus Torvalds read_lock_bh(&rt6_lock); 5601da177e4SLinus Torvalds 5618238dd06SYOSHIFUJI Hideaki restart_2: 5621da177e4SLinus Torvalds fn = fib6_lookup(&ip6_routing_table, &skb->nh.ipv6h->daddr, 5631da177e4SLinus Torvalds &skb->nh.ipv6h->saddr); 5641da177e4SLinus Torvalds 5651da177e4SLinus Torvalds restart: 5668238dd06SYOSHIFUJI Hideaki rt = rt6_select(&fn->leaf, skb->dev->ifindex, strict | reachable); 5671da177e4SLinus Torvalds BACKTRACK(); 5688238dd06SYOSHIFUJI Hideaki if (rt == &ip6_null_entry || 5698238dd06SYOSHIFUJI Hideaki rt->rt6i_flags & RTF_CACHE) 5701da177e4SLinus Torvalds goto out; 5711da177e4SLinus Torvalds 5721da177e4SLinus Torvalds dst_hold(&rt->u.dst); 5731da177e4SLinus Torvalds read_unlock_bh(&rt6_lock); 5741da177e4SLinus Torvalds 575519fbd87SYOSHIFUJI Hideaki if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP)) 576519fbd87SYOSHIFUJI Hideaki nrt = rt6_alloc_cow(rt, &skb->nh.ipv6h->daddr, &skb->nh.ipv6h->saddr); 577519fbd87SYOSHIFUJI Hideaki else { 578519fbd87SYOSHIFUJI Hideaki #if CLONE_OFFLINK_ROUTE 579519fbd87SYOSHIFUJI Hideaki nrt = rt6_alloc_clone(rt, &skb->nh.ipv6h->daddr); 580519fbd87SYOSHIFUJI Hideaki #else 581519fbd87SYOSHIFUJI Hideaki goto out2; 582519fbd87SYOSHIFUJI Hideaki #endif 583519fbd87SYOSHIFUJI Hideaki } 5841da177e4SLinus Torvalds 5851da177e4SLinus Torvalds dst_release(&rt->u.dst); 586e40cf353SYOSHIFUJI Hideaki rt = nrt ? : &ip6_null_entry; 5871da177e4SLinus Torvalds 588e40cf353SYOSHIFUJI Hideaki dst_hold(&rt->u.dst); 589e40cf353SYOSHIFUJI Hideaki if (nrt) { 590519fbd87SYOSHIFUJI Hideaki err = ip6_ins_rt(nrt, NULL, NULL, &NETLINK_CB(skb)); 591e40cf353SYOSHIFUJI Hideaki if (!err) 592e40cf353SYOSHIFUJI Hideaki goto out2; 593e40cf353SYOSHIFUJI Hideaki } 594e40cf353SYOSHIFUJI Hideaki 595e40cf353SYOSHIFUJI Hideaki if (--attempts <= 0) 5961da177e4SLinus Torvalds goto out2; 5971da177e4SLinus Torvalds 598519fbd87SYOSHIFUJI Hideaki /* 599519fbd87SYOSHIFUJI Hideaki * Race condition! In the gap, when rt6_lock was 600519fbd87SYOSHIFUJI Hideaki * released someone could insert this route. Relookup. 6011da177e4SLinus Torvalds */ 6021da177e4SLinus Torvalds dst_release(&rt->u.dst); 6031da177e4SLinus Torvalds goto relookup; 6041da177e4SLinus Torvalds 605519fbd87SYOSHIFUJI Hideaki out: 6068238dd06SYOSHIFUJI Hideaki if (reachable) { 6078238dd06SYOSHIFUJI Hideaki reachable = 0; 6088238dd06SYOSHIFUJI Hideaki goto restart_2; 6098238dd06SYOSHIFUJI Hideaki } 610519fbd87SYOSHIFUJI Hideaki dst_hold(&rt->u.dst); 611519fbd87SYOSHIFUJI Hideaki read_unlock_bh(&rt6_lock); 6121da177e4SLinus Torvalds out2: 6131da177e4SLinus Torvalds rt->u.dst.lastuse = jiffies; 6141da177e4SLinus Torvalds rt->u.dst.__use++; 6151da177e4SLinus Torvalds skb->dst = (struct dst_entry *) rt; 616fb9de91eSYOSHIFUJI Hideaki return; 6171da177e4SLinus Torvalds } 6181da177e4SLinus Torvalds 6191da177e4SLinus Torvalds struct dst_entry * ip6_route_output(struct sock *sk, struct flowi *fl) 6201da177e4SLinus Torvalds { 6211da177e4SLinus Torvalds struct fib6_node *fn; 622519fbd87SYOSHIFUJI Hideaki struct rt6_info *rt, *nrt; 6231da177e4SLinus Torvalds int strict; 6241da177e4SLinus Torvalds int attempts = 3; 625519fbd87SYOSHIFUJI Hideaki int err; 6268238dd06SYOSHIFUJI Hideaki int reachable = RT6_SELECT_F_REACHABLE; 6271da177e4SLinus Torvalds 628554cfb7eSYOSHIFUJI Hideaki strict = ipv6_addr_type(&fl->fl6_dst) & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL) ? RT6_SELECT_F_IFACE : 0; 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds relookup: 6311da177e4SLinus Torvalds read_lock_bh(&rt6_lock); 6321da177e4SLinus Torvalds 6338238dd06SYOSHIFUJI Hideaki restart_2: 6341da177e4SLinus Torvalds fn = fib6_lookup(&ip6_routing_table, &fl->fl6_dst, &fl->fl6_src); 6351da177e4SLinus Torvalds 6361da177e4SLinus Torvalds restart: 6378238dd06SYOSHIFUJI Hideaki rt = rt6_select(&fn->leaf, fl->oif, strict | reachable); 6381da177e4SLinus Torvalds BACKTRACK(); 6398238dd06SYOSHIFUJI Hideaki if (rt == &ip6_null_entry || 6408238dd06SYOSHIFUJI Hideaki rt->rt6i_flags & RTF_CACHE) 6411da177e4SLinus Torvalds goto out; 6421da177e4SLinus Torvalds 6431da177e4SLinus Torvalds dst_hold(&rt->u.dst); 6441da177e4SLinus Torvalds read_unlock_bh(&rt6_lock); 6451da177e4SLinus Torvalds 646519fbd87SYOSHIFUJI Hideaki if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP)) 647e40cf353SYOSHIFUJI Hideaki nrt = rt6_alloc_cow(rt, &fl->fl6_dst, &fl->fl6_src); 648519fbd87SYOSHIFUJI Hideaki else { 649519fbd87SYOSHIFUJI Hideaki #if CLONE_OFFLINK_ROUTE 650519fbd87SYOSHIFUJI Hideaki nrt = rt6_alloc_clone(rt, &fl->fl6_dst); 651519fbd87SYOSHIFUJI Hideaki #else 652519fbd87SYOSHIFUJI Hideaki goto out2; 653519fbd87SYOSHIFUJI Hideaki #endif 654519fbd87SYOSHIFUJI Hideaki } 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds dst_release(&rt->u.dst); 657e40cf353SYOSHIFUJI Hideaki rt = nrt ? : &ip6_null_entry; 6581da177e4SLinus Torvalds 659e40cf353SYOSHIFUJI Hideaki dst_hold(&rt->u.dst); 660e40cf353SYOSHIFUJI Hideaki if (nrt) { 661e40cf353SYOSHIFUJI Hideaki err = ip6_ins_rt(nrt, NULL, NULL, NULL); 662e40cf353SYOSHIFUJI Hideaki if (!err) 663e40cf353SYOSHIFUJI Hideaki goto out2; 664e40cf353SYOSHIFUJI Hideaki } 665e40cf353SYOSHIFUJI Hideaki 666e40cf353SYOSHIFUJI Hideaki if (--attempts <= 0) 6671da177e4SLinus Torvalds goto out2; 6681da177e4SLinus Torvalds 669519fbd87SYOSHIFUJI Hideaki /* 670519fbd87SYOSHIFUJI Hideaki * Race condition! In the gap, when rt6_lock was 671519fbd87SYOSHIFUJI Hideaki * released someone could insert this route. Relookup. 6721da177e4SLinus Torvalds */ 6731da177e4SLinus Torvalds dst_release(&rt->u.dst); 6741da177e4SLinus Torvalds goto relookup; 675e40cf353SYOSHIFUJI Hideaki 676519fbd87SYOSHIFUJI Hideaki out: 6778238dd06SYOSHIFUJI Hideaki if (reachable) { 6788238dd06SYOSHIFUJI Hideaki reachable = 0; 6798238dd06SYOSHIFUJI Hideaki goto restart_2; 6808238dd06SYOSHIFUJI Hideaki } 681519fbd87SYOSHIFUJI Hideaki dst_hold(&rt->u.dst); 682519fbd87SYOSHIFUJI Hideaki read_unlock_bh(&rt6_lock); 6831da177e4SLinus Torvalds out2: 6841da177e4SLinus Torvalds rt->u.dst.lastuse = jiffies; 6851da177e4SLinus Torvalds rt->u.dst.__use++; 6861da177e4SLinus Torvalds return &rt->u.dst; 6871da177e4SLinus Torvalds } 6881da177e4SLinus Torvalds 6891da177e4SLinus Torvalds 6901da177e4SLinus Torvalds /* 6911da177e4SLinus Torvalds * Destination cache support functions 6921da177e4SLinus Torvalds */ 6931da177e4SLinus Torvalds 6941da177e4SLinus Torvalds static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) 6951da177e4SLinus Torvalds { 6961da177e4SLinus Torvalds struct rt6_info *rt; 6971da177e4SLinus Torvalds 6981da177e4SLinus Torvalds rt = (struct rt6_info *) dst; 6991da177e4SLinus Torvalds 7001da177e4SLinus Torvalds if (rt && rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) 7011da177e4SLinus Torvalds return dst; 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds return NULL; 7041da177e4SLinus Torvalds } 7051da177e4SLinus Torvalds 7061da177e4SLinus Torvalds static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 7071da177e4SLinus Torvalds { 7081da177e4SLinus Torvalds struct rt6_info *rt = (struct rt6_info *) dst; 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds if (rt) { 7111da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) 7120d51aa80SJamal Hadi Salim ip6_del_rt(rt, NULL, NULL, NULL); 7131da177e4SLinus Torvalds else 7141da177e4SLinus Torvalds dst_release(dst); 7151da177e4SLinus Torvalds } 7161da177e4SLinus Torvalds return NULL; 7171da177e4SLinus Torvalds } 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds static void ip6_link_failure(struct sk_buff *skb) 7201da177e4SLinus Torvalds { 7211da177e4SLinus Torvalds struct rt6_info *rt; 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0, skb->dev); 7241da177e4SLinus Torvalds 7251da177e4SLinus Torvalds rt = (struct rt6_info *) skb->dst; 7261da177e4SLinus Torvalds if (rt) { 7271da177e4SLinus Torvalds if (rt->rt6i_flags&RTF_CACHE) { 7281da177e4SLinus Torvalds dst_set_expires(&rt->u.dst, 0); 7291da177e4SLinus Torvalds rt->rt6i_flags |= RTF_EXPIRES; 7301da177e4SLinus Torvalds } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) 7311da177e4SLinus Torvalds rt->rt6i_node->fn_sernum = -1; 7321da177e4SLinus Torvalds } 7331da177e4SLinus Torvalds } 7341da177e4SLinus Torvalds 7351da177e4SLinus Torvalds static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu) 7361da177e4SLinus Torvalds { 7371da177e4SLinus Torvalds struct rt6_info *rt6 = (struct rt6_info*)dst; 7381da177e4SLinus Torvalds 7391da177e4SLinus Torvalds if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) { 7401da177e4SLinus Torvalds rt6->rt6i_flags |= RTF_MODIFIED; 7411da177e4SLinus Torvalds if (mtu < IPV6_MIN_MTU) { 7421da177e4SLinus Torvalds mtu = IPV6_MIN_MTU; 7431da177e4SLinus Torvalds dst->metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG; 7441da177e4SLinus Torvalds } 7451da177e4SLinus Torvalds dst->metrics[RTAX_MTU-1] = mtu; 7468d71740cSTom Tucker call_netevent_notifiers(NETEVENT_PMTU_UPDATE, dst); 7471da177e4SLinus Torvalds } 7481da177e4SLinus Torvalds } 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds static int ipv6_get_mtu(struct net_device *dev); 7511da177e4SLinus Torvalds 7521da177e4SLinus Torvalds static inline unsigned int ipv6_advmss(unsigned int mtu) 7531da177e4SLinus Torvalds { 7541da177e4SLinus Torvalds mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); 7551da177e4SLinus Torvalds 7561da177e4SLinus Torvalds if (mtu < ip6_rt_min_advmss) 7571da177e4SLinus Torvalds mtu = ip6_rt_min_advmss; 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds /* 7601da177e4SLinus Torvalds * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and 7611da177e4SLinus Torvalds * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. 7621da177e4SLinus Torvalds * IPV6_MAXPLEN is also valid and means: "any MSS, 7631da177e4SLinus Torvalds * rely only on pmtu discovery" 7641da177e4SLinus Torvalds */ 7651da177e4SLinus Torvalds if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) 7661da177e4SLinus Torvalds mtu = IPV6_MAXPLEN; 7671da177e4SLinus Torvalds return mtu; 7681da177e4SLinus Torvalds } 7691da177e4SLinus Torvalds 770*5d0bbeebSThomas Graf static struct dst_entry *ndisc_dst_gc_list; 771*5d0bbeebSThomas Graf DEFINE_SPINLOCK(ndisc_lock); 772*5d0bbeebSThomas Graf 7731da177e4SLinus Torvalds struct dst_entry *ndisc_dst_alloc(struct net_device *dev, 7741da177e4SLinus Torvalds struct neighbour *neigh, 7751da177e4SLinus Torvalds struct in6_addr *addr, 7761da177e4SLinus Torvalds int (*output)(struct sk_buff *)) 7771da177e4SLinus Torvalds { 7781da177e4SLinus Torvalds struct rt6_info *rt; 7791da177e4SLinus Torvalds struct inet6_dev *idev = in6_dev_get(dev); 7801da177e4SLinus Torvalds 7811da177e4SLinus Torvalds if (unlikely(idev == NULL)) 7821da177e4SLinus Torvalds return NULL; 7831da177e4SLinus Torvalds 7841da177e4SLinus Torvalds rt = ip6_dst_alloc(); 7851da177e4SLinus Torvalds if (unlikely(rt == NULL)) { 7861da177e4SLinus Torvalds in6_dev_put(idev); 7871da177e4SLinus Torvalds goto out; 7881da177e4SLinus Torvalds } 7891da177e4SLinus Torvalds 7901da177e4SLinus Torvalds dev_hold(dev); 7911da177e4SLinus Torvalds if (neigh) 7921da177e4SLinus Torvalds neigh_hold(neigh); 7931da177e4SLinus Torvalds else 7941da177e4SLinus Torvalds neigh = ndisc_get_neigh(dev, addr); 7951da177e4SLinus Torvalds 7961da177e4SLinus Torvalds rt->rt6i_dev = dev; 7971da177e4SLinus Torvalds rt->rt6i_idev = idev; 7981da177e4SLinus Torvalds rt->rt6i_nexthop = neigh; 7991da177e4SLinus Torvalds atomic_set(&rt->u.dst.__refcnt, 1); 8001da177e4SLinus Torvalds rt->u.dst.metrics[RTAX_HOPLIMIT-1] = 255; 8011da177e4SLinus Torvalds rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(rt->rt6i_dev); 8021da177e4SLinus Torvalds rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(dst_mtu(&rt->u.dst)); 8031da177e4SLinus Torvalds rt->u.dst.output = output; 8041da177e4SLinus Torvalds 8051da177e4SLinus Torvalds #if 0 /* there's no chance to use these for ndisc */ 8061da177e4SLinus Torvalds rt->u.dst.flags = ipv6_addr_type(addr) & IPV6_ADDR_UNICAST 8071da177e4SLinus Torvalds ? DST_HOST 8081da177e4SLinus Torvalds : 0; 8091da177e4SLinus Torvalds ipv6_addr_copy(&rt->rt6i_dst.addr, addr); 8101da177e4SLinus Torvalds rt->rt6i_dst.plen = 128; 8111da177e4SLinus Torvalds #endif 8121da177e4SLinus Torvalds 813*5d0bbeebSThomas Graf spin_lock_bh(&ndisc_lock); 8141da177e4SLinus Torvalds rt->u.dst.next = ndisc_dst_gc_list; 8151da177e4SLinus Torvalds ndisc_dst_gc_list = &rt->u.dst; 816*5d0bbeebSThomas Graf spin_unlock_bh(&ndisc_lock); 8171da177e4SLinus Torvalds 8181da177e4SLinus Torvalds fib6_force_start_gc(); 8191da177e4SLinus Torvalds 8201da177e4SLinus Torvalds out: 8211da177e4SLinus Torvalds return (struct dst_entry *)rt; 8221da177e4SLinus Torvalds } 8231da177e4SLinus Torvalds 8241da177e4SLinus Torvalds int ndisc_dst_gc(int *more) 8251da177e4SLinus Torvalds { 8261da177e4SLinus Torvalds struct dst_entry *dst, *next, **pprev; 8271da177e4SLinus Torvalds int freed; 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds next = NULL; 8301da177e4SLinus Torvalds freed = 0; 831*5d0bbeebSThomas Graf 832*5d0bbeebSThomas Graf spin_lock_bh(&ndisc_lock); 833*5d0bbeebSThomas Graf pprev = &ndisc_dst_gc_list; 834*5d0bbeebSThomas Graf 8351da177e4SLinus Torvalds while ((dst = *pprev) != NULL) { 8361da177e4SLinus Torvalds if (!atomic_read(&dst->__refcnt)) { 8371da177e4SLinus Torvalds *pprev = dst->next; 8381da177e4SLinus Torvalds dst_free(dst); 8391da177e4SLinus Torvalds freed++; 8401da177e4SLinus Torvalds } else { 8411da177e4SLinus Torvalds pprev = &dst->next; 8421da177e4SLinus Torvalds (*more)++; 8431da177e4SLinus Torvalds } 8441da177e4SLinus Torvalds } 8451da177e4SLinus Torvalds 846*5d0bbeebSThomas Graf spin_unlock_bh(&ndisc_lock); 847*5d0bbeebSThomas Graf 8481da177e4SLinus Torvalds return freed; 8491da177e4SLinus Torvalds } 8501da177e4SLinus Torvalds 8511da177e4SLinus Torvalds static int ip6_dst_gc(void) 8521da177e4SLinus Torvalds { 8531da177e4SLinus Torvalds static unsigned expire = 30*HZ; 8541da177e4SLinus Torvalds static unsigned long last_gc; 8551da177e4SLinus Torvalds unsigned long now = jiffies; 8561da177e4SLinus Torvalds 8571da177e4SLinus Torvalds if (time_after(last_gc + ip6_rt_gc_min_interval, now) && 8581da177e4SLinus Torvalds atomic_read(&ip6_dst_ops.entries) <= ip6_rt_max_size) 8591da177e4SLinus Torvalds goto out; 8601da177e4SLinus Torvalds 8611da177e4SLinus Torvalds expire++; 8621da177e4SLinus Torvalds fib6_run_gc(expire); 8631da177e4SLinus Torvalds last_gc = now; 8641da177e4SLinus Torvalds if (atomic_read(&ip6_dst_ops.entries) < ip6_dst_ops.gc_thresh) 8651da177e4SLinus Torvalds expire = ip6_rt_gc_timeout>>1; 8661da177e4SLinus Torvalds 8671da177e4SLinus Torvalds out: 8681da177e4SLinus Torvalds expire -= expire>>ip6_rt_gc_elasticity; 8691da177e4SLinus Torvalds return (atomic_read(&ip6_dst_ops.entries) > ip6_rt_max_size); 8701da177e4SLinus Torvalds } 8711da177e4SLinus Torvalds 8721da177e4SLinus Torvalds /* Clean host part of a prefix. Not necessary in radix tree, 8731da177e4SLinus Torvalds but results in cleaner routing tables. 8741da177e4SLinus Torvalds 8751da177e4SLinus Torvalds Remove it only when all the things will work! 8761da177e4SLinus Torvalds */ 8771da177e4SLinus Torvalds 8781da177e4SLinus Torvalds static int ipv6_get_mtu(struct net_device *dev) 8791da177e4SLinus Torvalds { 8801da177e4SLinus Torvalds int mtu = IPV6_MIN_MTU; 8811da177e4SLinus Torvalds struct inet6_dev *idev; 8821da177e4SLinus Torvalds 8831da177e4SLinus Torvalds idev = in6_dev_get(dev); 8841da177e4SLinus Torvalds if (idev) { 8851da177e4SLinus Torvalds mtu = idev->cnf.mtu6; 8861da177e4SLinus Torvalds in6_dev_put(idev); 8871da177e4SLinus Torvalds } 8881da177e4SLinus Torvalds return mtu; 8891da177e4SLinus Torvalds } 8901da177e4SLinus Torvalds 8911da177e4SLinus Torvalds int ipv6_get_hoplimit(struct net_device *dev) 8921da177e4SLinus Torvalds { 8931da177e4SLinus Torvalds int hoplimit = ipv6_devconf.hop_limit; 8941da177e4SLinus Torvalds struct inet6_dev *idev; 8951da177e4SLinus Torvalds 8961da177e4SLinus Torvalds idev = in6_dev_get(dev); 8971da177e4SLinus Torvalds if (idev) { 8981da177e4SLinus Torvalds hoplimit = idev->cnf.hop_limit; 8991da177e4SLinus Torvalds in6_dev_put(idev); 9001da177e4SLinus Torvalds } 9011da177e4SLinus Torvalds return hoplimit; 9021da177e4SLinus Torvalds } 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds /* 9051da177e4SLinus Torvalds * 9061da177e4SLinus Torvalds */ 9071da177e4SLinus Torvalds 9080d51aa80SJamal Hadi Salim int ip6_route_add(struct in6_rtmsg *rtmsg, struct nlmsghdr *nlh, 9090d51aa80SJamal Hadi Salim void *_rtattr, struct netlink_skb_parms *req) 9101da177e4SLinus Torvalds { 9111da177e4SLinus Torvalds int err; 9121da177e4SLinus Torvalds struct rtmsg *r; 9131da177e4SLinus Torvalds struct rtattr **rta; 9141da177e4SLinus Torvalds struct rt6_info *rt = NULL; 9151da177e4SLinus Torvalds struct net_device *dev = NULL; 9161da177e4SLinus Torvalds struct inet6_dev *idev = NULL; 9171da177e4SLinus Torvalds int addr_type; 9181da177e4SLinus Torvalds 9191da177e4SLinus Torvalds rta = (struct rtattr **) _rtattr; 9201da177e4SLinus Torvalds 9211da177e4SLinus Torvalds if (rtmsg->rtmsg_dst_len > 128 || rtmsg->rtmsg_src_len > 128) 9221da177e4SLinus Torvalds return -EINVAL; 9231da177e4SLinus Torvalds #ifndef CONFIG_IPV6_SUBTREES 9241da177e4SLinus Torvalds if (rtmsg->rtmsg_src_len) 9251da177e4SLinus Torvalds return -EINVAL; 9261da177e4SLinus Torvalds #endif 9271da177e4SLinus Torvalds if (rtmsg->rtmsg_ifindex) { 9281da177e4SLinus Torvalds err = -ENODEV; 9291da177e4SLinus Torvalds dev = dev_get_by_index(rtmsg->rtmsg_ifindex); 9301da177e4SLinus Torvalds if (!dev) 9311da177e4SLinus Torvalds goto out; 9321da177e4SLinus Torvalds idev = in6_dev_get(dev); 9331da177e4SLinus Torvalds if (!idev) 9341da177e4SLinus Torvalds goto out; 9351da177e4SLinus Torvalds } 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds if (rtmsg->rtmsg_metric == 0) 9381da177e4SLinus Torvalds rtmsg->rtmsg_metric = IP6_RT_PRIO_USER; 9391da177e4SLinus Torvalds 9401da177e4SLinus Torvalds rt = ip6_dst_alloc(); 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds if (rt == NULL) { 9431da177e4SLinus Torvalds err = -ENOMEM; 9441da177e4SLinus Torvalds goto out; 9451da177e4SLinus Torvalds } 9461da177e4SLinus Torvalds 9471da177e4SLinus Torvalds rt->u.dst.obsolete = -1; 9483dd4bc68SYOSHIFUJI Hideaki rt->rt6i_expires = jiffies + clock_t_to_jiffies(rtmsg->rtmsg_info); 9491da177e4SLinus Torvalds if (nlh && (r = NLMSG_DATA(nlh))) { 9501da177e4SLinus Torvalds rt->rt6i_protocol = r->rtm_protocol; 9511da177e4SLinus Torvalds } else { 9521da177e4SLinus Torvalds rt->rt6i_protocol = RTPROT_BOOT; 9531da177e4SLinus Torvalds } 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds addr_type = ipv6_addr_type(&rtmsg->rtmsg_dst); 9561da177e4SLinus Torvalds 9571da177e4SLinus Torvalds if (addr_type & IPV6_ADDR_MULTICAST) 9581da177e4SLinus Torvalds rt->u.dst.input = ip6_mc_input; 9591da177e4SLinus Torvalds else 9601da177e4SLinus Torvalds rt->u.dst.input = ip6_forward; 9611da177e4SLinus Torvalds 9621da177e4SLinus Torvalds rt->u.dst.output = ip6_output; 9631da177e4SLinus Torvalds 9641da177e4SLinus Torvalds ipv6_addr_prefix(&rt->rt6i_dst.addr, 9651da177e4SLinus Torvalds &rtmsg->rtmsg_dst, rtmsg->rtmsg_dst_len); 9661da177e4SLinus Torvalds rt->rt6i_dst.plen = rtmsg->rtmsg_dst_len; 9671da177e4SLinus Torvalds if (rt->rt6i_dst.plen == 128) 9681da177e4SLinus Torvalds rt->u.dst.flags = DST_HOST; 9691da177e4SLinus Torvalds 9701da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 9711da177e4SLinus Torvalds ipv6_addr_prefix(&rt->rt6i_src.addr, 9721da177e4SLinus Torvalds &rtmsg->rtmsg_src, rtmsg->rtmsg_src_len); 9731da177e4SLinus Torvalds rt->rt6i_src.plen = rtmsg->rtmsg_src_len; 9741da177e4SLinus Torvalds #endif 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds rt->rt6i_metric = rtmsg->rtmsg_metric; 9771da177e4SLinus Torvalds 9781da177e4SLinus Torvalds /* We cannot add true routes via loopback here, 9791da177e4SLinus Torvalds they would result in kernel looping; promote them to reject routes 9801da177e4SLinus Torvalds */ 9811da177e4SLinus Torvalds if ((rtmsg->rtmsg_flags&RTF_REJECT) || 9821da177e4SLinus Torvalds (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK))) { 9831da177e4SLinus Torvalds /* hold loopback dev/idev if we haven't done so. */ 9841da177e4SLinus Torvalds if (dev != &loopback_dev) { 9851da177e4SLinus Torvalds if (dev) { 9861da177e4SLinus Torvalds dev_put(dev); 9871da177e4SLinus Torvalds in6_dev_put(idev); 9881da177e4SLinus Torvalds } 9891da177e4SLinus Torvalds dev = &loopback_dev; 9901da177e4SLinus Torvalds dev_hold(dev); 9911da177e4SLinus Torvalds idev = in6_dev_get(dev); 9921da177e4SLinus Torvalds if (!idev) { 9931da177e4SLinus Torvalds err = -ENODEV; 9941da177e4SLinus Torvalds goto out; 9951da177e4SLinus Torvalds } 9961da177e4SLinus Torvalds } 9971da177e4SLinus Torvalds rt->u.dst.output = ip6_pkt_discard_out; 9981da177e4SLinus Torvalds rt->u.dst.input = ip6_pkt_discard; 9991da177e4SLinus Torvalds rt->u.dst.error = -ENETUNREACH; 10001da177e4SLinus Torvalds rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP; 10011da177e4SLinus Torvalds goto install_route; 10021da177e4SLinus Torvalds } 10031da177e4SLinus Torvalds 10041da177e4SLinus Torvalds if (rtmsg->rtmsg_flags & RTF_GATEWAY) { 10051da177e4SLinus Torvalds struct in6_addr *gw_addr; 10061da177e4SLinus Torvalds int gwa_type; 10071da177e4SLinus Torvalds 10081da177e4SLinus Torvalds gw_addr = &rtmsg->rtmsg_gateway; 10091da177e4SLinus Torvalds ipv6_addr_copy(&rt->rt6i_gateway, &rtmsg->rtmsg_gateway); 10101da177e4SLinus Torvalds gwa_type = ipv6_addr_type(gw_addr); 10111da177e4SLinus Torvalds 10121da177e4SLinus Torvalds if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) { 10131da177e4SLinus Torvalds struct rt6_info *grt; 10141da177e4SLinus Torvalds 10151da177e4SLinus Torvalds /* IPv6 strictly inhibits using not link-local 10161da177e4SLinus Torvalds addresses as nexthop address. 10171da177e4SLinus Torvalds Otherwise, router will not able to send redirects. 10181da177e4SLinus Torvalds It is very good, but in some (rare!) circumstances 10191da177e4SLinus Torvalds (SIT, PtP, NBMA NOARP links) it is handy to allow 10201da177e4SLinus Torvalds some exceptions. --ANK 10211da177e4SLinus Torvalds */ 10221da177e4SLinus Torvalds err = -EINVAL; 10231da177e4SLinus Torvalds if (!(gwa_type&IPV6_ADDR_UNICAST)) 10241da177e4SLinus Torvalds goto out; 10251da177e4SLinus Torvalds 10261da177e4SLinus Torvalds grt = rt6_lookup(gw_addr, NULL, rtmsg->rtmsg_ifindex, 1); 10271da177e4SLinus Torvalds 10281da177e4SLinus Torvalds err = -EHOSTUNREACH; 10291da177e4SLinus Torvalds if (grt == NULL) 10301da177e4SLinus Torvalds goto out; 10311da177e4SLinus Torvalds if (dev) { 10321da177e4SLinus Torvalds if (dev != grt->rt6i_dev) { 10331da177e4SLinus Torvalds dst_release(&grt->u.dst); 10341da177e4SLinus Torvalds goto out; 10351da177e4SLinus Torvalds } 10361da177e4SLinus Torvalds } else { 10371da177e4SLinus Torvalds dev = grt->rt6i_dev; 10381da177e4SLinus Torvalds idev = grt->rt6i_idev; 10391da177e4SLinus Torvalds dev_hold(dev); 10401da177e4SLinus Torvalds in6_dev_hold(grt->rt6i_idev); 10411da177e4SLinus Torvalds } 10421da177e4SLinus Torvalds if (!(grt->rt6i_flags&RTF_GATEWAY)) 10431da177e4SLinus Torvalds err = 0; 10441da177e4SLinus Torvalds dst_release(&grt->u.dst); 10451da177e4SLinus Torvalds 10461da177e4SLinus Torvalds if (err) 10471da177e4SLinus Torvalds goto out; 10481da177e4SLinus Torvalds } 10491da177e4SLinus Torvalds err = -EINVAL; 10501da177e4SLinus Torvalds if (dev == NULL || (dev->flags&IFF_LOOPBACK)) 10511da177e4SLinus Torvalds goto out; 10521da177e4SLinus Torvalds } 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds err = -ENODEV; 10551da177e4SLinus Torvalds if (dev == NULL) 10561da177e4SLinus Torvalds goto out; 10571da177e4SLinus Torvalds 10581da177e4SLinus Torvalds if (rtmsg->rtmsg_flags & (RTF_GATEWAY|RTF_NONEXTHOP)) { 10591da177e4SLinus Torvalds rt->rt6i_nexthop = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev); 10601da177e4SLinus Torvalds if (IS_ERR(rt->rt6i_nexthop)) { 10611da177e4SLinus Torvalds err = PTR_ERR(rt->rt6i_nexthop); 10621da177e4SLinus Torvalds rt->rt6i_nexthop = NULL; 10631da177e4SLinus Torvalds goto out; 10641da177e4SLinus Torvalds } 10651da177e4SLinus Torvalds } 10661da177e4SLinus Torvalds 10671da177e4SLinus Torvalds rt->rt6i_flags = rtmsg->rtmsg_flags; 10681da177e4SLinus Torvalds 10691da177e4SLinus Torvalds install_route: 10701da177e4SLinus Torvalds if (rta && rta[RTA_METRICS-1]) { 10711da177e4SLinus Torvalds int attrlen = RTA_PAYLOAD(rta[RTA_METRICS-1]); 10721da177e4SLinus Torvalds struct rtattr *attr = RTA_DATA(rta[RTA_METRICS-1]); 10731da177e4SLinus Torvalds 10741da177e4SLinus Torvalds while (RTA_OK(attr, attrlen)) { 10751da177e4SLinus Torvalds unsigned flavor = attr->rta_type; 10761da177e4SLinus Torvalds if (flavor) { 10771da177e4SLinus Torvalds if (flavor > RTAX_MAX) { 10781da177e4SLinus Torvalds err = -EINVAL; 10791da177e4SLinus Torvalds goto out; 10801da177e4SLinus Torvalds } 10811da177e4SLinus Torvalds rt->u.dst.metrics[flavor-1] = 10821da177e4SLinus Torvalds *(u32 *)RTA_DATA(attr); 10831da177e4SLinus Torvalds } 10841da177e4SLinus Torvalds attr = RTA_NEXT(attr, attrlen); 10851da177e4SLinus Torvalds } 10861da177e4SLinus Torvalds } 10871da177e4SLinus Torvalds 10881da177e4SLinus Torvalds if (rt->u.dst.metrics[RTAX_HOPLIMIT-1] == 0) 10891da177e4SLinus Torvalds rt->u.dst.metrics[RTAX_HOPLIMIT-1] = -1; 10901da177e4SLinus Torvalds if (!rt->u.dst.metrics[RTAX_MTU-1]) 10911da177e4SLinus Torvalds rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(dev); 10921da177e4SLinus Torvalds if (!rt->u.dst.metrics[RTAX_ADVMSS-1]) 10931da177e4SLinus Torvalds rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(dst_mtu(&rt->u.dst)); 10941da177e4SLinus Torvalds rt->u.dst.dev = dev; 10951da177e4SLinus Torvalds rt->rt6i_idev = idev; 10960d51aa80SJamal Hadi Salim return ip6_ins_rt(rt, nlh, _rtattr, req); 10971da177e4SLinus Torvalds 10981da177e4SLinus Torvalds out: 10991da177e4SLinus Torvalds if (dev) 11001da177e4SLinus Torvalds dev_put(dev); 11011da177e4SLinus Torvalds if (idev) 11021da177e4SLinus Torvalds in6_dev_put(idev); 11031da177e4SLinus Torvalds if (rt) 11041da177e4SLinus Torvalds dst_free((struct dst_entry *) rt); 11051da177e4SLinus Torvalds return err; 11061da177e4SLinus Torvalds } 11071da177e4SLinus Torvalds 11080d51aa80SJamal Hadi Salim int ip6_del_rt(struct rt6_info *rt, struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req) 11091da177e4SLinus Torvalds { 11101da177e4SLinus Torvalds int err; 11111da177e4SLinus Torvalds 11121da177e4SLinus Torvalds write_lock_bh(&rt6_lock); 11131da177e4SLinus Torvalds 11140d51aa80SJamal Hadi Salim err = fib6_del(rt, nlh, _rtattr, req); 11151da177e4SLinus Torvalds dst_release(&rt->u.dst); 11161da177e4SLinus Torvalds 11171da177e4SLinus Torvalds write_unlock_bh(&rt6_lock); 11181da177e4SLinus Torvalds 11191da177e4SLinus Torvalds return err; 11201da177e4SLinus Torvalds } 11211da177e4SLinus Torvalds 11220d51aa80SJamal Hadi Salim static int ip6_route_del(struct in6_rtmsg *rtmsg, struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req) 11231da177e4SLinus Torvalds { 11241da177e4SLinus Torvalds struct fib6_node *fn; 11251da177e4SLinus Torvalds struct rt6_info *rt; 11261da177e4SLinus Torvalds int err = -ESRCH; 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds read_lock_bh(&rt6_lock); 11291da177e4SLinus Torvalds 11301da177e4SLinus Torvalds fn = fib6_locate(&ip6_routing_table, 11311da177e4SLinus Torvalds &rtmsg->rtmsg_dst, rtmsg->rtmsg_dst_len, 11321da177e4SLinus Torvalds &rtmsg->rtmsg_src, rtmsg->rtmsg_src_len); 11331da177e4SLinus Torvalds 11341da177e4SLinus Torvalds if (fn) { 11351da177e4SLinus Torvalds for (rt = fn->leaf; rt; rt = rt->u.next) { 11361da177e4SLinus Torvalds if (rtmsg->rtmsg_ifindex && 11371da177e4SLinus Torvalds (rt->rt6i_dev == NULL || 11381da177e4SLinus Torvalds rt->rt6i_dev->ifindex != rtmsg->rtmsg_ifindex)) 11391da177e4SLinus Torvalds continue; 11401da177e4SLinus Torvalds if (rtmsg->rtmsg_flags&RTF_GATEWAY && 11411da177e4SLinus Torvalds !ipv6_addr_equal(&rtmsg->rtmsg_gateway, &rt->rt6i_gateway)) 11421da177e4SLinus Torvalds continue; 11431da177e4SLinus Torvalds if (rtmsg->rtmsg_metric && 11441da177e4SLinus Torvalds rtmsg->rtmsg_metric != rt->rt6i_metric) 11451da177e4SLinus Torvalds continue; 11461da177e4SLinus Torvalds dst_hold(&rt->u.dst); 11471da177e4SLinus Torvalds read_unlock_bh(&rt6_lock); 11481da177e4SLinus Torvalds 11490d51aa80SJamal Hadi Salim return ip6_del_rt(rt, nlh, _rtattr, req); 11501da177e4SLinus Torvalds } 11511da177e4SLinus Torvalds } 11521da177e4SLinus Torvalds read_unlock_bh(&rt6_lock); 11531da177e4SLinus Torvalds 11541da177e4SLinus Torvalds return err; 11551da177e4SLinus Torvalds } 11561da177e4SLinus Torvalds 11571da177e4SLinus Torvalds /* 11581da177e4SLinus Torvalds * Handle redirects 11591da177e4SLinus Torvalds */ 11601da177e4SLinus Torvalds void rt6_redirect(struct in6_addr *dest, struct in6_addr *saddr, 11611da177e4SLinus Torvalds struct neighbour *neigh, u8 *lladdr, int on_link) 11621da177e4SLinus Torvalds { 1163e843b9e1SYOSHIFUJI Hideaki struct rt6_info *rt, *nrt = NULL; 1164e843b9e1SYOSHIFUJI Hideaki int strict; 1165e843b9e1SYOSHIFUJI Hideaki struct fib6_node *fn; 11668d71740cSTom Tucker struct netevent_redirect netevent; 11671da177e4SLinus Torvalds 1168e843b9e1SYOSHIFUJI Hideaki /* 1169e843b9e1SYOSHIFUJI Hideaki * Get the "current" route for this destination and 1170e843b9e1SYOSHIFUJI Hideaki * check if the redirect has come from approriate router. 1171e843b9e1SYOSHIFUJI Hideaki * 1172e843b9e1SYOSHIFUJI Hideaki * RFC 2461 specifies that redirects should only be 1173e843b9e1SYOSHIFUJI Hideaki * accepted if they come from the nexthop to the target. 1174e843b9e1SYOSHIFUJI Hideaki * Due to the way the routes are chosen, this notion 1175e843b9e1SYOSHIFUJI Hideaki * is a bit fuzzy and one might need to check all possible 1176e843b9e1SYOSHIFUJI Hideaki * routes. 1177e843b9e1SYOSHIFUJI Hideaki */ 1178e843b9e1SYOSHIFUJI Hideaki strict = ipv6_addr_type(dest) & (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL); 11791da177e4SLinus Torvalds 1180e843b9e1SYOSHIFUJI Hideaki read_lock_bh(&rt6_lock); 1181e843b9e1SYOSHIFUJI Hideaki fn = fib6_lookup(&ip6_routing_table, dest, NULL); 1182e843b9e1SYOSHIFUJI Hideaki restart: 1183e843b9e1SYOSHIFUJI Hideaki for (rt = fn->leaf; rt; rt = rt->u.next) { 11841da177e4SLinus Torvalds /* 11851da177e4SLinus Torvalds * Current route is on-link; redirect is always invalid. 11861da177e4SLinus Torvalds * 11871da177e4SLinus Torvalds * Seems, previous statement is not true. It could 11881da177e4SLinus Torvalds * be node, which looks for us as on-link (f.e. proxy ndisc) 11891da177e4SLinus Torvalds * But then router serving it might decide, that we should 11901da177e4SLinus Torvalds * know truth 8)8) --ANK (980726). 11911da177e4SLinus Torvalds */ 1192e843b9e1SYOSHIFUJI Hideaki if (rt6_check_expired(rt)) 1193e843b9e1SYOSHIFUJI Hideaki continue; 11941da177e4SLinus Torvalds if (!(rt->rt6i_flags & RTF_GATEWAY)) 1195e843b9e1SYOSHIFUJI Hideaki continue; 1196e843b9e1SYOSHIFUJI Hideaki if (neigh->dev != rt->rt6i_dev) 1197e843b9e1SYOSHIFUJI Hideaki continue; 1198e843b9e1SYOSHIFUJI Hideaki if (!ipv6_addr_equal(saddr, &rt->rt6i_gateway)) 1199e843b9e1SYOSHIFUJI Hideaki continue; 1200e843b9e1SYOSHIFUJI Hideaki break; 1201e843b9e1SYOSHIFUJI Hideaki } 1202e843b9e1SYOSHIFUJI Hideaki if (rt) 1203e843b9e1SYOSHIFUJI Hideaki dst_hold(&rt->u.dst); 1204e843b9e1SYOSHIFUJI Hideaki else if (strict) { 1205e843b9e1SYOSHIFUJI Hideaki while ((fn = fn->parent) != NULL) { 1206e843b9e1SYOSHIFUJI Hideaki if (fn->fn_flags & RTN_ROOT) 1207e843b9e1SYOSHIFUJI Hideaki break; 1208e843b9e1SYOSHIFUJI Hideaki if (fn->fn_flags & RTN_RTINFO) 1209e843b9e1SYOSHIFUJI Hideaki goto restart; 1210e843b9e1SYOSHIFUJI Hideaki } 1211e843b9e1SYOSHIFUJI Hideaki } 1212e843b9e1SYOSHIFUJI Hideaki read_unlock_bh(&rt6_lock); 12131da177e4SLinus Torvalds 1214e843b9e1SYOSHIFUJI Hideaki if (!rt) { 12151da177e4SLinus Torvalds if (net_ratelimit()) 12161da177e4SLinus Torvalds printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop " 12171da177e4SLinus Torvalds "for redirect target\n"); 1218e843b9e1SYOSHIFUJI Hideaki return; 12191da177e4SLinus Torvalds } 12201da177e4SLinus Torvalds 12211da177e4SLinus Torvalds /* 12221da177e4SLinus Torvalds * We have finally decided to accept it. 12231da177e4SLinus Torvalds */ 12241da177e4SLinus Torvalds 12251da177e4SLinus Torvalds neigh_update(neigh, lladdr, NUD_STALE, 12261da177e4SLinus Torvalds NEIGH_UPDATE_F_WEAK_OVERRIDE| 12271da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE| 12281da177e4SLinus Torvalds (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| 12291da177e4SLinus Torvalds NEIGH_UPDATE_F_ISROUTER)) 12301da177e4SLinus Torvalds ); 12311da177e4SLinus Torvalds 12321da177e4SLinus Torvalds /* 12331da177e4SLinus Torvalds * Redirect received -> path was valid. 12341da177e4SLinus Torvalds * Look, redirects are sent only in response to data packets, 12351da177e4SLinus Torvalds * so that this nexthop apparently is reachable. --ANK 12361da177e4SLinus Torvalds */ 12371da177e4SLinus Torvalds dst_confirm(&rt->u.dst); 12381da177e4SLinus Torvalds 12391da177e4SLinus Torvalds /* Duplicate redirect: silently ignore. */ 12401da177e4SLinus Torvalds if (neigh == rt->u.dst.neighbour) 12411da177e4SLinus Torvalds goto out; 12421da177e4SLinus Torvalds 12431da177e4SLinus Torvalds nrt = ip6_rt_copy(rt); 12441da177e4SLinus Torvalds if (nrt == NULL) 12451da177e4SLinus Torvalds goto out; 12461da177e4SLinus Torvalds 12471da177e4SLinus Torvalds nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; 12481da177e4SLinus Torvalds if (on_link) 12491da177e4SLinus Torvalds nrt->rt6i_flags &= ~RTF_GATEWAY; 12501da177e4SLinus Torvalds 12511da177e4SLinus Torvalds ipv6_addr_copy(&nrt->rt6i_dst.addr, dest); 12521da177e4SLinus Torvalds nrt->rt6i_dst.plen = 128; 12531da177e4SLinus Torvalds nrt->u.dst.flags |= DST_HOST; 12541da177e4SLinus Torvalds 12551da177e4SLinus Torvalds ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key); 12561da177e4SLinus Torvalds nrt->rt6i_nexthop = neigh_clone(neigh); 12571da177e4SLinus Torvalds /* Reset pmtu, it may be better */ 12581da177e4SLinus Torvalds nrt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(neigh->dev); 12591da177e4SLinus Torvalds nrt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(dst_mtu(&nrt->u.dst)); 12601da177e4SLinus Torvalds 12610d51aa80SJamal Hadi Salim if (ip6_ins_rt(nrt, NULL, NULL, NULL)) 12621da177e4SLinus Torvalds goto out; 12631da177e4SLinus Torvalds 12648d71740cSTom Tucker netevent.old = &rt->u.dst; 12658d71740cSTom Tucker netevent.new = &nrt->u.dst; 12668d71740cSTom Tucker call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); 12678d71740cSTom Tucker 12681da177e4SLinus Torvalds if (rt->rt6i_flags&RTF_CACHE) { 12690d51aa80SJamal Hadi Salim ip6_del_rt(rt, NULL, NULL, NULL); 12701da177e4SLinus Torvalds return; 12711da177e4SLinus Torvalds } 12721da177e4SLinus Torvalds 12731da177e4SLinus Torvalds out: 12741da177e4SLinus Torvalds dst_release(&rt->u.dst); 12751da177e4SLinus Torvalds return; 12761da177e4SLinus Torvalds } 12771da177e4SLinus Torvalds 12781da177e4SLinus Torvalds /* 12791da177e4SLinus Torvalds * Handle ICMP "packet too big" messages 12801da177e4SLinus Torvalds * i.e. Path MTU discovery 12811da177e4SLinus Torvalds */ 12821da177e4SLinus Torvalds 12831da177e4SLinus Torvalds void rt6_pmtu_discovery(struct in6_addr *daddr, struct in6_addr *saddr, 12841da177e4SLinus Torvalds struct net_device *dev, u32 pmtu) 12851da177e4SLinus Torvalds { 12861da177e4SLinus Torvalds struct rt6_info *rt, *nrt; 12871da177e4SLinus Torvalds int allfrag = 0; 12881da177e4SLinus Torvalds 12891da177e4SLinus Torvalds rt = rt6_lookup(daddr, saddr, dev->ifindex, 0); 12901da177e4SLinus Torvalds if (rt == NULL) 12911da177e4SLinus Torvalds return; 12921da177e4SLinus Torvalds 12931da177e4SLinus Torvalds if (pmtu >= dst_mtu(&rt->u.dst)) 12941da177e4SLinus Torvalds goto out; 12951da177e4SLinus Torvalds 12961da177e4SLinus Torvalds if (pmtu < IPV6_MIN_MTU) { 12971da177e4SLinus Torvalds /* 12981da177e4SLinus Torvalds * According to RFC2460, PMTU is set to the IPv6 Minimum Link 12991da177e4SLinus Torvalds * MTU (1280) and a fragment header should always be included 13001da177e4SLinus Torvalds * after a node receiving Too Big message reporting PMTU is 13011da177e4SLinus Torvalds * less than the IPv6 Minimum Link MTU. 13021da177e4SLinus Torvalds */ 13031da177e4SLinus Torvalds pmtu = IPV6_MIN_MTU; 13041da177e4SLinus Torvalds allfrag = 1; 13051da177e4SLinus Torvalds } 13061da177e4SLinus Torvalds 13071da177e4SLinus Torvalds /* New mtu received -> path was valid. 13081da177e4SLinus Torvalds They are sent only in response to data packets, 13091da177e4SLinus Torvalds so that this nexthop apparently is reachable. --ANK 13101da177e4SLinus Torvalds */ 13111da177e4SLinus Torvalds dst_confirm(&rt->u.dst); 13121da177e4SLinus Torvalds 13131da177e4SLinus Torvalds /* Host route. If it is static, it would be better 13141da177e4SLinus Torvalds not to override it, but add new one, so that 13151da177e4SLinus Torvalds when cache entry will expire old pmtu 13161da177e4SLinus Torvalds would return automatically. 13171da177e4SLinus Torvalds */ 13181da177e4SLinus Torvalds if (rt->rt6i_flags & RTF_CACHE) { 13191da177e4SLinus Torvalds rt->u.dst.metrics[RTAX_MTU-1] = pmtu; 13201da177e4SLinus Torvalds if (allfrag) 13211da177e4SLinus Torvalds rt->u.dst.metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG; 13221da177e4SLinus Torvalds dst_set_expires(&rt->u.dst, ip6_rt_mtu_expires); 13231da177e4SLinus Torvalds rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES; 13241da177e4SLinus Torvalds goto out; 13251da177e4SLinus Torvalds } 13261da177e4SLinus Torvalds 13271da177e4SLinus Torvalds /* Network route. 13281da177e4SLinus Torvalds Two cases are possible: 13291da177e4SLinus Torvalds 1. It is connected route. Action: COW 13301da177e4SLinus Torvalds 2. It is gatewayed route or NONEXTHOP route. Action: clone it. 13311da177e4SLinus Torvalds */ 1332d5315b50SYOSHIFUJI Hideaki if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP)) 1333a1e78363SYOSHIFUJI Hideaki nrt = rt6_alloc_cow(rt, daddr, saddr); 1334d5315b50SYOSHIFUJI Hideaki else 1335d5315b50SYOSHIFUJI Hideaki nrt = rt6_alloc_clone(rt, daddr); 1336a1e78363SYOSHIFUJI Hideaki 1337d5315b50SYOSHIFUJI Hideaki if (nrt) { 13381da177e4SLinus Torvalds nrt->u.dst.metrics[RTAX_MTU-1] = pmtu; 13391da177e4SLinus Torvalds if (allfrag) 13401da177e4SLinus Torvalds nrt->u.dst.metrics[RTAX_FEATURES-1] |= RTAX_FEATURE_ALLFRAG; 1341a1e78363SYOSHIFUJI Hideaki 13421da177e4SLinus Torvalds /* According to RFC 1981, detecting PMTU increase shouldn't be 1343a1e78363SYOSHIFUJI Hideaki * happened within 5 mins, the recommended timer is 10 mins. 1344a1e78363SYOSHIFUJI Hideaki * Here this route expiration time is set to ip6_rt_mtu_expires 1345a1e78363SYOSHIFUJI Hideaki * which is 10 mins. After 10 mins the decreased pmtu is expired 1346a1e78363SYOSHIFUJI Hideaki * and detecting PMTU increase will be automatically happened. 13471da177e4SLinus Torvalds */ 13481da177e4SLinus Torvalds dst_set_expires(&nrt->u.dst, ip6_rt_mtu_expires); 13491da177e4SLinus Torvalds nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES; 1350a1e78363SYOSHIFUJI Hideaki 1351a1e78363SYOSHIFUJI Hideaki ip6_ins_rt(nrt, NULL, NULL, NULL); 13521da177e4SLinus Torvalds } 13531da177e4SLinus Torvalds out: 13541da177e4SLinus Torvalds dst_release(&rt->u.dst); 13551da177e4SLinus Torvalds } 13561da177e4SLinus Torvalds 13571da177e4SLinus Torvalds /* 13581da177e4SLinus Torvalds * Misc support functions 13591da177e4SLinus Torvalds */ 13601da177e4SLinus Torvalds 13611da177e4SLinus Torvalds static struct rt6_info * ip6_rt_copy(struct rt6_info *ort) 13621da177e4SLinus Torvalds { 13631da177e4SLinus Torvalds struct rt6_info *rt = ip6_dst_alloc(); 13641da177e4SLinus Torvalds 13651da177e4SLinus Torvalds if (rt) { 13661da177e4SLinus Torvalds rt->u.dst.input = ort->u.dst.input; 13671da177e4SLinus Torvalds rt->u.dst.output = ort->u.dst.output; 13681da177e4SLinus Torvalds 13691da177e4SLinus Torvalds memcpy(rt->u.dst.metrics, ort->u.dst.metrics, RTAX_MAX*sizeof(u32)); 13701da177e4SLinus Torvalds rt->u.dst.dev = ort->u.dst.dev; 13711da177e4SLinus Torvalds if (rt->u.dst.dev) 13721da177e4SLinus Torvalds dev_hold(rt->u.dst.dev); 13731da177e4SLinus Torvalds rt->rt6i_idev = ort->rt6i_idev; 13741da177e4SLinus Torvalds if (rt->rt6i_idev) 13751da177e4SLinus Torvalds in6_dev_hold(rt->rt6i_idev); 13761da177e4SLinus Torvalds rt->u.dst.lastuse = jiffies; 13771da177e4SLinus Torvalds rt->rt6i_expires = 0; 13781da177e4SLinus Torvalds 13791da177e4SLinus Torvalds ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway); 13801da177e4SLinus Torvalds rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES; 13811da177e4SLinus Torvalds rt->rt6i_metric = 0; 13821da177e4SLinus Torvalds 13831da177e4SLinus Torvalds memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); 13841da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 13851da177e4SLinus Torvalds memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); 13861da177e4SLinus Torvalds #endif 13871da177e4SLinus Torvalds } 13881da177e4SLinus Torvalds return rt; 13891da177e4SLinus Torvalds } 13901da177e4SLinus Torvalds 139170ceb4f5SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 139270ceb4f5SYOSHIFUJI Hideaki static struct rt6_info *rt6_get_route_info(struct in6_addr *prefix, int prefixlen, 139370ceb4f5SYOSHIFUJI Hideaki struct in6_addr *gwaddr, int ifindex) 139470ceb4f5SYOSHIFUJI Hideaki { 139570ceb4f5SYOSHIFUJI Hideaki struct fib6_node *fn; 139670ceb4f5SYOSHIFUJI Hideaki struct rt6_info *rt = NULL; 139770ceb4f5SYOSHIFUJI Hideaki 139870ceb4f5SYOSHIFUJI Hideaki write_lock_bh(&rt6_lock); 139970ceb4f5SYOSHIFUJI Hideaki fn = fib6_locate(&ip6_routing_table, prefix ,prefixlen, NULL, 0); 140070ceb4f5SYOSHIFUJI Hideaki if (!fn) 140170ceb4f5SYOSHIFUJI Hideaki goto out; 140270ceb4f5SYOSHIFUJI Hideaki 140370ceb4f5SYOSHIFUJI Hideaki for (rt = fn->leaf; rt; rt = rt->u.next) { 140470ceb4f5SYOSHIFUJI Hideaki if (rt->rt6i_dev->ifindex != ifindex) 140570ceb4f5SYOSHIFUJI Hideaki continue; 140670ceb4f5SYOSHIFUJI Hideaki if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) 140770ceb4f5SYOSHIFUJI Hideaki continue; 140870ceb4f5SYOSHIFUJI Hideaki if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr)) 140970ceb4f5SYOSHIFUJI Hideaki continue; 141070ceb4f5SYOSHIFUJI Hideaki dst_hold(&rt->u.dst); 141170ceb4f5SYOSHIFUJI Hideaki break; 141270ceb4f5SYOSHIFUJI Hideaki } 141370ceb4f5SYOSHIFUJI Hideaki out: 141470ceb4f5SYOSHIFUJI Hideaki write_unlock_bh(&rt6_lock); 141570ceb4f5SYOSHIFUJI Hideaki return rt; 141670ceb4f5SYOSHIFUJI Hideaki } 141770ceb4f5SYOSHIFUJI Hideaki 141870ceb4f5SYOSHIFUJI Hideaki static struct rt6_info *rt6_add_route_info(struct in6_addr *prefix, int prefixlen, 141970ceb4f5SYOSHIFUJI Hideaki struct in6_addr *gwaddr, int ifindex, 142070ceb4f5SYOSHIFUJI Hideaki unsigned pref) 142170ceb4f5SYOSHIFUJI Hideaki { 142270ceb4f5SYOSHIFUJI Hideaki struct in6_rtmsg rtmsg; 142370ceb4f5SYOSHIFUJI Hideaki 142470ceb4f5SYOSHIFUJI Hideaki memset(&rtmsg, 0, sizeof(rtmsg)); 142570ceb4f5SYOSHIFUJI Hideaki rtmsg.rtmsg_type = RTMSG_NEWROUTE; 142670ceb4f5SYOSHIFUJI Hideaki ipv6_addr_copy(&rtmsg.rtmsg_dst, prefix); 142770ceb4f5SYOSHIFUJI Hideaki rtmsg.rtmsg_dst_len = prefixlen; 142870ceb4f5SYOSHIFUJI Hideaki ipv6_addr_copy(&rtmsg.rtmsg_gateway, gwaddr); 142970ceb4f5SYOSHIFUJI Hideaki rtmsg.rtmsg_metric = 1024; 143070ceb4f5SYOSHIFUJI Hideaki rtmsg.rtmsg_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | RTF_UP | RTF_PREF(pref); 1431e317da96SYOSHIFUJI Hideaki /* We should treat it as a default route if prefix length is 0. */ 1432e317da96SYOSHIFUJI Hideaki if (!prefixlen) 1433e317da96SYOSHIFUJI Hideaki rtmsg.rtmsg_flags |= RTF_DEFAULT; 143470ceb4f5SYOSHIFUJI Hideaki rtmsg.rtmsg_ifindex = ifindex; 143570ceb4f5SYOSHIFUJI Hideaki 143670ceb4f5SYOSHIFUJI Hideaki ip6_route_add(&rtmsg, NULL, NULL, NULL); 143770ceb4f5SYOSHIFUJI Hideaki 143870ceb4f5SYOSHIFUJI Hideaki return rt6_get_route_info(prefix, prefixlen, gwaddr, ifindex); 143970ceb4f5SYOSHIFUJI Hideaki } 144070ceb4f5SYOSHIFUJI Hideaki #endif 144170ceb4f5SYOSHIFUJI Hideaki 14421da177e4SLinus Torvalds struct rt6_info *rt6_get_dflt_router(struct in6_addr *addr, struct net_device *dev) 14431da177e4SLinus Torvalds { 14441da177e4SLinus Torvalds struct rt6_info *rt; 14451da177e4SLinus Torvalds struct fib6_node *fn; 14461da177e4SLinus Torvalds 14471da177e4SLinus Torvalds fn = &ip6_routing_table; 14481da177e4SLinus Torvalds 14491da177e4SLinus Torvalds write_lock_bh(&rt6_lock); 14501da177e4SLinus Torvalds for (rt = fn->leaf; rt; rt=rt->u.next) { 14511da177e4SLinus Torvalds if (dev == rt->rt6i_dev && 1452045927ffSYOSHIFUJI Hideaki ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && 14531da177e4SLinus Torvalds ipv6_addr_equal(&rt->rt6i_gateway, addr)) 14541da177e4SLinus Torvalds break; 14551da177e4SLinus Torvalds } 14561da177e4SLinus Torvalds if (rt) 14571da177e4SLinus Torvalds dst_hold(&rt->u.dst); 14581da177e4SLinus Torvalds write_unlock_bh(&rt6_lock); 14591da177e4SLinus Torvalds return rt; 14601da177e4SLinus Torvalds } 14611da177e4SLinus Torvalds 14621da177e4SLinus Torvalds struct rt6_info *rt6_add_dflt_router(struct in6_addr *gwaddr, 1463ebacaaa0SYOSHIFUJI Hideaki struct net_device *dev, 1464ebacaaa0SYOSHIFUJI Hideaki unsigned int pref) 14651da177e4SLinus Torvalds { 14661da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 14671da177e4SLinus Torvalds 14681da177e4SLinus Torvalds memset(&rtmsg, 0, sizeof(struct in6_rtmsg)); 14691da177e4SLinus Torvalds rtmsg.rtmsg_type = RTMSG_NEWROUTE; 14701da177e4SLinus Torvalds ipv6_addr_copy(&rtmsg.rtmsg_gateway, gwaddr); 14711da177e4SLinus Torvalds rtmsg.rtmsg_metric = 1024; 1472ebacaaa0SYOSHIFUJI Hideaki rtmsg.rtmsg_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | RTF_UP | RTF_EXPIRES | 1473ebacaaa0SYOSHIFUJI Hideaki RTF_PREF(pref); 14741da177e4SLinus Torvalds 14751da177e4SLinus Torvalds rtmsg.rtmsg_ifindex = dev->ifindex; 14761da177e4SLinus Torvalds 14770d51aa80SJamal Hadi Salim ip6_route_add(&rtmsg, NULL, NULL, NULL); 14781da177e4SLinus Torvalds return rt6_get_dflt_router(gwaddr, dev); 14791da177e4SLinus Torvalds } 14801da177e4SLinus Torvalds 14811da177e4SLinus Torvalds void rt6_purge_dflt_routers(void) 14821da177e4SLinus Torvalds { 14831da177e4SLinus Torvalds struct rt6_info *rt; 14841da177e4SLinus Torvalds 14851da177e4SLinus Torvalds restart: 14861da177e4SLinus Torvalds read_lock_bh(&rt6_lock); 14871da177e4SLinus Torvalds for (rt = ip6_routing_table.leaf; rt; rt = rt->u.next) { 14881da177e4SLinus Torvalds if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) { 14891da177e4SLinus Torvalds dst_hold(&rt->u.dst); 14901da177e4SLinus Torvalds 14911da177e4SLinus Torvalds read_unlock_bh(&rt6_lock); 14921da177e4SLinus Torvalds 14930d51aa80SJamal Hadi Salim ip6_del_rt(rt, NULL, NULL, NULL); 14941da177e4SLinus Torvalds 14951da177e4SLinus Torvalds goto restart; 14961da177e4SLinus Torvalds } 14971da177e4SLinus Torvalds } 14981da177e4SLinus Torvalds read_unlock_bh(&rt6_lock); 14991da177e4SLinus Torvalds } 15001da177e4SLinus Torvalds 15011da177e4SLinus Torvalds int ipv6_route_ioctl(unsigned int cmd, void __user *arg) 15021da177e4SLinus Torvalds { 15031da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 15041da177e4SLinus Torvalds int err; 15051da177e4SLinus Torvalds 15061da177e4SLinus Torvalds switch(cmd) { 15071da177e4SLinus Torvalds case SIOCADDRT: /* Add a route */ 15081da177e4SLinus Torvalds case SIOCDELRT: /* Delete a route */ 15091da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 15101da177e4SLinus Torvalds return -EPERM; 15111da177e4SLinus Torvalds err = copy_from_user(&rtmsg, arg, 15121da177e4SLinus Torvalds sizeof(struct in6_rtmsg)); 15131da177e4SLinus Torvalds if (err) 15141da177e4SLinus Torvalds return -EFAULT; 15151da177e4SLinus Torvalds 15161da177e4SLinus Torvalds rtnl_lock(); 15171da177e4SLinus Torvalds switch (cmd) { 15181da177e4SLinus Torvalds case SIOCADDRT: 15190d51aa80SJamal Hadi Salim err = ip6_route_add(&rtmsg, NULL, NULL, NULL); 15201da177e4SLinus Torvalds break; 15211da177e4SLinus Torvalds case SIOCDELRT: 15220d51aa80SJamal Hadi Salim err = ip6_route_del(&rtmsg, NULL, NULL, NULL); 15231da177e4SLinus Torvalds break; 15241da177e4SLinus Torvalds default: 15251da177e4SLinus Torvalds err = -EINVAL; 15261da177e4SLinus Torvalds } 15271da177e4SLinus Torvalds rtnl_unlock(); 15281da177e4SLinus Torvalds 15291da177e4SLinus Torvalds return err; 15301da177e4SLinus Torvalds }; 15311da177e4SLinus Torvalds 15321da177e4SLinus Torvalds return -EINVAL; 15331da177e4SLinus Torvalds } 15341da177e4SLinus Torvalds 15351da177e4SLinus Torvalds /* 15361da177e4SLinus Torvalds * Drop the packet on the floor 15371da177e4SLinus Torvalds */ 15381da177e4SLinus Torvalds 153920380731SArnaldo Carvalho de Melo static int ip6_pkt_discard(struct sk_buff *skb) 15401da177e4SLinus Torvalds { 154176d0cc1bSLv Liangying int type = ipv6_addr_type(&skb->nh.ipv6h->daddr); 154276d0cc1bSLv Liangying if (type == IPV6_ADDR_ANY || type == IPV6_ADDR_RESERVED) 154376d0cc1bSLv Liangying IP6_INC_STATS(IPSTATS_MIB_INADDRERRORS); 154476d0cc1bSLv Liangying 15451da177e4SLinus Torvalds IP6_INC_STATS(IPSTATS_MIB_OUTNOROUTES); 15461da177e4SLinus Torvalds icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_NOROUTE, 0, skb->dev); 15471da177e4SLinus Torvalds kfree_skb(skb); 15481da177e4SLinus Torvalds return 0; 15491da177e4SLinus Torvalds } 15501da177e4SLinus Torvalds 155120380731SArnaldo Carvalho de Melo static int ip6_pkt_discard_out(struct sk_buff *skb) 15521da177e4SLinus Torvalds { 15531da177e4SLinus Torvalds skb->dev = skb->dst->dev; 15541da177e4SLinus Torvalds return ip6_pkt_discard(skb); 15551da177e4SLinus Torvalds } 15561da177e4SLinus Torvalds 15571da177e4SLinus Torvalds /* 15581da177e4SLinus Torvalds * Allocate a dst for local (unicast / anycast) address. 15591da177e4SLinus Torvalds */ 15601da177e4SLinus Torvalds 15611da177e4SLinus Torvalds struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, 15621da177e4SLinus Torvalds const struct in6_addr *addr, 15631da177e4SLinus Torvalds int anycast) 15641da177e4SLinus Torvalds { 15651da177e4SLinus Torvalds struct rt6_info *rt = ip6_dst_alloc(); 15661da177e4SLinus Torvalds 15671da177e4SLinus Torvalds if (rt == NULL) 15681da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 15691da177e4SLinus Torvalds 15701da177e4SLinus Torvalds dev_hold(&loopback_dev); 15711da177e4SLinus Torvalds in6_dev_hold(idev); 15721da177e4SLinus Torvalds 15731da177e4SLinus Torvalds rt->u.dst.flags = DST_HOST; 15741da177e4SLinus Torvalds rt->u.dst.input = ip6_input; 15751da177e4SLinus Torvalds rt->u.dst.output = ip6_output; 15761da177e4SLinus Torvalds rt->rt6i_dev = &loopback_dev; 15771da177e4SLinus Torvalds rt->rt6i_idev = idev; 15781da177e4SLinus Torvalds rt->u.dst.metrics[RTAX_MTU-1] = ipv6_get_mtu(rt->rt6i_dev); 15791da177e4SLinus Torvalds rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(dst_mtu(&rt->u.dst)); 15801da177e4SLinus Torvalds rt->u.dst.metrics[RTAX_HOPLIMIT-1] = -1; 15811da177e4SLinus Torvalds rt->u.dst.obsolete = -1; 15821da177e4SLinus Torvalds 15831da177e4SLinus Torvalds rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP; 158458c4fb86SYOSHIFUJI Hideaki if (anycast) 158558c4fb86SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_ANYCAST; 158658c4fb86SYOSHIFUJI Hideaki else 15871da177e4SLinus Torvalds rt->rt6i_flags |= RTF_LOCAL; 15881da177e4SLinus Torvalds rt->rt6i_nexthop = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway); 15891da177e4SLinus Torvalds if (rt->rt6i_nexthop == NULL) { 15901da177e4SLinus Torvalds dst_free((struct dst_entry *) rt); 15911da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 15921da177e4SLinus Torvalds } 15931da177e4SLinus Torvalds 15941da177e4SLinus Torvalds ipv6_addr_copy(&rt->rt6i_dst.addr, addr); 15951da177e4SLinus Torvalds rt->rt6i_dst.plen = 128; 15961da177e4SLinus Torvalds 15971da177e4SLinus Torvalds atomic_set(&rt->u.dst.__refcnt, 1); 15981da177e4SLinus Torvalds 15991da177e4SLinus Torvalds return rt; 16001da177e4SLinus Torvalds } 16011da177e4SLinus Torvalds 16021da177e4SLinus Torvalds static int fib6_ifdown(struct rt6_info *rt, void *arg) 16031da177e4SLinus Torvalds { 16041da177e4SLinus Torvalds if (((void*)rt->rt6i_dev == arg || arg == NULL) && 16051da177e4SLinus Torvalds rt != &ip6_null_entry) { 16061da177e4SLinus Torvalds RT6_TRACE("deleted by ifdown %p\n", rt); 16071da177e4SLinus Torvalds return -1; 16081da177e4SLinus Torvalds } 16091da177e4SLinus Torvalds return 0; 16101da177e4SLinus Torvalds } 16111da177e4SLinus Torvalds 16121da177e4SLinus Torvalds void rt6_ifdown(struct net_device *dev) 16131da177e4SLinus Torvalds { 16141da177e4SLinus Torvalds write_lock_bh(&rt6_lock); 16151da177e4SLinus Torvalds fib6_clean_tree(&ip6_routing_table, fib6_ifdown, 0, dev); 16161da177e4SLinus Torvalds write_unlock_bh(&rt6_lock); 16171da177e4SLinus Torvalds } 16181da177e4SLinus Torvalds 16191da177e4SLinus Torvalds struct rt6_mtu_change_arg 16201da177e4SLinus Torvalds { 16211da177e4SLinus Torvalds struct net_device *dev; 16221da177e4SLinus Torvalds unsigned mtu; 16231da177e4SLinus Torvalds }; 16241da177e4SLinus Torvalds 16251da177e4SLinus Torvalds static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg) 16261da177e4SLinus Torvalds { 16271da177e4SLinus Torvalds struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; 16281da177e4SLinus Torvalds struct inet6_dev *idev; 16291da177e4SLinus Torvalds 16301da177e4SLinus Torvalds /* In IPv6 pmtu discovery is not optional, 16311da177e4SLinus Torvalds so that RTAX_MTU lock cannot disable it. 16321da177e4SLinus Torvalds We still use this lock to block changes 16331da177e4SLinus Torvalds caused by addrconf/ndisc. 16341da177e4SLinus Torvalds */ 16351da177e4SLinus Torvalds 16361da177e4SLinus Torvalds idev = __in6_dev_get(arg->dev); 16371da177e4SLinus Torvalds if (idev == NULL) 16381da177e4SLinus Torvalds return 0; 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds /* For administrative MTU increase, there is no way to discover 16411da177e4SLinus Torvalds IPv6 PMTU increase, so PMTU increase should be updated here. 16421da177e4SLinus Torvalds Since RFC 1981 doesn't include administrative MTU increase 16431da177e4SLinus Torvalds update PMTU increase is a MUST. (i.e. jumbo frame) 16441da177e4SLinus Torvalds */ 16451da177e4SLinus Torvalds /* 16461da177e4SLinus Torvalds If new MTU is less than route PMTU, this new MTU will be the 16471da177e4SLinus Torvalds lowest MTU in the path, update the route PMTU to reflect PMTU 16481da177e4SLinus Torvalds decreases; if new MTU is greater than route PMTU, and the 16491da177e4SLinus Torvalds old MTU is the lowest MTU in the path, update the route PMTU 16501da177e4SLinus Torvalds to reflect the increase. In this case if the other nodes' MTU 16511da177e4SLinus Torvalds also have the lowest MTU, TOO BIG MESSAGE will be lead to 16521da177e4SLinus Torvalds PMTU discouvery. 16531da177e4SLinus Torvalds */ 16541da177e4SLinus Torvalds if (rt->rt6i_dev == arg->dev && 16551da177e4SLinus Torvalds !dst_metric_locked(&rt->u.dst, RTAX_MTU) && 16561da177e4SLinus Torvalds (dst_mtu(&rt->u.dst) > arg->mtu || 16571da177e4SLinus Torvalds (dst_mtu(&rt->u.dst) < arg->mtu && 16581da177e4SLinus Torvalds dst_mtu(&rt->u.dst) == idev->cnf.mtu6))) 16591da177e4SLinus Torvalds rt->u.dst.metrics[RTAX_MTU-1] = arg->mtu; 16601da177e4SLinus Torvalds rt->u.dst.metrics[RTAX_ADVMSS-1] = ipv6_advmss(arg->mtu); 16611da177e4SLinus Torvalds return 0; 16621da177e4SLinus Torvalds } 16631da177e4SLinus Torvalds 16641da177e4SLinus Torvalds void rt6_mtu_change(struct net_device *dev, unsigned mtu) 16651da177e4SLinus Torvalds { 16661da177e4SLinus Torvalds struct rt6_mtu_change_arg arg; 16671da177e4SLinus Torvalds 16681da177e4SLinus Torvalds arg.dev = dev; 16691da177e4SLinus Torvalds arg.mtu = mtu; 16701da177e4SLinus Torvalds read_lock_bh(&rt6_lock); 16711da177e4SLinus Torvalds fib6_clean_tree(&ip6_routing_table, rt6_mtu_change_route, 0, &arg); 16721da177e4SLinus Torvalds read_unlock_bh(&rt6_lock); 16731da177e4SLinus Torvalds } 16741da177e4SLinus Torvalds 16751da177e4SLinus Torvalds static int inet6_rtm_to_rtmsg(struct rtmsg *r, struct rtattr **rta, 16761da177e4SLinus Torvalds struct in6_rtmsg *rtmsg) 16771da177e4SLinus Torvalds { 16781da177e4SLinus Torvalds memset(rtmsg, 0, sizeof(*rtmsg)); 16791da177e4SLinus Torvalds 16801da177e4SLinus Torvalds rtmsg->rtmsg_dst_len = r->rtm_dst_len; 16811da177e4SLinus Torvalds rtmsg->rtmsg_src_len = r->rtm_src_len; 16821da177e4SLinus Torvalds rtmsg->rtmsg_flags = RTF_UP; 16831da177e4SLinus Torvalds if (r->rtm_type == RTN_UNREACHABLE) 16841da177e4SLinus Torvalds rtmsg->rtmsg_flags |= RTF_REJECT; 16851da177e4SLinus Torvalds 16861da177e4SLinus Torvalds if (rta[RTA_GATEWAY-1]) { 16871da177e4SLinus Torvalds if (rta[RTA_GATEWAY-1]->rta_len != RTA_LENGTH(16)) 16881da177e4SLinus Torvalds return -EINVAL; 16891da177e4SLinus Torvalds memcpy(&rtmsg->rtmsg_gateway, RTA_DATA(rta[RTA_GATEWAY-1]), 16); 16901da177e4SLinus Torvalds rtmsg->rtmsg_flags |= RTF_GATEWAY; 16911da177e4SLinus Torvalds } 16921da177e4SLinus Torvalds if (rta[RTA_DST-1]) { 16931da177e4SLinus Torvalds if (RTA_PAYLOAD(rta[RTA_DST-1]) < ((r->rtm_dst_len+7)>>3)) 16941da177e4SLinus Torvalds return -EINVAL; 16951da177e4SLinus Torvalds memcpy(&rtmsg->rtmsg_dst, RTA_DATA(rta[RTA_DST-1]), ((r->rtm_dst_len+7)>>3)); 16961da177e4SLinus Torvalds } 16971da177e4SLinus Torvalds if (rta[RTA_SRC-1]) { 16981da177e4SLinus Torvalds if (RTA_PAYLOAD(rta[RTA_SRC-1]) < ((r->rtm_src_len+7)>>3)) 16991da177e4SLinus Torvalds return -EINVAL; 17001da177e4SLinus Torvalds memcpy(&rtmsg->rtmsg_src, RTA_DATA(rta[RTA_SRC-1]), ((r->rtm_src_len+7)>>3)); 17011da177e4SLinus Torvalds } 17021da177e4SLinus Torvalds if (rta[RTA_OIF-1]) { 17031da177e4SLinus Torvalds if (rta[RTA_OIF-1]->rta_len != RTA_LENGTH(sizeof(int))) 17041da177e4SLinus Torvalds return -EINVAL; 17051da177e4SLinus Torvalds memcpy(&rtmsg->rtmsg_ifindex, RTA_DATA(rta[RTA_OIF-1]), sizeof(int)); 17061da177e4SLinus Torvalds } 17071da177e4SLinus Torvalds if (rta[RTA_PRIORITY-1]) { 17081da177e4SLinus Torvalds if (rta[RTA_PRIORITY-1]->rta_len != RTA_LENGTH(4)) 17091da177e4SLinus Torvalds return -EINVAL; 17101da177e4SLinus Torvalds memcpy(&rtmsg->rtmsg_metric, RTA_DATA(rta[RTA_PRIORITY-1]), 4); 17111da177e4SLinus Torvalds } 17121da177e4SLinus Torvalds return 0; 17131da177e4SLinus Torvalds } 17141da177e4SLinus Torvalds 17151da177e4SLinus Torvalds int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 17161da177e4SLinus Torvalds { 17171da177e4SLinus Torvalds struct rtmsg *r = NLMSG_DATA(nlh); 17181da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 17191da177e4SLinus Torvalds 17201da177e4SLinus Torvalds if (inet6_rtm_to_rtmsg(r, arg, &rtmsg)) 17211da177e4SLinus Torvalds return -EINVAL; 17220d51aa80SJamal Hadi Salim return ip6_route_del(&rtmsg, nlh, arg, &NETLINK_CB(skb)); 17231da177e4SLinus Torvalds } 17241da177e4SLinus Torvalds 17251da177e4SLinus Torvalds int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 17261da177e4SLinus Torvalds { 17271da177e4SLinus Torvalds struct rtmsg *r = NLMSG_DATA(nlh); 17281da177e4SLinus Torvalds struct in6_rtmsg rtmsg; 17291da177e4SLinus Torvalds 17301da177e4SLinus Torvalds if (inet6_rtm_to_rtmsg(r, arg, &rtmsg)) 17311da177e4SLinus Torvalds return -EINVAL; 17320d51aa80SJamal Hadi Salim return ip6_route_add(&rtmsg, nlh, arg, &NETLINK_CB(skb)); 17331da177e4SLinus Torvalds } 17341da177e4SLinus Torvalds 17351da177e4SLinus Torvalds struct rt6_rtnl_dump_arg 17361da177e4SLinus Torvalds { 17371da177e4SLinus Torvalds struct sk_buff *skb; 17381da177e4SLinus Torvalds struct netlink_callback *cb; 17391da177e4SLinus Torvalds }; 17401da177e4SLinus Torvalds 17411da177e4SLinus Torvalds static int rt6_fill_node(struct sk_buff *skb, struct rt6_info *rt, 17420d51aa80SJamal Hadi Salim struct in6_addr *dst, struct in6_addr *src, 17430d51aa80SJamal Hadi Salim int iif, int type, u32 pid, u32 seq, 17440d51aa80SJamal Hadi Salim int prefix, unsigned int flags) 17451da177e4SLinus Torvalds { 17461da177e4SLinus Torvalds struct rtmsg *rtm; 17471da177e4SLinus Torvalds struct nlmsghdr *nlh; 17481da177e4SLinus Torvalds unsigned char *b = skb->tail; 17491da177e4SLinus Torvalds struct rta_cacheinfo ci; 17501da177e4SLinus Torvalds 17511da177e4SLinus Torvalds if (prefix) { /* user wants prefix routes only */ 17521da177e4SLinus Torvalds if (!(rt->rt6i_flags & RTF_PREFIX_RT)) { 17531da177e4SLinus Torvalds /* success since this is not a prefix route */ 17541da177e4SLinus Torvalds return 1; 17551da177e4SLinus Torvalds } 17561da177e4SLinus Torvalds } 17571da177e4SLinus Torvalds 1758b6544c0bSJamal Hadi Salim nlh = NLMSG_NEW(skb, pid, seq, type, sizeof(*rtm), flags); 17591da177e4SLinus Torvalds rtm = NLMSG_DATA(nlh); 17601da177e4SLinus Torvalds rtm->rtm_family = AF_INET6; 17611da177e4SLinus Torvalds rtm->rtm_dst_len = rt->rt6i_dst.plen; 17621da177e4SLinus Torvalds rtm->rtm_src_len = rt->rt6i_src.plen; 17631da177e4SLinus Torvalds rtm->rtm_tos = 0; 17641da177e4SLinus Torvalds rtm->rtm_table = RT_TABLE_MAIN; 17651da177e4SLinus Torvalds if (rt->rt6i_flags&RTF_REJECT) 17661da177e4SLinus Torvalds rtm->rtm_type = RTN_UNREACHABLE; 17671da177e4SLinus Torvalds else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK)) 17681da177e4SLinus Torvalds rtm->rtm_type = RTN_LOCAL; 17691da177e4SLinus Torvalds else 17701da177e4SLinus Torvalds rtm->rtm_type = RTN_UNICAST; 17711da177e4SLinus Torvalds rtm->rtm_flags = 0; 17721da177e4SLinus Torvalds rtm->rtm_scope = RT_SCOPE_UNIVERSE; 17731da177e4SLinus Torvalds rtm->rtm_protocol = rt->rt6i_protocol; 17741da177e4SLinus Torvalds if (rt->rt6i_flags&RTF_DYNAMIC) 17751da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_REDIRECT; 17761da177e4SLinus Torvalds else if (rt->rt6i_flags & RTF_ADDRCONF) 17771da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_KERNEL; 17781da177e4SLinus Torvalds else if (rt->rt6i_flags&RTF_DEFAULT) 17791da177e4SLinus Torvalds rtm->rtm_protocol = RTPROT_RA; 17801da177e4SLinus Torvalds 17811da177e4SLinus Torvalds if (rt->rt6i_flags&RTF_CACHE) 17821da177e4SLinus Torvalds rtm->rtm_flags |= RTM_F_CLONED; 17831da177e4SLinus Torvalds 17841da177e4SLinus Torvalds if (dst) { 17851da177e4SLinus Torvalds RTA_PUT(skb, RTA_DST, 16, dst); 17861da177e4SLinus Torvalds rtm->rtm_dst_len = 128; 17871da177e4SLinus Torvalds } else if (rtm->rtm_dst_len) 17881da177e4SLinus Torvalds RTA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr); 17891da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 17901da177e4SLinus Torvalds if (src) { 17911da177e4SLinus Torvalds RTA_PUT(skb, RTA_SRC, 16, src); 17921da177e4SLinus Torvalds rtm->rtm_src_len = 128; 17931da177e4SLinus Torvalds } else if (rtm->rtm_src_len) 17941da177e4SLinus Torvalds RTA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr); 17951da177e4SLinus Torvalds #endif 17961da177e4SLinus Torvalds if (iif) 17971da177e4SLinus Torvalds RTA_PUT(skb, RTA_IIF, 4, &iif); 17981da177e4SLinus Torvalds else if (dst) { 17991da177e4SLinus Torvalds struct in6_addr saddr_buf; 18001da177e4SLinus Torvalds if (ipv6_get_saddr(&rt->u.dst, dst, &saddr_buf) == 0) 18011da177e4SLinus Torvalds RTA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf); 18021da177e4SLinus Torvalds } 18031da177e4SLinus Torvalds if (rtnetlink_put_metrics(skb, rt->u.dst.metrics) < 0) 18041da177e4SLinus Torvalds goto rtattr_failure; 18051da177e4SLinus Torvalds if (rt->u.dst.neighbour) 18061da177e4SLinus Torvalds RTA_PUT(skb, RTA_GATEWAY, 16, &rt->u.dst.neighbour->primary_key); 18071da177e4SLinus Torvalds if (rt->u.dst.dev) 18081da177e4SLinus Torvalds RTA_PUT(skb, RTA_OIF, sizeof(int), &rt->rt6i_dev->ifindex); 18091da177e4SLinus Torvalds RTA_PUT(skb, RTA_PRIORITY, 4, &rt->rt6i_metric); 18101da177e4SLinus Torvalds ci.rta_lastuse = jiffies_to_clock_t(jiffies - rt->u.dst.lastuse); 18111da177e4SLinus Torvalds if (rt->rt6i_expires) 18121da177e4SLinus Torvalds ci.rta_expires = jiffies_to_clock_t(rt->rt6i_expires - jiffies); 18131da177e4SLinus Torvalds else 18141da177e4SLinus Torvalds ci.rta_expires = 0; 18151da177e4SLinus Torvalds ci.rta_used = rt->u.dst.__use; 18161da177e4SLinus Torvalds ci.rta_clntref = atomic_read(&rt->u.dst.__refcnt); 18171da177e4SLinus Torvalds ci.rta_error = rt->u.dst.error; 18181da177e4SLinus Torvalds ci.rta_id = 0; 18191da177e4SLinus Torvalds ci.rta_ts = 0; 18201da177e4SLinus Torvalds ci.rta_tsage = 0; 18211da177e4SLinus Torvalds RTA_PUT(skb, RTA_CACHEINFO, sizeof(ci), &ci); 18221da177e4SLinus Torvalds nlh->nlmsg_len = skb->tail - b; 18231da177e4SLinus Torvalds return skb->len; 18241da177e4SLinus Torvalds 18251da177e4SLinus Torvalds nlmsg_failure: 18261da177e4SLinus Torvalds rtattr_failure: 18271da177e4SLinus Torvalds skb_trim(skb, b - skb->data); 18281da177e4SLinus Torvalds return -1; 18291da177e4SLinus Torvalds } 18301da177e4SLinus Torvalds 18311da177e4SLinus Torvalds static int rt6_dump_route(struct rt6_info *rt, void *p_arg) 18321da177e4SLinus Torvalds { 18331da177e4SLinus Torvalds struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; 18341da177e4SLinus Torvalds int prefix; 18351da177e4SLinus Torvalds 18361da177e4SLinus Torvalds if (arg->cb->nlh->nlmsg_len >= NLMSG_LENGTH(sizeof(struct rtmsg))) { 18371da177e4SLinus Torvalds struct rtmsg *rtm = NLMSG_DATA(arg->cb->nlh); 18381da177e4SLinus Torvalds prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0; 18391da177e4SLinus Torvalds } else 18401da177e4SLinus Torvalds prefix = 0; 18411da177e4SLinus Torvalds 18421da177e4SLinus Torvalds return rt6_fill_node(arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE, 18431da177e4SLinus Torvalds NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq, 18440d51aa80SJamal Hadi Salim prefix, NLM_F_MULTI); 18451da177e4SLinus Torvalds } 18461da177e4SLinus Torvalds 18471da177e4SLinus Torvalds static int fib6_dump_node(struct fib6_walker_t *w) 18481da177e4SLinus Torvalds { 18491da177e4SLinus Torvalds int res; 18501da177e4SLinus Torvalds struct rt6_info *rt; 18511da177e4SLinus Torvalds 18521da177e4SLinus Torvalds for (rt = w->leaf; rt; rt = rt->u.next) { 18531da177e4SLinus Torvalds res = rt6_dump_route(rt, w->args); 18541da177e4SLinus Torvalds if (res < 0) { 18551da177e4SLinus Torvalds /* Frame is full, suspend walking */ 18561da177e4SLinus Torvalds w->leaf = rt; 18571da177e4SLinus Torvalds return 1; 18581da177e4SLinus Torvalds } 18591da177e4SLinus Torvalds BUG_TRAP(res!=0); 18601da177e4SLinus Torvalds } 18611da177e4SLinus Torvalds w->leaf = NULL; 18621da177e4SLinus Torvalds return 0; 18631da177e4SLinus Torvalds } 18641da177e4SLinus Torvalds 18651da177e4SLinus Torvalds static void fib6_dump_end(struct netlink_callback *cb) 18661da177e4SLinus Torvalds { 18671da177e4SLinus Torvalds struct fib6_walker_t *w = (void*)cb->args[0]; 18681da177e4SLinus Torvalds 18691da177e4SLinus Torvalds if (w) { 18701da177e4SLinus Torvalds cb->args[0] = 0; 18711da177e4SLinus Torvalds fib6_walker_unlink(w); 18721da177e4SLinus Torvalds kfree(w); 18731da177e4SLinus Torvalds } 18741da177e4SLinus Torvalds cb->done = (void*)cb->args[1]; 18751da177e4SLinus Torvalds cb->args[1] = 0; 18761da177e4SLinus Torvalds } 18771da177e4SLinus Torvalds 18781da177e4SLinus Torvalds static int fib6_dump_done(struct netlink_callback *cb) 18791da177e4SLinus Torvalds { 18801da177e4SLinus Torvalds fib6_dump_end(cb); 1881a8f74b22SThomas Graf return cb->done ? cb->done(cb) : 0; 18821da177e4SLinus Torvalds } 18831da177e4SLinus Torvalds 18841da177e4SLinus Torvalds int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb) 18851da177e4SLinus Torvalds { 18861da177e4SLinus Torvalds struct rt6_rtnl_dump_arg arg; 18871da177e4SLinus Torvalds struct fib6_walker_t *w; 18881da177e4SLinus Torvalds int res; 18891da177e4SLinus Torvalds 18901da177e4SLinus Torvalds arg.skb = skb; 18911da177e4SLinus Torvalds arg.cb = cb; 18921da177e4SLinus Torvalds 18931da177e4SLinus Torvalds w = (void*)cb->args[0]; 18941da177e4SLinus Torvalds if (w == NULL) { 18951da177e4SLinus Torvalds /* New dump: 18961da177e4SLinus Torvalds * 18971da177e4SLinus Torvalds * 1. hook callback destructor. 18981da177e4SLinus Torvalds */ 18991da177e4SLinus Torvalds cb->args[1] = (long)cb->done; 19001da177e4SLinus Torvalds cb->done = fib6_dump_done; 19011da177e4SLinus Torvalds 19021da177e4SLinus Torvalds /* 19031da177e4SLinus Torvalds * 2. allocate and initialize walker. 19041da177e4SLinus Torvalds */ 19050c600edaSIngo Oeser w = kzalloc(sizeof(*w), GFP_ATOMIC); 19061da177e4SLinus Torvalds if (w == NULL) 19071da177e4SLinus Torvalds return -ENOMEM; 19081da177e4SLinus Torvalds RT6_TRACE("dump<%p", w); 19091da177e4SLinus Torvalds w->root = &ip6_routing_table; 19101da177e4SLinus Torvalds w->func = fib6_dump_node; 19111da177e4SLinus Torvalds w->args = &arg; 19121da177e4SLinus Torvalds cb->args[0] = (long)w; 19131da177e4SLinus Torvalds read_lock_bh(&rt6_lock); 19141da177e4SLinus Torvalds res = fib6_walk(w); 19151da177e4SLinus Torvalds read_unlock_bh(&rt6_lock); 19161da177e4SLinus Torvalds } else { 19171da177e4SLinus Torvalds w->args = &arg; 19181da177e4SLinus Torvalds read_lock_bh(&rt6_lock); 19191da177e4SLinus Torvalds res = fib6_walk_continue(w); 19201da177e4SLinus Torvalds read_unlock_bh(&rt6_lock); 19211da177e4SLinus Torvalds } 19221da177e4SLinus Torvalds #if RT6_DEBUG >= 3 19231da177e4SLinus Torvalds if (res <= 0 && skb->len == 0) 19241da177e4SLinus Torvalds RT6_TRACE("%p>dump end\n", w); 19251da177e4SLinus Torvalds #endif 19261da177e4SLinus Torvalds res = res < 0 ? res : skb->len; 19271da177e4SLinus Torvalds /* res < 0 is an error. (really, impossible) 19281da177e4SLinus Torvalds res == 0 means that dump is complete, but skb still can contain data. 19291da177e4SLinus Torvalds res > 0 dump is not complete, but frame is full. 19301da177e4SLinus Torvalds */ 19311da177e4SLinus Torvalds /* Destroy walker, if dump of this table is complete. */ 19321da177e4SLinus Torvalds if (res <= 0) 19331da177e4SLinus Torvalds fib6_dump_end(cb); 19341da177e4SLinus Torvalds return res; 19351da177e4SLinus Torvalds } 19361da177e4SLinus Torvalds 19371da177e4SLinus Torvalds int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg) 19381da177e4SLinus Torvalds { 19391da177e4SLinus Torvalds struct rtattr **rta = arg; 19401da177e4SLinus Torvalds int iif = 0; 19411da177e4SLinus Torvalds int err = -ENOBUFS; 19421da177e4SLinus Torvalds struct sk_buff *skb; 19431da177e4SLinus Torvalds struct flowi fl; 19441da177e4SLinus Torvalds struct rt6_info *rt; 19451da177e4SLinus Torvalds 19461da177e4SLinus Torvalds skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 19471da177e4SLinus Torvalds if (skb == NULL) 19481da177e4SLinus Torvalds goto out; 19491da177e4SLinus Torvalds 19501da177e4SLinus Torvalds /* Reserve room for dummy headers, this skb can pass 19511da177e4SLinus Torvalds through good chunk of routing engine. 19521da177e4SLinus Torvalds */ 19531da177e4SLinus Torvalds skb->mac.raw = skb->data; 19541da177e4SLinus Torvalds skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr)); 19551da177e4SLinus Torvalds 19561da177e4SLinus Torvalds memset(&fl, 0, sizeof(fl)); 19571da177e4SLinus Torvalds if (rta[RTA_SRC-1]) 19581da177e4SLinus Torvalds ipv6_addr_copy(&fl.fl6_src, 19591da177e4SLinus Torvalds (struct in6_addr*)RTA_DATA(rta[RTA_SRC-1])); 19601da177e4SLinus Torvalds if (rta[RTA_DST-1]) 19611da177e4SLinus Torvalds ipv6_addr_copy(&fl.fl6_dst, 19621da177e4SLinus Torvalds (struct in6_addr*)RTA_DATA(rta[RTA_DST-1])); 19631da177e4SLinus Torvalds 19641da177e4SLinus Torvalds if (rta[RTA_IIF-1]) 19651da177e4SLinus Torvalds memcpy(&iif, RTA_DATA(rta[RTA_IIF-1]), sizeof(int)); 19661da177e4SLinus Torvalds 19671da177e4SLinus Torvalds if (iif) { 19681da177e4SLinus Torvalds struct net_device *dev; 19691da177e4SLinus Torvalds dev = __dev_get_by_index(iif); 19701da177e4SLinus Torvalds if (!dev) { 19711da177e4SLinus Torvalds err = -ENODEV; 19721da177e4SLinus Torvalds goto out_free; 19731da177e4SLinus Torvalds } 19741da177e4SLinus Torvalds } 19751da177e4SLinus Torvalds 19761da177e4SLinus Torvalds fl.oif = 0; 19771da177e4SLinus Torvalds if (rta[RTA_OIF-1]) 19781da177e4SLinus Torvalds memcpy(&fl.oif, RTA_DATA(rta[RTA_OIF-1]), sizeof(int)); 19791da177e4SLinus Torvalds 19801da177e4SLinus Torvalds rt = (struct rt6_info*)ip6_route_output(NULL, &fl); 19811da177e4SLinus Torvalds 19821da177e4SLinus Torvalds skb->dst = &rt->u.dst; 19831da177e4SLinus Torvalds 19841da177e4SLinus Torvalds NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid; 19851da177e4SLinus Torvalds err = rt6_fill_node(skb, rt, 19861da177e4SLinus Torvalds &fl.fl6_dst, &fl.fl6_src, 19871da177e4SLinus Torvalds iif, 19881da177e4SLinus Torvalds RTM_NEWROUTE, NETLINK_CB(in_skb).pid, 19890d51aa80SJamal Hadi Salim nlh->nlmsg_seq, 0, 0); 19901da177e4SLinus Torvalds if (err < 0) { 19911da177e4SLinus Torvalds err = -EMSGSIZE; 19921da177e4SLinus Torvalds goto out_free; 19931da177e4SLinus Torvalds } 19941da177e4SLinus Torvalds 19951da177e4SLinus Torvalds err = netlink_unicast(rtnl, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT); 19961da177e4SLinus Torvalds if (err > 0) 19971da177e4SLinus Torvalds err = 0; 19981da177e4SLinus Torvalds out: 19991da177e4SLinus Torvalds return err; 20001da177e4SLinus Torvalds out_free: 20011da177e4SLinus Torvalds kfree_skb(skb); 20021da177e4SLinus Torvalds goto out; 20031da177e4SLinus Torvalds } 20041da177e4SLinus Torvalds 20050d51aa80SJamal Hadi Salim void inet6_rt_notify(int event, struct rt6_info *rt, struct nlmsghdr *nlh, 20060d51aa80SJamal Hadi Salim struct netlink_skb_parms *req) 20071da177e4SLinus Torvalds { 20081da177e4SLinus Torvalds struct sk_buff *skb; 20091da177e4SLinus Torvalds int size = NLMSG_SPACE(sizeof(struct rtmsg)+256); 20100d51aa80SJamal Hadi Salim u32 pid = current->pid; 20110d51aa80SJamal Hadi Salim u32 seq = 0; 20120d51aa80SJamal Hadi Salim 20130d51aa80SJamal Hadi Salim if (req) 20140d51aa80SJamal Hadi Salim pid = req->pid; 20150d51aa80SJamal Hadi Salim if (nlh) 20160d51aa80SJamal Hadi Salim seq = nlh->nlmsg_seq; 20171da177e4SLinus Torvalds 20181da177e4SLinus Torvalds skb = alloc_skb(size, gfp_any()); 20191da177e4SLinus Torvalds if (!skb) { 2020ac6d439dSPatrick McHardy netlink_set_err(rtnl, 0, RTNLGRP_IPV6_ROUTE, ENOBUFS); 20211da177e4SLinus Torvalds return; 20221da177e4SLinus Torvalds } 20230d51aa80SJamal Hadi Salim if (rt6_fill_node(skb, rt, NULL, NULL, 0, event, pid, seq, 0, 0) < 0) { 20241da177e4SLinus Torvalds kfree_skb(skb); 2025ac6d439dSPatrick McHardy netlink_set_err(rtnl, 0, RTNLGRP_IPV6_ROUTE, EINVAL); 20261da177e4SLinus Torvalds return; 20271da177e4SLinus Torvalds } 2028ac6d439dSPatrick McHardy NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_ROUTE; 2029ac6d439dSPatrick McHardy netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_ROUTE, gfp_any()); 20301da177e4SLinus Torvalds } 20311da177e4SLinus Torvalds 20321da177e4SLinus Torvalds /* 20331da177e4SLinus Torvalds * /proc 20341da177e4SLinus Torvalds */ 20351da177e4SLinus Torvalds 20361da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 20371da177e4SLinus Torvalds 20381da177e4SLinus Torvalds #define RT6_INFO_LEN (32 + 4 + 32 + 4 + 32 + 40 + 5 + 1) 20391da177e4SLinus Torvalds 20401da177e4SLinus Torvalds struct rt6_proc_arg 20411da177e4SLinus Torvalds { 20421da177e4SLinus Torvalds char *buffer; 20431da177e4SLinus Torvalds int offset; 20441da177e4SLinus Torvalds int length; 20451da177e4SLinus Torvalds int skip; 20461da177e4SLinus Torvalds int len; 20471da177e4SLinus Torvalds }; 20481da177e4SLinus Torvalds 20491da177e4SLinus Torvalds static int rt6_info_route(struct rt6_info *rt, void *p_arg) 20501da177e4SLinus Torvalds { 20511da177e4SLinus Torvalds struct rt6_proc_arg *arg = (struct rt6_proc_arg *) p_arg; 20521da177e4SLinus Torvalds int i; 20531da177e4SLinus Torvalds 20541da177e4SLinus Torvalds if (arg->skip < arg->offset / RT6_INFO_LEN) { 20551da177e4SLinus Torvalds arg->skip++; 20561da177e4SLinus Torvalds return 0; 20571da177e4SLinus Torvalds } 20581da177e4SLinus Torvalds 20591da177e4SLinus Torvalds if (arg->len >= arg->length) 20601da177e4SLinus Torvalds return 0; 20611da177e4SLinus Torvalds 20621da177e4SLinus Torvalds for (i=0; i<16; i++) { 20631da177e4SLinus Torvalds sprintf(arg->buffer + arg->len, "%02x", 20641da177e4SLinus Torvalds rt->rt6i_dst.addr.s6_addr[i]); 20651da177e4SLinus Torvalds arg->len += 2; 20661da177e4SLinus Torvalds } 20671da177e4SLinus Torvalds arg->len += sprintf(arg->buffer + arg->len, " %02x ", 20681da177e4SLinus Torvalds rt->rt6i_dst.plen); 20691da177e4SLinus Torvalds 20701da177e4SLinus Torvalds #ifdef CONFIG_IPV6_SUBTREES 20711da177e4SLinus Torvalds for (i=0; i<16; i++) { 20721da177e4SLinus Torvalds sprintf(arg->buffer + arg->len, "%02x", 20731da177e4SLinus Torvalds rt->rt6i_src.addr.s6_addr[i]); 20741da177e4SLinus Torvalds arg->len += 2; 20751da177e4SLinus Torvalds } 20761da177e4SLinus Torvalds arg->len += sprintf(arg->buffer + arg->len, " %02x ", 20771da177e4SLinus Torvalds rt->rt6i_src.plen); 20781da177e4SLinus Torvalds #else 20791da177e4SLinus Torvalds sprintf(arg->buffer + arg->len, 20801da177e4SLinus Torvalds "00000000000000000000000000000000 00 "); 20811da177e4SLinus Torvalds arg->len += 36; 20821da177e4SLinus Torvalds #endif 20831da177e4SLinus Torvalds 20841da177e4SLinus Torvalds if (rt->rt6i_nexthop) { 20851da177e4SLinus Torvalds for (i=0; i<16; i++) { 20861da177e4SLinus Torvalds sprintf(arg->buffer + arg->len, "%02x", 20871da177e4SLinus Torvalds rt->rt6i_nexthop->primary_key[i]); 20881da177e4SLinus Torvalds arg->len += 2; 20891da177e4SLinus Torvalds } 20901da177e4SLinus Torvalds } else { 20911da177e4SLinus Torvalds sprintf(arg->buffer + arg->len, 20921da177e4SLinus Torvalds "00000000000000000000000000000000"); 20931da177e4SLinus Torvalds arg->len += 32; 20941da177e4SLinus Torvalds } 20951da177e4SLinus Torvalds arg->len += sprintf(arg->buffer + arg->len, 20961da177e4SLinus Torvalds " %08x %08x %08x %08x %8s\n", 20971da177e4SLinus Torvalds rt->rt6i_metric, atomic_read(&rt->u.dst.__refcnt), 20981da177e4SLinus Torvalds rt->u.dst.__use, rt->rt6i_flags, 20991da177e4SLinus Torvalds rt->rt6i_dev ? rt->rt6i_dev->name : ""); 21001da177e4SLinus Torvalds return 0; 21011da177e4SLinus Torvalds } 21021da177e4SLinus Torvalds 21031da177e4SLinus Torvalds static int rt6_proc_info(char *buffer, char **start, off_t offset, int length) 21041da177e4SLinus Torvalds { 21051da177e4SLinus Torvalds struct rt6_proc_arg arg; 21061da177e4SLinus Torvalds arg.buffer = buffer; 21071da177e4SLinus Torvalds arg.offset = offset; 21081da177e4SLinus Torvalds arg.length = length; 21091da177e4SLinus Torvalds arg.skip = 0; 21101da177e4SLinus Torvalds arg.len = 0; 21111da177e4SLinus Torvalds 21121da177e4SLinus Torvalds read_lock_bh(&rt6_lock); 21131da177e4SLinus Torvalds fib6_clean_tree(&ip6_routing_table, rt6_info_route, 0, &arg); 21141da177e4SLinus Torvalds read_unlock_bh(&rt6_lock); 21151da177e4SLinus Torvalds 21161da177e4SLinus Torvalds *start = buffer; 21171da177e4SLinus Torvalds if (offset) 21181da177e4SLinus Torvalds *start += offset % RT6_INFO_LEN; 21191da177e4SLinus Torvalds 21201da177e4SLinus Torvalds arg.len -= offset % RT6_INFO_LEN; 21211da177e4SLinus Torvalds 21221da177e4SLinus Torvalds if (arg.len > length) 21231da177e4SLinus Torvalds arg.len = length; 21241da177e4SLinus Torvalds if (arg.len < 0) 21251da177e4SLinus Torvalds arg.len = 0; 21261da177e4SLinus Torvalds 21271da177e4SLinus Torvalds return arg.len; 21281da177e4SLinus Torvalds } 21291da177e4SLinus Torvalds 21301da177e4SLinus Torvalds static int rt6_stats_seq_show(struct seq_file *seq, void *v) 21311da177e4SLinus Torvalds { 21321da177e4SLinus Torvalds seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", 21331da177e4SLinus Torvalds rt6_stats.fib_nodes, rt6_stats.fib_route_nodes, 21341da177e4SLinus Torvalds rt6_stats.fib_rt_alloc, rt6_stats.fib_rt_entries, 21351da177e4SLinus Torvalds rt6_stats.fib_rt_cache, 21361da177e4SLinus Torvalds atomic_read(&ip6_dst_ops.entries), 21371da177e4SLinus Torvalds rt6_stats.fib_discarded_routes); 21381da177e4SLinus Torvalds 21391da177e4SLinus Torvalds return 0; 21401da177e4SLinus Torvalds } 21411da177e4SLinus Torvalds 21421da177e4SLinus Torvalds static int rt6_stats_seq_open(struct inode *inode, struct file *file) 21431da177e4SLinus Torvalds { 21441da177e4SLinus Torvalds return single_open(file, rt6_stats_seq_show, NULL); 21451da177e4SLinus Torvalds } 21461da177e4SLinus Torvalds 21471da177e4SLinus Torvalds static struct file_operations rt6_stats_seq_fops = { 21481da177e4SLinus Torvalds .owner = THIS_MODULE, 21491da177e4SLinus Torvalds .open = rt6_stats_seq_open, 21501da177e4SLinus Torvalds .read = seq_read, 21511da177e4SLinus Torvalds .llseek = seq_lseek, 21521da177e4SLinus Torvalds .release = single_release, 21531da177e4SLinus Torvalds }; 21541da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 21551da177e4SLinus Torvalds 21561da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 21571da177e4SLinus Torvalds 21581da177e4SLinus Torvalds static int flush_delay; 21591da177e4SLinus Torvalds 21601da177e4SLinus Torvalds static 21611da177e4SLinus Torvalds int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write, struct file * filp, 21621da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 21631da177e4SLinus Torvalds { 21641da177e4SLinus Torvalds if (write) { 21651da177e4SLinus Torvalds proc_dointvec(ctl, write, filp, buffer, lenp, ppos); 21661da177e4SLinus Torvalds fib6_run_gc(flush_delay <= 0 ? ~0UL : (unsigned long)flush_delay); 21671da177e4SLinus Torvalds return 0; 21681da177e4SLinus Torvalds } else 21691da177e4SLinus Torvalds return -EINVAL; 21701da177e4SLinus Torvalds } 21711da177e4SLinus Torvalds 21721da177e4SLinus Torvalds ctl_table ipv6_route_table[] = { 21731da177e4SLinus Torvalds { 21741da177e4SLinus Torvalds .ctl_name = NET_IPV6_ROUTE_FLUSH, 21751da177e4SLinus Torvalds .procname = "flush", 21761da177e4SLinus Torvalds .data = &flush_delay, 21771da177e4SLinus Torvalds .maxlen = sizeof(int), 217889c8b3a1SDave Jones .mode = 0200, 21791da177e4SLinus Torvalds .proc_handler = &ipv6_sysctl_rtcache_flush 21801da177e4SLinus Torvalds }, 21811da177e4SLinus Torvalds { 21821da177e4SLinus Torvalds .ctl_name = NET_IPV6_ROUTE_GC_THRESH, 21831da177e4SLinus Torvalds .procname = "gc_thresh", 21841da177e4SLinus Torvalds .data = &ip6_dst_ops.gc_thresh, 21851da177e4SLinus Torvalds .maxlen = sizeof(int), 21861da177e4SLinus Torvalds .mode = 0644, 21871da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 21881da177e4SLinus Torvalds }, 21891da177e4SLinus Torvalds { 21901da177e4SLinus Torvalds .ctl_name = NET_IPV6_ROUTE_MAX_SIZE, 21911da177e4SLinus Torvalds .procname = "max_size", 21921da177e4SLinus Torvalds .data = &ip6_rt_max_size, 21931da177e4SLinus Torvalds .maxlen = sizeof(int), 21941da177e4SLinus Torvalds .mode = 0644, 21951da177e4SLinus Torvalds .proc_handler = &proc_dointvec, 21961da177e4SLinus Torvalds }, 21971da177e4SLinus Torvalds { 21981da177e4SLinus Torvalds .ctl_name = NET_IPV6_ROUTE_GC_MIN_INTERVAL, 21991da177e4SLinus Torvalds .procname = "gc_min_interval", 22001da177e4SLinus Torvalds .data = &ip6_rt_gc_min_interval, 22011da177e4SLinus Torvalds .maxlen = sizeof(int), 22021da177e4SLinus Torvalds .mode = 0644, 22031da177e4SLinus Torvalds .proc_handler = &proc_dointvec_jiffies, 22041da177e4SLinus Torvalds .strategy = &sysctl_jiffies, 22051da177e4SLinus Torvalds }, 22061da177e4SLinus Torvalds { 22071da177e4SLinus Torvalds .ctl_name = NET_IPV6_ROUTE_GC_TIMEOUT, 22081da177e4SLinus Torvalds .procname = "gc_timeout", 22091da177e4SLinus Torvalds .data = &ip6_rt_gc_timeout, 22101da177e4SLinus Torvalds .maxlen = sizeof(int), 22111da177e4SLinus Torvalds .mode = 0644, 22121da177e4SLinus Torvalds .proc_handler = &proc_dointvec_jiffies, 22131da177e4SLinus Torvalds .strategy = &sysctl_jiffies, 22141da177e4SLinus Torvalds }, 22151da177e4SLinus Torvalds { 22161da177e4SLinus Torvalds .ctl_name = NET_IPV6_ROUTE_GC_INTERVAL, 22171da177e4SLinus Torvalds .procname = "gc_interval", 22181da177e4SLinus Torvalds .data = &ip6_rt_gc_interval, 22191da177e4SLinus Torvalds .maxlen = sizeof(int), 22201da177e4SLinus Torvalds .mode = 0644, 22211da177e4SLinus Torvalds .proc_handler = &proc_dointvec_jiffies, 22221da177e4SLinus Torvalds .strategy = &sysctl_jiffies, 22231da177e4SLinus Torvalds }, 22241da177e4SLinus Torvalds { 22251da177e4SLinus Torvalds .ctl_name = NET_IPV6_ROUTE_GC_ELASTICITY, 22261da177e4SLinus Torvalds .procname = "gc_elasticity", 22271da177e4SLinus Torvalds .data = &ip6_rt_gc_elasticity, 22281da177e4SLinus Torvalds .maxlen = sizeof(int), 22291da177e4SLinus Torvalds .mode = 0644, 22301da177e4SLinus Torvalds .proc_handler = &proc_dointvec_jiffies, 22311da177e4SLinus Torvalds .strategy = &sysctl_jiffies, 22321da177e4SLinus Torvalds }, 22331da177e4SLinus Torvalds { 22341da177e4SLinus Torvalds .ctl_name = NET_IPV6_ROUTE_MTU_EXPIRES, 22351da177e4SLinus Torvalds .procname = "mtu_expires", 22361da177e4SLinus Torvalds .data = &ip6_rt_mtu_expires, 22371da177e4SLinus Torvalds .maxlen = sizeof(int), 22381da177e4SLinus Torvalds .mode = 0644, 22391da177e4SLinus Torvalds .proc_handler = &proc_dointvec_jiffies, 22401da177e4SLinus Torvalds .strategy = &sysctl_jiffies, 22411da177e4SLinus Torvalds }, 22421da177e4SLinus Torvalds { 22431da177e4SLinus Torvalds .ctl_name = NET_IPV6_ROUTE_MIN_ADVMSS, 22441da177e4SLinus Torvalds .procname = "min_adv_mss", 22451da177e4SLinus Torvalds .data = &ip6_rt_min_advmss, 22461da177e4SLinus Torvalds .maxlen = sizeof(int), 22471da177e4SLinus Torvalds .mode = 0644, 22481da177e4SLinus Torvalds .proc_handler = &proc_dointvec_jiffies, 22491da177e4SLinus Torvalds .strategy = &sysctl_jiffies, 22501da177e4SLinus Torvalds }, 22511da177e4SLinus Torvalds { 22521da177e4SLinus Torvalds .ctl_name = NET_IPV6_ROUTE_GC_MIN_INTERVAL_MS, 22531da177e4SLinus Torvalds .procname = "gc_min_interval_ms", 22541da177e4SLinus Torvalds .data = &ip6_rt_gc_min_interval, 22551da177e4SLinus Torvalds .maxlen = sizeof(int), 22561da177e4SLinus Torvalds .mode = 0644, 22571da177e4SLinus Torvalds .proc_handler = &proc_dointvec_ms_jiffies, 22581da177e4SLinus Torvalds .strategy = &sysctl_ms_jiffies, 22591da177e4SLinus Torvalds }, 22601da177e4SLinus Torvalds { .ctl_name = 0 } 22611da177e4SLinus Torvalds }; 22621da177e4SLinus Torvalds 22631da177e4SLinus Torvalds #endif 22641da177e4SLinus Torvalds 22651da177e4SLinus Torvalds void __init ip6_route_init(void) 22661da177e4SLinus Torvalds { 22671da177e4SLinus Torvalds struct proc_dir_entry *p; 22681da177e4SLinus Torvalds 22691da177e4SLinus Torvalds ip6_dst_ops.kmem_cachep = kmem_cache_create("ip6_dst_cache", 22701da177e4SLinus Torvalds sizeof(struct rt6_info), 22711da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 22721da177e4SLinus Torvalds NULL, NULL); 22731da177e4SLinus Torvalds if (!ip6_dst_ops.kmem_cachep) 22741da177e4SLinus Torvalds panic("cannot create ip6_dst_cache"); 22751da177e4SLinus Torvalds 22761da177e4SLinus Torvalds fib6_init(); 22771da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 22781da177e4SLinus Torvalds p = proc_net_create("ipv6_route", 0, rt6_proc_info); 22791da177e4SLinus Torvalds if (p) 22801da177e4SLinus Torvalds p->owner = THIS_MODULE; 22811da177e4SLinus Torvalds 22821da177e4SLinus Torvalds proc_net_fops_create("rt6_stats", S_IRUGO, &rt6_stats_seq_fops); 22831da177e4SLinus Torvalds #endif 22841da177e4SLinus Torvalds #ifdef CONFIG_XFRM 22851da177e4SLinus Torvalds xfrm6_init(); 22861da177e4SLinus Torvalds #endif 22871da177e4SLinus Torvalds } 22881da177e4SLinus Torvalds 22891da177e4SLinus Torvalds void ip6_route_cleanup(void) 22901da177e4SLinus Torvalds { 22911da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 22921da177e4SLinus Torvalds proc_net_remove("ipv6_route"); 22931da177e4SLinus Torvalds proc_net_remove("rt6_stats"); 22941da177e4SLinus Torvalds #endif 22951da177e4SLinus Torvalds #ifdef CONFIG_XFRM 22961da177e4SLinus Torvalds xfrm6_fini(); 22971da177e4SLinus Torvalds #endif 22981da177e4SLinus Torvalds rt6_ifdown(NULL); 22991da177e4SLinus Torvalds fib6_gc_cleanup(); 23001da177e4SLinus Torvalds kmem_cache_destroy(ip6_dst_ops.kmem_cachep); 23011da177e4SLinus Torvalds } 2302