xref: /openbmc/linux/net/ipv6/addrconf.c (revision 4b7a4274ca63dadd9c4f17fc953f3a5d19855c4c)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	IPv6 Address [auto]configuration
31da177e4SLinus Torvalds  *	Linux INET6 implementation
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *	Authors:
61da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
71da177e4SLinus Torvalds  *	Alexey Kuznetsov	<kuznet@ms2.inr.ac.ru>
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
101da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
111da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
121da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds /*
161da177e4SLinus Torvalds  *	Changes:
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  *	Janos Farkas			:	delete timer on ifdown
191da177e4SLinus Torvalds  *	<chexum@bankinf.banki.hu>
201da177e4SLinus Torvalds  *	Andi Kleen			:	kill double kfree on module
211da177e4SLinus Torvalds  *						unload.
221da177e4SLinus Torvalds  *	Maciej W. Rozycki		:	FDDI support
231da177e4SLinus Torvalds  *	sekiya@USAGI			:	Don't send too many RS
241da177e4SLinus Torvalds  *						packets.
251da177e4SLinus Torvalds  *	yoshfuji@USAGI			:       Fixed interval between DAD
261da177e4SLinus Torvalds  *						packets.
271da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	improved accuracy of
281da177e4SLinus Torvalds  *						address validation timer.
291da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	Privacy Extensions (RFC3041)
301da177e4SLinus Torvalds  *						support.
311da177e4SLinus Torvalds  *	Yuji SEKIYA @USAGI		:	Don't assign a same IPv6
321da177e4SLinus Torvalds  *						address on a same interface.
331da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	ARCnet support
341da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	convert /proc/net/if_inet6 to
351da177e4SLinus Torvalds  *						seq_file.
36b1cacb68SYOSHIFUJI Hideaki  *	YOSHIFUJI Hideaki @USAGI	:	improved source address
37b1cacb68SYOSHIFUJI Hideaki  *						selection; consider scope,
38b1cacb68SYOSHIFUJI Hideaki  *						status etc.
391da177e4SLinus Torvalds  */
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #include <linux/errno.h>
421da177e4SLinus Torvalds #include <linux/types.h>
431da177e4SLinus Torvalds #include <linux/socket.h>
441da177e4SLinus Torvalds #include <linux/sockios.h>
451da177e4SLinus Torvalds #include <linux/net.h>
461da177e4SLinus Torvalds #include <linux/in6.h>
471da177e4SLinus Torvalds #include <linux/netdevice.h>
481823730fSThomas Graf #include <linux/if_addr.h>
491da177e4SLinus Torvalds #include <linux/if_arp.h>
501da177e4SLinus Torvalds #include <linux/if_arcnet.h>
511da177e4SLinus Torvalds #include <linux/if_infiniband.h>
521da177e4SLinus Torvalds #include <linux/route.h>
531da177e4SLinus Torvalds #include <linux/inetdevice.h>
541da177e4SLinus Torvalds #include <linux/init.h>
551da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
561da177e4SLinus Torvalds #include <linux/sysctl.h>
571da177e4SLinus Torvalds #endif
584fc268d2SRandy Dunlap #include <linux/capability.h>
591da177e4SLinus Torvalds #include <linux/delay.h>
601da177e4SLinus Torvalds #include <linux/notifier.h>
61543537bdSPaulo Marques #include <linux/string.h>
621da177e4SLinus Torvalds 
63457c4cbcSEric W. Biederman #include <net/net_namespace.h>
641da177e4SLinus Torvalds #include <net/sock.h>
651da177e4SLinus Torvalds #include <net/snmp.h>
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds #include <net/ipv6.h>
681da177e4SLinus Torvalds #include <net/protocol.h>
691da177e4SLinus Torvalds #include <net/ndisc.h>
701da177e4SLinus Torvalds #include <net/ip6_route.h>
711da177e4SLinus Torvalds #include <net/addrconf.h>
721da177e4SLinus Torvalds #include <net/tcp.h>
731da177e4SLinus Torvalds #include <net/ip.h>
745d620266SThomas Graf #include <net/netlink.h>
75f24e3d65SMitsuru Chinen #include <net/pkt_sched.h>
761da177e4SLinus Torvalds #include <linux/if_tunnel.h>
771da177e4SLinus Torvalds #include <linux/rtnetlink.h>
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
801da177e4SLinus Torvalds #include <linux/random.h>
811da177e4SLinus Torvalds #endif
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds #include <asm/uaccess.h>
847f7d9a6bSHerbert Xu #include <asm/unaligned.h>
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds #include <linux/proc_fs.h>
871da177e4SLinus Torvalds #include <linux/seq_file.h>
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds /* Set to 3 to get tracing... */
901da177e4SLinus Torvalds #define ACONF_DEBUG 2
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds #if ACONF_DEBUG >= 3
931da177e4SLinus Torvalds #define ADBG(x) printk x
941da177e4SLinus Torvalds #else
951da177e4SLinus Torvalds #define ADBG(x)
961da177e4SLinus Torvalds #endif
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds #define	INFINITY_LIFE_TIME	0xFFFFFFFF
991da177e4SLinus Torvalds #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
102f52295a9SPavel Emelyanov static void addrconf_sysctl_register(struct inet6_dev *idev);
103408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev);
104408c4768SPavel Emelyanov #else
105408c4768SPavel Emelyanov static inline void addrconf_sysctl_register(struct inet6_dev *idev)
106408c4768SPavel Emelyanov {
107408c4768SPavel Emelyanov }
108408c4768SPavel Emelyanov 
109408c4768SPavel Emelyanov static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
110408c4768SPavel Emelyanov {
111408c4768SPavel Emelyanov }
1121da177e4SLinus Torvalds #endif
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
1151da177e4SLinus Torvalds static int __ipv6_regen_rndid(struct inet6_dev *idev);
1161da177e4SLinus Torvalds static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
1171da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data);
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds static int desync_factor = MAX_DESYNC_FACTOR * HZ;
1201da177e4SLinus Torvalds #endif
1211da177e4SLinus Torvalds 
1221b34be74SYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
1231da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev);
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds /*
1261da177e4SLinus Torvalds  *	Configured unicast address hash table
1271da177e4SLinus Torvalds  */
1281da177e4SLinus Torvalds static struct inet6_ifaddr		*inet6_addr_lst[IN6_ADDR_HSIZE];
1291da177e4SLinus Torvalds static DEFINE_RWLOCK(addrconf_hash_lock);
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds static void addrconf_verify(unsigned long);
1321da177e4SLinus Torvalds 
1338d06afabSIngo Molnar static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
1341da177e4SLinus Torvalds static DEFINE_SPINLOCK(addrconf_verify_lock);
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
1371da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how);
1401da177e4SLinus Torvalds 
141e431b8c0SJamal Hadi Salim static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
1421da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data);
1431da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
144c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev);
1451da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data);
1461da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1471da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
1501da177e4SLinus Torvalds 				struct prefix_info *pinfo);
15106bfe655SDaniel Lezcano static int ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
15206bfe655SDaniel Lezcano 			      struct net_device *dev);
1531da177e4SLinus Torvalds 
154e041c683SAlan Stern static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
1551da177e4SLinus Torvalds 
156888c848eSAdrian Bunk static struct ipv6_devconf ipv6_devconf __read_mostly = {
1571da177e4SLinus Torvalds 	.forwarding		= 0,
1581da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
1591da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
1601da177e4SLinus Torvalds 	.accept_ra		= 1,
1611da177e4SLinus Torvalds 	.accept_redirects	= 1,
1621da177e4SLinus Torvalds 	.autoconf		= 1,
1631da177e4SLinus Torvalds 	.force_mld_version	= 0,
1641da177e4SLinus Torvalds 	.dad_transmits		= 1,
1651da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
1661da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
1671da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
1681da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
1691da177e4SLinus Torvalds 	.use_tempaddr 		= 0,
1701da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
1711da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
1721da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
1731da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
1741da177e4SLinus Torvalds #endif
1751da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
17665f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
177c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
178930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
179930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
18052e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
18109c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
18209c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
18309c884d4SYOSHIFUJI Hideaki #endif
184930d6ff2SYOSHIFUJI Hideaki #endif
185fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
1860bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
187778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
1881b34be74SYOSHIFUJI Hideaki 	.accept_dad		= 1,
1891da177e4SLinus Torvalds };
1901da177e4SLinus Torvalds 
191ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
1921da177e4SLinus Torvalds 	.forwarding		= 0,
1931da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
1941da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
1951da177e4SLinus Torvalds 	.accept_ra		= 1,
1961da177e4SLinus Torvalds 	.accept_redirects	= 1,
1971da177e4SLinus Torvalds 	.autoconf		= 1,
1981da177e4SLinus Torvalds 	.dad_transmits		= 1,
1991da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
2001da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
2011da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
2021da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
2031da177e4SLinus Torvalds 	.use_tempaddr		= 0,
2041da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
2051da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
2061da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
2071da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
2081da177e4SLinus Torvalds #endif
2091da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
21065f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
211c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
212930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
213930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
21452e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
21509c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
21609c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
21709c884d4SYOSHIFUJI Hideaki #endif
218930d6ff2SYOSHIFUJI Hideaki #endif
219fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2200bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
221778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
2221b34be74SYOSHIFUJI Hideaki 	.accept_dad		= 1,
2231da177e4SLinus Torvalds };
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
2261da177e4SLinus Torvalds const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
2271da177e4SLinus Torvalds const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
228f3ee4010SYOSHIFUJI Hideaki const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
229f3ee4010SYOSHIFUJI Hideaki const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
2301da177e4SLinus Torvalds 
231f24e3d65SMitsuru Chinen /* Check if a valid qdisc is available */
23205297949SDavid S. Miller static inline bool addrconf_qdisc_ok(const struct net_device *dev)
233f24e3d65SMitsuru Chinen {
23405297949SDavid S. Miller 	return !qdisc_tx_is_noop(dev);
235f24e3d65SMitsuru Chinen }
236f24e3d65SMitsuru Chinen 
2372b5ead46SYOSHIFUJI Hideaki /* Check if a route is valid prefix route */
2382b5ead46SYOSHIFUJI Hideaki static inline int addrconf_is_prefix_route(const struct rt6_info *rt)
2392b5ead46SYOSHIFUJI Hideaki {
2402b5ead46SYOSHIFUJI Hideaki 	return ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0);
2412b5ead46SYOSHIFUJI Hideaki }
2422b5ead46SYOSHIFUJI Hideaki 
2431da177e4SLinus Torvalds static void addrconf_del_timer(struct inet6_ifaddr *ifp)
2441da177e4SLinus Torvalds {
2451da177e4SLinus Torvalds 	if (del_timer(&ifp->timer))
2461da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds enum addrconf_timer_t
2501da177e4SLinus Torvalds {
2511da177e4SLinus Torvalds 	AC_NONE,
2521da177e4SLinus Torvalds 	AC_DAD,
2531da177e4SLinus Torvalds 	AC_RS,
2541da177e4SLinus Torvalds };
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
2571da177e4SLinus Torvalds 			       enum addrconf_timer_t what,
2581da177e4SLinus Torvalds 			       unsigned long when)
2591da177e4SLinus Torvalds {
2601da177e4SLinus Torvalds 	if (!del_timer(&ifp->timer))
2611da177e4SLinus Torvalds 		in6_ifa_hold(ifp);
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	switch (what) {
2641da177e4SLinus Torvalds 	case AC_DAD:
2651da177e4SLinus Torvalds 		ifp->timer.function = addrconf_dad_timer;
2661da177e4SLinus Torvalds 		break;
2671da177e4SLinus Torvalds 	case AC_RS:
2681da177e4SLinus Torvalds 		ifp->timer.function = addrconf_rs_timer;
2691da177e4SLinus Torvalds 		break;
2701da177e4SLinus Torvalds 	default:;
2711da177e4SLinus Torvalds 	}
2721da177e4SLinus Torvalds 	ifp->timer.expires = jiffies + when;
2731da177e4SLinus Torvalds 	add_timer(&ifp->timer);
2741da177e4SLinus Torvalds }
2751da177e4SLinus Torvalds 
2767f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev)
2777f7d9a6bSHerbert Xu {
2787f7d9a6bSHerbert Xu 	if (snmp_mib_init((void **)idev->stats.ipv6,
279c69bce20SYOSHIFUJI Hideaki 			  sizeof(struct ipstats_mib)) < 0)
2807f7d9a6bSHerbert Xu 		goto err_ip;
2817f7d9a6bSHerbert Xu 	if (snmp_mib_init((void **)idev->stats.icmpv6,
282c69bce20SYOSHIFUJI Hideaki 			  sizeof(struct icmpv6_mib)) < 0)
2837f7d9a6bSHerbert Xu 		goto err_icmp;
28414878f75SDavid L Stevens 	if (snmp_mib_init((void **)idev->stats.icmpv6msg,
285c69bce20SYOSHIFUJI Hideaki 			  sizeof(struct icmpv6msg_mib)) < 0)
28614878f75SDavid L Stevens 		goto err_icmpmsg;
2877f7d9a6bSHerbert Xu 
2887f7d9a6bSHerbert Xu 	return 0;
2897f7d9a6bSHerbert Xu 
29014878f75SDavid L Stevens err_icmpmsg:
29114878f75SDavid L Stevens 	snmp_mib_free((void **)idev->stats.icmpv6);
2927f7d9a6bSHerbert Xu err_icmp:
2937f7d9a6bSHerbert Xu 	snmp_mib_free((void **)idev->stats.ipv6);
2947f7d9a6bSHerbert Xu err_ip:
295aaf70ec7SPavel Emelyanov 	return -ENOMEM;
2967f7d9a6bSHerbert Xu }
2977f7d9a6bSHerbert Xu 
29816910b98SPavel Emelyanov static void snmp6_free_dev(struct inet6_dev *idev)
2997f7d9a6bSHerbert Xu {
30014878f75SDavid L Stevens 	snmp_mib_free((void **)idev->stats.icmpv6msg);
3017f7d9a6bSHerbert Xu 	snmp_mib_free((void **)idev->stats.icmpv6);
3027f7d9a6bSHerbert Xu 	snmp_mib_free((void **)idev->stats.ipv6);
3037f7d9a6bSHerbert Xu }
3047f7d9a6bSHerbert Xu 
3051da177e4SLinus Torvalds /* Nobody refers to this device, we may destroy it. */
3061da177e4SLinus Torvalds 
3078814c4b5SYOSHIFUJI Hideaki static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
3088814c4b5SYOSHIFUJI Hideaki {
3098814c4b5SYOSHIFUJI Hideaki 	struct inet6_dev *idev = container_of(head, struct inet6_dev, rcu);
3108814c4b5SYOSHIFUJI Hideaki 	kfree(idev);
3118814c4b5SYOSHIFUJI Hideaki }
3128814c4b5SYOSHIFUJI Hideaki 
3131da177e4SLinus Torvalds void in6_dev_finish_destroy(struct inet6_dev *idev)
3141da177e4SLinus Torvalds {
3151da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
316547b792cSIlpo Järvinen 
317547b792cSIlpo Järvinen 	WARN_ON(idev->addr_list != NULL);
318547b792cSIlpo Järvinen 	WARN_ON(idev->mc_list != NULL);
319547b792cSIlpo Järvinen 
3201da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
3211da177e4SLinus Torvalds 	printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
3221da177e4SLinus Torvalds #endif
3231da177e4SLinus Torvalds 	dev_put(dev);
3241da177e4SLinus Torvalds 	if (!idev->dead) {
3251da177e4SLinus Torvalds 		printk("Freeing alive inet6 device %p\n", idev);
3261da177e4SLinus Torvalds 		return;
3271da177e4SLinus Torvalds 	}
3281da177e4SLinus Torvalds 	snmp6_free_dev(idev);
3298814c4b5SYOSHIFUJI Hideaki 	call_rcu(&idev->rcu, in6_dev_finish_destroy_rcu);
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds 
3327159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(in6_dev_finish_destroy);
3337159039aSYOSHIFUJI Hideaki 
3341da177e4SLinus Torvalds static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
3351da177e4SLinus Torvalds {
3361da177e4SLinus Torvalds 	struct inet6_dev *ndev;
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 	ASSERT_RTNL();
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	if (dev->mtu < IPV6_MIN_MTU)
3411da177e4SLinus Torvalds 		return NULL;
3421da177e4SLinus Torvalds 
343322f74a4SIngo Oeser 	ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
3441da177e4SLinus Torvalds 
345322f74a4SIngo Oeser 	if (ndev == NULL)
346322f74a4SIngo Oeser 		return NULL;
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 	rwlock_init(&ndev->lock);
3491da177e4SLinus Torvalds 	ndev->dev = dev;
350c346dca1SYOSHIFUJI Hideaki 	memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
3511da177e4SLinus Torvalds 	ndev->cnf.mtu6 = dev->mtu;
3521da177e4SLinus Torvalds 	ndev->cnf.sysctl = NULL;
3531da177e4SLinus Torvalds 	ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
3541da177e4SLinus Torvalds 	if (ndev->nd_parms == NULL) {
3551da177e4SLinus Torvalds 		kfree(ndev);
3561da177e4SLinus Torvalds 		return NULL;
3571da177e4SLinus Torvalds 	}
3580187bdfbSBen Hutchings 	if (ndev->cnf.forwarding)
3590187bdfbSBen Hutchings 		dev_disable_lro(dev);
3601da177e4SLinus Torvalds 	/* We refer to the device */
3611da177e4SLinus Torvalds 	dev_hold(dev);
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	if (snmp6_alloc_dev(ndev) < 0) {
3641da177e4SLinus Torvalds 		ADBG((KERN_WARNING
3651da177e4SLinus Torvalds 			"%s(): cannot allocate memory for statistics; dev=%s.\n",
3660dc47877SHarvey Harrison 			__func__, dev->name));
3671da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, ndev->nd_parms);
3681da177e4SLinus Torvalds 		ndev->dead = 1;
3691da177e4SLinus Torvalds 		in6_dev_finish_destroy(ndev);
3701da177e4SLinus Torvalds 		return NULL;
3711da177e4SLinus Torvalds 	}
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	if (snmp6_register_dev(ndev) < 0) {
3741da177e4SLinus Torvalds 		ADBG((KERN_WARNING
3751da177e4SLinus Torvalds 			"%s(): cannot create /proc/net/dev_snmp6/%s\n",
3760dc47877SHarvey Harrison 			__func__, dev->name));
3771da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, ndev->nd_parms);
3781da177e4SLinus Torvalds 		ndev->dead = 1;
3791da177e4SLinus Torvalds 		in6_dev_finish_destroy(ndev);
3801da177e4SLinus Torvalds 		return NULL;
3811da177e4SLinus Torvalds 	}
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 	/* One reference from device.  We must do this before
3841da177e4SLinus Torvalds 	 * we invoke __ipv6_regen_rndid().
3851da177e4SLinus Torvalds 	 */
3861da177e4SLinus Torvalds 	in6_dev_hold(ndev);
3871da177e4SLinus Torvalds 
3881b34be74SYOSHIFUJI Hideaki 	if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
3891b34be74SYOSHIFUJI Hideaki 		ndev->cnf.accept_dad = -1;
3901b34be74SYOSHIFUJI Hideaki 
3910be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
392c7dc89c0SFred L. Templin 	if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
393c7dc89c0SFred L. Templin 		printk(KERN_INFO
394c7dc89c0SFred L. Templin 		       "%s: Disabled Multicast RS\n",
395c7dc89c0SFred L. Templin 		       dev->name);
396c7dc89c0SFred L. Templin 		ndev->cnf.rtr_solicits = 0;
397c7dc89c0SFred L. Templin 	}
398b077d7abSYOSHIFUJI Hideaki #endif
399b077d7abSYOSHIFUJI Hideaki 
4001da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
4011da177e4SLinus Torvalds 	setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
4021da177e4SLinus Torvalds 	if ((dev->flags&IFF_LOOPBACK) ||
4031da177e4SLinus Torvalds 	    dev->type == ARPHRD_TUNNEL ||
4049625ed72SYOSHIFUJI Hideaki 	    dev->type == ARPHRD_TUNNEL6 ||
4051da177e4SLinus Torvalds 	    dev->type == ARPHRD_SIT ||
4061da177e4SLinus Torvalds 	    dev->type == ARPHRD_NONE) {
4071da177e4SLinus Torvalds 		printk(KERN_INFO
4081da177e4SLinus Torvalds 		       "%s: Disabled Privacy Extensions\n",
4091da177e4SLinus Torvalds 		       dev->name);
4101da177e4SLinus Torvalds 		ndev->cnf.use_tempaddr = -1;
4111da177e4SLinus Torvalds 	} else {
4121da177e4SLinus Torvalds 		in6_dev_hold(ndev);
4131da177e4SLinus Torvalds 		ipv6_regen_rndid((unsigned long) ndev);
4141da177e4SLinus Torvalds 	}
4151da177e4SLinus Torvalds #endif
4161da177e4SLinus Torvalds 
417f24e3d65SMitsuru Chinen 	if (netif_running(dev) && addrconf_qdisc_ok(dev))
41853aadcc9SHerbert Xu 		ndev->if_flags |= IF_READY;
41953aadcc9SHerbert Xu 
4201da177e4SLinus Torvalds 	ipv6_mc_init_dev(ndev);
4211da177e4SLinus Torvalds 	ndev->tstamp = jiffies;
422f52295a9SPavel Emelyanov 	addrconf_sysctl_register(ndev);
42330c4cf57SDavid L Stevens 	/* protected by rtnl_lock */
42430c4cf57SDavid L Stevens 	rcu_assign_pointer(dev->ip6_ptr, ndev);
425d88ae4ccSYOSHIFUJI Hideaki 
426d88ae4ccSYOSHIFUJI Hideaki 	/* Join all-node multicast group */
427f3ee4010SYOSHIFUJI Hideaki 	ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
428d88ae4ccSYOSHIFUJI Hideaki 
4291da177e4SLinus Torvalds 	return ndev;
4301da177e4SLinus Torvalds }
4311da177e4SLinus Torvalds 
432cee89473SYOSHIFUJI Hideaki static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
4331da177e4SLinus Torvalds {
4341da177e4SLinus Torvalds 	struct inet6_dev *idev;
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	ASSERT_RTNL();
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 	if ((idev = __in6_dev_get(dev)) == NULL) {
4391da177e4SLinus Torvalds 		if ((idev = ipv6_add_dev(dev)) == NULL)
4401da177e4SLinus Torvalds 			return NULL;
4411da177e4SLinus Torvalds 	}
442c5e33bddSYOSHIFUJI Hideaki 
4431da177e4SLinus Torvalds 	if (dev->flags&IFF_UP)
4441da177e4SLinus Torvalds 		ipv6_mc_up(idev);
4451da177e4SLinus Torvalds 	return idev;
4461da177e4SLinus Torvalds }
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
4491da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev)
4501da177e4SLinus Torvalds {
4511da177e4SLinus Torvalds 	struct net_device *dev;
4521da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	if (!idev)
4551da177e4SLinus Torvalds 		return;
4561da177e4SLinus Torvalds 	dev = idev->dev;
4570187bdfbSBen Hutchings 	if (idev->cnf.forwarding)
4580187bdfbSBen Hutchings 		dev_disable_lro(dev);
4591da177e4SLinus Torvalds 	if (dev && (dev->flags & IFF_MULTICAST)) {
4601da177e4SLinus Torvalds 		if (idev->cnf.forwarding)
461f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
4621da177e4SLinus Torvalds 		else
463f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
4641da177e4SLinus Torvalds 	}
4651da177e4SLinus Torvalds 	for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
4662c12a74cSMichal Wrobel 		if (ifa->flags&IFA_F_TENTATIVE)
4672c12a74cSMichal Wrobel 			continue;
4681da177e4SLinus Torvalds 		if (idev->cnf.forwarding)
4691da177e4SLinus Torvalds 			addrconf_join_anycast(ifa);
4701da177e4SLinus Torvalds 		else
4711da177e4SLinus Torvalds 			addrconf_leave_anycast(ifa);
4721da177e4SLinus Torvalds 	}
4731da177e4SLinus Torvalds }
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 
476e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf)
4771da177e4SLinus Torvalds {
4781da177e4SLinus Torvalds 	struct net_device *dev;
4791da177e4SLinus Torvalds 	struct inet6_dev *idev;
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
482bff16c2fSPavel Emelyanov 	for_each_netdev(net, dev) {
4838814c4b5SYOSHIFUJI Hideaki 		rcu_read_lock();
4841da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
4851da177e4SLinus Torvalds 		if (idev) {
486e186932bSPavel Emelyanov 			int changed = (!idev->cnf.forwarding) ^ (!newf);
487e186932bSPavel Emelyanov 			idev->cnf.forwarding = newf;
4881da177e4SLinus Torvalds 			if (changed)
4891da177e4SLinus Torvalds 				dev_forward_change(idev);
4901da177e4SLinus Torvalds 		}
4918814c4b5SYOSHIFUJI Hideaki 		rcu_read_unlock();
4921da177e4SLinus Torvalds 	}
4931da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
4941da177e4SLinus Torvalds }
495c8fecf22SPavel Emelyanov 
496c8fecf22SPavel Emelyanov static void addrconf_fixup_forwarding(struct ctl_table *table, int *p, int old)
497c8fecf22SPavel Emelyanov {
498bff16c2fSPavel Emelyanov 	struct net *net;
499bff16c2fSPavel Emelyanov 
500bff16c2fSPavel Emelyanov 	net = (struct net *)table->extra2;
501441fc2a2SPavel Emelyanov 	if (p == &net->ipv6.devconf_dflt->forwarding)
502c8fecf22SPavel Emelyanov 		return;
503c8fecf22SPavel Emelyanov 
5040187bdfbSBen Hutchings 	rtnl_lock();
505e186932bSPavel Emelyanov 	if (p == &net->ipv6.devconf_all->forwarding) {
506e186932bSPavel Emelyanov 		__s32 newf = net->ipv6.devconf_all->forwarding;
507e186932bSPavel Emelyanov 		net->ipv6.devconf_dflt->forwarding = newf;
508e186932bSPavel Emelyanov 		addrconf_forward_change(net, newf);
509c8fecf22SPavel Emelyanov 	} else if ((!*p) ^ (!old))
510c8fecf22SPavel Emelyanov 		dev_forward_change((struct inet6_dev *)table->extra1);
5110187bdfbSBen Hutchings 	rtnl_unlock();
512c8fecf22SPavel Emelyanov 
513c8fecf22SPavel Emelyanov 	if (*p)
5147b4da532SDaniel Lezcano 		rt6_purge_dflt_routers(net);
515c8fecf22SPavel Emelyanov }
5161da177e4SLinus Torvalds #endif
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds /* Nobody refers to this ifaddr, destroy it */
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
5211da177e4SLinus Torvalds {
522547b792cSIlpo Järvinen 	WARN_ON(ifp->if_next != NULL);
523547b792cSIlpo Järvinen 	WARN_ON(ifp->lst_next != NULL);
524547b792cSIlpo Järvinen 
5251da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
5261da177e4SLinus Torvalds 	printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
5271da177e4SLinus Torvalds #endif
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 	in6_dev_put(ifp->idev);
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 	if (del_timer(&ifp->timer))
5321da177e4SLinus Torvalds 		printk("Timer is still running, when freeing ifa=%p\n", ifp);
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 	if (!ifp->dead) {
5351da177e4SLinus Torvalds 		printk("Freeing alive inet6 address %p\n", ifp);
5361da177e4SLinus Torvalds 		return;
5371da177e4SLinus Torvalds 	}
5381da177e4SLinus Torvalds 	dst_release(&ifp->rt->u.dst);
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds 	kfree(ifp);
5411da177e4SLinus Torvalds }
5421da177e4SLinus Torvalds 
543e55ffac6SBrian Haley static void
544e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
545e55ffac6SBrian Haley {
546e55ffac6SBrian Haley 	struct inet6_ifaddr *ifa, **ifap;
5478a6ce0c0SYOSHIFUJI Hideaki 	int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
548e55ffac6SBrian Haley 
549e55ffac6SBrian Haley 	/*
550e55ffac6SBrian Haley 	 * Each device address list is sorted in order of scope -
551e55ffac6SBrian Haley 	 * global before linklocal.
552e55ffac6SBrian Haley 	 */
553e55ffac6SBrian Haley 	for (ifap = &idev->addr_list; (ifa = *ifap) != NULL;
554e55ffac6SBrian Haley 	     ifap = &ifa->if_next) {
5558a6ce0c0SYOSHIFUJI Hideaki 		if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
556e55ffac6SBrian Haley 			break;
557e55ffac6SBrian Haley 	}
558e55ffac6SBrian Haley 
559e55ffac6SBrian Haley 	ifp->if_next = *ifap;
560e55ffac6SBrian Haley 	*ifap = ifp;
561e55ffac6SBrian Haley }
562e55ffac6SBrian Haley 
5633eb84f49SYOSHIFUJI Hideaki /*
5643eb84f49SYOSHIFUJI Hideaki  *	Hash function taken from net_alias.c
5653eb84f49SYOSHIFUJI Hideaki  */
5663eb84f49SYOSHIFUJI Hideaki static u8 ipv6_addr_hash(const struct in6_addr *addr)
5673eb84f49SYOSHIFUJI Hideaki {
5683eb84f49SYOSHIFUJI Hideaki 	__u32 word;
5693eb84f49SYOSHIFUJI Hideaki 
5703eb84f49SYOSHIFUJI Hideaki 	/*
5713eb84f49SYOSHIFUJI Hideaki 	 * We perform the hash function over the last 64 bits of the address
5723eb84f49SYOSHIFUJI Hideaki 	 * This will include the IEEE address token on links that support it.
5733eb84f49SYOSHIFUJI Hideaki 	 */
5743eb84f49SYOSHIFUJI Hideaki 
5753eb84f49SYOSHIFUJI Hideaki 	word = (__force u32)(addr->s6_addr32[2] ^ addr->s6_addr32[3]);
5763eb84f49SYOSHIFUJI Hideaki 	word ^= (word >> 16);
5773eb84f49SYOSHIFUJI Hideaki 	word ^= (word >> 8);
5783eb84f49SYOSHIFUJI Hideaki 
5793eb84f49SYOSHIFUJI Hideaki 	return ((word ^ (word >> 4)) & 0x0f);
5803eb84f49SYOSHIFUJI Hideaki }
5813eb84f49SYOSHIFUJI Hideaki 
5821da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds static struct inet6_ifaddr *
5851da177e4SLinus Torvalds ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
586e431b8c0SJamal Hadi Salim 	      int scope, u32 flags)
5871da177e4SLinus Torvalds {
5881da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
5891da177e4SLinus Torvalds 	struct rt6_info *rt;
5901da177e4SLinus Torvalds 	int hash;
5911da177e4SLinus Torvalds 	int err = 0;
592d68b8270SYOSHIFUJI Hideaki 	int addr_type = ipv6_addr_type(addr);
593d68b8270SYOSHIFUJI Hideaki 
594d68b8270SYOSHIFUJI Hideaki 	if (addr_type == IPV6_ADDR_ANY ||
595d68b8270SYOSHIFUJI Hideaki 	    addr_type & IPV6_ADDR_MULTICAST ||
596d68b8270SYOSHIFUJI Hideaki 	    (!(idev->dev->flags & IFF_LOOPBACK) &&
597d68b8270SYOSHIFUJI Hideaki 	     addr_type & IPV6_ADDR_LOOPBACK))
598d68b8270SYOSHIFUJI Hideaki 		return ERR_PTR(-EADDRNOTAVAIL);
5991da177e4SLinus Torvalds 
6008814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
6011da177e4SLinus Torvalds 	if (idev->dead) {
6021da177e4SLinus Torvalds 		err = -ENODEV;			/*XXX*/
6031da177e4SLinus Torvalds 		goto out2;
6041da177e4SLinus Torvalds 	}
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds 	write_lock(&addrconf_hash_lock);
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 	/* Ignore adding duplicate addresses on an interface */
609c346dca1SYOSHIFUJI Hideaki 	if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
6101da177e4SLinus Torvalds 		ADBG(("ipv6_add_addr: already assigned\n"));
6111da177e4SLinus Torvalds 		err = -EEXIST;
6121da177e4SLinus Torvalds 		goto out;
6131da177e4SLinus Torvalds 	}
6141da177e4SLinus Torvalds 
615322f74a4SIngo Oeser 	ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 	if (ifa == NULL) {
6181da177e4SLinus Torvalds 		ADBG(("ipv6_add_addr: malloc failed\n"));
6191da177e4SLinus Torvalds 		err = -ENOBUFS;
6201da177e4SLinus Torvalds 		goto out;
6211da177e4SLinus Torvalds 	}
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	rt = addrconf_dst_alloc(idev, addr, 0);
6241da177e4SLinus Torvalds 	if (IS_ERR(rt)) {
6251da177e4SLinus Torvalds 		err = PTR_ERR(rt);
6261da177e4SLinus Torvalds 		goto out;
6271da177e4SLinus Torvalds 	}
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	ipv6_addr_copy(&ifa->addr, addr);
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 	spin_lock_init(&ifa->lock);
6321da177e4SLinus Torvalds 	init_timer(&ifa->timer);
6331da177e4SLinus Torvalds 	ifa->timer.data = (unsigned long) ifa;
6341da177e4SLinus Torvalds 	ifa->scope = scope;
6351da177e4SLinus Torvalds 	ifa->prefix_len = pfxlen;
6361da177e4SLinus Torvalds 	ifa->flags = flags | IFA_F_TENTATIVE;
6371da177e4SLinus Torvalds 	ifa->cstamp = ifa->tstamp = jiffies;
6381da177e4SLinus Torvalds 
63957f5f544SKeir Fraser 	ifa->rt = rt;
64057f5f544SKeir Fraser 
64195c385b4SNeil Horman 	/*
64295c385b4SNeil Horman 	 * part one of RFC 4429, section 3.3
64395c385b4SNeil Horman 	 * We should not configure an address as
64495c385b4SNeil Horman 	 * optimistic if we do not yet know the link
64595c385b4SNeil Horman 	 * layer address of our nexhop router
64695c385b4SNeil Horman 	 */
64795c385b4SNeil Horman 
64895c385b4SNeil Horman 	if (rt->rt6i_nexthop == NULL)
64995c385b4SNeil Horman 		ifa->flags &= ~IFA_F_OPTIMISTIC;
65095c385b4SNeil Horman 
6511da177e4SLinus Torvalds 	ifa->idev = idev;
6521da177e4SLinus Torvalds 	in6_dev_hold(idev);
6531da177e4SLinus Torvalds 	/* For caller */
6541da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 	/* Add to big hash table */
6571da177e4SLinus Torvalds 	hash = ipv6_addr_hash(addr);
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 	ifa->lst_next = inet6_addr_lst[hash];
6601da177e4SLinus Torvalds 	inet6_addr_lst[hash] = ifa;
6611da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
6621da177e4SLinus Torvalds 	write_unlock(&addrconf_hash_lock);
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 	write_lock(&idev->lock);
6651da177e4SLinus Torvalds 	/* Add to inet6_dev unicast addr list. */
666e55ffac6SBrian Haley 	ipv6_link_dev_addr(idev, ifa);
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
6691da177e4SLinus Torvalds 	if (ifa->flags&IFA_F_TEMPORARY) {
6701da177e4SLinus Torvalds 		ifa->tmp_next = idev->tempaddr_list;
6711da177e4SLinus Torvalds 		idev->tempaddr_list = ifa;
6721da177e4SLinus Torvalds 		in6_ifa_hold(ifa);
6731da177e4SLinus Torvalds 	}
6741da177e4SLinus Torvalds #endif
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
6771da177e4SLinus Torvalds 	write_unlock(&idev->lock);
6781da177e4SLinus Torvalds out2:
6798814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
6801da177e4SLinus Torvalds 
681fd92833aSYOSHIFUJI Hideaki 	if (likely(err == 0))
682e041c683SAlan Stern 		atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
6831da177e4SLinus Torvalds 	else {
6841da177e4SLinus Torvalds 		kfree(ifa);
6851da177e4SLinus Torvalds 		ifa = ERR_PTR(err);
6861da177e4SLinus Torvalds 	}
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds 	return ifa;
6891da177e4SLinus Torvalds out:
6901da177e4SLinus Torvalds 	write_unlock(&addrconf_hash_lock);
6911da177e4SLinus Torvalds 	goto out2;
6921da177e4SLinus Torvalds }
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp)
6971da177e4SLinus Torvalds {
6981da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa, **ifap;
6991da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
7001da177e4SLinus Torvalds 	int hash;
7011da177e4SLinus Torvalds 	int deleted = 0, onlink = 0;
7021da177e4SLinus Torvalds 	unsigned long expires = jiffies;
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 	hash = ipv6_addr_hash(&ifp->addr);
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 	ifp->dead = 1;
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds 	write_lock_bh(&addrconf_hash_lock);
7091da177e4SLinus Torvalds 	for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
7101da177e4SLinus Torvalds 	     ifap = &ifa->lst_next) {
7111da177e4SLinus Torvalds 		if (ifa == ifp) {
7121da177e4SLinus Torvalds 			*ifap = ifa->lst_next;
7131da177e4SLinus Torvalds 			__in6_ifa_put(ifp);
7141da177e4SLinus Torvalds 			ifa->lst_next = NULL;
7151da177e4SLinus Torvalds 			break;
7161da177e4SLinus Torvalds 		}
7171da177e4SLinus Torvalds 	}
7181da177e4SLinus Torvalds 	write_unlock_bh(&addrconf_hash_lock);
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
7211da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
7221da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_TEMPORARY) {
7231da177e4SLinus Torvalds 		for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
7241da177e4SLinus Torvalds 		     ifap = &ifa->tmp_next) {
7251da177e4SLinus Torvalds 			if (ifa == ifp) {
7261da177e4SLinus Torvalds 				*ifap = ifa->tmp_next;
7271da177e4SLinus Torvalds 				if (ifp->ifpub) {
7281da177e4SLinus Torvalds 					in6_ifa_put(ifp->ifpub);
7291da177e4SLinus Torvalds 					ifp->ifpub = NULL;
7301da177e4SLinus Torvalds 				}
7311da177e4SLinus Torvalds 				__in6_ifa_put(ifp);
7321da177e4SLinus Torvalds 				ifa->tmp_next = NULL;
7331da177e4SLinus Torvalds 				break;
7341da177e4SLinus Torvalds 			}
7351da177e4SLinus Torvalds 		}
7361da177e4SLinus Torvalds 	}
7371da177e4SLinus Torvalds #endif
7381da177e4SLinus Torvalds 
7391d142804SKristian Slavov 	for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
7401da177e4SLinus Torvalds 		if (ifa == ifp) {
7411da177e4SLinus Torvalds 			*ifap = ifa->if_next;
7421da177e4SLinus Torvalds 			__in6_ifa_put(ifp);
7431da177e4SLinus Torvalds 			ifa->if_next = NULL;
7441da177e4SLinus Torvalds 			if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
7451da177e4SLinus Torvalds 				break;
7461da177e4SLinus Torvalds 			deleted = 1;
7471d142804SKristian Slavov 			continue;
7481da177e4SLinus Torvalds 		} else if (ifp->flags & IFA_F_PERMANENT) {
7491da177e4SLinus Torvalds 			if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
7501da177e4SLinus Torvalds 					      ifp->prefix_len)) {
7511da177e4SLinus Torvalds 				if (ifa->flags & IFA_F_PERMANENT) {
7521da177e4SLinus Torvalds 					onlink = 1;
7531da177e4SLinus Torvalds 					if (deleted)
7541da177e4SLinus Torvalds 						break;
7551da177e4SLinus Torvalds 				} else {
7561da177e4SLinus Torvalds 					unsigned long lifetime;
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds 					if (!onlink)
7591da177e4SLinus Torvalds 						onlink = -1;
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 					spin_lock(&ifa->lock);
7624bed72e4SYOSHIFUJI Hideaki 
7634bed72e4SYOSHIFUJI Hideaki 					lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
7644bed72e4SYOSHIFUJI Hideaki 					/*
7654bed72e4SYOSHIFUJI Hideaki 					 * Note: Because this address is
7664bed72e4SYOSHIFUJI Hideaki 					 * not permanent, lifetime <
7674bed72e4SYOSHIFUJI Hideaki 					 * LONG_MAX / HZ here.
7684bed72e4SYOSHIFUJI Hideaki 					 */
7691da177e4SLinus Torvalds 					if (time_before(expires,
7701da177e4SLinus Torvalds 							ifa->tstamp + lifetime * HZ))
7711da177e4SLinus Torvalds 						expires = ifa->tstamp + lifetime * HZ;
7721da177e4SLinus Torvalds 					spin_unlock(&ifa->lock);
7731da177e4SLinus Torvalds 				}
7741da177e4SLinus Torvalds 			}
7751da177e4SLinus Torvalds 		}
7761d142804SKristian Slavov 		ifap = &ifa->if_next;
7771da177e4SLinus Torvalds 	}
7781da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
7791da177e4SLinus Torvalds 
780b2238566SAndrey Vagin 	addrconf_del_timer(ifp);
781b2238566SAndrey Vagin 
7821da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_DELADDR, ifp);
7831da177e4SLinus Torvalds 
784e041c683SAlan Stern 	atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	/*
7871da177e4SLinus Torvalds 	 * Purge or update corresponding prefix
7881da177e4SLinus Torvalds 	 *
7891da177e4SLinus Torvalds 	 * 1) we don't purge prefix here if address was not permanent.
7901da177e4SLinus Torvalds 	 *    prefix is managed by its own lifetime.
7911da177e4SLinus Torvalds 	 * 2) if there're no addresses, delete prefix.
7921da177e4SLinus Torvalds 	 * 3) if there're still other permanent address(es),
7931da177e4SLinus Torvalds 	 *    corresponding prefix is still permanent.
7941da177e4SLinus Torvalds 	 * 4) otherwise, update prefix lifetime to the
7951da177e4SLinus Torvalds 	 *    longest valid lifetime among the corresponding
7961da177e4SLinus Torvalds 	 *    addresses on the device.
7971da177e4SLinus Torvalds 	 *    Note: subsequent RA will update lifetime.
7981da177e4SLinus Torvalds 	 *
7991da177e4SLinus Torvalds 	 * --yoshfuji
8001da177e4SLinus Torvalds 	 */
8011da177e4SLinus Torvalds 	if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
8021da177e4SLinus Torvalds 		struct in6_addr prefix;
8031da177e4SLinus Torvalds 		struct rt6_info *rt;
804c346dca1SYOSHIFUJI Hideaki 		struct net *net = dev_net(ifp->idev->dev);
8051da177e4SLinus Torvalds 		ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
8066fda7350SBenjamin Thery 		rt = rt6_lookup(net, &prefix, NULL, ifp->idev->dev->ifindex, 1);
8071da177e4SLinus Torvalds 
8082b5ead46SYOSHIFUJI Hideaki 		if (rt && addrconf_is_prefix_route(rt)) {
8091da177e4SLinus Torvalds 			if (onlink == 0) {
810e0a1ad73SThomas Graf 				ip6_del_rt(rt);
8111da177e4SLinus Torvalds 				rt = NULL;
8121da177e4SLinus Torvalds 			} else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
8131da177e4SLinus Torvalds 				rt->rt6i_expires = expires;
8141da177e4SLinus Torvalds 				rt->rt6i_flags |= RTF_EXPIRES;
8151da177e4SLinus Torvalds 			}
8161da177e4SLinus Torvalds 		}
8171da177e4SLinus Torvalds 		dst_release(&rt->u.dst);
8181da177e4SLinus Torvalds 	}
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds 	in6_ifa_put(ifp);
8211da177e4SLinus Torvalds }
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
8241da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
8251da177e4SLinus Torvalds {
8261da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
8271da177e4SLinus Torvalds 	struct in6_addr addr, *tmpaddr;
8281da177e4SLinus Torvalds 	unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
829eac55bf9SBenoit Boissinot 	unsigned long regen_advance;
8301da177e4SLinus Torvalds 	int tmp_plen;
8311da177e4SLinus Torvalds 	int ret = 0;
8321da177e4SLinus Torvalds 	int max_addresses;
83395c385b4SNeil Horman 	u32 addr_flags;
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 	write_lock(&idev->lock);
8361da177e4SLinus Torvalds 	if (ift) {
8371da177e4SLinus Torvalds 		spin_lock_bh(&ift->lock);
8381da177e4SLinus Torvalds 		memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
8391da177e4SLinus Torvalds 		spin_unlock_bh(&ift->lock);
8401da177e4SLinus Torvalds 		tmpaddr = &addr;
8411da177e4SLinus Torvalds 	} else {
8421da177e4SLinus Torvalds 		tmpaddr = NULL;
8431da177e4SLinus Torvalds 	}
8441da177e4SLinus Torvalds retry:
8451da177e4SLinus Torvalds 	in6_dev_hold(idev);
8461da177e4SLinus Torvalds 	if (idev->cnf.use_tempaddr <= 0) {
8471da177e4SLinus Torvalds 		write_unlock(&idev->lock);
8481da177e4SLinus Torvalds 		printk(KERN_INFO
8491da177e4SLinus Torvalds 			"ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
8501da177e4SLinus Torvalds 		in6_dev_put(idev);
8511da177e4SLinus Torvalds 		ret = -1;
8521da177e4SLinus Torvalds 		goto out;
8531da177e4SLinus Torvalds 	}
8541da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
8551da177e4SLinus Torvalds 	if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
8561da177e4SLinus Torvalds 		idev->cnf.use_tempaddr = -1;	/*XXX*/
8571da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
8581da177e4SLinus Torvalds 		write_unlock(&idev->lock);
8591da177e4SLinus Torvalds 		printk(KERN_WARNING
8601da177e4SLinus Torvalds 			"ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
8611da177e4SLinus Torvalds 		in6_dev_put(idev);
8621da177e4SLinus Torvalds 		ret = -1;
8631da177e4SLinus Torvalds 		goto out;
8641da177e4SLinus Torvalds 	}
8651da177e4SLinus Torvalds 	in6_ifa_hold(ifp);
8661da177e4SLinus Torvalds 	memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
8671da177e4SLinus Torvalds 	if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
8681da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
8691da177e4SLinus Torvalds 		write_unlock(&idev->lock);
8701da177e4SLinus Torvalds 		printk(KERN_WARNING
8711da177e4SLinus Torvalds 			"ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
8721da177e4SLinus Torvalds 		in6_ifa_put(ifp);
8731da177e4SLinus Torvalds 		in6_dev_put(idev);
8741da177e4SLinus Torvalds 		ret = -1;
8751da177e4SLinus Torvalds 		goto out;
8761da177e4SLinus Torvalds 	}
8771da177e4SLinus Torvalds 	memcpy(&addr.s6_addr[8], idev->rndid, 8);
8781da177e4SLinus Torvalds 	tmp_valid_lft = min_t(__u32,
8791da177e4SLinus Torvalds 			      ifp->valid_lft,
8801da177e4SLinus Torvalds 			      idev->cnf.temp_valid_lft);
8811da177e4SLinus Torvalds 	tmp_prefered_lft = min_t(__u32,
8821da177e4SLinus Torvalds 				 ifp->prefered_lft,
8831da177e4SLinus Torvalds 				 idev->cnf.temp_prefered_lft - desync_factor / HZ);
8841da177e4SLinus Torvalds 	tmp_plen = ifp->prefix_len;
8851da177e4SLinus Torvalds 	max_addresses = idev->cnf.max_addresses;
8861da177e4SLinus Torvalds 	tmp_cstamp = ifp->cstamp;
8871da177e4SLinus Torvalds 	tmp_tstamp = ifp->tstamp;
8881da177e4SLinus Torvalds 	spin_unlock_bh(&ifp->lock);
8891da177e4SLinus Torvalds 
890eac55bf9SBenoit Boissinot 	regen_advance = idev->cnf.regen_max_retry *
891eac55bf9SBenoit Boissinot 	                idev->cnf.dad_transmits *
892eac55bf9SBenoit Boissinot 	                idev->nd_parms->retrans_time / HZ;
8931da177e4SLinus Torvalds 	write_unlock(&idev->lock);
89495c385b4SNeil Horman 
895eac55bf9SBenoit Boissinot 	/* A temporary address is created only if this calculated Preferred
896eac55bf9SBenoit Boissinot 	 * Lifetime is greater than REGEN_ADVANCE time units.  In particular,
897eac55bf9SBenoit Boissinot 	 * an implementation must not create a temporary address with a zero
898eac55bf9SBenoit Boissinot 	 * Preferred Lifetime.
899eac55bf9SBenoit Boissinot 	 */
900eac55bf9SBenoit Boissinot 	if (tmp_prefered_lft <= regen_advance) {
901eac55bf9SBenoit Boissinot 		in6_ifa_put(ifp);
902eac55bf9SBenoit Boissinot 		in6_dev_put(idev);
903eac55bf9SBenoit Boissinot 		ret = -1;
904eac55bf9SBenoit Boissinot 		goto out;
905eac55bf9SBenoit Boissinot 	}
906eac55bf9SBenoit Boissinot 
90795c385b4SNeil Horman 	addr_flags = IFA_F_TEMPORARY;
90895c385b4SNeil Horman 	/* set in addrconf_prefix_rcv() */
90995c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
91095c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
91195c385b4SNeil Horman 
9121da177e4SLinus Torvalds 	ift = !max_addresses ||
9131da177e4SLinus Torvalds 	      ipv6_count_addresses(idev) < max_addresses ?
9141da177e4SLinus Torvalds 		ipv6_add_addr(idev, &addr, tmp_plen,
91595c385b4SNeil Horman 			      ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK,
91695c385b4SNeil Horman 			      addr_flags) : NULL;
9171da177e4SLinus Torvalds 	if (!ift || IS_ERR(ift)) {
9181da177e4SLinus Torvalds 		in6_ifa_put(ifp);
9191da177e4SLinus Torvalds 		in6_dev_put(idev);
9201da177e4SLinus Torvalds 		printk(KERN_INFO
9211da177e4SLinus Torvalds 			"ipv6_create_tempaddr(): retry temporary address regeneration.\n");
9221da177e4SLinus Torvalds 		tmpaddr = &addr;
9231da177e4SLinus Torvalds 		write_lock(&idev->lock);
9241da177e4SLinus Torvalds 		goto retry;
9251da177e4SLinus Torvalds 	}
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 	spin_lock_bh(&ift->lock);
9281da177e4SLinus Torvalds 	ift->ifpub = ifp;
9291da177e4SLinus Torvalds 	ift->valid_lft = tmp_valid_lft;
9301da177e4SLinus Torvalds 	ift->prefered_lft = tmp_prefered_lft;
9311da177e4SLinus Torvalds 	ift->cstamp = tmp_cstamp;
9321da177e4SLinus Torvalds 	ift->tstamp = tmp_tstamp;
9331da177e4SLinus Torvalds 	spin_unlock_bh(&ift->lock);
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 	addrconf_dad_start(ift, 0);
9361da177e4SLinus Torvalds 	in6_ifa_put(ift);
9371da177e4SLinus Torvalds 	in6_dev_put(idev);
9381da177e4SLinus Torvalds out:
9391da177e4SLinus Torvalds 	return ret;
9401da177e4SLinus Torvalds }
9411da177e4SLinus Torvalds #endif
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds /*
944072047e4SYOSHIFUJI Hideaki  *	Choose an appropriate source address (RFC3484)
9451da177e4SLinus Torvalds  */
946a9b05723SYOSHIFUJI Hideaki enum {
947a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_INIT = 0,
948a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LOCAL,
949a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_SCOPE,
950a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFERRED,
951a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
952a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_HOA,
953a9b05723SYOSHIFUJI Hideaki #endif
954a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_OIF,
955a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LABEL,
956a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_PRIVACY
957a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PRIVACY,
958a9b05723SYOSHIFUJI Hideaki #endif
959a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_ORCHID,
960a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFIX,
961a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_MAX
962072047e4SYOSHIFUJI Hideaki };
963072047e4SYOSHIFUJI Hideaki 
964a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score {
965a9b05723SYOSHIFUJI Hideaki 	int			rule;
966a9b05723SYOSHIFUJI Hideaki 	int			addr_type;
967a9b05723SYOSHIFUJI Hideaki 	struct inet6_ifaddr	*ifa;
968a9b05723SYOSHIFUJI Hideaki 	DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
969a9b05723SYOSHIFUJI Hideaki 	int			scopedist;
970a9b05723SYOSHIFUJI Hideaki 	int			matchlen;
971a9b05723SYOSHIFUJI Hideaki };
972a9b05723SYOSHIFUJI Hideaki 
973a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst {
9749acd9f3aSYOSHIFUJI Hideaki 	const struct in6_addr *addr;
975a9b05723SYOSHIFUJI Hideaki 	int ifindex;
976a9b05723SYOSHIFUJI Hideaki 	int scope;
977a9b05723SYOSHIFUJI Hideaki 	int label;
9787cbca67cSYOSHIFUJI Hideaki 	unsigned int prefs;
979a9b05723SYOSHIFUJI Hideaki };
980072047e4SYOSHIFUJI Hideaki 
981b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type)
9821da177e4SLinus Torvalds {
983072047e4SYOSHIFUJI Hideaki 	if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
984072047e4SYOSHIFUJI Hideaki 		    IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
985072047e4SYOSHIFUJI Hideaki 		return 1;
986072047e4SYOSHIFUJI Hideaki 	return 0;
9871da177e4SLinus Torvalds }
9881da177e4SLinus Torvalds 
9893de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net,
9903de23255SBenjamin Thery 			       struct ipv6_saddr_score *score,
991a9b05723SYOSHIFUJI Hideaki 			       struct ipv6_saddr_dst *dst,
992a9b05723SYOSHIFUJI Hideaki 			       int i)
993a9b05723SYOSHIFUJI Hideaki {
994a9b05723SYOSHIFUJI Hideaki 	int ret;
995a9b05723SYOSHIFUJI Hideaki 
996a9b05723SYOSHIFUJI Hideaki 	if (i <= score->rule) {
997a9b05723SYOSHIFUJI Hideaki 		switch (i) {
998a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_SCOPE:
999a9b05723SYOSHIFUJI Hideaki 			ret = score->scopedist;
1000a9b05723SYOSHIFUJI Hideaki 			break;
1001a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_PREFIX:
1002a9b05723SYOSHIFUJI Hideaki 			ret = score->matchlen;
1003a9b05723SYOSHIFUJI Hideaki 			break;
1004a9b05723SYOSHIFUJI Hideaki 		default:
1005a9b05723SYOSHIFUJI Hideaki 			ret = !!test_bit(i, score->scorebits);
1006a9b05723SYOSHIFUJI Hideaki 		}
1007a9b05723SYOSHIFUJI Hideaki 		goto out;
1008a9b05723SYOSHIFUJI Hideaki 	}
1009a9b05723SYOSHIFUJI Hideaki 
1010a9b05723SYOSHIFUJI Hideaki 	switch (i) {
1011a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_INIT:
1012a9b05723SYOSHIFUJI Hideaki 		/* Rule 0: remember if hiscore is not ready yet */
1013a9b05723SYOSHIFUJI Hideaki 		ret = !!score->ifa;
1014a9b05723SYOSHIFUJI Hideaki 		break;
1015a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LOCAL:
1016a9b05723SYOSHIFUJI Hideaki 		/* Rule 1: Prefer same address */
1017a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1018a9b05723SYOSHIFUJI Hideaki 		break;
1019a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_SCOPE:
1020a9b05723SYOSHIFUJI Hideaki 		/* Rule 2: Prefer appropriate scope
1021a9b05723SYOSHIFUJI Hideaki 		 *
1022a9b05723SYOSHIFUJI Hideaki 		 *      ret
1023a9b05723SYOSHIFUJI Hideaki 		 *       ^
1024a9b05723SYOSHIFUJI Hideaki 		 *    -1 |  d 15
1025a9b05723SYOSHIFUJI Hideaki 		 *    ---+--+-+---> scope
1026a9b05723SYOSHIFUJI Hideaki 		 *       |
1027a9b05723SYOSHIFUJI Hideaki 		 *       |             d is scope of the destination.
1028a9b05723SYOSHIFUJI Hideaki 		 *  B-d  |  \
1029a9b05723SYOSHIFUJI Hideaki 		 *       |   \      <- smaller scope is better if
1030a9b05723SYOSHIFUJI Hideaki 		 *  B-15 |    \        if scope is enough for destinaion.
1031a9b05723SYOSHIFUJI Hideaki 		 *       |             ret = B - scope (-1 <= scope >= d <= 15).
1032a9b05723SYOSHIFUJI Hideaki 		 * d-C-1 | /
1033a9b05723SYOSHIFUJI Hideaki 		 *       |/         <- greater is better
1034a9b05723SYOSHIFUJI Hideaki 		 *   -C  /             if scope is not enough for destination.
1035a9b05723SYOSHIFUJI Hideaki 		 *      /|             ret = scope - C (-1 <= d < scope <= 15).
1036a9b05723SYOSHIFUJI Hideaki 		 *
1037a9b05723SYOSHIFUJI Hideaki 		 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1038a9b05723SYOSHIFUJI Hideaki 		 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1039a9b05723SYOSHIFUJI Hideaki 		 * Assume B = 0 and we get C > 29.
1040a9b05723SYOSHIFUJI Hideaki 		 */
1041a9b05723SYOSHIFUJI Hideaki 		ret = __ipv6_addr_src_scope(score->addr_type);
1042a9b05723SYOSHIFUJI Hideaki 		if (ret >= dst->scope)
1043a9b05723SYOSHIFUJI Hideaki 			ret = -ret;
1044a9b05723SYOSHIFUJI Hideaki 		else
1045a9b05723SYOSHIFUJI Hideaki 			ret -= 128;	/* 30 is enough */
1046a9b05723SYOSHIFUJI Hideaki 		score->scopedist = ret;
1047a9b05723SYOSHIFUJI Hideaki 		break;
1048a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFERRED:
1049a9b05723SYOSHIFUJI Hideaki 		/* Rule 3: Avoid deprecated and optimistic addresses */
1050a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_saddr_preferred(score->addr_type) ||
1051a9b05723SYOSHIFUJI Hideaki 		      !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC));
1052a9b05723SYOSHIFUJI Hideaki 		break;
1053a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1054a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_HOA:
10557cbca67cSYOSHIFUJI Hideaki 	    {
1056a9b05723SYOSHIFUJI Hideaki 		/* Rule 4: Prefer home address */
10577cbca67cSYOSHIFUJI Hideaki 		int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
10587cbca67cSYOSHIFUJI Hideaki 		ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1059a9b05723SYOSHIFUJI Hideaki 		break;
10607cbca67cSYOSHIFUJI Hideaki 	    }
1061a9b05723SYOSHIFUJI Hideaki #endif
1062a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_OIF:
1063a9b05723SYOSHIFUJI Hideaki 		/* Rule 5: Prefer outgoing interface */
1064a9b05723SYOSHIFUJI Hideaki 		ret = (!dst->ifindex ||
1065a9b05723SYOSHIFUJI Hideaki 		       dst->ifindex == score->ifa->idev->dev->ifindex);
1066a9b05723SYOSHIFUJI Hideaki 		break;
1067a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LABEL:
1068a9b05723SYOSHIFUJI Hideaki 		/* Rule 6: Prefer matching label */
10693de23255SBenjamin Thery 		ret = ipv6_addr_label(net,
10703de23255SBenjamin Thery 				      &score->ifa->addr, score->addr_type,
1071a9b05723SYOSHIFUJI Hideaki 				      score->ifa->idev->dev->ifindex) == dst->label;
1072a9b05723SYOSHIFUJI Hideaki 		break;
1073a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_PRIVACY
1074a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PRIVACY:
10757cbca67cSYOSHIFUJI Hideaki 	    {
1076a9b05723SYOSHIFUJI Hideaki 		/* Rule 7: Prefer public address
1077a9b05723SYOSHIFUJI Hideaki 		 * Note: prefer temprary address if use_tempaddr >= 2
1078a9b05723SYOSHIFUJI Hideaki 		 */
10797cbca67cSYOSHIFUJI Hideaki 		int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
10807cbca67cSYOSHIFUJI Hideaki 				!!(dst->prefs & IPV6_PREFER_SRC_TMP) :
10817cbca67cSYOSHIFUJI Hideaki 				score->ifa->idev->cnf.use_tempaddr >= 2;
10827cbca67cSYOSHIFUJI Hideaki 		ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1083a9b05723SYOSHIFUJI Hideaki 		break;
10847cbca67cSYOSHIFUJI Hideaki 	    }
1085a9b05723SYOSHIFUJI Hideaki #endif
1086a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_ORCHID:
1087a9b05723SYOSHIFUJI Hideaki 		/* Rule 8-: Prefer ORCHID vs ORCHID or
1088a9b05723SYOSHIFUJI Hideaki 		 *	    non-ORCHID vs non-ORCHID
1089a9b05723SYOSHIFUJI Hideaki 		 */
1090a9b05723SYOSHIFUJI Hideaki 		ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1091a9b05723SYOSHIFUJI Hideaki 			ipv6_addr_orchid(dst->addr));
1092a9b05723SYOSHIFUJI Hideaki 		break;
1093a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFIX:
1094a9b05723SYOSHIFUJI Hideaki 		/* Rule 8: Use longest matching prefix */
1095a9b05723SYOSHIFUJI Hideaki 		score->matchlen = ret = ipv6_addr_diff(&score->ifa->addr,
1096a9b05723SYOSHIFUJI Hideaki 						       dst->addr);
1097a9b05723SYOSHIFUJI Hideaki 		break;
1098a9b05723SYOSHIFUJI Hideaki 	default:
1099a9b05723SYOSHIFUJI Hideaki 		ret = 0;
1100a9b05723SYOSHIFUJI Hideaki 	}
1101a9b05723SYOSHIFUJI Hideaki 
1102a9b05723SYOSHIFUJI Hideaki 	if (ret)
1103a9b05723SYOSHIFUJI Hideaki 		__set_bit(i, score->scorebits);
1104a9b05723SYOSHIFUJI Hideaki 	score->rule = i;
1105a9b05723SYOSHIFUJI Hideaki out:
1106a9b05723SYOSHIFUJI Hideaki 	return ret;
1107a9b05723SYOSHIFUJI Hideaki }
1108a9b05723SYOSHIFUJI Hideaki 
1109191cd582SBrian Haley int ipv6_dev_get_saddr(struct net *net, struct net_device *dst_dev,
11109acd9f3aSYOSHIFUJI Hideaki 		       const struct in6_addr *daddr, unsigned int prefs,
11117cbca67cSYOSHIFUJI Hideaki 		       struct in6_addr *saddr)
11121da177e4SLinus Torvalds {
1113a9b05723SYOSHIFUJI Hideaki 	struct ipv6_saddr_score scores[2],
1114a9b05723SYOSHIFUJI Hideaki 				*score = &scores[0], *hiscore = &scores[1];
1115a9b05723SYOSHIFUJI Hideaki 	struct ipv6_saddr_dst dst;
1116072047e4SYOSHIFUJI Hideaki 	struct net_device *dev;
1117a9b05723SYOSHIFUJI Hideaki 	int dst_type;
11181da177e4SLinus Torvalds 
1119a9b05723SYOSHIFUJI Hideaki 	dst_type = __ipv6_addr_type(daddr);
1120a9b05723SYOSHIFUJI Hideaki 	dst.addr = daddr;
1121a9b05723SYOSHIFUJI Hideaki 	dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1122a9b05723SYOSHIFUJI Hideaki 	dst.scope = __ipv6_addr_src_scope(dst_type);
11233de23255SBenjamin Thery 	dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
11247cbca67cSYOSHIFUJI Hideaki 	dst.prefs = prefs;
1125a9b05723SYOSHIFUJI Hideaki 
1126a9b05723SYOSHIFUJI Hideaki 	hiscore->rule = -1;
1127a9b05723SYOSHIFUJI Hideaki 	hiscore->ifa = NULL;
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
11308814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
1131072047e4SYOSHIFUJI Hideaki 
11326fda7350SBenjamin Thery 	for_each_netdev(net, dev) {
1133072047e4SYOSHIFUJI Hideaki 		struct inet6_dev *idev;
1134072047e4SYOSHIFUJI Hideaki 
1135a9b05723SYOSHIFUJI Hideaki 		/* Candidate Source Address (section 4)
1136072047e4SYOSHIFUJI Hideaki 		 *  - multicast and link-local destination address,
1137072047e4SYOSHIFUJI Hideaki 		 *    the set of candidate source address MUST only
1138072047e4SYOSHIFUJI Hideaki 		 *    include addresses assigned to interfaces
1139072047e4SYOSHIFUJI Hideaki 		 *    belonging to the same link as the outgoing
1140072047e4SYOSHIFUJI Hideaki 		 *    interface.
1141072047e4SYOSHIFUJI Hideaki 		 * (- For site-local destination addresses, the
1142072047e4SYOSHIFUJI Hideaki 		 *    set of candidate source addresses MUST only
1143072047e4SYOSHIFUJI Hideaki 		 *    include addresses assigned to interfaces
1144072047e4SYOSHIFUJI Hideaki 		 *    belonging to the same site as the outgoing
1145072047e4SYOSHIFUJI Hideaki 		 *    interface.)
1146072047e4SYOSHIFUJI Hideaki 		 */
1147a9b05723SYOSHIFUJI Hideaki 		if (((dst_type & IPV6_ADDR_MULTICAST) ||
1148a9b05723SYOSHIFUJI Hideaki 		     dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
1149a9b05723SYOSHIFUJI Hideaki 		    dst.ifindex && dev->ifindex != dst.ifindex)
1150072047e4SYOSHIFUJI Hideaki 			continue;
1151072047e4SYOSHIFUJI Hideaki 
11521da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
1153072047e4SYOSHIFUJI Hideaki 		if (!idev)
1154072047e4SYOSHIFUJI Hideaki 			continue;
1155072047e4SYOSHIFUJI Hideaki 
11561da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
1157a9b05723SYOSHIFUJI Hideaki 		for (score->ifa = idev->addr_list; score->ifa; score->ifa = score->ifa->if_next) {
1158a9b05723SYOSHIFUJI Hideaki 			int i;
1159072047e4SYOSHIFUJI Hideaki 
1160a9b05723SYOSHIFUJI Hideaki 			/*
11616b3ae80aSYOSHIFUJI Hideaki 			 * - Tentative Address (RFC2462 section 5.4)
11626b3ae80aSYOSHIFUJI Hideaki 			 *  - A tentative address is not considered
11636b3ae80aSYOSHIFUJI Hideaki 			 *    "assigned to an interface" in the traditional
116495c385b4SNeil Horman 			 *    sense, unless it is also flagged as optimistic.
11656b3ae80aSYOSHIFUJI Hideaki 			 * - Candidate Source Address (section 4)
1166072047e4SYOSHIFUJI Hideaki 			 *  - In any case, anycast addresses, multicast
1167072047e4SYOSHIFUJI Hideaki 			 *    addresses, and the unspecified address MUST
1168072047e4SYOSHIFUJI Hideaki 			 *    NOT be included in a candidate set.
1169072047e4SYOSHIFUJI Hideaki 			 */
1170a9b05723SYOSHIFUJI Hideaki 			if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1171a9b05723SYOSHIFUJI Hideaki 			    (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
11726b3ae80aSYOSHIFUJI Hideaki 				continue;
1173a9b05723SYOSHIFUJI Hideaki 
1174a9b05723SYOSHIFUJI Hideaki 			score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1175a9b05723SYOSHIFUJI Hideaki 
1176a9b05723SYOSHIFUJI Hideaki 			if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1177a9b05723SYOSHIFUJI Hideaki 				     score->addr_type & IPV6_ADDR_MULTICAST)) {
1178072047e4SYOSHIFUJI Hideaki 				LIMIT_NETDEBUG(KERN_DEBUG
1179072047e4SYOSHIFUJI Hideaki 					       "ADDRCONF: unspecified / multicast address "
1180072047e4SYOSHIFUJI Hideaki 					       "assigned as unicast address on %s",
1181072047e4SYOSHIFUJI Hideaki 					       dev->name);
11821da177e4SLinus Torvalds 				continue;
1183072047e4SYOSHIFUJI Hideaki 			}
1184072047e4SYOSHIFUJI Hideaki 
1185a9b05723SYOSHIFUJI Hideaki 			score->rule = -1;
1186a9b05723SYOSHIFUJI Hideaki 			bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1187072047e4SYOSHIFUJI Hideaki 
1188a9b05723SYOSHIFUJI Hideaki 			for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1189a9b05723SYOSHIFUJI Hideaki 				int minihiscore, miniscore;
1190072047e4SYOSHIFUJI Hideaki 
11913de23255SBenjamin Thery 				minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i);
11923de23255SBenjamin Thery 				miniscore = ipv6_get_saddr_eval(net, score, &dst, i);
1193072047e4SYOSHIFUJI Hideaki 
1194a9b05723SYOSHIFUJI Hideaki 				if (minihiscore > miniscore) {
1195a9b05723SYOSHIFUJI Hideaki 					if (i == IPV6_SADDR_RULE_SCOPE &&
1196a9b05723SYOSHIFUJI Hideaki 					    score->scopedist > 0) {
1197a9b05723SYOSHIFUJI Hideaki 						/*
1198a9b05723SYOSHIFUJI Hideaki 						 * special case:
1199a9b05723SYOSHIFUJI Hideaki 						 * each remaining entry
1200a9b05723SYOSHIFUJI Hideaki 						 * has too small (not enough)
1201a9b05723SYOSHIFUJI Hideaki 						 * scope, because ifa entries
1202a9b05723SYOSHIFUJI Hideaki 						 * are sorted by their scope
1203a9b05723SYOSHIFUJI Hideaki 						 * values.
1204072047e4SYOSHIFUJI Hideaki 						 */
1205a9b05723SYOSHIFUJI Hideaki 						goto try_nextdev;
1206072047e4SYOSHIFUJI Hideaki 					}
1207a9b05723SYOSHIFUJI Hideaki 					break;
1208a9b05723SYOSHIFUJI Hideaki 				} else if (minihiscore < miniscore) {
1209a9b05723SYOSHIFUJI Hideaki 					struct ipv6_saddr_score *tmp;
121099cd07a5SJuha-Matti Tapio 
1211a9b05723SYOSHIFUJI Hideaki 					if (hiscore->ifa)
1212a9b05723SYOSHIFUJI Hideaki 						in6_ifa_put(hiscore->ifa);
121399cd07a5SJuha-Matti Tapio 
1214a9b05723SYOSHIFUJI Hideaki 					in6_ifa_hold(score->ifa);
1215072047e4SYOSHIFUJI Hideaki 
1216a9b05723SYOSHIFUJI Hideaki 					tmp = hiscore;
12171da177e4SLinus Torvalds 					hiscore = score;
1218a9b05723SYOSHIFUJI Hideaki 					score = tmp;
1219a9b05723SYOSHIFUJI Hideaki 
1220a9b05723SYOSHIFUJI Hideaki 					/* restore our iterator */
1221a9b05723SYOSHIFUJI Hideaki 					score->ifa = hiscore->ifa;
1222a9b05723SYOSHIFUJI Hideaki 
1223a9b05723SYOSHIFUJI Hideaki 					break;
12241da177e4SLinus Torvalds 				}
1225a9b05723SYOSHIFUJI Hideaki 			}
1226a9b05723SYOSHIFUJI Hideaki 		}
1227a9b05723SYOSHIFUJI Hideaki try_nextdev:
12281da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
12291da177e4SLinus Torvalds 	}
12308814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
12311da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
12321da177e4SLinus Torvalds 
1233a9b05723SYOSHIFUJI Hideaki 	if (!hiscore->ifa)
1234072047e4SYOSHIFUJI Hideaki 		return -EADDRNOTAVAIL;
12351da177e4SLinus Torvalds 
1236a9b05723SYOSHIFUJI Hideaki 	ipv6_addr_copy(saddr, &hiscore->ifa->addr);
1237a9b05723SYOSHIFUJI Hideaki 	in6_ifa_put(hiscore->ifa);
1238072047e4SYOSHIFUJI Hideaki 	return 0;
12391da177e4SLinus Torvalds }
12401da177e4SLinus Torvalds 
12415e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr);
12421da177e4SLinus Torvalds 
124395c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
124495c385b4SNeil Horman 		    unsigned char banned_flags)
12451da177e4SLinus Torvalds {
12461da177e4SLinus Torvalds 	struct inet6_dev *idev;
12471da177e4SLinus Torvalds 	int err = -EADDRNOTAVAIL;
12481da177e4SLinus Torvalds 
12498814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
12501da177e4SLinus Torvalds 	if ((idev = __in6_dev_get(dev)) != NULL) {
12511da177e4SLinus Torvalds 		struct inet6_ifaddr *ifp;
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
12541da177e4SLinus Torvalds 		for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
125595c385b4SNeil Horman 			if (ifp->scope == IFA_LINK && !(ifp->flags & banned_flags)) {
12561da177e4SLinus Torvalds 				ipv6_addr_copy(addr, &ifp->addr);
12571da177e4SLinus Torvalds 				err = 0;
12581da177e4SLinus Torvalds 				break;
12591da177e4SLinus Torvalds 			}
12601da177e4SLinus Torvalds 		}
12611da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
12621da177e4SLinus Torvalds 	}
12638814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
12641da177e4SLinus Torvalds 	return err;
12651da177e4SLinus Torvalds }
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev)
12681da177e4SLinus Torvalds {
12691da177e4SLinus Torvalds 	int cnt = 0;
12701da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
12731da177e4SLinus Torvalds 	for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
12741da177e4SLinus Torvalds 		cnt++;
12751da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
12761da177e4SLinus Torvalds 	return cnt;
12771da177e4SLinus Torvalds }
12781da177e4SLinus Torvalds 
1279bfeade08SDaniel Lezcano int ipv6_chk_addr(struct net *net, struct in6_addr *addr,
1280bfeade08SDaniel Lezcano 		  struct net_device *dev, int strict)
12811da177e4SLinus Torvalds {
12821da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
12831da177e4SLinus Torvalds 	u8 hash = ipv6_addr_hash(addr);
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds 	read_lock_bh(&addrconf_hash_lock);
12861da177e4SLinus Torvalds 	for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1287878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
1288bfeade08SDaniel Lezcano 			continue;
12891da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr) &&
12901da177e4SLinus Torvalds 		    !(ifp->flags&IFA_F_TENTATIVE)) {
12911da177e4SLinus Torvalds 			if (dev == NULL || ifp->idev->dev == dev ||
12921da177e4SLinus Torvalds 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
12931da177e4SLinus Torvalds 				break;
12941da177e4SLinus Torvalds 		}
12951da177e4SLinus Torvalds 	}
12961da177e4SLinus Torvalds 	read_unlock_bh(&addrconf_hash_lock);
12971da177e4SLinus Torvalds 	return ifp != NULL;
12981da177e4SLinus Torvalds }
12997159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_addr);
13007159039aSYOSHIFUJI Hideaki 
13011da177e4SLinus Torvalds static
130206bfe655SDaniel Lezcano int ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
130306bfe655SDaniel Lezcano 		       struct net_device *dev)
13041da177e4SLinus Torvalds {
13051da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
13061da177e4SLinus Torvalds 	u8 hash = ipv6_addr_hash(addr);
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 	for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1309878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
131006bfe655SDaniel Lezcano 			continue;
13111da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
13121da177e4SLinus Torvalds 			if (dev == NULL || ifp->idev->dev == dev)
13131da177e4SLinus Torvalds 				break;
13141da177e4SLinus Torvalds 		}
13151da177e4SLinus Torvalds 	}
13161da177e4SLinus Torvalds 	return ifp != NULL;
13171da177e4SLinus Torvalds }
13181da177e4SLinus Torvalds 
131952eeeb84SYOSHIFUJI Hideaki int ipv6_chk_prefix(struct in6_addr *addr, struct net_device *dev)
132052eeeb84SYOSHIFUJI Hideaki {
132152eeeb84SYOSHIFUJI Hideaki 	struct inet6_dev *idev;
132252eeeb84SYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifa;
132352eeeb84SYOSHIFUJI Hideaki 	int	onlink;
132452eeeb84SYOSHIFUJI Hideaki 
132552eeeb84SYOSHIFUJI Hideaki 	onlink = 0;
132652eeeb84SYOSHIFUJI Hideaki 	rcu_read_lock();
132752eeeb84SYOSHIFUJI Hideaki 	idev = __in6_dev_get(dev);
132852eeeb84SYOSHIFUJI Hideaki 	if (idev) {
132952eeeb84SYOSHIFUJI Hideaki 		read_lock_bh(&idev->lock);
133052eeeb84SYOSHIFUJI Hideaki 		for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
133152eeeb84SYOSHIFUJI Hideaki 			onlink = ipv6_prefix_equal(addr, &ifa->addr,
133252eeeb84SYOSHIFUJI Hideaki 						   ifa->prefix_len);
133352eeeb84SYOSHIFUJI Hideaki 			if (onlink)
133452eeeb84SYOSHIFUJI Hideaki 				break;
133552eeeb84SYOSHIFUJI Hideaki 		}
133652eeeb84SYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
133752eeeb84SYOSHIFUJI Hideaki 	}
133852eeeb84SYOSHIFUJI Hideaki 	rcu_read_unlock();
133952eeeb84SYOSHIFUJI Hideaki 	return onlink;
134052eeeb84SYOSHIFUJI Hideaki }
134152eeeb84SYOSHIFUJI Hideaki 
134252eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix);
134352eeeb84SYOSHIFUJI Hideaki 
13449acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
13451cab3da6SDaniel Lezcano 				     struct net_device *dev, int strict)
13461da177e4SLinus Torvalds {
13471da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
13481da177e4SLinus Torvalds 	u8 hash = ipv6_addr_hash(addr);
13491da177e4SLinus Torvalds 
13501da177e4SLinus Torvalds 	read_lock_bh(&addrconf_hash_lock);
13511da177e4SLinus Torvalds 	for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
1352878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
13531cab3da6SDaniel Lezcano 			continue;
13541da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
13551da177e4SLinus Torvalds 			if (dev == NULL || ifp->idev->dev == dev ||
13561da177e4SLinus Torvalds 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
13571da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
13581da177e4SLinus Torvalds 				break;
13591da177e4SLinus Torvalds 			}
13601da177e4SLinus Torvalds 		}
13611da177e4SLinus Torvalds 	}
13621da177e4SLinus Torvalds 	read_unlock_bh(&addrconf_hash_lock);
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds 	return ifp;
13651da177e4SLinus Torvalds }
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
13681da177e4SLinus Torvalds {
13691da177e4SLinus Torvalds 	const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
13700fa1a53eSArnaldo Carvalho de Melo 	const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
137182103232SAl Viro 	__be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
137282103232SAl Viro 	__be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
13731da177e4SLinus Torvalds 	int sk_ipv6only = ipv6_only_sock(sk);
1374463c84b9SArnaldo Carvalho de Melo 	int sk2_ipv6only = inet_v6_ipv6only(sk2);
13751da177e4SLinus Torvalds 	int addr_type = ipv6_addr_type(sk_rcv_saddr6);
13761da177e4SLinus Torvalds 	int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds 	if (!sk2_rcv_saddr && !sk_ipv6only)
13791da177e4SLinus Torvalds 		return 1;
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds 	if (addr_type2 == IPV6_ADDR_ANY &&
13821da177e4SLinus Torvalds 	    !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
13831da177e4SLinus Torvalds 		return 1;
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds 	if (addr_type == IPV6_ADDR_ANY &&
13861da177e4SLinus Torvalds 	    !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
13871da177e4SLinus Torvalds 		return 1;
13881da177e4SLinus Torvalds 
13891da177e4SLinus Torvalds 	if (sk2_rcv_saddr6 &&
13901da177e4SLinus Torvalds 	    ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
13911da177e4SLinus Torvalds 		return 1;
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds 	if (addr_type == IPV6_ADDR_MAPPED &&
13941da177e4SLinus Torvalds 	    !sk2_ipv6only &&
13951da177e4SLinus Torvalds 	    (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
13961da177e4SLinus Torvalds 		return 1;
13971da177e4SLinus Torvalds 
13981da177e4SLinus Torvalds 	return 0;
13991da177e4SLinus Torvalds }
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */
14021da177e4SLinus Torvalds 
14039f5336e2SAdrian Bunk static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
14041da177e4SLinus Torvalds {
14051da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_PERMANENT) {
14061da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
14071da177e4SLinus Torvalds 		addrconf_del_timer(ifp);
14081da177e4SLinus Torvalds 		ifp->flags |= IFA_F_TENTATIVE;
14091da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
14101da177e4SLinus Torvalds 		in6_ifa_put(ifp);
14111da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
14121da177e4SLinus Torvalds 	} else if (ifp->flags&IFA_F_TEMPORARY) {
14131da177e4SLinus Torvalds 		struct inet6_ifaddr *ifpub;
14141da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
14151da177e4SLinus Torvalds 		ifpub = ifp->ifpub;
14161da177e4SLinus Torvalds 		if (ifpub) {
14171da177e4SLinus Torvalds 			in6_ifa_hold(ifpub);
14181da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
14191da177e4SLinus Torvalds 			ipv6_create_tempaddr(ifpub, ifp);
14201da177e4SLinus Torvalds 			in6_ifa_put(ifpub);
14211da177e4SLinus Torvalds 		} else {
14221da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
14231da177e4SLinus Torvalds 		}
14241da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
14251da177e4SLinus Torvalds #endif
14261da177e4SLinus Torvalds 	} else
14271da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
14281da177e4SLinus Torvalds }
14291da177e4SLinus Torvalds 
14303c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp)
14313c21edbdSYOSHIFUJI Hideaki {
14321b34be74SYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
14331b34be74SYOSHIFUJI Hideaki 	if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
14341b34be74SYOSHIFUJI Hideaki 		struct in6_addr addr;
14351b34be74SYOSHIFUJI Hideaki 
14361b34be74SYOSHIFUJI Hideaki 		addr.s6_addr32[0] = htonl(0xfe800000);
14371b34be74SYOSHIFUJI Hideaki 		addr.s6_addr32[1] = 0;
14381b34be74SYOSHIFUJI Hideaki 
14391b34be74SYOSHIFUJI Hideaki 		if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
14401b34be74SYOSHIFUJI Hideaki 		    ipv6_addr_equal(&ifp->addr, &addr)) {
14411b34be74SYOSHIFUJI Hideaki 			/* DAD failed for link-local based on MAC address */
14421b34be74SYOSHIFUJI Hideaki 			idev->cnf.disable_ipv6 = 1;
14431b34be74SYOSHIFUJI Hideaki 		}
14441b34be74SYOSHIFUJI Hideaki 	}
14451b34be74SYOSHIFUJI Hideaki 
14463c21edbdSYOSHIFUJI Hideaki 	if (net_ratelimit())
14473c21edbdSYOSHIFUJI Hideaki 		printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
14483c21edbdSYOSHIFUJI Hideaki 	addrconf_dad_stop(ifp);
14493c21edbdSYOSHIFUJI Hideaki }
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds /* Join to solicited addr multicast group. */
14521da177e4SLinus Torvalds 
14531da177e4SLinus Torvalds void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
14541da177e4SLinus Torvalds {
14551da177e4SLinus Torvalds 	struct in6_addr maddr;
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds 	if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
14581da177e4SLinus Torvalds 		return;
14591da177e4SLinus Torvalds 
14601da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
14611da177e4SLinus Torvalds 	ipv6_dev_mc_inc(dev, &maddr);
14621da177e4SLinus Torvalds }
14631da177e4SLinus Torvalds 
14641da177e4SLinus Torvalds void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
14651da177e4SLinus Torvalds {
14661da177e4SLinus Torvalds 	struct in6_addr maddr;
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds 	if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
14691da177e4SLinus Torvalds 		return;
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
14721da177e4SLinus Torvalds 	__ipv6_dev_mc_dec(idev, &maddr);
14731da177e4SLinus Torvalds }
14741da177e4SLinus Torvalds 
147520380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
14761da177e4SLinus Torvalds {
14771da177e4SLinus Torvalds 	struct in6_addr addr;
14781da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
14791da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
14801da177e4SLinus Torvalds 		return;
14811da177e4SLinus Torvalds 	ipv6_dev_ac_inc(ifp->idev->dev, &addr);
14821da177e4SLinus Torvalds }
14831da177e4SLinus Torvalds 
148420380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
14851da177e4SLinus Torvalds {
14861da177e4SLinus Torvalds 	struct in6_addr addr;
14871da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
14881da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
14891da177e4SLinus Torvalds 		return;
14901da177e4SLinus Torvalds 	__ipv6_dev_ac_dec(ifp->idev, &addr);
14911da177e4SLinus Torvalds }
14921da177e4SLinus Torvalds 
1493073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
14941da177e4SLinus Torvalds {
14951da177e4SLinus Torvalds 	if (dev->addr_len != ETH_ALEN)
14961da177e4SLinus Torvalds 		return -1;
14971da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr, 3);
14981da177e4SLinus Torvalds 	memcpy(eui + 5, dev->dev_addr + 3, 3);
14991da177e4SLinus Torvalds 
15001da177e4SLinus Torvalds 	/*
15011da177e4SLinus Torvalds 	 * The zSeries OSA network cards can be shared among various
15021da177e4SLinus Torvalds 	 * OS instances, but the OSA cards have only one MAC address.
15031da177e4SLinus Torvalds 	 * This leads to duplicate address conflicts in conjunction
15041da177e4SLinus Torvalds 	 * with IPv6 if more than one instance uses the same card.
15051da177e4SLinus Torvalds 	 *
15061da177e4SLinus Torvalds 	 * The driver for these cards can deliver a unique 16-bit
15071da177e4SLinus Torvalds 	 * identifier for each instance sharing the same card.  It is
15081da177e4SLinus Torvalds 	 * placed instead of 0xFFFE in the interface identifier.  The
15091da177e4SLinus Torvalds 	 * "u" bit of the interface identifier is not inverted in this
15101da177e4SLinus Torvalds 	 * case.  Hence the resulting interface identifier has local
15111da177e4SLinus Torvalds 	 * scope according to RFC2373.
15121da177e4SLinus Torvalds 	 */
15131da177e4SLinus Torvalds 	if (dev->dev_id) {
15141da177e4SLinus Torvalds 		eui[3] = (dev->dev_id >> 8) & 0xFF;
15151da177e4SLinus Torvalds 		eui[4] = dev->dev_id & 0xFF;
15161da177e4SLinus Torvalds 	} else {
15171da177e4SLinus Torvalds 		eui[3] = 0xFF;
15181da177e4SLinus Torvalds 		eui[4] = 0xFE;
15191da177e4SLinus Torvalds 		eui[0] ^= 2;
15201da177e4SLinus Torvalds 	}
15211da177e4SLinus Torvalds 	return 0;
1522073a8e0eSYOSHIFUJI Hideaki }
1523073a8e0eSYOSHIFUJI Hideaki 
1524073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1525073a8e0eSYOSHIFUJI Hideaki {
15261da177e4SLinus Torvalds 	/* XXX: inherit EUI-64 from other interface -- yoshfuji */
15271da177e4SLinus Torvalds 	if (dev->addr_len != ARCNET_ALEN)
15281da177e4SLinus Torvalds 		return -1;
15291da177e4SLinus Torvalds 	memset(eui, 0, 7);
15301da177e4SLinus Torvalds 	eui[7] = *(u8*)dev->dev_addr;
15311da177e4SLinus Torvalds 	return 0;
1532073a8e0eSYOSHIFUJI Hideaki }
1533073a8e0eSYOSHIFUJI Hideaki 
1534073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1535073a8e0eSYOSHIFUJI Hideaki {
15361da177e4SLinus Torvalds 	if (dev->addr_len != INFINIBAND_ALEN)
15371da177e4SLinus Torvalds 		return -1;
15381da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr + 12, 8);
15391da177e4SLinus Torvalds 	eui[0] |= 2;
15401da177e4SLinus Torvalds 	return 0;
15411da177e4SLinus Torvalds }
1542073a8e0eSYOSHIFUJI Hideaki 
1543dfd982baSYOSHIFUJI Hideaki int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
1544dfd982baSYOSHIFUJI Hideaki {
1545dfd982baSYOSHIFUJI Hideaki 	eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1546dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1547dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1548dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1549dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1550dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1551dfd982baSYOSHIFUJI Hideaki 	eui[1] = 0;
1552dfd982baSYOSHIFUJI Hideaki 	eui[2] = 0x5E;
1553dfd982baSYOSHIFUJI Hideaki 	eui[3] = 0xFE;
1554dfd982baSYOSHIFUJI Hideaki 	memcpy(eui + 4, &addr, 4);
1555dfd982baSYOSHIFUJI Hideaki 	return 0;
1556dfd982baSYOSHIFUJI Hideaki }
1557dfd982baSYOSHIFUJI Hideaki EXPORT_SYMBOL(__ipv6_isatap_ifid);
1558dfd982baSYOSHIFUJI Hideaki 
1559dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1560dfd982baSYOSHIFUJI Hideaki {
1561dfd982baSYOSHIFUJI Hideaki 	if (dev->priv_flags & IFF_ISATAP)
1562dfd982baSYOSHIFUJI Hideaki 		return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1563dfd982baSYOSHIFUJI Hideaki 	return -1;
1564dfd982baSYOSHIFUJI Hideaki }
1565dfd982baSYOSHIFUJI Hideaki 
1566073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1567073a8e0eSYOSHIFUJI Hideaki {
1568073a8e0eSYOSHIFUJI Hideaki 	switch (dev->type) {
1569073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ETHER:
1570073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_FDDI:
1571073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_IEEE802_TR:
1572073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_eui48(eui, dev);
1573073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ARCNET:
1574073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_arcnet(eui, dev);
1575073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_INFINIBAND:
1576073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_infiniband(eui, dev);
1577c7dc89c0SFred L. Templin 	case ARPHRD_SIT:
1578dfd982baSYOSHIFUJI Hideaki 		return addrconf_ifid_sit(eui, dev);
1579073a8e0eSYOSHIFUJI Hideaki 	}
15801da177e4SLinus Torvalds 	return -1;
15811da177e4SLinus Torvalds }
15821da177e4SLinus Torvalds 
15831da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
15841da177e4SLinus Torvalds {
15851da177e4SLinus Torvalds 	int err = -1;
15861da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
15871da177e4SLinus Torvalds 
15881da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
15891da177e4SLinus Torvalds 	for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
15901da177e4SLinus Torvalds 		if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
15911da177e4SLinus Torvalds 			memcpy(eui, ifp->addr.s6_addr+8, 8);
15921da177e4SLinus Torvalds 			err = 0;
15931da177e4SLinus Torvalds 			break;
15941da177e4SLinus Torvalds 		}
15951da177e4SLinus Torvalds 	}
15961da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
15971da177e4SLinus Torvalds 	return err;
15981da177e4SLinus Torvalds }
15991da177e4SLinus Torvalds 
16001da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
16011da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
16021da177e4SLinus Torvalds static int __ipv6_regen_rndid(struct inet6_dev *idev)
16031da177e4SLinus Torvalds {
16041da177e4SLinus Torvalds regen:
1605955189efSYOSHIFUJI Hideaki 	get_random_bytes(idev->rndid, sizeof(idev->rndid));
16061da177e4SLinus Torvalds 	idev->rndid[0] &= ~0x02;
16071da177e4SLinus Torvalds 
16081da177e4SLinus Torvalds 	/*
16091da177e4SLinus Torvalds 	 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
16101da177e4SLinus Torvalds 	 * check if generated address is not inappropriate
16111da177e4SLinus Torvalds 	 *
16121da177e4SLinus Torvalds 	 *  - Reserved subnet anycast (RFC 2526)
16131da177e4SLinus Torvalds 	 *	11111101 11....11 1xxxxxxx
1614c7dc89c0SFred L. Templin 	 *  - ISATAP (RFC4214) 6.1
16151da177e4SLinus Torvalds 	 *	00-00-5E-FE-xx-xx-xx-xx
16161da177e4SLinus Torvalds 	 *  - value 0
16171da177e4SLinus Torvalds 	 *  - XXX: already assigned to an address on the device
16181da177e4SLinus Torvalds 	 */
16191da177e4SLinus Torvalds 	if (idev->rndid[0] == 0xfd &&
16201da177e4SLinus Torvalds 	    (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
16211da177e4SLinus Torvalds 	    (idev->rndid[7]&0x80))
16221da177e4SLinus Torvalds 		goto regen;
16231da177e4SLinus Torvalds 	if ((idev->rndid[0]|idev->rndid[1]) == 0) {
16241da177e4SLinus Torvalds 		if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
16251da177e4SLinus Torvalds 			goto regen;
16261da177e4SLinus Torvalds 		if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
16271da177e4SLinus Torvalds 			goto regen;
16281da177e4SLinus Torvalds 	}
16291da177e4SLinus Torvalds 
16301da177e4SLinus Torvalds 	return 0;
16311da177e4SLinus Torvalds }
16321da177e4SLinus Torvalds 
16331da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data)
16341da177e4SLinus Torvalds {
16351da177e4SLinus Torvalds 	struct inet6_dev *idev = (struct inet6_dev *) data;
16361da177e4SLinus Torvalds 	unsigned long expires;
16371da177e4SLinus Torvalds 
16388814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
16391da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
16401da177e4SLinus Torvalds 
16411da177e4SLinus Torvalds 	if (idev->dead)
16421da177e4SLinus Torvalds 		goto out;
16431da177e4SLinus Torvalds 
16441da177e4SLinus Torvalds 	if (__ipv6_regen_rndid(idev) < 0)
16451da177e4SLinus Torvalds 		goto out;
16461da177e4SLinus Torvalds 
16471da177e4SLinus Torvalds 	expires = jiffies +
16481da177e4SLinus Torvalds 		idev->cnf.temp_prefered_lft * HZ -
16491da177e4SLinus Torvalds 		idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
16501da177e4SLinus Torvalds 	if (time_before(expires, jiffies)) {
16511da177e4SLinus Torvalds 		printk(KERN_WARNING
16521da177e4SLinus Torvalds 			"ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
16531da177e4SLinus Torvalds 			idev->dev->name);
16541da177e4SLinus Torvalds 		goto out;
16551da177e4SLinus Torvalds 	}
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds 	if (!mod_timer(&idev->regen_timer, expires))
16581da177e4SLinus Torvalds 		in6_dev_hold(idev);
16591da177e4SLinus Torvalds 
16601da177e4SLinus Torvalds out:
16611da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
16628814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
16631da177e4SLinus Torvalds 	in6_dev_put(idev);
16641da177e4SLinus Torvalds }
16651da177e4SLinus Torvalds 
16661da177e4SLinus Torvalds static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
16671da177e4SLinus Torvalds 	int ret = 0;
16681da177e4SLinus Torvalds 
16691da177e4SLinus Torvalds 	if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
16701da177e4SLinus Torvalds 		ret = __ipv6_regen_rndid(idev);
16711da177e4SLinus Torvalds 	return ret;
16721da177e4SLinus Torvalds }
16731da177e4SLinus Torvalds #endif
16741da177e4SLinus Torvalds 
16751da177e4SLinus Torvalds /*
16761da177e4SLinus Torvalds  *	Add prefix route.
16771da177e4SLinus Torvalds  */
16781da177e4SLinus Torvalds 
16791da177e4SLinus Torvalds static void
16801da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1681e431b8c0SJamal Hadi Salim 		      unsigned long expires, u32 flags)
16821da177e4SLinus Torvalds {
168386872cb5SThomas Graf 	struct fib6_config cfg = {
168486872cb5SThomas Graf 		.fc_table = RT6_TABLE_PREFIX,
168586872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
168686872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
168786872cb5SThomas Graf 		.fc_expires = expires,
168886872cb5SThomas Graf 		.fc_dst_len = plen,
168986872cb5SThomas Graf 		.fc_flags = RTF_UP | flags,
1690c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
1691f410a1fbSStephen Hemminger 		.fc_protocol = RTPROT_KERNEL,
169286872cb5SThomas Graf 	};
16931da177e4SLinus Torvalds 
169486872cb5SThomas Graf 	ipv6_addr_copy(&cfg.fc_dst, pfx);
16951da177e4SLinus Torvalds 
16961da177e4SLinus Torvalds 	/* Prevent useless cloning on PtP SIT.
16971da177e4SLinus Torvalds 	   This thing is done here expecting that the whole
16981da177e4SLinus Torvalds 	   class of non-broadcast devices need not cloning.
16991da177e4SLinus Torvalds 	 */
17000be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
17011da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
170286872cb5SThomas Graf 		cfg.fc_flags |= RTF_NONEXTHOP;
17030be669bbSJoerg Roedel #endif
17041da177e4SLinus Torvalds 
170586872cb5SThomas Graf 	ip6_route_add(&cfg);
17061da177e4SLinus Torvalds }
17071da177e4SLinus Torvalds 
17081da177e4SLinus Torvalds /* Create "default" multicast route to the interface */
17091da177e4SLinus Torvalds 
17101da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev)
17111da177e4SLinus Torvalds {
171286872cb5SThomas Graf 	struct fib6_config cfg = {
171386872cb5SThomas Graf 		.fc_table = RT6_TABLE_LOCAL,
171486872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
171586872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
171686872cb5SThomas Graf 		.fc_dst_len = 8,
171786872cb5SThomas Graf 		.fc_flags = RTF_UP,
1718c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
171986872cb5SThomas Graf 	};
17201da177e4SLinus Torvalds 
172186872cb5SThomas Graf 	ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
172286872cb5SThomas Graf 
172386872cb5SThomas Graf 	ip6_route_add(&cfg);
17241da177e4SLinus Torvalds }
17251da177e4SLinus Torvalds 
17260be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
17271da177e4SLinus Torvalds static void sit_route_add(struct net_device *dev)
17281da177e4SLinus Torvalds {
172986872cb5SThomas Graf 	struct fib6_config cfg = {
173086872cb5SThomas Graf 		.fc_table = RT6_TABLE_MAIN,
173186872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
173286872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
173386872cb5SThomas Graf 		.fc_dst_len = 96,
173486872cb5SThomas Graf 		.fc_flags = RTF_UP | RTF_NONEXTHOP,
1735c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
173686872cb5SThomas Graf 	};
17371da177e4SLinus Torvalds 
17381da177e4SLinus Torvalds 	/* prefix length - 96 bits "::d.d.d.d" */
173986872cb5SThomas Graf 	ip6_route_add(&cfg);
17401da177e4SLinus Torvalds }
17410be669bbSJoerg Roedel #endif
17421da177e4SLinus Torvalds 
17431da177e4SLinus Torvalds static void addrconf_add_lroute(struct net_device *dev)
17441da177e4SLinus Torvalds {
17451da177e4SLinus Torvalds 	struct in6_addr addr;
17461da177e4SLinus Torvalds 
17471da177e4SLinus Torvalds 	ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
17481da177e4SLinus Torvalds 	addrconf_prefix_route(&addr, 64, dev, 0, 0);
17491da177e4SLinus Torvalds }
17501da177e4SLinus Torvalds 
17511da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
17521da177e4SLinus Torvalds {
17531da177e4SLinus Torvalds 	struct inet6_dev *idev;
17541da177e4SLinus Torvalds 
17551da177e4SLinus Torvalds 	ASSERT_RTNL();
17561da177e4SLinus Torvalds 
17571da177e4SLinus Torvalds 	if ((idev = ipv6_find_idev(dev)) == NULL)
17581da177e4SLinus Torvalds 		return NULL;
17591da177e4SLinus Torvalds 
17601da177e4SLinus Torvalds 	/* Add default multicast route */
17611da177e4SLinus Torvalds 	addrconf_add_mroute(dev);
17621da177e4SLinus Torvalds 
17631da177e4SLinus Torvalds 	/* Add link local route */
17641da177e4SLinus Torvalds 	addrconf_add_lroute(dev);
17651da177e4SLinus Torvalds 	return idev;
17661da177e4SLinus Torvalds }
17671da177e4SLinus Torvalds 
17681da177e4SLinus Torvalds void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
17691da177e4SLinus Torvalds {
17701da177e4SLinus Torvalds 	struct prefix_info *pinfo;
17711da177e4SLinus Torvalds 	__u32 valid_lft;
17721da177e4SLinus Torvalds 	__u32 prefered_lft;
17731da177e4SLinus Torvalds 	int addr_type;
17741da177e4SLinus Torvalds 	struct inet6_dev *in6_dev;
17751da177e4SLinus Torvalds 
17761da177e4SLinus Torvalds 	pinfo = (struct prefix_info *) opt;
17771da177e4SLinus Torvalds 
17781da177e4SLinus Torvalds 	if (len < sizeof(struct prefix_info)) {
17791da177e4SLinus Torvalds 		ADBG(("addrconf: prefix option too short\n"));
17801da177e4SLinus Torvalds 		return;
17811da177e4SLinus Torvalds 	}
17821da177e4SLinus Torvalds 
17831da177e4SLinus Torvalds 	/*
17841da177e4SLinus Torvalds 	 *	Validation checks ([ADDRCONF], page 19)
17851da177e4SLinus Torvalds 	 */
17861da177e4SLinus Torvalds 
17871da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&pinfo->prefix);
17881da177e4SLinus Torvalds 
17891da177e4SLinus Torvalds 	if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
17901da177e4SLinus Torvalds 		return;
17911da177e4SLinus Torvalds 
17921da177e4SLinus Torvalds 	valid_lft = ntohl(pinfo->valid);
17931da177e4SLinus Torvalds 	prefered_lft = ntohl(pinfo->prefered);
17941da177e4SLinus Torvalds 
17951da177e4SLinus Torvalds 	if (prefered_lft > valid_lft) {
17961da177e4SLinus Torvalds 		if (net_ratelimit())
17971da177e4SLinus Torvalds 			printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
17981da177e4SLinus Torvalds 		return;
17991da177e4SLinus Torvalds 	}
18001da177e4SLinus Torvalds 
18011da177e4SLinus Torvalds 	in6_dev = in6_dev_get(dev);
18021da177e4SLinus Torvalds 
18031da177e4SLinus Torvalds 	if (in6_dev == NULL) {
18041da177e4SLinus Torvalds 		if (net_ratelimit())
18051da177e4SLinus Torvalds 			printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
18061da177e4SLinus Torvalds 		return;
18071da177e4SLinus Torvalds 	}
18081da177e4SLinus Torvalds 
18091da177e4SLinus Torvalds 	/*
18101da177e4SLinus Torvalds 	 *	Two things going on here:
18111da177e4SLinus Torvalds 	 *	1) Add routes for on-link prefixes
18121da177e4SLinus Torvalds 	 *	2) Configure prefixes with the auto flag set
18131da177e4SLinus Torvalds 	 */
18141da177e4SLinus Torvalds 
18154bed72e4SYOSHIFUJI Hideaki 	if (pinfo->onlink) {
18164bed72e4SYOSHIFUJI Hideaki 		struct rt6_info *rt;
18174bed72e4SYOSHIFUJI Hideaki 		unsigned long rt_expires;
18184bed72e4SYOSHIFUJI Hideaki 
18191da177e4SLinus Torvalds 		/* Avoid arithmetic overflow. Really, we could
18206f704992SYOSHIFUJI Hideaki 		 * save rt_expires in seconds, likely valid_lft,
18216f704992SYOSHIFUJI Hideaki 		 * but it would require division in fib gc, that it
18226f704992SYOSHIFUJI Hideaki 		 * not good.
18231da177e4SLinus Torvalds 		 */
18244bed72e4SYOSHIFUJI Hideaki 		if (HZ > USER_HZ)
18254bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
18264bed72e4SYOSHIFUJI Hideaki 		else
18274bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
18283dd4bc68SYOSHIFUJI Hideaki 
18294bed72e4SYOSHIFUJI Hideaki 		if (addrconf_finite_timeout(rt_expires))
18304bed72e4SYOSHIFUJI Hideaki 			rt_expires *= HZ;
18311da177e4SLinus Torvalds 
1832c346dca1SYOSHIFUJI Hideaki 		rt = rt6_lookup(dev_net(dev), &pinfo->prefix, NULL,
18336fda7350SBenjamin Thery 				dev->ifindex, 1);
18341da177e4SLinus Torvalds 
18352b5ead46SYOSHIFUJI Hideaki 		if (rt && addrconf_is_prefix_route(rt)) {
18366f704992SYOSHIFUJI Hideaki 			/* Autoconf prefix route */
18371da177e4SLinus Torvalds 			if (valid_lft == 0) {
1838e0a1ad73SThomas Graf 				ip6_del_rt(rt);
18391da177e4SLinus Torvalds 				rt = NULL;
18404bed72e4SYOSHIFUJI Hideaki 			} else if (addrconf_finite_timeout(rt_expires)) {
18416f704992SYOSHIFUJI Hideaki 				/* not infinity */
18423dd4bc68SYOSHIFUJI Hideaki 				rt->rt6i_expires = jiffies + rt_expires;
18436f704992SYOSHIFUJI Hideaki 				rt->rt6i_flags |= RTF_EXPIRES;
18446f704992SYOSHIFUJI Hideaki 			} else {
18456f704992SYOSHIFUJI Hideaki 				rt->rt6i_flags &= ~RTF_EXPIRES;
18466f704992SYOSHIFUJI Hideaki 				rt->rt6i_expires = 0;
18471da177e4SLinus Torvalds 			}
18481da177e4SLinus Torvalds 		} else if (valid_lft) {
18496f704992SYOSHIFUJI Hideaki 			clock_t expires = 0;
18504bed72e4SYOSHIFUJI Hideaki 			int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
18514bed72e4SYOSHIFUJI Hideaki 			if (addrconf_finite_timeout(rt_expires)) {
18526f704992SYOSHIFUJI Hideaki 				/* not infinity */
18536f704992SYOSHIFUJI Hideaki 				flags |= RTF_EXPIRES;
18546f704992SYOSHIFUJI Hideaki 				expires = jiffies_to_clock_t(rt_expires);
18556f704992SYOSHIFUJI Hideaki 			}
18561da177e4SLinus Torvalds 			addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
18576f704992SYOSHIFUJI Hideaki 					      dev, expires, flags);
18581da177e4SLinus Torvalds 		}
18591da177e4SLinus Torvalds 		if (rt)
18601da177e4SLinus Torvalds 			dst_release(&rt->u.dst);
18611da177e4SLinus Torvalds 	}
18621da177e4SLinus Torvalds 
18631da177e4SLinus Torvalds 	/* Try to figure out our local address for this prefix */
18641da177e4SLinus Torvalds 
18651da177e4SLinus Torvalds 	if (pinfo->autoconf && in6_dev->cnf.autoconf) {
18661da177e4SLinus Torvalds 		struct inet6_ifaddr * ifp;
18671da177e4SLinus Torvalds 		struct in6_addr addr;
18681da177e4SLinus Torvalds 		int create = 0, update_lft = 0;
186953b7997fSYOSHIFUJI Hideaki 		struct net *net = dev_net(dev);
18701da177e4SLinus Torvalds 
18711da177e4SLinus Torvalds 		if (pinfo->prefix_len == 64) {
18721da177e4SLinus Torvalds 			memcpy(&addr, &pinfo->prefix, 8);
18731da177e4SLinus Torvalds 			if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
18741da177e4SLinus Torvalds 			    ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
18751da177e4SLinus Torvalds 				in6_dev_put(in6_dev);
18761da177e4SLinus Torvalds 				return;
18771da177e4SLinus Torvalds 			}
18781da177e4SLinus Torvalds 			goto ok;
18791da177e4SLinus Torvalds 		}
18801da177e4SLinus Torvalds 		if (net_ratelimit())
18811da177e4SLinus Torvalds 			printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
18821da177e4SLinus Torvalds 			       pinfo->prefix_len);
18831da177e4SLinus Torvalds 		in6_dev_put(in6_dev);
18841da177e4SLinus Torvalds 		return;
18851da177e4SLinus Torvalds 
18861da177e4SLinus Torvalds ok:
18871da177e4SLinus Torvalds 
188853b7997fSYOSHIFUJI Hideaki 		ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
18891da177e4SLinus Torvalds 
18901da177e4SLinus Torvalds 		if (ifp == NULL && valid_lft) {
18911da177e4SLinus Torvalds 			int max_addresses = in6_dev->cnf.max_addresses;
189295c385b4SNeil Horman 			u32 addr_flags = 0;
189395c385b4SNeil Horman 
189495c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
189595c385b4SNeil Horman 			if (in6_dev->cnf.optimistic_dad &&
189653b7997fSYOSHIFUJI Hideaki 			    !net->ipv6.devconf_all->forwarding)
189795c385b4SNeil Horman 				addr_flags = IFA_F_OPTIMISTIC;
189895c385b4SNeil Horman #endif
18991da177e4SLinus Torvalds 
19001da177e4SLinus Torvalds 			/* Do not allow to create too much of autoconfigured
19011da177e4SLinus Torvalds 			 * addresses; this would be too easy way to crash kernel.
19021da177e4SLinus Torvalds 			 */
19031da177e4SLinus Torvalds 			if (!max_addresses ||
19041da177e4SLinus Torvalds 			    ipv6_count_addresses(in6_dev) < max_addresses)
19051da177e4SLinus Torvalds 				ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
190695c385b4SNeil Horman 						    addr_type&IPV6_ADDR_SCOPE_MASK,
190795c385b4SNeil Horman 						    addr_flags);
19081da177e4SLinus Torvalds 
19091da177e4SLinus Torvalds 			if (!ifp || IS_ERR(ifp)) {
19101da177e4SLinus Torvalds 				in6_dev_put(in6_dev);
19111da177e4SLinus Torvalds 				return;
19121da177e4SLinus Torvalds 			}
19131da177e4SLinus Torvalds 
19141da177e4SLinus Torvalds 			update_lft = create = 1;
19151da177e4SLinus Torvalds 			ifp->cstamp = jiffies;
19161da177e4SLinus Torvalds 			addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
19171da177e4SLinus Torvalds 		}
19181da177e4SLinus Torvalds 
19191da177e4SLinus Torvalds 		if (ifp) {
19201da177e4SLinus Torvalds 			int flags;
19211da177e4SLinus Torvalds 			unsigned long now;
19221da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
19231da177e4SLinus Torvalds 			struct inet6_ifaddr *ift;
19241da177e4SLinus Torvalds #endif
19251da177e4SLinus Torvalds 			u32 stored_lft;
19261da177e4SLinus Torvalds 
19271da177e4SLinus Torvalds 			/* update lifetime (RFC2462 5.5.3 e) */
19281da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
19291da177e4SLinus Torvalds 			now = jiffies;
19301da177e4SLinus Torvalds 			if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
19311da177e4SLinus Torvalds 				stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
19321da177e4SLinus Torvalds 			else
19331da177e4SLinus Torvalds 				stored_lft = 0;
19341da177e4SLinus Torvalds 			if (!update_lft && stored_lft) {
19351da177e4SLinus Torvalds 				if (valid_lft > MIN_VALID_LIFETIME ||
19361da177e4SLinus Torvalds 				    valid_lft > stored_lft)
19371da177e4SLinus Torvalds 					update_lft = 1;
19381da177e4SLinus Torvalds 				else if (stored_lft <= MIN_VALID_LIFETIME) {
19391da177e4SLinus Torvalds 					/* valid_lft <= stored_lft is always true */
19401da177e4SLinus Torvalds 					/* XXX: IPsec */
19411da177e4SLinus Torvalds 					update_lft = 0;
19421da177e4SLinus Torvalds 				} else {
19431da177e4SLinus Torvalds 					valid_lft = MIN_VALID_LIFETIME;
19441da177e4SLinus Torvalds 					if (valid_lft < prefered_lft)
19451da177e4SLinus Torvalds 						prefered_lft = valid_lft;
19461da177e4SLinus Torvalds 					update_lft = 1;
19471da177e4SLinus Torvalds 				}
19481da177e4SLinus Torvalds 			}
19491da177e4SLinus Torvalds 
19501da177e4SLinus Torvalds 			if (update_lft) {
19511da177e4SLinus Torvalds 				ifp->valid_lft = valid_lft;
19521da177e4SLinus Torvalds 				ifp->prefered_lft = prefered_lft;
19531da177e4SLinus Torvalds 				ifp->tstamp = now;
19541da177e4SLinus Torvalds 				flags = ifp->flags;
19551da177e4SLinus Torvalds 				ifp->flags &= ~IFA_F_DEPRECATED;
19561da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
19571da177e4SLinus Torvalds 
19581da177e4SLinus Torvalds 				if (!(flags&IFA_F_TENTATIVE))
19591da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
19601da177e4SLinus Torvalds 			} else
19611da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
19621da177e4SLinus Torvalds 
19631da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
19641da177e4SLinus Torvalds 			read_lock_bh(&in6_dev->lock);
19651da177e4SLinus Torvalds 			/* update all temporary addresses in the list */
19661da177e4SLinus Torvalds 			for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
19671da177e4SLinus Torvalds 				/*
19681da177e4SLinus Torvalds 				 * When adjusting the lifetimes of an existing
19691da177e4SLinus Torvalds 				 * temporary address, only lower the lifetimes.
19701da177e4SLinus Torvalds 				 * Implementations must not increase the
19711da177e4SLinus Torvalds 				 * lifetimes of an existing temporary address
19721da177e4SLinus Torvalds 				 * when processing a Prefix Information Option.
19731da177e4SLinus Torvalds 				 */
1974c6fbfac2SBenoit Boissinot 				if (ifp != ift->ifpub)
1975c6fbfac2SBenoit Boissinot 					continue;
1976c6fbfac2SBenoit Boissinot 
19771da177e4SLinus Torvalds 				spin_lock(&ift->lock);
19781da177e4SLinus Torvalds 				flags = ift->flags;
19791da177e4SLinus Torvalds 				if (ift->valid_lft > valid_lft &&
19801da177e4SLinus Torvalds 				    ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
19811da177e4SLinus Torvalds 					ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
19821da177e4SLinus Torvalds 				if (ift->prefered_lft > prefered_lft &&
19831da177e4SLinus Torvalds 				    ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
19841da177e4SLinus Torvalds 					ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
19851da177e4SLinus Torvalds 				spin_unlock(&ift->lock);
19861da177e4SLinus Torvalds 				if (!(flags&IFA_F_TENTATIVE))
19871da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ift);
19881da177e4SLinus Torvalds 			}
19891da177e4SLinus Torvalds 
19901da177e4SLinus Torvalds 			if (create && in6_dev->cnf.use_tempaddr > 0) {
19911da177e4SLinus Torvalds 				/*
19921da177e4SLinus Torvalds 				 * When a new public address is created as described in [ADDRCONF],
19931da177e4SLinus Torvalds 				 * also create a new temporary address.
19941da177e4SLinus Torvalds 				 */
19951da177e4SLinus Torvalds 				read_unlock_bh(&in6_dev->lock);
19961da177e4SLinus Torvalds 				ipv6_create_tempaddr(ifp, NULL);
19971da177e4SLinus Torvalds 			} else {
19981da177e4SLinus Torvalds 				read_unlock_bh(&in6_dev->lock);
19991da177e4SLinus Torvalds 			}
20001da177e4SLinus Torvalds #endif
20011da177e4SLinus Torvalds 			in6_ifa_put(ifp);
20021da177e4SLinus Torvalds 			addrconf_verify(0);
20031da177e4SLinus Torvalds 		}
20041da177e4SLinus Torvalds 	}
20051da177e4SLinus Torvalds 	inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
20061da177e4SLinus Torvalds 	in6_dev_put(in6_dev);
20071da177e4SLinus Torvalds }
20081da177e4SLinus Torvalds 
20091da177e4SLinus Torvalds /*
20101da177e4SLinus Torvalds  *	Set destination address.
20111da177e4SLinus Torvalds  *	Special case for SIT interfaces where we create a new "virtual"
20121da177e4SLinus Torvalds  *	device.
20131da177e4SLinus Torvalds  */
2014af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg)
20151da177e4SLinus Torvalds {
20161da177e4SLinus Torvalds 	struct in6_ifreq ireq;
20171da177e4SLinus Torvalds 	struct net_device *dev;
20181da177e4SLinus Torvalds 	int err = -EINVAL;
20191da177e4SLinus Torvalds 
20201da177e4SLinus Torvalds 	rtnl_lock();
20211da177e4SLinus Torvalds 
20221da177e4SLinus Torvalds 	err = -EFAULT;
20231da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
20241da177e4SLinus Torvalds 		goto err_exit;
20251da177e4SLinus Torvalds 
2026af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
20271da177e4SLinus Torvalds 
20281da177e4SLinus Torvalds 	err = -ENODEV;
20291da177e4SLinus Torvalds 	if (dev == NULL)
20301da177e4SLinus Torvalds 		goto err_exit;
20311da177e4SLinus Torvalds 
20320be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
20331da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT) {
20341da177e4SLinus Torvalds 		struct ifreq ifr;
20351da177e4SLinus Torvalds 		mm_segment_t	oldfs;
20361da177e4SLinus Torvalds 		struct ip_tunnel_parm p;
20371da177e4SLinus Torvalds 
20381da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
20391da177e4SLinus Torvalds 		if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
20401da177e4SLinus Torvalds 			goto err_exit;
20411da177e4SLinus Torvalds 
20421da177e4SLinus Torvalds 		memset(&p, 0, sizeof(p));
20431da177e4SLinus Torvalds 		p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
20441da177e4SLinus Torvalds 		p.iph.saddr = 0;
20451da177e4SLinus Torvalds 		p.iph.version = 4;
20461da177e4SLinus Torvalds 		p.iph.ihl = 5;
20471da177e4SLinus Torvalds 		p.iph.protocol = IPPROTO_IPV6;
20481da177e4SLinus Torvalds 		p.iph.ttl = 64;
2049d20b3109SStephen Hemminger 		ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
20501da177e4SLinus Torvalds 
20511da177e4SLinus Torvalds 		oldfs = get_fs(); set_fs(KERNEL_DS);
20521da177e4SLinus Torvalds 		err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
20531da177e4SLinus Torvalds 		set_fs(oldfs);
20541da177e4SLinus Torvalds 
20551da177e4SLinus Torvalds 		if (err == 0) {
20561da177e4SLinus Torvalds 			err = -ENOBUFS;
2057af284937SDaniel Lezcano 			dev = __dev_get_by_name(net, p.name);
2058af284937SDaniel Lezcano 			if (!dev)
20591da177e4SLinus Torvalds 				goto err_exit;
20601da177e4SLinus Torvalds 			err = dev_open(dev);
20611da177e4SLinus Torvalds 		}
20621da177e4SLinus Torvalds 	}
20630be669bbSJoerg Roedel #endif
20641da177e4SLinus Torvalds 
20651da177e4SLinus Torvalds err_exit:
20661da177e4SLinus Torvalds 	rtnl_unlock();
20671da177e4SLinus Torvalds 	return err;
20681da177e4SLinus Torvalds }
20691da177e4SLinus Torvalds 
20701da177e4SLinus Torvalds /*
20711da177e4SLinus Torvalds  *	Manual configuration of address on an interface
20721da177e4SLinus Torvalds  */
2073af284937SDaniel Lezcano static int inet6_addr_add(struct net *net, int ifindex, struct in6_addr *pfx,
207424ef0da7SThomas Graf 			  unsigned int plen, __u8 ifa_flags, __u32 prefered_lft,
2075af284937SDaniel Lezcano 			  __u32 valid_lft)
20761da177e4SLinus Torvalds {
20771da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
20781da177e4SLinus Torvalds 	struct inet6_dev *idev;
20791da177e4SLinus Torvalds 	struct net_device *dev;
20801da177e4SLinus Torvalds 	int scope;
20816f704992SYOSHIFUJI Hideaki 	u32 flags;
20826f704992SYOSHIFUJI Hideaki 	clock_t expires;
20834bed72e4SYOSHIFUJI Hideaki 	unsigned long timeout;
20841da177e4SLinus Torvalds 
20851da177e4SLinus Torvalds 	ASSERT_RTNL();
20861da177e4SLinus Torvalds 
208724ef0da7SThomas Graf 	if (plen > 128)
208824ef0da7SThomas Graf 		return -EINVAL;
208924ef0da7SThomas Graf 
20900778769dSNoriaki TAKAMIYA 	/* check the lifetime */
20910778769dSNoriaki TAKAMIYA 	if (!valid_lft || prefered_lft > valid_lft)
20920778769dSNoriaki TAKAMIYA 		return -EINVAL;
20930778769dSNoriaki TAKAMIYA 
2094af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2095af284937SDaniel Lezcano 	if (!dev)
20961da177e4SLinus Torvalds 		return -ENODEV;
20971da177e4SLinus Torvalds 
20981da177e4SLinus Torvalds 	if ((idev = addrconf_add_dev(dev)) == NULL)
20991da177e4SLinus Torvalds 		return -ENOBUFS;
21001da177e4SLinus Torvalds 
21011da177e4SLinus Torvalds 	scope = ipv6_addr_scope(pfx);
21021da177e4SLinus Torvalds 
21034bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
21044bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
21054bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
21064bed72e4SYOSHIFUJI Hideaki 		valid_lft = timeout;
21076f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
21084bed72e4SYOSHIFUJI Hideaki 	} else {
21094bed72e4SYOSHIFUJI Hideaki 		expires = 0;
21104bed72e4SYOSHIFUJI Hideaki 		flags = 0;
21114bed72e4SYOSHIFUJI Hideaki 		ifa_flags |= IFA_F_PERMANENT;
21126f704992SYOSHIFUJI Hideaki 	}
21130778769dSNoriaki TAKAMIYA 
21144bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
21154bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
21164bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
21170778769dSNoriaki TAKAMIYA 			ifa_flags |= IFA_F_DEPRECATED;
21184bed72e4SYOSHIFUJI Hideaki 		prefered_lft = timeout;
21194bed72e4SYOSHIFUJI Hideaki 	}
21200778769dSNoriaki TAKAMIYA 
21210778769dSNoriaki TAKAMIYA 	ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
21220778769dSNoriaki TAKAMIYA 
21231da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
212406aebfb7SHerbert Xu 		spin_lock_bh(&ifp->lock);
21250778769dSNoriaki TAKAMIYA 		ifp->valid_lft = valid_lft;
21260778769dSNoriaki TAKAMIYA 		ifp->prefered_lft = prefered_lft;
21270778769dSNoriaki TAKAMIYA 		ifp->tstamp = jiffies;
212806aebfb7SHerbert Xu 		spin_unlock_bh(&ifp->lock);
21290778769dSNoriaki TAKAMIYA 
213046d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
21316f704992SYOSHIFUJI Hideaki 				      expires, flags);
213295c385b4SNeil Horman 		/*
213395c385b4SNeil Horman 		 * Note that section 3.1 of RFC 4429 indicates
213495c385b4SNeil Horman 		 * that the Optimistic flag should not be set for
213595c385b4SNeil Horman 		 * manually configured addresses
213695c385b4SNeil Horman 		 */
21371da177e4SLinus Torvalds 		addrconf_dad_start(ifp, 0);
21381da177e4SLinus Torvalds 		in6_ifa_put(ifp);
21390778769dSNoriaki TAKAMIYA 		addrconf_verify(0);
21401da177e4SLinus Torvalds 		return 0;
21411da177e4SLinus Torvalds 	}
21421da177e4SLinus Torvalds 
21431da177e4SLinus Torvalds 	return PTR_ERR(ifp);
21441da177e4SLinus Torvalds }
21451da177e4SLinus Torvalds 
2146af284937SDaniel Lezcano static int inet6_addr_del(struct net *net, int ifindex, struct in6_addr *pfx,
214724ef0da7SThomas Graf 			  unsigned int plen)
21481da177e4SLinus Torvalds {
21491da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
21501da177e4SLinus Torvalds 	struct inet6_dev *idev;
21511da177e4SLinus Torvalds 	struct net_device *dev;
21521da177e4SLinus Torvalds 
215324ef0da7SThomas Graf 	if (plen > 128)
215424ef0da7SThomas Graf 		return -EINVAL;
215524ef0da7SThomas Graf 
2156af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2157af284937SDaniel Lezcano 	if (!dev)
21581da177e4SLinus Torvalds 		return -ENODEV;
21591da177e4SLinus Torvalds 
21601da177e4SLinus Torvalds 	if ((idev = __in6_dev_get(dev)) == NULL)
21611da177e4SLinus Torvalds 		return -ENXIO;
21621da177e4SLinus Torvalds 
21631da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
21641da177e4SLinus Torvalds 	for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
21651da177e4SLinus Torvalds 		if (ifp->prefix_len == plen &&
21661da177e4SLinus Torvalds 		    ipv6_addr_equal(pfx, &ifp->addr)) {
21671da177e4SLinus Torvalds 			in6_ifa_hold(ifp);
21681da177e4SLinus Torvalds 			read_unlock_bh(&idev->lock);
21691da177e4SLinus Torvalds 
21701da177e4SLinus Torvalds 			ipv6_del_addr(ifp);
21711da177e4SLinus Torvalds 
21721da177e4SLinus Torvalds 			/* If the last address is deleted administratively,
21731da177e4SLinus Torvalds 			   disable IPv6 on this interface.
21741da177e4SLinus Torvalds 			 */
21751da177e4SLinus Torvalds 			if (idev->addr_list == NULL)
21761da177e4SLinus Torvalds 				addrconf_ifdown(idev->dev, 1);
21771da177e4SLinus Torvalds 			return 0;
21781da177e4SLinus Torvalds 		}
21791da177e4SLinus Torvalds 	}
21801da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
21811da177e4SLinus Torvalds 	return -EADDRNOTAVAIL;
21821da177e4SLinus Torvalds }
21831da177e4SLinus Torvalds 
21841da177e4SLinus Torvalds 
2185af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg)
21861da177e4SLinus Torvalds {
21871da177e4SLinus Torvalds 	struct in6_ifreq ireq;
21881da177e4SLinus Torvalds 	int err;
21891da177e4SLinus Torvalds 
21901da177e4SLinus Torvalds 	if (!capable(CAP_NET_ADMIN))
21911da177e4SLinus Torvalds 		return -EPERM;
21921da177e4SLinus Torvalds 
21931da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
21941da177e4SLinus Torvalds 		return -EFAULT;
21951da177e4SLinus Torvalds 
21961da177e4SLinus Torvalds 	rtnl_lock();
2197af284937SDaniel Lezcano 	err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2198af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2199af284937SDaniel Lezcano 			     INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
22001da177e4SLinus Torvalds 	rtnl_unlock();
22011da177e4SLinus Torvalds 	return err;
22021da177e4SLinus Torvalds }
22031da177e4SLinus Torvalds 
2204af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg)
22051da177e4SLinus Torvalds {
22061da177e4SLinus Torvalds 	struct in6_ifreq ireq;
22071da177e4SLinus Torvalds 	int err;
22081da177e4SLinus Torvalds 
22091da177e4SLinus Torvalds 	if (!capable(CAP_NET_ADMIN))
22101da177e4SLinus Torvalds 		return -EPERM;
22111da177e4SLinus Torvalds 
22121da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
22131da177e4SLinus Torvalds 		return -EFAULT;
22141da177e4SLinus Torvalds 
22151da177e4SLinus Torvalds 	rtnl_lock();
2216af284937SDaniel Lezcano 	err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2217af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen);
22181da177e4SLinus Torvalds 	rtnl_unlock();
22191da177e4SLinus Torvalds 	return err;
22201da177e4SLinus Torvalds }
22211da177e4SLinus Torvalds 
22220be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
22231da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev)
22241da177e4SLinus Torvalds {
22251da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
22261da177e4SLinus Torvalds 	struct in6_addr addr;
22271da177e4SLinus Torvalds 	struct net_device *dev;
2228c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
22291da177e4SLinus Torvalds 	int scope;
22301da177e4SLinus Torvalds 
22311da177e4SLinus Torvalds 	ASSERT_RTNL();
22321da177e4SLinus Torvalds 
22331da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
22341da177e4SLinus Torvalds 	memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
22351da177e4SLinus Torvalds 
22361da177e4SLinus Torvalds 	if (idev->dev->flags&IFF_POINTOPOINT) {
22371da177e4SLinus Torvalds 		addr.s6_addr32[0] = htonl(0xfe800000);
22381da177e4SLinus Torvalds 		scope = IFA_LINK;
22391da177e4SLinus Torvalds 	} else {
22401da177e4SLinus Torvalds 		scope = IPV6_ADDR_COMPATv4;
22411da177e4SLinus Torvalds 	}
22421da177e4SLinus Torvalds 
22431da177e4SLinus Torvalds 	if (addr.s6_addr32[3]) {
22441da177e4SLinus Torvalds 		ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
22451da177e4SLinus Torvalds 		if (!IS_ERR(ifp)) {
22461da177e4SLinus Torvalds 			spin_lock_bh(&ifp->lock);
22471da177e4SLinus Torvalds 			ifp->flags &= ~IFA_F_TENTATIVE;
22481da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
22491da177e4SLinus Torvalds 			ipv6_ifa_notify(RTM_NEWADDR, ifp);
22501da177e4SLinus Torvalds 			in6_ifa_put(ifp);
22511da177e4SLinus Torvalds 		}
22521da177e4SLinus Torvalds 		return;
22531da177e4SLinus Torvalds 	}
22541da177e4SLinus Torvalds 
22556fda7350SBenjamin Thery 	for_each_netdev(net, dev) {
2256e5ed6399SHerbert Xu 		struct in_device * in_dev = __in_dev_get_rtnl(dev);
22571da177e4SLinus Torvalds 		if (in_dev && (dev->flags & IFF_UP)) {
22581da177e4SLinus Torvalds 			struct in_ifaddr * ifa;
22591da177e4SLinus Torvalds 
22601da177e4SLinus Torvalds 			int flag = scope;
22611da177e4SLinus Torvalds 
22621da177e4SLinus Torvalds 			for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
22631da177e4SLinus Torvalds 				int plen;
22641da177e4SLinus Torvalds 
22651da177e4SLinus Torvalds 				addr.s6_addr32[3] = ifa->ifa_local;
22661da177e4SLinus Torvalds 
22671da177e4SLinus Torvalds 				if (ifa->ifa_scope == RT_SCOPE_LINK)
22681da177e4SLinus Torvalds 					continue;
22691da177e4SLinus Torvalds 				if (ifa->ifa_scope >= RT_SCOPE_HOST) {
22701da177e4SLinus Torvalds 					if (idev->dev->flags&IFF_POINTOPOINT)
22711da177e4SLinus Torvalds 						continue;
22721da177e4SLinus Torvalds 					flag |= IFA_HOST;
22731da177e4SLinus Torvalds 				}
22741da177e4SLinus Torvalds 				if (idev->dev->flags&IFF_POINTOPOINT)
22751da177e4SLinus Torvalds 					plen = 64;
22761da177e4SLinus Torvalds 				else
22771da177e4SLinus Torvalds 					plen = 96;
22781da177e4SLinus Torvalds 
22791da177e4SLinus Torvalds 				ifp = ipv6_add_addr(idev, &addr, plen, flag,
22801da177e4SLinus Torvalds 						    IFA_F_PERMANENT);
22811da177e4SLinus Torvalds 				if (!IS_ERR(ifp)) {
22821da177e4SLinus Torvalds 					spin_lock_bh(&ifp->lock);
22831da177e4SLinus Torvalds 					ifp->flags &= ~IFA_F_TENTATIVE;
22841da177e4SLinus Torvalds 					spin_unlock_bh(&ifp->lock);
22851da177e4SLinus Torvalds 					ipv6_ifa_notify(RTM_NEWADDR, ifp);
22861da177e4SLinus Torvalds 					in6_ifa_put(ifp);
22871da177e4SLinus Torvalds 				}
22881da177e4SLinus Torvalds 			}
22891da177e4SLinus Torvalds 		}
22901da177e4SLinus Torvalds 	}
22911da177e4SLinus Torvalds }
22920be669bbSJoerg Roedel #endif
22931da177e4SLinus Torvalds 
22941da177e4SLinus Torvalds static void init_loopback(struct net_device *dev)
22951da177e4SLinus Torvalds {
22961da177e4SLinus Torvalds 	struct inet6_dev  *idev;
22971da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
22981da177e4SLinus Torvalds 
22991da177e4SLinus Torvalds 	/* ::1 */
23001da177e4SLinus Torvalds 
23011da177e4SLinus Torvalds 	ASSERT_RTNL();
23021da177e4SLinus Torvalds 
23031da177e4SLinus Torvalds 	if ((idev = ipv6_find_idev(dev)) == NULL) {
23041da177e4SLinus Torvalds 		printk(KERN_DEBUG "init loopback: add_dev failed\n");
23051da177e4SLinus Torvalds 		return;
23061da177e4SLinus Torvalds 	}
23071da177e4SLinus Torvalds 
23081da177e4SLinus Torvalds 	ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
23091da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
23101da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
23111da177e4SLinus Torvalds 		ifp->flags &= ~IFA_F_TENTATIVE;
23121da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
23131da177e4SLinus Torvalds 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
23141da177e4SLinus Torvalds 		in6_ifa_put(ifp);
23151da177e4SLinus Torvalds 	}
23161da177e4SLinus Torvalds }
23171da177e4SLinus Torvalds 
23181da177e4SLinus Torvalds static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
23191da177e4SLinus Torvalds {
23201da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
232195c385b4SNeil Horman 	u32 addr_flags = IFA_F_PERMANENT;
23221da177e4SLinus Torvalds 
232395c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
232495c385b4SNeil Horman 	if (idev->cnf.optimistic_dad &&
2325702beb87SDavid Miller 	    !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
232695c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
232795c385b4SNeil Horman #endif
232895c385b4SNeil Horman 
232995c385b4SNeil Horman 
233095c385b4SNeil Horman 	ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags);
23311da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
233246d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
23331da177e4SLinus Torvalds 		addrconf_dad_start(ifp, 0);
23341da177e4SLinus Torvalds 		in6_ifa_put(ifp);
23351da177e4SLinus Torvalds 	}
23361da177e4SLinus Torvalds }
23371da177e4SLinus Torvalds 
23381da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev)
23391da177e4SLinus Torvalds {
23401da177e4SLinus Torvalds 	struct in6_addr addr;
23411da177e4SLinus Torvalds 	struct inet6_dev    * idev;
23421da177e4SLinus Torvalds 
23431da177e4SLinus Torvalds 	ASSERT_RTNL();
23441da177e4SLinus Torvalds 
234574235a25SHerbert Xu 	if ((dev->type != ARPHRD_ETHER) &&
234674235a25SHerbert Xu 	    (dev->type != ARPHRD_FDDI) &&
234774235a25SHerbert Xu 	    (dev->type != ARPHRD_IEEE802_TR) &&
234874235a25SHerbert Xu 	    (dev->type != ARPHRD_ARCNET) &&
234974235a25SHerbert Xu 	    (dev->type != ARPHRD_INFINIBAND)) {
235074235a25SHerbert Xu 		/* Alas, we support only Ethernet autoconfiguration. */
235174235a25SHerbert Xu 		return;
235274235a25SHerbert Xu 	}
235374235a25SHerbert Xu 
23541da177e4SLinus Torvalds 	idev = addrconf_add_dev(dev);
23551da177e4SLinus Torvalds 	if (idev == NULL)
23561da177e4SLinus Torvalds 		return;
23571da177e4SLinus Torvalds 
23581da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
23591da177e4SLinus Torvalds 	addr.s6_addr32[0] = htonl(0xFE800000);
23601da177e4SLinus Torvalds 
23611da177e4SLinus Torvalds 	if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
23621da177e4SLinus Torvalds 		addrconf_add_linklocal(idev, &addr);
23631da177e4SLinus Torvalds }
23641da177e4SLinus Torvalds 
23650be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
23661da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev)
23671da177e4SLinus Torvalds {
23681da177e4SLinus Torvalds 	struct inet6_dev *idev;
23691da177e4SLinus Torvalds 
23701da177e4SLinus Torvalds 	ASSERT_RTNL();
23711da177e4SLinus Torvalds 
23721da177e4SLinus Torvalds 	/*
23731da177e4SLinus Torvalds 	 * Configure the tunnel with one of our IPv4
23741da177e4SLinus Torvalds 	 * addresses... we should configure all of
23751da177e4SLinus Torvalds 	 * our v4 addrs in the tunnel
23761da177e4SLinus Torvalds 	 */
23771da177e4SLinus Torvalds 
23781da177e4SLinus Torvalds 	if ((idev = ipv6_find_idev(dev)) == NULL) {
23791da177e4SLinus Torvalds 		printk(KERN_DEBUG "init sit: add_dev failed\n");
23801da177e4SLinus Torvalds 		return;
23811da177e4SLinus Torvalds 	}
23821da177e4SLinus Torvalds 
2383c7dc89c0SFred L. Templin 	if (dev->priv_flags & IFF_ISATAP) {
2384c7dc89c0SFred L. Templin 		struct in6_addr addr;
2385c7dc89c0SFred L. Templin 
2386c7dc89c0SFred L. Templin 		ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
2387c7dc89c0SFred L. Templin 		addrconf_prefix_route(&addr, 64, dev, 0, 0);
2388c7dc89c0SFred L. Templin 		if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2389c7dc89c0SFred L. Templin 			addrconf_add_linklocal(idev, &addr);
2390c7dc89c0SFred L. Templin 		return;
2391c7dc89c0SFred L. Templin 	}
2392c7dc89c0SFred L. Templin 
23931da177e4SLinus Torvalds 	sit_add_v4_addrs(idev);
23941da177e4SLinus Torvalds 
23951da177e4SLinus Torvalds 	if (dev->flags&IFF_POINTOPOINT) {
23961da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
23971da177e4SLinus Torvalds 		addrconf_add_lroute(dev);
23981da177e4SLinus Torvalds 	} else
23991da177e4SLinus Torvalds 		sit_route_add(dev);
24001da177e4SLinus Torvalds }
24010be669bbSJoerg Roedel #endif
24021da177e4SLinus Torvalds 
24031da177e4SLinus Torvalds static inline int
24041da177e4SLinus Torvalds ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
24051da177e4SLinus Torvalds {
24061da177e4SLinus Torvalds 	struct in6_addr lladdr;
24071da177e4SLinus Torvalds 
240895c385b4SNeil Horman 	if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
24091da177e4SLinus Torvalds 		addrconf_add_linklocal(idev, &lladdr);
24101da177e4SLinus Torvalds 		return 0;
24111da177e4SLinus Torvalds 	}
24121da177e4SLinus Torvalds 	return -1;
24131da177e4SLinus Torvalds }
24141da177e4SLinus Torvalds 
24151da177e4SLinus Torvalds static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
24161da177e4SLinus Torvalds {
24171da177e4SLinus Torvalds 	struct net_device *link_dev;
2418c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
24191da177e4SLinus Torvalds 
24201da177e4SLinus Torvalds 	/* first try to inherit the link-local address from the link device */
24211da177e4SLinus Torvalds 	if (idev->dev->iflink &&
24226fda7350SBenjamin Thery 	    (link_dev = __dev_get_by_index(net, idev->dev->iflink))) {
24231da177e4SLinus Torvalds 		if (!ipv6_inherit_linklocal(idev, link_dev))
24241da177e4SLinus Torvalds 			return;
24251da177e4SLinus Torvalds 	}
24261da177e4SLinus Torvalds 	/* then try to inherit it from any device */
24276fda7350SBenjamin Thery 	for_each_netdev(net, link_dev) {
24281da177e4SLinus Torvalds 		if (!ipv6_inherit_linklocal(idev, link_dev))
24291da177e4SLinus Torvalds 			return;
24301da177e4SLinus Torvalds 	}
24311da177e4SLinus Torvalds 	printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
24321da177e4SLinus Torvalds }
24331da177e4SLinus Torvalds 
24341da177e4SLinus Torvalds /*
24351da177e4SLinus Torvalds  * Autoconfigure tunnel with a link-local address so routing protocols,
24361da177e4SLinus Torvalds  * DHCPv6, MLD etc. can be run over the virtual link
24371da177e4SLinus Torvalds  */
24381da177e4SLinus Torvalds 
24391da177e4SLinus Torvalds static void addrconf_ip6_tnl_config(struct net_device *dev)
24401da177e4SLinus Torvalds {
24411da177e4SLinus Torvalds 	struct inet6_dev *idev;
24421da177e4SLinus Torvalds 
24431da177e4SLinus Torvalds 	ASSERT_RTNL();
24441da177e4SLinus Torvalds 
24451da177e4SLinus Torvalds 	if ((idev = addrconf_add_dev(dev)) == NULL) {
24461da177e4SLinus Torvalds 		printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
24471da177e4SLinus Torvalds 		return;
24481da177e4SLinus Torvalds 	}
24491da177e4SLinus Torvalds 	ip6_tnl_add_linklocal(idev);
24501da177e4SLinus Torvalds }
24511da177e4SLinus Torvalds 
24521da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event,
24531da177e4SLinus Torvalds 			   void * data)
24541da177e4SLinus Torvalds {
24551da177e4SLinus Torvalds 	struct net_device *dev = (struct net_device *) data;
245674235a25SHerbert Xu 	struct inet6_dev *idev = __in6_dev_get(dev);
2457c5e33bddSYOSHIFUJI Hideaki 	int run_pending = 0;
2458b217d616SHerbert Xu 	int err;
24591da177e4SLinus Torvalds 
24601da177e4SLinus Torvalds 	switch(event) {
246145ba9dd2SYOSHIFUJI Hideaki 	case NETDEV_REGISTER:
246274235a25SHerbert Xu 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
246345ba9dd2SYOSHIFUJI Hideaki 			idev = ipv6_add_dev(dev);
246445ba9dd2SYOSHIFUJI Hideaki 			if (!idev)
2465b217d616SHerbert Xu 				return notifier_from_errno(-ENOMEM);
246645ba9dd2SYOSHIFUJI Hideaki 		}
246745ba9dd2SYOSHIFUJI Hideaki 		break;
24681da177e4SLinus Torvalds 	case NETDEV_UP:
24693c21edbdSYOSHIFUJI Hideaki 	case NETDEV_CHANGE:
2470c2edacf8SJay Vosburgh 		if (dev->flags & IFF_SLAVE)
2471c2edacf8SJay Vosburgh 			break;
2472c2edacf8SJay Vosburgh 
24733c21edbdSYOSHIFUJI Hideaki 		if (event == NETDEV_UP) {
2474f24e3d65SMitsuru Chinen 			if (!addrconf_qdisc_ok(dev)) {
24753c21edbdSYOSHIFUJI Hideaki 				/* device is not ready yet. */
24763c21edbdSYOSHIFUJI Hideaki 				printk(KERN_INFO
24773c21edbdSYOSHIFUJI Hideaki 					"ADDRCONF(NETDEV_UP): %s: "
24783c21edbdSYOSHIFUJI Hideaki 					"link is not ready\n",
24793c21edbdSYOSHIFUJI Hideaki 					dev->name);
24803c21edbdSYOSHIFUJI Hideaki 				break;
24813c21edbdSYOSHIFUJI Hideaki 			}
248299081049SKristian Slavov 
2483d31c7b8fSEvgeniy Polyakov 			if (!idev && dev->mtu >= IPV6_MIN_MTU)
2484d31c7b8fSEvgeniy Polyakov 				idev = ipv6_add_dev(dev);
2485d31c7b8fSEvgeniy Polyakov 
248699081049SKristian Slavov 			if (idev)
248799081049SKristian Slavov 				idev->if_flags |= IF_READY;
24883c21edbdSYOSHIFUJI Hideaki 		} else {
2489f24e3d65SMitsuru Chinen 			if (!addrconf_qdisc_ok(dev)) {
24903c21edbdSYOSHIFUJI Hideaki 				/* device is still not ready. */
24913c21edbdSYOSHIFUJI Hideaki 				break;
24923c21edbdSYOSHIFUJI Hideaki 			}
24933c21edbdSYOSHIFUJI Hideaki 
24943c21edbdSYOSHIFUJI Hideaki 			if (idev) {
24953c21edbdSYOSHIFUJI Hideaki 				if (idev->if_flags & IF_READY) {
24963c21edbdSYOSHIFUJI Hideaki 					/* device is already configured. */
24973c21edbdSYOSHIFUJI Hideaki 					break;
24983c21edbdSYOSHIFUJI Hideaki 				}
24993c21edbdSYOSHIFUJI Hideaki 				idev->if_flags |= IF_READY;
25003c21edbdSYOSHIFUJI Hideaki 			}
25013c21edbdSYOSHIFUJI Hideaki 
25023c21edbdSYOSHIFUJI Hideaki 			printk(KERN_INFO
25033c21edbdSYOSHIFUJI Hideaki 					"ADDRCONF(NETDEV_CHANGE): %s: "
25043c21edbdSYOSHIFUJI Hideaki 					"link becomes ready\n",
25053c21edbdSYOSHIFUJI Hideaki 					dev->name);
25063c21edbdSYOSHIFUJI Hideaki 
2507c5e33bddSYOSHIFUJI Hideaki 			run_pending = 1;
25083c21edbdSYOSHIFUJI Hideaki 		}
25093c21edbdSYOSHIFUJI Hideaki 
25101da177e4SLinus Torvalds 		switch(dev->type) {
25110be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
25121da177e4SLinus Torvalds 		case ARPHRD_SIT:
25131da177e4SLinus Torvalds 			addrconf_sit_config(dev);
25141da177e4SLinus Torvalds 			break;
25150be669bbSJoerg Roedel #endif
25161da177e4SLinus Torvalds 		case ARPHRD_TUNNEL6:
25171da177e4SLinus Torvalds 			addrconf_ip6_tnl_config(dev);
25181da177e4SLinus Torvalds 			break;
25191da177e4SLinus Torvalds 		case ARPHRD_LOOPBACK:
25201da177e4SLinus Torvalds 			init_loopback(dev);
25211da177e4SLinus Torvalds 			break;
25221da177e4SLinus Torvalds 
25231da177e4SLinus Torvalds 		default:
25241da177e4SLinus Torvalds 			addrconf_dev_config(dev);
25251da177e4SLinus Torvalds 			break;
25263ff50b79SStephen Hemminger 		}
25271da177e4SLinus Torvalds 		if (idev) {
2528c5e33bddSYOSHIFUJI Hideaki 			if (run_pending)
2529c5e33bddSYOSHIFUJI Hideaki 				addrconf_dad_run(idev);
2530c5e33bddSYOSHIFUJI Hideaki 
25311da177e4SLinus Torvalds 			/* If the MTU changed during the interface down, when the
25321da177e4SLinus Torvalds 			   interface up, the changed MTU must be reflected in the
25331da177e4SLinus Torvalds 			   idev as well as routers.
25341da177e4SLinus Torvalds 			 */
25351da177e4SLinus Torvalds 			if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
25361da177e4SLinus Torvalds 				rt6_mtu_change(dev, dev->mtu);
25371da177e4SLinus Torvalds 				idev->cnf.mtu6 = dev->mtu;
25381da177e4SLinus Torvalds 			}
25391da177e4SLinus Torvalds 			idev->tstamp = jiffies;
25401da177e4SLinus Torvalds 			inet6_ifinfo_notify(RTM_NEWLINK, idev);
25411da177e4SLinus Torvalds 			/* If the changed mtu during down is lower than IPV6_MIN_MTU
25421da177e4SLinus Torvalds 			   stop IPv6 on this interface.
25431da177e4SLinus Torvalds 			 */
25441da177e4SLinus Torvalds 			if (dev->mtu < IPV6_MIN_MTU)
25451da177e4SLinus Torvalds 				addrconf_ifdown(dev, event != NETDEV_DOWN);
25461da177e4SLinus Torvalds 		}
25471da177e4SLinus Torvalds 		break;
25481da177e4SLinus Torvalds 
25491da177e4SLinus Torvalds 	case NETDEV_CHANGEMTU:
25501da177e4SLinus Torvalds 		if (idev && dev->mtu >= IPV6_MIN_MTU) {
25511da177e4SLinus Torvalds 			rt6_mtu_change(dev, dev->mtu);
25521da177e4SLinus Torvalds 			idev->cnf.mtu6 = dev->mtu;
25531da177e4SLinus Torvalds 			break;
25541da177e4SLinus Torvalds 		}
25551da177e4SLinus Torvalds 
2556d31c7b8fSEvgeniy Polyakov 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2557d31c7b8fSEvgeniy Polyakov 			idev = ipv6_add_dev(dev);
2558d31c7b8fSEvgeniy Polyakov 			if (idev)
2559d31c7b8fSEvgeniy Polyakov 				break;
2560d31c7b8fSEvgeniy Polyakov 		}
2561d31c7b8fSEvgeniy Polyakov 
25621da177e4SLinus Torvalds 		/* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
25631da177e4SLinus Torvalds 
25641da177e4SLinus Torvalds 	case NETDEV_DOWN:
25651da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
25661da177e4SLinus Torvalds 		/*
25671da177e4SLinus Torvalds 		 *	Remove all addresses from this interface.
25681da177e4SLinus Torvalds 		 */
25691da177e4SLinus Torvalds 		addrconf_ifdown(dev, event != NETDEV_DOWN);
25701da177e4SLinus Torvalds 		break;
25713c21edbdSYOSHIFUJI Hideaki 
25721da177e4SLinus Torvalds 	case NETDEV_CHANGENAME:
25731da177e4SLinus Torvalds 		if (idev) {
25745632c515SStephen Hemminger 			snmp6_unregister_dev(idev);
2575408c4768SPavel Emelyanov 			addrconf_sysctl_unregister(idev);
2576f52295a9SPavel Emelyanov 			addrconf_sysctl_register(idev);
2577b217d616SHerbert Xu 			err = snmp6_register_dev(idev);
2578b217d616SHerbert Xu 			if (err)
2579b217d616SHerbert Xu 				return notifier_from_errno(err);
25805632c515SStephen Hemminger 		}
25811da177e4SLinus Torvalds 		break;
25823ff50b79SStephen Hemminger 	}
25831da177e4SLinus Torvalds 
25841da177e4SLinus Torvalds 	return NOTIFY_OK;
25851da177e4SLinus Torvalds }
25861da177e4SLinus Torvalds 
25871da177e4SLinus Torvalds /*
25881da177e4SLinus Torvalds  *	addrconf module should be notified of a device going up
25891da177e4SLinus Torvalds  */
25901da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = {
25911da177e4SLinus Torvalds 	.notifier_call = addrconf_notify,
25921da177e4SLinus Torvalds 	.priority = 0
25931da177e4SLinus Torvalds };
25941da177e4SLinus Torvalds 
25951da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how)
25961da177e4SLinus Torvalds {
25971da177e4SLinus Torvalds 	struct inet6_dev *idev;
25981da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa, **bifa;
2599c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
26001da177e4SLinus Torvalds 	int i;
26011da177e4SLinus Torvalds 
26021da177e4SLinus Torvalds 	ASSERT_RTNL();
26031da177e4SLinus Torvalds 
2604eb867579SDenis V. Lunev 	if ((dev->flags & IFF_LOOPBACK) && how == 1)
26051da177e4SLinus Torvalds 		how = 0;
26061da177e4SLinus Torvalds 
2607f3db4851SDaniel Lezcano 	rt6_ifdown(net, dev);
26081da177e4SLinus Torvalds 	neigh_ifdown(&nd_tbl, dev);
26091da177e4SLinus Torvalds 
26101da177e4SLinus Torvalds 	idev = __in6_dev_get(dev);
26111da177e4SLinus Torvalds 	if (idev == NULL)
26121da177e4SLinus Torvalds 		return -ENODEV;
26131da177e4SLinus Torvalds 
26141da177e4SLinus Torvalds 	/* Step 1: remove reference to ipv6 device from parent device.
26151da177e4SLinus Torvalds 		   Do not dev_put!
26161da177e4SLinus Torvalds 	 */
2617439e2385SDenis V. Lunev 	if (how) {
26181da177e4SLinus Torvalds 		idev->dead = 1;
26198814c4b5SYOSHIFUJI Hideaki 
26208814c4b5SYOSHIFUJI Hideaki 		/* protected by rtnl_lock */
26218814c4b5SYOSHIFUJI Hideaki 		rcu_assign_pointer(dev->ip6_ptr, NULL);
26221da177e4SLinus Torvalds 
26231da177e4SLinus Torvalds 		/* Step 1.5: remove snmp6 entry */
26241da177e4SLinus Torvalds 		snmp6_unregister_dev(idev);
26251da177e4SLinus Torvalds 
26261da177e4SLinus Torvalds 	}
26271da177e4SLinus Torvalds 
26281da177e4SLinus Torvalds 	/* Step 2: clear hash table */
26291da177e4SLinus Torvalds 	for (i=0; i<IN6_ADDR_HSIZE; i++) {
26301da177e4SLinus Torvalds 		bifa = &inet6_addr_lst[i];
26311da177e4SLinus Torvalds 
26321da177e4SLinus Torvalds 		write_lock_bh(&addrconf_hash_lock);
26331da177e4SLinus Torvalds 		while ((ifa = *bifa) != NULL) {
26341da177e4SLinus Torvalds 			if (ifa->idev == idev) {
26351da177e4SLinus Torvalds 				*bifa = ifa->lst_next;
26361da177e4SLinus Torvalds 				ifa->lst_next = NULL;
26371da177e4SLinus Torvalds 				addrconf_del_timer(ifa);
26381da177e4SLinus Torvalds 				in6_ifa_put(ifa);
26391da177e4SLinus Torvalds 				continue;
26401da177e4SLinus Torvalds 			}
26411da177e4SLinus Torvalds 			bifa = &ifa->lst_next;
26421da177e4SLinus Torvalds 		}
26431da177e4SLinus Torvalds 		write_unlock_bh(&addrconf_hash_lock);
26441da177e4SLinus Torvalds 	}
26451da177e4SLinus Torvalds 
26461da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
26471da177e4SLinus Torvalds 
26481da177e4SLinus Torvalds 	/* Step 3: clear flags for stateless addrconf */
2649439e2385SDenis V. Lunev 	if (!how)
26503c21edbdSYOSHIFUJI Hideaki 		idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
26511da177e4SLinus Torvalds 
26521da177e4SLinus Torvalds 	/* Step 4: clear address list */
26531da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
2654439e2385SDenis V. Lunev 	if (how && del_timer(&idev->regen_timer))
26551da177e4SLinus Torvalds 		in6_dev_put(idev);
26561da177e4SLinus Torvalds 
26571da177e4SLinus Torvalds 	/* clear tempaddr list */
26581da177e4SLinus Torvalds 	while ((ifa = idev->tempaddr_list) != NULL) {
26591da177e4SLinus Torvalds 		idev->tempaddr_list = ifa->tmp_next;
26601da177e4SLinus Torvalds 		ifa->tmp_next = NULL;
26611da177e4SLinus Torvalds 		ifa->dead = 1;
26621da177e4SLinus Torvalds 		write_unlock_bh(&idev->lock);
26631da177e4SLinus Torvalds 		spin_lock_bh(&ifa->lock);
26641da177e4SLinus Torvalds 
26651da177e4SLinus Torvalds 		if (ifa->ifpub) {
26661da177e4SLinus Torvalds 			in6_ifa_put(ifa->ifpub);
26671da177e4SLinus Torvalds 			ifa->ifpub = NULL;
26681da177e4SLinus Torvalds 		}
26691da177e4SLinus Torvalds 		spin_unlock_bh(&ifa->lock);
26701da177e4SLinus Torvalds 		in6_ifa_put(ifa);
26711da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
26721da177e4SLinus Torvalds 	}
26731da177e4SLinus Torvalds #endif
26741da177e4SLinus Torvalds 	while ((ifa = idev->addr_list) != NULL) {
26751da177e4SLinus Torvalds 		idev->addr_list = ifa->if_next;
26761da177e4SLinus Torvalds 		ifa->if_next = NULL;
26771da177e4SLinus Torvalds 		ifa->dead = 1;
26781da177e4SLinus Torvalds 		addrconf_del_timer(ifa);
26791da177e4SLinus Torvalds 		write_unlock_bh(&idev->lock);
26801da177e4SLinus Torvalds 
26811da177e4SLinus Torvalds 		__ipv6_ifa_notify(RTM_DELADDR, ifa);
2682063ed369SVlad Yasevich 		atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa);
26831da177e4SLinus Torvalds 		in6_ifa_put(ifa);
26841da177e4SLinus Torvalds 
26851da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
26861da177e4SLinus Torvalds 	}
26871da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
26881da177e4SLinus Torvalds 
26891da177e4SLinus Torvalds 	/* Step 5: Discard multicast list */
26901da177e4SLinus Torvalds 
2691439e2385SDenis V. Lunev 	if (how)
26921da177e4SLinus Torvalds 		ipv6_mc_destroy_dev(idev);
26931da177e4SLinus Torvalds 	else
26941da177e4SLinus Torvalds 		ipv6_mc_down(idev);
26951da177e4SLinus Torvalds 
26961da177e4SLinus Torvalds 	idev->tstamp = jiffies;
26971da177e4SLinus Torvalds 
26981da177e4SLinus Torvalds 	/* Shot the device (if unregistered) */
26991da177e4SLinus Torvalds 
2700439e2385SDenis V. Lunev 	if (how) {
2701408c4768SPavel Emelyanov 		addrconf_sysctl_unregister(idev);
27021da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, idev->nd_parms);
27031da177e4SLinus Torvalds 		neigh_ifdown(&nd_tbl, dev);
27041da177e4SLinus Torvalds 		in6_dev_put(idev);
27051da177e4SLinus Torvalds 	}
27061da177e4SLinus Torvalds 	return 0;
27071da177e4SLinus Torvalds }
27081da177e4SLinus Torvalds 
27091da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data)
27101da177e4SLinus Torvalds {
27111da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
27121da177e4SLinus Torvalds 
27131da177e4SLinus Torvalds 	if (ifp->idev->cnf.forwarding)
27141da177e4SLinus Torvalds 		goto out;
27151da177e4SLinus Torvalds 
27161da177e4SLinus Torvalds 	if (ifp->idev->if_flags & IF_RA_RCVD) {
27171da177e4SLinus Torvalds 		/*
27181da177e4SLinus Torvalds 		 *	Announcement received after solicitation
27191da177e4SLinus Torvalds 		 *	was sent
27201da177e4SLinus Torvalds 		 */
27211da177e4SLinus Torvalds 		goto out;
27221da177e4SLinus Torvalds 	}
27231da177e4SLinus Torvalds 
27241da177e4SLinus Torvalds 	spin_lock(&ifp->lock);
27251da177e4SLinus Torvalds 	if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
27261da177e4SLinus Torvalds 		/* The wait after the last probe can be shorter */
27271da177e4SLinus Torvalds 		addrconf_mod_timer(ifp, AC_RS,
27281da177e4SLinus Torvalds 				   (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
27291da177e4SLinus Torvalds 				   ifp->idev->cnf.rtr_solicit_delay :
27301da177e4SLinus Torvalds 				   ifp->idev->cnf.rtr_solicit_interval);
27311da177e4SLinus Torvalds 		spin_unlock(&ifp->lock);
27321da177e4SLinus Torvalds 
2733f3ee4010SYOSHIFUJI Hideaki 		ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
27341da177e4SLinus Torvalds 	} else {
27351da177e4SLinus Torvalds 		spin_unlock(&ifp->lock);
27361da177e4SLinus Torvalds 		/*
27371da177e4SLinus Torvalds 		 * Note: we do not support deprecated "all on-link"
27381da177e4SLinus Torvalds 		 * assumption any longer.
27391da177e4SLinus Torvalds 		 */
27401da177e4SLinus Torvalds 		printk(KERN_DEBUG "%s: no IPv6 routers present\n",
27411da177e4SLinus Torvalds 		       ifp->idev->dev->name);
27421da177e4SLinus Torvalds 	}
27431da177e4SLinus Torvalds 
27441da177e4SLinus Torvalds out:
27451da177e4SLinus Torvalds 	in6_ifa_put(ifp);
27461da177e4SLinus Torvalds }
27471da177e4SLinus Torvalds 
27481da177e4SLinus Torvalds /*
27491da177e4SLinus Torvalds  *	Duplicate Address Detection
27501da177e4SLinus Torvalds  */
27513c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
27523c21edbdSYOSHIFUJI Hideaki {
27533c21edbdSYOSHIFUJI Hideaki 	unsigned long rand_num;
27543c21edbdSYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
27553c21edbdSYOSHIFUJI Hideaki 
275695c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
275795c385b4SNeil Horman 		rand_num = 0;
275895c385b4SNeil Horman 	else
27593c21edbdSYOSHIFUJI Hideaki 		rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
276095c385b4SNeil Horman 
27613c21edbdSYOSHIFUJI Hideaki 	ifp->probes = idev->cnf.dad_transmits;
27623c21edbdSYOSHIFUJI Hideaki 	addrconf_mod_timer(ifp, AC_DAD, rand_num);
27633c21edbdSYOSHIFUJI Hideaki }
27643c21edbdSYOSHIFUJI Hideaki 
2765e431b8c0SJamal Hadi Salim static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
27661da177e4SLinus Torvalds {
27671da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
27681da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
27691da177e4SLinus Torvalds 
27701da177e4SLinus Torvalds 	addrconf_join_solict(dev, &ifp->addr);
27711da177e4SLinus Torvalds 
27721da177e4SLinus Torvalds 	net_srandom(ifp->addr.s6_addr32[3]);
27731da177e4SLinus Torvalds 
27741da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
27751da177e4SLinus Torvalds 	if (ifp->dead)
27761da177e4SLinus Torvalds 		goto out;
27771da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
27781da177e4SLinus Torvalds 
27791da177e4SLinus Torvalds 	if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
27801b34be74SYOSHIFUJI Hideaki 	    idev->cnf.accept_dad < 1 ||
278155ebaef1SNoriaki TAKAMIYA 	    !(ifp->flags&IFA_F_TENTATIVE) ||
278255ebaef1SNoriaki TAKAMIYA 	    ifp->flags & IFA_F_NODAD) {
278395c385b4SNeil Horman 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC);
27841da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
27851da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
27861da177e4SLinus Torvalds 
27871da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
27881da177e4SLinus Torvalds 		return;
27891da177e4SLinus Torvalds 	}
27901da177e4SLinus Torvalds 
27916732badeSYOSHIFUJI Hideaki 	if (!(idev->if_flags & IF_READY)) {
27923dd3bf83SYOSHIFUJI Hideaki 		spin_unlock_bh(&ifp->lock);
27936732badeSYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
27943c21edbdSYOSHIFUJI Hideaki 		/*
27953c21edbdSYOSHIFUJI Hideaki 		 * If the defice is not ready:
27963c21edbdSYOSHIFUJI Hideaki 		 * - keep it tentative if it is a permanent address.
27973c21edbdSYOSHIFUJI Hideaki 		 * - otherwise, kill it.
27983c21edbdSYOSHIFUJI Hideaki 		 */
27993c21edbdSYOSHIFUJI Hideaki 		in6_ifa_hold(ifp);
28003c21edbdSYOSHIFUJI Hideaki 		addrconf_dad_stop(ifp);
28016732badeSYOSHIFUJI Hideaki 		return;
28023c21edbdSYOSHIFUJI Hideaki 	}
280395c385b4SNeil Horman 
280495c385b4SNeil Horman 	/*
280595c385b4SNeil Horman 	 * Optimistic nodes can start receiving
280695c385b4SNeil Horman 	 * Frames right away
280795c385b4SNeil Horman 	 */
280895c385b4SNeil Horman 	if(ifp->flags & IFA_F_OPTIMISTIC)
280995c385b4SNeil Horman 		ip6_ins_rt(ifp->rt);
281095c385b4SNeil Horman 
28116732badeSYOSHIFUJI Hideaki 	addrconf_dad_kick(ifp);
28126732badeSYOSHIFUJI Hideaki 	spin_unlock_bh(&ifp->lock);
28131da177e4SLinus Torvalds out:
28141da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
28151da177e4SLinus Torvalds }
28161da177e4SLinus Torvalds 
28171da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data)
28181da177e4SLinus Torvalds {
28191da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
28201da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
28211da177e4SLinus Torvalds 	struct in6_addr mcaddr;
28221da177e4SLinus Torvalds 
28231da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
28241da177e4SLinus Torvalds 	if (idev->dead) {
28251da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
28261da177e4SLinus Torvalds 		goto out;
28271da177e4SLinus Torvalds 	}
28281b34be74SYOSHIFUJI Hideaki 	if (idev->cnf.accept_dad > 1 && idev->cnf.disable_ipv6) {
28291b34be74SYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
28301b34be74SYOSHIFUJI Hideaki 		addrconf_dad_failure(ifp);
28311b34be74SYOSHIFUJI Hideaki 		return;
28321b34be74SYOSHIFUJI Hideaki 	}
28331da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
28341da177e4SLinus Torvalds 	if (ifp->probes == 0) {
28351da177e4SLinus Torvalds 		/*
28361da177e4SLinus Torvalds 		 * DAD was successful
28371da177e4SLinus Torvalds 		 */
28381da177e4SLinus Torvalds 
283995c385b4SNeil Horman 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC);
28401da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
28411da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
28421da177e4SLinus Torvalds 
28431da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
28441da177e4SLinus Torvalds 
28451da177e4SLinus Torvalds 		goto out;
28461da177e4SLinus Torvalds 	}
28471da177e4SLinus Torvalds 
28481da177e4SLinus Torvalds 	ifp->probes--;
28491da177e4SLinus Torvalds 	addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
28501da177e4SLinus Torvalds 	spin_unlock_bh(&ifp->lock);
28511da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
28521da177e4SLinus Torvalds 
28531da177e4SLinus Torvalds 	/* send a neighbour solicitation for our addr */
28541da177e4SLinus Torvalds 	addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2855d7aabf22SYOSHIFUJI Hideaki 	ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
28561da177e4SLinus Torvalds out:
28571da177e4SLinus Torvalds 	in6_ifa_put(ifp);
28581da177e4SLinus Torvalds }
28591da177e4SLinus Torvalds 
28601da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
28611da177e4SLinus Torvalds {
28621da177e4SLinus Torvalds 	struct net_device *	dev = ifp->idev->dev;
28631da177e4SLinus Torvalds 
28641da177e4SLinus Torvalds 	/*
28651da177e4SLinus Torvalds 	 *	Configure the address for reception. Now it is valid.
28661da177e4SLinus Torvalds 	 */
28671da177e4SLinus Torvalds 
28681da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_NEWADDR, ifp);
28691da177e4SLinus Torvalds 
28701da177e4SLinus Torvalds 	/* If added prefix is link local and forwarding is off,
28711da177e4SLinus Torvalds 	   start sending router solicitations.
28721da177e4SLinus Torvalds 	 */
28731da177e4SLinus Torvalds 
28741da177e4SLinus Torvalds 	if (ifp->idev->cnf.forwarding == 0 &&
28751da177e4SLinus Torvalds 	    ifp->idev->cnf.rtr_solicits > 0 &&
28761da177e4SLinus Torvalds 	    (dev->flags&IFF_LOOPBACK) == 0 &&
28771da177e4SLinus Torvalds 	    (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
28781da177e4SLinus Torvalds 		/*
28791da177e4SLinus Torvalds 		 *	If a host as already performed a random delay
28801da177e4SLinus Torvalds 		 *	[...] as part of DAD [...] there is no need
28811da177e4SLinus Torvalds 		 *	to delay again before sending the first RS
28821da177e4SLinus Torvalds 		 */
2883f3ee4010SYOSHIFUJI Hideaki 		ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
28841da177e4SLinus Torvalds 
28851da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
28861da177e4SLinus Torvalds 		ifp->probes = 1;
28871da177e4SLinus Torvalds 		ifp->idev->if_flags |= IF_RS_SENT;
28881da177e4SLinus Torvalds 		addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
28891da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
28901da177e4SLinus Torvalds 	}
28911da177e4SLinus Torvalds }
28921da177e4SLinus Torvalds 
2893c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev) {
2894c5e33bddSYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifp;
2895c5e33bddSYOSHIFUJI Hideaki 
2896c5e33bddSYOSHIFUJI Hideaki 	read_lock_bh(&idev->lock);
2897c5e33bddSYOSHIFUJI Hideaki 	for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2898c5e33bddSYOSHIFUJI Hideaki 		spin_lock_bh(&ifp->lock);
2899c5e33bddSYOSHIFUJI Hideaki 		if (!(ifp->flags & IFA_F_TENTATIVE)) {
2900c5e33bddSYOSHIFUJI Hideaki 			spin_unlock_bh(&ifp->lock);
2901c5e33bddSYOSHIFUJI Hideaki 			continue;
2902c5e33bddSYOSHIFUJI Hideaki 		}
2903c5e33bddSYOSHIFUJI Hideaki 		spin_unlock_bh(&ifp->lock);
2904c5e33bddSYOSHIFUJI Hideaki 		addrconf_dad_kick(ifp);
2905c5e33bddSYOSHIFUJI Hideaki 	}
2906c5e33bddSYOSHIFUJI Hideaki 	read_unlock_bh(&idev->lock);
2907c5e33bddSYOSHIFUJI Hideaki }
2908c5e33bddSYOSHIFUJI Hideaki 
29091da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
29101da177e4SLinus Torvalds struct if6_iter_state {
29113c40090aSDaniel Lezcano 	struct seq_net_private p;
29121da177e4SLinus Torvalds 	int bucket;
29131da177e4SLinus Torvalds };
29141da177e4SLinus Torvalds 
29151da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
29161da177e4SLinus Torvalds {
29171da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
29181da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
29191218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
29201da177e4SLinus Torvalds 
29211da177e4SLinus Torvalds 	for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
29221da177e4SLinus Torvalds 		ifa = inet6_addr_lst[state->bucket];
29233c40090aSDaniel Lezcano 
2924878628fbSYOSHIFUJI Hideaki 		while (ifa && !net_eq(dev_net(ifa->idev->dev), net))
29253c40090aSDaniel Lezcano 			ifa = ifa->lst_next;
29261da177e4SLinus Torvalds 		if (ifa)
29271da177e4SLinus Torvalds 			break;
29281da177e4SLinus Torvalds 	}
29291da177e4SLinus Torvalds 	return ifa;
29301da177e4SLinus Torvalds }
29311da177e4SLinus Torvalds 
29321da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
29331da177e4SLinus Torvalds {
29341da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
29351218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
29361da177e4SLinus Torvalds 
29371da177e4SLinus Torvalds 	ifa = ifa->lst_next;
29381da177e4SLinus Torvalds try_again:
29393c40090aSDaniel Lezcano 	if (ifa) {
2940878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifa->idev->dev), net)) {
29413c40090aSDaniel Lezcano 			ifa = ifa->lst_next;
29423c40090aSDaniel Lezcano 			goto try_again;
29433c40090aSDaniel Lezcano 		}
29443c40090aSDaniel Lezcano 	}
29453c40090aSDaniel Lezcano 
29461da177e4SLinus Torvalds 	if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
29471da177e4SLinus Torvalds 		ifa = inet6_addr_lst[state->bucket];
29481da177e4SLinus Torvalds 		goto try_again;
29491da177e4SLinus Torvalds 	}
29503c40090aSDaniel Lezcano 
29511da177e4SLinus Torvalds 	return ifa;
29521da177e4SLinus Torvalds }
29531da177e4SLinus Torvalds 
29541da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
29551da177e4SLinus Torvalds {
29561da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = if6_get_first(seq);
29571da177e4SLinus Torvalds 
29581da177e4SLinus Torvalds 	if (ifa)
29591da177e4SLinus Torvalds 		while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
29601da177e4SLinus Torvalds 			--pos;
29611da177e4SLinus Torvalds 	return pos ? NULL : ifa;
29621da177e4SLinus Torvalds }
29631da177e4SLinus Torvalds 
29641da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2965d20b3109SStephen Hemminger 	__acquires(addrconf_hash_lock)
29661da177e4SLinus Torvalds {
29671da177e4SLinus Torvalds 	read_lock_bh(&addrconf_hash_lock);
29681da177e4SLinus Torvalds 	return if6_get_idx(seq, *pos);
29691da177e4SLinus Torvalds }
29701da177e4SLinus Torvalds 
29711da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
29721da177e4SLinus Torvalds {
29731da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
29741da177e4SLinus Torvalds 
29751da177e4SLinus Torvalds 	ifa = if6_get_next(seq, v);
29761da177e4SLinus Torvalds 	++*pos;
29771da177e4SLinus Torvalds 	return ifa;
29781da177e4SLinus Torvalds }
29791da177e4SLinus Torvalds 
29801da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v)
2981d20b3109SStephen Hemminger 	__releases(addrconf_hash_lock)
29821da177e4SLinus Torvalds {
29831da177e4SLinus Torvalds 	read_unlock_bh(&addrconf_hash_lock);
29841da177e4SLinus Torvalds }
29851da177e4SLinus Torvalds 
29861da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v)
29871da177e4SLinus Torvalds {
29881da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
2989*4b7a4274SHarvey Harrison 	seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
2990b071195dSHarvey Harrison 		   &ifp->addr,
29911da177e4SLinus Torvalds 		   ifp->idev->dev->ifindex,
29921da177e4SLinus Torvalds 		   ifp->prefix_len,
29931da177e4SLinus Torvalds 		   ifp->scope,
29941da177e4SLinus Torvalds 		   ifp->flags,
29951da177e4SLinus Torvalds 		   ifp->idev->dev->name);
29961da177e4SLinus Torvalds 	return 0;
29971da177e4SLinus Torvalds }
29981da177e4SLinus Torvalds 
299956b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = {
30001da177e4SLinus Torvalds 	.start	= if6_seq_start,
30011da177e4SLinus Torvalds 	.next	= if6_seq_next,
30021da177e4SLinus Torvalds 	.show	= if6_seq_show,
30031da177e4SLinus Torvalds 	.stop	= if6_seq_stop,
30041da177e4SLinus Torvalds };
30051da177e4SLinus Torvalds 
30061da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file)
30071da177e4SLinus Torvalds {
30083c40090aSDaniel Lezcano 	return seq_open_net(inode, file, &if6_seq_ops,
3009cf7732e4SPavel Emelyanov 			    sizeof(struct if6_iter_state));
30101da177e4SLinus Torvalds }
30111da177e4SLinus Torvalds 
30129a32144eSArjan van de Ven static const struct file_operations if6_fops = {
30131da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
30141da177e4SLinus Torvalds 	.open		= if6_seq_open,
30151da177e4SLinus Torvalds 	.read		= seq_read,
30161da177e4SLinus Torvalds 	.llseek		= seq_lseek,
30173c40090aSDaniel Lezcano 	.release	= seq_release_net,
30181da177e4SLinus Torvalds };
30191da177e4SLinus Torvalds 
30203c40090aSDaniel Lezcano static int if6_proc_net_init(struct net *net)
30211da177e4SLinus Torvalds {
30223c40090aSDaniel Lezcano 	if (!proc_net_fops_create(net, "if_inet6", S_IRUGO, &if6_fops))
30231da177e4SLinus Torvalds 		return -ENOMEM;
30241da177e4SLinus Torvalds 	return 0;
30251da177e4SLinus Torvalds }
30261da177e4SLinus Torvalds 
30273c40090aSDaniel Lezcano static void if6_proc_net_exit(struct net *net)
30283c40090aSDaniel Lezcano {
30293c40090aSDaniel Lezcano        proc_net_remove(net, "if_inet6");
30303c40090aSDaniel Lezcano }
30313c40090aSDaniel Lezcano 
30323c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = {
30333c40090aSDaniel Lezcano        .init = if6_proc_net_init,
30343c40090aSDaniel Lezcano        .exit = if6_proc_net_exit,
30353c40090aSDaniel Lezcano };
30363c40090aSDaniel Lezcano 
30373c40090aSDaniel Lezcano int __init if6_proc_init(void)
30383c40090aSDaniel Lezcano {
30393c40090aSDaniel Lezcano 	return register_pernet_subsys(&if6_proc_net_ops);
30403c40090aSDaniel Lezcano }
30413c40090aSDaniel Lezcano 
30421da177e4SLinus Torvalds void if6_proc_exit(void)
30431da177e4SLinus Torvalds {
30443c40090aSDaniel Lezcano 	unregister_pernet_subsys(&if6_proc_net_ops);
30451da177e4SLinus Torvalds }
30461da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
30471da177e4SLinus Torvalds 
304859fbb3a6SMasahide NAKAMURA #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
30493b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */
3050389f6612SDaniel Lezcano int ipv6_chk_home_addr(struct net *net, struct in6_addr *addr)
30513b9f9a1cSNoriaki TAKAMIYA {
30523b9f9a1cSNoriaki TAKAMIYA 	int ret = 0;
30533b9f9a1cSNoriaki TAKAMIYA 	struct inet6_ifaddr * ifp;
30543b9f9a1cSNoriaki TAKAMIYA 	u8 hash = ipv6_addr_hash(addr);
30553b9f9a1cSNoriaki TAKAMIYA 	read_lock_bh(&addrconf_hash_lock);
30563b9f9a1cSNoriaki TAKAMIYA 	for (ifp = inet6_addr_lst[hash]; ifp; ifp = ifp->lst_next) {
3057878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
3058389f6612SDaniel Lezcano 			continue;
3059caad295fSYOSHIFUJI Hideaki 		if (ipv6_addr_equal(&ifp->addr, addr) &&
30603b9f9a1cSNoriaki TAKAMIYA 		    (ifp->flags & IFA_F_HOMEADDRESS)) {
30613b9f9a1cSNoriaki TAKAMIYA 			ret = 1;
30623b9f9a1cSNoriaki TAKAMIYA 			break;
30633b9f9a1cSNoriaki TAKAMIYA 		}
30643b9f9a1cSNoriaki TAKAMIYA 	}
30653b9f9a1cSNoriaki TAKAMIYA 	read_unlock_bh(&addrconf_hash_lock);
30663b9f9a1cSNoriaki TAKAMIYA 	return ret;
30673b9f9a1cSNoriaki TAKAMIYA }
30683b9f9a1cSNoriaki TAKAMIYA #endif
30693b9f9a1cSNoriaki TAKAMIYA 
30701da177e4SLinus Torvalds /*
30711da177e4SLinus Torvalds  *	Periodic address status verification
30721da177e4SLinus Torvalds  */
30731da177e4SLinus Torvalds 
30741da177e4SLinus Torvalds static void addrconf_verify(unsigned long foo)
30751da177e4SLinus Torvalds {
30761da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
30771da177e4SLinus Torvalds 	unsigned long now, next;
30781da177e4SLinus Torvalds 	int i;
30791da177e4SLinus Torvalds 
30801da177e4SLinus Torvalds 	spin_lock_bh(&addrconf_verify_lock);
30811da177e4SLinus Torvalds 	now = jiffies;
30821da177e4SLinus Torvalds 	next = now + ADDR_CHECK_FREQUENCY;
30831da177e4SLinus Torvalds 
30841da177e4SLinus Torvalds 	del_timer(&addr_chk_timer);
30851da177e4SLinus Torvalds 
30861da177e4SLinus Torvalds 	for (i=0; i < IN6_ADDR_HSIZE; i++) {
30871da177e4SLinus Torvalds 
30881da177e4SLinus Torvalds restart:
30895d5780dfSYan Zheng 		read_lock(&addrconf_hash_lock);
30901da177e4SLinus Torvalds 		for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
30911da177e4SLinus Torvalds 			unsigned long age;
30921da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
30931da177e4SLinus Torvalds 			unsigned long regen_advance;
30941da177e4SLinus Torvalds #endif
30951da177e4SLinus Torvalds 
30961da177e4SLinus Torvalds 			if (ifp->flags & IFA_F_PERMANENT)
30971da177e4SLinus Torvalds 				continue;
30981da177e4SLinus Torvalds 
30991da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
31001da177e4SLinus Torvalds 			age = (now - ifp->tstamp) / HZ;
31011da177e4SLinus Torvalds 
31021da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
31031da177e4SLinus Torvalds 			regen_advance = ifp->idev->cnf.regen_max_retry *
31041da177e4SLinus Torvalds 					ifp->idev->cnf.dad_transmits *
31051da177e4SLinus Torvalds 					ifp->idev->nd_parms->retrans_time / HZ;
31061da177e4SLinus Torvalds #endif
31071da177e4SLinus Torvalds 
31088f27ebb9SYOSHIFUJI Hideaki 			if (ifp->valid_lft != INFINITY_LIFE_TIME &&
31098f27ebb9SYOSHIFUJI Hideaki 			    age >= ifp->valid_lft) {
31101da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
31111da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
31125d5780dfSYan Zheng 				read_unlock(&addrconf_hash_lock);
31131da177e4SLinus Torvalds 				ipv6_del_addr(ifp);
31141da177e4SLinus Torvalds 				goto restart;
31158f27ebb9SYOSHIFUJI Hideaki 			} else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
31168f27ebb9SYOSHIFUJI Hideaki 				spin_unlock(&ifp->lock);
31178f27ebb9SYOSHIFUJI Hideaki 				continue;
31181da177e4SLinus Torvalds 			} else if (age >= ifp->prefered_lft) {
31191da177e4SLinus Torvalds 				/* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
31201da177e4SLinus Torvalds 				int deprecate = 0;
31211da177e4SLinus Torvalds 
31221da177e4SLinus Torvalds 				if (!(ifp->flags&IFA_F_DEPRECATED)) {
31231da177e4SLinus Torvalds 					deprecate = 1;
31241da177e4SLinus Torvalds 					ifp->flags |= IFA_F_DEPRECATED;
31251da177e4SLinus Torvalds 				}
31261da177e4SLinus Torvalds 
31271da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
31281da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->valid_lft * HZ;
31291da177e4SLinus Torvalds 
31301da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
31311da177e4SLinus Torvalds 
31321da177e4SLinus Torvalds 				if (deprecate) {
31331da177e4SLinus Torvalds 					in6_ifa_hold(ifp);
31345d5780dfSYan Zheng 					read_unlock(&addrconf_hash_lock);
31351da177e4SLinus Torvalds 
31361da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
31371da177e4SLinus Torvalds 					in6_ifa_put(ifp);
31381da177e4SLinus Torvalds 					goto restart;
31391da177e4SLinus Torvalds 				}
31401da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
31411da177e4SLinus Torvalds 			} else if ((ifp->flags&IFA_F_TEMPORARY) &&
31421da177e4SLinus Torvalds 				   !(ifp->flags&IFA_F_TENTATIVE)) {
31431da177e4SLinus Torvalds 				if (age >= ifp->prefered_lft - regen_advance) {
31441da177e4SLinus Torvalds 					struct inet6_ifaddr *ifpub = ifp->ifpub;
31451da177e4SLinus Torvalds 					if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
31461da177e4SLinus Torvalds 						next = ifp->tstamp + ifp->prefered_lft * HZ;
31471da177e4SLinus Torvalds 					if (!ifp->regen_count && ifpub) {
31481da177e4SLinus Torvalds 						ifp->regen_count++;
31491da177e4SLinus Torvalds 						in6_ifa_hold(ifp);
31501da177e4SLinus Torvalds 						in6_ifa_hold(ifpub);
31511da177e4SLinus Torvalds 						spin_unlock(&ifp->lock);
31525d5780dfSYan Zheng 						read_unlock(&addrconf_hash_lock);
3153291d809bSHiroyuki YAMAMORI 						spin_lock(&ifpub->lock);
3154291d809bSHiroyuki YAMAMORI 						ifpub->regen_count = 0;
3155291d809bSHiroyuki YAMAMORI 						spin_unlock(&ifpub->lock);
31561da177e4SLinus Torvalds 						ipv6_create_tempaddr(ifpub, ifp);
31571da177e4SLinus Torvalds 						in6_ifa_put(ifpub);
31581da177e4SLinus Torvalds 						in6_ifa_put(ifp);
31591da177e4SLinus Torvalds 						goto restart;
31601da177e4SLinus Torvalds 					}
31611da177e4SLinus Torvalds 				} else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
31621da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
31631da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
31641da177e4SLinus Torvalds #endif
31651da177e4SLinus Torvalds 			} else {
31661da177e4SLinus Torvalds 				/* ifp->prefered_lft <= ifp->valid_lft */
31671da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
31681da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ;
31691da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
31701da177e4SLinus Torvalds 			}
31711da177e4SLinus Torvalds 		}
31725d5780dfSYan Zheng 		read_unlock(&addrconf_hash_lock);
31731da177e4SLinus Torvalds 	}
31741da177e4SLinus Torvalds 
31751da177e4SLinus Torvalds 	addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
31761da177e4SLinus Torvalds 	add_timer(&addr_chk_timer);
31771da177e4SLinus Torvalds 	spin_unlock_bh(&addrconf_verify_lock);
31781da177e4SLinus Torvalds }
31791da177e4SLinus Torvalds 
3180461d8837SThomas Graf static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
3181461d8837SThomas Graf {
3182461d8837SThomas Graf 	struct in6_addr *pfx = NULL;
3183461d8837SThomas Graf 
3184461d8837SThomas Graf 	if (addr)
3185461d8837SThomas Graf 		pfx = nla_data(addr);
3186461d8837SThomas Graf 
3187461d8837SThomas Graf 	if (local) {
3188461d8837SThomas Graf 		if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3189461d8837SThomas Graf 			pfx = NULL;
3190461d8837SThomas Graf 		else
3191461d8837SThomas Graf 			pfx = nla_data(local);
3192461d8837SThomas Graf 	}
3193461d8837SThomas Graf 
3194461d8837SThomas Graf 	return pfx;
3195461d8837SThomas Graf }
3196461d8837SThomas Graf 
3197ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
3198461d8837SThomas Graf 	[IFA_ADDRESS]		= { .len = sizeof(struct in6_addr) },
3199461d8837SThomas Graf 	[IFA_LOCAL]		= { .len = sizeof(struct in6_addr) },
3200461d8837SThomas Graf 	[IFA_CACHEINFO]		= { .len = sizeof(struct ifa_cacheinfo) },
3201461d8837SThomas Graf };
3202461d8837SThomas Graf 
32031da177e4SLinus Torvalds static int
32041da177e4SLinus Torvalds inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
32051da177e4SLinus Torvalds {
32063b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
3207b933f716SThomas Graf 	struct ifaddrmsg *ifm;
3208b933f716SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
32091da177e4SLinus Torvalds 	struct in6_addr *pfx;
3210b933f716SThomas Graf 	int err;
32111da177e4SLinus Torvalds 
3212b933f716SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3213b933f716SThomas Graf 	if (err < 0)
3214b933f716SThomas Graf 		return err;
3215b933f716SThomas Graf 
3216b933f716SThomas Graf 	ifm = nlmsg_data(nlh);
3217b933f716SThomas Graf 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
32181da177e4SLinus Torvalds 	if (pfx == NULL)
32191da177e4SLinus Torvalds 		return -EINVAL;
32201da177e4SLinus Torvalds 
3221af284937SDaniel Lezcano 	return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen);
32221da177e4SLinus Torvalds }
32231da177e4SLinus Torvalds 
322455ebaef1SNoriaki TAKAMIYA static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
322555ebaef1SNoriaki TAKAMIYA 			     u32 prefered_lft, u32 valid_lft)
3226081bba5bSNoriaki TAKAMIYA {
32276f704992SYOSHIFUJI Hideaki 	u32 flags;
32286f704992SYOSHIFUJI Hideaki 	clock_t expires;
32294bed72e4SYOSHIFUJI Hideaki 	unsigned long timeout;
323046d48046SYOSHIFUJI Hideaki 
3231081bba5bSNoriaki TAKAMIYA 	if (!valid_lft || (prefered_lft > valid_lft))
3232081bba5bSNoriaki TAKAMIYA 		return -EINVAL;
3233081bba5bSNoriaki TAKAMIYA 
32344bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
32354bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
32364bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
32374bed72e4SYOSHIFUJI Hideaki 		valid_lft = timeout;
32386f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
32394bed72e4SYOSHIFUJI Hideaki 	} else {
32404bed72e4SYOSHIFUJI Hideaki 		expires = 0;
32414bed72e4SYOSHIFUJI Hideaki 		flags = 0;
32424bed72e4SYOSHIFUJI Hideaki 		ifa_flags |= IFA_F_PERMANENT;
32436f704992SYOSHIFUJI Hideaki 	}
3244081bba5bSNoriaki TAKAMIYA 
32454bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
32464bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
32474bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
324855ebaef1SNoriaki TAKAMIYA 			ifa_flags |= IFA_F_DEPRECATED;
32494bed72e4SYOSHIFUJI Hideaki 		prefered_lft = timeout;
32504bed72e4SYOSHIFUJI Hideaki 	}
3251081bba5bSNoriaki TAKAMIYA 
3252081bba5bSNoriaki TAKAMIYA 	spin_lock_bh(&ifp->lock);
32533b9f9a1cSNoriaki TAKAMIYA 	ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags;
3254081bba5bSNoriaki TAKAMIYA 	ifp->tstamp = jiffies;
3255081bba5bSNoriaki TAKAMIYA 	ifp->valid_lft = valid_lft;
3256081bba5bSNoriaki TAKAMIYA 	ifp->prefered_lft = prefered_lft;
3257081bba5bSNoriaki TAKAMIYA 
3258081bba5bSNoriaki TAKAMIYA 	spin_unlock_bh(&ifp->lock);
3259081bba5bSNoriaki TAKAMIYA 	if (!(ifp->flags&IFA_F_TENTATIVE))
3260081bba5bSNoriaki TAKAMIYA 		ipv6_ifa_notify(0, ifp);
3261081bba5bSNoriaki TAKAMIYA 
326246d48046SYOSHIFUJI Hideaki 	addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
32636f704992SYOSHIFUJI Hideaki 			      expires, flags);
3264081bba5bSNoriaki TAKAMIYA 	addrconf_verify(0);
3265081bba5bSNoriaki TAKAMIYA 
3266081bba5bSNoriaki TAKAMIYA 	return 0;
3267081bba5bSNoriaki TAKAMIYA }
3268081bba5bSNoriaki TAKAMIYA 
3269081bba5bSNoriaki TAKAMIYA static int
32701da177e4SLinus Torvalds inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
32711da177e4SLinus Torvalds {
32723b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
3273461d8837SThomas Graf 	struct ifaddrmsg *ifm;
3274461d8837SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
32751da177e4SLinus Torvalds 	struct in6_addr *pfx;
32767198f8ceSThomas Graf 	struct inet6_ifaddr *ifa;
32777198f8ceSThomas Graf 	struct net_device *dev;
327855ebaef1SNoriaki TAKAMIYA 	u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
327955ebaef1SNoriaki TAKAMIYA 	u8 ifa_flags;
3280461d8837SThomas Graf 	int err;
32811da177e4SLinus Torvalds 
3282461d8837SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3283461d8837SThomas Graf 	if (err < 0)
3284461d8837SThomas Graf 		return err;
3285461d8837SThomas Graf 
3286461d8837SThomas Graf 	ifm = nlmsg_data(nlh);
3287461d8837SThomas Graf 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
32881da177e4SLinus Torvalds 	if (pfx == NULL)
32891da177e4SLinus Torvalds 		return -EINVAL;
32901da177e4SLinus Torvalds 
3291461d8837SThomas Graf 	if (tb[IFA_CACHEINFO]) {
32920778769dSNoriaki TAKAMIYA 		struct ifa_cacheinfo *ci;
3293461d8837SThomas Graf 
3294461d8837SThomas Graf 		ci = nla_data(tb[IFA_CACHEINFO]);
32950778769dSNoriaki TAKAMIYA 		valid_lft = ci->ifa_valid;
3296461d8837SThomas Graf 		preferred_lft = ci->ifa_prefered;
3297461d8837SThomas Graf 	} else {
3298461d8837SThomas Graf 		preferred_lft = INFINITY_LIFE_TIME;
3299461d8837SThomas Graf 		valid_lft = INFINITY_LIFE_TIME;
33000778769dSNoriaki TAKAMIYA 	}
33010778769dSNoriaki TAKAMIYA 
3302af284937SDaniel Lezcano 	dev =  __dev_get_by_index(net, ifm->ifa_index);
33037198f8ceSThomas Graf 	if (dev == NULL)
33047198f8ceSThomas Graf 		return -ENODEV;
3305081bba5bSNoriaki TAKAMIYA 
330655ebaef1SNoriaki TAKAMIYA 	/* We ignore other flags so far. */
33073b9f9a1cSNoriaki TAKAMIYA 	ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
330855ebaef1SNoriaki TAKAMIYA 
33091cab3da6SDaniel Lezcano 	ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
33107198f8ceSThomas Graf 	if (ifa == NULL) {
33117198f8ceSThomas Graf 		/*
33127198f8ceSThomas Graf 		 * It would be best to check for !NLM_F_CREATE here but
33137198f8ceSThomas Graf 		 * userspace alreay relies on not having to provide this.
33147198f8ceSThomas Graf 		 */
3315af284937SDaniel Lezcano 		return inet6_addr_add(net, ifm->ifa_index, pfx,
3316af284937SDaniel Lezcano 				      ifm->ifa_prefixlen, ifa_flags,
3317af284937SDaniel Lezcano 				      preferred_lft, valid_lft);
33181da177e4SLinus Torvalds 	}
33191da177e4SLinus Torvalds 
33207198f8ceSThomas Graf 	if (nlh->nlmsg_flags & NLM_F_EXCL ||
33217198f8ceSThomas Graf 	    !(nlh->nlmsg_flags & NLM_F_REPLACE))
33227198f8ceSThomas Graf 		err = -EEXIST;
33237198f8ceSThomas Graf 	else
332455ebaef1SNoriaki TAKAMIYA 		err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
33257198f8ceSThomas Graf 
33267198f8ceSThomas Graf 	in6_ifa_put(ifa);
33277198f8ceSThomas Graf 
33287198f8ceSThomas Graf 	return err;
33297198f8ceSThomas Graf }
33307198f8ceSThomas Graf 
3331101bb229SThomas Graf static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3332101bb229SThomas Graf 			  u8 scope, int ifindex)
3333101bb229SThomas Graf {
3334101bb229SThomas Graf 	struct ifaddrmsg *ifm;
3335101bb229SThomas Graf 
3336101bb229SThomas Graf 	ifm = nlmsg_data(nlh);
3337101bb229SThomas Graf 	ifm->ifa_family = AF_INET6;
3338101bb229SThomas Graf 	ifm->ifa_prefixlen = prefixlen;
3339101bb229SThomas Graf 	ifm->ifa_flags = flags;
3340101bb229SThomas Graf 	ifm->ifa_scope = scope;
3341101bb229SThomas Graf 	ifm->ifa_index = ifindex;
3342101bb229SThomas Graf }
3343101bb229SThomas Graf 
334485486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
334585486af0SThomas Graf 			 unsigned long tstamp, u32 preferred, u32 valid)
334685486af0SThomas Graf {
334785486af0SThomas Graf 	struct ifa_cacheinfo ci;
334885486af0SThomas Graf 
334985486af0SThomas Graf 	ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100
335085486af0SThomas Graf 			+ TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
335185486af0SThomas Graf 	ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100
335285486af0SThomas Graf 			+ TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
335385486af0SThomas Graf 	ci.ifa_prefered = preferred;
335485486af0SThomas Graf 	ci.ifa_valid = valid;
335585486af0SThomas Graf 
335685486af0SThomas Graf 	return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
335785486af0SThomas Graf }
335885486af0SThomas Graf 
3359101bb229SThomas Graf static inline int rt_scope(int ifa_scope)
3360101bb229SThomas Graf {
3361101bb229SThomas Graf 	if (ifa_scope & IFA_HOST)
3362101bb229SThomas Graf 		return RT_SCOPE_HOST;
3363101bb229SThomas Graf 	else if (ifa_scope & IFA_LINK)
3364101bb229SThomas Graf 		return RT_SCOPE_LINK;
3365101bb229SThomas Graf 	else if (ifa_scope & IFA_SITE)
3366101bb229SThomas Graf 		return RT_SCOPE_SITE;
3367101bb229SThomas Graf 	else
3368101bb229SThomas Graf 		return RT_SCOPE_UNIVERSE;
3369101bb229SThomas Graf }
3370101bb229SThomas Graf 
33710ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void)
33720ab6803bSThomas Graf {
3373339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3374339bf98fSThomas Graf 	       + nla_total_size(16) /* IFA_ADDRESS */
3375339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct ifa_cacheinfo));
33760ab6803bSThomas Graf }
3377c5396a31SYOSHIFUJI Hideaki 
33781da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
3379b6544c0bSJamal Hadi Salim 			     u32 pid, u32 seq, int event, unsigned int flags)
33801da177e4SLinus Torvalds {
33811da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
338285486af0SThomas Graf 	u32 preferred, valid;
33831da177e4SLinus Torvalds 
33840ab6803bSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
33850ab6803bSThomas Graf 	if (nlh == NULL)
338626932566SPatrick McHardy 		return -EMSGSIZE;
33870ab6803bSThomas Graf 
3388101bb229SThomas Graf 	put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3389101bb229SThomas Graf 		      ifa->idev->dev->ifindex);
3390101bb229SThomas Graf 
33911da177e4SLinus Torvalds 	if (!(ifa->flags&IFA_F_PERMANENT)) {
339285486af0SThomas Graf 		preferred = ifa->prefered_lft;
339385486af0SThomas Graf 		valid = ifa->valid_lft;
339485486af0SThomas Graf 		if (preferred != INFINITY_LIFE_TIME) {
33951da177e4SLinus Torvalds 			long tval = (jiffies - ifa->tstamp)/HZ;
339685486af0SThomas Graf 			preferred -= tval;
339785486af0SThomas Graf 			if (valid != INFINITY_LIFE_TIME)
339885486af0SThomas Graf 				valid -= tval;
33991da177e4SLinus Torvalds 		}
34001da177e4SLinus Torvalds 	} else {
340185486af0SThomas Graf 		preferred = INFINITY_LIFE_TIME;
340285486af0SThomas Graf 		valid = INFINITY_LIFE_TIME;
34031da177e4SLinus Torvalds 	}
340485486af0SThomas Graf 
34050ab6803bSThomas Graf 	if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
340626932566SPatrick McHardy 	    put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
340726932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
340826932566SPatrick McHardy 		return -EMSGSIZE;
340926932566SPatrick McHardy 	}
341085486af0SThomas Graf 
34110ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
34121da177e4SLinus Torvalds }
34131da177e4SLinus Torvalds 
34141da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
3415e431b8c0SJamal Hadi Salim 				u32 pid, u32 seq, int event, u16 flags)
34161da177e4SLinus Torvalds {
34171da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
3418101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
3419101bb229SThomas Graf 	int ifindex = ifmca->idev->dev->ifindex;
34201da177e4SLinus Torvalds 
34211da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3422101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
3423101bb229SThomas Graf 
34240ab6803bSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
34250ab6803bSThomas Graf 	if (nlh == NULL)
342626932566SPatrick McHardy 		return -EMSGSIZE;
34270ab6803bSThomas Graf 
3428101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
34290ab6803bSThomas Graf 	if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
34300ab6803bSThomas Graf 	    put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
343126932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
343226932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
343326932566SPatrick McHardy 		return -EMSGSIZE;
343426932566SPatrick McHardy 	}
343585486af0SThomas Graf 
34360ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
34371da177e4SLinus Torvalds }
34381da177e4SLinus Torvalds 
34391da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
3440b6544c0bSJamal Hadi Salim 				u32 pid, u32 seq, int event, unsigned int flags)
34411da177e4SLinus Torvalds {
34421da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
3443101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
3444101bb229SThomas Graf 	int ifindex = ifaca->aca_idev->dev->ifindex;
34451da177e4SLinus Torvalds 
34461da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3447101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
3448101bb229SThomas Graf 
34490ab6803bSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
34500ab6803bSThomas Graf 	if (nlh == NULL)
345126932566SPatrick McHardy 		return -EMSGSIZE;
34520ab6803bSThomas Graf 
3453101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
34540ab6803bSThomas Graf 	if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
34550ab6803bSThomas Graf 	    put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
345626932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
345726932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
345826932566SPatrick McHardy 		return -EMSGSIZE;
345926932566SPatrick McHardy 	}
346085486af0SThomas Graf 
34610ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
34621da177e4SLinus Torvalds }
34631da177e4SLinus Torvalds 
34641da177e4SLinus Torvalds enum addr_type_t
34651da177e4SLinus Torvalds {
34661da177e4SLinus Torvalds 	UNICAST_ADDR,
34671da177e4SLinus Torvalds 	MULTICAST_ADDR,
34681da177e4SLinus Torvalds 	ANYCAST_ADDR,
34691da177e4SLinus Torvalds };
34701da177e4SLinus Torvalds 
34711da177e4SLinus Torvalds static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
34721da177e4SLinus Torvalds 			   enum addr_type_t type)
34731da177e4SLinus Torvalds {
34741da177e4SLinus Torvalds 	int idx, ip_idx;
34751da177e4SLinus Torvalds 	int s_idx, s_ip_idx;
34761da177e4SLinus Torvalds 	int err = 1;
34771da177e4SLinus Torvalds 	struct net_device *dev;
34781da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
34791da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
34801da177e4SLinus Torvalds 	struct ifmcaddr6 *ifmca;
34811da177e4SLinus Torvalds 	struct ifacaddr6 *ifaca;
34823b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
34831da177e4SLinus Torvalds 
34841da177e4SLinus Torvalds 	s_idx = cb->args[0];
34851da177e4SLinus Torvalds 	s_ip_idx = ip_idx = cb->args[1];
34861da177e4SLinus Torvalds 
34877562f876SPavel Emelianov 	idx = 0;
34886fda7350SBenjamin Thery 	for_each_netdev(net, dev) {
34891da177e4SLinus Torvalds 		if (idx < s_idx)
34907562f876SPavel Emelianov 			goto cont;
34911da177e4SLinus Torvalds 		if (idx > s_idx)
34921da177e4SLinus Torvalds 			s_ip_idx = 0;
34931da177e4SLinus Torvalds 		ip_idx = 0;
34941da177e4SLinus Torvalds 		if ((idev = in6_dev_get(dev)) == NULL)
34957562f876SPavel Emelianov 			goto cont;
34961da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
34971da177e4SLinus Torvalds 		switch (type) {
34981da177e4SLinus Torvalds 		case UNICAST_ADDR:
3499ae9cda5dSYOSHIFUJI Hideaki 			/* unicast address incl. temp addr */
35001da177e4SLinus Torvalds 			for (ifa = idev->addr_list; ifa;
35011da177e4SLinus Torvalds 			     ifa = ifa->if_next, ip_idx++) {
35021da177e4SLinus Torvalds 				if (ip_idx < s_ip_idx)
35031da177e4SLinus Torvalds 					continue;
35045d5619b4SYOSHIFUJI Hideaki 				err = inet6_fill_ifaddr(skb, ifa,
35051da177e4SLinus Torvalds 							NETLINK_CB(cb->skb).pid,
35065d5619b4SYOSHIFUJI Hideaki 							cb->nlh->nlmsg_seq,
35075d5619b4SYOSHIFUJI Hideaki 							RTM_NEWADDR,
35085d5619b4SYOSHIFUJI Hideaki 							NLM_F_MULTI);
35091da177e4SLinus Torvalds 			}
35101da177e4SLinus Torvalds 			break;
35111da177e4SLinus Torvalds 		case MULTICAST_ADDR:
35121da177e4SLinus Torvalds 			/* multicast address */
35131da177e4SLinus Torvalds 			for (ifmca = idev->mc_list; ifmca;
35141da177e4SLinus Torvalds 			     ifmca = ifmca->next, ip_idx++) {
35151da177e4SLinus Torvalds 				if (ip_idx < s_ip_idx)
35161da177e4SLinus Torvalds 					continue;
35175d5619b4SYOSHIFUJI Hideaki 				err = inet6_fill_ifmcaddr(skb, ifmca,
35181da177e4SLinus Torvalds 							  NETLINK_CB(cb->skb).pid,
35195d5619b4SYOSHIFUJI Hideaki 							  cb->nlh->nlmsg_seq,
35205d5619b4SYOSHIFUJI Hideaki 							  RTM_GETMULTICAST,
35215d5619b4SYOSHIFUJI Hideaki 							  NLM_F_MULTI);
35221da177e4SLinus Torvalds 			}
35231da177e4SLinus Torvalds 			break;
35241da177e4SLinus Torvalds 		case ANYCAST_ADDR:
35251da177e4SLinus Torvalds 			/* anycast address */
35261da177e4SLinus Torvalds 			for (ifaca = idev->ac_list; ifaca;
35271da177e4SLinus Torvalds 			     ifaca = ifaca->aca_next, ip_idx++) {
35281da177e4SLinus Torvalds 				if (ip_idx < s_ip_idx)
35291da177e4SLinus Torvalds 					continue;
35305d5619b4SYOSHIFUJI Hideaki 				err = inet6_fill_ifacaddr(skb, ifaca,
35311da177e4SLinus Torvalds 							  NETLINK_CB(cb->skb).pid,
35325d5619b4SYOSHIFUJI Hideaki 							  cb->nlh->nlmsg_seq,
35335d5619b4SYOSHIFUJI Hideaki 							  RTM_GETANYCAST,
35345d5619b4SYOSHIFUJI Hideaki 							  NLM_F_MULTI);
35351da177e4SLinus Torvalds 			}
35361da177e4SLinus Torvalds 			break;
35371da177e4SLinus Torvalds 		default:
35381da177e4SLinus Torvalds 			break;
35391da177e4SLinus Torvalds 		}
35401da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
35411da177e4SLinus Torvalds 		in6_dev_put(idev);
35425d5619b4SYOSHIFUJI Hideaki 
35435d5619b4SYOSHIFUJI Hideaki 		if (err <= 0)
35445d5619b4SYOSHIFUJI Hideaki 			break;
35457562f876SPavel Emelianov cont:
35467562f876SPavel Emelianov 		idx++;
35471da177e4SLinus Torvalds 	}
35481da177e4SLinus Torvalds 	cb->args[0] = idx;
35491da177e4SLinus Torvalds 	cb->args[1] = ip_idx;
35501da177e4SLinus Torvalds 	return skb->len;
35511da177e4SLinus Torvalds }
35521da177e4SLinus Torvalds 
35531da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
35541da177e4SLinus Torvalds {
35551da177e4SLinus Torvalds 	enum addr_type_t type = UNICAST_ADDR;
3556b854272bSDenis V. Lunev 
35571da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
35581da177e4SLinus Torvalds }
35591da177e4SLinus Torvalds 
35601da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
35611da177e4SLinus Torvalds {
35621da177e4SLinus Torvalds 	enum addr_type_t type = MULTICAST_ADDR;
3563b854272bSDenis V. Lunev 
35641da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
35651da177e4SLinus Torvalds }
35661da177e4SLinus Torvalds 
35671da177e4SLinus Torvalds 
35681da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
35691da177e4SLinus Torvalds {
35701da177e4SLinus Torvalds 	enum addr_type_t type = ANYCAST_ADDR;
3571b854272bSDenis V. Lunev 
35721da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
35731da177e4SLinus Torvalds }
35741da177e4SLinus Torvalds 
35751b29fc2cSThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
35761b29fc2cSThomas Graf 			     void *arg)
35776c223828SNoriaki TAKAMIYA {
35783b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
35791b29fc2cSThomas Graf 	struct ifaddrmsg *ifm;
35801b29fc2cSThomas Graf 	struct nlattr *tb[IFA_MAX+1];
35816c223828SNoriaki TAKAMIYA 	struct in6_addr *addr = NULL;
35826c223828SNoriaki TAKAMIYA 	struct net_device *dev = NULL;
35836c223828SNoriaki TAKAMIYA 	struct inet6_ifaddr *ifa;
35846c223828SNoriaki TAKAMIYA 	struct sk_buff *skb;
35856c223828SNoriaki TAKAMIYA 	int err;
35866c223828SNoriaki TAKAMIYA 
35871b29fc2cSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
35881b29fc2cSThomas Graf 	if (err < 0)
35891b29fc2cSThomas Graf 		goto errout;
35906c223828SNoriaki TAKAMIYA 
35911b29fc2cSThomas Graf 	addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
35921b29fc2cSThomas Graf 	if (addr == NULL) {
35931b29fc2cSThomas Graf 		err = -EINVAL;
35941b29fc2cSThomas Graf 		goto errout;
35951b29fc2cSThomas Graf 	}
35961b29fc2cSThomas Graf 
35971b29fc2cSThomas Graf 	ifm = nlmsg_data(nlh);
35986c223828SNoriaki TAKAMIYA 	if (ifm->ifa_index)
35996fda7350SBenjamin Thery 		dev = __dev_get_by_index(net, ifm->ifa_index);
36006c223828SNoriaki TAKAMIYA 
36011cab3da6SDaniel Lezcano 	if ((ifa = ipv6_get_ifaddr(net, addr, dev, 1)) == NULL) {
36021b29fc2cSThomas Graf 		err = -EADDRNOTAVAIL;
36031b29fc2cSThomas Graf 		goto errout;
36046c223828SNoriaki TAKAMIYA 	}
36056c223828SNoriaki TAKAMIYA 
36060ab6803bSThomas Graf 	if ((skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL)) == NULL) {
36071b29fc2cSThomas Graf 		err = -ENOBUFS;
36081b29fc2cSThomas Graf 		goto errout_ifa;
36091b29fc2cSThomas Graf 	}
36101b29fc2cSThomas Graf 
36116c223828SNoriaki TAKAMIYA 	err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
36126c223828SNoriaki TAKAMIYA 				nlh->nlmsg_seq, RTM_NEWADDR, 0);
361326932566SPatrick McHardy 	if (err < 0) {
361426932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
361526932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
361626932566SPatrick McHardy 		kfree_skb(skb);
361726932566SPatrick McHardy 		goto errout_ifa;
361826932566SPatrick McHardy 	}
36196fda7350SBenjamin Thery 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
36201b29fc2cSThomas Graf errout_ifa:
36216c223828SNoriaki TAKAMIYA 	in6_ifa_put(ifa);
36221b29fc2cSThomas Graf errout:
36236c223828SNoriaki TAKAMIYA 	return err;
36246c223828SNoriaki TAKAMIYA }
36256c223828SNoriaki TAKAMIYA 
36261da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
36271da177e4SLinus Torvalds {
36281da177e4SLinus Torvalds 	struct sk_buff *skb;
3629c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(ifa->idev->dev);
36305d620266SThomas Graf 	int err = -ENOBUFS;
36311da177e4SLinus Torvalds 
36320ab6803bSThomas Graf 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
36335d620266SThomas Graf 	if (skb == NULL)
36345d620266SThomas Graf 		goto errout;
36355d620266SThomas Graf 
36365d620266SThomas Graf 	err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
363726932566SPatrick McHardy 	if (err < 0) {
363826932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
363926932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
364026932566SPatrick McHardy 		kfree_skb(skb);
364126932566SPatrick McHardy 		goto errout;
364226932566SPatrick McHardy 	}
36436fda7350SBenjamin Thery 	err = rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
36445d620266SThomas Graf errout:
36455d620266SThomas Graf 	if (err < 0)
36466fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
36471da177e4SLinus Torvalds }
36481da177e4SLinus Torvalds 
3649b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
36501da177e4SLinus Torvalds 				__s32 *array, int bytes)
36511da177e4SLinus Torvalds {
365204561c1fSThomas Graf 	BUG_ON(bytes < (DEVCONF_MAX * 4));
365304561c1fSThomas Graf 
36541da177e4SLinus Torvalds 	memset(array, 0, bytes);
36551da177e4SLinus Torvalds 	array[DEVCONF_FORWARDING] = cnf->forwarding;
36561da177e4SLinus Torvalds 	array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
36571da177e4SLinus Torvalds 	array[DEVCONF_MTU6] = cnf->mtu6;
36581da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
36591da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
36601da177e4SLinus Torvalds 	array[DEVCONF_AUTOCONF] = cnf->autoconf;
36611da177e4SLinus Torvalds 	array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
36621da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
36631da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
36641da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
36651da177e4SLinus Torvalds 	array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
36661da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
36671da177e4SLinus Torvalds 	array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
36681da177e4SLinus Torvalds 	array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
36691da177e4SLinus Torvalds 	array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
36701da177e4SLinus Torvalds 	array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
36711da177e4SLinus Torvalds 	array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
36721da177e4SLinus Torvalds #endif
36731da177e4SLinus Torvalds 	array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
367465f5c7c1SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
3675c4fd30ebSYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
3676930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
3677930d6ff2SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
367852e16356SYOSHIFUJI Hideaki 	array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval;
3679fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
368009c884d4SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
368109c884d4SYOSHIFUJI Hideaki #endif
3682930d6ff2SYOSHIFUJI Hideaki #endif
3683fbea49e1SYOSHIFUJI Hideaki 	array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
36840bcbc926SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
368595c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
368695c385b4SNeil Horman 	array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
368795c385b4SNeil Horman #endif
36887bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
36897bc570c8SYOSHIFUJI Hideaki 	array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
36907bc570c8SYOSHIFUJI Hideaki #endif
3691778d80beSYOSHIFUJI Hideaki 	array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
36921b34be74SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
36931da177e4SLinus Torvalds }
36941da177e4SLinus Torvalds 
3695339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void)
3696339bf98fSThomas Graf {
3697339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3698339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3699339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3700339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
3701339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
3702339bf98fSThomas Graf 	       + nla_total_size( /* IFLA_PROTINFO */
3703339bf98fSThomas Graf 			nla_total_size(4) /* IFLA_INET6_FLAGS */
3704339bf98fSThomas Graf 			+ nla_total_size(sizeof(struct ifla_cacheinfo))
3705339bf98fSThomas Graf 			+ nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
3706bf99f1bdSYOSHIFUJI Hideaki 			+ nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
3707bf99f1bdSYOSHIFUJI Hideaki 			+ nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
3708339bf98fSThomas Graf 		 );
3709339bf98fSThomas Graf }
3710c5396a31SYOSHIFUJI Hideaki 
37117f7d9a6bSHerbert Xu static inline void __snmp6_fill_stats(u64 *stats, void **mib, int items,
37127f7d9a6bSHerbert Xu 				      int bytes)
37137f7d9a6bSHerbert Xu {
37147f7d9a6bSHerbert Xu 	int i;
37157f7d9a6bSHerbert Xu 	int pad = bytes - sizeof(u64) * items;
37167f7d9a6bSHerbert Xu 	BUG_ON(pad < 0);
37177f7d9a6bSHerbert Xu 
37187f7d9a6bSHerbert Xu 	/* Use put_unaligned() because stats may not be aligned for u64. */
37197f7d9a6bSHerbert Xu 	put_unaligned(items, &stats[0]);
37207f7d9a6bSHerbert Xu 	for (i = 1; i < items; i++)
37217f7d9a6bSHerbert Xu 		put_unaligned(snmp_fold_field(mib, i), &stats[i]);
37227f7d9a6bSHerbert Xu 
37237f7d9a6bSHerbert Xu 	memset(&stats[items], 0, pad);
37247f7d9a6bSHerbert Xu }
37257f7d9a6bSHerbert Xu 
37267f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
37277f7d9a6bSHerbert Xu 			     int bytes)
37287f7d9a6bSHerbert Xu {
37297f7d9a6bSHerbert Xu 	switch(attrtype) {
37307f7d9a6bSHerbert Xu 	case IFLA_INET6_STATS:
37317f7d9a6bSHerbert Xu 		__snmp6_fill_stats(stats, (void **)idev->stats.ipv6, IPSTATS_MIB_MAX, bytes);
37327f7d9a6bSHerbert Xu 		break;
37337f7d9a6bSHerbert Xu 	case IFLA_INET6_ICMP6STATS:
37347f7d9a6bSHerbert Xu 		__snmp6_fill_stats(stats, (void **)idev->stats.icmpv6, ICMP6_MIB_MAX, bytes);
37357f7d9a6bSHerbert Xu 		break;
37367f7d9a6bSHerbert Xu 	}
37377f7d9a6bSHerbert Xu }
37387f7d9a6bSHerbert Xu 
37391da177e4SLinus Torvalds static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
3740b6544c0bSJamal Hadi Salim 			     u32 pid, u32 seq, int event, unsigned int flags)
37411da177e4SLinus Torvalds {
37421da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
3743bf99f1bdSYOSHIFUJI Hideaki 	struct nlattr *nla;
374404561c1fSThomas Graf 	struct ifinfomsg *hdr;
37451da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
374604561c1fSThomas Graf 	void *protoinfo;
37471da177e4SLinus Torvalds 	struct ifla_cacheinfo ci;
37481da177e4SLinus Torvalds 
374904561c1fSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
375004561c1fSThomas Graf 	if (nlh == NULL)
375126932566SPatrick McHardy 		return -EMSGSIZE;
37521da177e4SLinus Torvalds 
375304561c1fSThomas Graf 	hdr = nlmsg_data(nlh);
375404561c1fSThomas Graf 	hdr->ifi_family = AF_INET6;
375504561c1fSThomas Graf 	hdr->__ifi_pad = 0;
375604561c1fSThomas Graf 	hdr->ifi_type = dev->type;
375704561c1fSThomas Graf 	hdr->ifi_index = dev->ifindex;
375804561c1fSThomas Graf 	hdr->ifi_flags = dev_get_flags(dev);
375904561c1fSThomas Graf 	hdr->ifi_change = 0;
376004561c1fSThomas Graf 
376104561c1fSThomas Graf 	NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
37621da177e4SLinus Torvalds 
37631da177e4SLinus Torvalds 	if (dev->addr_len)
376404561c1fSThomas Graf 		NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
37651da177e4SLinus Torvalds 
376604561c1fSThomas Graf 	NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
37671da177e4SLinus Torvalds 	if (dev->ifindex != dev->iflink)
376804561c1fSThomas Graf 		NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
37691da177e4SLinus Torvalds 
377004561c1fSThomas Graf 	protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
377104561c1fSThomas Graf 	if (protoinfo == NULL)
377204561c1fSThomas Graf 		goto nla_put_failure;
37731da177e4SLinus Torvalds 
377404561c1fSThomas Graf 	NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags);
37751da177e4SLinus Torvalds 
37761da177e4SLinus Torvalds 	ci.max_reasm_len = IPV6_MAXPLEN;
37771da177e4SLinus Torvalds 	ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
37781da177e4SLinus Torvalds 		    + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
37791da177e4SLinus Torvalds 	ci.reachable_time = idev->nd_parms->reachable_time;
37801da177e4SLinus Torvalds 	ci.retrans_time = idev->nd_parms->retrans_time;
378104561c1fSThomas Graf 	NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
37821da177e4SLinus Torvalds 
3783bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
3784bf99f1bdSYOSHIFUJI Hideaki 	if (nla == NULL)
378504561c1fSThomas Graf 		goto nla_put_failure;
3786bf99f1bdSYOSHIFUJI Hideaki 	ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
37871da177e4SLinus Torvalds 
3788bf99f1bdSYOSHIFUJI Hideaki 	/* XXX - MC not implemented */
3789bf99f1bdSYOSHIFUJI Hideaki 
3790bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
3791bf99f1bdSYOSHIFUJI Hideaki 	if (nla == NULL)
3792bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
3793bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
3794bf99f1bdSYOSHIFUJI Hideaki 
3795bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
3796bf99f1bdSYOSHIFUJI Hideaki 	if (nla == NULL)
3797bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
3798bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
37991da177e4SLinus Torvalds 
380004561c1fSThomas Graf 	nla_nest_end(skb, protoinfo);
380104561c1fSThomas Graf 	return nlmsg_end(skb, nlh);
38021da177e4SLinus Torvalds 
380304561c1fSThomas Graf nla_put_failure:
380426932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
380526932566SPatrick McHardy 	return -EMSGSIZE;
38061da177e4SLinus Torvalds }
38071da177e4SLinus Torvalds 
38081da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
38091da177e4SLinus Torvalds {
38103b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
38111da177e4SLinus Torvalds 	int idx, err;
38121da177e4SLinus Torvalds 	int s_idx = cb->args[0];
38131da177e4SLinus Torvalds 	struct net_device *dev;
38141da177e4SLinus Torvalds 	struct inet6_dev *idev;
38151da177e4SLinus Torvalds 
38161da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
38177562f876SPavel Emelianov 	idx = 0;
38186fda7350SBenjamin Thery 	for_each_netdev(net, dev) {
38191da177e4SLinus Torvalds 		if (idx < s_idx)
38207562f876SPavel Emelianov 			goto cont;
38211da177e4SLinus Torvalds 		if ((idev = in6_dev_get(dev)) == NULL)
38227562f876SPavel Emelianov 			goto cont;
38231da177e4SLinus Torvalds 		err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
3824b6544c0bSJamal Hadi Salim 				cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
38251da177e4SLinus Torvalds 		in6_dev_put(idev);
38261da177e4SLinus Torvalds 		if (err <= 0)
38271da177e4SLinus Torvalds 			break;
38287562f876SPavel Emelianov cont:
38297562f876SPavel Emelianov 		idx++;
38301da177e4SLinus Torvalds 	}
38311da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
38321da177e4SLinus Torvalds 	cb->args[0] = idx;
38331da177e4SLinus Torvalds 
38341da177e4SLinus Torvalds 	return skb->len;
38351da177e4SLinus Torvalds }
38361da177e4SLinus Torvalds 
38371da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
38381da177e4SLinus Torvalds {
38391da177e4SLinus Torvalds 	struct sk_buff *skb;
3840c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
38418d7a76c9SThomas Graf 	int err = -ENOBUFS;
38421da177e4SLinus Torvalds 
3843339bf98fSThomas Graf 	skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
38448d7a76c9SThomas Graf 	if (skb == NULL)
38458d7a76c9SThomas Graf 		goto errout;
38468d7a76c9SThomas Graf 
38478d7a76c9SThomas Graf 	err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
384826932566SPatrick McHardy 	if (err < 0) {
384926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
385026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
385126932566SPatrick McHardy 		kfree_skb(skb);
385226932566SPatrick McHardy 		goto errout;
385326932566SPatrick McHardy 	}
38546fda7350SBenjamin Thery 	err = rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
38558d7a76c9SThomas Graf errout:
38568d7a76c9SThomas Graf 	if (err < 0)
38576fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
38581da177e4SLinus Torvalds }
38591da177e4SLinus Torvalds 
3860339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void)
3861339bf98fSThomas Graf {
3862339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct prefixmsg))
3863339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct in6_addr))
3864339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct prefix_cacheinfo));
3865339bf98fSThomas Graf }
3866c5396a31SYOSHIFUJI Hideaki 
38671da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
3868b6544c0bSJamal Hadi Salim 			     struct prefix_info *pinfo, u32 pid, u32 seq,
3869b6544c0bSJamal Hadi Salim 			     int event, unsigned int flags)
38701da177e4SLinus Torvalds {
38711da177e4SLinus Torvalds 	struct prefixmsg *pmsg;
38721da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
38731da177e4SLinus Torvalds 	struct prefix_cacheinfo	ci;
38741da177e4SLinus Torvalds 
38756051e2f4SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags);
38766051e2f4SThomas Graf 	if (nlh == NULL)
387726932566SPatrick McHardy 		return -EMSGSIZE;
38786051e2f4SThomas Graf 
38796051e2f4SThomas Graf 	pmsg = nlmsg_data(nlh);
38801da177e4SLinus Torvalds 	pmsg->prefix_family = AF_INET6;
38818a47077aSPatrick McHardy 	pmsg->prefix_pad1 = 0;
38828a47077aSPatrick McHardy 	pmsg->prefix_pad2 = 0;
38831da177e4SLinus Torvalds 	pmsg->prefix_ifindex = idev->dev->ifindex;
38841da177e4SLinus Torvalds 	pmsg->prefix_len = pinfo->prefix_len;
38851da177e4SLinus Torvalds 	pmsg->prefix_type = pinfo->type;
38868a47077aSPatrick McHardy 	pmsg->prefix_pad3 = 0;
38871da177e4SLinus Torvalds 	pmsg->prefix_flags = 0;
38881da177e4SLinus Torvalds 	if (pinfo->onlink)
38891da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_ONLINK;
38901da177e4SLinus Torvalds 	if (pinfo->autoconf)
38911da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
38921da177e4SLinus Torvalds 
38936051e2f4SThomas Graf 	NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
38941da177e4SLinus Torvalds 
38951da177e4SLinus Torvalds 	ci.preferred_time = ntohl(pinfo->prefered);
38961da177e4SLinus Torvalds 	ci.valid_time = ntohl(pinfo->valid);
38976051e2f4SThomas Graf 	NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
38981da177e4SLinus Torvalds 
38996051e2f4SThomas Graf 	return nlmsg_end(skb, nlh);
39001da177e4SLinus Torvalds 
39016051e2f4SThomas Graf nla_put_failure:
390226932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
390326932566SPatrick McHardy 	return -EMSGSIZE;
39041da177e4SLinus Torvalds }
39051da177e4SLinus Torvalds 
39061da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
39071da177e4SLinus Torvalds 			 struct prefix_info *pinfo)
39081da177e4SLinus Torvalds {
39091da177e4SLinus Torvalds 	struct sk_buff *skb;
3910c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
39118c384bfaSThomas Graf 	int err = -ENOBUFS;
39121da177e4SLinus Torvalds 
3913339bf98fSThomas Graf 	skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
39148c384bfaSThomas Graf 	if (skb == NULL)
39158c384bfaSThomas Graf 		goto errout;
39168c384bfaSThomas Graf 
39178c384bfaSThomas Graf 	err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
391826932566SPatrick McHardy 	if (err < 0) {
391926932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
392026932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
392126932566SPatrick McHardy 		kfree_skb(skb);
392226932566SPatrick McHardy 		goto errout;
392326932566SPatrick McHardy 	}
39246fda7350SBenjamin Thery 	err = rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
39258c384bfaSThomas Graf errout:
39268c384bfaSThomas Graf 	if (err < 0)
39276fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
39281da177e4SLinus Torvalds }
39291da177e4SLinus Torvalds 
39301da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
39311da177e4SLinus Torvalds {
39321da177e4SLinus Torvalds 	inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
39331da177e4SLinus Torvalds 
39341da177e4SLinus Torvalds 	switch (event) {
39351da177e4SLinus Torvalds 	case RTM_NEWADDR:
393695c385b4SNeil Horman 		/*
393795c385b4SNeil Horman 		 * If the address was optimistic
393895c385b4SNeil Horman 		 * we inserted the route at the start of
393995c385b4SNeil Horman 		 * our DAD process, so we don't need
394095c385b4SNeil Horman 		 * to do it again
394195c385b4SNeil Horman 		 */
394295c385b4SNeil Horman 		if (!(ifp->rt->rt6i_node))
394340e22e8fSThomas Graf 			ip6_ins_rt(ifp->rt);
39441da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
39451da177e4SLinus Torvalds 			addrconf_join_anycast(ifp);
39461da177e4SLinus Torvalds 		break;
39471da177e4SLinus Torvalds 	case RTM_DELADDR:
39481da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
39491da177e4SLinus Torvalds 			addrconf_leave_anycast(ifp);
39501da177e4SLinus Torvalds 		addrconf_leave_solict(ifp->idev, &ifp->addr);
39511da177e4SLinus Torvalds 		dst_hold(&ifp->rt->u.dst);
3952e0a1ad73SThomas Graf 		if (ip6_del_rt(ifp->rt))
39531da177e4SLinus Torvalds 			dst_free(&ifp->rt->u.dst);
39541da177e4SLinus Torvalds 		break;
39551da177e4SLinus Torvalds 	}
39561da177e4SLinus Torvalds }
39571da177e4SLinus Torvalds 
39581da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
39591da177e4SLinus Torvalds {
39608814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
39611da177e4SLinus Torvalds 	if (likely(ifp->idev->dead == 0))
39621da177e4SLinus Torvalds 		__ipv6_ifa_notify(event, ifp);
39638814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
39641da177e4SLinus Torvalds }
39651da177e4SLinus Torvalds 
39661da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
39671da177e4SLinus Torvalds 
39681da177e4SLinus Torvalds static
39691da177e4SLinus Torvalds int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
39701da177e4SLinus Torvalds 			   void __user *buffer, size_t *lenp, loff_t *ppos)
39711da177e4SLinus Torvalds {
39721da177e4SLinus Torvalds 	int *valp = ctl->data;
39731da177e4SLinus Torvalds 	int val = *valp;
39741da177e4SLinus Torvalds 	int ret;
39751da177e4SLinus Torvalds 
39761da177e4SLinus Torvalds 	ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
39771da177e4SLinus Torvalds 
3978c8fecf22SPavel Emelyanov 	if (write)
3979c8fecf22SPavel Emelyanov 		addrconf_fixup_forwarding(ctl, valp, val);
39801da177e4SLinus Torvalds 	return ret;
39811da177e4SLinus Torvalds }
39821da177e4SLinus Torvalds 
39831da177e4SLinus Torvalds static int addrconf_sysctl_forward_strategy(ctl_table *table,
39841da177e4SLinus Torvalds 					    void __user *oldval,
39851da177e4SLinus Torvalds 					    size_t __user *oldlenp,
39861f29bcd7SAlexey Dobriyan 					    void __user *newval, size_t newlen)
39871da177e4SLinus Torvalds {
39881da177e4SLinus Torvalds 	int *valp = table->data;
3989c8fecf22SPavel Emelyanov 	int val = *valp;
39901da177e4SLinus Torvalds 	int new;
39911da177e4SLinus Torvalds 
39921da177e4SLinus Torvalds 	if (!newval || !newlen)
39931da177e4SLinus Torvalds 		return 0;
39941da177e4SLinus Torvalds 	if (newlen != sizeof(int))
39951da177e4SLinus Torvalds 		return -EINVAL;
39961da177e4SLinus Torvalds 	if (get_user(new, (int __user *)newval))
39971da177e4SLinus Torvalds 		return -EFAULT;
39981da177e4SLinus Torvalds 	if (new == *valp)
39991da177e4SLinus Torvalds 		return 0;
40001da177e4SLinus Torvalds 	if (oldval && oldlenp) {
40011da177e4SLinus Torvalds 		size_t len;
40021da177e4SLinus Torvalds 		if (get_user(len, oldlenp))
40031da177e4SLinus Torvalds 			return -EFAULT;
40041da177e4SLinus Torvalds 		if (len) {
40051da177e4SLinus Torvalds 			if (len > table->maxlen)
40061da177e4SLinus Torvalds 				len = table->maxlen;
40071da177e4SLinus Torvalds 			if (copy_to_user(oldval, valp, len))
40081da177e4SLinus Torvalds 				return -EFAULT;
40091da177e4SLinus Torvalds 			if (put_user(len, oldlenp))
40101da177e4SLinus Torvalds 				return -EFAULT;
40111da177e4SLinus Torvalds 		}
40121da177e4SLinus Torvalds 	}
40131da177e4SLinus Torvalds 
40141da177e4SLinus Torvalds 	*valp = new;
4015c8fecf22SPavel Emelyanov 	addrconf_fixup_forwarding(table, valp, val);
40161da177e4SLinus Torvalds 	return 1;
40171da177e4SLinus Torvalds }
40181da177e4SLinus Torvalds 
40191da177e4SLinus Torvalds static struct addrconf_sysctl_table
40201da177e4SLinus Torvalds {
40211da177e4SLinus Torvalds 	struct ctl_table_header *sysctl_header;
4022f6a07b29SYOSHIFUJI Hideaki 	ctl_table addrconf_vars[DEVCONF_MAX+1];
40231dab6222SPavel Emelyanov 	char *dev_name;
4024ab32ea5dSBrian Haley } addrconf_sysctl __read_mostly = {
40251da177e4SLinus Torvalds 	.sysctl_header = NULL,
40261da177e4SLinus Torvalds 	.addrconf_vars = {
40271da177e4SLinus Torvalds 		{
40281da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_FORWARDING,
40291da177e4SLinus Torvalds 			.procname	=	"forwarding",
40301da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.forwarding,
40311da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40321da177e4SLinus Torvalds 			.mode		=	0644,
40331da177e4SLinus Torvalds 			.proc_handler	=	&addrconf_sysctl_forward,
40341da177e4SLinus Torvalds 			.strategy	=	&addrconf_sysctl_forward_strategy,
40351da177e4SLinus Torvalds 		},
40361da177e4SLinus Torvalds 		{
40371da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_HOP_LIMIT,
40381da177e4SLinus Torvalds 			.procname	=	"hop_limit",
40391da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.hop_limit,
40401da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40411da177e4SLinus Torvalds 			.mode		=	0644,
40421da177e4SLinus Torvalds 			.proc_handler	=	proc_dointvec,
40431da177e4SLinus Torvalds 		},
40441da177e4SLinus Torvalds 		{
40451da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_MTU,
40461da177e4SLinus Torvalds 			.procname	=	"mtu",
40471da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.mtu6,
40481da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40491da177e4SLinus Torvalds 			.mode		=	0644,
40501da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
40511da177e4SLinus Torvalds 		},
40521da177e4SLinus Torvalds 		{
40531da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_ACCEPT_RA,
40541da177e4SLinus Torvalds 			.procname	=	"accept_ra",
40551da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.accept_ra,
40561da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40571da177e4SLinus Torvalds 			.mode		=	0644,
40581da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
40591da177e4SLinus Torvalds 		},
40601da177e4SLinus Torvalds 		{
40611da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_ACCEPT_REDIRECTS,
40621da177e4SLinus Torvalds 			.procname	=	"accept_redirects",
40631da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.accept_redirects,
40641da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40651da177e4SLinus Torvalds 			.mode		=	0644,
40661da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
40671da177e4SLinus Torvalds 		},
40681da177e4SLinus Torvalds 		{
40691da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_AUTOCONF,
40701da177e4SLinus Torvalds 			.procname	=	"autoconf",
40711da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.autoconf,
40721da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40731da177e4SLinus Torvalds 			.mode		=	0644,
40741da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
40751da177e4SLinus Torvalds 		},
40761da177e4SLinus Torvalds 		{
40771da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_DAD_TRANSMITS,
40781da177e4SLinus Torvalds 			.procname	=	"dad_transmits",
40791da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.dad_transmits,
40801da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40811da177e4SLinus Torvalds 			.mode		=	0644,
40821da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
40831da177e4SLinus Torvalds 		},
40841da177e4SLinus Torvalds 		{
40851da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_RTR_SOLICITS,
40861da177e4SLinus Torvalds 			.procname	=	"router_solicitations",
40871da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.rtr_solicits,
40881da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40891da177e4SLinus Torvalds 			.mode		=	0644,
40901da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
40911da177e4SLinus Torvalds 		},
40921da177e4SLinus Torvalds 		{
40931da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_RTR_SOLICIT_INTERVAL,
40941da177e4SLinus Torvalds 			.procname	=	"router_solicitation_interval",
40951da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.rtr_solicit_interval,
40961da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40971da177e4SLinus Torvalds 			.mode		=	0644,
40981da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec_jiffies,
40991da177e4SLinus Torvalds 			.strategy	=	&sysctl_jiffies,
41001da177e4SLinus Torvalds 		},
41011da177e4SLinus Torvalds 		{
41021da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_RTR_SOLICIT_DELAY,
41031da177e4SLinus Torvalds 			.procname	=	"router_solicitation_delay",
41041da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.rtr_solicit_delay,
41051da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
41061da177e4SLinus Torvalds 			.mode		=	0644,
41071da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec_jiffies,
41081da177e4SLinus Torvalds 			.strategy	=	&sysctl_jiffies,
41091da177e4SLinus Torvalds 		},
41101da177e4SLinus Torvalds 		{
41111da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_FORCE_MLD_VERSION,
41121da177e4SLinus Torvalds 			.procname	=	"force_mld_version",
41131da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.force_mld_version,
41141da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
41151da177e4SLinus Torvalds 			.mode		=	0644,
41161da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
41171da177e4SLinus Torvalds 		},
41181da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
41191da177e4SLinus Torvalds 		{
41201da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_USE_TEMPADDR,
41211da177e4SLinus Torvalds 			.procname	=	"use_tempaddr",
41221da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.use_tempaddr,
41231da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
41241da177e4SLinus Torvalds 			.mode		=	0644,
41251da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
41261da177e4SLinus Torvalds 		},
41271da177e4SLinus Torvalds 		{
41281da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_TEMP_VALID_LFT,
41291da177e4SLinus Torvalds 			.procname	=	"temp_valid_lft",
41301da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.temp_valid_lft,
41311da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
41321da177e4SLinus Torvalds 			.mode		=	0644,
41331da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
41341da177e4SLinus Torvalds 		},
41351da177e4SLinus Torvalds 		{
41361da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_TEMP_PREFERED_LFT,
41371da177e4SLinus Torvalds 			.procname	=	"temp_prefered_lft",
41381da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.temp_prefered_lft,
41391da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
41401da177e4SLinus Torvalds 			.mode		=	0644,
41411da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
41421da177e4SLinus Torvalds 		},
41431da177e4SLinus Torvalds 		{
41441da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_REGEN_MAX_RETRY,
41451da177e4SLinus Torvalds 			.procname	=	"regen_max_retry",
41461da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.regen_max_retry,
41471da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
41481da177e4SLinus Torvalds 			.mode		=	0644,
41491da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
41501da177e4SLinus Torvalds 		},
41511da177e4SLinus Torvalds 		{
41521da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_MAX_DESYNC_FACTOR,
41531da177e4SLinus Torvalds 			.procname	=	"max_desync_factor",
41541da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.max_desync_factor,
41551da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
41561da177e4SLinus Torvalds 			.mode		=	0644,
41571da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
41581da177e4SLinus Torvalds 		},
41591da177e4SLinus Torvalds #endif
41601da177e4SLinus Torvalds 		{
41611da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_MAX_ADDRESSES,
41621da177e4SLinus Torvalds 			.procname	=	"max_addresses",
41631da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.max_addresses,
41641da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
41651da177e4SLinus Torvalds 			.mode		=	0644,
41661da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
41671da177e4SLinus Torvalds 		},
41681da177e4SLinus Torvalds 		{
416965f5c7c1SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_RA_DEFRTR,
417065f5c7c1SYOSHIFUJI Hideaki 			.procname	=	"accept_ra_defrtr",
417165f5c7c1SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_ra_defrtr,
417265f5c7c1SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
417365f5c7c1SYOSHIFUJI Hideaki 			.mode		=	0644,
417465f5c7c1SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
417565f5c7c1SYOSHIFUJI Hideaki 		},
417665f5c7c1SYOSHIFUJI Hideaki 		{
4177c4fd30ebSYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_RA_PINFO,
4178c4fd30ebSYOSHIFUJI Hideaki 			.procname	=	"accept_ra_pinfo",
4179c4fd30ebSYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_ra_pinfo,
4180c4fd30ebSYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
4181c4fd30ebSYOSHIFUJI Hideaki 			.mode		=	0644,
4182c4fd30ebSYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
4183c4fd30ebSYOSHIFUJI Hideaki 		},
4184930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
4185930d6ff2SYOSHIFUJI Hideaki 		{
4186930d6ff2SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_RA_RTR_PREF,
4187930d6ff2SYOSHIFUJI Hideaki 			.procname	=	"accept_ra_rtr_pref",
4188930d6ff2SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_ra_rtr_pref,
4189930d6ff2SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
4190930d6ff2SYOSHIFUJI Hideaki 			.mode		=	0644,
4191930d6ff2SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
4192930d6ff2SYOSHIFUJI Hideaki 		},
419352e16356SYOSHIFUJI Hideaki 		{
419452e16356SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_RTR_PROBE_INTERVAL,
419552e16356SYOSHIFUJI Hideaki 			.procname	=	"router_probe_interval",
419652e16356SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.rtr_probe_interval,
419752e16356SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
419852e16356SYOSHIFUJI Hideaki 			.mode		=	0644,
419952e16356SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec_jiffies,
420052e16356SYOSHIFUJI Hideaki 			.strategy	=	&sysctl_jiffies,
420152e16356SYOSHIFUJI Hideaki 		},
4202fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
420309c884d4SYOSHIFUJI Hideaki 		{
420409c884d4SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN,
420509c884d4SYOSHIFUJI Hideaki 			.procname	=	"accept_ra_rt_info_max_plen",
420609c884d4SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_ra_rt_info_max_plen,
420709c884d4SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
420809c884d4SYOSHIFUJI Hideaki 			.mode		=	0644,
420909c884d4SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
421009c884d4SYOSHIFUJI Hideaki 		},
421109c884d4SYOSHIFUJI Hideaki #endif
4212930d6ff2SYOSHIFUJI Hideaki #endif
4213c4fd30ebSYOSHIFUJI Hideaki 		{
4214fbea49e1SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_PROXY_NDP,
4215fbea49e1SYOSHIFUJI Hideaki 			.procname	=	"proxy_ndp",
4216fbea49e1SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.proxy_ndp,
4217fbea49e1SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
4218fbea49e1SYOSHIFUJI Hideaki 			.mode		=	0644,
4219fbea49e1SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
4220fbea49e1SYOSHIFUJI Hideaki 		},
4221fbea49e1SYOSHIFUJI Hideaki 		{
42220bcbc926SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_SOURCE_ROUTE,
42230bcbc926SYOSHIFUJI Hideaki 			.procname	=	"accept_source_route",
42240bcbc926SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_source_route,
42250bcbc926SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
42260bcbc926SYOSHIFUJI Hideaki 			.mode		=	0644,
42270bcbc926SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
42280bcbc926SYOSHIFUJI Hideaki 		},
422995c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
423095c385b4SNeil Horman 		{
423195c385b4SNeil Horman 			.ctl_name	=	CTL_UNNUMBERED,
423295c385b4SNeil Horman 			.procname       =       "optimistic_dad",
423395c385b4SNeil Horman 			.data           =       &ipv6_devconf.optimistic_dad,
423495c385b4SNeil Horman 			.maxlen         =       sizeof(int),
423595c385b4SNeil Horman 			.mode           =       0644,
423695c385b4SNeil Horman 			.proc_handler   =       &proc_dointvec,
423795c385b4SNeil Horman 
423895c385b4SNeil Horman 		},
423995c385b4SNeil Horman #endif
42407bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
42417bc570c8SYOSHIFUJI Hideaki 		{
42427bc570c8SYOSHIFUJI Hideaki 			.ctl_name	=	CTL_UNNUMBERED,
42437bc570c8SYOSHIFUJI Hideaki 			.procname	=	"mc_forwarding",
42447bc570c8SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.mc_forwarding,
42457bc570c8SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
42467bc570c8SYOSHIFUJI Hideaki 			.mode		=	0644,
42477bc570c8SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
42487bc570c8SYOSHIFUJI Hideaki 		},
42497bc570c8SYOSHIFUJI Hideaki #endif
42500bcbc926SYOSHIFUJI Hideaki 		{
4251778d80beSYOSHIFUJI Hideaki 			.ctl_name	=	CTL_UNNUMBERED,
4252778d80beSYOSHIFUJI Hideaki 			.procname	=	"disable_ipv6",
4253778d80beSYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.disable_ipv6,
4254778d80beSYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
4255778d80beSYOSHIFUJI Hideaki 			.mode		=	0644,
4256778d80beSYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
4257778d80beSYOSHIFUJI Hideaki 		},
4258778d80beSYOSHIFUJI Hideaki 		{
42591b34be74SYOSHIFUJI Hideaki 			.ctl_name	=	CTL_UNNUMBERED,
42601b34be74SYOSHIFUJI Hideaki 			.procname	=	"accept_dad",
42611b34be74SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_dad,
42621b34be74SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
42631b34be74SYOSHIFUJI Hideaki 			.mode		=	0644,
42641b34be74SYOSHIFUJI Hideaki 			.proc_handler	=	&proc_dointvec,
42651b34be74SYOSHIFUJI Hideaki 		},
42661b34be74SYOSHIFUJI Hideaki 		{
42671da177e4SLinus Torvalds 			.ctl_name	=	0,	/* sentinel */
42681da177e4SLinus Torvalds 		}
42691da177e4SLinus Torvalds 	},
42701da177e4SLinus Torvalds };
42711da177e4SLinus Torvalds 
4272bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name,
4273bff16c2fSPavel Emelyanov 		int ctl_name, struct inet6_dev *idev, struct ipv6_devconf *p)
42741da177e4SLinus Torvalds {
42751da177e4SLinus Torvalds 	int i;
42761da177e4SLinus Torvalds 	struct addrconf_sysctl_table *t;
42771da177e4SLinus Torvalds 
42781dab6222SPavel Emelyanov #define ADDRCONF_CTL_PATH_DEV	3
42791dab6222SPavel Emelyanov 
42801dab6222SPavel Emelyanov 	struct ctl_path addrconf_ctl_path[] = {
42811dab6222SPavel Emelyanov 		{ .procname = "net", .ctl_name = CTL_NET, },
42821dab6222SPavel Emelyanov 		{ .procname = "ipv6", .ctl_name = NET_IPV6, },
42831dab6222SPavel Emelyanov 		{ .procname = "conf", .ctl_name = NET_IPV6_CONF, },
42841dab6222SPavel Emelyanov 		{ /* to be set */ },
42851dab6222SPavel Emelyanov 		{ },
42861dab6222SPavel Emelyanov 	};
42871dab6222SPavel Emelyanov 
42881dab6222SPavel Emelyanov 
4289af879cc7SArnaldo Carvalho de Melo 	t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
42901da177e4SLinus Torvalds 	if (t == NULL)
4291f68635e6SPavel Emelyanov 		goto out;
4292f68635e6SPavel Emelyanov 
42931da177e4SLinus Torvalds 	for (i=0; t->addrconf_vars[i].data; i++) {
42941da177e4SLinus Torvalds 		t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
42951da177e4SLinus Torvalds 		t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
4296bff16c2fSPavel Emelyanov 		t->addrconf_vars[i].extra2 = net;
42971da177e4SLinus Torvalds 	}
42981da177e4SLinus Torvalds 
42991da177e4SLinus Torvalds 	/*
43001da177e4SLinus Torvalds 	 * Make a copy of dev_name, because '.procname' is regarded as const
43011da177e4SLinus Torvalds 	 * by sysctl and we wouldn't want anyone to change it under our feet
43021da177e4SLinus Torvalds 	 * (see SIOCSIFNAME).
43031da177e4SLinus Torvalds 	 */
43041dab6222SPavel Emelyanov 	t->dev_name = kstrdup(dev_name, GFP_KERNEL);
43051dab6222SPavel Emelyanov 	if (!t->dev_name)
43061da177e4SLinus Torvalds 		goto free;
43071da177e4SLinus Torvalds 
43081dab6222SPavel Emelyanov 	addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].procname = t->dev_name;
43091dab6222SPavel Emelyanov 	addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].ctl_name = ctl_name;
43101da177e4SLinus Torvalds 
4311bff16c2fSPavel Emelyanov 	t->sysctl_header = register_net_sysctl_table(net, addrconf_ctl_path,
43121dab6222SPavel Emelyanov 			t->addrconf_vars);
43131da177e4SLinus Torvalds 	if (t->sysctl_header == NULL)
43141da177e4SLinus Torvalds 		goto free_procname;
4315f68635e6SPavel Emelyanov 
43161da177e4SLinus Torvalds 	p->sysctl = t;
431795897312SPavel Emelyanov 	return 0;
43181da177e4SLinus Torvalds 
43191da177e4SLinus Torvalds free_procname:
43201dab6222SPavel Emelyanov 	kfree(t->dev_name);
43211da177e4SLinus Torvalds free:
43221da177e4SLinus Torvalds 	kfree(t);
4323f68635e6SPavel Emelyanov out:
432495897312SPavel Emelyanov 	return -ENOBUFS;
43251da177e4SLinus Torvalds }
43261da177e4SLinus Torvalds 
4327408c4768SPavel Emelyanov static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
4328f52295a9SPavel Emelyanov {
4329408c4768SPavel Emelyanov 	struct addrconf_sysctl_table *t;
4330f52295a9SPavel Emelyanov 
4331408c4768SPavel Emelyanov 	if (p->sysctl == NULL)
4332408c4768SPavel Emelyanov 		return;
4333408c4768SPavel Emelyanov 
4334408c4768SPavel Emelyanov 	t = p->sysctl;
43351da177e4SLinus Torvalds 	p->sysctl = NULL;
43361da177e4SLinus Torvalds 	unregister_sysctl_table(t->sysctl_header);
43371dab6222SPavel Emelyanov 	kfree(t->dev_name);
43381da177e4SLinus Torvalds 	kfree(t);
43391da177e4SLinus Torvalds }
4340408c4768SPavel Emelyanov 
4341408c4768SPavel Emelyanov static void addrconf_sysctl_register(struct inet6_dev *idev)
4342408c4768SPavel Emelyanov {
4343408c4768SPavel Emelyanov 	neigh_sysctl_register(idev->dev, idev->nd_parms, NET_IPV6,
4344408c4768SPavel Emelyanov 			      NET_IPV6_NEIGH, "ipv6",
4345408c4768SPavel Emelyanov 			      &ndisc_ifinfo_sysctl_change,
43460686caa3SYOSHIFUJI Hideaki 			      ndisc_ifinfo_sysctl_strategy);
4347c346dca1SYOSHIFUJI Hideaki 	__addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
4348bff16c2fSPavel Emelyanov 			idev->dev->ifindex, idev, &idev->cnf);
4349408c4768SPavel Emelyanov }
4350408c4768SPavel Emelyanov 
4351408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev)
4352408c4768SPavel Emelyanov {
4353408c4768SPavel Emelyanov 	__addrconf_sysctl_unregister(&idev->cnf);
4354408c4768SPavel Emelyanov 	neigh_sysctl_unregister(idev->nd_parms);
43551da177e4SLinus Torvalds }
43561da177e4SLinus Torvalds 
43571da177e4SLinus Torvalds 
43581da177e4SLinus Torvalds #endif
43591da177e4SLinus Torvalds 
4360e0da5a48SPavel Emelyanov static int addrconf_init_net(struct net *net)
4361e0da5a48SPavel Emelyanov {
4362e0da5a48SPavel Emelyanov 	int err;
4363e0da5a48SPavel Emelyanov 	struct ipv6_devconf *all, *dflt;
4364e0da5a48SPavel Emelyanov 
4365e0da5a48SPavel Emelyanov 	err = -ENOMEM;
4366e0da5a48SPavel Emelyanov 	all = &ipv6_devconf;
4367e0da5a48SPavel Emelyanov 	dflt = &ipv6_devconf_dflt;
4368e0da5a48SPavel Emelyanov 
4369e0da5a48SPavel Emelyanov 	if (net != &init_net) {
4370e0da5a48SPavel Emelyanov 		all = kmemdup(all, sizeof(ipv6_devconf), GFP_KERNEL);
4371e0da5a48SPavel Emelyanov 		if (all == NULL)
4372e0da5a48SPavel Emelyanov 			goto err_alloc_all;
4373e0da5a48SPavel Emelyanov 
4374e0da5a48SPavel Emelyanov 		dflt = kmemdup(dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
4375e0da5a48SPavel Emelyanov 		if (dflt == NULL)
4376e0da5a48SPavel Emelyanov 			goto err_alloc_dflt;
4377e0da5a48SPavel Emelyanov 	}
4378e0da5a48SPavel Emelyanov 
4379e0da5a48SPavel Emelyanov 	net->ipv6.devconf_all = all;
4380e0da5a48SPavel Emelyanov 	net->ipv6.devconf_dflt = dflt;
4381e0da5a48SPavel Emelyanov 
4382e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
4383e0da5a48SPavel Emelyanov 	err = __addrconf_sysctl_register(net, "all", NET_PROTO_CONF_ALL,
4384e0da5a48SPavel Emelyanov 			NULL, all);
4385e0da5a48SPavel Emelyanov 	if (err < 0)
4386e0da5a48SPavel Emelyanov 		goto err_reg_all;
4387e0da5a48SPavel Emelyanov 
4388e0da5a48SPavel Emelyanov 	err = __addrconf_sysctl_register(net, "default", NET_PROTO_CONF_DEFAULT,
4389e0da5a48SPavel Emelyanov 			NULL, dflt);
4390e0da5a48SPavel Emelyanov 	if (err < 0)
4391e0da5a48SPavel Emelyanov 		goto err_reg_dflt;
4392e0da5a48SPavel Emelyanov #endif
4393e0da5a48SPavel Emelyanov 	return 0;
4394e0da5a48SPavel Emelyanov 
4395e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
4396e0da5a48SPavel Emelyanov err_reg_dflt:
4397e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(all);
4398e0da5a48SPavel Emelyanov err_reg_all:
4399e0da5a48SPavel Emelyanov 	kfree(dflt);
4400e0da5a48SPavel Emelyanov #endif
4401e0da5a48SPavel Emelyanov err_alloc_dflt:
4402e0da5a48SPavel Emelyanov 	kfree(all);
4403e0da5a48SPavel Emelyanov err_alloc_all:
4404e0da5a48SPavel Emelyanov 	return err;
4405e0da5a48SPavel Emelyanov }
4406e0da5a48SPavel Emelyanov 
4407e0da5a48SPavel Emelyanov static void addrconf_exit_net(struct net *net)
4408e0da5a48SPavel Emelyanov {
4409e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
4410e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
4411e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(net->ipv6.devconf_all);
4412e0da5a48SPavel Emelyanov #endif
4413e0da5a48SPavel Emelyanov 	if (net != &init_net) {
4414e0da5a48SPavel Emelyanov 		kfree(net->ipv6.devconf_dflt);
4415e0da5a48SPavel Emelyanov 		kfree(net->ipv6.devconf_all);
4416e0da5a48SPavel Emelyanov 	}
4417e0da5a48SPavel Emelyanov }
4418e0da5a48SPavel Emelyanov 
4419e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = {
4420e0da5a48SPavel Emelyanov 	.init = addrconf_init_net,
4421e0da5a48SPavel Emelyanov 	.exit = addrconf_exit_net,
4422e0da5a48SPavel Emelyanov };
4423e0da5a48SPavel Emelyanov 
44241da177e4SLinus Torvalds /*
44251da177e4SLinus Torvalds  *      Device notifier
44261da177e4SLinus Torvalds  */
44271da177e4SLinus Torvalds 
44281da177e4SLinus Torvalds int register_inet6addr_notifier(struct notifier_block *nb)
44291da177e4SLinus Torvalds {
4430e041c683SAlan Stern 	return atomic_notifier_chain_register(&inet6addr_chain, nb);
44311da177e4SLinus Torvalds }
44321da177e4SLinus Torvalds 
44337159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(register_inet6addr_notifier);
44347159039aSYOSHIFUJI Hideaki 
44351da177e4SLinus Torvalds int unregister_inet6addr_notifier(struct notifier_block *nb)
44361da177e4SLinus Torvalds {
4437e041c683SAlan Stern 	return atomic_notifier_chain_unregister(&inet6addr_chain,nb);
44381da177e4SLinus Torvalds }
44391da177e4SLinus Torvalds 
44407159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(unregister_inet6addr_notifier);
44417159039aSYOSHIFUJI Hideaki 
4442af284937SDaniel Lezcano static void addrconf_net_exit(struct net *net)
4443af284937SDaniel Lezcano {
44446fda7350SBenjamin Thery 	struct net_device *dev;
44456fda7350SBenjamin Thery 
44466fda7350SBenjamin Thery 	rtnl_lock();
44476fda7350SBenjamin Thery 	/* clean dev list */
44486fda7350SBenjamin Thery 	for_each_netdev(net, dev) {
44496fda7350SBenjamin Thery 		if (__in6_dev_get(dev) == NULL)
44506fda7350SBenjamin Thery 			continue;
44516fda7350SBenjamin Thery 		addrconf_ifdown(dev, 1);
44526fda7350SBenjamin Thery 	}
44536fda7350SBenjamin Thery 	addrconf_ifdown(net->loopback_dev, 2);
44546fda7350SBenjamin Thery 	rtnl_unlock();
4455af284937SDaniel Lezcano }
4456af284937SDaniel Lezcano 
4457af284937SDaniel Lezcano static struct pernet_operations addrconf_net_ops = {
4458af284937SDaniel Lezcano 	.exit = addrconf_net_exit,
4459af284937SDaniel Lezcano };
4460af284937SDaniel Lezcano 
44611da177e4SLinus Torvalds /*
44621da177e4SLinus Torvalds  *	Init / cleanup code
44631da177e4SLinus Torvalds  */
44641da177e4SLinus Torvalds 
44651da177e4SLinus Torvalds int __init addrconf_init(void)
44661da177e4SLinus Torvalds {
44672a8cc6c8SYOSHIFUJI Hideaki 	int err;
44682a8cc6c8SYOSHIFUJI Hideaki 
44692a8cc6c8SYOSHIFUJI Hideaki 	if ((err = ipv6_addr_label_init()) < 0) {
44702a8cc6c8SYOSHIFUJI Hideaki 		printk(KERN_CRIT "IPv6 Addrconf: cannot initialize default policy table: %d.\n",
44712a8cc6c8SYOSHIFUJI Hideaki 			err);
44722a8cc6c8SYOSHIFUJI Hideaki 		return err;
44732a8cc6c8SYOSHIFUJI Hideaki 	}
44741da177e4SLinus Torvalds 
4475e0da5a48SPavel Emelyanov 	register_pernet_subsys(&addrconf_ops);
4476e0da5a48SPavel Emelyanov 
44771da177e4SLinus Torvalds 	/* The addrconf netdev notifier requires that loopback_dev
44781da177e4SLinus Torvalds 	 * has it's ipv6 private information allocated and setup
44791da177e4SLinus Torvalds 	 * before it can bring up and give link-local addresses
44801da177e4SLinus Torvalds 	 * to other devices which are up.
44811da177e4SLinus Torvalds 	 *
44821da177e4SLinus Torvalds 	 * Unfortunately, loopback_dev is not necessarily the first
44831da177e4SLinus Torvalds 	 * entry in the global dev_base list of net devices.  In fact,
44841da177e4SLinus Torvalds 	 * it is likely to be the very last entry on that list.
44851da177e4SLinus Torvalds 	 * So this causes the notifier registry below to try and
44861da177e4SLinus Torvalds 	 * give link-local addresses to all devices besides loopback_dev
44871da177e4SLinus Torvalds 	 * first, then loopback_dev, which cases all the non-loopback_dev
44881da177e4SLinus Torvalds 	 * devices to fail to get a link-local address.
44891da177e4SLinus Torvalds 	 *
44901da177e4SLinus Torvalds 	 * So, as a temporary fix, allocate the ipv6 structure for
44911da177e4SLinus Torvalds 	 * loopback_dev first by hand.
44921da177e4SLinus Torvalds 	 * Longer term, all of the dependencies ipv6 has upon the loopback
44931da177e4SLinus Torvalds 	 * device and it being up should be removed.
44941da177e4SLinus Torvalds 	 */
44951da177e4SLinus Torvalds 	rtnl_lock();
44962774c7abSEric W. Biederman 	if (!ipv6_add_dev(init_net.loopback_dev))
44971da177e4SLinus Torvalds 		err = -ENOMEM;
44981da177e4SLinus Torvalds 	rtnl_unlock();
44991da177e4SLinus Torvalds 	if (err)
4500e0da5a48SPavel Emelyanov 		goto errlo;
45011da177e4SLinus Torvalds 
4502af284937SDaniel Lezcano 	err = register_pernet_device(&addrconf_net_ops);
4503af284937SDaniel Lezcano 	if (err)
4504af284937SDaniel Lezcano 		return err;
4505af284937SDaniel Lezcano 
45061da177e4SLinus Torvalds 	register_netdevice_notifier(&ipv6_dev_notf);
45071da177e4SLinus Torvalds 
45081da177e4SLinus Torvalds 	addrconf_verify(0);
4509c127ea2cSThomas Graf 
4510c127ea2cSThomas Graf 	err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo);
4511c127ea2cSThomas Graf 	if (err < 0)
4512c127ea2cSThomas Graf 		goto errout;
4513c127ea2cSThomas Graf 
4514c127ea2cSThomas Graf 	/* Only the first call to __rtnl_register can fail */
4515c127ea2cSThomas Graf 	__rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL);
4516c127ea2cSThomas Graf 	__rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL);
4517c127ea2cSThomas Graf 	__rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, inet6_dump_ifaddr);
4518c127ea2cSThomas Graf 	__rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, inet6_dump_ifmcaddr);
4519c127ea2cSThomas Graf 	__rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, inet6_dump_ifacaddr);
4520c127ea2cSThomas Graf 
45212a8cc6c8SYOSHIFUJI Hideaki 	ipv6_addr_label_rtnl_register();
45222a8cc6c8SYOSHIFUJI Hideaki 
45231da177e4SLinus Torvalds 	return 0;
4524c127ea2cSThomas Graf errout:
4525c127ea2cSThomas Graf 	unregister_netdevice_notifier(&ipv6_dev_notf);
4526e0da5a48SPavel Emelyanov errlo:
4527e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
4528c127ea2cSThomas Graf 
4529c127ea2cSThomas Graf 	return err;
45301da177e4SLinus Torvalds }
45311da177e4SLinus Torvalds 
453209f7709fSDaniel Lezcano void addrconf_cleanup(void)
45331da177e4SLinus Torvalds {
45341da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
45351da177e4SLinus Torvalds 	int i;
45361da177e4SLinus Torvalds 
45371da177e4SLinus Torvalds 	unregister_netdevice_notifier(&ipv6_dev_notf);
4538af284937SDaniel Lezcano 	unregister_pernet_device(&addrconf_net_ops);
45391da177e4SLinus Torvalds 
4540e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
45411da177e4SLinus Torvalds 
45421da177e4SLinus Torvalds 	rtnl_lock();
45431da177e4SLinus Torvalds 
45441da177e4SLinus Torvalds 	/*
45451da177e4SLinus Torvalds 	 *	Check hash table.
45461da177e4SLinus Torvalds 	 */
45471da177e4SLinus Torvalds 	write_lock_bh(&addrconf_hash_lock);
45481da177e4SLinus Torvalds 	for (i=0; i < IN6_ADDR_HSIZE; i++) {
45491da177e4SLinus Torvalds 		for (ifa=inet6_addr_lst[i]; ifa; ) {
45501da177e4SLinus Torvalds 			struct inet6_ifaddr *bifa;
45511da177e4SLinus Torvalds 
45521da177e4SLinus Torvalds 			bifa = ifa;
45531da177e4SLinus Torvalds 			ifa = ifa->lst_next;
45541da177e4SLinus Torvalds 			printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
45551da177e4SLinus Torvalds 			/* Do not free it; something is wrong.
45561da177e4SLinus Torvalds 			   Now we can investigate it with debugger.
45571da177e4SLinus Torvalds 			 */
45581da177e4SLinus Torvalds 		}
45591da177e4SLinus Torvalds 	}
45601da177e4SLinus Torvalds 	write_unlock_bh(&addrconf_hash_lock);
45611da177e4SLinus Torvalds 
45621da177e4SLinus Torvalds 	del_timer(&addr_chk_timer);
45631da177e4SLinus Torvalds 	rtnl_unlock();
4564af284937SDaniel Lezcano 
4565af284937SDaniel Lezcano 	unregister_pernet_subsys(&addrconf_net_ops);
45661da177e4SLinus Torvalds }
4567