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> 565a0e3ad6STejun Heo #include <linux/slab.h> 571da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 581da177e4SLinus Torvalds #include <linux/sysctl.h> 591da177e4SLinus Torvalds #endif 604fc268d2SRandy Dunlap #include <linux/capability.h> 611da177e4SLinus Torvalds #include <linux/delay.h> 621da177e4SLinus Torvalds #include <linux/notifier.h> 63543537bdSPaulo Marques #include <linux/string.h> 641da177e4SLinus Torvalds 65457c4cbcSEric W. Biederman #include <net/net_namespace.h> 661da177e4SLinus Torvalds #include <net/sock.h> 671da177e4SLinus Torvalds #include <net/snmp.h> 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds #include <net/ipv6.h> 701da177e4SLinus Torvalds #include <net/protocol.h> 711da177e4SLinus Torvalds #include <net/ndisc.h> 721da177e4SLinus Torvalds #include <net/ip6_route.h> 731da177e4SLinus Torvalds #include <net/addrconf.h> 741da177e4SLinus Torvalds #include <net/tcp.h> 751da177e4SLinus Torvalds #include <net/ip.h> 765d620266SThomas Graf #include <net/netlink.h> 77f24e3d65SMitsuru Chinen #include <net/pkt_sched.h> 781da177e4SLinus Torvalds #include <linux/if_tunnel.h> 791da177e4SLinus Torvalds #include <linux/rtnetlink.h> 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 821da177e4SLinus Torvalds #include <linux/random.h> 831da177e4SLinus Torvalds #endif 841da177e4SLinus Torvalds 85e21e8467SStephen Hemminger #include <linux/uaccess.h> 867f7d9a6bSHerbert Xu #include <asm/unaligned.h> 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds #include <linux/proc_fs.h> 891da177e4SLinus Torvalds #include <linux/seq_file.h> 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds /* Set to 3 to get tracing... */ 921da177e4SLinus Torvalds #define ACONF_DEBUG 2 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds #if ACONF_DEBUG >= 3 951da177e4SLinus Torvalds #define ADBG(x) printk x 961da177e4SLinus Torvalds #else 971da177e4SLinus Torvalds #define ADBG(x) 981da177e4SLinus Torvalds #endif 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds #define INFINITY_LIFE_TIME 0xFFFFFFFF 1011da177e4SLinus Torvalds #define TIME_DELTA(a, b) ((unsigned long)((long)(a) - (long)(b))) 1021da177e4SLinus Torvalds 103b2db7564SYOSHIFUJI Hideaki #define ADDRCONF_TIMER_FUZZ_MINUS (HZ > 50 ? HZ/50 : 1) 104b2db7564SYOSHIFUJI Hideaki #define ADDRCONF_TIMER_FUZZ (HZ / 4) 105b2db7564SYOSHIFUJI Hideaki #define ADDRCONF_TIMER_FUZZ_MAX (HZ) 106b2db7564SYOSHIFUJI Hideaki 1071da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 108f52295a9SPavel Emelyanov static void addrconf_sysctl_register(struct inet6_dev *idev); 109408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev); 110408c4768SPavel Emelyanov #else 111408c4768SPavel Emelyanov static inline void addrconf_sysctl_register(struct inet6_dev *idev) 112408c4768SPavel Emelyanov { 113408c4768SPavel Emelyanov } 114408c4768SPavel Emelyanov 115408c4768SPavel Emelyanov static inline void addrconf_sysctl_unregister(struct inet6_dev *idev) 116408c4768SPavel Emelyanov { 117408c4768SPavel Emelyanov } 1181da177e4SLinus Torvalds #endif 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 1211da177e4SLinus Torvalds static int __ipv6_regen_rndid(struct inet6_dev *idev); 1221da177e4SLinus Torvalds static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr); 1231da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data); 1241da177e4SLinus Torvalds #endif 1251da177e4SLinus Torvalds 1261b34be74SYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev); 1271da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev); 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds /* 1301da177e4SLinus Torvalds * Configured unicast address hash table 1311da177e4SLinus Torvalds */ 132c2e21293Sstephen hemminger static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE]; 1335c578aedSstephen hemminger static DEFINE_SPINLOCK(addrconf_hash_lock); 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds static void addrconf_verify(unsigned long); 1361da177e4SLinus Torvalds 1378d06afabSIngo Molnar static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0); 1381da177e4SLinus Torvalds static DEFINE_SPINLOCK(addrconf_verify_lock); 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp); 1411da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp); 1421da177e4SLinus Torvalds 14393d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, 14475c78500SMoni Shoua unsigned long event); 1451da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how); 1461da177e4SLinus Torvalds 147e431b8c0SJamal Hadi Salim static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags); 1481da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data); 1491da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp); 150c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev); 1511da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data); 1521da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 1531da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 1561da177e4SLinus Torvalds struct prefix_info *pinfo); 1573e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 15806bfe655SDaniel Lezcano struct net_device *dev); 1591da177e4SLinus Torvalds 160e041c683SAlan Stern static ATOMIC_NOTIFIER_HEAD(inet6addr_chain); 1611da177e4SLinus Torvalds 162888c848eSAdrian Bunk static struct ipv6_devconf ipv6_devconf __read_mostly = { 1631da177e4SLinus Torvalds .forwarding = 0, 1641da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 1651da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 1661da177e4SLinus Torvalds .accept_ra = 1, 1671da177e4SLinus Torvalds .accept_redirects = 1, 1681da177e4SLinus Torvalds .autoconf = 1, 1691da177e4SLinus Torvalds .force_mld_version = 0, 1701da177e4SLinus Torvalds .dad_transmits = 1, 1711da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 1721da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 1731da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 1741da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 1751da177e4SLinus Torvalds .use_tempaddr = 0, 1761da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 1771da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 1781da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 1791da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 1801da177e4SLinus Torvalds #endif 1811da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 18265f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 183c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 184930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 185930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 18652e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 18709c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 18809c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 18909c884d4SYOSHIFUJI Hideaki #endif 190930d6ff2SYOSHIFUJI Hideaki #endif 191fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0, 1920bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */ 193778d80beSYOSHIFUJI Hideaki .disable_ipv6 = 0, 1941b34be74SYOSHIFUJI Hideaki .accept_dad = 1, 1951da177e4SLinus Torvalds }; 1961da177e4SLinus Torvalds 197ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = { 1981da177e4SLinus Torvalds .forwarding = 0, 1991da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 2001da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 2011da177e4SLinus Torvalds .accept_ra = 1, 2021da177e4SLinus Torvalds .accept_redirects = 1, 2031da177e4SLinus Torvalds .autoconf = 1, 2041da177e4SLinus Torvalds .dad_transmits = 1, 2051da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 2061da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 2071da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 2081da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 2091da177e4SLinus Torvalds .use_tempaddr = 0, 2101da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 2111da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 2121da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 2131da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 2141da177e4SLinus Torvalds #endif 2151da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 21665f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 217c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 218930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 219930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 22052e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 22109c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 22209c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 22309c884d4SYOSHIFUJI Hideaki #endif 224930d6ff2SYOSHIFUJI Hideaki #endif 225fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0, 2260bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */ 227778d80beSYOSHIFUJI Hideaki .disable_ipv6 = 0, 2281b34be74SYOSHIFUJI Hideaki .accept_dad = 1, 2291da177e4SLinus Torvalds }; 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */ 2321da177e4SLinus Torvalds const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; 2331da177e4SLinus Torvalds const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; 234f3ee4010SYOSHIFUJI Hideaki const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT; 235f3ee4010SYOSHIFUJI Hideaki const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT; 2361da177e4SLinus Torvalds 237f24e3d65SMitsuru Chinen /* Check if a valid qdisc is available */ 23805297949SDavid S. Miller static inline bool addrconf_qdisc_ok(const struct net_device *dev) 239f24e3d65SMitsuru Chinen { 24005297949SDavid S. Miller return !qdisc_tx_is_noop(dev); 241f24e3d65SMitsuru Chinen } 242f24e3d65SMitsuru Chinen 2432b5ead46SYOSHIFUJI Hideaki /* Check if a route is valid prefix route */ 2442b5ead46SYOSHIFUJI Hideaki static inline int addrconf_is_prefix_route(const struct rt6_info *rt) 2452b5ead46SYOSHIFUJI Hideaki { 246a02cec21SEric Dumazet return (rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0; 2472b5ead46SYOSHIFUJI Hideaki } 2482b5ead46SYOSHIFUJI Hideaki 2491da177e4SLinus Torvalds static void addrconf_del_timer(struct inet6_ifaddr *ifp) 2501da177e4SLinus Torvalds { 2511da177e4SLinus Torvalds if (del_timer(&ifp->timer)) 2521da177e4SLinus Torvalds __in6_ifa_put(ifp); 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds 255e21e8467SStephen Hemminger enum addrconf_timer_t { 2561da177e4SLinus Torvalds AC_NONE, 2571da177e4SLinus Torvalds AC_DAD, 2581da177e4SLinus Torvalds AC_RS, 2591da177e4SLinus Torvalds }; 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds static void addrconf_mod_timer(struct inet6_ifaddr *ifp, 2621da177e4SLinus Torvalds enum addrconf_timer_t what, 2631da177e4SLinus Torvalds unsigned long when) 2641da177e4SLinus Torvalds { 2651da177e4SLinus Torvalds if (!del_timer(&ifp->timer)) 2661da177e4SLinus Torvalds in6_ifa_hold(ifp); 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds switch (what) { 2691da177e4SLinus Torvalds case AC_DAD: 2701da177e4SLinus Torvalds ifp->timer.function = addrconf_dad_timer; 2711da177e4SLinus Torvalds break; 2721da177e4SLinus Torvalds case AC_RS: 2731da177e4SLinus Torvalds ifp->timer.function = addrconf_rs_timer; 2741da177e4SLinus Torvalds break; 275e21e8467SStephen Hemminger default: 276e21e8467SStephen Hemminger break; 2771da177e4SLinus Torvalds } 2781da177e4SLinus Torvalds ifp->timer.expires = jiffies + when; 2791da177e4SLinus Torvalds add_timer(&ifp->timer); 2801da177e4SLinus Torvalds } 2811da177e4SLinus Torvalds 2827f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev) 2837f7d9a6bSHerbert Xu { 2847d720c3eSTejun Heo if (snmp_mib_init((void __percpu **)idev->stats.ipv6, 2851823e4c8SEric Dumazet sizeof(struct ipstats_mib), 2861823e4c8SEric Dumazet __alignof__(struct ipstats_mib)) < 0) 2877f7d9a6bSHerbert Xu goto err_ip; 2887d720c3eSTejun Heo if (snmp_mib_init((void __percpu **)idev->stats.icmpv6, 2891823e4c8SEric Dumazet sizeof(struct icmpv6_mib), 2901823e4c8SEric Dumazet __alignof__(struct icmpv6_mib)) < 0) 2917f7d9a6bSHerbert Xu goto err_icmp; 2927d720c3eSTejun Heo if (snmp_mib_init((void __percpu **)idev->stats.icmpv6msg, 2931823e4c8SEric Dumazet sizeof(struct icmpv6msg_mib), 2941823e4c8SEric Dumazet __alignof__(struct icmpv6msg_mib)) < 0) 29514878f75SDavid L Stevens goto err_icmpmsg; 2967f7d9a6bSHerbert Xu 2977f7d9a6bSHerbert Xu return 0; 2987f7d9a6bSHerbert Xu 29914878f75SDavid L Stevens err_icmpmsg: 3007d720c3eSTejun Heo snmp_mib_free((void __percpu **)idev->stats.icmpv6); 3017f7d9a6bSHerbert Xu err_icmp: 3027d720c3eSTejun Heo snmp_mib_free((void __percpu **)idev->stats.ipv6); 3037f7d9a6bSHerbert Xu err_ip: 304aaf70ec7SPavel Emelyanov return -ENOMEM; 3057f7d9a6bSHerbert Xu } 3067f7d9a6bSHerbert Xu 30716910b98SPavel Emelyanov static void snmp6_free_dev(struct inet6_dev *idev) 3087f7d9a6bSHerbert Xu { 3097d720c3eSTejun Heo snmp_mib_free((void __percpu **)idev->stats.icmpv6msg); 3107d720c3eSTejun Heo snmp_mib_free((void __percpu **)idev->stats.icmpv6); 3117d720c3eSTejun Heo snmp_mib_free((void __percpu **)idev->stats.ipv6); 3127f7d9a6bSHerbert Xu } 3137f7d9a6bSHerbert Xu 3141da177e4SLinus Torvalds /* Nobody refers to this device, we may destroy it. */ 3151da177e4SLinus Torvalds 3168814c4b5SYOSHIFUJI Hideaki static void in6_dev_finish_destroy_rcu(struct rcu_head *head) 3178814c4b5SYOSHIFUJI Hideaki { 3188814c4b5SYOSHIFUJI Hideaki struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu); 3198814c4b5SYOSHIFUJI Hideaki kfree(idev); 3208814c4b5SYOSHIFUJI Hideaki } 3218814c4b5SYOSHIFUJI Hideaki 3221da177e4SLinus Torvalds void in6_dev_finish_destroy(struct inet6_dev *idev) 3231da177e4SLinus Torvalds { 3241da177e4SLinus Torvalds struct net_device *dev = idev->dev; 325547b792cSIlpo Järvinen 326502a2ffdSstephen hemminger WARN_ON(!list_empty(&idev->addr_list)); 327547b792cSIlpo Järvinen WARN_ON(idev->mc_list != NULL); 328547b792cSIlpo Järvinen 3291da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 3301da177e4SLinus Torvalds printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL"); 3311da177e4SLinus Torvalds #endif 3321da177e4SLinus Torvalds dev_put(dev); 3331da177e4SLinus Torvalds if (!idev->dead) { 334e21e8467SStephen Hemminger pr_warning("Freeing alive inet6 device %p\n", idev); 3351da177e4SLinus Torvalds return; 3361da177e4SLinus Torvalds } 3371da177e4SLinus Torvalds snmp6_free_dev(idev); 3388814c4b5SYOSHIFUJI Hideaki call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu); 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds 3417159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(in6_dev_finish_destroy); 3427159039aSYOSHIFUJI Hideaki 3431da177e4SLinus Torvalds static struct inet6_dev * ipv6_add_dev(struct net_device *dev) 3441da177e4SLinus Torvalds { 3451da177e4SLinus Torvalds struct inet6_dev *ndev; 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds ASSERT_RTNL(); 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 3501da177e4SLinus Torvalds return NULL; 3511da177e4SLinus Torvalds 352322f74a4SIngo Oeser ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL); 3531da177e4SLinus Torvalds 354322f74a4SIngo Oeser if (ndev == NULL) 355322f74a4SIngo Oeser return NULL; 3561da177e4SLinus Torvalds 3571da177e4SLinus Torvalds rwlock_init(&ndev->lock); 3581da177e4SLinus Torvalds ndev->dev = dev; 359502a2ffdSstephen hemminger INIT_LIST_HEAD(&ndev->addr_list); 360502a2ffdSstephen hemminger 361c346dca1SYOSHIFUJI Hideaki memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf)); 3621da177e4SLinus Torvalds ndev->cnf.mtu6 = dev->mtu; 3631da177e4SLinus Torvalds ndev->cnf.sysctl = NULL; 3641da177e4SLinus Torvalds ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl); 3651da177e4SLinus Torvalds if (ndev->nd_parms == NULL) { 3661da177e4SLinus Torvalds kfree(ndev); 3671da177e4SLinus Torvalds return NULL; 3681da177e4SLinus Torvalds } 3690187bdfbSBen Hutchings if (ndev->cnf.forwarding) 3700187bdfbSBen Hutchings dev_disable_lro(dev); 3711da177e4SLinus Torvalds /* We refer to the device */ 3721da177e4SLinus Torvalds dev_hold(dev); 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds if (snmp6_alloc_dev(ndev) < 0) { 3751da177e4SLinus Torvalds ADBG((KERN_WARNING 3761da177e4SLinus Torvalds "%s(): cannot allocate memory for statistics; dev=%s.\n", 3770dc47877SHarvey Harrison __func__, dev->name)); 3781da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, ndev->nd_parms); 3791da177e4SLinus Torvalds ndev->dead = 1; 3801da177e4SLinus Torvalds in6_dev_finish_destroy(ndev); 3811da177e4SLinus Torvalds return NULL; 3821da177e4SLinus Torvalds } 3831da177e4SLinus Torvalds 3841da177e4SLinus Torvalds if (snmp6_register_dev(ndev) < 0) { 3851da177e4SLinus Torvalds ADBG((KERN_WARNING 3861da177e4SLinus Torvalds "%s(): cannot create /proc/net/dev_snmp6/%s\n", 3870dc47877SHarvey Harrison __func__, dev->name)); 3881da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, ndev->nd_parms); 3891da177e4SLinus Torvalds ndev->dead = 1; 3901da177e4SLinus Torvalds in6_dev_finish_destroy(ndev); 3911da177e4SLinus Torvalds return NULL; 3921da177e4SLinus Torvalds } 3931da177e4SLinus Torvalds 3941da177e4SLinus Torvalds /* One reference from device. We must do this before 3951da177e4SLinus Torvalds * we invoke __ipv6_regen_rndid(). 3961da177e4SLinus Torvalds */ 3971da177e4SLinus Torvalds in6_dev_hold(ndev); 3981da177e4SLinus Torvalds 3991b34be74SYOSHIFUJI Hideaki if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 4001b34be74SYOSHIFUJI Hideaki ndev->cnf.accept_dad = -1; 4011b34be74SYOSHIFUJI Hideaki 4020be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 403c7dc89c0SFred L. Templin if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) { 404c7dc89c0SFred L. Templin printk(KERN_INFO 405c7dc89c0SFred L. Templin "%s: Disabled Multicast RS\n", 406c7dc89c0SFred L. Templin dev->name); 407c7dc89c0SFred L. Templin ndev->cnf.rtr_solicits = 0; 408c7dc89c0SFred L. Templin } 409b077d7abSYOSHIFUJI Hideaki #endif 410b077d7abSYOSHIFUJI Hideaki 4111da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 412372e6c8fSstephen hemminger INIT_LIST_HEAD(&ndev->tempaddr_list); 4131da177e4SLinus Torvalds setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev); 4141da177e4SLinus Torvalds if ((dev->flags&IFF_LOOPBACK) || 4151da177e4SLinus Torvalds dev->type == ARPHRD_TUNNEL || 4169625ed72SYOSHIFUJI Hideaki dev->type == ARPHRD_TUNNEL6 || 4171da177e4SLinus Torvalds dev->type == ARPHRD_SIT || 4181da177e4SLinus Torvalds dev->type == ARPHRD_NONE) { 4191da177e4SLinus Torvalds printk(KERN_INFO 4201da177e4SLinus Torvalds "%s: Disabled Privacy Extensions\n", 4211da177e4SLinus Torvalds dev->name); 4221da177e4SLinus Torvalds ndev->cnf.use_tempaddr = -1; 4231da177e4SLinus Torvalds } else { 4241da177e4SLinus Torvalds in6_dev_hold(ndev); 4251da177e4SLinus Torvalds ipv6_regen_rndid((unsigned long) ndev); 4261da177e4SLinus Torvalds } 4271da177e4SLinus Torvalds #endif 4281da177e4SLinus Torvalds 429f24e3d65SMitsuru Chinen if (netif_running(dev) && addrconf_qdisc_ok(dev)) 43053aadcc9SHerbert Xu ndev->if_flags |= IF_READY; 43153aadcc9SHerbert Xu 4321da177e4SLinus Torvalds ipv6_mc_init_dev(ndev); 4331da177e4SLinus Torvalds ndev->tstamp = jiffies; 434f52295a9SPavel Emelyanov addrconf_sysctl_register(ndev); 43530c4cf57SDavid L Stevens /* protected by rtnl_lock */ 43630c4cf57SDavid L Stevens rcu_assign_pointer(dev->ip6_ptr, ndev); 437d88ae4ccSYOSHIFUJI Hideaki 438d88ae4ccSYOSHIFUJI Hideaki /* Join all-node multicast group */ 439f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes); 440d88ae4ccSYOSHIFUJI Hideaki 4411da177e4SLinus Torvalds return ndev; 4421da177e4SLinus Torvalds } 4431da177e4SLinus Torvalds 444cee89473SYOSHIFUJI Hideaki static struct inet6_dev * ipv6_find_idev(struct net_device *dev) 4451da177e4SLinus Torvalds { 4461da177e4SLinus Torvalds struct inet6_dev *idev; 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds ASSERT_RTNL(); 4491da177e4SLinus Torvalds 450e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 451e21e8467SStephen Hemminger if (!idev) { 452e21e8467SStephen Hemminger idev = ipv6_add_dev(dev); 453e21e8467SStephen Hemminger if (!idev) 4541da177e4SLinus Torvalds return NULL; 4551da177e4SLinus Torvalds } 456c5e33bddSYOSHIFUJI Hideaki 4571da177e4SLinus Torvalds if (dev->flags&IFF_UP) 4581da177e4SLinus Torvalds ipv6_mc_up(idev); 4591da177e4SLinus Torvalds return idev; 4601da177e4SLinus Torvalds } 4611da177e4SLinus Torvalds 4621da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 4631da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev) 4641da177e4SLinus Torvalds { 4651da177e4SLinus Torvalds struct net_device *dev; 4661da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 4671da177e4SLinus Torvalds 4681da177e4SLinus Torvalds if (!idev) 4691da177e4SLinus Torvalds return; 4701da177e4SLinus Torvalds dev = idev->dev; 4710187bdfbSBen Hutchings if (idev->cnf.forwarding) 4720187bdfbSBen Hutchings dev_disable_lro(dev); 4731da177e4SLinus Torvalds if (dev && (dev->flags & IFF_MULTICAST)) { 4741da177e4SLinus Torvalds if (idev->cnf.forwarding) 475f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 4761da177e4SLinus Torvalds else 477f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters); 4781da177e4SLinus Torvalds } 479502a2ffdSstephen hemminger 480502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 4812c12a74cSMichal Wrobel if (ifa->flags&IFA_F_TENTATIVE) 4822c12a74cSMichal Wrobel continue; 4831da177e4SLinus Torvalds if (idev->cnf.forwarding) 4841da177e4SLinus Torvalds addrconf_join_anycast(ifa); 4851da177e4SLinus Torvalds else 4861da177e4SLinus Torvalds addrconf_leave_anycast(ifa); 4871da177e4SLinus Torvalds } 4881da177e4SLinus Torvalds } 4891da177e4SLinus Torvalds 4901da177e4SLinus Torvalds 491e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf) 4921da177e4SLinus Torvalds { 4931da177e4SLinus Torvalds struct net_device *dev; 4941da177e4SLinus Torvalds struct inet6_dev *idev; 4951da177e4SLinus Torvalds 4968814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 497c6d14c84SEric Dumazet for_each_netdev_rcu(net, dev) { 4981da177e4SLinus Torvalds idev = __in6_dev_get(dev); 4991da177e4SLinus Torvalds if (idev) { 500e186932bSPavel Emelyanov int changed = (!idev->cnf.forwarding) ^ (!newf); 501e186932bSPavel Emelyanov idev->cnf.forwarding = newf; 5021da177e4SLinus Torvalds if (changed) 5031da177e4SLinus Torvalds dev_forward_change(idev); 5041da177e4SLinus Torvalds } 5051da177e4SLinus Torvalds } 506c6d14c84SEric Dumazet rcu_read_unlock(); 5071da177e4SLinus Torvalds } 508c8fecf22SPavel Emelyanov 509b325fddbSStephen Hemminger static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int old) 510c8fecf22SPavel Emelyanov { 511bff16c2fSPavel Emelyanov struct net *net; 512bff16c2fSPavel Emelyanov 513bff16c2fSPavel Emelyanov net = (struct net *)table->extra2; 514441fc2a2SPavel Emelyanov if (p == &net->ipv6.devconf_dflt->forwarding) 515b325fddbSStephen Hemminger return 0; 516c8fecf22SPavel Emelyanov 51788af182eSEric W. Biederman if (!rtnl_trylock()) { 51888af182eSEric W. Biederman /* Restore the original values before restarting */ 51988af182eSEric W. Biederman *p = old; 5205007392dSEric W. Biederman return restart_syscall(); 52188af182eSEric W. Biederman } 522b325fddbSStephen Hemminger 523e186932bSPavel Emelyanov if (p == &net->ipv6.devconf_all->forwarding) { 524e186932bSPavel Emelyanov __s32 newf = net->ipv6.devconf_all->forwarding; 525e186932bSPavel Emelyanov net->ipv6.devconf_dflt->forwarding = newf; 526e186932bSPavel Emelyanov addrconf_forward_change(net, newf); 527c8fecf22SPavel Emelyanov } else if ((!*p) ^ (!old)) 528c8fecf22SPavel Emelyanov dev_forward_change((struct inet6_dev *)table->extra1); 5290187bdfbSBen Hutchings rtnl_unlock(); 530c8fecf22SPavel Emelyanov 531c8fecf22SPavel Emelyanov if (*p) 5327b4da532SDaniel Lezcano rt6_purge_dflt_routers(net); 533b325fddbSStephen Hemminger return 1; 534c8fecf22SPavel Emelyanov } 5351da177e4SLinus Torvalds #endif 5361da177e4SLinus Torvalds 5375c578aedSstephen hemminger static void inet6_ifa_finish_destroy_rcu(struct rcu_head *head) 5385c578aedSstephen hemminger { 5395c578aedSstephen hemminger struct inet6_ifaddr *ifp = container_of(head, struct inet6_ifaddr, rcu); 5405c578aedSstephen hemminger kfree(ifp); 5415c578aedSstephen hemminger } 5421da177e4SLinus Torvalds 5435c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */ 5441da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp) 5451da177e4SLinus Torvalds { 546c2e21293Sstephen hemminger WARN_ON(!hlist_unhashed(&ifp->addr_lst)); 547547b792cSIlpo Järvinen 5481da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 5491da177e4SLinus Torvalds printk(KERN_DEBUG "inet6_ifa_finish_destroy\n"); 5501da177e4SLinus Torvalds #endif 5511da177e4SLinus Torvalds 5521da177e4SLinus Torvalds in6_dev_put(ifp->idev); 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds if (del_timer(&ifp->timer)) 555e21e8467SStephen Hemminger pr_notice("Timer is still running, when freeing ifa=%p\n", ifp); 5561da177e4SLinus Torvalds 557e9d3e084SHerbert Xu if (ifp->state != INET6_IFADDR_STATE_DEAD) { 558e21e8467SStephen Hemminger pr_warning("Freeing alive inet6 address %p\n", ifp); 5591da177e4SLinus Torvalds return; 5601da177e4SLinus Torvalds } 561d8d1f30bSChangli Gao dst_release(&ifp->rt->dst); 5621da177e4SLinus Torvalds 5635c578aedSstephen hemminger call_rcu(&ifp->rcu, inet6_ifa_finish_destroy_rcu); 5641da177e4SLinus Torvalds } 5651da177e4SLinus Torvalds 566e55ffac6SBrian Haley static void 567e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp) 568e55ffac6SBrian Haley { 569502a2ffdSstephen hemminger struct list_head *p; 5708a6ce0c0SYOSHIFUJI Hideaki int ifp_scope = ipv6_addr_src_scope(&ifp->addr); 571e55ffac6SBrian Haley 572e55ffac6SBrian Haley /* 573e55ffac6SBrian Haley * Each device address list is sorted in order of scope - 574e55ffac6SBrian Haley * global before linklocal. 575e55ffac6SBrian Haley */ 576502a2ffdSstephen hemminger list_for_each(p, &idev->addr_list) { 577502a2ffdSstephen hemminger struct inet6_ifaddr *ifa 578502a2ffdSstephen hemminger = list_entry(p, struct inet6_ifaddr, if_list); 5798a6ce0c0SYOSHIFUJI Hideaki if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr)) 580e55ffac6SBrian Haley break; 581e55ffac6SBrian Haley } 582e55ffac6SBrian Haley 583b54c9b98SDavid S. Miller list_add_tail(&ifp->if_list, p); 584e55ffac6SBrian Haley } 585e55ffac6SBrian Haley 5863a88a81dSstephen hemminger static u32 ipv6_addr_hash(const struct in6_addr *addr) 5873eb84f49SYOSHIFUJI Hideaki { 5883eb84f49SYOSHIFUJI Hideaki /* 5893eb84f49SYOSHIFUJI Hideaki * We perform the hash function over the last 64 bits of the address 5903eb84f49SYOSHIFUJI Hideaki * This will include the IEEE address token on links that support it. 5913eb84f49SYOSHIFUJI Hideaki */ 5920eae88f3SEric Dumazet return jhash_2words((__force u32)addr->s6_addr32[2], 5930eae88f3SEric Dumazet (__force u32)addr->s6_addr32[3], 0) 5943a88a81dSstephen hemminger & (IN6_ADDR_HSIZE - 1); 5953eb84f49SYOSHIFUJI Hideaki } 5963eb84f49SYOSHIFUJI Hideaki 5971da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */ 5981da177e4SLinus Torvalds 5991da177e4SLinus Torvalds static struct inet6_ifaddr * 6001da177e4SLinus Torvalds ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen, 601e431b8c0SJamal Hadi Salim int scope, u32 flags) 6021da177e4SLinus Torvalds { 6031da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 6041da177e4SLinus Torvalds struct rt6_info *rt; 6053a88a81dSstephen hemminger unsigned int hash; 6061da177e4SLinus Torvalds int err = 0; 607d68b8270SYOSHIFUJI Hideaki int addr_type = ipv6_addr_type(addr); 608d68b8270SYOSHIFUJI Hideaki 609d68b8270SYOSHIFUJI Hideaki if (addr_type == IPV6_ADDR_ANY || 610d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_MULTICAST || 611d68b8270SYOSHIFUJI Hideaki (!(idev->dev->flags & IFF_LOOPBACK) && 612d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_LOOPBACK)) 613d68b8270SYOSHIFUJI Hideaki return ERR_PTR(-EADDRNOTAVAIL); 6141da177e4SLinus Torvalds 6158814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 6161da177e4SLinus Torvalds if (idev->dead) { 6171da177e4SLinus Torvalds err = -ENODEV; /*XXX*/ 6181da177e4SLinus Torvalds goto out2; 6191da177e4SLinus Torvalds } 6201da177e4SLinus Torvalds 62156d417b1SBrian Haley if (idev->cnf.disable_ipv6) { 6229bdd8d40SBrian Haley err = -EACCES; 6239bdd8d40SBrian Haley goto out2; 6249bdd8d40SBrian Haley } 6259bdd8d40SBrian Haley 6265c578aedSstephen hemminger spin_lock(&addrconf_hash_lock); 6271da177e4SLinus Torvalds 6281da177e4SLinus Torvalds /* Ignore adding duplicate addresses on an interface */ 629c346dca1SYOSHIFUJI Hideaki if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) { 6301da177e4SLinus Torvalds ADBG(("ipv6_add_addr: already assigned\n")); 6311da177e4SLinus Torvalds err = -EEXIST; 6321da177e4SLinus Torvalds goto out; 6331da177e4SLinus Torvalds } 6341da177e4SLinus Torvalds 635322f74a4SIngo Oeser ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC); 6361da177e4SLinus Torvalds 6371da177e4SLinus Torvalds if (ifa == NULL) { 6381da177e4SLinus Torvalds ADBG(("ipv6_add_addr: malloc failed\n")); 6391da177e4SLinus Torvalds err = -ENOBUFS; 6401da177e4SLinus Torvalds goto out; 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds 6431da177e4SLinus Torvalds rt = addrconf_dst_alloc(idev, addr, 0); 6441da177e4SLinus Torvalds if (IS_ERR(rt)) { 6451da177e4SLinus Torvalds err = PTR_ERR(rt); 6461da177e4SLinus Torvalds goto out; 6471da177e4SLinus Torvalds } 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds ipv6_addr_copy(&ifa->addr, addr); 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds spin_lock_init(&ifa->lock); 652e9d3e084SHerbert Xu spin_lock_init(&ifa->state_lock); 6531da177e4SLinus Torvalds init_timer(&ifa->timer); 654c2e21293Sstephen hemminger INIT_HLIST_NODE(&ifa->addr_lst); 6551da177e4SLinus Torvalds ifa->timer.data = (unsigned long) ifa; 6561da177e4SLinus Torvalds ifa->scope = scope; 6571da177e4SLinus Torvalds ifa->prefix_len = pfxlen; 6581da177e4SLinus Torvalds ifa->flags = flags | IFA_F_TENTATIVE; 6591da177e4SLinus Torvalds ifa->cstamp = ifa->tstamp = jiffies; 6601da177e4SLinus Torvalds 66157f5f544SKeir Fraser ifa->rt = rt; 66257f5f544SKeir Fraser 66395c385b4SNeil Horman /* 66495c385b4SNeil Horman * part one of RFC 4429, section 3.3 66595c385b4SNeil Horman * We should not configure an address as 66695c385b4SNeil Horman * optimistic if we do not yet know the link 66795c385b4SNeil Horman * layer address of our nexhop router 66895c385b4SNeil Horman */ 66995c385b4SNeil Horman 67095c385b4SNeil Horman if (rt->rt6i_nexthop == NULL) 67195c385b4SNeil Horman ifa->flags &= ~IFA_F_OPTIMISTIC; 67295c385b4SNeil Horman 6731da177e4SLinus Torvalds ifa->idev = idev; 6741da177e4SLinus Torvalds in6_dev_hold(idev); 6751da177e4SLinus Torvalds /* For caller */ 6761da177e4SLinus Torvalds in6_ifa_hold(ifa); 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds /* Add to big hash table */ 6791da177e4SLinus Torvalds hash = ipv6_addr_hash(addr); 6801da177e4SLinus Torvalds 6815c578aedSstephen hemminger hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]); 6825c578aedSstephen hemminger spin_unlock(&addrconf_hash_lock); 6831da177e4SLinus Torvalds 6841da177e4SLinus Torvalds write_lock(&idev->lock); 6851da177e4SLinus Torvalds /* Add to inet6_dev unicast addr list. */ 686e55ffac6SBrian Haley ipv6_link_dev_addr(idev, ifa); 6871da177e4SLinus Torvalds 6881da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 6891da177e4SLinus Torvalds if (ifa->flags&IFA_F_TEMPORARY) { 690372e6c8fSstephen hemminger list_add(&ifa->tmp_list, &idev->tempaddr_list); 6911da177e4SLinus Torvalds in6_ifa_hold(ifa); 6921da177e4SLinus Torvalds } 6931da177e4SLinus Torvalds #endif 6941da177e4SLinus Torvalds 6951da177e4SLinus Torvalds in6_ifa_hold(ifa); 6961da177e4SLinus Torvalds write_unlock(&idev->lock); 6971da177e4SLinus Torvalds out2: 6988814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 6991da177e4SLinus Torvalds 700fd92833aSYOSHIFUJI Hideaki if (likely(err == 0)) 701e041c683SAlan Stern atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa); 7021da177e4SLinus Torvalds else { 7031da177e4SLinus Torvalds kfree(ifa); 7041da177e4SLinus Torvalds ifa = ERR_PTR(err); 7051da177e4SLinus Torvalds } 7061da177e4SLinus Torvalds 7071da177e4SLinus Torvalds return ifa; 7081da177e4SLinus Torvalds out: 7095c578aedSstephen hemminger spin_unlock(&addrconf_hash_lock); 7101da177e4SLinus Torvalds goto out2; 7111da177e4SLinus Torvalds } 7121da177e4SLinus Torvalds 7131da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */ 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp) 7161da177e4SLinus Torvalds { 717502a2ffdSstephen hemminger struct inet6_ifaddr *ifa, *ifn; 7181da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 7194c5ff6a6SHerbert Xu int state; 7201da177e4SLinus Torvalds int hash; 7211da177e4SLinus Torvalds int deleted = 0, onlink = 0; 7221da177e4SLinus Torvalds unsigned long expires = jiffies; 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds hash = ipv6_addr_hash(&ifp->addr); 7251da177e4SLinus Torvalds 7264c5ff6a6SHerbert Xu spin_lock_bh(&ifp->state_lock); 7274c5ff6a6SHerbert Xu state = ifp->state; 728e9d3e084SHerbert Xu ifp->state = INET6_IFADDR_STATE_DEAD; 7294c5ff6a6SHerbert Xu spin_unlock_bh(&ifp->state_lock); 7304c5ff6a6SHerbert Xu 7314c5ff6a6SHerbert Xu if (state == INET6_IFADDR_STATE_DEAD) 7324c5ff6a6SHerbert Xu goto out; 7331da177e4SLinus Torvalds 7345c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 7355c578aedSstephen hemminger hlist_del_init_rcu(&ifp->addr_lst); 7365c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 7371da177e4SLinus Torvalds 7381da177e4SLinus Torvalds write_lock_bh(&idev->lock); 7391da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 7401da177e4SLinus Torvalds if (ifp->flags&IFA_F_TEMPORARY) { 741372e6c8fSstephen hemminger list_del(&ifp->tmp_list); 7421da177e4SLinus Torvalds if (ifp->ifpub) { 7431da177e4SLinus Torvalds in6_ifa_put(ifp->ifpub); 7441da177e4SLinus Torvalds ifp->ifpub = NULL; 7451da177e4SLinus Torvalds } 7461da177e4SLinus Torvalds __in6_ifa_put(ifp); 7471da177e4SLinus Torvalds } 7481da177e4SLinus Torvalds #endif 7491da177e4SLinus Torvalds 750502a2ffdSstephen hemminger list_for_each_entry_safe(ifa, ifn, &idev->addr_list, if_list) { 7511da177e4SLinus Torvalds if (ifa == ifp) { 752502a2ffdSstephen hemminger list_del_init(&ifp->if_list); 7531da177e4SLinus Torvalds __in6_ifa_put(ifp); 754502a2ffdSstephen hemminger 7551da177e4SLinus Torvalds if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0) 7561da177e4SLinus Torvalds break; 7571da177e4SLinus Torvalds deleted = 1; 7581d142804SKristian Slavov continue; 7591da177e4SLinus Torvalds } else if (ifp->flags & IFA_F_PERMANENT) { 7601da177e4SLinus Torvalds if (ipv6_prefix_equal(&ifa->addr, &ifp->addr, 7611da177e4SLinus Torvalds ifp->prefix_len)) { 7621da177e4SLinus Torvalds if (ifa->flags & IFA_F_PERMANENT) { 7631da177e4SLinus Torvalds onlink = 1; 7641da177e4SLinus Torvalds if (deleted) 7651da177e4SLinus Torvalds break; 7661da177e4SLinus Torvalds } else { 7671da177e4SLinus Torvalds unsigned long lifetime; 7681da177e4SLinus Torvalds 7691da177e4SLinus Torvalds if (!onlink) 7701da177e4SLinus Torvalds onlink = -1; 7711da177e4SLinus Torvalds 7721da177e4SLinus Torvalds spin_lock(&ifa->lock); 7734bed72e4SYOSHIFUJI Hideaki 7744bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ); 7754bed72e4SYOSHIFUJI Hideaki /* 7764bed72e4SYOSHIFUJI Hideaki * Note: Because this address is 7774bed72e4SYOSHIFUJI Hideaki * not permanent, lifetime < 7784bed72e4SYOSHIFUJI Hideaki * LONG_MAX / HZ here. 7794bed72e4SYOSHIFUJI Hideaki */ 7801da177e4SLinus Torvalds if (time_before(expires, 7811da177e4SLinus Torvalds ifa->tstamp + lifetime * HZ)) 7821da177e4SLinus Torvalds expires = ifa->tstamp + lifetime * HZ; 7831da177e4SLinus Torvalds spin_unlock(&ifa->lock); 7841da177e4SLinus Torvalds } 7851da177e4SLinus Torvalds } 7861da177e4SLinus Torvalds } 7871da177e4SLinus Torvalds } 7881da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 7891da177e4SLinus Torvalds 790b2238566SAndrey Vagin addrconf_del_timer(ifp); 791b2238566SAndrey Vagin 7921da177e4SLinus Torvalds ipv6_ifa_notify(RTM_DELADDR, ifp); 7931da177e4SLinus Torvalds 794e041c683SAlan Stern atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp); 7951da177e4SLinus Torvalds 7961da177e4SLinus Torvalds /* 7971da177e4SLinus Torvalds * Purge or update corresponding prefix 7981da177e4SLinus Torvalds * 7991da177e4SLinus Torvalds * 1) we don't purge prefix here if address was not permanent. 8001da177e4SLinus Torvalds * prefix is managed by its own lifetime. 8011da177e4SLinus Torvalds * 2) if there're no addresses, delete prefix. 8021da177e4SLinus Torvalds * 3) if there're still other permanent address(es), 8031da177e4SLinus Torvalds * corresponding prefix is still permanent. 8041da177e4SLinus Torvalds * 4) otherwise, update prefix lifetime to the 8051da177e4SLinus Torvalds * longest valid lifetime among the corresponding 8061da177e4SLinus Torvalds * addresses on the device. 8071da177e4SLinus Torvalds * Note: subsequent RA will update lifetime. 8081da177e4SLinus Torvalds * 8091da177e4SLinus Torvalds * --yoshfuji 8101da177e4SLinus Torvalds */ 8111da177e4SLinus Torvalds if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) { 8121da177e4SLinus Torvalds struct in6_addr prefix; 8131da177e4SLinus Torvalds struct rt6_info *rt; 814c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(ifp->idev->dev); 8151da177e4SLinus Torvalds ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len); 8166fda7350SBenjamin Thery rt = rt6_lookup(net, &prefix, NULL, ifp->idev->dev->ifindex, 1); 8171da177e4SLinus Torvalds 8182b5ead46SYOSHIFUJI Hideaki if (rt && addrconf_is_prefix_route(rt)) { 8191da177e4SLinus Torvalds if (onlink == 0) { 820e0a1ad73SThomas Graf ip6_del_rt(rt); 8211da177e4SLinus Torvalds rt = NULL; 8221da177e4SLinus Torvalds } else if (!(rt->rt6i_flags & RTF_EXPIRES)) { 8231da177e4SLinus Torvalds rt->rt6i_expires = expires; 8241da177e4SLinus Torvalds rt->rt6i_flags |= RTF_EXPIRES; 8251da177e4SLinus Torvalds } 8261da177e4SLinus Torvalds } 827d8d1f30bSChangli Gao dst_release(&rt->dst); 8281da177e4SLinus Torvalds } 8291da177e4SLinus Torvalds 8304c5ff6a6SHerbert Xu out: 8311da177e4SLinus Torvalds in6_ifa_put(ifp); 8321da177e4SLinus Torvalds } 8331da177e4SLinus Torvalds 8341da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 8351da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift) 8361da177e4SLinus Torvalds { 8371da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 8381da177e4SLinus Torvalds struct in6_addr addr, *tmpaddr; 8397a876b0eSGlenn Wurster unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp, age; 840eac55bf9SBenoit Boissinot unsigned long regen_advance; 8411da177e4SLinus Torvalds int tmp_plen; 8421da177e4SLinus Torvalds int ret = 0; 8431da177e4SLinus Torvalds int max_addresses; 84495c385b4SNeil Horman u32 addr_flags; 8451da177e4SLinus Torvalds 8461da177e4SLinus Torvalds write_lock(&idev->lock); 8471da177e4SLinus Torvalds if (ift) { 8481da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 8491da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8); 8501da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 8511da177e4SLinus Torvalds tmpaddr = &addr; 8521da177e4SLinus Torvalds } else { 8531da177e4SLinus Torvalds tmpaddr = NULL; 8541da177e4SLinus Torvalds } 8551da177e4SLinus Torvalds retry: 8561da177e4SLinus Torvalds in6_dev_hold(idev); 8571da177e4SLinus Torvalds if (idev->cnf.use_tempaddr <= 0) { 8581da177e4SLinus Torvalds write_unlock(&idev->lock); 8591da177e4SLinus Torvalds printk(KERN_INFO 8601da177e4SLinus Torvalds "ipv6_create_tempaddr(): use_tempaddr is disabled.\n"); 8611da177e4SLinus Torvalds in6_dev_put(idev); 8621da177e4SLinus Torvalds ret = -1; 8631da177e4SLinus Torvalds goto out; 8641da177e4SLinus Torvalds } 8651da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 8661da177e4SLinus Torvalds if (ifp->regen_count++ >= idev->cnf.regen_max_retry) { 8671da177e4SLinus Torvalds idev->cnf.use_tempaddr = -1; /*XXX*/ 8681da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 8691da177e4SLinus Torvalds write_unlock(&idev->lock); 8701da177e4SLinus Torvalds printk(KERN_WARNING 8711da177e4SLinus Torvalds "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n"); 8721da177e4SLinus Torvalds in6_dev_put(idev); 8731da177e4SLinus Torvalds ret = -1; 8741da177e4SLinus Torvalds goto out; 8751da177e4SLinus Torvalds } 8761da177e4SLinus Torvalds in6_ifa_hold(ifp); 8771da177e4SLinus Torvalds memcpy(addr.s6_addr, ifp->addr.s6_addr, 8); 8781da177e4SLinus Torvalds if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) { 8791da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 8801da177e4SLinus Torvalds write_unlock(&idev->lock); 8811da177e4SLinus Torvalds printk(KERN_WARNING 8821da177e4SLinus Torvalds "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n"); 8831da177e4SLinus Torvalds in6_ifa_put(ifp); 8841da177e4SLinus Torvalds in6_dev_put(idev); 8851da177e4SLinus Torvalds ret = -1; 8861da177e4SLinus Torvalds goto out; 8871da177e4SLinus Torvalds } 8881da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], idev->rndid, 8); 8897a876b0eSGlenn Wurster age = (jiffies - ifp->tstamp) / HZ; 8901da177e4SLinus Torvalds tmp_valid_lft = min_t(__u32, 8911da177e4SLinus Torvalds ifp->valid_lft, 8927a876b0eSGlenn Wurster idev->cnf.temp_valid_lft + age); 8931da177e4SLinus Torvalds tmp_prefered_lft = min_t(__u32, 8941da177e4SLinus Torvalds ifp->prefered_lft, 8957a876b0eSGlenn Wurster idev->cnf.temp_prefered_lft + age - 896784e2710SBen Hutchings idev->cnf.max_desync_factor); 8971da177e4SLinus Torvalds tmp_plen = ifp->prefix_len; 8981da177e4SLinus Torvalds max_addresses = idev->cnf.max_addresses; 8991da177e4SLinus Torvalds tmp_cstamp = ifp->cstamp; 9001da177e4SLinus Torvalds tmp_tstamp = ifp->tstamp; 9011da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 9021da177e4SLinus Torvalds 903eac55bf9SBenoit Boissinot regen_advance = idev->cnf.regen_max_retry * 904eac55bf9SBenoit Boissinot idev->cnf.dad_transmits * 905eac55bf9SBenoit Boissinot idev->nd_parms->retrans_time / HZ; 9061da177e4SLinus Torvalds write_unlock(&idev->lock); 90795c385b4SNeil Horman 908eac55bf9SBenoit Boissinot /* A temporary address is created only if this calculated Preferred 909eac55bf9SBenoit Boissinot * Lifetime is greater than REGEN_ADVANCE time units. In particular, 910eac55bf9SBenoit Boissinot * an implementation must not create a temporary address with a zero 911eac55bf9SBenoit Boissinot * Preferred Lifetime. 912eac55bf9SBenoit Boissinot */ 913eac55bf9SBenoit Boissinot if (tmp_prefered_lft <= regen_advance) { 914eac55bf9SBenoit Boissinot in6_ifa_put(ifp); 915eac55bf9SBenoit Boissinot in6_dev_put(idev); 916eac55bf9SBenoit Boissinot ret = -1; 917eac55bf9SBenoit Boissinot goto out; 918eac55bf9SBenoit Boissinot } 919eac55bf9SBenoit Boissinot 92095c385b4SNeil Horman addr_flags = IFA_F_TEMPORARY; 92195c385b4SNeil Horman /* set in addrconf_prefix_rcv() */ 92295c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 92395c385b4SNeil Horman addr_flags |= IFA_F_OPTIMISTIC; 92495c385b4SNeil Horman 9251da177e4SLinus Torvalds ift = !max_addresses || 9261da177e4SLinus Torvalds ipv6_count_addresses(idev) < max_addresses ? 9271da177e4SLinus Torvalds ipv6_add_addr(idev, &addr, tmp_plen, 92895c385b4SNeil Horman ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, 92995c385b4SNeil Horman addr_flags) : NULL; 9301da177e4SLinus Torvalds if (!ift || IS_ERR(ift)) { 9311da177e4SLinus Torvalds in6_ifa_put(ifp); 9321da177e4SLinus Torvalds in6_dev_put(idev); 9331da177e4SLinus Torvalds printk(KERN_INFO 9341da177e4SLinus Torvalds "ipv6_create_tempaddr(): retry temporary address regeneration.\n"); 9351da177e4SLinus Torvalds tmpaddr = &addr; 9361da177e4SLinus Torvalds write_lock(&idev->lock); 9371da177e4SLinus Torvalds goto retry; 9381da177e4SLinus Torvalds } 9391da177e4SLinus Torvalds 9401da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 9411da177e4SLinus Torvalds ift->ifpub = ifp; 9421da177e4SLinus Torvalds ift->valid_lft = tmp_valid_lft; 9431da177e4SLinus Torvalds ift->prefered_lft = tmp_prefered_lft; 9441da177e4SLinus Torvalds ift->cstamp = tmp_cstamp; 9451da177e4SLinus Torvalds ift->tstamp = tmp_tstamp; 9461da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 9471da177e4SLinus Torvalds 9481da177e4SLinus Torvalds addrconf_dad_start(ift, 0); 9491da177e4SLinus Torvalds in6_ifa_put(ift); 9501da177e4SLinus Torvalds in6_dev_put(idev); 9511da177e4SLinus Torvalds out: 9521da177e4SLinus Torvalds return ret; 9531da177e4SLinus Torvalds } 9541da177e4SLinus Torvalds #endif 9551da177e4SLinus Torvalds 9561da177e4SLinus Torvalds /* 957072047e4SYOSHIFUJI Hideaki * Choose an appropriate source address (RFC3484) 9581da177e4SLinus Torvalds */ 959a9b05723SYOSHIFUJI Hideaki enum { 960a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_INIT = 0, 961a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LOCAL, 962a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_SCOPE, 963a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFERRED, 964a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 965a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_HOA, 966a9b05723SYOSHIFUJI Hideaki #endif 967a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_OIF, 968a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LABEL, 969a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_PRIVACY 970a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PRIVACY, 971a9b05723SYOSHIFUJI Hideaki #endif 972a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_ORCHID, 973a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFIX, 974a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_MAX 975072047e4SYOSHIFUJI Hideaki }; 976072047e4SYOSHIFUJI Hideaki 977a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score { 978a9b05723SYOSHIFUJI Hideaki int rule; 979a9b05723SYOSHIFUJI Hideaki int addr_type; 980a9b05723SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 981a9b05723SYOSHIFUJI Hideaki DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX); 982a9b05723SYOSHIFUJI Hideaki int scopedist; 983a9b05723SYOSHIFUJI Hideaki int matchlen; 984a9b05723SYOSHIFUJI Hideaki }; 985a9b05723SYOSHIFUJI Hideaki 986a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst { 9879acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *addr; 988a9b05723SYOSHIFUJI Hideaki int ifindex; 989a9b05723SYOSHIFUJI Hideaki int scope; 990a9b05723SYOSHIFUJI Hideaki int label; 9917cbca67cSYOSHIFUJI Hideaki unsigned int prefs; 992a9b05723SYOSHIFUJI Hideaki }; 993072047e4SYOSHIFUJI Hideaki 994b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type) 9951da177e4SLinus Torvalds { 99645bb0060SUlrich Weber if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK)) 997072047e4SYOSHIFUJI Hideaki return 1; 998072047e4SYOSHIFUJI Hideaki return 0; 9991da177e4SLinus Torvalds } 10001da177e4SLinus Torvalds 10013de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net, 10023de23255SBenjamin Thery struct ipv6_saddr_score *score, 1003a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst *dst, 1004a9b05723SYOSHIFUJI Hideaki int i) 1005a9b05723SYOSHIFUJI Hideaki { 1006a9b05723SYOSHIFUJI Hideaki int ret; 1007a9b05723SYOSHIFUJI Hideaki 1008a9b05723SYOSHIFUJI Hideaki if (i <= score->rule) { 1009a9b05723SYOSHIFUJI Hideaki switch (i) { 1010a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1011a9b05723SYOSHIFUJI Hideaki ret = score->scopedist; 1012a9b05723SYOSHIFUJI Hideaki break; 1013a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1014a9b05723SYOSHIFUJI Hideaki ret = score->matchlen; 1015a9b05723SYOSHIFUJI Hideaki break; 1016a9b05723SYOSHIFUJI Hideaki default: 1017a9b05723SYOSHIFUJI Hideaki ret = !!test_bit(i, score->scorebits); 1018a9b05723SYOSHIFUJI Hideaki } 1019a9b05723SYOSHIFUJI Hideaki goto out; 1020a9b05723SYOSHIFUJI Hideaki } 1021a9b05723SYOSHIFUJI Hideaki 1022a9b05723SYOSHIFUJI Hideaki switch (i) { 1023a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_INIT: 1024a9b05723SYOSHIFUJI Hideaki /* Rule 0: remember if hiscore is not ready yet */ 1025a9b05723SYOSHIFUJI Hideaki ret = !!score->ifa; 1026a9b05723SYOSHIFUJI Hideaki break; 1027a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LOCAL: 1028a9b05723SYOSHIFUJI Hideaki /* Rule 1: Prefer same address */ 1029a9b05723SYOSHIFUJI Hideaki ret = ipv6_addr_equal(&score->ifa->addr, dst->addr); 1030a9b05723SYOSHIFUJI Hideaki break; 1031a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1032a9b05723SYOSHIFUJI Hideaki /* Rule 2: Prefer appropriate scope 1033a9b05723SYOSHIFUJI Hideaki * 1034a9b05723SYOSHIFUJI Hideaki * ret 1035a9b05723SYOSHIFUJI Hideaki * ^ 1036a9b05723SYOSHIFUJI Hideaki * -1 | d 15 1037a9b05723SYOSHIFUJI Hideaki * ---+--+-+---> scope 1038a9b05723SYOSHIFUJI Hideaki * | 1039a9b05723SYOSHIFUJI Hideaki * | d is scope of the destination. 1040a9b05723SYOSHIFUJI Hideaki * B-d | \ 1041a9b05723SYOSHIFUJI Hideaki * | \ <- smaller scope is better if 1042a9b05723SYOSHIFUJI Hideaki * B-15 | \ if scope is enough for destinaion. 1043a9b05723SYOSHIFUJI Hideaki * | ret = B - scope (-1 <= scope >= d <= 15). 1044a9b05723SYOSHIFUJI Hideaki * d-C-1 | / 1045a9b05723SYOSHIFUJI Hideaki * |/ <- greater is better 1046a9b05723SYOSHIFUJI Hideaki * -C / if scope is not enough for destination. 1047a9b05723SYOSHIFUJI Hideaki * /| ret = scope - C (-1 <= d < scope <= 15). 1048a9b05723SYOSHIFUJI Hideaki * 1049a9b05723SYOSHIFUJI Hideaki * d - C - 1 < B -15 (for all -1 <= d <= 15). 1050a9b05723SYOSHIFUJI Hideaki * C > d + 14 - B >= 15 + 14 - B = 29 - B. 1051a9b05723SYOSHIFUJI Hideaki * Assume B = 0 and we get C > 29. 1052a9b05723SYOSHIFUJI Hideaki */ 1053a9b05723SYOSHIFUJI Hideaki ret = __ipv6_addr_src_scope(score->addr_type); 1054a9b05723SYOSHIFUJI Hideaki if (ret >= dst->scope) 1055a9b05723SYOSHIFUJI Hideaki ret = -ret; 1056a9b05723SYOSHIFUJI Hideaki else 1057a9b05723SYOSHIFUJI Hideaki ret -= 128; /* 30 is enough */ 1058a9b05723SYOSHIFUJI Hideaki score->scopedist = ret; 1059a9b05723SYOSHIFUJI Hideaki break; 1060a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFERRED: 1061a9b05723SYOSHIFUJI Hideaki /* Rule 3: Avoid deprecated and optimistic addresses */ 1062a9b05723SYOSHIFUJI Hideaki ret = ipv6_saddr_preferred(score->addr_type) || 1063a9b05723SYOSHIFUJI Hideaki !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC)); 1064a9b05723SYOSHIFUJI Hideaki break; 1065a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1066a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_HOA: 10677cbca67cSYOSHIFUJI Hideaki { 1068a9b05723SYOSHIFUJI Hideaki /* Rule 4: Prefer home address */ 10697cbca67cSYOSHIFUJI Hideaki int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA); 10707cbca67cSYOSHIFUJI Hideaki ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome; 1071a9b05723SYOSHIFUJI Hideaki break; 10727cbca67cSYOSHIFUJI Hideaki } 1073a9b05723SYOSHIFUJI Hideaki #endif 1074a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_OIF: 1075a9b05723SYOSHIFUJI Hideaki /* Rule 5: Prefer outgoing interface */ 1076a9b05723SYOSHIFUJI Hideaki ret = (!dst->ifindex || 1077a9b05723SYOSHIFUJI Hideaki dst->ifindex == score->ifa->idev->dev->ifindex); 1078a9b05723SYOSHIFUJI Hideaki break; 1079a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LABEL: 1080a9b05723SYOSHIFUJI Hideaki /* Rule 6: Prefer matching label */ 10813de23255SBenjamin Thery ret = ipv6_addr_label(net, 10823de23255SBenjamin Thery &score->ifa->addr, score->addr_type, 1083a9b05723SYOSHIFUJI Hideaki score->ifa->idev->dev->ifindex) == dst->label; 1084a9b05723SYOSHIFUJI Hideaki break; 1085a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_PRIVACY 1086a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PRIVACY: 10877cbca67cSYOSHIFUJI Hideaki { 1088a9b05723SYOSHIFUJI Hideaki /* Rule 7: Prefer public address 1089a9b05723SYOSHIFUJI Hideaki * Note: prefer temprary address if use_tempaddr >= 2 1090a9b05723SYOSHIFUJI Hideaki */ 10917cbca67cSYOSHIFUJI Hideaki int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ? 10927cbca67cSYOSHIFUJI Hideaki !!(dst->prefs & IPV6_PREFER_SRC_TMP) : 10937cbca67cSYOSHIFUJI Hideaki score->ifa->idev->cnf.use_tempaddr >= 2; 10947cbca67cSYOSHIFUJI Hideaki ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp; 1095a9b05723SYOSHIFUJI Hideaki break; 10967cbca67cSYOSHIFUJI Hideaki } 1097a9b05723SYOSHIFUJI Hideaki #endif 1098a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_ORCHID: 1099a9b05723SYOSHIFUJI Hideaki /* Rule 8-: Prefer ORCHID vs ORCHID or 1100a9b05723SYOSHIFUJI Hideaki * non-ORCHID vs non-ORCHID 1101a9b05723SYOSHIFUJI Hideaki */ 1102a9b05723SYOSHIFUJI Hideaki ret = !(ipv6_addr_orchid(&score->ifa->addr) ^ 1103a9b05723SYOSHIFUJI Hideaki ipv6_addr_orchid(dst->addr)); 1104a9b05723SYOSHIFUJI Hideaki break; 1105a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1106a9b05723SYOSHIFUJI Hideaki /* Rule 8: Use longest matching prefix */ 1107a9b05723SYOSHIFUJI Hideaki score->matchlen = ret = ipv6_addr_diff(&score->ifa->addr, 1108a9b05723SYOSHIFUJI Hideaki dst->addr); 1109a9b05723SYOSHIFUJI Hideaki break; 1110a9b05723SYOSHIFUJI Hideaki default: 1111a9b05723SYOSHIFUJI Hideaki ret = 0; 1112a9b05723SYOSHIFUJI Hideaki } 1113a9b05723SYOSHIFUJI Hideaki 1114a9b05723SYOSHIFUJI Hideaki if (ret) 1115a9b05723SYOSHIFUJI Hideaki __set_bit(i, score->scorebits); 1116a9b05723SYOSHIFUJI Hideaki score->rule = i; 1117a9b05723SYOSHIFUJI Hideaki out: 1118a9b05723SYOSHIFUJI Hideaki return ret; 1119a9b05723SYOSHIFUJI Hideaki } 1120a9b05723SYOSHIFUJI Hideaki 1121191cd582SBrian Haley int ipv6_dev_get_saddr(struct net *net, struct net_device *dst_dev, 11229acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *daddr, unsigned int prefs, 11237cbca67cSYOSHIFUJI Hideaki struct in6_addr *saddr) 11241da177e4SLinus Torvalds { 1125a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score scores[2], 1126a9b05723SYOSHIFUJI Hideaki *score = &scores[0], *hiscore = &scores[1]; 1127a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst dst; 1128072047e4SYOSHIFUJI Hideaki struct net_device *dev; 1129a9b05723SYOSHIFUJI Hideaki int dst_type; 11301da177e4SLinus Torvalds 1131a9b05723SYOSHIFUJI Hideaki dst_type = __ipv6_addr_type(daddr); 1132a9b05723SYOSHIFUJI Hideaki dst.addr = daddr; 1133a9b05723SYOSHIFUJI Hideaki dst.ifindex = dst_dev ? dst_dev->ifindex : 0; 1134a9b05723SYOSHIFUJI Hideaki dst.scope = __ipv6_addr_src_scope(dst_type); 11353de23255SBenjamin Thery dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex); 11367cbca67cSYOSHIFUJI Hideaki dst.prefs = prefs; 1137a9b05723SYOSHIFUJI Hideaki 1138a9b05723SYOSHIFUJI Hideaki hiscore->rule = -1; 1139a9b05723SYOSHIFUJI Hideaki hiscore->ifa = NULL; 11401da177e4SLinus Torvalds 11418814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 1142072047e4SYOSHIFUJI Hideaki 1143c6d14c84SEric Dumazet for_each_netdev_rcu(net, dev) { 1144072047e4SYOSHIFUJI Hideaki struct inet6_dev *idev; 1145072047e4SYOSHIFUJI Hideaki 1146a9b05723SYOSHIFUJI Hideaki /* Candidate Source Address (section 4) 1147072047e4SYOSHIFUJI Hideaki * - multicast and link-local destination address, 1148072047e4SYOSHIFUJI Hideaki * the set of candidate source address MUST only 1149072047e4SYOSHIFUJI Hideaki * include addresses assigned to interfaces 1150072047e4SYOSHIFUJI Hideaki * belonging to the same link as the outgoing 1151072047e4SYOSHIFUJI Hideaki * interface. 1152072047e4SYOSHIFUJI Hideaki * (- For site-local destination addresses, the 1153072047e4SYOSHIFUJI Hideaki * set of candidate source addresses MUST only 1154072047e4SYOSHIFUJI Hideaki * include addresses assigned to interfaces 1155072047e4SYOSHIFUJI Hideaki * belonging to the same site as the outgoing 1156072047e4SYOSHIFUJI Hideaki * interface.) 1157072047e4SYOSHIFUJI Hideaki */ 1158a9b05723SYOSHIFUJI Hideaki if (((dst_type & IPV6_ADDR_MULTICAST) || 1159a9b05723SYOSHIFUJI Hideaki dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) && 1160a9b05723SYOSHIFUJI Hideaki dst.ifindex && dev->ifindex != dst.ifindex) 1161072047e4SYOSHIFUJI Hideaki continue; 1162072047e4SYOSHIFUJI Hideaki 11631da177e4SLinus Torvalds idev = __in6_dev_get(dev); 1164072047e4SYOSHIFUJI Hideaki if (!idev) 1165072047e4SYOSHIFUJI Hideaki continue; 1166072047e4SYOSHIFUJI Hideaki 11671da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1168502a2ffdSstephen hemminger list_for_each_entry(score->ifa, &idev->addr_list, if_list) { 1169a9b05723SYOSHIFUJI Hideaki int i; 1170072047e4SYOSHIFUJI Hideaki 1171a9b05723SYOSHIFUJI Hideaki /* 11726b3ae80aSYOSHIFUJI Hideaki * - Tentative Address (RFC2462 section 5.4) 11736b3ae80aSYOSHIFUJI Hideaki * - A tentative address is not considered 11746b3ae80aSYOSHIFUJI Hideaki * "assigned to an interface" in the traditional 117595c385b4SNeil Horman * sense, unless it is also flagged as optimistic. 11766b3ae80aSYOSHIFUJI Hideaki * - Candidate Source Address (section 4) 1177072047e4SYOSHIFUJI Hideaki * - In any case, anycast addresses, multicast 1178072047e4SYOSHIFUJI Hideaki * addresses, and the unspecified address MUST 1179072047e4SYOSHIFUJI Hideaki * NOT be included in a candidate set. 1180072047e4SYOSHIFUJI Hideaki */ 1181a9b05723SYOSHIFUJI Hideaki if ((score->ifa->flags & IFA_F_TENTATIVE) && 1182a9b05723SYOSHIFUJI Hideaki (!(score->ifa->flags & IFA_F_OPTIMISTIC))) 11836b3ae80aSYOSHIFUJI Hideaki continue; 1184a9b05723SYOSHIFUJI Hideaki 1185a9b05723SYOSHIFUJI Hideaki score->addr_type = __ipv6_addr_type(&score->ifa->addr); 1186a9b05723SYOSHIFUJI Hideaki 1187a9b05723SYOSHIFUJI Hideaki if (unlikely(score->addr_type == IPV6_ADDR_ANY || 1188a9b05723SYOSHIFUJI Hideaki score->addr_type & IPV6_ADDR_MULTICAST)) { 1189072047e4SYOSHIFUJI Hideaki LIMIT_NETDEBUG(KERN_DEBUG 1190072047e4SYOSHIFUJI Hideaki "ADDRCONF: unspecified / multicast address " 1191072047e4SYOSHIFUJI Hideaki "assigned as unicast address on %s", 1192072047e4SYOSHIFUJI Hideaki dev->name); 11931da177e4SLinus Torvalds continue; 1194072047e4SYOSHIFUJI Hideaki } 1195072047e4SYOSHIFUJI Hideaki 1196a9b05723SYOSHIFUJI Hideaki score->rule = -1; 1197a9b05723SYOSHIFUJI Hideaki bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX); 1198072047e4SYOSHIFUJI Hideaki 1199a9b05723SYOSHIFUJI Hideaki for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) { 1200a9b05723SYOSHIFUJI Hideaki int minihiscore, miniscore; 1201072047e4SYOSHIFUJI Hideaki 12023de23255SBenjamin Thery minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i); 12033de23255SBenjamin Thery miniscore = ipv6_get_saddr_eval(net, score, &dst, i); 1204072047e4SYOSHIFUJI Hideaki 1205a9b05723SYOSHIFUJI Hideaki if (minihiscore > miniscore) { 1206a9b05723SYOSHIFUJI Hideaki if (i == IPV6_SADDR_RULE_SCOPE && 1207a9b05723SYOSHIFUJI Hideaki score->scopedist > 0) { 1208a9b05723SYOSHIFUJI Hideaki /* 1209a9b05723SYOSHIFUJI Hideaki * special case: 1210a9b05723SYOSHIFUJI Hideaki * each remaining entry 1211a9b05723SYOSHIFUJI Hideaki * has too small (not enough) 1212a9b05723SYOSHIFUJI Hideaki * scope, because ifa entries 1213a9b05723SYOSHIFUJI Hideaki * are sorted by their scope 1214a9b05723SYOSHIFUJI Hideaki * values. 1215072047e4SYOSHIFUJI Hideaki */ 1216a9b05723SYOSHIFUJI Hideaki goto try_nextdev; 1217072047e4SYOSHIFUJI Hideaki } 1218a9b05723SYOSHIFUJI Hideaki break; 1219a9b05723SYOSHIFUJI Hideaki } else if (minihiscore < miniscore) { 1220a9b05723SYOSHIFUJI Hideaki if (hiscore->ifa) 1221a9b05723SYOSHIFUJI Hideaki in6_ifa_put(hiscore->ifa); 122299cd07a5SJuha-Matti Tapio 1223a9b05723SYOSHIFUJI Hideaki in6_ifa_hold(score->ifa); 1224072047e4SYOSHIFUJI Hideaki 1225a0bffffcSIlpo Järvinen swap(hiscore, score); 1226a9b05723SYOSHIFUJI Hideaki 1227a9b05723SYOSHIFUJI Hideaki /* restore our iterator */ 1228a9b05723SYOSHIFUJI Hideaki score->ifa = hiscore->ifa; 1229a9b05723SYOSHIFUJI Hideaki 1230a9b05723SYOSHIFUJI Hideaki break; 12311da177e4SLinus Torvalds } 1232a9b05723SYOSHIFUJI Hideaki } 1233a9b05723SYOSHIFUJI Hideaki } 1234a9b05723SYOSHIFUJI Hideaki try_nextdev: 12351da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 12361da177e4SLinus Torvalds } 12378814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 12381da177e4SLinus Torvalds 1239a9b05723SYOSHIFUJI Hideaki if (!hiscore->ifa) 1240072047e4SYOSHIFUJI Hideaki return -EADDRNOTAVAIL; 12411da177e4SLinus Torvalds 1242a9b05723SYOSHIFUJI Hideaki ipv6_addr_copy(saddr, &hiscore->ifa->addr); 1243a9b05723SYOSHIFUJI Hideaki in6_ifa_put(hiscore->ifa); 1244072047e4SYOSHIFUJI Hideaki return 0; 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(); 1255e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 1256e21e8467SStephen Hemminger if (idev) { 12571da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 12581da177e4SLinus Torvalds 12591da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1260502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 1261502a2ffdSstephen hemminger if (ifp->scope == IFA_LINK && 1262502a2ffdSstephen hemminger !(ifp->flags & banned_flags)) { 12631da177e4SLinus Torvalds ipv6_addr_copy(addr, &ifp->addr); 12641da177e4SLinus Torvalds err = 0; 12651da177e4SLinus Torvalds break; 12661da177e4SLinus Torvalds } 12671da177e4SLinus Torvalds } 12681da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 12691da177e4SLinus Torvalds } 12708814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 12711da177e4SLinus Torvalds return err; 12721da177e4SLinus Torvalds } 12731da177e4SLinus Torvalds 12741da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev) 12751da177e4SLinus Torvalds { 12761da177e4SLinus Torvalds int cnt = 0; 12771da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 12781da177e4SLinus Torvalds 12791da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1280502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) 12811da177e4SLinus Torvalds cnt++; 12821da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 12831da177e4SLinus Torvalds return cnt; 12841da177e4SLinus Torvalds } 12851da177e4SLinus Torvalds 1286bfeade08SDaniel Lezcano int ipv6_chk_addr(struct net *net, struct in6_addr *addr, 1287bfeade08SDaniel Lezcano struct net_device *dev, int strict) 12881da177e4SLinus Torvalds { 1289eedf042aSStephen Hemminger struct inet6_ifaddr *ifp; 1290c2e21293Sstephen hemminger struct hlist_node *node; 12913a88a81dSstephen hemminger unsigned int hash = ipv6_addr_hash(addr); 12921da177e4SLinus Torvalds 12935c578aedSstephen hemminger rcu_read_lock_bh(); 12945c578aedSstephen hemminger hlist_for_each_entry_rcu(ifp, node, &inet6_addr_lst[hash], addr_lst) { 1295878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 1296bfeade08SDaniel Lezcano continue; 12971da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr) && 1298eedf042aSStephen Hemminger !(ifp->flags&IFA_F_TENTATIVE) && 1299eedf042aSStephen Hemminger (dev == NULL || ifp->idev->dev == dev || 1300eedf042aSStephen Hemminger !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) { 13015c578aedSstephen hemminger rcu_read_unlock_bh(); 1302eedf042aSStephen Hemminger return 1; 1303eedf042aSStephen Hemminger } 1304eedf042aSStephen Hemminger } 13055c578aedSstephen hemminger 1306eedf042aSStephen Hemminger rcu_read_unlock_bh(); 1307eedf042aSStephen Hemminger return 0; 13081da177e4SLinus Torvalds } 13097159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_addr); 13107159039aSYOSHIFUJI Hideaki 13113e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 131206bfe655SDaniel Lezcano struct net_device *dev) 13131da177e4SLinus Torvalds { 13143e81c6daSDavid S. Miller unsigned int hash = ipv6_addr_hash(addr); 13151da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 1316c2e21293Sstephen hemminger struct hlist_node *node; 13171da177e4SLinus Torvalds 1318c2e21293Sstephen hemminger hlist_for_each_entry(ifp, node, &inet6_addr_lst[hash], addr_lst) { 1319878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 132006bfe655SDaniel Lezcano continue; 13211da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 13221da177e4SLinus Torvalds if (dev == NULL || ifp->idev->dev == dev) 13233e81c6daSDavid S. Miller return true; 13241da177e4SLinus Torvalds } 13251da177e4SLinus Torvalds } 13263e81c6daSDavid S. Miller return false; 13271da177e4SLinus Torvalds } 13281da177e4SLinus Torvalds 132952eeeb84SYOSHIFUJI Hideaki int ipv6_chk_prefix(struct in6_addr *addr, struct net_device *dev) 133052eeeb84SYOSHIFUJI Hideaki { 133152eeeb84SYOSHIFUJI Hideaki struct inet6_dev *idev; 133252eeeb84SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 133352eeeb84SYOSHIFUJI Hideaki int onlink; 133452eeeb84SYOSHIFUJI Hideaki 133552eeeb84SYOSHIFUJI Hideaki onlink = 0; 133652eeeb84SYOSHIFUJI Hideaki rcu_read_lock(); 133752eeeb84SYOSHIFUJI Hideaki idev = __in6_dev_get(dev); 133852eeeb84SYOSHIFUJI Hideaki if (idev) { 133952eeeb84SYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 1340502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 134152eeeb84SYOSHIFUJI Hideaki onlink = ipv6_prefix_equal(addr, &ifa->addr, 134252eeeb84SYOSHIFUJI Hideaki ifa->prefix_len); 134352eeeb84SYOSHIFUJI Hideaki if (onlink) 134452eeeb84SYOSHIFUJI Hideaki break; 134552eeeb84SYOSHIFUJI Hideaki } 134652eeeb84SYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 134752eeeb84SYOSHIFUJI Hideaki } 134852eeeb84SYOSHIFUJI Hideaki rcu_read_unlock(); 134952eeeb84SYOSHIFUJI Hideaki return onlink; 135052eeeb84SYOSHIFUJI Hideaki } 135152eeeb84SYOSHIFUJI Hideaki 135252eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix); 135352eeeb84SYOSHIFUJI Hideaki 13549acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr, 13551cab3da6SDaniel Lezcano struct net_device *dev, int strict) 13561da177e4SLinus Torvalds { 1357b79d1d54SDavid S. Miller struct inet6_ifaddr *ifp, *result = NULL; 13583a88a81dSstephen hemminger unsigned int hash = ipv6_addr_hash(addr); 1359b79d1d54SDavid S. Miller struct hlist_node *node; 13601da177e4SLinus Torvalds 13615c578aedSstephen hemminger rcu_read_lock_bh(); 13624f70eccaSEric Dumazet hlist_for_each_entry_rcu_bh(ifp, node, &inet6_addr_lst[hash], addr_lst) { 1363878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 13641cab3da6SDaniel Lezcano continue; 13651da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 13661da177e4SLinus Torvalds if (dev == NULL || ifp->idev->dev == dev || 13671da177e4SLinus Torvalds !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) { 1368b79d1d54SDavid S. Miller result = ifp; 13691da177e4SLinus Torvalds in6_ifa_hold(ifp); 13701da177e4SLinus Torvalds break; 13711da177e4SLinus Torvalds } 13721da177e4SLinus Torvalds } 13731da177e4SLinus Torvalds } 13745c578aedSstephen hemminger rcu_read_unlock_bh(); 13751da177e4SLinus Torvalds 1376b79d1d54SDavid S. Miller return result; 13771da177e4SLinus Torvalds } 13781da177e4SLinus Torvalds 13791da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */ 13801da177e4SLinus Torvalds 1381cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed) 13821da177e4SLinus Torvalds { 13831da177e4SLinus Torvalds if (ifp->flags&IFA_F_PERMANENT) { 13841da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 13851da177e4SLinus Torvalds addrconf_del_timer(ifp); 13861da177e4SLinus Torvalds ifp->flags |= IFA_F_TENTATIVE; 1387cc411d0bSBrian Haley if (dad_failed) 1388cc411d0bSBrian Haley ifp->flags |= IFA_F_DADFAILED; 13891da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 1390e2577a06SHerbert Xu if (dad_failed) 1391e2577a06SHerbert Xu ipv6_ifa_notify(0, ifp); 13921da177e4SLinus Torvalds in6_ifa_put(ifp); 13931da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 13941da177e4SLinus Torvalds } else if (ifp->flags&IFA_F_TEMPORARY) { 13951da177e4SLinus Torvalds struct inet6_ifaddr *ifpub; 13961da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 13971da177e4SLinus Torvalds ifpub = ifp->ifpub; 13981da177e4SLinus Torvalds if (ifpub) { 13991da177e4SLinus Torvalds in6_ifa_hold(ifpub); 14001da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 14011da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 14021da177e4SLinus Torvalds in6_ifa_put(ifpub); 14031da177e4SLinus Torvalds } else { 14041da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 14051da177e4SLinus Torvalds } 14061da177e4SLinus Torvalds ipv6_del_addr(ifp); 14071da177e4SLinus Torvalds #endif 14081da177e4SLinus Torvalds } else 14091da177e4SLinus Torvalds ipv6_del_addr(ifp); 14101da177e4SLinus Torvalds } 14111da177e4SLinus Torvalds 1412f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp) 1413f2344a13SHerbert Xu { 1414f2344a13SHerbert Xu int err = -ENOENT; 1415f2344a13SHerbert Xu 1416f2344a13SHerbert Xu spin_lock(&ifp->state_lock); 1417f2344a13SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DAD) { 1418f2344a13SHerbert Xu ifp->state = INET6_IFADDR_STATE_POSTDAD; 1419f2344a13SHerbert Xu err = 0; 1420f2344a13SHerbert Xu } 1421f2344a13SHerbert Xu spin_unlock(&ifp->state_lock); 1422f2344a13SHerbert Xu 1423f2344a13SHerbert Xu return err; 1424f2344a13SHerbert Xu } 1425f2344a13SHerbert Xu 14263c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp) 14273c21edbdSYOSHIFUJI Hideaki { 14281b34be74SYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 14299bdd8d40SBrian Haley 1430*853dc2e0SUrsula Braun if (addrconf_dad_end(ifp)) { 1431*853dc2e0SUrsula Braun in6_ifa_put(ifp); 1432f2344a13SHerbert Xu return; 1433*853dc2e0SUrsula Braun } 1434f2344a13SHerbert Xu 14359bdd8d40SBrian Haley if (net_ratelimit()) 14360522fea6SJens Rosenboom printk(KERN_INFO "%s: IPv6 duplicate address %pI6c detected!\n", 14370522fea6SJens Rosenboom ifp->idev->dev->name, &ifp->addr); 14389bdd8d40SBrian Haley 14391b34be74SYOSHIFUJI Hideaki if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) { 14401b34be74SYOSHIFUJI Hideaki struct in6_addr addr; 14411b34be74SYOSHIFUJI Hideaki 14421b34be74SYOSHIFUJI Hideaki addr.s6_addr32[0] = htonl(0xfe800000); 14431b34be74SYOSHIFUJI Hideaki addr.s6_addr32[1] = 0; 14441b34be74SYOSHIFUJI Hideaki 14451b34be74SYOSHIFUJI Hideaki if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) && 14461b34be74SYOSHIFUJI Hideaki ipv6_addr_equal(&ifp->addr, &addr)) { 14471b34be74SYOSHIFUJI Hideaki /* DAD failed for link-local based on MAC address */ 14481b34be74SYOSHIFUJI Hideaki idev->cnf.disable_ipv6 = 1; 14499bdd8d40SBrian Haley 14509bdd8d40SBrian Haley printk(KERN_INFO "%s: IPv6 being disabled!\n", 14519bdd8d40SBrian Haley ifp->idev->dev->name); 14521b34be74SYOSHIFUJI Hideaki } 14531b34be74SYOSHIFUJI Hideaki } 14541b34be74SYOSHIFUJI Hideaki 1455cc411d0bSBrian Haley addrconf_dad_stop(ifp, 1); 14563c21edbdSYOSHIFUJI Hideaki } 14571da177e4SLinus Torvalds 14581da177e4SLinus Torvalds /* Join to solicited addr multicast group. */ 14591da177e4SLinus Torvalds 14601da177e4SLinus Torvalds void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr) 14611da177e4SLinus Torvalds { 14621da177e4SLinus Torvalds struct in6_addr maddr; 14631da177e4SLinus Torvalds 14641da177e4SLinus Torvalds if (dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 14651da177e4SLinus Torvalds return; 14661da177e4SLinus Torvalds 14671da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 14681da177e4SLinus Torvalds ipv6_dev_mc_inc(dev, &maddr); 14691da177e4SLinus Torvalds } 14701da177e4SLinus Torvalds 14711da177e4SLinus Torvalds void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr) 14721da177e4SLinus Torvalds { 14731da177e4SLinus Torvalds struct in6_addr maddr; 14741da177e4SLinus Torvalds 14751da177e4SLinus Torvalds if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 14761da177e4SLinus Torvalds return; 14771da177e4SLinus Torvalds 14781da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 14791da177e4SLinus Torvalds __ipv6_dev_mc_dec(idev, &maddr); 14801da177e4SLinus Torvalds } 14811da177e4SLinus Torvalds 148220380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp) 14831da177e4SLinus Torvalds { 14841da177e4SLinus Torvalds struct in6_addr addr; 14851da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 14861da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 14871da177e4SLinus Torvalds return; 14881da177e4SLinus Torvalds ipv6_dev_ac_inc(ifp->idev->dev, &addr); 14891da177e4SLinus Torvalds } 14901da177e4SLinus Torvalds 149120380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp) 14921da177e4SLinus Torvalds { 14931da177e4SLinus Torvalds struct in6_addr addr; 14941da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 14951da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 14961da177e4SLinus Torvalds return; 14971da177e4SLinus Torvalds __ipv6_dev_ac_dec(ifp->idev, &addr); 14981da177e4SLinus Torvalds } 14991da177e4SLinus Torvalds 1500073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev) 15011da177e4SLinus Torvalds { 15021da177e4SLinus Torvalds if (dev->addr_len != ETH_ALEN) 15031da177e4SLinus Torvalds return -1; 15041da177e4SLinus Torvalds memcpy(eui, dev->dev_addr, 3); 15051da177e4SLinus Torvalds memcpy(eui + 5, dev->dev_addr + 3, 3); 15061da177e4SLinus Torvalds 15071da177e4SLinus Torvalds /* 15081da177e4SLinus Torvalds * The zSeries OSA network cards can be shared among various 15091da177e4SLinus Torvalds * OS instances, but the OSA cards have only one MAC address. 15101da177e4SLinus Torvalds * This leads to duplicate address conflicts in conjunction 15111da177e4SLinus Torvalds * with IPv6 if more than one instance uses the same card. 15121da177e4SLinus Torvalds * 15131da177e4SLinus Torvalds * The driver for these cards can deliver a unique 16-bit 15141da177e4SLinus Torvalds * identifier for each instance sharing the same card. It is 15151da177e4SLinus Torvalds * placed instead of 0xFFFE in the interface identifier. The 15161da177e4SLinus Torvalds * "u" bit of the interface identifier is not inverted in this 15171da177e4SLinus Torvalds * case. Hence the resulting interface identifier has local 15181da177e4SLinus Torvalds * scope according to RFC2373. 15191da177e4SLinus Torvalds */ 15201da177e4SLinus Torvalds if (dev->dev_id) { 15211da177e4SLinus Torvalds eui[3] = (dev->dev_id >> 8) & 0xFF; 15221da177e4SLinus Torvalds eui[4] = dev->dev_id & 0xFF; 15231da177e4SLinus Torvalds } else { 15241da177e4SLinus Torvalds eui[3] = 0xFF; 15251da177e4SLinus Torvalds eui[4] = 0xFE; 15261da177e4SLinus Torvalds eui[0] ^= 2; 15271da177e4SLinus Torvalds } 15281da177e4SLinus Torvalds return 0; 1529073a8e0eSYOSHIFUJI Hideaki } 1530073a8e0eSYOSHIFUJI Hideaki 1531073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev) 1532073a8e0eSYOSHIFUJI Hideaki { 15331da177e4SLinus Torvalds /* XXX: inherit EUI-64 from other interface -- yoshfuji */ 15341da177e4SLinus Torvalds if (dev->addr_len != ARCNET_ALEN) 15351da177e4SLinus Torvalds return -1; 15361da177e4SLinus Torvalds memset(eui, 0, 7); 15371da177e4SLinus Torvalds eui[7] = *(u8*)dev->dev_addr; 15381da177e4SLinus Torvalds return 0; 1539073a8e0eSYOSHIFUJI Hideaki } 1540073a8e0eSYOSHIFUJI Hideaki 1541073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev) 1542073a8e0eSYOSHIFUJI Hideaki { 15431da177e4SLinus Torvalds if (dev->addr_len != INFINIBAND_ALEN) 15441da177e4SLinus Torvalds return -1; 15451da177e4SLinus Torvalds memcpy(eui, dev->dev_addr + 12, 8); 15461da177e4SLinus Torvalds eui[0] |= 2; 15471da177e4SLinus Torvalds return 0; 15481da177e4SLinus Torvalds } 1549073a8e0eSYOSHIFUJI Hideaki 1550c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr) 1551dfd982baSYOSHIFUJI Hideaki { 15529af28511SSascha Hlusiak if (addr == 0) 15539af28511SSascha Hlusiak return -1; 1554dfd982baSYOSHIFUJI Hideaki eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) || 1555dfd982baSYOSHIFUJI Hideaki ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) || 1556dfd982baSYOSHIFUJI Hideaki ipv4_is_private_172(addr) || ipv4_is_test_192(addr) || 1557dfd982baSYOSHIFUJI Hideaki ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) || 1558dfd982baSYOSHIFUJI Hideaki ipv4_is_test_198(addr) || ipv4_is_multicast(addr) || 1559dfd982baSYOSHIFUJI Hideaki ipv4_is_lbcast(addr)) ? 0x00 : 0x02; 1560dfd982baSYOSHIFUJI Hideaki eui[1] = 0; 1561dfd982baSYOSHIFUJI Hideaki eui[2] = 0x5E; 1562dfd982baSYOSHIFUJI Hideaki eui[3] = 0xFE; 1563dfd982baSYOSHIFUJI Hideaki memcpy(eui + 4, &addr, 4); 1564dfd982baSYOSHIFUJI Hideaki return 0; 1565dfd982baSYOSHIFUJI Hideaki } 1566dfd982baSYOSHIFUJI Hideaki 1567dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev) 1568dfd982baSYOSHIFUJI Hideaki { 1569dfd982baSYOSHIFUJI Hideaki if (dev->priv_flags & IFF_ISATAP) 1570dfd982baSYOSHIFUJI Hideaki return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 1571dfd982baSYOSHIFUJI Hideaki return -1; 1572dfd982baSYOSHIFUJI Hideaki } 1573dfd982baSYOSHIFUJI Hideaki 1574073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev) 1575073a8e0eSYOSHIFUJI Hideaki { 1576073a8e0eSYOSHIFUJI Hideaki switch (dev->type) { 1577073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ETHER: 1578073a8e0eSYOSHIFUJI Hideaki case ARPHRD_FDDI: 1579073a8e0eSYOSHIFUJI Hideaki case ARPHRD_IEEE802_TR: 1580073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_eui48(eui, dev); 1581073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ARCNET: 1582073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_arcnet(eui, dev); 1583073a8e0eSYOSHIFUJI Hideaki case ARPHRD_INFINIBAND: 1584073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_infiniband(eui, dev); 1585c7dc89c0SFred L. Templin case ARPHRD_SIT: 1586dfd982baSYOSHIFUJI Hideaki return addrconf_ifid_sit(eui, dev); 1587073a8e0eSYOSHIFUJI Hideaki } 15881da177e4SLinus Torvalds return -1; 15891da177e4SLinus Torvalds } 15901da177e4SLinus Torvalds 15911da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev) 15921da177e4SLinus Torvalds { 15931da177e4SLinus Torvalds int err = -1; 15941da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 15951da177e4SLinus Torvalds 15961da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1597502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 15981da177e4SLinus Torvalds if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) { 15991da177e4SLinus Torvalds memcpy(eui, ifp->addr.s6_addr+8, 8); 16001da177e4SLinus Torvalds err = 0; 16011da177e4SLinus Torvalds break; 16021da177e4SLinus Torvalds } 16031da177e4SLinus Torvalds } 16041da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 16051da177e4SLinus Torvalds return err; 16061da177e4SLinus Torvalds } 16071da177e4SLinus Torvalds 16081da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 16091da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */ 16101da177e4SLinus Torvalds static int __ipv6_regen_rndid(struct inet6_dev *idev) 16111da177e4SLinus Torvalds { 16121da177e4SLinus Torvalds regen: 1613955189efSYOSHIFUJI Hideaki get_random_bytes(idev->rndid, sizeof(idev->rndid)); 16141da177e4SLinus Torvalds idev->rndid[0] &= ~0x02; 16151da177e4SLinus Torvalds 16161da177e4SLinus Torvalds /* 16171da177e4SLinus Torvalds * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>: 16181da177e4SLinus Torvalds * check if generated address is not inappropriate 16191da177e4SLinus Torvalds * 16201da177e4SLinus Torvalds * - Reserved subnet anycast (RFC 2526) 16211da177e4SLinus Torvalds * 11111101 11....11 1xxxxxxx 1622c7dc89c0SFred L. Templin * - ISATAP (RFC4214) 6.1 16231da177e4SLinus Torvalds * 00-00-5E-FE-xx-xx-xx-xx 16241da177e4SLinus Torvalds * - value 0 16251da177e4SLinus Torvalds * - XXX: already assigned to an address on the device 16261da177e4SLinus Torvalds */ 16271da177e4SLinus Torvalds if (idev->rndid[0] == 0xfd && 16281da177e4SLinus Torvalds (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff && 16291da177e4SLinus Torvalds (idev->rndid[7]&0x80)) 16301da177e4SLinus Torvalds goto regen; 16311da177e4SLinus Torvalds if ((idev->rndid[0]|idev->rndid[1]) == 0) { 16321da177e4SLinus Torvalds if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe) 16331da177e4SLinus Torvalds goto regen; 16341da177e4SLinus Torvalds if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00) 16351da177e4SLinus Torvalds goto regen; 16361da177e4SLinus Torvalds } 16371da177e4SLinus Torvalds 16381da177e4SLinus Torvalds return 0; 16391da177e4SLinus Torvalds } 16401da177e4SLinus Torvalds 16411da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data) 16421da177e4SLinus Torvalds { 16431da177e4SLinus Torvalds struct inet6_dev *idev = (struct inet6_dev *) data; 16441da177e4SLinus Torvalds unsigned long expires; 16451da177e4SLinus Torvalds 16468814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 16471da177e4SLinus Torvalds write_lock_bh(&idev->lock); 16481da177e4SLinus Torvalds 16491da177e4SLinus Torvalds if (idev->dead) 16501da177e4SLinus Torvalds goto out; 16511da177e4SLinus Torvalds 16521da177e4SLinus Torvalds if (__ipv6_regen_rndid(idev) < 0) 16531da177e4SLinus Torvalds goto out; 16541da177e4SLinus Torvalds 16551da177e4SLinus Torvalds expires = jiffies + 16561da177e4SLinus Torvalds idev->cnf.temp_prefered_lft * HZ - 1657784e2710SBen Hutchings idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - 1658784e2710SBen Hutchings idev->cnf.max_desync_factor * HZ; 16591da177e4SLinus Torvalds if (time_before(expires, jiffies)) { 16601da177e4SLinus Torvalds printk(KERN_WARNING 16611da177e4SLinus Torvalds "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n", 16621da177e4SLinus Torvalds idev->dev->name); 16631da177e4SLinus Torvalds goto out; 16641da177e4SLinus Torvalds } 16651da177e4SLinus Torvalds 16661da177e4SLinus Torvalds if (!mod_timer(&idev->regen_timer, expires)) 16671da177e4SLinus Torvalds in6_dev_hold(idev); 16681da177e4SLinus Torvalds 16691da177e4SLinus Torvalds out: 16701da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 16718814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 16721da177e4SLinus Torvalds in6_dev_put(idev); 16731da177e4SLinus Torvalds } 16741da177e4SLinus Torvalds 16751da177e4SLinus Torvalds static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) { 16761da177e4SLinus Torvalds int ret = 0; 16771da177e4SLinus Torvalds 16781da177e4SLinus Torvalds if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0) 16791da177e4SLinus Torvalds ret = __ipv6_regen_rndid(idev); 16801da177e4SLinus Torvalds return ret; 16811da177e4SLinus Torvalds } 16821da177e4SLinus Torvalds #endif 16831da177e4SLinus Torvalds 16841da177e4SLinus Torvalds /* 16851da177e4SLinus Torvalds * Add prefix route. 16861da177e4SLinus Torvalds */ 16871da177e4SLinus Torvalds 16881da177e4SLinus Torvalds static void 16891da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev, 1690e431b8c0SJamal Hadi Salim unsigned long expires, u32 flags) 16911da177e4SLinus Torvalds { 169286872cb5SThomas Graf struct fib6_config cfg = { 169386872cb5SThomas Graf .fc_table = RT6_TABLE_PREFIX, 169486872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 169586872cb5SThomas Graf .fc_ifindex = dev->ifindex, 169686872cb5SThomas Graf .fc_expires = expires, 169786872cb5SThomas Graf .fc_dst_len = plen, 169886872cb5SThomas Graf .fc_flags = RTF_UP | flags, 1699c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 1700f410a1fbSStephen Hemminger .fc_protocol = RTPROT_KERNEL, 170186872cb5SThomas Graf }; 17021da177e4SLinus Torvalds 170386872cb5SThomas Graf ipv6_addr_copy(&cfg.fc_dst, pfx); 17041da177e4SLinus Torvalds 17051da177e4SLinus Torvalds /* Prevent useless cloning on PtP SIT. 17061da177e4SLinus Torvalds This thing is done here expecting that the whole 17071da177e4SLinus Torvalds class of non-broadcast devices need not cloning. 17081da177e4SLinus Torvalds */ 17090be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 17101da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT)) 171186872cb5SThomas Graf cfg.fc_flags |= RTF_NONEXTHOP; 17120be669bbSJoerg Roedel #endif 17131da177e4SLinus Torvalds 171486872cb5SThomas Graf ip6_route_add(&cfg); 17151da177e4SLinus Torvalds } 17161da177e4SLinus Torvalds 17171da177e4SLinus Torvalds /* Create "default" multicast route to the interface */ 17181da177e4SLinus Torvalds 17191da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev) 17201da177e4SLinus Torvalds { 172186872cb5SThomas Graf struct fib6_config cfg = { 172286872cb5SThomas Graf .fc_table = RT6_TABLE_LOCAL, 172386872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 172486872cb5SThomas Graf .fc_ifindex = dev->ifindex, 172586872cb5SThomas Graf .fc_dst_len = 8, 172686872cb5SThomas Graf .fc_flags = RTF_UP, 1727c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 172886872cb5SThomas Graf }; 17291da177e4SLinus Torvalds 173086872cb5SThomas Graf ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0); 173186872cb5SThomas Graf 173286872cb5SThomas Graf ip6_route_add(&cfg); 17331da177e4SLinus Torvalds } 17341da177e4SLinus Torvalds 17350be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 17361da177e4SLinus Torvalds static void sit_route_add(struct net_device *dev) 17371da177e4SLinus Torvalds { 173886872cb5SThomas Graf struct fib6_config cfg = { 173986872cb5SThomas Graf .fc_table = RT6_TABLE_MAIN, 174086872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 174186872cb5SThomas Graf .fc_ifindex = dev->ifindex, 174286872cb5SThomas Graf .fc_dst_len = 96, 174386872cb5SThomas Graf .fc_flags = RTF_UP | RTF_NONEXTHOP, 1744c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 174586872cb5SThomas Graf }; 17461da177e4SLinus Torvalds 17471da177e4SLinus Torvalds /* prefix length - 96 bits "::d.d.d.d" */ 174886872cb5SThomas Graf ip6_route_add(&cfg); 17491da177e4SLinus Torvalds } 17500be669bbSJoerg Roedel #endif 17511da177e4SLinus Torvalds 17521da177e4SLinus Torvalds static void addrconf_add_lroute(struct net_device *dev) 17531da177e4SLinus Torvalds { 17541da177e4SLinus Torvalds struct in6_addr addr; 17551da177e4SLinus Torvalds 17561da177e4SLinus Torvalds ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 17571da177e4SLinus Torvalds addrconf_prefix_route(&addr, 64, dev, 0, 0); 17581da177e4SLinus Torvalds } 17591da177e4SLinus Torvalds 17601da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev) 17611da177e4SLinus Torvalds { 17621da177e4SLinus Torvalds struct inet6_dev *idev; 17631da177e4SLinus Torvalds 17641da177e4SLinus Torvalds ASSERT_RTNL(); 17651da177e4SLinus Torvalds 1766e21e8467SStephen Hemminger idev = ipv6_find_idev(dev); 1767e21e8467SStephen Hemminger if (!idev) 176864e724f6SBrian Haley return ERR_PTR(-ENOBUFS); 176964e724f6SBrian Haley 177064e724f6SBrian Haley if (idev->cnf.disable_ipv6) 177164e724f6SBrian Haley return ERR_PTR(-EACCES); 17721da177e4SLinus Torvalds 17731da177e4SLinus Torvalds /* Add default multicast route */ 17741da177e4SLinus Torvalds addrconf_add_mroute(dev); 17751da177e4SLinus Torvalds 17761da177e4SLinus Torvalds /* Add link local route */ 17771da177e4SLinus Torvalds addrconf_add_lroute(dev); 17781da177e4SLinus Torvalds return idev; 17791da177e4SLinus Torvalds } 17801da177e4SLinus Torvalds 17811da177e4SLinus Torvalds void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len) 17821da177e4SLinus Torvalds { 17831da177e4SLinus Torvalds struct prefix_info *pinfo; 17841da177e4SLinus Torvalds __u32 valid_lft; 17851da177e4SLinus Torvalds __u32 prefered_lft; 17861da177e4SLinus Torvalds int addr_type; 17871da177e4SLinus Torvalds struct inet6_dev *in6_dev; 178856d417b1SBrian Haley struct net *net = dev_net(dev); 17891da177e4SLinus Torvalds 17901da177e4SLinus Torvalds pinfo = (struct prefix_info *) opt; 17911da177e4SLinus Torvalds 17921da177e4SLinus Torvalds if (len < sizeof(struct prefix_info)) { 17931da177e4SLinus Torvalds ADBG(("addrconf: prefix option too short\n")); 17941da177e4SLinus Torvalds return; 17951da177e4SLinus Torvalds } 17961da177e4SLinus Torvalds 17971da177e4SLinus Torvalds /* 17981da177e4SLinus Torvalds * Validation checks ([ADDRCONF], page 19) 17991da177e4SLinus Torvalds */ 18001da177e4SLinus Torvalds 18011da177e4SLinus Torvalds addr_type = ipv6_addr_type(&pinfo->prefix); 18021da177e4SLinus Torvalds 18031da177e4SLinus Torvalds if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)) 18041da177e4SLinus Torvalds return; 18051da177e4SLinus Torvalds 18061da177e4SLinus Torvalds valid_lft = ntohl(pinfo->valid); 18071da177e4SLinus Torvalds prefered_lft = ntohl(pinfo->prefered); 18081da177e4SLinus Torvalds 18091da177e4SLinus Torvalds if (prefered_lft > valid_lft) { 18101da177e4SLinus Torvalds if (net_ratelimit()) 18111da177e4SLinus Torvalds printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n"); 18121da177e4SLinus Torvalds return; 18131da177e4SLinus Torvalds } 18141da177e4SLinus Torvalds 18151da177e4SLinus Torvalds in6_dev = in6_dev_get(dev); 18161da177e4SLinus Torvalds 18171da177e4SLinus Torvalds if (in6_dev == NULL) { 18181da177e4SLinus Torvalds if (net_ratelimit()) 18191da177e4SLinus Torvalds printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name); 18201da177e4SLinus Torvalds return; 18211da177e4SLinus Torvalds } 18221da177e4SLinus Torvalds 18231da177e4SLinus Torvalds /* 18241da177e4SLinus Torvalds * Two things going on here: 18251da177e4SLinus Torvalds * 1) Add routes for on-link prefixes 18261da177e4SLinus Torvalds * 2) Configure prefixes with the auto flag set 18271da177e4SLinus Torvalds */ 18281da177e4SLinus Torvalds 18294bed72e4SYOSHIFUJI Hideaki if (pinfo->onlink) { 18304bed72e4SYOSHIFUJI Hideaki struct rt6_info *rt; 18314bed72e4SYOSHIFUJI Hideaki unsigned long rt_expires; 18324bed72e4SYOSHIFUJI Hideaki 18331da177e4SLinus Torvalds /* Avoid arithmetic overflow. Really, we could 18346f704992SYOSHIFUJI Hideaki * save rt_expires in seconds, likely valid_lft, 18356f704992SYOSHIFUJI Hideaki * but it would require division in fib gc, that it 18366f704992SYOSHIFUJI Hideaki * not good. 18371da177e4SLinus Torvalds */ 18384bed72e4SYOSHIFUJI Hideaki if (HZ > USER_HZ) 18394bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, HZ); 18404bed72e4SYOSHIFUJI Hideaki else 18414bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ); 18423dd4bc68SYOSHIFUJI Hideaki 18434bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) 18444bed72e4SYOSHIFUJI Hideaki rt_expires *= HZ; 18451da177e4SLinus Torvalds 184656d417b1SBrian Haley rt = rt6_lookup(net, &pinfo->prefix, NULL, 18476fda7350SBenjamin Thery dev->ifindex, 1); 18481da177e4SLinus Torvalds 18492b5ead46SYOSHIFUJI Hideaki if (rt && addrconf_is_prefix_route(rt)) { 18506f704992SYOSHIFUJI Hideaki /* Autoconf prefix route */ 18511da177e4SLinus Torvalds if (valid_lft == 0) { 1852e0a1ad73SThomas Graf ip6_del_rt(rt); 18531da177e4SLinus Torvalds rt = NULL; 18544bed72e4SYOSHIFUJI Hideaki } else if (addrconf_finite_timeout(rt_expires)) { 18556f704992SYOSHIFUJI Hideaki /* not infinity */ 18563dd4bc68SYOSHIFUJI Hideaki rt->rt6i_expires = jiffies + rt_expires; 18576f704992SYOSHIFUJI Hideaki rt->rt6i_flags |= RTF_EXPIRES; 18586f704992SYOSHIFUJI Hideaki } else { 18596f704992SYOSHIFUJI Hideaki rt->rt6i_flags &= ~RTF_EXPIRES; 18606f704992SYOSHIFUJI Hideaki rt->rt6i_expires = 0; 18611da177e4SLinus Torvalds } 18621da177e4SLinus Torvalds } else if (valid_lft) { 18636f704992SYOSHIFUJI Hideaki clock_t expires = 0; 18644bed72e4SYOSHIFUJI Hideaki int flags = RTF_ADDRCONF | RTF_PREFIX_RT; 18654bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) { 18666f704992SYOSHIFUJI Hideaki /* not infinity */ 18676f704992SYOSHIFUJI Hideaki flags |= RTF_EXPIRES; 18686f704992SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(rt_expires); 18696f704992SYOSHIFUJI Hideaki } 18701da177e4SLinus Torvalds addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len, 18716f704992SYOSHIFUJI Hideaki dev, expires, flags); 18721da177e4SLinus Torvalds } 18731da177e4SLinus Torvalds if (rt) 1874d8d1f30bSChangli Gao dst_release(&rt->dst); 18751da177e4SLinus Torvalds } 18761da177e4SLinus Torvalds 18771da177e4SLinus Torvalds /* Try to figure out our local address for this prefix */ 18781da177e4SLinus Torvalds 18791da177e4SLinus Torvalds if (pinfo->autoconf && in6_dev->cnf.autoconf) { 18801da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 18811da177e4SLinus Torvalds struct in6_addr addr; 18821da177e4SLinus Torvalds int create = 0, update_lft = 0; 18831da177e4SLinus Torvalds 18841da177e4SLinus Torvalds if (pinfo->prefix_len == 64) { 18851da177e4SLinus Torvalds memcpy(&addr, &pinfo->prefix, 8); 18861da177e4SLinus Torvalds if (ipv6_generate_eui64(addr.s6_addr + 8, dev) && 18871da177e4SLinus Torvalds ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) { 18881da177e4SLinus Torvalds in6_dev_put(in6_dev); 18891da177e4SLinus Torvalds return; 18901da177e4SLinus Torvalds } 18911da177e4SLinus Torvalds goto ok; 18921da177e4SLinus Torvalds } 18931da177e4SLinus Torvalds if (net_ratelimit()) 18941da177e4SLinus Torvalds printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n", 18951da177e4SLinus Torvalds pinfo->prefix_len); 18961da177e4SLinus Torvalds in6_dev_put(in6_dev); 18971da177e4SLinus Torvalds return; 18981da177e4SLinus Torvalds 18991da177e4SLinus Torvalds ok: 19001da177e4SLinus Torvalds 190153b7997fSYOSHIFUJI Hideaki ifp = ipv6_get_ifaddr(net, &addr, dev, 1); 19021da177e4SLinus Torvalds 19031da177e4SLinus Torvalds if (ifp == NULL && valid_lft) { 19041da177e4SLinus Torvalds int max_addresses = in6_dev->cnf.max_addresses; 190595c385b4SNeil Horman u32 addr_flags = 0; 190695c385b4SNeil Horman 190795c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 190895c385b4SNeil Horman if (in6_dev->cnf.optimistic_dad && 190953b7997fSYOSHIFUJI Hideaki !net->ipv6.devconf_all->forwarding) 191095c385b4SNeil Horman addr_flags = IFA_F_OPTIMISTIC; 191195c385b4SNeil Horman #endif 19121da177e4SLinus Torvalds 19131da177e4SLinus Torvalds /* Do not allow to create too much of autoconfigured 19141da177e4SLinus Torvalds * addresses; this would be too easy way to crash kernel. 19151da177e4SLinus Torvalds */ 19161da177e4SLinus Torvalds if (!max_addresses || 19171da177e4SLinus Torvalds ipv6_count_addresses(in6_dev) < max_addresses) 19181da177e4SLinus Torvalds ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len, 191995c385b4SNeil Horman addr_type&IPV6_ADDR_SCOPE_MASK, 192095c385b4SNeil Horman addr_flags); 19211da177e4SLinus Torvalds 19221da177e4SLinus Torvalds if (!ifp || IS_ERR(ifp)) { 19231da177e4SLinus Torvalds in6_dev_put(in6_dev); 19241da177e4SLinus Torvalds return; 19251da177e4SLinus Torvalds } 19261da177e4SLinus Torvalds 19271da177e4SLinus Torvalds update_lft = create = 1; 19281da177e4SLinus Torvalds ifp->cstamp = jiffies; 19291da177e4SLinus Torvalds addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT); 19301da177e4SLinus Torvalds } 19311da177e4SLinus Torvalds 19321da177e4SLinus Torvalds if (ifp) { 19331da177e4SLinus Torvalds int flags; 19341da177e4SLinus Torvalds unsigned long now; 19351da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 19361da177e4SLinus Torvalds struct inet6_ifaddr *ift; 19371da177e4SLinus Torvalds #endif 19381da177e4SLinus Torvalds u32 stored_lft; 19391da177e4SLinus Torvalds 19401da177e4SLinus Torvalds /* update lifetime (RFC2462 5.5.3 e) */ 19411da177e4SLinus Torvalds spin_lock(&ifp->lock); 19421da177e4SLinus Torvalds now = jiffies; 19431da177e4SLinus Torvalds if (ifp->valid_lft > (now - ifp->tstamp) / HZ) 19441da177e4SLinus Torvalds stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ; 19451da177e4SLinus Torvalds else 19461da177e4SLinus Torvalds stored_lft = 0; 19471da177e4SLinus Torvalds if (!update_lft && stored_lft) { 19481da177e4SLinus Torvalds if (valid_lft > MIN_VALID_LIFETIME || 19491da177e4SLinus Torvalds valid_lft > stored_lft) 19501da177e4SLinus Torvalds update_lft = 1; 19511da177e4SLinus Torvalds else if (stored_lft <= MIN_VALID_LIFETIME) { 19521da177e4SLinus Torvalds /* valid_lft <= stored_lft is always true */ 1953a1ed0526SBrian Haley /* 1954a1ed0526SBrian Haley * RFC 4862 Section 5.5.3e: 1955a1ed0526SBrian Haley * "Note that the preferred lifetime of 1956a1ed0526SBrian Haley * the corresponding address is always 1957a1ed0526SBrian Haley * reset to the Preferred Lifetime in 1958a1ed0526SBrian Haley * the received Prefix Information 1959a1ed0526SBrian Haley * option, regardless of whether the 1960a1ed0526SBrian Haley * valid lifetime is also reset or 1961a1ed0526SBrian Haley * ignored." 1962a1ed0526SBrian Haley * 1963a1ed0526SBrian Haley * So if the preferred lifetime in 1964a1ed0526SBrian Haley * this advertisement is different 1965a1ed0526SBrian Haley * than what we have stored, but the 1966a1ed0526SBrian Haley * valid lifetime is invalid, just 1967a1ed0526SBrian Haley * reset prefered_lft. 1968a1ed0526SBrian Haley * 1969a1ed0526SBrian Haley * We must set the valid lifetime 1970a1ed0526SBrian Haley * to the stored lifetime since we'll 1971a1ed0526SBrian Haley * be updating the timestamp below, 1972a1ed0526SBrian Haley * else we'll set it back to the 1973a1ed0526SBrian Haley * minumum. 1974a1ed0526SBrian Haley */ 1975a1ed0526SBrian Haley if (prefered_lft != ifp->prefered_lft) { 1976a1ed0526SBrian Haley valid_lft = stored_lft; 1977a1ed0526SBrian Haley update_lft = 1; 1978a1ed0526SBrian Haley } 19791da177e4SLinus Torvalds } else { 19801da177e4SLinus Torvalds valid_lft = MIN_VALID_LIFETIME; 19811da177e4SLinus Torvalds if (valid_lft < prefered_lft) 19821da177e4SLinus Torvalds prefered_lft = valid_lft; 19831da177e4SLinus Torvalds update_lft = 1; 19841da177e4SLinus Torvalds } 19851da177e4SLinus Torvalds } 19861da177e4SLinus Torvalds 19871da177e4SLinus Torvalds if (update_lft) { 19881da177e4SLinus Torvalds ifp->valid_lft = valid_lft; 19891da177e4SLinus Torvalds ifp->prefered_lft = prefered_lft; 19901da177e4SLinus Torvalds ifp->tstamp = now; 19911da177e4SLinus Torvalds flags = ifp->flags; 19921da177e4SLinus Torvalds ifp->flags &= ~IFA_F_DEPRECATED; 19931da177e4SLinus Torvalds spin_unlock(&ifp->lock); 19941da177e4SLinus Torvalds 19951da177e4SLinus Torvalds if (!(flags&IFA_F_TENTATIVE)) 19961da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 19971da177e4SLinus Torvalds } else 19981da177e4SLinus Torvalds spin_unlock(&ifp->lock); 19991da177e4SLinus Torvalds 20001da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 20011da177e4SLinus Torvalds read_lock_bh(&in6_dev->lock); 20021da177e4SLinus Torvalds /* update all temporary addresses in the list */ 2003372e6c8fSstephen hemminger list_for_each_entry(ift, &in6_dev->tempaddr_list, tmp_list) { 20041da177e4SLinus Torvalds /* 20051da177e4SLinus Torvalds * When adjusting the lifetimes of an existing 20061da177e4SLinus Torvalds * temporary address, only lower the lifetimes. 20071da177e4SLinus Torvalds * Implementations must not increase the 20081da177e4SLinus Torvalds * lifetimes of an existing temporary address 20091da177e4SLinus Torvalds * when processing a Prefix Information Option. 20101da177e4SLinus Torvalds */ 2011c6fbfac2SBenoit Boissinot if (ifp != ift->ifpub) 2012c6fbfac2SBenoit Boissinot continue; 2013c6fbfac2SBenoit Boissinot 20141da177e4SLinus Torvalds spin_lock(&ift->lock); 20151da177e4SLinus Torvalds flags = ift->flags; 20161da177e4SLinus Torvalds if (ift->valid_lft > valid_lft && 20171da177e4SLinus Torvalds ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ) 20181da177e4SLinus Torvalds ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ; 20191da177e4SLinus Torvalds if (ift->prefered_lft > prefered_lft && 20201da177e4SLinus Torvalds ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ) 20211da177e4SLinus Torvalds ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ; 20221da177e4SLinus Torvalds spin_unlock(&ift->lock); 20231da177e4SLinus Torvalds if (!(flags&IFA_F_TENTATIVE)) 20241da177e4SLinus Torvalds ipv6_ifa_notify(0, ift); 20251da177e4SLinus Torvalds } 20261da177e4SLinus Torvalds 2027aed65501SGlenn Wurster if ((create || list_empty(&in6_dev->tempaddr_list)) && in6_dev->cnf.use_tempaddr > 0) { 20281da177e4SLinus Torvalds /* 20291da177e4SLinus Torvalds * When a new public address is created as described in [ADDRCONF], 2030aed65501SGlenn Wurster * also create a new temporary address. Also create a temporary 2031aed65501SGlenn Wurster * address if it's enabled but no temporary address currently exists. 20321da177e4SLinus Torvalds */ 20331da177e4SLinus Torvalds read_unlock_bh(&in6_dev->lock); 20341da177e4SLinus Torvalds ipv6_create_tempaddr(ifp, NULL); 20351da177e4SLinus Torvalds } else { 20361da177e4SLinus Torvalds read_unlock_bh(&in6_dev->lock); 20371da177e4SLinus Torvalds } 20381da177e4SLinus Torvalds #endif 20391da177e4SLinus Torvalds in6_ifa_put(ifp); 20401da177e4SLinus Torvalds addrconf_verify(0); 20411da177e4SLinus Torvalds } 20421da177e4SLinus Torvalds } 20431da177e4SLinus Torvalds inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo); 20441da177e4SLinus Torvalds in6_dev_put(in6_dev); 20451da177e4SLinus Torvalds } 20461da177e4SLinus Torvalds 20471da177e4SLinus Torvalds /* 20481da177e4SLinus Torvalds * Set destination address. 20491da177e4SLinus Torvalds * Special case for SIT interfaces where we create a new "virtual" 20501da177e4SLinus Torvalds * device. 20511da177e4SLinus Torvalds */ 2052af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg) 20531da177e4SLinus Torvalds { 20541da177e4SLinus Torvalds struct in6_ifreq ireq; 20551da177e4SLinus Torvalds struct net_device *dev; 20561da177e4SLinus Torvalds int err = -EINVAL; 20571da177e4SLinus Torvalds 20581da177e4SLinus Torvalds rtnl_lock(); 20591da177e4SLinus Torvalds 20601da177e4SLinus Torvalds err = -EFAULT; 20611da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 20621da177e4SLinus Torvalds goto err_exit; 20631da177e4SLinus Torvalds 2064af284937SDaniel Lezcano dev = __dev_get_by_index(net, ireq.ifr6_ifindex); 20651da177e4SLinus Torvalds 20661da177e4SLinus Torvalds err = -ENODEV; 20671da177e4SLinus Torvalds if (dev == NULL) 20681da177e4SLinus Torvalds goto err_exit; 20691da177e4SLinus Torvalds 20700be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 20711da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT) { 20725bc3eb7eSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 20731da177e4SLinus Torvalds struct ifreq ifr; 20741da177e4SLinus Torvalds struct ip_tunnel_parm p; 20751da177e4SLinus Torvalds 20761da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 20771da177e4SLinus Torvalds if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4)) 20781da177e4SLinus Torvalds goto err_exit; 20791da177e4SLinus Torvalds 20801da177e4SLinus Torvalds memset(&p, 0, sizeof(p)); 20811da177e4SLinus Torvalds p.iph.daddr = ireq.ifr6_addr.s6_addr32[3]; 20821da177e4SLinus Torvalds p.iph.saddr = 0; 20831da177e4SLinus Torvalds p.iph.version = 4; 20841da177e4SLinus Torvalds p.iph.ihl = 5; 20851da177e4SLinus Torvalds p.iph.protocol = IPPROTO_IPV6; 20861da177e4SLinus Torvalds p.iph.ttl = 64; 2087d20b3109SStephen Hemminger ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 20881da177e4SLinus Torvalds 20895bc3eb7eSStephen Hemminger if (ops->ndo_do_ioctl) { 20905bc3eb7eSStephen Hemminger mm_segment_t oldfs = get_fs(); 20915bc3eb7eSStephen Hemminger 20925bc3eb7eSStephen Hemminger set_fs(KERNEL_DS); 20935bc3eb7eSStephen Hemminger err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); 20941da177e4SLinus Torvalds set_fs(oldfs); 20955bc3eb7eSStephen Hemminger } else 20965bc3eb7eSStephen Hemminger err = -EOPNOTSUPP; 20971da177e4SLinus Torvalds 20981da177e4SLinus Torvalds if (err == 0) { 20991da177e4SLinus Torvalds err = -ENOBUFS; 2100af284937SDaniel Lezcano dev = __dev_get_by_name(net, p.name); 2101af284937SDaniel Lezcano if (!dev) 21021da177e4SLinus Torvalds goto err_exit; 21031da177e4SLinus Torvalds err = dev_open(dev); 21041da177e4SLinus Torvalds } 21051da177e4SLinus Torvalds } 21060be669bbSJoerg Roedel #endif 21071da177e4SLinus Torvalds 21081da177e4SLinus Torvalds err_exit: 21091da177e4SLinus Torvalds rtnl_unlock(); 21101da177e4SLinus Torvalds return err; 21111da177e4SLinus Torvalds } 21121da177e4SLinus Torvalds 21131da177e4SLinus Torvalds /* 21141da177e4SLinus Torvalds * Manual configuration of address on an interface 21151da177e4SLinus Torvalds */ 2116af284937SDaniel Lezcano static int inet6_addr_add(struct net *net, int ifindex, struct in6_addr *pfx, 211724ef0da7SThomas Graf unsigned int plen, __u8 ifa_flags, __u32 prefered_lft, 2118af284937SDaniel Lezcano __u32 valid_lft) 21191da177e4SLinus Torvalds { 21201da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 21211da177e4SLinus Torvalds struct inet6_dev *idev; 21221da177e4SLinus Torvalds struct net_device *dev; 21231da177e4SLinus Torvalds int scope; 21246f704992SYOSHIFUJI Hideaki u32 flags; 21256f704992SYOSHIFUJI Hideaki clock_t expires; 21264bed72e4SYOSHIFUJI Hideaki unsigned long timeout; 21271da177e4SLinus Torvalds 21281da177e4SLinus Torvalds ASSERT_RTNL(); 21291da177e4SLinus Torvalds 213024ef0da7SThomas Graf if (plen > 128) 213124ef0da7SThomas Graf return -EINVAL; 213224ef0da7SThomas Graf 21330778769dSNoriaki TAKAMIYA /* check the lifetime */ 21340778769dSNoriaki TAKAMIYA if (!valid_lft || prefered_lft > valid_lft) 21350778769dSNoriaki TAKAMIYA return -EINVAL; 21360778769dSNoriaki TAKAMIYA 2137af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2138af284937SDaniel Lezcano if (!dev) 21391da177e4SLinus Torvalds return -ENODEV; 21401da177e4SLinus Torvalds 214164e724f6SBrian Haley idev = addrconf_add_dev(dev); 214264e724f6SBrian Haley if (IS_ERR(idev)) 214364e724f6SBrian Haley return PTR_ERR(idev); 21441da177e4SLinus Torvalds 21451da177e4SLinus Torvalds scope = ipv6_addr_scope(pfx); 21461da177e4SLinus Torvalds 21474bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(valid_lft, HZ); 21484bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 21494bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 21504bed72e4SYOSHIFUJI Hideaki valid_lft = timeout; 21516f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 21524bed72e4SYOSHIFUJI Hideaki } else { 21534bed72e4SYOSHIFUJI Hideaki expires = 0; 21544bed72e4SYOSHIFUJI Hideaki flags = 0; 21554bed72e4SYOSHIFUJI Hideaki ifa_flags |= IFA_F_PERMANENT; 21566f704992SYOSHIFUJI Hideaki } 21570778769dSNoriaki TAKAMIYA 21584bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(prefered_lft, HZ); 21594bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 21604bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 21610778769dSNoriaki TAKAMIYA ifa_flags |= IFA_F_DEPRECATED; 21624bed72e4SYOSHIFUJI Hideaki prefered_lft = timeout; 21634bed72e4SYOSHIFUJI Hideaki } 21640778769dSNoriaki TAKAMIYA 21650778769dSNoriaki TAKAMIYA ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags); 21660778769dSNoriaki TAKAMIYA 21671da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 216806aebfb7SHerbert Xu spin_lock_bh(&ifp->lock); 21690778769dSNoriaki TAKAMIYA ifp->valid_lft = valid_lft; 21700778769dSNoriaki TAKAMIYA ifp->prefered_lft = prefered_lft; 21710778769dSNoriaki TAKAMIYA ifp->tstamp = jiffies; 217206aebfb7SHerbert Xu spin_unlock_bh(&ifp->lock); 21730778769dSNoriaki TAKAMIYA 217446d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 21756f704992SYOSHIFUJI Hideaki expires, flags); 217695c385b4SNeil Horman /* 217795c385b4SNeil Horman * Note that section 3.1 of RFC 4429 indicates 217895c385b4SNeil Horman * that the Optimistic flag should not be set for 217995c385b4SNeil Horman * manually configured addresses 218095c385b4SNeil Horman */ 21811da177e4SLinus Torvalds addrconf_dad_start(ifp, 0); 21821da177e4SLinus Torvalds in6_ifa_put(ifp); 21830778769dSNoriaki TAKAMIYA addrconf_verify(0); 21841da177e4SLinus Torvalds return 0; 21851da177e4SLinus Torvalds } 21861da177e4SLinus Torvalds 21871da177e4SLinus Torvalds return PTR_ERR(ifp); 21881da177e4SLinus Torvalds } 21891da177e4SLinus Torvalds 2190af284937SDaniel Lezcano static int inet6_addr_del(struct net *net, int ifindex, struct in6_addr *pfx, 219124ef0da7SThomas Graf unsigned int plen) 21921da177e4SLinus Torvalds { 21931da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 21941da177e4SLinus Torvalds struct inet6_dev *idev; 21951da177e4SLinus Torvalds struct net_device *dev; 21961da177e4SLinus Torvalds 219724ef0da7SThomas Graf if (plen > 128) 219824ef0da7SThomas Graf return -EINVAL; 219924ef0da7SThomas Graf 2200af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2201af284937SDaniel Lezcano if (!dev) 22021da177e4SLinus Torvalds return -ENODEV; 22031da177e4SLinus Torvalds 22041da177e4SLinus Torvalds if ((idev = __in6_dev_get(dev)) == NULL) 22051da177e4SLinus Torvalds return -ENXIO; 22061da177e4SLinus Torvalds 22071da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2208502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 22091da177e4SLinus Torvalds if (ifp->prefix_len == plen && 22101da177e4SLinus Torvalds ipv6_addr_equal(pfx, &ifp->addr)) { 22111da177e4SLinus Torvalds in6_ifa_hold(ifp); 22121da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 22131da177e4SLinus Torvalds 22141da177e4SLinus Torvalds ipv6_del_addr(ifp); 22151da177e4SLinus Torvalds 22161da177e4SLinus Torvalds /* If the last address is deleted administratively, 22171da177e4SLinus Torvalds disable IPv6 on this interface. 22181da177e4SLinus Torvalds */ 2219502a2ffdSstephen hemminger if (list_empty(&idev->addr_list)) 22201da177e4SLinus Torvalds addrconf_ifdown(idev->dev, 1); 22211da177e4SLinus Torvalds return 0; 22221da177e4SLinus Torvalds } 22231da177e4SLinus Torvalds } 22241da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 22251da177e4SLinus Torvalds return -EADDRNOTAVAIL; 22261da177e4SLinus Torvalds } 22271da177e4SLinus Torvalds 22281da177e4SLinus Torvalds 2229af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg) 22301da177e4SLinus Torvalds { 22311da177e4SLinus Torvalds struct in6_ifreq ireq; 22321da177e4SLinus Torvalds int err; 22331da177e4SLinus Torvalds 22341da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 22351da177e4SLinus Torvalds return -EPERM; 22361da177e4SLinus Torvalds 22371da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 22381da177e4SLinus Torvalds return -EFAULT; 22391da177e4SLinus Torvalds 22401da177e4SLinus Torvalds rtnl_lock(); 2241af284937SDaniel Lezcano err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, 2242af284937SDaniel Lezcano ireq.ifr6_prefixlen, IFA_F_PERMANENT, 2243af284937SDaniel Lezcano INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 22441da177e4SLinus Torvalds rtnl_unlock(); 22451da177e4SLinus Torvalds return err; 22461da177e4SLinus Torvalds } 22471da177e4SLinus Torvalds 2248af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg) 22491da177e4SLinus Torvalds { 22501da177e4SLinus Torvalds struct in6_ifreq ireq; 22511da177e4SLinus Torvalds int err; 22521da177e4SLinus Torvalds 22531da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 22541da177e4SLinus Torvalds return -EPERM; 22551da177e4SLinus Torvalds 22561da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 22571da177e4SLinus Torvalds return -EFAULT; 22581da177e4SLinus Torvalds 22591da177e4SLinus Torvalds rtnl_lock(); 2260af284937SDaniel Lezcano err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, 2261af284937SDaniel Lezcano ireq.ifr6_prefixlen); 22621da177e4SLinus Torvalds rtnl_unlock(); 22631da177e4SLinus Torvalds return err; 22641da177e4SLinus Torvalds } 22651da177e4SLinus Torvalds 2266b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr, 2267b5f348e5SIlpo Järvinen int plen, int scope) 2268b5f348e5SIlpo Järvinen { 2269b5f348e5SIlpo Järvinen struct inet6_ifaddr *ifp; 2270b5f348e5SIlpo Järvinen 2271b5f348e5SIlpo Järvinen ifp = ipv6_add_addr(idev, addr, plen, scope, IFA_F_PERMANENT); 2272b5f348e5SIlpo Järvinen if (!IS_ERR(ifp)) { 2273b5f348e5SIlpo Järvinen spin_lock_bh(&ifp->lock); 2274b5f348e5SIlpo Järvinen ifp->flags &= ~IFA_F_TENTATIVE; 2275b5f348e5SIlpo Järvinen spin_unlock_bh(&ifp->lock); 2276b5f348e5SIlpo Järvinen ipv6_ifa_notify(RTM_NEWADDR, ifp); 2277b5f348e5SIlpo Järvinen in6_ifa_put(ifp); 2278b5f348e5SIlpo Järvinen } 2279b5f348e5SIlpo Järvinen } 2280b5f348e5SIlpo Järvinen 22810be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 22821da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev) 22831da177e4SLinus Torvalds { 22841da177e4SLinus Torvalds struct in6_addr addr; 22851da177e4SLinus Torvalds struct net_device *dev; 2286c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 22871da177e4SLinus Torvalds int scope; 22881da177e4SLinus Torvalds 22891da177e4SLinus Torvalds ASSERT_RTNL(); 22901da177e4SLinus Torvalds 22911da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr)); 22921da177e4SLinus Torvalds memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4); 22931da177e4SLinus Torvalds 22941da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) { 22951da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xfe800000); 22961da177e4SLinus Torvalds scope = IFA_LINK; 22971da177e4SLinus Torvalds } else { 22981da177e4SLinus Torvalds scope = IPV6_ADDR_COMPATv4; 22991da177e4SLinus Torvalds } 23001da177e4SLinus Torvalds 23011da177e4SLinus Torvalds if (addr.s6_addr32[3]) { 2302b5f348e5SIlpo Järvinen add_addr(idev, &addr, 128, scope); 23031da177e4SLinus Torvalds return; 23041da177e4SLinus Torvalds } 23051da177e4SLinus Torvalds 23066fda7350SBenjamin Thery for_each_netdev(net, dev) { 2307e5ed6399SHerbert Xu struct in_device * in_dev = __in_dev_get_rtnl(dev); 23081da177e4SLinus Torvalds if (in_dev && (dev->flags & IFF_UP)) { 23091da177e4SLinus Torvalds struct in_ifaddr * ifa; 23101da177e4SLinus Torvalds 23111da177e4SLinus Torvalds int flag = scope; 23121da177e4SLinus Torvalds 23131da177e4SLinus Torvalds for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { 23141da177e4SLinus Torvalds int plen; 23151da177e4SLinus Torvalds 23161da177e4SLinus Torvalds addr.s6_addr32[3] = ifa->ifa_local; 23171da177e4SLinus Torvalds 23181da177e4SLinus Torvalds if (ifa->ifa_scope == RT_SCOPE_LINK) 23191da177e4SLinus Torvalds continue; 23201da177e4SLinus Torvalds if (ifa->ifa_scope >= RT_SCOPE_HOST) { 23211da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) 23221da177e4SLinus Torvalds continue; 23231da177e4SLinus Torvalds flag |= IFA_HOST; 23241da177e4SLinus Torvalds } 23251da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) 23261da177e4SLinus Torvalds plen = 64; 23271da177e4SLinus Torvalds else 23281da177e4SLinus Torvalds plen = 96; 23291da177e4SLinus Torvalds 2330b5f348e5SIlpo Järvinen add_addr(idev, &addr, plen, flag); 23311da177e4SLinus Torvalds } 23321da177e4SLinus Torvalds } 23331da177e4SLinus Torvalds } 23341da177e4SLinus Torvalds } 23350be669bbSJoerg Roedel #endif 23361da177e4SLinus Torvalds 23371da177e4SLinus Torvalds static void init_loopback(struct net_device *dev) 23381da177e4SLinus Torvalds { 23391da177e4SLinus Torvalds struct inet6_dev *idev; 23401da177e4SLinus Torvalds 23411da177e4SLinus Torvalds /* ::1 */ 23421da177e4SLinus Torvalds 23431da177e4SLinus Torvalds ASSERT_RTNL(); 23441da177e4SLinus Torvalds 23451da177e4SLinus Torvalds if ((idev = ipv6_find_idev(dev)) == NULL) { 23461da177e4SLinus Torvalds printk(KERN_DEBUG "init loopback: add_dev failed\n"); 23471da177e4SLinus Torvalds return; 23481da177e4SLinus Torvalds } 23491da177e4SLinus Torvalds 2350b5f348e5SIlpo Järvinen add_addr(idev, &in6addr_loopback, 128, IFA_HOST); 23511da177e4SLinus Torvalds } 23521da177e4SLinus Torvalds 23531da177e4SLinus Torvalds static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr) 23541da177e4SLinus Torvalds { 23551da177e4SLinus Torvalds struct inet6_ifaddr * ifp; 235695c385b4SNeil Horman u32 addr_flags = IFA_F_PERMANENT; 23571da177e4SLinus Torvalds 235895c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 235995c385b4SNeil Horman if (idev->cnf.optimistic_dad && 2360702beb87SDavid Miller !dev_net(idev->dev)->ipv6.devconf_all->forwarding) 236195c385b4SNeil Horman addr_flags |= IFA_F_OPTIMISTIC; 236295c385b4SNeil Horman #endif 236395c385b4SNeil Horman 236495c385b4SNeil Horman 236595c385b4SNeil Horman ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags); 23661da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 236746d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0); 23681da177e4SLinus Torvalds addrconf_dad_start(ifp, 0); 23691da177e4SLinus Torvalds in6_ifa_put(ifp); 23701da177e4SLinus Torvalds } 23711da177e4SLinus Torvalds } 23721da177e4SLinus Torvalds 23731da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev) 23741da177e4SLinus Torvalds { 23751da177e4SLinus Torvalds struct in6_addr addr; 23761da177e4SLinus Torvalds struct inet6_dev * idev; 23771da177e4SLinus Torvalds 23781da177e4SLinus Torvalds ASSERT_RTNL(); 23791da177e4SLinus Torvalds 238074235a25SHerbert Xu if ((dev->type != ARPHRD_ETHER) && 238174235a25SHerbert Xu (dev->type != ARPHRD_FDDI) && 238274235a25SHerbert Xu (dev->type != ARPHRD_IEEE802_TR) && 238374235a25SHerbert Xu (dev->type != ARPHRD_ARCNET) && 238474235a25SHerbert Xu (dev->type != ARPHRD_INFINIBAND)) { 238574235a25SHerbert Xu /* Alas, we support only Ethernet autoconfiguration. */ 238674235a25SHerbert Xu return; 238774235a25SHerbert Xu } 238874235a25SHerbert Xu 23891da177e4SLinus Torvalds idev = addrconf_add_dev(dev); 239064e724f6SBrian Haley if (IS_ERR(idev)) 23911da177e4SLinus Torvalds return; 23921da177e4SLinus Torvalds 23931da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr)); 23941da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xFE800000); 23951da177e4SLinus Torvalds 23961da177e4SLinus Torvalds if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0) 23971da177e4SLinus Torvalds addrconf_add_linklocal(idev, &addr); 23981da177e4SLinus Torvalds } 23991da177e4SLinus Torvalds 24000be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 24011da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev) 24021da177e4SLinus Torvalds { 24031da177e4SLinus Torvalds struct inet6_dev *idev; 24041da177e4SLinus Torvalds 24051da177e4SLinus Torvalds ASSERT_RTNL(); 24061da177e4SLinus Torvalds 24071da177e4SLinus Torvalds /* 24081da177e4SLinus Torvalds * Configure the tunnel with one of our IPv4 24091da177e4SLinus Torvalds * addresses... we should configure all of 24101da177e4SLinus Torvalds * our v4 addrs in the tunnel 24111da177e4SLinus Torvalds */ 24121da177e4SLinus Torvalds 24131da177e4SLinus Torvalds if ((idev = ipv6_find_idev(dev)) == NULL) { 24141da177e4SLinus Torvalds printk(KERN_DEBUG "init sit: add_dev failed\n"); 24151da177e4SLinus Torvalds return; 24161da177e4SLinus Torvalds } 24171da177e4SLinus Torvalds 2418c7dc89c0SFred L. Templin if (dev->priv_flags & IFF_ISATAP) { 2419c7dc89c0SFred L. Templin struct in6_addr addr; 2420c7dc89c0SFred L. Templin 2421c7dc89c0SFred L. Templin ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 2422c7dc89c0SFred L. Templin addrconf_prefix_route(&addr, 64, dev, 0, 0); 2423c7dc89c0SFred L. Templin if (!ipv6_generate_eui64(addr.s6_addr + 8, dev)) 2424c7dc89c0SFred L. Templin addrconf_add_linklocal(idev, &addr); 2425c7dc89c0SFred L. Templin return; 2426c7dc89c0SFred L. Templin } 2427c7dc89c0SFred L. Templin 24281da177e4SLinus Torvalds sit_add_v4_addrs(idev); 24291da177e4SLinus Torvalds 24301da177e4SLinus Torvalds if (dev->flags&IFF_POINTOPOINT) { 24311da177e4SLinus Torvalds addrconf_add_mroute(dev); 24321da177e4SLinus Torvalds addrconf_add_lroute(dev); 24331da177e4SLinus Torvalds } else 24341da177e4SLinus Torvalds sit_route_add(dev); 24351da177e4SLinus Torvalds } 24360be669bbSJoerg Roedel #endif 24371da177e4SLinus Torvalds 24381da177e4SLinus Torvalds static inline int 24391da177e4SLinus Torvalds ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev) 24401da177e4SLinus Torvalds { 24411da177e4SLinus Torvalds struct in6_addr lladdr; 24421da177e4SLinus Torvalds 244395c385b4SNeil Horman if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) { 24441da177e4SLinus Torvalds addrconf_add_linklocal(idev, &lladdr); 24451da177e4SLinus Torvalds return 0; 24461da177e4SLinus Torvalds } 24471da177e4SLinus Torvalds return -1; 24481da177e4SLinus Torvalds } 24491da177e4SLinus Torvalds 24501da177e4SLinus Torvalds static void ip6_tnl_add_linklocal(struct inet6_dev *idev) 24511da177e4SLinus Torvalds { 24521da177e4SLinus Torvalds struct net_device *link_dev; 2453c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 24541da177e4SLinus Torvalds 24551da177e4SLinus Torvalds /* first try to inherit the link-local address from the link device */ 24561da177e4SLinus Torvalds if (idev->dev->iflink && 24576fda7350SBenjamin Thery (link_dev = __dev_get_by_index(net, idev->dev->iflink))) { 24581da177e4SLinus Torvalds if (!ipv6_inherit_linklocal(idev, link_dev)) 24591da177e4SLinus Torvalds return; 24601da177e4SLinus Torvalds } 24611da177e4SLinus Torvalds /* then try to inherit it from any device */ 24626fda7350SBenjamin Thery for_each_netdev(net, link_dev) { 24631da177e4SLinus Torvalds if (!ipv6_inherit_linklocal(idev, link_dev)) 24641da177e4SLinus Torvalds return; 24651da177e4SLinus Torvalds } 24661da177e4SLinus Torvalds printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n"); 24671da177e4SLinus Torvalds } 24681da177e4SLinus Torvalds 24691da177e4SLinus Torvalds /* 24701da177e4SLinus Torvalds * Autoconfigure tunnel with a link-local address so routing protocols, 24711da177e4SLinus Torvalds * DHCPv6, MLD etc. can be run over the virtual link 24721da177e4SLinus Torvalds */ 24731da177e4SLinus Torvalds 24741da177e4SLinus Torvalds static void addrconf_ip6_tnl_config(struct net_device *dev) 24751da177e4SLinus Torvalds { 24761da177e4SLinus Torvalds struct inet6_dev *idev; 24771da177e4SLinus Torvalds 24781da177e4SLinus Torvalds ASSERT_RTNL(); 24791da177e4SLinus Torvalds 2480e21e8467SStephen Hemminger idev = addrconf_add_dev(dev); 248164e724f6SBrian Haley if (IS_ERR(idev)) { 24821da177e4SLinus Torvalds printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n"); 24831da177e4SLinus Torvalds return; 24841da177e4SLinus Torvalds } 24851da177e4SLinus Torvalds ip6_tnl_add_linklocal(idev); 24861da177e4SLinus Torvalds } 24871da177e4SLinus Torvalds 24881da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event, 24891da177e4SLinus Torvalds void * data) 24901da177e4SLinus Torvalds { 24911da177e4SLinus Torvalds struct net_device *dev = (struct net_device *) data; 249274235a25SHerbert Xu struct inet6_dev *idev = __in6_dev_get(dev); 2493c5e33bddSYOSHIFUJI Hideaki int run_pending = 0; 2494b217d616SHerbert Xu int err; 24951da177e4SLinus Torvalds 24961da177e4SLinus Torvalds switch (event) { 249745ba9dd2SYOSHIFUJI Hideaki case NETDEV_REGISTER: 249874235a25SHerbert Xu if (!idev && dev->mtu >= IPV6_MIN_MTU) { 249945ba9dd2SYOSHIFUJI Hideaki idev = ipv6_add_dev(dev); 250045ba9dd2SYOSHIFUJI Hideaki if (!idev) 2501b217d616SHerbert Xu return notifier_from_errno(-ENOMEM); 250245ba9dd2SYOSHIFUJI Hideaki } 250345ba9dd2SYOSHIFUJI Hideaki break; 2504bcdd553fSStephen Hemminger 25051da177e4SLinus Torvalds case NETDEV_UP: 25063c21edbdSYOSHIFUJI Hideaki case NETDEV_CHANGE: 2507c2edacf8SJay Vosburgh if (dev->flags & IFF_SLAVE) 2508c2edacf8SJay Vosburgh break; 2509c2edacf8SJay Vosburgh 25103c21edbdSYOSHIFUJI Hideaki if (event == NETDEV_UP) { 2511f24e3d65SMitsuru Chinen if (!addrconf_qdisc_ok(dev)) { 25123c21edbdSYOSHIFUJI Hideaki /* device is not ready yet. */ 25133c21edbdSYOSHIFUJI Hideaki printk(KERN_INFO 25143c21edbdSYOSHIFUJI Hideaki "ADDRCONF(NETDEV_UP): %s: " 25153c21edbdSYOSHIFUJI Hideaki "link is not ready\n", 25163c21edbdSYOSHIFUJI Hideaki dev->name); 25173c21edbdSYOSHIFUJI Hideaki break; 25183c21edbdSYOSHIFUJI Hideaki } 251999081049SKristian Slavov 2520d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU) 2521d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev); 2522d31c7b8fSEvgeniy Polyakov 2523e3ec6cfcSBenjamin Thery if (idev) { 252499081049SKristian Slavov idev->if_flags |= IF_READY; 2525e3ec6cfcSBenjamin Thery run_pending = 1; 2526e3ec6cfcSBenjamin Thery } 25273c21edbdSYOSHIFUJI Hideaki } else { 2528f24e3d65SMitsuru Chinen if (!addrconf_qdisc_ok(dev)) { 25293c21edbdSYOSHIFUJI Hideaki /* device is still not ready. */ 25303c21edbdSYOSHIFUJI Hideaki break; 25313c21edbdSYOSHIFUJI Hideaki } 25323c21edbdSYOSHIFUJI Hideaki 25333c21edbdSYOSHIFUJI Hideaki if (idev) { 2534bcdd553fSStephen Hemminger if (idev->if_flags & IF_READY) 25353c21edbdSYOSHIFUJI Hideaki /* device is already configured. */ 25363c21edbdSYOSHIFUJI Hideaki break; 25373c21edbdSYOSHIFUJI Hideaki idev->if_flags |= IF_READY; 25383c21edbdSYOSHIFUJI Hideaki } 25393c21edbdSYOSHIFUJI Hideaki 25403c21edbdSYOSHIFUJI Hideaki printk(KERN_INFO 25413c21edbdSYOSHIFUJI Hideaki "ADDRCONF(NETDEV_CHANGE): %s: " 25423c21edbdSYOSHIFUJI Hideaki "link becomes ready\n", 25433c21edbdSYOSHIFUJI Hideaki dev->name); 25443c21edbdSYOSHIFUJI Hideaki 2545c5e33bddSYOSHIFUJI Hideaki run_pending = 1; 25463c21edbdSYOSHIFUJI Hideaki } 25473c21edbdSYOSHIFUJI Hideaki 25481da177e4SLinus Torvalds switch (dev->type) { 25490be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE) 25501da177e4SLinus Torvalds case ARPHRD_SIT: 25511da177e4SLinus Torvalds addrconf_sit_config(dev); 25521da177e4SLinus Torvalds break; 25530be669bbSJoerg Roedel #endif 25541da177e4SLinus Torvalds case ARPHRD_TUNNEL6: 25551da177e4SLinus Torvalds addrconf_ip6_tnl_config(dev); 25561da177e4SLinus Torvalds break; 25571da177e4SLinus Torvalds case ARPHRD_LOOPBACK: 25581da177e4SLinus Torvalds init_loopback(dev); 25591da177e4SLinus Torvalds break; 25601da177e4SLinus Torvalds 25611da177e4SLinus Torvalds default: 25621da177e4SLinus Torvalds addrconf_dev_config(dev); 25631da177e4SLinus Torvalds break; 25643ff50b79SStephen Hemminger } 2565bcdd553fSStephen Hemminger 25661da177e4SLinus Torvalds if (idev) { 2567c5e33bddSYOSHIFUJI Hideaki if (run_pending) 2568c5e33bddSYOSHIFUJI Hideaki addrconf_dad_run(idev); 2569c5e33bddSYOSHIFUJI Hideaki 2570bcdd553fSStephen Hemminger /* 2571bcdd553fSStephen Hemminger * If the MTU changed during the interface down, 2572bcdd553fSStephen Hemminger * when the interface up, the changed MTU must be 2573bcdd553fSStephen Hemminger * reflected in the idev as well as routers. 25741da177e4SLinus Torvalds */ 2575bcdd553fSStephen Hemminger if (idev->cnf.mtu6 != dev->mtu && 2576bcdd553fSStephen Hemminger dev->mtu >= IPV6_MIN_MTU) { 25771da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 25781da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 25791da177e4SLinus Torvalds } 25801da177e4SLinus Torvalds idev->tstamp = jiffies; 25811da177e4SLinus Torvalds inet6_ifinfo_notify(RTM_NEWLINK, idev); 2582bcdd553fSStephen Hemminger 2583bcdd553fSStephen Hemminger /* 2584bcdd553fSStephen Hemminger * If the changed mtu during down is lower than 2585bcdd553fSStephen Hemminger * IPV6_MIN_MTU stop IPv6 on this interface. 25861da177e4SLinus Torvalds */ 25871da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 2588bcdd553fSStephen Hemminger addrconf_ifdown(dev, 1); 25891da177e4SLinus Torvalds } 25901da177e4SLinus Torvalds break; 25911da177e4SLinus Torvalds 25921da177e4SLinus Torvalds case NETDEV_CHANGEMTU: 25931da177e4SLinus Torvalds if (idev && dev->mtu >= IPV6_MIN_MTU) { 25941da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 25951da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 25961da177e4SLinus Torvalds break; 25971da177e4SLinus Torvalds } 25981da177e4SLinus Torvalds 2599d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU) { 2600d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev); 2601d31c7b8fSEvgeniy Polyakov if (idev) 2602d31c7b8fSEvgeniy Polyakov break; 2603d31c7b8fSEvgeniy Polyakov } 2604d31c7b8fSEvgeniy Polyakov 2605bcdd553fSStephen Hemminger /* 2606bcdd553fSStephen Hemminger * MTU falled under IPV6_MIN_MTU. 2607bcdd553fSStephen Hemminger * Stop IPv6 on this interface. 2608bcdd553fSStephen Hemminger */ 26091da177e4SLinus Torvalds 26101da177e4SLinus Torvalds case NETDEV_DOWN: 26111da177e4SLinus Torvalds case NETDEV_UNREGISTER: 26121da177e4SLinus Torvalds /* 26131da177e4SLinus Torvalds * Remove all addresses from this interface. 26141da177e4SLinus Torvalds */ 26151da177e4SLinus Torvalds addrconf_ifdown(dev, event != NETDEV_DOWN); 26161da177e4SLinus Torvalds break; 26173c21edbdSYOSHIFUJI Hideaki 26181da177e4SLinus Torvalds case NETDEV_CHANGENAME: 26191da177e4SLinus Torvalds if (idev) { 26205632c515SStephen Hemminger snmp6_unregister_dev(idev); 2621408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 2622f52295a9SPavel Emelyanov addrconf_sysctl_register(idev); 2623b217d616SHerbert Xu err = snmp6_register_dev(idev); 2624b217d616SHerbert Xu if (err) 2625b217d616SHerbert Xu return notifier_from_errno(err); 26265632c515SStephen Hemminger } 26271da177e4SLinus Torvalds break; 2628bcdd553fSStephen Hemminger 262993d9b7d7SJiri Pirko case NETDEV_PRE_TYPE_CHANGE: 263093d9b7d7SJiri Pirko case NETDEV_POST_TYPE_CHANGE: 263193d9b7d7SJiri Pirko addrconf_type_change(dev, event); 263275c78500SMoni Shoua break; 26333ff50b79SStephen Hemminger } 26341da177e4SLinus Torvalds 26351da177e4SLinus Torvalds return NOTIFY_OK; 26361da177e4SLinus Torvalds } 26371da177e4SLinus Torvalds 26381da177e4SLinus Torvalds /* 26391da177e4SLinus Torvalds * addrconf module should be notified of a device going up 26401da177e4SLinus Torvalds */ 26411da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = { 26421da177e4SLinus Torvalds .notifier_call = addrconf_notify, 26431da177e4SLinus Torvalds }; 26441da177e4SLinus Torvalds 264593d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event) 264675c78500SMoni Shoua { 264775c78500SMoni Shoua struct inet6_dev *idev; 264875c78500SMoni Shoua ASSERT_RTNL(); 264975c78500SMoni Shoua 265075c78500SMoni Shoua idev = __in6_dev_get(dev); 265175c78500SMoni Shoua 265293d9b7d7SJiri Pirko if (event == NETDEV_POST_TYPE_CHANGE) 265375c78500SMoni Shoua ipv6_mc_remap(idev); 265493d9b7d7SJiri Pirko else if (event == NETDEV_PRE_TYPE_CHANGE) 265575c78500SMoni Shoua ipv6_mc_unmap(idev); 265675c78500SMoni Shoua } 265775c78500SMoni Shoua 26581da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how) 26591da177e4SLinus Torvalds { 2660c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 2661502a2ffdSstephen hemminger struct inet6_dev *idev; 2662502a2ffdSstephen hemminger struct inet6_ifaddr *ifa; 2663502a2ffdSstephen hemminger LIST_HEAD(keep_list); 26644c5ff6a6SHerbert Xu int state; 26651da177e4SLinus Torvalds 26661da177e4SLinus Torvalds ASSERT_RTNL(); 26671da177e4SLinus Torvalds 2668f3db4851SDaniel Lezcano rt6_ifdown(net, dev); 26691da177e4SLinus Torvalds neigh_ifdown(&nd_tbl, dev); 26701da177e4SLinus Torvalds 26711da177e4SLinus Torvalds idev = __in6_dev_get(dev); 26721da177e4SLinus Torvalds if (idev == NULL) 26731da177e4SLinus Torvalds return -ENODEV; 26741da177e4SLinus Torvalds 2675bcdd553fSStephen Hemminger /* 2676bcdd553fSStephen Hemminger * Step 1: remove reference to ipv6 device from parent device. 2677bcdd553fSStephen Hemminger * Do not dev_put! 26781da177e4SLinus Torvalds */ 2679439e2385SDenis V. Lunev if (how) { 26801da177e4SLinus Torvalds idev->dead = 1; 26818814c4b5SYOSHIFUJI Hideaki 26828814c4b5SYOSHIFUJI Hideaki /* protected by rtnl_lock */ 26838814c4b5SYOSHIFUJI Hideaki rcu_assign_pointer(dev->ip6_ptr, NULL); 26841da177e4SLinus Torvalds 26851da177e4SLinus Torvalds /* Step 1.5: remove snmp6 entry */ 26861da177e4SLinus Torvalds snmp6_unregister_dev(idev); 26871da177e4SLinus Torvalds 26881da177e4SLinus Torvalds } 26891da177e4SLinus Torvalds 26901da177e4SLinus Torvalds write_lock_bh(&idev->lock); 26911da177e4SLinus Torvalds 2692bcdd553fSStephen Hemminger /* Step 2: clear flags for stateless addrconf */ 2693439e2385SDenis V. Lunev if (!how) 26943c21edbdSYOSHIFUJI Hideaki idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); 26951da177e4SLinus Torvalds 26961da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 2697439e2385SDenis V. Lunev if (how && del_timer(&idev->regen_timer)) 26981da177e4SLinus Torvalds in6_dev_put(idev); 26991da177e4SLinus Torvalds 2700bcdd553fSStephen Hemminger /* Step 3: clear tempaddr list */ 2701372e6c8fSstephen hemminger while (!list_empty(&idev->tempaddr_list)) { 2702372e6c8fSstephen hemminger ifa = list_first_entry(&idev->tempaddr_list, 2703372e6c8fSstephen hemminger struct inet6_ifaddr, tmp_list); 2704372e6c8fSstephen hemminger list_del(&ifa->tmp_list); 27051da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 27061da177e4SLinus Torvalds spin_lock_bh(&ifa->lock); 27071da177e4SLinus Torvalds 27081da177e4SLinus Torvalds if (ifa->ifpub) { 27091da177e4SLinus Torvalds in6_ifa_put(ifa->ifpub); 27101da177e4SLinus Torvalds ifa->ifpub = NULL; 27111da177e4SLinus Torvalds } 27121da177e4SLinus Torvalds spin_unlock_bh(&ifa->lock); 27131da177e4SLinus Torvalds in6_ifa_put(ifa); 27141da177e4SLinus Torvalds write_lock_bh(&idev->lock); 27151da177e4SLinus Torvalds } 27161da177e4SLinus Torvalds #endif 27178f37ada5Sstephen hemminger 2718502a2ffdSstephen hemminger while (!list_empty(&idev->addr_list)) { 2719502a2ffdSstephen hemminger ifa = list_first_entry(&idev->addr_list, 2720502a2ffdSstephen hemminger struct inet6_ifaddr, if_list); 272184e8b803Sstephen hemminger addrconf_del_timer(ifa); 272284e8b803Sstephen hemminger 272384e8b803Sstephen hemminger /* If just doing link down, and address is permanent 272484e8b803Sstephen hemminger and not link-local, then retain it. */ 2725bcdd553fSStephen Hemminger if (!how && 272684e8b803Sstephen hemminger (ifa->flags&IFA_F_PERMANENT) && 272784e8b803Sstephen hemminger !(ipv6_addr_type(&ifa->addr) & IPV6_ADDR_LINKLOCAL)) { 2728502a2ffdSstephen hemminger list_move_tail(&ifa->if_list, &keep_list); 2729dc2b99f7Sstephen hemminger 273084e8b803Sstephen hemminger /* If not doing DAD on this address, just keep it. */ 273184e8b803Sstephen hemminger if ((dev->flags&(IFF_NOARP|IFF_LOOPBACK)) || 2732dc2b99f7Sstephen hemminger idev->cnf.accept_dad <= 0 || 273384e8b803Sstephen hemminger (ifa->flags & IFA_F_NODAD)) 273484e8b803Sstephen hemminger continue; 273584e8b803Sstephen hemminger 273684e8b803Sstephen hemminger /* If it was tentative already, no need to notify */ 273784e8b803Sstephen hemminger if (ifa->flags & IFA_F_TENTATIVE) 273884e8b803Sstephen hemminger continue; 273984e8b803Sstephen hemminger 274084e8b803Sstephen hemminger /* Flag it for later restoration when link comes up */ 2741dc2b99f7Sstephen hemminger ifa->flags |= IFA_F_TENTATIVE; 2742f2344a13SHerbert Xu ifa->state = INET6_IFADDR_STATE_DAD; 27434c5ff6a6SHerbert Xu 274427bdb2abSstephen hemminger write_unlock_bh(&idev->lock); 27454c5ff6a6SHerbert Xu 27464c5ff6a6SHerbert Xu in6_ifa_hold(ifa); 2747dc2b99f7Sstephen hemminger } else { 2748502a2ffdSstephen hemminger list_del(&ifa->if_list); 27491da177e4SLinus Torvalds 2750c2e21293Sstephen hemminger /* clear hash table */ 27515c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 27525c578aedSstephen hemminger hlist_del_init_rcu(&ifa->addr_lst); 27535c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 27544c5ff6a6SHerbert Xu 27554c5ff6a6SHerbert Xu write_unlock_bh(&idev->lock); 27564c5ff6a6SHerbert Xu spin_lock_bh(&ifa->state_lock); 27574c5ff6a6SHerbert Xu state = ifa->state; 27584c5ff6a6SHerbert Xu ifa->state = INET6_IFADDR_STATE_DEAD; 27594c5ff6a6SHerbert Xu spin_unlock_bh(&ifa->state_lock); 27604c5ff6a6SHerbert Xu 27614c5ff6a6SHerbert Xu if (state == INET6_IFADDR_STATE_DEAD) 27624c5ff6a6SHerbert Xu goto put_ifa; 276327bdb2abSstephen hemminger } 2764c2e21293Sstephen hemminger 27651da177e4SLinus Torvalds __ipv6_ifa_notify(RTM_DELADDR, ifa); 2766e9d3e084SHerbert Xu if (ifa->state == INET6_IFADDR_STATE_DEAD) 27678595805aSstephen hemminger atomic_notifier_call_chain(&inet6addr_chain, 27688595805aSstephen hemminger NETDEV_DOWN, ifa); 27694c5ff6a6SHerbert Xu 27704c5ff6a6SHerbert Xu put_ifa: 27711da177e4SLinus Torvalds in6_ifa_put(ifa); 27721da177e4SLinus Torvalds 27731da177e4SLinus Torvalds write_lock_bh(&idev->lock); 27741da177e4SLinus Torvalds } 27758f37ada5Sstephen hemminger 2776502a2ffdSstephen hemminger list_splice(&keep_list, &idev->addr_list); 27778f37ada5Sstephen hemminger 27781da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 27791da177e4SLinus Torvalds 27801da177e4SLinus Torvalds /* Step 5: Discard multicast list */ 2781439e2385SDenis V. Lunev if (how) 27821da177e4SLinus Torvalds ipv6_mc_destroy_dev(idev); 27831da177e4SLinus Torvalds else 27841da177e4SLinus Torvalds ipv6_mc_down(idev); 27851da177e4SLinus Torvalds 27861da177e4SLinus Torvalds idev->tstamp = jiffies; 27871da177e4SLinus Torvalds 2788bcdd553fSStephen Hemminger /* Last: Shot the device (if unregistered) */ 2789439e2385SDenis V. Lunev if (how) { 2790408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 27911da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, idev->nd_parms); 27921da177e4SLinus Torvalds neigh_ifdown(&nd_tbl, dev); 27931da177e4SLinus Torvalds in6_dev_put(idev); 27941da177e4SLinus Torvalds } 27951da177e4SLinus Torvalds return 0; 27961da177e4SLinus Torvalds } 27971da177e4SLinus Torvalds 27981da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data) 27991da177e4SLinus Torvalds { 28001da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data; 28015b2a1953Sstephen hemminger struct inet6_dev *idev = ifp->idev; 28021da177e4SLinus Torvalds 28035b2a1953Sstephen hemminger read_lock(&idev->lock); 28045b2a1953Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) 28051da177e4SLinus Torvalds goto out; 28061da177e4SLinus Torvalds 28075b2a1953Sstephen hemminger if (idev->cnf.forwarding) 28081da177e4SLinus Torvalds goto out; 28095b2a1953Sstephen hemminger 28105b2a1953Sstephen hemminger /* Announcement received after solicitation was sent */ 28115b2a1953Sstephen hemminger if (idev->if_flags & IF_RA_RCVD) 28125b2a1953Sstephen hemminger goto out; 28131da177e4SLinus Torvalds 28141da177e4SLinus Torvalds spin_lock(&ifp->lock); 28155b2a1953Sstephen hemminger if (ifp->probes++ < idev->cnf.rtr_solicits) { 28161da177e4SLinus Torvalds /* The wait after the last probe can be shorter */ 28171da177e4SLinus Torvalds addrconf_mod_timer(ifp, AC_RS, 28185b2a1953Sstephen hemminger (ifp->probes == idev->cnf.rtr_solicits) ? 28195b2a1953Sstephen hemminger idev->cnf.rtr_solicit_delay : 28205b2a1953Sstephen hemminger idev->cnf.rtr_solicit_interval); 28211da177e4SLinus Torvalds spin_unlock(&ifp->lock); 28221da177e4SLinus Torvalds 28235b2a1953Sstephen hemminger ndisc_send_rs(idev->dev, &ifp->addr, &in6addr_linklocal_allrouters); 28241da177e4SLinus Torvalds } else { 28251da177e4SLinus Torvalds spin_unlock(&ifp->lock); 28261da177e4SLinus Torvalds /* 28271da177e4SLinus Torvalds * Note: we do not support deprecated "all on-link" 28281da177e4SLinus Torvalds * assumption any longer. 28291da177e4SLinus Torvalds */ 28301da177e4SLinus Torvalds printk(KERN_DEBUG "%s: no IPv6 routers present\n", 28315b2a1953Sstephen hemminger idev->dev->name); 28321da177e4SLinus Torvalds } 28331da177e4SLinus Torvalds 28341da177e4SLinus Torvalds out: 28355b2a1953Sstephen hemminger read_unlock(&idev->lock); 28361da177e4SLinus Torvalds in6_ifa_put(ifp); 28371da177e4SLinus Torvalds } 28381da177e4SLinus Torvalds 28391da177e4SLinus Torvalds /* 28401da177e4SLinus Torvalds * Duplicate Address Detection 28411da177e4SLinus Torvalds */ 28423c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp) 28433c21edbdSYOSHIFUJI Hideaki { 28443c21edbdSYOSHIFUJI Hideaki unsigned long rand_num; 28453c21edbdSYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 28463c21edbdSYOSHIFUJI Hideaki 284795c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 284895c385b4SNeil Horman rand_num = 0; 284995c385b4SNeil Horman else 28503c21edbdSYOSHIFUJI Hideaki rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1); 285195c385b4SNeil Horman 28523c21edbdSYOSHIFUJI Hideaki ifp->probes = idev->cnf.dad_transmits; 28533c21edbdSYOSHIFUJI Hideaki addrconf_mod_timer(ifp, AC_DAD, rand_num); 28543c21edbdSYOSHIFUJI Hideaki } 28553c21edbdSYOSHIFUJI Hideaki 2856e431b8c0SJamal Hadi Salim static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags) 28571da177e4SLinus Torvalds { 28581da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 28591da177e4SLinus Torvalds struct net_device *dev = idev->dev; 28601da177e4SLinus Torvalds 28611da177e4SLinus Torvalds addrconf_join_solict(dev, &ifp->addr); 28621da177e4SLinus Torvalds 28631da177e4SLinus Torvalds net_srandom(ifp->addr.s6_addr32[3]); 28641da177e4SLinus Torvalds 28651da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2866622ccdf1SHerbert Xu spin_lock(&ifp->lock); 2867e9d3e084SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) 28681da177e4SLinus Torvalds goto out; 28691da177e4SLinus Torvalds 28701da177e4SLinus Torvalds if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) || 28711b34be74SYOSHIFUJI Hideaki idev->cnf.accept_dad < 1 || 287255ebaef1SNoriaki TAKAMIYA !(ifp->flags&IFA_F_TENTATIVE) || 287355ebaef1SNoriaki TAKAMIYA ifp->flags & IFA_F_NODAD) { 2874cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 287521809fafSstephen hemminger spin_unlock(&ifp->lock); 28761da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 28771da177e4SLinus Torvalds 28781da177e4SLinus Torvalds addrconf_dad_completed(ifp); 28791da177e4SLinus Torvalds return; 28801da177e4SLinus Torvalds } 28811da177e4SLinus Torvalds 28826732badeSYOSHIFUJI Hideaki if (!(idev->if_flags & IF_READY)) { 288321809fafSstephen hemminger spin_unlock(&ifp->lock); 28846732badeSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 28853c21edbdSYOSHIFUJI Hideaki /* 2886590a9887SMasatake YAMATO * If the device is not ready: 28873c21edbdSYOSHIFUJI Hideaki * - keep it tentative if it is a permanent address. 28883c21edbdSYOSHIFUJI Hideaki * - otherwise, kill it. 28893c21edbdSYOSHIFUJI Hideaki */ 28903c21edbdSYOSHIFUJI Hideaki in6_ifa_hold(ifp); 2891cc411d0bSBrian Haley addrconf_dad_stop(ifp, 0); 28926732badeSYOSHIFUJI Hideaki return; 28933c21edbdSYOSHIFUJI Hideaki } 289495c385b4SNeil Horman 289595c385b4SNeil Horman /* 289695c385b4SNeil Horman * Optimistic nodes can start receiving 289795c385b4SNeil Horman * Frames right away 289895c385b4SNeil Horman */ 289995c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 290095c385b4SNeil Horman ip6_ins_rt(ifp->rt); 290195c385b4SNeil Horman 29026732badeSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 29031da177e4SLinus Torvalds out: 2904622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 29051da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 29061da177e4SLinus Torvalds } 29071da177e4SLinus Torvalds 29081da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data) 29091da177e4SLinus Torvalds { 29101da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data; 29111da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 29121da177e4SLinus Torvalds struct in6_addr mcaddr; 29131da177e4SLinus Torvalds 2914f2344a13SHerbert Xu if (!ifp->probes && addrconf_dad_end(ifp)) 2915f2344a13SHerbert Xu goto out; 2916f2344a13SHerbert Xu 2917122e4519Sstephen hemminger read_lock(&idev->lock); 2918122e4519Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) { 2919122e4519Sstephen hemminger read_unlock(&idev->lock); 29201da177e4SLinus Torvalds goto out; 29211da177e4SLinus Torvalds } 292221809fafSstephen hemminger 292321809fafSstephen hemminger spin_lock(&ifp->lock); 2924622ccdf1SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) { 2925622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 2926622ccdf1SHerbert Xu read_unlock(&idev->lock); 2927622ccdf1SHerbert Xu goto out; 2928622ccdf1SHerbert Xu } 2929622ccdf1SHerbert Xu 29301da177e4SLinus Torvalds if (ifp->probes == 0) { 29311da177e4SLinus Torvalds /* 29321da177e4SLinus Torvalds * DAD was successful 29331da177e4SLinus Torvalds */ 29341da177e4SLinus Torvalds 2935cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 293621809fafSstephen hemminger spin_unlock(&ifp->lock); 2937122e4519Sstephen hemminger read_unlock(&idev->lock); 29381da177e4SLinus Torvalds 29391da177e4SLinus Torvalds addrconf_dad_completed(ifp); 29401da177e4SLinus Torvalds 29411da177e4SLinus Torvalds goto out; 29421da177e4SLinus Torvalds } 29431da177e4SLinus Torvalds 29441da177e4SLinus Torvalds ifp->probes--; 29451da177e4SLinus Torvalds addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time); 294621809fafSstephen hemminger spin_unlock(&ifp->lock); 2947122e4519Sstephen hemminger read_unlock(&idev->lock); 29481da177e4SLinus Torvalds 29491da177e4SLinus Torvalds /* send a neighbour solicitation for our addr */ 29501da177e4SLinus Torvalds addrconf_addr_solict_mult(&ifp->addr, &mcaddr); 2951d7aabf22SYOSHIFUJI Hideaki ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any); 29521da177e4SLinus Torvalds out: 29531da177e4SLinus Torvalds in6_ifa_put(ifp); 29541da177e4SLinus Torvalds } 29551da177e4SLinus Torvalds 29561da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp) 29571da177e4SLinus Torvalds { 29581da177e4SLinus Torvalds struct net_device *dev = ifp->idev->dev; 29591da177e4SLinus Torvalds 29601da177e4SLinus Torvalds /* 29611da177e4SLinus Torvalds * Configure the address for reception. Now it is valid. 29621da177e4SLinus Torvalds */ 29631da177e4SLinus Torvalds 29641da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 29651da177e4SLinus Torvalds 29661da177e4SLinus Torvalds /* If added prefix is link local and forwarding is off, 29671da177e4SLinus Torvalds start sending router solicitations. 29681da177e4SLinus Torvalds */ 29691da177e4SLinus Torvalds 2970c3bccac2SThomas Graf if ((ifp->idev->cnf.forwarding == 0 || 2971c3bccac2SThomas Graf ifp->idev->cnf.forwarding == 2) && 29721da177e4SLinus Torvalds ifp->idev->cnf.rtr_solicits > 0 && 29731da177e4SLinus Torvalds (dev->flags&IFF_LOOPBACK) == 0 && 29741da177e4SLinus Torvalds (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) { 29751da177e4SLinus Torvalds /* 29761da177e4SLinus Torvalds * If a host as already performed a random delay 29771da177e4SLinus Torvalds * [...] as part of DAD [...] there is no need 29781da177e4SLinus Torvalds * to delay again before sending the first RS 29791da177e4SLinus Torvalds */ 2980f3ee4010SYOSHIFUJI Hideaki ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters); 29811da177e4SLinus Torvalds 29821da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 29831da177e4SLinus Torvalds ifp->probes = 1; 29841da177e4SLinus Torvalds ifp->idev->if_flags |= IF_RS_SENT; 29851da177e4SLinus Torvalds addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval); 29861da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 29871da177e4SLinus Torvalds } 29881da177e4SLinus Torvalds } 29891da177e4SLinus Torvalds 2990e21e8467SStephen Hemminger static void addrconf_dad_run(struct inet6_dev *idev) 2991e21e8467SStephen Hemminger { 2992c5e33bddSYOSHIFUJI Hideaki struct inet6_ifaddr *ifp; 2993c5e33bddSYOSHIFUJI Hideaki 2994c5e33bddSYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 2995502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 299621809fafSstephen hemminger spin_lock(&ifp->lock); 2997f2344a13SHerbert Xu if (ifp->flags & IFA_F_TENTATIVE && 2998f2344a13SHerbert Xu ifp->state == INET6_IFADDR_STATE_DAD) 2999c5e33bddSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 3000f2344a13SHerbert Xu spin_unlock(&ifp->lock); 3001c5e33bddSYOSHIFUJI Hideaki } 3002c5e33bddSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 3003c5e33bddSYOSHIFUJI Hideaki } 3004c5e33bddSYOSHIFUJI Hideaki 30051da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 30061da177e4SLinus Torvalds struct if6_iter_state { 30073c40090aSDaniel Lezcano struct seq_net_private p; 30081da177e4SLinus Torvalds int bucket; 30091da177e4SLinus Torvalds }; 30101da177e4SLinus Torvalds 30111da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_first(struct seq_file *seq) 30121da177e4SLinus Torvalds { 30131da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 30141da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 30151218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 30161da177e4SLinus Torvalds 30171da177e4SLinus Torvalds for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) { 3018c2e21293Sstephen hemminger struct hlist_node *n; 30194f70eccaSEric Dumazet hlist_for_each_entry_rcu_bh(ifa, n, &inet6_addr_lst[state->bucket], 30205c578aedSstephen hemminger addr_lst) 3021c2e21293Sstephen hemminger if (net_eq(dev_net(ifa->idev->dev), net)) 30221da177e4SLinus Torvalds return ifa; 30231da177e4SLinus Torvalds } 3024c2e21293Sstephen hemminger return NULL; 3025c2e21293Sstephen hemminger } 30261da177e4SLinus Torvalds 3027c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, 3028c2e21293Sstephen hemminger struct inet6_ifaddr *ifa) 30291da177e4SLinus Torvalds { 30301da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 30311218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 3032c2e21293Sstephen hemminger struct hlist_node *n = &ifa->addr_lst; 30331da177e4SLinus Torvalds 30344f70eccaSEric Dumazet hlist_for_each_entry_continue_rcu_bh(ifa, n, addr_lst) 3035c2e21293Sstephen hemminger if (net_eq(dev_net(ifa->idev->dev), net)) 30361da177e4SLinus Torvalds return ifa; 30371da177e4SLinus Torvalds 3038c2e21293Sstephen hemminger while (++state->bucket < IN6_ADDR_HSIZE) { 30394f70eccaSEric Dumazet hlist_for_each_entry_rcu_bh(ifa, n, 3040c2e21293Sstephen hemminger &inet6_addr_lst[state->bucket], addr_lst) { 3041c2e21293Sstephen hemminger if (net_eq(dev_net(ifa->idev->dev), net)) 3042c2e21293Sstephen hemminger return ifa; 3043c2e21293Sstephen hemminger } 3044c2e21293Sstephen hemminger } 3045c2e21293Sstephen hemminger 3046c2e21293Sstephen hemminger return NULL; 3047c2e21293Sstephen hemminger } 3048c2e21293Sstephen hemminger 30491da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos) 30501da177e4SLinus Torvalds { 30511da177e4SLinus Torvalds struct inet6_ifaddr *ifa = if6_get_first(seq); 30521da177e4SLinus Torvalds 30531da177e4SLinus Torvalds if (ifa) 30541da177e4SLinus Torvalds while (pos && (ifa = if6_get_next(seq, ifa)) != NULL) 30551da177e4SLinus Torvalds --pos; 30561da177e4SLinus Torvalds return pos ? NULL : ifa; 30571da177e4SLinus Torvalds } 30581da177e4SLinus Torvalds 30591da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos) 30604f70eccaSEric Dumazet __acquires(rcu_bh) 30611da177e4SLinus Torvalds { 30625c578aedSstephen hemminger rcu_read_lock_bh(); 30631da177e4SLinus Torvalds return if6_get_idx(seq, *pos); 30641da177e4SLinus Torvalds } 30651da177e4SLinus Torvalds 30661da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos) 30671da177e4SLinus Torvalds { 30681da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 30691da177e4SLinus Torvalds 30701da177e4SLinus Torvalds ifa = if6_get_next(seq, v); 30711da177e4SLinus Torvalds ++*pos; 30721da177e4SLinus Torvalds return ifa; 30731da177e4SLinus Torvalds } 30741da177e4SLinus Torvalds 30751da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v) 30764f70eccaSEric Dumazet __releases(rcu_bh) 30771da177e4SLinus Torvalds { 30785c578aedSstephen hemminger rcu_read_unlock_bh(); 30791da177e4SLinus Torvalds } 30801da177e4SLinus Torvalds 30811da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v) 30821da177e4SLinus Torvalds { 30831da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v; 30844b7a4274SHarvey Harrison seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n", 3085b071195dSHarvey Harrison &ifp->addr, 30861da177e4SLinus Torvalds ifp->idev->dev->ifindex, 30871da177e4SLinus Torvalds ifp->prefix_len, 30881da177e4SLinus Torvalds ifp->scope, 30891da177e4SLinus Torvalds ifp->flags, 30901da177e4SLinus Torvalds ifp->idev->dev->name); 30911da177e4SLinus Torvalds return 0; 30921da177e4SLinus Torvalds } 30931da177e4SLinus Torvalds 309456b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = { 30951da177e4SLinus Torvalds .start = if6_seq_start, 30961da177e4SLinus Torvalds .next = if6_seq_next, 30971da177e4SLinus Torvalds .show = if6_seq_show, 30981da177e4SLinus Torvalds .stop = if6_seq_stop, 30991da177e4SLinus Torvalds }; 31001da177e4SLinus Torvalds 31011da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file) 31021da177e4SLinus Torvalds { 31033c40090aSDaniel Lezcano return seq_open_net(inode, file, &if6_seq_ops, 3104cf7732e4SPavel Emelyanov sizeof(struct if6_iter_state)); 31051da177e4SLinus Torvalds } 31061da177e4SLinus Torvalds 31079a32144eSArjan van de Ven static const struct file_operations if6_fops = { 31081da177e4SLinus Torvalds .owner = THIS_MODULE, 31091da177e4SLinus Torvalds .open = if6_seq_open, 31101da177e4SLinus Torvalds .read = seq_read, 31111da177e4SLinus Torvalds .llseek = seq_lseek, 31123c40090aSDaniel Lezcano .release = seq_release_net, 31131da177e4SLinus Torvalds }; 31141da177e4SLinus Torvalds 31152c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net) 31161da177e4SLinus Torvalds { 31173c40090aSDaniel Lezcano if (!proc_net_fops_create(net, "if_inet6", S_IRUGO, &if6_fops)) 31181da177e4SLinus Torvalds return -ENOMEM; 31191da177e4SLinus Torvalds return 0; 31201da177e4SLinus Torvalds } 31211da177e4SLinus Torvalds 31222c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net) 31233c40090aSDaniel Lezcano { 31243c40090aSDaniel Lezcano proc_net_remove(net, "if_inet6"); 31253c40090aSDaniel Lezcano } 31263c40090aSDaniel Lezcano 31273c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = { 31283c40090aSDaniel Lezcano .init = if6_proc_net_init, 31293c40090aSDaniel Lezcano .exit = if6_proc_net_exit, 31303c40090aSDaniel Lezcano }; 31313c40090aSDaniel Lezcano 31323c40090aSDaniel Lezcano int __init if6_proc_init(void) 31333c40090aSDaniel Lezcano { 31343c40090aSDaniel Lezcano return register_pernet_subsys(&if6_proc_net_ops); 31353c40090aSDaniel Lezcano } 31363c40090aSDaniel Lezcano 31371da177e4SLinus Torvalds void if6_proc_exit(void) 31381da177e4SLinus Torvalds { 31393c40090aSDaniel Lezcano unregister_pernet_subsys(&if6_proc_net_ops); 31401da177e4SLinus Torvalds } 31411da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 31421da177e4SLinus Torvalds 314359fbb3a6SMasahide NAKAMURA #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE) 31443b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */ 3145389f6612SDaniel Lezcano int ipv6_chk_home_addr(struct net *net, struct in6_addr *addr) 31463b9f9a1cSNoriaki TAKAMIYA { 31473b9f9a1cSNoriaki TAKAMIYA int ret = 0; 3148c2e21293Sstephen hemminger struct inet6_ifaddr *ifp = NULL; 3149c2e21293Sstephen hemminger struct hlist_node *n; 31503a88a81dSstephen hemminger unsigned int hash = ipv6_addr_hash(addr); 3151c2e21293Sstephen hemminger 31525c578aedSstephen hemminger rcu_read_lock_bh(); 31534f70eccaSEric Dumazet hlist_for_each_entry_rcu_bh(ifp, n, &inet6_addr_lst[hash], addr_lst) { 3154878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 3155389f6612SDaniel Lezcano continue; 3156caad295fSYOSHIFUJI Hideaki if (ipv6_addr_equal(&ifp->addr, addr) && 31573b9f9a1cSNoriaki TAKAMIYA (ifp->flags & IFA_F_HOMEADDRESS)) { 31583b9f9a1cSNoriaki TAKAMIYA ret = 1; 31593b9f9a1cSNoriaki TAKAMIYA break; 31603b9f9a1cSNoriaki TAKAMIYA } 31613b9f9a1cSNoriaki TAKAMIYA } 31625c578aedSstephen hemminger rcu_read_unlock_bh(); 31633b9f9a1cSNoriaki TAKAMIYA return ret; 31643b9f9a1cSNoriaki TAKAMIYA } 31653b9f9a1cSNoriaki TAKAMIYA #endif 31663b9f9a1cSNoriaki TAKAMIYA 31671da177e4SLinus Torvalds /* 31681da177e4SLinus Torvalds * Periodic address status verification 31691da177e4SLinus Torvalds */ 31701da177e4SLinus Torvalds 31711da177e4SLinus Torvalds static void addrconf_verify(unsigned long foo) 31721da177e4SLinus Torvalds { 3173b2db7564SYOSHIFUJI Hideaki unsigned long now, next, next_sec, next_sched; 31741da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 3175c2e21293Sstephen hemminger struct hlist_node *node; 31761da177e4SLinus Torvalds int i; 31771da177e4SLinus Torvalds 31785c578aedSstephen hemminger rcu_read_lock_bh(); 31795c578aedSstephen hemminger spin_lock(&addrconf_verify_lock); 31801da177e4SLinus Torvalds now = jiffies; 3181b2db7564SYOSHIFUJI Hideaki next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY); 31821da177e4SLinus Torvalds 31831da177e4SLinus Torvalds del_timer(&addr_chk_timer); 31841da177e4SLinus Torvalds 31851da177e4SLinus Torvalds for (i = 0; i < IN6_ADDR_HSIZE; i++) { 31861da177e4SLinus Torvalds restart: 31874f70eccaSEric Dumazet hlist_for_each_entry_rcu_bh(ifp, node, 31885c578aedSstephen hemminger &inet6_addr_lst[i], addr_lst) { 31891da177e4SLinus Torvalds unsigned long age; 31901da177e4SLinus Torvalds 31911da177e4SLinus Torvalds if (ifp->flags & IFA_F_PERMANENT) 31921da177e4SLinus Torvalds continue; 31931da177e4SLinus Torvalds 31941da177e4SLinus Torvalds spin_lock(&ifp->lock); 3195b2db7564SYOSHIFUJI Hideaki /* We try to batch several events at once. */ 3196b2db7564SYOSHIFUJI Hideaki age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 31971da177e4SLinus Torvalds 31988f27ebb9SYOSHIFUJI Hideaki if (ifp->valid_lft != INFINITY_LIFE_TIME && 31998f27ebb9SYOSHIFUJI Hideaki age >= ifp->valid_lft) { 32001da177e4SLinus Torvalds spin_unlock(&ifp->lock); 32011da177e4SLinus Torvalds in6_ifa_hold(ifp); 32021da177e4SLinus Torvalds ipv6_del_addr(ifp); 32031da177e4SLinus Torvalds goto restart; 32048f27ebb9SYOSHIFUJI Hideaki } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) { 32058f27ebb9SYOSHIFUJI Hideaki spin_unlock(&ifp->lock); 32068f27ebb9SYOSHIFUJI Hideaki continue; 32071da177e4SLinus Torvalds } else if (age >= ifp->prefered_lft) { 3208a1ed0526SBrian Haley /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */ 32091da177e4SLinus Torvalds int deprecate = 0; 32101da177e4SLinus Torvalds 32111da177e4SLinus Torvalds if (!(ifp->flags&IFA_F_DEPRECATED)) { 32121da177e4SLinus Torvalds deprecate = 1; 32131da177e4SLinus Torvalds ifp->flags |= IFA_F_DEPRECATED; 32141da177e4SLinus Torvalds } 32151da177e4SLinus Torvalds 32161da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)) 32171da177e4SLinus Torvalds next = ifp->tstamp + ifp->valid_lft * HZ; 32181da177e4SLinus Torvalds 32191da177e4SLinus Torvalds spin_unlock(&ifp->lock); 32201da177e4SLinus Torvalds 32211da177e4SLinus Torvalds if (deprecate) { 32221da177e4SLinus Torvalds in6_ifa_hold(ifp); 32231da177e4SLinus Torvalds 32241da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 32251da177e4SLinus Torvalds in6_ifa_put(ifp); 32261da177e4SLinus Torvalds goto restart; 32271da177e4SLinus Torvalds } 32281da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 32291da177e4SLinus Torvalds } else if ((ifp->flags&IFA_F_TEMPORARY) && 32301da177e4SLinus Torvalds !(ifp->flags&IFA_F_TENTATIVE)) { 323188949cf4Sstephen hemminger unsigned long regen_advance = ifp->idev->cnf.regen_max_retry * 323288949cf4Sstephen hemminger ifp->idev->cnf.dad_transmits * 323388949cf4Sstephen hemminger ifp->idev->nd_parms->retrans_time / HZ; 323488949cf4Sstephen hemminger 32351da177e4SLinus Torvalds if (age >= ifp->prefered_lft - regen_advance) { 32361da177e4SLinus Torvalds struct inet6_ifaddr *ifpub = ifp->ifpub; 32371da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 32381da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 32391da177e4SLinus Torvalds if (!ifp->regen_count && ifpub) { 32401da177e4SLinus Torvalds ifp->regen_count++; 32411da177e4SLinus Torvalds in6_ifa_hold(ifp); 32421da177e4SLinus Torvalds in6_ifa_hold(ifpub); 32431da177e4SLinus Torvalds spin_unlock(&ifp->lock); 32445c578aedSstephen hemminger 3245291d809bSHiroyuki YAMAMORI spin_lock(&ifpub->lock); 3246291d809bSHiroyuki YAMAMORI ifpub->regen_count = 0; 3247291d809bSHiroyuki YAMAMORI spin_unlock(&ifpub->lock); 32481da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 32491da177e4SLinus Torvalds in6_ifa_put(ifpub); 32501da177e4SLinus Torvalds in6_ifa_put(ifp); 32511da177e4SLinus Torvalds goto restart; 32521da177e4SLinus Torvalds } 32531da177e4SLinus Torvalds } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) 32541da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; 32551da177e4SLinus Torvalds spin_unlock(&ifp->lock); 32561da177e4SLinus Torvalds #endif 32571da177e4SLinus Torvalds } else { 32581da177e4SLinus Torvalds /* ifp->prefered_lft <= ifp->valid_lft */ 32591da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 32601da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 32611da177e4SLinus Torvalds spin_unlock(&ifp->lock); 32621da177e4SLinus Torvalds } 32631da177e4SLinus Torvalds } 32641da177e4SLinus Torvalds } 32651da177e4SLinus Torvalds 3266b2db7564SYOSHIFUJI Hideaki next_sec = round_jiffies_up(next); 3267b2db7564SYOSHIFUJI Hideaki next_sched = next; 3268b2db7564SYOSHIFUJI Hideaki 3269b2db7564SYOSHIFUJI Hideaki /* If rounded timeout is accurate enough, accept it. */ 3270b2db7564SYOSHIFUJI Hideaki if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ)) 3271b2db7564SYOSHIFUJI Hideaki next_sched = next_sec; 3272b2db7564SYOSHIFUJI Hideaki 3273b2db7564SYOSHIFUJI Hideaki /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */ 3274b2db7564SYOSHIFUJI Hideaki if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX)) 3275b2db7564SYOSHIFUJI Hideaki next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX; 3276b2db7564SYOSHIFUJI Hideaki 3277b2db7564SYOSHIFUJI Hideaki ADBG((KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n", 3278b2db7564SYOSHIFUJI Hideaki now, next, next_sec, next_sched)); 3279b2db7564SYOSHIFUJI Hideaki 3280b2db7564SYOSHIFUJI Hideaki addr_chk_timer.expires = next_sched; 32811da177e4SLinus Torvalds add_timer(&addr_chk_timer); 32825c578aedSstephen hemminger spin_unlock(&addrconf_verify_lock); 32835c578aedSstephen hemminger rcu_read_unlock_bh(); 32841da177e4SLinus Torvalds } 32851da177e4SLinus Torvalds 3286461d8837SThomas Graf static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local) 3287461d8837SThomas Graf { 3288461d8837SThomas Graf struct in6_addr *pfx = NULL; 3289461d8837SThomas Graf 3290461d8837SThomas Graf if (addr) 3291461d8837SThomas Graf pfx = nla_data(addr); 3292461d8837SThomas Graf 3293461d8837SThomas Graf if (local) { 3294461d8837SThomas Graf if (pfx && nla_memcmp(local, pfx, sizeof(*pfx))) 3295461d8837SThomas Graf pfx = NULL; 3296461d8837SThomas Graf else 3297461d8837SThomas Graf pfx = nla_data(local); 3298461d8837SThomas Graf } 3299461d8837SThomas Graf 3300461d8837SThomas Graf return pfx; 3301461d8837SThomas Graf } 3302461d8837SThomas Graf 3303ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = { 3304461d8837SThomas Graf [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) }, 3305461d8837SThomas Graf [IFA_LOCAL] = { .len = sizeof(struct in6_addr) }, 3306461d8837SThomas Graf [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, 3307461d8837SThomas Graf }; 3308461d8837SThomas Graf 33091da177e4SLinus Torvalds static int 33101da177e4SLinus Torvalds inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 33111da177e4SLinus Torvalds { 33123b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 3313b933f716SThomas Graf struct ifaddrmsg *ifm; 3314b933f716SThomas Graf struct nlattr *tb[IFA_MAX+1]; 33151da177e4SLinus Torvalds struct in6_addr *pfx; 3316b933f716SThomas Graf int err; 33171da177e4SLinus Torvalds 3318b933f716SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 3319b933f716SThomas Graf if (err < 0) 3320b933f716SThomas Graf return err; 3321b933f716SThomas Graf 3322b933f716SThomas Graf ifm = nlmsg_data(nlh); 3323b933f716SThomas Graf pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]); 33241da177e4SLinus Torvalds if (pfx == NULL) 33251da177e4SLinus Torvalds return -EINVAL; 33261da177e4SLinus Torvalds 3327af284937SDaniel Lezcano return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen); 33281da177e4SLinus Torvalds } 33291da177e4SLinus Torvalds 333055ebaef1SNoriaki TAKAMIYA static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags, 333155ebaef1SNoriaki TAKAMIYA u32 prefered_lft, u32 valid_lft) 3332081bba5bSNoriaki TAKAMIYA { 33336f704992SYOSHIFUJI Hideaki u32 flags; 33346f704992SYOSHIFUJI Hideaki clock_t expires; 33354bed72e4SYOSHIFUJI Hideaki unsigned long timeout; 333646d48046SYOSHIFUJI Hideaki 3337081bba5bSNoriaki TAKAMIYA if (!valid_lft || (prefered_lft > valid_lft)) 3338081bba5bSNoriaki TAKAMIYA return -EINVAL; 3339081bba5bSNoriaki TAKAMIYA 33404bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(valid_lft, HZ); 33414bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 33424bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 33434bed72e4SYOSHIFUJI Hideaki valid_lft = timeout; 33446f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 33454bed72e4SYOSHIFUJI Hideaki } else { 33464bed72e4SYOSHIFUJI Hideaki expires = 0; 33474bed72e4SYOSHIFUJI Hideaki flags = 0; 33484bed72e4SYOSHIFUJI Hideaki ifa_flags |= IFA_F_PERMANENT; 33496f704992SYOSHIFUJI Hideaki } 3350081bba5bSNoriaki TAKAMIYA 33514bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(prefered_lft, HZ); 33524bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 33534bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 335455ebaef1SNoriaki TAKAMIYA ifa_flags |= IFA_F_DEPRECATED; 33554bed72e4SYOSHIFUJI Hideaki prefered_lft = timeout; 33564bed72e4SYOSHIFUJI Hideaki } 3357081bba5bSNoriaki TAKAMIYA 3358081bba5bSNoriaki TAKAMIYA spin_lock_bh(&ifp->lock); 33593b9f9a1cSNoriaki TAKAMIYA ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags; 3360081bba5bSNoriaki TAKAMIYA ifp->tstamp = jiffies; 3361081bba5bSNoriaki TAKAMIYA ifp->valid_lft = valid_lft; 3362081bba5bSNoriaki TAKAMIYA ifp->prefered_lft = prefered_lft; 3363081bba5bSNoriaki TAKAMIYA 3364081bba5bSNoriaki TAKAMIYA spin_unlock_bh(&ifp->lock); 3365081bba5bSNoriaki TAKAMIYA if (!(ifp->flags&IFA_F_TENTATIVE)) 3366081bba5bSNoriaki TAKAMIYA ipv6_ifa_notify(0, ifp); 3367081bba5bSNoriaki TAKAMIYA 336846d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev, 33696f704992SYOSHIFUJI Hideaki expires, flags); 3370081bba5bSNoriaki TAKAMIYA addrconf_verify(0); 3371081bba5bSNoriaki TAKAMIYA 3372081bba5bSNoriaki TAKAMIYA return 0; 3373081bba5bSNoriaki TAKAMIYA } 3374081bba5bSNoriaki TAKAMIYA 3375081bba5bSNoriaki TAKAMIYA static int 33761da177e4SLinus Torvalds inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 33771da177e4SLinus Torvalds { 33783b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 3379461d8837SThomas Graf struct ifaddrmsg *ifm; 3380461d8837SThomas Graf struct nlattr *tb[IFA_MAX+1]; 33811da177e4SLinus Torvalds struct in6_addr *pfx; 33827198f8ceSThomas Graf struct inet6_ifaddr *ifa; 33837198f8ceSThomas Graf struct net_device *dev; 338455ebaef1SNoriaki TAKAMIYA u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME; 338555ebaef1SNoriaki TAKAMIYA u8 ifa_flags; 3386461d8837SThomas Graf int err; 33871da177e4SLinus Torvalds 3388461d8837SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 3389461d8837SThomas Graf if (err < 0) 3390461d8837SThomas Graf return err; 3391461d8837SThomas Graf 3392461d8837SThomas Graf ifm = nlmsg_data(nlh); 3393461d8837SThomas Graf pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]); 33941da177e4SLinus Torvalds if (pfx == NULL) 33951da177e4SLinus Torvalds return -EINVAL; 33961da177e4SLinus Torvalds 3397461d8837SThomas Graf if (tb[IFA_CACHEINFO]) { 33980778769dSNoriaki TAKAMIYA struct ifa_cacheinfo *ci; 3399461d8837SThomas Graf 3400461d8837SThomas Graf ci = nla_data(tb[IFA_CACHEINFO]); 34010778769dSNoriaki TAKAMIYA valid_lft = ci->ifa_valid; 3402461d8837SThomas Graf preferred_lft = ci->ifa_prefered; 3403461d8837SThomas Graf } else { 3404461d8837SThomas Graf preferred_lft = INFINITY_LIFE_TIME; 3405461d8837SThomas Graf valid_lft = INFINITY_LIFE_TIME; 34060778769dSNoriaki TAKAMIYA } 34070778769dSNoriaki TAKAMIYA 3408af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifm->ifa_index); 34097198f8ceSThomas Graf if (dev == NULL) 34107198f8ceSThomas Graf return -ENODEV; 3411081bba5bSNoriaki TAKAMIYA 341255ebaef1SNoriaki TAKAMIYA /* We ignore other flags so far. */ 34133b9f9a1cSNoriaki TAKAMIYA ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS); 341455ebaef1SNoriaki TAKAMIYA 34151cab3da6SDaniel Lezcano ifa = ipv6_get_ifaddr(net, pfx, dev, 1); 34167198f8ceSThomas Graf if (ifa == NULL) { 34177198f8ceSThomas Graf /* 34187198f8ceSThomas Graf * It would be best to check for !NLM_F_CREATE here but 34197198f8ceSThomas Graf * userspace alreay relies on not having to provide this. 34207198f8ceSThomas Graf */ 3421af284937SDaniel Lezcano return inet6_addr_add(net, ifm->ifa_index, pfx, 3422af284937SDaniel Lezcano ifm->ifa_prefixlen, ifa_flags, 3423af284937SDaniel Lezcano preferred_lft, valid_lft); 34241da177e4SLinus Torvalds } 34251da177e4SLinus Torvalds 34267198f8ceSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL || 34277198f8ceSThomas Graf !(nlh->nlmsg_flags & NLM_F_REPLACE)) 34287198f8ceSThomas Graf err = -EEXIST; 34297198f8ceSThomas Graf else 343055ebaef1SNoriaki TAKAMIYA err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft); 34317198f8ceSThomas Graf 34327198f8ceSThomas Graf in6_ifa_put(ifa); 34337198f8ceSThomas Graf 34347198f8ceSThomas Graf return err; 34357198f8ceSThomas Graf } 34367198f8ceSThomas Graf 3437101bb229SThomas Graf static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags, 3438101bb229SThomas Graf u8 scope, int ifindex) 3439101bb229SThomas Graf { 3440101bb229SThomas Graf struct ifaddrmsg *ifm; 3441101bb229SThomas Graf 3442101bb229SThomas Graf ifm = nlmsg_data(nlh); 3443101bb229SThomas Graf ifm->ifa_family = AF_INET6; 3444101bb229SThomas Graf ifm->ifa_prefixlen = prefixlen; 3445101bb229SThomas Graf ifm->ifa_flags = flags; 3446101bb229SThomas Graf ifm->ifa_scope = scope; 3447101bb229SThomas Graf ifm->ifa_index = ifindex; 3448101bb229SThomas Graf } 3449101bb229SThomas Graf 345085486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, 345185486af0SThomas Graf unsigned long tstamp, u32 preferred, u32 valid) 345285486af0SThomas Graf { 345385486af0SThomas Graf struct ifa_cacheinfo ci; 345485486af0SThomas Graf 345585486af0SThomas Graf ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100 345685486af0SThomas Graf + TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ); 345785486af0SThomas Graf ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100 345885486af0SThomas Graf + TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ); 345985486af0SThomas Graf ci.ifa_prefered = preferred; 346085486af0SThomas Graf ci.ifa_valid = valid; 346185486af0SThomas Graf 346285486af0SThomas Graf return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); 346385486af0SThomas Graf } 346485486af0SThomas Graf 3465101bb229SThomas Graf static inline int rt_scope(int ifa_scope) 3466101bb229SThomas Graf { 3467101bb229SThomas Graf if (ifa_scope & IFA_HOST) 3468101bb229SThomas Graf return RT_SCOPE_HOST; 3469101bb229SThomas Graf else if (ifa_scope & IFA_LINK) 3470101bb229SThomas Graf return RT_SCOPE_LINK; 3471101bb229SThomas Graf else if (ifa_scope & IFA_SITE) 3472101bb229SThomas Graf return RT_SCOPE_SITE; 3473101bb229SThomas Graf else 3474101bb229SThomas Graf return RT_SCOPE_UNIVERSE; 3475101bb229SThomas Graf } 3476101bb229SThomas Graf 34770ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void) 34780ab6803bSThomas Graf { 3479339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) 3480339bf98fSThomas Graf + nla_total_size(16) /* IFA_ADDRESS */ 3481339bf98fSThomas Graf + nla_total_size(sizeof(struct ifa_cacheinfo)); 34820ab6803bSThomas Graf } 3483c5396a31SYOSHIFUJI Hideaki 34841da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa, 3485b6544c0bSJamal Hadi Salim u32 pid, u32 seq, int event, unsigned int flags) 34861da177e4SLinus Torvalds { 34871da177e4SLinus Torvalds struct nlmsghdr *nlh; 348885486af0SThomas Graf u32 preferred, valid; 34891da177e4SLinus Torvalds 34900ab6803bSThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags); 34910ab6803bSThomas Graf if (nlh == NULL) 349226932566SPatrick McHardy return -EMSGSIZE; 34930ab6803bSThomas Graf 3494101bb229SThomas Graf put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), 3495101bb229SThomas Graf ifa->idev->dev->ifindex); 3496101bb229SThomas Graf 34971da177e4SLinus Torvalds if (!(ifa->flags&IFA_F_PERMANENT)) { 349885486af0SThomas Graf preferred = ifa->prefered_lft; 349985486af0SThomas Graf valid = ifa->valid_lft; 350085486af0SThomas Graf if (preferred != INFINITY_LIFE_TIME) { 35011da177e4SLinus Torvalds long tval = (jiffies - ifa->tstamp)/HZ; 3502a1faa698SJens Rosenboom if (preferred > tval) 350385486af0SThomas Graf preferred -= tval; 3504a1faa698SJens Rosenboom else 3505a1faa698SJens Rosenboom preferred = 0; 3506f56619fcSBen Hutchings if (valid != INFINITY_LIFE_TIME) { 3507f56619fcSBen Hutchings if (valid > tval) 350885486af0SThomas Graf valid -= tval; 3509f56619fcSBen Hutchings else 3510f56619fcSBen Hutchings valid = 0; 3511f56619fcSBen Hutchings } 35121da177e4SLinus Torvalds } 35131da177e4SLinus Torvalds } else { 351485486af0SThomas Graf preferred = INFINITY_LIFE_TIME; 351585486af0SThomas Graf valid = INFINITY_LIFE_TIME; 35161da177e4SLinus Torvalds } 351785486af0SThomas Graf 35180ab6803bSThomas Graf if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 || 351926932566SPatrick McHardy put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) { 352026932566SPatrick McHardy nlmsg_cancel(skb, nlh); 352126932566SPatrick McHardy return -EMSGSIZE; 352226932566SPatrick McHardy } 352385486af0SThomas Graf 35240ab6803bSThomas Graf return nlmsg_end(skb, nlh); 35251da177e4SLinus Torvalds } 35261da177e4SLinus Torvalds 35271da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca, 3528e431b8c0SJamal Hadi Salim u32 pid, u32 seq, int event, u16 flags) 35291da177e4SLinus Torvalds { 35301da177e4SLinus Torvalds struct nlmsghdr *nlh; 3531101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 3532101bb229SThomas Graf int ifindex = ifmca->idev->dev->ifindex; 35331da177e4SLinus Torvalds 35341da177e4SLinus Torvalds if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE) 3535101bb229SThomas Graf scope = RT_SCOPE_SITE; 3536101bb229SThomas Graf 35370ab6803bSThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags); 35380ab6803bSThomas Graf if (nlh == NULL) 353926932566SPatrick McHardy return -EMSGSIZE; 35400ab6803bSThomas Graf 3541101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 35420ab6803bSThomas Graf if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 || 35430ab6803bSThomas Graf put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, 354426932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 354526932566SPatrick McHardy nlmsg_cancel(skb, nlh); 354626932566SPatrick McHardy return -EMSGSIZE; 354726932566SPatrick McHardy } 354885486af0SThomas Graf 35490ab6803bSThomas Graf return nlmsg_end(skb, nlh); 35501da177e4SLinus Torvalds } 35511da177e4SLinus Torvalds 35521da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca, 3553b6544c0bSJamal Hadi Salim u32 pid, u32 seq, int event, unsigned int flags) 35541da177e4SLinus Torvalds { 35551da177e4SLinus Torvalds struct nlmsghdr *nlh; 3556101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 3557101bb229SThomas Graf int ifindex = ifaca->aca_idev->dev->ifindex; 35581da177e4SLinus Torvalds 35591da177e4SLinus Torvalds if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) 3560101bb229SThomas Graf scope = RT_SCOPE_SITE; 3561101bb229SThomas Graf 35620ab6803bSThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags); 35630ab6803bSThomas Graf if (nlh == NULL) 356426932566SPatrick McHardy return -EMSGSIZE; 35650ab6803bSThomas Graf 3566101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 35670ab6803bSThomas Graf if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 || 35680ab6803bSThomas Graf put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, 356926932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 357026932566SPatrick McHardy nlmsg_cancel(skb, nlh); 357126932566SPatrick McHardy return -EMSGSIZE; 357226932566SPatrick McHardy } 357385486af0SThomas Graf 35740ab6803bSThomas Graf return nlmsg_end(skb, nlh); 35751da177e4SLinus Torvalds } 35761da177e4SLinus Torvalds 3577e21e8467SStephen Hemminger enum addr_type_t { 35781da177e4SLinus Torvalds UNICAST_ADDR, 35791da177e4SLinus Torvalds MULTICAST_ADDR, 35801da177e4SLinus Torvalds ANYCAST_ADDR, 35811da177e4SLinus Torvalds }; 35821da177e4SLinus Torvalds 3583234b27c3SEric Dumazet /* called with rcu_read_lock() */ 3584234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb, 3585234b27c3SEric Dumazet struct netlink_callback *cb, enum addr_type_t type, 3586234b27c3SEric Dumazet int s_ip_idx, int *p_ip_idx) 35871da177e4SLinus Torvalds { 35881da177e4SLinus Torvalds struct ifmcaddr6 *ifmca; 35891da177e4SLinus Torvalds struct ifacaddr6 *ifaca; 3590234b27c3SEric Dumazet int err = 1; 3591234b27c3SEric Dumazet int ip_idx = *p_ip_idx; 35921da177e4SLinus Torvalds 35931da177e4SLinus Torvalds read_lock_bh(&idev->lock); 35941da177e4SLinus Torvalds switch (type) { 3595502a2ffdSstephen hemminger case UNICAST_ADDR: { 3596502a2ffdSstephen hemminger struct inet6_ifaddr *ifa; 3597502a2ffdSstephen hemminger 3598ae9cda5dSYOSHIFUJI Hideaki /* unicast address incl. temp addr */ 3599502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 3600502a2ffdSstephen hemminger if (++ip_idx < s_ip_idx) 36011da177e4SLinus Torvalds continue; 36025d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifaddr(skb, ifa, 36031da177e4SLinus Torvalds NETLINK_CB(cb->skb).pid, 36045d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 36055d5619b4SYOSHIFUJI Hideaki RTM_NEWADDR, 36065d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 3607bcd32326SEric Dumazet if (err <= 0) 3608bcd32326SEric Dumazet break; 36091da177e4SLinus Torvalds } 36101da177e4SLinus Torvalds break; 3611502a2ffdSstephen hemminger } 36121da177e4SLinus Torvalds case MULTICAST_ADDR: 36131da177e4SLinus Torvalds /* multicast address */ 36141da177e4SLinus Torvalds for (ifmca = idev->mc_list; ifmca; 36151da177e4SLinus Torvalds ifmca = ifmca->next, ip_idx++) { 36161da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 36171da177e4SLinus Torvalds continue; 36185d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifmcaddr(skb, ifmca, 36191da177e4SLinus Torvalds NETLINK_CB(cb->skb).pid, 36205d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 36215d5619b4SYOSHIFUJI Hideaki RTM_GETMULTICAST, 36225d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 3623bcd32326SEric Dumazet if (err <= 0) 3624bcd32326SEric Dumazet break; 36251da177e4SLinus Torvalds } 36261da177e4SLinus Torvalds break; 36271da177e4SLinus Torvalds case ANYCAST_ADDR: 36281da177e4SLinus Torvalds /* anycast address */ 36291da177e4SLinus Torvalds for (ifaca = idev->ac_list; ifaca; 36301da177e4SLinus Torvalds ifaca = ifaca->aca_next, ip_idx++) { 36311da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 36321da177e4SLinus Torvalds continue; 36335d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifacaddr(skb, ifaca, 36341da177e4SLinus Torvalds NETLINK_CB(cb->skb).pid, 36355d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 36365d5619b4SYOSHIFUJI Hideaki RTM_GETANYCAST, 36375d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 3638bcd32326SEric Dumazet if (err <= 0) 3639bcd32326SEric Dumazet break; 36401da177e4SLinus Torvalds } 36411da177e4SLinus Torvalds break; 36421da177e4SLinus Torvalds default: 36431da177e4SLinus Torvalds break; 36441da177e4SLinus Torvalds } 36451da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 3646234b27c3SEric Dumazet *p_ip_idx = ip_idx; 3647234b27c3SEric Dumazet return err; 3648234b27c3SEric Dumazet } 36495d5619b4SYOSHIFUJI Hideaki 3650234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, 3651234b27c3SEric Dumazet enum addr_type_t type) 3652234b27c3SEric Dumazet { 3653234b27c3SEric Dumazet struct net *net = sock_net(skb->sk); 3654234b27c3SEric Dumazet int h, s_h; 3655234b27c3SEric Dumazet int idx, ip_idx; 3656234b27c3SEric Dumazet int s_idx, s_ip_idx; 3657234b27c3SEric Dumazet struct net_device *dev; 3658234b27c3SEric Dumazet struct inet6_dev *idev; 3659234b27c3SEric Dumazet struct hlist_head *head; 3660234b27c3SEric Dumazet struct hlist_node *node; 3661234b27c3SEric Dumazet 3662234b27c3SEric Dumazet s_h = cb->args[0]; 3663234b27c3SEric Dumazet s_idx = idx = cb->args[1]; 3664234b27c3SEric Dumazet s_ip_idx = ip_idx = cb->args[2]; 3665234b27c3SEric Dumazet 3666234b27c3SEric Dumazet rcu_read_lock(); 3667234b27c3SEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 3668234b27c3SEric Dumazet idx = 0; 3669234b27c3SEric Dumazet head = &net->dev_index_head[h]; 3670234b27c3SEric Dumazet hlist_for_each_entry_rcu(dev, node, head, index_hlist) { 3671234b27c3SEric Dumazet if (idx < s_idx) 3672234b27c3SEric Dumazet goto cont; 36734b97efdfSPatrick McHardy if (h > s_h || idx > s_idx) 3674234b27c3SEric Dumazet s_ip_idx = 0; 3675234b27c3SEric Dumazet ip_idx = 0; 3676e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 3677e21e8467SStephen Hemminger if (!idev) 3678234b27c3SEric Dumazet goto cont; 3679234b27c3SEric Dumazet 3680234b27c3SEric Dumazet if (in6_dump_addrs(idev, skb, cb, type, 3681234b27c3SEric Dumazet s_ip_idx, &ip_idx) <= 0) 3682234b27c3SEric Dumazet goto done; 36837562f876SPavel Emelianov cont: 36847562f876SPavel Emelianov idx++; 36851da177e4SLinus Torvalds } 3686234b27c3SEric Dumazet } 3687234b27c3SEric Dumazet done: 3688234b27c3SEric Dumazet rcu_read_unlock(); 3689234b27c3SEric Dumazet cb->args[0] = h; 3690234b27c3SEric Dumazet cb->args[1] = idx; 3691234b27c3SEric Dumazet cb->args[2] = ip_idx; 3692234b27c3SEric Dumazet 36931da177e4SLinus Torvalds return skb->len; 36941da177e4SLinus Torvalds } 36951da177e4SLinus Torvalds 36961da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 36971da177e4SLinus Torvalds { 36981da177e4SLinus Torvalds enum addr_type_t type = UNICAST_ADDR; 3699b854272bSDenis V. Lunev 37001da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 37011da177e4SLinus Torvalds } 37021da177e4SLinus Torvalds 37031da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) 37041da177e4SLinus Torvalds { 37051da177e4SLinus Torvalds enum addr_type_t type = MULTICAST_ADDR; 3706b854272bSDenis V. Lunev 37071da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 37081da177e4SLinus Torvalds } 37091da177e4SLinus Torvalds 37101da177e4SLinus Torvalds 37111da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) 37121da177e4SLinus Torvalds { 37131da177e4SLinus Torvalds enum addr_type_t type = ANYCAST_ADDR; 3714b854272bSDenis V. Lunev 37151da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 37161da177e4SLinus Torvalds } 37171da177e4SLinus Torvalds 37181b29fc2cSThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh, 37191b29fc2cSThomas Graf void *arg) 37206c223828SNoriaki TAKAMIYA { 37213b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 37221b29fc2cSThomas Graf struct ifaddrmsg *ifm; 37231b29fc2cSThomas Graf struct nlattr *tb[IFA_MAX+1]; 37246c223828SNoriaki TAKAMIYA struct in6_addr *addr = NULL; 37256c223828SNoriaki TAKAMIYA struct net_device *dev = NULL; 37266c223828SNoriaki TAKAMIYA struct inet6_ifaddr *ifa; 37276c223828SNoriaki TAKAMIYA struct sk_buff *skb; 37286c223828SNoriaki TAKAMIYA int err; 37296c223828SNoriaki TAKAMIYA 37301b29fc2cSThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 37311b29fc2cSThomas Graf if (err < 0) 37321b29fc2cSThomas Graf goto errout; 37336c223828SNoriaki TAKAMIYA 37341b29fc2cSThomas Graf addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]); 37351b29fc2cSThomas Graf if (addr == NULL) { 37361b29fc2cSThomas Graf err = -EINVAL; 37371b29fc2cSThomas Graf goto errout; 37381b29fc2cSThomas Graf } 37391b29fc2cSThomas Graf 37401b29fc2cSThomas Graf ifm = nlmsg_data(nlh); 37416c223828SNoriaki TAKAMIYA if (ifm->ifa_index) 37426fda7350SBenjamin Thery dev = __dev_get_by_index(net, ifm->ifa_index); 37436c223828SNoriaki TAKAMIYA 3744e21e8467SStephen Hemminger ifa = ipv6_get_ifaddr(net, addr, dev, 1); 3745e21e8467SStephen Hemminger if (!ifa) { 37461b29fc2cSThomas Graf err = -EADDRNOTAVAIL; 37471b29fc2cSThomas Graf goto errout; 37486c223828SNoriaki TAKAMIYA } 37496c223828SNoriaki TAKAMIYA 3750e21e8467SStephen Hemminger skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL); 3751e21e8467SStephen Hemminger if (!skb) { 37521b29fc2cSThomas Graf err = -ENOBUFS; 37531b29fc2cSThomas Graf goto errout_ifa; 37541b29fc2cSThomas Graf } 37551b29fc2cSThomas Graf 37566c223828SNoriaki TAKAMIYA err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid, 37576c223828SNoriaki TAKAMIYA nlh->nlmsg_seq, RTM_NEWADDR, 0); 375826932566SPatrick McHardy if (err < 0) { 375926932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 376026932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 376126932566SPatrick McHardy kfree_skb(skb); 376226932566SPatrick McHardy goto errout_ifa; 376326932566SPatrick McHardy } 37646fda7350SBenjamin Thery err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid); 37651b29fc2cSThomas Graf errout_ifa: 37666c223828SNoriaki TAKAMIYA in6_ifa_put(ifa); 37671b29fc2cSThomas Graf errout: 37686c223828SNoriaki TAKAMIYA return err; 37696c223828SNoriaki TAKAMIYA } 37706c223828SNoriaki TAKAMIYA 37711da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) 37721da177e4SLinus Torvalds { 37731da177e4SLinus Torvalds struct sk_buff *skb; 3774c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(ifa->idev->dev); 37755d620266SThomas Graf int err = -ENOBUFS; 37761da177e4SLinus Torvalds 37770ab6803bSThomas Graf skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); 37785d620266SThomas Graf if (skb == NULL) 37795d620266SThomas Graf goto errout; 37805d620266SThomas Graf 37815d620266SThomas Graf err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0); 378226932566SPatrick McHardy if (err < 0) { 378326932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 378426932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 378526932566SPatrick McHardy kfree_skb(skb); 378626932566SPatrick McHardy goto errout; 378726932566SPatrick McHardy } 37881ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 37891ce85fe4SPablo Neira Ayuso return; 37905d620266SThomas Graf errout: 37915d620266SThomas Graf if (err < 0) 37926fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); 37931da177e4SLinus Torvalds } 37941da177e4SLinus Torvalds 3795b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf, 37961da177e4SLinus Torvalds __s32 *array, int bytes) 37971da177e4SLinus Torvalds { 379804561c1fSThomas Graf BUG_ON(bytes < (DEVCONF_MAX * 4)); 379904561c1fSThomas Graf 38001da177e4SLinus Torvalds memset(array, 0, bytes); 38011da177e4SLinus Torvalds array[DEVCONF_FORWARDING] = cnf->forwarding; 38021da177e4SLinus Torvalds array[DEVCONF_HOPLIMIT] = cnf->hop_limit; 38031da177e4SLinus Torvalds array[DEVCONF_MTU6] = cnf->mtu6; 38041da177e4SLinus Torvalds array[DEVCONF_ACCEPT_RA] = cnf->accept_ra; 38051da177e4SLinus Torvalds array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects; 38061da177e4SLinus Torvalds array[DEVCONF_AUTOCONF] = cnf->autoconf; 38071da177e4SLinus Torvalds array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits; 38081da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits; 38091da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval; 38101da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay; 38111da177e4SLinus Torvalds array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version; 38121da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 38131da177e4SLinus Torvalds array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; 38141da177e4SLinus Torvalds array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; 38151da177e4SLinus Torvalds array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; 38161da177e4SLinus Torvalds array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; 38171da177e4SLinus Torvalds array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; 38181da177e4SLinus Torvalds #endif 38191da177e4SLinus Torvalds array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; 382065f5c7c1SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; 3821c4fd30ebSYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; 3822930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 3823930d6ff2SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref; 382452e16356SYOSHIFUJI Hideaki array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval; 3825fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 382609c884d4SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen; 382709c884d4SYOSHIFUJI Hideaki #endif 3828930d6ff2SYOSHIFUJI Hideaki #endif 3829fbea49e1SYOSHIFUJI Hideaki array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp; 38300bcbc926SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route; 383195c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 383295c385b4SNeil Horman array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad; 383395c385b4SNeil Horman #endif 38347bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 38357bc570c8SYOSHIFUJI Hideaki array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding; 38367bc570c8SYOSHIFUJI Hideaki #endif 3837778d80beSYOSHIFUJI Hideaki array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6; 38381b34be74SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad; 3839c3faca05SCosmin Ratiu array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao; 38401da177e4SLinus Torvalds } 38411da177e4SLinus Torvalds 3842339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void) 3843339bf98fSThomas Graf { 3844339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 3845339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 3846339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 3847339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 3848339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 3849339bf98fSThomas Graf + nla_total_size( /* IFLA_PROTINFO */ 3850339bf98fSThomas Graf nla_total_size(4) /* IFLA_INET6_FLAGS */ 3851339bf98fSThomas Graf + nla_total_size(sizeof(struct ifla_cacheinfo)) 3852339bf98fSThomas Graf + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */ 3853bf99f1bdSYOSHIFUJI Hideaki + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */ 3854bf99f1bdSYOSHIFUJI Hideaki + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */ 3855339bf98fSThomas Graf ); 3856339bf98fSThomas Graf } 3857c5396a31SYOSHIFUJI Hideaki 38587d720c3eSTejun Heo static inline void __snmp6_fill_stats(u64 *stats, void __percpu **mib, 38597d720c3eSTejun Heo int items, int bytes) 38607f7d9a6bSHerbert Xu { 38617f7d9a6bSHerbert Xu int i; 38627f7d9a6bSHerbert Xu int pad = bytes - sizeof(u64) * items; 38637f7d9a6bSHerbert Xu BUG_ON(pad < 0); 38647f7d9a6bSHerbert Xu 38657f7d9a6bSHerbert Xu /* Use put_unaligned() because stats may not be aligned for u64. */ 38667f7d9a6bSHerbert Xu put_unaligned(items, &stats[0]); 38677f7d9a6bSHerbert Xu for (i = 1; i < items; i++) 38687f7d9a6bSHerbert Xu put_unaligned(snmp_fold_field(mib, i), &stats[i]); 38697f7d9a6bSHerbert Xu 38707f7d9a6bSHerbert Xu memset(&stats[items], 0, pad); 38717f7d9a6bSHerbert Xu } 38727f7d9a6bSHerbert Xu 38734ce3c183SEric Dumazet static inline void __snmp6_fill_stats64(u64 *stats, void __percpu **mib, 38744ce3c183SEric Dumazet int items, int bytes, size_t syncpoff) 38754ce3c183SEric Dumazet { 38764ce3c183SEric Dumazet int i; 38774ce3c183SEric Dumazet int pad = bytes - sizeof(u64) * items; 38784ce3c183SEric Dumazet BUG_ON(pad < 0); 38794ce3c183SEric Dumazet 38804ce3c183SEric Dumazet /* Use put_unaligned() because stats may not be aligned for u64. */ 38814ce3c183SEric Dumazet put_unaligned(items, &stats[0]); 38824ce3c183SEric Dumazet for (i = 1; i < items; i++) 38834ce3c183SEric Dumazet put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]); 38844ce3c183SEric Dumazet 38854ce3c183SEric Dumazet memset(&stats[items], 0, pad); 38864ce3c183SEric Dumazet } 38874ce3c183SEric Dumazet 38887f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype, 38897f7d9a6bSHerbert Xu int bytes) 38907f7d9a6bSHerbert Xu { 38917f7d9a6bSHerbert Xu switch (attrtype) { 38927f7d9a6bSHerbert Xu case IFLA_INET6_STATS: 38934ce3c183SEric Dumazet __snmp6_fill_stats64(stats, (void __percpu **)idev->stats.ipv6, 38944ce3c183SEric Dumazet IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp)); 38957f7d9a6bSHerbert Xu break; 38967f7d9a6bSHerbert Xu case IFLA_INET6_ICMP6STATS: 38977d720c3eSTejun Heo __snmp6_fill_stats(stats, (void __percpu **)idev->stats.icmpv6, ICMP6_MIB_MAX, bytes); 38987f7d9a6bSHerbert Xu break; 38997f7d9a6bSHerbert Xu } 39007f7d9a6bSHerbert Xu } 39017f7d9a6bSHerbert Xu 39021da177e4SLinus Torvalds static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, 3903b6544c0bSJamal Hadi Salim u32 pid, u32 seq, int event, unsigned int flags) 39041da177e4SLinus Torvalds { 39051da177e4SLinus Torvalds struct net_device *dev = idev->dev; 3906bf99f1bdSYOSHIFUJI Hideaki struct nlattr *nla; 390704561c1fSThomas Graf struct ifinfomsg *hdr; 39081da177e4SLinus Torvalds struct nlmsghdr *nlh; 390904561c1fSThomas Graf void *protoinfo; 39101da177e4SLinus Torvalds struct ifla_cacheinfo ci; 39111da177e4SLinus Torvalds 391204561c1fSThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags); 391304561c1fSThomas Graf if (nlh == NULL) 391426932566SPatrick McHardy return -EMSGSIZE; 39151da177e4SLinus Torvalds 391604561c1fSThomas Graf hdr = nlmsg_data(nlh); 391704561c1fSThomas Graf hdr->ifi_family = AF_INET6; 391804561c1fSThomas Graf hdr->__ifi_pad = 0; 391904561c1fSThomas Graf hdr->ifi_type = dev->type; 392004561c1fSThomas Graf hdr->ifi_index = dev->ifindex; 392104561c1fSThomas Graf hdr->ifi_flags = dev_get_flags(dev); 392204561c1fSThomas Graf hdr->ifi_change = 0; 392304561c1fSThomas Graf 392404561c1fSThomas Graf NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name); 39251da177e4SLinus Torvalds 39261da177e4SLinus Torvalds if (dev->addr_len) 392704561c1fSThomas Graf NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr); 39281da177e4SLinus Torvalds 392904561c1fSThomas Graf NLA_PUT_U32(skb, IFLA_MTU, dev->mtu); 39301da177e4SLinus Torvalds if (dev->ifindex != dev->iflink) 393104561c1fSThomas Graf NLA_PUT_U32(skb, IFLA_LINK, dev->iflink); 39321da177e4SLinus Torvalds 393304561c1fSThomas Graf protoinfo = nla_nest_start(skb, IFLA_PROTINFO); 393404561c1fSThomas Graf if (protoinfo == NULL) 393504561c1fSThomas Graf goto nla_put_failure; 39361da177e4SLinus Torvalds 393704561c1fSThomas Graf NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags); 39381da177e4SLinus Torvalds 39391da177e4SLinus Torvalds ci.max_reasm_len = IPV6_MAXPLEN; 39401da177e4SLinus Torvalds ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100 39411da177e4SLinus Torvalds + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ); 39421da177e4SLinus Torvalds ci.reachable_time = idev->nd_parms->reachable_time; 39431da177e4SLinus Torvalds ci.retrans_time = idev->nd_parms->retrans_time; 394404561c1fSThomas Graf NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci); 39451da177e4SLinus Torvalds 3946bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); 3947bf99f1bdSYOSHIFUJI Hideaki if (nla == NULL) 394804561c1fSThomas Graf goto nla_put_failure; 3949bf99f1bdSYOSHIFUJI Hideaki ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); 39501da177e4SLinus Torvalds 3951bf99f1bdSYOSHIFUJI Hideaki /* XXX - MC not implemented */ 3952bf99f1bdSYOSHIFUJI Hideaki 3953bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); 3954bf99f1bdSYOSHIFUJI Hideaki if (nla == NULL) 3955bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 3956bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); 3957bf99f1bdSYOSHIFUJI Hideaki 3958bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); 3959bf99f1bdSYOSHIFUJI Hideaki if (nla == NULL) 3960bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 3961bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); 39621da177e4SLinus Torvalds 396304561c1fSThomas Graf nla_nest_end(skb, protoinfo); 396404561c1fSThomas Graf return nlmsg_end(skb, nlh); 39651da177e4SLinus Torvalds 396604561c1fSThomas Graf nla_put_failure: 396726932566SPatrick McHardy nlmsg_cancel(skb, nlh); 396826932566SPatrick McHardy return -EMSGSIZE; 39691da177e4SLinus Torvalds } 39701da177e4SLinus Torvalds 39711da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 39721da177e4SLinus Torvalds { 39733b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 397484d2697dSEric Dumazet int h, s_h; 3975434a8a58SDavid S. Miller int idx = 0, s_idx; 39761da177e4SLinus Torvalds struct net_device *dev; 39771da177e4SLinus Torvalds struct inet6_dev *idev; 397884d2697dSEric Dumazet struct hlist_head *head; 397984d2697dSEric Dumazet struct hlist_node *node; 39801da177e4SLinus Torvalds 398184d2697dSEric Dumazet s_h = cb->args[0]; 398284d2697dSEric Dumazet s_idx = cb->args[1]; 398384d2697dSEric Dumazet 398484d2697dSEric Dumazet rcu_read_lock(); 398584d2697dSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 39867562f876SPavel Emelianov idx = 0; 398784d2697dSEric Dumazet head = &net->dev_index_head[h]; 398884d2697dSEric Dumazet hlist_for_each_entry_rcu(dev, node, head, index_hlist) { 39891da177e4SLinus Torvalds if (idx < s_idx) 39907562f876SPavel Emelianov goto cont; 399184d2697dSEric Dumazet idev = __in6_dev_get(dev); 399284d2697dSEric Dumazet if (!idev) 39937562f876SPavel Emelianov goto cont; 399484d2697dSEric Dumazet if (inet6_fill_ifinfo(skb, idev, 399584d2697dSEric Dumazet NETLINK_CB(cb->skb).pid, 399684d2697dSEric Dumazet cb->nlh->nlmsg_seq, 399784d2697dSEric Dumazet RTM_NEWLINK, NLM_F_MULTI) <= 0) 399884d2697dSEric Dumazet goto out; 39997562f876SPavel Emelianov cont: 40007562f876SPavel Emelianov idx++; 40011da177e4SLinus Torvalds } 400284d2697dSEric Dumazet } 400384d2697dSEric Dumazet out: 400484d2697dSEric Dumazet rcu_read_unlock(); 400584d2697dSEric Dumazet cb->args[1] = idx; 400684d2697dSEric Dumazet cb->args[0] = h; 40071da177e4SLinus Torvalds 40081da177e4SLinus Torvalds return skb->len; 40091da177e4SLinus Torvalds } 40101da177e4SLinus Torvalds 40111da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev) 40121da177e4SLinus Torvalds { 40131da177e4SLinus Torvalds struct sk_buff *skb; 4014c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 40158d7a76c9SThomas Graf int err = -ENOBUFS; 40161da177e4SLinus Torvalds 4017339bf98fSThomas Graf skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); 40188d7a76c9SThomas Graf if (skb == NULL) 40198d7a76c9SThomas Graf goto errout; 40208d7a76c9SThomas Graf 40218d7a76c9SThomas Graf err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); 402226932566SPatrick McHardy if (err < 0) { 402326932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */ 402426932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 402526932566SPatrick McHardy kfree_skb(skb); 402626932566SPatrick McHardy goto errout; 402726932566SPatrick McHardy } 40281ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 40291ce85fe4SPablo Neira Ayuso return; 40308d7a76c9SThomas Graf errout: 40318d7a76c9SThomas Graf if (err < 0) 40326fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); 40331da177e4SLinus Torvalds } 40341da177e4SLinus Torvalds 4035339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void) 4036339bf98fSThomas Graf { 4037339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct prefixmsg)) 4038339bf98fSThomas Graf + nla_total_size(sizeof(struct in6_addr)) 4039339bf98fSThomas Graf + nla_total_size(sizeof(struct prefix_cacheinfo)); 4040339bf98fSThomas Graf } 4041c5396a31SYOSHIFUJI Hideaki 40421da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, 4043b6544c0bSJamal Hadi Salim struct prefix_info *pinfo, u32 pid, u32 seq, 4044b6544c0bSJamal Hadi Salim int event, unsigned int flags) 40451da177e4SLinus Torvalds { 40461da177e4SLinus Torvalds struct prefixmsg *pmsg; 40471da177e4SLinus Torvalds struct nlmsghdr *nlh; 40481da177e4SLinus Torvalds struct prefix_cacheinfo ci; 40491da177e4SLinus Torvalds 40506051e2f4SThomas Graf nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags); 40516051e2f4SThomas Graf if (nlh == NULL) 405226932566SPatrick McHardy return -EMSGSIZE; 40536051e2f4SThomas Graf 40546051e2f4SThomas Graf pmsg = nlmsg_data(nlh); 40551da177e4SLinus Torvalds pmsg->prefix_family = AF_INET6; 40568a47077aSPatrick McHardy pmsg->prefix_pad1 = 0; 40578a47077aSPatrick McHardy pmsg->prefix_pad2 = 0; 40581da177e4SLinus Torvalds pmsg->prefix_ifindex = idev->dev->ifindex; 40591da177e4SLinus Torvalds pmsg->prefix_len = pinfo->prefix_len; 40601da177e4SLinus Torvalds pmsg->prefix_type = pinfo->type; 40618a47077aSPatrick McHardy pmsg->prefix_pad3 = 0; 40621da177e4SLinus Torvalds pmsg->prefix_flags = 0; 40631da177e4SLinus Torvalds if (pinfo->onlink) 40641da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_ONLINK; 40651da177e4SLinus Torvalds if (pinfo->autoconf) 40661da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_AUTOCONF; 40671da177e4SLinus Torvalds 40686051e2f4SThomas Graf NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix); 40691da177e4SLinus Torvalds 40701da177e4SLinus Torvalds ci.preferred_time = ntohl(pinfo->prefered); 40711da177e4SLinus Torvalds ci.valid_time = ntohl(pinfo->valid); 40726051e2f4SThomas Graf NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci); 40731da177e4SLinus Torvalds 40746051e2f4SThomas Graf return nlmsg_end(skb, nlh); 40751da177e4SLinus Torvalds 40766051e2f4SThomas Graf nla_put_failure: 407726932566SPatrick McHardy nlmsg_cancel(skb, nlh); 407826932566SPatrick McHardy return -EMSGSIZE; 40791da177e4SLinus Torvalds } 40801da177e4SLinus Torvalds 40811da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 40821da177e4SLinus Torvalds struct prefix_info *pinfo) 40831da177e4SLinus Torvalds { 40841da177e4SLinus Torvalds struct sk_buff *skb; 4085c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 40868c384bfaSThomas Graf int err = -ENOBUFS; 40871da177e4SLinus Torvalds 4088339bf98fSThomas Graf skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); 40898c384bfaSThomas Graf if (skb == NULL) 40908c384bfaSThomas Graf goto errout; 40918c384bfaSThomas Graf 40928c384bfaSThomas Graf err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); 409326932566SPatrick McHardy if (err < 0) { 409426932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */ 409526932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 409626932566SPatrick McHardy kfree_skb(skb); 409726932566SPatrick McHardy goto errout; 409826932566SPatrick McHardy } 40991ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); 41001ce85fe4SPablo Neira Ayuso return; 41018c384bfaSThomas Graf errout: 41028c384bfaSThomas Graf if (err < 0) 41036fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err); 41041da177e4SLinus Torvalds } 41051da177e4SLinus Torvalds 41061da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 41071da177e4SLinus Torvalds { 41081da177e4SLinus Torvalds inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); 41091da177e4SLinus Torvalds 41101da177e4SLinus Torvalds switch (event) { 41111da177e4SLinus Torvalds case RTM_NEWADDR: 411295c385b4SNeil Horman /* 411395c385b4SNeil Horman * If the address was optimistic 411495c385b4SNeil Horman * we inserted the route at the start of 411595c385b4SNeil Horman * our DAD process, so we don't need 411695c385b4SNeil Horman * to do it again 411795c385b4SNeil Horman */ 411895c385b4SNeil Horman if (!(ifp->rt->rt6i_node)) 411940e22e8fSThomas Graf ip6_ins_rt(ifp->rt); 41201da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 41211da177e4SLinus Torvalds addrconf_join_anycast(ifp); 41221da177e4SLinus Torvalds break; 41231da177e4SLinus Torvalds case RTM_DELADDR: 41241da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 41251da177e4SLinus Torvalds addrconf_leave_anycast(ifp); 41261da177e4SLinus Torvalds addrconf_leave_solict(ifp->idev, &ifp->addr); 4127d8d1f30bSChangli Gao dst_hold(&ifp->rt->dst); 412893fa159aSstephen hemminger 4129e9d3e084SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD && 4130e9d3e084SHerbert Xu ip6_del_rt(ifp->rt)) 4131d8d1f30bSChangli Gao dst_free(&ifp->rt->dst); 41321da177e4SLinus Torvalds break; 41331da177e4SLinus Torvalds } 41341da177e4SLinus Torvalds } 41351da177e4SLinus Torvalds 41361da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 41371da177e4SLinus Torvalds { 41388814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 41391da177e4SLinus Torvalds if (likely(ifp->idev->dead == 0)) 41401da177e4SLinus Torvalds __ipv6_ifa_notify(event, ifp); 41418814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 41421da177e4SLinus Torvalds } 41431da177e4SLinus Torvalds 41441da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 41451da177e4SLinus Torvalds 41461da177e4SLinus Torvalds static 41478d65af78SAlexey Dobriyan int addrconf_sysctl_forward(ctl_table *ctl, int write, 41481da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 41491da177e4SLinus Torvalds { 41501da177e4SLinus Torvalds int *valp = ctl->data; 41511da177e4SLinus Torvalds int val = *valp; 415288af182eSEric W. Biederman loff_t pos = *ppos; 41531da177e4SLinus Torvalds int ret; 41541da177e4SLinus Torvalds 41558d65af78SAlexey Dobriyan ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 41561da177e4SLinus Torvalds 4157c8fecf22SPavel Emelyanov if (write) 4158b325fddbSStephen Hemminger ret = addrconf_fixup_forwarding(ctl, valp, val); 415988af182eSEric W. Biederman if (ret) 416088af182eSEric W. Biederman *ppos = pos; 41611da177e4SLinus Torvalds return ret; 41621da177e4SLinus Torvalds } 41631da177e4SLinus Torvalds 416456d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev) 416556d417b1SBrian Haley { 416656d417b1SBrian Haley if (!idev || !idev->dev) 416756d417b1SBrian Haley return; 416856d417b1SBrian Haley 416956d417b1SBrian Haley if (idev->cnf.disable_ipv6) 417056d417b1SBrian Haley addrconf_notify(NULL, NETDEV_DOWN, idev->dev); 417156d417b1SBrian Haley else 417256d417b1SBrian Haley addrconf_notify(NULL, NETDEV_UP, idev->dev); 417356d417b1SBrian Haley } 417456d417b1SBrian Haley 417556d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf) 417656d417b1SBrian Haley { 417756d417b1SBrian Haley struct net_device *dev; 417856d417b1SBrian Haley struct inet6_dev *idev; 417956d417b1SBrian Haley 418056d417b1SBrian Haley rcu_read_lock(); 4181c6d14c84SEric Dumazet for_each_netdev_rcu(net, dev) { 418256d417b1SBrian Haley idev = __in6_dev_get(dev); 418356d417b1SBrian Haley if (idev) { 418456d417b1SBrian Haley int changed = (!idev->cnf.disable_ipv6) ^ (!newf); 418556d417b1SBrian Haley idev->cnf.disable_ipv6 = newf; 418656d417b1SBrian Haley if (changed) 418756d417b1SBrian Haley dev_disable_change(idev); 418856d417b1SBrian Haley } 418956d417b1SBrian Haley } 4190c6d14c84SEric Dumazet rcu_read_unlock(); 419156d417b1SBrian Haley } 419256d417b1SBrian Haley 419356d417b1SBrian Haley static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int old) 419456d417b1SBrian Haley { 419556d417b1SBrian Haley struct net *net; 419656d417b1SBrian Haley 419756d417b1SBrian Haley net = (struct net *)table->extra2; 419856d417b1SBrian Haley 419956d417b1SBrian Haley if (p == &net->ipv6.devconf_dflt->disable_ipv6) 420056d417b1SBrian Haley return 0; 420156d417b1SBrian Haley 420288af182eSEric W. Biederman if (!rtnl_trylock()) { 420388af182eSEric W. Biederman /* Restore the original values before restarting */ 420488af182eSEric W. Biederman *p = old; 420556d417b1SBrian Haley return restart_syscall(); 420688af182eSEric W. Biederman } 420756d417b1SBrian Haley 420856d417b1SBrian Haley if (p == &net->ipv6.devconf_all->disable_ipv6) { 420956d417b1SBrian Haley __s32 newf = net->ipv6.devconf_all->disable_ipv6; 421056d417b1SBrian Haley net->ipv6.devconf_dflt->disable_ipv6 = newf; 421156d417b1SBrian Haley addrconf_disable_change(net, newf); 421256d417b1SBrian Haley } else if ((!*p) ^ (!old)) 421356d417b1SBrian Haley dev_disable_change((struct inet6_dev *)table->extra1); 421456d417b1SBrian Haley 421556d417b1SBrian Haley rtnl_unlock(); 421656d417b1SBrian Haley return 0; 421756d417b1SBrian Haley } 421856d417b1SBrian Haley 421956d417b1SBrian Haley static 42208d65af78SAlexey Dobriyan int addrconf_sysctl_disable(ctl_table *ctl, int write, 422156d417b1SBrian Haley void __user *buffer, size_t *lenp, loff_t *ppos) 422256d417b1SBrian Haley { 422356d417b1SBrian Haley int *valp = ctl->data; 422456d417b1SBrian Haley int val = *valp; 422588af182eSEric W. Biederman loff_t pos = *ppos; 422656d417b1SBrian Haley int ret; 422756d417b1SBrian Haley 42288d65af78SAlexey Dobriyan ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 422956d417b1SBrian Haley 423056d417b1SBrian Haley if (write) 423156d417b1SBrian Haley ret = addrconf_disable_ipv6(ctl, valp, val); 423288af182eSEric W. Biederman if (ret) 423388af182eSEric W. Biederman *ppos = pos; 423456d417b1SBrian Haley return ret; 423556d417b1SBrian Haley } 423656d417b1SBrian Haley 42371da177e4SLinus Torvalds static struct addrconf_sysctl_table 42381da177e4SLinus Torvalds { 42391da177e4SLinus Torvalds struct ctl_table_header *sysctl_header; 4240f6a07b29SYOSHIFUJI Hideaki ctl_table addrconf_vars[DEVCONF_MAX+1]; 42411dab6222SPavel Emelyanov char *dev_name; 4242ab32ea5dSBrian Haley } addrconf_sysctl __read_mostly = { 42431da177e4SLinus Torvalds .sysctl_header = NULL, 42441da177e4SLinus Torvalds .addrconf_vars = { 42451da177e4SLinus Torvalds { 42461da177e4SLinus Torvalds .procname = "forwarding", 42471da177e4SLinus Torvalds .data = &ipv6_devconf.forwarding, 42481da177e4SLinus Torvalds .maxlen = sizeof(int), 42491da177e4SLinus Torvalds .mode = 0644, 42506d9f239aSAlexey Dobriyan .proc_handler = addrconf_sysctl_forward, 42511da177e4SLinus Torvalds }, 42521da177e4SLinus Torvalds { 42531da177e4SLinus Torvalds .procname = "hop_limit", 42541da177e4SLinus Torvalds .data = &ipv6_devconf.hop_limit, 42551da177e4SLinus Torvalds .maxlen = sizeof(int), 42561da177e4SLinus Torvalds .mode = 0644, 42571da177e4SLinus Torvalds .proc_handler = proc_dointvec, 42581da177e4SLinus Torvalds }, 42591da177e4SLinus Torvalds { 42601da177e4SLinus Torvalds .procname = "mtu", 42611da177e4SLinus Torvalds .data = &ipv6_devconf.mtu6, 42621da177e4SLinus Torvalds .maxlen = sizeof(int), 42631da177e4SLinus Torvalds .mode = 0644, 42646d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 42651da177e4SLinus Torvalds }, 42661da177e4SLinus Torvalds { 42671da177e4SLinus Torvalds .procname = "accept_ra", 42681da177e4SLinus Torvalds .data = &ipv6_devconf.accept_ra, 42691da177e4SLinus Torvalds .maxlen = sizeof(int), 42701da177e4SLinus Torvalds .mode = 0644, 42716d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 42721da177e4SLinus Torvalds }, 42731da177e4SLinus Torvalds { 42741da177e4SLinus Torvalds .procname = "accept_redirects", 42751da177e4SLinus Torvalds .data = &ipv6_devconf.accept_redirects, 42761da177e4SLinus Torvalds .maxlen = sizeof(int), 42771da177e4SLinus Torvalds .mode = 0644, 42786d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 42791da177e4SLinus Torvalds }, 42801da177e4SLinus Torvalds { 42811da177e4SLinus Torvalds .procname = "autoconf", 42821da177e4SLinus Torvalds .data = &ipv6_devconf.autoconf, 42831da177e4SLinus Torvalds .maxlen = sizeof(int), 42841da177e4SLinus Torvalds .mode = 0644, 42856d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 42861da177e4SLinus Torvalds }, 42871da177e4SLinus Torvalds { 42881da177e4SLinus Torvalds .procname = "dad_transmits", 42891da177e4SLinus Torvalds .data = &ipv6_devconf.dad_transmits, 42901da177e4SLinus Torvalds .maxlen = sizeof(int), 42911da177e4SLinus Torvalds .mode = 0644, 42926d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 42931da177e4SLinus Torvalds }, 42941da177e4SLinus Torvalds { 42951da177e4SLinus Torvalds .procname = "router_solicitations", 42961da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicits, 42971da177e4SLinus Torvalds .maxlen = sizeof(int), 42981da177e4SLinus Torvalds .mode = 0644, 42996d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 43001da177e4SLinus Torvalds }, 43011da177e4SLinus Torvalds { 43021da177e4SLinus Torvalds .procname = "router_solicitation_interval", 43031da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_interval, 43041da177e4SLinus Torvalds .maxlen = sizeof(int), 43051da177e4SLinus Torvalds .mode = 0644, 43066d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 43071da177e4SLinus Torvalds }, 43081da177e4SLinus Torvalds { 43091da177e4SLinus Torvalds .procname = "router_solicitation_delay", 43101da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_delay, 43111da177e4SLinus Torvalds .maxlen = sizeof(int), 43121da177e4SLinus Torvalds .mode = 0644, 43136d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 43141da177e4SLinus Torvalds }, 43151da177e4SLinus Torvalds { 43161da177e4SLinus Torvalds .procname = "force_mld_version", 43171da177e4SLinus Torvalds .data = &ipv6_devconf.force_mld_version, 43181da177e4SLinus Torvalds .maxlen = sizeof(int), 43191da177e4SLinus Torvalds .mode = 0644, 43206d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 43211da177e4SLinus Torvalds }, 43221da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 43231da177e4SLinus Torvalds { 43241da177e4SLinus Torvalds .procname = "use_tempaddr", 43251da177e4SLinus Torvalds .data = &ipv6_devconf.use_tempaddr, 43261da177e4SLinus Torvalds .maxlen = sizeof(int), 43271da177e4SLinus Torvalds .mode = 0644, 43286d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 43291da177e4SLinus Torvalds }, 43301da177e4SLinus Torvalds { 43311da177e4SLinus Torvalds .procname = "temp_valid_lft", 43321da177e4SLinus Torvalds .data = &ipv6_devconf.temp_valid_lft, 43331da177e4SLinus Torvalds .maxlen = sizeof(int), 43341da177e4SLinus Torvalds .mode = 0644, 43356d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 43361da177e4SLinus Torvalds }, 43371da177e4SLinus Torvalds { 43381da177e4SLinus Torvalds .procname = "temp_prefered_lft", 43391da177e4SLinus Torvalds .data = &ipv6_devconf.temp_prefered_lft, 43401da177e4SLinus Torvalds .maxlen = sizeof(int), 43411da177e4SLinus Torvalds .mode = 0644, 43426d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 43431da177e4SLinus Torvalds }, 43441da177e4SLinus Torvalds { 43451da177e4SLinus Torvalds .procname = "regen_max_retry", 43461da177e4SLinus Torvalds .data = &ipv6_devconf.regen_max_retry, 43471da177e4SLinus Torvalds .maxlen = sizeof(int), 43481da177e4SLinus Torvalds .mode = 0644, 43496d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 43501da177e4SLinus Torvalds }, 43511da177e4SLinus Torvalds { 43521da177e4SLinus Torvalds .procname = "max_desync_factor", 43531da177e4SLinus Torvalds .data = &ipv6_devconf.max_desync_factor, 43541da177e4SLinus Torvalds .maxlen = sizeof(int), 43551da177e4SLinus Torvalds .mode = 0644, 43566d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 43571da177e4SLinus Torvalds }, 43581da177e4SLinus Torvalds #endif 43591da177e4SLinus Torvalds { 43601da177e4SLinus Torvalds .procname = "max_addresses", 43611da177e4SLinus Torvalds .data = &ipv6_devconf.max_addresses, 43621da177e4SLinus Torvalds .maxlen = sizeof(int), 43631da177e4SLinus Torvalds .mode = 0644, 43646d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 43651da177e4SLinus Torvalds }, 43661da177e4SLinus Torvalds { 436765f5c7c1SYOSHIFUJI Hideaki .procname = "accept_ra_defrtr", 436865f5c7c1SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_defrtr, 436965f5c7c1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 437065f5c7c1SYOSHIFUJI Hideaki .mode = 0644, 43716d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 437265f5c7c1SYOSHIFUJI Hideaki }, 437365f5c7c1SYOSHIFUJI Hideaki { 4374c4fd30ebSYOSHIFUJI Hideaki .procname = "accept_ra_pinfo", 4375c4fd30ebSYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_pinfo, 4376c4fd30ebSYOSHIFUJI Hideaki .maxlen = sizeof(int), 4377c4fd30ebSYOSHIFUJI Hideaki .mode = 0644, 43786d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 4379c4fd30ebSYOSHIFUJI Hideaki }, 4380930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 4381930d6ff2SYOSHIFUJI Hideaki { 4382930d6ff2SYOSHIFUJI Hideaki .procname = "accept_ra_rtr_pref", 4383930d6ff2SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rtr_pref, 4384930d6ff2SYOSHIFUJI Hideaki .maxlen = sizeof(int), 4385930d6ff2SYOSHIFUJI Hideaki .mode = 0644, 43866d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 4387930d6ff2SYOSHIFUJI Hideaki }, 438852e16356SYOSHIFUJI Hideaki { 438952e16356SYOSHIFUJI Hideaki .procname = "router_probe_interval", 439052e16356SYOSHIFUJI Hideaki .data = &ipv6_devconf.rtr_probe_interval, 439152e16356SYOSHIFUJI Hideaki .maxlen = sizeof(int), 439252e16356SYOSHIFUJI Hideaki .mode = 0644, 43936d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 439452e16356SYOSHIFUJI Hideaki }, 4395fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 439609c884d4SYOSHIFUJI Hideaki { 439709c884d4SYOSHIFUJI Hideaki .procname = "accept_ra_rt_info_max_plen", 439809c884d4SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rt_info_max_plen, 439909c884d4SYOSHIFUJI Hideaki .maxlen = sizeof(int), 440009c884d4SYOSHIFUJI Hideaki .mode = 0644, 44016d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 440209c884d4SYOSHIFUJI Hideaki }, 440309c884d4SYOSHIFUJI Hideaki #endif 4404930d6ff2SYOSHIFUJI Hideaki #endif 4405c4fd30ebSYOSHIFUJI Hideaki { 4406fbea49e1SYOSHIFUJI Hideaki .procname = "proxy_ndp", 4407fbea49e1SYOSHIFUJI Hideaki .data = &ipv6_devconf.proxy_ndp, 4408fbea49e1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 4409fbea49e1SYOSHIFUJI Hideaki .mode = 0644, 44106d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 4411fbea49e1SYOSHIFUJI Hideaki }, 4412fbea49e1SYOSHIFUJI Hideaki { 44130bcbc926SYOSHIFUJI Hideaki .procname = "accept_source_route", 44140bcbc926SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_source_route, 44150bcbc926SYOSHIFUJI Hideaki .maxlen = sizeof(int), 44160bcbc926SYOSHIFUJI Hideaki .mode = 0644, 44176d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 44180bcbc926SYOSHIFUJI Hideaki }, 441995c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 442095c385b4SNeil Horman { 442195c385b4SNeil Horman .procname = "optimistic_dad", 442295c385b4SNeil Horman .data = &ipv6_devconf.optimistic_dad, 442395c385b4SNeil Horman .maxlen = sizeof(int), 442495c385b4SNeil Horman .mode = 0644, 44256d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 442695c385b4SNeil Horman 442795c385b4SNeil Horman }, 442895c385b4SNeil Horman #endif 44297bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 44307bc570c8SYOSHIFUJI Hideaki { 44317bc570c8SYOSHIFUJI Hideaki .procname = "mc_forwarding", 44327bc570c8SYOSHIFUJI Hideaki .data = &ipv6_devconf.mc_forwarding, 44337bc570c8SYOSHIFUJI Hideaki .maxlen = sizeof(int), 4434a4e6db07SDavid S. Miller .mode = 0444, 44356d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 44367bc570c8SYOSHIFUJI Hideaki }, 44377bc570c8SYOSHIFUJI Hideaki #endif 44380bcbc926SYOSHIFUJI Hideaki { 4439778d80beSYOSHIFUJI Hideaki .procname = "disable_ipv6", 4440778d80beSYOSHIFUJI Hideaki .data = &ipv6_devconf.disable_ipv6, 4441778d80beSYOSHIFUJI Hideaki .maxlen = sizeof(int), 4442778d80beSYOSHIFUJI Hideaki .mode = 0644, 444356d417b1SBrian Haley .proc_handler = addrconf_sysctl_disable, 4444778d80beSYOSHIFUJI Hideaki }, 4445778d80beSYOSHIFUJI Hideaki { 44461b34be74SYOSHIFUJI Hideaki .procname = "accept_dad", 44471b34be74SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_dad, 44481b34be74SYOSHIFUJI Hideaki .maxlen = sizeof(int), 44491b34be74SYOSHIFUJI Hideaki .mode = 0644, 44506d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 44511b34be74SYOSHIFUJI Hideaki }, 44521b34be74SYOSHIFUJI Hideaki { 4453f7734fdfSOctavian Purdila .procname = "force_tllao", 4454f7734fdfSOctavian Purdila .data = &ipv6_devconf.force_tllao, 4455f7734fdfSOctavian Purdila .maxlen = sizeof(int), 4456f7734fdfSOctavian Purdila .mode = 0644, 4457f7734fdfSOctavian Purdila .proc_handler = proc_dointvec 4458f7734fdfSOctavian Purdila }, 4459f7734fdfSOctavian Purdila { 4460f8572d8fSEric W. Biederman /* sentinel */ 44611da177e4SLinus Torvalds } 44621da177e4SLinus Torvalds }, 44631da177e4SLinus Torvalds }; 44641da177e4SLinus Torvalds 4465bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name, 4466f8572d8fSEric W. Biederman struct inet6_dev *idev, struct ipv6_devconf *p) 44671da177e4SLinus Torvalds { 44681da177e4SLinus Torvalds int i; 44691da177e4SLinus Torvalds struct addrconf_sysctl_table *t; 44701da177e4SLinus Torvalds 44711dab6222SPavel Emelyanov #define ADDRCONF_CTL_PATH_DEV 3 44721dab6222SPavel Emelyanov 44731dab6222SPavel Emelyanov struct ctl_path addrconf_ctl_path[] = { 4474f8572d8fSEric W. Biederman { .procname = "net", }, 4475f8572d8fSEric W. Biederman { .procname = "ipv6", }, 4476f8572d8fSEric W. Biederman { .procname = "conf", }, 44771dab6222SPavel Emelyanov { /* to be set */ }, 44781dab6222SPavel Emelyanov { }, 44791dab6222SPavel Emelyanov }; 44801dab6222SPavel Emelyanov 44811dab6222SPavel Emelyanov 4482af879cc7SArnaldo Carvalho de Melo t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL); 44831da177e4SLinus Torvalds if (t == NULL) 4484f68635e6SPavel Emelyanov goto out; 4485f68635e6SPavel Emelyanov 44861da177e4SLinus Torvalds for (i = 0; t->addrconf_vars[i].data; i++) { 44871da177e4SLinus Torvalds t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf; 44881da177e4SLinus Torvalds t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */ 4489bff16c2fSPavel Emelyanov t->addrconf_vars[i].extra2 = net; 44901da177e4SLinus Torvalds } 44911da177e4SLinus Torvalds 44921da177e4SLinus Torvalds /* 44931da177e4SLinus Torvalds * Make a copy of dev_name, because '.procname' is regarded as const 44941da177e4SLinus Torvalds * by sysctl and we wouldn't want anyone to change it under our feet 44951da177e4SLinus Torvalds * (see SIOCSIFNAME). 44961da177e4SLinus Torvalds */ 44971dab6222SPavel Emelyanov t->dev_name = kstrdup(dev_name, GFP_KERNEL); 44981dab6222SPavel Emelyanov if (!t->dev_name) 44991da177e4SLinus Torvalds goto free; 45001da177e4SLinus Torvalds 45011dab6222SPavel Emelyanov addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].procname = t->dev_name; 45021da177e4SLinus Torvalds 4503bff16c2fSPavel Emelyanov t->sysctl_header = register_net_sysctl_table(net, addrconf_ctl_path, 45041dab6222SPavel Emelyanov t->addrconf_vars); 45051da177e4SLinus Torvalds if (t->sysctl_header == NULL) 45061da177e4SLinus Torvalds goto free_procname; 4507f68635e6SPavel Emelyanov 45081da177e4SLinus Torvalds p->sysctl = t; 450995897312SPavel Emelyanov return 0; 45101da177e4SLinus Torvalds 45111da177e4SLinus Torvalds free_procname: 45121dab6222SPavel Emelyanov kfree(t->dev_name); 45131da177e4SLinus Torvalds free: 45141da177e4SLinus Torvalds kfree(t); 4515f68635e6SPavel Emelyanov out: 451695897312SPavel Emelyanov return -ENOBUFS; 45171da177e4SLinus Torvalds } 45181da177e4SLinus Torvalds 4519408c4768SPavel Emelyanov static void __addrconf_sysctl_unregister(struct ipv6_devconf *p) 4520f52295a9SPavel Emelyanov { 4521408c4768SPavel Emelyanov struct addrconf_sysctl_table *t; 4522f52295a9SPavel Emelyanov 4523408c4768SPavel Emelyanov if (p->sysctl == NULL) 4524408c4768SPavel Emelyanov return; 4525408c4768SPavel Emelyanov 4526408c4768SPavel Emelyanov t = p->sysctl; 45271da177e4SLinus Torvalds p->sysctl = NULL; 45281da177e4SLinus Torvalds unregister_sysctl_table(t->sysctl_header); 45291dab6222SPavel Emelyanov kfree(t->dev_name); 45301da177e4SLinus Torvalds kfree(t); 45311da177e4SLinus Torvalds } 4532408c4768SPavel Emelyanov 4533408c4768SPavel Emelyanov static void addrconf_sysctl_register(struct inet6_dev *idev) 4534408c4768SPavel Emelyanov { 453554716e3bSEric W. Biederman neigh_sysctl_register(idev->dev, idev->nd_parms, "ipv6", 4536f8572d8fSEric W. Biederman &ndisc_ifinfo_sysctl_change); 4537c346dca1SYOSHIFUJI Hideaki __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name, 4538f8572d8fSEric W. Biederman idev, &idev->cnf); 4539408c4768SPavel Emelyanov } 4540408c4768SPavel Emelyanov 4541408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev) 4542408c4768SPavel Emelyanov { 4543408c4768SPavel Emelyanov __addrconf_sysctl_unregister(&idev->cnf); 4544408c4768SPavel Emelyanov neigh_sysctl_unregister(idev->nd_parms); 45451da177e4SLinus Torvalds } 45461da177e4SLinus Torvalds 45471da177e4SLinus Torvalds 45481da177e4SLinus Torvalds #endif 45491da177e4SLinus Torvalds 45502c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net) 4551e0da5a48SPavel Emelyanov { 4552e0da5a48SPavel Emelyanov int err; 4553e0da5a48SPavel Emelyanov struct ipv6_devconf *all, *dflt; 4554e0da5a48SPavel Emelyanov 4555e0da5a48SPavel Emelyanov err = -ENOMEM; 4556e0da5a48SPavel Emelyanov all = &ipv6_devconf; 4557e0da5a48SPavel Emelyanov dflt = &ipv6_devconf_dflt; 4558e0da5a48SPavel Emelyanov 455909ad9bc7SOctavian Purdila if (!net_eq(net, &init_net)) { 4560e0da5a48SPavel Emelyanov all = kmemdup(all, sizeof(ipv6_devconf), GFP_KERNEL); 4561e0da5a48SPavel Emelyanov if (all == NULL) 4562e0da5a48SPavel Emelyanov goto err_alloc_all; 4563e0da5a48SPavel Emelyanov 4564e0da5a48SPavel Emelyanov dflt = kmemdup(dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); 4565e0da5a48SPavel Emelyanov if (dflt == NULL) 4566e0da5a48SPavel Emelyanov goto err_alloc_dflt; 456756d417b1SBrian Haley } else { 456856d417b1SBrian Haley /* these will be inherited by all namespaces */ 456956d417b1SBrian Haley dflt->autoconf = ipv6_defaults.autoconf; 457056d417b1SBrian Haley dflt->disable_ipv6 = ipv6_defaults.disable_ipv6; 4571e0da5a48SPavel Emelyanov } 4572e0da5a48SPavel Emelyanov 4573e0da5a48SPavel Emelyanov net->ipv6.devconf_all = all; 4574e0da5a48SPavel Emelyanov net->ipv6.devconf_dflt = dflt; 4575e0da5a48SPavel Emelyanov 4576e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 4577f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "all", NULL, all); 4578e0da5a48SPavel Emelyanov if (err < 0) 4579e0da5a48SPavel Emelyanov goto err_reg_all; 4580e0da5a48SPavel Emelyanov 4581f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "default", NULL, dflt); 4582e0da5a48SPavel Emelyanov if (err < 0) 4583e0da5a48SPavel Emelyanov goto err_reg_dflt; 4584e0da5a48SPavel Emelyanov #endif 4585e0da5a48SPavel Emelyanov return 0; 4586e0da5a48SPavel Emelyanov 4587e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 4588e0da5a48SPavel Emelyanov err_reg_dflt: 4589e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(all); 4590e0da5a48SPavel Emelyanov err_reg_all: 4591e0da5a48SPavel Emelyanov kfree(dflt); 4592e0da5a48SPavel Emelyanov #endif 4593e0da5a48SPavel Emelyanov err_alloc_dflt: 4594e0da5a48SPavel Emelyanov kfree(all); 4595e0da5a48SPavel Emelyanov err_alloc_all: 4596e0da5a48SPavel Emelyanov return err; 4597e0da5a48SPavel Emelyanov } 4598e0da5a48SPavel Emelyanov 45992c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net) 4600e0da5a48SPavel Emelyanov { 4601e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 4602e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(net->ipv6.devconf_dflt); 4603e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(net->ipv6.devconf_all); 4604e0da5a48SPavel Emelyanov #endif 460509ad9bc7SOctavian Purdila if (!net_eq(net, &init_net)) { 4606e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_dflt); 4607e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_all); 4608e0da5a48SPavel Emelyanov } 4609e0da5a48SPavel Emelyanov } 4610e0da5a48SPavel Emelyanov 4611e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = { 4612e0da5a48SPavel Emelyanov .init = addrconf_init_net, 4613e0da5a48SPavel Emelyanov .exit = addrconf_exit_net, 4614e0da5a48SPavel Emelyanov }; 4615e0da5a48SPavel Emelyanov 46161da177e4SLinus Torvalds /* 46171da177e4SLinus Torvalds * Device notifier 46181da177e4SLinus Torvalds */ 46191da177e4SLinus Torvalds 46201da177e4SLinus Torvalds int register_inet6addr_notifier(struct notifier_block *nb) 46211da177e4SLinus Torvalds { 4622e041c683SAlan Stern return atomic_notifier_chain_register(&inet6addr_chain, nb); 46231da177e4SLinus Torvalds } 46247159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(register_inet6addr_notifier); 46257159039aSYOSHIFUJI Hideaki 46261da177e4SLinus Torvalds int unregister_inet6addr_notifier(struct notifier_block *nb) 46271da177e4SLinus Torvalds { 4628e041c683SAlan Stern return atomic_notifier_chain_unregister(&inet6addr_chain, nb); 46291da177e4SLinus Torvalds } 46307159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(unregister_inet6addr_notifier); 46317159039aSYOSHIFUJI Hideaki 46321da177e4SLinus Torvalds /* 46331da177e4SLinus Torvalds * Init / cleanup code 46341da177e4SLinus Torvalds */ 46351da177e4SLinus Torvalds 46361da177e4SLinus Torvalds int __init addrconf_init(void) 46371da177e4SLinus Torvalds { 4638c2e21293Sstephen hemminger int i, err; 46392a8cc6c8SYOSHIFUJI Hideaki 4640e21e8467SStephen Hemminger err = ipv6_addr_label_init(); 4641e21e8467SStephen Hemminger if (err < 0) { 4642e21e8467SStephen Hemminger printk(KERN_CRIT "IPv6 Addrconf:" 4643e21e8467SStephen Hemminger " cannot initialize default policy table: %d.\n", err); 46442cc6d2bfSNeil Horman goto out; 46452a8cc6c8SYOSHIFUJI Hideaki } 46461da177e4SLinus Torvalds 46472cc6d2bfSNeil Horman err = register_pernet_subsys(&addrconf_ops); 46482cc6d2bfSNeil Horman if (err < 0) 46492cc6d2bfSNeil Horman goto out_addrlabel; 4650e0da5a48SPavel Emelyanov 46511da177e4SLinus Torvalds /* The addrconf netdev notifier requires that loopback_dev 46521da177e4SLinus Torvalds * has it's ipv6 private information allocated and setup 46531da177e4SLinus Torvalds * before it can bring up and give link-local addresses 46541da177e4SLinus Torvalds * to other devices which are up. 46551da177e4SLinus Torvalds * 46561da177e4SLinus Torvalds * Unfortunately, loopback_dev is not necessarily the first 46571da177e4SLinus Torvalds * entry in the global dev_base list of net devices. In fact, 46581da177e4SLinus Torvalds * it is likely to be the very last entry on that list. 46591da177e4SLinus Torvalds * So this causes the notifier registry below to try and 46601da177e4SLinus Torvalds * give link-local addresses to all devices besides loopback_dev 46611da177e4SLinus Torvalds * first, then loopback_dev, which cases all the non-loopback_dev 46621da177e4SLinus Torvalds * devices to fail to get a link-local address. 46631da177e4SLinus Torvalds * 46641da177e4SLinus Torvalds * So, as a temporary fix, allocate the ipv6 structure for 46651da177e4SLinus Torvalds * loopback_dev first by hand. 46661da177e4SLinus Torvalds * Longer term, all of the dependencies ipv6 has upon the loopback 46671da177e4SLinus Torvalds * device and it being up should be removed. 46681da177e4SLinus Torvalds */ 46691da177e4SLinus Torvalds rtnl_lock(); 46702774c7abSEric W. Biederman if (!ipv6_add_dev(init_net.loopback_dev)) 46711da177e4SLinus Torvalds err = -ENOMEM; 46721da177e4SLinus Torvalds rtnl_unlock(); 46731da177e4SLinus Torvalds if (err) 4674e0da5a48SPavel Emelyanov goto errlo; 46751da177e4SLinus Torvalds 4676c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 4677c2e21293Sstephen hemminger INIT_HLIST_HEAD(&inet6_addr_lst[i]); 4678c2e21293Sstephen hemminger 46791da177e4SLinus Torvalds register_netdevice_notifier(&ipv6_dev_notf); 46801da177e4SLinus Torvalds 46811da177e4SLinus Torvalds addrconf_verify(0); 4682c127ea2cSThomas Graf 4683c127ea2cSThomas Graf err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo); 4684c127ea2cSThomas Graf if (err < 0) 4685c127ea2cSThomas Graf goto errout; 4686c127ea2cSThomas Graf 4687c127ea2cSThomas Graf /* Only the first call to __rtnl_register can fail */ 4688c127ea2cSThomas Graf __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL); 4689c127ea2cSThomas Graf __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL); 4690c127ea2cSThomas Graf __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, inet6_dump_ifaddr); 4691c127ea2cSThomas Graf __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, inet6_dump_ifmcaddr); 4692c127ea2cSThomas Graf __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, inet6_dump_ifacaddr); 4693c127ea2cSThomas Graf 46942a8cc6c8SYOSHIFUJI Hideaki ipv6_addr_label_rtnl_register(); 46952a8cc6c8SYOSHIFUJI Hideaki 46961da177e4SLinus Torvalds return 0; 4697c127ea2cSThomas Graf errout: 4698c127ea2cSThomas Graf unregister_netdevice_notifier(&ipv6_dev_notf); 4699e0da5a48SPavel Emelyanov errlo: 4700e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 47012cc6d2bfSNeil Horman out_addrlabel: 47022cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 47032cc6d2bfSNeil Horman out: 4704c127ea2cSThomas Graf return err; 47051da177e4SLinus Torvalds } 47061da177e4SLinus Torvalds 470709f7709fSDaniel Lezcano void addrconf_cleanup(void) 47081da177e4SLinus Torvalds { 4709176c39afSDaniel Lezcano struct net_device *dev; 47101da177e4SLinus Torvalds int i; 47111da177e4SLinus Torvalds 47121da177e4SLinus Torvalds unregister_netdevice_notifier(&ipv6_dev_notf); 4713e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 47142cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 47151da177e4SLinus Torvalds 47161da177e4SLinus Torvalds rtnl_lock(); 47171da177e4SLinus Torvalds 4718176c39afSDaniel Lezcano /* clean dev list */ 4719176c39afSDaniel Lezcano for_each_netdev(&init_net, dev) { 4720176c39afSDaniel Lezcano if (__in6_dev_get(dev) == NULL) 4721176c39afSDaniel Lezcano continue; 4722176c39afSDaniel Lezcano addrconf_ifdown(dev, 1); 4723176c39afSDaniel Lezcano } 4724176c39afSDaniel Lezcano addrconf_ifdown(init_net.loopback_dev, 2); 4725176c39afSDaniel Lezcano 47261da177e4SLinus Torvalds /* 47271da177e4SLinus Torvalds * Check hash table. 47281da177e4SLinus Torvalds */ 47295c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 4730c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 4731c2e21293Sstephen hemminger WARN_ON(!hlist_empty(&inet6_addr_lst[i])); 47325c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 47331da177e4SLinus Torvalds 47341da177e4SLinus Torvalds del_timer(&addr_chk_timer); 47351da177e4SLinus Torvalds rtnl_unlock(); 47361da177e4SLinus Torvalds } 4737