11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * IPv6 Address [auto]configuration 31da177e4SLinus Torvalds * Linux INET6 implementation 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Authors: 61da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 71da177e4SLinus Torvalds * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 101da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 111da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 121da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 131da177e4SLinus Torvalds */ 141da177e4SLinus Torvalds 151da177e4SLinus Torvalds /* 161da177e4SLinus Torvalds * Changes: 171da177e4SLinus Torvalds * 181da177e4SLinus Torvalds * Janos Farkas : delete timer on ifdown 191da177e4SLinus Torvalds * <chexum@bankinf.banki.hu> 201da177e4SLinus Torvalds * Andi Kleen : kill double kfree on module 211da177e4SLinus Torvalds * unload. 221da177e4SLinus Torvalds * Maciej W. Rozycki : FDDI support 231da177e4SLinus Torvalds * sekiya@USAGI : Don't send too many RS 241da177e4SLinus Torvalds * packets. 251da177e4SLinus Torvalds * yoshfuji@USAGI : Fixed interval between DAD 261da177e4SLinus Torvalds * packets. 271da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : improved accuracy of 281da177e4SLinus Torvalds * address validation timer. 291da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041) 301da177e4SLinus Torvalds * support. 311da177e4SLinus Torvalds * Yuji SEKIYA @USAGI : Don't assign a same IPv6 321da177e4SLinus Torvalds * address on a same interface. 331da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : ARCnet support 341da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to 351da177e4SLinus Torvalds * seq_file. 36b1cacb68SYOSHIFUJI Hideaki * YOSHIFUJI Hideaki @USAGI : improved source address 37b1cacb68SYOSHIFUJI Hideaki * selection; consider scope, 38b1cacb68SYOSHIFUJI Hideaki * status etc. 391da177e4SLinus Torvalds */ 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds #include <linux/errno.h> 421da177e4SLinus Torvalds #include <linux/types.h> 43a0bffffcSIlpo Järvinen #include <linux/kernel.h> 441da177e4SLinus Torvalds #include <linux/socket.h> 451da177e4SLinus Torvalds #include <linux/sockios.h> 461da177e4SLinus Torvalds #include <linux/net.h> 471da177e4SLinus Torvalds #include <linux/in6.h> 481da177e4SLinus Torvalds #include <linux/netdevice.h> 491823730fSThomas Graf #include <linux/if_addr.h> 501da177e4SLinus Torvalds #include <linux/if_arp.h> 511da177e4SLinus Torvalds #include <linux/if_arcnet.h> 521da177e4SLinus Torvalds #include <linux/if_infiniband.h> 531da177e4SLinus Torvalds #include <linux/route.h> 541da177e4SLinus Torvalds #include <linux/inetdevice.h> 551da177e4SLinus Torvalds #include <linux/init.h> 561da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 571da177e4SLinus Torvalds #include <linux/sysctl.h> 581da177e4SLinus Torvalds #endif 594fc268d2SRandy Dunlap #include <linux/capability.h> 601da177e4SLinus Torvalds #include <linux/delay.h> 611da177e4SLinus Torvalds #include <linux/notifier.h> 62543537bdSPaulo Marques #include <linux/string.h> 631da177e4SLinus Torvalds 64457c4cbcSEric W. Biederman #include <net/net_namespace.h> 651da177e4SLinus Torvalds #include <net/sock.h> 661da177e4SLinus Torvalds #include <net/snmp.h> 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds #include <net/ipv6.h> 691da177e4SLinus Torvalds #include <net/protocol.h> 701da177e4SLinus Torvalds #include <net/ndisc.h> 711da177e4SLinus Torvalds #include <net/ip6_route.h> 721da177e4SLinus Torvalds #include <net/addrconf.h> 731da177e4SLinus Torvalds #include <net/tcp.h> 741da177e4SLinus Torvalds #include <net/ip.h> 755d620266SThomas Graf #include <net/netlink.h> 76f24e3d65SMitsuru Chinen #include <net/pkt_sched.h> 771da177e4SLinus Torvalds #include <linux/if_tunnel.h> 781da177e4SLinus Torvalds #include <linux/rtnetlink.h> 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 811da177e4SLinus Torvalds #include <linux/random.h> 821da177e4SLinus Torvalds #endif 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds #include <asm/uaccess.h> 857f7d9a6bSHerbert Xu #include <asm/unaligned.h> 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds #include <linux/proc_fs.h> 881da177e4SLinus Torvalds #include <linux/seq_file.h> 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds /* Set to 3 to get tracing... */ 911da177e4SLinus Torvalds #define ACONF_DEBUG 2 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds #if ACONF_DEBUG >= 3 941da177e4SLinus Torvalds #define ADBG(x) printk x 951da177e4SLinus Torvalds #else 961da177e4SLinus Torvalds #define ADBG(x) 971da177e4SLinus Torvalds #endif 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds #define INFINITY_LIFE_TIME 0xFFFFFFFF 1001da177e4SLinus Torvalds #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b))) 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 103f52295a9SPavel Emelyanov static void addrconf_sysctl_register(struct inet6_dev *idev); 104408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev); 105408c4768SPavel Emelyanov #else 106408c4768SPavel Emelyanov static inline void addrconf_sysctl_register(struct inet6_dev *idev) 107408c4768SPavel Emelyanov { 108408c4768SPavel Emelyanov } 109408c4768SPavel Emelyanov 110408c4768SPavel Emelyanov static inline void addrconf_sysctl_unregister(struct inet6_dev *idev) 111408c4768SPavel Emelyanov { 112408c4768SPavel Emelyanov } 1131da177e4SLinus Torvalds #endif 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 1161da177e4SLinus Torvalds static int __ipv6_regen_rndid(struct inet6_dev *idev); 1171da177e4SLinus Torvalds static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr); 1181da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data); 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds static int desync_factor = MAX_DESYNC_FACTOR * HZ; 1211da177e4SLinus Torvalds #endif 1221da177e4SLinus Torvalds 1231b34be74SYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev); 1241da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev); 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds /* 1271da177e4SLinus Torvalds * Configured unicast address hash table 1281da177e4SLinus Torvalds */ 1291da177e4SLinus Torvalds static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE]; 1301da177e4SLinus Torvalds static DEFINE_RWLOCK(addrconf_hash_lock); 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds static void addrconf_verify(unsigned long); 1331da177e4SLinus Torvalds 1348d06afabSIngo Molnar static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0); 1351da177e4SLinus Torvalds static DEFINE_SPINLOCK(addrconf_verify_lock); 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp); 1381da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp); 1391da177e4SLinus Torvalds 14075c78500SMoni Shoua static void addrconf_bonding_change(struct net_device *dev, 14175c78500SMoni Shoua unsigned long event); 1421da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how); 1431da177e4SLinus Torvalds 144e431b8c0SJamal Hadi Salim static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags); 1451da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data); 1461da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp); 147c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev); 1481da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data); 1491da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 1501da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 1531da177e4SLinus Torvalds struct prefix_info *pinfo); 15406bfe655SDaniel Lezcano static int ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 15506bfe655SDaniel Lezcano struct net_device *dev); 1561da177e4SLinus Torvalds 157e041c683SAlan Stern static ATOMIC_NOTIFIER_HEAD(inet6addr_chain); 1581da177e4SLinus Torvalds 159888c848eSAdrian Bunk static struct ipv6_devconf ipv6_devconf __read_mostly = { 1601da177e4SLinus Torvalds .forwarding = 0, 1611da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 1621da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 1631da177e4SLinus Torvalds .accept_ra = 1, 1641da177e4SLinus Torvalds .accept_redirects = 1, 1651da177e4SLinus Torvalds .autoconf = 1, 1661da177e4SLinus Torvalds .force_mld_version = 0, 1671da177e4SLinus Torvalds .dad_transmits = 1, 1681da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 1691da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 1701da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 1711da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 1721da177e4SLinus Torvalds .use_tempaddr = 0, 1731da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 1741da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 1751da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 1761da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 1771da177e4SLinus Torvalds #endif 1781da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 17965f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 180c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 181930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 182930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 18352e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 18409c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 18509c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 18609c884d4SYOSHIFUJI Hideaki #endif 187930d6ff2SYOSHIFUJI Hideaki #endif 188fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0, 1890bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */ 190778d80beSYOSHIFUJI Hideaki .disable_ipv6 = 0, 1911b34be74SYOSHIFUJI Hideaki .accept_dad = 1, 1921da177e4SLinus Torvalds }; 1931da177e4SLinus Torvalds 194ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = { 1951da177e4SLinus Torvalds .forwarding = 0, 1961da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 1971da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 1981da177e4SLinus Torvalds .accept_ra = 1, 1991da177e4SLinus Torvalds .accept_redirects = 1, 2001da177e4SLinus Torvalds .autoconf = 1, 2011da177e4SLinus Torvalds .dad_transmits = 1, 2021da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 2031da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 2041da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 2051da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 2061da177e4SLinus Torvalds .use_tempaddr = 0, 2071da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 2081da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 2091da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 2101da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 2111da177e4SLinus Torvalds #endif 2121da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 21365f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 214c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 215930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 216930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 21752e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 21809c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 21909c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 22009c884d4SYOSHIFUJI Hideaki #endif 221930d6ff2SYOSHIFUJI Hideaki #endif 222fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0, 2230bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */ 224778d80beSYOSHIFUJI Hideaki .disable_ipv6 = 0, 2251b34be74SYOSHIFUJI Hideaki .accept_dad = 1, 2261da177e4SLinus Torvalds }; 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */ 2291da177e4SLinus Torvalds const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; 2301da177e4SLinus Torvalds const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; 231f3ee4010SYOSHIFUJI Hideaki const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT; 232f3ee4010SYOSHIFUJI Hideaki const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT; 2331da177e4SLinus Torvalds 234f24e3d65SMitsuru Chinen /* Check if a valid qdisc is available */ 23505297949SDavid S. Miller static inline bool addrconf_qdisc_ok(const struct net_device *dev) 236f24e3d65SMitsuru Chinen { 23705297949SDavid S. Miller return !qdisc_tx_is_noop(dev); 238f24e3d65SMitsuru Chinen } 239f24e3d65SMitsuru Chinen 2402b5ead46SYOSHIFUJI Hideaki /* Check if a route is valid prefix route */ 2412b5ead46SYOSHIFUJI Hideaki static inline int addrconf_is_prefix_route(const struct rt6_info *rt) 2422b5ead46SYOSHIFUJI Hideaki { 2432b5ead46SYOSHIFUJI Hideaki return ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0); 2442b5ead46SYOSHIFUJI Hideaki } 2452b5ead46SYOSHIFUJI Hideaki 2461da177e4SLinus Torvalds static void addrconf_del_timer(struct inet6_ifaddr *ifp) 2471da177e4SLinus Torvalds { 2481da177e4SLinus Torvalds if (del_timer(&ifp->timer)) 2491da177e4SLinus Torvalds __in6_ifa_put(ifp); 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds enum addrconf_timer_t 2531da177e4SLinus Torvalds { 2541da177e4SLinus Torvalds AC_NONE, 2551da177e4SLinus Torvalds AC_DAD, 2561da177e4SLinus Torvalds AC_RS, 2571da177e4SLinus Torvalds }; 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds static void addrconf_mod_timer(struct inet6_ifaddr *ifp, 2601da177e4SLinus Torvalds enum addrconf_timer_t what, 2611da177e4SLinus Torvalds unsigned long when) 2621da177e4SLinus Torvalds { 2631da177e4SLinus Torvalds if (!del_timer(&ifp->timer)) 2641da177e4SLinus Torvalds in6_ifa_hold(ifp); 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds switch (what) { 2671da177e4SLinus Torvalds case AC_DAD: 2681da177e4SLinus Torvalds ifp->timer.function = addrconf_dad_timer; 2691da177e4SLinus Torvalds break; 2701da177e4SLinus Torvalds case AC_RS: 2711da177e4SLinus Torvalds ifp->timer.function = addrconf_rs_timer; 2721da177e4SLinus Torvalds break; 2731da177e4SLinus Torvalds default:; 2741da177e4SLinus Torvalds } 2751da177e4SLinus Torvalds ifp->timer.expires = jiffies + when; 2761da177e4SLinus Torvalds add_timer(&ifp->timer); 2771da177e4SLinus Torvalds } 2781da177e4SLinus Torvalds 2797f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev) 2807f7d9a6bSHerbert Xu { 2817d720c3eSTejun Heo if (snmp_mib_init((void __percpu **)idev->stats.ipv6, 282c69bce20SYOSHIFUJI Hideaki sizeof(struct ipstats_mib)) < 0) 2837f7d9a6bSHerbert Xu goto err_ip; 2847d720c3eSTejun Heo if (snmp_mib_init((void __percpu **)idev->stats.icmpv6, 285c69bce20SYOSHIFUJI Hideaki sizeof(struct icmpv6_mib)) < 0) 2867f7d9a6bSHerbert Xu goto err_icmp; 2877d720c3eSTejun Heo if (snmp_mib_init((void __percpu **)idev->stats.icmpv6msg, 288c69bce20SYOSHIFUJI Hideaki sizeof(struct icmpv6msg_mib)) < 0) 28914878f75SDavid L Stevens goto err_icmpmsg; 2907f7d9a6bSHerbert Xu 2917f7d9a6bSHerbert Xu return 0; 2927f7d9a6bSHerbert Xu 29314878f75SDavid L Stevens err_icmpmsg: 2947d720c3eSTejun Heo snmp_mib_free((void __percpu **)idev->stats.icmpv6); 2957f7d9a6bSHerbert Xu err_icmp: 2967d720c3eSTejun Heo snmp_mib_free((void __percpu **)idev->stats.ipv6); 2977f7d9a6bSHerbert Xu err_ip: 298aaf70ec7SPavel Emelyanov return -ENOMEM; 2997f7d9a6bSHerbert Xu } 3007f7d9a6bSHerbert Xu 30116910b98SPavel Emelyanov static void snmp6_free_dev(struct inet6_dev *idev) 3027f7d9a6bSHerbert Xu { 3037d720c3eSTejun Heo snmp_mib_free((void __percpu **)idev->stats.icmpv6msg); 3047d720c3eSTejun Heo snmp_mib_free((void __percpu **)idev->stats.icmpv6); 3057d720c3eSTejun Heo snmp_mib_free((void __percpu **)idev->stats.ipv6); 3067f7d9a6bSHerbert Xu } 3077f7d9a6bSHerbert Xu 3081da177e4SLinus Torvalds /* Nobody refers to this device, we may destroy it. */ 3091da177e4SLinus Torvalds 3108814c4b5SYOSHIFUJI Hideaki static void in6_dev_finish_destroy_rcu(struct rcu_head *head) 3118814c4b5SYOSHIFUJI Hideaki { 3128814c4b5SYOSHIFUJI Hideaki struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu); 3138814c4b5SYOSHIFUJI Hideaki kfree(idev); 3148814c4b5SYOSHIFUJI Hideaki } 3158814c4b5SYOSHIFUJI Hideaki 3161da177e4SLinus Torvalds void in6_dev_finish_destroy(struct inet6_dev *idev) 3171da177e4SLinus Torvalds { 3181da177e4SLinus Torvalds struct net_device *dev = idev->dev; 319547b792cSIlpo Järvinen 320547b792cSIlpo Järvinen WARN_ON(idev->addr_list != NULL); 321547b792cSIlpo Järvinen WARN_ON(idev->mc_list != NULL); 322547b792cSIlpo Järvinen 3231da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 3241da177e4SLinus Torvalds printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL"); 3251da177e4SLinus Torvalds #endif 3261da177e4SLinus Torvalds dev_put(dev); 3271da177e4SLinus Torvalds if (!idev->dead) { 3281da177e4SLinus Torvalds printk("Freeing alive inet6 device %p\n", idev); 3291da177e4SLinus Torvalds return; 3301da177e4SLinus Torvalds } 3311da177e4SLinus Torvalds snmp6_free_dev(idev); 3328814c4b5SYOSHIFUJI Hideaki call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu); 3331da177e4SLinus Torvalds } 3341da177e4SLinus Torvalds 3357159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(in6_dev_finish_destroy); 3367159039aSYOSHIFUJI Hideaki 3371da177e4SLinus Torvalds static struct inet6_dev * ipv6_add_dev(struct net_device *dev) 3381da177e4SLinus Torvalds { 3391da177e4SLinus Torvalds struct inet6_dev *ndev; 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds ASSERT_RTNL(); 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 3441da177e4SLinus Torvalds return NULL; 3451da177e4SLinus Torvalds 346322f74a4SIngo Oeser ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL); 3471da177e4SLinus Torvalds 348322f74a4SIngo Oeser if (ndev == NULL) 349322f74a4SIngo Oeser return NULL; 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds rwlock_init(&ndev->lock); 3521da177e4SLinus Torvalds ndev->dev = dev; 353c346dca1SYOSHIFUJI Hideaki memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf)); 3541da177e4SLinus Torvalds ndev->cnf.mtu6 = dev->mtu; 3551da177e4SLinus Torvalds ndev->cnf.sysctl = NULL; 3561da177e4SLinus Torvalds ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl); 3571da177e4SLinus Torvalds if (ndev->nd_parms == NULL) { 3581da177e4SLinus Torvalds kfree(ndev); 3591da177e4SLinus Torvalds return NULL; 3601da177e4SLinus Torvalds } 3610187bdfbSBen Hutchings if (ndev->cnf.forwarding) 3620187bdfbSBen Hutchings dev_disable_lro(dev); 3631da177e4SLinus Torvalds /* We refer to the device */ 3641da177e4SLinus Torvalds dev_hold(dev); 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds if (snmp6_alloc_dev(ndev) < 0) { 3671da177e4SLinus Torvalds ADBG((KERN_WARNING 3681da177e4SLinus Torvalds "%s(): cannot allocate memory for statistics; dev=%s.\n", 3690dc47877SHarvey Harrison __func__, dev->name)); 3701da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, ndev->nd_parms); 3711da177e4SLinus Torvalds ndev->dead = 1; 3721da177e4SLinus Torvalds in6_dev_finish_destroy(ndev); 3731da177e4SLinus Torvalds return NULL; 3741da177e4SLinus Torvalds } 3751da177e4SLinus Torvalds 3761da177e4SLinus Torvalds if (snmp6_register_dev(ndev) < 0) { 3771da177e4SLinus Torvalds ADBG((KERN_WARNING 3781da177e4SLinus Torvalds "%s(): cannot create /proc/net/dev_snmp6/%s\n", 3790dc47877SHarvey Harrison __func__, dev->name)); 3801da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, ndev->nd_parms); 3811da177e4SLinus Torvalds ndev->dead = 1; 3821da177e4SLinus Torvalds in6_dev_finish_destroy(ndev); 3831da177e4SLinus Torvalds return NULL; 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds /* One reference from device. We must do this before 3871da177e4SLinus Torvalds * we invoke __ipv6_regen_rndid(). 3881da177e4SLinus Torvalds */ 3891da177e4SLinus Torvalds in6_dev_hold(ndev); 3901da177e4SLinus Torvalds 3911b34be74SYOSHIFUJI Hideaki if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 3921b34be74SYOSHIFUJI Hideaki ndev->cnf.accept_dad = -1; 3931b34be74SYOSHIFUJI Hideaki 3940be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 395c7dc89c0SFred L. Templin if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) { 396c7dc89c0SFred L. Templin printk(KERN_INFO 397c7dc89c0SFred L. Templin "%s: Disabled Multicast RS\n", 398c7dc89c0SFred L. Templin dev->name); 399c7dc89c0SFred L. Templin ndev->cnf.rtr_solicits = 0; 400c7dc89c0SFred L. Templin } 401b077d7abSYOSHIFUJI Hideaki #endif 402b077d7abSYOSHIFUJI Hideaki 4031da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 4041da177e4SLinus Torvalds setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev); 4051da177e4SLinus Torvalds if ((dev->flags&IFF_LOOPBACK) || 4061da177e4SLinus Torvalds dev->type == ARPHRD_TUNNEL || 4079625ed72SYOSHIFUJI Hideaki dev->type == ARPHRD_TUNNEL6 || 4081da177e4SLinus Torvalds dev->type == ARPHRD_SIT || 4091da177e4SLinus Torvalds dev->type == ARPHRD_NONE) { 4101da177e4SLinus Torvalds printk(KERN_INFO 4111da177e4SLinus Torvalds "%s: Disabled Privacy Extensions\n", 4121da177e4SLinus Torvalds dev->name); 4131da177e4SLinus Torvalds ndev->cnf.use_tempaddr = -1; 4141da177e4SLinus Torvalds } else { 4151da177e4SLinus Torvalds in6_dev_hold(ndev); 4161da177e4SLinus Torvalds ipv6_regen_rndid((unsigned long) ndev); 4171da177e4SLinus Torvalds } 4181da177e4SLinus Torvalds #endif 4191da177e4SLinus Torvalds 420f24e3d65SMitsuru Chinen if (netif_running(dev) && addrconf_qdisc_ok(dev)) 42153aadcc9SHerbert Xu ndev->if_flags |= IF_READY; 42253aadcc9SHerbert Xu 4231da177e4SLinus Torvalds ipv6_mc_init_dev(ndev); 4241da177e4SLinus Torvalds ndev->tstamp = jiffies; 425f52295a9SPavel Emelyanov addrconf_sysctl_register(ndev); 42630c4cf57SDavid L Stevens /* protected by rtnl_lock */ 42730c4cf57SDavid L Stevens rcu_assign_pointer(dev->ip6_ptr, ndev); 428d88ae4ccSYOSHIFUJI Hideaki 429d88ae4ccSYOSHIFUJI Hideaki /* Join all-node multicast group */ 430f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes); 431d88ae4ccSYOSHIFUJI Hideaki 4321da177e4SLinus Torvalds return ndev; 4331da177e4SLinus Torvalds } 4341da177e4SLinus Torvalds 435cee89473SYOSHIFUJI Hideaki static struct inet6_dev * ipv6_find_idev(struct net_device *dev) 4361da177e4SLinus Torvalds { 4371da177e4SLinus Torvalds struct inet6_dev *idev; 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds ASSERT_RTNL(); 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds if ((idev = __in6_dev_get(dev)) == NULL) { 4421da177e4SLinus Torvalds if ((idev = ipv6_add_dev(dev)) == NULL) 4431da177e4SLinus Torvalds return NULL; 4441da177e4SLinus Torvalds } 445c5e33bddSYOSHIFUJI Hideaki 4461da177e4SLinus Torvalds if (dev->flags&IFF_UP) 4471da177e4SLinus Torvalds ipv6_mc_up(idev); 4481da177e4SLinus Torvalds return idev; 4491da177e4SLinus Torvalds } 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 4521da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev) 4531da177e4SLinus Torvalds { 4541da177e4SLinus Torvalds struct net_device *dev; 4551da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds if (!idev) 4581da177e4SLinus Torvalds return; 4591da177e4SLinus Torvalds dev = idev->dev; 4600187bdfbSBen Hutchings if (idev->cnf.forwarding) 4610187bdfbSBen Hutchings dev_disable_lro(dev); 4621da177e4SLinus Torvalds if (dev && (dev->flags & IFF_MULTICAST)) { 4631da177e4SLinus Torvalds if (idev->cnf.forwarding) 464f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 4651da177e4SLinus Torvalds else 466f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters); 4671da177e4SLinus Torvalds } 4681da177e4SLinus Torvalds for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) { 4692c12a74cSMichal Wrobel if (ifa->flags&IFA_F_TENTATIVE) 4702c12a74cSMichal Wrobel continue; 4711da177e4SLinus Torvalds if (idev->cnf.forwarding) 4721da177e4SLinus Torvalds addrconf_join_anycast(ifa); 4731da177e4SLinus Torvalds else 4741da177e4SLinus Torvalds addrconf_leave_anycast(ifa); 4751da177e4SLinus Torvalds } 4761da177e4SLinus Torvalds } 4771da177e4SLinus Torvalds 4781da177e4SLinus Torvalds 479e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf) 4801da177e4SLinus Torvalds { 4811da177e4SLinus Torvalds struct net_device *dev; 4821da177e4SLinus Torvalds struct inet6_dev *idev; 4831da177e4SLinus Torvalds 4848814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 485c6d14c84SEric Dumazet for_each_netdev_rcu(net, dev) { 4861da177e4SLinus Torvalds idev = __in6_dev_get(dev); 4871da177e4SLinus Torvalds if (idev) { 488e186932bSPavel Emelyanov int changed = (!idev->cnf.forwarding) ^ (!newf); 489e186932bSPavel Emelyanov idev->cnf.forwarding = newf; 4901da177e4SLinus Torvalds if (changed) 4911da177e4SLinus Torvalds dev_forward_change(idev); 4921da177e4SLinus Torvalds } 4931da177e4SLinus Torvalds } 494c6d14c84SEric Dumazet rcu_read_unlock(); 4951da177e4SLinus Torvalds } 496c8fecf22SPavel Emelyanov 497b325fddbSStephen Hemminger static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int old) 498c8fecf22SPavel Emelyanov { 499bff16c2fSPavel Emelyanov struct net *net; 500bff16c2fSPavel Emelyanov 501bff16c2fSPavel Emelyanov net = (struct net *)table->extra2; 502441fc2a2SPavel Emelyanov if (p == &net->ipv6.devconf_dflt->forwarding) 503b325fddbSStephen Hemminger return 0; 504c8fecf22SPavel Emelyanov 50588af182eSEric W. Biederman if (!rtnl_trylock()) { 50688af182eSEric W. Biederman /* Restore the original values before restarting */ 50788af182eSEric W. Biederman *p = old; 5085007392dSEric W. Biederman return restart_syscall(); 50988af182eSEric W. Biederman } 510b325fddbSStephen Hemminger 511e186932bSPavel Emelyanov if (p == &net->ipv6.devconf_all->forwarding) { 512e186932bSPavel Emelyanov __s32 newf = net->ipv6.devconf_all->forwarding; 513e186932bSPavel Emelyanov net->ipv6.devconf_dflt->forwarding = newf; 514e186932bSPavel Emelyanov addrconf_forward_change(net, newf); 515c8fecf22SPavel Emelyanov } else if ((!*p) ^ (!old)) 516c8fecf22SPavel Emelyanov dev_forward_change((struct inet6_dev *)table->extra1); 5170187bdfbSBen Hutchings rtnl_unlock(); 518c8fecf22SPavel Emelyanov 519c8fecf22SPavel Emelyanov if (*p) 5207b4da532SDaniel Lezcano rt6_purge_dflt_routers(net); 521b325fddbSStephen Hemminger return 1; 522c8fecf22SPavel Emelyanov } 5231da177e4SLinus Torvalds #endif 5241da177e4SLinus Torvalds 5251da177e4SLinus Torvalds /* Nobody refers to this ifaddr, destroy it */ 5261da177e4SLinus Torvalds 5271da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp) 5281da177e4SLinus Torvalds { 529547b792cSIlpo Järvinen WARN_ON(ifp->if_next != NULL); 530547b792cSIlpo Järvinen WARN_ON(ifp->lst_next != NULL); 531547b792cSIlpo Järvinen 5321da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 5331da177e4SLinus Torvalds printk(KERN_DEBUG "inet6_ifa_finish_destroy\n"); 5341da177e4SLinus Torvalds #endif 5351da177e4SLinus Torvalds 5361da177e4SLinus Torvalds in6_dev_put(ifp->idev); 5371da177e4SLinus Torvalds 5381da177e4SLinus Torvalds if (del_timer(&ifp->timer)) 5391da177e4SLinus Torvalds printk("Timer is still running, when freeing ifa=%p\n", ifp); 5401da177e4SLinus Torvalds 5411da177e4SLinus Torvalds if (!ifp->dead) { 5421da177e4SLinus Torvalds printk("Freeing alive inet6 address %p\n", ifp); 5431da177e4SLinus Torvalds return; 5441da177e4SLinus Torvalds } 5451da177e4SLinus Torvalds dst_release(&ifp->rt->u.dst); 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds kfree(ifp); 5481da177e4SLinus Torvalds } 5491da177e4SLinus Torvalds 550e55ffac6SBrian Haley static void 551e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp) 552e55ffac6SBrian Haley { 553e55ffac6SBrian Haley struct inet6_ifaddr *ifa, **ifap; 5548a6ce0c0SYOSHIFUJI Hideaki int ifp_scope = ipv6_addr_src_scope(&ifp->addr); 555e55ffac6SBrian Haley 556e55ffac6SBrian Haley /* 557e55ffac6SBrian Haley * Each device address list is sorted in order of scope - 558e55ffac6SBrian Haley * global before linklocal. 559e55ffac6SBrian Haley */ 560e55ffac6SBrian Haley for (ifap = &idev->addr_list; (ifa = *ifap) != NULL; 561e55ffac6SBrian Haley ifap = &ifa->if_next) { 5628a6ce0c0SYOSHIFUJI Hideaki if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr)) 563e55ffac6SBrian Haley break; 564e55ffac6SBrian Haley } 565e55ffac6SBrian Haley 566e55ffac6SBrian Haley ifp->if_next = *ifap; 567e55ffac6SBrian Haley *ifap = ifp; 568e55ffac6SBrian Haley } 569e55ffac6SBrian Haley 5703eb84f49SYOSHIFUJI Hideaki /* 5713eb84f49SYOSHIFUJI Hideaki * Hash function taken from net_alias.c 5723eb84f49SYOSHIFUJI Hideaki */ 5733eb84f49SYOSHIFUJI Hideaki static u8 ipv6_addr_hash(const struct in6_addr *addr) 5743eb84f49SYOSHIFUJI Hideaki { 5753eb84f49SYOSHIFUJI Hideaki __u32 word; 5763eb84f49SYOSHIFUJI Hideaki 5773eb84f49SYOSHIFUJI Hideaki /* 5783eb84f49SYOSHIFUJI Hideaki * We perform the hash function over the last 64 bits of the address 5793eb84f49SYOSHIFUJI Hideaki * This will include the IEEE address token on links that support it. 5803eb84f49SYOSHIFUJI Hideaki */ 5813eb84f49SYOSHIFUJI Hideaki 5823eb84f49SYOSHIFUJI Hideaki word = (__force u32)(addr->s6_addr32[2] ^ addr->s6_addr32[3]); 5833eb84f49SYOSHIFUJI Hideaki word ^= (word >> 16); 5843eb84f49SYOSHIFUJI Hideaki word ^= (word >> 8); 5853eb84f49SYOSHIFUJI Hideaki 5863eb84f49SYOSHIFUJI Hideaki return ((word ^ (word >> 4)) & 0x0f); 5873eb84f49SYOSHIFUJI Hideaki } 5883eb84f49SYOSHIFUJI Hideaki 5891da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */ 5901da177e4SLinus Torvalds 5911da177e4SLinus Torvalds static struct inet6_ifaddr * 5921da177e4SLinus Torvalds ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen, 593e431b8c0SJamal Hadi Salim int scope, u32 flags) 5941da177e4SLinus Torvalds { 5951da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 5961da177e4SLinus Torvalds struct rt6_info *rt; 5971da177e4SLinus Torvalds int hash; 5981da177e4SLinus Torvalds int err = 0; 599d68b8270SYOSHIFUJI Hideaki int addr_type = ipv6_addr_type(addr); 600d68b8270SYOSHIFUJI Hideaki 601d68b8270SYOSHIFUJI Hideaki if (addr_type == IPV6_ADDR_ANY || 602d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_MULTICAST || 603d68b8270SYOSHIFUJI Hideaki (!(idev->dev->flags & IFF_LOOPBACK) && 604d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_LOOPBACK)) 605d68b8270SYOSHIFUJI Hideaki return ERR_PTR(-EADDRNOTAVAIL); 6061da177e4SLinus Torvalds 6078814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 6081da177e4SLinus Torvalds if (idev->dead) { 6091da177e4SLinus Torvalds err = -ENODEV; /*XXX*/ 6101da177e4SLinus Torvalds goto out2; 6111da177e4SLinus Torvalds } 6121da177e4SLinus Torvalds 61356d417b1SBrian Haley if (idev->cnf.disable_ipv6) { 6149bdd8d40SBrian Haley err = -EACCES; 6159bdd8d40SBrian Haley goto out2; 6169bdd8d40SBrian Haley } 6179bdd8d40SBrian Haley 6181da177e4SLinus Torvalds write_lock(&addrconf_hash_lock); 6191da177e4SLinus Torvalds 6201da177e4SLinus Torvalds /* Ignore adding duplicate addresses on an interface */ 621c346dca1SYOSHIFUJI Hideaki if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) { 6221da177e4SLinus Torvalds ADBG(("ipv6_add_addr: already assigned\n")); 6231da177e4SLinus Torvalds err = -EEXIST; 6241da177e4SLinus Torvalds goto out; 6251da177e4SLinus Torvalds } 6261da177e4SLinus Torvalds 627322f74a4SIngo Oeser ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC); 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds if (ifa == NULL) { 6301da177e4SLinus Torvalds ADBG(("ipv6_add_addr: malloc failed\n")); 6311da177e4SLinus Torvalds err = -ENOBUFS; 6321da177e4SLinus Torvalds goto out; 6331da177e4SLinus Torvalds } 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds rt = addrconf_dst_alloc(idev, addr, 0); 6361da177e4SLinus Torvalds if (IS_ERR(rt)) { 6371da177e4SLinus Torvalds err = PTR_ERR(rt); 6381da177e4SLinus Torvalds goto out; 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds 6411da177e4SLinus Torvalds ipv6_addr_copy(&ifa->addr, addr); 6421da177e4SLinus Torvalds 6431da177e4SLinus Torvalds spin_lock_init(&ifa->lock); 6441da177e4SLinus Torvalds init_timer(&ifa->timer); 6451da177e4SLinus Torvalds ifa->timer.data = (unsigned long) ifa; 6461da177e4SLinus Torvalds ifa->scope = scope; 6471da177e4SLinus Torvalds ifa->prefix_len = pfxlen; 6481da177e4SLinus Torvalds ifa->flags = flags | IFA_F_TENTATIVE; 6491da177e4SLinus Torvalds ifa->cstamp = ifa->tstamp = jiffies; 6501da177e4SLinus Torvalds 65157f5f544SKeir Fraser ifa->rt = rt; 65257f5f544SKeir Fraser 65395c385b4SNeil Horman /* 65495c385b4SNeil Horman * part one of RFC 4429, section 3.3 65595c385b4SNeil Horman * We should not configure an address as 65695c385b4SNeil Horman * optimistic if we do not yet know the link 65795c385b4SNeil Horman * layer address of our nexhop router 65895c385b4SNeil Horman */ 65995c385b4SNeil Horman 66095c385b4SNeil Horman if (rt->rt6i_nexthop == NULL) 66195c385b4SNeil Horman ifa->flags &= ~IFA_F_OPTIMISTIC; 66295c385b4SNeil Horman 6631da177e4SLinus Torvalds ifa->idev = idev; 6641da177e4SLinus Torvalds in6_dev_hold(idev); 6651da177e4SLinus Torvalds /* For caller */ 6661da177e4SLinus Torvalds in6_ifa_hold(ifa); 6671da177e4SLinus Torvalds 6681da177e4SLinus Torvalds /* Add to big hash table */ 6691da177e4SLinus Torvalds hash = ipv6_addr_hash(addr); 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds ifa->lst_next = inet6_addr_lst[hash]; 6721da177e4SLinus Torvalds inet6_addr_lst[hash] = ifa; 6731da177e4SLinus Torvalds in6_ifa_hold(ifa); 6741da177e4SLinus Torvalds write_unlock(&addrconf_hash_lock); 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds write_lock(&idev->lock); 6771da177e4SLinus Torvalds /* Add to inet6_dev unicast addr list. */ 678e55ffac6SBrian Haley ipv6_link_dev_addr(idev, ifa); 6791da177e4SLinus Torvalds 6801da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 6811da177e4SLinus Torvalds if (ifa->flags&IFA_F_TEMPORARY) { 6821da177e4SLinus Torvalds ifa->tmp_next = idev->tempaddr_list; 6831da177e4SLinus Torvalds idev->tempaddr_list = ifa; 6841da177e4SLinus Torvalds in6_ifa_hold(ifa); 6851da177e4SLinus Torvalds } 6861da177e4SLinus Torvalds #endif 6871da177e4SLinus Torvalds 6881da177e4SLinus Torvalds in6_ifa_hold(ifa); 6891da177e4SLinus Torvalds write_unlock(&idev->lock); 6901da177e4SLinus Torvalds out2: 6918814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 6921da177e4SLinus Torvalds 693fd92833aSYOSHIFUJI Hideaki if (likely(err == 0)) 694e041c683SAlan Stern atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa); 6951da177e4SLinus Torvalds else { 6961da177e4SLinus Torvalds kfree(ifa); 6971da177e4SLinus Torvalds ifa = ERR_PTR(err); 6981da177e4SLinus Torvalds } 6991da177e4SLinus Torvalds 7001da177e4SLinus Torvalds return ifa; 7011da177e4SLinus Torvalds out: 7021da177e4SLinus Torvalds write_unlock(&addrconf_hash_lock); 7031da177e4SLinus Torvalds goto out2; 7041da177e4SLinus Torvalds } 7051da177e4SLinus Torvalds 7061da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */ 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp) 7091da177e4SLinus Torvalds { 7101da177e4SLinus Torvalds struct inet6_ifaddr *ifa, **ifap; 7111da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 7121da177e4SLinus Torvalds int hash; 7131da177e4SLinus Torvalds int deleted = 0, onlink = 0; 7141da177e4SLinus Torvalds unsigned long expires = jiffies; 7151da177e4SLinus Torvalds 7161da177e4SLinus Torvalds hash = ipv6_addr_hash(&ifp->addr); 7171da177e4SLinus Torvalds 7181da177e4SLinus Torvalds ifp->dead = 1; 7191da177e4SLinus Torvalds 7201da177e4SLinus Torvalds write_lock_bh(&addrconf_hash_lock); 7211da177e4SLinus Torvalds for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL; 7221da177e4SLinus Torvalds ifap = &ifa->lst_next) { 7231da177e4SLinus Torvalds if (ifa == ifp) { 7241da177e4SLinus Torvalds *ifap = ifa->lst_next; 7251da177e4SLinus Torvalds __in6_ifa_put(ifp); 7261da177e4SLinus Torvalds ifa->lst_next = NULL; 7271da177e4SLinus Torvalds break; 7281da177e4SLinus Torvalds } 7291da177e4SLinus Torvalds } 7301da177e4SLinus Torvalds write_unlock_bh(&addrconf_hash_lock); 7311da177e4SLinus Torvalds 7321da177e4SLinus Torvalds write_lock_bh(&idev->lock); 7331da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 7341da177e4SLinus Torvalds if (ifp->flags&IFA_F_TEMPORARY) { 7351da177e4SLinus Torvalds for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL; 7361da177e4SLinus Torvalds ifap = &ifa->tmp_next) { 7371da177e4SLinus Torvalds if (ifa == ifp) { 7381da177e4SLinus Torvalds *ifap = ifa->tmp_next; 7391da177e4SLinus Torvalds if (ifp->ifpub) { 7401da177e4SLinus Torvalds in6_ifa_put(ifp->ifpub); 7411da177e4SLinus Torvalds ifp->ifpub = NULL; 7421da177e4SLinus Torvalds } 7431da177e4SLinus Torvalds __in6_ifa_put(ifp); 7441da177e4SLinus Torvalds ifa->tmp_next = NULL; 7451da177e4SLinus Torvalds break; 7461da177e4SLinus Torvalds } 7471da177e4SLinus Torvalds } 7481da177e4SLinus Torvalds } 7491da177e4SLinus Torvalds #endif 7501da177e4SLinus Torvalds 7511d142804SKristian Slavov for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) { 7521da177e4SLinus Torvalds if (ifa == ifp) { 7531da177e4SLinus Torvalds *ifap = ifa->if_next; 7541da177e4SLinus Torvalds __in6_ifa_put(ifp); 7551da177e4SLinus Torvalds ifa->if_next = NULL; 7561da177e4SLinus Torvalds if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0) 7571da177e4SLinus Torvalds break; 7581da177e4SLinus Torvalds deleted = 1; 7591d142804SKristian Slavov continue; 7601da177e4SLinus Torvalds } else if (ifp->flags & IFA_F_PERMANENT) { 7611da177e4SLinus Torvalds if (ipv6_prefix_equal(&ifa->addr, &ifp->addr, 7621da177e4SLinus Torvalds ifp->prefix_len)) { 7631da177e4SLinus Torvalds if (ifa->flags & IFA_F_PERMANENT) { 7641da177e4SLinus Torvalds onlink = 1; 7651da177e4SLinus Torvalds if (deleted) 7661da177e4SLinus Torvalds break; 7671da177e4SLinus Torvalds } else { 7681da177e4SLinus Torvalds unsigned long lifetime; 7691da177e4SLinus Torvalds 7701da177e4SLinus Torvalds if (!onlink) 7711da177e4SLinus Torvalds onlink = -1; 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds spin_lock(&ifa->lock); 7744bed72e4SYOSHIFUJI Hideaki 7754bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ); 7764bed72e4SYOSHIFUJI Hideaki /* 7774bed72e4SYOSHIFUJI Hideaki * Note: Because this address is 7784bed72e4SYOSHIFUJI Hideaki * not permanent, lifetime < 7794bed72e4SYOSHIFUJI Hideaki * LONG_MAX / HZ here. 7804bed72e4SYOSHIFUJI Hideaki */ 7811da177e4SLinus Torvalds if (time_before(expires, 7821da177e4SLinus Torvalds ifa->tstamp + lifetime * HZ)) 7831da177e4SLinus Torvalds expires = ifa->tstamp + lifetime * HZ; 7841da177e4SLinus Torvalds spin_unlock(&ifa->lock); 7851da177e4SLinus Torvalds } 7861da177e4SLinus Torvalds } 7871da177e4SLinus Torvalds } 7881d142804SKristian Slavov ifap = &ifa->if_next; 7891da177e4SLinus Torvalds } 7901da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 7911da177e4SLinus Torvalds 792b2238566SAndrey Vagin addrconf_del_timer(ifp); 793b2238566SAndrey Vagin 7941da177e4SLinus Torvalds ipv6_ifa_notify(RTM_DELADDR, ifp); 7951da177e4SLinus Torvalds 796e041c683SAlan Stern atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp); 7971da177e4SLinus Torvalds 7981da177e4SLinus Torvalds /* 7991da177e4SLinus Torvalds * Purge or update corresponding prefix 8001da177e4SLinus Torvalds * 8011da177e4SLinus Torvalds * 1) we don't purge prefix here if address was not permanent. 8021da177e4SLinus Torvalds * prefix is managed by its own lifetime. 8031da177e4SLinus Torvalds * 2) if there're no addresses, delete prefix. 8041da177e4SLinus Torvalds * 3) if there're still other permanent address(es), 8051da177e4SLinus Torvalds * corresponding prefix is still permanent. 8061da177e4SLinus Torvalds * 4) otherwise, update prefix lifetime to the 8071da177e4SLinus Torvalds * longest valid lifetime among the corresponding 8081da177e4SLinus Torvalds * addresses on the device. 8091da177e4SLinus Torvalds * Note: subsequent RA will update lifetime. 8101da177e4SLinus Torvalds * 8111da177e4SLinus Torvalds * --yoshfuji 8121da177e4SLinus Torvalds */ 8131da177e4SLinus Torvalds if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) { 8141da177e4SLinus Torvalds struct in6_addr prefix; 8151da177e4SLinus Torvalds struct rt6_info *rt; 816c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(ifp->idev->dev); 8171da177e4SLinus Torvalds ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len); 8186fda7350SBenjamin Thery rt = rt6_lookup(net, &prefix, NULL, ifp->idev->dev->ifindex, 1); 8191da177e4SLinus Torvalds 8202b5ead46SYOSHIFUJI Hideaki if (rt && addrconf_is_prefix_route(rt)) { 8211da177e4SLinus Torvalds if (onlink == 0) { 822e0a1ad73SThomas Graf ip6_del_rt(rt); 8231da177e4SLinus Torvalds rt = NULL; 8241da177e4SLinus Torvalds } else if (!(rt->rt6i_flags & RTF_EXPIRES)) { 8251da177e4SLinus Torvalds rt->rt6i_expires = expires; 8261da177e4SLinus Torvalds rt->rt6i_flags |= RTF_EXPIRES; 8271da177e4SLinus Torvalds } 8281da177e4SLinus Torvalds } 8291da177e4SLinus Torvalds dst_release(&rt->u.dst); 8301da177e4SLinus Torvalds } 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds in6_ifa_put(ifp); 8331da177e4SLinus Torvalds } 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 8361da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift) 8371da177e4SLinus Torvalds { 8381da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 8391da177e4SLinus Torvalds struct in6_addr addr, *tmpaddr; 8401da177e4SLinus Torvalds unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp; 841eac55bf9SBenoit Boissinot unsigned long regen_advance; 8421da177e4SLinus Torvalds int tmp_plen; 8431da177e4SLinus Torvalds int ret = 0; 8441da177e4SLinus Torvalds int max_addresses; 84595c385b4SNeil Horman u32 addr_flags; 8461da177e4SLinus Torvalds 8471da177e4SLinus Torvalds write_lock(&idev->lock); 8481da177e4SLinus Torvalds if (ift) { 8491da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 8501da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8); 8511da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 8521da177e4SLinus Torvalds tmpaddr = &addr; 8531da177e4SLinus Torvalds } else { 8541da177e4SLinus Torvalds tmpaddr = NULL; 8551da177e4SLinus Torvalds } 8561da177e4SLinus Torvalds retry: 8571da177e4SLinus Torvalds in6_dev_hold(idev); 8581da177e4SLinus Torvalds if (idev->cnf.use_tempaddr <= 0) { 8591da177e4SLinus Torvalds write_unlock(&idev->lock); 8601da177e4SLinus Torvalds printk(KERN_INFO 8611da177e4SLinus Torvalds "ipv6_create_tempaddr(): use_tempaddr is disabled.\n"); 8621da177e4SLinus Torvalds in6_dev_put(idev); 8631da177e4SLinus Torvalds ret = -1; 8641da177e4SLinus Torvalds goto out; 8651da177e4SLinus Torvalds } 8661da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 8671da177e4SLinus Torvalds if (ifp->regen_count++ >= idev->cnf.regen_max_retry) { 8681da177e4SLinus Torvalds idev->cnf.use_tempaddr = -1; /*XXX*/ 8691da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 8701da177e4SLinus Torvalds write_unlock(&idev->lock); 8711da177e4SLinus Torvalds printk(KERN_WARNING 8721da177e4SLinus Torvalds "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n"); 8731da177e4SLinus Torvalds in6_dev_put(idev); 8741da177e4SLinus Torvalds ret = -1; 8751da177e4SLinus Torvalds goto out; 8761da177e4SLinus Torvalds } 8771da177e4SLinus Torvalds in6_ifa_hold(ifp); 8781da177e4SLinus Torvalds memcpy(addr.s6_addr, ifp->addr.s6_addr, 8); 8791da177e4SLinus Torvalds if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) { 8801da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 8811da177e4SLinus Torvalds write_unlock(&idev->lock); 8821da177e4SLinus Torvalds printk(KERN_WARNING 8831da177e4SLinus Torvalds "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n"); 8841da177e4SLinus Torvalds in6_ifa_put(ifp); 8851da177e4SLinus Torvalds in6_dev_put(idev); 8861da177e4SLinus Torvalds ret = -1; 8871da177e4SLinus Torvalds goto out; 8881da177e4SLinus Torvalds } 8891da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], idev->rndid, 8); 8901da177e4SLinus Torvalds tmp_valid_lft = min_t(__u32, 8911da177e4SLinus Torvalds ifp->valid_lft, 8921da177e4SLinus Torvalds idev->cnf.temp_valid_lft); 8931da177e4SLinus Torvalds tmp_prefered_lft = min_t(__u32, 8941da177e4SLinus Torvalds ifp->prefered_lft, 8951da177e4SLinus Torvalds idev->cnf.temp_prefered_lft - desync_factor / HZ); 8961da177e4SLinus Torvalds tmp_plen = ifp->prefix_len; 8971da177e4SLinus Torvalds max_addresses = idev->cnf.max_addresses; 8981da177e4SLinus Torvalds tmp_cstamp = ifp->cstamp; 8991da177e4SLinus Torvalds tmp_tstamp = ifp->tstamp; 9001da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 9011da177e4SLinus Torvalds 902eac55bf9SBenoit Boissinot regen_advance = idev->cnf.regen_max_retry * 903eac55bf9SBenoit Boissinot idev->cnf.dad_transmits * 904eac55bf9SBenoit Boissinot idev->nd_parms->retrans_time / HZ; 9051da177e4SLinus Torvalds write_unlock(&idev->lock); 90695c385b4SNeil Horman 907eac55bf9SBenoit Boissinot /* A temporary address is created only if this calculated Preferred 908eac55bf9SBenoit Boissinot * Lifetime is greater than REGEN_ADVANCE time units. In particular, 909eac55bf9SBenoit Boissinot * an implementation must not create a temporary address with a zero 910eac55bf9SBenoit Boissinot * Preferred Lifetime. 911eac55bf9SBenoit Boissinot */ 912eac55bf9SBenoit Boissinot if (tmp_prefered_lft <= regen_advance) { 913eac55bf9SBenoit Boissinot in6_ifa_put(ifp); 914eac55bf9SBenoit Boissinot in6_dev_put(idev); 915eac55bf9SBenoit Boissinot ret = -1; 916eac55bf9SBenoit Boissinot goto out; 917eac55bf9SBenoit Boissinot } 918eac55bf9SBenoit Boissinot 91995c385b4SNeil Horman addr_flags = IFA_F_TEMPORARY; 92095c385b4SNeil Horman /* set in addrconf_prefix_rcv() */ 92195c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 92295c385b4SNeil Horman addr_flags |= IFA_F_OPTIMISTIC; 92395c385b4SNeil Horman 9241da177e4SLinus Torvalds ift = !max_addresses || 9251da177e4SLinus Torvalds ipv6_count_addresses(idev) < max_addresses ? 9261da177e4SLinus Torvalds ipv6_add_addr(idev, &addr, tmp_plen, 92795c385b4SNeil Horman ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, 92895c385b4SNeil Horman addr_flags) : NULL; 9291da177e4SLinus Torvalds if (!ift || IS_ERR(ift)) { 9301da177e4SLinus Torvalds in6_ifa_put(ifp); 9311da177e4SLinus Torvalds in6_dev_put(idev); 9321da177e4SLinus Torvalds printk(KERN_INFO 9331da177e4SLinus Torvalds "ipv6_create_tempaddr(): retry temporary address regeneration.\n"); 9341da177e4SLinus Torvalds tmpaddr = &addr; 9351da177e4SLinus Torvalds write_lock(&idev->lock); 9361da177e4SLinus Torvalds goto retry; 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds 9391da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 9401da177e4SLinus Torvalds ift->ifpub = ifp; 9411da177e4SLinus Torvalds ift->valid_lft = tmp_valid_lft; 9421da177e4SLinus Torvalds ift->prefered_lft = tmp_prefered_lft; 9431da177e4SLinus Torvalds ift->cstamp = tmp_cstamp; 9441da177e4SLinus Torvalds ift->tstamp = tmp_tstamp; 9451da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 9461da177e4SLinus Torvalds 9471da177e4SLinus Torvalds addrconf_dad_start(ift, 0); 9481da177e4SLinus Torvalds in6_ifa_put(ift); 9491da177e4SLinus Torvalds in6_dev_put(idev); 9501da177e4SLinus Torvalds out: 9511da177e4SLinus Torvalds return ret; 9521da177e4SLinus Torvalds } 9531da177e4SLinus Torvalds #endif 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds /* 956072047e4SYOSHIFUJI Hideaki * Choose an appropriate source address (RFC3484) 9571da177e4SLinus Torvalds */ 958a9b05723SYOSHIFUJI Hideaki enum { 959a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_INIT = 0, 960a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LOCAL, 961a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_SCOPE, 962a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFERRED, 963a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 964a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_HOA, 965a9b05723SYOSHIFUJI Hideaki #endif 966a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_OIF, 967a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LABEL, 968a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_PRIVACY 969a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PRIVACY, 970a9b05723SYOSHIFUJI Hideaki #endif 971a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_ORCHID, 972a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFIX, 973a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_MAX 974072047e4SYOSHIFUJI Hideaki }; 975072047e4SYOSHIFUJI Hideaki 976a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score { 977a9b05723SYOSHIFUJI Hideaki int rule; 978a9b05723SYOSHIFUJI Hideaki int addr_type; 979a9b05723SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 980a9b05723SYOSHIFUJI Hideaki DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX); 981a9b05723SYOSHIFUJI Hideaki int scopedist; 982a9b05723SYOSHIFUJI Hideaki int matchlen; 983a9b05723SYOSHIFUJI Hideaki }; 984a9b05723SYOSHIFUJI Hideaki 985a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst { 9869acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *addr; 987a9b05723SYOSHIFUJI Hideaki int ifindex; 988a9b05723SYOSHIFUJI Hideaki int scope; 989a9b05723SYOSHIFUJI Hideaki int label; 9907cbca67cSYOSHIFUJI Hideaki unsigned int prefs; 991a9b05723SYOSHIFUJI Hideaki }; 992072047e4SYOSHIFUJI Hideaki 993b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type) 9941da177e4SLinus Torvalds { 99545bb0060SUlrich Weber if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK)) 996072047e4SYOSHIFUJI Hideaki return 1; 997072047e4SYOSHIFUJI Hideaki return 0; 9981da177e4SLinus Torvalds } 9991da177e4SLinus Torvalds 10003de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net, 10013de23255SBenjamin Thery struct ipv6_saddr_score *score, 1002a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst *dst, 1003a9b05723SYOSHIFUJI Hideaki int i) 1004a9b05723SYOSHIFUJI Hideaki { 1005a9b05723SYOSHIFUJI Hideaki int ret; 1006a9b05723SYOSHIFUJI Hideaki 1007a9b05723SYOSHIFUJI Hideaki if (i <= score->rule) { 1008a9b05723SYOSHIFUJI Hideaki switch (i) { 1009a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1010a9b05723SYOSHIFUJI Hideaki ret = score->scopedist; 1011a9b05723SYOSHIFUJI Hideaki break; 1012a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1013a9b05723SYOSHIFUJI Hideaki ret = score->matchlen; 1014a9b05723SYOSHIFUJI Hideaki break; 1015a9b05723SYOSHIFUJI Hideaki default: 1016a9b05723SYOSHIFUJI Hideaki ret = !!test_bit(i, score->scorebits); 1017a9b05723SYOSHIFUJI Hideaki } 1018a9b05723SYOSHIFUJI Hideaki goto out; 1019a9b05723SYOSHIFUJI Hideaki } 1020a9b05723SYOSHIFUJI Hideaki 1021a9b05723SYOSHIFUJI Hideaki switch (i) { 1022a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_INIT: 1023a9b05723SYOSHIFUJI Hideaki /* Rule 0: remember if hiscore is not ready yet */ 1024a9b05723SYOSHIFUJI Hideaki ret = !!score->ifa; 1025a9b05723SYOSHIFUJI Hideaki break; 1026a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LOCAL: 1027a9b05723SYOSHIFUJI Hideaki /* Rule 1: Prefer same address */ 1028a9b05723SYOSHIFUJI Hideaki ret = ipv6_addr_equal(&score->ifa->addr, dst->addr); 1029a9b05723SYOSHIFUJI Hideaki break; 1030a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1031a9b05723SYOSHIFUJI Hideaki /* Rule 2: Prefer appropriate scope 1032a9b05723SYOSHIFUJI Hideaki * 1033a9b05723SYOSHIFUJI Hideaki * ret 1034a9b05723SYOSHIFUJI Hideaki * ^ 1035a9b05723SYOSHIFUJI Hideaki * -1 | d 15 1036a9b05723SYOSHIFUJI Hideaki * ---+--+-+---> scope 1037a9b05723SYOSHIFUJI Hideaki * | 1038a9b05723SYOSHIFUJI Hideaki * | d is scope of the destination. 1039a9b05723SYOSHIFUJI Hideaki * B-d | \ 1040a9b05723SYOSHIFUJI Hideaki * | \ <- smaller scope is better if 1041a9b05723SYOSHIFUJI Hideaki * B-15 | \ if scope is enough for destinaion. 1042a9b05723SYOSHIFUJI Hideaki * | ret = B - scope (-1 <= scope >= d <= 15). 1043a9b05723SYOSHIFUJI Hideaki * d-C-1 | / 1044a9b05723SYOSHIFUJI Hideaki * |/ <- greater is better 1045a9b05723SYOSHIFUJI Hideaki * -C / if scope is not enough for destination. 1046a9b05723SYOSHIFUJI Hideaki * /| ret = scope - C (-1 <= d < scope <= 15). 1047a9b05723SYOSHIFUJI Hideaki * 1048a9b05723SYOSHIFUJI Hideaki * d - C - 1 < B -15 (for all -1 <= d <= 15). 1049a9b05723SYOSHIFUJI Hideaki * C > d + 14 - B >= 15 + 14 - B = 29 - B. 1050a9b05723SYOSHIFUJI Hideaki * Assume B = 0 and we get C > 29. 1051a9b05723SYOSHIFUJI Hideaki */ 1052a9b05723SYOSHIFUJI Hideaki ret = __ipv6_addr_src_scope(score->addr_type); 1053a9b05723SYOSHIFUJI Hideaki if (ret >= dst->scope) 1054a9b05723SYOSHIFUJI Hideaki ret = -ret; 1055a9b05723SYOSHIFUJI Hideaki else 1056a9b05723SYOSHIFUJI Hideaki ret -= 128; /* 30 is enough */ 1057a9b05723SYOSHIFUJI Hideaki score->scopedist = ret; 1058a9b05723SYOSHIFUJI Hideaki break; 1059a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFERRED: 1060a9b05723SYOSHIFUJI Hideaki /* Rule 3: Avoid deprecated and optimistic addresses */ 1061a9b05723SYOSHIFUJI Hideaki ret = ipv6_saddr_preferred(score->addr_type) || 1062a9b05723SYOSHIFUJI Hideaki !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC)); 1063a9b05723SYOSHIFUJI Hideaki break; 1064a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1065a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_HOA: 10667cbca67cSYOSHIFUJI Hideaki { 1067a9b05723SYOSHIFUJI Hideaki /* Rule 4: Prefer home address */ 10687cbca67cSYOSHIFUJI Hideaki int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA); 10697cbca67cSYOSHIFUJI Hideaki ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome; 1070a9b05723SYOSHIFUJI Hideaki break; 10717cbca67cSYOSHIFUJI Hideaki } 1072a9b05723SYOSHIFUJI Hideaki #endif 1073a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_OIF: 1074a9b05723SYOSHIFUJI Hideaki /* Rule 5: Prefer outgoing interface */ 1075a9b05723SYOSHIFUJI Hideaki ret = (!dst->ifindex || 1076a9b05723SYOSHIFUJI Hideaki dst->ifindex == score->ifa->idev->dev->ifindex); 1077a9b05723SYOSHIFUJI Hideaki break; 1078a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LABEL: 1079a9b05723SYOSHIFUJI Hideaki /* Rule 6: Prefer matching label */ 10803de23255SBenjamin Thery ret = ipv6_addr_label(net, 10813de23255SBenjamin Thery &score->ifa->addr, score->addr_type, 1082a9b05723SYOSHIFUJI Hideaki score->ifa->idev->dev->ifindex) == dst->label; 1083a9b05723SYOSHIFUJI Hideaki break; 1084a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_PRIVACY 1085a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PRIVACY: 10867cbca67cSYOSHIFUJI Hideaki { 1087a9b05723SYOSHIFUJI Hideaki /* Rule 7: Prefer public address 1088a9b05723SYOSHIFUJI Hideaki * Note: prefer temprary address if use_tempaddr >= 2 1089a9b05723SYOSHIFUJI Hideaki */ 10907cbca67cSYOSHIFUJI Hideaki int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ? 10917cbca67cSYOSHIFUJI Hideaki !!(dst->prefs & IPV6_PREFER_SRC_TMP) : 10927cbca67cSYOSHIFUJI Hideaki score->ifa->idev->cnf.use_tempaddr >= 2; 10937cbca67cSYOSHIFUJI Hideaki ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp; 1094a9b05723SYOSHIFUJI Hideaki break; 10957cbca67cSYOSHIFUJI Hideaki } 1096a9b05723SYOSHIFUJI Hideaki #endif 1097a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_ORCHID: 1098a9b05723SYOSHIFUJI Hideaki /* Rule 8-: Prefer ORCHID vs ORCHID or 1099a9b05723SYOSHIFUJI Hideaki * non-ORCHID vs non-ORCHID 1100a9b05723SYOSHIFUJI Hideaki */ 1101a9b05723SYOSHIFUJI Hideaki ret = !(ipv6_addr_orchid(&score->ifa->addr) ^ 1102a9b05723SYOSHIFUJI Hideaki ipv6_addr_orchid(dst->addr)); 1103a9b05723SYOSHIFUJI Hideaki break; 1104a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1105a9b05723SYOSHIFUJI Hideaki /* Rule 8: Use longest matching prefix */ 1106a9b05723SYOSHIFUJI Hideaki score->matchlen = ret = ipv6_addr_diff(&score->ifa->addr, 1107a9b05723SYOSHIFUJI Hideaki dst->addr); 1108a9b05723SYOSHIFUJI Hideaki break; 1109a9b05723SYOSHIFUJI Hideaki default: 1110a9b05723SYOSHIFUJI Hideaki ret = 0; 1111a9b05723SYOSHIFUJI Hideaki } 1112a9b05723SYOSHIFUJI Hideaki 1113a9b05723SYOSHIFUJI Hideaki if (ret) 1114a9b05723SYOSHIFUJI Hideaki __set_bit(i, score->scorebits); 1115a9b05723SYOSHIFUJI Hideaki score->rule = i; 1116a9b05723SYOSHIFUJI Hideaki out: 1117a9b05723SYOSHIFUJI Hideaki return ret; 1118a9b05723SYOSHIFUJI Hideaki } 1119a9b05723SYOSHIFUJI Hideaki 1120191cd582SBrian Haley int ipv6_dev_get_saddr(struct net *net, struct net_device *dst_dev, 11219acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *daddr, unsigned int prefs, 11227cbca67cSYOSHIFUJI Hideaki struct in6_addr *saddr) 11231da177e4SLinus Torvalds { 1124a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score scores[2], 1125a9b05723SYOSHIFUJI Hideaki *score = &scores[0], *hiscore = &scores[1]; 1126a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst dst; 1127072047e4SYOSHIFUJI Hideaki struct net_device *dev; 1128a9b05723SYOSHIFUJI Hideaki int dst_type; 11291da177e4SLinus Torvalds 1130a9b05723SYOSHIFUJI Hideaki dst_type = __ipv6_addr_type(daddr); 1131a9b05723SYOSHIFUJI Hideaki dst.addr = daddr; 1132a9b05723SYOSHIFUJI Hideaki dst.ifindex = dst_dev ? dst_dev->ifindex : 0; 1133a9b05723SYOSHIFUJI Hideaki dst.scope = __ipv6_addr_src_scope(dst_type); 11343de23255SBenjamin Thery dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex); 11357cbca67cSYOSHIFUJI Hideaki dst.prefs = prefs; 1136a9b05723SYOSHIFUJI Hideaki 1137a9b05723SYOSHIFUJI Hideaki hiscore->rule = -1; 1138a9b05723SYOSHIFUJI Hideaki hiscore->ifa = NULL; 11391da177e4SLinus Torvalds 11408814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 1141072047e4SYOSHIFUJI Hideaki 1142c6d14c84SEric Dumazet for_each_netdev_rcu(net, dev) { 1143072047e4SYOSHIFUJI Hideaki struct inet6_dev *idev; 1144072047e4SYOSHIFUJI Hideaki 1145a9b05723SYOSHIFUJI Hideaki /* Candidate Source Address (section 4) 1146072047e4SYOSHIFUJI Hideaki * - multicast and link-local destination address, 1147072047e4SYOSHIFUJI Hideaki * the set of candidate source address MUST only 1148072047e4SYOSHIFUJI Hideaki * include addresses assigned to interfaces 1149072047e4SYOSHIFUJI Hideaki * belonging to the same link as the outgoing 1150072047e4SYOSHIFUJI Hideaki * interface. 1151072047e4SYOSHIFUJI Hideaki * (- For site-local destination addresses, the 1152072047e4SYOSHIFUJI Hideaki * set of candidate source addresses MUST only 1153072047e4SYOSHIFUJI Hideaki * include addresses assigned to interfaces 1154072047e4SYOSHIFUJI Hideaki * belonging to the same site as the outgoing 1155072047e4SYOSHIFUJI Hideaki * interface.) 1156072047e4SYOSHIFUJI Hideaki */ 1157a9b05723SYOSHIFUJI Hideaki if (((dst_type & IPV6_ADDR_MULTICAST) || 1158a9b05723SYOSHIFUJI Hideaki dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) && 1159a9b05723SYOSHIFUJI Hideaki dst.ifindex && dev->ifindex != dst.ifindex) 1160072047e4SYOSHIFUJI Hideaki continue; 1161072047e4SYOSHIFUJI Hideaki 11621da177e4SLinus Torvalds idev = __in6_dev_get(dev); 1163072047e4SYOSHIFUJI Hideaki if (!idev) 1164072047e4SYOSHIFUJI Hideaki continue; 1165072047e4SYOSHIFUJI Hideaki 11661da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1167a9b05723SYOSHIFUJI Hideaki for (score->ifa = idev->addr_list; score->ifa; score->ifa = score->ifa->if_next) { 1168a9b05723SYOSHIFUJI Hideaki int i; 1169072047e4SYOSHIFUJI Hideaki 1170a9b05723SYOSHIFUJI Hideaki /* 11716b3ae80aSYOSHIFUJI Hideaki * - Tentative Address (RFC2462 section 5.4) 11726b3ae80aSYOSHIFUJI Hideaki * - A tentative address is not considered 11736b3ae80aSYOSHIFUJI Hideaki * "assigned to an interface" in the traditional 117495c385b4SNeil Horman * sense, unless it is also flagged as optimistic. 11756b3ae80aSYOSHIFUJI Hideaki * - Candidate Source Address (section 4) 1176072047e4SYOSHIFUJI Hideaki * - In any case, anycast addresses, multicast 1177072047e4SYOSHIFUJI Hideaki * addresses, and the unspecified address MUST 1178072047e4SYOSHIFUJI Hideaki * NOT be included in a candidate set. 1179072047e4SYOSHIFUJI Hideaki */ 1180a9b05723SYOSHIFUJI Hideaki if ((score->ifa->flags & IFA_F_TENTATIVE) && 1181a9b05723SYOSHIFUJI Hideaki (!(score->ifa->flags & IFA_F_OPTIMISTIC))) 11826b3ae80aSYOSHIFUJI Hideaki continue; 1183a9b05723SYOSHIFUJI Hideaki 1184a9b05723SYOSHIFUJI Hideaki score->addr_type = __ipv6_addr_type(&score->ifa->addr); 1185a9b05723SYOSHIFUJI Hideaki 1186a9b05723SYOSHIFUJI Hideaki if (unlikely(score->addr_type == IPV6_ADDR_ANY || 1187a9b05723SYOSHIFUJI Hideaki score->addr_type & IPV6_ADDR_MULTICAST)) { 1188072047e4SYOSHIFUJI Hideaki LIMIT_NETDEBUG(KERN_DEBUG 1189072047e4SYOSHIFUJI Hideaki "ADDRCONF: unspecified / multicast address " 1190072047e4SYOSHIFUJI Hideaki "assigned as unicast address on %s", 1191072047e4SYOSHIFUJI Hideaki dev->name); 11921da177e4SLinus Torvalds continue; 1193072047e4SYOSHIFUJI Hideaki } 1194072047e4SYOSHIFUJI Hideaki 1195a9b05723SYOSHIFUJI Hideaki score->rule = -1; 1196a9b05723SYOSHIFUJI Hideaki bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX); 1197072047e4SYOSHIFUJI Hideaki 1198a9b05723SYOSHIFUJI Hideaki for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) { 1199a9b05723SYOSHIFUJI Hideaki int minihiscore, miniscore; 1200072047e4SYOSHIFUJI Hideaki 12013de23255SBenjamin Thery minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i); 12023de23255SBenjamin Thery miniscore = ipv6_get_saddr_eval(net, score, &dst, i); 1203072047e4SYOSHIFUJI Hideaki 1204a9b05723SYOSHIFUJI Hideaki if (minihiscore > miniscore) { 1205a9b05723SYOSHIFUJI Hideaki if (i == IPV6_SADDR_RULE_SCOPE && 1206a9b05723SYOSHIFUJI Hideaki score->scopedist > 0) { 1207a9b05723SYOSHIFUJI Hideaki /* 1208a9b05723SYOSHIFUJI Hideaki * special case: 1209a9b05723SYOSHIFUJI Hideaki * each remaining entry 1210a9b05723SYOSHIFUJI Hideaki * has too small (not enough) 1211a9b05723SYOSHIFUJI Hideaki * scope, because ifa entries 1212a9b05723SYOSHIFUJI Hideaki * are sorted by their scope 1213a9b05723SYOSHIFUJI Hideaki * values. 1214072047e4SYOSHIFUJI Hideaki */ 1215a9b05723SYOSHIFUJI Hideaki goto try_nextdev; 1216072047e4SYOSHIFUJI Hideaki } 1217a9b05723SYOSHIFUJI Hideaki break; 1218a9b05723SYOSHIFUJI Hideaki } else if (minihiscore < miniscore) { 1219a9b05723SYOSHIFUJI Hideaki if (hiscore->ifa) 1220a9b05723SYOSHIFUJI Hideaki in6_ifa_put(hiscore->ifa); 122199cd07a5SJuha-Matti Tapio 1222a9b05723SYOSHIFUJI Hideaki in6_ifa_hold(score->ifa); 1223072047e4SYOSHIFUJI Hideaki 1224a0bffffcSIlpo Järvinen swap(hiscore, score); 1225a9b05723SYOSHIFUJI Hideaki 1226a9b05723SYOSHIFUJI Hideaki /* restore our iterator */ 1227a9b05723SYOSHIFUJI Hideaki score->ifa = hiscore->ifa; 1228a9b05723SYOSHIFUJI Hideaki 1229a9b05723SYOSHIFUJI Hideaki break; 12301da177e4SLinus Torvalds } 1231a9b05723SYOSHIFUJI Hideaki } 1232a9b05723SYOSHIFUJI Hideaki } 1233a9b05723SYOSHIFUJI Hideaki try_nextdev: 12341da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 12351da177e4SLinus Torvalds } 12368814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 12371da177e4SLinus Torvalds 1238a9b05723SYOSHIFUJI Hideaki if (!hiscore->ifa) 1239072047e4SYOSHIFUJI Hideaki return -EADDRNOTAVAIL; 12401da177e4SLinus Torvalds 1241a9b05723SYOSHIFUJI Hideaki ipv6_addr_copy(saddr, &hiscore->ifa->addr); 1242a9b05723SYOSHIFUJI Hideaki in6_ifa_put(hiscore->ifa); 1243072047e4SYOSHIFUJI Hideaki return 0; 12441da177e4SLinus Torvalds } 12451da177e4SLinus Torvalds 12465e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr); 12471da177e4SLinus Torvalds 124895c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr, 124995c385b4SNeil Horman unsigned char banned_flags) 12501da177e4SLinus Torvalds { 12511da177e4SLinus Torvalds struct inet6_dev *idev; 12521da177e4SLinus Torvalds int err = -EADDRNOTAVAIL; 12531da177e4SLinus Torvalds 12548814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 12551da177e4SLinus Torvalds if ((idev = __in6_dev_get(dev)) != NULL) { 12561da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 12571da177e4SLinus Torvalds 12581da177e4SLinus Torvalds read_lock_bh(&idev->lock); 12591da177e4SLinus Torvalds for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) { 126095c385b4SNeil Horman if (ifp->scope == IFA_LINK && !(ifp->flags & banned_flags)) { 12611da177e4SLinus Torvalds ipv6_addr_copy(addr, &ifp->addr); 12621da177e4SLinus Torvalds err = 0; 12631da177e4SLinus Torvalds break; 12641da177e4SLinus Torvalds } 12651da177e4SLinus Torvalds } 12661da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 12671da177e4SLinus Torvalds } 12688814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 12691da177e4SLinus Torvalds return err; 12701da177e4SLinus Torvalds } 12711da177e4SLinus Torvalds 12721da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev) 12731da177e4SLinus Torvalds { 12741da177e4SLinus Torvalds int cnt = 0; 12751da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 12761da177e4SLinus Torvalds 12771da177e4SLinus Torvalds read_lock_bh(&idev->lock); 12781da177e4SLinus Torvalds for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) 12791da177e4SLinus Torvalds cnt++; 12801da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 12811da177e4SLinus Torvalds return cnt; 12821da177e4SLinus Torvalds } 12831da177e4SLinus Torvalds 1284bfeade08SDaniel Lezcano int ipv6_chk_addr(struct net *net, struct in6_addr *addr, 1285bfeade08SDaniel Lezcano struct net_device *dev, int strict) 12861da177e4SLinus Torvalds { 12871da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 12881da177e4SLinus Torvalds u8 hash = ipv6_addr_hash(addr); 12891da177e4SLinus Torvalds 12901da177e4SLinus Torvalds read_lock_bh(&addrconf_hash_lock); 12911da177e4SLinus Torvalds for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) { 1292878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 1293bfeade08SDaniel Lezcano continue; 12941da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr) && 12951da177e4SLinus Torvalds !(ifp->flags&IFA_F_TENTATIVE)) { 12961da177e4SLinus Torvalds if (dev == NULL || ifp->idev->dev == dev || 12971da177e4SLinus Torvalds !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) 12981da177e4SLinus Torvalds break; 12991da177e4SLinus Torvalds } 13001da177e4SLinus Torvalds } 13011da177e4SLinus Torvalds read_unlock_bh(&addrconf_hash_lock); 13021da177e4SLinus Torvalds return ifp != NULL; 13031da177e4SLinus Torvalds } 13047159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_addr); 13057159039aSYOSHIFUJI Hideaki 13061da177e4SLinus Torvalds static 130706bfe655SDaniel Lezcano int ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 130806bfe655SDaniel Lezcano struct net_device *dev) 13091da177e4SLinus Torvalds { 13101da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 13111da177e4SLinus Torvalds u8 hash = ipv6_addr_hash(addr); 13121da177e4SLinus Torvalds 13131da177e4SLinus Torvalds for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) { 1314878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 131506bfe655SDaniel Lezcano continue; 13161da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 13171da177e4SLinus Torvalds if (dev == NULL || ifp->idev->dev == dev) 13181da177e4SLinus Torvalds break; 13191da177e4SLinus Torvalds } 13201da177e4SLinus Torvalds } 13211da177e4SLinus Torvalds return ifp != NULL; 13221da177e4SLinus Torvalds } 13231da177e4SLinus Torvalds 132452eeeb84SYOSHIFUJI Hideaki int ipv6_chk_prefix(struct in6_addr *addr, struct net_device *dev) 132552eeeb84SYOSHIFUJI Hideaki { 132652eeeb84SYOSHIFUJI Hideaki struct inet6_dev *idev; 132752eeeb84SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 132852eeeb84SYOSHIFUJI Hideaki int onlink; 132952eeeb84SYOSHIFUJI Hideaki 133052eeeb84SYOSHIFUJI Hideaki onlink = 0; 133152eeeb84SYOSHIFUJI Hideaki rcu_read_lock(); 133252eeeb84SYOSHIFUJI Hideaki idev = __in6_dev_get(dev); 133352eeeb84SYOSHIFUJI Hideaki if (idev) { 133452eeeb84SYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 133552eeeb84SYOSHIFUJI Hideaki for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) { 133652eeeb84SYOSHIFUJI Hideaki onlink = ipv6_prefix_equal(addr, &ifa->addr, 133752eeeb84SYOSHIFUJI Hideaki ifa->prefix_len); 133852eeeb84SYOSHIFUJI Hideaki if (onlink) 133952eeeb84SYOSHIFUJI Hideaki break; 134052eeeb84SYOSHIFUJI Hideaki } 134152eeeb84SYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 134252eeeb84SYOSHIFUJI Hideaki } 134352eeeb84SYOSHIFUJI Hideaki rcu_read_unlock(); 134452eeeb84SYOSHIFUJI Hideaki return onlink; 134552eeeb84SYOSHIFUJI Hideaki } 134652eeeb84SYOSHIFUJI Hideaki 134752eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix); 134852eeeb84SYOSHIFUJI Hideaki 13499acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr, 13501cab3da6SDaniel Lezcano struct net_device *dev, int strict) 13511da177e4SLinus Torvalds { 13521da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 13531da177e4SLinus Torvalds u8 hash = ipv6_addr_hash(addr); 13541da177e4SLinus Torvalds 13551da177e4SLinus Torvalds read_lock_bh(&addrconf_hash_lock); 13561da177e4SLinus Torvalds for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) { 1357878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 13581cab3da6SDaniel Lezcano continue; 13591da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 13601da177e4SLinus Torvalds if (dev == NULL || ifp->idev->dev == dev || 13611da177e4SLinus Torvalds !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) { 13621da177e4SLinus Torvalds in6_ifa_hold(ifp); 13631da177e4SLinus Torvalds break; 13641da177e4SLinus Torvalds } 13651da177e4SLinus Torvalds } 13661da177e4SLinus Torvalds } 13671da177e4SLinus Torvalds read_unlock_bh(&addrconf_hash_lock); 13681da177e4SLinus Torvalds 13691da177e4SLinus Torvalds return ifp; 13701da177e4SLinus Torvalds } 13711da177e4SLinus Torvalds 13721da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */ 13731da177e4SLinus Torvalds 1374cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed) 13751da177e4SLinus Torvalds { 13761da177e4SLinus Torvalds if (ifp->flags&IFA_F_PERMANENT) { 13771da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 13781da177e4SLinus Torvalds addrconf_del_timer(ifp); 13791da177e4SLinus Torvalds ifp->flags |= IFA_F_TENTATIVE; 1380cc411d0bSBrian Haley if (dad_failed) 1381cc411d0bSBrian Haley ifp->flags |= IFA_F_DADFAILED; 13821da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 13831da177e4SLinus Torvalds in6_ifa_put(ifp); 13841da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 13851da177e4SLinus Torvalds } else if (ifp->flags&IFA_F_TEMPORARY) { 13861da177e4SLinus Torvalds struct inet6_ifaddr *ifpub; 13871da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 13881da177e4SLinus Torvalds ifpub = ifp->ifpub; 13891da177e4SLinus Torvalds if (ifpub) { 13901da177e4SLinus Torvalds in6_ifa_hold(ifpub); 13911da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 13921da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 13931da177e4SLinus Torvalds in6_ifa_put(ifpub); 13941da177e4SLinus Torvalds } else { 13951da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 13961da177e4SLinus Torvalds } 13971da177e4SLinus Torvalds ipv6_del_addr(ifp); 13981da177e4SLinus Torvalds #endif 13991da177e4SLinus Torvalds } else 14001da177e4SLinus Torvalds ipv6_del_addr(ifp); 14011da177e4SLinus Torvalds } 14021da177e4SLinus Torvalds 14033c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp) 14043c21edbdSYOSHIFUJI Hideaki { 14051b34be74SYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 14069bdd8d40SBrian Haley 14079bdd8d40SBrian Haley if (net_ratelimit()) 14080522fea6SJens Rosenboom printk(KERN_INFO "%s: IPv6 duplicate address %pI6c detected!\n", 14090522fea6SJens Rosenboom ifp->idev->dev->name, &ifp->addr); 14109bdd8d40SBrian Haley 14111b34be74SYOSHIFUJI Hideaki if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) { 14121b34be74SYOSHIFUJI Hideaki struct in6_addr addr; 14131b34be74SYOSHIFUJI Hideaki 14141b34be74SYOSHIFUJI Hideaki addr.s6_addr32[0] = htonl(0xfe800000); 14151b34be74SYOSHIFUJI Hideaki addr.s6_addr32[1] = 0; 14161b34be74SYOSHIFUJI Hideaki 14171b34be74SYOSHIFUJI Hideaki if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) && 14181b34be74SYOSHIFUJI Hideaki ipv6_addr_equal(&ifp->addr, &addr)) { 14191b34be74SYOSHIFUJI Hideaki /* DAD failed for link-local based on MAC address */ 14201b34be74SYOSHIFUJI Hideaki idev->cnf.disable_ipv6 = 1; 14219bdd8d40SBrian Haley 14229bdd8d40SBrian Haley printk(KERN_INFO "%s: IPv6 being disabled!\n", 14239bdd8d40SBrian Haley ifp->idev->dev->name); 14241b34be74SYOSHIFUJI Hideaki } 14251b34be74SYOSHIFUJI Hideaki } 14261b34be74SYOSHIFUJI Hideaki 1427cc411d0bSBrian Haley addrconf_dad_stop(ifp, 1); 14283c21edbdSYOSHIFUJI Hideaki } 14291da177e4SLinus Torvalds 14301da177e4SLinus Torvalds /* Join to solicited addr multicast group. */ 14311da177e4SLinus Torvalds 14321da177e4SLinus Torvalds void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr) 14331da177e4SLinus Torvalds { 14341da177e4SLinus Torvalds struct in6_addr maddr; 14351da177e4SLinus Torvalds 14361da177e4SLinus Torvalds if (dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 14371da177e4SLinus Torvalds return; 14381da177e4SLinus Torvalds 14391da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 14401da177e4SLinus Torvalds ipv6_dev_mc_inc(dev, &maddr); 14411da177e4SLinus Torvalds } 14421da177e4SLinus Torvalds 14431da177e4SLinus Torvalds void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr) 14441da177e4SLinus Torvalds { 14451da177e4SLinus Torvalds struct in6_addr maddr; 14461da177e4SLinus Torvalds 14471da177e4SLinus Torvalds if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 14481da177e4SLinus Torvalds return; 14491da177e4SLinus Torvalds 14501da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 14511da177e4SLinus Torvalds __ipv6_dev_mc_dec(idev, &maddr); 14521da177e4SLinus Torvalds } 14531da177e4SLinus Torvalds 145420380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp) 14551da177e4SLinus Torvalds { 14561da177e4SLinus Torvalds struct in6_addr addr; 14571da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 14581da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 14591da177e4SLinus Torvalds return; 14601da177e4SLinus Torvalds ipv6_dev_ac_inc(ifp->idev->dev, &addr); 14611da177e4SLinus Torvalds } 14621da177e4SLinus Torvalds 146320380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp) 14641da177e4SLinus Torvalds { 14651da177e4SLinus Torvalds struct in6_addr addr; 14661da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 14671da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 14681da177e4SLinus Torvalds return; 14691da177e4SLinus Torvalds __ipv6_dev_ac_dec(ifp->idev, &addr); 14701da177e4SLinus Torvalds } 14711da177e4SLinus Torvalds 1472073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev) 14731da177e4SLinus Torvalds { 14741da177e4SLinus Torvalds if (dev->addr_len != ETH_ALEN) 14751da177e4SLinus Torvalds return -1; 14761da177e4SLinus Torvalds memcpy(eui, dev->dev_addr, 3); 14771da177e4SLinus Torvalds memcpy(eui + 5, dev->dev_addr + 3, 3); 14781da177e4SLinus Torvalds 14791da177e4SLinus Torvalds /* 14801da177e4SLinus Torvalds * The zSeries OSA network cards can be shared among various 14811da177e4SLinus Torvalds * OS instances, but the OSA cards have only one MAC address. 14821da177e4SLinus Torvalds * This leads to duplicate address conflicts in conjunction 14831da177e4SLinus Torvalds * with IPv6 if more than one instance uses the same card. 14841da177e4SLinus Torvalds * 14851da177e4SLinus Torvalds * The driver for these cards can deliver a unique 16-bit 14861da177e4SLinus Torvalds * identifier for each instance sharing the same card. It is 14871da177e4SLinus Torvalds * placed instead of 0xFFFE in the interface identifier. The 14881da177e4SLinus Torvalds * "u" bit of the interface identifier is not inverted in this 14891da177e4SLinus Torvalds * case. Hence the resulting interface identifier has local 14901da177e4SLinus Torvalds * scope according to RFC2373. 14911da177e4SLinus Torvalds */ 14921da177e4SLinus Torvalds if (dev->dev_id) { 14931da177e4SLinus Torvalds eui[3] = (dev->dev_id >> 8) & 0xFF; 14941da177e4SLinus Torvalds eui[4] = dev->dev_id & 0xFF; 14951da177e4SLinus Torvalds } else { 14961da177e4SLinus Torvalds eui[3] = 0xFF; 14971da177e4SLinus Torvalds eui[4] = 0xFE; 14981da177e4SLinus Torvalds eui[0] ^= 2; 14991da177e4SLinus Torvalds } 15001da177e4SLinus Torvalds return 0; 1501073a8e0eSYOSHIFUJI Hideaki } 1502073a8e0eSYOSHIFUJI Hideaki 1503073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev) 1504073a8e0eSYOSHIFUJI Hideaki { 15051da177e4SLinus Torvalds /* XXX: inherit EUI-64 from other interface -- yoshfuji */ 15061da177e4SLinus Torvalds if (dev->addr_len != ARCNET_ALEN) 15071da177e4SLinus Torvalds return -1; 15081da177e4SLinus Torvalds memset(eui, 0, 7); 15091da177e4SLinus Torvalds eui[7] = *(u8*)dev->dev_addr; 15101da177e4SLinus Torvalds return 0; 1511073a8e0eSYOSHIFUJI Hideaki } 1512073a8e0eSYOSHIFUJI Hideaki 1513073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev) 1514073a8e0eSYOSHIFUJI Hideaki { 15151da177e4SLinus Torvalds if (dev->addr_len != INFINIBAND_ALEN) 15161da177e4SLinus Torvalds return -1; 15171da177e4SLinus Torvalds memcpy(eui, dev->dev_addr + 12, 8); 15181da177e4SLinus Torvalds eui[0] |= 2; 15191da177e4SLinus Torvalds return 0; 15201da177e4SLinus Torvalds } 1521073a8e0eSYOSHIFUJI Hideaki 1522dfd982baSYOSHIFUJI Hideaki int __ipv6_isatap_ifid(u8 *eui, __be32 addr) 1523dfd982baSYOSHIFUJI Hideaki { 15249af28511SSascha Hlusiak if (addr == 0) 15259af28511SSascha Hlusiak return -1; 1526dfd982baSYOSHIFUJI Hideaki eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) || 1527dfd982baSYOSHIFUJI Hideaki ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) || 1528dfd982baSYOSHIFUJI Hideaki ipv4_is_private_172(addr) || ipv4_is_test_192(addr) || 1529dfd982baSYOSHIFUJI Hideaki ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) || 1530dfd982baSYOSHIFUJI Hideaki ipv4_is_test_198(addr) || ipv4_is_multicast(addr) || 1531dfd982baSYOSHIFUJI Hideaki ipv4_is_lbcast(addr)) ? 0x00 : 0x02; 1532dfd982baSYOSHIFUJI Hideaki eui[1] = 0; 1533dfd982baSYOSHIFUJI Hideaki eui[2] = 0x5E; 1534dfd982baSYOSHIFUJI Hideaki eui[3] = 0xFE; 1535dfd982baSYOSHIFUJI Hideaki memcpy(eui + 4, &addr, 4); 1536dfd982baSYOSHIFUJI Hideaki return 0; 1537dfd982baSYOSHIFUJI Hideaki } 1538dfd982baSYOSHIFUJI Hideaki EXPORT_SYMBOL(__ipv6_isatap_ifid); 1539dfd982baSYOSHIFUJI Hideaki 1540dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev) 1541dfd982baSYOSHIFUJI Hideaki { 1542dfd982baSYOSHIFUJI Hideaki if (dev->priv_flags & IFF_ISATAP) 1543dfd982baSYOSHIFUJI Hideaki return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 1544dfd982baSYOSHIFUJI Hideaki return -1; 1545dfd982baSYOSHIFUJI Hideaki } 1546dfd982baSYOSHIFUJI Hideaki 1547073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev) 1548073a8e0eSYOSHIFUJI Hideaki { 1549073a8e0eSYOSHIFUJI Hideaki switch (dev->type) { 1550073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ETHER: 1551073a8e0eSYOSHIFUJI Hideaki case ARPHRD_FDDI: 1552073a8e0eSYOSHIFUJI Hideaki case ARPHRD_IEEE802_TR: 1553073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_eui48(eui, dev); 1554073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ARCNET: 1555073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_arcnet(eui, dev); 1556073a8e0eSYOSHIFUJI Hideaki case ARPHRD_INFINIBAND: 1557073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_infiniband(eui, dev); 1558c7dc89c0SFred L. Templin case ARPHRD_SIT: 1559dfd982baSYOSHIFUJI Hideaki return addrconf_ifid_sit(eui, dev); 1560073a8e0eSYOSHIFUJI Hideaki } 15611da177e4SLinus Torvalds return -1; 15621da177e4SLinus Torvalds } 15631da177e4SLinus Torvalds 15641da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev) 15651da177e4SLinus Torvalds { 15661da177e4SLinus Torvalds int err = -1; 15671da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 15681da177e4SLinus Torvalds 15691da177e4SLinus Torvalds read_lock_bh(&idev->lock); 15701da177e4SLinus Torvalds for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) { 15711da177e4SLinus Torvalds if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) { 15721da177e4SLinus Torvalds memcpy(eui, ifp->addr.s6_addr+8, 8); 15731da177e4SLinus Torvalds err = 0; 15741da177e4SLinus Torvalds break; 15751da177e4SLinus Torvalds } 15761da177e4SLinus Torvalds } 15771da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 15781da177e4SLinus Torvalds return err; 15791da177e4SLinus Torvalds } 15801da177e4SLinus Torvalds 15811da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 15821da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */ 15831da177e4SLinus Torvalds static int __ipv6_regen_rndid(struct inet6_dev *idev) 15841da177e4SLinus Torvalds { 15851da177e4SLinus Torvalds regen: 1586955189efSYOSHIFUJI Hideaki get_random_bytes(idev->rndid, sizeof(idev->rndid)); 15871da177e4SLinus Torvalds idev->rndid[0] &= ~0x02; 15881da177e4SLinus Torvalds 15891da177e4SLinus Torvalds /* 15901da177e4SLinus Torvalds * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>: 15911da177e4SLinus Torvalds * check if generated address is not inappropriate 15921da177e4SLinus Torvalds * 15931da177e4SLinus Torvalds * - Reserved subnet anycast (RFC 2526) 15941da177e4SLinus Torvalds * 11111101 11....11 1xxxxxxx 1595c7dc89c0SFred L. Templin * - ISATAP (RFC4214) 6.1 15961da177e4SLinus Torvalds * 00-00-5E-FE-xx-xx-xx-xx 15971da177e4SLinus Torvalds * - value 0 15981da177e4SLinus Torvalds * - XXX: already assigned to an address on the device 15991da177e4SLinus Torvalds */ 16001da177e4SLinus Torvalds if (idev->rndid[0] == 0xfd && 16011da177e4SLinus Torvalds (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff && 16021da177e4SLinus Torvalds (idev->rndid[7]&0x80)) 16031da177e4SLinus Torvalds goto regen; 16041da177e4SLinus Torvalds if ((idev->rndid[0]|idev->rndid[1]) == 0) { 16051da177e4SLinus Torvalds if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe) 16061da177e4SLinus Torvalds goto regen; 16071da177e4SLinus Torvalds if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00) 16081da177e4SLinus Torvalds goto regen; 16091da177e4SLinus Torvalds } 16101da177e4SLinus Torvalds 16111da177e4SLinus Torvalds return 0; 16121da177e4SLinus Torvalds } 16131da177e4SLinus Torvalds 16141da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data) 16151da177e4SLinus Torvalds { 16161da177e4SLinus Torvalds struct inet6_dev *idev = (struct inet6_dev *) data; 16171da177e4SLinus Torvalds unsigned long expires; 16181da177e4SLinus Torvalds 16198814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 16201da177e4SLinus Torvalds write_lock_bh(&idev->lock); 16211da177e4SLinus Torvalds 16221da177e4SLinus Torvalds if (idev->dead) 16231da177e4SLinus Torvalds goto out; 16241da177e4SLinus Torvalds 16251da177e4SLinus Torvalds if (__ipv6_regen_rndid(idev) < 0) 16261da177e4SLinus Torvalds goto out; 16271da177e4SLinus Torvalds 16281da177e4SLinus Torvalds expires = jiffies + 16291da177e4SLinus Torvalds idev->cnf.temp_prefered_lft * HZ - 16301da177e4SLinus Torvalds idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor; 16311da177e4SLinus Torvalds if (time_before(expires, jiffies)) { 16321da177e4SLinus Torvalds printk(KERN_WARNING 16331da177e4SLinus Torvalds "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n", 16341da177e4SLinus Torvalds idev->dev->name); 16351da177e4SLinus Torvalds goto out; 16361da177e4SLinus Torvalds } 16371da177e4SLinus Torvalds 16381da177e4SLinus Torvalds if (!mod_timer(&idev->regen_timer, expires)) 16391da177e4SLinus Torvalds in6_dev_hold(idev); 16401da177e4SLinus Torvalds 16411da177e4SLinus Torvalds out: 16421da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 16438814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 16441da177e4SLinus Torvalds in6_dev_put(idev); 16451da177e4SLinus Torvalds } 16461da177e4SLinus Torvalds 16471da177e4SLinus Torvalds static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) { 16481da177e4SLinus Torvalds int ret = 0; 16491da177e4SLinus Torvalds 16501da177e4SLinus Torvalds if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0) 16511da177e4SLinus Torvalds ret = __ipv6_regen_rndid(idev); 16521da177e4SLinus Torvalds return ret; 16531da177e4SLinus Torvalds } 16541da177e4SLinus Torvalds #endif 16551da177e4SLinus Torvalds 16561da177e4SLinus Torvalds /* 16571da177e4SLinus Torvalds * Add prefix route. 16581da177e4SLinus Torvalds */ 16591da177e4SLinus Torvalds 16601da177e4SLinus Torvalds static void 16611da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev, 1662e431b8c0SJamal Hadi Salim unsigned long expires, u32 flags) 16631da177e4SLinus Torvalds { 166486872cb5SThomas Graf struct fib6_config cfg = { 166586872cb5SThomas Graf .fc_table = RT6_TABLE_PREFIX, 166686872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 166786872cb5SThomas Graf .fc_ifindex = dev->ifindex, 166886872cb5SThomas Graf .fc_expires = expires, 166986872cb5SThomas Graf .fc_dst_len = plen, 167086872cb5SThomas Graf .fc_flags = RTF_UP | flags, 1671c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 1672f410a1fbSStephen Hemminger .fc_protocol = RTPROT_KERNEL, 167386872cb5SThomas Graf }; 16741da177e4SLinus Torvalds 167586872cb5SThomas Graf ipv6_addr_copy(&cfg.fc_dst, pfx); 16761da177e4SLinus Torvalds 16771da177e4SLinus Torvalds /* Prevent useless cloning on PtP SIT. 16781da177e4SLinus Torvalds This thing is done here expecting that the whole 16791da177e4SLinus Torvalds class of non-broadcast devices need not cloning. 16801da177e4SLinus Torvalds */ 16810be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 16821da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT)) 168386872cb5SThomas Graf cfg.fc_flags |= RTF_NONEXTHOP; 16840be669bbSJoerg Roedel #endif 16851da177e4SLinus Torvalds 168686872cb5SThomas Graf ip6_route_add(&cfg); 16871da177e4SLinus Torvalds } 16881da177e4SLinus Torvalds 16891da177e4SLinus Torvalds /* Create "default" multicast route to the interface */ 16901da177e4SLinus Torvalds 16911da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev) 16921da177e4SLinus Torvalds { 169386872cb5SThomas Graf struct fib6_config cfg = { 169486872cb5SThomas Graf .fc_table = RT6_TABLE_LOCAL, 169586872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 169686872cb5SThomas Graf .fc_ifindex = dev->ifindex, 169786872cb5SThomas Graf .fc_dst_len = 8, 169886872cb5SThomas Graf .fc_flags = RTF_UP, 1699c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 170086872cb5SThomas Graf }; 17011da177e4SLinus Torvalds 170286872cb5SThomas Graf ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0); 170386872cb5SThomas Graf 170486872cb5SThomas Graf ip6_route_add(&cfg); 17051da177e4SLinus Torvalds } 17061da177e4SLinus Torvalds 17070be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 17081da177e4SLinus Torvalds static void sit_route_add(struct net_device *dev) 17091da177e4SLinus Torvalds { 171086872cb5SThomas Graf struct fib6_config cfg = { 171186872cb5SThomas Graf .fc_table = RT6_TABLE_MAIN, 171286872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 171386872cb5SThomas Graf .fc_ifindex = dev->ifindex, 171486872cb5SThomas Graf .fc_dst_len = 96, 171586872cb5SThomas Graf .fc_flags = RTF_UP | RTF_NONEXTHOP, 1716c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 171786872cb5SThomas Graf }; 17181da177e4SLinus Torvalds 17191da177e4SLinus Torvalds /* prefix length - 96 bits "::d.d.d.d" */ 172086872cb5SThomas Graf ip6_route_add(&cfg); 17211da177e4SLinus Torvalds } 17220be669bbSJoerg Roedel #endif 17231da177e4SLinus Torvalds 17241da177e4SLinus Torvalds static void addrconf_add_lroute(struct net_device *dev) 17251da177e4SLinus Torvalds { 17261da177e4SLinus Torvalds struct in6_addr addr; 17271da177e4SLinus Torvalds 17281da177e4SLinus Torvalds ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 17291da177e4SLinus Torvalds addrconf_prefix_route(&addr, 64, dev, 0, 0); 17301da177e4SLinus Torvalds } 17311da177e4SLinus Torvalds 17321da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev) 17331da177e4SLinus Torvalds { 17341da177e4SLinus Torvalds struct inet6_dev *idev; 17351da177e4SLinus Torvalds 17361da177e4SLinus Torvalds ASSERT_RTNL(); 17371da177e4SLinus Torvalds 17381da177e4SLinus Torvalds if ((idev = ipv6_find_idev(dev)) == NULL) 17391da177e4SLinus Torvalds return NULL; 17401da177e4SLinus Torvalds 17411da177e4SLinus Torvalds /* Add default multicast route */ 17421da177e4SLinus Torvalds addrconf_add_mroute(dev); 17431da177e4SLinus Torvalds 17441da177e4SLinus Torvalds /* Add link local route */ 17451da177e4SLinus Torvalds addrconf_add_lroute(dev); 17461da177e4SLinus Torvalds return idev; 17471da177e4SLinus Torvalds } 17481da177e4SLinus Torvalds 17491da177e4SLinus Torvalds void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len) 17501da177e4SLinus Torvalds { 17511da177e4SLinus Torvalds struct prefix_info *pinfo; 17521da177e4SLinus Torvalds __u32 valid_lft; 17531da177e4SLinus Torvalds __u32 prefered_lft; 17541da177e4SLinus Torvalds int addr_type; 17551da177e4SLinus Torvalds struct inet6_dev *in6_dev; 175656d417b1SBrian Haley struct net *net = dev_net(dev); 17571da177e4SLinus Torvalds 17581da177e4SLinus Torvalds pinfo = (struct prefix_info *) opt; 17591da177e4SLinus Torvalds 17601da177e4SLinus Torvalds if (len < sizeof(struct prefix_info)) { 17611da177e4SLinus Torvalds ADBG(("addrconf: prefix option too short\n")); 17621da177e4SLinus Torvalds return; 17631da177e4SLinus Torvalds } 17641da177e4SLinus Torvalds 17651da177e4SLinus Torvalds /* 17661da177e4SLinus Torvalds * Validation checks ([ADDRCONF], page 19) 17671da177e4SLinus Torvalds */ 17681da177e4SLinus Torvalds 17691da177e4SLinus Torvalds addr_type = ipv6_addr_type(&pinfo->prefix); 17701da177e4SLinus Torvalds 17711da177e4SLinus Torvalds if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)) 17721da177e4SLinus Torvalds return; 17731da177e4SLinus Torvalds 17741da177e4SLinus Torvalds valid_lft = ntohl(pinfo->valid); 17751da177e4SLinus Torvalds prefered_lft = ntohl(pinfo->prefered); 17761da177e4SLinus Torvalds 17771da177e4SLinus Torvalds if (prefered_lft > valid_lft) { 17781da177e4SLinus Torvalds if (net_ratelimit()) 17791da177e4SLinus Torvalds printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n"); 17801da177e4SLinus Torvalds return; 17811da177e4SLinus Torvalds } 17821da177e4SLinus Torvalds 17831da177e4SLinus Torvalds in6_dev = in6_dev_get(dev); 17841da177e4SLinus Torvalds 17851da177e4SLinus Torvalds if (in6_dev == NULL) { 17861da177e4SLinus Torvalds if (net_ratelimit()) 17871da177e4SLinus Torvalds printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name); 17881da177e4SLinus Torvalds return; 17891da177e4SLinus Torvalds } 17901da177e4SLinus Torvalds 17911da177e4SLinus Torvalds /* 17921da177e4SLinus Torvalds * Two things going on here: 17931da177e4SLinus Torvalds * 1) Add routes for on-link prefixes 17941da177e4SLinus Torvalds * 2) Configure prefixes with the auto flag set 17951da177e4SLinus Torvalds */ 17961da177e4SLinus Torvalds 17974bed72e4SYOSHIFUJI Hideaki if (pinfo->onlink) { 17984bed72e4SYOSHIFUJI Hideaki struct rt6_info *rt; 17994bed72e4SYOSHIFUJI Hideaki unsigned long rt_expires; 18004bed72e4SYOSHIFUJI Hideaki 18011da177e4SLinus Torvalds /* Avoid arithmetic overflow. Really, we could 18026f704992SYOSHIFUJI Hideaki * save rt_expires in seconds, likely valid_lft, 18036f704992SYOSHIFUJI Hideaki * but it would require division in fib gc, that it 18046f704992SYOSHIFUJI Hideaki * not good. 18051da177e4SLinus Torvalds */ 18064bed72e4SYOSHIFUJI Hideaki if (HZ > USER_HZ) 18074bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, HZ); 18084bed72e4SYOSHIFUJI Hideaki else 18094bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ); 18103dd4bc68SYOSHIFUJI Hideaki 18114bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) 18124bed72e4SYOSHIFUJI Hideaki rt_expires *= HZ; 18131da177e4SLinus Torvalds 181456d417b1SBrian Haley rt = rt6_lookup(net, &pinfo->prefix, NULL, 18156fda7350SBenjamin Thery dev->ifindex, 1); 18161da177e4SLinus Torvalds 18172b5ead46SYOSHIFUJI Hideaki if (rt && addrconf_is_prefix_route(rt)) { 18186f704992SYOSHIFUJI Hideaki /* Autoconf prefix route */ 18191da177e4SLinus Torvalds if (valid_lft == 0) { 1820e0a1ad73SThomas Graf ip6_del_rt(rt); 18211da177e4SLinus Torvalds rt = NULL; 18224bed72e4SYOSHIFUJI Hideaki } else if (addrconf_finite_timeout(rt_expires)) { 18236f704992SYOSHIFUJI Hideaki /* not infinity */ 18243dd4bc68SYOSHIFUJI Hideaki rt->rt6i_expires = jiffies + rt_expires; 18256f704992SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_EXPIRES; 18266f704992SYOSHIFUJI Hideaki } else { 18276f704992SYOSHIFUJI Hideaki rt->rt6i_flags &= ~RTF_EXPIRES; 18286f704992SYOSHIFUJI Hideaki rt->rt6i_expires = 0; 18291da177e4SLinus Torvalds } 18301da177e4SLinus Torvalds } else if (valid_lft) { 18316f704992SYOSHIFUJI Hideaki clock_t expires = 0; 18324bed72e4SYOSHIFUJI Hideaki int flags = RTF_ADDRCONF | RTF_PREFIX_RT; 18334bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) { 18346f704992SYOSHIFUJI Hideaki /* not infinity */ 18356f704992SYOSHIFUJI Hideaki flags |= RTF_EXPIRES; 18366f704992SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(rt_expires); 18376f704992SYOSHIFUJI Hideaki } 18381da177e4SLinus Torvalds addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len, 18396f704992SYOSHIFUJI Hideaki dev, expires, flags); 18401da177e4SLinus Torvalds } 18411da177e4SLinus Torvalds if (rt) 18421da177e4SLinus Torvalds dst_release(&rt->u.dst); 18431da177e4SLinus Torvalds } 18441da177e4SLinus Torvalds 18451da177e4SLinus Torvalds /* Try to figure out our local address for this prefix */ 18461da177e4SLinus Torvalds 18471da177e4SLinus Torvalds if (pinfo->autoconf && in6_dev->cnf.autoconf) { 18481da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 18491da177e4SLinus Torvalds struct in6_addr addr; 18501da177e4SLinus Torvalds int create = 0, update_lft = 0; 18511da177e4SLinus Torvalds 18521da177e4SLinus Torvalds if (pinfo->prefix_len == 64) { 18531da177e4SLinus Torvalds memcpy(&addr, &pinfo->prefix, 8); 18541da177e4SLinus Torvalds if (ipv6_generate_eui64(addr.s6_addr + 8, dev) && 18551da177e4SLinus Torvalds ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) { 18561da177e4SLinus Torvalds in6_dev_put(in6_dev); 18571da177e4SLinus Torvalds return; 18581da177e4SLinus Torvalds } 18591da177e4SLinus Torvalds goto ok; 18601da177e4SLinus Torvalds } 18611da177e4SLinus Torvalds if (net_ratelimit()) 18621da177e4SLinus Torvalds printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n", 18631da177e4SLinus Torvalds pinfo->prefix_len); 18641da177e4SLinus Torvalds in6_dev_put(in6_dev); 18651da177e4SLinus Torvalds return; 18661da177e4SLinus Torvalds 18671da177e4SLinus Torvalds ok: 18681da177e4SLinus Torvalds 186953b7997fSYOSHIFUJI Hideaki ifp = ipv6_get_ifaddr(net, &addr, dev, 1); 18701da177e4SLinus Torvalds 18711da177e4SLinus Torvalds if (ifp == NULL && valid_lft) { 18721da177e4SLinus Torvalds int max_addresses = in6_dev->cnf.max_addresses; 187395c385b4SNeil Horman u32 addr_flags = 0; 187495c385b4SNeil Horman 187595c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 187695c385b4SNeil Horman if (in6_dev->cnf.optimistic_dad && 187753b7997fSYOSHIFUJI Hideaki !net->ipv6.devconf_all->forwarding) 187895c385b4SNeil Horman addr_flags = IFA_F_OPTIMISTIC; 187995c385b4SNeil Horman #endif 18801da177e4SLinus Torvalds 18811da177e4SLinus Torvalds /* Do not allow to create too much of autoconfigured 18821da177e4SLinus Torvalds * addresses; this would be too easy way to crash kernel. 18831da177e4SLinus Torvalds */ 18841da177e4SLinus Torvalds if (!max_addresses || 18851da177e4SLinus Torvalds ipv6_count_addresses(in6_dev) < max_addresses) 18861da177e4SLinus Torvalds ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len, 188795c385b4SNeil Horman addr_type&IPV6_ADDR_SCOPE_MASK, 188895c385b4SNeil Horman addr_flags); 18891da177e4SLinus Torvalds 18901da177e4SLinus Torvalds if (!ifp || IS_ERR(ifp)) { 18911da177e4SLinus Torvalds in6_dev_put(in6_dev); 18921da177e4SLinus Torvalds return; 18931da177e4SLinus Torvalds } 18941da177e4SLinus Torvalds 18951da177e4SLinus Torvalds update_lft = create = 1; 18961da177e4SLinus Torvalds ifp->cstamp = jiffies; 18971da177e4SLinus Torvalds addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT); 18981da177e4SLinus Torvalds } 18991da177e4SLinus Torvalds 19001da177e4SLinus Torvalds if (ifp) { 19011da177e4SLinus Torvalds int flags; 19021da177e4SLinus Torvalds unsigned long now; 19031da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 19041da177e4SLinus Torvalds struct inet6_ifaddr *ift; 19051da177e4SLinus Torvalds #endif 19061da177e4SLinus Torvalds u32 stored_lft; 19071da177e4SLinus Torvalds 19081da177e4SLinus Torvalds /* update lifetime (RFC2462 5.5.3 e) */ 19091da177e4SLinus Torvalds spin_lock(&ifp->lock); 19101da177e4SLinus Torvalds now = jiffies; 19111da177e4SLinus Torvalds if (ifp->valid_lft > (now - ifp->tstamp) / HZ) 19121da177e4SLinus Torvalds stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ; 19131da177e4SLinus Torvalds else 19141da177e4SLinus Torvalds stored_lft = 0; 19151da177e4SLinus Torvalds if (!update_lft && stored_lft) { 19161da177e4SLinus Torvalds if (valid_lft > MIN_VALID_LIFETIME || 19171da177e4SLinus Torvalds valid_lft > stored_lft) 19181da177e4SLinus Torvalds update_lft = 1; 19191da177e4SLinus Torvalds else if (stored_lft <= MIN_VALID_LIFETIME) { 19201da177e4SLinus Torvalds /* valid_lft <= stored_lft is always true */ 1921a1ed0526SBrian Haley /* 1922a1ed0526SBrian Haley * RFC 4862 Section 5.5.3e: 1923a1ed0526SBrian Haley * "Note that the preferred lifetime of 1924a1ed0526SBrian Haley * the corresponding address is always 1925a1ed0526SBrian Haley * reset to the Preferred Lifetime in 1926a1ed0526SBrian Haley * the received Prefix Information 1927a1ed0526SBrian Haley * option, regardless of whether the 1928a1ed0526SBrian Haley * valid lifetime is also reset or 1929a1ed0526SBrian Haley * ignored." 1930a1ed0526SBrian Haley * 1931a1ed0526SBrian Haley * So if the preferred lifetime in 1932a1ed0526SBrian Haley * this advertisement is different 1933a1ed0526SBrian Haley * than what we have stored, but the 1934a1ed0526SBrian Haley * valid lifetime is invalid, just 1935a1ed0526SBrian Haley * reset prefered_lft. 1936a1ed0526SBrian Haley * 1937a1ed0526SBrian Haley * We must set the valid lifetime 1938a1ed0526SBrian Haley * to the stored lifetime since we'll 1939a1ed0526SBrian Haley * be updating the timestamp below, 1940a1ed0526SBrian Haley * else we'll set it back to the 1941a1ed0526SBrian Haley * minumum. 1942a1ed0526SBrian Haley */ 1943a1ed0526SBrian Haley if (prefered_lft != ifp->prefered_lft) { 1944a1ed0526SBrian Haley valid_lft = stored_lft; 1945a1ed0526SBrian Haley update_lft = 1; 1946a1ed0526SBrian Haley } 19471da177e4SLinus Torvalds } else { 19481da177e4SLinus Torvalds valid_lft = MIN_VALID_LIFETIME; 19491da177e4SLinus Torvalds if (valid_lft < prefered_lft) 19501da177e4SLinus Torvalds prefered_lft = valid_lft; 19511da177e4SLinus Torvalds update_lft = 1; 19521da177e4SLinus Torvalds } 19531da177e4SLinus Torvalds } 19541da177e4SLinus Torvalds 19551da177e4SLinus Torvalds if (update_lft) { 19561da177e4SLinus Torvalds ifp->valid_lft = valid_lft; 19571da177e4SLinus Torvalds ifp->prefered_lft = prefered_lft; 19581da177e4SLinus Torvalds ifp->tstamp = now; 19591da177e4SLinus Torvalds flags = ifp->flags; 19601da177e4SLinus Torvalds ifp->flags &= ~IFA_F_DEPRECATED; 19611da177e4SLinus Torvalds spin_unlock(&ifp->lock); 19621da177e4SLinus Torvalds 19631da177e4SLinus Torvalds if (!(flags&IFA_F_TENTATIVE)) 19641da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 19651da177e4SLinus Torvalds } else 19661da177e4SLinus Torvalds spin_unlock(&ifp->lock); 19671da177e4SLinus Torvalds 19681da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 19691da177e4SLinus Torvalds read_lock_bh(&in6_dev->lock); 19701da177e4SLinus Torvalds /* update all temporary addresses in the list */ 19711da177e4SLinus Torvalds for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) { 19721da177e4SLinus Torvalds /* 19731da177e4SLinus Torvalds * When adjusting the lifetimes of an existing 19741da177e4SLinus Torvalds * temporary address, only lower the lifetimes. 19751da177e4SLinus Torvalds * Implementations must not increase the 19761da177e4SLinus Torvalds * lifetimes of an existing temporary address 19771da177e4SLinus Torvalds * when processing a Prefix Information Option. 19781da177e4SLinus Torvalds */ 1979c6fbfac2SBenoit Boissinot if (ifp != ift->ifpub) 1980c6fbfac2SBenoit Boissinot continue; 1981c6fbfac2SBenoit Boissinot 19821da177e4SLinus Torvalds spin_lock(&ift->lock); 19831da177e4SLinus Torvalds flags = ift->flags; 19841da177e4SLinus Torvalds if (ift->valid_lft > valid_lft && 19851da177e4SLinus Torvalds ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ) 19861da177e4SLinus Torvalds ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ; 19871da177e4SLinus Torvalds if (ift->prefered_lft > prefered_lft && 19881da177e4SLinus Torvalds ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ) 19891da177e4SLinus Torvalds ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ; 19901da177e4SLinus Torvalds spin_unlock(&ift->lock); 19911da177e4SLinus Torvalds if (!(flags&IFA_F_TENTATIVE)) 19921da177e4SLinus Torvalds ipv6_ifa_notify(0, ift); 19931da177e4SLinus Torvalds } 19941da177e4SLinus Torvalds 19951da177e4SLinus Torvalds if (create && in6_dev->cnf.use_tempaddr > 0) { 19961da177e4SLinus Torvalds /* 19971da177e4SLinus Torvalds * When a new public address is created as described in [ADDRCONF], 19981da177e4SLinus Torvalds * also create a new temporary address. 19991da177e4SLinus Torvalds */ 20001da177e4SLinus Torvalds read_unlock_bh(&in6_dev->lock); 20011da177e4SLinus Torvalds ipv6_create_tempaddr(ifp, NULL); 20021da177e4SLinus Torvalds } else { 20031da177e4SLinus Torvalds read_unlock_bh(&in6_dev->lock); 20041da177e4SLinus Torvalds } 20051da177e4SLinus Torvalds #endif 20061da177e4SLinus Torvalds in6_ifa_put(ifp); 20071da177e4SLinus Torvalds addrconf_verify(0); 20081da177e4SLinus Torvalds } 20091da177e4SLinus Torvalds } 20101da177e4SLinus Torvalds inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo); 20111da177e4SLinus Torvalds in6_dev_put(in6_dev); 20121da177e4SLinus Torvalds } 20131da177e4SLinus Torvalds 20141da177e4SLinus Torvalds /* 20151da177e4SLinus Torvalds * Set destination address. 20161da177e4SLinus Torvalds * Special case for SIT interfaces where we create a new "virtual" 20171da177e4SLinus Torvalds * device. 20181da177e4SLinus Torvalds */ 2019af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg) 20201da177e4SLinus Torvalds { 20211da177e4SLinus Torvalds struct in6_ifreq ireq; 20221da177e4SLinus Torvalds struct net_device *dev; 20231da177e4SLinus Torvalds int err = -EINVAL; 20241da177e4SLinus Torvalds 20251da177e4SLinus Torvalds rtnl_lock(); 20261da177e4SLinus Torvalds 20271da177e4SLinus Torvalds err = -EFAULT; 20281da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 20291da177e4SLinus Torvalds goto err_exit; 20301da177e4SLinus Torvalds 2031af284937SDaniel Lezcano dev = __dev_get_by_index(net, ireq.ifr6_ifindex); 20321da177e4SLinus Torvalds 20331da177e4SLinus Torvalds err = -ENODEV; 20341da177e4SLinus Torvalds if (dev == NULL) 20351da177e4SLinus Torvalds goto err_exit; 20361da177e4SLinus Torvalds 20370be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 20381da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT) { 20395bc3eb7eSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 20401da177e4SLinus Torvalds struct ifreq ifr; 20411da177e4SLinus Torvalds struct ip_tunnel_parm p; 20421da177e4SLinus Torvalds 20431da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 20441da177e4SLinus Torvalds if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4)) 20451da177e4SLinus Torvalds goto err_exit; 20461da177e4SLinus Torvalds 20471da177e4SLinus Torvalds memset(&p, 0, sizeof(p)); 20481da177e4SLinus Torvalds p.iph.daddr = ireq.ifr6_addr.s6_addr32[3]; 20491da177e4SLinus Torvalds p.iph.saddr = 0; 20501da177e4SLinus Torvalds p.iph.version = 4; 20511da177e4SLinus Torvalds p.iph.ihl = 5; 20521da177e4SLinus Torvalds p.iph.protocol = IPPROTO_IPV6; 20531da177e4SLinus Torvalds p.iph.ttl = 64; 2054d20b3109SStephen Hemminger ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 20551da177e4SLinus Torvalds 20565bc3eb7eSStephen Hemminger if (ops->ndo_do_ioctl) { 20575bc3eb7eSStephen Hemminger mm_segment_t oldfs = get_fs(); 20585bc3eb7eSStephen Hemminger 20595bc3eb7eSStephen Hemminger set_fs(KERNEL_DS); 20605bc3eb7eSStephen Hemminger err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); 20611da177e4SLinus Torvalds set_fs(oldfs); 20625bc3eb7eSStephen Hemminger } else 20635bc3eb7eSStephen Hemminger err = -EOPNOTSUPP; 20641da177e4SLinus Torvalds 20651da177e4SLinus Torvalds if (err == 0) { 20661da177e4SLinus Torvalds err = -ENOBUFS; 2067af284937SDaniel Lezcano dev = __dev_get_by_name(net, p.name); 2068af284937SDaniel Lezcano if (!dev) 20691da177e4SLinus Torvalds goto err_exit; 20701da177e4SLinus Torvalds err = dev_open(dev); 20711da177e4SLinus Torvalds } 20721da177e4SLinus Torvalds } 20730be669bbSJoerg Roedel #endif 20741da177e4SLinus Torvalds 20751da177e4SLinus Torvalds err_exit: 20761da177e4SLinus Torvalds rtnl_unlock(); 20771da177e4SLinus Torvalds return err; 20781da177e4SLinus Torvalds } 20791da177e4SLinus Torvalds 20801da177e4SLinus Torvalds /* 20811da177e4SLinus Torvalds * Manual configuration of address on an interface 20821da177e4SLinus Torvalds */ 2083af284937SDaniel Lezcano static int inet6_addr_add(struct net *net, int ifindex, struct in6_addr *pfx, 208424ef0da7SThomas Graf unsigned int plen, __u8 ifa_flags, __u32 prefered_lft, 2085af284937SDaniel Lezcano __u32 valid_lft) 20861da177e4SLinus Torvalds { 20871da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 20881da177e4SLinus Torvalds struct inet6_dev *idev; 20891da177e4SLinus Torvalds struct net_device *dev; 20901da177e4SLinus Torvalds int scope; 20916f704992SYOSHIFUJI Hideaki u32 flags; 20926f704992SYOSHIFUJI Hideaki clock_t expires; 20934bed72e4SYOSHIFUJI Hideaki unsigned long timeout; 20941da177e4SLinus Torvalds 20951da177e4SLinus Torvalds ASSERT_RTNL(); 20961da177e4SLinus Torvalds 209724ef0da7SThomas Graf if (plen > 128) 209824ef0da7SThomas Graf return -EINVAL; 209924ef0da7SThomas Graf 21000778769dSNoriaki TAKAMIYA /* check the lifetime */ 21010778769dSNoriaki TAKAMIYA if (!valid_lft || prefered_lft > valid_lft) 21020778769dSNoriaki TAKAMIYA return -EINVAL; 21030778769dSNoriaki TAKAMIYA 2104af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2105af284937SDaniel Lezcano if (!dev) 21061da177e4SLinus Torvalds return -ENODEV; 21071da177e4SLinus Torvalds 21081da177e4SLinus Torvalds if ((idev = addrconf_add_dev(dev)) == NULL) 21091da177e4SLinus Torvalds return -ENOBUFS; 21101da177e4SLinus Torvalds 21111da177e4SLinus Torvalds scope = ipv6_addr_scope(pfx); 21121da177e4SLinus Torvalds 21134bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(valid_lft, HZ); 21144bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 21154bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 21164bed72e4SYOSHIFUJI Hideaki valid_lft = timeout; 21176f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 21184bed72e4SYOSHIFUJI Hideaki } else { 21194bed72e4SYOSHIFUJI Hideaki expires = 0; 21204bed72e4SYOSHIFUJI Hideaki flags = 0; 21214bed72e4SYOSHIFUJI Hideaki ifa_flags |= IFA_F_PERMANENT; 21226f704992SYOSHIFUJI Hideaki } 21230778769dSNoriaki TAKAMIYA 21244bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(prefered_lft, HZ); 21254bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 21264bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 21270778769dSNoriaki TAKAMIYA ifa_flags |= IFA_F_DEPRECATED; 21284bed72e4SYOSHIFUJI Hideaki prefered_lft = timeout; 21294bed72e4SYOSHIFUJI Hideaki } 21300778769dSNoriaki TAKAMIYA 21310778769dSNoriaki TAKAMIYA ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags); 21320778769dSNoriaki TAKAMIYA 21331da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 213406aebfb7SHerbert Xu spin_lock_bh(&ifp->lock); 21350778769dSNoriaki TAKAMIYA ifp->valid_lft = valid_lft; 21360778769dSNoriaki TAKAMIYA ifp->prefered_lft = prefered_lft; 21370778769dSNoriaki TAKAMIYA ifp->tstamp = jiffies; 213806aebfb7SHerbert Xu spin_unlock_bh(&ifp->lock); 21390778769dSNoriaki TAKAMIYA 214046d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 21416f704992SYOSHIFUJI Hideaki expires, flags); 214295c385b4SNeil Horman /* 214395c385b4SNeil Horman * Note that section 3.1 of RFC 4429 indicates 214495c385b4SNeil Horman * that the Optimistic flag should not be set for 214595c385b4SNeil Horman * manually configured addresses 214695c385b4SNeil Horman */ 21471da177e4SLinus Torvalds addrconf_dad_start(ifp, 0); 21481da177e4SLinus Torvalds in6_ifa_put(ifp); 21490778769dSNoriaki TAKAMIYA addrconf_verify(0); 21501da177e4SLinus Torvalds return 0; 21511da177e4SLinus Torvalds } 21521da177e4SLinus Torvalds 21531da177e4SLinus Torvalds return PTR_ERR(ifp); 21541da177e4SLinus Torvalds } 21551da177e4SLinus Torvalds 2156af284937SDaniel Lezcano static int inet6_addr_del(struct net *net, int ifindex, struct in6_addr *pfx, 215724ef0da7SThomas Graf unsigned int plen) 21581da177e4SLinus Torvalds { 21591da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 21601da177e4SLinus Torvalds struct inet6_dev *idev; 21611da177e4SLinus Torvalds struct net_device *dev; 21621da177e4SLinus Torvalds 216324ef0da7SThomas Graf if (plen > 128) 216424ef0da7SThomas Graf return -EINVAL; 216524ef0da7SThomas Graf 2166af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2167af284937SDaniel Lezcano if (!dev) 21681da177e4SLinus Torvalds return -ENODEV; 21691da177e4SLinus Torvalds 21701da177e4SLinus Torvalds if ((idev = __in6_dev_get(dev)) == NULL) 21711da177e4SLinus Torvalds return -ENXIO; 21721da177e4SLinus Torvalds 21731da177e4SLinus Torvalds read_lock_bh(&idev->lock); 21741da177e4SLinus Torvalds for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) { 21751da177e4SLinus Torvalds if (ifp->prefix_len == plen && 21761da177e4SLinus Torvalds ipv6_addr_equal(pfx, &ifp->addr)) { 21771da177e4SLinus Torvalds in6_ifa_hold(ifp); 21781da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 21791da177e4SLinus Torvalds 21801da177e4SLinus Torvalds ipv6_del_addr(ifp); 21811da177e4SLinus Torvalds 21821da177e4SLinus Torvalds /* If the last address is deleted administratively, 21831da177e4SLinus Torvalds disable IPv6 on this interface. 21841da177e4SLinus Torvalds */ 21851da177e4SLinus Torvalds if (idev->addr_list == NULL) 21861da177e4SLinus Torvalds addrconf_ifdown(idev->dev, 1); 21871da177e4SLinus Torvalds return 0; 21881da177e4SLinus Torvalds } 21891da177e4SLinus Torvalds } 21901da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 21911da177e4SLinus Torvalds return -EADDRNOTAVAIL; 21921da177e4SLinus Torvalds } 21931da177e4SLinus Torvalds 21941da177e4SLinus Torvalds 2195af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg) 21961da177e4SLinus Torvalds { 21971da177e4SLinus Torvalds struct in6_ifreq ireq; 21981da177e4SLinus Torvalds int err; 21991da177e4SLinus Torvalds 22001da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 22011da177e4SLinus Torvalds return -EPERM; 22021da177e4SLinus Torvalds 22031da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 22041da177e4SLinus Torvalds return -EFAULT; 22051da177e4SLinus Torvalds 22061da177e4SLinus Torvalds rtnl_lock(); 2207af284937SDaniel Lezcano err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, 2208af284937SDaniel Lezcano ireq.ifr6_prefixlen, IFA_F_PERMANENT, 2209af284937SDaniel Lezcano INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 22101da177e4SLinus Torvalds rtnl_unlock(); 22111da177e4SLinus Torvalds return err; 22121da177e4SLinus Torvalds } 22131da177e4SLinus Torvalds 2214af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg) 22151da177e4SLinus Torvalds { 22161da177e4SLinus Torvalds struct in6_ifreq ireq; 22171da177e4SLinus Torvalds int err; 22181da177e4SLinus Torvalds 22191da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 22201da177e4SLinus Torvalds return -EPERM; 22211da177e4SLinus Torvalds 22221da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 22231da177e4SLinus Torvalds return -EFAULT; 22241da177e4SLinus Torvalds 22251da177e4SLinus Torvalds rtnl_lock(); 2226af284937SDaniel Lezcano err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, 2227af284937SDaniel Lezcano ireq.ifr6_prefixlen); 22281da177e4SLinus Torvalds rtnl_unlock(); 22291da177e4SLinus Torvalds return err; 22301da177e4SLinus Torvalds } 22311da177e4SLinus Torvalds 2232b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr, 2233b5f348e5SIlpo Järvinen int plen, int scope) 2234b5f348e5SIlpo Järvinen { 2235b5f348e5SIlpo Järvinen struct inet6_ifaddr *ifp; 2236b5f348e5SIlpo Järvinen 2237b5f348e5SIlpo Järvinen ifp = ipv6_add_addr(idev, addr, plen, scope, IFA_F_PERMANENT); 2238b5f348e5SIlpo Järvinen if (!IS_ERR(ifp)) { 2239b5f348e5SIlpo Järvinen spin_lock_bh(&ifp->lock); 2240b5f348e5SIlpo Järvinen ifp->flags &= ~IFA_F_TENTATIVE; 2241b5f348e5SIlpo Järvinen spin_unlock_bh(&ifp->lock); 2242b5f348e5SIlpo Järvinen ipv6_ifa_notify(RTM_NEWADDR, ifp); 2243b5f348e5SIlpo Järvinen in6_ifa_put(ifp); 2244b5f348e5SIlpo Järvinen } 2245b5f348e5SIlpo Järvinen } 2246b5f348e5SIlpo Järvinen 22470be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 22481da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev) 22491da177e4SLinus Torvalds { 22501da177e4SLinus Torvalds struct in6_addr addr; 22511da177e4SLinus Torvalds struct net_device *dev; 2252c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 22531da177e4SLinus Torvalds int scope; 22541da177e4SLinus Torvalds 22551da177e4SLinus Torvalds ASSERT_RTNL(); 22561da177e4SLinus Torvalds 22571da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr)); 22581da177e4SLinus Torvalds memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4); 22591da177e4SLinus Torvalds 22601da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) { 22611da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xfe800000); 22621da177e4SLinus Torvalds scope = IFA_LINK; 22631da177e4SLinus Torvalds } else { 22641da177e4SLinus Torvalds scope = IPV6_ADDR_COMPATv4; 22651da177e4SLinus Torvalds } 22661da177e4SLinus Torvalds 22671da177e4SLinus Torvalds if (addr.s6_addr32[3]) { 2268b5f348e5SIlpo Järvinen add_addr(idev, &addr, 128, scope); 22691da177e4SLinus Torvalds return; 22701da177e4SLinus Torvalds } 22711da177e4SLinus Torvalds 22726fda7350SBenjamin Thery for_each_netdev(net, dev) { 2273e5ed6399SHerbert Xu struct in_device * in_dev = __in_dev_get_rtnl(dev); 22741da177e4SLinus Torvalds if (in_dev && (dev->flags & IFF_UP)) { 22751da177e4SLinus Torvalds struct in_ifaddr * ifa; 22761da177e4SLinus Torvalds 22771da177e4SLinus Torvalds int flag = scope; 22781da177e4SLinus Torvalds 22791da177e4SLinus Torvalds for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { 22801da177e4SLinus Torvalds int plen; 22811da177e4SLinus Torvalds 22821da177e4SLinus Torvalds addr.s6_addr32[3] = ifa->ifa_local; 22831da177e4SLinus Torvalds 22841da177e4SLinus Torvalds if (ifa->ifa_scope == RT_SCOPE_LINK) 22851da177e4SLinus Torvalds continue; 22861da177e4SLinus Torvalds if (ifa->ifa_scope >= RT_SCOPE_HOST) { 22871da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) 22881da177e4SLinus Torvalds continue; 22891da177e4SLinus Torvalds flag |= IFA_HOST; 22901da177e4SLinus Torvalds } 22911da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) 22921da177e4SLinus Torvalds plen = 64; 22931da177e4SLinus Torvalds else 22941da177e4SLinus Torvalds plen = 96; 22951da177e4SLinus Torvalds 2296b5f348e5SIlpo Järvinen add_addr(idev, &addr, plen, flag); 22971da177e4SLinus Torvalds } 22981da177e4SLinus Torvalds } 22991da177e4SLinus Torvalds } 23001da177e4SLinus Torvalds } 23010be669bbSJoerg Roedel #endif 23021da177e4SLinus Torvalds 23031da177e4SLinus Torvalds static void init_loopback(struct net_device *dev) 23041da177e4SLinus Torvalds { 23051da177e4SLinus Torvalds struct inet6_dev *idev; 23061da177e4SLinus Torvalds 23071da177e4SLinus Torvalds /* ::1 */ 23081da177e4SLinus Torvalds 23091da177e4SLinus Torvalds ASSERT_RTNL(); 23101da177e4SLinus Torvalds 23111da177e4SLinus Torvalds if ((idev = ipv6_find_idev(dev)) == NULL) { 23121da177e4SLinus Torvalds printk(KERN_DEBUG "init loopback: add_dev failed\n"); 23131da177e4SLinus Torvalds return; 23141da177e4SLinus Torvalds } 23151da177e4SLinus Torvalds 2316b5f348e5SIlpo Järvinen add_addr(idev, &in6addr_loopback, 128, IFA_HOST); 23171da177e4SLinus Torvalds } 23181da177e4SLinus Torvalds 23191da177e4SLinus Torvalds static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr) 23201da177e4SLinus Torvalds { 23211da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 232295c385b4SNeil Horman u32 addr_flags = IFA_F_PERMANENT; 23231da177e4SLinus Torvalds 232495c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 232595c385b4SNeil Horman if (idev->cnf.optimistic_dad && 2326702beb87SDavid Miller !dev_net(idev->dev)->ipv6.devconf_all->forwarding) 232795c385b4SNeil Horman addr_flags |= IFA_F_OPTIMISTIC; 232895c385b4SNeil Horman #endif 232995c385b4SNeil Horman 233095c385b4SNeil Horman 233195c385b4SNeil Horman ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags); 23321da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 233346d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0); 23341da177e4SLinus Torvalds addrconf_dad_start(ifp, 0); 23351da177e4SLinus Torvalds in6_ifa_put(ifp); 23361da177e4SLinus Torvalds } 23371da177e4SLinus Torvalds } 23381da177e4SLinus Torvalds 23391da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev) 23401da177e4SLinus Torvalds { 23411da177e4SLinus Torvalds struct in6_addr addr; 23421da177e4SLinus Torvalds struct inet6_dev * idev; 23431da177e4SLinus Torvalds 23441da177e4SLinus Torvalds ASSERT_RTNL(); 23451da177e4SLinus Torvalds 234674235a25SHerbert Xu if ((dev->type != ARPHRD_ETHER) && 234774235a25SHerbert Xu (dev->type != ARPHRD_FDDI) && 234874235a25SHerbert Xu (dev->type != ARPHRD_IEEE802_TR) && 234974235a25SHerbert Xu (dev->type != ARPHRD_ARCNET) && 235074235a25SHerbert Xu (dev->type != ARPHRD_INFINIBAND)) { 235174235a25SHerbert Xu /* Alas, we support only Ethernet autoconfiguration. */ 235274235a25SHerbert Xu return; 235374235a25SHerbert Xu } 235474235a25SHerbert Xu 23551da177e4SLinus Torvalds idev = addrconf_add_dev(dev); 23561da177e4SLinus Torvalds if (idev == NULL) 23571da177e4SLinus Torvalds return; 23581da177e4SLinus Torvalds 23591da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr)); 23601da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xFE800000); 23611da177e4SLinus Torvalds 23621da177e4SLinus Torvalds if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0) 23631da177e4SLinus Torvalds addrconf_add_linklocal(idev, &addr); 23641da177e4SLinus Torvalds } 23651da177e4SLinus Torvalds 23660be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 23671da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev) 23681da177e4SLinus Torvalds { 23691da177e4SLinus Torvalds struct inet6_dev *idev; 23701da177e4SLinus Torvalds 23711da177e4SLinus Torvalds ASSERT_RTNL(); 23721da177e4SLinus Torvalds 23731da177e4SLinus Torvalds /* 23741da177e4SLinus Torvalds * Configure the tunnel with one of our IPv4 23751da177e4SLinus Torvalds * addresses... we should configure all of 23761da177e4SLinus Torvalds * our v4 addrs in the tunnel 23771da177e4SLinus Torvalds */ 23781da177e4SLinus Torvalds 23791da177e4SLinus Torvalds if ((idev = ipv6_find_idev(dev)) == NULL) { 23801da177e4SLinus Torvalds printk(KERN_DEBUG "init sit: add_dev failed\n"); 23811da177e4SLinus Torvalds return; 23821da177e4SLinus Torvalds } 23831da177e4SLinus Torvalds 2384c7dc89c0SFred L. Templin if (dev->priv_flags & IFF_ISATAP) { 2385c7dc89c0SFred L. Templin struct in6_addr addr; 2386c7dc89c0SFred L. Templin 2387c7dc89c0SFred L. Templin ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 2388c7dc89c0SFred L. Templin addrconf_prefix_route(&addr, 64, dev, 0, 0); 2389c7dc89c0SFred L. Templin if (!ipv6_generate_eui64(addr.s6_addr + 8, dev)) 2390c7dc89c0SFred L. Templin addrconf_add_linklocal(idev, &addr); 2391c7dc89c0SFred L. Templin return; 2392c7dc89c0SFred L. Templin } 2393c7dc89c0SFred L. Templin 23941da177e4SLinus Torvalds sit_add_v4_addrs(idev); 23951da177e4SLinus Torvalds 23961da177e4SLinus Torvalds if (dev->flags&IFF_POINTOPOINT) { 23971da177e4SLinus Torvalds addrconf_add_mroute(dev); 23981da177e4SLinus Torvalds addrconf_add_lroute(dev); 23991da177e4SLinus Torvalds } else 24001da177e4SLinus Torvalds sit_route_add(dev); 24011da177e4SLinus Torvalds } 24020be669bbSJoerg Roedel #endif 24031da177e4SLinus Torvalds 24041da177e4SLinus Torvalds static inline int 24051da177e4SLinus Torvalds ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev) 24061da177e4SLinus Torvalds { 24071da177e4SLinus Torvalds struct in6_addr lladdr; 24081da177e4SLinus Torvalds 240995c385b4SNeil Horman if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) { 24101da177e4SLinus Torvalds addrconf_add_linklocal(idev, &lladdr); 24111da177e4SLinus Torvalds return 0; 24121da177e4SLinus Torvalds } 24131da177e4SLinus Torvalds return -1; 24141da177e4SLinus Torvalds } 24151da177e4SLinus Torvalds 24161da177e4SLinus Torvalds static void ip6_tnl_add_linklocal(struct inet6_dev *idev) 24171da177e4SLinus Torvalds { 24181da177e4SLinus Torvalds struct net_device *link_dev; 2419c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 24201da177e4SLinus Torvalds 24211da177e4SLinus Torvalds /* first try to inherit the link-local address from the link device */ 24221da177e4SLinus Torvalds if (idev->dev->iflink && 24236fda7350SBenjamin Thery (link_dev = __dev_get_by_index(net, idev->dev->iflink))) { 24241da177e4SLinus Torvalds if (!ipv6_inherit_linklocal(idev, link_dev)) 24251da177e4SLinus Torvalds return; 24261da177e4SLinus Torvalds } 24271da177e4SLinus Torvalds /* then try to inherit it from any device */ 24286fda7350SBenjamin Thery for_each_netdev(net, link_dev) { 24291da177e4SLinus Torvalds if (!ipv6_inherit_linklocal(idev, link_dev)) 24301da177e4SLinus Torvalds return; 24311da177e4SLinus Torvalds } 24321da177e4SLinus Torvalds printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n"); 24331da177e4SLinus Torvalds } 24341da177e4SLinus Torvalds 24351da177e4SLinus Torvalds /* 24361da177e4SLinus Torvalds * Autoconfigure tunnel with a link-local address so routing protocols, 24371da177e4SLinus Torvalds * DHCPv6, MLD etc. can be run over the virtual link 24381da177e4SLinus Torvalds */ 24391da177e4SLinus Torvalds 24401da177e4SLinus Torvalds static void addrconf_ip6_tnl_config(struct net_device *dev) 24411da177e4SLinus Torvalds { 24421da177e4SLinus Torvalds struct inet6_dev *idev; 24431da177e4SLinus Torvalds 24441da177e4SLinus Torvalds ASSERT_RTNL(); 24451da177e4SLinus Torvalds 24461da177e4SLinus Torvalds if ((idev = addrconf_add_dev(dev)) == NULL) { 24471da177e4SLinus Torvalds printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n"); 24481da177e4SLinus Torvalds return; 24491da177e4SLinus Torvalds } 24501da177e4SLinus Torvalds ip6_tnl_add_linklocal(idev); 24511da177e4SLinus Torvalds } 24521da177e4SLinus Torvalds 24531da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event, 24541da177e4SLinus Torvalds void * data) 24551da177e4SLinus Torvalds { 24561da177e4SLinus Torvalds struct net_device *dev = (struct net_device *) data; 245774235a25SHerbert Xu struct inet6_dev *idev = __in6_dev_get(dev); 2458c5e33bddSYOSHIFUJI Hideaki int run_pending = 0; 2459b217d616SHerbert Xu int err; 24601da177e4SLinus Torvalds 24611da177e4SLinus Torvalds switch(event) { 246245ba9dd2SYOSHIFUJI Hideaki case NETDEV_REGISTER: 246374235a25SHerbert Xu if (!idev && dev->mtu >= IPV6_MIN_MTU) { 246445ba9dd2SYOSHIFUJI Hideaki idev = ipv6_add_dev(dev); 246545ba9dd2SYOSHIFUJI Hideaki if (!idev) 2466b217d616SHerbert Xu return notifier_from_errno(-ENOMEM); 246745ba9dd2SYOSHIFUJI Hideaki } 246845ba9dd2SYOSHIFUJI Hideaki break; 24691da177e4SLinus Torvalds case NETDEV_UP: 24703c21edbdSYOSHIFUJI Hideaki case NETDEV_CHANGE: 2471c2edacf8SJay Vosburgh if (dev->flags & IFF_SLAVE) 2472c2edacf8SJay Vosburgh break; 2473c2edacf8SJay Vosburgh 24743c21edbdSYOSHIFUJI Hideaki if (event == NETDEV_UP) { 2475f24e3d65SMitsuru Chinen if (!addrconf_qdisc_ok(dev)) { 24763c21edbdSYOSHIFUJI Hideaki /* device is not ready yet. */ 24773c21edbdSYOSHIFUJI Hideaki printk(KERN_INFO 24783c21edbdSYOSHIFUJI Hideaki "ADDRCONF(NETDEV_UP): %s: " 24793c21edbdSYOSHIFUJI Hideaki "link is not ready\n", 24803c21edbdSYOSHIFUJI Hideaki dev->name); 24813c21edbdSYOSHIFUJI Hideaki break; 24823c21edbdSYOSHIFUJI Hideaki } 248399081049SKristian Slavov 2484d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU) 2485d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev); 2486d31c7b8fSEvgeniy Polyakov 2487e3ec6cfcSBenjamin Thery if (idev) { 248899081049SKristian Slavov idev->if_flags |= IF_READY; 2489e3ec6cfcSBenjamin Thery run_pending = 1; 2490e3ec6cfcSBenjamin Thery } 24913c21edbdSYOSHIFUJI Hideaki } else { 2492f24e3d65SMitsuru Chinen if (!addrconf_qdisc_ok(dev)) { 24933c21edbdSYOSHIFUJI Hideaki /* device is still not ready. */ 24943c21edbdSYOSHIFUJI Hideaki break; 24953c21edbdSYOSHIFUJI Hideaki } 24963c21edbdSYOSHIFUJI Hideaki 24973c21edbdSYOSHIFUJI Hideaki if (idev) { 24983c21edbdSYOSHIFUJI Hideaki if (idev->if_flags & IF_READY) { 24993c21edbdSYOSHIFUJI Hideaki /* device is already configured. */ 25003c21edbdSYOSHIFUJI Hideaki break; 25013c21edbdSYOSHIFUJI Hideaki } 25023c21edbdSYOSHIFUJI Hideaki idev->if_flags |= IF_READY; 25033c21edbdSYOSHIFUJI Hideaki } 25043c21edbdSYOSHIFUJI Hideaki 25053c21edbdSYOSHIFUJI Hideaki printk(KERN_INFO 25063c21edbdSYOSHIFUJI Hideaki "ADDRCONF(NETDEV_CHANGE): %s: " 25073c21edbdSYOSHIFUJI Hideaki "link becomes ready\n", 25083c21edbdSYOSHIFUJI Hideaki dev->name); 25093c21edbdSYOSHIFUJI Hideaki 2510c5e33bddSYOSHIFUJI Hideaki run_pending = 1; 25113c21edbdSYOSHIFUJI Hideaki } 25123c21edbdSYOSHIFUJI Hideaki 25131da177e4SLinus Torvalds switch(dev->type) { 25140be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 25151da177e4SLinus Torvalds case ARPHRD_SIT: 25161da177e4SLinus Torvalds addrconf_sit_config(dev); 25171da177e4SLinus Torvalds break; 25180be669bbSJoerg Roedel #endif 25191da177e4SLinus Torvalds case ARPHRD_TUNNEL6: 25201da177e4SLinus Torvalds addrconf_ip6_tnl_config(dev); 25211da177e4SLinus Torvalds break; 25221da177e4SLinus Torvalds case ARPHRD_LOOPBACK: 25231da177e4SLinus Torvalds init_loopback(dev); 25241da177e4SLinus Torvalds break; 25251da177e4SLinus Torvalds 25261da177e4SLinus Torvalds default: 25271da177e4SLinus Torvalds addrconf_dev_config(dev); 25281da177e4SLinus Torvalds break; 25293ff50b79SStephen Hemminger } 25301da177e4SLinus Torvalds if (idev) { 2531c5e33bddSYOSHIFUJI Hideaki if (run_pending) 2532c5e33bddSYOSHIFUJI Hideaki addrconf_dad_run(idev); 2533c5e33bddSYOSHIFUJI Hideaki 25341da177e4SLinus Torvalds /* If the MTU changed during the interface down, when the 25351da177e4SLinus Torvalds interface up, the changed MTU must be reflected in the 25361da177e4SLinus Torvalds idev as well as routers. 25371da177e4SLinus Torvalds */ 25381da177e4SLinus Torvalds if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) { 25391da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 25401da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 25411da177e4SLinus Torvalds } 25421da177e4SLinus Torvalds idev->tstamp = jiffies; 25431da177e4SLinus Torvalds inet6_ifinfo_notify(RTM_NEWLINK, idev); 25441da177e4SLinus Torvalds /* If the changed mtu during down is lower than IPV6_MIN_MTU 25451da177e4SLinus Torvalds stop IPv6 on this interface. 25461da177e4SLinus Torvalds */ 25471da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 25481da177e4SLinus Torvalds addrconf_ifdown(dev, event != NETDEV_DOWN); 25491da177e4SLinus Torvalds } 25501da177e4SLinus Torvalds break; 25511da177e4SLinus Torvalds 25521da177e4SLinus Torvalds case NETDEV_CHANGEMTU: 25531da177e4SLinus Torvalds if (idev && dev->mtu >= IPV6_MIN_MTU) { 25541da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 25551da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 25561da177e4SLinus Torvalds break; 25571da177e4SLinus Torvalds } 25581da177e4SLinus Torvalds 2559d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU) { 2560d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev); 2561d31c7b8fSEvgeniy Polyakov if (idev) 2562d31c7b8fSEvgeniy Polyakov break; 2563d31c7b8fSEvgeniy Polyakov } 2564d31c7b8fSEvgeniy Polyakov 25651da177e4SLinus Torvalds /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */ 25661da177e4SLinus Torvalds 25671da177e4SLinus Torvalds case NETDEV_DOWN: 25681da177e4SLinus Torvalds case NETDEV_UNREGISTER: 25691da177e4SLinus Torvalds /* 25701da177e4SLinus Torvalds * Remove all addresses from this interface. 25711da177e4SLinus Torvalds */ 25721da177e4SLinus Torvalds addrconf_ifdown(dev, event != NETDEV_DOWN); 25731da177e4SLinus Torvalds break; 25743c21edbdSYOSHIFUJI Hideaki 25751da177e4SLinus Torvalds case NETDEV_CHANGENAME: 25761da177e4SLinus Torvalds if (idev) { 25775632c515SStephen Hemminger snmp6_unregister_dev(idev); 2578408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 2579f52295a9SPavel Emelyanov addrconf_sysctl_register(idev); 2580b217d616SHerbert Xu err = snmp6_register_dev(idev); 2581b217d616SHerbert Xu if (err) 2582b217d616SHerbert Xu return notifier_from_errno(err); 25835632c515SStephen Hemminger } 25841da177e4SLinus Torvalds break; 258575c78500SMoni Shoua case NETDEV_BONDING_OLDTYPE: 258675c78500SMoni Shoua case NETDEV_BONDING_NEWTYPE: 258775c78500SMoni Shoua addrconf_bonding_change(dev, event); 258875c78500SMoni Shoua break; 25893ff50b79SStephen Hemminger } 25901da177e4SLinus Torvalds 25911da177e4SLinus Torvalds return NOTIFY_OK; 25921da177e4SLinus Torvalds } 25931da177e4SLinus Torvalds 25941da177e4SLinus Torvalds /* 25951da177e4SLinus Torvalds * addrconf module should be notified of a device going up 25961da177e4SLinus Torvalds */ 25971da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = { 25981da177e4SLinus Torvalds .notifier_call = addrconf_notify, 25991da177e4SLinus Torvalds .priority = 0 26001da177e4SLinus Torvalds }; 26011da177e4SLinus Torvalds 260275c78500SMoni Shoua static void addrconf_bonding_change(struct net_device *dev, unsigned long event) 260375c78500SMoni Shoua { 260475c78500SMoni Shoua struct inet6_dev *idev; 260575c78500SMoni Shoua ASSERT_RTNL(); 260675c78500SMoni Shoua 260775c78500SMoni Shoua idev = __in6_dev_get(dev); 260875c78500SMoni Shoua 260975c78500SMoni Shoua if (event == NETDEV_BONDING_NEWTYPE) 261075c78500SMoni Shoua ipv6_mc_remap(idev); 261175c78500SMoni Shoua else if (event == NETDEV_BONDING_OLDTYPE) 261275c78500SMoni Shoua ipv6_mc_unmap(idev); 261375c78500SMoni Shoua } 261475c78500SMoni Shoua 26151da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how) 26161da177e4SLinus Torvalds { 26171da177e4SLinus Torvalds struct inet6_dev *idev; 2618*8f37ada5Sstephen hemminger struct inet6_ifaddr *ifa, *keep_list, **bifa; 2619c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 26201da177e4SLinus Torvalds int i; 26211da177e4SLinus Torvalds 26221da177e4SLinus Torvalds ASSERT_RTNL(); 26231da177e4SLinus Torvalds 2624f3db4851SDaniel Lezcano rt6_ifdown(net, dev); 26251da177e4SLinus Torvalds neigh_ifdown(&nd_tbl, dev); 26261da177e4SLinus Torvalds 26271da177e4SLinus Torvalds idev = __in6_dev_get(dev); 26281da177e4SLinus Torvalds if (idev == NULL) 26291da177e4SLinus Torvalds return -ENODEV; 26301da177e4SLinus Torvalds 26311da177e4SLinus Torvalds /* Step 1: remove reference to ipv6 device from parent device. 26321da177e4SLinus Torvalds Do not dev_put! 26331da177e4SLinus Torvalds */ 2634439e2385SDenis V. Lunev if (how) { 26351da177e4SLinus Torvalds idev->dead = 1; 26368814c4b5SYOSHIFUJI Hideaki 26378814c4b5SYOSHIFUJI Hideaki /* protected by rtnl_lock */ 26388814c4b5SYOSHIFUJI Hideaki rcu_assign_pointer(dev->ip6_ptr, NULL); 26391da177e4SLinus Torvalds 26401da177e4SLinus Torvalds /* Step 1.5: remove snmp6 entry */ 26411da177e4SLinus Torvalds snmp6_unregister_dev(idev); 26421da177e4SLinus Torvalds 26431da177e4SLinus Torvalds } 26441da177e4SLinus Torvalds 26451da177e4SLinus Torvalds /* Step 2: clear hash table */ 26461da177e4SLinus Torvalds for (i=0; i<IN6_ADDR_HSIZE; i++) { 26471da177e4SLinus Torvalds bifa = &inet6_addr_lst[i]; 26481da177e4SLinus Torvalds 26491da177e4SLinus Torvalds write_lock_bh(&addrconf_hash_lock); 26501da177e4SLinus Torvalds while ((ifa = *bifa) != NULL) { 2651dc2b99f7Sstephen hemminger if (ifa->idev == idev && 265284e8b803Sstephen hemminger (how || !(ifa->flags&IFA_F_PERMANENT) || 265384e8b803Sstephen hemminger ipv6_addr_type(&ifa->addr) & IPV6_ADDR_LINKLOCAL)) { 26541da177e4SLinus Torvalds *bifa = ifa->lst_next; 26551da177e4SLinus Torvalds ifa->lst_next = NULL; 265684e8b803Sstephen hemminger __in6_ifa_put(ifa); 26571da177e4SLinus Torvalds continue; 26581da177e4SLinus Torvalds } 26591da177e4SLinus Torvalds bifa = &ifa->lst_next; 26601da177e4SLinus Torvalds } 26611da177e4SLinus Torvalds write_unlock_bh(&addrconf_hash_lock); 26621da177e4SLinus Torvalds } 26631da177e4SLinus Torvalds 26641da177e4SLinus Torvalds write_lock_bh(&idev->lock); 26651da177e4SLinus Torvalds 26661da177e4SLinus Torvalds /* Step 3: clear flags for stateless addrconf */ 2667439e2385SDenis V. Lunev if (!how) 26683c21edbdSYOSHIFUJI Hideaki idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); 26691da177e4SLinus Torvalds 26701da177e4SLinus Torvalds /* Step 4: clear address list */ 26711da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 2672439e2385SDenis V. Lunev if (how && del_timer(&idev->regen_timer)) 26731da177e4SLinus Torvalds in6_dev_put(idev); 26741da177e4SLinus Torvalds 26751da177e4SLinus Torvalds /* clear tempaddr list */ 26761da177e4SLinus Torvalds while ((ifa = idev->tempaddr_list) != NULL) { 26771da177e4SLinus Torvalds idev->tempaddr_list = ifa->tmp_next; 26781da177e4SLinus Torvalds ifa->tmp_next = NULL; 26791da177e4SLinus Torvalds ifa->dead = 1; 26801da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 26811da177e4SLinus Torvalds spin_lock_bh(&ifa->lock); 26821da177e4SLinus Torvalds 26831da177e4SLinus Torvalds if (ifa->ifpub) { 26841da177e4SLinus Torvalds in6_ifa_put(ifa->ifpub); 26851da177e4SLinus Torvalds ifa->ifpub = NULL; 26861da177e4SLinus Torvalds } 26871da177e4SLinus Torvalds spin_unlock_bh(&ifa->lock); 26881da177e4SLinus Torvalds in6_ifa_put(ifa); 26891da177e4SLinus Torvalds write_lock_bh(&idev->lock); 26901da177e4SLinus Torvalds } 26911da177e4SLinus Torvalds #endif 2692*8f37ada5Sstephen hemminger keep_list = NULL; 2693*8f37ada5Sstephen hemminger bifa = &keep_list; 2694*8f37ada5Sstephen hemminger while ((ifa = idev->addr_list) != NULL) { 2695*8f37ada5Sstephen hemminger idev->addr_list = ifa->if_next; 2696*8f37ada5Sstephen hemminger ifa->if_next = NULL; 2697*8f37ada5Sstephen hemminger 269884e8b803Sstephen hemminger addrconf_del_timer(ifa); 269984e8b803Sstephen hemminger 270084e8b803Sstephen hemminger /* If just doing link down, and address is permanent 270184e8b803Sstephen hemminger and not link-local, then retain it. */ 270284e8b803Sstephen hemminger if (how == 0 && 270384e8b803Sstephen hemminger (ifa->flags&IFA_F_PERMANENT) && 270484e8b803Sstephen hemminger !(ipv6_addr_type(&ifa->addr) & IPV6_ADDR_LINKLOCAL)) { 2705*8f37ada5Sstephen hemminger 2706*8f37ada5Sstephen hemminger /* Move to holding list */ 2707*8f37ada5Sstephen hemminger *bifa = ifa; 2708dc2b99f7Sstephen hemminger bifa = &ifa->if_next; 2709dc2b99f7Sstephen hemminger 271084e8b803Sstephen hemminger /* If not doing DAD on this address, just keep it. */ 271184e8b803Sstephen hemminger if ((dev->flags&(IFF_NOARP|IFF_LOOPBACK)) || 2712dc2b99f7Sstephen hemminger idev->cnf.accept_dad <= 0 || 271384e8b803Sstephen hemminger (ifa->flags & IFA_F_NODAD)) 271484e8b803Sstephen hemminger continue; 271584e8b803Sstephen hemminger 271684e8b803Sstephen hemminger /* If it was tentative already, no need to notify */ 271784e8b803Sstephen hemminger if (ifa->flags & IFA_F_TENTATIVE) 271884e8b803Sstephen hemminger continue; 271984e8b803Sstephen hemminger 272084e8b803Sstephen hemminger /* Flag it for later restoration when link comes up */ 2721dc2b99f7Sstephen hemminger ifa->flags |= IFA_F_TENTATIVE; 272284e8b803Sstephen hemminger in6_ifa_hold(ifa); 2723dc2b99f7Sstephen hemminger } else { 27241da177e4SLinus Torvalds ifa->dead = 1; 272584e8b803Sstephen hemminger } 27261da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 27271da177e4SLinus Torvalds 27281da177e4SLinus Torvalds __ipv6_ifa_notify(RTM_DELADDR, ifa); 2729063ed369SVlad Yasevich atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa); 27301da177e4SLinus Torvalds in6_ifa_put(ifa); 27311da177e4SLinus Torvalds 27321da177e4SLinus Torvalds write_lock_bh(&idev->lock); 27331da177e4SLinus Torvalds } 2734*8f37ada5Sstephen hemminger 2735*8f37ada5Sstephen hemminger idev->addr_list = keep_list; 2736*8f37ada5Sstephen hemminger 27371da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 27381da177e4SLinus Torvalds 27391da177e4SLinus Torvalds /* Step 5: Discard multicast list */ 27401da177e4SLinus Torvalds 2741439e2385SDenis V. Lunev if (how) 27421da177e4SLinus Torvalds ipv6_mc_destroy_dev(idev); 27431da177e4SLinus Torvalds else 27441da177e4SLinus Torvalds ipv6_mc_down(idev); 27451da177e4SLinus Torvalds 27461da177e4SLinus Torvalds idev->tstamp = jiffies; 27471da177e4SLinus Torvalds 27481da177e4SLinus Torvalds /* Shot the device (if unregistered) */ 27491da177e4SLinus Torvalds 2750439e2385SDenis V. Lunev if (how) { 2751408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 27521da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, idev->nd_parms); 27531da177e4SLinus Torvalds neigh_ifdown(&nd_tbl, dev); 27541da177e4SLinus Torvalds in6_dev_put(idev); 27551da177e4SLinus Torvalds } 27561da177e4SLinus Torvalds return 0; 27571da177e4SLinus Torvalds } 27581da177e4SLinus Torvalds 27591da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data) 27601da177e4SLinus Torvalds { 27611da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data; 27625b2a1953Sstephen hemminger struct inet6_dev *idev = ifp->idev; 27631da177e4SLinus Torvalds 27645b2a1953Sstephen hemminger read_lock(&idev->lock); 27655b2a1953Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) 27661da177e4SLinus Torvalds goto out; 27671da177e4SLinus Torvalds 27685b2a1953Sstephen hemminger if (idev->cnf.forwarding) 27691da177e4SLinus Torvalds goto out; 27705b2a1953Sstephen hemminger 27715b2a1953Sstephen hemminger /* Announcement received after solicitation was sent */ 27725b2a1953Sstephen hemminger if (idev->if_flags & IF_RA_RCVD) 27735b2a1953Sstephen hemminger goto out; 27741da177e4SLinus Torvalds 27751da177e4SLinus Torvalds spin_lock(&ifp->lock); 27765b2a1953Sstephen hemminger if (ifp->probes++ < idev->cnf.rtr_solicits) { 27771da177e4SLinus Torvalds /* The wait after the last probe can be shorter */ 27781da177e4SLinus Torvalds addrconf_mod_timer(ifp, AC_RS, 27795b2a1953Sstephen hemminger (ifp->probes == idev->cnf.rtr_solicits) ? 27805b2a1953Sstephen hemminger idev->cnf.rtr_solicit_delay : 27815b2a1953Sstephen hemminger idev->cnf.rtr_solicit_interval); 27821da177e4SLinus Torvalds spin_unlock(&ifp->lock); 27831da177e4SLinus Torvalds 27845b2a1953Sstephen hemminger ndisc_send_rs(idev->dev, &ifp->addr, &in6addr_linklocal_allrouters); 27851da177e4SLinus Torvalds } else { 27861da177e4SLinus Torvalds spin_unlock(&ifp->lock); 27871da177e4SLinus Torvalds /* 27881da177e4SLinus Torvalds * Note: we do not support deprecated "all on-link" 27891da177e4SLinus Torvalds * assumption any longer. 27901da177e4SLinus Torvalds */ 27911da177e4SLinus Torvalds printk(KERN_DEBUG "%s: no IPv6 routers present\n", 27925b2a1953Sstephen hemminger idev->dev->name); 27931da177e4SLinus Torvalds } 27941da177e4SLinus Torvalds 27951da177e4SLinus Torvalds out: 27965b2a1953Sstephen hemminger read_unlock(&idev->lock); 27971da177e4SLinus Torvalds in6_ifa_put(ifp); 27981da177e4SLinus Torvalds } 27991da177e4SLinus Torvalds 28001da177e4SLinus Torvalds /* 28011da177e4SLinus Torvalds * Duplicate Address Detection 28021da177e4SLinus Torvalds */ 28033c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp) 28043c21edbdSYOSHIFUJI Hideaki { 28053c21edbdSYOSHIFUJI Hideaki unsigned long rand_num; 28063c21edbdSYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 28073c21edbdSYOSHIFUJI Hideaki 280895c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 280995c385b4SNeil Horman rand_num = 0; 281095c385b4SNeil Horman else 28113c21edbdSYOSHIFUJI Hideaki rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1); 281295c385b4SNeil Horman 28133c21edbdSYOSHIFUJI Hideaki ifp->probes = idev->cnf.dad_transmits; 28143c21edbdSYOSHIFUJI Hideaki addrconf_mod_timer(ifp, AC_DAD, rand_num); 28153c21edbdSYOSHIFUJI Hideaki } 28163c21edbdSYOSHIFUJI Hideaki 2817e431b8c0SJamal Hadi Salim static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags) 28181da177e4SLinus Torvalds { 28191da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 28201da177e4SLinus Torvalds struct net_device *dev = idev->dev; 28211da177e4SLinus Torvalds 28221da177e4SLinus Torvalds addrconf_join_solict(dev, &ifp->addr); 28231da177e4SLinus Torvalds 28241da177e4SLinus Torvalds net_srandom(ifp->addr.s6_addr32[3]); 28251da177e4SLinus Torvalds 28261da177e4SLinus Torvalds read_lock_bh(&idev->lock); 28271da177e4SLinus Torvalds if (ifp->dead) 28281da177e4SLinus Torvalds goto out; 28291da177e4SLinus Torvalds 283021809fafSstephen hemminger spin_lock(&ifp->lock); 28311da177e4SLinus Torvalds if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) || 28321b34be74SYOSHIFUJI Hideaki idev->cnf.accept_dad < 1 || 283355ebaef1SNoriaki TAKAMIYA !(ifp->flags&IFA_F_TENTATIVE) || 283455ebaef1SNoriaki TAKAMIYA ifp->flags & IFA_F_NODAD) { 2835cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 283621809fafSstephen hemminger spin_unlock(&ifp->lock); 28371da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 28381da177e4SLinus Torvalds 28391da177e4SLinus Torvalds addrconf_dad_completed(ifp); 28401da177e4SLinus Torvalds return; 28411da177e4SLinus Torvalds } 28421da177e4SLinus Torvalds 28436732badeSYOSHIFUJI Hideaki if (!(idev->if_flags & IF_READY)) { 284421809fafSstephen hemminger spin_unlock(&ifp->lock); 28456732badeSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 28463c21edbdSYOSHIFUJI Hideaki /* 2847590a9887SMasatake YAMATO * If the device is not ready: 28483c21edbdSYOSHIFUJI Hideaki * - keep it tentative if it is a permanent address. 28493c21edbdSYOSHIFUJI Hideaki * - otherwise, kill it. 28503c21edbdSYOSHIFUJI Hideaki */ 28513c21edbdSYOSHIFUJI Hideaki in6_ifa_hold(ifp); 2852cc411d0bSBrian Haley addrconf_dad_stop(ifp, 0); 28536732badeSYOSHIFUJI Hideaki return; 28543c21edbdSYOSHIFUJI Hideaki } 285595c385b4SNeil Horman 285695c385b4SNeil Horman /* 285795c385b4SNeil Horman * Optimistic nodes can start receiving 285895c385b4SNeil Horman * Frames right away 285995c385b4SNeil Horman */ 286095c385b4SNeil Horman if(ifp->flags & IFA_F_OPTIMISTIC) 286195c385b4SNeil Horman ip6_ins_rt(ifp->rt); 286295c385b4SNeil Horman 28636732badeSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 286421809fafSstephen hemminger spin_unlock(&ifp->lock); 28651da177e4SLinus Torvalds out: 28661da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 28671da177e4SLinus Torvalds } 28681da177e4SLinus Torvalds 28691da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data) 28701da177e4SLinus Torvalds { 28711da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data; 28721da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 28731da177e4SLinus Torvalds struct in6_addr mcaddr; 28741da177e4SLinus Torvalds 2875122e4519Sstephen hemminger read_lock(&idev->lock); 2876122e4519Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) { 2877122e4519Sstephen hemminger read_unlock(&idev->lock); 28781da177e4SLinus Torvalds goto out; 28791da177e4SLinus Torvalds } 288021809fafSstephen hemminger 288121809fafSstephen hemminger spin_lock(&ifp->lock); 28821da177e4SLinus Torvalds if (ifp->probes == 0) { 28831da177e4SLinus Torvalds /* 28841da177e4SLinus Torvalds * DAD was successful 28851da177e4SLinus Torvalds */ 28861da177e4SLinus Torvalds 2887cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 288821809fafSstephen hemminger spin_unlock(&ifp->lock); 2889122e4519Sstephen hemminger read_unlock(&idev->lock); 28901da177e4SLinus Torvalds 28911da177e4SLinus Torvalds addrconf_dad_completed(ifp); 28921da177e4SLinus Torvalds 28931da177e4SLinus Torvalds goto out; 28941da177e4SLinus Torvalds } 28951da177e4SLinus Torvalds 28961da177e4SLinus Torvalds ifp->probes--; 28971da177e4SLinus Torvalds addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time); 289821809fafSstephen hemminger spin_unlock(&ifp->lock); 2899122e4519Sstephen hemminger read_unlock(&idev->lock); 29001da177e4SLinus Torvalds 29011da177e4SLinus Torvalds /* send a neighbour solicitation for our addr */ 29021da177e4SLinus Torvalds addrconf_addr_solict_mult(&ifp->addr, &mcaddr); 2903d7aabf22SYOSHIFUJI Hideaki ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any); 29041da177e4SLinus Torvalds out: 29051da177e4SLinus Torvalds in6_ifa_put(ifp); 29061da177e4SLinus Torvalds } 29071da177e4SLinus Torvalds 29081da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp) 29091da177e4SLinus Torvalds { 29101da177e4SLinus Torvalds struct net_device * dev = ifp->idev->dev; 29111da177e4SLinus Torvalds 29121da177e4SLinus Torvalds /* 29131da177e4SLinus Torvalds * Configure the address for reception. Now it is valid. 29141da177e4SLinus Torvalds */ 29151da177e4SLinus Torvalds 29161da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 29171da177e4SLinus Torvalds 29181da177e4SLinus Torvalds /* If added prefix is link local and forwarding is off, 29191da177e4SLinus Torvalds start sending router solicitations. 29201da177e4SLinus Torvalds */ 29211da177e4SLinus Torvalds 29221da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding == 0 && 29231da177e4SLinus Torvalds ifp->idev->cnf.rtr_solicits > 0 && 29241da177e4SLinus Torvalds (dev->flags&IFF_LOOPBACK) == 0 && 29251da177e4SLinus Torvalds (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) { 29261da177e4SLinus Torvalds /* 29271da177e4SLinus Torvalds * If a host as already performed a random delay 29281da177e4SLinus Torvalds * [...] as part of DAD [...] there is no need 29291da177e4SLinus Torvalds * to delay again before sending the first RS 29301da177e4SLinus Torvalds */ 2931f3ee4010SYOSHIFUJI Hideaki ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters); 29321da177e4SLinus Torvalds 29331da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 29341da177e4SLinus Torvalds ifp->probes = 1; 29351da177e4SLinus Torvalds ifp->idev->if_flags |= IF_RS_SENT; 29361da177e4SLinus Torvalds addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval); 29371da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 29381da177e4SLinus Torvalds } 29391da177e4SLinus Torvalds } 29401da177e4SLinus Torvalds 2941c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev) { 2942c5e33bddSYOSHIFUJI Hideaki struct inet6_ifaddr *ifp; 2943c5e33bddSYOSHIFUJI Hideaki 2944c5e33bddSYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 2945c5e33bddSYOSHIFUJI Hideaki for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) { 294621809fafSstephen hemminger spin_lock(&ifp->lock); 2947c5e33bddSYOSHIFUJI Hideaki if (!(ifp->flags & IFA_F_TENTATIVE)) { 294821809fafSstephen hemminger spin_unlock(&ifp->lock); 2949c5e33bddSYOSHIFUJI Hideaki continue; 2950c5e33bddSYOSHIFUJI Hideaki } 295121809fafSstephen hemminger spin_unlock(&ifp->lock); 2952c5e33bddSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 2953c5e33bddSYOSHIFUJI Hideaki } 2954c5e33bddSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 2955c5e33bddSYOSHIFUJI Hideaki } 2956c5e33bddSYOSHIFUJI Hideaki 29571da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 29581da177e4SLinus Torvalds struct if6_iter_state { 29593c40090aSDaniel Lezcano struct seq_net_private p; 29601da177e4SLinus Torvalds int bucket; 29611da177e4SLinus Torvalds }; 29621da177e4SLinus Torvalds 29631da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_first(struct seq_file *seq) 29641da177e4SLinus Torvalds { 29651da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 29661da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 29671218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 29681da177e4SLinus Torvalds 29691da177e4SLinus Torvalds for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) { 29701da177e4SLinus Torvalds ifa = inet6_addr_lst[state->bucket]; 29713c40090aSDaniel Lezcano 2972878628fbSYOSHIFUJI Hideaki while (ifa && !net_eq(dev_net(ifa->idev->dev), net)) 29733c40090aSDaniel Lezcano ifa = ifa->lst_next; 29741da177e4SLinus Torvalds if (ifa) 29751da177e4SLinus Torvalds break; 29761da177e4SLinus Torvalds } 29771da177e4SLinus Torvalds return ifa; 29781da177e4SLinus Torvalds } 29791da177e4SLinus Torvalds 29801da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa) 29811da177e4SLinus Torvalds { 29821da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 29831218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 29841da177e4SLinus Torvalds 29851da177e4SLinus Torvalds ifa = ifa->lst_next; 29861da177e4SLinus Torvalds try_again: 29873c40090aSDaniel Lezcano if (ifa) { 2988878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifa->idev->dev), net)) { 29893c40090aSDaniel Lezcano ifa = ifa->lst_next; 29903c40090aSDaniel Lezcano goto try_again; 29913c40090aSDaniel Lezcano } 29923c40090aSDaniel Lezcano } 29933c40090aSDaniel Lezcano 29941da177e4SLinus Torvalds if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) { 29951da177e4SLinus Torvalds ifa = inet6_addr_lst[state->bucket]; 29961da177e4SLinus Torvalds goto try_again; 29971da177e4SLinus Torvalds } 29983c40090aSDaniel Lezcano 29991da177e4SLinus Torvalds return ifa; 30001da177e4SLinus Torvalds } 30011da177e4SLinus Torvalds 30021da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos) 30031da177e4SLinus Torvalds { 30041da177e4SLinus Torvalds struct inet6_ifaddr *ifa = if6_get_first(seq); 30051da177e4SLinus Torvalds 30061da177e4SLinus Torvalds if (ifa) 30071da177e4SLinus Torvalds while(pos && (ifa = if6_get_next(seq, ifa)) != NULL) 30081da177e4SLinus Torvalds --pos; 30091da177e4SLinus Torvalds return pos ? NULL : ifa; 30101da177e4SLinus Torvalds } 30111da177e4SLinus Torvalds 30121da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos) 3013d20b3109SStephen Hemminger __acquires(addrconf_hash_lock) 30141da177e4SLinus Torvalds { 30151da177e4SLinus Torvalds read_lock_bh(&addrconf_hash_lock); 30161da177e4SLinus Torvalds return if6_get_idx(seq, *pos); 30171da177e4SLinus Torvalds } 30181da177e4SLinus Torvalds 30191da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos) 30201da177e4SLinus Torvalds { 30211da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 30221da177e4SLinus Torvalds 30231da177e4SLinus Torvalds ifa = if6_get_next(seq, v); 30241da177e4SLinus Torvalds ++*pos; 30251da177e4SLinus Torvalds return ifa; 30261da177e4SLinus Torvalds } 30271da177e4SLinus Torvalds 30281da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v) 3029d20b3109SStephen Hemminger __releases(addrconf_hash_lock) 30301da177e4SLinus Torvalds { 30311da177e4SLinus Torvalds read_unlock_bh(&addrconf_hash_lock); 30321da177e4SLinus Torvalds } 30331da177e4SLinus Torvalds 30341da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v) 30351da177e4SLinus Torvalds { 30361da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v; 30374b7a4274SHarvey Harrison seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n", 3038b071195dSHarvey Harrison &ifp->addr, 30391da177e4SLinus Torvalds ifp->idev->dev->ifindex, 30401da177e4SLinus Torvalds ifp->prefix_len, 30411da177e4SLinus Torvalds ifp->scope, 30421da177e4SLinus Torvalds ifp->flags, 30431da177e4SLinus Torvalds ifp->idev->dev->name); 30441da177e4SLinus Torvalds return 0; 30451da177e4SLinus Torvalds } 30461da177e4SLinus Torvalds 304756b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = { 30481da177e4SLinus Torvalds .start = if6_seq_start, 30491da177e4SLinus Torvalds .next = if6_seq_next, 30501da177e4SLinus Torvalds .show = if6_seq_show, 30511da177e4SLinus Torvalds .stop = if6_seq_stop, 30521da177e4SLinus Torvalds }; 30531da177e4SLinus Torvalds 30541da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file) 30551da177e4SLinus Torvalds { 30563c40090aSDaniel Lezcano return seq_open_net(inode, file, &if6_seq_ops, 3057cf7732e4SPavel Emelyanov sizeof(struct if6_iter_state)); 30581da177e4SLinus Torvalds } 30591da177e4SLinus Torvalds 30609a32144eSArjan van de Ven static const struct file_operations if6_fops = { 30611da177e4SLinus Torvalds .owner = THIS_MODULE, 30621da177e4SLinus Torvalds .open = if6_seq_open, 30631da177e4SLinus Torvalds .read = seq_read, 30641da177e4SLinus Torvalds .llseek = seq_lseek, 30653c40090aSDaniel Lezcano .release = seq_release_net, 30661da177e4SLinus Torvalds }; 30671da177e4SLinus Torvalds 30682c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net) 30691da177e4SLinus Torvalds { 30703c40090aSDaniel Lezcano if (!proc_net_fops_create(net, "if_inet6", S_IRUGO, &if6_fops)) 30711da177e4SLinus Torvalds return -ENOMEM; 30721da177e4SLinus Torvalds return 0; 30731da177e4SLinus Torvalds } 30741da177e4SLinus Torvalds 30752c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net) 30763c40090aSDaniel Lezcano { 30773c40090aSDaniel Lezcano proc_net_remove(net, "if_inet6"); 30783c40090aSDaniel Lezcano } 30793c40090aSDaniel Lezcano 30803c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = { 30813c40090aSDaniel Lezcano .init = if6_proc_net_init, 30823c40090aSDaniel Lezcano .exit = if6_proc_net_exit, 30833c40090aSDaniel Lezcano }; 30843c40090aSDaniel Lezcano 30853c40090aSDaniel Lezcano int __init if6_proc_init(void) 30863c40090aSDaniel Lezcano { 30873c40090aSDaniel Lezcano return register_pernet_subsys(&if6_proc_net_ops); 30883c40090aSDaniel Lezcano } 30893c40090aSDaniel Lezcano 30901da177e4SLinus Torvalds void if6_proc_exit(void) 30911da177e4SLinus Torvalds { 30923c40090aSDaniel Lezcano unregister_pernet_subsys(&if6_proc_net_ops); 30931da177e4SLinus Torvalds } 30941da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 30951da177e4SLinus Torvalds 309659fbb3a6SMasahide NAKAMURA #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) 30973b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */ 3098389f6612SDaniel Lezcano int ipv6_chk_home_addr(struct net *net, struct in6_addr *addr) 30993b9f9a1cSNoriaki TAKAMIYA { 31003b9f9a1cSNoriaki TAKAMIYA int ret = 0; 31013b9f9a1cSNoriaki TAKAMIYA struct inet6_ifaddr * ifp; 31023b9f9a1cSNoriaki TAKAMIYA u8 hash = ipv6_addr_hash(addr); 31033b9f9a1cSNoriaki TAKAMIYA read_lock_bh(&addrconf_hash_lock); 31043b9f9a1cSNoriaki TAKAMIYA for (ifp = inet6_addr_lst[hash]; ifp; ifp = ifp->lst_next) { 3105878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 3106389f6612SDaniel Lezcano continue; 3107caad295fSYOSHIFUJI Hideaki if (ipv6_addr_equal(&ifp->addr, addr) && 31083b9f9a1cSNoriaki TAKAMIYA (ifp->flags & IFA_F_HOMEADDRESS)) { 31093b9f9a1cSNoriaki TAKAMIYA ret = 1; 31103b9f9a1cSNoriaki TAKAMIYA break; 31113b9f9a1cSNoriaki TAKAMIYA } 31123b9f9a1cSNoriaki TAKAMIYA } 31133b9f9a1cSNoriaki TAKAMIYA read_unlock_bh(&addrconf_hash_lock); 31143b9f9a1cSNoriaki TAKAMIYA return ret; 31153b9f9a1cSNoriaki TAKAMIYA } 31163b9f9a1cSNoriaki TAKAMIYA #endif 31173b9f9a1cSNoriaki TAKAMIYA 31181da177e4SLinus Torvalds /* 31191da177e4SLinus Torvalds * Periodic address status verification 31201da177e4SLinus Torvalds */ 31211da177e4SLinus Torvalds 31221da177e4SLinus Torvalds static void addrconf_verify(unsigned long foo) 31231da177e4SLinus Torvalds { 31241da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 31251da177e4SLinus Torvalds unsigned long now, next; 31261da177e4SLinus Torvalds int i; 31271da177e4SLinus Torvalds 31281da177e4SLinus Torvalds spin_lock_bh(&addrconf_verify_lock); 31291da177e4SLinus Torvalds now = jiffies; 31301da177e4SLinus Torvalds next = now + ADDR_CHECK_FREQUENCY; 31311da177e4SLinus Torvalds 31321da177e4SLinus Torvalds del_timer(&addr_chk_timer); 31331da177e4SLinus Torvalds 31341da177e4SLinus Torvalds for (i=0; i < IN6_ADDR_HSIZE; i++) { 31351da177e4SLinus Torvalds 31361da177e4SLinus Torvalds restart: 31375d5780dfSYan Zheng read_lock(&addrconf_hash_lock); 31381da177e4SLinus Torvalds for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) { 31391da177e4SLinus Torvalds unsigned long age; 31401da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 31411da177e4SLinus Torvalds unsigned long regen_advance; 31421da177e4SLinus Torvalds #endif 31431da177e4SLinus Torvalds 31441da177e4SLinus Torvalds if (ifp->flags & IFA_F_PERMANENT) 31451da177e4SLinus Torvalds continue; 31461da177e4SLinus Torvalds 31471da177e4SLinus Torvalds spin_lock(&ifp->lock); 31481da177e4SLinus Torvalds age = (now - ifp->tstamp) / HZ; 31491da177e4SLinus Torvalds 31501da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 31511da177e4SLinus Torvalds regen_advance = ifp->idev->cnf.regen_max_retry * 31521da177e4SLinus Torvalds ifp->idev->cnf.dad_transmits * 31531da177e4SLinus Torvalds ifp->idev->nd_parms->retrans_time / HZ; 31541da177e4SLinus Torvalds #endif 31551da177e4SLinus Torvalds 31568f27ebb9SYOSHIFUJI Hideaki if (ifp->valid_lft != INFINITY_LIFE_TIME && 31578f27ebb9SYOSHIFUJI Hideaki age >= ifp->valid_lft) { 31581da177e4SLinus Torvalds spin_unlock(&ifp->lock); 31591da177e4SLinus Torvalds in6_ifa_hold(ifp); 31605d5780dfSYan Zheng read_unlock(&addrconf_hash_lock); 31611da177e4SLinus Torvalds ipv6_del_addr(ifp); 31621da177e4SLinus Torvalds goto restart; 31638f27ebb9SYOSHIFUJI Hideaki } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) { 31648f27ebb9SYOSHIFUJI Hideaki spin_unlock(&ifp->lock); 31658f27ebb9SYOSHIFUJI Hideaki continue; 31661da177e4SLinus Torvalds } else if (age >= ifp->prefered_lft) { 3167a1ed0526SBrian Haley /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */ 31681da177e4SLinus Torvalds int deprecate = 0; 31691da177e4SLinus Torvalds 31701da177e4SLinus Torvalds if (!(ifp->flags&IFA_F_DEPRECATED)) { 31711da177e4SLinus Torvalds deprecate = 1; 31721da177e4SLinus Torvalds ifp->flags |= IFA_F_DEPRECATED; 31731da177e4SLinus Torvalds } 31741da177e4SLinus Torvalds 31751da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)) 31761da177e4SLinus Torvalds next = ifp->tstamp + ifp->valid_lft * HZ; 31771da177e4SLinus Torvalds 31781da177e4SLinus Torvalds spin_unlock(&ifp->lock); 31791da177e4SLinus Torvalds 31801da177e4SLinus Torvalds if (deprecate) { 31811da177e4SLinus Torvalds in6_ifa_hold(ifp); 31825d5780dfSYan Zheng read_unlock(&addrconf_hash_lock); 31831da177e4SLinus Torvalds 31841da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 31851da177e4SLinus Torvalds in6_ifa_put(ifp); 31861da177e4SLinus Torvalds goto restart; 31871da177e4SLinus Torvalds } 31881da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 31891da177e4SLinus Torvalds } else if ((ifp->flags&IFA_F_TEMPORARY) && 31901da177e4SLinus Torvalds !(ifp->flags&IFA_F_TENTATIVE)) { 31911da177e4SLinus Torvalds if (age >= ifp->prefered_lft - regen_advance) { 31921da177e4SLinus Torvalds struct inet6_ifaddr *ifpub = ifp->ifpub; 31931da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 31941da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 31951da177e4SLinus Torvalds if (!ifp->regen_count && ifpub) { 31961da177e4SLinus Torvalds ifp->regen_count++; 31971da177e4SLinus Torvalds in6_ifa_hold(ifp); 31981da177e4SLinus Torvalds in6_ifa_hold(ifpub); 31991da177e4SLinus Torvalds spin_unlock(&ifp->lock); 32005d5780dfSYan Zheng read_unlock(&addrconf_hash_lock); 3201291d809bSHiroyuki YAMAMORI spin_lock(&ifpub->lock); 3202291d809bSHiroyuki YAMAMORI ifpub->regen_count = 0; 3203291d809bSHiroyuki YAMAMORI spin_unlock(&ifpub->lock); 32041da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 32051da177e4SLinus Torvalds in6_ifa_put(ifpub); 32061da177e4SLinus Torvalds in6_ifa_put(ifp); 32071da177e4SLinus Torvalds goto restart; 32081da177e4SLinus Torvalds } 32091da177e4SLinus Torvalds } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) 32101da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; 32111da177e4SLinus Torvalds spin_unlock(&ifp->lock); 32121da177e4SLinus Torvalds #endif 32131da177e4SLinus Torvalds } else { 32141da177e4SLinus Torvalds /* ifp->prefered_lft <= ifp->valid_lft */ 32151da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 32161da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 32171da177e4SLinus Torvalds spin_unlock(&ifp->lock); 32181da177e4SLinus Torvalds } 32191da177e4SLinus Torvalds } 32205d5780dfSYan Zheng read_unlock(&addrconf_hash_lock); 32211da177e4SLinus Torvalds } 32221da177e4SLinus Torvalds 32231da177e4SLinus Torvalds addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next; 32241da177e4SLinus Torvalds add_timer(&addr_chk_timer); 32251da177e4SLinus Torvalds spin_unlock_bh(&addrconf_verify_lock); 32261da177e4SLinus Torvalds } 32271da177e4SLinus Torvalds 3228461d8837SThomas Graf static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local) 3229461d8837SThomas Graf { 3230461d8837SThomas Graf struct in6_addr *pfx = NULL; 3231461d8837SThomas Graf 3232461d8837SThomas Graf if (addr) 3233461d8837SThomas Graf pfx = nla_data(addr); 3234461d8837SThomas Graf 3235461d8837SThomas Graf if (local) { 3236461d8837SThomas Graf if (pfx && nla_memcmp(local, pfx, sizeof(*pfx))) 3237461d8837SThomas Graf pfx = NULL; 3238461d8837SThomas Graf else 3239461d8837SThomas Graf pfx = nla_data(local); 3240461d8837SThomas Graf } 3241461d8837SThomas Graf 3242461d8837SThomas Graf return pfx; 3243461d8837SThomas Graf } 3244461d8837SThomas Graf 3245ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = { 3246461d8837SThomas Graf [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) }, 3247461d8837SThomas Graf [IFA_LOCAL] = { .len = sizeof(struct in6_addr) }, 3248461d8837SThomas Graf [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, 3249461d8837SThomas Graf }; 3250461d8837SThomas Graf 32511da177e4SLinus Torvalds static int 32521da177e4SLinus Torvalds inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 32531da177e4SLinus Torvalds { 32543b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 3255b933f716SThomas Graf struct ifaddrmsg *ifm; 3256b933f716SThomas Graf struct nlattr *tb[IFA_MAX+1]; 32571da177e4SLinus Torvalds struct in6_addr *pfx; 3258b933f716SThomas Graf int err; 32591da177e4SLinus Torvalds 3260b933f716SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 3261b933f716SThomas Graf if (err < 0) 3262b933f716SThomas Graf return err; 3263b933f716SThomas Graf 3264b933f716SThomas Graf ifm = nlmsg_data(nlh); 3265b933f716SThomas Graf pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]); 32661da177e4SLinus Torvalds if (pfx == NULL) 32671da177e4SLinus Torvalds return -EINVAL; 32681da177e4SLinus Torvalds 3269af284937SDaniel Lezcano return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen); 32701da177e4SLinus Torvalds } 32711da177e4SLinus Torvalds 327255ebaef1SNoriaki TAKAMIYA static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags, 327355ebaef1SNoriaki TAKAMIYA u32 prefered_lft, u32 valid_lft) 3274081bba5bSNoriaki TAKAMIYA { 32756f704992SYOSHIFUJI Hideaki u32 flags; 32766f704992SYOSHIFUJI Hideaki clock_t expires; 32774bed72e4SYOSHIFUJI Hideaki unsigned long timeout; 327846d48046SYOSHIFUJI Hideaki 3279081bba5bSNoriaki TAKAMIYA if (!valid_lft || (prefered_lft > valid_lft)) 3280081bba5bSNoriaki TAKAMIYA return -EINVAL; 3281081bba5bSNoriaki TAKAMIYA 32824bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(valid_lft, HZ); 32834bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 32844bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 32854bed72e4SYOSHIFUJI Hideaki valid_lft = timeout; 32866f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 32874bed72e4SYOSHIFUJI Hideaki } else { 32884bed72e4SYOSHIFUJI Hideaki expires = 0; 32894bed72e4SYOSHIFUJI Hideaki flags = 0; 32904bed72e4SYOSHIFUJI Hideaki ifa_flags |= IFA_F_PERMANENT; 32916f704992SYOSHIFUJI Hideaki } 3292081bba5bSNoriaki TAKAMIYA 32934bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(prefered_lft, HZ); 32944bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 32954bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 329655ebaef1SNoriaki TAKAMIYA ifa_flags |= IFA_F_DEPRECATED; 32974bed72e4SYOSHIFUJI Hideaki prefered_lft = timeout; 32984bed72e4SYOSHIFUJI Hideaki } 3299081bba5bSNoriaki TAKAMIYA 3300081bba5bSNoriaki TAKAMIYA spin_lock_bh(&ifp->lock); 33013b9f9a1cSNoriaki TAKAMIYA ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags; 3302081bba5bSNoriaki TAKAMIYA ifp->tstamp = jiffies; 3303081bba5bSNoriaki TAKAMIYA ifp->valid_lft = valid_lft; 3304081bba5bSNoriaki TAKAMIYA ifp->prefered_lft = prefered_lft; 3305081bba5bSNoriaki TAKAMIYA 3306081bba5bSNoriaki TAKAMIYA spin_unlock_bh(&ifp->lock); 3307081bba5bSNoriaki TAKAMIYA if (!(ifp->flags&IFA_F_TENTATIVE)) 3308081bba5bSNoriaki TAKAMIYA ipv6_ifa_notify(0, ifp); 3309081bba5bSNoriaki TAKAMIYA 331046d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev, 33116f704992SYOSHIFUJI Hideaki expires, flags); 3312081bba5bSNoriaki TAKAMIYA addrconf_verify(0); 3313081bba5bSNoriaki TAKAMIYA 3314081bba5bSNoriaki TAKAMIYA return 0; 3315081bba5bSNoriaki TAKAMIYA } 3316081bba5bSNoriaki TAKAMIYA 3317081bba5bSNoriaki TAKAMIYA static int 33181da177e4SLinus Torvalds inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 33191da177e4SLinus Torvalds { 33203b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 3321461d8837SThomas Graf struct ifaddrmsg *ifm; 3322461d8837SThomas Graf struct nlattr *tb[IFA_MAX+1]; 33231da177e4SLinus Torvalds struct in6_addr *pfx; 33247198f8ceSThomas Graf struct inet6_ifaddr *ifa; 33257198f8ceSThomas Graf struct net_device *dev; 332655ebaef1SNoriaki TAKAMIYA u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME; 332755ebaef1SNoriaki TAKAMIYA u8 ifa_flags; 3328461d8837SThomas Graf int err; 33291da177e4SLinus Torvalds 3330461d8837SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 3331461d8837SThomas Graf if (err < 0) 3332461d8837SThomas Graf return err; 3333461d8837SThomas Graf 3334461d8837SThomas Graf ifm = nlmsg_data(nlh); 3335461d8837SThomas Graf pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]); 33361da177e4SLinus Torvalds if (pfx == NULL) 33371da177e4SLinus Torvalds return -EINVAL; 33381da177e4SLinus Torvalds 3339461d8837SThomas Graf if (tb[IFA_CACHEINFO]) { 33400778769dSNoriaki TAKAMIYA struct ifa_cacheinfo *ci; 3341461d8837SThomas Graf 3342461d8837SThomas Graf ci = nla_data(tb[IFA_CACHEINFO]); 33430778769dSNoriaki TAKAMIYA valid_lft = ci->ifa_valid; 3344461d8837SThomas Graf preferred_lft = ci->ifa_prefered; 3345461d8837SThomas Graf } else { 3346461d8837SThomas Graf preferred_lft = INFINITY_LIFE_TIME; 3347461d8837SThomas Graf valid_lft = INFINITY_LIFE_TIME; 33480778769dSNoriaki TAKAMIYA } 33490778769dSNoriaki TAKAMIYA 3350af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifm->ifa_index); 33517198f8ceSThomas Graf if (dev == NULL) 33527198f8ceSThomas Graf return -ENODEV; 3353081bba5bSNoriaki TAKAMIYA 335455ebaef1SNoriaki TAKAMIYA /* We ignore other flags so far. */ 33553b9f9a1cSNoriaki TAKAMIYA ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS); 335655ebaef1SNoriaki TAKAMIYA 33571cab3da6SDaniel Lezcano ifa = ipv6_get_ifaddr(net, pfx, dev, 1); 33587198f8ceSThomas Graf if (ifa == NULL) { 33597198f8ceSThomas Graf /* 33607198f8ceSThomas Graf * It would be best to check for !NLM_F_CREATE here but 33617198f8ceSThomas Graf * userspace alreay relies on not having to provide this. 33627198f8ceSThomas Graf */ 3363af284937SDaniel Lezcano return inet6_addr_add(net, ifm->ifa_index, pfx, 3364af284937SDaniel Lezcano ifm->ifa_prefixlen, ifa_flags, 3365af284937SDaniel Lezcano preferred_lft, valid_lft); 33661da177e4SLinus Torvalds } 33671da177e4SLinus Torvalds 33687198f8ceSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL || 33697198f8ceSThomas Graf !(nlh->nlmsg_flags & NLM_F_REPLACE)) 33707198f8ceSThomas Graf err = -EEXIST; 33717198f8ceSThomas Graf else 337255ebaef1SNoriaki TAKAMIYA err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft); 33737198f8ceSThomas Graf 33747198f8ceSThomas Graf in6_ifa_put(ifa); 33757198f8ceSThomas Graf 33767198f8ceSThomas Graf return err; 33777198f8ceSThomas Graf } 33787198f8ceSThomas Graf 3379101bb229SThomas Graf static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags, 3380101bb229SThomas Graf u8 scope, int ifindex) 3381101bb229SThomas Graf { 3382101bb229SThomas Graf struct ifaddrmsg *ifm; 3383101bb229SThomas Graf 3384101bb229SThomas Graf ifm = nlmsg_data(nlh); 3385101bb229SThomas Graf ifm->ifa_family = AF_INET6; 3386101bb229SThomas Graf ifm->ifa_prefixlen = prefixlen; 3387101bb229SThomas Graf ifm->ifa_flags = flags; 3388101bb229SThomas Graf ifm->ifa_scope = scope; 3389101bb229SThomas Graf ifm->ifa_index = ifindex; 3390101bb229SThomas Graf } 3391101bb229SThomas Graf 339285486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, 339385486af0SThomas Graf unsigned long tstamp, u32 preferred, u32 valid) 339485486af0SThomas Graf { 339585486af0SThomas Graf struct ifa_cacheinfo ci; 339685486af0SThomas Graf 339785486af0SThomas Graf ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100 339885486af0SThomas Graf + TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ); 339985486af0SThomas Graf ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100 340085486af0SThomas Graf + TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ); 340185486af0SThomas Graf ci.ifa_prefered = preferred; 340285486af0SThomas Graf ci.ifa_valid = valid; 340385486af0SThomas Graf 340485486af0SThomas Graf return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); 340585486af0SThomas Graf } 340685486af0SThomas Graf 3407101bb229SThomas Graf static inline int rt_scope(int ifa_scope) 3408101bb229SThomas Graf { 3409101bb229SThomas Graf if (ifa_scope & IFA_HOST) 3410101bb229SThomas Graf return RT_SCOPE_HOST; 3411101bb229SThomas Graf else if (ifa_scope & IFA_LINK) 3412101bb229SThomas Graf return RT_SCOPE_LINK; 3413101bb229SThomas Graf else if (ifa_scope & IFA_SITE) 3414101bb229SThomas Graf return RT_SCOPE_SITE; 3415101bb229SThomas Graf else 3416101bb229SThomas Graf return RT_SCOPE_UNIVERSE; 3417101bb229SThomas Graf } 3418101bb229SThomas Graf 34190ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void) 34200ab6803bSThomas Graf { 3421339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) 3422339bf98fSThomas Graf + nla_total_size(16) /* IFA_ADDRESS */ 3423339bf98fSThomas Graf + nla_total_size(sizeof(struct ifa_cacheinfo)); 34240ab6803bSThomas Graf } 3425c5396a31SYOSHIFUJI Hideaki 34261da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa, 3427b6544c0bSJamal Hadi Salim u32 pid, u32 seq, int event, unsigned int flags) 34281da177e4SLinus Torvalds { 34291da177e4SLinus Torvalds struct nlmsghdr *nlh; 343085486af0SThomas Graf u32 preferred, valid; 34311da177e4SLinus Torvalds 34320ab6803bSThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags); 34330ab6803bSThomas Graf if (nlh == NULL) 343426932566SPatrick McHardy return -EMSGSIZE; 34350ab6803bSThomas Graf 3436101bb229SThomas Graf put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), 3437101bb229SThomas Graf ifa->idev->dev->ifindex); 3438101bb229SThomas Graf 34391da177e4SLinus Torvalds if (!(ifa->flags&IFA_F_PERMANENT)) { 344085486af0SThomas Graf preferred = ifa->prefered_lft; 344185486af0SThomas Graf valid = ifa->valid_lft; 344285486af0SThomas Graf if (preferred != INFINITY_LIFE_TIME) { 34431da177e4SLinus Torvalds long tval = (jiffies - ifa->tstamp)/HZ; 3444a1faa698SJens Rosenboom if (preferred > tval) 344585486af0SThomas Graf preferred -= tval; 3446a1faa698SJens Rosenboom else 3447a1faa698SJens Rosenboom preferred = 0; 344885486af0SThomas Graf if (valid != INFINITY_LIFE_TIME) 344985486af0SThomas Graf valid -= tval; 34501da177e4SLinus Torvalds } 34511da177e4SLinus Torvalds } else { 345285486af0SThomas Graf preferred = INFINITY_LIFE_TIME; 345385486af0SThomas Graf valid = INFINITY_LIFE_TIME; 34541da177e4SLinus Torvalds } 345585486af0SThomas Graf 34560ab6803bSThomas Graf if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 || 345726932566SPatrick McHardy put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) { 345826932566SPatrick McHardy nlmsg_cancel(skb, nlh); 345926932566SPatrick McHardy return -EMSGSIZE; 346026932566SPatrick McHardy } 346185486af0SThomas Graf 34620ab6803bSThomas Graf return nlmsg_end(skb, nlh); 34631da177e4SLinus Torvalds } 34641da177e4SLinus Torvalds 34651da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca, 3466e431b8c0SJamal Hadi Salim u32 pid, u32 seq, int event, u16 flags) 34671da177e4SLinus Torvalds { 34681da177e4SLinus Torvalds struct nlmsghdr *nlh; 3469101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 3470101bb229SThomas Graf int ifindex = ifmca->idev->dev->ifindex; 34711da177e4SLinus Torvalds 34721da177e4SLinus Torvalds if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE) 3473101bb229SThomas Graf scope = RT_SCOPE_SITE; 3474101bb229SThomas Graf 34750ab6803bSThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags); 34760ab6803bSThomas Graf if (nlh == NULL) 347726932566SPatrick McHardy return -EMSGSIZE; 34780ab6803bSThomas Graf 3479101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 34800ab6803bSThomas Graf if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 || 34810ab6803bSThomas Graf put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, 348226932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 348326932566SPatrick McHardy nlmsg_cancel(skb, nlh); 348426932566SPatrick McHardy return -EMSGSIZE; 348526932566SPatrick McHardy } 348685486af0SThomas Graf 34870ab6803bSThomas Graf return nlmsg_end(skb, nlh); 34881da177e4SLinus Torvalds } 34891da177e4SLinus Torvalds 34901da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca, 3491b6544c0bSJamal Hadi Salim u32 pid, u32 seq, int event, unsigned int flags) 34921da177e4SLinus Torvalds { 34931da177e4SLinus Torvalds struct nlmsghdr *nlh; 3494101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 3495101bb229SThomas Graf int ifindex = ifaca->aca_idev->dev->ifindex; 34961da177e4SLinus Torvalds 34971da177e4SLinus Torvalds if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) 3498101bb229SThomas Graf scope = RT_SCOPE_SITE; 3499101bb229SThomas Graf 35000ab6803bSThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags); 35010ab6803bSThomas Graf if (nlh == NULL) 350226932566SPatrick McHardy return -EMSGSIZE; 35030ab6803bSThomas Graf 3504101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 35050ab6803bSThomas Graf if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 || 35060ab6803bSThomas Graf put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, 350726932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 350826932566SPatrick McHardy nlmsg_cancel(skb, nlh); 350926932566SPatrick McHardy return -EMSGSIZE; 351026932566SPatrick McHardy } 351185486af0SThomas Graf 35120ab6803bSThomas Graf return nlmsg_end(skb, nlh); 35131da177e4SLinus Torvalds } 35141da177e4SLinus Torvalds 35151da177e4SLinus Torvalds enum addr_type_t 35161da177e4SLinus Torvalds { 35171da177e4SLinus Torvalds UNICAST_ADDR, 35181da177e4SLinus Torvalds MULTICAST_ADDR, 35191da177e4SLinus Torvalds ANYCAST_ADDR, 35201da177e4SLinus Torvalds }; 35211da177e4SLinus Torvalds 3522234b27c3SEric Dumazet /* called with rcu_read_lock() */ 3523234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb, 3524234b27c3SEric Dumazet struct netlink_callback *cb, enum addr_type_t type, 3525234b27c3SEric Dumazet int s_ip_idx, int *p_ip_idx) 35261da177e4SLinus Torvalds { 35271da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 35281da177e4SLinus Torvalds struct ifmcaddr6 *ifmca; 35291da177e4SLinus Torvalds struct ifacaddr6 *ifaca; 3530234b27c3SEric Dumazet int err = 1; 3531234b27c3SEric Dumazet int ip_idx = *p_ip_idx; 35321da177e4SLinus Torvalds 35331da177e4SLinus Torvalds read_lock_bh(&idev->lock); 35341da177e4SLinus Torvalds switch (type) { 35351da177e4SLinus Torvalds case UNICAST_ADDR: 3536ae9cda5dSYOSHIFUJI Hideaki /* unicast address incl. temp addr */ 35371da177e4SLinus Torvalds for (ifa = idev->addr_list; ifa; 35381da177e4SLinus Torvalds ifa = ifa->if_next, ip_idx++) { 35391da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 35401da177e4SLinus Torvalds continue; 35415d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifaddr(skb, ifa, 35421da177e4SLinus Torvalds NETLINK_CB(cb->skb).pid, 35435d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 35445d5619b4SYOSHIFUJI Hideaki RTM_NEWADDR, 35455d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 3546bcd32326SEric Dumazet if (err <= 0) 3547bcd32326SEric Dumazet break; 35481da177e4SLinus Torvalds } 35491da177e4SLinus Torvalds break; 35501da177e4SLinus Torvalds case MULTICAST_ADDR: 35511da177e4SLinus Torvalds /* multicast address */ 35521da177e4SLinus Torvalds for (ifmca = idev->mc_list; ifmca; 35531da177e4SLinus Torvalds ifmca = ifmca->next, ip_idx++) { 35541da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 35551da177e4SLinus Torvalds continue; 35565d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifmcaddr(skb, ifmca, 35571da177e4SLinus Torvalds NETLINK_CB(cb->skb).pid, 35585d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 35595d5619b4SYOSHIFUJI Hideaki RTM_GETMULTICAST, 35605d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 3561bcd32326SEric Dumazet if (err <= 0) 3562bcd32326SEric Dumazet break; 35631da177e4SLinus Torvalds } 35641da177e4SLinus Torvalds break; 35651da177e4SLinus Torvalds case ANYCAST_ADDR: 35661da177e4SLinus Torvalds /* anycast address */ 35671da177e4SLinus Torvalds for (ifaca = idev->ac_list; ifaca; 35681da177e4SLinus Torvalds ifaca = ifaca->aca_next, ip_idx++) { 35691da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 35701da177e4SLinus Torvalds continue; 35715d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifacaddr(skb, ifaca, 35721da177e4SLinus Torvalds NETLINK_CB(cb->skb).pid, 35735d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 35745d5619b4SYOSHIFUJI Hideaki RTM_GETANYCAST, 35755d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 3576bcd32326SEric Dumazet if (err <= 0) 3577bcd32326SEric Dumazet break; 35781da177e4SLinus Torvalds } 35791da177e4SLinus Torvalds break; 35801da177e4SLinus Torvalds default: 35811da177e4SLinus Torvalds break; 35821da177e4SLinus Torvalds } 35831da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 3584234b27c3SEric Dumazet *p_ip_idx = ip_idx; 3585234b27c3SEric Dumazet return err; 3586234b27c3SEric Dumazet } 35875d5619b4SYOSHIFUJI Hideaki 3588234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, 3589234b27c3SEric Dumazet enum addr_type_t type) 3590234b27c3SEric Dumazet { 3591234b27c3SEric Dumazet struct net *net = sock_net(skb->sk); 3592234b27c3SEric Dumazet int h, s_h; 3593234b27c3SEric Dumazet int idx, ip_idx; 3594234b27c3SEric Dumazet int s_idx, s_ip_idx; 3595234b27c3SEric Dumazet struct net_device *dev; 3596234b27c3SEric Dumazet struct inet6_dev *idev; 3597234b27c3SEric Dumazet struct hlist_head *head; 3598234b27c3SEric Dumazet struct hlist_node *node; 3599234b27c3SEric Dumazet 3600234b27c3SEric Dumazet s_h = cb->args[0]; 3601234b27c3SEric Dumazet s_idx = idx = cb->args[1]; 3602234b27c3SEric Dumazet s_ip_idx = ip_idx = cb->args[2]; 3603234b27c3SEric Dumazet 3604234b27c3SEric Dumazet rcu_read_lock(); 3605234b27c3SEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 3606234b27c3SEric Dumazet idx = 0; 3607234b27c3SEric Dumazet head = &net->dev_index_head[h]; 3608234b27c3SEric Dumazet hlist_for_each_entry_rcu(dev, node, head, index_hlist) { 3609234b27c3SEric Dumazet if (idx < s_idx) 3610234b27c3SEric Dumazet goto cont; 3611234b27c3SEric Dumazet if (idx > s_idx) 3612234b27c3SEric Dumazet s_ip_idx = 0; 3613234b27c3SEric Dumazet ip_idx = 0; 3614234b27c3SEric Dumazet if ((idev = __in6_dev_get(dev)) == NULL) 3615234b27c3SEric Dumazet goto cont; 3616234b27c3SEric Dumazet 3617234b27c3SEric Dumazet if (in6_dump_addrs(idev, skb, cb, type, 3618234b27c3SEric Dumazet s_ip_idx, &ip_idx) <= 0) 3619234b27c3SEric Dumazet goto done; 36207562f876SPavel Emelianov cont: 36217562f876SPavel Emelianov idx++; 36221da177e4SLinus Torvalds } 3623234b27c3SEric Dumazet } 3624234b27c3SEric Dumazet done: 3625234b27c3SEric Dumazet rcu_read_unlock(); 3626234b27c3SEric Dumazet cb->args[0] = h; 3627234b27c3SEric Dumazet cb->args[1] = idx; 3628234b27c3SEric Dumazet cb->args[2] = ip_idx; 3629234b27c3SEric Dumazet 36301da177e4SLinus Torvalds return skb->len; 36311da177e4SLinus Torvalds } 36321da177e4SLinus Torvalds 36331da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 36341da177e4SLinus Torvalds { 36351da177e4SLinus Torvalds enum addr_type_t type = UNICAST_ADDR; 3636b854272bSDenis V. Lunev 36371da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 36381da177e4SLinus Torvalds } 36391da177e4SLinus Torvalds 36401da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) 36411da177e4SLinus Torvalds { 36421da177e4SLinus Torvalds enum addr_type_t type = MULTICAST_ADDR; 3643b854272bSDenis V. Lunev 36441da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 36451da177e4SLinus Torvalds } 36461da177e4SLinus Torvalds 36471da177e4SLinus Torvalds 36481da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) 36491da177e4SLinus Torvalds { 36501da177e4SLinus Torvalds enum addr_type_t type = ANYCAST_ADDR; 3651b854272bSDenis V. Lunev 36521da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 36531da177e4SLinus Torvalds } 36541da177e4SLinus Torvalds 36551b29fc2cSThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh, 36561b29fc2cSThomas Graf void *arg) 36576c223828SNoriaki TAKAMIYA { 36583b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 36591b29fc2cSThomas Graf struct ifaddrmsg *ifm; 36601b29fc2cSThomas Graf struct nlattr *tb[IFA_MAX+1]; 36616c223828SNoriaki TAKAMIYA struct in6_addr *addr = NULL; 36626c223828SNoriaki TAKAMIYA struct net_device *dev = NULL; 36636c223828SNoriaki TAKAMIYA struct inet6_ifaddr *ifa; 36646c223828SNoriaki TAKAMIYA struct sk_buff *skb; 36656c223828SNoriaki TAKAMIYA int err; 36666c223828SNoriaki TAKAMIYA 36671b29fc2cSThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 36681b29fc2cSThomas Graf if (err < 0) 36691b29fc2cSThomas Graf goto errout; 36706c223828SNoriaki TAKAMIYA 36711b29fc2cSThomas Graf addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]); 36721b29fc2cSThomas Graf if (addr == NULL) { 36731b29fc2cSThomas Graf err = -EINVAL; 36741b29fc2cSThomas Graf goto errout; 36751b29fc2cSThomas Graf } 36761b29fc2cSThomas Graf 36771b29fc2cSThomas Graf ifm = nlmsg_data(nlh); 36786c223828SNoriaki TAKAMIYA if (ifm->ifa_index) 36796fda7350SBenjamin Thery dev = __dev_get_by_index(net, ifm->ifa_index); 36806c223828SNoriaki TAKAMIYA 36811cab3da6SDaniel Lezcano if ((ifa = ipv6_get_ifaddr(net, addr, dev, 1)) == NULL) { 36821b29fc2cSThomas Graf err = -EADDRNOTAVAIL; 36831b29fc2cSThomas Graf goto errout; 36846c223828SNoriaki TAKAMIYA } 36856c223828SNoriaki TAKAMIYA 36860ab6803bSThomas Graf if ((skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL)) == NULL) { 36871b29fc2cSThomas Graf err = -ENOBUFS; 36881b29fc2cSThomas Graf goto errout_ifa; 36891b29fc2cSThomas Graf } 36901b29fc2cSThomas Graf 36916c223828SNoriaki TAKAMIYA err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid, 36926c223828SNoriaki TAKAMIYA nlh->nlmsg_seq, RTM_NEWADDR, 0); 369326932566SPatrick McHardy if (err < 0) { 369426932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 369526932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 369626932566SPatrick McHardy kfree_skb(skb); 369726932566SPatrick McHardy goto errout_ifa; 369826932566SPatrick McHardy } 36996fda7350SBenjamin Thery err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid); 37001b29fc2cSThomas Graf errout_ifa: 37016c223828SNoriaki TAKAMIYA in6_ifa_put(ifa); 37021b29fc2cSThomas Graf errout: 37036c223828SNoriaki TAKAMIYA return err; 37046c223828SNoriaki TAKAMIYA } 37056c223828SNoriaki TAKAMIYA 37061da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) 37071da177e4SLinus Torvalds { 37081da177e4SLinus Torvalds struct sk_buff *skb; 3709c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(ifa->idev->dev); 37105d620266SThomas Graf int err = -ENOBUFS; 37111da177e4SLinus Torvalds 37120ab6803bSThomas Graf skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); 37135d620266SThomas Graf if (skb == NULL) 37145d620266SThomas Graf goto errout; 37155d620266SThomas Graf 37165d620266SThomas Graf err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0); 371726932566SPatrick McHardy if (err < 0) { 371826932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 371926932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 372026932566SPatrick McHardy kfree_skb(skb); 372126932566SPatrick McHardy goto errout; 372226932566SPatrick McHardy } 37231ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 37241ce85fe4SPablo Neira Ayuso return; 37255d620266SThomas Graf errout: 37265d620266SThomas Graf if (err < 0) 37276fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); 37281da177e4SLinus Torvalds } 37291da177e4SLinus Torvalds 3730b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf, 37311da177e4SLinus Torvalds __s32 *array, int bytes) 37321da177e4SLinus Torvalds { 373304561c1fSThomas Graf BUG_ON(bytes < (DEVCONF_MAX * 4)); 373404561c1fSThomas Graf 37351da177e4SLinus Torvalds memset(array, 0, bytes); 37361da177e4SLinus Torvalds array[DEVCONF_FORWARDING] = cnf->forwarding; 37371da177e4SLinus Torvalds array[DEVCONF_HOPLIMIT] = cnf->hop_limit; 37381da177e4SLinus Torvalds array[DEVCONF_MTU6] = cnf->mtu6; 37391da177e4SLinus Torvalds array[DEVCONF_ACCEPT_RA] = cnf->accept_ra; 37401da177e4SLinus Torvalds array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects; 37411da177e4SLinus Torvalds array[DEVCONF_AUTOCONF] = cnf->autoconf; 37421da177e4SLinus Torvalds array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits; 37431da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits; 37441da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval; 37451da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay; 37461da177e4SLinus Torvalds array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version; 37471da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 37481da177e4SLinus Torvalds array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; 37491da177e4SLinus Torvalds array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; 37501da177e4SLinus Torvalds array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; 37511da177e4SLinus Torvalds array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; 37521da177e4SLinus Torvalds array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; 37531da177e4SLinus Torvalds #endif 37541da177e4SLinus Torvalds array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; 375565f5c7c1SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; 3756c4fd30ebSYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; 3757930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 3758930d6ff2SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref; 375952e16356SYOSHIFUJI Hideaki array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval; 3760fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 376109c884d4SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen; 376209c884d4SYOSHIFUJI Hideaki #endif 3763930d6ff2SYOSHIFUJI Hideaki #endif 3764fbea49e1SYOSHIFUJI Hideaki array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp; 37650bcbc926SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route; 376695c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 376795c385b4SNeil Horman array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad; 376895c385b4SNeil Horman #endif 37697bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 37707bc570c8SYOSHIFUJI Hideaki array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding; 37717bc570c8SYOSHIFUJI Hideaki #endif 3772778d80beSYOSHIFUJI Hideaki array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6; 37731b34be74SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad; 3774c3faca05SCosmin Ratiu array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao; 37751da177e4SLinus Torvalds } 37761da177e4SLinus Torvalds 3777339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void) 3778339bf98fSThomas Graf { 3779339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 3780339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 3781339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 3782339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 3783339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 3784339bf98fSThomas Graf + nla_total_size( /* IFLA_PROTINFO */ 3785339bf98fSThomas Graf nla_total_size(4) /* IFLA_INET6_FLAGS */ 3786339bf98fSThomas Graf + nla_total_size(sizeof(struct ifla_cacheinfo)) 3787339bf98fSThomas Graf + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */ 3788bf99f1bdSYOSHIFUJI Hideaki + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */ 3789bf99f1bdSYOSHIFUJI Hideaki + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */ 3790339bf98fSThomas Graf ); 3791339bf98fSThomas Graf } 3792c5396a31SYOSHIFUJI Hideaki 37937d720c3eSTejun Heo static inline void __snmp6_fill_stats(u64 *stats, void __percpu **mib, 37947d720c3eSTejun Heo int items, int bytes) 37957f7d9a6bSHerbert Xu { 37967f7d9a6bSHerbert Xu int i; 37977f7d9a6bSHerbert Xu int pad = bytes - sizeof(u64) * items; 37987f7d9a6bSHerbert Xu BUG_ON(pad < 0); 37997f7d9a6bSHerbert Xu 38007f7d9a6bSHerbert Xu /* Use put_unaligned() because stats may not be aligned for u64. */ 38017f7d9a6bSHerbert Xu put_unaligned(items, &stats[0]); 38027f7d9a6bSHerbert Xu for (i = 1; i < items; i++) 38037f7d9a6bSHerbert Xu put_unaligned(snmp_fold_field(mib, i), &stats[i]); 38047f7d9a6bSHerbert Xu 38057f7d9a6bSHerbert Xu memset(&stats[items], 0, pad); 38067f7d9a6bSHerbert Xu } 38077f7d9a6bSHerbert Xu 38087f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype, 38097f7d9a6bSHerbert Xu int bytes) 38107f7d9a6bSHerbert Xu { 38117f7d9a6bSHerbert Xu switch(attrtype) { 38127f7d9a6bSHerbert Xu case IFLA_INET6_STATS: 38137d720c3eSTejun Heo __snmp6_fill_stats(stats, (void __percpu **)idev->stats.ipv6, IPSTATS_MIB_MAX, bytes); 38147f7d9a6bSHerbert Xu break; 38157f7d9a6bSHerbert Xu case IFLA_INET6_ICMP6STATS: 38167d720c3eSTejun Heo __snmp6_fill_stats(stats, (void __percpu **)idev->stats.icmpv6, ICMP6_MIB_MAX, bytes); 38177f7d9a6bSHerbert Xu break; 38187f7d9a6bSHerbert Xu } 38197f7d9a6bSHerbert Xu } 38207f7d9a6bSHerbert Xu 38211da177e4SLinus Torvalds static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, 3822b6544c0bSJamal Hadi Salim u32 pid, u32 seq, int event, unsigned int flags) 38231da177e4SLinus Torvalds { 38241da177e4SLinus Torvalds struct net_device *dev = idev->dev; 3825bf99f1bdSYOSHIFUJI Hideaki struct nlattr *nla; 382604561c1fSThomas Graf struct ifinfomsg *hdr; 38271da177e4SLinus Torvalds struct nlmsghdr *nlh; 382804561c1fSThomas Graf void *protoinfo; 38291da177e4SLinus Torvalds struct ifla_cacheinfo ci; 38301da177e4SLinus Torvalds 383104561c1fSThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags); 383204561c1fSThomas Graf if (nlh == NULL) 383326932566SPatrick McHardy return -EMSGSIZE; 38341da177e4SLinus Torvalds 383504561c1fSThomas Graf hdr = nlmsg_data(nlh); 383604561c1fSThomas Graf hdr->ifi_family = AF_INET6; 383704561c1fSThomas Graf hdr->__ifi_pad = 0; 383804561c1fSThomas Graf hdr->ifi_type = dev->type; 383904561c1fSThomas Graf hdr->ifi_index = dev->ifindex; 384004561c1fSThomas Graf hdr->ifi_flags = dev_get_flags(dev); 384104561c1fSThomas Graf hdr->ifi_change = 0; 384204561c1fSThomas Graf 384304561c1fSThomas Graf NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name); 38441da177e4SLinus Torvalds 38451da177e4SLinus Torvalds if (dev->addr_len) 384604561c1fSThomas Graf NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr); 38471da177e4SLinus Torvalds 384804561c1fSThomas Graf NLA_PUT_U32(skb, IFLA_MTU, dev->mtu); 38491da177e4SLinus Torvalds if (dev->ifindex != dev->iflink) 385004561c1fSThomas Graf NLA_PUT_U32(skb, IFLA_LINK, dev->iflink); 38511da177e4SLinus Torvalds 385204561c1fSThomas Graf protoinfo = nla_nest_start(skb, IFLA_PROTINFO); 385304561c1fSThomas Graf if (protoinfo == NULL) 385404561c1fSThomas Graf goto nla_put_failure; 38551da177e4SLinus Torvalds 385604561c1fSThomas Graf NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags); 38571da177e4SLinus Torvalds 38581da177e4SLinus Torvalds ci.max_reasm_len = IPV6_MAXPLEN; 38591da177e4SLinus Torvalds ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100 38601da177e4SLinus Torvalds + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ); 38611da177e4SLinus Torvalds ci.reachable_time = idev->nd_parms->reachable_time; 38621da177e4SLinus Torvalds ci.retrans_time = idev->nd_parms->retrans_time; 386304561c1fSThomas Graf NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci); 38641da177e4SLinus Torvalds 3865bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); 3866bf99f1bdSYOSHIFUJI Hideaki if (nla == NULL) 386704561c1fSThomas Graf goto nla_put_failure; 3868bf99f1bdSYOSHIFUJI Hideaki ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); 38691da177e4SLinus Torvalds 3870bf99f1bdSYOSHIFUJI Hideaki /* XXX - MC not implemented */ 3871bf99f1bdSYOSHIFUJI Hideaki 3872bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); 3873bf99f1bdSYOSHIFUJI Hideaki if (nla == NULL) 3874bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 3875bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); 3876bf99f1bdSYOSHIFUJI Hideaki 3877bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); 3878bf99f1bdSYOSHIFUJI Hideaki if (nla == NULL) 3879bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 3880bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); 38811da177e4SLinus Torvalds 388204561c1fSThomas Graf nla_nest_end(skb, protoinfo); 388304561c1fSThomas Graf return nlmsg_end(skb, nlh); 38841da177e4SLinus Torvalds 388504561c1fSThomas Graf nla_put_failure: 388626932566SPatrick McHardy nlmsg_cancel(skb, nlh); 388726932566SPatrick McHardy return -EMSGSIZE; 38881da177e4SLinus Torvalds } 38891da177e4SLinus Torvalds 38901da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 38911da177e4SLinus Torvalds { 38923b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 389384d2697dSEric Dumazet int h, s_h; 3894434a8a58SDavid S. Miller int idx = 0, s_idx; 38951da177e4SLinus Torvalds struct net_device *dev; 38961da177e4SLinus Torvalds struct inet6_dev *idev; 389784d2697dSEric Dumazet struct hlist_head *head; 389884d2697dSEric Dumazet struct hlist_node *node; 38991da177e4SLinus Torvalds 390084d2697dSEric Dumazet s_h = cb->args[0]; 390184d2697dSEric Dumazet s_idx = cb->args[1]; 390284d2697dSEric Dumazet 390384d2697dSEric Dumazet rcu_read_lock(); 390484d2697dSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 39057562f876SPavel Emelianov idx = 0; 390684d2697dSEric Dumazet head = &net->dev_index_head[h]; 390784d2697dSEric Dumazet hlist_for_each_entry_rcu(dev, node, head, index_hlist) { 39081da177e4SLinus Torvalds if (idx < s_idx) 39097562f876SPavel Emelianov goto cont; 391084d2697dSEric Dumazet idev = __in6_dev_get(dev); 391184d2697dSEric Dumazet if (!idev) 39127562f876SPavel Emelianov goto cont; 391384d2697dSEric Dumazet if (inet6_fill_ifinfo(skb, idev, 391484d2697dSEric Dumazet NETLINK_CB(cb->skb).pid, 391584d2697dSEric Dumazet cb->nlh->nlmsg_seq, 391684d2697dSEric Dumazet RTM_NEWLINK, NLM_F_MULTI) <= 0) 391784d2697dSEric Dumazet goto out; 39187562f876SPavel Emelianov cont: 39197562f876SPavel Emelianov idx++; 39201da177e4SLinus Torvalds } 392184d2697dSEric Dumazet } 392284d2697dSEric Dumazet out: 392384d2697dSEric Dumazet rcu_read_unlock(); 392484d2697dSEric Dumazet cb->args[1] = idx; 392584d2697dSEric Dumazet cb->args[0] = h; 39261da177e4SLinus Torvalds 39271da177e4SLinus Torvalds return skb->len; 39281da177e4SLinus Torvalds } 39291da177e4SLinus Torvalds 39301da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev) 39311da177e4SLinus Torvalds { 39321da177e4SLinus Torvalds struct sk_buff *skb; 3933c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 39348d7a76c9SThomas Graf int err = -ENOBUFS; 39351da177e4SLinus Torvalds 3936339bf98fSThomas Graf skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); 39378d7a76c9SThomas Graf if (skb == NULL) 39388d7a76c9SThomas Graf goto errout; 39398d7a76c9SThomas Graf 39408d7a76c9SThomas Graf err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); 394126932566SPatrick McHardy if (err < 0) { 394226932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */ 394326932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 394426932566SPatrick McHardy kfree_skb(skb); 394526932566SPatrick McHardy goto errout; 394626932566SPatrick McHardy } 39471ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 39481ce85fe4SPablo Neira Ayuso return; 39498d7a76c9SThomas Graf errout: 39508d7a76c9SThomas Graf if (err < 0) 39516fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); 39521da177e4SLinus Torvalds } 39531da177e4SLinus Torvalds 3954339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void) 3955339bf98fSThomas Graf { 3956339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct prefixmsg)) 3957339bf98fSThomas Graf + nla_total_size(sizeof(struct in6_addr)) 3958339bf98fSThomas Graf + nla_total_size(sizeof(struct prefix_cacheinfo)); 3959339bf98fSThomas Graf } 3960c5396a31SYOSHIFUJI Hideaki 39611da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, 3962b6544c0bSJamal Hadi Salim struct prefix_info *pinfo, u32 pid, u32 seq, 3963b6544c0bSJamal Hadi Salim int event, unsigned int flags) 39641da177e4SLinus Torvalds { 39651da177e4SLinus Torvalds struct prefixmsg *pmsg; 39661da177e4SLinus Torvalds struct nlmsghdr *nlh; 39671da177e4SLinus Torvalds struct prefix_cacheinfo ci; 39681da177e4SLinus Torvalds 39696051e2f4SThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags); 39706051e2f4SThomas Graf if (nlh == NULL) 397126932566SPatrick McHardy return -EMSGSIZE; 39726051e2f4SThomas Graf 39736051e2f4SThomas Graf pmsg = nlmsg_data(nlh); 39741da177e4SLinus Torvalds pmsg->prefix_family = AF_INET6; 39758a47077aSPatrick McHardy pmsg->prefix_pad1 = 0; 39768a47077aSPatrick McHardy pmsg->prefix_pad2 = 0; 39771da177e4SLinus Torvalds pmsg->prefix_ifindex = idev->dev->ifindex; 39781da177e4SLinus Torvalds pmsg->prefix_len = pinfo->prefix_len; 39791da177e4SLinus Torvalds pmsg->prefix_type = pinfo->type; 39808a47077aSPatrick McHardy pmsg->prefix_pad3 = 0; 39811da177e4SLinus Torvalds pmsg->prefix_flags = 0; 39821da177e4SLinus Torvalds if (pinfo->onlink) 39831da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_ONLINK; 39841da177e4SLinus Torvalds if (pinfo->autoconf) 39851da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_AUTOCONF; 39861da177e4SLinus Torvalds 39876051e2f4SThomas Graf NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix); 39881da177e4SLinus Torvalds 39891da177e4SLinus Torvalds ci.preferred_time = ntohl(pinfo->prefered); 39901da177e4SLinus Torvalds ci.valid_time = ntohl(pinfo->valid); 39916051e2f4SThomas Graf NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci); 39921da177e4SLinus Torvalds 39936051e2f4SThomas Graf return nlmsg_end(skb, nlh); 39941da177e4SLinus Torvalds 39956051e2f4SThomas Graf nla_put_failure: 399626932566SPatrick McHardy nlmsg_cancel(skb, nlh); 399726932566SPatrick McHardy return -EMSGSIZE; 39981da177e4SLinus Torvalds } 39991da177e4SLinus Torvalds 40001da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 40011da177e4SLinus Torvalds struct prefix_info *pinfo) 40021da177e4SLinus Torvalds { 40031da177e4SLinus Torvalds struct sk_buff *skb; 4004c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 40058c384bfaSThomas Graf int err = -ENOBUFS; 40061da177e4SLinus Torvalds 4007339bf98fSThomas Graf skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); 40088c384bfaSThomas Graf if (skb == NULL) 40098c384bfaSThomas Graf goto errout; 40108c384bfaSThomas Graf 40118c384bfaSThomas Graf err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); 401226932566SPatrick McHardy if (err < 0) { 401326932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */ 401426932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 401526932566SPatrick McHardy kfree_skb(skb); 401626932566SPatrick McHardy goto errout; 401726932566SPatrick McHardy } 40181ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); 40191ce85fe4SPablo Neira Ayuso return; 40208c384bfaSThomas Graf errout: 40218c384bfaSThomas Graf if (err < 0) 40226fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err); 40231da177e4SLinus Torvalds } 40241da177e4SLinus Torvalds 40251da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 40261da177e4SLinus Torvalds { 40271da177e4SLinus Torvalds inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); 40281da177e4SLinus Torvalds 40291da177e4SLinus Torvalds switch (event) { 40301da177e4SLinus Torvalds case RTM_NEWADDR: 403195c385b4SNeil Horman /* 403295c385b4SNeil Horman * If the address was optimistic 403395c385b4SNeil Horman * we inserted the route at the start of 403495c385b4SNeil Horman * our DAD process, so we don't need 403595c385b4SNeil Horman * to do it again 403695c385b4SNeil Horman */ 403795c385b4SNeil Horman if (!(ifp->rt->rt6i_node)) 403840e22e8fSThomas Graf ip6_ins_rt(ifp->rt); 40391da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 40401da177e4SLinus Torvalds addrconf_join_anycast(ifp); 40411da177e4SLinus Torvalds break; 40421da177e4SLinus Torvalds case RTM_DELADDR: 40431da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 40441da177e4SLinus Torvalds addrconf_leave_anycast(ifp); 40451da177e4SLinus Torvalds addrconf_leave_solict(ifp->idev, &ifp->addr); 40461da177e4SLinus Torvalds dst_hold(&ifp->rt->u.dst); 4047e0a1ad73SThomas Graf if (ip6_del_rt(ifp->rt)) 40481da177e4SLinus Torvalds dst_free(&ifp->rt->u.dst); 40491da177e4SLinus Torvalds break; 40501da177e4SLinus Torvalds } 40511da177e4SLinus Torvalds } 40521da177e4SLinus Torvalds 40531da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 40541da177e4SLinus Torvalds { 40558814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 40561da177e4SLinus Torvalds if (likely(ifp->idev->dead == 0)) 40571da177e4SLinus Torvalds __ipv6_ifa_notify(event, ifp); 40588814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 40591da177e4SLinus Torvalds } 40601da177e4SLinus Torvalds 40611da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 40621da177e4SLinus Torvalds 40631da177e4SLinus Torvalds static 40648d65af78SAlexey Dobriyan int addrconf_sysctl_forward(ctl_table *ctl, int write, 40651da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 40661da177e4SLinus Torvalds { 40671da177e4SLinus Torvalds int *valp = ctl->data; 40681da177e4SLinus Torvalds int val = *valp; 406988af182eSEric W. Biederman loff_t pos = *ppos; 40701da177e4SLinus Torvalds int ret; 40711da177e4SLinus Torvalds 40728d65af78SAlexey Dobriyan ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 40731da177e4SLinus Torvalds 4074c8fecf22SPavel Emelyanov if (write) 4075b325fddbSStephen Hemminger ret = addrconf_fixup_forwarding(ctl, valp, val); 407688af182eSEric W. Biederman if (ret) 407788af182eSEric W. Biederman *ppos = pos; 40781da177e4SLinus Torvalds return ret; 40791da177e4SLinus Torvalds } 40801da177e4SLinus Torvalds 408156d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev) 408256d417b1SBrian Haley { 408356d417b1SBrian Haley if (!idev || !idev->dev) 408456d417b1SBrian Haley return; 408556d417b1SBrian Haley 408656d417b1SBrian Haley if (idev->cnf.disable_ipv6) 408756d417b1SBrian Haley addrconf_notify(NULL, NETDEV_DOWN, idev->dev); 408856d417b1SBrian Haley else 408956d417b1SBrian Haley addrconf_notify(NULL, NETDEV_UP, idev->dev); 409056d417b1SBrian Haley } 409156d417b1SBrian Haley 409256d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf) 409356d417b1SBrian Haley { 409456d417b1SBrian Haley struct net_device *dev; 409556d417b1SBrian Haley struct inet6_dev *idev; 409656d417b1SBrian Haley 409756d417b1SBrian Haley rcu_read_lock(); 4098c6d14c84SEric Dumazet for_each_netdev_rcu(net, dev) { 409956d417b1SBrian Haley idev = __in6_dev_get(dev); 410056d417b1SBrian Haley if (idev) { 410156d417b1SBrian Haley int changed = (!idev->cnf.disable_ipv6) ^ (!newf); 410256d417b1SBrian Haley idev->cnf.disable_ipv6 = newf; 410356d417b1SBrian Haley if (changed) 410456d417b1SBrian Haley dev_disable_change(idev); 410556d417b1SBrian Haley } 410656d417b1SBrian Haley } 4107c6d14c84SEric Dumazet rcu_read_unlock(); 410856d417b1SBrian Haley } 410956d417b1SBrian Haley 411056d417b1SBrian Haley static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int old) 411156d417b1SBrian Haley { 411256d417b1SBrian Haley struct net *net; 411356d417b1SBrian Haley 411456d417b1SBrian Haley net = (struct net *)table->extra2; 411556d417b1SBrian Haley 411656d417b1SBrian Haley if (p == &net->ipv6.devconf_dflt->disable_ipv6) 411756d417b1SBrian Haley return 0; 411856d417b1SBrian Haley 411988af182eSEric W. Biederman if (!rtnl_trylock()) { 412088af182eSEric W. Biederman /* Restore the original values before restarting */ 412188af182eSEric W. Biederman *p = old; 412256d417b1SBrian Haley return restart_syscall(); 412388af182eSEric W. Biederman } 412456d417b1SBrian Haley 412556d417b1SBrian Haley if (p == &net->ipv6.devconf_all->disable_ipv6) { 412656d417b1SBrian Haley __s32 newf = net->ipv6.devconf_all->disable_ipv6; 412756d417b1SBrian Haley net->ipv6.devconf_dflt->disable_ipv6 = newf; 412856d417b1SBrian Haley addrconf_disable_change(net, newf); 412956d417b1SBrian Haley } else if ((!*p) ^ (!old)) 413056d417b1SBrian Haley dev_disable_change((struct inet6_dev *)table->extra1); 413156d417b1SBrian Haley 413256d417b1SBrian Haley rtnl_unlock(); 413356d417b1SBrian Haley return 0; 413456d417b1SBrian Haley } 413556d417b1SBrian Haley 413656d417b1SBrian Haley static 41378d65af78SAlexey Dobriyan int addrconf_sysctl_disable(ctl_table *ctl, int write, 413856d417b1SBrian Haley void __user *buffer, size_t *lenp, loff_t *ppos) 413956d417b1SBrian Haley { 414056d417b1SBrian Haley int *valp = ctl->data; 414156d417b1SBrian Haley int val = *valp; 414288af182eSEric W. Biederman loff_t pos = *ppos; 414356d417b1SBrian Haley int ret; 414456d417b1SBrian Haley 41458d65af78SAlexey Dobriyan ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 414656d417b1SBrian Haley 414756d417b1SBrian Haley if (write) 414856d417b1SBrian Haley ret = addrconf_disable_ipv6(ctl, valp, val); 414988af182eSEric W. Biederman if (ret) 415088af182eSEric W. Biederman *ppos = pos; 415156d417b1SBrian Haley return ret; 415256d417b1SBrian Haley } 415356d417b1SBrian Haley 41541da177e4SLinus Torvalds static struct addrconf_sysctl_table 41551da177e4SLinus Torvalds { 41561da177e4SLinus Torvalds struct ctl_table_header *sysctl_header; 4157f6a07b29SYOSHIFUJI Hideaki ctl_table addrconf_vars[DEVCONF_MAX+1]; 41581dab6222SPavel Emelyanov char *dev_name; 4159ab32ea5dSBrian Haley } addrconf_sysctl __read_mostly = { 41601da177e4SLinus Torvalds .sysctl_header = NULL, 41611da177e4SLinus Torvalds .addrconf_vars = { 41621da177e4SLinus Torvalds { 41631da177e4SLinus Torvalds .procname = "forwarding", 41641da177e4SLinus Torvalds .data = &ipv6_devconf.forwarding, 41651da177e4SLinus Torvalds .maxlen = sizeof(int), 41661da177e4SLinus Torvalds .mode = 0644, 41676d9f239aSAlexey Dobriyan .proc_handler = addrconf_sysctl_forward, 41681da177e4SLinus Torvalds }, 41691da177e4SLinus Torvalds { 41701da177e4SLinus Torvalds .procname = "hop_limit", 41711da177e4SLinus Torvalds .data = &ipv6_devconf.hop_limit, 41721da177e4SLinus Torvalds .maxlen = sizeof(int), 41731da177e4SLinus Torvalds .mode = 0644, 41741da177e4SLinus Torvalds .proc_handler = proc_dointvec, 41751da177e4SLinus Torvalds }, 41761da177e4SLinus Torvalds { 41771da177e4SLinus Torvalds .procname = "mtu", 41781da177e4SLinus Torvalds .data = &ipv6_devconf.mtu6, 41791da177e4SLinus Torvalds .maxlen = sizeof(int), 41801da177e4SLinus Torvalds .mode = 0644, 41816d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 41821da177e4SLinus Torvalds }, 41831da177e4SLinus Torvalds { 41841da177e4SLinus Torvalds .procname = "accept_ra", 41851da177e4SLinus Torvalds .data = &ipv6_devconf.accept_ra, 41861da177e4SLinus Torvalds .maxlen = sizeof(int), 41871da177e4SLinus Torvalds .mode = 0644, 41886d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 41891da177e4SLinus Torvalds }, 41901da177e4SLinus Torvalds { 41911da177e4SLinus Torvalds .procname = "accept_redirects", 41921da177e4SLinus Torvalds .data = &ipv6_devconf.accept_redirects, 41931da177e4SLinus Torvalds .maxlen = sizeof(int), 41941da177e4SLinus Torvalds .mode = 0644, 41956d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 41961da177e4SLinus Torvalds }, 41971da177e4SLinus Torvalds { 41981da177e4SLinus Torvalds .procname = "autoconf", 41991da177e4SLinus Torvalds .data = &ipv6_devconf.autoconf, 42001da177e4SLinus Torvalds .maxlen = sizeof(int), 42011da177e4SLinus Torvalds .mode = 0644, 42026d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 42031da177e4SLinus Torvalds }, 42041da177e4SLinus Torvalds { 42051da177e4SLinus Torvalds .procname = "dad_transmits", 42061da177e4SLinus Torvalds .data = &ipv6_devconf.dad_transmits, 42071da177e4SLinus Torvalds .maxlen = sizeof(int), 42081da177e4SLinus Torvalds .mode = 0644, 42096d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 42101da177e4SLinus Torvalds }, 42111da177e4SLinus Torvalds { 42121da177e4SLinus Torvalds .procname = "router_solicitations", 42131da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicits, 42141da177e4SLinus Torvalds .maxlen = sizeof(int), 42151da177e4SLinus Torvalds .mode = 0644, 42166d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 42171da177e4SLinus Torvalds }, 42181da177e4SLinus Torvalds { 42191da177e4SLinus Torvalds .procname = "router_solicitation_interval", 42201da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_interval, 42211da177e4SLinus Torvalds .maxlen = sizeof(int), 42221da177e4SLinus Torvalds .mode = 0644, 42236d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 42241da177e4SLinus Torvalds }, 42251da177e4SLinus Torvalds { 42261da177e4SLinus Torvalds .procname = "router_solicitation_delay", 42271da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_delay, 42281da177e4SLinus Torvalds .maxlen = sizeof(int), 42291da177e4SLinus Torvalds .mode = 0644, 42306d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 42311da177e4SLinus Torvalds }, 42321da177e4SLinus Torvalds { 42331da177e4SLinus Torvalds .procname = "force_mld_version", 42341da177e4SLinus Torvalds .data = &ipv6_devconf.force_mld_version, 42351da177e4SLinus Torvalds .maxlen = sizeof(int), 42361da177e4SLinus Torvalds .mode = 0644, 42376d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 42381da177e4SLinus Torvalds }, 42391da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 42401da177e4SLinus Torvalds { 42411da177e4SLinus Torvalds .procname = "use_tempaddr", 42421da177e4SLinus Torvalds .data = &ipv6_devconf.use_tempaddr, 42431da177e4SLinus Torvalds .maxlen = sizeof(int), 42441da177e4SLinus Torvalds .mode = 0644, 42456d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 42461da177e4SLinus Torvalds }, 42471da177e4SLinus Torvalds { 42481da177e4SLinus Torvalds .procname = "temp_valid_lft", 42491da177e4SLinus Torvalds .data = &ipv6_devconf.temp_valid_lft, 42501da177e4SLinus Torvalds .maxlen = sizeof(int), 42511da177e4SLinus Torvalds .mode = 0644, 42526d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 42531da177e4SLinus Torvalds }, 42541da177e4SLinus Torvalds { 42551da177e4SLinus Torvalds .procname = "temp_prefered_lft", 42561da177e4SLinus Torvalds .data = &ipv6_devconf.temp_prefered_lft, 42571da177e4SLinus Torvalds .maxlen = sizeof(int), 42581da177e4SLinus Torvalds .mode = 0644, 42596d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 42601da177e4SLinus Torvalds }, 42611da177e4SLinus Torvalds { 42621da177e4SLinus Torvalds .procname = "regen_max_retry", 42631da177e4SLinus Torvalds .data = &ipv6_devconf.regen_max_retry, 42641da177e4SLinus Torvalds .maxlen = sizeof(int), 42651da177e4SLinus Torvalds .mode = 0644, 42666d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 42671da177e4SLinus Torvalds }, 42681da177e4SLinus Torvalds { 42691da177e4SLinus Torvalds .procname = "max_desync_factor", 42701da177e4SLinus Torvalds .data = &ipv6_devconf.max_desync_factor, 42711da177e4SLinus Torvalds .maxlen = sizeof(int), 42721da177e4SLinus Torvalds .mode = 0644, 42736d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 42741da177e4SLinus Torvalds }, 42751da177e4SLinus Torvalds #endif 42761da177e4SLinus Torvalds { 42771da177e4SLinus Torvalds .procname = "max_addresses", 42781da177e4SLinus Torvalds .data = &ipv6_devconf.max_addresses, 42791da177e4SLinus Torvalds .maxlen = sizeof(int), 42801da177e4SLinus Torvalds .mode = 0644, 42816d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 42821da177e4SLinus Torvalds }, 42831da177e4SLinus Torvalds { 428465f5c7c1SYOSHIFUJI Hideaki .procname = "accept_ra_defrtr", 428565f5c7c1SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_defrtr, 428665f5c7c1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 428765f5c7c1SYOSHIFUJI Hideaki .mode = 0644, 42886d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 428965f5c7c1SYOSHIFUJI Hideaki }, 429065f5c7c1SYOSHIFUJI Hideaki { 4291c4fd30ebSYOSHIFUJI Hideaki .procname = "accept_ra_pinfo", 4292c4fd30ebSYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_pinfo, 4293c4fd30ebSYOSHIFUJI Hideaki .maxlen = sizeof(int), 4294c4fd30ebSYOSHIFUJI Hideaki .mode = 0644, 42956d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 4296c4fd30ebSYOSHIFUJI Hideaki }, 4297930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 4298930d6ff2SYOSHIFUJI Hideaki { 4299930d6ff2SYOSHIFUJI Hideaki .procname = "accept_ra_rtr_pref", 4300930d6ff2SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rtr_pref, 4301930d6ff2SYOSHIFUJI Hideaki .maxlen = sizeof(int), 4302930d6ff2SYOSHIFUJI Hideaki .mode = 0644, 43036d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 4304930d6ff2SYOSHIFUJI Hideaki }, 430552e16356SYOSHIFUJI Hideaki { 430652e16356SYOSHIFUJI Hideaki .procname = "router_probe_interval", 430752e16356SYOSHIFUJI Hideaki .data = &ipv6_devconf.rtr_probe_interval, 430852e16356SYOSHIFUJI Hideaki .maxlen = sizeof(int), 430952e16356SYOSHIFUJI Hideaki .mode = 0644, 43106d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 431152e16356SYOSHIFUJI Hideaki }, 4312fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 431309c884d4SYOSHIFUJI Hideaki { 431409c884d4SYOSHIFUJI Hideaki .procname = "accept_ra_rt_info_max_plen", 431509c884d4SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rt_info_max_plen, 431609c884d4SYOSHIFUJI Hideaki .maxlen = sizeof(int), 431709c884d4SYOSHIFUJI Hideaki .mode = 0644, 43186d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 431909c884d4SYOSHIFUJI Hideaki }, 432009c884d4SYOSHIFUJI Hideaki #endif 4321930d6ff2SYOSHIFUJI Hideaki #endif 4322c4fd30ebSYOSHIFUJI Hideaki { 4323fbea49e1SYOSHIFUJI Hideaki .procname = "proxy_ndp", 4324fbea49e1SYOSHIFUJI Hideaki .data = &ipv6_devconf.proxy_ndp, 4325fbea49e1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 4326fbea49e1SYOSHIFUJI Hideaki .mode = 0644, 43276d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 4328fbea49e1SYOSHIFUJI Hideaki }, 4329fbea49e1SYOSHIFUJI Hideaki { 43300bcbc926SYOSHIFUJI Hideaki .procname = "accept_source_route", 43310bcbc926SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_source_route, 43320bcbc926SYOSHIFUJI Hideaki .maxlen = sizeof(int), 43330bcbc926SYOSHIFUJI Hideaki .mode = 0644, 43346d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 43350bcbc926SYOSHIFUJI Hideaki }, 433695c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 433795c385b4SNeil Horman { 433895c385b4SNeil Horman .procname = "optimistic_dad", 433995c385b4SNeil Horman .data = &ipv6_devconf.optimistic_dad, 434095c385b4SNeil Horman .maxlen = sizeof(int), 434195c385b4SNeil Horman .mode = 0644, 43426d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 434395c385b4SNeil Horman 434495c385b4SNeil Horman }, 434595c385b4SNeil Horman #endif 43467bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 43477bc570c8SYOSHIFUJI Hideaki { 43487bc570c8SYOSHIFUJI Hideaki .procname = "mc_forwarding", 43497bc570c8SYOSHIFUJI Hideaki .data = &ipv6_devconf.mc_forwarding, 43507bc570c8SYOSHIFUJI Hideaki .maxlen = sizeof(int), 4351a4e6db07SDavid S. Miller .mode = 0444, 43526d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 43537bc570c8SYOSHIFUJI Hideaki }, 43547bc570c8SYOSHIFUJI Hideaki #endif 43550bcbc926SYOSHIFUJI Hideaki { 4356778d80beSYOSHIFUJI Hideaki .procname = "disable_ipv6", 4357778d80beSYOSHIFUJI Hideaki .data = &ipv6_devconf.disable_ipv6, 4358778d80beSYOSHIFUJI Hideaki .maxlen = sizeof(int), 4359778d80beSYOSHIFUJI Hideaki .mode = 0644, 436056d417b1SBrian Haley .proc_handler = addrconf_sysctl_disable, 4361778d80beSYOSHIFUJI Hideaki }, 4362778d80beSYOSHIFUJI Hideaki { 43631b34be74SYOSHIFUJI Hideaki .procname = "accept_dad", 43641b34be74SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_dad, 43651b34be74SYOSHIFUJI Hideaki .maxlen = sizeof(int), 43661b34be74SYOSHIFUJI Hideaki .mode = 0644, 43676d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 43681b34be74SYOSHIFUJI Hideaki }, 43691b34be74SYOSHIFUJI Hideaki { 4370f7734fdfSOctavian Purdila .procname = "force_tllao", 4371f7734fdfSOctavian Purdila .data = &ipv6_devconf.force_tllao, 4372f7734fdfSOctavian Purdila .maxlen = sizeof(int), 4373f7734fdfSOctavian Purdila .mode = 0644, 4374f7734fdfSOctavian Purdila .proc_handler = proc_dointvec 4375f7734fdfSOctavian Purdila }, 4376f7734fdfSOctavian Purdila { 4377f8572d8fSEric W. Biederman /* sentinel */ 43781da177e4SLinus Torvalds } 43791da177e4SLinus Torvalds }, 43801da177e4SLinus Torvalds }; 43811da177e4SLinus Torvalds 4382bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name, 4383f8572d8fSEric W. Biederman struct inet6_dev *idev, struct ipv6_devconf *p) 43841da177e4SLinus Torvalds { 43851da177e4SLinus Torvalds int i; 43861da177e4SLinus Torvalds struct addrconf_sysctl_table *t; 43871da177e4SLinus Torvalds 43881dab6222SPavel Emelyanov #define ADDRCONF_CTL_PATH_DEV 3 43891dab6222SPavel Emelyanov 43901dab6222SPavel Emelyanov struct ctl_path addrconf_ctl_path[] = { 4391f8572d8fSEric W. Biederman { .procname = "net", }, 4392f8572d8fSEric W. Biederman { .procname = "ipv6", }, 4393f8572d8fSEric W. Biederman { .procname = "conf", }, 43941dab6222SPavel Emelyanov { /* to be set */ }, 43951dab6222SPavel Emelyanov { }, 43961dab6222SPavel Emelyanov }; 43971dab6222SPavel Emelyanov 43981dab6222SPavel Emelyanov 4399af879cc7SArnaldo Carvalho de Melo t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL); 44001da177e4SLinus Torvalds if (t == NULL) 4401f68635e6SPavel Emelyanov goto out; 4402f68635e6SPavel Emelyanov 44031da177e4SLinus Torvalds for (i=0; t->addrconf_vars[i].data; i++) { 44041da177e4SLinus Torvalds t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf; 44051da177e4SLinus Torvalds t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */ 4406bff16c2fSPavel Emelyanov t->addrconf_vars[i].extra2 = net; 44071da177e4SLinus Torvalds } 44081da177e4SLinus Torvalds 44091da177e4SLinus Torvalds /* 44101da177e4SLinus Torvalds * Make a copy of dev_name, because '.procname' is regarded as const 44111da177e4SLinus Torvalds * by sysctl and we wouldn't want anyone to change it under our feet 44121da177e4SLinus Torvalds * (see SIOCSIFNAME). 44131da177e4SLinus Torvalds */ 44141dab6222SPavel Emelyanov t->dev_name = kstrdup(dev_name, GFP_KERNEL); 44151dab6222SPavel Emelyanov if (!t->dev_name) 44161da177e4SLinus Torvalds goto free; 44171da177e4SLinus Torvalds 44181dab6222SPavel Emelyanov addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].procname = t->dev_name; 44191da177e4SLinus Torvalds 4420bff16c2fSPavel Emelyanov t->sysctl_header = register_net_sysctl_table(net, addrconf_ctl_path, 44211dab6222SPavel Emelyanov t->addrconf_vars); 44221da177e4SLinus Torvalds if (t->sysctl_header == NULL) 44231da177e4SLinus Torvalds goto free_procname; 4424f68635e6SPavel Emelyanov 44251da177e4SLinus Torvalds p->sysctl = t; 442695897312SPavel Emelyanov return 0; 44271da177e4SLinus Torvalds 44281da177e4SLinus Torvalds free_procname: 44291dab6222SPavel Emelyanov kfree(t->dev_name); 44301da177e4SLinus Torvalds free: 44311da177e4SLinus Torvalds kfree(t); 4432f68635e6SPavel Emelyanov out: 443395897312SPavel Emelyanov return -ENOBUFS; 44341da177e4SLinus Torvalds } 44351da177e4SLinus Torvalds 4436408c4768SPavel Emelyanov static void __addrconf_sysctl_unregister(struct ipv6_devconf *p) 4437f52295a9SPavel Emelyanov { 4438408c4768SPavel Emelyanov struct addrconf_sysctl_table *t; 4439f52295a9SPavel Emelyanov 4440408c4768SPavel Emelyanov if (p->sysctl == NULL) 4441408c4768SPavel Emelyanov return; 4442408c4768SPavel Emelyanov 4443408c4768SPavel Emelyanov t = p->sysctl; 44441da177e4SLinus Torvalds p->sysctl = NULL; 44451da177e4SLinus Torvalds unregister_sysctl_table(t->sysctl_header); 44461dab6222SPavel Emelyanov kfree(t->dev_name); 44471da177e4SLinus Torvalds kfree(t); 44481da177e4SLinus Torvalds } 4449408c4768SPavel Emelyanov 4450408c4768SPavel Emelyanov static void addrconf_sysctl_register(struct inet6_dev *idev) 4451408c4768SPavel Emelyanov { 445254716e3bSEric W. Biederman neigh_sysctl_register(idev->dev, idev->nd_parms, "ipv6", 4453f8572d8fSEric W. Biederman &ndisc_ifinfo_sysctl_change); 4454c346dca1SYOSHIFUJI Hideaki __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name, 4455f8572d8fSEric W. Biederman idev, &idev->cnf); 4456408c4768SPavel Emelyanov } 4457408c4768SPavel Emelyanov 4458408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev) 4459408c4768SPavel Emelyanov { 4460408c4768SPavel Emelyanov __addrconf_sysctl_unregister(&idev->cnf); 4461408c4768SPavel Emelyanov neigh_sysctl_unregister(idev->nd_parms); 44621da177e4SLinus Torvalds } 44631da177e4SLinus Torvalds 44641da177e4SLinus Torvalds 44651da177e4SLinus Torvalds #endif 44661da177e4SLinus Torvalds 44672c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net) 4468e0da5a48SPavel Emelyanov { 4469e0da5a48SPavel Emelyanov int err; 4470e0da5a48SPavel Emelyanov struct ipv6_devconf *all, *dflt; 4471e0da5a48SPavel Emelyanov 4472e0da5a48SPavel Emelyanov err = -ENOMEM; 4473e0da5a48SPavel Emelyanov all = &ipv6_devconf; 4474e0da5a48SPavel Emelyanov dflt = &ipv6_devconf_dflt; 4475e0da5a48SPavel Emelyanov 447609ad9bc7SOctavian Purdila if (!net_eq(net, &init_net)) { 4477e0da5a48SPavel Emelyanov all = kmemdup(all, sizeof(ipv6_devconf), GFP_KERNEL); 4478e0da5a48SPavel Emelyanov if (all == NULL) 4479e0da5a48SPavel Emelyanov goto err_alloc_all; 4480e0da5a48SPavel Emelyanov 4481e0da5a48SPavel Emelyanov dflt = kmemdup(dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); 4482e0da5a48SPavel Emelyanov if (dflt == NULL) 4483e0da5a48SPavel Emelyanov goto err_alloc_dflt; 448456d417b1SBrian Haley } else { 448556d417b1SBrian Haley /* these will be inherited by all namespaces */ 448656d417b1SBrian Haley dflt->autoconf = ipv6_defaults.autoconf; 448756d417b1SBrian Haley dflt->disable_ipv6 = ipv6_defaults.disable_ipv6; 4488e0da5a48SPavel Emelyanov } 4489e0da5a48SPavel Emelyanov 4490e0da5a48SPavel Emelyanov net->ipv6.devconf_all = all; 4491e0da5a48SPavel Emelyanov net->ipv6.devconf_dflt = dflt; 4492e0da5a48SPavel Emelyanov 4493e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 4494f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "all", NULL, all); 4495e0da5a48SPavel Emelyanov if (err < 0) 4496e0da5a48SPavel Emelyanov goto err_reg_all; 4497e0da5a48SPavel Emelyanov 4498f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "default", NULL, dflt); 4499e0da5a48SPavel Emelyanov if (err < 0) 4500e0da5a48SPavel Emelyanov goto err_reg_dflt; 4501e0da5a48SPavel Emelyanov #endif 4502e0da5a48SPavel Emelyanov return 0; 4503e0da5a48SPavel Emelyanov 4504e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 4505e0da5a48SPavel Emelyanov err_reg_dflt: 4506e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(all); 4507e0da5a48SPavel Emelyanov err_reg_all: 4508e0da5a48SPavel Emelyanov kfree(dflt); 4509e0da5a48SPavel Emelyanov #endif 4510e0da5a48SPavel Emelyanov err_alloc_dflt: 4511e0da5a48SPavel Emelyanov kfree(all); 4512e0da5a48SPavel Emelyanov err_alloc_all: 4513e0da5a48SPavel Emelyanov return err; 4514e0da5a48SPavel Emelyanov } 4515e0da5a48SPavel Emelyanov 45162c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net) 4517e0da5a48SPavel Emelyanov { 4518e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 4519e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(net->ipv6.devconf_dflt); 4520e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(net->ipv6.devconf_all); 4521e0da5a48SPavel Emelyanov #endif 452209ad9bc7SOctavian Purdila if (!net_eq(net, &init_net)) { 4523e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_dflt); 4524e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_all); 4525e0da5a48SPavel Emelyanov } 4526e0da5a48SPavel Emelyanov } 4527e0da5a48SPavel Emelyanov 4528e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = { 4529e0da5a48SPavel Emelyanov .init = addrconf_init_net, 4530e0da5a48SPavel Emelyanov .exit = addrconf_exit_net, 4531e0da5a48SPavel Emelyanov }; 4532e0da5a48SPavel Emelyanov 45331da177e4SLinus Torvalds /* 45341da177e4SLinus Torvalds * Device notifier 45351da177e4SLinus Torvalds */ 45361da177e4SLinus Torvalds 45371da177e4SLinus Torvalds int register_inet6addr_notifier(struct notifier_block *nb) 45381da177e4SLinus Torvalds { 4539e041c683SAlan Stern return atomic_notifier_chain_register(&inet6addr_chain, nb); 45401da177e4SLinus Torvalds } 45411da177e4SLinus Torvalds 45427159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(register_inet6addr_notifier); 45437159039aSYOSHIFUJI Hideaki 45441da177e4SLinus Torvalds int unregister_inet6addr_notifier(struct notifier_block *nb) 45451da177e4SLinus Torvalds { 4546e041c683SAlan Stern return atomic_notifier_chain_unregister(&inet6addr_chain,nb); 45471da177e4SLinus Torvalds } 45481da177e4SLinus Torvalds 45497159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(unregister_inet6addr_notifier); 45507159039aSYOSHIFUJI Hideaki 45511da177e4SLinus Torvalds /* 45521da177e4SLinus Torvalds * Init / cleanup code 45531da177e4SLinus Torvalds */ 45541da177e4SLinus Torvalds 45551da177e4SLinus Torvalds int __init addrconf_init(void) 45561da177e4SLinus Torvalds { 45572a8cc6c8SYOSHIFUJI Hideaki int err; 45582a8cc6c8SYOSHIFUJI Hideaki 45592a8cc6c8SYOSHIFUJI Hideaki if ((err = ipv6_addr_label_init()) < 0) { 45602a8cc6c8SYOSHIFUJI Hideaki printk(KERN_CRIT "IPv6 Addrconf: cannot initialize default policy table: %d.\n", 45612a8cc6c8SYOSHIFUJI Hideaki err); 45622a8cc6c8SYOSHIFUJI Hideaki return err; 45632a8cc6c8SYOSHIFUJI Hideaki } 45641da177e4SLinus Torvalds 4565e0da5a48SPavel Emelyanov register_pernet_subsys(&addrconf_ops); 4566e0da5a48SPavel Emelyanov 45671da177e4SLinus Torvalds /* The addrconf netdev notifier requires that loopback_dev 45681da177e4SLinus Torvalds * has it's ipv6 private information allocated and setup 45691da177e4SLinus Torvalds * before it can bring up and give link-local addresses 45701da177e4SLinus Torvalds * to other devices which are up. 45711da177e4SLinus Torvalds * 45721da177e4SLinus Torvalds * Unfortunately, loopback_dev is not necessarily the first 45731da177e4SLinus Torvalds * entry in the global dev_base list of net devices. In fact, 45741da177e4SLinus Torvalds * it is likely to be the very last entry on that list. 45751da177e4SLinus Torvalds * So this causes the notifier registry below to try and 45761da177e4SLinus Torvalds * give link-local addresses to all devices besides loopback_dev 45771da177e4SLinus Torvalds * first, then loopback_dev, which cases all the non-loopback_dev 45781da177e4SLinus Torvalds * devices to fail to get a link-local address. 45791da177e4SLinus Torvalds * 45801da177e4SLinus Torvalds * So, as a temporary fix, allocate the ipv6 structure for 45811da177e4SLinus Torvalds * loopback_dev first by hand. 45821da177e4SLinus Torvalds * Longer term, all of the dependencies ipv6 has upon the loopback 45831da177e4SLinus Torvalds * device and it being up should be removed. 45841da177e4SLinus Torvalds */ 45851da177e4SLinus Torvalds rtnl_lock(); 45862774c7abSEric W. Biederman if (!ipv6_add_dev(init_net.loopback_dev)) 45871da177e4SLinus Torvalds err = -ENOMEM; 45881da177e4SLinus Torvalds rtnl_unlock(); 45891da177e4SLinus Torvalds if (err) 4590e0da5a48SPavel Emelyanov goto errlo; 45911da177e4SLinus Torvalds 45921da177e4SLinus Torvalds register_netdevice_notifier(&ipv6_dev_notf); 45931da177e4SLinus Torvalds 45941da177e4SLinus Torvalds addrconf_verify(0); 4595c127ea2cSThomas Graf 4596c127ea2cSThomas Graf err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo); 4597c127ea2cSThomas Graf if (err < 0) 4598c127ea2cSThomas Graf goto errout; 4599c127ea2cSThomas Graf 4600c127ea2cSThomas Graf /* Only the first call to __rtnl_register can fail */ 4601c127ea2cSThomas Graf __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL); 4602c127ea2cSThomas Graf __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL); 4603c127ea2cSThomas Graf __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, inet6_dump_ifaddr); 4604c127ea2cSThomas Graf __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, inet6_dump_ifmcaddr); 4605c127ea2cSThomas Graf __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, inet6_dump_ifacaddr); 4606c127ea2cSThomas Graf 46072a8cc6c8SYOSHIFUJI Hideaki ipv6_addr_label_rtnl_register(); 46082a8cc6c8SYOSHIFUJI Hideaki 46091da177e4SLinus Torvalds return 0; 4610c127ea2cSThomas Graf errout: 4611c127ea2cSThomas Graf unregister_netdevice_notifier(&ipv6_dev_notf); 4612e0da5a48SPavel Emelyanov errlo: 4613e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 4614c127ea2cSThomas Graf 4615c127ea2cSThomas Graf return err; 46161da177e4SLinus Torvalds } 46171da177e4SLinus Torvalds 461809f7709fSDaniel Lezcano void addrconf_cleanup(void) 46191da177e4SLinus Torvalds { 46201da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 4621176c39afSDaniel Lezcano struct net_device *dev; 46221da177e4SLinus Torvalds int i; 46231da177e4SLinus Torvalds 46241da177e4SLinus Torvalds unregister_netdevice_notifier(&ipv6_dev_notf); 4625e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 46261da177e4SLinus Torvalds 46271da177e4SLinus Torvalds rtnl_lock(); 46281da177e4SLinus Torvalds 4629176c39afSDaniel Lezcano /* clean dev list */ 4630176c39afSDaniel Lezcano for_each_netdev(&init_net, dev) { 4631176c39afSDaniel Lezcano if (__in6_dev_get(dev) == NULL) 4632176c39afSDaniel Lezcano continue; 4633176c39afSDaniel Lezcano addrconf_ifdown(dev, 1); 4634176c39afSDaniel Lezcano } 4635176c39afSDaniel Lezcano addrconf_ifdown(init_net.loopback_dev, 2); 4636176c39afSDaniel Lezcano 46371da177e4SLinus Torvalds /* 46381da177e4SLinus Torvalds * Check hash table. 46391da177e4SLinus Torvalds */ 46401da177e4SLinus Torvalds write_lock_bh(&addrconf_hash_lock); 46411da177e4SLinus Torvalds for (i=0; i < IN6_ADDR_HSIZE; i++) { 46421da177e4SLinus Torvalds for (ifa=inet6_addr_lst[i]; ifa; ) { 46431da177e4SLinus Torvalds struct inet6_ifaddr *bifa; 46441da177e4SLinus Torvalds 46451da177e4SLinus Torvalds bifa = ifa; 46461da177e4SLinus Torvalds ifa = ifa->lst_next; 46471da177e4SLinus Torvalds printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa); 46481da177e4SLinus Torvalds /* Do not free it; something is wrong. 46491da177e4SLinus Torvalds Now we can investigate it with debugger. 46501da177e4SLinus Torvalds */ 46511da177e4SLinus Torvalds } 46521da177e4SLinus Torvalds } 46531da177e4SLinus Torvalds write_unlock_bh(&addrconf_hash_lock); 46541da177e4SLinus Torvalds 46551da177e4SLinus Torvalds del_timer(&addr_chk_timer); 46561da177e4SLinus Torvalds rtnl_unlock(); 46571da177e4SLinus Torvalds } 4658