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; 3280*b173ee48SHannes Frederic Sowa bool send_rs, send_mld; 3281b7b1bfceSHannes Frederic Sowa 3282b7b1bfceSHannes Frederic Sowa addrconf_del_dad_timer(ifp); 32831da177e4SLinus Torvalds 32841da177e4SLinus Torvalds /* 32851da177e4SLinus Torvalds * Configure the address for reception. Now it is valid. 32861da177e4SLinus Torvalds */ 32871da177e4SLinus Torvalds 32881da177e4SLinus Torvalds ipv6_ifa_notify(RTM_NEWADDR, ifp); 32891da177e4SLinus Torvalds 3290026359bcSTore Anderson /* If added prefix is link local and we are prepared to process 3291026359bcSTore Anderson router advertisements, start sending router solicitations. 32921da177e4SLinus Torvalds */ 32931da177e4SLinus Torvalds 32941ec047ebSHannes Frederic Sowa read_lock_bh(&ifp->idev->lock); 32951ec047ebSHannes Frederic Sowa spin_lock(&ifp->lock); 3296*b173ee48SHannes Frederic Sowa send_mld = ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL && 32971ec047ebSHannes Frederic Sowa ifp->idev->valid_ll_addr_cnt == 1; 3298*b173ee48SHannes Frederic Sowa send_rs = send_mld && 3299*b173ee48SHannes Frederic Sowa ipv6_accept_ra(ifp->idev) && 3300*b173ee48SHannes Frederic Sowa ifp->idev->cnf.rtr_solicits > 0 && 3301*b173ee48SHannes Frederic Sowa (dev->flags&IFF_LOOPBACK) == 0; 33021ec047ebSHannes Frederic Sowa spin_unlock(&ifp->lock); 33031ec047ebSHannes Frederic Sowa read_unlock_bh(&ifp->idev->lock); 33041ec047ebSHannes Frederic Sowa 3305*b173ee48SHannes Frederic Sowa /* While dad is in progress mld report's source address is in6_addrany. 3306*b173ee48SHannes Frederic Sowa * Resend with proper ll now. 3307*b173ee48SHannes Frederic Sowa */ 3308*b173ee48SHannes Frederic Sowa if (send_mld) 3309*b173ee48SHannes Frederic Sowa ipv6_mc_dad_complete(ifp->idev); 3310*b173ee48SHannes Frederic Sowa 33111ec047ebSHannes Frederic Sowa if (send_rs) { 33121da177e4SLinus Torvalds /* 33131da177e4SLinus Torvalds * If a host as already performed a random delay 33141da177e4SLinus Torvalds * [...] as part of DAD [...] there is no need 33151da177e4SLinus Torvalds * to delay again before sending the first RS 33161da177e4SLinus Torvalds */ 33171ec047ebSHannes Frederic Sowa if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE)) 3318b7b1bfceSHannes Frederic Sowa return; 33191ec047ebSHannes Frederic Sowa ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters); 33201da177e4SLinus Torvalds 3321b7b1bfceSHannes Frederic Sowa write_lock_bh(&ifp->idev->lock); 3322b7b1bfceSHannes Frederic Sowa spin_lock(&ifp->lock); 3323b7b1bfceSHannes Frederic Sowa ifp->idev->rs_probes = 1; 33241da177e4SLinus Torvalds ifp->idev->if_flags |= IF_RS_SENT; 3325b7b1bfceSHannes Frederic Sowa addrconf_mod_rs_timer(ifp->idev, 3326b7b1bfceSHannes Frederic Sowa ifp->idev->cnf.rtr_solicit_interval); 3327b7b1bfceSHannes Frederic Sowa spin_unlock(&ifp->lock); 3328b7b1bfceSHannes Frederic Sowa write_unlock_bh(&ifp->idev->lock); 33291da177e4SLinus Torvalds } 33301da177e4SLinus Torvalds } 33311da177e4SLinus Torvalds 3332e21e8467SStephen Hemminger static void addrconf_dad_run(struct inet6_dev *idev) 3333e21e8467SStephen Hemminger { 3334c5e33bddSYOSHIFUJI Hideaki struct inet6_ifaddr *ifp; 3335c5e33bddSYOSHIFUJI Hideaki 3336c5e33bddSYOSHIFUJI Hideaki read_lock_bh(&idev->lock); 3337502a2ffdSstephen hemminger list_for_each_entry(ifp, &idev->addr_list, if_list) { 333821809fafSstephen hemminger spin_lock(&ifp->lock); 3339f2344a13SHerbert Xu if (ifp->flags & IFA_F_TENTATIVE && 3340f2344a13SHerbert Xu ifp->state == INET6_IFADDR_STATE_DAD) 3341c5e33bddSYOSHIFUJI Hideaki addrconf_dad_kick(ifp); 3342f2344a13SHerbert Xu spin_unlock(&ifp->lock); 3343c5e33bddSYOSHIFUJI Hideaki } 3344c5e33bddSYOSHIFUJI Hideaki read_unlock_bh(&idev->lock); 3345c5e33bddSYOSHIFUJI Hideaki } 3346c5e33bddSYOSHIFUJI Hideaki 33471da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 33481da177e4SLinus Torvalds struct if6_iter_state { 33493c40090aSDaniel Lezcano struct seq_net_private p; 33501da177e4SLinus Torvalds int bucket; 33511d578303SMihai Maruseac int offset; 33521da177e4SLinus Torvalds }; 33531da177e4SLinus Torvalds 33541d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos) 33551da177e4SLinus Torvalds { 33561da177e4SLinus Torvalds struct inet6_ifaddr *ifa = NULL; 33571da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 33581218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 33591d578303SMihai Maruseac int p = 0; 33601da177e4SLinus Torvalds 33611d578303SMihai Maruseac /* initial bucket if pos is 0 */ 33621d578303SMihai Maruseac if (pos == 0) { 33631d578303SMihai Maruseac state->bucket = 0; 33641d578303SMihai Maruseac state->offset = 0; 33651d578303SMihai Maruseac } 33661d578303SMihai Maruseac 33671d578303SMihai Maruseac for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) { 3368b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket], 33691d578303SMihai Maruseac addr_lst) { 33709f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 33719f0d3c27SEric Dumazet continue; 33721d578303SMihai Maruseac /* sync with offset */ 33731d578303SMihai Maruseac if (p < state->offset) { 33741d578303SMihai Maruseac p++; 33751d578303SMihai Maruseac continue; 33761d578303SMihai Maruseac } 33771d578303SMihai Maruseac state->offset++; 33781da177e4SLinus Torvalds return ifa; 33791da177e4SLinus Torvalds } 33801d578303SMihai Maruseac 33811d578303SMihai Maruseac /* prepare for next bucket */ 33821d578303SMihai Maruseac state->offset = 0; 33831d578303SMihai Maruseac p = 0; 33841d578303SMihai Maruseac } 3385c2e21293Sstephen hemminger return NULL; 3386c2e21293Sstephen hemminger } 33871da177e4SLinus Torvalds 3388c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, 3389c2e21293Sstephen hemminger struct inet6_ifaddr *ifa) 33901da177e4SLinus Torvalds { 33911da177e4SLinus Torvalds struct if6_iter_state *state = seq->private; 33921218854aSYOSHIFUJI Hideaki struct net *net = seq_file_net(seq); 33931da177e4SLinus Torvalds 3394b67bfe0dSSasha Levin hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) { 33959f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 33969f0d3c27SEric Dumazet continue; 33971d578303SMihai Maruseac state->offset++; 33981da177e4SLinus Torvalds return ifa; 33991d578303SMihai Maruseac } 34001da177e4SLinus Torvalds 3401c2e21293Sstephen hemminger while (++state->bucket < IN6_ADDR_HSIZE) { 34021d578303SMihai Maruseac state->offset = 0; 3403b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifa, 3404c2e21293Sstephen hemminger &inet6_addr_lst[state->bucket], addr_lst) { 34059f0d3c27SEric Dumazet if (!net_eq(dev_net(ifa->idev->dev), net)) 34069f0d3c27SEric Dumazet continue; 34071d578303SMihai Maruseac state->offset++; 3408c2e21293Sstephen hemminger return ifa; 3409c2e21293Sstephen hemminger } 3410c2e21293Sstephen hemminger } 3411c2e21293Sstephen hemminger 3412c2e21293Sstephen hemminger return NULL; 3413c2e21293Sstephen hemminger } 3414c2e21293Sstephen hemminger 34151da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos) 34164f70eccaSEric Dumazet __acquires(rcu_bh) 34171da177e4SLinus Torvalds { 34185c578aedSstephen hemminger rcu_read_lock_bh(); 34191d578303SMihai Maruseac return if6_get_first(seq, *pos); 34201da177e4SLinus Torvalds } 34211da177e4SLinus Torvalds 34221da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos) 34231da177e4SLinus Torvalds { 34241da177e4SLinus Torvalds struct inet6_ifaddr *ifa; 34251da177e4SLinus Torvalds 34261da177e4SLinus Torvalds ifa = if6_get_next(seq, v); 34271da177e4SLinus Torvalds ++*pos; 34281da177e4SLinus Torvalds return ifa; 34291da177e4SLinus Torvalds } 34301da177e4SLinus Torvalds 34311da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v) 34324f70eccaSEric Dumazet __releases(rcu_bh) 34331da177e4SLinus Torvalds { 34345c578aedSstephen hemminger rcu_read_unlock_bh(); 34351da177e4SLinus Torvalds } 34361da177e4SLinus Torvalds 34371da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v) 34381da177e4SLinus Torvalds { 34391da177e4SLinus Torvalds struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v; 34404b7a4274SHarvey Harrison seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n", 3441b071195dSHarvey Harrison &ifp->addr, 34421da177e4SLinus Torvalds ifp->idev->dev->ifindex, 34431da177e4SLinus Torvalds ifp->prefix_len, 34441da177e4SLinus Torvalds ifp->scope, 34451da177e4SLinus Torvalds ifp->flags, 34461da177e4SLinus Torvalds ifp->idev->dev->name); 34471da177e4SLinus Torvalds return 0; 34481da177e4SLinus Torvalds } 34491da177e4SLinus Torvalds 345056b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = { 34511da177e4SLinus Torvalds .start = if6_seq_start, 34521da177e4SLinus Torvalds .next = if6_seq_next, 34531da177e4SLinus Torvalds .show = if6_seq_show, 34541da177e4SLinus Torvalds .stop = if6_seq_stop, 34551da177e4SLinus Torvalds }; 34561da177e4SLinus Torvalds 34571da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file) 34581da177e4SLinus Torvalds { 34593c40090aSDaniel Lezcano return seq_open_net(inode, file, &if6_seq_ops, 3460cf7732e4SPavel Emelyanov sizeof(struct if6_iter_state)); 34611da177e4SLinus Torvalds } 34621da177e4SLinus Torvalds 34639a32144eSArjan van de Ven static const struct file_operations if6_fops = { 34641da177e4SLinus Torvalds .owner = THIS_MODULE, 34651da177e4SLinus Torvalds .open = if6_seq_open, 34661da177e4SLinus Torvalds .read = seq_read, 34671da177e4SLinus Torvalds .llseek = seq_lseek, 34683c40090aSDaniel Lezcano .release = seq_release_net, 34691da177e4SLinus Torvalds }; 34701da177e4SLinus Torvalds 34712c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net) 34721da177e4SLinus Torvalds { 3473d4beaa66SGao feng if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops)) 34741da177e4SLinus Torvalds return -ENOMEM; 34751da177e4SLinus Torvalds return 0; 34761da177e4SLinus Torvalds } 34771da177e4SLinus Torvalds 34782c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net) 34793c40090aSDaniel Lezcano { 3480ece31ffdSGao feng remove_proc_entry("if_inet6", net->proc_net); 34813c40090aSDaniel Lezcano } 34823c40090aSDaniel Lezcano 34833c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = { 34843c40090aSDaniel Lezcano .init = if6_proc_net_init, 34853c40090aSDaniel Lezcano .exit = if6_proc_net_exit, 34863c40090aSDaniel Lezcano }; 34873c40090aSDaniel Lezcano 34883c40090aSDaniel Lezcano int __init if6_proc_init(void) 34893c40090aSDaniel Lezcano { 34903c40090aSDaniel Lezcano return register_pernet_subsys(&if6_proc_net_ops); 34913c40090aSDaniel Lezcano } 34923c40090aSDaniel Lezcano 34931da177e4SLinus Torvalds void if6_proc_exit(void) 34941da177e4SLinus Torvalds { 34953c40090aSDaniel Lezcano unregister_pernet_subsys(&if6_proc_net_ops); 34961da177e4SLinus Torvalds } 34971da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 34981da177e4SLinus Torvalds 349907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6) 35003b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */ 3501b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr) 35023b9f9a1cSNoriaki TAKAMIYA { 35033b9f9a1cSNoriaki TAKAMIYA int ret = 0; 3504c2e21293Sstephen hemminger struct inet6_ifaddr *ifp = NULL; 3505ddbe5032SEric Dumazet unsigned int hash = inet6_addr_hash(addr); 3506c2e21293Sstephen hemminger 35075c578aedSstephen hemminger rcu_read_lock_bh(); 3508b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) { 3509878628fbSYOSHIFUJI Hideaki if (!net_eq(dev_net(ifp->idev->dev), net)) 3510389f6612SDaniel Lezcano continue; 3511caad295fSYOSHIFUJI Hideaki if (ipv6_addr_equal(&ifp->addr, addr) && 35123b9f9a1cSNoriaki TAKAMIYA (ifp->flags & IFA_F_HOMEADDRESS)) { 35133b9f9a1cSNoriaki TAKAMIYA ret = 1; 35143b9f9a1cSNoriaki TAKAMIYA break; 35153b9f9a1cSNoriaki TAKAMIYA } 35163b9f9a1cSNoriaki TAKAMIYA } 35175c578aedSstephen hemminger rcu_read_unlock_bh(); 35183b9f9a1cSNoriaki TAKAMIYA return ret; 35193b9f9a1cSNoriaki TAKAMIYA } 35203b9f9a1cSNoriaki TAKAMIYA #endif 35213b9f9a1cSNoriaki TAKAMIYA 35221da177e4SLinus Torvalds /* 35231da177e4SLinus Torvalds * Periodic address status verification 35241da177e4SLinus Torvalds */ 35251da177e4SLinus Torvalds 35261da177e4SLinus Torvalds static void addrconf_verify(unsigned long foo) 35271da177e4SLinus Torvalds { 3528b2db7564SYOSHIFUJI Hideaki unsigned long now, next, next_sec, next_sched; 35291da177e4SLinus Torvalds struct inet6_ifaddr *ifp; 35301da177e4SLinus Torvalds int i; 35311da177e4SLinus Torvalds 35325c578aedSstephen hemminger rcu_read_lock_bh(); 35335c578aedSstephen hemminger spin_lock(&addrconf_verify_lock); 35341da177e4SLinus Torvalds now = jiffies; 3535b2db7564SYOSHIFUJI Hideaki next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY); 35361da177e4SLinus Torvalds 35371da177e4SLinus Torvalds del_timer(&addr_chk_timer); 35381da177e4SLinus Torvalds 35391da177e4SLinus Torvalds for (i = 0; i < IN6_ADDR_HSIZE; i++) { 35401da177e4SLinus Torvalds restart: 3541b67bfe0dSSasha Levin hlist_for_each_entry_rcu_bh(ifp, 35425c578aedSstephen hemminger &inet6_addr_lst[i], addr_lst) { 35431da177e4SLinus Torvalds unsigned long age; 35441da177e4SLinus Torvalds 35451da177e4SLinus Torvalds if (ifp->flags & IFA_F_PERMANENT) 35461da177e4SLinus Torvalds continue; 35471da177e4SLinus Torvalds 35481da177e4SLinus Torvalds spin_lock(&ifp->lock); 3549b2db7564SYOSHIFUJI Hideaki /* We try to batch several events at once. */ 3550b2db7564SYOSHIFUJI Hideaki age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 35511da177e4SLinus Torvalds 35528f27ebb9SYOSHIFUJI Hideaki if (ifp->valid_lft != INFINITY_LIFE_TIME && 35538f27ebb9SYOSHIFUJI Hideaki age >= ifp->valid_lft) { 35541da177e4SLinus Torvalds spin_unlock(&ifp->lock); 35551da177e4SLinus Torvalds in6_ifa_hold(ifp); 35561da177e4SLinus Torvalds ipv6_del_addr(ifp); 35571da177e4SLinus Torvalds goto restart; 35588f27ebb9SYOSHIFUJI Hideaki } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) { 35598f27ebb9SYOSHIFUJI Hideaki spin_unlock(&ifp->lock); 35608f27ebb9SYOSHIFUJI Hideaki continue; 35611da177e4SLinus Torvalds } else if (age >= ifp->prefered_lft) { 3562a1ed0526SBrian Haley /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */ 35631da177e4SLinus Torvalds int deprecate = 0; 35641da177e4SLinus Torvalds 35651da177e4SLinus Torvalds if (!(ifp->flags&IFA_F_DEPRECATED)) { 35661da177e4SLinus Torvalds deprecate = 1; 35671da177e4SLinus Torvalds ifp->flags |= IFA_F_DEPRECATED; 35681da177e4SLinus Torvalds } 35691da177e4SLinus Torvalds 35701da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)) 35711da177e4SLinus Torvalds next = ifp->tstamp + ifp->valid_lft * HZ; 35721da177e4SLinus Torvalds 35731da177e4SLinus Torvalds spin_unlock(&ifp->lock); 35741da177e4SLinus Torvalds 35751da177e4SLinus Torvalds if (deprecate) { 35761da177e4SLinus Torvalds in6_ifa_hold(ifp); 35771da177e4SLinus Torvalds 35781da177e4SLinus Torvalds ipv6_ifa_notify(0, ifp); 35791da177e4SLinus Torvalds in6_ifa_put(ifp); 35801da177e4SLinus Torvalds goto restart; 35811da177e4SLinus Torvalds } 35821da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 35831da177e4SLinus Torvalds } else if ((ifp->flags&IFA_F_TEMPORARY) && 35841da177e4SLinus Torvalds !(ifp->flags&IFA_F_TENTATIVE)) { 358588949cf4Sstephen hemminger unsigned long regen_advance = ifp->idev->cnf.regen_max_retry * 358688949cf4Sstephen hemminger ifp->idev->cnf.dad_transmits * 358788949cf4Sstephen hemminger ifp->idev->nd_parms->retrans_time / HZ; 358888949cf4Sstephen hemminger 35891da177e4SLinus Torvalds if (age >= ifp->prefered_lft - regen_advance) { 35901da177e4SLinus Torvalds struct inet6_ifaddr *ifpub = ifp->ifpub; 35911da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 35921da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 35931da177e4SLinus Torvalds if (!ifp->regen_count && ifpub) { 35941da177e4SLinus Torvalds ifp->regen_count++; 35951da177e4SLinus Torvalds in6_ifa_hold(ifp); 35961da177e4SLinus Torvalds in6_ifa_hold(ifpub); 35971da177e4SLinus Torvalds spin_unlock(&ifp->lock); 35985c578aedSstephen hemminger 3599291d809bSHiroyuki YAMAMORI spin_lock(&ifpub->lock); 3600291d809bSHiroyuki YAMAMORI ifpub->regen_count = 0; 3601291d809bSHiroyuki YAMAMORI spin_unlock(&ifpub->lock); 36021da177e4SLinus Torvalds ipv6_create_tempaddr(ifpub, ifp); 36031da177e4SLinus Torvalds in6_ifa_put(ifpub); 36041da177e4SLinus Torvalds in6_ifa_put(ifp); 36051da177e4SLinus Torvalds goto restart; 36061da177e4SLinus Torvalds } 36071da177e4SLinus Torvalds } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) 36081da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; 36091da177e4SLinus Torvalds spin_unlock(&ifp->lock); 36101da177e4SLinus Torvalds #endif 36111da177e4SLinus Torvalds } else { 36121da177e4SLinus Torvalds /* ifp->prefered_lft <= ifp->valid_lft */ 36131da177e4SLinus Torvalds if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 36141da177e4SLinus Torvalds next = ifp->tstamp + ifp->prefered_lft * HZ; 36151da177e4SLinus Torvalds spin_unlock(&ifp->lock); 36161da177e4SLinus Torvalds } 36171da177e4SLinus Torvalds } 36181da177e4SLinus Torvalds } 36191da177e4SLinus Torvalds 3620b2db7564SYOSHIFUJI Hideaki next_sec = round_jiffies_up(next); 3621b2db7564SYOSHIFUJI Hideaki next_sched = next; 3622b2db7564SYOSHIFUJI Hideaki 3623b2db7564SYOSHIFUJI Hideaki /* If rounded timeout is accurate enough, accept it. */ 3624b2db7564SYOSHIFUJI Hideaki if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ)) 3625b2db7564SYOSHIFUJI Hideaki next_sched = next_sec; 3626b2db7564SYOSHIFUJI Hideaki 3627b2db7564SYOSHIFUJI Hideaki /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */ 3628b2db7564SYOSHIFUJI Hideaki if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX)) 3629b2db7564SYOSHIFUJI Hideaki next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX; 3630b2db7564SYOSHIFUJI Hideaki 3631b2db7564SYOSHIFUJI Hideaki ADBG((KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n", 3632b2db7564SYOSHIFUJI Hideaki now, next, next_sec, next_sched)); 3633b2db7564SYOSHIFUJI Hideaki 3634b2db7564SYOSHIFUJI Hideaki addr_chk_timer.expires = next_sched; 36351da177e4SLinus Torvalds add_timer(&addr_chk_timer); 36365c578aedSstephen hemminger spin_unlock(&addrconf_verify_lock); 36375c578aedSstephen hemminger rcu_read_unlock_bh(); 36381da177e4SLinus Torvalds } 36391da177e4SLinus Torvalds 3640caeaba79SNicolas Dichtel static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local, 3641caeaba79SNicolas Dichtel struct in6_addr **peer_pfx) 3642461d8837SThomas Graf { 3643461d8837SThomas Graf struct in6_addr *pfx = NULL; 3644461d8837SThomas Graf 3645caeaba79SNicolas Dichtel *peer_pfx = NULL; 3646caeaba79SNicolas Dichtel 3647461d8837SThomas Graf if (addr) 3648461d8837SThomas Graf pfx = nla_data(addr); 3649461d8837SThomas Graf 3650461d8837SThomas Graf if (local) { 3651461d8837SThomas Graf if (pfx && nla_memcmp(local, pfx, sizeof(*pfx))) 3652caeaba79SNicolas Dichtel *peer_pfx = pfx; 3653461d8837SThomas Graf pfx = nla_data(local); 3654461d8837SThomas Graf } 3655461d8837SThomas Graf 3656461d8837SThomas Graf return pfx; 3657461d8837SThomas Graf } 3658461d8837SThomas Graf 3659ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = { 3660461d8837SThomas Graf [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) }, 3661461d8837SThomas Graf [IFA_LOCAL] = { .len = sizeof(struct in6_addr) }, 3662461d8837SThomas Graf [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, 3663461d8837SThomas Graf }; 3664461d8837SThomas Graf 36651da177e4SLinus Torvalds static int 3666661d2967SThomas Graf inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh) 36671da177e4SLinus Torvalds { 36683b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 3669b933f716SThomas Graf struct ifaddrmsg *ifm; 3670b933f716SThomas Graf struct nlattr *tb[IFA_MAX+1]; 3671caeaba79SNicolas Dichtel struct in6_addr *pfx, *peer_pfx; 3672b933f716SThomas Graf int err; 36731da177e4SLinus Torvalds 3674b933f716SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 3675b933f716SThomas Graf if (err < 0) 3676b933f716SThomas Graf return err; 3677b933f716SThomas Graf 3678b933f716SThomas Graf ifm = nlmsg_data(nlh); 3679caeaba79SNicolas Dichtel pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 36801da177e4SLinus Torvalds if (pfx == NULL) 36811da177e4SLinus Torvalds return -EINVAL; 36821da177e4SLinus Torvalds 3683af284937SDaniel Lezcano return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen); 36841da177e4SLinus Torvalds } 36851da177e4SLinus Torvalds 368655ebaef1SNoriaki TAKAMIYA static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags, 368755ebaef1SNoriaki TAKAMIYA u32 prefered_lft, u32 valid_lft) 3688081bba5bSNoriaki TAKAMIYA { 36896f704992SYOSHIFUJI Hideaki u32 flags; 36906f704992SYOSHIFUJI Hideaki clock_t expires; 36914bed72e4SYOSHIFUJI Hideaki unsigned long timeout; 369246d48046SYOSHIFUJI Hideaki 3693081bba5bSNoriaki TAKAMIYA if (!valid_lft || (prefered_lft > valid_lft)) 3694081bba5bSNoriaki TAKAMIYA return -EINVAL; 3695081bba5bSNoriaki TAKAMIYA 36964bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(valid_lft, HZ); 36974bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 36984bed72e4SYOSHIFUJI Hideaki expires = jiffies_to_clock_t(timeout * HZ); 36994bed72e4SYOSHIFUJI Hideaki valid_lft = timeout; 37006f704992SYOSHIFUJI Hideaki flags = RTF_EXPIRES; 37014bed72e4SYOSHIFUJI Hideaki } else { 37024bed72e4SYOSHIFUJI Hideaki expires = 0; 37034bed72e4SYOSHIFUJI Hideaki flags = 0; 37044bed72e4SYOSHIFUJI Hideaki ifa_flags |= IFA_F_PERMANENT; 37056f704992SYOSHIFUJI Hideaki } 3706081bba5bSNoriaki TAKAMIYA 37074bed72e4SYOSHIFUJI Hideaki timeout = addrconf_timeout_fixup(prefered_lft, HZ); 37084bed72e4SYOSHIFUJI Hideaki if (addrconf_finite_timeout(timeout)) { 37094bed72e4SYOSHIFUJI Hideaki if (timeout == 0) 371055ebaef1SNoriaki TAKAMIYA ifa_flags |= IFA_F_DEPRECATED; 37114bed72e4SYOSHIFUJI Hideaki prefered_lft = timeout; 37124bed72e4SYOSHIFUJI Hideaki } 3713081bba5bSNoriaki TAKAMIYA 3714081bba5bSNoriaki TAKAMIYA spin_lock_bh(&ifp->lock); 37153b9f9a1cSNoriaki TAKAMIYA ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags; 3716081bba5bSNoriaki TAKAMIYA ifp->tstamp = jiffies; 3717081bba5bSNoriaki TAKAMIYA ifp->valid_lft = valid_lft; 3718081bba5bSNoriaki TAKAMIYA ifp->prefered_lft = prefered_lft; 3719081bba5bSNoriaki TAKAMIYA 3720081bba5bSNoriaki TAKAMIYA spin_unlock_bh(&ifp->lock); 3721081bba5bSNoriaki TAKAMIYA if (!(ifp->flags&IFA_F_TENTATIVE)) 3722081bba5bSNoriaki TAKAMIYA ipv6_ifa_notify(0, ifp); 3723081bba5bSNoriaki TAKAMIYA 372446d48046SYOSHIFUJI Hideaki addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev, 37256f704992SYOSHIFUJI Hideaki expires, flags); 3726081bba5bSNoriaki TAKAMIYA addrconf_verify(0); 3727081bba5bSNoriaki TAKAMIYA 3728081bba5bSNoriaki TAKAMIYA return 0; 3729081bba5bSNoriaki TAKAMIYA } 3730081bba5bSNoriaki TAKAMIYA 3731081bba5bSNoriaki TAKAMIYA static int 3732661d2967SThomas Graf inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh) 37331da177e4SLinus Torvalds { 37343b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 3735461d8837SThomas Graf struct ifaddrmsg *ifm; 3736461d8837SThomas Graf struct nlattr *tb[IFA_MAX+1]; 3737caeaba79SNicolas Dichtel struct in6_addr *pfx, *peer_pfx; 37387198f8ceSThomas Graf struct inet6_ifaddr *ifa; 37397198f8ceSThomas Graf struct net_device *dev; 374055ebaef1SNoriaki TAKAMIYA u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME; 374155ebaef1SNoriaki TAKAMIYA u8 ifa_flags; 3742461d8837SThomas Graf int err; 37431da177e4SLinus Torvalds 3744461d8837SThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 3745461d8837SThomas Graf if (err < 0) 3746461d8837SThomas Graf return err; 3747461d8837SThomas Graf 3748461d8837SThomas Graf ifm = nlmsg_data(nlh); 3749caeaba79SNicolas Dichtel pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 37501da177e4SLinus Torvalds if (pfx == NULL) 37511da177e4SLinus Torvalds return -EINVAL; 37521da177e4SLinus Torvalds 3753461d8837SThomas Graf if (tb[IFA_CACHEINFO]) { 37540778769dSNoriaki TAKAMIYA struct ifa_cacheinfo *ci; 3755461d8837SThomas Graf 3756461d8837SThomas Graf ci = nla_data(tb[IFA_CACHEINFO]); 37570778769dSNoriaki TAKAMIYA valid_lft = ci->ifa_valid; 3758461d8837SThomas Graf preferred_lft = ci->ifa_prefered; 3759461d8837SThomas Graf } else { 3760461d8837SThomas Graf preferred_lft = INFINITY_LIFE_TIME; 3761461d8837SThomas Graf valid_lft = INFINITY_LIFE_TIME; 37620778769dSNoriaki TAKAMIYA } 37630778769dSNoriaki TAKAMIYA 3764af284937SDaniel Lezcano dev = __dev_get_by_index(net, ifm->ifa_index); 37657198f8ceSThomas Graf if (dev == NULL) 37667198f8ceSThomas Graf return -ENODEV; 3767081bba5bSNoriaki TAKAMIYA 376855ebaef1SNoriaki TAKAMIYA /* We ignore other flags so far. */ 37693b9f9a1cSNoriaki TAKAMIYA ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS); 377055ebaef1SNoriaki TAKAMIYA 37711cab3da6SDaniel Lezcano ifa = ipv6_get_ifaddr(net, pfx, dev, 1); 37727198f8ceSThomas Graf if (ifa == NULL) { 37737198f8ceSThomas Graf /* 37747198f8ceSThomas Graf * It would be best to check for !NLM_F_CREATE here but 37757198f8ceSThomas Graf * userspace alreay relies on not having to provide this. 37767198f8ceSThomas Graf */ 3777caeaba79SNicolas Dichtel return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx, 3778af284937SDaniel Lezcano ifm->ifa_prefixlen, ifa_flags, 3779af284937SDaniel Lezcano preferred_lft, valid_lft); 37801da177e4SLinus Torvalds } 37811da177e4SLinus Torvalds 37827198f8ceSThomas Graf if (nlh->nlmsg_flags & NLM_F_EXCL || 37837198f8ceSThomas Graf !(nlh->nlmsg_flags & NLM_F_REPLACE)) 37847198f8ceSThomas Graf err = -EEXIST; 37857198f8ceSThomas Graf else 378655ebaef1SNoriaki TAKAMIYA err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft); 37877198f8ceSThomas Graf 37887198f8ceSThomas Graf in6_ifa_put(ifa); 37897198f8ceSThomas Graf 37907198f8ceSThomas Graf return err; 37917198f8ceSThomas Graf } 37927198f8ceSThomas Graf 3793101bb229SThomas Graf static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags, 3794101bb229SThomas Graf u8 scope, int ifindex) 3795101bb229SThomas Graf { 3796101bb229SThomas Graf struct ifaddrmsg *ifm; 3797101bb229SThomas Graf 3798101bb229SThomas Graf ifm = nlmsg_data(nlh); 3799101bb229SThomas Graf ifm->ifa_family = AF_INET6; 3800101bb229SThomas Graf ifm->ifa_prefixlen = prefixlen; 3801101bb229SThomas Graf ifm->ifa_flags = flags; 3802101bb229SThomas Graf ifm->ifa_scope = scope; 3803101bb229SThomas Graf ifm->ifa_index = ifindex; 3804101bb229SThomas Graf } 3805101bb229SThomas Graf 380685486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, 380785486af0SThomas Graf unsigned long tstamp, u32 preferred, u32 valid) 380885486af0SThomas Graf { 380985486af0SThomas Graf struct ifa_cacheinfo ci; 381085486af0SThomas Graf 381118a31e1eSThomas Graf ci.cstamp = cstamp_delta(cstamp); 381218a31e1eSThomas Graf ci.tstamp = cstamp_delta(tstamp); 381385486af0SThomas Graf ci.ifa_prefered = preferred; 381485486af0SThomas Graf ci.ifa_valid = valid; 381585486af0SThomas Graf 381685486af0SThomas Graf return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); 381785486af0SThomas Graf } 381885486af0SThomas Graf 3819101bb229SThomas Graf static inline int rt_scope(int ifa_scope) 3820101bb229SThomas Graf { 3821101bb229SThomas Graf if (ifa_scope & IFA_HOST) 3822101bb229SThomas Graf return RT_SCOPE_HOST; 3823101bb229SThomas Graf else if (ifa_scope & IFA_LINK) 3824101bb229SThomas Graf return RT_SCOPE_LINK; 3825101bb229SThomas Graf else if (ifa_scope & IFA_SITE) 3826101bb229SThomas Graf return RT_SCOPE_SITE; 3827101bb229SThomas Graf else 3828101bb229SThomas Graf return RT_SCOPE_UNIVERSE; 3829101bb229SThomas Graf } 3830101bb229SThomas Graf 38310ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void) 38320ab6803bSThomas Graf { 3833339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) 3834caeaba79SNicolas Dichtel + nla_total_size(16) /* IFA_LOCAL */ 3835339bf98fSThomas Graf + nla_total_size(16) /* IFA_ADDRESS */ 3836339bf98fSThomas Graf + nla_total_size(sizeof(struct ifa_cacheinfo)); 38370ab6803bSThomas Graf } 3838c5396a31SYOSHIFUJI Hideaki 38391da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa, 384015e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 38411da177e4SLinus Torvalds { 38421da177e4SLinus Torvalds struct nlmsghdr *nlh; 384385486af0SThomas Graf u32 preferred, valid; 38441da177e4SLinus Torvalds 384515e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 38460ab6803bSThomas Graf if (nlh == NULL) 384726932566SPatrick McHardy return -EMSGSIZE; 38480ab6803bSThomas Graf 3849101bb229SThomas Graf put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), 3850101bb229SThomas Graf ifa->idev->dev->ifindex); 3851101bb229SThomas Graf 38521da177e4SLinus Torvalds if (!(ifa->flags&IFA_F_PERMANENT)) { 385385486af0SThomas Graf preferred = ifa->prefered_lft; 385485486af0SThomas Graf valid = ifa->valid_lft; 385585486af0SThomas Graf if (preferred != INFINITY_LIFE_TIME) { 38561da177e4SLinus Torvalds long tval = (jiffies - ifa->tstamp)/HZ; 3857a1faa698SJens Rosenboom if (preferred > tval) 385885486af0SThomas Graf preferred -= tval; 3859a1faa698SJens Rosenboom else 3860a1faa698SJens Rosenboom preferred = 0; 3861f56619fcSBen Hutchings if (valid != INFINITY_LIFE_TIME) { 3862f56619fcSBen Hutchings if (valid > tval) 386385486af0SThomas Graf valid -= tval; 3864f56619fcSBen Hutchings else 3865f56619fcSBen Hutchings valid = 0; 3866f56619fcSBen Hutchings } 38671da177e4SLinus Torvalds } 38681da177e4SLinus Torvalds } else { 386985486af0SThomas Graf preferred = INFINITY_LIFE_TIME; 387085486af0SThomas Graf valid = INFINITY_LIFE_TIME; 38711da177e4SLinus Torvalds } 387285486af0SThomas Graf 38737996c799SCong Wang if (!ipv6_addr_any(&ifa->peer_addr)) { 3874caeaba79SNicolas Dichtel if (nla_put(skb, IFA_LOCAL, 16, &ifa->addr) < 0 || 3875caeaba79SNicolas Dichtel nla_put(skb, IFA_ADDRESS, 16, &ifa->peer_addr) < 0) 3876caeaba79SNicolas Dichtel goto error; 3877caeaba79SNicolas Dichtel } else 3878caeaba79SNicolas Dichtel if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0) 3879caeaba79SNicolas Dichtel goto error; 3880caeaba79SNicolas Dichtel 3881caeaba79SNicolas Dichtel if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) 3882caeaba79SNicolas Dichtel goto error; 388385486af0SThomas Graf 38840ab6803bSThomas Graf return nlmsg_end(skb, nlh); 3885caeaba79SNicolas Dichtel 3886caeaba79SNicolas Dichtel error: 3887caeaba79SNicolas Dichtel nlmsg_cancel(skb, nlh); 3888caeaba79SNicolas Dichtel return -EMSGSIZE; 38891da177e4SLinus Torvalds } 38901da177e4SLinus Torvalds 38911da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca, 389215e47304SEric W. Biederman u32 portid, u32 seq, int event, u16 flags) 38931da177e4SLinus Torvalds { 38941da177e4SLinus Torvalds struct nlmsghdr *nlh; 3895101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 3896101bb229SThomas Graf int ifindex = ifmca->idev->dev->ifindex; 38971da177e4SLinus Torvalds 38981da177e4SLinus Torvalds if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE) 3899101bb229SThomas Graf scope = RT_SCOPE_SITE; 3900101bb229SThomas Graf 390115e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 39020ab6803bSThomas Graf if (nlh == NULL) 390326932566SPatrick McHardy return -EMSGSIZE; 39040ab6803bSThomas Graf 3905101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 39060ab6803bSThomas Graf if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 || 39070ab6803bSThomas Graf put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, 390826932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 390926932566SPatrick McHardy nlmsg_cancel(skb, nlh); 391026932566SPatrick McHardy return -EMSGSIZE; 391126932566SPatrick McHardy } 391285486af0SThomas Graf 39130ab6803bSThomas Graf return nlmsg_end(skb, nlh); 39141da177e4SLinus Torvalds } 39151da177e4SLinus Torvalds 39161da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca, 391715e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 39181da177e4SLinus Torvalds { 39191da177e4SLinus Torvalds struct nlmsghdr *nlh; 3920101bb229SThomas Graf u8 scope = RT_SCOPE_UNIVERSE; 3921101bb229SThomas Graf int ifindex = ifaca->aca_idev->dev->ifindex; 39221da177e4SLinus Torvalds 39231da177e4SLinus Torvalds if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE) 3924101bb229SThomas Graf scope = RT_SCOPE_SITE; 3925101bb229SThomas Graf 392615e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 39270ab6803bSThomas Graf if (nlh == NULL) 392826932566SPatrick McHardy return -EMSGSIZE; 39290ab6803bSThomas Graf 3930101bb229SThomas Graf put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 39310ab6803bSThomas Graf if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 || 39320ab6803bSThomas Graf put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, 393326932566SPatrick McHardy INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 393426932566SPatrick McHardy nlmsg_cancel(skb, nlh); 393526932566SPatrick McHardy return -EMSGSIZE; 393626932566SPatrick McHardy } 393785486af0SThomas Graf 39380ab6803bSThomas Graf return nlmsg_end(skb, nlh); 39391da177e4SLinus Torvalds } 39401da177e4SLinus Torvalds 3941e21e8467SStephen Hemminger enum addr_type_t { 39421da177e4SLinus Torvalds UNICAST_ADDR, 39431da177e4SLinus Torvalds MULTICAST_ADDR, 39441da177e4SLinus Torvalds ANYCAST_ADDR, 39451da177e4SLinus Torvalds }; 39461da177e4SLinus Torvalds 3947234b27c3SEric Dumazet /* called with rcu_read_lock() */ 3948234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb, 3949234b27c3SEric Dumazet struct netlink_callback *cb, enum addr_type_t type, 3950234b27c3SEric Dumazet int s_ip_idx, int *p_ip_idx) 39511da177e4SLinus Torvalds { 39521da177e4SLinus Torvalds struct ifmcaddr6 *ifmca; 39531da177e4SLinus Torvalds struct ifacaddr6 *ifaca; 3954234b27c3SEric Dumazet int err = 1; 3955234b27c3SEric Dumazet int ip_idx = *p_ip_idx; 39561da177e4SLinus Torvalds 39571da177e4SLinus Torvalds read_lock_bh(&idev->lock); 39581da177e4SLinus Torvalds switch (type) { 3959502a2ffdSstephen hemminger case UNICAST_ADDR: { 3960502a2ffdSstephen hemminger struct inet6_ifaddr *ifa; 3961502a2ffdSstephen hemminger 3962ae9cda5dSYOSHIFUJI Hideaki /* unicast address incl. temp addr */ 3963502a2ffdSstephen hemminger list_for_each_entry(ifa, &idev->addr_list, if_list) { 3964502a2ffdSstephen hemminger if (++ip_idx < s_ip_idx) 39651da177e4SLinus Torvalds continue; 39665d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifaddr(skb, ifa, 396715e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 39685d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 39695d5619b4SYOSHIFUJI Hideaki RTM_NEWADDR, 39705d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 3971bcd32326SEric Dumazet if (err <= 0) 3972bcd32326SEric Dumazet break; 397363998ac2SNicolas Dichtel nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 39741da177e4SLinus Torvalds } 39751da177e4SLinus Torvalds break; 3976502a2ffdSstephen hemminger } 39771da177e4SLinus Torvalds case MULTICAST_ADDR: 39781da177e4SLinus Torvalds /* multicast address */ 39791da177e4SLinus Torvalds for (ifmca = idev->mc_list; ifmca; 39801da177e4SLinus Torvalds ifmca = ifmca->next, ip_idx++) { 39811da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 39821da177e4SLinus Torvalds continue; 39835d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifmcaddr(skb, ifmca, 398415e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 39855d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 39865d5619b4SYOSHIFUJI Hideaki RTM_GETMULTICAST, 39875d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 3988bcd32326SEric Dumazet if (err <= 0) 3989bcd32326SEric Dumazet break; 39901da177e4SLinus Torvalds } 39911da177e4SLinus Torvalds break; 39921da177e4SLinus Torvalds case ANYCAST_ADDR: 39931da177e4SLinus Torvalds /* anycast address */ 39941da177e4SLinus Torvalds for (ifaca = idev->ac_list; ifaca; 39951da177e4SLinus Torvalds ifaca = ifaca->aca_next, ip_idx++) { 39961da177e4SLinus Torvalds if (ip_idx < s_ip_idx) 39971da177e4SLinus Torvalds continue; 39985d5619b4SYOSHIFUJI Hideaki err = inet6_fill_ifacaddr(skb, ifaca, 399915e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 40005d5619b4SYOSHIFUJI Hideaki cb->nlh->nlmsg_seq, 40015d5619b4SYOSHIFUJI Hideaki RTM_GETANYCAST, 40025d5619b4SYOSHIFUJI Hideaki NLM_F_MULTI); 4003bcd32326SEric Dumazet if (err <= 0) 4004bcd32326SEric Dumazet break; 40051da177e4SLinus Torvalds } 40061da177e4SLinus Torvalds break; 40071da177e4SLinus Torvalds default: 40081da177e4SLinus Torvalds break; 40091da177e4SLinus Torvalds } 40101da177e4SLinus Torvalds read_unlock_bh(&idev->lock); 4011234b27c3SEric Dumazet *p_ip_idx = ip_idx; 4012234b27c3SEric Dumazet return err; 4013234b27c3SEric Dumazet } 40145d5619b4SYOSHIFUJI Hideaki 4015234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, 4016234b27c3SEric Dumazet enum addr_type_t type) 4017234b27c3SEric Dumazet { 4018234b27c3SEric Dumazet struct net *net = sock_net(skb->sk); 4019234b27c3SEric Dumazet int h, s_h; 4020234b27c3SEric Dumazet int idx, ip_idx; 4021234b27c3SEric Dumazet int s_idx, s_ip_idx; 4022234b27c3SEric Dumazet struct net_device *dev; 4023234b27c3SEric Dumazet struct inet6_dev *idev; 4024234b27c3SEric Dumazet struct hlist_head *head; 4025234b27c3SEric Dumazet 4026234b27c3SEric Dumazet s_h = cb->args[0]; 4027234b27c3SEric Dumazet s_idx = idx = cb->args[1]; 4028234b27c3SEric Dumazet s_ip_idx = ip_idx = cb->args[2]; 4029234b27c3SEric Dumazet 4030234b27c3SEric Dumazet rcu_read_lock(); 403163998ac2SNicolas Dichtel cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq; 4032234b27c3SEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 4033234b27c3SEric Dumazet idx = 0; 4034234b27c3SEric Dumazet head = &net->dev_index_head[h]; 4035b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 4036234b27c3SEric Dumazet if (idx < s_idx) 4037234b27c3SEric Dumazet goto cont; 40384b97efdfSPatrick McHardy if (h > s_h || idx > s_idx) 4039234b27c3SEric Dumazet s_ip_idx = 0; 4040234b27c3SEric Dumazet ip_idx = 0; 4041e21e8467SStephen Hemminger idev = __in6_dev_get(dev); 4042e21e8467SStephen Hemminger if (!idev) 4043234b27c3SEric Dumazet goto cont; 4044234b27c3SEric Dumazet 4045234b27c3SEric Dumazet if (in6_dump_addrs(idev, skb, cb, type, 4046234b27c3SEric Dumazet s_ip_idx, &ip_idx) <= 0) 4047234b27c3SEric Dumazet goto done; 40487562f876SPavel Emelianov cont: 40497562f876SPavel Emelianov idx++; 40501da177e4SLinus Torvalds } 4051234b27c3SEric Dumazet } 4052234b27c3SEric Dumazet done: 4053234b27c3SEric Dumazet rcu_read_unlock(); 4054234b27c3SEric Dumazet cb->args[0] = h; 4055234b27c3SEric Dumazet cb->args[1] = idx; 4056234b27c3SEric Dumazet cb->args[2] = ip_idx; 4057234b27c3SEric Dumazet 40581da177e4SLinus Torvalds return skb->len; 40591da177e4SLinus Torvalds } 40601da177e4SLinus Torvalds 40611da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 40621da177e4SLinus Torvalds { 40631da177e4SLinus Torvalds enum addr_type_t type = UNICAST_ADDR; 4064b854272bSDenis V. Lunev 40651da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 40661da177e4SLinus Torvalds } 40671da177e4SLinus Torvalds 40681da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) 40691da177e4SLinus Torvalds { 40701da177e4SLinus Torvalds enum addr_type_t type = MULTICAST_ADDR; 4071b854272bSDenis V. Lunev 40721da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 40731da177e4SLinus Torvalds } 40741da177e4SLinus Torvalds 40751da177e4SLinus Torvalds 40761da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb) 40771da177e4SLinus Torvalds { 40781da177e4SLinus Torvalds enum addr_type_t type = ANYCAST_ADDR; 4079b854272bSDenis V. Lunev 40801da177e4SLinus Torvalds return inet6_dump_addr(skb, cb, type); 40811da177e4SLinus Torvalds } 40821da177e4SLinus Torvalds 4083661d2967SThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh) 40846c223828SNoriaki TAKAMIYA { 40853b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(in_skb->sk); 40861b29fc2cSThomas Graf struct ifaddrmsg *ifm; 40871b29fc2cSThomas Graf struct nlattr *tb[IFA_MAX+1]; 4088caeaba79SNicolas Dichtel struct in6_addr *addr = NULL, *peer; 40896c223828SNoriaki TAKAMIYA struct net_device *dev = NULL; 40906c223828SNoriaki TAKAMIYA struct inet6_ifaddr *ifa; 40916c223828SNoriaki TAKAMIYA struct sk_buff *skb; 40926c223828SNoriaki TAKAMIYA int err; 40936c223828SNoriaki TAKAMIYA 40941b29fc2cSThomas Graf err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy); 40951b29fc2cSThomas Graf if (err < 0) 40961b29fc2cSThomas Graf goto errout; 40976c223828SNoriaki TAKAMIYA 4098caeaba79SNicolas Dichtel addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer); 40991b29fc2cSThomas Graf if (addr == NULL) { 41001b29fc2cSThomas Graf err = -EINVAL; 41011b29fc2cSThomas Graf goto errout; 41021b29fc2cSThomas Graf } 41031b29fc2cSThomas Graf 41041b29fc2cSThomas Graf ifm = nlmsg_data(nlh); 41056c223828SNoriaki TAKAMIYA if (ifm->ifa_index) 41066fda7350SBenjamin Thery dev = __dev_get_by_index(net, ifm->ifa_index); 41076c223828SNoriaki TAKAMIYA 4108e21e8467SStephen Hemminger ifa = ipv6_get_ifaddr(net, addr, dev, 1); 4109e21e8467SStephen Hemminger if (!ifa) { 41101b29fc2cSThomas Graf err = -EADDRNOTAVAIL; 41111b29fc2cSThomas Graf goto errout; 41126c223828SNoriaki TAKAMIYA } 41136c223828SNoriaki TAKAMIYA 4114e21e8467SStephen Hemminger skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL); 4115e21e8467SStephen Hemminger if (!skb) { 41161b29fc2cSThomas Graf err = -ENOBUFS; 41171b29fc2cSThomas Graf goto errout_ifa; 41181b29fc2cSThomas Graf } 41191b29fc2cSThomas Graf 412015e47304SEric W. Biederman err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid, 41216c223828SNoriaki TAKAMIYA nlh->nlmsg_seq, RTM_NEWADDR, 0); 412226932566SPatrick McHardy if (err < 0) { 412326932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 412426932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 412526932566SPatrick McHardy kfree_skb(skb); 412626932566SPatrick McHardy goto errout_ifa; 412726932566SPatrick McHardy } 412815e47304SEric W. Biederman err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 41291b29fc2cSThomas Graf errout_ifa: 41306c223828SNoriaki TAKAMIYA in6_ifa_put(ifa); 41311b29fc2cSThomas Graf errout: 41326c223828SNoriaki TAKAMIYA return err; 41336c223828SNoriaki TAKAMIYA } 41346c223828SNoriaki TAKAMIYA 41351da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa) 41361da177e4SLinus Torvalds { 41371da177e4SLinus Torvalds struct sk_buff *skb; 4138c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(ifa->idev->dev); 41395d620266SThomas Graf int err = -ENOBUFS; 41401da177e4SLinus Torvalds 41410ab6803bSThomas Graf skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); 41425d620266SThomas Graf if (skb == NULL) 41435d620266SThomas Graf goto errout; 41445d620266SThomas Graf 41455d620266SThomas Graf err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0); 414626932566SPatrick McHardy if (err < 0) { 414726932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */ 414826932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 414926932566SPatrick McHardy kfree_skb(skb); 415026932566SPatrick McHardy goto errout; 415126932566SPatrick McHardy } 41521ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC); 41531ce85fe4SPablo Neira Ayuso return; 41545d620266SThomas Graf errout: 41555d620266SThomas Graf if (err < 0) 41566fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err); 41571da177e4SLinus Torvalds } 41581da177e4SLinus Torvalds 4159b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf, 41601da177e4SLinus Torvalds __s32 *array, int bytes) 41611da177e4SLinus Torvalds { 416204561c1fSThomas Graf BUG_ON(bytes < (DEVCONF_MAX * 4)); 416304561c1fSThomas Graf 41641da177e4SLinus Torvalds memset(array, 0, bytes); 41651da177e4SLinus Torvalds array[DEVCONF_FORWARDING] = cnf->forwarding; 41661da177e4SLinus Torvalds array[DEVCONF_HOPLIMIT] = cnf->hop_limit; 41671da177e4SLinus Torvalds array[DEVCONF_MTU6] = cnf->mtu6; 41681da177e4SLinus Torvalds array[DEVCONF_ACCEPT_RA] = cnf->accept_ra; 41691da177e4SLinus Torvalds array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects; 41701da177e4SLinus Torvalds array[DEVCONF_AUTOCONF] = cnf->autoconf; 41711da177e4SLinus Torvalds array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits; 41721da177e4SLinus Torvalds array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits; 417393908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_INTERVAL] = 417493908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_interval); 417593908d19SThomas Graf array[DEVCONF_RTR_SOLICIT_DELAY] = 417693908d19SThomas Graf jiffies_to_msecs(cnf->rtr_solicit_delay); 41771da177e4SLinus Torvalds array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version; 41781da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 41791da177e4SLinus Torvalds array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; 41801da177e4SLinus Torvalds array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; 41811da177e4SLinus Torvalds array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; 41821da177e4SLinus Torvalds array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; 41831da177e4SLinus Torvalds array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; 41841da177e4SLinus Torvalds #endif 41851da177e4SLinus Torvalds array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; 418665f5c7c1SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; 4187c4fd30ebSYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; 4188930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 4189930d6ff2SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref; 419093908d19SThomas Graf array[DEVCONF_RTR_PROBE_INTERVAL] = 419193908d19SThomas Graf jiffies_to_msecs(cnf->rtr_probe_interval); 4192fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 419309c884d4SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen; 419409c884d4SYOSHIFUJI Hideaki #endif 4195930d6ff2SYOSHIFUJI Hideaki #endif 4196fbea49e1SYOSHIFUJI Hideaki array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp; 41970bcbc926SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route; 419895c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 419995c385b4SNeil Horman array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad; 420095c385b4SNeil Horman #endif 42017bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 42027bc570c8SYOSHIFUJI Hideaki array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding; 42037bc570c8SYOSHIFUJI Hideaki #endif 4204778d80beSYOSHIFUJI Hideaki array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6; 42051b34be74SYOSHIFUJI Hideaki array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad; 4206c3faca05SCosmin Ratiu array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao; 42075cb04436SHannes Frederic Sowa array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify; 42081da177e4SLinus Torvalds } 42091da177e4SLinus Torvalds 4210b382b191SThomas Graf static inline size_t inet6_ifla6_size(void) 4211b382b191SThomas Graf { 4212b382b191SThomas Graf return nla_total_size(4) /* IFLA_INET6_FLAGS */ 4213b382b191SThomas Graf + nla_total_size(sizeof(struct ifla_cacheinfo)) 4214b382b191SThomas Graf + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */ 4215b382b191SThomas Graf + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */ 4216f53adae4SDaniel Borkmann + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */ 4217f53adae4SDaniel Borkmann + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */ 4218b382b191SThomas Graf } 4219b382b191SThomas Graf 4220339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void) 4221339bf98fSThomas Graf { 4222339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 4223339bf98fSThomas Graf + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 4224339bf98fSThomas Graf + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 4225339bf98fSThomas Graf + nla_total_size(4) /* IFLA_MTU */ 4226339bf98fSThomas Graf + nla_total_size(4) /* IFLA_LINK */ 4227b382b191SThomas Graf + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */ 4228339bf98fSThomas Graf } 4229c5396a31SYOSHIFUJI Hideaki 4230be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib, 42317d720c3eSTejun Heo int items, int bytes) 42327f7d9a6bSHerbert Xu { 42337f7d9a6bSHerbert Xu int i; 42347f7d9a6bSHerbert Xu int pad = bytes - sizeof(u64) * items; 42357f7d9a6bSHerbert Xu BUG_ON(pad < 0); 42367f7d9a6bSHerbert Xu 42377f7d9a6bSHerbert Xu /* Use put_unaligned() because stats may not be aligned for u64. */ 42387f7d9a6bSHerbert Xu put_unaligned(items, &stats[0]); 42397f7d9a6bSHerbert Xu for (i = 1; i < items; i++) 4240be281e55SEric Dumazet put_unaligned(atomic_long_read(&mib[i]), &stats[i]); 42417f7d9a6bSHerbert Xu 42427f7d9a6bSHerbert Xu memset(&stats[items], 0, pad); 42437f7d9a6bSHerbert Xu } 42447f7d9a6bSHerbert Xu 42454ce3c183SEric Dumazet static inline void __snmp6_fill_stats64(u64 *stats, void __percpu **mib, 42464ce3c183SEric Dumazet int items, int bytes, size_t syncpoff) 42474ce3c183SEric Dumazet { 42484ce3c183SEric Dumazet int i; 42494ce3c183SEric Dumazet int pad = bytes - sizeof(u64) * items; 42504ce3c183SEric Dumazet BUG_ON(pad < 0); 42514ce3c183SEric Dumazet 42524ce3c183SEric Dumazet /* Use put_unaligned() because stats may not be aligned for u64. */ 42534ce3c183SEric Dumazet put_unaligned(items, &stats[0]); 42544ce3c183SEric Dumazet for (i = 1; i < items; i++) 42554ce3c183SEric Dumazet put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]); 42564ce3c183SEric Dumazet 42574ce3c183SEric Dumazet memset(&stats[items], 0, pad); 42584ce3c183SEric Dumazet } 42594ce3c183SEric Dumazet 42607f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype, 42617f7d9a6bSHerbert Xu int bytes) 42627f7d9a6bSHerbert Xu { 42637f7d9a6bSHerbert Xu switch (attrtype) { 42647f7d9a6bSHerbert Xu case IFLA_INET6_STATS: 42654ce3c183SEric Dumazet __snmp6_fill_stats64(stats, (void __percpu **)idev->stats.ipv6, 42664ce3c183SEric Dumazet IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp)); 42677f7d9a6bSHerbert Xu break; 42687f7d9a6bSHerbert Xu case IFLA_INET6_ICMP6STATS: 4269be281e55SEric Dumazet __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes); 42707f7d9a6bSHerbert Xu break; 42717f7d9a6bSHerbert Xu } 42727f7d9a6bSHerbert Xu } 42737f7d9a6bSHerbert Xu 4274b382b191SThomas Graf static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev) 42751da177e4SLinus Torvalds { 4276bf99f1bdSYOSHIFUJI Hideaki struct nlattr *nla; 42771da177e4SLinus Torvalds struct ifla_cacheinfo ci; 42781da177e4SLinus Torvalds 4279c78679e8SDavid S. Miller if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags)) 4280c78679e8SDavid S. Miller goto nla_put_failure; 42811da177e4SLinus Torvalds ci.max_reasm_len = IPV6_MAXPLEN; 428224912420SDavid S. Miller ci.tstamp = cstamp_delta(idev->tstamp); 428324912420SDavid S. Miller ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time); 428424912420SDavid S. Miller ci.retrans_time = jiffies_to_msecs(idev->nd_parms->retrans_time); 4285c78679e8SDavid S. Miller if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci)) 4286c78679e8SDavid S. Miller goto nla_put_failure; 4287bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); 4288bf99f1bdSYOSHIFUJI Hideaki if (nla == NULL) 428904561c1fSThomas Graf goto nla_put_failure; 4290bf99f1bdSYOSHIFUJI Hideaki ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); 42911da177e4SLinus Torvalds 4292bf99f1bdSYOSHIFUJI Hideaki /* XXX - MC not implemented */ 4293bf99f1bdSYOSHIFUJI Hideaki 4294bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); 4295bf99f1bdSYOSHIFUJI Hideaki if (nla == NULL) 4296bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 4297bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); 4298bf99f1bdSYOSHIFUJI Hideaki 4299bf99f1bdSYOSHIFUJI Hideaki nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); 4300bf99f1bdSYOSHIFUJI Hideaki if (nla == NULL) 4301bf99f1bdSYOSHIFUJI Hideaki goto nla_put_failure; 4302bf99f1bdSYOSHIFUJI Hideaki snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); 43031da177e4SLinus Torvalds 4304f53adae4SDaniel Borkmann nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr)); 4305f53adae4SDaniel Borkmann if (nla == NULL) 4306f53adae4SDaniel Borkmann goto nla_put_failure; 4307f53adae4SDaniel Borkmann read_lock_bh(&idev->lock); 4308f53adae4SDaniel Borkmann memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla)); 4309f53adae4SDaniel Borkmann read_unlock_bh(&idev->lock); 4310f53adae4SDaniel Borkmann 4311b382b191SThomas Graf return 0; 4312b382b191SThomas Graf 4313b382b191SThomas Graf nla_put_failure: 4314b382b191SThomas Graf return -EMSGSIZE; 4315b382b191SThomas Graf } 4316b382b191SThomas Graf 4317b382b191SThomas Graf static size_t inet6_get_link_af_size(const struct net_device *dev) 4318b382b191SThomas Graf { 4319b382b191SThomas Graf if (!__in6_dev_get(dev)) 4320b382b191SThomas Graf return 0; 4321b382b191SThomas Graf 4322b382b191SThomas Graf return inet6_ifla6_size(); 4323b382b191SThomas Graf } 4324b382b191SThomas Graf 4325b382b191SThomas Graf static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev) 4326b382b191SThomas Graf { 4327b382b191SThomas Graf struct inet6_dev *idev = __in6_dev_get(dev); 4328b382b191SThomas Graf 4329b382b191SThomas Graf if (!idev) 4330b382b191SThomas Graf return -ENODATA; 4331b382b191SThomas Graf 4332b382b191SThomas Graf if (inet6_fill_ifla6_attrs(skb, idev) < 0) 4333b382b191SThomas Graf return -EMSGSIZE; 4334b382b191SThomas Graf 4335b382b191SThomas Graf return 0; 4336b382b191SThomas Graf } 4337b382b191SThomas Graf 4338f53adae4SDaniel Borkmann static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token) 4339f53adae4SDaniel Borkmann { 4340f53adae4SDaniel Borkmann struct inet6_ifaddr *ifp; 4341f53adae4SDaniel Borkmann struct net_device *dev = idev->dev; 4342fc403832SDaniel Borkmann bool update_rs = false; 4343f53adae4SDaniel Borkmann 4344f53adae4SDaniel Borkmann if (token == NULL) 4345f53adae4SDaniel Borkmann return -EINVAL; 4346f53adae4SDaniel Borkmann if (ipv6_addr_any(token)) 4347f53adae4SDaniel Borkmann return -EINVAL; 4348f53adae4SDaniel Borkmann if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) 4349f53adae4SDaniel Borkmann return -EINVAL; 4350f53adae4SDaniel Borkmann if (!ipv6_accept_ra(idev)) 4351f53adae4SDaniel Borkmann return -EINVAL; 4352f53adae4SDaniel Borkmann if (idev->cnf.rtr_solicits <= 0) 4353f53adae4SDaniel Borkmann return -EINVAL; 4354f53adae4SDaniel Borkmann 4355f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 4356f53adae4SDaniel Borkmann 4357f53adae4SDaniel Borkmann BUILD_BUG_ON(sizeof(token->s6_addr) != 16); 4358f53adae4SDaniel Borkmann memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8); 4359f53adae4SDaniel Borkmann 4360f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 4361f53adae4SDaniel Borkmann 4362fc403832SDaniel Borkmann if (!idev->dead && (idev->if_flags & IF_READY)) { 4363fc403832SDaniel Borkmann struct in6_addr ll_addr; 4364fc403832SDaniel Borkmann 4365fc403832SDaniel Borkmann ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE | 4366fc403832SDaniel Borkmann IFA_F_OPTIMISTIC); 4367fc403832SDaniel Borkmann 4368fc403832SDaniel Borkmann /* If we're not ready, then normal ifup will take care 4369fc403832SDaniel Borkmann * of this. Otherwise, we need to request our rs here. 4370fc403832SDaniel Borkmann */ 4371f53adae4SDaniel Borkmann ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters); 4372fc403832SDaniel Borkmann update_rs = true; 4373fc403832SDaniel Borkmann } 4374f53adae4SDaniel Borkmann 4375f53adae4SDaniel Borkmann write_lock_bh(&idev->lock); 4376fc403832SDaniel Borkmann 437777ecaaceSHannes Frederic Sowa if (update_rs) { 4378f53adae4SDaniel Borkmann idev->if_flags |= IF_RS_SENT; 437977ecaaceSHannes Frederic Sowa idev->rs_probes = 1; 438077ecaaceSHannes Frederic Sowa addrconf_mod_rs_timer(idev, idev->cnf.rtr_solicit_interval); 438177ecaaceSHannes Frederic Sowa } 4382f53adae4SDaniel Borkmann 4383f53adae4SDaniel Borkmann /* Well, that's kinda nasty ... */ 4384f53adae4SDaniel Borkmann list_for_each_entry(ifp, &idev->addr_list, if_list) { 4385f53adae4SDaniel Borkmann spin_lock(&ifp->lock); 4386617fe29dSDaniel Borkmann if (ifp->tokenized) { 4387f53adae4SDaniel Borkmann ifp->valid_lft = 0; 4388f53adae4SDaniel Borkmann ifp->prefered_lft = 0; 4389f53adae4SDaniel Borkmann } 4390f53adae4SDaniel Borkmann spin_unlock(&ifp->lock); 4391f53adae4SDaniel Borkmann } 4392f53adae4SDaniel Borkmann 4393f53adae4SDaniel Borkmann write_unlock_bh(&idev->lock); 43942b9651d7SHannes Frederic Sowa addrconf_verify(0); 4395f53adae4SDaniel Borkmann return 0; 4396f53adae4SDaniel Borkmann } 4397f53adae4SDaniel Borkmann 4398f53adae4SDaniel Borkmann static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla) 4399f53adae4SDaniel Borkmann { 4400f53adae4SDaniel Borkmann int err = -EINVAL; 4401f53adae4SDaniel Borkmann struct inet6_dev *idev = __in6_dev_get(dev); 4402f53adae4SDaniel Borkmann struct nlattr *tb[IFLA_INET6_MAX + 1]; 4403f53adae4SDaniel Borkmann 4404f53adae4SDaniel Borkmann if (!idev) 4405f53adae4SDaniel Borkmann return -EAFNOSUPPORT; 4406f53adae4SDaniel Borkmann 4407f53adae4SDaniel Borkmann if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0) 4408f53adae4SDaniel Borkmann BUG(); 4409f53adae4SDaniel Borkmann 4410f53adae4SDaniel Borkmann if (tb[IFLA_INET6_TOKEN]) 4411f53adae4SDaniel Borkmann err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN])); 4412f53adae4SDaniel Borkmann 4413f53adae4SDaniel Borkmann return err; 4414f53adae4SDaniel Borkmann } 4415f53adae4SDaniel Borkmann 4416b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev, 441715e47304SEric W. Biederman u32 portid, u32 seq, int event, unsigned int flags) 4418b382b191SThomas Graf { 4419b382b191SThomas Graf struct net_device *dev = idev->dev; 4420b382b191SThomas Graf struct ifinfomsg *hdr; 4421b382b191SThomas Graf struct nlmsghdr *nlh; 4422b382b191SThomas Graf void *protoinfo; 4423b382b191SThomas Graf 442415e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags); 4425b382b191SThomas Graf if (nlh == NULL) 4426b382b191SThomas Graf return -EMSGSIZE; 4427b382b191SThomas Graf 4428b382b191SThomas Graf hdr = nlmsg_data(nlh); 4429b382b191SThomas Graf hdr->ifi_family = AF_INET6; 4430b382b191SThomas Graf hdr->__ifi_pad = 0; 4431b382b191SThomas Graf hdr->ifi_type = dev->type; 4432b382b191SThomas Graf hdr->ifi_index = dev->ifindex; 4433b382b191SThomas Graf hdr->ifi_flags = dev_get_flags(dev); 4434b382b191SThomas Graf hdr->ifi_change = 0; 4435b382b191SThomas Graf 4436c78679e8SDavid S. Miller if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 4437c78679e8SDavid S. Miller (dev->addr_len && 4438c78679e8SDavid S. Miller nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 4439c78679e8SDavid S. Miller nla_put_u32(skb, IFLA_MTU, dev->mtu) || 4440c78679e8SDavid S. Miller (dev->ifindex != dev->iflink && 4441c78679e8SDavid S. Miller nla_put_u32(skb, IFLA_LINK, dev->iflink))) 4442c78679e8SDavid S. Miller goto nla_put_failure; 4443b382b191SThomas Graf protoinfo = nla_nest_start(skb, IFLA_PROTINFO); 4444b382b191SThomas Graf if (protoinfo == NULL) 4445b382b191SThomas Graf goto nla_put_failure; 4446b382b191SThomas Graf 4447b382b191SThomas Graf if (inet6_fill_ifla6_attrs(skb, idev) < 0) 4448b382b191SThomas Graf goto nla_put_failure; 4449b382b191SThomas Graf 445004561c1fSThomas Graf nla_nest_end(skb, protoinfo); 445104561c1fSThomas Graf return nlmsg_end(skb, nlh); 44521da177e4SLinus Torvalds 445304561c1fSThomas Graf nla_put_failure: 445426932566SPatrick McHardy nlmsg_cancel(skb, nlh); 445526932566SPatrick McHardy return -EMSGSIZE; 44561da177e4SLinus Torvalds } 44571da177e4SLinus Torvalds 44581da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 44591da177e4SLinus Torvalds { 44603b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 446184d2697dSEric Dumazet int h, s_h; 4462434a8a58SDavid S. Miller int idx = 0, s_idx; 44631da177e4SLinus Torvalds struct net_device *dev; 44641da177e4SLinus Torvalds struct inet6_dev *idev; 446584d2697dSEric Dumazet struct hlist_head *head; 44661da177e4SLinus Torvalds 446784d2697dSEric Dumazet s_h = cb->args[0]; 446884d2697dSEric Dumazet s_idx = cb->args[1]; 446984d2697dSEric Dumazet 447084d2697dSEric Dumazet rcu_read_lock(); 447184d2697dSEric Dumazet for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 44727562f876SPavel Emelianov idx = 0; 447384d2697dSEric Dumazet head = &net->dev_index_head[h]; 4474b67bfe0dSSasha Levin hlist_for_each_entry_rcu(dev, head, index_hlist) { 44751da177e4SLinus Torvalds if (idx < s_idx) 44767562f876SPavel Emelianov goto cont; 447784d2697dSEric Dumazet idev = __in6_dev_get(dev); 447884d2697dSEric Dumazet if (!idev) 44797562f876SPavel Emelianov goto cont; 448084d2697dSEric Dumazet if (inet6_fill_ifinfo(skb, idev, 448115e47304SEric W. Biederman NETLINK_CB(cb->skb).portid, 448284d2697dSEric Dumazet cb->nlh->nlmsg_seq, 448384d2697dSEric Dumazet RTM_NEWLINK, NLM_F_MULTI) <= 0) 448484d2697dSEric Dumazet goto out; 44857562f876SPavel Emelianov cont: 44867562f876SPavel Emelianov idx++; 44871da177e4SLinus Torvalds } 448884d2697dSEric Dumazet } 448984d2697dSEric Dumazet out: 449084d2697dSEric Dumazet rcu_read_unlock(); 449184d2697dSEric Dumazet cb->args[1] = idx; 449284d2697dSEric Dumazet cb->args[0] = h; 44931da177e4SLinus Torvalds 44941da177e4SLinus Torvalds return skb->len; 44951da177e4SLinus Torvalds } 44961da177e4SLinus Torvalds 44971da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev) 44981da177e4SLinus Torvalds { 44991da177e4SLinus Torvalds struct sk_buff *skb; 4500c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 45018d7a76c9SThomas Graf int err = -ENOBUFS; 45021da177e4SLinus Torvalds 4503339bf98fSThomas Graf skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); 45048d7a76c9SThomas Graf if (skb == NULL) 45058d7a76c9SThomas Graf goto errout; 45068d7a76c9SThomas Graf 45078d7a76c9SThomas Graf err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); 450826932566SPatrick McHardy if (err < 0) { 450926932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */ 451026932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 451126932566SPatrick McHardy kfree_skb(skb); 451226932566SPatrick McHardy goto errout; 451326932566SPatrick McHardy } 45145f75a104SNicolas Dichtel rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC); 45151ce85fe4SPablo Neira Ayuso return; 45168d7a76c9SThomas Graf errout: 45178d7a76c9SThomas Graf if (err < 0) 45185f75a104SNicolas Dichtel rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err); 45191da177e4SLinus Torvalds } 45201da177e4SLinus Torvalds 4521339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void) 4522339bf98fSThomas Graf { 4523339bf98fSThomas Graf return NLMSG_ALIGN(sizeof(struct prefixmsg)) 4524339bf98fSThomas Graf + nla_total_size(sizeof(struct in6_addr)) 4525339bf98fSThomas Graf + nla_total_size(sizeof(struct prefix_cacheinfo)); 4526339bf98fSThomas Graf } 4527c5396a31SYOSHIFUJI Hideaki 45281da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev, 452915e47304SEric W. Biederman struct prefix_info *pinfo, u32 portid, u32 seq, 4530b6544c0bSJamal Hadi Salim int event, unsigned int flags) 45311da177e4SLinus Torvalds { 45321da177e4SLinus Torvalds struct prefixmsg *pmsg; 45331da177e4SLinus Torvalds struct nlmsghdr *nlh; 45341da177e4SLinus Torvalds struct prefix_cacheinfo ci; 45351da177e4SLinus Torvalds 453615e47304SEric W. Biederman nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags); 45376051e2f4SThomas Graf if (nlh == NULL) 453826932566SPatrick McHardy return -EMSGSIZE; 45396051e2f4SThomas Graf 45406051e2f4SThomas Graf pmsg = nlmsg_data(nlh); 45411da177e4SLinus Torvalds pmsg->prefix_family = AF_INET6; 45428a47077aSPatrick McHardy pmsg->prefix_pad1 = 0; 45438a47077aSPatrick McHardy pmsg->prefix_pad2 = 0; 45441da177e4SLinus Torvalds pmsg->prefix_ifindex = idev->dev->ifindex; 45451da177e4SLinus Torvalds pmsg->prefix_len = pinfo->prefix_len; 45461da177e4SLinus Torvalds pmsg->prefix_type = pinfo->type; 45478a47077aSPatrick McHardy pmsg->prefix_pad3 = 0; 45481da177e4SLinus Torvalds pmsg->prefix_flags = 0; 45491da177e4SLinus Torvalds if (pinfo->onlink) 45501da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_ONLINK; 45511da177e4SLinus Torvalds if (pinfo->autoconf) 45521da177e4SLinus Torvalds pmsg->prefix_flags |= IF_PREFIX_AUTOCONF; 45531da177e4SLinus Torvalds 4554c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix)) 4555c78679e8SDavid S. Miller goto nla_put_failure; 45561da177e4SLinus Torvalds ci.preferred_time = ntohl(pinfo->prefered); 45571da177e4SLinus Torvalds ci.valid_time = ntohl(pinfo->valid); 4558c78679e8SDavid S. Miller if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci)) 4559c78679e8SDavid S. Miller goto nla_put_failure; 45606051e2f4SThomas Graf return nlmsg_end(skb, nlh); 45611da177e4SLinus Torvalds 45626051e2f4SThomas Graf nla_put_failure: 456326932566SPatrick McHardy nlmsg_cancel(skb, nlh); 456426932566SPatrick McHardy return -EMSGSIZE; 45651da177e4SLinus Torvalds } 45661da177e4SLinus Torvalds 45671da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev, 45681da177e4SLinus Torvalds struct prefix_info *pinfo) 45691da177e4SLinus Torvalds { 45701da177e4SLinus Torvalds struct sk_buff *skb; 4571c346dca1SYOSHIFUJI Hideaki struct net *net = dev_net(idev->dev); 45728c384bfaSThomas Graf int err = -ENOBUFS; 45731da177e4SLinus Torvalds 4574339bf98fSThomas Graf skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); 45758c384bfaSThomas Graf if (skb == NULL) 45768c384bfaSThomas Graf goto errout; 45778c384bfaSThomas Graf 45788c384bfaSThomas Graf err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); 457926932566SPatrick McHardy if (err < 0) { 458026932566SPatrick McHardy /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */ 458126932566SPatrick McHardy WARN_ON(err == -EMSGSIZE); 458226932566SPatrick McHardy kfree_skb(skb); 458326932566SPatrick McHardy goto errout; 458426932566SPatrick McHardy } 45851ce85fe4SPablo Neira Ayuso rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC); 45861ce85fe4SPablo Neira Ayuso return; 45878c384bfaSThomas Graf errout: 45888c384bfaSThomas Graf if (err < 0) 45896fda7350SBenjamin Thery rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err); 45901da177e4SLinus Torvalds } 45911da177e4SLinus Torvalds 45921ec047ebSHannes Frederic Sowa static void update_valid_ll_addr_cnt(struct inet6_ifaddr *ifp, int count) 45931ec047ebSHannes Frederic Sowa { 45941ec047ebSHannes Frederic Sowa write_lock_bh(&ifp->idev->lock); 45951ec047ebSHannes Frederic Sowa spin_lock(&ifp->lock); 45961ec047ebSHannes Frederic Sowa if (((ifp->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|IFA_F_OPTIMISTIC| 45971ec047ebSHannes Frederic Sowa IFA_F_DADFAILED)) == IFA_F_PERMANENT) && 45981ec047ebSHannes Frederic Sowa (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) 45991ec047ebSHannes Frederic Sowa ifp->idev->valid_ll_addr_cnt += count; 46001ec047ebSHannes Frederic Sowa WARN_ON(ifp->idev->valid_ll_addr_cnt < 0); 46011ec047ebSHannes Frederic Sowa spin_unlock(&ifp->lock); 46021ec047ebSHannes Frederic Sowa write_unlock_bh(&ifp->idev->lock); 46031ec047ebSHannes Frederic Sowa } 46041ec047ebSHannes Frederic Sowa 46051da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 46061da177e4SLinus Torvalds { 460763998ac2SNicolas Dichtel struct net *net = dev_net(ifp->idev->dev); 460863998ac2SNicolas Dichtel 46091da177e4SLinus Torvalds inet6_ifa_notify(event ? : RTM_NEWADDR, ifp); 46101da177e4SLinus Torvalds 46111da177e4SLinus Torvalds switch (event) { 46121da177e4SLinus Torvalds case RTM_NEWADDR: 46131ec047ebSHannes Frederic Sowa update_valid_ll_addr_cnt(ifp, 1); 46141ec047ebSHannes Frederic Sowa 461595c385b4SNeil Horman /* 461695c385b4SNeil Horman * If the address was optimistic 461795c385b4SNeil Horman * we inserted the route at the start of 461895c385b4SNeil Horman * our DAD process, so we don't need 461995c385b4SNeil Horman * to do it again 462095c385b4SNeil Horman */ 462195c385b4SNeil Horman if (!(ifp->rt->rt6i_node)) 462240e22e8fSThomas Graf ip6_ins_rt(ifp->rt); 46231da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 46241da177e4SLinus Torvalds addrconf_join_anycast(ifp); 46257996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) 4626caeaba79SNicolas Dichtel addrconf_prefix_route(&ifp->peer_addr, 128, 4627caeaba79SNicolas Dichtel ifp->idev->dev, 0, 0); 46281da177e4SLinus Torvalds break; 46291da177e4SLinus Torvalds case RTM_DELADDR: 46301ec047ebSHannes Frederic Sowa update_valid_ll_addr_cnt(ifp, -1); 46311ec047ebSHannes Frederic Sowa 46321da177e4SLinus Torvalds if (ifp->idev->cnf.forwarding) 46331da177e4SLinus Torvalds addrconf_leave_anycast(ifp); 46341da177e4SLinus Torvalds addrconf_leave_solict(ifp->idev, &ifp->addr); 46357996c799SCong Wang if (!ipv6_addr_any(&ifp->peer_addr)) { 4636caeaba79SNicolas Dichtel struct rt6_info *rt; 4637caeaba79SNicolas Dichtel struct net_device *dev = ifp->idev->dev; 4638caeaba79SNicolas Dichtel 4639caeaba79SNicolas Dichtel rt = rt6_lookup(dev_net(dev), &ifp->peer_addr, NULL, 4640caeaba79SNicolas Dichtel dev->ifindex, 1); 4641caeaba79SNicolas Dichtel if (rt) { 4642caeaba79SNicolas Dichtel dst_hold(&rt->dst); 4643caeaba79SNicolas Dichtel if (ip6_del_rt(rt)) 4644caeaba79SNicolas Dichtel dst_free(&rt->dst); 4645caeaba79SNicolas Dichtel } 4646caeaba79SNicolas Dichtel } 4647d8d1f30bSChangli Gao dst_hold(&ifp->rt->dst); 464893fa159aSstephen hemminger 464973a8bd74SDavid S. Miller if (ip6_del_rt(ifp->rt)) 4650d8d1f30bSChangli Gao dst_free(&ifp->rt->dst); 46511da177e4SLinus Torvalds break; 46521da177e4SLinus Torvalds } 465363998ac2SNicolas Dichtel atomic_inc(&net->ipv6.dev_addr_genid); 46541da177e4SLinus Torvalds } 46551da177e4SLinus Torvalds 46561da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 46571da177e4SLinus Torvalds { 46588814c4b5SYOSHIFUJI Hideaki rcu_read_lock_bh(); 46591da177e4SLinus Torvalds if (likely(ifp->idev->dead == 0)) 46601da177e4SLinus Torvalds __ipv6_ifa_notify(event, ifp); 46618814c4b5SYOSHIFUJI Hideaki rcu_read_unlock_bh(); 46621da177e4SLinus Torvalds } 46631da177e4SLinus Torvalds 46641da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 46651da177e4SLinus Torvalds 46661da177e4SLinus Torvalds static 4667fe2c6338SJoe Perches int addrconf_sysctl_forward(struct ctl_table *ctl, int write, 46681da177e4SLinus Torvalds void __user *buffer, size_t *lenp, loff_t *ppos) 46691da177e4SLinus Torvalds { 46701da177e4SLinus Torvalds int *valp = ctl->data; 46711da177e4SLinus Torvalds int val = *valp; 467288af182eSEric W. Biederman loff_t pos = *ppos; 4673fe2c6338SJoe Perches struct ctl_table lctl; 46741da177e4SLinus Torvalds int ret; 46751da177e4SLinus Torvalds 4676013d97e9SFrancesco Ruggeri /* 4677013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.forwarding, we should 4678013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 4679013d97e9SFrancesco Ruggeri */ 4680013d97e9SFrancesco Ruggeri lctl = *ctl; 4681013d97e9SFrancesco Ruggeri lctl.data = &val; 4682013d97e9SFrancesco Ruggeri 4683013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 46841da177e4SLinus Torvalds 4685c8fecf22SPavel Emelyanov if (write) 4686b325fddbSStephen Hemminger ret = addrconf_fixup_forwarding(ctl, valp, val); 468788af182eSEric W. Biederman if (ret) 468888af182eSEric W. Biederman *ppos = pos; 46891da177e4SLinus Torvalds return ret; 46901da177e4SLinus Torvalds } 46911da177e4SLinus Torvalds 469256d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev) 469356d417b1SBrian Haley { 469475538c2bSCong Wang struct netdev_notifier_info info; 469575538c2bSCong Wang 469656d417b1SBrian Haley if (!idev || !idev->dev) 469756d417b1SBrian Haley return; 469856d417b1SBrian Haley 469975538c2bSCong Wang netdev_notifier_info_init(&info, idev->dev); 470056d417b1SBrian Haley if (idev->cnf.disable_ipv6) 470175538c2bSCong Wang addrconf_notify(NULL, NETDEV_DOWN, &info); 470256d417b1SBrian Haley else 470375538c2bSCong Wang addrconf_notify(NULL, NETDEV_UP, &info); 470456d417b1SBrian Haley } 470556d417b1SBrian Haley 470656d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf) 470756d417b1SBrian Haley { 470856d417b1SBrian Haley struct net_device *dev; 470956d417b1SBrian Haley struct inet6_dev *idev; 471056d417b1SBrian Haley 471156d417b1SBrian Haley rcu_read_lock(); 4712c6d14c84SEric Dumazet for_each_netdev_rcu(net, dev) { 471356d417b1SBrian Haley idev = __in6_dev_get(dev); 471456d417b1SBrian Haley if (idev) { 471556d417b1SBrian Haley int changed = (!idev->cnf.disable_ipv6) ^ (!newf); 471656d417b1SBrian Haley idev->cnf.disable_ipv6 = newf; 471756d417b1SBrian Haley if (changed) 471856d417b1SBrian Haley dev_disable_change(idev); 471956d417b1SBrian Haley } 472056d417b1SBrian Haley } 4721c6d14c84SEric Dumazet rcu_read_unlock(); 472256d417b1SBrian Haley } 472356d417b1SBrian Haley 4724013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf) 472556d417b1SBrian Haley { 472656d417b1SBrian Haley struct net *net; 4727013d97e9SFrancesco Ruggeri int old; 4728013d97e9SFrancesco Ruggeri 4729013d97e9SFrancesco Ruggeri if (!rtnl_trylock()) 4730013d97e9SFrancesco Ruggeri return restart_syscall(); 473156d417b1SBrian Haley 473256d417b1SBrian Haley net = (struct net *)table->extra2; 4733013d97e9SFrancesco Ruggeri old = *p; 4734013d97e9SFrancesco Ruggeri *p = newf; 473556d417b1SBrian Haley 4736013d97e9SFrancesco Ruggeri if (p == &net->ipv6.devconf_dflt->disable_ipv6) { 4737013d97e9SFrancesco Ruggeri rtnl_unlock(); 473856d417b1SBrian Haley return 0; 473988af182eSEric W. Biederman } 474056d417b1SBrian Haley 474156d417b1SBrian Haley if (p == &net->ipv6.devconf_all->disable_ipv6) { 474256d417b1SBrian Haley net->ipv6.devconf_dflt->disable_ipv6 = newf; 474356d417b1SBrian Haley addrconf_disable_change(net, newf); 4744013d97e9SFrancesco Ruggeri } else if ((!newf) ^ (!old)) 474556d417b1SBrian Haley dev_disable_change((struct inet6_dev *)table->extra1); 474656d417b1SBrian Haley 474756d417b1SBrian Haley rtnl_unlock(); 474856d417b1SBrian Haley return 0; 474956d417b1SBrian Haley } 475056d417b1SBrian Haley 475156d417b1SBrian Haley static 4752fe2c6338SJoe Perches int addrconf_sysctl_disable(struct ctl_table *ctl, int write, 475356d417b1SBrian Haley void __user *buffer, size_t *lenp, loff_t *ppos) 475456d417b1SBrian Haley { 475556d417b1SBrian Haley int *valp = ctl->data; 475656d417b1SBrian Haley int val = *valp; 475788af182eSEric W. Biederman loff_t pos = *ppos; 4758fe2c6338SJoe Perches struct ctl_table lctl; 475956d417b1SBrian Haley int ret; 476056d417b1SBrian Haley 4761013d97e9SFrancesco Ruggeri /* 4762013d97e9SFrancesco Ruggeri * ctl->data points to idev->cnf.disable_ipv6, we should 4763013d97e9SFrancesco Ruggeri * not modify it until we get the rtnl lock. 4764013d97e9SFrancesco Ruggeri */ 4765013d97e9SFrancesco Ruggeri lctl = *ctl; 4766013d97e9SFrancesco Ruggeri lctl.data = &val; 4767013d97e9SFrancesco Ruggeri 4768013d97e9SFrancesco Ruggeri ret = proc_dointvec(&lctl, write, buffer, lenp, ppos); 476956d417b1SBrian Haley 477056d417b1SBrian Haley if (write) 477156d417b1SBrian Haley ret = addrconf_disable_ipv6(ctl, valp, val); 477288af182eSEric W. Biederman if (ret) 477388af182eSEric W. Biederman *ppos = pos; 477456d417b1SBrian Haley return ret; 477556d417b1SBrian Haley } 477656d417b1SBrian Haley 47771da177e4SLinus Torvalds static struct addrconf_sysctl_table 47781da177e4SLinus Torvalds { 47791da177e4SLinus Torvalds struct ctl_table_header *sysctl_header; 4780fe2c6338SJoe Perches struct ctl_table addrconf_vars[DEVCONF_MAX+1]; 4781ab32ea5dSBrian Haley } addrconf_sysctl __read_mostly = { 47821da177e4SLinus Torvalds .sysctl_header = NULL, 47831da177e4SLinus Torvalds .addrconf_vars = { 47841da177e4SLinus Torvalds { 47851da177e4SLinus Torvalds .procname = "forwarding", 47861da177e4SLinus Torvalds .data = &ipv6_devconf.forwarding, 47871da177e4SLinus Torvalds .maxlen = sizeof(int), 47881da177e4SLinus Torvalds .mode = 0644, 47896d9f239aSAlexey Dobriyan .proc_handler = addrconf_sysctl_forward, 47901da177e4SLinus Torvalds }, 47911da177e4SLinus Torvalds { 47921da177e4SLinus Torvalds .procname = "hop_limit", 47931da177e4SLinus Torvalds .data = &ipv6_devconf.hop_limit, 47941da177e4SLinus Torvalds .maxlen = sizeof(int), 47951da177e4SLinus Torvalds .mode = 0644, 47961da177e4SLinus Torvalds .proc_handler = proc_dointvec, 47971da177e4SLinus Torvalds }, 47981da177e4SLinus Torvalds { 47991da177e4SLinus Torvalds .procname = "mtu", 48001da177e4SLinus Torvalds .data = &ipv6_devconf.mtu6, 48011da177e4SLinus Torvalds .maxlen = sizeof(int), 48021da177e4SLinus Torvalds .mode = 0644, 48036d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 48041da177e4SLinus Torvalds }, 48051da177e4SLinus Torvalds { 48061da177e4SLinus Torvalds .procname = "accept_ra", 48071da177e4SLinus Torvalds .data = &ipv6_devconf.accept_ra, 48081da177e4SLinus Torvalds .maxlen = sizeof(int), 48091da177e4SLinus Torvalds .mode = 0644, 48106d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 48111da177e4SLinus Torvalds }, 48121da177e4SLinus Torvalds { 48131da177e4SLinus Torvalds .procname = "accept_redirects", 48141da177e4SLinus Torvalds .data = &ipv6_devconf.accept_redirects, 48151da177e4SLinus Torvalds .maxlen = sizeof(int), 48161da177e4SLinus Torvalds .mode = 0644, 48176d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 48181da177e4SLinus Torvalds }, 48191da177e4SLinus Torvalds { 48201da177e4SLinus Torvalds .procname = "autoconf", 48211da177e4SLinus Torvalds .data = &ipv6_devconf.autoconf, 48221da177e4SLinus Torvalds .maxlen = sizeof(int), 48231da177e4SLinus Torvalds .mode = 0644, 48246d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 48251da177e4SLinus Torvalds }, 48261da177e4SLinus Torvalds { 48271da177e4SLinus Torvalds .procname = "dad_transmits", 48281da177e4SLinus Torvalds .data = &ipv6_devconf.dad_transmits, 48291da177e4SLinus Torvalds .maxlen = sizeof(int), 48301da177e4SLinus Torvalds .mode = 0644, 48316d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 48321da177e4SLinus Torvalds }, 48331da177e4SLinus Torvalds { 48341da177e4SLinus Torvalds .procname = "router_solicitations", 48351da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicits, 48361da177e4SLinus Torvalds .maxlen = sizeof(int), 48371da177e4SLinus Torvalds .mode = 0644, 48386d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 48391da177e4SLinus Torvalds }, 48401da177e4SLinus Torvalds { 48411da177e4SLinus Torvalds .procname = "router_solicitation_interval", 48421da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_interval, 48431da177e4SLinus Torvalds .maxlen = sizeof(int), 48441da177e4SLinus Torvalds .mode = 0644, 48456d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 48461da177e4SLinus Torvalds }, 48471da177e4SLinus Torvalds { 48481da177e4SLinus Torvalds .procname = "router_solicitation_delay", 48491da177e4SLinus Torvalds .data = &ipv6_devconf.rtr_solicit_delay, 48501da177e4SLinus Torvalds .maxlen = sizeof(int), 48511da177e4SLinus Torvalds .mode = 0644, 48526d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 48531da177e4SLinus Torvalds }, 48541da177e4SLinus Torvalds { 48551da177e4SLinus Torvalds .procname = "force_mld_version", 48561da177e4SLinus Torvalds .data = &ipv6_devconf.force_mld_version, 48571da177e4SLinus Torvalds .maxlen = sizeof(int), 48581da177e4SLinus Torvalds .mode = 0644, 48596d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 48601da177e4SLinus Torvalds }, 48611da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY 48621da177e4SLinus Torvalds { 48631da177e4SLinus Torvalds .procname = "use_tempaddr", 48641da177e4SLinus Torvalds .data = &ipv6_devconf.use_tempaddr, 48651da177e4SLinus Torvalds .maxlen = sizeof(int), 48661da177e4SLinus Torvalds .mode = 0644, 48676d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 48681da177e4SLinus Torvalds }, 48691da177e4SLinus Torvalds { 48701da177e4SLinus Torvalds .procname = "temp_valid_lft", 48711da177e4SLinus Torvalds .data = &ipv6_devconf.temp_valid_lft, 48721da177e4SLinus Torvalds .maxlen = sizeof(int), 48731da177e4SLinus Torvalds .mode = 0644, 48746d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 48751da177e4SLinus Torvalds }, 48761da177e4SLinus Torvalds { 48771da177e4SLinus Torvalds .procname = "temp_prefered_lft", 48781da177e4SLinus Torvalds .data = &ipv6_devconf.temp_prefered_lft, 48791da177e4SLinus Torvalds .maxlen = sizeof(int), 48801da177e4SLinus Torvalds .mode = 0644, 48816d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 48821da177e4SLinus Torvalds }, 48831da177e4SLinus Torvalds { 48841da177e4SLinus Torvalds .procname = "regen_max_retry", 48851da177e4SLinus Torvalds .data = &ipv6_devconf.regen_max_retry, 48861da177e4SLinus Torvalds .maxlen = sizeof(int), 48871da177e4SLinus Torvalds .mode = 0644, 48886d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 48891da177e4SLinus Torvalds }, 48901da177e4SLinus Torvalds { 48911da177e4SLinus Torvalds .procname = "max_desync_factor", 48921da177e4SLinus Torvalds .data = &ipv6_devconf.max_desync_factor, 48931da177e4SLinus Torvalds .maxlen = sizeof(int), 48941da177e4SLinus Torvalds .mode = 0644, 48956d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 48961da177e4SLinus Torvalds }, 48971da177e4SLinus Torvalds #endif 48981da177e4SLinus Torvalds { 48991da177e4SLinus Torvalds .procname = "max_addresses", 49001da177e4SLinus Torvalds .data = &ipv6_devconf.max_addresses, 49011da177e4SLinus Torvalds .maxlen = sizeof(int), 49021da177e4SLinus Torvalds .mode = 0644, 49036d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 49041da177e4SLinus Torvalds }, 49051da177e4SLinus Torvalds { 490665f5c7c1SYOSHIFUJI Hideaki .procname = "accept_ra_defrtr", 490765f5c7c1SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_defrtr, 490865f5c7c1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 490965f5c7c1SYOSHIFUJI Hideaki .mode = 0644, 49106d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 491165f5c7c1SYOSHIFUJI Hideaki }, 491265f5c7c1SYOSHIFUJI Hideaki { 4913c4fd30ebSYOSHIFUJI Hideaki .procname = "accept_ra_pinfo", 4914c4fd30ebSYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_pinfo, 4915c4fd30ebSYOSHIFUJI Hideaki .maxlen = sizeof(int), 4916c4fd30ebSYOSHIFUJI Hideaki .mode = 0644, 49176d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 4918c4fd30ebSYOSHIFUJI Hideaki }, 4919930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF 4920930d6ff2SYOSHIFUJI Hideaki { 4921930d6ff2SYOSHIFUJI Hideaki .procname = "accept_ra_rtr_pref", 4922930d6ff2SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rtr_pref, 4923930d6ff2SYOSHIFUJI Hideaki .maxlen = sizeof(int), 4924930d6ff2SYOSHIFUJI Hideaki .mode = 0644, 49256d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 4926930d6ff2SYOSHIFUJI Hideaki }, 492752e16356SYOSHIFUJI Hideaki { 492852e16356SYOSHIFUJI Hideaki .procname = "router_probe_interval", 492952e16356SYOSHIFUJI Hideaki .data = &ipv6_devconf.rtr_probe_interval, 493052e16356SYOSHIFUJI Hideaki .maxlen = sizeof(int), 493152e16356SYOSHIFUJI Hideaki .mode = 0644, 49326d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 493352e16356SYOSHIFUJI Hideaki }, 4934fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO 493509c884d4SYOSHIFUJI Hideaki { 493609c884d4SYOSHIFUJI Hideaki .procname = "accept_ra_rt_info_max_plen", 493709c884d4SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_ra_rt_info_max_plen, 493809c884d4SYOSHIFUJI Hideaki .maxlen = sizeof(int), 493909c884d4SYOSHIFUJI Hideaki .mode = 0644, 49406d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 494109c884d4SYOSHIFUJI Hideaki }, 494209c884d4SYOSHIFUJI Hideaki #endif 4943930d6ff2SYOSHIFUJI Hideaki #endif 4944c4fd30ebSYOSHIFUJI Hideaki { 4945fbea49e1SYOSHIFUJI Hideaki .procname = "proxy_ndp", 4946fbea49e1SYOSHIFUJI Hideaki .data = &ipv6_devconf.proxy_ndp, 4947fbea49e1SYOSHIFUJI Hideaki .maxlen = sizeof(int), 4948fbea49e1SYOSHIFUJI Hideaki .mode = 0644, 49496d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 4950fbea49e1SYOSHIFUJI Hideaki }, 4951fbea49e1SYOSHIFUJI Hideaki { 49520bcbc926SYOSHIFUJI Hideaki .procname = "accept_source_route", 49530bcbc926SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_source_route, 49540bcbc926SYOSHIFUJI Hideaki .maxlen = sizeof(int), 49550bcbc926SYOSHIFUJI Hideaki .mode = 0644, 49566d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 49570bcbc926SYOSHIFUJI Hideaki }, 495895c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD 495995c385b4SNeil Horman { 496095c385b4SNeil Horman .procname = "optimistic_dad", 496195c385b4SNeil Horman .data = &ipv6_devconf.optimistic_dad, 496295c385b4SNeil Horman .maxlen = sizeof(int), 496395c385b4SNeil Horman .mode = 0644, 49646d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 496595c385b4SNeil Horman 496695c385b4SNeil Horman }, 496795c385b4SNeil Horman #endif 49687bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE 49697bc570c8SYOSHIFUJI Hideaki { 49707bc570c8SYOSHIFUJI Hideaki .procname = "mc_forwarding", 49717bc570c8SYOSHIFUJI Hideaki .data = &ipv6_devconf.mc_forwarding, 49727bc570c8SYOSHIFUJI Hideaki .maxlen = sizeof(int), 4973a4e6db07SDavid S. Miller .mode = 0444, 49746d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 49757bc570c8SYOSHIFUJI Hideaki }, 49767bc570c8SYOSHIFUJI Hideaki #endif 49770bcbc926SYOSHIFUJI Hideaki { 4978778d80beSYOSHIFUJI Hideaki .procname = "disable_ipv6", 4979778d80beSYOSHIFUJI Hideaki .data = &ipv6_devconf.disable_ipv6, 4980778d80beSYOSHIFUJI Hideaki .maxlen = sizeof(int), 4981778d80beSYOSHIFUJI Hideaki .mode = 0644, 498256d417b1SBrian Haley .proc_handler = addrconf_sysctl_disable, 4983778d80beSYOSHIFUJI Hideaki }, 4984778d80beSYOSHIFUJI Hideaki { 49851b34be74SYOSHIFUJI Hideaki .procname = "accept_dad", 49861b34be74SYOSHIFUJI Hideaki .data = &ipv6_devconf.accept_dad, 49871b34be74SYOSHIFUJI Hideaki .maxlen = sizeof(int), 49881b34be74SYOSHIFUJI Hideaki .mode = 0644, 49896d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec, 49901b34be74SYOSHIFUJI Hideaki }, 49911b34be74SYOSHIFUJI Hideaki { 4992f7734fdfSOctavian Purdila .procname = "force_tllao", 4993f7734fdfSOctavian Purdila .data = &ipv6_devconf.force_tllao, 4994f7734fdfSOctavian Purdila .maxlen = sizeof(int), 4995f7734fdfSOctavian Purdila .mode = 0644, 4996f7734fdfSOctavian Purdila .proc_handler = proc_dointvec 4997f7734fdfSOctavian Purdila }, 4998f7734fdfSOctavian Purdila { 49995cb04436SHannes Frederic Sowa .procname = "ndisc_notify", 50005cb04436SHannes Frederic Sowa .data = &ipv6_devconf.ndisc_notify, 50015cb04436SHannes Frederic Sowa .maxlen = sizeof(int), 50025cb04436SHannes Frederic Sowa .mode = 0644, 50035cb04436SHannes Frederic Sowa .proc_handler = proc_dointvec 50045cb04436SHannes Frederic Sowa }, 50055cb04436SHannes Frederic Sowa { 5006f8572d8fSEric W. Biederman /* sentinel */ 50071da177e4SLinus Torvalds } 50081da177e4SLinus Torvalds }, 50091da177e4SLinus Torvalds }; 50101da177e4SLinus Torvalds 5011bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name, 5012f8572d8fSEric W. Biederman struct inet6_dev *idev, struct ipv6_devconf *p) 50131da177e4SLinus Torvalds { 50141da177e4SLinus Torvalds int i; 50151da177e4SLinus Torvalds struct addrconf_sysctl_table *t; 50166105e293SEric W. Biederman char path[sizeof("net/ipv6/conf/") + IFNAMSIZ]; 50171dab6222SPavel Emelyanov 5018af879cc7SArnaldo Carvalho de Melo t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL); 50191da177e4SLinus Torvalds if (t == NULL) 5020f68635e6SPavel Emelyanov goto out; 5021f68635e6SPavel Emelyanov 50221da177e4SLinus Torvalds for (i = 0; t->addrconf_vars[i].data; i++) { 50231da177e4SLinus Torvalds t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf; 50241da177e4SLinus Torvalds t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */ 5025bff16c2fSPavel Emelyanov t->addrconf_vars[i].extra2 = net; 50261da177e4SLinus Torvalds } 50271da177e4SLinus Torvalds 50286105e293SEric W. Biederman snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name); 50291da177e4SLinus Torvalds 50306105e293SEric W. Biederman t->sysctl_header = register_net_sysctl(net, path, t->addrconf_vars); 50311da177e4SLinus Torvalds if (t->sysctl_header == NULL) 50326105e293SEric W. Biederman goto free; 5033f68635e6SPavel Emelyanov 50341da177e4SLinus Torvalds p->sysctl = t; 503595897312SPavel Emelyanov return 0; 50361da177e4SLinus Torvalds 50371da177e4SLinus Torvalds free: 50381da177e4SLinus Torvalds kfree(t); 5039f68635e6SPavel Emelyanov out: 504095897312SPavel Emelyanov return -ENOBUFS; 50411da177e4SLinus Torvalds } 50421da177e4SLinus Torvalds 5043408c4768SPavel Emelyanov static void __addrconf_sysctl_unregister(struct ipv6_devconf *p) 5044f52295a9SPavel Emelyanov { 5045408c4768SPavel Emelyanov struct addrconf_sysctl_table *t; 5046f52295a9SPavel Emelyanov 5047408c4768SPavel Emelyanov if (p->sysctl == NULL) 5048408c4768SPavel Emelyanov return; 5049408c4768SPavel Emelyanov 5050408c4768SPavel Emelyanov t = p->sysctl; 50511da177e4SLinus Torvalds p->sysctl = NULL; 5052ff538818SLucian Adrian Grijincu unregister_net_sysctl_table(t->sysctl_header); 50531da177e4SLinus Torvalds kfree(t); 50541da177e4SLinus Torvalds } 5055408c4768SPavel Emelyanov 5056408c4768SPavel Emelyanov static void addrconf_sysctl_register(struct inet6_dev *idev) 5057408c4768SPavel Emelyanov { 505854716e3bSEric W. Biederman neigh_sysctl_register(idev->dev, idev->nd_parms, "ipv6", 5059f8572d8fSEric W. Biederman &ndisc_ifinfo_sysctl_change); 5060c346dca1SYOSHIFUJI Hideaki __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name, 5061f8572d8fSEric W. Biederman idev, &idev->cnf); 5062408c4768SPavel Emelyanov } 5063408c4768SPavel Emelyanov 5064408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev) 5065408c4768SPavel Emelyanov { 5066408c4768SPavel Emelyanov __addrconf_sysctl_unregister(&idev->cnf); 5067408c4768SPavel Emelyanov neigh_sysctl_unregister(idev->nd_parms); 50681da177e4SLinus Torvalds } 50691da177e4SLinus Torvalds 50701da177e4SLinus Torvalds 50711da177e4SLinus Torvalds #endif 50721da177e4SLinus Torvalds 50732c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net) 5074e0da5a48SPavel Emelyanov { 5075a79ca223SHong Zhiguo int err = -ENOMEM; 5076e0da5a48SPavel Emelyanov struct ipv6_devconf *all, *dflt; 5077e0da5a48SPavel Emelyanov 5078a79ca223SHong Zhiguo all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL); 5079e0da5a48SPavel Emelyanov if (all == NULL) 5080e0da5a48SPavel Emelyanov goto err_alloc_all; 5081e0da5a48SPavel Emelyanov 5082a79ca223SHong Zhiguo dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); 5083e0da5a48SPavel Emelyanov if (dflt == NULL) 5084e0da5a48SPavel Emelyanov goto err_alloc_dflt; 5085a79ca223SHong Zhiguo 508656d417b1SBrian Haley /* these will be inherited by all namespaces */ 508756d417b1SBrian Haley dflt->autoconf = ipv6_defaults.autoconf; 508856d417b1SBrian Haley dflt->disable_ipv6 = ipv6_defaults.disable_ipv6; 5089e0da5a48SPavel Emelyanov 5090e0da5a48SPavel Emelyanov net->ipv6.devconf_all = all; 5091e0da5a48SPavel Emelyanov net->ipv6.devconf_dflt = dflt; 5092e0da5a48SPavel Emelyanov 5093e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 5094f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "all", NULL, all); 5095e0da5a48SPavel Emelyanov if (err < 0) 5096e0da5a48SPavel Emelyanov goto err_reg_all; 5097e0da5a48SPavel Emelyanov 5098f8572d8fSEric W. Biederman err = __addrconf_sysctl_register(net, "default", NULL, dflt); 5099e0da5a48SPavel Emelyanov if (err < 0) 5100e0da5a48SPavel Emelyanov goto err_reg_dflt; 5101e0da5a48SPavel Emelyanov #endif 5102e0da5a48SPavel Emelyanov return 0; 5103e0da5a48SPavel Emelyanov 5104e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 5105e0da5a48SPavel Emelyanov err_reg_dflt: 5106e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(all); 5107e0da5a48SPavel Emelyanov err_reg_all: 5108e0da5a48SPavel Emelyanov kfree(dflt); 5109e0da5a48SPavel Emelyanov #endif 5110e0da5a48SPavel Emelyanov err_alloc_dflt: 5111e0da5a48SPavel Emelyanov kfree(all); 5112e0da5a48SPavel Emelyanov err_alloc_all: 5113e0da5a48SPavel Emelyanov return err; 5114e0da5a48SPavel Emelyanov } 5115e0da5a48SPavel Emelyanov 51162c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net) 5117e0da5a48SPavel Emelyanov { 5118e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL 5119e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(net->ipv6.devconf_dflt); 5120e0da5a48SPavel Emelyanov __addrconf_sysctl_unregister(net->ipv6.devconf_all); 5121e0da5a48SPavel Emelyanov #endif 512209ad9bc7SOctavian Purdila if (!net_eq(net, &init_net)) { 5123e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_dflt); 5124e0da5a48SPavel Emelyanov kfree(net->ipv6.devconf_all); 5125e0da5a48SPavel Emelyanov } 5126e0da5a48SPavel Emelyanov } 5127e0da5a48SPavel Emelyanov 5128e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = { 5129e0da5a48SPavel Emelyanov .init = addrconf_init_net, 5130e0da5a48SPavel Emelyanov .exit = addrconf_exit_net, 5131e0da5a48SPavel Emelyanov }; 5132e0da5a48SPavel Emelyanov 5133b382b191SThomas Graf static struct rtnl_af_ops inet6_ops = { 5134b382b191SThomas Graf .family = AF_INET6, 5135b382b191SThomas Graf .fill_link_af = inet6_fill_link_af, 5136b382b191SThomas Graf .get_link_af_size = inet6_get_link_af_size, 5137f53adae4SDaniel Borkmann .set_link_af = inet6_set_link_af, 5138b382b191SThomas Graf }; 5139b382b191SThomas Graf 51401da177e4SLinus Torvalds /* 51411da177e4SLinus Torvalds * Init / cleanup code 51421da177e4SLinus Torvalds */ 51431da177e4SLinus Torvalds 51441da177e4SLinus Torvalds int __init addrconf_init(void) 51451da177e4SLinus Torvalds { 5146c2e21293Sstephen hemminger int i, err; 51472a8cc6c8SYOSHIFUJI Hideaki 5148e21e8467SStephen Hemminger err = ipv6_addr_label_init(); 5149e21e8467SStephen Hemminger if (err < 0) { 5150f3213831SJoe Perches pr_crit("%s: cannot initialize default policy table: %d\n", 5151f3213831SJoe Perches __func__, err); 51522cc6d2bfSNeil Horman goto out; 51532a8cc6c8SYOSHIFUJI Hideaki } 51541da177e4SLinus Torvalds 51552cc6d2bfSNeil Horman err = register_pernet_subsys(&addrconf_ops); 51562cc6d2bfSNeil Horman if (err < 0) 51572cc6d2bfSNeil Horman goto out_addrlabel; 5158e0da5a48SPavel Emelyanov 51591da177e4SLinus Torvalds /* The addrconf netdev notifier requires that loopback_dev 51601da177e4SLinus Torvalds * has it's ipv6 private information allocated and setup 51611da177e4SLinus Torvalds * before it can bring up and give link-local addresses 51621da177e4SLinus Torvalds * to other devices which are up. 51631da177e4SLinus Torvalds * 51641da177e4SLinus Torvalds * Unfortunately, loopback_dev is not necessarily the first 51651da177e4SLinus Torvalds * entry in the global dev_base list of net devices. In fact, 51661da177e4SLinus Torvalds * it is likely to be the very last entry on that list. 51671da177e4SLinus Torvalds * So this causes the notifier registry below to try and 51681da177e4SLinus Torvalds * give link-local addresses to all devices besides loopback_dev 51691da177e4SLinus Torvalds * first, then loopback_dev, which cases all the non-loopback_dev 51701da177e4SLinus Torvalds * devices to fail to get a link-local address. 51711da177e4SLinus Torvalds * 51721da177e4SLinus Torvalds * So, as a temporary fix, allocate the ipv6 structure for 51731da177e4SLinus Torvalds * loopback_dev first by hand. 51741da177e4SLinus Torvalds * Longer term, all of the dependencies ipv6 has upon the loopback 51751da177e4SLinus Torvalds * device and it being up should be removed. 51761da177e4SLinus Torvalds */ 51771da177e4SLinus Torvalds rtnl_lock(); 51782774c7abSEric W. Biederman if (!ipv6_add_dev(init_net.loopback_dev)) 51791da177e4SLinus Torvalds err = -ENOMEM; 51801da177e4SLinus Torvalds rtnl_unlock(); 51811da177e4SLinus Torvalds if (err) 5182e0da5a48SPavel Emelyanov goto errlo; 51831da177e4SLinus Torvalds 5184c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 5185c2e21293Sstephen hemminger INIT_HLIST_HEAD(&inet6_addr_lst[i]); 5186c2e21293Sstephen hemminger 51871da177e4SLinus Torvalds register_netdevice_notifier(&ipv6_dev_notf); 51881da177e4SLinus Torvalds 51891da177e4SLinus Torvalds addrconf_verify(0); 5190c127ea2cSThomas Graf 5191b382b191SThomas Graf err = rtnl_af_register(&inet6_ops); 5192b382b191SThomas Graf if (err < 0) 5193b382b191SThomas Graf goto errout_af; 5194b382b191SThomas Graf 5195c7ac8679SGreg Rose err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo, 5196c7ac8679SGreg Rose NULL); 5197c127ea2cSThomas Graf if (err < 0) 5198c127ea2cSThomas Graf goto errout; 5199c127ea2cSThomas Graf 5200c127ea2cSThomas Graf /* Only the first call to __rtnl_register can fail */ 5201c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL); 5202c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL); 5203c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, 5204c7ac8679SGreg Rose inet6_dump_ifaddr, NULL); 5205c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, 5206c7ac8679SGreg Rose inet6_dump_ifmcaddr, NULL); 5207c7ac8679SGreg Rose __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, 5208c7ac8679SGreg Rose inet6_dump_ifacaddr, NULL); 520976f8f6cbSNicolas Dichtel __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf, 52107a674200SNicolas Dichtel inet6_netconf_dump_devconf, NULL); 5211c127ea2cSThomas Graf 52122a8cc6c8SYOSHIFUJI Hideaki ipv6_addr_label_rtnl_register(); 52132a8cc6c8SYOSHIFUJI Hideaki 52141da177e4SLinus Torvalds return 0; 5215c127ea2cSThomas Graf errout: 5216b382b191SThomas Graf rtnl_af_unregister(&inet6_ops); 5217b382b191SThomas Graf errout_af: 5218c127ea2cSThomas Graf unregister_netdevice_notifier(&ipv6_dev_notf); 5219e0da5a48SPavel Emelyanov errlo: 5220e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 52212cc6d2bfSNeil Horman out_addrlabel: 52222cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 52232cc6d2bfSNeil Horman out: 5224c127ea2cSThomas Graf return err; 52251da177e4SLinus Torvalds } 52261da177e4SLinus Torvalds 522709f7709fSDaniel Lezcano void addrconf_cleanup(void) 52281da177e4SLinus Torvalds { 5229176c39afSDaniel Lezcano struct net_device *dev; 52301da177e4SLinus Torvalds int i; 52311da177e4SLinus Torvalds 52321da177e4SLinus Torvalds unregister_netdevice_notifier(&ipv6_dev_notf); 5233e0da5a48SPavel Emelyanov unregister_pernet_subsys(&addrconf_ops); 52342cc6d2bfSNeil Horman ipv6_addr_label_cleanup(); 52351da177e4SLinus Torvalds 52361da177e4SLinus Torvalds rtnl_lock(); 52371da177e4SLinus Torvalds 5238b382b191SThomas Graf __rtnl_af_unregister(&inet6_ops); 5239b382b191SThomas Graf 5240176c39afSDaniel Lezcano /* clean dev list */ 5241176c39afSDaniel Lezcano for_each_netdev(&init_net, dev) { 5242176c39afSDaniel Lezcano if (__in6_dev_get(dev) == NULL) 5243176c39afSDaniel Lezcano continue; 5244176c39afSDaniel Lezcano addrconf_ifdown(dev, 1); 5245176c39afSDaniel Lezcano } 5246176c39afSDaniel Lezcano addrconf_ifdown(init_net.loopback_dev, 2); 5247176c39afSDaniel Lezcano 52481da177e4SLinus Torvalds /* 52491da177e4SLinus Torvalds * Check hash table. 52501da177e4SLinus Torvalds */ 52515c578aedSstephen hemminger spin_lock_bh(&addrconf_hash_lock); 5252c2e21293Sstephen hemminger for (i = 0; i < IN6_ADDR_HSIZE; i++) 5253c2e21293Sstephen hemminger WARN_ON(!hlist_empty(&inet6_addr_lst[i])); 52545c578aedSstephen hemminger spin_unlock_bh(&addrconf_hash_lock); 52551da177e4SLinus Torvalds 52561da177e4SLinus Torvalds del_timer(&addr_chk_timer); 52571da177e4SLinus Torvalds rtnl_unlock(); 52581da177e4SLinus Torvalds } 5259