xref: /openbmc/linux/net/ipv6/addrconf.c (revision 1ce85fe402137824246bad03ff85f3913d565c17)
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) {
20345bc3eb7eSStephen Hemminger 		const struct net_device_ops *ops = dev->netdev_ops;
20351da177e4SLinus Torvalds 		struct ifreq ifr;
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 
20515bc3eb7eSStephen Hemminger 		if (ops->ndo_do_ioctl) {
20525bc3eb7eSStephen Hemminger 			mm_segment_t oldfs = get_fs();
20535bc3eb7eSStephen Hemminger 
20545bc3eb7eSStephen Hemminger 			set_fs(KERNEL_DS);
20555bc3eb7eSStephen Hemminger 			err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
20561da177e4SLinus Torvalds 			set_fs(oldfs);
20575bc3eb7eSStephen Hemminger 		} else
20585bc3eb7eSStephen Hemminger 			err = -EOPNOTSUPP;
20591da177e4SLinus Torvalds 
20601da177e4SLinus Torvalds 		if (err == 0) {
20611da177e4SLinus Torvalds 			err = -ENOBUFS;
2062af284937SDaniel Lezcano 			dev = __dev_get_by_name(net, p.name);
2063af284937SDaniel Lezcano 			if (!dev)
20641da177e4SLinus Torvalds 				goto err_exit;
20651da177e4SLinus Torvalds 			err = dev_open(dev);
20661da177e4SLinus Torvalds 		}
20671da177e4SLinus Torvalds 	}
20680be669bbSJoerg Roedel #endif
20691da177e4SLinus Torvalds 
20701da177e4SLinus Torvalds err_exit:
20711da177e4SLinus Torvalds 	rtnl_unlock();
20721da177e4SLinus Torvalds 	return err;
20731da177e4SLinus Torvalds }
20741da177e4SLinus Torvalds 
20751da177e4SLinus Torvalds /*
20761da177e4SLinus Torvalds  *	Manual configuration of address on an interface
20771da177e4SLinus Torvalds  */
2078af284937SDaniel Lezcano static int inet6_addr_add(struct net *net, int ifindex, struct in6_addr *pfx,
207924ef0da7SThomas Graf 			  unsigned int plen, __u8 ifa_flags, __u32 prefered_lft,
2080af284937SDaniel Lezcano 			  __u32 valid_lft)
20811da177e4SLinus Torvalds {
20821da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
20831da177e4SLinus Torvalds 	struct inet6_dev *idev;
20841da177e4SLinus Torvalds 	struct net_device *dev;
20851da177e4SLinus Torvalds 	int scope;
20866f704992SYOSHIFUJI Hideaki 	u32 flags;
20876f704992SYOSHIFUJI Hideaki 	clock_t expires;
20884bed72e4SYOSHIFUJI Hideaki 	unsigned long timeout;
20891da177e4SLinus Torvalds 
20901da177e4SLinus Torvalds 	ASSERT_RTNL();
20911da177e4SLinus Torvalds 
209224ef0da7SThomas Graf 	if (plen > 128)
209324ef0da7SThomas Graf 		return -EINVAL;
209424ef0da7SThomas Graf 
20950778769dSNoriaki TAKAMIYA 	/* check the lifetime */
20960778769dSNoriaki TAKAMIYA 	if (!valid_lft || prefered_lft > valid_lft)
20970778769dSNoriaki TAKAMIYA 		return -EINVAL;
20980778769dSNoriaki TAKAMIYA 
2099af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2100af284937SDaniel Lezcano 	if (!dev)
21011da177e4SLinus Torvalds 		return -ENODEV;
21021da177e4SLinus Torvalds 
21031da177e4SLinus Torvalds 	if ((idev = addrconf_add_dev(dev)) == NULL)
21041da177e4SLinus Torvalds 		return -ENOBUFS;
21051da177e4SLinus Torvalds 
21061da177e4SLinus Torvalds 	scope = ipv6_addr_scope(pfx);
21071da177e4SLinus Torvalds 
21084bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
21094bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
21104bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
21114bed72e4SYOSHIFUJI Hideaki 		valid_lft = timeout;
21126f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
21134bed72e4SYOSHIFUJI Hideaki 	} else {
21144bed72e4SYOSHIFUJI Hideaki 		expires = 0;
21154bed72e4SYOSHIFUJI Hideaki 		flags = 0;
21164bed72e4SYOSHIFUJI Hideaki 		ifa_flags |= IFA_F_PERMANENT;
21176f704992SYOSHIFUJI Hideaki 	}
21180778769dSNoriaki TAKAMIYA 
21194bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
21204bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
21214bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
21220778769dSNoriaki TAKAMIYA 			ifa_flags |= IFA_F_DEPRECATED;
21234bed72e4SYOSHIFUJI Hideaki 		prefered_lft = timeout;
21244bed72e4SYOSHIFUJI Hideaki 	}
21250778769dSNoriaki TAKAMIYA 
21260778769dSNoriaki TAKAMIYA 	ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
21270778769dSNoriaki TAKAMIYA 
21281da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
212906aebfb7SHerbert Xu 		spin_lock_bh(&ifp->lock);
21300778769dSNoriaki TAKAMIYA 		ifp->valid_lft = valid_lft;
21310778769dSNoriaki TAKAMIYA 		ifp->prefered_lft = prefered_lft;
21320778769dSNoriaki TAKAMIYA 		ifp->tstamp = jiffies;
213306aebfb7SHerbert Xu 		spin_unlock_bh(&ifp->lock);
21340778769dSNoriaki TAKAMIYA 
213546d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
21366f704992SYOSHIFUJI Hideaki 				      expires, flags);
213795c385b4SNeil Horman 		/*
213895c385b4SNeil Horman 		 * Note that section 3.1 of RFC 4429 indicates
213995c385b4SNeil Horman 		 * that the Optimistic flag should not be set for
214095c385b4SNeil Horman 		 * manually configured addresses
214195c385b4SNeil Horman 		 */
21421da177e4SLinus Torvalds 		addrconf_dad_start(ifp, 0);
21431da177e4SLinus Torvalds 		in6_ifa_put(ifp);
21440778769dSNoriaki TAKAMIYA 		addrconf_verify(0);
21451da177e4SLinus Torvalds 		return 0;
21461da177e4SLinus Torvalds 	}
21471da177e4SLinus Torvalds 
21481da177e4SLinus Torvalds 	return PTR_ERR(ifp);
21491da177e4SLinus Torvalds }
21501da177e4SLinus Torvalds 
2151af284937SDaniel Lezcano static int inet6_addr_del(struct net *net, int ifindex, struct in6_addr *pfx,
215224ef0da7SThomas Graf 			  unsigned int plen)
21531da177e4SLinus Torvalds {
21541da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
21551da177e4SLinus Torvalds 	struct inet6_dev *idev;
21561da177e4SLinus Torvalds 	struct net_device *dev;
21571da177e4SLinus Torvalds 
215824ef0da7SThomas Graf 	if (plen > 128)
215924ef0da7SThomas Graf 		return -EINVAL;
216024ef0da7SThomas Graf 
2161af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2162af284937SDaniel Lezcano 	if (!dev)
21631da177e4SLinus Torvalds 		return -ENODEV;
21641da177e4SLinus Torvalds 
21651da177e4SLinus Torvalds 	if ((idev = __in6_dev_get(dev)) == NULL)
21661da177e4SLinus Torvalds 		return -ENXIO;
21671da177e4SLinus Torvalds 
21681da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
21691da177e4SLinus Torvalds 	for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
21701da177e4SLinus Torvalds 		if (ifp->prefix_len == plen &&
21711da177e4SLinus Torvalds 		    ipv6_addr_equal(pfx, &ifp->addr)) {
21721da177e4SLinus Torvalds 			in6_ifa_hold(ifp);
21731da177e4SLinus Torvalds 			read_unlock_bh(&idev->lock);
21741da177e4SLinus Torvalds 
21751da177e4SLinus Torvalds 			ipv6_del_addr(ifp);
21761da177e4SLinus Torvalds 
21771da177e4SLinus Torvalds 			/* If the last address is deleted administratively,
21781da177e4SLinus Torvalds 			   disable IPv6 on this interface.
21791da177e4SLinus Torvalds 			 */
21801da177e4SLinus Torvalds 			if (idev->addr_list == NULL)
21811da177e4SLinus Torvalds 				addrconf_ifdown(idev->dev, 1);
21821da177e4SLinus Torvalds 			return 0;
21831da177e4SLinus Torvalds 		}
21841da177e4SLinus Torvalds 	}
21851da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
21861da177e4SLinus Torvalds 	return -EADDRNOTAVAIL;
21871da177e4SLinus Torvalds }
21881da177e4SLinus Torvalds 
21891da177e4SLinus Torvalds 
2190af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg)
21911da177e4SLinus Torvalds {
21921da177e4SLinus Torvalds 	struct in6_ifreq ireq;
21931da177e4SLinus Torvalds 	int err;
21941da177e4SLinus Torvalds 
21951da177e4SLinus Torvalds 	if (!capable(CAP_NET_ADMIN))
21961da177e4SLinus Torvalds 		return -EPERM;
21971da177e4SLinus Torvalds 
21981da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
21991da177e4SLinus Torvalds 		return -EFAULT;
22001da177e4SLinus Torvalds 
22011da177e4SLinus Torvalds 	rtnl_lock();
2202af284937SDaniel Lezcano 	err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2203af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2204af284937SDaniel Lezcano 			     INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
22051da177e4SLinus Torvalds 	rtnl_unlock();
22061da177e4SLinus Torvalds 	return err;
22071da177e4SLinus Torvalds }
22081da177e4SLinus Torvalds 
2209af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg)
22101da177e4SLinus Torvalds {
22111da177e4SLinus Torvalds 	struct in6_ifreq ireq;
22121da177e4SLinus Torvalds 	int err;
22131da177e4SLinus Torvalds 
22141da177e4SLinus Torvalds 	if (!capable(CAP_NET_ADMIN))
22151da177e4SLinus Torvalds 		return -EPERM;
22161da177e4SLinus Torvalds 
22171da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
22181da177e4SLinus Torvalds 		return -EFAULT;
22191da177e4SLinus Torvalds 
22201da177e4SLinus Torvalds 	rtnl_lock();
2221af284937SDaniel Lezcano 	err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2222af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen);
22231da177e4SLinus Torvalds 	rtnl_unlock();
22241da177e4SLinus Torvalds 	return err;
22251da177e4SLinus Torvalds }
22261da177e4SLinus Torvalds 
2227b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2228b5f348e5SIlpo Järvinen 		     int plen, int scope)
2229b5f348e5SIlpo Järvinen {
2230b5f348e5SIlpo Järvinen 	struct inet6_ifaddr *ifp;
2231b5f348e5SIlpo Järvinen 
2232b5f348e5SIlpo Järvinen 	ifp = ipv6_add_addr(idev, addr, plen, scope, IFA_F_PERMANENT);
2233b5f348e5SIlpo Järvinen 	if (!IS_ERR(ifp)) {
2234b5f348e5SIlpo Järvinen 		spin_lock_bh(&ifp->lock);
2235b5f348e5SIlpo Järvinen 		ifp->flags &= ~IFA_F_TENTATIVE;
2236b5f348e5SIlpo Järvinen 		spin_unlock_bh(&ifp->lock);
2237b5f348e5SIlpo Järvinen 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
2238b5f348e5SIlpo Järvinen 		in6_ifa_put(ifp);
2239b5f348e5SIlpo Järvinen 	}
2240b5f348e5SIlpo Järvinen }
2241b5f348e5SIlpo Järvinen 
22420be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
22431da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev)
22441da177e4SLinus Torvalds {
22451da177e4SLinus Torvalds 	struct in6_addr addr;
22461da177e4SLinus Torvalds 	struct net_device *dev;
2247c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
22481da177e4SLinus Torvalds 	int scope;
22491da177e4SLinus Torvalds 
22501da177e4SLinus Torvalds 	ASSERT_RTNL();
22511da177e4SLinus Torvalds 
22521da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
22531da177e4SLinus Torvalds 	memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
22541da177e4SLinus Torvalds 
22551da177e4SLinus Torvalds 	if (idev->dev->flags&IFF_POINTOPOINT) {
22561da177e4SLinus Torvalds 		addr.s6_addr32[0] = htonl(0xfe800000);
22571da177e4SLinus Torvalds 		scope = IFA_LINK;
22581da177e4SLinus Torvalds 	} else {
22591da177e4SLinus Torvalds 		scope = IPV6_ADDR_COMPATv4;
22601da177e4SLinus Torvalds 	}
22611da177e4SLinus Torvalds 
22621da177e4SLinus Torvalds 	if (addr.s6_addr32[3]) {
2263b5f348e5SIlpo Järvinen 		add_addr(idev, &addr, 128, scope);
22641da177e4SLinus Torvalds 		return;
22651da177e4SLinus Torvalds 	}
22661da177e4SLinus Torvalds 
22676fda7350SBenjamin Thery 	for_each_netdev(net, dev) {
2268e5ed6399SHerbert Xu 		struct in_device * in_dev = __in_dev_get_rtnl(dev);
22691da177e4SLinus Torvalds 		if (in_dev && (dev->flags & IFF_UP)) {
22701da177e4SLinus Torvalds 			struct in_ifaddr * ifa;
22711da177e4SLinus Torvalds 
22721da177e4SLinus Torvalds 			int flag = scope;
22731da177e4SLinus Torvalds 
22741da177e4SLinus Torvalds 			for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
22751da177e4SLinus Torvalds 				int plen;
22761da177e4SLinus Torvalds 
22771da177e4SLinus Torvalds 				addr.s6_addr32[3] = ifa->ifa_local;
22781da177e4SLinus Torvalds 
22791da177e4SLinus Torvalds 				if (ifa->ifa_scope == RT_SCOPE_LINK)
22801da177e4SLinus Torvalds 					continue;
22811da177e4SLinus Torvalds 				if (ifa->ifa_scope >= RT_SCOPE_HOST) {
22821da177e4SLinus Torvalds 					if (idev->dev->flags&IFF_POINTOPOINT)
22831da177e4SLinus Torvalds 						continue;
22841da177e4SLinus Torvalds 					flag |= IFA_HOST;
22851da177e4SLinus Torvalds 				}
22861da177e4SLinus Torvalds 				if (idev->dev->flags&IFF_POINTOPOINT)
22871da177e4SLinus Torvalds 					plen = 64;
22881da177e4SLinus Torvalds 				else
22891da177e4SLinus Torvalds 					plen = 96;
22901da177e4SLinus Torvalds 
2291b5f348e5SIlpo Järvinen 				add_addr(idev, &addr, plen, flag);
22921da177e4SLinus Torvalds 			}
22931da177e4SLinus Torvalds 		}
22941da177e4SLinus Torvalds 	}
22951da177e4SLinus Torvalds }
22960be669bbSJoerg Roedel #endif
22971da177e4SLinus Torvalds 
22981da177e4SLinus Torvalds static void init_loopback(struct net_device *dev)
22991da177e4SLinus Torvalds {
23001da177e4SLinus Torvalds 	struct inet6_dev  *idev;
23011da177e4SLinus Torvalds 
23021da177e4SLinus Torvalds 	/* ::1 */
23031da177e4SLinus Torvalds 
23041da177e4SLinus Torvalds 	ASSERT_RTNL();
23051da177e4SLinus Torvalds 
23061da177e4SLinus Torvalds 	if ((idev = ipv6_find_idev(dev)) == NULL) {
23071da177e4SLinus Torvalds 		printk(KERN_DEBUG "init loopback: add_dev failed\n");
23081da177e4SLinus Torvalds 		return;
23091da177e4SLinus Torvalds 	}
23101da177e4SLinus Torvalds 
2311b5f348e5SIlpo Järvinen 	add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
23121da177e4SLinus Torvalds }
23131da177e4SLinus Torvalds 
23141da177e4SLinus Torvalds static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
23151da177e4SLinus Torvalds {
23161da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
231795c385b4SNeil Horman 	u32 addr_flags = IFA_F_PERMANENT;
23181da177e4SLinus Torvalds 
231995c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
232095c385b4SNeil Horman 	if (idev->cnf.optimistic_dad &&
2321702beb87SDavid Miller 	    !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
232295c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
232395c385b4SNeil Horman #endif
232495c385b4SNeil Horman 
232595c385b4SNeil Horman 
232695c385b4SNeil Horman 	ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags);
23271da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
232846d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
23291da177e4SLinus Torvalds 		addrconf_dad_start(ifp, 0);
23301da177e4SLinus Torvalds 		in6_ifa_put(ifp);
23311da177e4SLinus Torvalds 	}
23321da177e4SLinus Torvalds }
23331da177e4SLinus Torvalds 
23341da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev)
23351da177e4SLinus Torvalds {
23361da177e4SLinus Torvalds 	struct in6_addr addr;
23371da177e4SLinus Torvalds 	struct inet6_dev    * idev;
23381da177e4SLinus Torvalds 
23391da177e4SLinus Torvalds 	ASSERT_RTNL();
23401da177e4SLinus Torvalds 
234174235a25SHerbert Xu 	if ((dev->type != ARPHRD_ETHER) &&
234274235a25SHerbert Xu 	    (dev->type != ARPHRD_FDDI) &&
234374235a25SHerbert Xu 	    (dev->type != ARPHRD_IEEE802_TR) &&
234474235a25SHerbert Xu 	    (dev->type != ARPHRD_ARCNET) &&
234574235a25SHerbert Xu 	    (dev->type != ARPHRD_INFINIBAND)) {
234674235a25SHerbert Xu 		/* Alas, we support only Ethernet autoconfiguration. */
234774235a25SHerbert Xu 		return;
234874235a25SHerbert Xu 	}
234974235a25SHerbert Xu 
23501da177e4SLinus Torvalds 	idev = addrconf_add_dev(dev);
23511da177e4SLinus Torvalds 	if (idev == NULL)
23521da177e4SLinus Torvalds 		return;
23531da177e4SLinus Torvalds 
23541da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
23551da177e4SLinus Torvalds 	addr.s6_addr32[0] = htonl(0xFE800000);
23561da177e4SLinus Torvalds 
23571da177e4SLinus Torvalds 	if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
23581da177e4SLinus Torvalds 		addrconf_add_linklocal(idev, &addr);
23591da177e4SLinus Torvalds }
23601da177e4SLinus Torvalds 
23610be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
23621da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev)
23631da177e4SLinus Torvalds {
23641da177e4SLinus Torvalds 	struct inet6_dev *idev;
23651da177e4SLinus Torvalds 
23661da177e4SLinus Torvalds 	ASSERT_RTNL();
23671da177e4SLinus Torvalds 
23681da177e4SLinus Torvalds 	/*
23691da177e4SLinus Torvalds 	 * Configure the tunnel with one of our IPv4
23701da177e4SLinus Torvalds 	 * addresses... we should configure all of
23711da177e4SLinus Torvalds 	 * our v4 addrs in the tunnel
23721da177e4SLinus Torvalds 	 */
23731da177e4SLinus Torvalds 
23741da177e4SLinus Torvalds 	if ((idev = ipv6_find_idev(dev)) == NULL) {
23751da177e4SLinus Torvalds 		printk(KERN_DEBUG "init sit: add_dev failed\n");
23761da177e4SLinus Torvalds 		return;
23771da177e4SLinus Torvalds 	}
23781da177e4SLinus Torvalds 
2379c7dc89c0SFred L. Templin 	if (dev->priv_flags & IFF_ISATAP) {
2380c7dc89c0SFred L. Templin 		struct in6_addr addr;
2381c7dc89c0SFred L. Templin 
2382c7dc89c0SFred L. Templin 		ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
2383c7dc89c0SFred L. Templin 		addrconf_prefix_route(&addr, 64, dev, 0, 0);
2384c7dc89c0SFred L. Templin 		if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2385c7dc89c0SFred L. Templin 			addrconf_add_linklocal(idev, &addr);
2386c7dc89c0SFred L. Templin 		return;
2387c7dc89c0SFred L. Templin 	}
2388c7dc89c0SFred L. Templin 
23891da177e4SLinus Torvalds 	sit_add_v4_addrs(idev);
23901da177e4SLinus Torvalds 
23911da177e4SLinus Torvalds 	if (dev->flags&IFF_POINTOPOINT) {
23921da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
23931da177e4SLinus Torvalds 		addrconf_add_lroute(dev);
23941da177e4SLinus Torvalds 	} else
23951da177e4SLinus Torvalds 		sit_route_add(dev);
23961da177e4SLinus Torvalds }
23970be669bbSJoerg Roedel #endif
23981da177e4SLinus Torvalds 
23991da177e4SLinus Torvalds static inline int
24001da177e4SLinus Torvalds ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
24011da177e4SLinus Torvalds {
24021da177e4SLinus Torvalds 	struct in6_addr lladdr;
24031da177e4SLinus Torvalds 
240495c385b4SNeil Horman 	if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
24051da177e4SLinus Torvalds 		addrconf_add_linklocal(idev, &lladdr);
24061da177e4SLinus Torvalds 		return 0;
24071da177e4SLinus Torvalds 	}
24081da177e4SLinus Torvalds 	return -1;
24091da177e4SLinus Torvalds }
24101da177e4SLinus Torvalds 
24111da177e4SLinus Torvalds static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
24121da177e4SLinus Torvalds {
24131da177e4SLinus Torvalds 	struct net_device *link_dev;
2414c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
24151da177e4SLinus Torvalds 
24161da177e4SLinus Torvalds 	/* first try to inherit the link-local address from the link device */
24171da177e4SLinus Torvalds 	if (idev->dev->iflink &&
24186fda7350SBenjamin Thery 	    (link_dev = __dev_get_by_index(net, idev->dev->iflink))) {
24191da177e4SLinus Torvalds 		if (!ipv6_inherit_linklocal(idev, link_dev))
24201da177e4SLinus Torvalds 			return;
24211da177e4SLinus Torvalds 	}
24221da177e4SLinus Torvalds 	/* then try to inherit it from any device */
24236fda7350SBenjamin Thery 	for_each_netdev(net, link_dev) {
24241da177e4SLinus Torvalds 		if (!ipv6_inherit_linklocal(idev, link_dev))
24251da177e4SLinus Torvalds 			return;
24261da177e4SLinus Torvalds 	}
24271da177e4SLinus Torvalds 	printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
24281da177e4SLinus Torvalds }
24291da177e4SLinus Torvalds 
24301da177e4SLinus Torvalds /*
24311da177e4SLinus Torvalds  * Autoconfigure tunnel with a link-local address so routing protocols,
24321da177e4SLinus Torvalds  * DHCPv6, MLD etc. can be run over the virtual link
24331da177e4SLinus Torvalds  */
24341da177e4SLinus Torvalds 
24351da177e4SLinus Torvalds static void addrconf_ip6_tnl_config(struct net_device *dev)
24361da177e4SLinus Torvalds {
24371da177e4SLinus Torvalds 	struct inet6_dev *idev;
24381da177e4SLinus Torvalds 
24391da177e4SLinus Torvalds 	ASSERT_RTNL();
24401da177e4SLinus Torvalds 
24411da177e4SLinus Torvalds 	if ((idev = addrconf_add_dev(dev)) == NULL) {
24421da177e4SLinus Torvalds 		printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
24431da177e4SLinus Torvalds 		return;
24441da177e4SLinus Torvalds 	}
24451da177e4SLinus Torvalds 	ip6_tnl_add_linklocal(idev);
24461da177e4SLinus Torvalds }
24471da177e4SLinus Torvalds 
24481da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event,
24491da177e4SLinus Torvalds 			   void * data)
24501da177e4SLinus Torvalds {
24511da177e4SLinus Torvalds 	struct net_device *dev = (struct net_device *) data;
245274235a25SHerbert Xu 	struct inet6_dev *idev = __in6_dev_get(dev);
2453c5e33bddSYOSHIFUJI Hideaki 	int run_pending = 0;
2454b217d616SHerbert Xu 	int err;
24551da177e4SLinus Torvalds 
24561da177e4SLinus Torvalds 	switch(event) {
245745ba9dd2SYOSHIFUJI Hideaki 	case NETDEV_REGISTER:
245874235a25SHerbert Xu 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
245945ba9dd2SYOSHIFUJI Hideaki 			idev = ipv6_add_dev(dev);
246045ba9dd2SYOSHIFUJI Hideaki 			if (!idev)
2461b217d616SHerbert Xu 				return notifier_from_errno(-ENOMEM);
246245ba9dd2SYOSHIFUJI Hideaki 		}
246345ba9dd2SYOSHIFUJI Hideaki 		break;
24641da177e4SLinus Torvalds 	case NETDEV_UP:
24653c21edbdSYOSHIFUJI Hideaki 	case NETDEV_CHANGE:
2466c2edacf8SJay Vosburgh 		if (dev->flags & IFF_SLAVE)
2467c2edacf8SJay Vosburgh 			break;
2468c2edacf8SJay Vosburgh 
24693c21edbdSYOSHIFUJI Hideaki 		if (event == NETDEV_UP) {
2470f24e3d65SMitsuru Chinen 			if (!addrconf_qdisc_ok(dev)) {
24713c21edbdSYOSHIFUJI Hideaki 				/* device is not ready yet. */
24723c21edbdSYOSHIFUJI Hideaki 				printk(KERN_INFO
24733c21edbdSYOSHIFUJI Hideaki 					"ADDRCONF(NETDEV_UP): %s: "
24743c21edbdSYOSHIFUJI Hideaki 					"link is not ready\n",
24753c21edbdSYOSHIFUJI Hideaki 					dev->name);
24763c21edbdSYOSHIFUJI Hideaki 				break;
24773c21edbdSYOSHIFUJI Hideaki 			}
247899081049SKristian Slavov 
2479d31c7b8fSEvgeniy Polyakov 			if (!idev && dev->mtu >= IPV6_MIN_MTU)
2480d31c7b8fSEvgeniy Polyakov 				idev = ipv6_add_dev(dev);
2481d31c7b8fSEvgeniy Polyakov 
2482e3ec6cfcSBenjamin Thery 			if (idev) {
248399081049SKristian Slavov 				idev->if_flags |= IF_READY;
2484e3ec6cfcSBenjamin Thery 				run_pending = 1;
2485e3ec6cfcSBenjamin Thery 			}
24863c21edbdSYOSHIFUJI Hideaki 		} else {
2487f24e3d65SMitsuru Chinen 			if (!addrconf_qdisc_ok(dev)) {
24883c21edbdSYOSHIFUJI Hideaki 				/* device is still not ready. */
24893c21edbdSYOSHIFUJI Hideaki 				break;
24903c21edbdSYOSHIFUJI Hideaki 			}
24913c21edbdSYOSHIFUJI Hideaki 
24923c21edbdSYOSHIFUJI Hideaki 			if (idev) {
24933c21edbdSYOSHIFUJI Hideaki 				if (idev->if_flags & IF_READY) {
24943c21edbdSYOSHIFUJI Hideaki 					/* device is already configured. */
24953c21edbdSYOSHIFUJI Hideaki 					break;
24963c21edbdSYOSHIFUJI Hideaki 				}
24973c21edbdSYOSHIFUJI Hideaki 				idev->if_flags |= IF_READY;
24983c21edbdSYOSHIFUJI Hideaki 			}
24993c21edbdSYOSHIFUJI Hideaki 
25003c21edbdSYOSHIFUJI Hideaki 			printk(KERN_INFO
25013c21edbdSYOSHIFUJI Hideaki 					"ADDRCONF(NETDEV_CHANGE): %s: "
25023c21edbdSYOSHIFUJI Hideaki 					"link becomes ready\n",
25033c21edbdSYOSHIFUJI Hideaki 					dev->name);
25043c21edbdSYOSHIFUJI Hideaki 
2505c5e33bddSYOSHIFUJI Hideaki 			run_pending = 1;
25063c21edbdSYOSHIFUJI Hideaki 		}
25073c21edbdSYOSHIFUJI Hideaki 
25081da177e4SLinus Torvalds 		switch(dev->type) {
25090be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
25101da177e4SLinus Torvalds 		case ARPHRD_SIT:
25111da177e4SLinus Torvalds 			addrconf_sit_config(dev);
25121da177e4SLinus Torvalds 			break;
25130be669bbSJoerg Roedel #endif
25141da177e4SLinus Torvalds 		case ARPHRD_TUNNEL6:
25151da177e4SLinus Torvalds 			addrconf_ip6_tnl_config(dev);
25161da177e4SLinus Torvalds 			break;
25171da177e4SLinus Torvalds 		case ARPHRD_LOOPBACK:
25181da177e4SLinus Torvalds 			init_loopback(dev);
25191da177e4SLinus Torvalds 			break;
25201da177e4SLinus Torvalds 
25211da177e4SLinus Torvalds 		default:
25221da177e4SLinus Torvalds 			addrconf_dev_config(dev);
25231da177e4SLinus Torvalds 			break;
25243ff50b79SStephen Hemminger 		}
25251da177e4SLinus Torvalds 		if (idev) {
2526c5e33bddSYOSHIFUJI Hideaki 			if (run_pending)
2527c5e33bddSYOSHIFUJI Hideaki 				addrconf_dad_run(idev);
2528c5e33bddSYOSHIFUJI Hideaki 
25291da177e4SLinus Torvalds 			/* If the MTU changed during the interface down, when the
25301da177e4SLinus Torvalds 			   interface up, the changed MTU must be reflected in the
25311da177e4SLinus Torvalds 			   idev as well as routers.
25321da177e4SLinus Torvalds 			 */
25331da177e4SLinus Torvalds 			if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
25341da177e4SLinus Torvalds 				rt6_mtu_change(dev, dev->mtu);
25351da177e4SLinus Torvalds 				idev->cnf.mtu6 = dev->mtu;
25361da177e4SLinus Torvalds 			}
25371da177e4SLinus Torvalds 			idev->tstamp = jiffies;
25381da177e4SLinus Torvalds 			inet6_ifinfo_notify(RTM_NEWLINK, idev);
25391da177e4SLinus Torvalds 			/* If the changed mtu during down is lower than IPV6_MIN_MTU
25401da177e4SLinus Torvalds 			   stop IPv6 on this interface.
25411da177e4SLinus Torvalds 			 */
25421da177e4SLinus Torvalds 			if (dev->mtu < IPV6_MIN_MTU)
25431da177e4SLinus Torvalds 				addrconf_ifdown(dev, event != NETDEV_DOWN);
25441da177e4SLinus Torvalds 		}
25451da177e4SLinus Torvalds 		break;
25461da177e4SLinus Torvalds 
25471da177e4SLinus Torvalds 	case NETDEV_CHANGEMTU:
25481da177e4SLinus Torvalds 		if (idev && dev->mtu >= IPV6_MIN_MTU) {
25491da177e4SLinus Torvalds 			rt6_mtu_change(dev, dev->mtu);
25501da177e4SLinus Torvalds 			idev->cnf.mtu6 = dev->mtu;
25511da177e4SLinus Torvalds 			break;
25521da177e4SLinus Torvalds 		}
25531da177e4SLinus Torvalds 
2554d31c7b8fSEvgeniy Polyakov 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2555d31c7b8fSEvgeniy Polyakov 			idev = ipv6_add_dev(dev);
2556d31c7b8fSEvgeniy Polyakov 			if (idev)
2557d31c7b8fSEvgeniy Polyakov 				break;
2558d31c7b8fSEvgeniy Polyakov 		}
2559d31c7b8fSEvgeniy Polyakov 
25601da177e4SLinus Torvalds 		/* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
25611da177e4SLinus Torvalds 
25621da177e4SLinus Torvalds 	case NETDEV_DOWN:
25631da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
25641da177e4SLinus Torvalds 		/*
25651da177e4SLinus Torvalds 		 *	Remove all addresses from this interface.
25661da177e4SLinus Torvalds 		 */
25671da177e4SLinus Torvalds 		addrconf_ifdown(dev, event != NETDEV_DOWN);
25681da177e4SLinus Torvalds 		break;
25693c21edbdSYOSHIFUJI Hideaki 
25701da177e4SLinus Torvalds 	case NETDEV_CHANGENAME:
25711da177e4SLinus Torvalds 		if (idev) {
25725632c515SStephen Hemminger 			snmp6_unregister_dev(idev);
2573408c4768SPavel Emelyanov 			addrconf_sysctl_unregister(idev);
2574f52295a9SPavel Emelyanov 			addrconf_sysctl_register(idev);
2575b217d616SHerbert Xu 			err = snmp6_register_dev(idev);
2576b217d616SHerbert Xu 			if (err)
2577b217d616SHerbert Xu 				return notifier_from_errno(err);
25785632c515SStephen Hemminger 		}
25791da177e4SLinus Torvalds 		break;
25803ff50b79SStephen Hemminger 	}
25811da177e4SLinus Torvalds 
25821da177e4SLinus Torvalds 	return NOTIFY_OK;
25831da177e4SLinus Torvalds }
25841da177e4SLinus Torvalds 
25851da177e4SLinus Torvalds /*
25861da177e4SLinus Torvalds  *	addrconf module should be notified of a device going up
25871da177e4SLinus Torvalds  */
25881da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = {
25891da177e4SLinus Torvalds 	.notifier_call = addrconf_notify,
25901da177e4SLinus Torvalds 	.priority = 0
25911da177e4SLinus Torvalds };
25921da177e4SLinus Torvalds 
25931da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how)
25941da177e4SLinus Torvalds {
25951da177e4SLinus Torvalds 	struct inet6_dev *idev;
25961da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa, **bifa;
2597c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
25981da177e4SLinus Torvalds 	int i;
25991da177e4SLinus Torvalds 
26001da177e4SLinus Torvalds 	ASSERT_RTNL();
26011da177e4SLinus Torvalds 
2602eb867579SDenis V. Lunev 	if ((dev->flags & IFF_LOOPBACK) && how == 1)
26031da177e4SLinus Torvalds 		how = 0;
26041da177e4SLinus Torvalds 
2605f3db4851SDaniel Lezcano 	rt6_ifdown(net, dev);
26061da177e4SLinus Torvalds 	neigh_ifdown(&nd_tbl, dev);
26071da177e4SLinus Torvalds 
26081da177e4SLinus Torvalds 	idev = __in6_dev_get(dev);
26091da177e4SLinus Torvalds 	if (idev == NULL)
26101da177e4SLinus Torvalds 		return -ENODEV;
26111da177e4SLinus Torvalds 
26121da177e4SLinus Torvalds 	/* Step 1: remove reference to ipv6 device from parent device.
26131da177e4SLinus Torvalds 		   Do not dev_put!
26141da177e4SLinus Torvalds 	 */
2615439e2385SDenis V. Lunev 	if (how) {
26161da177e4SLinus Torvalds 		idev->dead = 1;
26178814c4b5SYOSHIFUJI Hideaki 
26188814c4b5SYOSHIFUJI Hideaki 		/* protected by rtnl_lock */
26198814c4b5SYOSHIFUJI Hideaki 		rcu_assign_pointer(dev->ip6_ptr, NULL);
26201da177e4SLinus Torvalds 
26211da177e4SLinus Torvalds 		/* Step 1.5: remove snmp6 entry */
26221da177e4SLinus Torvalds 		snmp6_unregister_dev(idev);
26231da177e4SLinus Torvalds 
26241da177e4SLinus Torvalds 	}
26251da177e4SLinus Torvalds 
26261da177e4SLinus Torvalds 	/* Step 2: clear hash table */
26271da177e4SLinus Torvalds 	for (i=0; i<IN6_ADDR_HSIZE; i++) {
26281da177e4SLinus Torvalds 		bifa = &inet6_addr_lst[i];
26291da177e4SLinus Torvalds 
26301da177e4SLinus Torvalds 		write_lock_bh(&addrconf_hash_lock);
26311da177e4SLinus Torvalds 		while ((ifa = *bifa) != NULL) {
26321da177e4SLinus Torvalds 			if (ifa->idev == idev) {
26331da177e4SLinus Torvalds 				*bifa = ifa->lst_next;
26341da177e4SLinus Torvalds 				ifa->lst_next = NULL;
26351da177e4SLinus Torvalds 				addrconf_del_timer(ifa);
26361da177e4SLinus Torvalds 				in6_ifa_put(ifa);
26371da177e4SLinus Torvalds 				continue;
26381da177e4SLinus Torvalds 			}
26391da177e4SLinus Torvalds 			bifa = &ifa->lst_next;
26401da177e4SLinus Torvalds 		}
26411da177e4SLinus Torvalds 		write_unlock_bh(&addrconf_hash_lock);
26421da177e4SLinus Torvalds 	}
26431da177e4SLinus Torvalds 
26441da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
26451da177e4SLinus Torvalds 
26461da177e4SLinus Torvalds 	/* Step 3: clear flags for stateless addrconf */
2647439e2385SDenis V. Lunev 	if (!how)
26483c21edbdSYOSHIFUJI Hideaki 		idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
26491da177e4SLinus Torvalds 
26501da177e4SLinus Torvalds 	/* Step 4: clear address list */
26511da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
2652439e2385SDenis V. Lunev 	if (how && del_timer(&idev->regen_timer))
26531da177e4SLinus Torvalds 		in6_dev_put(idev);
26541da177e4SLinus Torvalds 
26551da177e4SLinus Torvalds 	/* clear tempaddr list */
26561da177e4SLinus Torvalds 	while ((ifa = idev->tempaddr_list) != NULL) {
26571da177e4SLinus Torvalds 		idev->tempaddr_list = ifa->tmp_next;
26581da177e4SLinus Torvalds 		ifa->tmp_next = NULL;
26591da177e4SLinus Torvalds 		ifa->dead = 1;
26601da177e4SLinus Torvalds 		write_unlock_bh(&idev->lock);
26611da177e4SLinus Torvalds 		spin_lock_bh(&ifa->lock);
26621da177e4SLinus Torvalds 
26631da177e4SLinus Torvalds 		if (ifa->ifpub) {
26641da177e4SLinus Torvalds 			in6_ifa_put(ifa->ifpub);
26651da177e4SLinus Torvalds 			ifa->ifpub = NULL;
26661da177e4SLinus Torvalds 		}
26671da177e4SLinus Torvalds 		spin_unlock_bh(&ifa->lock);
26681da177e4SLinus Torvalds 		in6_ifa_put(ifa);
26691da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
26701da177e4SLinus Torvalds 	}
26711da177e4SLinus Torvalds #endif
26721da177e4SLinus Torvalds 	while ((ifa = idev->addr_list) != NULL) {
26731da177e4SLinus Torvalds 		idev->addr_list = ifa->if_next;
26741da177e4SLinus Torvalds 		ifa->if_next = NULL;
26751da177e4SLinus Torvalds 		ifa->dead = 1;
26761da177e4SLinus Torvalds 		addrconf_del_timer(ifa);
26771da177e4SLinus Torvalds 		write_unlock_bh(&idev->lock);
26781da177e4SLinus Torvalds 
26791da177e4SLinus Torvalds 		__ipv6_ifa_notify(RTM_DELADDR, ifa);
2680063ed369SVlad Yasevich 		atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa);
26811da177e4SLinus Torvalds 		in6_ifa_put(ifa);
26821da177e4SLinus Torvalds 
26831da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
26841da177e4SLinus Torvalds 	}
26851da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
26861da177e4SLinus Torvalds 
26871da177e4SLinus Torvalds 	/* Step 5: Discard multicast list */
26881da177e4SLinus Torvalds 
2689439e2385SDenis V. Lunev 	if (how)
26901da177e4SLinus Torvalds 		ipv6_mc_destroy_dev(idev);
26911da177e4SLinus Torvalds 	else
26921da177e4SLinus Torvalds 		ipv6_mc_down(idev);
26931da177e4SLinus Torvalds 
26941da177e4SLinus Torvalds 	idev->tstamp = jiffies;
26951da177e4SLinus Torvalds 
26961da177e4SLinus Torvalds 	/* Shot the device (if unregistered) */
26971da177e4SLinus Torvalds 
2698439e2385SDenis V. Lunev 	if (how) {
2699408c4768SPavel Emelyanov 		addrconf_sysctl_unregister(idev);
27001da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, idev->nd_parms);
27011da177e4SLinus Torvalds 		neigh_ifdown(&nd_tbl, dev);
27021da177e4SLinus Torvalds 		in6_dev_put(idev);
27031da177e4SLinus Torvalds 	}
27041da177e4SLinus Torvalds 	return 0;
27051da177e4SLinus Torvalds }
27061da177e4SLinus Torvalds 
27071da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data)
27081da177e4SLinus Torvalds {
27091da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
27101da177e4SLinus Torvalds 
27111da177e4SLinus Torvalds 	if (ifp->idev->cnf.forwarding)
27121da177e4SLinus Torvalds 		goto out;
27131da177e4SLinus Torvalds 
27141da177e4SLinus Torvalds 	if (ifp->idev->if_flags & IF_RA_RCVD) {
27151da177e4SLinus Torvalds 		/*
27161da177e4SLinus Torvalds 		 *	Announcement received after solicitation
27171da177e4SLinus Torvalds 		 *	was sent
27181da177e4SLinus Torvalds 		 */
27191da177e4SLinus Torvalds 		goto out;
27201da177e4SLinus Torvalds 	}
27211da177e4SLinus Torvalds 
27221da177e4SLinus Torvalds 	spin_lock(&ifp->lock);
27231da177e4SLinus Torvalds 	if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
27241da177e4SLinus Torvalds 		/* The wait after the last probe can be shorter */
27251da177e4SLinus Torvalds 		addrconf_mod_timer(ifp, AC_RS,
27261da177e4SLinus Torvalds 				   (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
27271da177e4SLinus Torvalds 				   ifp->idev->cnf.rtr_solicit_delay :
27281da177e4SLinus Torvalds 				   ifp->idev->cnf.rtr_solicit_interval);
27291da177e4SLinus Torvalds 		spin_unlock(&ifp->lock);
27301da177e4SLinus Torvalds 
2731f3ee4010SYOSHIFUJI Hideaki 		ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
27321da177e4SLinus Torvalds 	} else {
27331da177e4SLinus Torvalds 		spin_unlock(&ifp->lock);
27341da177e4SLinus Torvalds 		/*
27351da177e4SLinus Torvalds 		 * Note: we do not support deprecated "all on-link"
27361da177e4SLinus Torvalds 		 * assumption any longer.
27371da177e4SLinus Torvalds 		 */
27381da177e4SLinus Torvalds 		printk(KERN_DEBUG "%s: no IPv6 routers present\n",
27391da177e4SLinus Torvalds 		       ifp->idev->dev->name);
27401da177e4SLinus Torvalds 	}
27411da177e4SLinus Torvalds 
27421da177e4SLinus Torvalds out:
27431da177e4SLinus Torvalds 	in6_ifa_put(ifp);
27441da177e4SLinus Torvalds }
27451da177e4SLinus Torvalds 
27461da177e4SLinus Torvalds /*
27471da177e4SLinus Torvalds  *	Duplicate Address Detection
27481da177e4SLinus Torvalds  */
27493c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
27503c21edbdSYOSHIFUJI Hideaki {
27513c21edbdSYOSHIFUJI Hideaki 	unsigned long rand_num;
27523c21edbdSYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
27533c21edbdSYOSHIFUJI Hideaki 
275495c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
275595c385b4SNeil Horman 		rand_num = 0;
275695c385b4SNeil Horman 	else
27573c21edbdSYOSHIFUJI Hideaki 		rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
275895c385b4SNeil Horman 
27593c21edbdSYOSHIFUJI Hideaki 	ifp->probes = idev->cnf.dad_transmits;
27603c21edbdSYOSHIFUJI Hideaki 	addrconf_mod_timer(ifp, AC_DAD, rand_num);
27613c21edbdSYOSHIFUJI Hideaki }
27623c21edbdSYOSHIFUJI Hideaki 
2763e431b8c0SJamal Hadi Salim static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
27641da177e4SLinus Torvalds {
27651da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
27661da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
27671da177e4SLinus Torvalds 
27681da177e4SLinus Torvalds 	addrconf_join_solict(dev, &ifp->addr);
27691da177e4SLinus Torvalds 
27701da177e4SLinus Torvalds 	net_srandom(ifp->addr.s6_addr32[3]);
27711da177e4SLinus Torvalds 
27721da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
27731da177e4SLinus Torvalds 	if (ifp->dead)
27741da177e4SLinus Torvalds 		goto out;
27751da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
27761da177e4SLinus Torvalds 
27771da177e4SLinus Torvalds 	if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
27781b34be74SYOSHIFUJI Hideaki 	    idev->cnf.accept_dad < 1 ||
277955ebaef1SNoriaki TAKAMIYA 	    !(ifp->flags&IFA_F_TENTATIVE) ||
278055ebaef1SNoriaki TAKAMIYA 	    ifp->flags & IFA_F_NODAD) {
278195c385b4SNeil Horman 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC);
27821da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
27831da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
27841da177e4SLinus Torvalds 
27851da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
27861da177e4SLinus Torvalds 		return;
27871da177e4SLinus Torvalds 	}
27881da177e4SLinus Torvalds 
27896732badeSYOSHIFUJI Hideaki 	if (!(idev->if_flags & IF_READY)) {
27903dd3bf83SYOSHIFUJI Hideaki 		spin_unlock_bh(&ifp->lock);
27916732badeSYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
27923c21edbdSYOSHIFUJI Hideaki 		/*
27933c21edbdSYOSHIFUJI Hideaki 		 * If the defice is not ready:
27943c21edbdSYOSHIFUJI Hideaki 		 * - keep it tentative if it is a permanent address.
27953c21edbdSYOSHIFUJI Hideaki 		 * - otherwise, kill it.
27963c21edbdSYOSHIFUJI Hideaki 		 */
27973c21edbdSYOSHIFUJI Hideaki 		in6_ifa_hold(ifp);
27983c21edbdSYOSHIFUJI Hideaki 		addrconf_dad_stop(ifp);
27996732badeSYOSHIFUJI Hideaki 		return;
28003c21edbdSYOSHIFUJI Hideaki 	}
280195c385b4SNeil Horman 
280295c385b4SNeil Horman 	/*
280395c385b4SNeil Horman 	 * Optimistic nodes can start receiving
280495c385b4SNeil Horman 	 * Frames right away
280595c385b4SNeil Horman 	 */
280695c385b4SNeil Horman 	if(ifp->flags & IFA_F_OPTIMISTIC)
280795c385b4SNeil Horman 		ip6_ins_rt(ifp->rt);
280895c385b4SNeil Horman 
28096732badeSYOSHIFUJI Hideaki 	addrconf_dad_kick(ifp);
28106732badeSYOSHIFUJI Hideaki 	spin_unlock_bh(&ifp->lock);
28111da177e4SLinus Torvalds out:
28121da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
28131da177e4SLinus Torvalds }
28141da177e4SLinus Torvalds 
28151da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data)
28161da177e4SLinus Torvalds {
28171da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
28181da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
28191da177e4SLinus Torvalds 	struct in6_addr mcaddr;
28201da177e4SLinus Torvalds 
28211da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
28221da177e4SLinus Torvalds 	if (idev->dead) {
28231da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
28241da177e4SLinus Torvalds 		goto out;
28251da177e4SLinus Torvalds 	}
28261b34be74SYOSHIFUJI Hideaki 	if (idev->cnf.accept_dad > 1 && idev->cnf.disable_ipv6) {
28271b34be74SYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
28281b34be74SYOSHIFUJI Hideaki 		addrconf_dad_failure(ifp);
28291b34be74SYOSHIFUJI Hideaki 		return;
28301b34be74SYOSHIFUJI Hideaki 	}
28311da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
28321da177e4SLinus Torvalds 	if (ifp->probes == 0) {
28331da177e4SLinus Torvalds 		/*
28341da177e4SLinus Torvalds 		 * DAD was successful
28351da177e4SLinus Torvalds 		 */
28361da177e4SLinus Torvalds 
283795c385b4SNeil Horman 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC);
28381da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
28391da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
28401da177e4SLinus Torvalds 
28411da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
28421da177e4SLinus Torvalds 
28431da177e4SLinus Torvalds 		goto out;
28441da177e4SLinus Torvalds 	}
28451da177e4SLinus Torvalds 
28461da177e4SLinus Torvalds 	ifp->probes--;
28471da177e4SLinus Torvalds 	addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
28481da177e4SLinus Torvalds 	spin_unlock_bh(&ifp->lock);
28491da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
28501da177e4SLinus Torvalds 
28511da177e4SLinus Torvalds 	/* send a neighbour solicitation for our addr */
28521da177e4SLinus Torvalds 	addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
2853d7aabf22SYOSHIFUJI Hideaki 	ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
28541da177e4SLinus Torvalds out:
28551da177e4SLinus Torvalds 	in6_ifa_put(ifp);
28561da177e4SLinus Torvalds }
28571da177e4SLinus Torvalds 
28581da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
28591da177e4SLinus Torvalds {
28601da177e4SLinus Torvalds 	struct net_device *	dev = ifp->idev->dev;
28611da177e4SLinus Torvalds 
28621da177e4SLinus Torvalds 	/*
28631da177e4SLinus Torvalds 	 *	Configure the address for reception. Now it is valid.
28641da177e4SLinus Torvalds 	 */
28651da177e4SLinus Torvalds 
28661da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_NEWADDR, ifp);
28671da177e4SLinus Torvalds 
28681da177e4SLinus Torvalds 	/* If added prefix is link local and forwarding is off,
28691da177e4SLinus Torvalds 	   start sending router solicitations.
28701da177e4SLinus Torvalds 	 */
28711da177e4SLinus Torvalds 
28721da177e4SLinus Torvalds 	if (ifp->idev->cnf.forwarding == 0 &&
28731da177e4SLinus Torvalds 	    ifp->idev->cnf.rtr_solicits > 0 &&
28741da177e4SLinus Torvalds 	    (dev->flags&IFF_LOOPBACK) == 0 &&
28751da177e4SLinus Torvalds 	    (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
28761da177e4SLinus Torvalds 		/*
28771da177e4SLinus Torvalds 		 *	If a host as already performed a random delay
28781da177e4SLinus Torvalds 		 *	[...] as part of DAD [...] there is no need
28791da177e4SLinus Torvalds 		 *	to delay again before sending the first RS
28801da177e4SLinus Torvalds 		 */
2881f3ee4010SYOSHIFUJI Hideaki 		ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
28821da177e4SLinus Torvalds 
28831da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
28841da177e4SLinus Torvalds 		ifp->probes = 1;
28851da177e4SLinus Torvalds 		ifp->idev->if_flags |= IF_RS_SENT;
28861da177e4SLinus Torvalds 		addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
28871da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
28881da177e4SLinus Torvalds 	}
28891da177e4SLinus Torvalds }
28901da177e4SLinus Torvalds 
2891c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev) {
2892c5e33bddSYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifp;
2893c5e33bddSYOSHIFUJI Hideaki 
2894c5e33bddSYOSHIFUJI Hideaki 	read_lock_bh(&idev->lock);
2895c5e33bddSYOSHIFUJI Hideaki 	for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2896c5e33bddSYOSHIFUJI Hideaki 		spin_lock_bh(&ifp->lock);
2897c5e33bddSYOSHIFUJI Hideaki 		if (!(ifp->flags & IFA_F_TENTATIVE)) {
2898c5e33bddSYOSHIFUJI Hideaki 			spin_unlock_bh(&ifp->lock);
2899c5e33bddSYOSHIFUJI Hideaki 			continue;
2900c5e33bddSYOSHIFUJI Hideaki 		}
2901c5e33bddSYOSHIFUJI Hideaki 		spin_unlock_bh(&ifp->lock);
2902c5e33bddSYOSHIFUJI Hideaki 		addrconf_dad_kick(ifp);
2903c5e33bddSYOSHIFUJI Hideaki 	}
2904c5e33bddSYOSHIFUJI Hideaki 	read_unlock_bh(&idev->lock);
2905c5e33bddSYOSHIFUJI Hideaki }
2906c5e33bddSYOSHIFUJI Hideaki 
29071da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
29081da177e4SLinus Torvalds struct if6_iter_state {
29093c40090aSDaniel Lezcano 	struct seq_net_private p;
29101da177e4SLinus Torvalds 	int bucket;
29111da177e4SLinus Torvalds };
29121da177e4SLinus Torvalds 
29131da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
29141da177e4SLinus Torvalds {
29151da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
29161da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
29171218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
29181da177e4SLinus Torvalds 
29191da177e4SLinus Torvalds 	for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
29201da177e4SLinus Torvalds 		ifa = inet6_addr_lst[state->bucket];
29213c40090aSDaniel Lezcano 
2922878628fbSYOSHIFUJI Hideaki 		while (ifa && !net_eq(dev_net(ifa->idev->dev), net))
29233c40090aSDaniel Lezcano 			ifa = ifa->lst_next;
29241da177e4SLinus Torvalds 		if (ifa)
29251da177e4SLinus Torvalds 			break;
29261da177e4SLinus Torvalds 	}
29271da177e4SLinus Torvalds 	return ifa;
29281da177e4SLinus Torvalds }
29291da177e4SLinus Torvalds 
29301da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
29311da177e4SLinus Torvalds {
29321da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
29331218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
29341da177e4SLinus Torvalds 
29351da177e4SLinus Torvalds 	ifa = ifa->lst_next;
29361da177e4SLinus Torvalds try_again:
29373c40090aSDaniel Lezcano 	if (ifa) {
2938878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifa->idev->dev), net)) {
29393c40090aSDaniel Lezcano 			ifa = ifa->lst_next;
29403c40090aSDaniel Lezcano 			goto try_again;
29413c40090aSDaniel Lezcano 		}
29423c40090aSDaniel Lezcano 	}
29433c40090aSDaniel Lezcano 
29441da177e4SLinus Torvalds 	if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
29451da177e4SLinus Torvalds 		ifa = inet6_addr_lst[state->bucket];
29461da177e4SLinus Torvalds 		goto try_again;
29471da177e4SLinus Torvalds 	}
29483c40090aSDaniel Lezcano 
29491da177e4SLinus Torvalds 	return ifa;
29501da177e4SLinus Torvalds }
29511da177e4SLinus Torvalds 
29521da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
29531da177e4SLinus Torvalds {
29541da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = if6_get_first(seq);
29551da177e4SLinus Torvalds 
29561da177e4SLinus Torvalds 	if (ifa)
29571da177e4SLinus Torvalds 		while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
29581da177e4SLinus Torvalds 			--pos;
29591da177e4SLinus Torvalds 	return pos ? NULL : ifa;
29601da177e4SLinus Torvalds }
29611da177e4SLinus Torvalds 
29621da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
2963d20b3109SStephen Hemminger 	__acquires(addrconf_hash_lock)
29641da177e4SLinus Torvalds {
29651da177e4SLinus Torvalds 	read_lock_bh(&addrconf_hash_lock);
29661da177e4SLinus Torvalds 	return if6_get_idx(seq, *pos);
29671da177e4SLinus Torvalds }
29681da177e4SLinus Torvalds 
29691da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
29701da177e4SLinus Torvalds {
29711da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
29721da177e4SLinus Torvalds 
29731da177e4SLinus Torvalds 	ifa = if6_get_next(seq, v);
29741da177e4SLinus Torvalds 	++*pos;
29751da177e4SLinus Torvalds 	return ifa;
29761da177e4SLinus Torvalds }
29771da177e4SLinus Torvalds 
29781da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v)
2979d20b3109SStephen Hemminger 	__releases(addrconf_hash_lock)
29801da177e4SLinus Torvalds {
29811da177e4SLinus Torvalds 	read_unlock_bh(&addrconf_hash_lock);
29821da177e4SLinus Torvalds }
29831da177e4SLinus Torvalds 
29841da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v)
29851da177e4SLinus Torvalds {
29861da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
29874b7a4274SHarvey Harrison 	seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
2988b071195dSHarvey Harrison 		   &ifp->addr,
29891da177e4SLinus Torvalds 		   ifp->idev->dev->ifindex,
29901da177e4SLinus Torvalds 		   ifp->prefix_len,
29911da177e4SLinus Torvalds 		   ifp->scope,
29921da177e4SLinus Torvalds 		   ifp->flags,
29931da177e4SLinus Torvalds 		   ifp->idev->dev->name);
29941da177e4SLinus Torvalds 	return 0;
29951da177e4SLinus Torvalds }
29961da177e4SLinus Torvalds 
299756b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = {
29981da177e4SLinus Torvalds 	.start	= if6_seq_start,
29991da177e4SLinus Torvalds 	.next	= if6_seq_next,
30001da177e4SLinus Torvalds 	.show	= if6_seq_show,
30011da177e4SLinus Torvalds 	.stop	= if6_seq_stop,
30021da177e4SLinus Torvalds };
30031da177e4SLinus Torvalds 
30041da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file)
30051da177e4SLinus Torvalds {
30063c40090aSDaniel Lezcano 	return seq_open_net(inode, file, &if6_seq_ops,
3007cf7732e4SPavel Emelyanov 			    sizeof(struct if6_iter_state));
30081da177e4SLinus Torvalds }
30091da177e4SLinus Torvalds 
30109a32144eSArjan van de Ven static const struct file_operations if6_fops = {
30111da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
30121da177e4SLinus Torvalds 	.open		= if6_seq_open,
30131da177e4SLinus Torvalds 	.read		= seq_read,
30141da177e4SLinus Torvalds 	.llseek		= seq_lseek,
30153c40090aSDaniel Lezcano 	.release	= seq_release_net,
30161da177e4SLinus Torvalds };
30171da177e4SLinus Torvalds 
30183c40090aSDaniel Lezcano static int if6_proc_net_init(struct net *net)
30191da177e4SLinus Torvalds {
30203c40090aSDaniel Lezcano 	if (!proc_net_fops_create(net, "if_inet6", S_IRUGO, &if6_fops))
30211da177e4SLinus Torvalds 		return -ENOMEM;
30221da177e4SLinus Torvalds 	return 0;
30231da177e4SLinus Torvalds }
30241da177e4SLinus Torvalds 
30253c40090aSDaniel Lezcano static void if6_proc_net_exit(struct net *net)
30263c40090aSDaniel Lezcano {
30273c40090aSDaniel Lezcano        proc_net_remove(net, "if_inet6");
30283c40090aSDaniel Lezcano }
30293c40090aSDaniel Lezcano 
30303c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = {
30313c40090aSDaniel Lezcano        .init = if6_proc_net_init,
30323c40090aSDaniel Lezcano        .exit = if6_proc_net_exit,
30333c40090aSDaniel Lezcano };
30343c40090aSDaniel Lezcano 
30353c40090aSDaniel Lezcano int __init if6_proc_init(void)
30363c40090aSDaniel Lezcano {
30373c40090aSDaniel Lezcano 	return register_pernet_subsys(&if6_proc_net_ops);
30383c40090aSDaniel Lezcano }
30393c40090aSDaniel Lezcano 
30401da177e4SLinus Torvalds void if6_proc_exit(void)
30411da177e4SLinus Torvalds {
30423c40090aSDaniel Lezcano 	unregister_pernet_subsys(&if6_proc_net_ops);
30431da177e4SLinus Torvalds }
30441da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
30451da177e4SLinus Torvalds 
304659fbb3a6SMasahide NAKAMURA #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
30473b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */
3048389f6612SDaniel Lezcano int ipv6_chk_home_addr(struct net *net, struct in6_addr *addr)
30493b9f9a1cSNoriaki TAKAMIYA {
30503b9f9a1cSNoriaki TAKAMIYA 	int ret = 0;
30513b9f9a1cSNoriaki TAKAMIYA 	struct inet6_ifaddr * ifp;
30523b9f9a1cSNoriaki TAKAMIYA 	u8 hash = ipv6_addr_hash(addr);
30533b9f9a1cSNoriaki TAKAMIYA 	read_lock_bh(&addrconf_hash_lock);
30543b9f9a1cSNoriaki TAKAMIYA 	for (ifp = inet6_addr_lst[hash]; ifp; ifp = ifp->lst_next) {
3055878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
3056389f6612SDaniel Lezcano 			continue;
3057caad295fSYOSHIFUJI Hideaki 		if (ipv6_addr_equal(&ifp->addr, addr) &&
30583b9f9a1cSNoriaki TAKAMIYA 		    (ifp->flags & IFA_F_HOMEADDRESS)) {
30593b9f9a1cSNoriaki TAKAMIYA 			ret = 1;
30603b9f9a1cSNoriaki TAKAMIYA 			break;
30613b9f9a1cSNoriaki TAKAMIYA 		}
30623b9f9a1cSNoriaki TAKAMIYA 	}
30633b9f9a1cSNoriaki TAKAMIYA 	read_unlock_bh(&addrconf_hash_lock);
30643b9f9a1cSNoriaki TAKAMIYA 	return ret;
30653b9f9a1cSNoriaki TAKAMIYA }
30663b9f9a1cSNoriaki TAKAMIYA #endif
30673b9f9a1cSNoriaki TAKAMIYA 
30681da177e4SLinus Torvalds /*
30691da177e4SLinus Torvalds  *	Periodic address status verification
30701da177e4SLinus Torvalds  */
30711da177e4SLinus Torvalds 
30721da177e4SLinus Torvalds static void addrconf_verify(unsigned long foo)
30731da177e4SLinus Torvalds {
30741da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
30751da177e4SLinus Torvalds 	unsigned long now, next;
30761da177e4SLinus Torvalds 	int i;
30771da177e4SLinus Torvalds 
30781da177e4SLinus Torvalds 	spin_lock_bh(&addrconf_verify_lock);
30791da177e4SLinus Torvalds 	now = jiffies;
30801da177e4SLinus Torvalds 	next = now + ADDR_CHECK_FREQUENCY;
30811da177e4SLinus Torvalds 
30821da177e4SLinus Torvalds 	del_timer(&addr_chk_timer);
30831da177e4SLinus Torvalds 
30841da177e4SLinus Torvalds 	for (i=0; i < IN6_ADDR_HSIZE; i++) {
30851da177e4SLinus Torvalds 
30861da177e4SLinus Torvalds restart:
30875d5780dfSYan Zheng 		read_lock(&addrconf_hash_lock);
30881da177e4SLinus Torvalds 		for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
30891da177e4SLinus Torvalds 			unsigned long age;
30901da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
30911da177e4SLinus Torvalds 			unsigned long regen_advance;
30921da177e4SLinus Torvalds #endif
30931da177e4SLinus Torvalds 
30941da177e4SLinus Torvalds 			if (ifp->flags & IFA_F_PERMANENT)
30951da177e4SLinus Torvalds 				continue;
30961da177e4SLinus Torvalds 
30971da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
30981da177e4SLinus Torvalds 			age = (now - ifp->tstamp) / HZ;
30991da177e4SLinus Torvalds 
31001da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
31011da177e4SLinus Torvalds 			regen_advance = ifp->idev->cnf.regen_max_retry *
31021da177e4SLinus Torvalds 					ifp->idev->cnf.dad_transmits *
31031da177e4SLinus Torvalds 					ifp->idev->nd_parms->retrans_time / HZ;
31041da177e4SLinus Torvalds #endif
31051da177e4SLinus Torvalds 
31068f27ebb9SYOSHIFUJI Hideaki 			if (ifp->valid_lft != INFINITY_LIFE_TIME &&
31078f27ebb9SYOSHIFUJI Hideaki 			    age >= ifp->valid_lft) {
31081da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
31091da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
31105d5780dfSYan Zheng 				read_unlock(&addrconf_hash_lock);
31111da177e4SLinus Torvalds 				ipv6_del_addr(ifp);
31121da177e4SLinus Torvalds 				goto restart;
31138f27ebb9SYOSHIFUJI Hideaki 			} else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
31148f27ebb9SYOSHIFUJI Hideaki 				spin_unlock(&ifp->lock);
31158f27ebb9SYOSHIFUJI Hideaki 				continue;
31161da177e4SLinus Torvalds 			} else if (age >= ifp->prefered_lft) {
31171da177e4SLinus Torvalds 				/* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
31181da177e4SLinus Torvalds 				int deprecate = 0;
31191da177e4SLinus Torvalds 
31201da177e4SLinus Torvalds 				if (!(ifp->flags&IFA_F_DEPRECATED)) {
31211da177e4SLinus Torvalds 					deprecate = 1;
31221da177e4SLinus Torvalds 					ifp->flags |= IFA_F_DEPRECATED;
31231da177e4SLinus Torvalds 				}
31241da177e4SLinus Torvalds 
31251da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
31261da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->valid_lft * HZ;
31271da177e4SLinus Torvalds 
31281da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
31291da177e4SLinus Torvalds 
31301da177e4SLinus Torvalds 				if (deprecate) {
31311da177e4SLinus Torvalds 					in6_ifa_hold(ifp);
31325d5780dfSYan Zheng 					read_unlock(&addrconf_hash_lock);
31331da177e4SLinus Torvalds 
31341da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
31351da177e4SLinus Torvalds 					in6_ifa_put(ifp);
31361da177e4SLinus Torvalds 					goto restart;
31371da177e4SLinus Torvalds 				}
31381da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
31391da177e4SLinus Torvalds 			} else if ((ifp->flags&IFA_F_TEMPORARY) &&
31401da177e4SLinus Torvalds 				   !(ifp->flags&IFA_F_TENTATIVE)) {
31411da177e4SLinus Torvalds 				if (age >= ifp->prefered_lft - regen_advance) {
31421da177e4SLinus Torvalds 					struct inet6_ifaddr *ifpub = ifp->ifpub;
31431da177e4SLinus Torvalds 					if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
31441da177e4SLinus Torvalds 						next = ifp->tstamp + ifp->prefered_lft * HZ;
31451da177e4SLinus Torvalds 					if (!ifp->regen_count && ifpub) {
31461da177e4SLinus Torvalds 						ifp->regen_count++;
31471da177e4SLinus Torvalds 						in6_ifa_hold(ifp);
31481da177e4SLinus Torvalds 						in6_ifa_hold(ifpub);
31491da177e4SLinus Torvalds 						spin_unlock(&ifp->lock);
31505d5780dfSYan Zheng 						read_unlock(&addrconf_hash_lock);
3151291d809bSHiroyuki YAMAMORI 						spin_lock(&ifpub->lock);
3152291d809bSHiroyuki YAMAMORI 						ifpub->regen_count = 0;
3153291d809bSHiroyuki YAMAMORI 						spin_unlock(&ifpub->lock);
31541da177e4SLinus Torvalds 						ipv6_create_tempaddr(ifpub, ifp);
31551da177e4SLinus Torvalds 						in6_ifa_put(ifpub);
31561da177e4SLinus Torvalds 						in6_ifa_put(ifp);
31571da177e4SLinus Torvalds 						goto restart;
31581da177e4SLinus Torvalds 					}
31591da177e4SLinus Torvalds 				} else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
31601da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
31611da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
31621da177e4SLinus Torvalds #endif
31631da177e4SLinus Torvalds 			} else {
31641da177e4SLinus Torvalds 				/* ifp->prefered_lft <= ifp->valid_lft */
31651da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
31661da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ;
31671da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
31681da177e4SLinus Torvalds 			}
31691da177e4SLinus Torvalds 		}
31705d5780dfSYan Zheng 		read_unlock(&addrconf_hash_lock);
31711da177e4SLinus Torvalds 	}
31721da177e4SLinus Torvalds 
31731da177e4SLinus Torvalds 	addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
31741da177e4SLinus Torvalds 	add_timer(&addr_chk_timer);
31751da177e4SLinus Torvalds 	spin_unlock_bh(&addrconf_verify_lock);
31761da177e4SLinus Torvalds }
31771da177e4SLinus Torvalds 
3178461d8837SThomas Graf static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
3179461d8837SThomas Graf {
3180461d8837SThomas Graf 	struct in6_addr *pfx = NULL;
3181461d8837SThomas Graf 
3182461d8837SThomas Graf 	if (addr)
3183461d8837SThomas Graf 		pfx = nla_data(addr);
3184461d8837SThomas Graf 
3185461d8837SThomas Graf 	if (local) {
3186461d8837SThomas Graf 		if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3187461d8837SThomas Graf 			pfx = NULL;
3188461d8837SThomas Graf 		else
3189461d8837SThomas Graf 			pfx = nla_data(local);
3190461d8837SThomas Graf 	}
3191461d8837SThomas Graf 
3192461d8837SThomas Graf 	return pfx;
3193461d8837SThomas Graf }
3194461d8837SThomas Graf 
3195ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
3196461d8837SThomas Graf 	[IFA_ADDRESS]		= { .len = sizeof(struct in6_addr) },
3197461d8837SThomas Graf 	[IFA_LOCAL]		= { .len = sizeof(struct in6_addr) },
3198461d8837SThomas Graf 	[IFA_CACHEINFO]		= { .len = sizeof(struct ifa_cacheinfo) },
3199461d8837SThomas Graf };
3200461d8837SThomas Graf 
32011da177e4SLinus Torvalds static int
32021da177e4SLinus Torvalds inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
32031da177e4SLinus Torvalds {
32043b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
3205b933f716SThomas Graf 	struct ifaddrmsg *ifm;
3206b933f716SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
32071da177e4SLinus Torvalds 	struct in6_addr *pfx;
3208b933f716SThomas Graf 	int err;
32091da177e4SLinus Torvalds 
3210b933f716SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3211b933f716SThomas Graf 	if (err < 0)
3212b933f716SThomas Graf 		return err;
3213b933f716SThomas Graf 
3214b933f716SThomas Graf 	ifm = nlmsg_data(nlh);
3215b933f716SThomas Graf 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
32161da177e4SLinus Torvalds 	if (pfx == NULL)
32171da177e4SLinus Torvalds 		return -EINVAL;
32181da177e4SLinus Torvalds 
3219af284937SDaniel Lezcano 	return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen);
32201da177e4SLinus Torvalds }
32211da177e4SLinus Torvalds 
322255ebaef1SNoriaki TAKAMIYA static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
322355ebaef1SNoriaki TAKAMIYA 			     u32 prefered_lft, u32 valid_lft)
3224081bba5bSNoriaki TAKAMIYA {
32256f704992SYOSHIFUJI Hideaki 	u32 flags;
32266f704992SYOSHIFUJI Hideaki 	clock_t expires;
32274bed72e4SYOSHIFUJI Hideaki 	unsigned long timeout;
322846d48046SYOSHIFUJI Hideaki 
3229081bba5bSNoriaki TAKAMIYA 	if (!valid_lft || (prefered_lft > valid_lft))
3230081bba5bSNoriaki TAKAMIYA 		return -EINVAL;
3231081bba5bSNoriaki TAKAMIYA 
32324bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
32334bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
32344bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
32354bed72e4SYOSHIFUJI Hideaki 		valid_lft = timeout;
32366f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
32374bed72e4SYOSHIFUJI Hideaki 	} else {
32384bed72e4SYOSHIFUJI Hideaki 		expires = 0;
32394bed72e4SYOSHIFUJI Hideaki 		flags = 0;
32404bed72e4SYOSHIFUJI Hideaki 		ifa_flags |= IFA_F_PERMANENT;
32416f704992SYOSHIFUJI Hideaki 	}
3242081bba5bSNoriaki TAKAMIYA 
32434bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
32444bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
32454bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
324655ebaef1SNoriaki TAKAMIYA 			ifa_flags |= IFA_F_DEPRECATED;
32474bed72e4SYOSHIFUJI Hideaki 		prefered_lft = timeout;
32484bed72e4SYOSHIFUJI Hideaki 	}
3249081bba5bSNoriaki TAKAMIYA 
3250081bba5bSNoriaki TAKAMIYA 	spin_lock_bh(&ifp->lock);
32513b9f9a1cSNoriaki TAKAMIYA 	ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags;
3252081bba5bSNoriaki TAKAMIYA 	ifp->tstamp = jiffies;
3253081bba5bSNoriaki TAKAMIYA 	ifp->valid_lft = valid_lft;
3254081bba5bSNoriaki TAKAMIYA 	ifp->prefered_lft = prefered_lft;
3255081bba5bSNoriaki TAKAMIYA 
3256081bba5bSNoriaki TAKAMIYA 	spin_unlock_bh(&ifp->lock);
3257081bba5bSNoriaki TAKAMIYA 	if (!(ifp->flags&IFA_F_TENTATIVE))
3258081bba5bSNoriaki TAKAMIYA 		ipv6_ifa_notify(0, ifp);
3259081bba5bSNoriaki TAKAMIYA 
326046d48046SYOSHIFUJI Hideaki 	addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
32616f704992SYOSHIFUJI Hideaki 			      expires, flags);
3262081bba5bSNoriaki TAKAMIYA 	addrconf_verify(0);
3263081bba5bSNoriaki TAKAMIYA 
3264081bba5bSNoriaki TAKAMIYA 	return 0;
3265081bba5bSNoriaki TAKAMIYA }
3266081bba5bSNoriaki TAKAMIYA 
3267081bba5bSNoriaki TAKAMIYA static int
32681da177e4SLinus Torvalds inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
32691da177e4SLinus Torvalds {
32703b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
3271461d8837SThomas Graf 	struct ifaddrmsg *ifm;
3272461d8837SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
32731da177e4SLinus Torvalds 	struct in6_addr *pfx;
32747198f8ceSThomas Graf 	struct inet6_ifaddr *ifa;
32757198f8ceSThomas Graf 	struct net_device *dev;
327655ebaef1SNoriaki TAKAMIYA 	u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
327755ebaef1SNoriaki TAKAMIYA 	u8 ifa_flags;
3278461d8837SThomas Graf 	int err;
32791da177e4SLinus Torvalds 
3280461d8837SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3281461d8837SThomas Graf 	if (err < 0)
3282461d8837SThomas Graf 		return err;
3283461d8837SThomas Graf 
3284461d8837SThomas Graf 	ifm = nlmsg_data(nlh);
3285461d8837SThomas Graf 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
32861da177e4SLinus Torvalds 	if (pfx == NULL)
32871da177e4SLinus Torvalds 		return -EINVAL;
32881da177e4SLinus Torvalds 
3289461d8837SThomas Graf 	if (tb[IFA_CACHEINFO]) {
32900778769dSNoriaki TAKAMIYA 		struct ifa_cacheinfo *ci;
3291461d8837SThomas Graf 
3292461d8837SThomas Graf 		ci = nla_data(tb[IFA_CACHEINFO]);
32930778769dSNoriaki TAKAMIYA 		valid_lft = ci->ifa_valid;
3294461d8837SThomas Graf 		preferred_lft = ci->ifa_prefered;
3295461d8837SThomas Graf 	} else {
3296461d8837SThomas Graf 		preferred_lft = INFINITY_LIFE_TIME;
3297461d8837SThomas Graf 		valid_lft = INFINITY_LIFE_TIME;
32980778769dSNoriaki TAKAMIYA 	}
32990778769dSNoriaki TAKAMIYA 
3300af284937SDaniel Lezcano 	dev =  __dev_get_by_index(net, ifm->ifa_index);
33017198f8ceSThomas Graf 	if (dev == NULL)
33027198f8ceSThomas Graf 		return -ENODEV;
3303081bba5bSNoriaki TAKAMIYA 
330455ebaef1SNoriaki TAKAMIYA 	/* We ignore other flags so far. */
33053b9f9a1cSNoriaki TAKAMIYA 	ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
330655ebaef1SNoriaki TAKAMIYA 
33071cab3da6SDaniel Lezcano 	ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
33087198f8ceSThomas Graf 	if (ifa == NULL) {
33097198f8ceSThomas Graf 		/*
33107198f8ceSThomas Graf 		 * It would be best to check for !NLM_F_CREATE here but
33117198f8ceSThomas Graf 		 * userspace alreay relies on not having to provide this.
33127198f8ceSThomas Graf 		 */
3313af284937SDaniel Lezcano 		return inet6_addr_add(net, ifm->ifa_index, pfx,
3314af284937SDaniel Lezcano 				      ifm->ifa_prefixlen, ifa_flags,
3315af284937SDaniel Lezcano 				      preferred_lft, valid_lft);
33161da177e4SLinus Torvalds 	}
33171da177e4SLinus Torvalds 
33187198f8ceSThomas Graf 	if (nlh->nlmsg_flags & NLM_F_EXCL ||
33197198f8ceSThomas Graf 	    !(nlh->nlmsg_flags & NLM_F_REPLACE))
33207198f8ceSThomas Graf 		err = -EEXIST;
33217198f8ceSThomas Graf 	else
332255ebaef1SNoriaki TAKAMIYA 		err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
33237198f8ceSThomas Graf 
33247198f8ceSThomas Graf 	in6_ifa_put(ifa);
33257198f8ceSThomas Graf 
33267198f8ceSThomas Graf 	return err;
33277198f8ceSThomas Graf }
33287198f8ceSThomas Graf 
3329101bb229SThomas Graf static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3330101bb229SThomas Graf 			  u8 scope, int ifindex)
3331101bb229SThomas Graf {
3332101bb229SThomas Graf 	struct ifaddrmsg *ifm;
3333101bb229SThomas Graf 
3334101bb229SThomas Graf 	ifm = nlmsg_data(nlh);
3335101bb229SThomas Graf 	ifm->ifa_family = AF_INET6;
3336101bb229SThomas Graf 	ifm->ifa_prefixlen = prefixlen;
3337101bb229SThomas Graf 	ifm->ifa_flags = flags;
3338101bb229SThomas Graf 	ifm->ifa_scope = scope;
3339101bb229SThomas Graf 	ifm->ifa_index = ifindex;
3340101bb229SThomas Graf }
3341101bb229SThomas Graf 
334285486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
334385486af0SThomas Graf 			 unsigned long tstamp, u32 preferred, u32 valid)
334485486af0SThomas Graf {
334585486af0SThomas Graf 	struct ifa_cacheinfo ci;
334685486af0SThomas Graf 
334785486af0SThomas Graf 	ci.cstamp = (u32)(TIME_DELTA(cstamp, INITIAL_JIFFIES) / HZ * 100
334885486af0SThomas Graf 			+ TIME_DELTA(cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
334985486af0SThomas Graf 	ci.tstamp = (u32)(TIME_DELTA(tstamp, INITIAL_JIFFIES) / HZ * 100
335085486af0SThomas Graf 			+ TIME_DELTA(tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
335185486af0SThomas Graf 	ci.ifa_prefered = preferred;
335285486af0SThomas Graf 	ci.ifa_valid = valid;
335385486af0SThomas Graf 
335485486af0SThomas Graf 	return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
335585486af0SThomas Graf }
335685486af0SThomas Graf 
3357101bb229SThomas Graf static inline int rt_scope(int ifa_scope)
3358101bb229SThomas Graf {
3359101bb229SThomas Graf 	if (ifa_scope & IFA_HOST)
3360101bb229SThomas Graf 		return RT_SCOPE_HOST;
3361101bb229SThomas Graf 	else if (ifa_scope & IFA_LINK)
3362101bb229SThomas Graf 		return RT_SCOPE_LINK;
3363101bb229SThomas Graf 	else if (ifa_scope & IFA_SITE)
3364101bb229SThomas Graf 		return RT_SCOPE_SITE;
3365101bb229SThomas Graf 	else
3366101bb229SThomas Graf 		return RT_SCOPE_UNIVERSE;
3367101bb229SThomas Graf }
3368101bb229SThomas Graf 
33690ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void)
33700ab6803bSThomas Graf {
3371339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3372339bf98fSThomas Graf 	       + nla_total_size(16) /* IFA_ADDRESS */
3373339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct ifa_cacheinfo));
33740ab6803bSThomas Graf }
3375c5396a31SYOSHIFUJI Hideaki 
33761da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
3377b6544c0bSJamal Hadi Salim 			     u32 pid, u32 seq, int event, unsigned int flags)
33781da177e4SLinus Torvalds {
33791da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
338085486af0SThomas Graf 	u32 preferred, valid;
33811da177e4SLinus Torvalds 
33820ab6803bSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
33830ab6803bSThomas Graf 	if (nlh == NULL)
338426932566SPatrick McHardy 		return -EMSGSIZE;
33850ab6803bSThomas Graf 
3386101bb229SThomas Graf 	put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3387101bb229SThomas Graf 		      ifa->idev->dev->ifindex);
3388101bb229SThomas Graf 
33891da177e4SLinus Torvalds 	if (!(ifa->flags&IFA_F_PERMANENT)) {
339085486af0SThomas Graf 		preferred = ifa->prefered_lft;
339185486af0SThomas Graf 		valid = ifa->valid_lft;
339285486af0SThomas Graf 		if (preferred != INFINITY_LIFE_TIME) {
33931da177e4SLinus Torvalds 			long tval = (jiffies - ifa->tstamp)/HZ;
339485486af0SThomas Graf 			preferred -= tval;
339585486af0SThomas Graf 			if (valid != INFINITY_LIFE_TIME)
339685486af0SThomas Graf 				valid -= tval;
33971da177e4SLinus Torvalds 		}
33981da177e4SLinus Torvalds 	} else {
339985486af0SThomas Graf 		preferred = INFINITY_LIFE_TIME;
340085486af0SThomas Graf 		valid = INFINITY_LIFE_TIME;
34011da177e4SLinus Torvalds 	}
340285486af0SThomas Graf 
34030ab6803bSThomas Graf 	if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
340426932566SPatrick McHardy 	    put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
340526932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
340626932566SPatrick McHardy 		return -EMSGSIZE;
340726932566SPatrick McHardy 	}
340885486af0SThomas Graf 
34090ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
34101da177e4SLinus Torvalds }
34111da177e4SLinus Torvalds 
34121da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
3413e431b8c0SJamal Hadi Salim 				u32 pid, u32 seq, int event, u16 flags)
34141da177e4SLinus Torvalds {
34151da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
3416101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
3417101bb229SThomas Graf 	int ifindex = ifmca->idev->dev->ifindex;
34181da177e4SLinus Torvalds 
34191da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3420101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
3421101bb229SThomas Graf 
34220ab6803bSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
34230ab6803bSThomas Graf 	if (nlh == NULL)
342426932566SPatrick McHardy 		return -EMSGSIZE;
34250ab6803bSThomas Graf 
3426101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
34270ab6803bSThomas Graf 	if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
34280ab6803bSThomas Graf 	    put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
342926932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
343026932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
343126932566SPatrick McHardy 		return -EMSGSIZE;
343226932566SPatrick McHardy 	}
343385486af0SThomas Graf 
34340ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
34351da177e4SLinus Torvalds }
34361da177e4SLinus Torvalds 
34371da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
3438b6544c0bSJamal Hadi Salim 				u32 pid, u32 seq, int event, unsigned int flags)
34391da177e4SLinus Torvalds {
34401da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
3441101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
3442101bb229SThomas Graf 	int ifindex = ifaca->aca_idev->dev->ifindex;
34431da177e4SLinus Torvalds 
34441da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3445101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
3446101bb229SThomas Graf 
34470ab6803bSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
34480ab6803bSThomas Graf 	if (nlh == NULL)
344926932566SPatrick McHardy 		return -EMSGSIZE;
34500ab6803bSThomas Graf 
3451101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
34520ab6803bSThomas Graf 	if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
34530ab6803bSThomas Graf 	    put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
345426932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
345526932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
345626932566SPatrick McHardy 		return -EMSGSIZE;
345726932566SPatrick McHardy 	}
345885486af0SThomas Graf 
34590ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
34601da177e4SLinus Torvalds }
34611da177e4SLinus Torvalds 
34621da177e4SLinus Torvalds enum addr_type_t
34631da177e4SLinus Torvalds {
34641da177e4SLinus Torvalds 	UNICAST_ADDR,
34651da177e4SLinus Torvalds 	MULTICAST_ADDR,
34661da177e4SLinus Torvalds 	ANYCAST_ADDR,
34671da177e4SLinus Torvalds };
34681da177e4SLinus Torvalds 
34691da177e4SLinus Torvalds static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
34701da177e4SLinus Torvalds 			   enum addr_type_t type)
34711da177e4SLinus Torvalds {
34721da177e4SLinus Torvalds 	int idx, ip_idx;
34731da177e4SLinus Torvalds 	int s_idx, s_ip_idx;
34741da177e4SLinus Torvalds 	int err = 1;
34751da177e4SLinus Torvalds 	struct net_device *dev;
34761da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
34771da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
34781da177e4SLinus Torvalds 	struct ifmcaddr6 *ifmca;
34791da177e4SLinus Torvalds 	struct ifacaddr6 *ifaca;
34803b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
34811da177e4SLinus Torvalds 
34821da177e4SLinus Torvalds 	s_idx = cb->args[0];
34831da177e4SLinus Torvalds 	s_ip_idx = ip_idx = cb->args[1];
34841da177e4SLinus Torvalds 
34857562f876SPavel Emelianov 	idx = 0;
34866fda7350SBenjamin Thery 	for_each_netdev(net, dev) {
34871da177e4SLinus Torvalds 		if (idx < s_idx)
34887562f876SPavel Emelianov 			goto cont;
34891da177e4SLinus Torvalds 		if (idx > s_idx)
34901da177e4SLinus Torvalds 			s_ip_idx = 0;
34911da177e4SLinus Torvalds 		ip_idx = 0;
34921da177e4SLinus Torvalds 		if ((idev = in6_dev_get(dev)) == NULL)
34937562f876SPavel Emelianov 			goto cont;
34941da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
34951da177e4SLinus Torvalds 		switch (type) {
34961da177e4SLinus Torvalds 		case UNICAST_ADDR:
3497ae9cda5dSYOSHIFUJI Hideaki 			/* unicast address incl. temp addr */
34981da177e4SLinus Torvalds 			for (ifa = idev->addr_list; ifa;
34991da177e4SLinus Torvalds 			     ifa = ifa->if_next, ip_idx++) {
35001da177e4SLinus Torvalds 				if (ip_idx < s_ip_idx)
35011da177e4SLinus Torvalds 					continue;
35025d5619b4SYOSHIFUJI Hideaki 				err = inet6_fill_ifaddr(skb, ifa,
35031da177e4SLinus Torvalds 							NETLINK_CB(cb->skb).pid,
35045d5619b4SYOSHIFUJI Hideaki 							cb->nlh->nlmsg_seq,
35055d5619b4SYOSHIFUJI Hideaki 							RTM_NEWADDR,
35065d5619b4SYOSHIFUJI Hideaki 							NLM_F_MULTI);
35071da177e4SLinus Torvalds 			}
35081da177e4SLinus Torvalds 			break;
35091da177e4SLinus Torvalds 		case MULTICAST_ADDR:
35101da177e4SLinus Torvalds 			/* multicast address */
35111da177e4SLinus Torvalds 			for (ifmca = idev->mc_list; ifmca;
35121da177e4SLinus Torvalds 			     ifmca = ifmca->next, ip_idx++) {
35131da177e4SLinus Torvalds 				if (ip_idx < s_ip_idx)
35141da177e4SLinus Torvalds 					continue;
35155d5619b4SYOSHIFUJI Hideaki 				err = inet6_fill_ifmcaddr(skb, ifmca,
35161da177e4SLinus Torvalds 							  NETLINK_CB(cb->skb).pid,
35175d5619b4SYOSHIFUJI Hideaki 							  cb->nlh->nlmsg_seq,
35185d5619b4SYOSHIFUJI Hideaki 							  RTM_GETMULTICAST,
35195d5619b4SYOSHIFUJI Hideaki 							  NLM_F_MULTI);
35201da177e4SLinus Torvalds 			}
35211da177e4SLinus Torvalds 			break;
35221da177e4SLinus Torvalds 		case ANYCAST_ADDR:
35231da177e4SLinus Torvalds 			/* anycast address */
35241da177e4SLinus Torvalds 			for (ifaca = idev->ac_list; ifaca;
35251da177e4SLinus Torvalds 			     ifaca = ifaca->aca_next, ip_idx++) {
35261da177e4SLinus Torvalds 				if (ip_idx < s_ip_idx)
35271da177e4SLinus Torvalds 					continue;
35285d5619b4SYOSHIFUJI Hideaki 				err = inet6_fill_ifacaddr(skb, ifaca,
35291da177e4SLinus Torvalds 							  NETLINK_CB(cb->skb).pid,
35305d5619b4SYOSHIFUJI Hideaki 							  cb->nlh->nlmsg_seq,
35315d5619b4SYOSHIFUJI Hideaki 							  RTM_GETANYCAST,
35325d5619b4SYOSHIFUJI Hideaki 							  NLM_F_MULTI);
35331da177e4SLinus Torvalds 			}
35341da177e4SLinus Torvalds 			break;
35351da177e4SLinus Torvalds 		default:
35361da177e4SLinus Torvalds 			break;
35371da177e4SLinus Torvalds 		}
35381da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
35391da177e4SLinus Torvalds 		in6_dev_put(idev);
35405d5619b4SYOSHIFUJI Hideaki 
35415d5619b4SYOSHIFUJI Hideaki 		if (err <= 0)
35425d5619b4SYOSHIFUJI Hideaki 			break;
35437562f876SPavel Emelianov cont:
35447562f876SPavel Emelianov 		idx++;
35451da177e4SLinus Torvalds 	}
35461da177e4SLinus Torvalds 	cb->args[0] = idx;
35471da177e4SLinus Torvalds 	cb->args[1] = ip_idx;
35481da177e4SLinus Torvalds 	return skb->len;
35491da177e4SLinus Torvalds }
35501da177e4SLinus Torvalds 
35511da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
35521da177e4SLinus Torvalds {
35531da177e4SLinus Torvalds 	enum addr_type_t type = UNICAST_ADDR;
3554b854272bSDenis V. Lunev 
35551da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
35561da177e4SLinus Torvalds }
35571da177e4SLinus Torvalds 
35581da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
35591da177e4SLinus Torvalds {
35601da177e4SLinus Torvalds 	enum addr_type_t type = MULTICAST_ADDR;
3561b854272bSDenis V. Lunev 
35621da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
35631da177e4SLinus Torvalds }
35641da177e4SLinus Torvalds 
35651da177e4SLinus Torvalds 
35661da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
35671da177e4SLinus Torvalds {
35681da177e4SLinus Torvalds 	enum addr_type_t type = ANYCAST_ADDR;
3569b854272bSDenis V. Lunev 
35701da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
35711da177e4SLinus Torvalds }
35721da177e4SLinus Torvalds 
35731b29fc2cSThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
35741b29fc2cSThomas Graf 			     void *arg)
35756c223828SNoriaki TAKAMIYA {
35763b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
35771b29fc2cSThomas Graf 	struct ifaddrmsg *ifm;
35781b29fc2cSThomas Graf 	struct nlattr *tb[IFA_MAX+1];
35796c223828SNoriaki TAKAMIYA 	struct in6_addr *addr = NULL;
35806c223828SNoriaki TAKAMIYA 	struct net_device *dev = NULL;
35816c223828SNoriaki TAKAMIYA 	struct inet6_ifaddr *ifa;
35826c223828SNoriaki TAKAMIYA 	struct sk_buff *skb;
35836c223828SNoriaki TAKAMIYA 	int err;
35846c223828SNoriaki TAKAMIYA 
35851b29fc2cSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
35861b29fc2cSThomas Graf 	if (err < 0)
35871b29fc2cSThomas Graf 		goto errout;
35886c223828SNoriaki TAKAMIYA 
35891b29fc2cSThomas Graf 	addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
35901b29fc2cSThomas Graf 	if (addr == NULL) {
35911b29fc2cSThomas Graf 		err = -EINVAL;
35921b29fc2cSThomas Graf 		goto errout;
35931b29fc2cSThomas Graf 	}
35941b29fc2cSThomas Graf 
35951b29fc2cSThomas Graf 	ifm = nlmsg_data(nlh);
35966c223828SNoriaki TAKAMIYA 	if (ifm->ifa_index)
35976fda7350SBenjamin Thery 		dev = __dev_get_by_index(net, ifm->ifa_index);
35986c223828SNoriaki TAKAMIYA 
35991cab3da6SDaniel Lezcano 	if ((ifa = ipv6_get_ifaddr(net, addr, dev, 1)) == NULL) {
36001b29fc2cSThomas Graf 		err = -EADDRNOTAVAIL;
36011b29fc2cSThomas Graf 		goto errout;
36026c223828SNoriaki TAKAMIYA 	}
36036c223828SNoriaki TAKAMIYA 
36040ab6803bSThomas Graf 	if ((skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL)) == NULL) {
36051b29fc2cSThomas Graf 		err = -ENOBUFS;
36061b29fc2cSThomas Graf 		goto errout_ifa;
36071b29fc2cSThomas Graf 	}
36081b29fc2cSThomas Graf 
36096c223828SNoriaki TAKAMIYA 	err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
36106c223828SNoriaki TAKAMIYA 				nlh->nlmsg_seq, RTM_NEWADDR, 0);
361126932566SPatrick McHardy 	if (err < 0) {
361226932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
361326932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
361426932566SPatrick McHardy 		kfree_skb(skb);
361526932566SPatrick McHardy 		goto errout_ifa;
361626932566SPatrick McHardy 	}
36176fda7350SBenjamin Thery 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
36181b29fc2cSThomas Graf errout_ifa:
36196c223828SNoriaki TAKAMIYA 	in6_ifa_put(ifa);
36201b29fc2cSThomas Graf errout:
36216c223828SNoriaki TAKAMIYA 	return err;
36226c223828SNoriaki TAKAMIYA }
36236c223828SNoriaki TAKAMIYA 
36241da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
36251da177e4SLinus Torvalds {
36261da177e4SLinus Torvalds 	struct sk_buff *skb;
3627c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(ifa->idev->dev);
36285d620266SThomas Graf 	int err = -ENOBUFS;
36291da177e4SLinus Torvalds 
36300ab6803bSThomas Graf 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
36315d620266SThomas Graf 	if (skb == NULL)
36325d620266SThomas Graf 		goto errout;
36335d620266SThomas Graf 
36345d620266SThomas Graf 	err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
363526932566SPatrick McHardy 	if (err < 0) {
363626932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
363726932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
363826932566SPatrick McHardy 		kfree_skb(skb);
363926932566SPatrick McHardy 		goto errout;
364026932566SPatrick McHardy 	}
3641*1ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3642*1ce85fe4SPablo Neira Ayuso 	return;
36435d620266SThomas Graf errout:
36445d620266SThomas Graf 	if (err < 0)
36456fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
36461da177e4SLinus Torvalds }
36471da177e4SLinus Torvalds 
3648b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
36491da177e4SLinus Torvalds 				__s32 *array, int bytes)
36501da177e4SLinus Torvalds {
365104561c1fSThomas Graf 	BUG_ON(bytes < (DEVCONF_MAX * 4));
365204561c1fSThomas Graf 
36531da177e4SLinus Torvalds 	memset(array, 0, bytes);
36541da177e4SLinus Torvalds 	array[DEVCONF_FORWARDING] = cnf->forwarding;
36551da177e4SLinus Torvalds 	array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
36561da177e4SLinus Torvalds 	array[DEVCONF_MTU6] = cnf->mtu6;
36571da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
36581da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
36591da177e4SLinus Torvalds 	array[DEVCONF_AUTOCONF] = cnf->autoconf;
36601da177e4SLinus Torvalds 	array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
36611da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
36621da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
36631da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
36641da177e4SLinus Torvalds 	array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
36651da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
36661da177e4SLinus Torvalds 	array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
36671da177e4SLinus Torvalds 	array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
36681da177e4SLinus Torvalds 	array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
36691da177e4SLinus Torvalds 	array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
36701da177e4SLinus Torvalds 	array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
36711da177e4SLinus Torvalds #endif
36721da177e4SLinus Torvalds 	array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
367365f5c7c1SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
3674c4fd30ebSYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
3675930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
3676930d6ff2SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
367752e16356SYOSHIFUJI Hideaki 	array[DEVCONF_RTR_PROBE_INTERVAL] = cnf->rtr_probe_interval;
3678fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
367909c884d4SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
368009c884d4SYOSHIFUJI Hideaki #endif
3681930d6ff2SYOSHIFUJI Hideaki #endif
3682fbea49e1SYOSHIFUJI Hideaki 	array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
36830bcbc926SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
368495c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
368595c385b4SNeil Horman 	array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
368695c385b4SNeil Horman #endif
36877bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
36887bc570c8SYOSHIFUJI Hideaki 	array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
36897bc570c8SYOSHIFUJI Hideaki #endif
3690778d80beSYOSHIFUJI Hideaki 	array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
36911b34be74SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
36921da177e4SLinus Torvalds }
36931da177e4SLinus Torvalds 
3694339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void)
3695339bf98fSThomas Graf {
3696339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3697339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3698339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3699339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
3700339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
3701339bf98fSThomas Graf 	       + nla_total_size( /* IFLA_PROTINFO */
3702339bf98fSThomas Graf 			nla_total_size(4) /* IFLA_INET6_FLAGS */
3703339bf98fSThomas Graf 			+ nla_total_size(sizeof(struct ifla_cacheinfo))
3704339bf98fSThomas Graf 			+ nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
3705bf99f1bdSYOSHIFUJI Hideaki 			+ nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
3706bf99f1bdSYOSHIFUJI Hideaki 			+ nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
3707339bf98fSThomas Graf 		 );
3708339bf98fSThomas Graf }
3709c5396a31SYOSHIFUJI Hideaki 
37107f7d9a6bSHerbert Xu static inline void __snmp6_fill_stats(u64 *stats, void **mib, int items,
37117f7d9a6bSHerbert Xu 				      int bytes)
37127f7d9a6bSHerbert Xu {
37137f7d9a6bSHerbert Xu 	int i;
37147f7d9a6bSHerbert Xu 	int pad = bytes - sizeof(u64) * items;
37157f7d9a6bSHerbert Xu 	BUG_ON(pad < 0);
37167f7d9a6bSHerbert Xu 
37177f7d9a6bSHerbert Xu 	/* Use put_unaligned() because stats may not be aligned for u64. */
37187f7d9a6bSHerbert Xu 	put_unaligned(items, &stats[0]);
37197f7d9a6bSHerbert Xu 	for (i = 1; i < items; i++)
37207f7d9a6bSHerbert Xu 		put_unaligned(snmp_fold_field(mib, i), &stats[i]);
37217f7d9a6bSHerbert Xu 
37227f7d9a6bSHerbert Xu 	memset(&stats[items], 0, pad);
37237f7d9a6bSHerbert Xu }
37247f7d9a6bSHerbert Xu 
37257f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
37267f7d9a6bSHerbert Xu 			     int bytes)
37277f7d9a6bSHerbert Xu {
37287f7d9a6bSHerbert Xu 	switch(attrtype) {
37297f7d9a6bSHerbert Xu 	case IFLA_INET6_STATS:
37307f7d9a6bSHerbert Xu 		__snmp6_fill_stats(stats, (void **)idev->stats.ipv6, IPSTATS_MIB_MAX, bytes);
37317f7d9a6bSHerbert Xu 		break;
37327f7d9a6bSHerbert Xu 	case IFLA_INET6_ICMP6STATS:
37337f7d9a6bSHerbert Xu 		__snmp6_fill_stats(stats, (void **)idev->stats.icmpv6, ICMP6_MIB_MAX, bytes);
37347f7d9a6bSHerbert Xu 		break;
37357f7d9a6bSHerbert Xu 	}
37367f7d9a6bSHerbert Xu }
37377f7d9a6bSHerbert Xu 
37381da177e4SLinus Torvalds static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
3739b6544c0bSJamal Hadi Salim 			     u32 pid, u32 seq, int event, unsigned int flags)
37401da177e4SLinus Torvalds {
37411da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
3742bf99f1bdSYOSHIFUJI Hideaki 	struct nlattr *nla;
374304561c1fSThomas Graf 	struct ifinfomsg *hdr;
37441da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
374504561c1fSThomas Graf 	void *protoinfo;
37461da177e4SLinus Torvalds 	struct ifla_cacheinfo ci;
37471da177e4SLinus Torvalds 
374804561c1fSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
374904561c1fSThomas Graf 	if (nlh == NULL)
375026932566SPatrick McHardy 		return -EMSGSIZE;
37511da177e4SLinus Torvalds 
375204561c1fSThomas Graf 	hdr = nlmsg_data(nlh);
375304561c1fSThomas Graf 	hdr->ifi_family = AF_INET6;
375404561c1fSThomas Graf 	hdr->__ifi_pad = 0;
375504561c1fSThomas Graf 	hdr->ifi_type = dev->type;
375604561c1fSThomas Graf 	hdr->ifi_index = dev->ifindex;
375704561c1fSThomas Graf 	hdr->ifi_flags = dev_get_flags(dev);
375804561c1fSThomas Graf 	hdr->ifi_change = 0;
375904561c1fSThomas Graf 
376004561c1fSThomas Graf 	NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
37611da177e4SLinus Torvalds 
37621da177e4SLinus Torvalds 	if (dev->addr_len)
376304561c1fSThomas Graf 		NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
37641da177e4SLinus Torvalds 
376504561c1fSThomas Graf 	NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
37661da177e4SLinus Torvalds 	if (dev->ifindex != dev->iflink)
376704561c1fSThomas Graf 		NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
37681da177e4SLinus Torvalds 
376904561c1fSThomas Graf 	protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
377004561c1fSThomas Graf 	if (protoinfo == NULL)
377104561c1fSThomas Graf 		goto nla_put_failure;
37721da177e4SLinus Torvalds 
377304561c1fSThomas Graf 	NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags);
37741da177e4SLinus Torvalds 
37751da177e4SLinus Torvalds 	ci.max_reasm_len = IPV6_MAXPLEN;
37761da177e4SLinus Torvalds 	ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
37771da177e4SLinus Torvalds 		    + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
37781da177e4SLinus Torvalds 	ci.reachable_time = idev->nd_parms->reachable_time;
37791da177e4SLinus Torvalds 	ci.retrans_time = idev->nd_parms->retrans_time;
378004561c1fSThomas Graf 	NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
37811da177e4SLinus Torvalds 
3782bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
3783bf99f1bdSYOSHIFUJI Hideaki 	if (nla == NULL)
378404561c1fSThomas Graf 		goto nla_put_failure;
3785bf99f1bdSYOSHIFUJI Hideaki 	ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
37861da177e4SLinus Torvalds 
3787bf99f1bdSYOSHIFUJI Hideaki 	/* XXX - MC not implemented */
3788bf99f1bdSYOSHIFUJI Hideaki 
3789bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
3790bf99f1bdSYOSHIFUJI Hideaki 	if (nla == NULL)
3791bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
3792bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
3793bf99f1bdSYOSHIFUJI Hideaki 
3794bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
3795bf99f1bdSYOSHIFUJI Hideaki 	if (nla == NULL)
3796bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
3797bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
37981da177e4SLinus Torvalds 
379904561c1fSThomas Graf 	nla_nest_end(skb, protoinfo);
380004561c1fSThomas Graf 	return nlmsg_end(skb, nlh);
38011da177e4SLinus Torvalds 
380204561c1fSThomas Graf nla_put_failure:
380326932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
380426932566SPatrick McHardy 	return -EMSGSIZE;
38051da177e4SLinus Torvalds }
38061da177e4SLinus Torvalds 
38071da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
38081da177e4SLinus Torvalds {
38093b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
38101da177e4SLinus Torvalds 	int idx, err;
38111da177e4SLinus Torvalds 	int s_idx = cb->args[0];
38121da177e4SLinus Torvalds 	struct net_device *dev;
38131da177e4SLinus Torvalds 	struct inet6_dev *idev;
38141da177e4SLinus Torvalds 
38151da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
38167562f876SPavel Emelianov 	idx = 0;
38176fda7350SBenjamin Thery 	for_each_netdev(net, dev) {
38181da177e4SLinus Torvalds 		if (idx < s_idx)
38197562f876SPavel Emelianov 			goto cont;
38201da177e4SLinus Torvalds 		if ((idev = in6_dev_get(dev)) == NULL)
38217562f876SPavel Emelianov 			goto cont;
38221da177e4SLinus Torvalds 		err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
3823b6544c0bSJamal Hadi Salim 				cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
38241da177e4SLinus Torvalds 		in6_dev_put(idev);
38251da177e4SLinus Torvalds 		if (err <= 0)
38261da177e4SLinus Torvalds 			break;
38277562f876SPavel Emelianov cont:
38287562f876SPavel Emelianov 		idx++;
38291da177e4SLinus Torvalds 	}
38301da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
38311da177e4SLinus Torvalds 	cb->args[0] = idx;
38321da177e4SLinus Torvalds 
38331da177e4SLinus Torvalds 	return skb->len;
38341da177e4SLinus Torvalds }
38351da177e4SLinus Torvalds 
38361da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
38371da177e4SLinus Torvalds {
38381da177e4SLinus Torvalds 	struct sk_buff *skb;
3839c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
38408d7a76c9SThomas Graf 	int err = -ENOBUFS;
38411da177e4SLinus Torvalds 
3842339bf98fSThomas Graf 	skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
38438d7a76c9SThomas Graf 	if (skb == NULL)
38448d7a76c9SThomas Graf 		goto errout;
38458d7a76c9SThomas Graf 
38468d7a76c9SThomas Graf 	err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
384726932566SPatrick McHardy 	if (err < 0) {
384826932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
384926932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
385026932566SPatrick McHardy 		kfree_skb(skb);
385126932566SPatrick McHardy 		goto errout;
385226932566SPatrick McHardy 	}
3853*1ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3854*1ce85fe4SPablo Neira Ayuso 	return;
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 	}
3924*1ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
3925*1ce85fe4SPablo Neira Ayuso 	return;
39268c384bfaSThomas Graf errout:
39278c384bfaSThomas Graf 	if (err < 0)
39286fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
39291da177e4SLinus Torvalds }
39301da177e4SLinus Torvalds 
39311da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
39321da177e4SLinus Torvalds {
39331da177e4SLinus Torvalds 	inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
39341da177e4SLinus Torvalds 
39351da177e4SLinus Torvalds 	switch (event) {
39361da177e4SLinus Torvalds 	case RTM_NEWADDR:
393795c385b4SNeil Horman 		/*
393895c385b4SNeil Horman 		 * If the address was optimistic
393995c385b4SNeil Horman 		 * we inserted the route at the start of
394095c385b4SNeil Horman 		 * our DAD process, so we don't need
394195c385b4SNeil Horman 		 * to do it again
394295c385b4SNeil Horman 		 */
394395c385b4SNeil Horman 		if (!(ifp->rt->rt6i_node))
394440e22e8fSThomas Graf 			ip6_ins_rt(ifp->rt);
39451da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
39461da177e4SLinus Torvalds 			addrconf_join_anycast(ifp);
39471da177e4SLinus Torvalds 		break;
39481da177e4SLinus Torvalds 	case RTM_DELADDR:
39491da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
39501da177e4SLinus Torvalds 			addrconf_leave_anycast(ifp);
39511da177e4SLinus Torvalds 		addrconf_leave_solict(ifp->idev, &ifp->addr);
39521da177e4SLinus Torvalds 		dst_hold(&ifp->rt->u.dst);
3953e0a1ad73SThomas Graf 		if (ip6_del_rt(ifp->rt))
39541da177e4SLinus Torvalds 			dst_free(&ifp->rt->u.dst);
39551da177e4SLinus Torvalds 		break;
39561da177e4SLinus Torvalds 	}
39571da177e4SLinus Torvalds }
39581da177e4SLinus Torvalds 
39591da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
39601da177e4SLinus Torvalds {
39618814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
39621da177e4SLinus Torvalds 	if (likely(ifp->idev->dead == 0))
39631da177e4SLinus Torvalds 		__ipv6_ifa_notify(event, ifp);
39648814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
39651da177e4SLinus Torvalds }
39661da177e4SLinus Torvalds 
39671da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
39681da177e4SLinus Torvalds 
39691da177e4SLinus Torvalds static
39701da177e4SLinus Torvalds int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
39711da177e4SLinus Torvalds 			   void __user *buffer, size_t *lenp, loff_t *ppos)
39721da177e4SLinus Torvalds {
39731da177e4SLinus Torvalds 	int *valp = ctl->data;
39741da177e4SLinus Torvalds 	int val = *valp;
39751da177e4SLinus Torvalds 	int ret;
39761da177e4SLinus Torvalds 
39771da177e4SLinus Torvalds 	ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
39781da177e4SLinus Torvalds 
3979c8fecf22SPavel Emelyanov 	if (write)
3980c8fecf22SPavel Emelyanov 		addrconf_fixup_forwarding(ctl, valp, val);
39811da177e4SLinus Torvalds 	return ret;
39821da177e4SLinus Torvalds }
39831da177e4SLinus Torvalds 
39841da177e4SLinus Torvalds static int addrconf_sysctl_forward_strategy(ctl_table *table,
39851da177e4SLinus Torvalds 					    void __user *oldval,
39861da177e4SLinus Torvalds 					    size_t __user *oldlenp,
39871f29bcd7SAlexey Dobriyan 					    void __user *newval, size_t newlen)
39881da177e4SLinus Torvalds {
39891da177e4SLinus Torvalds 	int *valp = table->data;
3990c8fecf22SPavel Emelyanov 	int val = *valp;
39911da177e4SLinus Torvalds 	int new;
39921da177e4SLinus Torvalds 
39931da177e4SLinus Torvalds 	if (!newval || !newlen)
39941da177e4SLinus Torvalds 		return 0;
39951da177e4SLinus Torvalds 	if (newlen != sizeof(int))
39961da177e4SLinus Torvalds 		return -EINVAL;
39971da177e4SLinus Torvalds 	if (get_user(new, (int __user *)newval))
39981da177e4SLinus Torvalds 		return -EFAULT;
39991da177e4SLinus Torvalds 	if (new == *valp)
40001da177e4SLinus Torvalds 		return 0;
40011da177e4SLinus Torvalds 	if (oldval && oldlenp) {
40021da177e4SLinus Torvalds 		size_t len;
40031da177e4SLinus Torvalds 		if (get_user(len, oldlenp))
40041da177e4SLinus Torvalds 			return -EFAULT;
40051da177e4SLinus Torvalds 		if (len) {
40061da177e4SLinus Torvalds 			if (len > table->maxlen)
40071da177e4SLinus Torvalds 				len = table->maxlen;
40081da177e4SLinus Torvalds 			if (copy_to_user(oldval, valp, len))
40091da177e4SLinus Torvalds 				return -EFAULT;
40101da177e4SLinus Torvalds 			if (put_user(len, oldlenp))
40111da177e4SLinus Torvalds 				return -EFAULT;
40121da177e4SLinus Torvalds 		}
40131da177e4SLinus Torvalds 	}
40141da177e4SLinus Torvalds 
40151da177e4SLinus Torvalds 	*valp = new;
4016c8fecf22SPavel Emelyanov 	addrconf_fixup_forwarding(table, valp, val);
40171da177e4SLinus Torvalds 	return 1;
40181da177e4SLinus Torvalds }
40191da177e4SLinus Torvalds 
40201da177e4SLinus Torvalds static struct addrconf_sysctl_table
40211da177e4SLinus Torvalds {
40221da177e4SLinus Torvalds 	struct ctl_table_header *sysctl_header;
4023f6a07b29SYOSHIFUJI Hideaki 	ctl_table addrconf_vars[DEVCONF_MAX+1];
40241dab6222SPavel Emelyanov 	char *dev_name;
4025ab32ea5dSBrian Haley } addrconf_sysctl __read_mostly = {
40261da177e4SLinus Torvalds 	.sysctl_header = NULL,
40271da177e4SLinus Torvalds 	.addrconf_vars = {
40281da177e4SLinus Torvalds 		{
40291da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_FORWARDING,
40301da177e4SLinus Torvalds 			.procname	=	"forwarding",
40311da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.forwarding,
40321da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40331da177e4SLinus Torvalds 			.mode		=	0644,
40346d9f239aSAlexey Dobriyan 			.proc_handler	=	addrconf_sysctl_forward,
40356d9f239aSAlexey Dobriyan 			.strategy	=	addrconf_sysctl_forward_strategy,
40361da177e4SLinus Torvalds 		},
40371da177e4SLinus Torvalds 		{
40381da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_HOP_LIMIT,
40391da177e4SLinus Torvalds 			.procname	=	"hop_limit",
40401da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.hop_limit,
40411da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40421da177e4SLinus Torvalds 			.mode		=	0644,
40431da177e4SLinus Torvalds 			.proc_handler	=	proc_dointvec,
40441da177e4SLinus Torvalds 		},
40451da177e4SLinus Torvalds 		{
40461da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_MTU,
40471da177e4SLinus Torvalds 			.procname	=	"mtu",
40481da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.mtu6,
40491da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40501da177e4SLinus Torvalds 			.mode		=	0644,
40516d9f239aSAlexey Dobriyan 			.proc_handler	=	proc_dointvec,
40521da177e4SLinus Torvalds 		},
40531da177e4SLinus Torvalds 		{
40541da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_ACCEPT_RA,
40551da177e4SLinus Torvalds 			.procname	=	"accept_ra",
40561da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.accept_ra,
40571da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40581da177e4SLinus Torvalds 			.mode		=	0644,
40596d9f239aSAlexey Dobriyan 			.proc_handler	=	proc_dointvec,
40601da177e4SLinus Torvalds 		},
40611da177e4SLinus Torvalds 		{
40621da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_ACCEPT_REDIRECTS,
40631da177e4SLinus Torvalds 			.procname	=	"accept_redirects",
40641da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.accept_redirects,
40651da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40661da177e4SLinus Torvalds 			.mode		=	0644,
40676d9f239aSAlexey Dobriyan 			.proc_handler	=	proc_dointvec,
40681da177e4SLinus Torvalds 		},
40691da177e4SLinus Torvalds 		{
40701da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_AUTOCONF,
40711da177e4SLinus Torvalds 			.procname	=	"autoconf",
40721da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.autoconf,
40731da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40741da177e4SLinus Torvalds 			.mode		=	0644,
40756d9f239aSAlexey Dobriyan 			.proc_handler	=	proc_dointvec,
40761da177e4SLinus Torvalds 		},
40771da177e4SLinus Torvalds 		{
40781da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_DAD_TRANSMITS,
40791da177e4SLinus Torvalds 			.procname	=	"dad_transmits",
40801da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.dad_transmits,
40811da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40821da177e4SLinus Torvalds 			.mode		=	0644,
40836d9f239aSAlexey Dobriyan 			.proc_handler	=	proc_dointvec,
40841da177e4SLinus Torvalds 		},
40851da177e4SLinus Torvalds 		{
40861da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_RTR_SOLICITS,
40871da177e4SLinus Torvalds 			.procname	=	"router_solicitations",
40881da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.rtr_solicits,
40891da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40901da177e4SLinus Torvalds 			.mode		=	0644,
40916d9f239aSAlexey Dobriyan 			.proc_handler	=	proc_dointvec,
40921da177e4SLinus Torvalds 		},
40931da177e4SLinus Torvalds 		{
40941da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_RTR_SOLICIT_INTERVAL,
40951da177e4SLinus Torvalds 			.procname	=	"router_solicitation_interval",
40961da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.rtr_solicit_interval,
40971da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
40981da177e4SLinus Torvalds 			.mode		=	0644,
40996d9f239aSAlexey Dobriyan 			.proc_handler	=	proc_dointvec_jiffies,
41006d9f239aSAlexey Dobriyan 			.strategy	=	sysctl_jiffies,
41011da177e4SLinus Torvalds 		},
41021da177e4SLinus Torvalds 		{
41031da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_RTR_SOLICIT_DELAY,
41041da177e4SLinus Torvalds 			.procname	=	"router_solicitation_delay",
41051da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.rtr_solicit_delay,
41061da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
41071da177e4SLinus Torvalds 			.mode		=	0644,
41086d9f239aSAlexey Dobriyan 			.proc_handler	=	proc_dointvec_jiffies,
41096d9f239aSAlexey Dobriyan 			.strategy	=	sysctl_jiffies,
41101da177e4SLinus Torvalds 		},
41111da177e4SLinus Torvalds 		{
41121da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_FORCE_MLD_VERSION,
41131da177e4SLinus Torvalds 			.procname	=	"force_mld_version",
41141da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.force_mld_version,
41151da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
41161da177e4SLinus Torvalds 			.mode		=	0644,
41176d9f239aSAlexey Dobriyan 			.proc_handler	=	proc_dointvec,
41181da177e4SLinus Torvalds 		},
41191da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
41201da177e4SLinus Torvalds 		{
41211da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_USE_TEMPADDR,
41221da177e4SLinus Torvalds 			.procname	=	"use_tempaddr",
41231da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.use_tempaddr,
41241da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
41251da177e4SLinus Torvalds 			.mode		=	0644,
41266d9f239aSAlexey Dobriyan 			.proc_handler	=	proc_dointvec,
41271da177e4SLinus Torvalds 		},
41281da177e4SLinus Torvalds 		{
41291da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_TEMP_VALID_LFT,
41301da177e4SLinus Torvalds 			.procname	=	"temp_valid_lft",
41311da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.temp_valid_lft,
41321da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
41331da177e4SLinus Torvalds 			.mode		=	0644,
41346d9f239aSAlexey Dobriyan 			.proc_handler	=	proc_dointvec,
41351da177e4SLinus Torvalds 		},
41361da177e4SLinus Torvalds 		{
41371da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_TEMP_PREFERED_LFT,
41381da177e4SLinus Torvalds 			.procname	=	"temp_prefered_lft",
41391da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.temp_prefered_lft,
41401da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
41411da177e4SLinus Torvalds 			.mode		=	0644,
41426d9f239aSAlexey Dobriyan 			.proc_handler	=	proc_dointvec,
41431da177e4SLinus Torvalds 		},
41441da177e4SLinus Torvalds 		{
41451da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_REGEN_MAX_RETRY,
41461da177e4SLinus Torvalds 			.procname	=	"regen_max_retry",
41471da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.regen_max_retry,
41481da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
41491da177e4SLinus Torvalds 			.mode		=	0644,
41506d9f239aSAlexey Dobriyan 			.proc_handler	=	proc_dointvec,
41511da177e4SLinus Torvalds 		},
41521da177e4SLinus Torvalds 		{
41531da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_MAX_DESYNC_FACTOR,
41541da177e4SLinus Torvalds 			.procname	=	"max_desync_factor",
41551da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.max_desync_factor,
41561da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
41571da177e4SLinus Torvalds 			.mode		=	0644,
41586d9f239aSAlexey Dobriyan 			.proc_handler	=	proc_dointvec,
41591da177e4SLinus Torvalds 		},
41601da177e4SLinus Torvalds #endif
41611da177e4SLinus Torvalds 		{
41621da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_MAX_ADDRESSES,
41631da177e4SLinus Torvalds 			.procname	=	"max_addresses",
41641da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.max_addresses,
41651da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
41661da177e4SLinus Torvalds 			.mode		=	0644,
41676d9f239aSAlexey Dobriyan 			.proc_handler	=	proc_dointvec,
41681da177e4SLinus Torvalds 		},
41691da177e4SLinus Torvalds 		{
417065f5c7c1SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_RA_DEFRTR,
417165f5c7c1SYOSHIFUJI Hideaki 			.procname	=	"accept_ra_defrtr",
417265f5c7c1SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_ra_defrtr,
417365f5c7c1SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
417465f5c7c1SYOSHIFUJI Hideaki 			.mode		=	0644,
41756d9f239aSAlexey Dobriyan 			.proc_handler	=	proc_dointvec,
417665f5c7c1SYOSHIFUJI Hideaki 		},
417765f5c7c1SYOSHIFUJI Hideaki 		{
4178c4fd30ebSYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_RA_PINFO,
4179c4fd30ebSYOSHIFUJI Hideaki 			.procname	=	"accept_ra_pinfo",
4180c4fd30ebSYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_ra_pinfo,
4181c4fd30ebSYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
4182c4fd30ebSYOSHIFUJI Hideaki 			.mode		=	0644,
41836d9f239aSAlexey Dobriyan 			.proc_handler	=	proc_dointvec,
4184c4fd30ebSYOSHIFUJI Hideaki 		},
4185930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
4186930d6ff2SYOSHIFUJI Hideaki 		{
4187930d6ff2SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_RA_RTR_PREF,
4188930d6ff2SYOSHIFUJI Hideaki 			.procname	=	"accept_ra_rtr_pref",
4189930d6ff2SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_ra_rtr_pref,
4190930d6ff2SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
4191930d6ff2SYOSHIFUJI Hideaki 			.mode		=	0644,
41926d9f239aSAlexey Dobriyan 			.proc_handler	=	proc_dointvec,
4193930d6ff2SYOSHIFUJI Hideaki 		},
419452e16356SYOSHIFUJI Hideaki 		{
419552e16356SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_RTR_PROBE_INTERVAL,
419652e16356SYOSHIFUJI Hideaki 			.procname	=	"router_probe_interval",
419752e16356SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.rtr_probe_interval,
419852e16356SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
419952e16356SYOSHIFUJI Hideaki 			.mode		=	0644,
42006d9f239aSAlexey Dobriyan 			.proc_handler	=	proc_dointvec_jiffies,
42016d9f239aSAlexey Dobriyan 			.strategy	=	sysctl_jiffies,
420252e16356SYOSHIFUJI Hideaki 		},
4203fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
420409c884d4SYOSHIFUJI Hideaki 		{
420509c884d4SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_RA_RT_INFO_MAX_PLEN,
420609c884d4SYOSHIFUJI Hideaki 			.procname	=	"accept_ra_rt_info_max_plen",
420709c884d4SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_ra_rt_info_max_plen,
420809c884d4SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
420909c884d4SYOSHIFUJI Hideaki 			.mode		=	0644,
42106d9f239aSAlexey Dobriyan 			.proc_handler	=	proc_dointvec,
421109c884d4SYOSHIFUJI Hideaki 		},
421209c884d4SYOSHIFUJI Hideaki #endif
4213930d6ff2SYOSHIFUJI Hideaki #endif
4214c4fd30ebSYOSHIFUJI Hideaki 		{
4215fbea49e1SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_PROXY_NDP,
4216fbea49e1SYOSHIFUJI Hideaki 			.procname	=	"proxy_ndp",
4217fbea49e1SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.proxy_ndp,
4218fbea49e1SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
4219fbea49e1SYOSHIFUJI Hideaki 			.mode		=	0644,
42206d9f239aSAlexey Dobriyan 			.proc_handler	=	proc_dointvec,
4221fbea49e1SYOSHIFUJI Hideaki 		},
4222fbea49e1SYOSHIFUJI Hideaki 		{
42230bcbc926SYOSHIFUJI Hideaki 			.ctl_name	=	NET_IPV6_ACCEPT_SOURCE_ROUTE,
42240bcbc926SYOSHIFUJI Hideaki 			.procname	=	"accept_source_route",
42250bcbc926SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_source_route,
42260bcbc926SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
42270bcbc926SYOSHIFUJI Hideaki 			.mode		=	0644,
42286d9f239aSAlexey Dobriyan 			.proc_handler	=	proc_dointvec,
42290bcbc926SYOSHIFUJI Hideaki 		},
423095c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
423195c385b4SNeil Horman 		{
423295c385b4SNeil Horman 			.ctl_name	=	CTL_UNNUMBERED,
423395c385b4SNeil Horman 			.procname       =       "optimistic_dad",
423495c385b4SNeil Horman 			.data           =       &ipv6_devconf.optimistic_dad,
423595c385b4SNeil Horman 			.maxlen         =       sizeof(int),
423695c385b4SNeil Horman 			.mode           =       0644,
42376d9f239aSAlexey Dobriyan 			.proc_handler   =       proc_dointvec,
423895c385b4SNeil Horman 
423995c385b4SNeil Horman 		},
424095c385b4SNeil Horman #endif
42417bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
42427bc570c8SYOSHIFUJI Hideaki 		{
42437bc570c8SYOSHIFUJI Hideaki 			.ctl_name	=	CTL_UNNUMBERED,
42447bc570c8SYOSHIFUJI Hideaki 			.procname	=	"mc_forwarding",
42457bc570c8SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.mc_forwarding,
42467bc570c8SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
4247a4e6db07SDavid S. Miller 			.mode		=	0444,
42486d9f239aSAlexey Dobriyan 			.proc_handler	=	proc_dointvec,
42497bc570c8SYOSHIFUJI Hideaki 		},
42507bc570c8SYOSHIFUJI Hideaki #endif
42510bcbc926SYOSHIFUJI Hideaki 		{
4252778d80beSYOSHIFUJI Hideaki 			.ctl_name	=	CTL_UNNUMBERED,
4253778d80beSYOSHIFUJI Hideaki 			.procname	=	"disable_ipv6",
4254778d80beSYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.disable_ipv6,
4255778d80beSYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
4256778d80beSYOSHIFUJI Hideaki 			.mode		=	0644,
42576d9f239aSAlexey Dobriyan 			.proc_handler	=	proc_dointvec,
4258778d80beSYOSHIFUJI Hideaki 		},
4259778d80beSYOSHIFUJI Hideaki 		{
42601b34be74SYOSHIFUJI Hideaki 			.ctl_name	=	CTL_UNNUMBERED,
42611b34be74SYOSHIFUJI Hideaki 			.procname	=	"accept_dad",
42621b34be74SYOSHIFUJI Hideaki 			.data		=	&ipv6_devconf.accept_dad,
42631b34be74SYOSHIFUJI Hideaki 			.maxlen		=	sizeof(int),
42641b34be74SYOSHIFUJI Hideaki 			.mode		=	0644,
42656d9f239aSAlexey Dobriyan 			.proc_handler	=	proc_dointvec,
42661b34be74SYOSHIFUJI Hideaki 		},
42671b34be74SYOSHIFUJI Hideaki 		{
42681da177e4SLinus Torvalds 			.ctl_name	=	0,	/* sentinel */
42691da177e4SLinus Torvalds 		}
42701da177e4SLinus Torvalds 	},
42711da177e4SLinus Torvalds };
42721da177e4SLinus Torvalds 
4273bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name,
4274bff16c2fSPavel Emelyanov 		int ctl_name, struct inet6_dev *idev, struct ipv6_devconf *p)
42751da177e4SLinus Torvalds {
42761da177e4SLinus Torvalds 	int i;
42771da177e4SLinus Torvalds 	struct addrconf_sysctl_table *t;
42781da177e4SLinus Torvalds 
42791dab6222SPavel Emelyanov #define ADDRCONF_CTL_PATH_DEV	3
42801dab6222SPavel Emelyanov 
42811dab6222SPavel Emelyanov 	struct ctl_path addrconf_ctl_path[] = {
42821dab6222SPavel Emelyanov 		{ .procname = "net", .ctl_name = CTL_NET, },
42831dab6222SPavel Emelyanov 		{ .procname = "ipv6", .ctl_name = NET_IPV6, },
42841dab6222SPavel Emelyanov 		{ .procname = "conf", .ctl_name = NET_IPV6_CONF, },
42851dab6222SPavel Emelyanov 		{ /* to be set */ },
42861dab6222SPavel Emelyanov 		{ },
42871dab6222SPavel Emelyanov 	};
42881dab6222SPavel Emelyanov 
42891dab6222SPavel Emelyanov 
4290af879cc7SArnaldo Carvalho de Melo 	t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
42911da177e4SLinus Torvalds 	if (t == NULL)
4292f68635e6SPavel Emelyanov 		goto out;
4293f68635e6SPavel Emelyanov 
42941da177e4SLinus Torvalds 	for (i=0; t->addrconf_vars[i].data; i++) {
42951da177e4SLinus Torvalds 		t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
42961da177e4SLinus Torvalds 		t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
4297bff16c2fSPavel Emelyanov 		t->addrconf_vars[i].extra2 = net;
42981da177e4SLinus Torvalds 	}
42991da177e4SLinus Torvalds 
43001da177e4SLinus Torvalds 	/*
43011da177e4SLinus Torvalds 	 * Make a copy of dev_name, because '.procname' is regarded as const
43021da177e4SLinus Torvalds 	 * by sysctl and we wouldn't want anyone to change it under our feet
43031da177e4SLinus Torvalds 	 * (see SIOCSIFNAME).
43041da177e4SLinus Torvalds 	 */
43051dab6222SPavel Emelyanov 	t->dev_name = kstrdup(dev_name, GFP_KERNEL);
43061dab6222SPavel Emelyanov 	if (!t->dev_name)
43071da177e4SLinus Torvalds 		goto free;
43081da177e4SLinus Torvalds 
43091dab6222SPavel Emelyanov 	addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].procname = t->dev_name;
43101dab6222SPavel Emelyanov 	addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].ctl_name = ctl_name;
43111da177e4SLinus Torvalds 
4312bff16c2fSPavel Emelyanov 	t->sysctl_header = register_net_sysctl_table(net, addrconf_ctl_path,
43131dab6222SPavel Emelyanov 			t->addrconf_vars);
43141da177e4SLinus Torvalds 	if (t->sysctl_header == NULL)
43151da177e4SLinus Torvalds 		goto free_procname;
4316f68635e6SPavel Emelyanov 
43171da177e4SLinus Torvalds 	p->sysctl = t;
431895897312SPavel Emelyanov 	return 0;
43191da177e4SLinus Torvalds 
43201da177e4SLinus Torvalds free_procname:
43211dab6222SPavel Emelyanov 	kfree(t->dev_name);
43221da177e4SLinus Torvalds free:
43231da177e4SLinus Torvalds 	kfree(t);
4324f68635e6SPavel Emelyanov out:
432595897312SPavel Emelyanov 	return -ENOBUFS;
43261da177e4SLinus Torvalds }
43271da177e4SLinus Torvalds 
4328408c4768SPavel Emelyanov static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
4329f52295a9SPavel Emelyanov {
4330408c4768SPavel Emelyanov 	struct addrconf_sysctl_table *t;
4331f52295a9SPavel Emelyanov 
4332408c4768SPavel Emelyanov 	if (p->sysctl == NULL)
4333408c4768SPavel Emelyanov 		return;
4334408c4768SPavel Emelyanov 
4335408c4768SPavel Emelyanov 	t = p->sysctl;
43361da177e4SLinus Torvalds 	p->sysctl = NULL;
43371da177e4SLinus Torvalds 	unregister_sysctl_table(t->sysctl_header);
43381dab6222SPavel Emelyanov 	kfree(t->dev_name);
43391da177e4SLinus Torvalds 	kfree(t);
43401da177e4SLinus Torvalds }
4341408c4768SPavel Emelyanov 
4342408c4768SPavel Emelyanov static void addrconf_sysctl_register(struct inet6_dev *idev)
4343408c4768SPavel Emelyanov {
4344408c4768SPavel Emelyanov 	neigh_sysctl_register(idev->dev, idev->nd_parms, NET_IPV6,
4345408c4768SPavel Emelyanov 			      NET_IPV6_NEIGH, "ipv6",
4346408c4768SPavel Emelyanov 			      &ndisc_ifinfo_sysctl_change,
43470686caa3SYOSHIFUJI Hideaki 			      ndisc_ifinfo_sysctl_strategy);
4348c346dca1SYOSHIFUJI Hideaki 	__addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
4349bff16c2fSPavel Emelyanov 			idev->dev->ifindex, idev, &idev->cnf);
4350408c4768SPavel Emelyanov }
4351408c4768SPavel Emelyanov 
4352408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev)
4353408c4768SPavel Emelyanov {
4354408c4768SPavel Emelyanov 	__addrconf_sysctl_unregister(&idev->cnf);
4355408c4768SPavel Emelyanov 	neigh_sysctl_unregister(idev->nd_parms);
43561da177e4SLinus Torvalds }
43571da177e4SLinus Torvalds 
43581da177e4SLinus Torvalds 
43591da177e4SLinus Torvalds #endif
43601da177e4SLinus Torvalds 
4361e0da5a48SPavel Emelyanov static int addrconf_init_net(struct net *net)
4362e0da5a48SPavel Emelyanov {
4363e0da5a48SPavel Emelyanov 	int err;
4364e0da5a48SPavel Emelyanov 	struct ipv6_devconf *all, *dflt;
4365e0da5a48SPavel Emelyanov 
4366e0da5a48SPavel Emelyanov 	err = -ENOMEM;
4367e0da5a48SPavel Emelyanov 	all = &ipv6_devconf;
4368e0da5a48SPavel Emelyanov 	dflt = &ipv6_devconf_dflt;
4369e0da5a48SPavel Emelyanov 
4370e0da5a48SPavel Emelyanov 	if (net != &init_net) {
4371e0da5a48SPavel Emelyanov 		all = kmemdup(all, sizeof(ipv6_devconf), GFP_KERNEL);
4372e0da5a48SPavel Emelyanov 		if (all == NULL)
4373e0da5a48SPavel Emelyanov 			goto err_alloc_all;
4374e0da5a48SPavel Emelyanov 
4375e0da5a48SPavel Emelyanov 		dflt = kmemdup(dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
4376e0da5a48SPavel Emelyanov 		if (dflt == NULL)
4377e0da5a48SPavel Emelyanov 			goto err_alloc_dflt;
4378e0da5a48SPavel Emelyanov 	}
4379e0da5a48SPavel Emelyanov 
4380e0da5a48SPavel Emelyanov 	net->ipv6.devconf_all = all;
4381e0da5a48SPavel Emelyanov 	net->ipv6.devconf_dflt = dflt;
4382e0da5a48SPavel Emelyanov 
4383e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
4384e0da5a48SPavel Emelyanov 	err = __addrconf_sysctl_register(net, "all", NET_PROTO_CONF_ALL,
4385e0da5a48SPavel Emelyanov 			NULL, all);
4386e0da5a48SPavel Emelyanov 	if (err < 0)
4387e0da5a48SPavel Emelyanov 		goto err_reg_all;
4388e0da5a48SPavel Emelyanov 
4389e0da5a48SPavel Emelyanov 	err = __addrconf_sysctl_register(net, "default", NET_PROTO_CONF_DEFAULT,
4390e0da5a48SPavel Emelyanov 			NULL, dflt);
4391e0da5a48SPavel Emelyanov 	if (err < 0)
4392e0da5a48SPavel Emelyanov 		goto err_reg_dflt;
4393e0da5a48SPavel Emelyanov #endif
4394e0da5a48SPavel Emelyanov 	return 0;
4395e0da5a48SPavel Emelyanov 
4396e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
4397e0da5a48SPavel Emelyanov err_reg_dflt:
4398e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(all);
4399e0da5a48SPavel Emelyanov err_reg_all:
4400e0da5a48SPavel Emelyanov 	kfree(dflt);
4401e0da5a48SPavel Emelyanov #endif
4402e0da5a48SPavel Emelyanov err_alloc_dflt:
4403e0da5a48SPavel Emelyanov 	kfree(all);
4404e0da5a48SPavel Emelyanov err_alloc_all:
4405e0da5a48SPavel Emelyanov 	return err;
4406e0da5a48SPavel Emelyanov }
4407e0da5a48SPavel Emelyanov 
4408e0da5a48SPavel Emelyanov static void addrconf_exit_net(struct net *net)
4409e0da5a48SPavel Emelyanov {
4410e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
4411e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
4412e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(net->ipv6.devconf_all);
4413e0da5a48SPavel Emelyanov #endif
4414e0da5a48SPavel Emelyanov 	if (net != &init_net) {
4415e0da5a48SPavel Emelyanov 		kfree(net->ipv6.devconf_dflt);
4416e0da5a48SPavel Emelyanov 		kfree(net->ipv6.devconf_all);
4417e0da5a48SPavel Emelyanov 	}
4418e0da5a48SPavel Emelyanov }
4419e0da5a48SPavel Emelyanov 
4420e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = {
4421e0da5a48SPavel Emelyanov 	.init = addrconf_init_net,
4422e0da5a48SPavel Emelyanov 	.exit = addrconf_exit_net,
4423e0da5a48SPavel Emelyanov };
4424e0da5a48SPavel Emelyanov 
44251da177e4SLinus Torvalds /*
44261da177e4SLinus Torvalds  *      Device notifier
44271da177e4SLinus Torvalds  */
44281da177e4SLinus Torvalds 
44291da177e4SLinus Torvalds int register_inet6addr_notifier(struct notifier_block *nb)
44301da177e4SLinus Torvalds {
4431e041c683SAlan Stern 	return atomic_notifier_chain_register(&inet6addr_chain, nb);
44321da177e4SLinus Torvalds }
44331da177e4SLinus Torvalds 
44347159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(register_inet6addr_notifier);
44357159039aSYOSHIFUJI Hideaki 
44361da177e4SLinus Torvalds int unregister_inet6addr_notifier(struct notifier_block *nb)
44371da177e4SLinus Torvalds {
4438e041c683SAlan Stern 	return atomic_notifier_chain_unregister(&inet6addr_chain,nb);
44391da177e4SLinus Torvalds }
44401da177e4SLinus Torvalds 
44417159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(unregister_inet6addr_notifier);
44427159039aSYOSHIFUJI Hideaki 
4443af284937SDaniel Lezcano static void addrconf_net_exit(struct net *net)
4444af284937SDaniel Lezcano {
44456fda7350SBenjamin Thery 	struct net_device *dev;
44466fda7350SBenjamin Thery 
44476fda7350SBenjamin Thery 	rtnl_lock();
44486fda7350SBenjamin Thery 	/* clean dev list */
44496fda7350SBenjamin Thery 	for_each_netdev(net, dev) {
44506fda7350SBenjamin Thery 		if (__in6_dev_get(dev) == NULL)
44516fda7350SBenjamin Thery 			continue;
44526fda7350SBenjamin Thery 		addrconf_ifdown(dev, 1);
44536fda7350SBenjamin Thery 	}
44546fda7350SBenjamin Thery 	addrconf_ifdown(net->loopback_dev, 2);
44556fda7350SBenjamin Thery 	rtnl_unlock();
4456af284937SDaniel Lezcano }
4457af284937SDaniel Lezcano 
4458af284937SDaniel Lezcano static struct pernet_operations addrconf_net_ops = {
4459af284937SDaniel Lezcano 	.exit = addrconf_net_exit,
4460af284937SDaniel Lezcano };
4461af284937SDaniel Lezcano 
44621da177e4SLinus Torvalds /*
44631da177e4SLinus Torvalds  *	Init / cleanup code
44641da177e4SLinus Torvalds  */
44651da177e4SLinus Torvalds 
44661da177e4SLinus Torvalds int __init addrconf_init(void)
44671da177e4SLinus Torvalds {
44682a8cc6c8SYOSHIFUJI Hideaki 	int err;
44692a8cc6c8SYOSHIFUJI Hideaki 
44702a8cc6c8SYOSHIFUJI Hideaki 	if ((err = ipv6_addr_label_init()) < 0) {
44712a8cc6c8SYOSHIFUJI Hideaki 		printk(KERN_CRIT "IPv6 Addrconf: cannot initialize default policy table: %d.\n",
44722a8cc6c8SYOSHIFUJI Hideaki 			err);
44732a8cc6c8SYOSHIFUJI Hideaki 		return err;
44742a8cc6c8SYOSHIFUJI Hideaki 	}
44751da177e4SLinus Torvalds 
4476e0da5a48SPavel Emelyanov 	register_pernet_subsys(&addrconf_ops);
4477e0da5a48SPavel Emelyanov 
44781da177e4SLinus Torvalds 	/* The addrconf netdev notifier requires that loopback_dev
44791da177e4SLinus Torvalds 	 * has it's ipv6 private information allocated and setup
44801da177e4SLinus Torvalds 	 * before it can bring up and give link-local addresses
44811da177e4SLinus Torvalds 	 * to other devices which are up.
44821da177e4SLinus Torvalds 	 *
44831da177e4SLinus Torvalds 	 * Unfortunately, loopback_dev is not necessarily the first
44841da177e4SLinus Torvalds 	 * entry in the global dev_base list of net devices.  In fact,
44851da177e4SLinus Torvalds 	 * it is likely to be the very last entry on that list.
44861da177e4SLinus Torvalds 	 * So this causes the notifier registry below to try and
44871da177e4SLinus Torvalds 	 * give link-local addresses to all devices besides loopback_dev
44881da177e4SLinus Torvalds 	 * first, then loopback_dev, which cases all the non-loopback_dev
44891da177e4SLinus Torvalds 	 * devices to fail to get a link-local address.
44901da177e4SLinus Torvalds 	 *
44911da177e4SLinus Torvalds 	 * So, as a temporary fix, allocate the ipv6 structure for
44921da177e4SLinus Torvalds 	 * loopback_dev first by hand.
44931da177e4SLinus Torvalds 	 * Longer term, all of the dependencies ipv6 has upon the loopback
44941da177e4SLinus Torvalds 	 * device and it being up should be removed.
44951da177e4SLinus Torvalds 	 */
44961da177e4SLinus Torvalds 	rtnl_lock();
44972774c7abSEric W. Biederman 	if (!ipv6_add_dev(init_net.loopback_dev))
44981da177e4SLinus Torvalds 		err = -ENOMEM;
44991da177e4SLinus Torvalds 	rtnl_unlock();
45001da177e4SLinus Torvalds 	if (err)
4501e0da5a48SPavel Emelyanov 		goto errlo;
45021da177e4SLinus Torvalds 
4503af284937SDaniel Lezcano 	err = register_pernet_device(&addrconf_net_ops);
4504af284937SDaniel Lezcano 	if (err)
4505af284937SDaniel Lezcano 		return err;
4506af284937SDaniel Lezcano 
45071da177e4SLinus Torvalds 	register_netdevice_notifier(&ipv6_dev_notf);
45081da177e4SLinus Torvalds 
45091da177e4SLinus Torvalds 	addrconf_verify(0);
4510c127ea2cSThomas Graf 
4511c127ea2cSThomas Graf 	err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo);
4512c127ea2cSThomas Graf 	if (err < 0)
4513c127ea2cSThomas Graf 		goto errout;
4514c127ea2cSThomas Graf 
4515c127ea2cSThomas Graf 	/* Only the first call to __rtnl_register can fail */
4516c127ea2cSThomas Graf 	__rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL);
4517c127ea2cSThomas Graf 	__rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL);
4518c127ea2cSThomas Graf 	__rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, inet6_dump_ifaddr);
4519c127ea2cSThomas Graf 	__rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, inet6_dump_ifmcaddr);
4520c127ea2cSThomas Graf 	__rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, inet6_dump_ifacaddr);
4521c127ea2cSThomas Graf 
45222a8cc6c8SYOSHIFUJI Hideaki 	ipv6_addr_label_rtnl_register();
45232a8cc6c8SYOSHIFUJI Hideaki 
45241da177e4SLinus Torvalds 	return 0;
4525c127ea2cSThomas Graf errout:
4526c127ea2cSThomas Graf 	unregister_netdevice_notifier(&ipv6_dev_notf);
4527e0da5a48SPavel Emelyanov errlo:
4528e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
4529c127ea2cSThomas Graf 
4530c127ea2cSThomas Graf 	return err;
45311da177e4SLinus Torvalds }
45321da177e4SLinus Torvalds 
453309f7709fSDaniel Lezcano void addrconf_cleanup(void)
45341da177e4SLinus Torvalds {
45351da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
45361da177e4SLinus Torvalds 	int i;
45371da177e4SLinus Torvalds 
45381da177e4SLinus Torvalds 	unregister_netdevice_notifier(&ipv6_dev_notf);
4539af284937SDaniel Lezcano 	unregister_pernet_device(&addrconf_net_ops);
45401da177e4SLinus Torvalds 
4541e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
45421da177e4SLinus Torvalds 
45431da177e4SLinus Torvalds 	rtnl_lock();
45441da177e4SLinus Torvalds 
45451da177e4SLinus Torvalds 	/*
45461da177e4SLinus Torvalds 	 *	Check hash table.
45471da177e4SLinus Torvalds 	 */
45481da177e4SLinus Torvalds 	write_lock_bh(&addrconf_hash_lock);
45491da177e4SLinus Torvalds 	for (i=0; i < IN6_ADDR_HSIZE; i++) {
45501da177e4SLinus Torvalds 		for (ifa=inet6_addr_lst[i]; ifa; ) {
45511da177e4SLinus Torvalds 			struct inet6_ifaddr *bifa;
45521da177e4SLinus Torvalds 
45531da177e4SLinus Torvalds 			bifa = ifa;
45541da177e4SLinus Torvalds 			ifa = ifa->lst_next;
45551da177e4SLinus Torvalds 			printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
45561da177e4SLinus Torvalds 			/* Do not free it; something is wrong.
45571da177e4SLinus Torvalds 			   Now we can investigate it with debugger.
45581da177e4SLinus Torvalds 			 */
45591da177e4SLinus Torvalds 		}
45601da177e4SLinus Torvalds 	}
45611da177e4SLinus Torvalds 	write_unlock_bh(&addrconf_hash_lock);
45621da177e4SLinus Torvalds 
45631da177e4SLinus Torvalds 	del_timer(&addr_chk_timer);
45641da177e4SLinus Torvalds 	rtnl_unlock();
4565af284937SDaniel Lezcano 
4566af284937SDaniel Lezcano 	unregister_pernet_subsys(&addrconf_net_ops);
45671da177e4SLinus Torvalds }
4568