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 41f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt 42f3213831SJoe Perches 431da177e4SLinus Torvalds #include <linux/errno.h> 441da177e4SLinus Torvalds #include <linux/types.h> 45a0bffffcSIlpo Järvinen #include <linux/kernel.h> 461da177e4SLinus Torvalds #include <linux/socket.h> 471da177e4SLinus Torvalds #include <linux/sockios.h> 481da177e4SLinus Torvalds #include <linux/net.h> 491da177e4SLinus Torvalds #include <linux/in6.h> 501da177e4SLinus Torvalds #include <linux/netdevice.h> 511823730fSThomas Graf #include <linux/if_addr.h> 521da177e4SLinus Torvalds #include <linux/if_arp.h> 531da177e4SLinus Torvalds #include <linux/if_arcnet.h> 541da177e4SLinus Torvalds #include <linux/if_infiniband.h> 551da177e4SLinus Torvalds #include <linux/route.h> 561da177e4SLinus Torvalds #include <linux/inetdevice.h> 571da177e4SLinus Torvalds #include <linux/init.h> 585a0e3ad6STejun Heo #include <linux/slab.h> 591da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 601da177e4SLinus Torvalds #include <linux/sysctl.h> 611da177e4SLinus Torvalds #endif 624fc268d2SRandy Dunlap #include <linux/capability.h> 631da177e4SLinus Torvalds #include <linux/delay.h> 641da177e4SLinus Torvalds #include <linux/notifier.h> 65543537bdSPaulo Marques #include <linux/string.h> 66ddbe5032SEric Dumazet #include <linux/hash.h> 671da177e4SLinus Torvalds 68457c4cbcSEric W. Biederman #include <net/net_namespace.h> 691da177e4SLinus Torvalds #include <net/sock.h> 701da177e4SLinus Torvalds #include <net/snmp.h> 711da177e4SLinus Torvalds 7206a4c1c5Salex.bluesman.smirnov@gmail.com #include <net/af_ieee802154.h> 73cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 #include <net/firewire.h> 741da177e4SLinus Torvalds #include <net/ipv6.h> 751da177e4SLinus Torvalds #include <net/protocol.h> 761da177e4SLinus Torvalds #include <net/ndisc.h> 771da177e4SLinus Torvalds #include <net/ip6_route.h> 781da177e4SLinus Torvalds #include <net/addrconf.h> 791da177e4SLinus Torvalds #include <net/tcp.h> 801da177e4SLinus Torvalds #include <net/ip.h> 815d620266SThomas Graf #include <net/netlink.h> 82f24e3d65SMitsuru Chinen #include <net/pkt_sched.h> 831da177e4SLinus Torvalds #include <linux/if_tunnel.h> 841da177e4SLinus Torvalds #include <linux/rtnetlink.h> 85f3a1bfb1SNicolas Dichtel #include <linux/netconf.h> 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 881da177e4SLinus Torvalds #include <linux/random.h> 891da177e4SLinus Torvalds #endif 901da177e4SLinus Torvalds 91e21e8467SStephen Hemminger #include <linux/uaccess.h> 927f7d9a6bSHerbert Xu #include <asm/unaligned.h> 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds #include <linux/proc_fs.h> 951da177e4SLinus Torvalds #include <linux/seq_file.h> 96bc3b2d7fSPaul Gortmaker #include <linux/export.h> 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds /* Set to 3 to get tracing... */ 991da177e4SLinus Torvalds #define ACONF_DEBUG 2 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds #if ACONF_DEBUG >= 3 1021da177e4SLinus Torvalds #define ADBG(x) printk x 1031da177e4SLinus Torvalds #else 1041da177e4SLinus Torvalds #define ADBG(x) 1051da177e4SLinus Torvalds #endif 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds #define INFINITY_LIFE_TIME 0xFFFFFFFF 10818a31e1eSThomas Graf 10918a31e1eSThomas Graf static inline u32 cstamp_delta(unsigned long cstamp) 11018a31e1eSThomas Graf { 11118a31e1eSThomas Graf return (cstamp - INITIAL_JIFFIES) * 100UL / HZ; 11218a31e1eSThomas Graf } 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 115f52295a9SPavel Emelyanov static void addrconf_sysctl_register(struct inet6_dev *idev); 116408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev); 117408c4768SPavel Emelyanov #else 118408c4768SPavel Emelyanov static inline void addrconf_sysctl_register(struct inet6_dev *idev) 119408c4768SPavel Emelyanov { 120408c4768SPavel Emelyanov } 121408c4768SPavel Emelyanov 122408c4768SPavel Emelyanov static inline void addrconf_sysctl_unregister(struct inet6_dev *idev) 123408c4768SPavel Emelyanov { 124408c4768SPavel Emelyanov } 1251da177e4SLinus Torvalds #endif 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 128eb7e0575SSorin Dumitru static void __ipv6_regen_rndid(struct inet6_dev *idev); 129eb7e0575SSorin Dumitru static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr); 1301da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data); 1311da177e4SLinus Torvalds #endif 1321da177e4SLinus Torvalds 1331b34be74SYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev); 1341da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev); 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds /* 1371da177e4SLinus Torvalds * Configured unicast address hash table 1381da177e4SLinus Torvalds */ 139c2e21293Sstephen hemminger static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE]; 1405c578aedSstephen hemminger static DEFINE_SPINLOCK(addrconf_hash_lock); 1411da177e4SLinus Torvalds 1421da177e4SLinus Torvalds static void addrconf_verify(unsigned long); 1431da177e4SLinus Torvalds 1448d06afabSIngo Molnar static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0); 1451da177e4SLinus Torvalds static DEFINE_SPINLOCK(addrconf_verify_lock); 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp); 1481da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp); 1491da177e4SLinus Torvalds 15093d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, 15175c78500SMoni Shoua unsigned long event); 1521da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how); 1531da177e4SLinus Torvalds 15421caa662SRomain Kuntz static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, 15521caa662SRomain Kuntz int plen, 15621caa662SRomain Kuntz const struct net_device *dev, 15721caa662SRomain Kuntz u32 flags, u32 noflags); 15821caa662SRomain Kuntz 159cf22f9a2SDavid S. Miller static void addrconf_dad_start(struct inet6_ifaddr *ifp); 1601da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data); 1611da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp); 162c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev); 1631da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data); 1641da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 1651da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 1681da177e4SLinus Torvalds struct prefix_info *pinfo); 1693e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 17006bfe655SDaniel Lezcano struct net_device *dev); 1711da177e4SLinus Torvalds 172888c848eSAdrian Bunk static struct ipv6_devconf ipv6_devconf __read_mostly = { 1731da177e4SLinus Torvalds .forwarding = 0, 1741da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 1751da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 1761da177e4SLinus Torvalds .accept_ra = 1, 1771da177e4SLinus Torvalds .accept_redirects = 1, 1781da177e4SLinus Torvalds .autoconf = 1, 1791da177e4SLinus Torvalds .force_mld_version = 0, 1801da177e4SLinus Torvalds .dad_transmits = 1, 1811da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 1821da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 1831da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 1841da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 1851da177e4SLinus Torvalds .use_tempaddr = 0, 1861da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 1871da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 1881da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 1891da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 1901da177e4SLinus Torvalds #endif 1911da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 19265f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 193c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 194930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 195930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 19652e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 19709c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 19809c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 19909c884d4SYOSHIFUJI Hideaki #endif 200930d6ff2SYOSHIFUJI Hideaki #endif 201fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0, 2020bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */ 203778d80beSYOSHIFUJI Hideaki .disable_ipv6 = 0, 2041b34be74SYOSHIFUJI Hideaki .accept_dad = 1, 2051da177e4SLinus Torvalds }; 2061da177e4SLinus Torvalds 207ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = { 2081da177e4SLinus Torvalds .forwarding = 0, 2091da177e4SLinus Torvalds .hop_limit = IPV6_DEFAULT_HOPLIMIT, 2101da177e4SLinus Torvalds .mtu6 = IPV6_MIN_MTU, 2111da177e4SLinus Torvalds .accept_ra = 1, 2121da177e4SLinus Torvalds .accept_redirects = 1, 2131da177e4SLinus Torvalds .autoconf = 1, 2141da177e4SLinus Torvalds .dad_transmits = 1, 2151da177e4SLinus Torvalds .rtr_solicits = MAX_RTR_SOLICITATIONS, 2161da177e4SLinus Torvalds .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 2171da177e4SLinus Torvalds .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 2181da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 2191da177e4SLinus Torvalds .use_tempaddr = 0, 2201da177e4SLinus Torvalds .temp_valid_lft = TEMP_VALID_LIFETIME, 2211da177e4SLinus Torvalds .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 2221da177e4SLinus Torvalds .regen_max_retry = REGEN_MAX_RETRY, 2231da177e4SLinus Torvalds .max_desync_factor = MAX_DESYNC_FACTOR, 2241da177e4SLinus Torvalds #endif 2251da177e4SLinus Torvalds .max_addresses = IPV6_MAX_ADDRESSES, 22665f5c7c1SYOSHIFUJI Hideaki .accept_ra_defrtr = 1, 227c4fd30ebSYOSHIFUJI Hideaki .accept_ra_pinfo = 1, 228930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 229930d6ff2SYOSHIFUJI Hideaki .accept_ra_rtr_pref = 1, 23052e16356SYOSHIFUJI Hideaki .rtr_probe_interval = 60 * HZ, 23109c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO 23209c884d4SYOSHIFUJI Hideaki .accept_ra_rt_info_max_plen = 0, 23309c884d4SYOSHIFUJI Hideaki #endif 234930d6ff2SYOSHIFUJI Hideaki #endif 235fbea49e1SYOSHIFUJI Hideaki .proxy_ndp = 0, 2360bcbc926SYOSHIFUJI Hideaki .accept_source_route = 0, /* we do not accept RH0 by default. */ 237778d80beSYOSHIFUJI Hideaki .disable_ipv6 = 0, 2381b34be74SYOSHIFUJI Hideaki .accept_dad = 1, 2391da177e4SLinus Torvalds }; 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */ 2421da177e4SLinus Torvalds const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; 2431da177e4SLinus Torvalds const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; 244f3ee4010SYOSHIFUJI Hideaki const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT; 245f3ee4010SYOSHIFUJI Hideaki const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT; 2462c5e8933SHannes Frederic Sowa const struct in6_addr in6addr_interfacelocal_allnodes = IN6ADDR_INTERFACELOCAL_ALLNODES_INIT; 2472c5e8933SHannes Frederic Sowa const struct in6_addr in6addr_interfacelocal_allrouters = IN6ADDR_INTERFACELOCAL_ALLROUTERS_INIT; 2482c5e8933SHannes Frederic Sowa const struct in6_addr in6addr_sitelocal_allrouters = IN6ADDR_SITELOCAL_ALLROUTERS_INIT; 2491da177e4SLinus Torvalds 250f24e3d65SMitsuru Chinen /* Check if a valid qdisc is available */ 25105297949SDavid S. Miller static inline bool addrconf_qdisc_ok(const struct net_device *dev) 252f24e3d65SMitsuru Chinen { 25305297949SDavid S. Miller return !qdisc_tx_is_noop(dev); 254f24e3d65SMitsuru Chinen } 255f24e3d65SMitsuru Chinen 256b7b1bfceSHannes Frederic Sowa static void addrconf_del_rs_timer(struct inet6_dev *idev) 2571da177e4SLinus Torvalds { 258b7b1bfceSHannes Frederic Sowa if (del_timer(&idev->rs_timer)) 259b7b1bfceSHannes Frederic Sowa __in6_dev_put(idev); 260b7b1bfceSHannes Frederic Sowa } 261b7b1bfceSHannes Frederic Sowa 262b7b1bfceSHannes Frederic Sowa static void addrconf_del_dad_timer(struct inet6_ifaddr *ifp) 263b7b1bfceSHannes Frederic Sowa { 264b7b1bfceSHannes Frederic Sowa if (del_timer(&ifp->dad_timer)) 2651da177e4SLinus Torvalds __in6_ifa_put(ifp); 2661da177e4SLinus Torvalds } 2671da177e4SLinus Torvalds 268b7b1bfceSHannes Frederic Sowa static void addrconf_mod_rs_timer(struct inet6_dev *idev, 2691da177e4SLinus Torvalds unsigned long when) 2701da177e4SLinus Torvalds { 271b7b1bfceSHannes Frederic Sowa if (!timer_pending(&idev->rs_timer)) 272b7b1bfceSHannes Frederic Sowa in6_dev_hold(idev); 273b7b1bfceSHannes Frederic Sowa mod_timer(&idev->rs_timer, jiffies + when); 2741da177e4SLinus Torvalds } 275b7b1bfceSHannes Frederic Sowa 276b7b1bfceSHannes Frederic Sowa static void addrconf_mod_dad_timer(struct inet6_ifaddr *ifp, 277b7b1bfceSHannes Frederic Sowa unsigned long when) 278b7b1bfceSHannes Frederic Sowa { 279b7b1bfceSHannes Frederic Sowa if (!timer_pending(&ifp->dad_timer)) 280b7b1bfceSHannes Frederic Sowa in6_ifa_hold(ifp); 281b7b1bfceSHannes Frederic Sowa mod_timer(&ifp->dad_timer, jiffies + when); 2821da177e4SLinus Torvalds } 2831da177e4SLinus Torvalds 2847f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev) 2857f7d9a6bSHerbert Xu { 2867d720c3eSTejun Heo if (snmp_mib_init((void __percpu **)idev->stats.ipv6, 2871823e4c8SEric Dumazet sizeof(struct ipstats_mib), 2881823e4c8SEric Dumazet __alignof__(struct ipstats_mib)) < 0) 2897f7d9a6bSHerbert Xu goto err_ip; 290be281e55SEric Dumazet idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device), 291be281e55SEric Dumazet GFP_KERNEL); 292be281e55SEric Dumazet if (!idev->stats.icmpv6dev) 2937f7d9a6bSHerbert Xu goto err_icmp; 294be281e55SEric Dumazet idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device), 295be281e55SEric Dumazet GFP_KERNEL); 296be281e55SEric Dumazet if (!idev->stats.icmpv6msgdev) 29714878f75SDavid L Stevens goto err_icmpmsg; 2987f7d9a6bSHerbert Xu 2997f7d9a6bSHerbert Xu return 0; 3007f7d9a6bSHerbert Xu 30114878f75SDavid L Stevens err_icmpmsg: 302be281e55SEric Dumazet kfree(idev->stats.icmpv6dev); 3037f7d9a6bSHerbert Xu err_icmp: 3047d720c3eSTejun Heo snmp_mib_free((void __percpu **)idev->stats.ipv6); 3057f7d9a6bSHerbert Xu err_ip: 306aaf70ec7SPavel Emelyanov return -ENOMEM; 3077f7d9a6bSHerbert Xu } 3087f7d9a6bSHerbert Xu 30916910b98SPavel Emelyanov static void snmp6_free_dev(struct inet6_dev *idev) 3107f7d9a6bSHerbert Xu { 311be281e55SEric Dumazet kfree(idev->stats.icmpv6msgdev); 312be281e55SEric Dumazet kfree(idev->stats.icmpv6dev); 3137d720c3eSTejun Heo snmp_mib_free((void __percpu **)idev->stats.ipv6); 3147f7d9a6bSHerbert Xu } 3157f7d9a6bSHerbert Xu 3161da177e4SLinus Torvalds /* Nobody refers to this device, we may destroy it. */ 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds void in6_dev_finish_destroy(struct inet6_dev *idev) 3191da177e4SLinus Torvalds { 3201da177e4SLinus Torvalds struct net_device *dev = idev->dev; 321547b792cSIlpo Järvinen 322502a2ffdSstephen hemminger WARN_ON(!list_empty(&idev->addr_list)); 323547b792cSIlpo Järvinen WARN_ON(idev->mc_list != NULL); 324b7b1bfceSHannes Frederic Sowa WARN_ON(timer_pending(&idev->rs_timer)); 325547b792cSIlpo Järvinen 3261da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 32791df42beSJoe Perches pr_debug("%s: %s\n", __func__, dev ? dev->name : "NIL"); 3281da177e4SLinus Torvalds #endif 3291da177e4SLinus Torvalds dev_put(dev); 3301da177e4SLinus Torvalds if (!idev->dead) { 331f3213831SJoe Perches pr_warn("Freeing alive inet6 device %p\n", idev); 3321da177e4SLinus Torvalds return; 3331da177e4SLinus Torvalds } 3341da177e4SLinus Torvalds snmp6_free_dev(idev); 33538f57d1aSLai Jiangshan kfree_rcu(idev, rcu); 3361da177e4SLinus Torvalds } 3377159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(in6_dev_finish_destroy); 3387159039aSYOSHIFUJI Hideaki 3391da177e4SLinus Torvalds static struct inet6_dev *ipv6_add_dev(struct net_device *dev) 3401da177e4SLinus Torvalds { 3411da177e4SLinus Torvalds struct inet6_dev *ndev; 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds ASSERT_RTNL(); 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 3461da177e4SLinus Torvalds return NULL; 3471da177e4SLinus Torvalds 348322f74a4SIngo Oeser ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL); 3491da177e4SLinus Torvalds 350322f74a4SIngo Oeser if (ndev == NULL) 351322f74a4SIngo Oeser return NULL; 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds rwlock_init(&ndev->lock); 3541da177e4SLinus Torvalds ndev->dev = dev; 355502a2ffdSstephen hemminger INIT_LIST_HEAD(&ndev->addr_list); 356b7b1bfceSHannes Frederic Sowa setup_timer(&ndev->rs_timer, addrconf_rs_timer, 357b7b1bfceSHannes Frederic Sowa (unsigned long)ndev); 358c346dca1SYOSHIFUJI Hideaki memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf)); 3591da177e4SLinus Torvalds ndev->cnf.mtu6 = dev->mtu; 3601da177e4SLinus Torvalds ndev->cnf.sysctl = NULL; 3611da177e4SLinus Torvalds ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl); 3621da177e4SLinus Torvalds if (ndev->nd_parms == NULL) { 3631da177e4SLinus Torvalds kfree(ndev); 3641da177e4SLinus Torvalds return NULL; 3651da177e4SLinus Torvalds } 3660187bdfbSBen Hutchings if (ndev->cnf.forwarding) 3670187bdfbSBen Hutchings dev_disable_lro(dev); 3681da177e4SLinus Torvalds /* We refer to the device */ 3691da177e4SLinus Torvalds dev_hold(dev); 3701da177e4SLinus Torvalds 3711da177e4SLinus Torvalds if (snmp6_alloc_dev(ndev) < 0) { 3721da177e4SLinus Torvalds ADBG((KERN_WARNING 373f3213831SJoe Perches "%s: cannot allocate memory for statistics; dev=%s.\n", 3740dc47877SHarvey Harrison __func__, dev->name)); 3751da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, ndev->nd_parms); 3768603e33dSRoy Li dev_put(dev); 3778603e33dSRoy Li kfree(ndev); 3781da177e4SLinus Torvalds return NULL; 3791da177e4SLinus Torvalds } 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds if (snmp6_register_dev(ndev) < 0) { 3821da177e4SLinus Torvalds ADBG((KERN_WARNING 383f3213831SJoe Perches "%s: cannot create /proc/net/dev_snmp6/%s\n", 3840dc47877SHarvey Harrison __func__, dev->name)); 3851da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, ndev->nd_parms); 3861da177e4SLinus Torvalds ndev->dead = 1; 3871da177e4SLinus Torvalds in6_dev_finish_destroy(ndev); 3881da177e4SLinus Torvalds return NULL; 3891da177e4SLinus Torvalds } 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds /* One reference from device. We must do this before 3921da177e4SLinus Torvalds * we invoke __ipv6_regen_rndid(). 3931da177e4SLinus Torvalds */ 3941da177e4SLinus Torvalds in6_dev_hold(ndev); 3951da177e4SLinus Torvalds 3961b34be74SYOSHIFUJI Hideaki if (dev->flags & (IFF_NOARP | IFF_LOOPBACK)) 3971b34be74SYOSHIFUJI Hideaki ndev->cnf.accept_dad = -1; 3981b34be74SYOSHIFUJI Hideaki 39907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 400c7dc89c0SFred L. Templin if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) { 401f3213831SJoe Perches pr_info("%s: Disabled Multicast RS\n", dev->name); 402c7dc89c0SFred L. Templin ndev->cnf.rtr_solicits = 0; 403c7dc89c0SFred L. Templin } 404b077d7abSYOSHIFUJI Hideaki #endif 405b077d7abSYOSHIFUJI Hideaki 4061da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 407372e6c8fSstephen hemminger INIT_LIST_HEAD(&ndev->tempaddr_list); 4081da177e4SLinus Torvalds setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev); 4091da177e4SLinus Torvalds if ((dev->flags&IFF_LOOPBACK) || 4101da177e4SLinus Torvalds dev->type == ARPHRD_TUNNEL || 4119625ed72SYOSHIFUJI Hideaki dev->type == ARPHRD_TUNNEL6 || 4121da177e4SLinus Torvalds dev->type == ARPHRD_SIT || 4131da177e4SLinus Torvalds dev->type == ARPHRD_NONE) { 4140be669bbSJoerg Roedel ndev->cnf.use_tempaddr = -1; 4151da177e4SLinus Torvalds } else { 4161da177e4SLinus Torvalds in6_dev_hold(ndev); 4171da177e4SLinus Torvalds ipv6_regen_rndid((unsigned long) ndev); 4181da177e4SLinus Torvalds } 4191da177e4SLinus Torvalds #endif 420914faa14SDaniel Borkmann ndev->token = in6addr_any; 4211da177e4SLinus Torvalds 422f24e3d65SMitsuru Chinen if (netif_running(dev) && addrconf_qdisc_ok(dev)) 42353aadcc9SHerbert Xu ndev->if_flags |= IF_READY; 42453aadcc9SHerbert Xu 4251da177e4SLinus Torvalds ipv6_mc_init_dev(ndev); 4261da177e4SLinus Torvalds ndev->tstamp = jiffies; 427f52295a9SPavel Emelyanov addrconf_sysctl_register(ndev); 42830c4cf57SDavid L Stevens /* protected by rtnl_lock */ 429cf778b00SEric Dumazet rcu_assign_pointer(dev->ip6_ptr, ndev); 430d88ae4ccSYOSHIFUJI Hideaki 4312c5e8933SHannes Frederic Sowa /* Join interface-local all-node multicast group */ 4322c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes); 4332c5e8933SHannes Frederic Sowa 434d88ae4ccSYOSHIFUJI Hideaki /* Join all-node multicast group */ 435f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes); 436d88ae4ccSYOSHIFUJI Hideaki 437d6ddef9eSLi Wei /* Join all-router multicast group if forwarding is set */ 4388b2aaedeSLi Wei if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST)) 439d6ddef9eSLi Wei ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 440d6ddef9eSLi Wei 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 462f3a1bfb1SNicolas Dichtel static int inet6_netconf_msgsize_devconf(int type) 463f3a1bfb1SNicolas Dichtel { 464f3a1bfb1SNicolas Dichtel int size = NLMSG_ALIGN(sizeof(struct netconfmsg)) 465f3a1bfb1SNicolas Dichtel + nla_total_size(4); /* NETCONFA_IFINDEX */ 466f3a1bfb1SNicolas Dichtel 46776f8f6cbSNicolas Dichtel /* type -1 is used for ALL */ 46876f8f6cbSNicolas Dichtel if (type == -1 || type == NETCONFA_FORWARDING) 469f3a1bfb1SNicolas Dichtel size += nla_total_size(4); 470b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE 471d67b8c61SNicolas Dichtel if (type == -1 || type == NETCONFA_MC_FORWARDING) 472d67b8c61SNicolas Dichtel size += nla_total_size(4); 473b1afce95SDavid S. Miller #endif 474f3a1bfb1SNicolas Dichtel 475f3a1bfb1SNicolas Dichtel return size; 476f3a1bfb1SNicolas Dichtel } 477f3a1bfb1SNicolas Dichtel 478f3a1bfb1SNicolas Dichtel static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex, 479f3a1bfb1SNicolas Dichtel struct ipv6_devconf *devconf, u32 portid, 480f3a1bfb1SNicolas Dichtel u32 seq, int event, unsigned int flags, 481f3a1bfb1SNicolas Dichtel int type) 482f3a1bfb1SNicolas Dichtel { 483f3a1bfb1SNicolas Dichtel struct nlmsghdr *nlh; 484f3a1bfb1SNicolas Dichtel struct netconfmsg *ncm; 485f3a1bfb1SNicolas Dichtel 486f3a1bfb1SNicolas Dichtel nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg), 487f3a1bfb1SNicolas Dichtel flags); 488f3a1bfb1SNicolas Dichtel if (nlh == NULL) 489f3a1bfb1SNicolas Dichtel return -EMSGSIZE; 490f3a1bfb1SNicolas Dichtel 491f3a1bfb1SNicolas Dichtel ncm = nlmsg_data(nlh); 492f3a1bfb1SNicolas Dichtel ncm->ncm_family = AF_INET6; 493f3a1bfb1SNicolas Dichtel 494f3a1bfb1SNicolas Dichtel if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0) 495f3a1bfb1SNicolas Dichtel goto nla_put_failure; 496f3a1bfb1SNicolas Dichtel 49776f8f6cbSNicolas Dichtel /* type -1 is used for ALL */ 49876f8f6cbSNicolas Dichtel if ((type == -1 || type == NETCONFA_FORWARDING) && 499f3a1bfb1SNicolas Dichtel nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0) 500f3a1bfb1SNicolas Dichtel goto nla_put_failure; 501b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE 502d67b8c61SNicolas Dichtel if ((type == -1 || type == NETCONFA_MC_FORWARDING) && 503d67b8c61SNicolas Dichtel nla_put_s32(skb, NETCONFA_MC_FORWARDING, 504d67b8c61SNicolas Dichtel devconf->mc_forwarding) < 0) 505d67b8c61SNicolas Dichtel goto nla_put_failure; 506b1afce95SDavid S. Miller #endif 507f3a1bfb1SNicolas Dichtel return nlmsg_end(skb, nlh); 508f3a1bfb1SNicolas Dichtel 509f3a1bfb1SNicolas Dichtel nla_put_failure: 510f3a1bfb1SNicolas Dichtel nlmsg_cancel(skb, nlh); 511f3a1bfb1SNicolas Dichtel return -EMSGSIZE; 512f3a1bfb1SNicolas Dichtel } 513f3a1bfb1SNicolas Dichtel 514d67b8c61SNicolas Dichtel void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex, 515f3a1bfb1SNicolas Dichtel struct ipv6_devconf *devconf) 516f3a1bfb1SNicolas Dichtel { 517f3a1bfb1SNicolas Dichtel struct sk_buff *skb; 518f3a1bfb1SNicolas Dichtel int err = -ENOBUFS; 519f3a1bfb1SNicolas Dichtel 520f3a1bfb1SNicolas Dichtel skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC); 521f3a1bfb1SNicolas Dichtel if (skb == NULL) 522f3a1bfb1SNicolas Dichtel goto errout; 523f3a1bfb1SNicolas Dichtel 524f3a1bfb1SNicolas Dichtel err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0, 525f3a1bfb1SNicolas Dichtel RTM_NEWNETCONF, 0, type); 526f3a1bfb1SNicolas Dichtel if (err < 0) { 527f3a1bfb1SNicolas Dichtel /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ 528f3a1bfb1SNicolas Dichtel WARN_ON(err == -EMSGSIZE); 529f3a1bfb1SNicolas Dichtel kfree_skb(skb); 530f3a1bfb1SNicolas Dichtel goto errout; 531f3a1bfb1SNicolas Dichtel } 532f3a1bfb1SNicolas Dichtel rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_ATOMIC); 533f3a1bfb1SNicolas Dichtel return; 534f3a1bfb1SNicolas Dichtel errout: 535f3a1bfb1SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err); 536f3a1bfb1SNicolas Dichtel } 537f3a1bfb1SNicolas Dichtel 53876f8f6cbSNicolas Dichtel static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = { 53976f8f6cbSNicolas Dichtel [NETCONFA_IFINDEX] = { .len = sizeof(int) }, 54076f8f6cbSNicolas Dichtel [NETCONFA_FORWARDING] = { .len = sizeof(int) }, 54176f8f6cbSNicolas Dichtel }; 54276f8f6cbSNicolas Dichtel 54376f8f6cbSNicolas Dichtel static int inet6_netconf_get_devconf(struct sk_buff *in_skb, 544661d2967SThomas Graf struct nlmsghdr *nlh) 54576f8f6cbSNicolas Dichtel { 54676f8f6cbSNicolas Dichtel struct net *net = sock_net(in_skb->sk); 54776f8f6cbSNicolas Dichtel struct nlattr *tb[NETCONFA_MAX+1]; 54876f8f6cbSNicolas Dichtel struct netconfmsg *ncm; 54976f8f6cbSNicolas Dichtel struct sk_buff *skb; 55076f8f6cbSNicolas Dichtel struct ipv6_devconf *devconf; 55176f8f6cbSNicolas Dichtel struct inet6_dev *in6_dev; 55276f8f6cbSNicolas Dichtel struct net_device *dev; 55376f8f6cbSNicolas Dichtel int ifindex; 55476f8f6cbSNicolas Dichtel int err; 55576f8f6cbSNicolas Dichtel 55676f8f6cbSNicolas Dichtel err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX, 55776f8f6cbSNicolas Dichtel devconf_ipv6_policy); 55876f8f6cbSNicolas Dichtel if (err < 0) 55976f8f6cbSNicolas Dichtel goto errout; 56076f8f6cbSNicolas Dichtel 56176f8f6cbSNicolas Dichtel err = EINVAL; 56276f8f6cbSNicolas Dichtel if (!tb[NETCONFA_IFINDEX]) 56376f8f6cbSNicolas Dichtel goto errout; 56476f8f6cbSNicolas Dichtel 56576f8f6cbSNicolas Dichtel ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]); 56676f8f6cbSNicolas Dichtel switch (ifindex) { 56776f8f6cbSNicolas Dichtel case NETCONFA_IFINDEX_ALL: 56876f8f6cbSNicolas Dichtel devconf = net->ipv6.devconf_all; 56976f8f6cbSNicolas Dichtel break; 57076f8f6cbSNicolas Dichtel case NETCONFA_IFINDEX_DEFAULT: 57176f8f6cbSNicolas Dichtel devconf = net->ipv6.devconf_dflt; 57276f8f6cbSNicolas Dichtel break; 57376f8f6cbSNicolas Dichtel default: 57476f8f6cbSNicolas Dichtel dev = __dev_get_by_index(net, ifindex); 57576f8f6cbSNicolas Dichtel if (dev == NULL) 57676f8f6cbSNicolas Dichtel goto errout; 57776f8f6cbSNicolas Dichtel in6_dev = __in6_dev_get(dev); 57876f8f6cbSNicolas Dichtel if (in6_dev == NULL) 57976f8f6cbSNicolas Dichtel goto errout; 58076f8f6cbSNicolas Dichtel devconf = &in6_dev->cnf; 58176f8f6cbSNicolas Dichtel break; 58276f8f6cbSNicolas Dichtel } 58376f8f6cbSNicolas Dichtel 58476f8f6cbSNicolas Dichtel err = -ENOBUFS; 58576f8f6cbSNicolas Dichtel skb = nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC); 58676f8f6cbSNicolas Dichtel if (skb == NULL) 58776f8f6cbSNicolas Dichtel goto errout; 58876f8f6cbSNicolas Dichtel 58976f8f6cbSNicolas Dichtel err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 59076f8f6cbSNicolas Dichtel NETLINK_CB(in_skb).portid, 59176f8f6cbSNicolas Dichtel nlh->nlmsg_seq, RTM_NEWNETCONF, 0, 59276f8f6cbSNicolas Dichtel -1); 59376f8f6cbSNicolas Dichtel if (err < 0) { 59476f8f6cbSNicolas Dichtel /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */ 59576f8f6cbSNicolas Dichtel WARN_ON(err == -EMSGSIZE); 59676f8f6cbSNicolas Dichtel kfree_skb(skb); 59776f8f6cbSNicolas Dichtel goto errout; 59876f8f6cbSNicolas Dichtel } 59976f8f6cbSNicolas Dichtel err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 60076f8f6cbSNicolas Dichtel errout: 60176f8f6cbSNicolas Dichtel return err; 60276f8f6cbSNicolas Dichtel } 60376f8f6cbSNicolas Dichtel 6047a674200SNicolas Dichtel static int inet6_netconf_dump_devconf(struct sk_buff *skb, 6057a674200SNicolas Dichtel struct netlink_callback *cb) 6067a674200SNicolas Dichtel { 6077a674200SNicolas Dichtel struct net *net = sock_net(skb->sk); 6087a674200SNicolas Dichtel int h, s_h; 6097a674200SNicolas Dichtel int idx, s_idx; 6107a674200SNicolas Dichtel struct net_device *dev; 6117a674200SNicolas Dichtel struct inet6_dev *idev; 6127a674200SNicolas Dichtel struct hlist_head *head; 6137a674200SNicolas Dichtel 6147a674200SNicolas Dichtel s_h = cb->args[0]; 6157a674200SNicolas Dichtel s_idx = idx = cb->args[1]; 6167a674200SNicolas Dichtel 6177a674200SNicolas Dichtel for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 6187a674200SNicolas Dichtel idx = 0; 6197a674200SNicolas Dichtel head = &net->dev_index_head[h]; 6207a674200SNicolas Dichtel rcu_read_lock(); 62163998ac2SNicolas Dichtel cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ 62263998ac2SNicolas Dichtel net->dev_base_seq; 6237a674200SNicolas Dichtel hlist_for_each_entry_rcu(dev, head, index_hlist) { 6247a674200SNicolas Dichtel if (idx < s_idx) 6257a674200SNicolas Dichtel goto cont; 6267a674200SNicolas Dichtel idev = __in6_dev_get(dev); 6277a674200SNicolas Dichtel if (!idev) 6287a674200SNicolas Dichtel goto cont; 6297a674200SNicolas Dichtel 6307a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, dev->ifindex, 6317a674200SNicolas Dichtel &idev->cnf, 6327a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 6337a674200SNicolas Dichtel cb->nlh->nlmsg_seq, 6347a674200SNicolas Dichtel RTM_NEWNETCONF, 6357a674200SNicolas Dichtel NLM_F_MULTI, 6367a674200SNicolas Dichtel -1) <= 0) { 6377a674200SNicolas Dichtel rcu_read_unlock(); 6387a674200SNicolas Dichtel goto done; 6397a674200SNicolas Dichtel } 64063998ac2SNicolas Dichtel nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 6417a674200SNicolas Dichtel cont: 6427a674200SNicolas Dichtel idx++; 6437a674200SNicolas Dichtel } 6447a674200SNicolas Dichtel rcu_read_unlock(); 6457a674200SNicolas Dichtel } 6467a674200SNicolas Dichtel if (h == NETDEV_HASHENTRIES) { 6477a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL, 6487a674200SNicolas Dichtel net->ipv6.devconf_all, 6497a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 6507a674200SNicolas Dichtel cb->nlh->nlmsg_seq, 6517a674200SNicolas Dichtel RTM_NEWNETCONF, NLM_F_MULTI, 6527a674200SNicolas Dichtel -1) <= 0) 6537a674200SNicolas Dichtel goto done; 6547a674200SNicolas Dichtel else 6557a674200SNicolas Dichtel h++; 6567a674200SNicolas Dichtel } 6577a674200SNicolas Dichtel if (h == NETDEV_HASHENTRIES + 1) { 6587a674200SNicolas Dichtel if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT, 6597a674200SNicolas Dichtel net->ipv6.devconf_dflt, 6607a674200SNicolas Dichtel NETLINK_CB(cb->skb).portid, 6617a674200SNicolas Dichtel cb->nlh->nlmsg_seq, 6627a674200SNicolas Dichtel RTM_NEWNETCONF, NLM_F_MULTI, 6637a674200SNicolas Dichtel -1) <= 0) 6647a674200SNicolas Dichtel goto done; 6657a674200SNicolas Dichtel else 6667a674200SNicolas Dichtel h++; 6677a674200SNicolas Dichtel } 6687a674200SNicolas Dichtel done: 6697a674200SNicolas Dichtel cb->args[0] = h; 6707a674200SNicolas Dichtel cb->args[1] = idx; 6717a674200SNicolas Dichtel 6727a674200SNicolas Dichtel return skb->len; 6737a674200SNicolas Dichtel } 6747a674200SNicolas Dichtel 6751da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 6761da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev) 6771da177e4SLinus Torvalds { 6781da177e4SLinus Torvalds struct net_device *dev; 6791da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 6801da177e4SLinus Torvalds 6811da177e4SLinus Torvalds if (!idev) 6821da177e4SLinus Torvalds return; 6831da177e4SLinus Torvalds dev = idev->dev; 6840187bdfbSBen Hutchings if (idev->cnf.forwarding) 6850187bdfbSBen Hutchings dev_disable_lro(dev); 6861a940835SAmerigo Wang if (dev->flags & IFF_MULTICAST) { 6872c5e8933SHannes Frederic Sowa if (idev->cnf.forwarding) { 688f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters); 6892c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters); 6902c5e8933SHannes Frederic Sowa ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters); 6912c5e8933SHannes Frederic Sowa } else { 692f3ee4010SYOSHIFUJI Hideaki ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters); 6932c5e8933SHannes Frederic Sowa ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters); 6942c5e8933SHannes Frederic Sowa ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters); 6952c5e8933SHannes Frederic Sowa } 6961da177e4SLinus Torvalds } 697502a2ffdSstephen hemminger 698502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 6992c12a74cSMichal Wrobel if (ifa->flags&IFA_F_TENTATIVE) 7002c12a74cSMichal Wrobel continue; 7011da177e4SLinus Torvalds if (idev->cnf.forwarding) 7021da177e4SLinus Torvalds addrconf_join_anycast(ifa); 7031da177e4SLinus Torvalds else 7041da177e4SLinus Torvalds addrconf_leave_anycast(ifa); 7051da177e4SLinus Torvalds } 706f3a1bfb1SNicolas Dichtel inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING, 707f3a1bfb1SNicolas Dichtel dev->ifindex, &idev->cnf); 7081da177e4SLinus Torvalds } 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds 711e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf) 7121da177e4SLinus Torvalds { 7131da177e4SLinus Torvalds struct net_device *dev; 7141da177e4SLinus Torvalds struct inet6_dev *idev; 7151da177e4SLinus Torvalds 7164acd4945SBen Hutchings for_each_netdev(net, dev) { 7171da177e4SLinus Torvalds idev = __in6_dev_get(dev); 7181da177e4SLinus Torvalds if (idev) { 719e186932bSPavel Emelyanov int changed = (!idev->cnf.forwarding) ^ (!newf); 720e186932bSPavel Emelyanov idev->cnf.forwarding = newf; 7211da177e4SLinus Torvalds if (changed) 7221da177e4SLinus Torvalds dev_forward_change(idev); 7231da177e4SLinus Torvalds } 7241da177e4SLinus Torvalds } 7251da177e4SLinus Torvalds } 726c8fecf22SPavel Emelyanov 727013d97e9SFrancesco Ruggeri static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf) 728c8fecf22SPavel Emelyanov { 729bff16c2fSPavel Emelyanov struct net *net; 730013d97e9SFrancesco Ruggeri int old; 731013d97e9SFrancesco Ruggeri 732013d97e9SFrancesco Ruggeri if (!rtnl_trylock()) 733013d97e9SFrancesco Ruggeri return restart_syscall(); 734bff16c2fSPavel Emelyanov 735bff16c2fSPavel Emelyanov net = (struct net *)table->extra2; 736013d97e9SFrancesco Ruggeri old = *p; 737013d97e9SFrancesco Ruggeri *p = newf; 738c8fecf22SPavel Emelyanov 739013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->forwarding) { 740f3a1bfb1SNicolas Dichtel if ((!newf) ^ (!old)) 741f3a1bfb1SNicolas Dichtel inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING, 742f3a1bfb1SNicolas Dichtel NETCONFA_IFINDEX_DEFAULT, 743f3a1bfb1SNicolas Dichtel net->ipv6.devconf_dflt); 744013d97e9SFrancesco Ruggeri rtnl_unlock(); 745013d97e9SFrancesco Ruggeri return 0; 74688af182eSEric W. Biederman } 747b325fddbSStephen Hemminger 748e186932bSPavel Emelyanov if (p == &net->ipv6.devconf_all->forwarding) { 749e186932bSPavel Emelyanov net->ipv6.devconf_dflt->forwarding = newf; 750e186932bSPavel Emelyanov addrconf_forward_change(net, newf); 751f3a1bfb1SNicolas Dichtel if ((!newf) ^ (!old)) 752f3a1bfb1SNicolas Dichtel inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING, 753f3a1bfb1SNicolas Dichtel NETCONFA_IFINDEX_ALL, 754f3a1bfb1SNicolas Dichtel net->ipv6.devconf_all); 755013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 756c8fecf22SPavel Emelyanov dev_forward_change((struct inet6_dev *)table->extra1); 7570187bdfbSBen Hutchings rtnl_unlock(); 758c8fecf22SPavel Emelyanov 759013d97e9SFrancesco Ruggeri if (newf) 7607b4da532SDaniel Lezcano rt6_purge_dflt_routers(net); 761b325fddbSStephen Hemminger return 1; 762c8fecf22SPavel Emelyanov } 7631da177e4SLinus Torvalds #endif 7641da177e4SLinus Torvalds 7655c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */ 7661da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp) 7671da177e4SLinus Torvalds { 768c2e21293Sstephen hemminger WARN_ON(!hlist_unhashed(&ifp->addr_lst)); 769547b792cSIlpo Järvinen 7701da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG 77191df42beSJoe Perches pr_debug("%s\n", __func__); 7721da177e4SLinus Torvalds #endif 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds in6_dev_put(ifp->idev); 7751da177e4SLinus Torvalds 776b7b1bfceSHannes Frederic Sowa if (del_timer(&ifp->dad_timer)) 777e21e8467SStephen Hemminger pr_notice("Timer is still running, when freeing ifa=%p\n", ifp); 7781da177e4SLinus Torvalds 779e9d3e084SHerbert Xu if (ifp->state != INET6_IFADDR_STATE_DEAD) { 780f3213831SJoe Perches pr_warn("Freeing alive inet6 address %p\n", ifp); 7811da177e4SLinus Torvalds return; 7821da177e4SLinus Torvalds } 78394e187c0SAmerigo Wang ip6_rt_put(ifp->rt); 7841da177e4SLinus Torvalds 785e5785985SLai Jiangshan kfree_rcu(ifp, rcu); 7861da177e4SLinus Torvalds } 7871da177e4SLinus Torvalds 788e55ffac6SBrian Haley static void 789e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp) 790e55ffac6SBrian Haley { 791502a2ffdSstephen hemminger struct list_head *p; 7928a6ce0c0SYOSHIFUJI Hideaki int ifp_scope = ipv6_addr_src_scope(&ifp->addr); 793e55ffac6SBrian Haley 794e55ffac6SBrian Haley /* 795e55ffac6SBrian Haley * Each device address list is sorted in order of scope - 796e55ffac6SBrian Haley * global before linklocal. 797e55ffac6SBrian Haley */ 798502a2ffdSstephen hemminger list_for_each(p, &idev->addr_list) { 799502a2ffdSstephen hemminger struct inet6_ifaddr *ifa 800502a2ffdSstephen hemminger = list_entry(p, struct inet6_ifaddr, if_list); 8018a6ce0c0SYOSHIFUJI Hideaki if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr)) 802e55ffac6SBrian Haley break; 803e55ffac6SBrian Haley } 804e55ffac6SBrian Haley 805b54c9b98SDavid S. Miller list_add_tail(&ifp->if_list, p); 806e55ffac6SBrian Haley } 807e55ffac6SBrian Haley 808ddbe5032SEric Dumazet static u32 inet6_addr_hash(const struct in6_addr *addr) 8093eb84f49SYOSHIFUJI Hideaki { 810ddbe5032SEric Dumazet return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT); 8113eb84f49SYOSHIFUJI Hideaki } 8123eb84f49SYOSHIFUJI Hideaki 8131da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */ 8141da177e4SLinus Torvalds 8151da177e4SLinus Torvalds static struct inet6_ifaddr * 8161da177e4SLinus Torvalds ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen, 817e431b8c0SJamal Hadi Salim int scope, u32 flags) 8181da177e4SLinus Torvalds { 8191da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 8201da177e4SLinus Torvalds struct rt6_info *rt; 8213a88a81dSstephen hemminger unsigned int hash; 8221da177e4SLinus Torvalds int err = 0; 823d68b8270SYOSHIFUJI Hideaki int addr_type = ipv6_addr_type(addr); 824d68b8270SYOSHIFUJI Hideaki 825d68b8270SYOSHIFUJI Hideaki if (addr_type == IPV6_ADDR_ANY || 826d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_MULTICAST || 827d68b8270SYOSHIFUJI Hideaki (!(idev->dev->flags & IFF_LOOPBACK) && 828d68b8270SYOSHIFUJI Hideaki addr_type & IPV6_ADDR_LOOPBACK)) 829d68b8270SYOSHIFUJI Hideaki return ERR_PTR(-EADDRNOTAVAIL); 8301da177e4SLinus Torvalds 8318814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 8321da177e4SLinus Torvalds if (idev->dead) { 8331da177e4SLinus Torvalds err = -ENODEV; /*XXX*/ 8341da177e4SLinus Torvalds goto out2; 8351da177e4SLinus Torvalds } 8361da177e4SLinus Torvalds 83756d417b1SBrian Haley if (idev->cnf.disable_ipv6) { 8389bdd8d40SBrian Haley err = -EACCES; 8399bdd8d40SBrian Haley goto out2; 8409bdd8d40SBrian Haley } 8419bdd8d40SBrian Haley 8425c578aedSstephen hemminger spin_lock(&addrconf_hash_lock); 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds /* Ignore adding duplicate addresses on an interface */ 845c346dca1SYOSHIFUJI Hideaki if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) { 8461da177e4SLinus Torvalds ADBG(("ipv6_add_addr: already assigned\n")); 8471da177e4SLinus Torvalds err = -EEXIST; 8481da177e4SLinus Torvalds goto out; 8491da177e4SLinus Torvalds } 8501da177e4SLinus Torvalds 851322f74a4SIngo Oeser ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC); 8521da177e4SLinus Torvalds 8531da177e4SLinus Torvalds if (ifa == NULL) { 8541da177e4SLinus Torvalds ADBG(("ipv6_add_addr: malloc failed\n")); 8551da177e4SLinus Torvalds err = -ENOBUFS; 8561da177e4SLinus Torvalds goto out; 8571da177e4SLinus Torvalds } 8581da177e4SLinus Torvalds 8598f031519SDavid S. Miller rt = addrconf_dst_alloc(idev, addr, false); 8601da177e4SLinus Torvalds if (IS_ERR(rt)) { 8611da177e4SLinus Torvalds err = PTR_ERR(rt); 8621da177e4SLinus Torvalds goto out; 8631da177e4SLinus Torvalds } 8641da177e4SLinus Torvalds 8654e3fd7a0SAlexey Dobriyan ifa->addr = *addr; 8661da177e4SLinus Torvalds 8671da177e4SLinus Torvalds spin_lock_init(&ifa->lock); 868e9d3e084SHerbert Xu spin_lock_init(&ifa->state_lock); 869b7b1bfceSHannes Frederic Sowa setup_timer(&ifa->dad_timer, addrconf_dad_timer, 870b7b1bfceSHannes Frederic Sowa (unsigned long)ifa); 871c2e21293Sstephen hemminger INIT_HLIST_NODE(&ifa->addr_lst); 8721da177e4SLinus Torvalds ifa->scope = scope; 8731da177e4SLinus Torvalds ifa->prefix_len = pfxlen; 8741da177e4SLinus Torvalds ifa->flags = flags | IFA_F_TENTATIVE; 8751da177e4SLinus Torvalds ifa->cstamp = ifa->tstamp = jiffies; 876617fe29dSDaniel Borkmann ifa->tokenized = false; 8771da177e4SLinus Torvalds 87857f5f544SKeir Fraser ifa->rt = rt; 87957f5f544SKeir Fraser 8801da177e4SLinus Torvalds ifa->idev = idev; 8811da177e4SLinus Torvalds in6_dev_hold(idev); 8821da177e4SLinus Torvalds /* For caller */ 8831da177e4SLinus Torvalds in6_ifa_hold(ifa); 8841da177e4SLinus Torvalds 8851da177e4SLinus Torvalds /* Add to big hash table */ 886ddbe5032SEric Dumazet hash = inet6_addr_hash(addr); 8871da177e4SLinus Torvalds 8885c578aedSstephen hemminger hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]); 8895c578aedSstephen hemminger spin_unlock(&addrconf_hash_lock); 8901da177e4SLinus Torvalds 8911da177e4SLinus Torvalds write_lock(&idev->lock); 8921da177e4SLinus Torvalds /* Add to inet6_dev unicast addr list. */ 893e55ffac6SBrian Haley ipv6_link_dev_addr(idev, ifa); 8941da177e4SLinus Torvalds 8951da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 8961da177e4SLinus Torvalds if (ifa->flags&IFA_F_TEMPORARY) { 897372e6c8fSstephen hemminger list_add(&ifa->tmp_list, &idev->tempaddr_list); 8981da177e4SLinus Torvalds in6_ifa_hold(ifa); 8991da177e4SLinus Torvalds } 9001da177e4SLinus Torvalds #endif 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds in6_ifa_hold(ifa); 9031da177e4SLinus Torvalds write_unlock(&idev->lock); 9041da177e4SLinus Torvalds out2: 9058814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 9061da177e4SLinus Torvalds 907fd92833aSYOSHIFUJI Hideaki if (likely(err == 0)) 908f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_UP, ifa); 9091da177e4SLinus Torvalds else { 9101da177e4SLinus Torvalds kfree(ifa); 9111da177e4SLinus Torvalds ifa = ERR_PTR(err); 9121da177e4SLinus Torvalds } 9131da177e4SLinus Torvalds 9141da177e4SLinus Torvalds return ifa; 9151da177e4SLinus Torvalds out: 9165c578aedSstephen hemminger spin_unlock(&addrconf_hash_lock); 9171da177e4SLinus Torvalds goto out2; 9181da177e4SLinus Torvalds } 9191da177e4SLinus Torvalds 9201da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */ 9211da177e4SLinus Torvalds 9221da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp) 9231da177e4SLinus Torvalds { 924502a2ffdSstephen hemminger struct inet6_ifaddr *ifa, *ifn; 9251da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 9264c5ff6a6SHerbert Xu int state; 9271da177e4SLinus Torvalds int deleted = 0, onlink = 0; 9281da177e4SLinus Torvalds unsigned long expires = jiffies; 9291da177e4SLinus Torvalds 9304c5ff6a6SHerbert Xu spin_lock_bh(&ifp->state_lock); 9314c5ff6a6SHerbert Xu state = ifp->state; 932e9d3e084SHerbert Xu ifp->state = INET6_IFADDR_STATE_DEAD; 9334c5ff6a6SHerbert Xu spin_unlock_bh(&ifp->state_lock); 9344c5ff6a6SHerbert Xu 9354c5ff6a6SHerbert Xu if (state == INET6_IFADDR_STATE_DEAD) 9364c5ff6a6SHerbert Xu goto out; 9371da177e4SLinus Torvalds 9385c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 9395c578aedSstephen hemminger hlist_del_init_rcu(&ifp->addr_lst); 9405c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds write_lock_bh(&idev->lock); 9431da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 9441da177e4SLinus Torvalds if (ifp->flags&IFA_F_TEMPORARY) { 945372e6c8fSstephen hemminger list_del(&ifp->tmp_list); 9461da177e4SLinus Torvalds if (ifp->ifpub) { 9471da177e4SLinus Torvalds in6_ifa_put(ifp->ifpub); 9481da177e4SLinus Torvalds ifp->ifpub = NULL; 9491da177e4SLinus Torvalds } 9501da177e4SLinus Torvalds __in6_ifa_put(ifp); 9511da177e4SLinus Torvalds } 9521da177e4SLinus Torvalds #endif 9531da177e4SLinus Torvalds 954502a2ffdSstephen hemminger list_for_each_entry_safe(ifa, ifn, &idev->addr_list, if_list) { 9551da177e4SLinus Torvalds if (ifa == ifp) { 956502a2ffdSstephen hemminger list_del_init(&ifp->if_list); 9571da177e4SLinus Torvalds __in6_ifa_put(ifp); 958502a2ffdSstephen hemminger 9591da177e4SLinus Torvalds if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0) 9601da177e4SLinus Torvalds break; 9611da177e4SLinus Torvalds deleted = 1; 9621d142804SKristian Slavov continue; 9631da177e4SLinus Torvalds } else if (ifp->flags & IFA_F_PERMANENT) { 9641da177e4SLinus Torvalds if (ipv6_prefix_equal(&ifa->addr, &ifp->addr, 9651da177e4SLinus Torvalds ifp->prefix_len)) { 9661da177e4SLinus Torvalds if (ifa->flags & IFA_F_PERMANENT) { 9671da177e4SLinus Torvalds onlink = 1; 9681da177e4SLinus Torvalds if (deleted) 9691da177e4SLinus Torvalds break; 9701da177e4SLinus Torvalds } else { 9711da177e4SLinus Torvalds unsigned long lifetime; 9721da177e4SLinus Torvalds 9731da177e4SLinus Torvalds if (!onlink) 9741da177e4SLinus Torvalds onlink = -1; 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds spin_lock(&ifa->lock); 9774bed72e4SYOSHIFUJI Hideaki 9784bed72e4SYOSHIFUJI Hideaki lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ); 9794bed72e4SYOSHIFUJI Hideaki /* 9804bed72e4SYOSHIFUJI Hideaki * Note: Because this address is 9814bed72e4SYOSHIFUJI Hideaki * not permanent, lifetime < 9824bed72e4SYOSHIFUJI Hideaki * LONG_MAX / HZ here. 9834bed72e4SYOSHIFUJI Hideaki */ 9841da177e4SLinus Torvalds if (time_before(expires, 9851da177e4SLinus Torvalds ifa->tstamp + lifetime * HZ)) 9861da177e4SLinus Torvalds expires = ifa->tstamp + lifetime * HZ; 9871da177e4SLinus Torvalds spin_unlock(&ifa->lock); 9881da177e4SLinus Torvalds } 9891da177e4SLinus Torvalds } 9901da177e4SLinus Torvalds } 9911da177e4SLinus Torvalds } 9921da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 9931da177e4SLinus Torvalds 994b7b1bfceSHannes Frederic Sowa addrconf_del_dad_timer(ifp); 995b2238566SAndrey Vagin 9961da177e4SLinus Torvalds ipv6_ifa_notify(RTM_DELADDR, ifp); 9971da177e4SLinus Torvalds 998f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifp); 9991da177e4SLinus Torvalds 10001da177e4SLinus Torvalds /* 10011da177e4SLinus Torvalds * Purge or update corresponding prefix 10021da177e4SLinus Torvalds * 10031da177e4SLinus Torvalds * 1) we don't purge prefix here if address was not permanent. 10041da177e4SLinus Torvalds * prefix is managed by its own lifetime. 10051da177e4SLinus Torvalds * 2) if there're no addresses, delete prefix. 10061da177e4SLinus Torvalds * 3) if there're still other permanent address(es), 10071da177e4SLinus Torvalds * corresponding prefix is still permanent. 10081da177e4SLinus Torvalds * 4) otherwise, update prefix lifetime to the 10091da177e4SLinus Torvalds * longest valid lifetime among the corresponding 10101da177e4SLinus Torvalds * addresses on the device. 10111da177e4SLinus Torvalds * Note: subsequent RA will update lifetime. 10121da177e4SLinus Torvalds * 10131da177e4SLinus Torvalds * --yoshfuji 10141da177e4SLinus Torvalds */ 10151da177e4SLinus Torvalds if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) { 10161da177e4SLinus Torvalds struct in6_addr prefix; 10171da177e4SLinus Torvalds struct rt6_info *rt; 10181da177e4SLinus Torvalds 101964c6d08eSNicolas Dichtel ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len); 102064c6d08eSNicolas Dichtel 102121caa662SRomain Kuntz rt = addrconf_get_prefix_route(&prefix, 102221caa662SRomain Kuntz ifp->prefix_len, 102321caa662SRomain Kuntz ifp->idev->dev, 102421caa662SRomain Kuntz 0, RTF_GATEWAY | RTF_DEFAULT); 102521caa662SRomain Kuntz 102621caa662SRomain Kuntz if (rt) { 10271da177e4SLinus Torvalds if (onlink == 0) { 1028e0a1ad73SThomas Graf ip6_del_rt(rt); 10291da177e4SLinus Torvalds rt = NULL; 10301da177e4SLinus Torvalds } else if (!(rt->rt6i_flags & RTF_EXPIRES)) { 10311716a961SGao feng rt6_set_expires(rt, expires); 10321da177e4SLinus Torvalds } 10331da177e4SLinus Torvalds } 103494e187c0SAmerigo Wang ip6_rt_put(rt); 10351da177e4SLinus Torvalds } 10361da177e4SLinus Torvalds 1037c3968a85SDaniel Walter /* clean up prefsrc entries */ 1038c3968a85SDaniel Walter rt6_remove_prefsrc(ifp); 10394c5ff6a6SHerbert Xu out: 10401da177e4SLinus Torvalds in6_ifa_put(ifp); 10411da177e4SLinus Torvalds } 10421da177e4SLinus Torvalds 10431da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 10441da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift) 10451da177e4SLinus Torvalds { 10461da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 10471da177e4SLinus Torvalds struct in6_addr addr, *tmpaddr; 104876f793e3SLorenzo Colitti unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age; 1049eac55bf9SBenoit Boissinot unsigned long regen_advance; 10501da177e4SLinus Torvalds int tmp_plen; 10511da177e4SLinus Torvalds int ret = 0; 10521da177e4SLinus Torvalds int max_addresses; 105395c385b4SNeil Horman u32 addr_flags; 105476f793e3SLorenzo Colitti unsigned long now = jiffies; 10551da177e4SLinus Torvalds 10561da177e4SLinus Torvalds write_lock(&idev->lock); 10571da177e4SLinus Torvalds if (ift) { 10581da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 10591da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8); 10601da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 10611da177e4SLinus Torvalds tmpaddr = &addr; 10621da177e4SLinus Torvalds } else { 10631da177e4SLinus Torvalds tmpaddr = NULL; 10641da177e4SLinus Torvalds } 10651da177e4SLinus Torvalds retry: 10661da177e4SLinus Torvalds in6_dev_hold(idev); 10671da177e4SLinus Torvalds if (idev->cnf.use_tempaddr <= 0) { 10681da177e4SLinus Torvalds write_unlock(&idev->lock); 1069f3213831SJoe Perches pr_info("%s: use_tempaddr is disabled\n", __func__); 10701da177e4SLinus Torvalds in6_dev_put(idev); 10711da177e4SLinus Torvalds ret = -1; 10721da177e4SLinus Torvalds goto out; 10731da177e4SLinus Torvalds } 10741da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 10751da177e4SLinus Torvalds if (ifp->regen_count++ >= idev->cnf.regen_max_retry) { 10761da177e4SLinus Torvalds idev->cnf.use_tempaddr = -1; /*XXX*/ 10771da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 10781da177e4SLinus Torvalds write_unlock(&idev->lock); 1079f3213831SJoe Perches pr_warn("%s: regeneration time exceeded - disabled temporary address support\n", 1080f3213831SJoe Perches __func__); 10811da177e4SLinus Torvalds in6_dev_put(idev); 10821da177e4SLinus Torvalds ret = -1; 10831da177e4SLinus Torvalds goto out; 10841da177e4SLinus Torvalds } 10851da177e4SLinus Torvalds in6_ifa_hold(ifp); 10861da177e4SLinus Torvalds memcpy(addr.s6_addr, ifp->addr.s6_addr, 8); 1087eb7e0575SSorin Dumitru __ipv6_try_regen_rndid(idev, tmpaddr); 10881da177e4SLinus Torvalds memcpy(&addr.s6_addr[8], idev->rndid, 8); 108976f793e3SLorenzo Colitti age = (now - ifp->tstamp) / HZ; 10901da177e4SLinus Torvalds tmp_valid_lft = min_t(__u32, 10911da177e4SLinus Torvalds ifp->valid_lft, 10927a876b0eSGlenn Wurster idev->cnf.temp_valid_lft + age); 10931da177e4SLinus Torvalds tmp_prefered_lft = min_t(__u32, 10941da177e4SLinus Torvalds ifp->prefered_lft, 10957a876b0eSGlenn Wurster idev->cnf.temp_prefered_lft + age - 1096784e2710SBen Hutchings idev->cnf.max_desync_factor); 10971da177e4SLinus Torvalds tmp_plen = ifp->prefix_len; 10981da177e4SLinus Torvalds max_addresses = idev->cnf.max_addresses; 10991da177e4SLinus Torvalds tmp_tstamp = ifp->tstamp; 11001da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 11011da177e4SLinus Torvalds 1102eac55bf9SBenoit Boissinot regen_advance = idev->cnf.regen_max_retry * 1103eac55bf9SBenoit Boissinot idev->cnf.dad_transmits * 1104eac55bf9SBenoit Boissinot idev->nd_parms->retrans_time / HZ; 11051da177e4SLinus Torvalds write_unlock(&idev->lock); 110695c385b4SNeil Horman 1107eac55bf9SBenoit Boissinot /* A temporary address is created only if this calculated Preferred 1108eac55bf9SBenoit Boissinot * Lifetime is greater than REGEN_ADVANCE time units. In particular, 1109eac55bf9SBenoit Boissinot * an implementation must not create a temporary address with a zero 1110eac55bf9SBenoit Boissinot * Preferred Lifetime. 1111eac55bf9SBenoit Boissinot */ 1112eac55bf9SBenoit Boissinot if (tmp_prefered_lft <= regen_advance) { 1113eac55bf9SBenoit Boissinot in6_ifa_put(ifp); 1114eac55bf9SBenoit Boissinot in6_dev_put(idev); 1115eac55bf9SBenoit Boissinot ret = -1; 1116eac55bf9SBenoit Boissinot goto out; 1117eac55bf9SBenoit Boissinot } 1118eac55bf9SBenoit Boissinot 111995c385b4SNeil Horman addr_flags = IFA_F_TEMPORARY; 112095c385b4SNeil Horman /* set in addrconf_prefix_rcv() */ 112195c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 112295c385b4SNeil Horman addr_flags |= IFA_F_OPTIMISTIC; 112395c385b4SNeil Horman 11241da177e4SLinus Torvalds ift = !max_addresses || 11251da177e4SLinus Torvalds ipv6_count_addresses(idev) < max_addresses ? 112688924753SCong Wang ipv6_add_addr(idev, &addr, tmp_plen, ipv6_addr_scope(&addr), 112795c385b4SNeil Horman addr_flags) : NULL; 11283f0d2ba0SYOSHIFUJI Hideaki / 吉藤英明 if (IS_ERR_OR_NULL(ift)) { 11291da177e4SLinus Torvalds in6_ifa_put(ifp); 11301da177e4SLinus Torvalds in6_dev_put(idev); 1131f3213831SJoe Perches pr_info("%s: retry temporary address regeneration\n", __func__); 11321da177e4SLinus Torvalds tmpaddr = &addr; 11331da177e4SLinus Torvalds write_lock(&idev->lock); 11341da177e4SLinus Torvalds goto retry; 11351da177e4SLinus Torvalds } 11361da177e4SLinus Torvalds 11371da177e4SLinus Torvalds spin_lock_bh(&ift->lock); 11381da177e4SLinus Torvalds ift->ifpub = ifp; 11391da177e4SLinus Torvalds ift->valid_lft = tmp_valid_lft; 11401da177e4SLinus Torvalds ift->prefered_lft = tmp_prefered_lft; 114176f793e3SLorenzo Colitti ift->cstamp = now; 11421da177e4SLinus Torvalds ift->tstamp = tmp_tstamp; 11431da177e4SLinus Torvalds spin_unlock_bh(&ift->lock); 11441da177e4SLinus Torvalds 1145cf22f9a2SDavid S. Miller addrconf_dad_start(ift); 11461da177e4SLinus Torvalds in6_ifa_put(ift); 11471da177e4SLinus Torvalds in6_dev_put(idev); 11481da177e4SLinus Torvalds out: 11491da177e4SLinus Torvalds return ret; 11501da177e4SLinus Torvalds } 11511da177e4SLinus Torvalds #endif 11521da177e4SLinus Torvalds 11531da177e4SLinus Torvalds /* 1154072047e4SYOSHIFUJI Hideaki * Choose an appropriate source address (RFC3484) 11551da177e4SLinus Torvalds */ 1156a9b05723SYOSHIFUJI Hideaki enum { 1157a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_INIT = 0, 1158a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LOCAL, 1159a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_SCOPE, 1160a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFERRED, 1161a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1162a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_HOA, 1163a9b05723SYOSHIFUJI Hideaki #endif 1164a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_OIF, 1165a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_LABEL, 1166a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_PRIVACY 1167a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PRIVACY, 1168a9b05723SYOSHIFUJI Hideaki #endif 1169a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_ORCHID, 1170a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_PREFIX, 1171a9b05723SYOSHIFUJI Hideaki IPV6_SADDR_RULE_MAX 1172072047e4SYOSHIFUJI Hideaki }; 1173072047e4SYOSHIFUJI Hideaki 1174a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score { 1175a9b05723SYOSHIFUJI Hideaki int rule; 1176a9b05723SYOSHIFUJI Hideaki int addr_type; 1177a9b05723SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 1178a9b05723SYOSHIFUJI Hideaki DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX); 1179a9b05723SYOSHIFUJI Hideaki int scopedist; 1180a9b05723SYOSHIFUJI Hideaki int matchlen; 1181a9b05723SYOSHIFUJI Hideaki }; 1182a9b05723SYOSHIFUJI Hideaki 1183a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst { 11849acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *addr; 1185a9b05723SYOSHIFUJI Hideaki int ifindex; 1186a9b05723SYOSHIFUJI Hideaki int scope; 1187a9b05723SYOSHIFUJI Hideaki int label; 11887cbca67cSYOSHIFUJI Hideaki unsigned int prefs; 1189a9b05723SYOSHIFUJI Hideaki }; 1190072047e4SYOSHIFUJI Hideaki 1191b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type) 11921da177e4SLinus Torvalds { 119345bb0060SUlrich Weber if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK)) 1194072047e4SYOSHIFUJI Hideaki return 1; 1195072047e4SYOSHIFUJI Hideaki return 0; 11961da177e4SLinus Torvalds } 11971da177e4SLinus Torvalds 11983de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net, 11993de23255SBenjamin Thery struct ipv6_saddr_score *score, 1200a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst *dst, 1201a9b05723SYOSHIFUJI Hideaki int i) 1202a9b05723SYOSHIFUJI Hideaki { 1203a9b05723SYOSHIFUJI Hideaki int ret; 1204a9b05723SYOSHIFUJI Hideaki 1205a9b05723SYOSHIFUJI Hideaki if (i <= score->rule) { 1206a9b05723SYOSHIFUJI Hideaki switch (i) { 1207a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1208a9b05723SYOSHIFUJI Hideaki ret = score->scopedist; 1209a9b05723SYOSHIFUJI Hideaki break; 1210a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1211a9b05723SYOSHIFUJI Hideaki ret = score->matchlen; 1212a9b05723SYOSHIFUJI Hideaki break; 1213a9b05723SYOSHIFUJI Hideaki default: 1214a9b05723SYOSHIFUJI Hideaki ret = !!test_bit(i, score->scorebits); 1215a9b05723SYOSHIFUJI Hideaki } 1216a9b05723SYOSHIFUJI Hideaki goto out; 1217a9b05723SYOSHIFUJI Hideaki } 1218a9b05723SYOSHIFUJI Hideaki 1219a9b05723SYOSHIFUJI Hideaki switch (i) { 1220a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_INIT: 1221a9b05723SYOSHIFUJI Hideaki /* Rule 0: remember if hiscore is not ready yet */ 1222a9b05723SYOSHIFUJI Hideaki ret = !!score->ifa; 1223a9b05723SYOSHIFUJI Hideaki break; 1224a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LOCAL: 1225a9b05723SYOSHIFUJI Hideaki /* Rule 1: Prefer same address */ 1226a9b05723SYOSHIFUJI Hideaki ret = ipv6_addr_equal(&score->ifa->addr, dst->addr); 1227a9b05723SYOSHIFUJI Hideaki break; 1228a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_SCOPE: 1229a9b05723SYOSHIFUJI Hideaki /* Rule 2: Prefer appropriate scope 1230a9b05723SYOSHIFUJI Hideaki * 1231a9b05723SYOSHIFUJI Hideaki * ret 1232a9b05723SYOSHIFUJI Hideaki * ^ 1233a9b05723SYOSHIFUJI Hideaki * -1 | d 15 1234a9b05723SYOSHIFUJI Hideaki * ---+--+-+---> scope 1235a9b05723SYOSHIFUJI Hideaki * | 1236a9b05723SYOSHIFUJI Hideaki * | d is scope of the destination. 1237a9b05723SYOSHIFUJI Hideaki * B-d | \ 1238a9b05723SYOSHIFUJI Hideaki * | \ <- smaller scope is better if 1239a9b05723SYOSHIFUJI Hideaki * B-15 | \ if scope is enough for destinaion. 1240a9b05723SYOSHIFUJI Hideaki * | ret = B - scope (-1 <= scope >= d <= 15). 1241a9b05723SYOSHIFUJI Hideaki * d-C-1 | / 1242a9b05723SYOSHIFUJI Hideaki * |/ <- greater is better 1243a9b05723SYOSHIFUJI Hideaki * -C / if scope is not enough for destination. 1244a9b05723SYOSHIFUJI Hideaki * /| ret = scope - C (-1 <= d < scope <= 15). 1245a9b05723SYOSHIFUJI Hideaki * 1246a9b05723SYOSHIFUJI Hideaki * d - C - 1 < B -15 (for all -1 <= d <= 15). 1247a9b05723SYOSHIFUJI Hideaki * C > d + 14 - B >= 15 + 14 - B = 29 - B. 1248a9b05723SYOSHIFUJI Hideaki * Assume B = 0 and we get C > 29. 1249a9b05723SYOSHIFUJI Hideaki */ 1250a9b05723SYOSHIFUJI Hideaki ret = __ipv6_addr_src_scope(score->addr_type); 1251a9b05723SYOSHIFUJI Hideaki if (ret >= dst->scope) 1252a9b05723SYOSHIFUJI Hideaki ret = -ret; 1253a9b05723SYOSHIFUJI Hideaki else 1254a9b05723SYOSHIFUJI Hideaki ret -= 128; /* 30 is enough */ 1255a9b05723SYOSHIFUJI Hideaki score->scopedist = ret; 1256a9b05723SYOSHIFUJI Hideaki break; 1257a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFERRED: 1258a9b05723SYOSHIFUJI Hideaki /* Rule 3: Avoid deprecated and optimistic addresses */ 1259a9b05723SYOSHIFUJI Hideaki ret = ipv6_saddr_preferred(score->addr_type) || 1260a9b05723SYOSHIFUJI Hideaki !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC)); 1261a9b05723SYOSHIFUJI Hideaki break; 1262a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6 1263a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_HOA: 12647cbca67cSYOSHIFUJI Hideaki { 1265a9b05723SYOSHIFUJI Hideaki /* Rule 4: Prefer home address */ 12667cbca67cSYOSHIFUJI Hideaki int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA); 12677cbca67cSYOSHIFUJI Hideaki ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome; 1268a9b05723SYOSHIFUJI Hideaki break; 12697cbca67cSYOSHIFUJI Hideaki } 1270a9b05723SYOSHIFUJI Hideaki #endif 1271a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_OIF: 1272a9b05723SYOSHIFUJI Hideaki /* Rule 5: Prefer outgoing interface */ 1273a9b05723SYOSHIFUJI Hideaki ret = (!dst->ifindex || 1274a9b05723SYOSHIFUJI Hideaki dst->ifindex == score->ifa->idev->dev->ifindex); 1275a9b05723SYOSHIFUJI Hideaki break; 1276a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_LABEL: 1277a9b05723SYOSHIFUJI Hideaki /* Rule 6: Prefer matching label */ 12783de23255SBenjamin Thery ret = ipv6_addr_label(net, 12793de23255SBenjamin Thery &score->ifa->addr, score->addr_type, 1280a9b05723SYOSHIFUJI Hideaki score->ifa->idev->dev->ifindex) == dst->label; 1281a9b05723SYOSHIFUJI Hideaki break; 1282a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_PRIVACY 1283a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PRIVACY: 12847cbca67cSYOSHIFUJI Hideaki { 1285a9b05723SYOSHIFUJI Hideaki /* Rule 7: Prefer public address 128625985edcSLucas De Marchi * Note: prefer temporary address if use_tempaddr >= 2 1287a9b05723SYOSHIFUJI Hideaki */ 12887cbca67cSYOSHIFUJI Hideaki int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ? 12897cbca67cSYOSHIFUJI Hideaki !!(dst->prefs & IPV6_PREFER_SRC_TMP) : 12907cbca67cSYOSHIFUJI Hideaki score->ifa->idev->cnf.use_tempaddr >= 2; 12917cbca67cSYOSHIFUJI Hideaki ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp; 1292a9b05723SYOSHIFUJI Hideaki break; 12937cbca67cSYOSHIFUJI Hideaki } 1294a9b05723SYOSHIFUJI Hideaki #endif 1295a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_ORCHID: 1296a9b05723SYOSHIFUJI Hideaki /* Rule 8-: Prefer ORCHID vs ORCHID or 1297a9b05723SYOSHIFUJI Hideaki * non-ORCHID vs non-ORCHID 1298a9b05723SYOSHIFUJI Hideaki */ 1299a9b05723SYOSHIFUJI Hideaki ret = !(ipv6_addr_orchid(&score->ifa->addr) ^ 1300a9b05723SYOSHIFUJI Hideaki ipv6_addr_orchid(dst->addr)); 1301a9b05723SYOSHIFUJI Hideaki break; 1302a9b05723SYOSHIFUJI Hideaki case IPV6_SADDR_RULE_PREFIX: 1303a9b05723SYOSHIFUJI Hideaki /* Rule 8: Use longest matching prefix */ 130491b4b04fSYOSHIFUJI Hideaki / 吉藤英明 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr); 130591b4b04fSYOSHIFUJI Hideaki / 吉藤英明 if (ret > score->ifa->prefix_len) 130691b4b04fSYOSHIFUJI Hideaki / 吉藤英明 ret = score->ifa->prefix_len; 130791b4b04fSYOSHIFUJI Hideaki / 吉藤英明 score->matchlen = ret; 1308a9b05723SYOSHIFUJI Hideaki break; 1309a9b05723SYOSHIFUJI Hideaki default: 1310a9b05723SYOSHIFUJI Hideaki ret = 0; 1311a9b05723SYOSHIFUJI Hideaki } 1312a9b05723SYOSHIFUJI Hideaki 1313a9b05723SYOSHIFUJI Hideaki if (ret) 1314a9b05723SYOSHIFUJI Hideaki __set_bit(i, score->scorebits); 1315a9b05723SYOSHIFUJI Hideaki score->rule = i; 1316a9b05723SYOSHIFUJI Hideaki out: 1317a9b05723SYOSHIFUJI Hideaki return ret; 1318a9b05723SYOSHIFUJI Hideaki } 1319a9b05723SYOSHIFUJI Hideaki 1320b3f644fcSPatrick McHardy int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev, 13219acd9f3aSYOSHIFUJI Hideaki const struct in6_addr *daddr, unsigned int prefs, 13227cbca67cSYOSHIFUJI Hideaki struct in6_addr *saddr) 13231da177e4SLinus Torvalds { 1324a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score scores[2], 1325a9b05723SYOSHIFUJI Hideaki *score = &scores[0], *hiscore = &scores[1]; 1326a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst dst; 1327072047e4SYOSHIFUJI Hideaki struct net_device *dev; 1328a9b05723SYOSHIFUJI Hideaki int dst_type; 13291da177e4SLinus Torvalds 1330a9b05723SYOSHIFUJI Hideaki dst_type = __ipv6_addr_type(daddr); 1331a9b05723SYOSHIFUJI Hideaki dst.addr = daddr; 1332a9b05723SYOSHIFUJI Hideaki dst.ifindex = dst_dev ? dst_dev->ifindex : 0; 1333a9b05723SYOSHIFUJI Hideaki dst.scope = __ipv6_addr_src_scope(dst_type); 13343de23255SBenjamin Thery dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex); 13357cbca67cSYOSHIFUJI Hideaki dst.prefs = prefs; 1336a9b05723SYOSHIFUJI Hideaki 1337a9b05723SYOSHIFUJI Hideaki hiscore->rule = -1; 1338a9b05723SYOSHIFUJI Hideaki hiscore->ifa = NULL; 13391da177e4SLinus Torvalds 13408814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 1341072047e4SYOSHIFUJI Hideaki 1342c6d14c84SEric Dumazet for_each_netdev_rcu(net, dev) { 1343072047e4SYOSHIFUJI Hideaki struct inet6_dev *idev; 1344072047e4SYOSHIFUJI Hideaki 1345a9b05723SYOSHIFUJI Hideaki /* Candidate Source Address (section 4) 1346072047e4SYOSHIFUJI Hideaki * - multicast and link-local destination address, 1347072047e4SYOSHIFUJI Hideaki * the set of candidate source address MUST only 1348072047e4SYOSHIFUJI Hideaki * include addresses assigned to interfaces 1349072047e4SYOSHIFUJI Hideaki * belonging to the same link as the outgoing 1350072047e4SYOSHIFUJI Hideaki * interface. 1351072047e4SYOSHIFUJI Hideaki * (- For site-local destination addresses, the 1352072047e4SYOSHIFUJI Hideaki * set of candidate source addresses MUST only 1353072047e4SYOSHIFUJI Hideaki * include addresses assigned to interfaces 1354072047e4SYOSHIFUJI Hideaki * belonging to the same site as the outgoing 1355072047e4SYOSHIFUJI Hideaki * interface.) 1356072047e4SYOSHIFUJI Hideaki */ 1357a9b05723SYOSHIFUJI Hideaki if (((dst_type & IPV6_ADDR_MULTICAST) || 1358a9b05723SYOSHIFUJI Hideaki dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) && 1359a9b05723SYOSHIFUJI Hideaki dst.ifindex && dev->ifindex != dst.ifindex) 1360072047e4SYOSHIFUJI Hideaki continue; 1361072047e4SYOSHIFUJI Hideaki 13621da177e4SLinus Torvalds idev = __in6_dev_get(dev); 1363072047e4SYOSHIFUJI Hideaki if (!idev) 1364072047e4SYOSHIFUJI Hideaki continue; 1365072047e4SYOSHIFUJI Hideaki 13661da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1367502a2ffdSstephen hemminger list_for_each_entry(score->ifa, &idev->addr_list, if_list) { 1368a9b05723SYOSHIFUJI Hideaki int i; 1369072047e4SYOSHIFUJI Hideaki 1370a9b05723SYOSHIFUJI Hideaki /* 13716b3ae80aSYOSHIFUJI Hideaki * - Tentative Address (RFC2462 section 5.4) 13726b3ae80aSYOSHIFUJI Hideaki * - A tentative address is not considered 13736b3ae80aSYOSHIFUJI Hideaki * "assigned to an interface" in the traditional 137495c385b4SNeil Horman * sense, unless it is also flagged as optimistic. 13756b3ae80aSYOSHIFUJI Hideaki * - Candidate Source Address (section 4) 1376072047e4SYOSHIFUJI Hideaki * - In any case, anycast addresses, multicast 1377072047e4SYOSHIFUJI Hideaki * addresses, and the unspecified address MUST 1378072047e4SYOSHIFUJI Hideaki * NOT be included in a candidate set. 1379072047e4SYOSHIFUJI Hideaki */ 1380a9b05723SYOSHIFUJI Hideaki if ((score->ifa->flags & IFA_F_TENTATIVE) && 1381a9b05723SYOSHIFUJI Hideaki (!(score->ifa->flags & IFA_F_OPTIMISTIC))) 13826b3ae80aSYOSHIFUJI Hideaki continue; 1383a9b05723SYOSHIFUJI Hideaki 1384a9b05723SYOSHIFUJI Hideaki score->addr_type = __ipv6_addr_type(&score->ifa->addr); 1385a9b05723SYOSHIFUJI Hideaki 1386a9b05723SYOSHIFUJI Hideaki if (unlikely(score->addr_type == IPV6_ADDR_ANY || 1387a9b05723SYOSHIFUJI Hideaki score->addr_type & IPV6_ADDR_MULTICAST)) { 1388072047e4SYOSHIFUJI Hideaki LIMIT_NETDEBUG(KERN_DEBUG 1389072047e4SYOSHIFUJI Hideaki "ADDRCONF: unspecified / multicast address " 1390072047e4SYOSHIFUJI Hideaki "assigned as unicast address on %s", 1391072047e4SYOSHIFUJI Hideaki dev->name); 13921da177e4SLinus Torvalds continue; 1393072047e4SYOSHIFUJI Hideaki } 1394072047e4SYOSHIFUJI Hideaki 1395a9b05723SYOSHIFUJI Hideaki score->rule = -1; 1396a9b05723SYOSHIFUJI Hideaki bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX); 1397072047e4SYOSHIFUJI Hideaki 1398a9b05723SYOSHIFUJI Hideaki for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) { 1399a9b05723SYOSHIFUJI Hideaki int minihiscore, miniscore; 1400072047e4SYOSHIFUJI Hideaki 14013de23255SBenjamin Thery minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i); 14023de23255SBenjamin Thery miniscore = ipv6_get_saddr_eval(net, score, &dst, i); 1403072047e4SYOSHIFUJI Hideaki 1404a9b05723SYOSHIFUJI Hideaki if (minihiscore > miniscore) { 1405a9b05723SYOSHIFUJI Hideaki if (i == IPV6_SADDR_RULE_SCOPE && 1406a9b05723SYOSHIFUJI Hideaki score->scopedist > 0) { 1407a9b05723SYOSHIFUJI Hideaki /* 1408a9b05723SYOSHIFUJI Hideaki * special case: 1409a9b05723SYOSHIFUJI Hideaki * each remaining entry 1410a9b05723SYOSHIFUJI Hideaki * has too small (not enough) 1411a9b05723SYOSHIFUJI Hideaki * scope, because ifa entries 1412a9b05723SYOSHIFUJI Hideaki * are sorted by their scope 1413a9b05723SYOSHIFUJI Hideaki * values. 1414072047e4SYOSHIFUJI Hideaki */ 1415a9b05723SYOSHIFUJI Hideaki goto try_nextdev; 1416072047e4SYOSHIFUJI Hideaki } 1417a9b05723SYOSHIFUJI Hideaki break; 1418a9b05723SYOSHIFUJI Hideaki } else if (minihiscore < miniscore) { 1419a9b05723SYOSHIFUJI Hideaki if (hiscore->ifa) 1420a9b05723SYOSHIFUJI Hideaki in6_ifa_put(hiscore->ifa); 142199cd07a5SJuha-Matti Tapio 1422a9b05723SYOSHIFUJI Hideaki in6_ifa_hold(score->ifa); 1423072047e4SYOSHIFUJI Hideaki 1424a0bffffcSIlpo Järvinen swap(hiscore, score); 1425a9b05723SYOSHIFUJI Hideaki 1426a9b05723SYOSHIFUJI Hideaki /* restore our iterator */ 1427a9b05723SYOSHIFUJI Hideaki score->ifa = hiscore->ifa; 1428a9b05723SYOSHIFUJI Hideaki 1429a9b05723SYOSHIFUJI Hideaki break; 14301da177e4SLinus Torvalds } 1431a9b05723SYOSHIFUJI Hideaki } 1432a9b05723SYOSHIFUJI Hideaki } 1433a9b05723SYOSHIFUJI Hideaki try_nextdev: 14341da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 14351da177e4SLinus Torvalds } 14368814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 14371da177e4SLinus Torvalds 1438a9b05723SYOSHIFUJI Hideaki if (!hiscore->ifa) 1439072047e4SYOSHIFUJI Hideaki return -EADDRNOTAVAIL; 14401da177e4SLinus Torvalds 14414e3fd7a0SAlexey Dobriyan *saddr = hiscore->ifa->addr; 1442a9b05723SYOSHIFUJI Hideaki in6_ifa_put(hiscore->ifa); 1443072047e4SYOSHIFUJI Hideaki return 0; 14441da177e4SLinus Torvalds } 14455e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr); 14461da177e4SLinus Torvalds 1447b7b1bfceSHannes Frederic Sowa static int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr, 1448b7b1bfceSHannes Frederic Sowa unsigned char banned_flags) 1449b7b1bfceSHannes Frederic Sowa { 1450b7b1bfceSHannes Frederic Sowa struct inet6_ifaddr *ifp; 1451b7b1bfceSHannes Frederic Sowa int err = -EADDRNOTAVAIL; 1452b7b1bfceSHannes Frederic Sowa 1453b7b1bfceSHannes Frederic Sowa list_for_each_entry(ifp, &idev->addr_list, if_list) { 1454b7b1bfceSHannes Frederic Sowa if (ifp->scope == IFA_LINK && 1455b7b1bfceSHannes Frederic Sowa !(ifp->flags & banned_flags)) { 1456b7b1bfceSHannes Frederic Sowa *addr = ifp->addr; 1457b7b1bfceSHannes Frederic Sowa err = 0; 1458b7b1bfceSHannes Frederic Sowa break; 1459b7b1bfceSHannes Frederic Sowa } 1460b7b1bfceSHannes Frederic Sowa } 1461b7b1bfceSHannes Frederic Sowa return err; 1462b7b1bfceSHannes Frederic Sowa } 1463b7b1bfceSHannes Frederic Sowa 146495c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr, 146595c385b4SNeil Horman unsigned char banned_flags) 14661da177e4SLinus Torvalds { 14671da177e4SLinus Torvalds struct inet6_dev *idev; 14681da177e4SLinus Torvalds int err = -EADDRNOTAVAIL; 14691da177e4SLinus Torvalds 14708814c4b5SYOSHIFUJI Hideaki rcu_read_lock(); 1471e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 1472e21e8467SStephen Hemminger if (idev) { 14731da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1474b7b1bfceSHannes Frederic Sowa err = __ipv6_get_lladdr(idev, addr, banned_flags); 14751da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 14761da177e4SLinus Torvalds } 14778814c4b5SYOSHIFUJI Hideaki rcu_read_unlock(); 14781da177e4SLinus Torvalds return err; 14791da177e4SLinus Torvalds } 14801da177e4SLinus Torvalds 14811da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev) 14821da177e4SLinus Torvalds { 14831da177e4SLinus Torvalds int cnt = 0; 14841da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 14851da177e4SLinus Torvalds 14861da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1487502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) 14881da177e4SLinus Torvalds cnt++; 14891da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 14901da177e4SLinus Torvalds return cnt; 14911da177e4SLinus Torvalds } 14921da177e4SLinus Torvalds 1493b71d1d42SEric Dumazet int ipv6_chk_addr(struct net *net, const struct in6_addr *addr, 14942a7851bfSFlorian Westphal const struct net_device *dev, int strict) 14951da177e4SLinus Torvalds { 1496eedf042aSStephen Hemminger struct inet6_ifaddr *ifp; 1497ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 14981da177e4SLinus Torvalds 14995c578aedSstephen hemminger rcu_read_lock_bh(); 1500b67bfe0dSSasha Levin hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) { 1501878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 1502bfeade08SDaniel Lezcano continue; 15031da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr) && 1504eedf042aSStephen Hemminger !(ifp->flags&IFA_F_TENTATIVE) && 1505eedf042aSStephen Hemminger (dev == NULL || ifp->idev->dev == dev || 1506eedf042aSStephen Hemminger !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) { 15075c578aedSstephen hemminger rcu_read_unlock_bh(); 1508eedf042aSStephen Hemminger return 1; 1509eedf042aSStephen Hemminger } 1510eedf042aSStephen Hemminger } 15115c578aedSstephen hemminger 1512eedf042aSStephen Hemminger rcu_read_unlock_bh(); 1513eedf042aSStephen Hemminger return 0; 15141da177e4SLinus Torvalds } 15157159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_addr); 15167159039aSYOSHIFUJI Hideaki 15173e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr, 151806bfe655SDaniel Lezcano struct net_device *dev) 15191da177e4SLinus Torvalds { 1520ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 15211da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 15221da177e4SLinus Torvalds 1523b67bfe0dSSasha Levin hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) { 1524878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 152506bfe655SDaniel Lezcano continue; 15261da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 15271da177e4SLinus Torvalds if (dev == NULL || ifp->idev->dev == dev) 15283e81c6daSDavid S. Miller return true; 15291da177e4SLinus Torvalds } 15301da177e4SLinus Torvalds } 15313e81c6daSDavid S. Miller return false; 15321da177e4SLinus Torvalds } 15331da177e4SLinus Torvalds 1534b71d1d42SEric Dumazet int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev) 153552eeeb84SYOSHIFUJI Hideaki { 153652eeeb84SYOSHIFUJI Hideaki struct inet6_dev *idev; 153752eeeb84SYOSHIFUJI Hideaki struct inet6_ifaddr *ifa; 153852eeeb84SYOSHIFUJI Hideaki int onlink; 153952eeeb84SYOSHIFUJI Hideaki 154052eeeb84SYOSHIFUJI Hideaki onlink = 0; 154152eeeb84SYOSHIFUJI Hideaki rcu_read_lock(); 154252eeeb84SYOSHIFUJI Hideaki idev = __in6_dev_get(dev); 154352eeeb84SYOSHIFUJI Hideaki if (idev) { 154452eeeb84SYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 1545502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 154652eeeb84SYOSHIFUJI Hideaki onlink = ipv6_prefix_equal(addr, &ifa->addr, 154752eeeb84SYOSHIFUJI Hideaki ifa->prefix_len); 154852eeeb84SYOSHIFUJI Hideaki if (onlink) 154952eeeb84SYOSHIFUJI Hideaki break; 155052eeeb84SYOSHIFUJI Hideaki } 155152eeeb84SYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 155252eeeb84SYOSHIFUJI Hideaki } 155352eeeb84SYOSHIFUJI Hideaki rcu_read_unlock(); 155452eeeb84SYOSHIFUJI Hideaki return onlink; 155552eeeb84SYOSHIFUJI Hideaki } 155652eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix); 155752eeeb84SYOSHIFUJI Hideaki 15589acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr, 15591cab3da6SDaniel Lezcano struct net_device *dev, int strict) 15601da177e4SLinus Torvalds { 1561b79d1d54SDavid S. Miller struct inet6_ifaddr *ifp, *result = NULL; 1562ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 15631da177e4SLinus Torvalds 15645c578aedSstephen hemminger rcu_read_lock_bh(); 1565b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) { 1566878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 15671cab3da6SDaniel Lezcano continue; 15681da177e4SLinus Torvalds if (ipv6_addr_equal(&ifp->addr, addr)) { 15691da177e4SLinus Torvalds if (dev == NULL || ifp->idev->dev == dev || 15701da177e4SLinus Torvalds !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) { 1571b79d1d54SDavid S. Miller result = ifp; 15721da177e4SLinus Torvalds in6_ifa_hold(ifp); 15731da177e4SLinus Torvalds break; 15741da177e4SLinus Torvalds } 15751da177e4SLinus Torvalds } 15761da177e4SLinus Torvalds } 15775c578aedSstephen hemminger rcu_read_unlock_bh(); 15781da177e4SLinus Torvalds 1579b79d1d54SDavid S. Miller return result; 15801da177e4SLinus Torvalds } 15811da177e4SLinus Torvalds 15821da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */ 15831da177e4SLinus Torvalds 1584cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed) 15851da177e4SLinus Torvalds { 15861da177e4SLinus Torvalds if (ifp->flags&IFA_F_PERMANENT) { 15871da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 1588b7b1bfceSHannes Frederic Sowa addrconf_del_dad_timer(ifp); 15891da177e4SLinus Torvalds ifp->flags |= IFA_F_TENTATIVE; 1590cc411d0bSBrian Haley if (dad_failed) 1591cc411d0bSBrian Haley ifp->flags |= IFA_F_DADFAILED; 15921da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 1593e2577a06SHerbert Xu if (dad_failed) 1594e2577a06SHerbert Xu ipv6_ifa_notify(0, ifp); 15951da177e4SLinus Torvalds in6_ifa_put(ifp); 15961da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 15971da177e4SLinus Torvalds } else if (ifp->flags&IFA_F_TEMPORARY) { 15981da177e4SLinus Torvalds struct inet6_ifaddr *ifpub; 15991da177e4SLinus Torvalds spin_lock_bh(&ifp->lock); 16001da177e4SLinus Torvalds ifpub = ifp->ifpub; 16011da177e4SLinus Torvalds if (ifpub) { 16021da177e4SLinus Torvalds in6_ifa_hold(ifpub); 16031da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 16041da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 16051da177e4SLinus Torvalds in6_ifa_put(ifpub); 16061da177e4SLinus Torvalds } else { 16071da177e4SLinus Torvalds spin_unlock_bh(&ifp->lock); 16081da177e4SLinus Torvalds } 16091da177e4SLinus Torvalds ipv6_del_addr(ifp); 16101da177e4SLinus Torvalds #endif 16111da177e4SLinus Torvalds } else 16121da177e4SLinus Torvalds ipv6_del_addr(ifp); 16131da177e4SLinus Torvalds } 16141da177e4SLinus Torvalds 1615f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp) 1616f2344a13SHerbert Xu { 1617f2344a13SHerbert Xu int err = -ENOENT; 1618f2344a13SHerbert Xu 1619f2344a13SHerbert Xu spin_lock(&ifp->state_lock); 1620f2344a13SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DAD) { 1621f2344a13SHerbert Xu ifp->state = INET6_IFADDR_STATE_POSTDAD; 1622f2344a13SHerbert Xu err = 0; 1623f2344a13SHerbert Xu } 1624f2344a13SHerbert Xu spin_unlock(&ifp->state_lock); 1625f2344a13SHerbert Xu 1626f2344a13SHerbert Xu return err; 1627f2344a13SHerbert Xu } 1628f2344a13SHerbert Xu 16293c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp) 16303c21edbdSYOSHIFUJI Hideaki { 16311b34be74SYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 16329bdd8d40SBrian Haley 1633853dc2e0SUrsula Braun if (addrconf_dad_end(ifp)) { 1634853dc2e0SUrsula Braun in6_ifa_put(ifp); 1635f2344a13SHerbert Xu return; 1636853dc2e0SUrsula Braun } 1637f2344a13SHerbert Xu 1638e87cc472SJoe Perches net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n", 16390522fea6SJens Rosenboom ifp->idev->dev->name, &ifp->addr); 16409bdd8d40SBrian Haley 16411b34be74SYOSHIFUJI Hideaki if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) { 16421b34be74SYOSHIFUJI Hideaki struct in6_addr addr; 16431b34be74SYOSHIFUJI Hideaki 16441b34be74SYOSHIFUJI Hideaki addr.s6_addr32[0] = htonl(0xfe800000); 16451b34be74SYOSHIFUJI Hideaki addr.s6_addr32[1] = 0; 16461b34be74SYOSHIFUJI Hideaki 16471b34be74SYOSHIFUJI Hideaki if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) && 16481b34be74SYOSHIFUJI Hideaki ipv6_addr_equal(&ifp->addr, &addr)) { 16491b34be74SYOSHIFUJI Hideaki /* DAD failed for link-local based on MAC address */ 16501b34be74SYOSHIFUJI Hideaki idev->cnf.disable_ipv6 = 1; 16519bdd8d40SBrian Haley 1652f3213831SJoe Perches pr_info("%s: IPv6 being disabled!\n", 16539bdd8d40SBrian Haley ifp->idev->dev->name); 16541b34be74SYOSHIFUJI Hideaki } 16551b34be74SYOSHIFUJI Hideaki } 16561b34be74SYOSHIFUJI Hideaki 1657cc411d0bSBrian Haley addrconf_dad_stop(ifp, 1); 16583c21edbdSYOSHIFUJI Hideaki } 16591da177e4SLinus Torvalds 16601da177e4SLinus Torvalds /* Join to solicited addr multicast group. */ 16611da177e4SLinus Torvalds 1662b71d1d42SEric Dumazet void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr) 16631da177e4SLinus Torvalds { 16641da177e4SLinus Torvalds struct in6_addr maddr; 16651da177e4SLinus Torvalds 16661da177e4SLinus Torvalds if (dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 16671da177e4SLinus Torvalds return; 16681da177e4SLinus Torvalds 16691da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 16701da177e4SLinus Torvalds ipv6_dev_mc_inc(dev, &maddr); 16711da177e4SLinus Torvalds } 16721da177e4SLinus Torvalds 1673b71d1d42SEric Dumazet void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr) 16741da177e4SLinus Torvalds { 16751da177e4SLinus Torvalds struct in6_addr maddr; 16761da177e4SLinus Torvalds 16771da177e4SLinus Torvalds if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP)) 16781da177e4SLinus Torvalds return; 16791da177e4SLinus Torvalds 16801da177e4SLinus Torvalds addrconf_addr_solict_mult(addr, &maddr); 16811da177e4SLinus Torvalds __ipv6_dev_mc_dec(idev, &maddr); 16821da177e4SLinus Torvalds } 16831da177e4SLinus Torvalds 168420380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp) 16851da177e4SLinus Torvalds { 16861da177e4SLinus Torvalds struct in6_addr addr; 16872bda8a0cSBjørn Mork if (ifp->prefix_len == 127) /* RFC 6164 */ 16882bda8a0cSBjørn Mork return; 16891da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 16901da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 16911da177e4SLinus Torvalds return; 16921da177e4SLinus Torvalds ipv6_dev_ac_inc(ifp->idev->dev, &addr); 16931da177e4SLinus Torvalds } 16941da177e4SLinus Torvalds 169520380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp) 16961da177e4SLinus Torvalds { 16971da177e4SLinus Torvalds struct in6_addr addr; 169832019e65SYOSHIFUJI Hideaki if (ifp->prefix_len == 127) /* RFC 6164 */ 169932019e65SYOSHIFUJI Hideaki return; 17001da177e4SLinus Torvalds ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 17011da177e4SLinus Torvalds if (ipv6_addr_any(&addr)) 17021da177e4SLinus Torvalds return; 17031da177e4SLinus Torvalds __ipv6_dev_ac_dec(ifp->idev, &addr); 17041da177e4SLinus Torvalds } 17051da177e4SLinus Torvalds 1706073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev) 17071da177e4SLinus Torvalds { 17081da177e4SLinus Torvalds if (dev->addr_len != ETH_ALEN) 17091da177e4SLinus Torvalds return -1; 17101da177e4SLinus Torvalds memcpy(eui, dev->dev_addr, 3); 17111da177e4SLinus Torvalds memcpy(eui + 5, dev->dev_addr + 3, 3); 17121da177e4SLinus Torvalds 17131da177e4SLinus Torvalds /* 17141da177e4SLinus Torvalds * The zSeries OSA network cards can be shared among various 17151da177e4SLinus Torvalds * OS instances, but the OSA cards have only one MAC address. 17161da177e4SLinus Torvalds * This leads to duplicate address conflicts in conjunction 17171da177e4SLinus Torvalds * with IPv6 if more than one instance uses the same card. 17181da177e4SLinus Torvalds * 17191da177e4SLinus Torvalds * The driver for these cards can deliver a unique 16-bit 17201da177e4SLinus Torvalds * identifier for each instance sharing the same card. It is 17211da177e4SLinus Torvalds * placed instead of 0xFFFE in the interface identifier. The 17221da177e4SLinus Torvalds * "u" bit of the interface identifier is not inverted in this 17231da177e4SLinus Torvalds * case. Hence the resulting interface identifier has local 17241da177e4SLinus Torvalds * scope according to RFC2373. 17251da177e4SLinus Torvalds */ 17261da177e4SLinus Torvalds if (dev->dev_id) { 17271da177e4SLinus Torvalds eui[3] = (dev->dev_id >> 8) & 0xFF; 17281da177e4SLinus Torvalds eui[4] = dev->dev_id & 0xFF; 17291da177e4SLinus Torvalds } else { 17301da177e4SLinus Torvalds eui[3] = 0xFF; 17311da177e4SLinus Torvalds eui[4] = 0xFE; 17321da177e4SLinus Torvalds eui[0] ^= 2; 17331da177e4SLinus Torvalds } 17341da177e4SLinus Torvalds return 0; 1735073a8e0eSYOSHIFUJI Hideaki } 1736073a8e0eSYOSHIFUJI Hideaki 173706a4c1c5Salex.bluesman.smirnov@gmail.com static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev) 173806a4c1c5Salex.bluesman.smirnov@gmail.com { 173906a4c1c5Salex.bluesman.smirnov@gmail.com if (dev->addr_len != IEEE802154_ADDR_LEN) 174006a4c1c5Salex.bluesman.smirnov@gmail.com return -1; 174106a4c1c5Salex.bluesman.smirnov@gmail.com memcpy(eui, dev->dev_addr, 8); 17425e98a36eSYOSHIFUJI Hideaki / 吉藤英明 eui[0] ^= 2; 174306a4c1c5Salex.bluesman.smirnov@gmail.com return 0; 174406a4c1c5Salex.bluesman.smirnov@gmail.com } 174506a4c1c5Salex.bluesman.smirnov@gmail.com 1746cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev) 1747cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 { 1748cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 union fwnet_hwaddr *ha; 1749cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 1750cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 if (dev->addr_len != FWNET_ALEN) 1751cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return -1; 1752cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 1753cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 ha = (union fwnet_hwaddr *)dev->dev_addr; 1754cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 1755cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id)); 1756cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 eui[0] ^= 2; 1757cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return 0; 1758cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 } 1759cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 1760073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev) 1761073a8e0eSYOSHIFUJI Hideaki { 17621da177e4SLinus Torvalds /* XXX: inherit EUI-64 from other interface -- yoshfuji */ 17631da177e4SLinus Torvalds if (dev->addr_len != ARCNET_ALEN) 17641da177e4SLinus Torvalds return -1; 17651da177e4SLinus Torvalds memset(eui, 0, 7); 17661da177e4SLinus Torvalds eui[7] = *(u8 *)dev->dev_addr; 17671da177e4SLinus Torvalds return 0; 1768073a8e0eSYOSHIFUJI Hideaki } 1769073a8e0eSYOSHIFUJI Hideaki 1770073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev) 1771073a8e0eSYOSHIFUJI Hideaki { 17721da177e4SLinus Torvalds if (dev->addr_len != INFINIBAND_ALEN) 17731da177e4SLinus Torvalds return -1; 17741da177e4SLinus Torvalds memcpy(eui, dev->dev_addr + 12, 8); 17751da177e4SLinus Torvalds eui[0] |= 2; 17761da177e4SLinus Torvalds return 0; 17771da177e4SLinus Torvalds } 1778073a8e0eSYOSHIFUJI Hideaki 1779c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr) 1780dfd982baSYOSHIFUJI Hideaki { 17819af28511SSascha Hlusiak if (addr == 0) 17829af28511SSascha Hlusiak return -1; 1783dfd982baSYOSHIFUJI Hideaki eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) || 1784dfd982baSYOSHIFUJI Hideaki ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) || 1785dfd982baSYOSHIFUJI Hideaki ipv4_is_private_172(addr) || ipv4_is_test_192(addr) || 1786dfd982baSYOSHIFUJI Hideaki ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) || 1787dfd982baSYOSHIFUJI Hideaki ipv4_is_test_198(addr) || ipv4_is_multicast(addr) || 1788dfd982baSYOSHIFUJI Hideaki ipv4_is_lbcast(addr)) ? 0x00 : 0x02; 1789dfd982baSYOSHIFUJI Hideaki eui[1] = 0; 1790dfd982baSYOSHIFUJI Hideaki eui[2] = 0x5E; 1791dfd982baSYOSHIFUJI Hideaki eui[3] = 0xFE; 1792dfd982baSYOSHIFUJI Hideaki memcpy(eui + 4, &addr, 4); 1793dfd982baSYOSHIFUJI Hideaki return 0; 1794dfd982baSYOSHIFUJI Hideaki } 1795dfd982baSYOSHIFUJI Hideaki 1796dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev) 1797dfd982baSYOSHIFUJI Hideaki { 1798dfd982baSYOSHIFUJI Hideaki if (dev->priv_flags & IFF_ISATAP) 1799dfd982baSYOSHIFUJI Hideaki return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 1800dfd982baSYOSHIFUJI Hideaki return -1; 1801dfd982baSYOSHIFUJI Hideaki } 1802dfd982baSYOSHIFUJI Hideaki 1803aee80b54Sstephen hemminger static int addrconf_ifid_gre(u8 *eui, struct net_device *dev) 1804aee80b54Sstephen hemminger { 1805aee80b54Sstephen hemminger return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr); 1806aee80b54Sstephen hemminger } 1807aee80b54Sstephen hemminger 1808073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev) 1809073a8e0eSYOSHIFUJI Hideaki { 1810073a8e0eSYOSHIFUJI Hideaki switch (dev->type) { 1811073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ETHER: 1812073a8e0eSYOSHIFUJI Hideaki case ARPHRD_FDDI: 1813073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_eui48(eui, dev); 1814073a8e0eSYOSHIFUJI Hideaki case ARPHRD_ARCNET: 1815073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_arcnet(eui, dev); 1816073a8e0eSYOSHIFUJI Hideaki case ARPHRD_INFINIBAND: 1817073a8e0eSYOSHIFUJI Hideaki return addrconf_ifid_infiniband(eui, dev); 1818c7dc89c0SFred L. Templin case ARPHRD_SIT: 1819dfd982baSYOSHIFUJI Hideaki return addrconf_ifid_sit(eui, dev); 1820aee80b54Sstephen hemminger case ARPHRD_IPGRE: 1821aee80b54Sstephen hemminger return addrconf_ifid_gre(eui, dev); 182206a4c1c5Salex.bluesman.smirnov@gmail.com case ARPHRD_IEEE802154: 182306a4c1c5Salex.bluesman.smirnov@gmail.com return addrconf_ifid_eui64(eui, dev); 1824cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 case ARPHRD_IEEE1394: 1825cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 return addrconf_ifid_ieee1394(eui, dev); 1826073a8e0eSYOSHIFUJI Hideaki } 18271da177e4SLinus Torvalds return -1; 18281da177e4SLinus Torvalds } 18291da177e4SLinus Torvalds 18301da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev) 18311da177e4SLinus Torvalds { 18321da177e4SLinus Torvalds int err = -1; 18331da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 18341da177e4SLinus Torvalds 18351da177e4SLinus Torvalds read_lock_bh(&idev->lock); 1836502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 18371da177e4SLinus Torvalds if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) { 18381da177e4SLinus Torvalds memcpy(eui, ifp->addr.s6_addr+8, 8); 18391da177e4SLinus Torvalds err = 0; 18401da177e4SLinus Torvalds break; 18411da177e4SLinus Torvalds } 18421da177e4SLinus Torvalds } 18431da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 18441da177e4SLinus Torvalds return err; 18451da177e4SLinus Torvalds } 18461da177e4SLinus Torvalds 18471da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 18481da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */ 1849eb7e0575SSorin Dumitru static void __ipv6_regen_rndid(struct inet6_dev *idev) 18501da177e4SLinus Torvalds { 18511da177e4SLinus Torvalds regen: 1852955189efSYOSHIFUJI Hideaki get_random_bytes(idev->rndid, sizeof(idev->rndid)); 18531da177e4SLinus Torvalds idev->rndid[0] &= ~0x02; 18541da177e4SLinus Torvalds 18551da177e4SLinus Torvalds /* 18561da177e4SLinus Torvalds * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>: 18571da177e4SLinus Torvalds * check if generated address is not inappropriate 18581da177e4SLinus Torvalds * 18591da177e4SLinus Torvalds * - Reserved subnet anycast (RFC 2526) 18601da177e4SLinus Torvalds * 11111101 11....11 1xxxxxxx 1861c7dc89c0SFred L. Templin * - ISATAP (RFC4214) 6.1 18621da177e4SLinus Torvalds * 00-00-5E-FE-xx-xx-xx-xx 18631da177e4SLinus Torvalds * - value 0 18641da177e4SLinus Torvalds * - XXX: already assigned to an address on the device 18651da177e4SLinus Torvalds */ 18661da177e4SLinus Torvalds if (idev->rndid[0] == 0xfd && 18671da177e4SLinus Torvalds (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff && 18681da177e4SLinus Torvalds (idev->rndid[7]&0x80)) 18691da177e4SLinus Torvalds goto regen; 18701da177e4SLinus Torvalds if ((idev->rndid[0]|idev->rndid[1]) == 0) { 18711da177e4SLinus Torvalds if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe) 18721da177e4SLinus Torvalds goto regen; 18731da177e4SLinus Torvalds if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00) 18741da177e4SLinus Torvalds goto regen; 18751da177e4SLinus Torvalds } 18761da177e4SLinus Torvalds } 18771da177e4SLinus Torvalds 18781da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data) 18791da177e4SLinus Torvalds { 18801da177e4SLinus Torvalds struct inet6_dev *idev = (struct inet6_dev *) data; 18811da177e4SLinus Torvalds unsigned long expires; 18821da177e4SLinus Torvalds 18838814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 18841da177e4SLinus Torvalds write_lock_bh(&idev->lock); 18851da177e4SLinus Torvalds 18861da177e4SLinus Torvalds if (idev->dead) 18871da177e4SLinus Torvalds goto out; 18881da177e4SLinus Torvalds 1889eb7e0575SSorin Dumitru __ipv6_regen_rndid(idev); 18901da177e4SLinus Torvalds 18911da177e4SLinus Torvalds expires = jiffies + 18921da177e4SLinus Torvalds idev->cnf.temp_prefered_lft * HZ - 1893784e2710SBen Hutchings idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - 1894784e2710SBen Hutchings idev->cnf.max_desync_factor * HZ; 18951da177e4SLinus Torvalds if (time_before(expires, jiffies)) { 1896f3213831SJoe Perches pr_warn("%s: too short regeneration interval; timer disabled for %s\n", 1897f3213831SJoe Perches __func__, idev->dev->name); 18981da177e4SLinus Torvalds goto out; 18991da177e4SLinus Torvalds } 19001da177e4SLinus Torvalds 19011da177e4SLinus Torvalds if (!mod_timer(&idev->regen_timer, expires)) 19021da177e4SLinus Torvalds in6_dev_hold(idev); 19031da177e4SLinus Torvalds 19041da177e4SLinus Torvalds out: 19051da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 19068814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 19071da177e4SLinus Torvalds in6_dev_put(idev); 19081da177e4SLinus Torvalds } 19091da177e4SLinus Torvalds 1910eb7e0575SSorin Dumitru static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) 19118e5e8f30SEldad Zack { 19121da177e4SLinus Torvalds if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0) 1913eb7e0575SSorin Dumitru __ipv6_regen_rndid(idev); 19141da177e4SLinus Torvalds } 19151da177e4SLinus Torvalds #endif 19161da177e4SLinus Torvalds 19171da177e4SLinus Torvalds /* 19181da177e4SLinus Torvalds * Add prefix route. 19191da177e4SLinus Torvalds */ 19201da177e4SLinus Torvalds 19211da177e4SLinus Torvalds static void 19221da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev, 1923e431b8c0SJamal Hadi Salim unsigned long expires, u32 flags) 19241da177e4SLinus Torvalds { 192586872cb5SThomas Graf struct fib6_config cfg = { 192686872cb5SThomas Graf .fc_table = RT6_TABLE_PREFIX, 192786872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 192886872cb5SThomas Graf .fc_ifindex = dev->ifindex, 192986872cb5SThomas Graf .fc_expires = expires, 193086872cb5SThomas Graf .fc_dst_len = plen, 193186872cb5SThomas Graf .fc_flags = RTF_UP | flags, 1932c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 1933f410a1fbSStephen Hemminger .fc_protocol = RTPROT_KERNEL, 193486872cb5SThomas Graf }; 19351da177e4SLinus Torvalds 19364e3fd7a0SAlexey Dobriyan cfg.fc_dst = *pfx; 19371da177e4SLinus Torvalds 19381da177e4SLinus Torvalds /* Prevent useless cloning on PtP SIT. 19391da177e4SLinus Torvalds This thing is done here expecting that the whole 19401da177e4SLinus Torvalds class of non-broadcast devices need not cloning. 19411da177e4SLinus Torvalds */ 194207a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 19431da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT)) 194486872cb5SThomas Graf cfg.fc_flags |= RTF_NONEXTHOP; 19450be669bbSJoerg Roedel #endif 19461da177e4SLinus Torvalds 194786872cb5SThomas Graf ip6_route_add(&cfg); 19481da177e4SLinus Torvalds } 19491da177e4SLinus Torvalds 195014ef37b6SAndreas Hofmeister 195114ef37b6SAndreas Hofmeister static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx, 195214ef37b6SAndreas Hofmeister int plen, 195314ef37b6SAndreas Hofmeister const struct net_device *dev, 195414ef37b6SAndreas Hofmeister u32 flags, u32 noflags) 195514ef37b6SAndreas Hofmeister { 195614ef37b6SAndreas Hofmeister struct fib6_node *fn; 195714ef37b6SAndreas Hofmeister struct rt6_info *rt = NULL; 195814ef37b6SAndreas Hofmeister struct fib6_table *table; 195914ef37b6SAndreas Hofmeister 196014ef37b6SAndreas Hofmeister table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX); 196114ef37b6SAndreas Hofmeister if (table == NULL) 196214ef37b6SAndreas Hofmeister return NULL; 196314ef37b6SAndreas Hofmeister 19645744dd9bSLi RongQing read_lock_bh(&table->tb6_lock); 196514ef37b6SAndreas Hofmeister fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0); 196614ef37b6SAndreas Hofmeister if (!fn) 196714ef37b6SAndreas Hofmeister goto out; 196814ef37b6SAndreas Hofmeister for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { 1969d1918542SDavid S. Miller if (rt->dst.dev->ifindex != dev->ifindex) 197014ef37b6SAndreas Hofmeister continue; 197114ef37b6SAndreas Hofmeister if ((rt->rt6i_flags & flags) != flags) 197214ef37b6SAndreas Hofmeister continue; 197385da53bfSRomain Kuntz if ((rt->rt6i_flags & noflags) != 0) 197414ef37b6SAndreas Hofmeister continue; 197514ef37b6SAndreas Hofmeister dst_hold(&rt->dst); 197614ef37b6SAndreas Hofmeister break; 197714ef37b6SAndreas Hofmeister } 197814ef37b6SAndreas Hofmeister out: 19795744dd9bSLi RongQing read_unlock_bh(&table->tb6_lock); 198014ef37b6SAndreas Hofmeister return rt; 198114ef37b6SAndreas Hofmeister } 198214ef37b6SAndreas Hofmeister 198314ef37b6SAndreas Hofmeister 19841da177e4SLinus Torvalds /* Create "default" multicast route to the interface */ 19851da177e4SLinus Torvalds 19861da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev) 19871da177e4SLinus Torvalds { 198886872cb5SThomas Graf struct fib6_config cfg = { 198986872cb5SThomas Graf .fc_table = RT6_TABLE_LOCAL, 199086872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 199186872cb5SThomas Graf .fc_ifindex = dev->ifindex, 199286872cb5SThomas Graf .fc_dst_len = 8, 199386872cb5SThomas Graf .fc_flags = RTF_UP, 1994c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 199586872cb5SThomas Graf }; 19961da177e4SLinus Torvalds 199786872cb5SThomas Graf ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0); 199886872cb5SThomas Graf 199986872cb5SThomas Graf ip6_route_add(&cfg); 20001da177e4SLinus Torvalds } 20011da177e4SLinus Torvalds 200207a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 20031da177e4SLinus Torvalds static void sit_route_add(struct net_device *dev) 20041da177e4SLinus Torvalds { 200586872cb5SThomas Graf struct fib6_config cfg = { 200686872cb5SThomas Graf .fc_table = RT6_TABLE_MAIN, 200786872cb5SThomas Graf .fc_metric = IP6_RT_PRIO_ADDRCONF, 200886872cb5SThomas Graf .fc_ifindex = dev->ifindex, 200986872cb5SThomas Graf .fc_dst_len = 96, 201086872cb5SThomas Graf .fc_flags = RTF_UP | RTF_NONEXTHOP, 2011c346dca1SYOSHIFUJI Hideaki .fc_nlinfo.nl_net = dev_net(dev), 201286872cb5SThomas Graf }; 20131da177e4SLinus Torvalds 20141da177e4SLinus Torvalds /* prefix length - 96 bits "::d.d.d.d" */ 201586872cb5SThomas Graf ip6_route_add(&cfg); 20161da177e4SLinus Torvalds } 20170be669bbSJoerg Roedel #endif 20181da177e4SLinus Torvalds 20191da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev) 20201da177e4SLinus Torvalds { 20211da177e4SLinus Torvalds struct inet6_dev *idev; 20221da177e4SLinus Torvalds 20231da177e4SLinus Torvalds ASSERT_RTNL(); 20241da177e4SLinus Torvalds 2025e21e8467SStephen Hemminger idev = ipv6_find_idev(dev); 2026e21e8467SStephen Hemminger if (!idev) 202764e724f6SBrian Haley return ERR_PTR(-ENOBUFS); 202864e724f6SBrian Haley 202964e724f6SBrian Haley if (idev->cnf.disable_ipv6) 203064e724f6SBrian Haley return ERR_PTR(-EACCES); 20311da177e4SLinus Torvalds 20321da177e4SLinus Torvalds /* Add default multicast route */ 20334af04abaSLi Wei if (!(dev->flags & IFF_LOOPBACK)) 20341da177e4SLinus Torvalds addrconf_add_mroute(dev); 20351da177e4SLinus Torvalds 20361da177e4SLinus Torvalds return idev; 20371da177e4SLinus Torvalds } 20381da177e4SLinus Torvalds 2039e6bff995SNeil Horman void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao) 20401da177e4SLinus Torvalds { 20411da177e4SLinus Torvalds struct prefix_info *pinfo; 20421da177e4SLinus Torvalds __u32 valid_lft; 20431da177e4SLinus Torvalds __u32 prefered_lft; 20441da177e4SLinus Torvalds int addr_type; 20451da177e4SLinus Torvalds struct inet6_dev *in6_dev; 204656d417b1SBrian Haley struct net *net = dev_net(dev); 20471da177e4SLinus Torvalds 20481da177e4SLinus Torvalds pinfo = (struct prefix_info *) opt; 20491da177e4SLinus Torvalds 20501da177e4SLinus Torvalds if (len < sizeof(struct prefix_info)) { 20511da177e4SLinus Torvalds ADBG(("addrconf: prefix option too short\n")); 20521da177e4SLinus Torvalds return; 20531da177e4SLinus Torvalds } 20541da177e4SLinus Torvalds 20551da177e4SLinus Torvalds /* 20561da177e4SLinus Torvalds * Validation checks ([ADDRCONF], page 19) 20571da177e4SLinus Torvalds */ 20581da177e4SLinus Torvalds 20591da177e4SLinus Torvalds addr_type = ipv6_addr_type(&pinfo->prefix); 20601da177e4SLinus Torvalds 20611da177e4SLinus Torvalds if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)) 20621da177e4SLinus Torvalds return; 20631da177e4SLinus Torvalds 20641da177e4SLinus Torvalds valid_lft = ntohl(pinfo->valid); 20651da177e4SLinus Torvalds prefered_lft = ntohl(pinfo->prefered); 20661da177e4SLinus Torvalds 20671da177e4SLinus Torvalds if (prefered_lft > valid_lft) { 2068e87cc472SJoe Perches net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n"); 20691da177e4SLinus Torvalds return; 20701da177e4SLinus Torvalds } 20711da177e4SLinus Torvalds 20721da177e4SLinus Torvalds in6_dev = in6_dev_get(dev); 20731da177e4SLinus Torvalds 20741da177e4SLinus Torvalds if (in6_dev == NULL) { 2075e87cc472SJoe Perches net_dbg_ratelimited("addrconf: device %s not configured\n", 2076e87cc472SJoe Perches dev->name); 20771da177e4SLinus Torvalds return; 20781da177e4SLinus Torvalds } 20791da177e4SLinus Torvalds 20801da177e4SLinus Torvalds /* 20811da177e4SLinus Torvalds * Two things going on here: 20821da177e4SLinus Torvalds * 1) Add routes for on-link prefixes 20831da177e4SLinus Torvalds * 2) Configure prefixes with the auto flag set 20841da177e4SLinus Torvalds */ 20851da177e4SLinus Torvalds 20864bed72e4SYOSHIFUJI Hideaki if (pinfo->onlink) { 20874bed72e4SYOSHIFUJI Hideaki struct rt6_info *rt; 20884bed72e4SYOSHIFUJI Hideaki unsigned long rt_expires; 20894bed72e4SYOSHIFUJI Hideaki 20901da177e4SLinus Torvalds /* Avoid arithmetic overflow. Really, we could 20916f704992SYOSHIFUJI Hideaki * save rt_expires in seconds, likely valid_lft, 20926f704992SYOSHIFUJI Hideaki * but it would require division in fib gc, that it 20936f704992SYOSHIFUJI Hideaki * not good. 20941da177e4SLinus Torvalds */ 20954bed72e4SYOSHIFUJI Hideaki if (HZ > USER_HZ) 20964bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, HZ); 20974bed72e4SYOSHIFUJI Hideaki else 20984bed72e4SYOSHIFUJI Hideaki rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ); 20993dd4bc68SYOSHIFUJI Hideaki 21004bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) 21014bed72e4SYOSHIFUJI Hideaki rt_expires *= HZ; 21021da177e4SLinus Torvalds 210314ef37b6SAndreas Hofmeister rt = addrconf_get_prefix_route(&pinfo->prefix, 210414ef37b6SAndreas Hofmeister pinfo->prefix_len, 210514ef37b6SAndreas Hofmeister dev, 210614ef37b6SAndreas Hofmeister RTF_ADDRCONF | RTF_PREFIX_RT, 210714ef37b6SAndreas Hofmeister RTF_GATEWAY | RTF_DEFAULT); 21081da177e4SLinus Torvalds 210914ef37b6SAndreas Hofmeister if (rt) { 21106f704992SYOSHIFUJI Hideaki /* Autoconf prefix route */ 21111da177e4SLinus Torvalds if (valid_lft == 0) { 2112e0a1ad73SThomas Graf ip6_del_rt(rt); 21131da177e4SLinus Torvalds rt = NULL; 21144bed72e4SYOSHIFUJI Hideaki } else if (addrconf_finite_timeout(rt_expires)) { 21156f704992SYOSHIFUJI Hideaki /* not infinity */ 21161716a961SGao feng rt6_set_expires(rt, jiffies + rt_expires); 21176f704992SYOSHIFUJI Hideaki } else { 21181716a961SGao feng rt6_clean_expires(rt); 21191da177e4SLinus Torvalds } 21201da177e4SLinus Torvalds } else if (valid_lft) { 21216f704992SYOSHIFUJI Hideaki clock_t expires = 0; 21224bed72e4SYOSHIFUJI Hideaki int flags = RTF_ADDRCONF | RTF_PREFIX_RT; 21234bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(rt_expires)) { 21246f704992SYOSHIFUJI Hideaki /* not infinity */ 21256f704992SYOSHIFUJI Hideaki flags |= RTF_EXPIRES; 21266f704992SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(rt_expires); 21276f704992SYOSHIFUJI Hideaki } 21281da177e4SLinus Torvalds addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len, 21296f704992SYOSHIFUJI Hideaki dev, expires, flags); 21301da177e4SLinus Torvalds } 213194e187c0SAmerigo Wang ip6_rt_put(rt); 21321da177e4SLinus Torvalds } 21331da177e4SLinus Torvalds 21341da177e4SLinus Torvalds /* Try to figure out our local address for this prefix */ 21351da177e4SLinus Torvalds 21361da177e4SLinus Torvalds if (pinfo->autoconf && in6_dev->cnf.autoconf) { 21371da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 21381da177e4SLinus Torvalds struct in6_addr addr; 21391da177e4SLinus Torvalds int create = 0, update_lft = 0; 2140617fe29dSDaniel Borkmann bool tokenized = false; 21411da177e4SLinus Torvalds 21421da177e4SLinus Torvalds if (pinfo->prefix_len == 64) { 21431da177e4SLinus Torvalds memcpy(&addr, &pinfo->prefix, 8); 2144f53adae4SDaniel Borkmann 2145f53adae4SDaniel Borkmann if (!ipv6_addr_any(&in6_dev->token)) { 2146f53adae4SDaniel Borkmann read_lock_bh(&in6_dev->lock); 2147f53adae4SDaniel Borkmann memcpy(addr.s6_addr + 8, 2148f53adae4SDaniel Borkmann in6_dev->token.s6_addr + 8, 8); 2149f53adae4SDaniel Borkmann read_unlock_bh(&in6_dev->lock); 2150617fe29dSDaniel Borkmann tokenized = true; 2151f53adae4SDaniel Borkmann } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) && 21521da177e4SLinus Torvalds ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) { 21531da177e4SLinus Torvalds in6_dev_put(in6_dev); 21541da177e4SLinus Torvalds return; 21551da177e4SLinus Torvalds } 21561da177e4SLinus Torvalds goto ok; 21571da177e4SLinus Torvalds } 2158e87cc472SJoe Perches net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n", 21591da177e4SLinus Torvalds pinfo->prefix_len); 21601da177e4SLinus Torvalds in6_dev_put(in6_dev); 21611da177e4SLinus Torvalds return; 21621da177e4SLinus Torvalds 21631da177e4SLinus Torvalds ok: 21641da177e4SLinus Torvalds 216553b7997fSYOSHIFUJI Hideaki ifp = ipv6_get_ifaddr(net, &addr, dev, 1); 21661da177e4SLinus Torvalds 21671da177e4SLinus Torvalds if (ifp == NULL && valid_lft) { 21681da177e4SLinus Torvalds int max_addresses = in6_dev->cnf.max_addresses; 216995c385b4SNeil Horman u32 addr_flags = 0; 217095c385b4SNeil Horman 217195c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 217295c385b4SNeil Horman if (in6_dev->cnf.optimistic_dad && 2173e6bff995SNeil Horman !net->ipv6.devconf_all->forwarding && sllao) 217495c385b4SNeil Horman addr_flags = IFA_F_OPTIMISTIC; 217595c385b4SNeil Horman #endif 21761da177e4SLinus Torvalds 21771da177e4SLinus Torvalds /* Do not allow to create too much of autoconfigured 21781da177e4SLinus Torvalds * addresses; this would be too easy way to crash kernel. 21791da177e4SLinus Torvalds */ 21801da177e4SLinus Torvalds if (!max_addresses || 21811da177e4SLinus Torvalds ipv6_count_addresses(in6_dev) < max_addresses) 21821da177e4SLinus Torvalds ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len, 218395c385b4SNeil Horman addr_type&IPV6_ADDR_SCOPE_MASK, 218495c385b4SNeil Horman addr_flags); 21851da177e4SLinus Torvalds 21863f0d2ba0SYOSHIFUJI Hideaki / 吉藤英明 if (IS_ERR_OR_NULL(ifp)) { 21871da177e4SLinus Torvalds in6_dev_put(in6_dev); 21881da177e4SLinus Torvalds return; 21891da177e4SLinus Torvalds } 21901da177e4SLinus Torvalds 21911da177e4SLinus Torvalds update_lft = create = 1; 21921da177e4SLinus Torvalds ifp->cstamp = jiffies; 2193617fe29dSDaniel Borkmann ifp->tokenized = tokenized; 2194cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 21951da177e4SLinus Torvalds } 21961da177e4SLinus Torvalds 21971da177e4SLinus Torvalds if (ifp) { 21981da177e4SLinus Torvalds int flags; 21991da177e4SLinus Torvalds unsigned long now; 22001da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 22011da177e4SLinus Torvalds struct inet6_ifaddr *ift; 22021da177e4SLinus Torvalds #endif 22031da177e4SLinus Torvalds u32 stored_lft; 22041da177e4SLinus Torvalds 22051da177e4SLinus Torvalds /* update lifetime (RFC2462 5.5.3 e) */ 22061da177e4SLinus Torvalds spin_lock(&ifp->lock); 22071da177e4SLinus Torvalds now = jiffies; 22081da177e4SLinus Torvalds if (ifp->valid_lft > (now - ifp->tstamp) / HZ) 22091da177e4SLinus Torvalds stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ; 22101da177e4SLinus Torvalds else 22111da177e4SLinus Torvalds stored_lft = 0; 22121da177e4SLinus Torvalds if (!update_lft && stored_lft) { 22131da177e4SLinus Torvalds if (valid_lft > MIN_VALID_LIFETIME || 22141da177e4SLinus Torvalds valid_lft > stored_lft) 22151da177e4SLinus Torvalds update_lft = 1; 22161da177e4SLinus Torvalds else if (stored_lft <= MIN_VALID_LIFETIME) { 22171da177e4SLinus Torvalds /* valid_lft <= stored_lft is always true */ 2218a1ed0526SBrian Haley /* 2219a1ed0526SBrian Haley * RFC 4862 Section 5.5.3e: 2220a1ed0526SBrian Haley * "Note that the preferred lifetime of 2221a1ed0526SBrian Haley * the corresponding address is always 2222a1ed0526SBrian Haley * reset to the Preferred Lifetime in 2223a1ed0526SBrian Haley * the received Prefix Information 2224a1ed0526SBrian Haley * option, regardless of whether the 2225a1ed0526SBrian Haley * valid lifetime is also reset or 2226a1ed0526SBrian Haley * ignored." 2227a1ed0526SBrian Haley * 2228a1ed0526SBrian Haley * So if the preferred lifetime in 2229a1ed0526SBrian Haley * this advertisement is different 2230a1ed0526SBrian Haley * than what we have stored, but the 2231a1ed0526SBrian Haley * valid lifetime is invalid, just 2232a1ed0526SBrian Haley * reset prefered_lft. 2233a1ed0526SBrian Haley * 2234a1ed0526SBrian Haley * We must set the valid lifetime 2235a1ed0526SBrian Haley * to the stored lifetime since we'll 2236a1ed0526SBrian Haley * be updating the timestamp below, 2237a1ed0526SBrian Haley * else we'll set it back to the 223825985edcSLucas De Marchi * minimum. 2239a1ed0526SBrian Haley */ 2240a1ed0526SBrian Haley if (prefered_lft != ifp->prefered_lft) { 2241a1ed0526SBrian Haley valid_lft = stored_lft; 2242a1ed0526SBrian Haley update_lft = 1; 2243a1ed0526SBrian Haley } 22441da177e4SLinus Torvalds } else { 22451da177e4SLinus Torvalds valid_lft = MIN_VALID_LIFETIME; 22461da177e4SLinus Torvalds if (valid_lft < prefered_lft) 22471da177e4SLinus Torvalds prefered_lft = valid_lft; 22481da177e4SLinus Torvalds update_lft = 1; 22491da177e4SLinus Torvalds } 22501da177e4SLinus Torvalds } 22511da177e4SLinus Torvalds 22521da177e4SLinus Torvalds if (update_lft) { 22531da177e4SLinus Torvalds ifp->valid_lft = valid_lft; 22541da177e4SLinus Torvalds ifp->prefered_lft = prefered_lft; 22551da177e4SLinus Torvalds ifp->tstamp = now; 22561da177e4SLinus Torvalds flags = ifp->flags; 22571da177e4SLinus Torvalds ifp->flags &= ~IFA_F_DEPRECATED; 22581da177e4SLinus Torvalds spin_unlock(&ifp->lock); 22591da177e4SLinus Torvalds 22601da177e4SLinus Torvalds if (!(flags&IFA_F_TENTATIVE)) 22611da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 22621da177e4SLinus Torvalds } else 22631da177e4SLinus Torvalds spin_unlock(&ifp->lock); 22641da177e4SLinus Torvalds 22651da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 22661da177e4SLinus Torvalds read_lock_bh(&in6_dev->lock); 22671da177e4SLinus Torvalds /* update all temporary addresses in the list */ 226876f793e3SLorenzo Colitti list_for_each_entry(ift, &in6_dev->tempaddr_list, 226976f793e3SLorenzo Colitti tmp_list) { 227076f793e3SLorenzo Colitti int age, max_valid, max_prefered; 227176f793e3SLorenzo Colitti 2272c6fbfac2SBenoit Boissinot if (ifp != ift->ifpub) 2273c6fbfac2SBenoit Boissinot continue; 2274c6fbfac2SBenoit Boissinot 227576f793e3SLorenzo Colitti /* 227676f793e3SLorenzo Colitti * RFC 4941 section 3.3: 227776f793e3SLorenzo Colitti * If a received option will extend the lifetime 227876f793e3SLorenzo Colitti * of a public address, the lifetimes of 227976f793e3SLorenzo Colitti * temporary addresses should be extended, 228076f793e3SLorenzo Colitti * subject to the overall constraint that no 228176f793e3SLorenzo Colitti * temporary addresses should ever remain 228276f793e3SLorenzo Colitti * "valid" or "preferred" for a time longer than 228376f793e3SLorenzo Colitti * (TEMP_VALID_LIFETIME) or 228476f793e3SLorenzo Colitti * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), 228576f793e3SLorenzo Colitti * respectively. 228676f793e3SLorenzo Colitti */ 228776f793e3SLorenzo Colitti age = (now - ift->cstamp) / HZ; 228876f793e3SLorenzo Colitti max_valid = in6_dev->cnf.temp_valid_lft - age; 228976f793e3SLorenzo Colitti if (max_valid < 0) 229076f793e3SLorenzo Colitti max_valid = 0; 229176f793e3SLorenzo Colitti 229276f793e3SLorenzo Colitti max_prefered = in6_dev->cnf.temp_prefered_lft - 229376f793e3SLorenzo Colitti in6_dev->cnf.max_desync_factor - 229476f793e3SLorenzo Colitti age; 229576f793e3SLorenzo Colitti if (max_prefered < 0) 229676f793e3SLorenzo Colitti max_prefered = 0; 229776f793e3SLorenzo Colitti 229876f793e3SLorenzo Colitti if (valid_lft > max_valid) 229976f793e3SLorenzo Colitti valid_lft = max_valid; 230076f793e3SLorenzo Colitti 230176f793e3SLorenzo Colitti if (prefered_lft > max_prefered) 230276f793e3SLorenzo Colitti prefered_lft = max_prefered; 230376f793e3SLorenzo Colitti 23041da177e4SLinus Torvalds spin_lock(&ift->lock); 23051da177e4SLinus Torvalds flags = ift->flags; 230676f793e3SLorenzo Colitti ift->valid_lft = valid_lft; 230776f793e3SLorenzo Colitti ift->prefered_lft = prefered_lft; 230876f793e3SLorenzo Colitti ift->tstamp = now; 230976f793e3SLorenzo Colitti if (prefered_lft > 0) 231076f793e3SLorenzo Colitti ift->flags &= ~IFA_F_DEPRECATED; 231176f793e3SLorenzo Colitti 23121da177e4SLinus Torvalds spin_unlock(&ift->lock); 23131da177e4SLinus Torvalds if (!(flags&IFA_F_TENTATIVE)) 23141da177e4SLinus Torvalds ipv6_ifa_notify(0, ift); 23151da177e4SLinus Torvalds } 23161da177e4SLinus Torvalds 2317aed65501SGlenn Wurster if ((create || list_empty(&in6_dev->tempaddr_list)) && in6_dev->cnf.use_tempaddr > 0) { 23181da177e4SLinus Torvalds /* 231976f793e3SLorenzo Colitti * When a new public address is created as 232076f793e3SLorenzo Colitti * described in [ADDRCONF], also create a new 232176f793e3SLorenzo Colitti * temporary address. Also create a temporary 232276f793e3SLorenzo Colitti * address if it's enabled but no temporary 232376f793e3SLorenzo Colitti * address currently exists. 23241da177e4SLinus Torvalds */ 23251da177e4SLinus Torvalds read_unlock_bh(&in6_dev->lock); 23261da177e4SLinus Torvalds ipv6_create_tempaddr(ifp, NULL); 23271da177e4SLinus Torvalds } else { 23281da177e4SLinus Torvalds read_unlock_bh(&in6_dev->lock); 23291da177e4SLinus Torvalds } 23301da177e4SLinus Torvalds #endif 23311da177e4SLinus Torvalds in6_ifa_put(ifp); 23321da177e4SLinus Torvalds addrconf_verify(0); 23331da177e4SLinus Torvalds } 23341da177e4SLinus Torvalds } 23351da177e4SLinus Torvalds inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo); 23361da177e4SLinus Torvalds in6_dev_put(in6_dev); 23371da177e4SLinus Torvalds } 23381da177e4SLinus Torvalds 23391da177e4SLinus Torvalds /* 23401da177e4SLinus Torvalds * Set destination address. 23411da177e4SLinus Torvalds * Special case for SIT interfaces where we create a new "virtual" 23421da177e4SLinus Torvalds * device. 23431da177e4SLinus Torvalds */ 2344af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg) 23451da177e4SLinus Torvalds { 23461da177e4SLinus Torvalds struct in6_ifreq ireq; 23471da177e4SLinus Torvalds struct net_device *dev; 23481da177e4SLinus Torvalds int err = -EINVAL; 23491da177e4SLinus Torvalds 23501da177e4SLinus Torvalds rtnl_lock(); 23511da177e4SLinus Torvalds 23521da177e4SLinus Torvalds err = -EFAULT; 23531da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 23541da177e4SLinus Torvalds goto err_exit; 23551da177e4SLinus Torvalds 2356af284937SDaniel Lezcano dev = __dev_get_by_index(net, ireq.ifr6_ifindex); 23571da177e4SLinus Torvalds 23581da177e4SLinus Torvalds err = -ENODEV; 23591da177e4SLinus Torvalds if (dev == NULL) 23601da177e4SLinus Torvalds goto err_exit; 23611da177e4SLinus Torvalds 236207a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 23631da177e4SLinus Torvalds if (dev->type == ARPHRD_SIT) { 23645bc3eb7eSStephen Hemminger const struct net_device_ops *ops = dev->netdev_ops; 23651da177e4SLinus Torvalds struct ifreq ifr; 23661da177e4SLinus Torvalds struct ip_tunnel_parm p; 23671da177e4SLinus Torvalds 23681da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 23691da177e4SLinus Torvalds if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4)) 23701da177e4SLinus Torvalds goto err_exit; 23711da177e4SLinus Torvalds 23721da177e4SLinus Torvalds memset(&p, 0, sizeof(p)); 23731da177e4SLinus Torvalds p.iph.daddr = ireq.ifr6_addr.s6_addr32[3]; 23741da177e4SLinus Torvalds p.iph.saddr = 0; 23751da177e4SLinus Torvalds p.iph.version = 4; 23761da177e4SLinus Torvalds p.iph.ihl = 5; 23771da177e4SLinus Torvalds p.iph.protocol = IPPROTO_IPV6; 23781da177e4SLinus Torvalds p.iph.ttl = 64; 2379d20b3109SStephen Hemminger ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 23801da177e4SLinus Torvalds 23815bc3eb7eSStephen Hemminger if (ops->ndo_do_ioctl) { 23825bc3eb7eSStephen Hemminger mm_segment_t oldfs = get_fs(); 23835bc3eb7eSStephen Hemminger 23845bc3eb7eSStephen Hemminger set_fs(KERNEL_DS); 23855bc3eb7eSStephen Hemminger err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); 23861da177e4SLinus Torvalds set_fs(oldfs); 23875bc3eb7eSStephen Hemminger } else 23885bc3eb7eSStephen Hemminger err = -EOPNOTSUPP; 23891da177e4SLinus Torvalds 23901da177e4SLinus Torvalds if (err == 0) { 23911da177e4SLinus Torvalds err = -ENOBUFS; 2392af284937SDaniel Lezcano dev = __dev_get_by_name(net, p.name); 2393af284937SDaniel Lezcano if (!dev) 23941da177e4SLinus Torvalds goto err_exit; 23951da177e4SLinus Torvalds err = dev_open(dev); 23961da177e4SLinus Torvalds } 23971da177e4SLinus Torvalds } 23980be669bbSJoerg Roedel #endif 23991da177e4SLinus Torvalds 24001da177e4SLinus Torvalds err_exit: 24011da177e4SLinus Torvalds rtnl_unlock(); 24021da177e4SLinus Torvalds return err; 24031da177e4SLinus Torvalds } 24041da177e4SLinus Torvalds 24051da177e4SLinus Torvalds /* 24061da177e4SLinus Torvalds * Manual configuration of address on an interface 24071da177e4SLinus Torvalds */ 2408b71d1d42SEric Dumazet static int inet6_addr_add(struct net *net, int ifindex, const struct in6_addr *pfx, 2409caeaba79SNicolas Dichtel const struct in6_addr *peer_pfx, 241024ef0da7SThomas Graf unsigned int plen, __u8 ifa_flags, __u32 prefered_lft, 2411af284937SDaniel Lezcano __u32 valid_lft) 24121da177e4SLinus Torvalds { 24131da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 24141da177e4SLinus Torvalds struct inet6_dev *idev; 24151da177e4SLinus Torvalds struct net_device *dev; 24161da177e4SLinus Torvalds int scope; 24176f704992SYOSHIFUJI Hideaki u32 flags; 24186f704992SYOSHIFUJI Hideaki clock_t expires; 24194bed72e4SYOSHIFUJI Hideaki unsigned long timeout; 24201da177e4SLinus Torvalds 24211da177e4SLinus Torvalds ASSERT_RTNL(); 24221da177e4SLinus Torvalds 242324ef0da7SThomas Graf if (plen > 128) 242424ef0da7SThomas Graf return -EINVAL; 242524ef0da7SThomas Graf 24260778769dSNoriaki TAKAMIYA /* check the lifetime */ 24270778769dSNoriaki TAKAMIYA if (!valid_lft || prefered_lft > valid_lft) 24280778769dSNoriaki TAKAMIYA return -EINVAL; 24290778769dSNoriaki TAKAMIYA 2430af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2431af284937SDaniel Lezcano if (!dev) 24321da177e4SLinus Torvalds return -ENODEV; 24331da177e4SLinus Torvalds 243464e724f6SBrian Haley idev = addrconf_add_dev(dev); 243564e724f6SBrian Haley if (IS_ERR(idev)) 243664e724f6SBrian Haley return PTR_ERR(idev); 24371da177e4SLinus Torvalds 24381da177e4SLinus Torvalds scope = ipv6_addr_scope(pfx); 24391da177e4SLinus Torvalds 24404bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(valid_lft, HZ); 24414bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 24424bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 24434bed72e4SYOSHIFUJI Hideaki valid_lft = timeout; 24446f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 24454bed72e4SYOSHIFUJI Hideaki } else { 24464bed72e4SYOSHIFUJI Hideaki expires = 0; 24474bed72e4SYOSHIFUJI Hideaki flags = 0; 24484bed72e4SYOSHIFUJI Hideaki ifa_flags |= IFA_F_PERMANENT; 24496f704992SYOSHIFUJI Hideaki } 24500778769dSNoriaki TAKAMIYA 24514bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(prefered_lft, HZ); 24524bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 24534bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 24540778769dSNoriaki TAKAMIYA ifa_flags |= IFA_F_DEPRECATED; 24554bed72e4SYOSHIFUJI Hideaki prefered_lft = timeout; 24564bed72e4SYOSHIFUJI Hideaki } 24570778769dSNoriaki TAKAMIYA 24580778769dSNoriaki TAKAMIYA ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags); 24590778769dSNoriaki TAKAMIYA 24601da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 246106aebfb7SHerbert Xu spin_lock_bh(&ifp->lock); 24620778769dSNoriaki TAKAMIYA ifp->valid_lft = valid_lft; 24630778769dSNoriaki TAKAMIYA ifp->prefered_lft = prefered_lft; 24640778769dSNoriaki TAKAMIYA ifp->tstamp = jiffies; 2465caeaba79SNicolas Dichtel if (peer_pfx) 2466caeaba79SNicolas Dichtel ifp->peer_addr = *peer_pfx; 246706aebfb7SHerbert Xu spin_unlock_bh(&ifp->lock); 24680778769dSNoriaki TAKAMIYA 246946d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 24706f704992SYOSHIFUJI Hideaki expires, flags); 247195c385b4SNeil Horman /* 247295c385b4SNeil Horman * Note that section 3.1 of RFC 4429 indicates 247395c385b4SNeil Horman * that the Optimistic flag should not be set for 247495c385b4SNeil Horman * manually configured addresses 247595c385b4SNeil Horman */ 2476cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 24771da177e4SLinus Torvalds in6_ifa_put(ifp); 24780778769dSNoriaki TAKAMIYA addrconf_verify(0); 24791da177e4SLinus Torvalds return 0; 24801da177e4SLinus Torvalds } 24811da177e4SLinus Torvalds 24821da177e4SLinus Torvalds return PTR_ERR(ifp); 24831da177e4SLinus Torvalds } 24841da177e4SLinus Torvalds 2485b71d1d42SEric Dumazet static int inet6_addr_del(struct net *net, int ifindex, const struct in6_addr *pfx, 248624ef0da7SThomas Graf unsigned int plen) 24871da177e4SLinus Torvalds { 24881da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 24891da177e4SLinus Torvalds struct inet6_dev *idev; 24901da177e4SLinus Torvalds struct net_device *dev; 24911da177e4SLinus Torvalds 249224ef0da7SThomas Graf if (plen > 128) 249324ef0da7SThomas Graf return -EINVAL; 249424ef0da7SThomas Graf 2495af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifindex); 2496af284937SDaniel Lezcano if (!dev) 24971da177e4SLinus Torvalds return -ENODEV; 24981da177e4SLinus Torvalds 24991da177e4SLinus Torvalds if ((idev = __in6_dev_get(dev)) == NULL) 25001da177e4SLinus Torvalds return -ENXIO; 25011da177e4SLinus Torvalds 25021da177e4SLinus Torvalds read_lock_bh(&idev->lock); 2503502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 25041da177e4SLinus Torvalds if (ifp->prefix_len == plen && 25051da177e4SLinus Torvalds ipv6_addr_equal(pfx, &ifp->addr)) { 25061da177e4SLinus Torvalds in6_ifa_hold(ifp); 25071da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 25081da177e4SLinus Torvalds 25091da177e4SLinus Torvalds ipv6_del_addr(ifp); 25101da177e4SLinus Torvalds return 0; 25111da177e4SLinus Torvalds } 25121da177e4SLinus Torvalds } 25131da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 25141da177e4SLinus Torvalds return -EADDRNOTAVAIL; 25151da177e4SLinus Torvalds } 25161da177e4SLinus Torvalds 25171da177e4SLinus Torvalds 2518af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg) 25191da177e4SLinus Torvalds { 25201da177e4SLinus Torvalds struct in6_ifreq ireq; 25211da177e4SLinus Torvalds int err; 25221da177e4SLinus Torvalds 2523af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 25241da177e4SLinus Torvalds return -EPERM; 25251da177e4SLinus Torvalds 25261da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 25271da177e4SLinus Torvalds return -EFAULT; 25281da177e4SLinus Torvalds 25291da177e4SLinus Torvalds rtnl_lock(); 2530caeaba79SNicolas Dichtel err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL, 2531af284937SDaniel Lezcano ireq.ifr6_prefixlen, IFA_F_PERMANENT, 2532af284937SDaniel Lezcano INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 25331da177e4SLinus Torvalds rtnl_unlock(); 25341da177e4SLinus Torvalds return err; 25351da177e4SLinus Torvalds } 25361da177e4SLinus Torvalds 2537af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg) 25381da177e4SLinus Torvalds { 25391da177e4SLinus Torvalds struct in6_ifreq ireq; 25401da177e4SLinus Torvalds int err; 25411da177e4SLinus Torvalds 2542af31f412SEric W. Biederman if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 25431da177e4SLinus Torvalds return -EPERM; 25441da177e4SLinus Torvalds 25451da177e4SLinus Torvalds if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq))) 25461da177e4SLinus Torvalds return -EFAULT; 25471da177e4SLinus Torvalds 25481da177e4SLinus Torvalds rtnl_lock(); 2549af284937SDaniel Lezcano err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, 2550af284937SDaniel Lezcano ireq.ifr6_prefixlen); 25511da177e4SLinus Torvalds rtnl_unlock(); 25521da177e4SLinus Torvalds return err; 25531da177e4SLinus Torvalds } 25541da177e4SLinus Torvalds 2555b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr, 2556b5f348e5SIlpo Järvinen int plen, int scope) 2557b5f348e5SIlpo Järvinen { 2558b5f348e5SIlpo Järvinen struct inet6_ifaddr *ifp; 2559b5f348e5SIlpo Järvinen 2560b5f348e5SIlpo Järvinen ifp = ipv6_add_addr(idev, addr, plen, scope, IFA_F_PERMANENT); 2561b5f348e5SIlpo Järvinen if (!IS_ERR(ifp)) { 2562b5f348e5SIlpo Järvinen spin_lock_bh(&ifp->lock); 2563b5f348e5SIlpo Järvinen ifp->flags &= ~IFA_F_TENTATIVE; 2564b5f348e5SIlpo Järvinen spin_unlock_bh(&ifp->lock); 2565b5f348e5SIlpo Järvinen ipv6_ifa_notify(RTM_NEWADDR, ifp); 2566b5f348e5SIlpo Järvinen in6_ifa_put(ifp); 2567b5f348e5SIlpo Järvinen } 2568b5f348e5SIlpo Järvinen } 2569b5f348e5SIlpo Järvinen 257007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 25711da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev) 25721da177e4SLinus Torvalds { 25731da177e4SLinus Torvalds struct in6_addr addr; 25741da177e4SLinus Torvalds struct net_device *dev; 2575c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 25761da177e4SLinus Torvalds int scope; 25771da177e4SLinus Torvalds 25781da177e4SLinus Torvalds ASSERT_RTNL(); 25791da177e4SLinus Torvalds 25801da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr)); 25811da177e4SLinus Torvalds memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4); 25821da177e4SLinus Torvalds 25831da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) { 25841da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xfe800000); 25851da177e4SLinus Torvalds scope = IFA_LINK; 25861da177e4SLinus Torvalds } else { 25871da177e4SLinus Torvalds scope = IPV6_ADDR_COMPATv4; 25881da177e4SLinus Torvalds } 25891da177e4SLinus Torvalds 25901da177e4SLinus Torvalds if (addr.s6_addr32[3]) { 2591b5f348e5SIlpo Järvinen add_addr(idev, &addr, 128, scope); 25921da177e4SLinus Torvalds return; 25931da177e4SLinus Torvalds } 25941da177e4SLinus Torvalds 25956fda7350SBenjamin Thery for_each_netdev(net, dev) { 2596e5ed6399SHerbert Xu struct in_device *in_dev = __in_dev_get_rtnl(dev); 25971da177e4SLinus Torvalds if (in_dev && (dev->flags & IFF_UP)) { 25981da177e4SLinus Torvalds struct in_ifaddr *ifa; 25991da177e4SLinus Torvalds 26001da177e4SLinus Torvalds int flag = scope; 26011da177e4SLinus Torvalds 26021da177e4SLinus Torvalds for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { 26031da177e4SLinus Torvalds int plen; 26041da177e4SLinus Torvalds 26051da177e4SLinus Torvalds addr.s6_addr32[3] = ifa->ifa_local; 26061da177e4SLinus Torvalds 26071da177e4SLinus Torvalds if (ifa->ifa_scope == RT_SCOPE_LINK) 26081da177e4SLinus Torvalds continue; 26091da177e4SLinus Torvalds if (ifa->ifa_scope >= RT_SCOPE_HOST) { 26101da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) 26111da177e4SLinus Torvalds continue; 26121da177e4SLinus Torvalds flag |= IFA_HOST; 26131da177e4SLinus Torvalds } 26141da177e4SLinus Torvalds if (idev->dev->flags&IFF_POINTOPOINT) 26151da177e4SLinus Torvalds plen = 64; 26161da177e4SLinus Torvalds else 26171da177e4SLinus Torvalds plen = 96; 26181da177e4SLinus Torvalds 2619b5f348e5SIlpo Järvinen add_addr(idev, &addr, plen, flag); 26201da177e4SLinus Torvalds } 26211da177e4SLinus Torvalds } 26221da177e4SLinus Torvalds } 26231da177e4SLinus Torvalds } 26240be669bbSJoerg Roedel #endif 26251da177e4SLinus Torvalds 26261da177e4SLinus Torvalds static void init_loopback(struct net_device *dev) 26271da177e4SLinus Torvalds { 26281da177e4SLinus Torvalds struct inet6_dev *idev; 262925fb6ca4SBalakumaran Kannan struct net_device *sp_dev; 263025fb6ca4SBalakumaran Kannan struct inet6_ifaddr *sp_ifa; 263125fb6ca4SBalakumaran Kannan struct rt6_info *sp_rt; 26321da177e4SLinus Torvalds 26331da177e4SLinus Torvalds /* ::1 */ 26341da177e4SLinus Torvalds 26351da177e4SLinus Torvalds ASSERT_RTNL(); 26361da177e4SLinus Torvalds 26371da177e4SLinus Torvalds if ((idev = ipv6_find_idev(dev)) == NULL) { 263891df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 26391da177e4SLinus Torvalds return; 26401da177e4SLinus Torvalds } 26411da177e4SLinus Torvalds 2642b5f348e5SIlpo Järvinen add_addr(idev, &in6addr_loopback, 128, IFA_HOST); 264325fb6ca4SBalakumaran Kannan 264425fb6ca4SBalakumaran Kannan /* Add routes to other interface's IPv6 addresses */ 264525fb6ca4SBalakumaran Kannan for_each_netdev(dev_net(dev), sp_dev) { 264625fb6ca4SBalakumaran Kannan if (!strcmp(sp_dev->name, dev->name)) 264725fb6ca4SBalakumaran Kannan continue; 264825fb6ca4SBalakumaran Kannan 264925fb6ca4SBalakumaran Kannan idev = __in6_dev_get(sp_dev); 265025fb6ca4SBalakumaran Kannan if (!idev) 265125fb6ca4SBalakumaran Kannan continue; 265225fb6ca4SBalakumaran Kannan 265325fb6ca4SBalakumaran Kannan read_lock_bh(&idev->lock); 265425fb6ca4SBalakumaran Kannan list_for_each_entry(sp_ifa, &idev->addr_list, if_list) { 265525fb6ca4SBalakumaran Kannan 265625fb6ca4SBalakumaran Kannan if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE)) 265725fb6ca4SBalakumaran Kannan continue; 265825fb6ca4SBalakumaran Kannan 265925fb6ca4SBalakumaran Kannan sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, 0); 266025fb6ca4SBalakumaran Kannan 266125fb6ca4SBalakumaran Kannan /* Failure cases are ignored */ 2662534c8779SGao feng if (!IS_ERR(sp_rt)) { 2663534c8779SGao feng sp_ifa->rt = sp_rt; 266425fb6ca4SBalakumaran Kannan ip6_ins_rt(sp_rt); 266525fb6ca4SBalakumaran Kannan } 2666534c8779SGao feng } 266725fb6ca4SBalakumaran Kannan read_unlock_bh(&idev->lock); 266825fb6ca4SBalakumaran Kannan } 26691da177e4SLinus Torvalds } 26701da177e4SLinus Torvalds 2671b71d1d42SEric Dumazet static void addrconf_add_linklocal(struct inet6_dev *idev, const struct in6_addr *addr) 26721da177e4SLinus Torvalds { 26731da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 267495c385b4SNeil Horman u32 addr_flags = IFA_F_PERMANENT; 26751da177e4SLinus Torvalds 267695c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 267795c385b4SNeil Horman if (idev->cnf.optimistic_dad && 2678702beb87SDavid Miller !dev_net(idev->dev)->ipv6.devconf_all->forwarding) 267995c385b4SNeil Horman addr_flags |= IFA_F_OPTIMISTIC; 268095c385b4SNeil Horman #endif 268195c385b4SNeil Horman 268295c385b4SNeil Horman 268395c385b4SNeil Horman ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags); 26841da177e4SLinus Torvalds if (!IS_ERR(ifp)) { 268546d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0); 2686cf22f9a2SDavid S. Miller addrconf_dad_start(ifp); 26871da177e4SLinus Torvalds in6_ifa_put(ifp); 26881da177e4SLinus Torvalds } 26891da177e4SLinus Torvalds } 26901da177e4SLinus Torvalds 26911da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev) 26921da177e4SLinus Torvalds { 26931da177e4SLinus Torvalds struct in6_addr addr; 26941da177e4SLinus Torvalds struct inet6_dev *idev; 26951da177e4SLinus Torvalds 26961da177e4SLinus Torvalds ASSERT_RTNL(); 26971da177e4SLinus Torvalds 269874235a25SHerbert Xu if ((dev->type != ARPHRD_ETHER) && 269974235a25SHerbert Xu (dev->type != ARPHRD_FDDI) && 270074235a25SHerbert Xu (dev->type != ARPHRD_ARCNET) && 270106a4c1c5Salex.bluesman.smirnov@gmail.com (dev->type != ARPHRD_INFINIBAND) && 2702cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 (dev->type != ARPHRD_IEEE802154) && 2703cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 (dev->type != ARPHRD_IEEE1394)) { 270474235a25SHerbert Xu /* Alas, we support only Ethernet autoconfiguration. */ 270574235a25SHerbert Xu return; 270674235a25SHerbert Xu } 270774235a25SHerbert Xu 27081da177e4SLinus Torvalds idev = addrconf_add_dev(dev); 270964e724f6SBrian Haley if (IS_ERR(idev)) 27101da177e4SLinus Torvalds return; 27111da177e4SLinus Torvalds 27121da177e4SLinus Torvalds memset(&addr, 0, sizeof(struct in6_addr)); 27131da177e4SLinus Torvalds addr.s6_addr32[0] = htonl(0xFE800000); 27141da177e4SLinus Torvalds 27151da177e4SLinus Torvalds if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0) 27161da177e4SLinus Torvalds addrconf_add_linklocal(idev, &addr); 27171da177e4SLinus Torvalds } 27181da177e4SLinus Torvalds 271907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 27201da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev) 27211da177e4SLinus Torvalds { 27221da177e4SLinus Torvalds struct inet6_dev *idev; 27231da177e4SLinus Torvalds 27241da177e4SLinus Torvalds ASSERT_RTNL(); 27251da177e4SLinus Torvalds 27261da177e4SLinus Torvalds /* 27271da177e4SLinus Torvalds * Configure the tunnel with one of our IPv4 27281da177e4SLinus Torvalds * addresses... we should configure all of 27291da177e4SLinus Torvalds * our v4 addrs in the tunnel 27301da177e4SLinus Torvalds */ 27311da177e4SLinus Torvalds 27321da177e4SLinus Torvalds if ((idev = ipv6_find_idev(dev)) == NULL) { 273391df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 27341da177e4SLinus Torvalds return; 27351da177e4SLinus Torvalds } 27361da177e4SLinus Torvalds 2737c7dc89c0SFred L. Templin if (dev->priv_flags & IFF_ISATAP) { 2738c7dc89c0SFred L. Templin struct in6_addr addr; 2739c7dc89c0SFred L. Templin 2740c7dc89c0SFred L. Templin ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 2741c7dc89c0SFred L. Templin addrconf_prefix_route(&addr, 64, dev, 0, 0); 2742c7dc89c0SFred L. Templin if (!ipv6_generate_eui64(addr.s6_addr + 8, dev)) 2743c7dc89c0SFred L. Templin addrconf_add_linklocal(idev, &addr); 2744c7dc89c0SFred L. Templin return; 2745c7dc89c0SFred L. Templin } 2746c7dc89c0SFred L. Templin 27471da177e4SLinus Torvalds sit_add_v4_addrs(idev); 27481da177e4SLinus Torvalds 274962b54dd9SNicolas Dichtel if (dev->flags&IFF_POINTOPOINT) 27501da177e4SLinus Torvalds addrconf_add_mroute(dev); 275162b54dd9SNicolas Dichtel else 27521da177e4SLinus Torvalds sit_route_add(dev); 27531da177e4SLinus Torvalds } 27540be669bbSJoerg Roedel #endif 27551da177e4SLinus Torvalds 275607a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE) 2757aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev) 2758aee80b54Sstephen hemminger { 2759aee80b54Sstephen hemminger struct inet6_dev *idev; 2760aee80b54Sstephen hemminger struct in6_addr addr; 2761aee80b54Sstephen hemminger 2762aee80b54Sstephen hemminger ASSERT_RTNL(); 2763aee80b54Sstephen hemminger 2764aee80b54Sstephen hemminger if ((idev = ipv6_find_idev(dev)) == NULL) { 276591df42beSJoe Perches pr_debug("%s: add_dev failed\n", __func__); 2766aee80b54Sstephen hemminger return; 2767aee80b54Sstephen hemminger } 2768aee80b54Sstephen hemminger 2769aee80b54Sstephen hemminger ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 2770aee80b54Sstephen hemminger addrconf_prefix_route(&addr, 64, dev, 0, 0); 2771aee80b54Sstephen hemminger 2772aee80b54Sstephen hemminger if (!ipv6_generate_eui64(addr.s6_addr + 8, dev)) 2773aee80b54Sstephen hemminger addrconf_add_linklocal(idev, &addr); 2774aee80b54Sstephen hemminger } 2775aee80b54Sstephen hemminger #endif 2776aee80b54Sstephen hemminger 27771da177e4SLinus Torvalds static inline int 27781da177e4SLinus Torvalds ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev) 27791da177e4SLinus Torvalds { 27801da177e4SLinus Torvalds struct in6_addr lladdr; 27811da177e4SLinus Torvalds 278295c385b4SNeil Horman if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) { 27831da177e4SLinus Torvalds addrconf_add_linklocal(idev, &lladdr); 27841da177e4SLinus Torvalds return 0; 27851da177e4SLinus Torvalds } 27861da177e4SLinus Torvalds return -1; 27871da177e4SLinus Torvalds } 27881da177e4SLinus Torvalds 27891da177e4SLinus Torvalds static void ip6_tnl_add_linklocal(struct inet6_dev *idev) 27901da177e4SLinus Torvalds { 27911da177e4SLinus Torvalds struct net_device *link_dev; 2792c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 27931da177e4SLinus Torvalds 27941da177e4SLinus Torvalds /* first try to inherit the link-local address from the link device */ 27951da177e4SLinus Torvalds if (idev->dev->iflink && 27966fda7350SBenjamin Thery (link_dev = __dev_get_by_index(net, idev->dev->iflink))) { 27971da177e4SLinus Torvalds if (!ipv6_inherit_linklocal(idev, link_dev)) 27981da177e4SLinus Torvalds return; 27991da177e4SLinus Torvalds } 28001da177e4SLinus Torvalds /* then try to inherit it from any device */ 28016fda7350SBenjamin Thery for_each_netdev(net, link_dev) { 28021da177e4SLinus Torvalds if (!ipv6_inherit_linklocal(idev, link_dev)) 28031da177e4SLinus Torvalds return; 28041da177e4SLinus Torvalds } 280591df42beSJoe Perches pr_debug("init ip6-ip6: add_linklocal failed\n"); 28061da177e4SLinus Torvalds } 28071da177e4SLinus Torvalds 28081da177e4SLinus Torvalds /* 28091da177e4SLinus Torvalds * Autoconfigure tunnel with a link-local address so routing protocols, 28101da177e4SLinus Torvalds * DHCPv6, MLD etc. can be run over the virtual link 28111da177e4SLinus Torvalds */ 28121da177e4SLinus Torvalds 28131da177e4SLinus Torvalds static void addrconf_ip6_tnl_config(struct net_device *dev) 28141da177e4SLinus Torvalds { 28151da177e4SLinus Torvalds struct inet6_dev *idev; 28161da177e4SLinus Torvalds 28171da177e4SLinus Torvalds ASSERT_RTNL(); 28181da177e4SLinus Torvalds 2819e21e8467SStephen Hemminger idev = addrconf_add_dev(dev); 282064e724f6SBrian Haley if (IS_ERR(idev)) { 282191df42beSJoe Perches pr_debug("init ip6-ip6: add_dev failed\n"); 28221da177e4SLinus Torvalds return; 28231da177e4SLinus Torvalds } 28241da177e4SLinus Torvalds ip6_tnl_add_linklocal(idev); 28251da177e4SLinus Torvalds } 28261da177e4SLinus Torvalds 28271da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event, 2828351638e7SJiri Pirko void *ptr) 28291da177e4SLinus Torvalds { 2830351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 2831748e2d93SEric Dumazet struct inet6_dev *idev = __in6_dev_get(dev); 2832c5e33bddSYOSHIFUJI Hideaki int run_pending = 0; 2833b217d616SHerbert Xu int err; 28341da177e4SLinus Torvalds 28351da177e4SLinus Torvalds switch (event) { 283645ba9dd2SYOSHIFUJI Hideaki case NETDEV_REGISTER: 283774235a25SHerbert Xu if (!idev && dev->mtu >= IPV6_MIN_MTU) { 283845ba9dd2SYOSHIFUJI Hideaki idev = ipv6_add_dev(dev); 283945ba9dd2SYOSHIFUJI Hideaki if (!idev) 2840b217d616SHerbert Xu return notifier_from_errno(-ENOMEM); 284145ba9dd2SYOSHIFUJI Hideaki } 284245ba9dd2SYOSHIFUJI Hideaki break; 2843bcdd553fSStephen Hemminger 28441da177e4SLinus Torvalds case NETDEV_UP: 28453c21edbdSYOSHIFUJI Hideaki case NETDEV_CHANGE: 2846c2edacf8SJay Vosburgh if (dev->flags & IFF_SLAVE) 2847c2edacf8SJay Vosburgh break; 2848c2edacf8SJay Vosburgh 28493c21edbdSYOSHIFUJI Hideaki if (event == NETDEV_UP) { 2850f24e3d65SMitsuru Chinen if (!addrconf_qdisc_ok(dev)) { 28513c21edbdSYOSHIFUJI Hideaki /* device is not ready yet. */ 2852f3213831SJoe Perches pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n", 28533c21edbdSYOSHIFUJI Hideaki dev->name); 28543c21edbdSYOSHIFUJI Hideaki break; 28553c21edbdSYOSHIFUJI Hideaki } 285699081049SKristian Slavov 2857d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU) 2858d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev); 2859d31c7b8fSEvgeniy Polyakov 2860e3ec6cfcSBenjamin Thery if (idev) { 286199081049SKristian Slavov idev->if_flags |= IF_READY; 2862e3ec6cfcSBenjamin Thery run_pending = 1; 2863e3ec6cfcSBenjamin Thery } 28643c21edbdSYOSHIFUJI Hideaki } else { 2865f24e3d65SMitsuru Chinen if (!addrconf_qdisc_ok(dev)) { 28663c21edbdSYOSHIFUJI Hideaki /* device is still not ready. */ 28673c21edbdSYOSHIFUJI Hideaki break; 28683c21edbdSYOSHIFUJI Hideaki } 28693c21edbdSYOSHIFUJI Hideaki 28703c21edbdSYOSHIFUJI Hideaki if (idev) { 2871bcdd553fSStephen Hemminger if (idev->if_flags & IF_READY) 28723c21edbdSYOSHIFUJI Hideaki /* device is already configured. */ 28733c21edbdSYOSHIFUJI Hideaki break; 28743c21edbdSYOSHIFUJI Hideaki idev->if_flags |= IF_READY; 28753c21edbdSYOSHIFUJI Hideaki } 28763c21edbdSYOSHIFUJI Hideaki 2877f3213831SJoe Perches pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n", 28783c21edbdSYOSHIFUJI Hideaki dev->name); 28793c21edbdSYOSHIFUJI Hideaki 2880c5e33bddSYOSHIFUJI Hideaki run_pending = 1; 28813c21edbdSYOSHIFUJI Hideaki } 28823c21edbdSYOSHIFUJI Hideaki 28831da177e4SLinus Torvalds switch (dev->type) { 288407a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT) 28851da177e4SLinus Torvalds case ARPHRD_SIT: 28861da177e4SLinus Torvalds addrconf_sit_config(dev); 28871da177e4SLinus Torvalds break; 28880be669bbSJoerg Roedel #endif 288907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE) 2890aee80b54Sstephen hemminger case ARPHRD_IPGRE: 2891aee80b54Sstephen hemminger addrconf_gre_config(dev); 2892aee80b54Sstephen hemminger break; 2893aee80b54Sstephen hemminger #endif 28941da177e4SLinus Torvalds case ARPHRD_TUNNEL6: 28951da177e4SLinus Torvalds addrconf_ip6_tnl_config(dev); 28961da177e4SLinus Torvalds break; 28971da177e4SLinus Torvalds case ARPHRD_LOOPBACK: 28981da177e4SLinus Torvalds init_loopback(dev); 28991da177e4SLinus Torvalds break; 29001da177e4SLinus Torvalds 29011da177e4SLinus Torvalds default: 29021da177e4SLinus Torvalds addrconf_dev_config(dev); 29031da177e4SLinus Torvalds break; 29043ff50b79SStephen Hemminger } 2905bcdd553fSStephen Hemminger 29061da177e4SLinus Torvalds if (idev) { 2907c5e33bddSYOSHIFUJI Hideaki if (run_pending) 2908c5e33bddSYOSHIFUJI Hideaki addrconf_dad_run(idev); 2909c5e33bddSYOSHIFUJI Hideaki 2910bcdd553fSStephen Hemminger /* 2911bcdd553fSStephen Hemminger * If the MTU changed during the interface down, 2912bcdd553fSStephen Hemminger * when the interface up, the changed MTU must be 2913bcdd553fSStephen Hemminger * reflected in the idev as well as routers. 29141da177e4SLinus Torvalds */ 2915bcdd553fSStephen Hemminger if (idev->cnf.mtu6 != dev->mtu && 2916bcdd553fSStephen Hemminger dev->mtu >= IPV6_MIN_MTU) { 29171da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 29181da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 29191da177e4SLinus Torvalds } 29201da177e4SLinus Torvalds idev->tstamp = jiffies; 29211da177e4SLinus Torvalds inet6_ifinfo_notify(RTM_NEWLINK, idev); 2922bcdd553fSStephen Hemminger 2923bcdd553fSStephen Hemminger /* 2924bcdd553fSStephen Hemminger * If the changed mtu during down is lower than 2925bcdd553fSStephen Hemminger * IPV6_MIN_MTU stop IPv6 on this interface. 29261da177e4SLinus Torvalds */ 29271da177e4SLinus Torvalds if (dev->mtu < IPV6_MIN_MTU) 2928bcdd553fSStephen Hemminger addrconf_ifdown(dev, 1); 29291da177e4SLinus Torvalds } 29301da177e4SLinus Torvalds break; 29311da177e4SLinus Torvalds 29321da177e4SLinus Torvalds case NETDEV_CHANGEMTU: 29331da177e4SLinus Torvalds if (idev && dev->mtu >= IPV6_MIN_MTU) { 29341da177e4SLinus Torvalds rt6_mtu_change(dev, dev->mtu); 29351da177e4SLinus Torvalds idev->cnf.mtu6 = dev->mtu; 29361da177e4SLinus Torvalds break; 29371da177e4SLinus Torvalds } 29381da177e4SLinus Torvalds 2939d31c7b8fSEvgeniy Polyakov if (!idev && dev->mtu >= IPV6_MIN_MTU) { 2940d31c7b8fSEvgeniy Polyakov idev = ipv6_add_dev(dev); 2941d31c7b8fSEvgeniy Polyakov if (idev) 2942d31c7b8fSEvgeniy Polyakov break; 2943d31c7b8fSEvgeniy Polyakov } 2944d31c7b8fSEvgeniy Polyakov 2945bcdd553fSStephen Hemminger /* 2946bcdd553fSStephen Hemminger * MTU falled under IPV6_MIN_MTU. 2947bcdd553fSStephen Hemminger * Stop IPv6 on this interface. 2948bcdd553fSStephen Hemminger */ 29491da177e4SLinus Torvalds 29501da177e4SLinus Torvalds case NETDEV_DOWN: 29511da177e4SLinus Torvalds case NETDEV_UNREGISTER: 29521da177e4SLinus Torvalds /* 29531da177e4SLinus Torvalds * Remove all addresses from this interface. 29541da177e4SLinus Torvalds */ 29551da177e4SLinus Torvalds addrconf_ifdown(dev, event != NETDEV_DOWN); 29561da177e4SLinus Torvalds break; 29573c21edbdSYOSHIFUJI Hideaki 29581da177e4SLinus Torvalds case NETDEV_CHANGENAME: 29591da177e4SLinus Torvalds if (idev) { 29605632c515SStephen Hemminger snmp6_unregister_dev(idev); 2961408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 2962f52295a9SPavel Emelyanov addrconf_sysctl_register(idev); 2963b217d616SHerbert Xu err = snmp6_register_dev(idev); 2964b217d616SHerbert Xu if (err) 2965b217d616SHerbert Xu return notifier_from_errno(err); 29665632c515SStephen Hemminger } 29671da177e4SLinus Torvalds break; 2968bcdd553fSStephen Hemminger 296993d9b7d7SJiri Pirko case NETDEV_PRE_TYPE_CHANGE: 297093d9b7d7SJiri Pirko case NETDEV_POST_TYPE_CHANGE: 297193d9b7d7SJiri Pirko addrconf_type_change(dev, event); 297275c78500SMoni Shoua break; 29733ff50b79SStephen Hemminger } 29741da177e4SLinus Torvalds 29751da177e4SLinus Torvalds return NOTIFY_OK; 29761da177e4SLinus Torvalds } 29771da177e4SLinus Torvalds 29781da177e4SLinus Torvalds /* 29791da177e4SLinus Torvalds * addrconf module should be notified of a device going up 29801da177e4SLinus Torvalds */ 29811da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = { 29821da177e4SLinus Torvalds .notifier_call = addrconf_notify, 29831da177e4SLinus Torvalds }; 29841da177e4SLinus Torvalds 298593d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event) 298675c78500SMoni Shoua { 298775c78500SMoni Shoua struct inet6_dev *idev; 298875c78500SMoni Shoua ASSERT_RTNL(); 298975c78500SMoni Shoua 299075c78500SMoni Shoua idev = __in6_dev_get(dev); 299175c78500SMoni Shoua 299293d9b7d7SJiri Pirko if (event == NETDEV_POST_TYPE_CHANGE) 299375c78500SMoni Shoua ipv6_mc_remap(idev); 299493d9b7d7SJiri Pirko else if (event == NETDEV_PRE_TYPE_CHANGE) 299575c78500SMoni Shoua ipv6_mc_unmap(idev); 299675c78500SMoni Shoua } 299775c78500SMoni Shoua 29981da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how) 29991da177e4SLinus Torvalds { 3000c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(dev); 3001502a2ffdSstephen hemminger struct inet6_dev *idev; 3002502a2ffdSstephen hemminger struct inet6_ifaddr *ifa; 300373a8bd74SDavid S. Miller int state, i; 30041da177e4SLinus Torvalds 30051da177e4SLinus Torvalds ASSERT_RTNL(); 30061da177e4SLinus Torvalds 3007f3db4851SDaniel Lezcano rt6_ifdown(net, dev); 300873a8bd74SDavid S. Miller neigh_ifdown(&nd_tbl, dev); 30091da177e4SLinus Torvalds 30101da177e4SLinus Torvalds idev = __in6_dev_get(dev); 30111da177e4SLinus Torvalds if (idev == NULL) 30121da177e4SLinus Torvalds return -ENODEV; 30131da177e4SLinus Torvalds 3014bcdd553fSStephen Hemminger /* 3015bcdd553fSStephen Hemminger * Step 1: remove reference to ipv6 device from parent device. 3016bcdd553fSStephen Hemminger * Do not dev_put! 30171da177e4SLinus Torvalds */ 3018439e2385SDenis V. Lunev if (how) { 30191da177e4SLinus Torvalds idev->dead = 1; 30208814c4b5SYOSHIFUJI Hideaki 30218814c4b5SYOSHIFUJI Hideaki /* protected by rtnl_lock */ 3022a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(dev->ip6_ptr, NULL); 30231da177e4SLinus Torvalds 30241da177e4SLinus Torvalds /* Step 1.5: remove snmp6 entry */ 30251da177e4SLinus Torvalds snmp6_unregister_dev(idev); 30261da177e4SLinus Torvalds 30271da177e4SLinus Torvalds } 30281da177e4SLinus Torvalds 302973a8bd74SDavid S. Miller /* Step 2: clear hash table */ 303073a8bd74SDavid S. Miller for (i = 0; i < IN6_ADDR_HSIZE; i++) { 303173a8bd74SDavid S. Miller struct hlist_head *h = &inet6_addr_lst[i]; 303273a8bd74SDavid S. Miller 303373a8bd74SDavid S. Miller spin_lock_bh(&addrconf_hash_lock); 303473a8bd74SDavid S. Miller restart: 3035b67bfe0dSSasha Levin hlist_for_each_entry_rcu(ifa, h, addr_lst) { 303673a8bd74SDavid S. Miller if (ifa->idev == idev) { 303773a8bd74SDavid S. Miller hlist_del_init_rcu(&ifa->addr_lst); 3038b7b1bfceSHannes Frederic Sowa addrconf_del_dad_timer(ifa); 303973a8bd74SDavid S. Miller goto restart; 304073a8bd74SDavid S. Miller } 304173a8bd74SDavid S. Miller } 304273a8bd74SDavid S. Miller spin_unlock_bh(&addrconf_hash_lock); 304373a8bd74SDavid S. Miller } 304473a8bd74SDavid S. Miller 30451da177e4SLinus Torvalds write_lock_bh(&idev->lock); 30461da177e4SLinus Torvalds 3047b7b1bfceSHannes Frederic Sowa addrconf_del_rs_timer(idev); 3048b7b1bfceSHannes Frederic Sowa 3049bcdd553fSStephen Hemminger /* Step 2: clear flags for stateless addrconf */ 3050439e2385SDenis V. Lunev if (!how) 30513c21edbdSYOSHIFUJI Hideaki idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); 30521da177e4SLinus Torvalds 30531da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 3054439e2385SDenis V. Lunev if (how && del_timer(&idev->regen_timer)) 30551da177e4SLinus Torvalds in6_dev_put(idev); 30561da177e4SLinus Torvalds 3057bcdd553fSStephen Hemminger /* Step 3: clear tempaddr list */ 3058372e6c8fSstephen hemminger while (!list_empty(&idev->tempaddr_list)) { 3059372e6c8fSstephen hemminger ifa = list_first_entry(&idev->tempaddr_list, 3060372e6c8fSstephen hemminger struct inet6_ifaddr, tmp_list); 3061372e6c8fSstephen hemminger list_del(&ifa->tmp_list); 30621da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 30631da177e4SLinus Torvalds spin_lock_bh(&ifa->lock); 30641da177e4SLinus Torvalds 30651da177e4SLinus Torvalds if (ifa->ifpub) { 30661da177e4SLinus Torvalds in6_ifa_put(ifa->ifpub); 30671da177e4SLinus Torvalds ifa->ifpub = NULL; 30681da177e4SLinus Torvalds } 30691da177e4SLinus Torvalds spin_unlock_bh(&ifa->lock); 30701da177e4SLinus Torvalds in6_ifa_put(ifa); 30711da177e4SLinus Torvalds write_lock_bh(&idev->lock); 30721da177e4SLinus Torvalds } 30731da177e4SLinus Torvalds #endif 30748f37ada5Sstephen hemminger 3075502a2ffdSstephen hemminger while (!list_empty(&idev->addr_list)) { 3076502a2ffdSstephen hemminger ifa = list_first_entry(&idev->addr_list, 3077502a2ffdSstephen hemminger struct inet6_ifaddr, if_list); 3078b7b1bfceSHannes Frederic Sowa addrconf_del_dad_timer(ifa); 307984e8b803Sstephen hemminger 3080502a2ffdSstephen hemminger list_del(&ifa->if_list); 30811da177e4SLinus Torvalds 30824c5ff6a6SHerbert Xu write_unlock_bh(&idev->lock); 308373a8bd74SDavid S. Miller 30844c5ff6a6SHerbert Xu spin_lock_bh(&ifa->state_lock); 30854c5ff6a6SHerbert Xu state = ifa->state; 30864c5ff6a6SHerbert Xu ifa->state = INET6_IFADDR_STATE_DEAD; 30874c5ff6a6SHerbert Xu spin_unlock_bh(&ifa->state_lock); 30884c5ff6a6SHerbert Xu 30899d82ca98SJohn Fastabend if (state != INET6_IFADDR_STATE_DEAD) { 30901da177e4SLinus Torvalds __ipv6_ifa_notify(RTM_DELADDR, ifa); 3091f88c91ddSCong Wang inet6addr_notifier_call_chain(NETDEV_DOWN, ifa); 30922de79570SLorenzo Colitti } 30939d82ca98SJohn Fastabend in6_ifa_put(ifa); 309473a8bd74SDavid S. Miller 30951da177e4SLinus Torvalds write_lock_bh(&idev->lock); 30961da177e4SLinus Torvalds } 30978f37ada5Sstephen hemminger 30981da177e4SLinus Torvalds write_unlock_bh(&idev->lock); 30991da177e4SLinus Torvalds 31001da177e4SLinus Torvalds /* Step 5: Discard multicast list */ 3101439e2385SDenis V. Lunev if (how) 31021da177e4SLinus Torvalds ipv6_mc_destroy_dev(idev); 31031da177e4SLinus Torvalds else 31041da177e4SLinus Torvalds ipv6_mc_down(idev); 31051da177e4SLinus Torvalds 31061da177e4SLinus Torvalds idev->tstamp = jiffies; 31071da177e4SLinus Torvalds 3108bcdd553fSStephen Hemminger /* Last: Shot the device (if unregistered) */ 3109439e2385SDenis V. Lunev if (how) { 3110408c4768SPavel Emelyanov addrconf_sysctl_unregister(idev); 31111da177e4SLinus Torvalds neigh_parms_release(&nd_tbl, idev->nd_parms); 31121da177e4SLinus Torvalds neigh_ifdown(&nd_tbl, dev); 31131da177e4SLinus Torvalds in6_dev_put(idev); 31141da177e4SLinus Torvalds } 31151da177e4SLinus Torvalds return 0; 31161da177e4SLinus Torvalds } 31171da177e4SLinus Torvalds 31181da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data) 31191da177e4SLinus Torvalds { 3120b7b1bfceSHannes Frederic Sowa struct inet6_dev *idev = (struct inet6_dev *)data; 3121b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr; 31221da177e4SLinus Torvalds 3123b7b1bfceSHannes Frederic Sowa write_lock(&idev->lock); 31245b2a1953Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) 31251da177e4SLinus Torvalds goto out; 31261da177e4SLinus Torvalds 31279ba2add3SShmulik Ladkani if (!ipv6_accept_ra(idev)) 31281da177e4SLinus Torvalds goto out; 31295b2a1953Sstephen hemminger 31305b2a1953Sstephen hemminger /* Announcement received after solicitation was sent */ 31315b2a1953Sstephen hemminger if (idev->if_flags & IF_RA_RCVD) 31325b2a1953Sstephen hemminger goto out; 31331da177e4SLinus Torvalds 3134b7b1bfceSHannes Frederic Sowa if (idev->rs_probes++ < idev->cnf.rtr_solicits) { 3135b7b1bfceSHannes Frederic Sowa if (!__ipv6_get_lladdr(idev, &lladdr, IFA_F_TENTATIVE)) 3136b7b1bfceSHannes Frederic Sowa ndisc_send_rs(idev->dev, &lladdr, 3137b7b1bfceSHannes Frederic Sowa &in6addr_linklocal_allrouters); 3138b7b1bfceSHannes Frederic Sowa else 3139b7b1bfceSHannes Frederic Sowa goto out; 3140b7b1bfceSHannes Frederic Sowa 31411da177e4SLinus Torvalds /* The wait after the last probe can be shorter */ 3142b7b1bfceSHannes Frederic Sowa addrconf_mod_rs_timer(idev, (idev->rs_probes == 3143b7b1bfceSHannes Frederic Sowa idev->cnf.rtr_solicits) ? 31445b2a1953Sstephen hemminger idev->cnf.rtr_solicit_delay : 31455b2a1953Sstephen hemminger idev->cnf.rtr_solicit_interval); 31461da177e4SLinus Torvalds } else { 31471da177e4SLinus Torvalds /* 31481da177e4SLinus Torvalds * Note: we do not support deprecated "all on-link" 31491da177e4SLinus Torvalds * assumption any longer. 31501da177e4SLinus Torvalds */ 315191df42beSJoe Perches pr_debug("%s: no IPv6 routers present\n", idev->dev->name); 31521da177e4SLinus Torvalds } 31531da177e4SLinus Torvalds 31541da177e4SLinus Torvalds out: 3155b7b1bfceSHannes Frederic Sowa write_unlock(&idev->lock); 3156b7b1bfceSHannes Frederic Sowa in6_dev_put(idev); 31571da177e4SLinus Torvalds } 31581da177e4SLinus Torvalds 31591da177e4SLinus Torvalds /* 31601da177e4SLinus Torvalds * Duplicate Address Detection 31611da177e4SLinus Torvalds */ 31623c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp) 31633c21edbdSYOSHIFUJI Hideaki { 31643c21edbdSYOSHIFUJI Hideaki unsigned long rand_num; 31653c21edbdSYOSHIFUJI Hideaki struct inet6_dev *idev = ifp->idev; 31663c21edbdSYOSHIFUJI Hideaki 316795c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 316895c385b4SNeil Horman rand_num = 0; 316995c385b4SNeil Horman else 31703c21edbdSYOSHIFUJI Hideaki rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1); 317195c385b4SNeil Horman 3172b7b1bfceSHannes Frederic Sowa ifp->dad_probes = idev->cnf.dad_transmits; 3173b7b1bfceSHannes Frederic Sowa addrconf_mod_dad_timer(ifp, rand_num); 31743c21edbdSYOSHIFUJI Hideaki } 31753c21edbdSYOSHIFUJI Hideaki 3176cf22f9a2SDavid S. Miller static void addrconf_dad_start(struct inet6_ifaddr *ifp) 31771da177e4SLinus Torvalds { 31781da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 31791da177e4SLinus Torvalds struct net_device *dev = idev->dev; 31801da177e4SLinus Torvalds 31811da177e4SLinus Torvalds addrconf_join_solict(dev, &ifp->addr); 31821da177e4SLinus Torvalds 31831da177e4SLinus Torvalds net_srandom(ifp->addr.s6_addr32[3]); 31841da177e4SLinus Torvalds 31851da177e4SLinus Torvalds read_lock_bh(&idev->lock); 3186622ccdf1SHerbert Xu spin_lock(&ifp->lock); 3187e9d3e084SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) 31881da177e4SLinus Torvalds goto out; 31891da177e4SLinus Torvalds 31901da177e4SLinus Torvalds if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) || 31911b34be74SYOSHIFUJI Hideaki idev->cnf.accept_dad < 1 || 319255ebaef1SNoriaki TAKAMIYA !(ifp->flags&IFA_F_TENTATIVE) || 319355ebaef1SNoriaki TAKAMIYA ifp->flags & IFA_F_NODAD) { 3194cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 319521809fafSstephen hemminger spin_unlock(&ifp->lock); 31961da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 31971da177e4SLinus Torvalds 31981da177e4SLinus Torvalds addrconf_dad_completed(ifp); 31991da177e4SLinus Torvalds return; 32001da177e4SLinus Torvalds } 32011da177e4SLinus Torvalds 32026732badeSYOSHIFUJI Hideaki if (!(idev->if_flags & IF_READY)) { 320321809fafSstephen hemminger spin_unlock(&ifp->lock); 32046732badeSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 32053c21edbdSYOSHIFUJI Hideaki /* 3206590a9887SMasatake YAMATO * If the device is not ready: 32073c21edbdSYOSHIFUJI Hideaki * - keep it tentative if it is a permanent address. 32083c21edbdSYOSHIFUJI Hideaki * - otherwise, kill it. 32093c21edbdSYOSHIFUJI Hideaki */ 32103c21edbdSYOSHIFUJI Hideaki in6_ifa_hold(ifp); 3211cc411d0bSBrian Haley addrconf_dad_stop(ifp, 0); 32126732badeSYOSHIFUJI Hideaki return; 32133c21edbdSYOSHIFUJI Hideaki } 321495c385b4SNeil Horman 321595c385b4SNeil Horman /* 321695c385b4SNeil Horman * Optimistic nodes can start receiving 321795c385b4SNeil Horman * Frames right away 321895c385b4SNeil Horman */ 321995c385b4SNeil Horman if (ifp->flags & IFA_F_OPTIMISTIC) 322095c385b4SNeil Horman ip6_ins_rt(ifp->rt); 322195c385b4SNeil Horman 32226732badeSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 32231da177e4SLinus Torvalds out: 3224622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 32251da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 32261da177e4SLinus Torvalds } 32271da177e4SLinus Torvalds 32281da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data) 32291da177e4SLinus Torvalds { 32301da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data; 32311da177e4SLinus Torvalds struct inet6_dev *idev = ifp->idev; 32321da177e4SLinus Torvalds struct in6_addr mcaddr; 32331da177e4SLinus Torvalds 3234b7b1bfceSHannes Frederic Sowa if (!ifp->dad_probes && addrconf_dad_end(ifp)) 3235f2344a13SHerbert Xu goto out; 3236f2344a13SHerbert Xu 3237b7b1bfceSHannes Frederic Sowa write_lock(&idev->lock); 3238122e4519Sstephen hemminger if (idev->dead || !(idev->if_flags & IF_READY)) { 3239b7b1bfceSHannes Frederic Sowa write_unlock(&idev->lock); 32401da177e4SLinus Torvalds goto out; 32411da177e4SLinus Torvalds } 324221809fafSstephen hemminger 324321809fafSstephen hemminger spin_lock(&ifp->lock); 3244622ccdf1SHerbert Xu if (ifp->state == INET6_IFADDR_STATE_DEAD) { 3245622ccdf1SHerbert Xu spin_unlock(&ifp->lock); 3246b7b1bfceSHannes Frederic Sowa write_unlock(&idev->lock); 3247622ccdf1SHerbert Xu goto out; 3248622ccdf1SHerbert Xu } 3249622ccdf1SHerbert Xu 3250b7b1bfceSHannes Frederic Sowa if (ifp->dad_probes == 0) { 32511da177e4SLinus Torvalds /* 32521da177e4SLinus Torvalds * DAD was successful 32531da177e4SLinus Torvalds */ 32541da177e4SLinus Torvalds 3255cc411d0bSBrian Haley ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED); 325621809fafSstephen hemminger spin_unlock(&ifp->lock); 3257b7b1bfceSHannes Frederic Sowa write_unlock(&idev->lock); 32581da177e4SLinus Torvalds 32591da177e4SLinus Torvalds addrconf_dad_completed(ifp); 32601da177e4SLinus Torvalds 32611da177e4SLinus Torvalds goto out; 32621da177e4SLinus Torvalds } 32631da177e4SLinus Torvalds 3264b7b1bfceSHannes Frederic Sowa ifp->dad_probes--; 3265b7b1bfceSHannes Frederic Sowa addrconf_mod_dad_timer(ifp, ifp->idev->nd_parms->retrans_time); 326621809fafSstephen hemminger spin_unlock(&ifp->lock); 3267b7b1bfceSHannes Frederic Sowa write_unlock(&idev->lock); 32681da177e4SLinus Torvalds 32691da177e4SLinus Torvalds /* send a neighbour solicitation for our addr */ 32701da177e4SLinus Torvalds addrconf_addr_solict_mult(&ifp->addr, &mcaddr); 3271d7aabf22SYOSHIFUJI Hideaki ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any); 32721da177e4SLinus Torvalds out: 32731da177e4SLinus Torvalds in6_ifa_put(ifp); 32741da177e4SLinus Torvalds } 32751da177e4SLinus Torvalds 32761da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp) 32771da177e4SLinus Torvalds { 32781da177e4SLinus Torvalds struct net_device *dev = ifp->idev->dev; 3279b7b1bfceSHannes Frederic Sowa struct in6_addr lladdr; 3280b7b1bfceSHannes Frederic Sowa 3281b7b1bfceSHannes Frederic Sowa addrconf_del_dad_timer(ifp); 32821da177e4SLinus Torvalds 32831da177e4SLinus Torvalds /* 32841da177e4SLinus Torvalds * Configure the address for reception. Now it is valid. 32851da177e4SLinus Torvalds */ 32861da177e4SLinus Torvalds 32871da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 32881da177e4SLinus Torvalds 3289026359bcSTore Anderson /* If added prefix is link local and we are prepared to process 3290026359bcSTore Anderson router advertisements, start sending router solicitations. 32911da177e4SLinus Torvalds */ 32921da177e4SLinus Torvalds 3293aeaf6e9dSShmulik Ladkani if (ipv6_accept_ra(ifp->idev) && 32941da177e4SLinus Torvalds ifp->idev->cnf.rtr_solicits > 0 && 32951da177e4SLinus Torvalds (dev->flags&IFF_LOOPBACK) == 0 && 32961da177e4SLinus Torvalds (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) { 32971da177e4SLinus Torvalds /* 32981da177e4SLinus Torvalds * If a host as already performed a random delay 32991da177e4SLinus Torvalds * [...] as part of DAD [...] there is no need 33001da177e4SLinus Torvalds * to delay again before sending the first RS 33011da177e4SLinus Torvalds */ 3302b7b1bfceSHannes Frederic Sowa if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 3303b7b1bfceSHannes Frederic Sowa ndisc_send_rs(dev, &lladdr, 3304b7b1bfceSHannes Frederic Sowa &in6addr_linklocal_allrouters); 3305b7b1bfceSHannes Frederic Sowa else 3306b7b1bfceSHannes Frederic Sowa return; 33071da177e4SLinus Torvalds 3308b7b1bfceSHannes Frederic Sowa write_lock_bh(&ifp->idev->lock); 3309b7b1bfceSHannes Frederic Sowa spin_lock(&ifp->lock); 3310b7b1bfceSHannes Frederic Sowa ifp->idev->rs_probes = 1; 33111da177e4SLinus Torvalds ifp->idev->if_flags |= IF_RS_SENT; 3312b7b1bfceSHannes Frederic Sowa addrconf_mod_rs_timer(ifp->idev, 3313b7b1bfceSHannes Frederic Sowa ifp->idev->cnf.rtr_solicit_interval); 3314b7b1bfceSHannes Frederic Sowa spin_unlock(&ifp->lock); 3315b7b1bfceSHannes Frederic Sowa write_unlock_bh(&ifp->idev->lock); 33161da177e4SLinus Torvalds } 33171da177e4SLinus Torvalds } 33181da177e4SLinus Torvalds 3319e21e8467SStephen Hemminger static void addrconf_dad_run(struct inet6_dev *idev) 3320e21e8467SStephen Hemminger { 3321c5e33bddSYOSHIFUJI Hideaki struct inet6_ifaddr *ifp; 3322c5e33bddSYOSHIFUJI Hideaki 3323c5e33bddSYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 3324502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 332521809fafSstephen hemminger spin_lock(&ifp->lock); 3326f2344a13SHerbert Xu if (ifp->flags & IFA_F_TENTATIVE && 3327f2344a13SHerbert Xu ifp->state == INET6_IFADDR_STATE_DAD) 3328c5e33bddSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 3329f2344a13SHerbert Xu spin_unlock(&ifp->lock); 3330c5e33bddSYOSHIFUJI Hideaki } 3331c5e33bddSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 3332c5e33bddSYOSHIFUJI Hideaki } 3333c5e33bddSYOSHIFUJI Hideaki 33341da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 33351da177e4SLinus Torvalds struct if6_iter_state { 33363c40090aSDaniel Lezcano struct seq_net_private p; 33371da177e4SLinus Torvalds int bucket; 33381d578303SMihai Maruseac int offset; 33391da177e4SLinus Torvalds }; 33401da177e4SLinus Torvalds 33411d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos) 33421da177e4SLinus Torvalds { 33431da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 33441da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 33451218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 33461d578303SMihai Maruseac int p = 0; 33471da177e4SLinus Torvalds 33481d578303SMihai Maruseac /* initial bucket if pos is 0 */ 33491d578303SMihai Maruseac if (pos == 0) { 33501d578303SMihai Maruseac state->bucket = 0; 33511d578303SMihai Maruseac state->offset = 0; 33521d578303SMihai Maruseac } 33531d578303SMihai Maruseac 33541d578303SMihai Maruseac for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) { 3355b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket], 33561d578303SMihai Maruseac addr_lst) { 33579f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 33589f0d3c27SEric Dumazet continue; 33591d578303SMihai Maruseac /* sync with offset */ 33601d578303SMihai Maruseac if (p < state->offset) { 33611d578303SMihai Maruseac p++; 33621d578303SMihai Maruseac continue; 33631d578303SMihai Maruseac } 33641d578303SMihai Maruseac state->offset++; 33651da177e4SLinus Torvalds return ifa; 33661da177e4SLinus Torvalds } 33671d578303SMihai Maruseac 33681d578303SMihai Maruseac /* prepare for next bucket */ 33691d578303SMihai Maruseac state->offset = 0; 33701d578303SMihai Maruseac p = 0; 33711d578303SMihai Maruseac } 3372c2e21293Sstephen hemminger return NULL; 3373c2e21293Sstephen hemminger } 33741da177e4SLinus Torvalds 3375c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, 3376c2e21293Sstephen hemminger struct inet6_ifaddr *ifa) 33771da177e4SLinus Torvalds { 33781da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 33791218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 33801da177e4SLinus Torvalds 3381b67bfe0dSSasha Levin hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) { 33829f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 33839f0d3c27SEric Dumazet continue; 33841d578303SMihai Maruseac state->offset++; 33851da177e4SLinus Torvalds return ifa; 33861d578303SMihai Maruseac } 33871da177e4SLinus Torvalds 3388c2e21293Sstephen hemminger while (++state->bucket < IN6_ADDR_HSIZE) { 33891d578303SMihai Maruseac state->offset = 0; 3390b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifa, 3391c2e21293Sstephen hemminger &inet6_addr_lst[state->bucket], addr_lst) { 33929f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 33939f0d3c27SEric Dumazet continue; 33941d578303SMihai Maruseac state->offset++; 3395c2e21293Sstephen hemminger return ifa; 3396c2e21293Sstephen hemminger } 3397c2e21293Sstephen hemminger } 3398c2e21293Sstephen hemminger 3399c2e21293Sstephen hemminger return NULL; 3400c2e21293Sstephen hemminger } 3401c2e21293Sstephen hemminger 34021da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos) 34034f70eccaSEric Dumazet __acquires(rcu_bh) 34041da177e4SLinus Torvalds { 34055c578aedSstephen hemminger rcu_read_lock_bh(); 34061d578303SMihai Maruseac return if6_get_first(seq, *pos); 34071da177e4SLinus Torvalds } 34081da177e4SLinus Torvalds 34091da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos) 34101da177e4SLinus Torvalds { 34111da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 34121da177e4SLinus Torvalds 34131da177e4SLinus Torvalds ifa = if6_get_next(seq, v); 34141da177e4SLinus Torvalds ++*pos; 34151da177e4SLinus Torvalds return ifa; 34161da177e4SLinus Torvalds } 34171da177e4SLinus Torvalds 34181da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v) 34194f70eccaSEric Dumazet __releases(rcu_bh) 34201da177e4SLinus Torvalds { 34215c578aedSstephen hemminger rcu_read_unlock_bh(); 34221da177e4SLinus Torvalds } 34231da177e4SLinus Torvalds 34241da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v) 34251da177e4SLinus Torvalds { 34261da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v; 34274b7a4274SHarvey Harrison seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n", 3428b071195dSHarvey Harrison &ifp->addr, 34291da177e4SLinus Torvalds ifp->idev->dev->ifindex, 34301da177e4SLinus Torvalds ifp->prefix_len, 34311da177e4SLinus Torvalds ifp->scope, 34321da177e4SLinus Torvalds ifp->flags, 34331da177e4SLinus Torvalds ifp->idev->dev->name); 34341da177e4SLinus Torvalds return 0; 34351da177e4SLinus Torvalds } 34361da177e4SLinus Torvalds 343756b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = { 34381da177e4SLinus Torvalds .start = if6_seq_start, 34391da177e4SLinus Torvalds .next = if6_seq_next, 34401da177e4SLinus Torvalds .show = if6_seq_show, 34411da177e4SLinus Torvalds .stop = if6_seq_stop, 34421da177e4SLinus Torvalds }; 34431da177e4SLinus Torvalds 34441da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file) 34451da177e4SLinus Torvalds { 34463c40090aSDaniel Lezcano return seq_open_net(inode, file, &if6_seq_ops, 3447cf7732e4SPavel Emelyanov sizeof(struct if6_iter_state)); 34481da177e4SLinus Torvalds } 34491da177e4SLinus Torvalds 34509a32144eSArjan van de Ven static const struct file_operations if6_fops = { 34511da177e4SLinus Torvalds .owner = THIS_MODULE, 34521da177e4SLinus Torvalds .open = if6_seq_open, 34531da177e4SLinus Torvalds .read = seq_read, 34541da177e4SLinus Torvalds .llseek = seq_lseek, 34553c40090aSDaniel Lezcano .release = seq_release_net, 34561da177e4SLinus Torvalds }; 34571da177e4SLinus Torvalds 34582c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net) 34591da177e4SLinus Torvalds { 3460d4beaa66SGao feng if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops)) 34611da177e4SLinus Torvalds return -ENOMEM; 34621da177e4SLinus Torvalds return 0; 34631da177e4SLinus Torvalds } 34641da177e4SLinus Torvalds 34652c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net) 34663c40090aSDaniel Lezcano { 3467ece31ffdSGao feng remove_proc_entry("if_inet6", net->proc_net); 34683c40090aSDaniel Lezcano } 34693c40090aSDaniel Lezcano 34703c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = { 34713c40090aSDaniel Lezcano .init = if6_proc_net_init, 34723c40090aSDaniel Lezcano .exit = if6_proc_net_exit, 34733c40090aSDaniel Lezcano }; 34743c40090aSDaniel Lezcano 34753c40090aSDaniel Lezcano int __init if6_proc_init(void) 34763c40090aSDaniel Lezcano { 34773c40090aSDaniel Lezcano return register_pernet_subsys(&if6_proc_net_ops); 34783c40090aSDaniel Lezcano } 34793c40090aSDaniel Lezcano 34801da177e4SLinus Torvalds void if6_proc_exit(void) 34811da177e4SLinus Torvalds { 34823c40090aSDaniel Lezcano unregister_pernet_subsys(&if6_proc_net_ops); 34831da177e4SLinus Torvalds } 34841da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 34851da177e4SLinus Torvalds 348607a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6) 34873b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */ 3488b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr) 34893b9f9a1cSNoriaki TAKAMIYA { 34903b9f9a1cSNoriaki TAKAMIYA int ret = 0; 3491c2e21293Sstephen hemminger struct inet6_ifaddr *ifp = NULL; 3492ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 3493c2e21293Sstephen hemminger 34945c578aedSstephen hemminger rcu_read_lock_bh(); 3495b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) { 3496878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 3497389f6612SDaniel Lezcano continue; 3498caad295fSYOSHIFUJI Hideaki if (ipv6_addr_equal(&ifp->addr, addr) && 34993b9f9a1cSNoriaki TAKAMIYA (ifp->flags & IFA_F_HOMEADDRESS)) { 35003b9f9a1cSNoriaki TAKAMIYA ret = 1; 35013b9f9a1cSNoriaki TAKAMIYA break; 35023b9f9a1cSNoriaki TAKAMIYA } 35033b9f9a1cSNoriaki TAKAMIYA } 35045c578aedSstephen hemminger rcu_read_unlock_bh(); 35053b9f9a1cSNoriaki TAKAMIYA return ret; 35063b9f9a1cSNoriaki TAKAMIYA } 35073b9f9a1cSNoriaki TAKAMIYA #endif 35083b9f9a1cSNoriaki TAKAMIYA 35091da177e4SLinus Torvalds /* 35101da177e4SLinus Torvalds * Periodic address status verification 35111da177e4SLinus Torvalds */ 35121da177e4SLinus Torvalds 35131da177e4SLinus Torvalds static void addrconf_verify(unsigned long foo) 35141da177e4SLinus Torvalds { 3515b2db7564SYOSHIFUJI Hideaki unsigned long now, next, next_sec, next_sched; 35161da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 35171da177e4SLinus Torvalds int i; 35181da177e4SLinus Torvalds 35195c578aedSstephen hemminger rcu_read_lock_bh(); 35205c578aedSstephen hemminger spin_lock(&addrconf_verify_lock); 35211da177e4SLinus Torvalds now = jiffies; 3522b2db7564SYOSHIFUJI Hideaki next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY); 35231da177e4SLinus Torvalds 35241da177e4SLinus Torvalds del_timer(&addr_chk_timer); 35251da177e4SLinus Torvalds 35261da177e4SLinus Torvalds for (i = 0; i < IN6_ADDR_HSIZE; i++) { 35271da177e4SLinus Torvalds restart: 3528b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifp, 35295c578aedSstephen hemminger &inet6_addr_lst[i], addr_lst) { 35301da177e4SLinus Torvalds unsigned long age; 35311da177e4SLinus Torvalds 35321da177e4SLinus Torvalds if (ifp->flags & IFA_F_PERMANENT) 35331da177e4SLinus Torvalds continue; 35341da177e4SLinus Torvalds 35351da177e4SLinus Torvalds spin_lock(&ifp->lock); 3536b2db7564SYOSHIFUJI Hideaki /* We try to batch several events at once. */ 3537b2db7564SYOSHIFUJI Hideaki age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 35381da177e4SLinus Torvalds 35398f27ebb9SYOSHIFUJI Hideaki if (ifp->valid_lft != INFINITY_LIFE_TIME && 35408f27ebb9SYOSHIFUJI Hideaki age >= ifp->valid_lft) { 35411da177e4SLinus Torvalds spin_unlock(&ifp->lock); 35421da177e4SLinus Torvalds in6_ifa_hold(ifp); 35431da177e4SLinus Torvalds ipv6_del_addr(ifp); 35441da177e4SLinus Torvalds goto restart; 35458f27ebb9SYOSHIFUJI Hideaki } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) { 35468f27ebb9SYOSHIFUJI Hideaki spin_unlock(&ifp->lock); 35478f27ebb9SYOSHIFUJI Hideaki continue; 35481da177e4SLinus Torvalds } else if (age >= ifp->prefered_lft) { 3549a1ed0526SBrian Haley /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */ 35501da177e4SLinus Torvalds int deprecate = 0; 35511da177e4SLinus Torvalds 35521da177e4SLinus Torvalds if (!(ifp->flags&IFA_F_DEPRECATED)) { 35531da177e4SLinus Torvalds deprecate = 1; 35541da177e4SLinus Torvalds ifp->flags |= IFA_F_DEPRECATED; 35551da177e4SLinus Torvalds } 35561da177e4SLinus Torvalds 35571da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)) 35581da177e4SLinus Torvalds next = ifp->tstamp + ifp->valid_lft * HZ; 35591da177e4SLinus Torvalds 35601da177e4SLinus Torvalds spin_unlock(&ifp->lock); 35611da177e4SLinus Torvalds 35621da177e4SLinus Torvalds if (deprecate) { 35631da177e4SLinus Torvalds in6_ifa_hold(ifp); 35641da177e4SLinus Torvalds 35651da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 35661da177e4SLinus Torvalds in6_ifa_put(ifp); 35671da177e4SLinus Torvalds goto restart; 35681da177e4SLinus Torvalds } 35691da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 35701da177e4SLinus Torvalds } else if ((ifp->flags&IFA_F_TEMPORARY) && 35711da177e4SLinus Torvalds !(ifp->flags&IFA_F_TENTATIVE)) { 357288949cf4Sstephen hemminger unsigned long regen_advance = ifp->idev->cnf.regen_max_retry * 357388949cf4Sstephen hemminger ifp->idev->cnf.dad_transmits * 357488949cf4Sstephen hemminger ifp->idev->nd_parms->retrans_time / HZ; 357588949cf4Sstephen hemminger 35761da177e4SLinus Torvalds if (age >= ifp->prefered_lft - regen_advance) { 35771da177e4SLinus Torvalds struct inet6_ifaddr *ifpub = ifp->ifpub; 35781da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 35791da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 35801da177e4SLinus Torvalds if (!ifp->regen_count && ifpub) { 35811da177e4SLinus Torvalds ifp->regen_count++; 35821da177e4SLinus Torvalds in6_ifa_hold(ifp); 35831da177e4SLinus Torvalds in6_ifa_hold(ifpub); 35841da177e4SLinus Torvalds spin_unlock(&ifp->lock); 35855c578aedSstephen hemminger 3586291d809bSHiroyuki YAMAMORI spin_lock(&ifpub->lock); 3587291d809bSHiroyuki YAMAMORI ifpub->regen_count = 0; 3588291d809bSHiroyuki YAMAMORI spin_unlock(&ifpub->lock); 35891da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 35901da177e4SLinus Torvalds in6_ifa_put(ifpub); 35911da177e4SLinus Torvalds in6_ifa_put(ifp); 35921da177e4SLinus Torvalds goto restart; 35931da177e4SLinus Torvalds } 35941da177e4SLinus Torvalds } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) 35951da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; 35961da177e4SLinus Torvalds spin_unlock(&ifp->lock); 35971da177e4SLinus Torvalds #endif 35981da177e4SLinus Torvalds } else { 35991da177e4SLinus Torvalds /* ifp->prefered_lft <= ifp->valid_lft */ 36001da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 36011da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 36021da177e4SLinus Torvalds spin_unlock(&ifp->lock); 36031da177e4SLinus Torvalds } 36041da177e4SLinus Torvalds } 36051da177e4SLinus Torvalds } 36061da177e4SLinus Torvalds 3607b2db7564SYOSHIFUJI Hideaki next_sec = round_jiffies_up(next); 3608b2db7564SYOSHIFUJI Hideaki next_sched = next; 3609b2db7564SYOSHIFUJI Hideaki 3610b2db7564SYOSHIFUJI Hideaki /* If rounded timeout is accurate enough, accept it. */ 3611b2db7564SYOSHIFUJI Hideaki if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ)) 3612b2db7564SYOSHIFUJI Hideaki next_sched = next_sec; 3613b2db7564SYOSHIFUJI Hideaki 3614b2db7564SYOSHIFUJI Hideaki /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */ 3615b2db7564SYOSHIFUJI Hideaki if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX)) 3616b2db7564SYOSHIFUJI Hideaki next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX; 3617b2db7564SYOSHIFUJI Hideaki 3618b2db7564SYOSHIFUJI Hideaki ADBG((KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n", 3619b2db7564SYOSHIFUJI Hideaki now, next, next_sec, next_sched)); 3620b2db7564SYOSHIFUJI Hideaki 3621b2db7564SYOSHIFUJI Hideaki addr_chk_timer.expires = next_sched; 36221da177e4SLinus Torvalds add_timer(&addr_chk_timer); 36235c578aedSstephen hemminger spin_unlock(&addrconf_verify_lock); 36245c578aedSstephen hemminger rcu_read_unlock_bh(); 36251da177e4SLinus Torvalds } 36261da177e4SLinus Torvalds 3627caeaba79SNicolas Dichtel static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local, 3628caeaba79SNicolas Dichtel struct in6_addr **peer_pfx) 3629461d8837SThomas Graf { 3630461d8837SThomas Graf struct in6_addr *pfx = NULL; 3631461d8837SThomas Graf 3632caeaba79SNicolas Dichtel *peer_pfx = NULL; 3633caeaba79SNicolas Dichtel 3634461d8837SThomas Graf if (addr) 3635461d8837SThomas Graf pfx = nla_data(addr); 3636461d8837SThomas Graf 3637461d8837SThomas Graf if (local) { 3638461d8837SThomas Graf if (pfx && nla_memcmp(local, pfx, sizeof(*pfx))) 3639caeaba79SNicolas Dichtel *peer_pfx = pfx; 3640461d8837SThomas Graf pfx = nla_data(local); 3641461d8837SThomas Graf } 3642461d8837SThomas Graf 3643461d8837SThomas Graf return pfx; 3644461d8837SThomas Graf } 3645461d8837SThomas Graf 3646ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = { 3647461d8837SThomas Graf [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) }, 3648461d8837SThomas Graf [IFA_LOCAL] = { .len = sizeof(struct in6_addr) }, 3649461d8837SThomas Graf [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, 3650461d8837SThomas Graf }; 3651461d8837SThomas Graf 36521da177e4SLinus Torvalds static int 3653661d2967SThomas Graf inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh) 36541da177e4SLinus Torvalds { 36553b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 3656b933f716SThomas Graf struct ifaddrmsg *ifm; 3657b933f716SThomas Graf struct nlattr *tb[IFA_MAX+1]; 3658caeaba79SNicolas Dichtel struct in6_addr *pfx, *peer_pfx; 3659b933f716SThomas Graf int err; 36601da177e4SLinus Torvalds 3661b933f716SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 3662b933f716SThomas Graf if (err < 0) 3663b933f716SThomas Graf return err; 3664b933f716SThomas Graf 3665b933f716SThomas Graf ifm = nlmsg_data(nlh); 3666caeaba79SNicolas Dichtel pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 36671da177e4SLinus Torvalds if (pfx == NULL) 36681da177e4SLinus Torvalds return -EINVAL; 36691da177e4SLinus Torvalds 3670af284937SDaniel Lezcano return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen); 36711da177e4SLinus Torvalds } 36721da177e4SLinus Torvalds 367355ebaef1SNoriaki TAKAMIYA static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags, 367455ebaef1SNoriaki TAKAMIYA u32 prefered_lft, u32 valid_lft) 3675081bba5bSNoriaki TAKAMIYA { 36766f704992SYOSHIFUJI Hideaki u32 flags; 36776f704992SYOSHIFUJI Hideaki clock_t expires; 36784bed72e4SYOSHIFUJI Hideaki unsigned long timeout; 367946d48046SYOSHIFUJI Hideaki 3680081bba5bSNoriaki TAKAMIYA if (!valid_lft || (prefered_lft > valid_lft)) 3681081bba5bSNoriaki TAKAMIYA return -EINVAL; 3682081bba5bSNoriaki TAKAMIYA 36834bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(valid_lft, HZ); 36844bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 36854bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 36864bed72e4SYOSHIFUJI Hideaki valid_lft = timeout; 36876f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 36884bed72e4SYOSHIFUJI Hideaki } else { 36894bed72e4SYOSHIFUJI Hideaki expires = 0; 36904bed72e4SYOSHIFUJI Hideaki flags = 0; 36914bed72e4SYOSHIFUJI Hideaki ifa_flags |= IFA_F_PERMANENT; 36926f704992SYOSHIFUJI Hideaki } 3693081bba5bSNoriaki TAKAMIYA 36944bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(prefered_lft, HZ); 36954bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 36964bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 369755ebaef1SNoriaki TAKAMIYA ifa_flags |= IFA_F_DEPRECATED; 36984bed72e4SYOSHIFUJI Hideaki prefered_lft = timeout; 36994bed72e4SYOSHIFUJI Hideaki } 3700081bba5bSNoriaki TAKAMIYA 3701081bba5bSNoriaki TAKAMIYA spin_lock_bh(&ifp->lock); 37023b9f9a1cSNoriaki TAKAMIYA ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags; 3703081bba5bSNoriaki TAKAMIYA ifp->tstamp = jiffies; 3704081bba5bSNoriaki TAKAMIYA ifp->valid_lft = valid_lft; 3705081bba5bSNoriaki TAKAMIYA ifp->prefered_lft = prefered_lft; 3706081bba5bSNoriaki TAKAMIYA 3707081bba5bSNoriaki TAKAMIYA spin_unlock_bh(&ifp->lock); 3708081bba5bSNoriaki TAKAMIYA if (!(ifp->flags&IFA_F_TENTATIVE)) 3709081bba5bSNoriaki TAKAMIYA ipv6_ifa_notify(0, ifp); 3710081bba5bSNoriaki TAKAMIYA 371146d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev, 37126f704992SYOSHIFUJI Hideaki expires, flags); 3713081bba5bSNoriaki TAKAMIYA addrconf_verify(0); 3714081bba5bSNoriaki TAKAMIYA 3715081bba5bSNoriaki TAKAMIYA return 0; 3716081bba5bSNoriaki TAKAMIYA } 3717081bba5bSNoriaki TAKAMIYA 3718081bba5bSNoriaki TAKAMIYA static int 3719661d2967SThomas Graf inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh) 37201da177e4SLinus Torvalds { 37213b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 3722461d8837SThomas Graf struct ifaddrmsg *ifm; 3723461d8837SThomas Graf struct nlattr *tb[IFA_MAX+1]; 3724caeaba79SNicolas Dichtel struct in6_addr *pfx, *peer_pfx; 37257198f8ceSThomas Graf struct inet6_ifaddr *ifa; 37267198f8ceSThomas Graf struct net_device *dev; 372755ebaef1SNoriaki TAKAMIYA u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME; 372855ebaef1SNoriaki TAKAMIYA u8 ifa_flags; 3729461d8837SThomas Graf int err; 37301da177e4SLinus Torvalds 3731461d8837SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 3732461d8837SThomas Graf if (err < 0) 3733461d8837SThomas Graf return err; 3734461d8837SThomas Graf 3735461d8837SThomas Graf ifm = nlmsg_data(nlh); 3736caeaba79SNicolas Dichtel pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 37371da177e4SLinus Torvalds if (pfx == NULL) 37381da177e4SLinus Torvalds return -EINVAL; 37391da177e4SLinus Torvalds 3740461d8837SThomas Graf if (tb[IFA_CACHEINFO]) { 37410778769dSNoriaki TAKAMIYA struct ifa_cacheinfo *ci; 3742461d8837SThomas Graf 3743461d8837SThomas Graf ci = nla_data(tb[IFA_CACHEINFO]); 37440778769dSNoriaki TAKAMIYA valid_lft = ci->ifa_valid; 3745461d8837SThomas Graf preferred_lft = ci->ifa_prefered; 3746461d8837SThomas Graf } else { 3747461d8837SThomas Graf preferred_lft = INFINITY_LIFE_TIME; 3748461d8837SThomas Graf valid_lft = INFINITY_LIFE_TIME; 37490778769dSNoriaki TAKAMIYA } 37500778769dSNoriaki TAKAMIYA 3751af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifm->ifa_index); 37527198f8ceSThomas Graf if (dev == NULL) 37537198f8ceSThomas Graf return -ENODEV; 3754081bba5bSNoriaki TAKAMIYA 375555ebaef1SNoriaki TAKAMIYA /* We ignore other flags so far. */ 37563b9f9a1cSNoriaki TAKAMIYA ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS); 375755ebaef1SNoriaki TAKAMIYA 37581cab3da6SDaniel Lezcano ifa = ipv6_get_ifaddr(net, pfx, dev, 1); 37597198f8ceSThomas Graf if (ifa == NULL) { 37607198f8ceSThomas Graf /* 37617198f8ceSThomas Graf * It would be best to check for !NLM_F_CREATE here but 37627198f8ceSThomas Graf * userspace alreay relies on not having to provide this. 37637198f8ceSThomas Graf */ 3764caeaba79SNicolas Dichtel return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx, 3765af284937SDaniel Lezcano ifm->ifa_prefixlen, ifa_flags, 3766af284937SDaniel Lezcano preferred_lft, valid_lft); 37671da177e4SLinus Torvalds } 37681da177e4SLinus Torvalds 37697198f8ceSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL || 37707198f8ceSThomas Graf !(nlh->nlmsg_flags & NLM_F_REPLACE)) 37717198f8ceSThomas Graf err = -EEXIST; 37727198f8ceSThomas Graf else 377355ebaef1SNoriaki TAKAMIYA err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft); 37747198f8ceSThomas Graf 37757198f8ceSThomas Graf in6_ifa_put(ifa); 37767198f8ceSThomas Graf 37777198f8ceSThomas Graf return err; 37787198f8ceSThomas Graf } 37797198f8ceSThomas Graf 3780101bb229SThomas Graf static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags, 3781101bb229SThomas Graf u8 scope, int ifindex) 3782101bb229SThomas Graf { 3783101bb229SThomas Graf struct ifaddrmsg *ifm; 3784101bb229SThomas Graf 3785101bb229SThomas Graf ifm = nlmsg_data(nlh); 3786101bb229SThomas Graf ifm->ifa_family = AF_INET6; 3787101bb229SThomas Graf ifm->ifa_prefixlen = prefixlen; 3788101bb229SThomas Graf ifm->ifa_flags = flags; 3789101bb229SThomas Graf ifm->ifa_scope = scope; 3790101bb229SThomas Graf ifm->ifa_index = ifindex; 3791101bb229SThomas Graf } 3792101bb229SThomas Graf 379385486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, 379485486af0SThomas Graf unsigned long tstamp, u32 preferred, u32 valid) 379585486af0SThomas Graf { 379685486af0SThomas Graf struct ifa_cacheinfo ci; 379785486af0SThomas Graf 379818a31e1eSThomas Graf ci.cstamp = cstamp_delta(cstamp); 379918a31e1eSThomas Graf ci.tstamp = cstamp_delta(tstamp); 380085486af0SThomas Graf ci.ifa_prefered = preferred; 380185486af0SThomas Graf ci.ifa_valid = valid; 380285486af0SThomas Graf 380385486af0SThomas Graf return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); 380485486af0SThomas Graf } 380585486af0SThomas Graf 3806101bb229SThomas Graf static inline int rt_scope(int ifa_scope) 3807101bb229SThomas Graf { 3808101bb229SThomas Graf if (ifa_scope & IFA_HOST) 3809101bb229SThomas Graf return RT_SCOPE_HOST; 3810101bb229SThomas Graf else if (ifa_scope & IFA_LINK) 3811101bb229SThomas Graf return RT_SCOPE_LINK; 3812101bb229SThomas Graf else if (ifa_scope & IFA_SITE) 3813101bb229SThomas Graf return RT_SCOPE_SITE; 3814101bb229SThomas Graf else 3815101bb229SThomas Graf return RT_SCOPE_UNIVERSE; 3816101bb229SThomas Graf } 3817101bb229SThomas Graf 38180ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void) 38190ab6803bSThomas Graf { 3820339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) 3821caeaba79SNicolas Dichtel + nla_total_size(16) /* IFA_LOCAL */ 3822339bf98fSThomas Graf + nla_total_size(16) /* IFA_ADDRESS */ 3823339bf98fSThomas Graf + nla_total_size(sizeof(struct ifa_cacheinfo)); 38240ab6803bSThomas Graf } 3825c5396a31SYOSHIFUJI Hideaki 38261da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa, 382715e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 38281da177e4SLinus Torvalds { 38291da177e4SLinus Torvalds struct nlmsghdr *nlh; 383085486af0SThomas Graf u32 preferred, valid; 38311da177e4SLinus Torvalds 383215e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 38330ab6803bSThomas Graf if (nlh == NULL) 383426932566SPatrick McHardy return -EMSGSIZE; 38350ab6803bSThomas Graf 3836101bb229SThomas Graf put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), 3837101bb229SThomas Graf ifa->idev->dev->ifindex); 3838101bb229SThomas Graf 38391da177e4SLinus Torvalds if (!(ifa->flags&IFA_F_PERMANENT)) { 384085486af0SThomas Graf preferred = ifa->prefered_lft; 384185486af0SThomas Graf valid = ifa->valid_lft; 384285486af0SThomas Graf if (preferred != INFINITY_LIFE_TIME) { 38431da177e4SLinus Torvalds long tval = (jiffies - ifa->tstamp)/HZ; 3844a1faa698SJens Rosenboom if (preferred > tval) 384585486af0SThomas Graf preferred -= tval; 3846a1faa698SJens Rosenboom else 3847a1faa698SJens Rosenboom preferred = 0; 3848f56619fcSBen Hutchings if (valid != INFINITY_LIFE_TIME) { 3849f56619fcSBen Hutchings if (valid > tval) 385085486af0SThomas Graf valid -= tval; 3851f56619fcSBen Hutchings else 3852f56619fcSBen Hutchings valid = 0; 3853f56619fcSBen Hutchings } 38541da177e4SLinus Torvalds } 38551da177e4SLinus Torvalds } else { 385685486af0SThomas Graf preferred = INFINITY_LIFE_TIME; 385785486af0SThomas Graf valid = INFINITY_LIFE_TIME; 38581da177e4SLinus Torvalds } 385985486af0SThomas Graf 38607996c799SCong Wang if (!ipv6_addr_any(&ifa->peer_addr)) { 3861caeaba79SNicolas Dichtel if (nla_put(skb, IFA_LOCAL, 16, &ifa->addr) < 0 || 3862caeaba79SNicolas Dichtel nla_put(skb, IFA_ADDRESS, 16, &ifa->peer_addr) < 0) 3863caeaba79SNicolas Dichtel goto error; 3864caeaba79SNicolas Dichtel } else 3865caeaba79SNicolas Dichtel if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0) 3866caeaba79SNicolas Dichtel goto error; 3867caeaba79SNicolas Dichtel 3868caeaba79SNicolas Dichtel if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) 3869caeaba79SNicolas Dichtel goto error; 387085486af0SThomas Graf 38710ab6803bSThomas Graf return nlmsg_end(skb, nlh); 3872caeaba79SNicolas Dichtel 3873caeaba79SNicolas Dichtel error: 3874caeaba79SNicolas Dichtel nlmsg_cancel(skb, nlh); 3875caeaba79SNicolas Dichtel return -EMSGSIZE; 38761da177e4SLinus Torvalds } 38771da177e4SLinus Torvalds 38781da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca, 387915e47304SEric W. Biederman u32 portid, u32 seq, int event, u16 flags) 38801da177e4SLinus Torvalds { 38811da177e4SLinus Torvalds struct nlmsghdr *nlh; 3882101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 3883101bb229SThomas Graf int ifindex = ifmca->idev->dev->ifindex; 38841da177e4SLinus Torvalds 38851da177e4SLinus Torvalds if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE) 3886101bb229SThomas Graf scope = RT_SCOPE_SITE; 3887101bb229SThomas Graf 388815e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 38890ab6803bSThomas Graf if (nlh == NULL) 389026932566SPatrick McHardy return -EMSGSIZE; 38910ab6803bSThomas Graf 3892101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 38930ab6803bSThomas Graf if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 || 38940ab6803bSThomas Graf put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, 389526932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 389626932566SPatrick McHardy nlmsg_cancel(skb, nlh); 389726932566SPatrick McHardy return -EMSGSIZE; 389826932566SPatrick McHardy } 389985486af0SThomas Graf 39000ab6803bSThomas Graf return nlmsg_end(skb, nlh); 39011da177e4SLinus Torvalds } 39021da177e4SLinus Torvalds 39031da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca, 390415e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 39051da177e4SLinus Torvalds { 39061da177e4SLinus Torvalds struct nlmsghdr *nlh; 3907101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 3908101bb229SThomas Graf int ifindex = ifaca->aca_idev->dev->ifindex; 39091da177e4SLinus Torvalds 39101da177e4SLinus Torvalds if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) 3911101bb229SThomas Graf scope = RT_SCOPE_SITE; 3912101bb229SThomas Graf 391315e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 39140ab6803bSThomas Graf if (nlh == NULL) 391526932566SPatrick McHardy return -EMSGSIZE; 39160ab6803bSThomas Graf 3917101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 39180ab6803bSThomas Graf if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 || 39190ab6803bSThomas Graf put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, 392026932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 392126932566SPatrick McHardy nlmsg_cancel(skb, nlh); 392226932566SPatrick McHardy return -EMSGSIZE; 392326932566SPatrick McHardy } 392485486af0SThomas Graf 39250ab6803bSThomas Graf return nlmsg_end(skb, nlh); 39261da177e4SLinus Torvalds } 39271da177e4SLinus Torvalds 3928e21e8467SStephen Hemminger enum addr_type_t { 39291da177e4SLinus Torvalds UNICAST_ADDR, 39301da177e4SLinus Torvalds MULTICAST_ADDR, 39311da177e4SLinus Torvalds ANYCAST_ADDR, 39321da177e4SLinus Torvalds }; 39331da177e4SLinus Torvalds 3934234b27c3SEric Dumazet /* called with rcu_read_lock() */ 3935234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb, 3936234b27c3SEric Dumazet struct netlink_callback *cb, enum addr_type_t type, 3937234b27c3SEric Dumazet int s_ip_idx, int *p_ip_idx) 39381da177e4SLinus Torvalds { 39391da177e4SLinus Torvalds struct ifmcaddr6 *ifmca; 39401da177e4SLinus Torvalds struct ifacaddr6 *ifaca; 3941234b27c3SEric Dumazet int err = 1; 3942234b27c3SEric Dumazet int ip_idx = *p_ip_idx; 39431da177e4SLinus Torvalds 39441da177e4SLinus Torvalds read_lock_bh(&idev->lock); 39451da177e4SLinus Torvalds switch (type) { 3946502a2ffdSstephen hemminger case UNICAST_ADDR: { 3947502a2ffdSstephen hemminger struct inet6_ifaddr *ifa; 3948502a2ffdSstephen hemminger 3949ae9cda5dSYOSHIFUJI Hideaki /* unicast address incl. temp addr */ 3950502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 3951502a2ffdSstephen hemminger if (++ip_idx < s_ip_idx) 39521da177e4SLinus Torvalds continue; 39535d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifaddr(skb, ifa, 395415e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 39555d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 39565d5619b4SYOSHIFUJI Hideaki RTM_NEWADDR, 39575d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 3958bcd32326SEric Dumazet if (err <= 0) 3959bcd32326SEric Dumazet break; 396063998ac2SNicolas Dichtel nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 39611da177e4SLinus Torvalds } 39621da177e4SLinus Torvalds break; 3963502a2ffdSstephen hemminger } 39641da177e4SLinus Torvalds case MULTICAST_ADDR: 39651da177e4SLinus Torvalds /* multicast address */ 39661da177e4SLinus Torvalds for (ifmca = idev->mc_list; ifmca; 39671da177e4SLinus Torvalds ifmca = ifmca->next, ip_idx++) { 39681da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 39691da177e4SLinus Torvalds continue; 39705d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifmcaddr(skb, ifmca, 397115e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 39725d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 39735d5619b4SYOSHIFUJI Hideaki RTM_GETMULTICAST, 39745d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 3975bcd32326SEric Dumazet if (err <= 0) 3976bcd32326SEric Dumazet break; 39771da177e4SLinus Torvalds } 39781da177e4SLinus Torvalds break; 39791da177e4SLinus Torvalds case ANYCAST_ADDR: 39801da177e4SLinus Torvalds /* anycast address */ 39811da177e4SLinus Torvalds for (ifaca = idev->ac_list; ifaca; 39821da177e4SLinus Torvalds ifaca = ifaca->aca_next, ip_idx++) { 39831da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 39841da177e4SLinus Torvalds continue; 39855d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifacaddr(skb, ifaca, 398615e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 39875d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 39885d5619b4SYOSHIFUJI Hideaki RTM_GETANYCAST, 39895d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 3990bcd32326SEric Dumazet if (err <= 0) 3991bcd32326SEric Dumazet break; 39921da177e4SLinus Torvalds } 39931da177e4SLinus Torvalds break; 39941da177e4SLinus Torvalds default: 39951da177e4SLinus Torvalds break; 39961da177e4SLinus Torvalds } 39971da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 3998234b27c3SEric Dumazet *p_ip_idx = ip_idx; 3999234b27c3SEric Dumazet return err; 4000234b27c3SEric Dumazet } 40015d5619b4SYOSHIFUJI Hideaki 4002234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, 4003234b27c3SEric Dumazet enum addr_type_t type) 4004234b27c3SEric Dumazet { 4005234b27c3SEric Dumazet struct net *net = sock_net(skb->sk); 4006234b27c3SEric Dumazet int h, s_h; 4007234b27c3SEric Dumazet int idx, ip_idx; 4008234b27c3SEric Dumazet int s_idx, s_ip_idx; 4009234b27c3SEric Dumazet struct net_device *dev; 4010234b27c3SEric Dumazet struct inet6_dev *idev; 4011234b27c3SEric Dumazet struct hlist_head *head; 4012234b27c3SEric Dumazet 4013234b27c3SEric Dumazet s_h = cb->args[0]; 4014234b27c3SEric Dumazet s_idx = idx = cb->args[1]; 4015234b27c3SEric Dumazet s_ip_idx = ip_idx = cb->args[2]; 4016234b27c3SEric Dumazet 4017234b27c3SEric Dumazet rcu_read_lock(); 401863998ac2SNicolas Dichtel cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq; 4019234b27c3SEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 4020234b27c3SEric Dumazet idx = 0; 4021234b27c3SEric Dumazet head = &net->dev_index_head[h]; 4022b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 4023234b27c3SEric Dumazet if (idx < s_idx) 4024234b27c3SEric Dumazet goto cont; 40254b97efdfSPatrick McHardy if (h > s_h || idx > s_idx) 4026234b27c3SEric Dumazet s_ip_idx = 0; 4027234b27c3SEric Dumazet ip_idx = 0; 4028e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 4029e21e8467SStephen Hemminger if (!idev) 4030234b27c3SEric Dumazet goto cont; 4031234b27c3SEric Dumazet 4032234b27c3SEric Dumazet if (in6_dump_addrs(idev, skb, cb, type, 4033234b27c3SEric Dumazet s_ip_idx, &ip_idx) <= 0) 4034234b27c3SEric Dumazet goto done; 40357562f876SPavel Emelianov cont: 40367562f876SPavel Emelianov idx++; 40371da177e4SLinus Torvalds } 4038234b27c3SEric Dumazet } 4039234b27c3SEric Dumazet done: 4040234b27c3SEric Dumazet rcu_read_unlock(); 4041234b27c3SEric Dumazet cb->args[0] = h; 4042234b27c3SEric Dumazet cb->args[1] = idx; 4043234b27c3SEric Dumazet cb->args[2] = ip_idx; 4044234b27c3SEric Dumazet 40451da177e4SLinus Torvalds return skb->len; 40461da177e4SLinus Torvalds } 40471da177e4SLinus Torvalds 40481da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 40491da177e4SLinus Torvalds { 40501da177e4SLinus Torvalds enum addr_type_t type = UNICAST_ADDR; 4051b854272bSDenis V. Lunev 40521da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 40531da177e4SLinus Torvalds } 40541da177e4SLinus Torvalds 40551da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) 40561da177e4SLinus Torvalds { 40571da177e4SLinus Torvalds enum addr_type_t type = MULTICAST_ADDR; 4058b854272bSDenis V. Lunev 40591da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 40601da177e4SLinus Torvalds } 40611da177e4SLinus Torvalds 40621da177e4SLinus Torvalds 40631da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) 40641da177e4SLinus Torvalds { 40651da177e4SLinus Torvalds enum addr_type_t type = ANYCAST_ADDR; 4066b854272bSDenis V. Lunev 40671da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 40681da177e4SLinus Torvalds } 40691da177e4SLinus Torvalds 4070661d2967SThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh) 40716c223828SNoriaki TAKAMIYA { 40723b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 40731b29fc2cSThomas Graf struct ifaddrmsg *ifm; 40741b29fc2cSThomas Graf struct nlattr *tb[IFA_MAX+1]; 4075caeaba79SNicolas Dichtel struct in6_addr *addr = NULL, *peer; 40766c223828SNoriaki TAKAMIYA struct net_device *dev = NULL; 40776c223828SNoriaki TAKAMIYA struct inet6_ifaddr *ifa; 40786c223828SNoriaki TAKAMIYA struct sk_buff *skb; 40796c223828SNoriaki TAKAMIYA int err; 40806c223828SNoriaki TAKAMIYA 40811b29fc2cSThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 40821b29fc2cSThomas Graf if (err < 0) 40831b29fc2cSThomas Graf goto errout; 40846c223828SNoriaki TAKAMIYA 4085caeaba79SNicolas Dichtel addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer); 40861b29fc2cSThomas Graf if (addr == NULL) { 40871b29fc2cSThomas Graf err = -EINVAL; 40881b29fc2cSThomas Graf goto errout; 40891b29fc2cSThomas Graf } 40901b29fc2cSThomas Graf 40911b29fc2cSThomas Graf ifm = nlmsg_data(nlh); 40926c223828SNoriaki TAKAMIYA if (ifm->ifa_index) 40936fda7350SBenjamin Thery dev = __dev_get_by_index(net, ifm->ifa_index); 40946c223828SNoriaki TAKAMIYA 4095e21e8467SStephen Hemminger ifa = ipv6_get_ifaddr(net, addr, dev, 1); 4096e21e8467SStephen Hemminger if (!ifa) { 40971b29fc2cSThomas Graf err = -EADDRNOTAVAIL; 40981b29fc2cSThomas Graf goto errout; 40996c223828SNoriaki TAKAMIYA } 41006c223828SNoriaki TAKAMIYA 4101e21e8467SStephen Hemminger skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL); 4102e21e8467SStephen Hemminger if (!skb) { 41031b29fc2cSThomas Graf err = -ENOBUFS; 41041b29fc2cSThomas Graf goto errout_ifa; 41051b29fc2cSThomas Graf } 41061b29fc2cSThomas Graf 410715e47304SEric W. Biederman err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid, 41086c223828SNoriaki TAKAMIYA nlh->nlmsg_seq, RTM_NEWADDR, 0); 410926932566SPatrick McHardy if (err < 0) { 411026932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 411126932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 411226932566SPatrick McHardy kfree_skb(skb); 411326932566SPatrick McHardy goto errout_ifa; 411426932566SPatrick McHardy } 411515e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 41161b29fc2cSThomas Graf errout_ifa: 41176c223828SNoriaki TAKAMIYA in6_ifa_put(ifa); 41181b29fc2cSThomas Graf errout: 41196c223828SNoriaki TAKAMIYA return err; 41206c223828SNoriaki TAKAMIYA } 41216c223828SNoriaki TAKAMIYA 41221da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) 41231da177e4SLinus Torvalds { 41241da177e4SLinus Torvalds struct sk_buff *skb; 4125c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(ifa->idev->dev); 41265d620266SThomas Graf int err = -ENOBUFS; 41271da177e4SLinus Torvalds 41280ab6803bSThomas Graf skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); 41295d620266SThomas Graf if (skb == NULL) 41305d620266SThomas Graf goto errout; 41315d620266SThomas Graf 41325d620266SThomas Graf err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0); 413326932566SPatrick McHardy if (err < 0) { 413426932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 413526932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 413626932566SPatrick McHardy kfree_skb(skb); 413726932566SPatrick McHardy goto errout; 413826932566SPatrick McHardy } 41391ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 41401ce85fe4SPablo Neira Ayuso return; 41415d620266SThomas Graf errout: 41425d620266SThomas Graf if (err < 0) 41436fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); 41441da177e4SLinus Torvalds } 41451da177e4SLinus Torvalds 4146b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf, 41471da177e4SLinus Torvalds __s32 *array, int bytes) 41481da177e4SLinus Torvalds { 414904561c1fSThomas Graf BUG_ON(bytes < (DEVCONF_MAX * 4)); 415004561c1fSThomas Graf 41511da177e4SLinus Torvalds memset(array, 0, bytes); 41521da177e4SLinus Torvalds array[DEVCONF_FORWARDING] = cnf->forwarding; 41531da177e4SLinus Torvalds array[DEVCONF_HOPLIMIT] = cnf->hop_limit; 41541da177e4SLinus Torvalds array[DEVCONF_MTU6] = cnf->mtu6; 41551da177e4SLinus Torvalds array[DEVCONF_ACCEPT_RA] = cnf->accept_ra; 41561da177e4SLinus Torvalds array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects; 41571da177e4SLinus Torvalds array[DEVCONF_AUTOCONF] = cnf->autoconf; 41581da177e4SLinus Torvalds array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits; 41591da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits; 416093908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_INTERVAL] = 416193908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_interval); 416293908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_DELAY] = 416393908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_delay); 41641da177e4SLinus Torvalds array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version; 41651da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 41661da177e4SLinus Torvalds array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; 41671da177e4SLinus Torvalds array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; 41681da177e4SLinus Torvalds array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; 41691da177e4SLinus Torvalds array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; 41701da177e4SLinus Torvalds array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; 41711da177e4SLinus Torvalds #endif 41721da177e4SLinus Torvalds array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; 417365f5c7c1SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; 4174c4fd30ebSYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; 4175930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 4176930d6ff2SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref; 417793908d19SThomas Graf array[DEVCONF_RTR_PROBE_INTERVAL] = 417893908d19SThomas Graf jiffies_to_msecs(cnf->rtr_probe_interval); 4179fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 418009c884d4SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen; 418109c884d4SYOSHIFUJI Hideaki #endif 4182930d6ff2SYOSHIFUJI Hideaki #endif 4183fbea49e1SYOSHIFUJI Hideaki array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp; 41840bcbc926SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route; 418595c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 418695c385b4SNeil Horman array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad; 418795c385b4SNeil Horman #endif 41887bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 41897bc570c8SYOSHIFUJI Hideaki array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding; 41907bc570c8SYOSHIFUJI Hideaki #endif 4191778d80beSYOSHIFUJI Hideaki array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6; 41921b34be74SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad; 4193c3faca05SCosmin Ratiu array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao; 41945cb04436SHannes Frederic Sowa array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify; 41951da177e4SLinus Torvalds } 41961da177e4SLinus Torvalds 4197b382b191SThomas Graf static inline size_t inet6_ifla6_size(void) 4198b382b191SThomas Graf { 4199b382b191SThomas Graf return nla_total_size(4) /* IFLA_INET6_FLAGS */ 4200b382b191SThomas Graf + nla_total_size(sizeof(struct ifla_cacheinfo)) 4201b382b191SThomas Graf + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */ 4202b382b191SThomas Graf + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */ 4203f53adae4SDaniel Borkmann + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */ 4204f53adae4SDaniel Borkmann + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */ 4205b382b191SThomas Graf } 4206b382b191SThomas Graf 4207339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void) 4208339bf98fSThomas Graf { 4209339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 4210339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 4211339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 4212339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 4213339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 4214b382b191SThomas Graf + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */ 4215339bf98fSThomas Graf } 4216c5396a31SYOSHIFUJI Hideaki 4217be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib, 42187d720c3eSTejun Heo int items, int bytes) 42197f7d9a6bSHerbert Xu { 42207f7d9a6bSHerbert Xu int i; 42217f7d9a6bSHerbert Xu int pad = bytes - sizeof(u64) * items; 42227f7d9a6bSHerbert Xu BUG_ON(pad < 0); 42237f7d9a6bSHerbert Xu 42247f7d9a6bSHerbert Xu /* Use put_unaligned() because stats may not be aligned for u64. */ 42257f7d9a6bSHerbert Xu put_unaligned(items, &stats[0]); 42267f7d9a6bSHerbert Xu for (i = 1; i < items; i++) 4227be281e55SEric Dumazet put_unaligned(atomic_long_read(&mib[i]), &stats[i]); 42287f7d9a6bSHerbert Xu 42297f7d9a6bSHerbert Xu memset(&stats[items], 0, pad); 42307f7d9a6bSHerbert Xu } 42317f7d9a6bSHerbert Xu 42324ce3c183SEric Dumazet static inline void __snmp6_fill_stats64(u64 *stats, void __percpu **mib, 42334ce3c183SEric Dumazet int items, int bytes, size_t syncpoff) 42344ce3c183SEric Dumazet { 42354ce3c183SEric Dumazet int i; 42364ce3c183SEric Dumazet int pad = bytes - sizeof(u64) * items; 42374ce3c183SEric Dumazet BUG_ON(pad < 0); 42384ce3c183SEric Dumazet 42394ce3c183SEric Dumazet /* Use put_unaligned() because stats may not be aligned for u64. */ 42404ce3c183SEric Dumazet put_unaligned(items, &stats[0]); 42414ce3c183SEric Dumazet for (i = 1; i < items; i++) 42424ce3c183SEric Dumazet put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]); 42434ce3c183SEric Dumazet 42444ce3c183SEric Dumazet memset(&stats[items], 0, pad); 42454ce3c183SEric Dumazet } 42464ce3c183SEric Dumazet 42477f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype, 42487f7d9a6bSHerbert Xu int bytes) 42497f7d9a6bSHerbert Xu { 42507f7d9a6bSHerbert Xu switch (attrtype) { 42517f7d9a6bSHerbert Xu case IFLA_INET6_STATS: 42524ce3c183SEric Dumazet __snmp6_fill_stats64(stats, (void __percpu **)idev->stats.ipv6, 42534ce3c183SEric Dumazet IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp)); 42547f7d9a6bSHerbert Xu break; 42557f7d9a6bSHerbert Xu case IFLA_INET6_ICMP6STATS: 4256be281e55SEric Dumazet __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes); 42577f7d9a6bSHerbert Xu break; 42587f7d9a6bSHerbert Xu } 42597f7d9a6bSHerbert Xu } 42607f7d9a6bSHerbert Xu 4261b382b191SThomas Graf static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev) 42621da177e4SLinus Torvalds { 4263bf99f1bdSYOSHIFUJI Hideaki struct nlattr *nla; 42641da177e4SLinus Torvalds struct ifla_cacheinfo ci; 42651da177e4SLinus Torvalds 4266c78679e8SDavid S. Miller if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags)) 4267c78679e8SDavid S. Miller goto nla_put_failure; 42681da177e4SLinus Torvalds ci.max_reasm_len = IPV6_MAXPLEN; 426924912420SDavid S. Miller ci.tstamp = cstamp_delta(idev->tstamp); 427024912420SDavid S. Miller ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time); 427124912420SDavid S. Miller ci.retrans_time = jiffies_to_msecs(idev->nd_parms->retrans_time); 4272c78679e8SDavid S. Miller if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci)) 4273c78679e8SDavid S. Miller goto nla_put_failure; 4274bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); 4275bf99f1bdSYOSHIFUJI Hideaki if (nla == NULL) 427604561c1fSThomas Graf goto nla_put_failure; 4277bf99f1bdSYOSHIFUJI Hideaki ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); 42781da177e4SLinus Torvalds 4279bf99f1bdSYOSHIFUJI Hideaki /* XXX - MC not implemented */ 4280bf99f1bdSYOSHIFUJI Hideaki 4281bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); 4282bf99f1bdSYOSHIFUJI Hideaki if (nla == NULL) 4283bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 4284bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); 4285bf99f1bdSYOSHIFUJI Hideaki 4286bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); 4287bf99f1bdSYOSHIFUJI Hideaki if (nla == NULL) 4288bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 4289bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); 42901da177e4SLinus Torvalds 4291f53adae4SDaniel Borkmann nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr)); 4292f53adae4SDaniel Borkmann if (nla == NULL) 4293f53adae4SDaniel Borkmann goto nla_put_failure; 4294f53adae4SDaniel Borkmann read_lock_bh(&idev->lock); 4295f53adae4SDaniel Borkmann memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla)); 4296f53adae4SDaniel Borkmann read_unlock_bh(&idev->lock); 4297f53adae4SDaniel Borkmann 4298b382b191SThomas Graf return 0; 4299b382b191SThomas Graf 4300b382b191SThomas Graf nla_put_failure: 4301b382b191SThomas Graf return -EMSGSIZE; 4302b382b191SThomas Graf } 4303b382b191SThomas Graf 4304b382b191SThomas Graf static size_t inet6_get_link_af_size(const struct net_device *dev) 4305b382b191SThomas Graf { 4306b382b191SThomas Graf if (!__in6_dev_get(dev)) 4307b382b191SThomas Graf return 0; 4308b382b191SThomas Graf 4309b382b191SThomas Graf return inet6_ifla6_size(); 4310b382b191SThomas Graf } 4311b382b191SThomas Graf 4312b382b191SThomas Graf static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev) 4313b382b191SThomas Graf { 4314b382b191SThomas Graf struct inet6_dev *idev = __in6_dev_get(dev); 4315b382b191SThomas Graf 4316b382b191SThomas Graf if (!idev) 4317b382b191SThomas Graf return -ENODATA; 4318b382b191SThomas Graf 4319b382b191SThomas Graf if (inet6_fill_ifla6_attrs(skb, idev) < 0) 4320b382b191SThomas Graf return -EMSGSIZE; 4321b382b191SThomas Graf 4322b382b191SThomas Graf return 0; 4323b382b191SThomas Graf } 4324b382b191SThomas Graf 4325f53adae4SDaniel Borkmann static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token) 4326f53adae4SDaniel Borkmann { 4327f53adae4SDaniel Borkmann struct inet6_ifaddr *ifp; 4328f53adae4SDaniel Borkmann struct net_device *dev = idev->dev; 4329fc403832SDaniel Borkmann bool update_rs = false; 4330f53adae4SDaniel Borkmann 4331f53adae4SDaniel Borkmann if (token == NULL) 4332f53adae4SDaniel Borkmann return -EINVAL; 4333f53adae4SDaniel Borkmann if (ipv6_addr_any(token)) 4334f53adae4SDaniel Borkmann return -EINVAL; 4335f53adae4SDaniel Borkmann if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) 4336f53adae4SDaniel Borkmann return -EINVAL; 4337f53adae4SDaniel Borkmann if (!ipv6_accept_ra(idev)) 4338f53adae4SDaniel Borkmann return -EINVAL; 4339f53adae4SDaniel Borkmann if (idev->cnf.rtr_solicits <= 0) 4340f53adae4SDaniel Borkmann return -EINVAL; 4341f53adae4SDaniel Borkmann 4342f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 4343f53adae4SDaniel Borkmann 4344f53adae4SDaniel Borkmann BUILD_BUG_ON(sizeof(token->s6_addr) != 16); 4345f53adae4SDaniel Borkmann memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8); 4346f53adae4SDaniel Borkmann 4347f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 4348f53adae4SDaniel Borkmann 4349fc403832SDaniel Borkmann if (!idev->dead && (idev->if_flags & IF_READY)) { 4350fc403832SDaniel Borkmann struct in6_addr ll_addr; 4351fc403832SDaniel Borkmann 4352fc403832SDaniel Borkmann ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE | 4353fc403832SDaniel Borkmann IFA_F_OPTIMISTIC); 4354fc403832SDaniel Borkmann 4355fc403832SDaniel Borkmann /* If we're not ready, then normal ifup will take care 4356fc403832SDaniel Borkmann * of this. Otherwise, we need to request our rs here. 4357fc403832SDaniel Borkmann */ 4358f53adae4SDaniel Borkmann ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters); 4359fc403832SDaniel Borkmann update_rs = true; 4360fc403832SDaniel Borkmann } 4361f53adae4SDaniel Borkmann 4362f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 4363fc403832SDaniel Borkmann 4364*77ecaaceSHannes Frederic Sowa if (update_rs) { 4365f53adae4SDaniel Borkmann idev->if_flags |= IF_RS_SENT; 4366*77ecaaceSHannes Frederic Sowa idev->rs_probes = 1; 4367*77ecaaceSHannes Frederic Sowa addrconf_mod_rs_timer(idev, idev->cnf.rtr_solicit_interval); 4368*77ecaaceSHannes Frederic Sowa } 4369f53adae4SDaniel Borkmann 4370f53adae4SDaniel Borkmann /* Well, that's kinda nasty ... */ 4371f53adae4SDaniel Borkmann list_for_each_entry(ifp, &idev->addr_list, if_list) { 4372f53adae4SDaniel Borkmann spin_lock(&ifp->lock); 4373617fe29dSDaniel Borkmann if (ifp->tokenized) { 4374f53adae4SDaniel Borkmann ifp->valid_lft = 0; 4375f53adae4SDaniel Borkmann ifp->prefered_lft = 0; 4376f53adae4SDaniel Borkmann } 4377f53adae4SDaniel Borkmann spin_unlock(&ifp->lock); 4378f53adae4SDaniel Borkmann } 4379f53adae4SDaniel Borkmann 4380f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 43812b9651d7SHannes Frederic Sowa addrconf_verify(0); 4382f53adae4SDaniel Borkmann return 0; 4383f53adae4SDaniel Borkmann } 4384f53adae4SDaniel Borkmann 4385f53adae4SDaniel Borkmann static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla) 4386f53adae4SDaniel Borkmann { 4387f53adae4SDaniel Borkmann int err = -EINVAL; 4388f53adae4SDaniel Borkmann struct inet6_dev *idev = __in6_dev_get(dev); 4389f53adae4SDaniel Borkmann struct nlattr *tb[IFLA_INET6_MAX + 1]; 4390f53adae4SDaniel Borkmann 4391f53adae4SDaniel Borkmann if (!idev) 4392f53adae4SDaniel Borkmann return -EAFNOSUPPORT; 4393f53adae4SDaniel Borkmann 4394f53adae4SDaniel Borkmann if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0) 4395f53adae4SDaniel Borkmann BUG(); 4396f53adae4SDaniel Borkmann 4397f53adae4SDaniel Borkmann if (tb[IFLA_INET6_TOKEN]) 4398f53adae4SDaniel Borkmann err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN])); 4399f53adae4SDaniel Borkmann 4400f53adae4SDaniel Borkmann return err; 4401f53adae4SDaniel Borkmann } 4402f53adae4SDaniel Borkmann 4403b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, 440415e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 4405b382b191SThomas Graf { 4406b382b191SThomas Graf struct net_device *dev = idev->dev; 4407b382b191SThomas Graf struct ifinfomsg *hdr; 4408b382b191SThomas Graf struct nlmsghdr *nlh; 4409b382b191SThomas Graf void *protoinfo; 4410b382b191SThomas Graf 441115e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags); 4412b382b191SThomas Graf if (nlh == NULL) 4413b382b191SThomas Graf return -EMSGSIZE; 4414b382b191SThomas Graf 4415b382b191SThomas Graf hdr = nlmsg_data(nlh); 4416b382b191SThomas Graf hdr->ifi_family = AF_INET6; 4417b382b191SThomas Graf hdr->__ifi_pad = 0; 4418b382b191SThomas Graf hdr->ifi_type = dev->type; 4419b382b191SThomas Graf hdr->ifi_index = dev->ifindex; 4420b382b191SThomas Graf hdr->ifi_flags = dev_get_flags(dev); 4421b382b191SThomas Graf hdr->ifi_change = 0; 4422b382b191SThomas Graf 4423c78679e8SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 4424c78679e8SDavid S. Miller (dev->addr_len && 4425c78679e8SDavid S. Miller nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 4426c78679e8SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 4427c78679e8SDavid S. Miller (dev->ifindex != dev->iflink && 4428c78679e8SDavid S. Miller nla_put_u32(skb, IFLA_LINK, dev->iflink))) 4429c78679e8SDavid S. Miller goto nla_put_failure; 4430b382b191SThomas Graf protoinfo = nla_nest_start(skb, IFLA_PROTINFO); 4431b382b191SThomas Graf if (protoinfo == NULL) 4432b382b191SThomas Graf goto nla_put_failure; 4433b382b191SThomas Graf 4434b382b191SThomas Graf if (inet6_fill_ifla6_attrs(skb, idev) < 0) 4435b382b191SThomas Graf goto nla_put_failure; 4436b382b191SThomas Graf 443704561c1fSThomas Graf nla_nest_end(skb, protoinfo); 443804561c1fSThomas Graf return nlmsg_end(skb, nlh); 44391da177e4SLinus Torvalds 444004561c1fSThomas Graf nla_put_failure: 444126932566SPatrick McHardy nlmsg_cancel(skb, nlh); 444226932566SPatrick McHardy return -EMSGSIZE; 44431da177e4SLinus Torvalds } 44441da177e4SLinus Torvalds 44451da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 44461da177e4SLinus Torvalds { 44473b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 444884d2697dSEric Dumazet int h, s_h; 4449434a8a58SDavid S. Miller int idx = 0, s_idx; 44501da177e4SLinus Torvalds struct net_device *dev; 44511da177e4SLinus Torvalds struct inet6_dev *idev; 445284d2697dSEric Dumazet struct hlist_head *head; 44531da177e4SLinus Torvalds 445484d2697dSEric Dumazet s_h = cb->args[0]; 445584d2697dSEric Dumazet s_idx = cb->args[1]; 445684d2697dSEric Dumazet 445784d2697dSEric Dumazet rcu_read_lock(); 445884d2697dSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 44597562f876SPavel Emelianov idx = 0; 446084d2697dSEric Dumazet head = &net->dev_index_head[h]; 4461b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 44621da177e4SLinus Torvalds if (idx < s_idx) 44637562f876SPavel Emelianov goto cont; 446484d2697dSEric Dumazet idev = __in6_dev_get(dev); 446584d2697dSEric Dumazet if (!idev) 44667562f876SPavel Emelianov goto cont; 446784d2697dSEric Dumazet if (inet6_fill_ifinfo(skb, idev, 446815e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 446984d2697dSEric Dumazet cb->nlh->nlmsg_seq, 447084d2697dSEric Dumazet RTM_NEWLINK, NLM_F_MULTI) <= 0) 447184d2697dSEric Dumazet goto out; 44727562f876SPavel Emelianov cont: 44737562f876SPavel Emelianov idx++; 44741da177e4SLinus Torvalds } 447584d2697dSEric Dumazet } 447684d2697dSEric Dumazet out: 447784d2697dSEric Dumazet rcu_read_unlock(); 447884d2697dSEric Dumazet cb->args[1] = idx; 447984d2697dSEric Dumazet cb->args[0] = h; 44801da177e4SLinus Torvalds 44811da177e4SLinus Torvalds return skb->len; 44821da177e4SLinus Torvalds } 44831da177e4SLinus Torvalds 44841da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev) 44851da177e4SLinus Torvalds { 44861da177e4SLinus Torvalds struct sk_buff *skb; 4487c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 44888d7a76c9SThomas Graf int err = -ENOBUFS; 44891da177e4SLinus Torvalds 4490339bf98fSThomas Graf skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); 44918d7a76c9SThomas Graf if (skb == NULL) 44928d7a76c9SThomas Graf goto errout; 44938d7a76c9SThomas Graf 44948d7a76c9SThomas Graf err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); 449526932566SPatrick McHardy if (err < 0) { 449626932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */ 449726932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 449826932566SPatrick McHardy kfree_skb(skb); 449926932566SPatrick McHardy goto errout; 450026932566SPatrick McHardy } 45015f75a104SNicolas Dichtel rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC); 45021ce85fe4SPablo Neira Ayuso return; 45038d7a76c9SThomas Graf errout: 45048d7a76c9SThomas Graf if (err < 0) 45055f75a104SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err); 45061da177e4SLinus Torvalds } 45071da177e4SLinus Torvalds 4508339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void) 4509339bf98fSThomas Graf { 4510339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct prefixmsg)) 4511339bf98fSThomas Graf + nla_total_size(sizeof(struct in6_addr)) 4512339bf98fSThomas Graf + nla_total_size(sizeof(struct prefix_cacheinfo)); 4513339bf98fSThomas Graf } 4514c5396a31SYOSHIFUJI Hideaki 45151da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, 451615e47304SEric W. Biederman struct prefix_info *pinfo, u32 portid, u32 seq, 4517b6544c0bSJamal Hadi Salim int event, unsigned int flags) 45181da177e4SLinus Torvalds { 45191da177e4SLinus Torvalds struct prefixmsg *pmsg; 45201da177e4SLinus Torvalds struct nlmsghdr *nlh; 45211da177e4SLinus Torvalds struct prefix_cacheinfo ci; 45221da177e4SLinus Torvalds 452315e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags); 45246051e2f4SThomas Graf if (nlh == NULL) 452526932566SPatrick McHardy return -EMSGSIZE; 45266051e2f4SThomas Graf 45276051e2f4SThomas Graf pmsg = nlmsg_data(nlh); 45281da177e4SLinus Torvalds pmsg->prefix_family = AF_INET6; 45298a47077aSPatrick McHardy pmsg->prefix_pad1 = 0; 45308a47077aSPatrick McHardy pmsg->prefix_pad2 = 0; 45311da177e4SLinus Torvalds pmsg->prefix_ifindex = idev->dev->ifindex; 45321da177e4SLinus Torvalds pmsg->prefix_len = pinfo->prefix_len; 45331da177e4SLinus Torvalds pmsg->prefix_type = pinfo->type; 45348a47077aSPatrick McHardy pmsg->prefix_pad3 = 0; 45351da177e4SLinus Torvalds pmsg->prefix_flags = 0; 45361da177e4SLinus Torvalds if (pinfo->onlink) 45371da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_ONLINK; 45381da177e4SLinus Torvalds if (pinfo->autoconf) 45391da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_AUTOCONF; 45401da177e4SLinus Torvalds 4541c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix)) 4542c78679e8SDavid S. Miller goto nla_put_failure; 45431da177e4SLinus Torvalds ci.preferred_time = ntohl(pinfo->prefered); 45441da177e4SLinus Torvalds ci.valid_time = ntohl(pinfo->valid); 4545c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci)) 4546c78679e8SDavid S. Miller goto nla_put_failure; 45476051e2f4SThomas Graf return nlmsg_end(skb, nlh); 45481da177e4SLinus Torvalds 45496051e2f4SThomas Graf nla_put_failure: 455026932566SPatrick McHardy nlmsg_cancel(skb, nlh); 455126932566SPatrick McHardy return -EMSGSIZE; 45521da177e4SLinus Torvalds } 45531da177e4SLinus Torvalds 45541da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 45551da177e4SLinus Torvalds struct prefix_info *pinfo) 45561da177e4SLinus Torvalds { 45571da177e4SLinus Torvalds struct sk_buff *skb; 4558c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 45598c384bfaSThomas Graf int err = -ENOBUFS; 45601da177e4SLinus Torvalds 4561339bf98fSThomas Graf skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); 45628c384bfaSThomas Graf if (skb == NULL) 45638c384bfaSThomas Graf goto errout; 45648c384bfaSThomas Graf 45658c384bfaSThomas Graf err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); 456626932566SPatrick McHardy if (err < 0) { 456726932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */ 456826932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 456926932566SPatrick McHardy kfree_skb(skb); 457026932566SPatrick McHardy goto errout; 457126932566SPatrick McHardy } 45721ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); 45731ce85fe4SPablo Neira Ayuso return; 45748c384bfaSThomas Graf errout: 45758c384bfaSThomas Graf if (err < 0) 45766fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err); 45771da177e4SLinus Torvalds } 45781da177e4SLinus Torvalds 45791da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 45801da177e4SLinus Torvalds { 458163998ac2SNicolas Dichtel struct net *net = dev_net(ifp->idev->dev); 458263998ac2SNicolas Dichtel 45831da177e4SLinus Torvalds inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); 45841da177e4SLinus Torvalds 45851da177e4SLinus Torvalds switch (event) { 45861da177e4SLinus Torvalds case RTM_NEWADDR: 458795c385b4SNeil Horman /* 458895c385b4SNeil Horman * If the address was optimistic 458995c385b4SNeil Horman * we inserted the route at the start of 459095c385b4SNeil Horman * our DAD process, so we don't need 459195c385b4SNeil Horman * to do it again 459295c385b4SNeil Horman */ 459395c385b4SNeil Horman if (!(ifp->rt->rt6i_node)) 459440e22e8fSThomas Graf ip6_ins_rt(ifp->rt); 45951da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 45961da177e4SLinus Torvalds addrconf_join_anycast(ifp); 45977996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) 4598caeaba79SNicolas Dichtel addrconf_prefix_route(&ifp->peer_addr, 128, 4599caeaba79SNicolas Dichtel ifp->idev->dev, 0, 0); 46001da177e4SLinus Torvalds break; 46011da177e4SLinus Torvalds case RTM_DELADDR: 46021da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 46031da177e4SLinus Torvalds addrconf_leave_anycast(ifp); 46041da177e4SLinus Torvalds addrconf_leave_solict(ifp->idev, &ifp->addr); 46057996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) { 4606caeaba79SNicolas Dichtel struct rt6_info *rt; 4607caeaba79SNicolas Dichtel struct net_device *dev = ifp->idev->dev; 4608caeaba79SNicolas Dichtel 4609caeaba79SNicolas Dichtel rt = rt6_lookup(dev_net(dev), &ifp->peer_addr, NULL, 4610caeaba79SNicolas Dichtel dev->ifindex, 1); 4611caeaba79SNicolas Dichtel if (rt) { 4612caeaba79SNicolas Dichtel dst_hold(&rt->dst); 4613caeaba79SNicolas Dichtel if (ip6_del_rt(rt)) 4614caeaba79SNicolas Dichtel dst_free(&rt->dst); 4615caeaba79SNicolas Dichtel } 4616caeaba79SNicolas Dichtel } 4617d8d1f30bSChangli Gao dst_hold(&ifp->rt->dst); 461893fa159aSstephen hemminger 461973a8bd74SDavid S. Miller if (ip6_del_rt(ifp->rt)) 4620d8d1f30bSChangli Gao dst_free(&ifp->rt->dst); 46211da177e4SLinus Torvalds break; 46221da177e4SLinus Torvalds } 462363998ac2SNicolas Dichtel atomic_inc(&net->ipv6.dev_addr_genid); 46241da177e4SLinus Torvalds } 46251da177e4SLinus Torvalds 46261da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 46271da177e4SLinus Torvalds { 46288814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 46291da177e4SLinus Torvalds if (likely(ifp->idev->dead == 0)) 46301da177e4SLinus Torvalds __ipv6_ifa_notify(event, ifp); 46318814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 46321da177e4SLinus Torvalds } 46331da177e4SLinus Torvalds 46341da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 46351da177e4SLinus Torvalds 46361da177e4SLinus Torvalds static 4637fe2c6338SJoe Perches int addrconf_sysctl_forward(struct ctl_table *ctl, int write, 46381da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 46391da177e4SLinus Torvalds { 46401da177e4SLinus Torvalds int *valp = ctl->data; 46411da177e4SLinus Torvalds int val = *valp; 464288af182eSEric W. Biederman loff_t pos = *ppos; 4643fe2c6338SJoe Perches struct ctl_table lctl; 46441da177e4SLinus Torvalds int ret; 46451da177e4SLinus Torvalds 4646013d97e9SFrancesco Ruggeri /* 4647013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.forwarding, we should 4648013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 4649013d97e9SFrancesco Ruggeri */ 4650013d97e9SFrancesco Ruggeri lctl = *ctl; 4651013d97e9SFrancesco Ruggeri lctl.data = &val; 4652013d97e9SFrancesco Ruggeri 4653013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 46541da177e4SLinus Torvalds 4655c8fecf22SPavel Emelyanov if (write) 4656b325fddbSStephen Hemminger ret = addrconf_fixup_forwarding(ctl, valp, val); 465788af182eSEric W. Biederman if (ret) 465888af182eSEric W. Biederman *ppos = pos; 46591da177e4SLinus Torvalds return ret; 46601da177e4SLinus Torvalds } 46611da177e4SLinus Torvalds 466256d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev) 466356d417b1SBrian Haley { 466475538c2bSCong Wang struct netdev_notifier_info info; 466575538c2bSCong Wang 466656d417b1SBrian Haley if (!idev || !idev->dev) 466756d417b1SBrian Haley return; 466856d417b1SBrian Haley 466975538c2bSCong Wang netdev_notifier_info_init(&info, idev->dev); 467056d417b1SBrian Haley if (idev->cnf.disable_ipv6) 467175538c2bSCong Wang addrconf_notify(NULL, NETDEV_DOWN, &info); 467256d417b1SBrian Haley else 467375538c2bSCong Wang addrconf_notify(NULL, NETDEV_UP, &info); 467456d417b1SBrian Haley } 467556d417b1SBrian Haley 467656d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf) 467756d417b1SBrian Haley { 467856d417b1SBrian Haley struct net_device *dev; 467956d417b1SBrian Haley struct inet6_dev *idev; 468056d417b1SBrian Haley 468156d417b1SBrian Haley rcu_read_lock(); 4682c6d14c84SEric Dumazet for_each_netdev_rcu(net, dev) { 468356d417b1SBrian Haley idev = __in6_dev_get(dev); 468456d417b1SBrian Haley if (idev) { 468556d417b1SBrian Haley int changed = (!idev->cnf.disable_ipv6) ^ (!newf); 468656d417b1SBrian Haley idev->cnf.disable_ipv6 = newf; 468756d417b1SBrian Haley if (changed) 468856d417b1SBrian Haley dev_disable_change(idev); 468956d417b1SBrian Haley } 469056d417b1SBrian Haley } 4691c6d14c84SEric Dumazet rcu_read_unlock(); 469256d417b1SBrian Haley } 469356d417b1SBrian Haley 4694013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf) 469556d417b1SBrian Haley { 469656d417b1SBrian Haley struct net *net; 4697013d97e9SFrancesco Ruggeri int old; 4698013d97e9SFrancesco Ruggeri 4699013d97e9SFrancesco Ruggeri if (!rtnl_trylock()) 4700013d97e9SFrancesco Ruggeri return restart_syscall(); 470156d417b1SBrian Haley 470256d417b1SBrian Haley net = (struct net *)table->extra2; 4703013d97e9SFrancesco Ruggeri old = *p; 4704013d97e9SFrancesco Ruggeri *p = newf; 470556d417b1SBrian Haley 4706013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->disable_ipv6) { 4707013d97e9SFrancesco Ruggeri rtnl_unlock(); 470856d417b1SBrian Haley return 0; 470988af182eSEric W. Biederman } 471056d417b1SBrian Haley 471156d417b1SBrian Haley if (p == &net->ipv6.devconf_all->disable_ipv6) { 471256d417b1SBrian Haley net->ipv6.devconf_dflt->disable_ipv6 = newf; 471356d417b1SBrian Haley addrconf_disable_change(net, newf); 4714013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 471556d417b1SBrian Haley dev_disable_change((struct inet6_dev *)table->extra1); 471656d417b1SBrian Haley 471756d417b1SBrian Haley rtnl_unlock(); 471856d417b1SBrian Haley return 0; 471956d417b1SBrian Haley } 472056d417b1SBrian Haley 472156d417b1SBrian Haley static 4722fe2c6338SJoe Perches int addrconf_sysctl_disable(struct ctl_table *ctl, int write, 472356d417b1SBrian Haley void __user *buffer, size_t *lenp, loff_t *ppos) 472456d417b1SBrian Haley { 472556d417b1SBrian Haley int *valp = ctl->data; 472656d417b1SBrian Haley int val = *valp; 472788af182eSEric W. Biederman loff_t pos = *ppos; 4728fe2c6338SJoe Perches struct ctl_table lctl; 472956d417b1SBrian Haley int ret; 473056d417b1SBrian Haley 4731013d97e9SFrancesco Ruggeri /* 4732013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.disable_ipv6, we should 4733013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 4734013d97e9SFrancesco Ruggeri */ 4735013d97e9SFrancesco Ruggeri lctl = *ctl; 4736013d97e9SFrancesco Ruggeri lctl.data = &val; 4737013d97e9SFrancesco Ruggeri 4738013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 473956d417b1SBrian Haley 474056d417b1SBrian Haley if (write) 474156d417b1SBrian Haley ret = addrconf_disable_ipv6(ctl, valp, val); 474288af182eSEric W. Biederman if (ret) 474388af182eSEric W. Biederman *ppos = pos; 474456d417b1SBrian Haley return ret; 474556d417b1SBrian Haley } 474656d417b1SBrian Haley 47471da177e4SLinus Torvalds static struct addrconf_sysctl_table 47481da177e4SLinus Torvalds { 47491da177e4SLinus Torvalds struct ctl_table_header *sysctl_header; 4750fe2c6338SJoe Perches struct ctl_table addrconf_vars[DEVCONF_MAX+1]; 4751ab32ea5dSBrian Haley } addrconf_sysctl __read_mostly = { 47521da177e4SLinus Torvalds .sysctl_header = NULL, 47531da177e4SLinus Torvalds .addrconf_vars = { 47541da177e4SLinus Torvalds { 47551da177e4SLinus Torvalds .procname = "forwarding", 47561da177e4SLinus Torvalds .data = &ipv6_devconf.forwarding, 47571da177e4SLinus Torvalds .maxlen = sizeof(int), 47581da177e4SLinus Torvalds .mode = 0644, 47596d9f239aSAlexey Dobriyan .proc_handler = addrconf_sysctl_forward, 47601da177e4SLinus Torvalds }, 47611da177e4SLinus Torvalds { 47621da177e4SLinus Torvalds .procname = "hop_limit", 47631da177e4SLinus Torvalds .data = &ipv6_devconf.hop_limit, 47641da177e4SLinus Torvalds .maxlen = sizeof(int), 47651da177e4SLinus Torvalds .mode = 0644, 47661da177e4SLinus Torvalds .proc_handler = proc_dointvec, 47671da177e4SLinus Torvalds }, 47681da177e4SLinus Torvalds { 47691da177e4SLinus Torvalds .procname = "mtu", 47701da177e4SLinus Torvalds .data = &ipv6_devconf.mtu6, 47711da177e4SLinus Torvalds .maxlen = sizeof(int), 47721da177e4SLinus Torvalds .mode = 0644, 47736d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 47741da177e4SLinus Torvalds }, 47751da177e4SLinus Torvalds { 47761da177e4SLinus Torvalds .procname = "accept_ra", 47771da177e4SLinus Torvalds .data = &ipv6_devconf.accept_ra, 47781da177e4SLinus Torvalds .maxlen = sizeof(int), 47791da177e4SLinus Torvalds .mode = 0644, 47806d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 47811da177e4SLinus Torvalds }, 47821da177e4SLinus Torvalds { 47831da177e4SLinus Torvalds .procname = "accept_redirects", 47841da177e4SLinus Torvalds .data = &ipv6_devconf.accept_redirects, 47851da177e4SLinus Torvalds .maxlen = sizeof(int), 47861da177e4SLinus Torvalds .mode = 0644, 47876d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 47881da177e4SLinus Torvalds }, 47891da177e4SLinus Torvalds { 47901da177e4SLinus Torvalds .procname = "autoconf", 47911da177e4SLinus Torvalds .data = &ipv6_devconf.autoconf, 47921da177e4SLinus Torvalds .maxlen = sizeof(int), 47931da177e4SLinus Torvalds .mode = 0644, 47946d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 47951da177e4SLinus Torvalds }, 47961da177e4SLinus Torvalds { 47971da177e4SLinus Torvalds .procname = "dad_transmits", 47981da177e4SLinus Torvalds .data = &ipv6_devconf.dad_transmits, 47991da177e4SLinus Torvalds .maxlen = sizeof(int), 48001da177e4SLinus Torvalds .mode = 0644, 48016d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 48021da177e4SLinus Torvalds }, 48031da177e4SLinus Torvalds { 48041da177e4SLinus Torvalds .procname = "router_solicitations", 48051da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicits, 48061da177e4SLinus Torvalds .maxlen = sizeof(int), 48071da177e4SLinus Torvalds .mode = 0644, 48086d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 48091da177e4SLinus Torvalds }, 48101da177e4SLinus Torvalds { 48111da177e4SLinus Torvalds .procname = "router_solicitation_interval", 48121da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_interval, 48131da177e4SLinus Torvalds .maxlen = sizeof(int), 48141da177e4SLinus Torvalds .mode = 0644, 48156d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 48161da177e4SLinus Torvalds }, 48171da177e4SLinus Torvalds { 48181da177e4SLinus Torvalds .procname = "router_solicitation_delay", 48191da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_delay, 48201da177e4SLinus Torvalds .maxlen = sizeof(int), 48211da177e4SLinus Torvalds .mode = 0644, 48226d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 48231da177e4SLinus Torvalds }, 48241da177e4SLinus Torvalds { 48251da177e4SLinus Torvalds .procname = "force_mld_version", 48261da177e4SLinus Torvalds .data = &ipv6_devconf.force_mld_version, 48271da177e4SLinus Torvalds .maxlen = sizeof(int), 48281da177e4SLinus Torvalds .mode = 0644, 48296d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 48301da177e4SLinus Torvalds }, 48311da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 48321da177e4SLinus Torvalds { 48331da177e4SLinus Torvalds .procname = "use_tempaddr", 48341da177e4SLinus Torvalds .data = &ipv6_devconf.use_tempaddr, 48351da177e4SLinus Torvalds .maxlen = sizeof(int), 48361da177e4SLinus Torvalds .mode = 0644, 48376d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 48381da177e4SLinus Torvalds }, 48391da177e4SLinus Torvalds { 48401da177e4SLinus Torvalds .procname = "temp_valid_lft", 48411da177e4SLinus Torvalds .data = &ipv6_devconf.temp_valid_lft, 48421da177e4SLinus Torvalds .maxlen = sizeof(int), 48431da177e4SLinus Torvalds .mode = 0644, 48446d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 48451da177e4SLinus Torvalds }, 48461da177e4SLinus Torvalds { 48471da177e4SLinus Torvalds .procname = "temp_prefered_lft", 48481da177e4SLinus Torvalds .data = &ipv6_devconf.temp_prefered_lft, 48491da177e4SLinus Torvalds .maxlen = sizeof(int), 48501da177e4SLinus Torvalds .mode = 0644, 48516d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 48521da177e4SLinus Torvalds }, 48531da177e4SLinus Torvalds { 48541da177e4SLinus Torvalds .procname = "regen_max_retry", 48551da177e4SLinus Torvalds .data = &ipv6_devconf.regen_max_retry, 48561da177e4SLinus Torvalds .maxlen = sizeof(int), 48571da177e4SLinus Torvalds .mode = 0644, 48586d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 48591da177e4SLinus Torvalds }, 48601da177e4SLinus Torvalds { 48611da177e4SLinus Torvalds .procname = "max_desync_factor", 48621da177e4SLinus Torvalds .data = &ipv6_devconf.max_desync_factor, 48631da177e4SLinus Torvalds .maxlen = sizeof(int), 48641da177e4SLinus Torvalds .mode = 0644, 48656d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 48661da177e4SLinus Torvalds }, 48671da177e4SLinus Torvalds #endif 48681da177e4SLinus Torvalds { 48691da177e4SLinus Torvalds .procname = "max_addresses", 48701da177e4SLinus Torvalds .data = &ipv6_devconf.max_addresses, 48711da177e4SLinus Torvalds .maxlen = sizeof(int), 48721da177e4SLinus Torvalds .mode = 0644, 48736d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 48741da177e4SLinus Torvalds }, 48751da177e4SLinus Torvalds { 487665f5c7c1SYOSHIFUJI Hideaki .procname = "accept_ra_defrtr", 487765f5c7c1SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_defrtr, 487865f5c7c1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 487965f5c7c1SYOSHIFUJI Hideaki .mode = 0644, 48806d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 488165f5c7c1SYOSHIFUJI Hideaki }, 488265f5c7c1SYOSHIFUJI Hideaki { 4883c4fd30ebSYOSHIFUJI Hideaki .procname = "accept_ra_pinfo", 4884c4fd30ebSYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_pinfo, 4885c4fd30ebSYOSHIFUJI Hideaki .maxlen = sizeof(int), 4886c4fd30ebSYOSHIFUJI Hideaki .mode = 0644, 48876d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 4888c4fd30ebSYOSHIFUJI Hideaki }, 4889930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 4890930d6ff2SYOSHIFUJI Hideaki { 4891930d6ff2SYOSHIFUJI Hideaki .procname = "accept_ra_rtr_pref", 4892930d6ff2SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rtr_pref, 4893930d6ff2SYOSHIFUJI Hideaki .maxlen = sizeof(int), 4894930d6ff2SYOSHIFUJI Hideaki .mode = 0644, 48956d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 4896930d6ff2SYOSHIFUJI Hideaki }, 489752e16356SYOSHIFUJI Hideaki { 489852e16356SYOSHIFUJI Hideaki .procname = "router_probe_interval", 489952e16356SYOSHIFUJI Hideaki .data = &ipv6_devconf.rtr_probe_interval, 490052e16356SYOSHIFUJI Hideaki .maxlen = sizeof(int), 490152e16356SYOSHIFUJI Hideaki .mode = 0644, 49026d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 490352e16356SYOSHIFUJI Hideaki }, 4904fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 490509c884d4SYOSHIFUJI Hideaki { 490609c884d4SYOSHIFUJI Hideaki .procname = "accept_ra_rt_info_max_plen", 490709c884d4SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rt_info_max_plen, 490809c884d4SYOSHIFUJI Hideaki .maxlen = sizeof(int), 490909c884d4SYOSHIFUJI Hideaki .mode = 0644, 49106d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 491109c884d4SYOSHIFUJI Hideaki }, 491209c884d4SYOSHIFUJI Hideaki #endif 4913930d6ff2SYOSHIFUJI Hideaki #endif 4914c4fd30ebSYOSHIFUJI Hideaki { 4915fbea49e1SYOSHIFUJI Hideaki .procname = "proxy_ndp", 4916fbea49e1SYOSHIFUJI Hideaki .data = &ipv6_devconf.proxy_ndp, 4917fbea49e1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 4918fbea49e1SYOSHIFUJI Hideaki .mode = 0644, 49196d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 4920fbea49e1SYOSHIFUJI Hideaki }, 4921fbea49e1SYOSHIFUJI Hideaki { 49220bcbc926SYOSHIFUJI Hideaki .procname = "accept_source_route", 49230bcbc926SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_source_route, 49240bcbc926SYOSHIFUJI Hideaki .maxlen = sizeof(int), 49250bcbc926SYOSHIFUJI Hideaki .mode = 0644, 49266d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 49270bcbc926SYOSHIFUJI Hideaki }, 492895c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 492995c385b4SNeil Horman { 493095c385b4SNeil Horman .procname = "optimistic_dad", 493195c385b4SNeil Horman .data = &ipv6_devconf.optimistic_dad, 493295c385b4SNeil Horman .maxlen = sizeof(int), 493395c385b4SNeil Horman .mode = 0644, 49346d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 493595c385b4SNeil Horman 493695c385b4SNeil Horman }, 493795c385b4SNeil Horman #endif 49387bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 49397bc570c8SYOSHIFUJI Hideaki { 49407bc570c8SYOSHIFUJI Hideaki .procname = "mc_forwarding", 49417bc570c8SYOSHIFUJI Hideaki .data = &ipv6_devconf.mc_forwarding, 49427bc570c8SYOSHIFUJI Hideaki .maxlen = sizeof(int), 4943a4e6db07SDavid S. Miller .mode = 0444, 49446d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 49457bc570c8SYOSHIFUJI Hideaki }, 49467bc570c8SYOSHIFUJI Hideaki #endif 49470bcbc926SYOSHIFUJI Hideaki { 4948778d80beSYOSHIFUJI Hideaki .procname = "disable_ipv6", 4949778d80beSYOSHIFUJI Hideaki .data = &ipv6_devconf.disable_ipv6, 4950778d80beSYOSHIFUJI Hideaki .maxlen = sizeof(int), 4951778d80beSYOSHIFUJI Hideaki .mode = 0644, 495256d417b1SBrian Haley .proc_handler = addrconf_sysctl_disable, 4953778d80beSYOSHIFUJI Hideaki }, 4954778d80beSYOSHIFUJI Hideaki { 49551b34be74SYOSHIFUJI Hideaki .procname = "accept_dad", 49561b34be74SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_dad, 49571b34be74SYOSHIFUJI Hideaki .maxlen = sizeof(int), 49581b34be74SYOSHIFUJI Hideaki .mode = 0644, 49596d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 49601b34be74SYOSHIFUJI Hideaki }, 49611b34be74SYOSHIFUJI Hideaki { 4962f7734fdfSOctavian Purdila .procname = "force_tllao", 4963f7734fdfSOctavian Purdila .data = &ipv6_devconf.force_tllao, 4964f7734fdfSOctavian Purdila .maxlen = sizeof(int), 4965f7734fdfSOctavian Purdila .mode = 0644, 4966f7734fdfSOctavian Purdila .proc_handler = proc_dointvec 4967f7734fdfSOctavian Purdila }, 4968f7734fdfSOctavian Purdila { 49695cb04436SHannes Frederic Sowa .procname = "ndisc_notify", 49705cb04436SHannes Frederic Sowa .data = &ipv6_devconf.ndisc_notify, 49715cb04436SHannes Frederic Sowa .maxlen = sizeof(int), 49725cb04436SHannes Frederic Sowa .mode = 0644, 49735cb04436SHannes Frederic Sowa .proc_handler = proc_dointvec 49745cb04436SHannes Frederic Sowa }, 49755cb04436SHannes Frederic Sowa { 4976f8572d8fSEric W. Biederman /* sentinel */ 49771da177e4SLinus Torvalds } 49781da177e4SLinus Torvalds }, 49791da177e4SLinus Torvalds }; 49801da177e4SLinus Torvalds 4981bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name, 4982f8572d8fSEric W. Biederman struct inet6_dev *idev, struct ipv6_devconf *p) 49831da177e4SLinus Torvalds { 49841da177e4SLinus Torvalds int i; 49851da177e4SLinus Torvalds struct addrconf_sysctl_table *t; 49866105e293SEric W. Biederman char path[sizeof("net/ipv6/conf/") + IFNAMSIZ]; 49871dab6222SPavel Emelyanov 4988af879cc7SArnaldo Carvalho de Melo t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL); 49891da177e4SLinus Torvalds if (t == NULL) 4990f68635e6SPavel Emelyanov goto out; 4991f68635e6SPavel Emelyanov 49921da177e4SLinus Torvalds for (i = 0; t->addrconf_vars[i].data; i++) { 49931da177e4SLinus Torvalds t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf; 49941da177e4SLinus Torvalds t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */ 4995bff16c2fSPavel Emelyanov t->addrconf_vars[i].extra2 = net; 49961da177e4SLinus Torvalds } 49971da177e4SLinus Torvalds 49986105e293SEric W. Biederman snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name); 49991da177e4SLinus Torvalds 50006105e293SEric W. Biederman t->sysctl_header = register_net_sysctl(net, path, t->addrconf_vars); 50011da177e4SLinus Torvalds if (t->sysctl_header == NULL) 50026105e293SEric W. Biederman goto free; 5003f68635e6SPavel Emelyanov 50041da177e4SLinus Torvalds p->sysctl = t; 500595897312SPavel Emelyanov return 0; 50061da177e4SLinus Torvalds 50071da177e4SLinus Torvalds free: 50081da177e4SLinus Torvalds kfree(t); 5009f68635e6SPavel Emelyanov out: 501095897312SPavel Emelyanov return -ENOBUFS; 50111da177e4SLinus Torvalds } 50121da177e4SLinus Torvalds 5013408c4768SPavel Emelyanov static void __addrconf_sysctl_unregister(struct ipv6_devconf *p) 5014f52295a9SPavel Emelyanov { 5015408c4768SPavel Emelyanov struct addrconf_sysctl_table *t; 5016f52295a9SPavel Emelyanov 5017408c4768SPavel Emelyanov if (p->sysctl == NULL) 5018408c4768SPavel Emelyanov return; 5019408c4768SPavel Emelyanov 5020408c4768SPavel Emelyanov t = p->sysctl; 50211da177e4SLinus Torvalds p->sysctl = NULL; 5022ff538818SLucian Adrian Grijincu unregister_net_sysctl_table(t->sysctl_header); 50231da177e4SLinus Torvalds kfree(t); 50241da177e4SLinus Torvalds } 5025408c4768SPavel Emelyanov 5026408c4768SPavel Emelyanov static void addrconf_sysctl_register(struct inet6_dev *idev) 5027408c4768SPavel Emelyanov { 502854716e3bSEric W. Biederman neigh_sysctl_register(idev->dev, idev->nd_parms, "ipv6", 5029f8572d8fSEric W. Biederman &ndisc_ifinfo_sysctl_change); 5030c346dca1SYOSHIFUJI Hideaki __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name, 5031f8572d8fSEric W. Biederman idev, &idev->cnf); 5032408c4768SPavel Emelyanov } 5033408c4768SPavel Emelyanov 5034408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev) 5035408c4768SPavel Emelyanov { 5036408c4768SPavel Emelyanov __addrconf_sysctl_unregister(&idev->cnf); 5037408c4768SPavel Emelyanov neigh_sysctl_unregister(idev->nd_parms); 50381da177e4SLinus Torvalds } 50391da177e4SLinus Torvalds 50401da177e4SLinus Torvalds 50411da177e4SLinus Torvalds #endif 50421da177e4SLinus Torvalds 50432c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net) 5044e0da5a48SPavel Emelyanov { 5045a79ca223SHong Zhiguo int err = -ENOMEM; 5046e0da5a48SPavel Emelyanov struct ipv6_devconf *all, *dflt; 5047e0da5a48SPavel Emelyanov 5048a79ca223SHong Zhiguo all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL); 5049e0da5a48SPavel Emelyanov if (all == NULL) 5050e0da5a48SPavel Emelyanov goto err_alloc_all; 5051e0da5a48SPavel Emelyanov 5052a79ca223SHong Zhiguo dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); 5053e0da5a48SPavel Emelyanov if (dflt == NULL) 5054e0da5a48SPavel Emelyanov goto err_alloc_dflt; 5055a79ca223SHong Zhiguo 505656d417b1SBrian Haley /* these will be inherited by all namespaces */ 505756d417b1SBrian Haley dflt->autoconf = ipv6_defaults.autoconf; 505856d417b1SBrian Haley dflt->disable_ipv6 = ipv6_defaults.disable_ipv6; 5059e0da5a48SPavel Emelyanov 5060e0da5a48SPavel Emelyanov net->ipv6.devconf_all = all; 5061e0da5a48SPavel Emelyanov net->ipv6.devconf_dflt = dflt; 5062e0da5a48SPavel Emelyanov 5063e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 5064f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "all", NULL, all); 5065e0da5a48SPavel Emelyanov if (err < 0) 5066e0da5a48SPavel Emelyanov goto err_reg_all; 5067e0da5a48SPavel Emelyanov 5068f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "default", NULL, dflt); 5069e0da5a48SPavel Emelyanov if (err < 0) 5070e0da5a48SPavel Emelyanov goto err_reg_dflt; 5071e0da5a48SPavel Emelyanov #endif 5072e0da5a48SPavel Emelyanov return 0; 5073e0da5a48SPavel Emelyanov 5074e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 5075e0da5a48SPavel Emelyanov err_reg_dflt: 5076e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(all); 5077e0da5a48SPavel Emelyanov err_reg_all: 5078e0da5a48SPavel Emelyanov kfree(dflt); 5079e0da5a48SPavel Emelyanov #endif 5080e0da5a48SPavel Emelyanov err_alloc_dflt: 5081e0da5a48SPavel Emelyanov kfree(all); 5082e0da5a48SPavel Emelyanov err_alloc_all: 5083e0da5a48SPavel Emelyanov return err; 5084e0da5a48SPavel Emelyanov } 5085e0da5a48SPavel Emelyanov 50862c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net) 5087e0da5a48SPavel Emelyanov { 5088e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 5089e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(net->ipv6.devconf_dflt); 5090e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(net->ipv6.devconf_all); 5091e0da5a48SPavel Emelyanov #endif 509209ad9bc7SOctavian Purdila if (!net_eq(net, &init_net)) { 5093e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_dflt); 5094e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_all); 5095e0da5a48SPavel Emelyanov } 5096e0da5a48SPavel Emelyanov } 5097e0da5a48SPavel Emelyanov 5098e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = { 5099e0da5a48SPavel Emelyanov .init = addrconf_init_net, 5100e0da5a48SPavel Emelyanov .exit = addrconf_exit_net, 5101e0da5a48SPavel Emelyanov }; 5102e0da5a48SPavel Emelyanov 5103b382b191SThomas Graf static struct rtnl_af_ops inet6_ops = { 5104b382b191SThomas Graf .family = AF_INET6, 5105b382b191SThomas Graf .fill_link_af = inet6_fill_link_af, 5106b382b191SThomas Graf .get_link_af_size = inet6_get_link_af_size, 5107f53adae4SDaniel Borkmann .set_link_af = inet6_set_link_af, 5108b382b191SThomas Graf }; 5109b382b191SThomas Graf 51101da177e4SLinus Torvalds /* 51111da177e4SLinus Torvalds * Init / cleanup code 51121da177e4SLinus Torvalds */ 51131da177e4SLinus Torvalds 51141da177e4SLinus Torvalds int __init addrconf_init(void) 51151da177e4SLinus Torvalds { 5116c2e21293Sstephen hemminger int i, err; 51172a8cc6c8SYOSHIFUJI Hideaki 5118e21e8467SStephen Hemminger err = ipv6_addr_label_init(); 5119e21e8467SStephen Hemminger if (err < 0) { 5120f3213831SJoe Perches pr_crit("%s: cannot initialize default policy table: %d\n", 5121f3213831SJoe Perches __func__, err); 51222cc6d2bfSNeil Horman goto out; 51232a8cc6c8SYOSHIFUJI Hideaki } 51241da177e4SLinus Torvalds 51252cc6d2bfSNeil Horman err = register_pernet_subsys(&addrconf_ops); 51262cc6d2bfSNeil Horman if (err < 0) 51272cc6d2bfSNeil Horman goto out_addrlabel; 5128e0da5a48SPavel Emelyanov 51291da177e4SLinus Torvalds /* The addrconf netdev notifier requires that loopback_dev 51301da177e4SLinus Torvalds * has it's ipv6 private information allocated and setup 51311da177e4SLinus Torvalds * before it can bring up and give link-local addresses 51321da177e4SLinus Torvalds * to other devices which are up. 51331da177e4SLinus Torvalds * 51341da177e4SLinus Torvalds * Unfortunately, loopback_dev is not necessarily the first 51351da177e4SLinus Torvalds * entry in the global dev_base list of net devices. In fact, 51361da177e4SLinus Torvalds * it is likely to be the very last entry on that list. 51371da177e4SLinus Torvalds * So this causes the notifier registry below to try and 51381da177e4SLinus Torvalds * give link-local addresses to all devices besides loopback_dev 51391da177e4SLinus Torvalds * first, then loopback_dev, which cases all the non-loopback_dev 51401da177e4SLinus Torvalds * devices to fail to get a link-local address. 51411da177e4SLinus Torvalds * 51421da177e4SLinus Torvalds * So, as a temporary fix, allocate the ipv6 structure for 51431da177e4SLinus Torvalds * loopback_dev first by hand. 51441da177e4SLinus Torvalds * Longer term, all of the dependencies ipv6 has upon the loopback 51451da177e4SLinus Torvalds * device and it being up should be removed. 51461da177e4SLinus Torvalds */ 51471da177e4SLinus Torvalds rtnl_lock(); 51482774c7abSEric W. Biederman if (!ipv6_add_dev(init_net.loopback_dev)) 51491da177e4SLinus Torvalds err = -ENOMEM; 51501da177e4SLinus Torvalds rtnl_unlock(); 51511da177e4SLinus Torvalds if (err) 5152e0da5a48SPavel Emelyanov goto errlo; 51531da177e4SLinus Torvalds 5154c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 5155c2e21293Sstephen hemminger INIT_HLIST_HEAD(&inet6_addr_lst[i]); 5156c2e21293Sstephen hemminger 51571da177e4SLinus Torvalds register_netdevice_notifier(&ipv6_dev_notf); 51581da177e4SLinus Torvalds 51591da177e4SLinus Torvalds addrconf_verify(0); 5160c127ea2cSThomas Graf 5161b382b191SThomas Graf err = rtnl_af_register(&inet6_ops); 5162b382b191SThomas Graf if (err < 0) 5163b382b191SThomas Graf goto errout_af; 5164b382b191SThomas Graf 5165c7ac8679SGreg Rose err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo, 5166c7ac8679SGreg Rose NULL); 5167c127ea2cSThomas Graf if (err < 0) 5168c127ea2cSThomas Graf goto errout; 5169c127ea2cSThomas Graf 5170c127ea2cSThomas Graf /* Only the first call to __rtnl_register can fail */ 5171c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL); 5172c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL); 5173c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, 5174c7ac8679SGreg Rose inet6_dump_ifaddr, NULL); 5175c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, 5176c7ac8679SGreg Rose inet6_dump_ifmcaddr, NULL); 5177c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, 5178c7ac8679SGreg Rose inet6_dump_ifacaddr, NULL); 517976f8f6cbSNicolas Dichtel __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf, 51807a674200SNicolas Dichtel inet6_netconf_dump_devconf, NULL); 5181c127ea2cSThomas Graf 51822a8cc6c8SYOSHIFUJI Hideaki ipv6_addr_label_rtnl_register(); 51832a8cc6c8SYOSHIFUJI Hideaki 51841da177e4SLinus Torvalds return 0; 5185c127ea2cSThomas Graf errout: 5186b382b191SThomas Graf rtnl_af_unregister(&inet6_ops); 5187b382b191SThomas Graf errout_af: 5188c127ea2cSThomas Graf unregister_netdevice_notifier(&ipv6_dev_notf); 5189e0da5a48SPavel Emelyanov errlo: 5190e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 51912cc6d2bfSNeil Horman out_addrlabel: 51922cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 51932cc6d2bfSNeil Horman out: 5194c127ea2cSThomas Graf return err; 51951da177e4SLinus Torvalds } 51961da177e4SLinus Torvalds 519709f7709fSDaniel Lezcano void addrconf_cleanup(void) 51981da177e4SLinus Torvalds { 5199176c39afSDaniel Lezcano struct net_device *dev; 52001da177e4SLinus Torvalds int i; 52011da177e4SLinus Torvalds 52021da177e4SLinus Torvalds unregister_netdevice_notifier(&ipv6_dev_notf); 5203e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 52042cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 52051da177e4SLinus Torvalds 52061da177e4SLinus Torvalds rtnl_lock(); 52071da177e4SLinus Torvalds 5208b382b191SThomas Graf __rtnl_af_unregister(&inet6_ops); 5209b382b191SThomas Graf 5210176c39afSDaniel Lezcano /* clean dev list */ 5211176c39afSDaniel Lezcano for_each_netdev(&init_net, dev) { 5212176c39afSDaniel Lezcano if (__in6_dev_get(dev) == NULL) 5213176c39afSDaniel Lezcano continue; 5214176c39afSDaniel Lezcano addrconf_ifdown(dev, 1); 5215176c39afSDaniel Lezcano } 5216176c39afSDaniel Lezcano addrconf_ifdown(init_net.loopback_dev, 2); 5217176c39afSDaniel Lezcano 52181da177e4SLinus Torvalds /* 52191da177e4SLinus Torvalds * Check hash table. 52201da177e4SLinus Torvalds */ 52215c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 5222c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 5223c2e21293Sstephen hemminger WARN_ON(!hlist_empty(&inet6_addr_lst[i])); 52245c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 52251da177e4SLinus Torvalds 52261da177e4SLinus Torvalds del_timer(&addr_chk_timer); 52271da177e4SLinus Torvalds rtnl_unlock(); 52281da177e4SLinus Torvalds } 5229