xref: /openbmc/linux/net/ipv6/addrconf.c (revision 9343e79a7bb2d3268d68997163608b87d58d8098)
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  *	$Id: addrconf.c,v 1.69 2001/10/31 21:55:54 davem Exp $
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
121da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
131da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
141da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds /*
181da177e4SLinus Torvalds  *	Changes:
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  *	Janos Farkas			:	delete timer on ifdown
211da177e4SLinus Torvalds  *	<chexum@bankinf.banki.hu>
221da177e4SLinus Torvalds  *	Andi Kleen			:	kill double kfree on module
231da177e4SLinus Torvalds  *						unload.
241da177e4SLinus Torvalds  *	Maciej W. Rozycki		:	FDDI support
251da177e4SLinus Torvalds  *	sekiya@USAGI			:	Don't send too many RS
261da177e4SLinus Torvalds  *						packets.
271da177e4SLinus Torvalds  *	yoshfuji@USAGI			:       Fixed interval between DAD
281da177e4SLinus Torvalds  *						packets.
291da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	improved accuracy of
301da177e4SLinus Torvalds  *						address validation timer.
311da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	Privacy Extensions (RFC3041)
321da177e4SLinus Torvalds  *						support.
331da177e4SLinus Torvalds  *	Yuji SEKIYA @USAGI		:	Don't assign a same IPv6
341da177e4SLinus Torvalds  *						address on a same interface.
351da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	ARCnet support
361da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	convert /proc/net/if_inet6 to
371da177e4SLinus Torvalds  *						seq_file.
38b1cacb68SYOSHIFUJI Hideaki  *	YOSHIFUJI Hideaki @USAGI	:	improved source address
39b1cacb68SYOSHIFUJI Hideaki  *						selection; consider scope,
40b1cacb68SYOSHIFUJI Hideaki  *						status etc.
411da177e4SLinus Torvalds  */
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #include <linux/config.h>
441da177e4SLinus Torvalds #include <linux/errno.h>
451da177e4SLinus Torvalds #include <linux/types.h>
461da177e4SLinus Torvalds #include <linux/socket.h>
471da177e4SLinus Torvalds #include <linux/sockios.h>
481da177e4SLinus Torvalds #include <linux/sched.h>
491da177e4SLinus Torvalds #include <linux/net.h>
501da177e4SLinus Torvalds #include <linux/in6.h>
511da177e4SLinus Torvalds #include <linux/netdevice.h>
521da177e4SLinus Torvalds #include <linux/if_arp.h>
531da177e4SLinus Torvalds #include <linux/if_arcnet.h>
541da177e4SLinus Torvalds #include <linux/if_infiniband.h>
551da177e4SLinus Torvalds #include <linux/route.h>
561da177e4SLinus Torvalds #include <linux/inetdevice.h>
571da177e4SLinus Torvalds #include <linux/init.h>
581da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
591da177e4SLinus Torvalds #include <linux/sysctl.h>
601da177e4SLinus Torvalds #endif
614fc268d2SRandy Dunlap #include <linux/capability.h>
621da177e4SLinus Torvalds #include <linux/delay.h>
631da177e4SLinus Torvalds #include <linux/notifier.h>
64543537bdSPaulo Marques #include <linux/string.h>
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds #include <net/sock.h>
671da177e4SLinus Torvalds #include <net/snmp.h>
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #include <net/ipv6.h>
701da177e4SLinus Torvalds #include <net/protocol.h>
711da177e4SLinus Torvalds #include <net/ndisc.h>
721da177e4SLinus Torvalds #include <net/ip6_route.h>
731da177e4SLinus Torvalds #include <net/addrconf.h>
741da177e4SLinus Torvalds #include <net/tcp.h>
751da177e4SLinus Torvalds #include <net/ip.h>
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 #include <linux/crypto.h>
82378f058cSDavid Hardeman #include <linux/scatterlist.h>
831da177e4SLinus Torvalds #endif
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds #include <asm/uaccess.h>
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds #include <linux/proc_fs.h>
881da177e4SLinus Torvalds #include <linux/seq_file.h>
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds /* Set to 3 to get tracing... */
911da177e4SLinus Torvalds #define ACONF_DEBUG 2
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds #if ACONF_DEBUG >= 3
941da177e4SLinus Torvalds #define ADBG(x) printk x
951da177e4SLinus Torvalds #else
961da177e4SLinus Torvalds #define ADBG(x)
971da177e4SLinus Torvalds #endif
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds #define	INFINITY_LIFE_TIME	0xFFFFFFFF
1001da177e4SLinus Torvalds #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
1031da177e4SLinus Torvalds static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p);
1041da177e4SLinus Torvalds static void addrconf_sysctl_unregister(struct ipv6_devconf *p);
1051da177e4SLinus Torvalds #endif
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
1081da177e4SLinus Torvalds static int __ipv6_regen_rndid(struct inet6_dev *idev);
1091da177e4SLinus Torvalds static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
1101da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data);
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds static int desync_factor = MAX_DESYNC_FACTOR * HZ;
1131da177e4SLinus Torvalds static struct crypto_tfm *md5_tfm;
1141da177e4SLinus Torvalds static DEFINE_SPINLOCK(md5_tfm_lock);
1151da177e4SLinus Torvalds #endif
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev);
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds /*
1201da177e4SLinus Torvalds  *	Configured unicast address hash table
1211da177e4SLinus Torvalds  */
1221da177e4SLinus Torvalds static struct inet6_ifaddr		*inet6_addr_lst[IN6_ADDR_HSIZE];
1231da177e4SLinus Torvalds static DEFINE_RWLOCK(addrconf_hash_lock);
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds /* Protects inet6 devices */
1261da177e4SLinus Torvalds DEFINE_RWLOCK(addrconf_lock);
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds static void addrconf_verify(unsigned long);
1291da177e4SLinus Torvalds 
1308d06afabSIngo Molnar static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
1311da177e4SLinus Torvalds static DEFINE_SPINLOCK(addrconf_verify_lock);
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
1341da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how);
1371da177e4SLinus Torvalds 
138e431b8c0SJamal Hadi Salim static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
1391da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data);
1401da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
141c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev);
1421da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data);
1431da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1441da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
1471da177e4SLinus Torvalds 				struct prefix_info *pinfo);
1481da177e4SLinus Torvalds static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds static struct notifier_block *inet6addr_chain;
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds struct ipv6_devconf ipv6_devconf = {
1531da177e4SLinus Torvalds 	.forwarding		= 0,
1541da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
1551da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
1561da177e4SLinus Torvalds 	.accept_ra		= 1,
1571da177e4SLinus Torvalds 	.accept_redirects	= 1,
1581da177e4SLinus Torvalds 	.autoconf		= 1,
1591da177e4SLinus Torvalds 	.force_mld_version	= 0,
1601da177e4SLinus Torvalds 	.dad_transmits		= 1,
1611da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
1621da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
1631da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
1641da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
1651da177e4SLinus Torvalds 	.use_tempaddr 		= 0,
1661da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
1671da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
1681da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
1691da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
1701da177e4SLinus Torvalds #endif
1711da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
1721da177e4SLinus Torvalds };
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds static struct ipv6_devconf ipv6_devconf_dflt = {
1751da177e4SLinus Torvalds 	.forwarding		= 0,
1761da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
1771da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
1781da177e4SLinus Torvalds 	.accept_ra		= 1,
1791da177e4SLinus Torvalds 	.accept_redirects	= 1,
1801da177e4SLinus Torvalds 	.autoconf		= 1,
1811da177e4SLinus Torvalds 	.dad_transmits		= 1,
1821da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
1831da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
1841da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
1851da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
1861da177e4SLinus Torvalds 	.use_tempaddr		= 0,
1871da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
1881da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
1891da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
1901da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
1911da177e4SLinus Torvalds #endif
1921da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
1931da177e4SLinus Torvalds };
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
1961da177e4SLinus Torvalds #if 0
1971da177e4SLinus Torvalds const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
1981da177e4SLinus Torvalds #endif
1991da177e4SLinus Torvalds const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
2001da177e4SLinus Torvalds 
201b1cacb68SYOSHIFUJI Hideaki #define IPV6_ADDR_SCOPE_TYPE(scope)	((scope) << 16)
202b1cacb68SYOSHIFUJI Hideaki 
203b1cacb68SYOSHIFUJI Hideaki static inline unsigned ipv6_addr_scope2type(unsigned scope)
2041da177e4SLinus Torvalds {
205b1cacb68SYOSHIFUJI Hideaki 	switch(scope) {
206b1cacb68SYOSHIFUJI Hideaki 	case IPV6_ADDR_SCOPE_NODELOCAL:
207b1cacb68SYOSHIFUJI Hideaki 		return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_NODELOCAL) |
208b1cacb68SYOSHIFUJI Hideaki 			IPV6_ADDR_LOOPBACK);
209b1cacb68SYOSHIFUJI Hideaki 	case IPV6_ADDR_SCOPE_LINKLOCAL:
210b1cacb68SYOSHIFUJI Hideaki 		return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL) |
211b1cacb68SYOSHIFUJI Hideaki 			IPV6_ADDR_LINKLOCAL);
212b1cacb68SYOSHIFUJI Hideaki 	case IPV6_ADDR_SCOPE_SITELOCAL:
213b1cacb68SYOSHIFUJI Hideaki 		return (IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL) |
214b1cacb68SYOSHIFUJI Hideaki 			IPV6_ADDR_SITELOCAL);
215b1cacb68SYOSHIFUJI Hideaki 	}
216b1cacb68SYOSHIFUJI Hideaki 	return IPV6_ADDR_SCOPE_TYPE(scope);
217b1cacb68SYOSHIFUJI Hideaki }
218b1cacb68SYOSHIFUJI Hideaki 
219b1cacb68SYOSHIFUJI Hideaki int __ipv6_addr_type(const struct in6_addr *addr)
220b1cacb68SYOSHIFUJI Hideaki {
2211da177e4SLinus Torvalds 	u32 st;
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	st = addr->s6_addr32[0];
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	/* Consider all addresses with the first three bits different of
226b1cacb68SYOSHIFUJI Hideaki 	   000 and 111 as unicasts.
2271da177e4SLinus Torvalds 	 */
2281da177e4SLinus Torvalds 	if ((st & htonl(0xE0000000)) != htonl(0x00000000) &&
2291da177e4SLinus Torvalds 	    (st & htonl(0xE0000000)) != htonl(0xE0000000))
230b1cacb68SYOSHIFUJI Hideaki 		return (IPV6_ADDR_UNICAST |
231b1cacb68SYOSHIFUJI Hideaki 			IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL));
232b1cacb68SYOSHIFUJI Hideaki 
233b1cacb68SYOSHIFUJI Hideaki 	if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) {
234b1cacb68SYOSHIFUJI Hideaki 		/* multicast */
235b1cacb68SYOSHIFUJI Hideaki 		/* addr-select 3.1 */
236b1cacb68SYOSHIFUJI Hideaki 		return (IPV6_ADDR_MULTICAST |
237b1cacb68SYOSHIFUJI Hideaki 			ipv6_addr_scope2type(IPV6_ADDR_MC_SCOPE(addr)));
238b1cacb68SYOSHIFUJI Hideaki 	}
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	if ((st & htonl(0xFFC00000)) == htonl(0xFE800000))
241b1cacb68SYOSHIFUJI Hideaki 		return (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST |
242b1cacb68SYOSHIFUJI Hideaki 			IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL));		/* addr-select 3.1 */
2431da177e4SLinus Torvalds 	if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000))
244b1cacb68SYOSHIFUJI Hideaki 		return (IPV6_ADDR_SITELOCAL | IPV6_ADDR_UNICAST |
245b1cacb68SYOSHIFUJI Hideaki 			IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_SITELOCAL));		/* addr-select 3.1 */
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds 	if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) {
2481da177e4SLinus Torvalds 		if (addr->s6_addr32[2] == 0) {
2491da177e4SLinus Torvalds 			if (addr->s6_addr32[3] == 0)
2501da177e4SLinus Torvalds 				return IPV6_ADDR_ANY;
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 			if (addr->s6_addr32[3] == htonl(0x00000001))
253b1cacb68SYOSHIFUJI Hideaki 				return (IPV6_ADDR_LOOPBACK | IPV6_ADDR_UNICAST |
254b1cacb68SYOSHIFUJI Hideaki 					IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_LINKLOCAL));	/* addr-select 3.4 */
2551da177e4SLinus Torvalds 
256b1cacb68SYOSHIFUJI Hideaki 			return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST |
257b1cacb68SYOSHIFUJI Hideaki 				IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL));	/* addr-select 3.3 */
2581da177e4SLinus Torvalds 		}
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 		if (addr->s6_addr32[2] == htonl(0x0000ffff))
261b1cacb68SYOSHIFUJI Hideaki 			return (IPV6_ADDR_MAPPED |
262b1cacb68SYOSHIFUJI Hideaki 				IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL));	/* addr-select 3.3 */
2631da177e4SLinus Torvalds 	}
2641da177e4SLinus Torvalds 
265b1cacb68SYOSHIFUJI Hideaki 	return (IPV6_ADDR_RESERVED |
266b1cacb68SYOSHIFUJI Hideaki 		IPV6_ADDR_SCOPE_TYPE(IPV6_ADDR_SCOPE_GLOBAL));	/* addr-select 3.4 */
2671da177e4SLinus Torvalds }
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds static void addrconf_del_timer(struct inet6_ifaddr *ifp)
2701da177e4SLinus Torvalds {
2711da177e4SLinus Torvalds 	if (del_timer(&ifp->timer))
2721da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
2731da177e4SLinus Torvalds }
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds enum addrconf_timer_t
2761da177e4SLinus Torvalds {
2771da177e4SLinus Torvalds 	AC_NONE,
2781da177e4SLinus Torvalds 	AC_DAD,
2791da177e4SLinus Torvalds 	AC_RS,
2801da177e4SLinus Torvalds };
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
2831da177e4SLinus Torvalds 			       enum addrconf_timer_t what,
2841da177e4SLinus Torvalds 			       unsigned long when)
2851da177e4SLinus Torvalds {
2861da177e4SLinus Torvalds 	if (!del_timer(&ifp->timer))
2871da177e4SLinus Torvalds 		in6_ifa_hold(ifp);
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds 	switch (what) {
2901da177e4SLinus Torvalds 	case AC_DAD:
2911da177e4SLinus Torvalds 		ifp->timer.function = addrconf_dad_timer;
2921da177e4SLinus Torvalds 		break;
2931da177e4SLinus Torvalds 	case AC_RS:
2941da177e4SLinus Torvalds 		ifp->timer.function = addrconf_rs_timer;
2951da177e4SLinus Torvalds 		break;
2961da177e4SLinus Torvalds 	default:;
2971da177e4SLinus Torvalds 	}
2981da177e4SLinus Torvalds 	ifp->timer.expires = jiffies + when;
2991da177e4SLinus Torvalds 	add_timer(&ifp->timer);
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds /* Nobody refers to this device, we may destroy it. */
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds void in6_dev_finish_destroy(struct inet6_dev *idev)
3051da177e4SLinus Torvalds {
3061da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
3071da177e4SLinus Torvalds 	BUG_TRAP(idev->addr_list==NULL);
3081da177e4SLinus Torvalds 	BUG_TRAP(idev->mc_list==NULL);
3091da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
3101da177e4SLinus Torvalds 	printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
3111da177e4SLinus Torvalds #endif
3121da177e4SLinus Torvalds 	dev_put(dev);
3131da177e4SLinus Torvalds 	if (!idev->dead) {
3141da177e4SLinus Torvalds 		printk("Freeing alive inet6 device %p\n", idev);
3151da177e4SLinus Torvalds 		return;
3161da177e4SLinus Torvalds 	}
3171da177e4SLinus Torvalds 	snmp6_free_dev(idev);
3181da177e4SLinus Torvalds 	kfree(idev);
3191da177e4SLinus Torvalds }
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
3221da177e4SLinus Torvalds {
3231da177e4SLinus Torvalds 	struct inet6_dev *ndev;
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 	ASSERT_RTNL();
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds 	if (dev->mtu < IPV6_MIN_MTU)
3281da177e4SLinus Torvalds 		return NULL;
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	ndev = kmalloc(sizeof(struct inet6_dev), GFP_KERNEL);
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	if (ndev) {
3331da177e4SLinus Torvalds 		memset(ndev, 0, sizeof(struct inet6_dev));
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 		rwlock_init(&ndev->lock);
3361da177e4SLinus Torvalds 		ndev->dev = dev;
3371da177e4SLinus Torvalds 		memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
3381da177e4SLinus Torvalds 		ndev->cnf.mtu6 = dev->mtu;
3391da177e4SLinus Torvalds 		ndev->cnf.sysctl = NULL;
3401da177e4SLinus Torvalds 		ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
3411da177e4SLinus Torvalds 		if (ndev->nd_parms == NULL) {
3421da177e4SLinus Torvalds 			kfree(ndev);
3431da177e4SLinus Torvalds 			return NULL;
3441da177e4SLinus Torvalds 		}
3451da177e4SLinus Torvalds 		/* We refer to the device */
3461da177e4SLinus Torvalds 		dev_hold(dev);
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 		if (snmp6_alloc_dev(ndev) < 0) {
3491da177e4SLinus Torvalds 			ADBG((KERN_WARNING
3501da177e4SLinus Torvalds 				"%s(): cannot allocate memory for statistics; dev=%s.\n",
3511da177e4SLinus Torvalds 				__FUNCTION__, dev->name));
3521da177e4SLinus Torvalds 			neigh_parms_release(&nd_tbl, ndev->nd_parms);
3531da177e4SLinus Torvalds 			ndev->dead = 1;
3541da177e4SLinus Torvalds 			in6_dev_finish_destroy(ndev);
3551da177e4SLinus Torvalds 			return NULL;
3561da177e4SLinus Torvalds 		}
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 		if (snmp6_register_dev(ndev) < 0) {
3591da177e4SLinus Torvalds 			ADBG((KERN_WARNING
3601da177e4SLinus Torvalds 				"%s(): cannot create /proc/net/dev_snmp6/%s\n",
3611da177e4SLinus Torvalds 				__FUNCTION__, dev->name));
3621da177e4SLinus Torvalds 			neigh_parms_release(&nd_tbl, ndev->nd_parms);
3631da177e4SLinus Torvalds 			ndev->dead = 1;
3641da177e4SLinus Torvalds 			in6_dev_finish_destroy(ndev);
3651da177e4SLinus Torvalds 			return NULL;
3661da177e4SLinus Torvalds 		}
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 		/* One reference from device.  We must do this before
3691da177e4SLinus Torvalds 		 * we invoke __ipv6_regen_rndid().
3701da177e4SLinus Torvalds 		 */
3711da177e4SLinus Torvalds 		in6_dev_hold(ndev);
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
3741da177e4SLinus Torvalds 		get_random_bytes(ndev->rndid, sizeof(ndev->rndid));
3751da177e4SLinus Torvalds 		get_random_bytes(ndev->entropy, sizeof(ndev->entropy));
3761da177e4SLinus Torvalds 		init_timer(&ndev->regen_timer);
3771da177e4SLinus Torvalds 		ndev->regen_timer.function = ipv6_regen_rndid;
3781da177e4SLinus Torvalds 		ndev->regen_timer.data = (unsigned long) ndev;
3791da177e4SLinus Torvalds 		if ((dev->flags&IFF_LOOPBACK) ||
3801da177e4SLinus Torvalds 		    dev->type == ARPHRD_TUNNEL ||
38177bd9196SRémi Denis-Courmont 		    dev->type == ARPHRD_NONE ||
3821da177e4SLinus Torvalds 		    dev->type == ARPHRD_SIT) {
3831da177e4SLinus Torvalds 			printk(KERN_INFO
384a1493d9cSDavid S. Miller 			       "%s: Disabled Privacy Extensions\n",
385a1493d9cSDavid S. Miller 			       dev->name);
3861da177e4SLinus Torvalds 			ndev->cnf.use_tempaddr = -1;
3871da177e4SLinus Torvalds 		} else {
3881da177e4SLinus Torvalds 			in6_dev_hold(ndev);
3891da177e4SLinus Torvalds 			ipv6_regen_rndid((unsigned long) ndev);
3901da177e4SLinus Torvalds 		}
3911da177e4SLinus Torvalds #endif
3921da177e4SLinus Torvalds 
3933c21edbdSYOSHIFUJI Hideaki 		if (netif_carrier_ok(dev))
3943c21edbdSYOSHIFUJI Hideaki 			ndev->if_flags |= IF_READY;
3953c21edbdSYOSHIFUJI Hideaki 
3961da177e4SLinus Torvalds 		write_lock_bh(&addrconf_lock);
3971da177e4SLinus Torvalds 		dev->ip6_ptr = ndev;
3981da177e4SLinus Torvalds 		write_unlock_bh(&addrconf_lock);
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds 		ipv6_mc_init_dev(ndev);
4011da177e4SLinus Torvalds 		ndev->tstamp = jiffies;
4021da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
4031da177e4SLinus Torvalds 		neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6,
4041da177e4SLinus Torvalds 				      NET_IPV6_NEIGH, "ipv6",
4051da177e4SLinus Torvalds 				      &ndisc_ifinfo_sysctl_change,
4061da177e4SLinus Torvalds 				      NULL);
4071da177e4SLinus Torvalds 		addrconf_sysctl_register(ndev, &ndev->cnf);
4081da177e4SLinus Torvalds #endif
4091da177e4SLinus Torvalds 	}
4101da177e4SLinus Torvalds 	return ndev;
4111da177e4SLinus Torvalds }
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
4141da177e4SLinus Torvalds {
4151da177e4SLinus Torvalds 	struct inet6_dev *idev;
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	ASSERT_RTNL();
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	if ((idev = __in6_dev_get(dev)) == NULL) {
4201da177e4SLinus Torvalds 		if ((idev = ipv6_add_dev(dev)) == NULL)
4211da177e4SLinus Torvalds 			return NULL;
4221da177e4SLinus Torvalds 	}
423c5e33bddSYOSHIFUJI Hideaki 
4241da177e4SLinus Torvalds 	if (dev->flags&IFF_UP)
4251da177e4SLinus Torvalds 		ipv6_mc_up(idev);
4261da177e4SLinus Torvalds 	return idev;
4271da177e4SLinus Torvalds }
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
4301da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev)
4311da177e4SLinus Torvalds {
4321da177e4SLinus Torvalds 	struct net_device *dev;
4331da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
4341da177e4SLinus Torvalds 	struct in6_addr addr;
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	if (!idev)
4371da177e4SLinus Torvalds 		return;
4381da177e4SLinus Torvalds 	dev = idev->dev;
4391da177e4SLinus Torvalds 	if (dev && (dev->flags & IFF_MULTICAST)) {
4401da177e4SLinus Torvalds 		ipv6_addr_all_routers(&addr);
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 		if (idev->cnf.forwarding)
4431da177e4SLinus Torvalds 			ipv6_dev_mc_inc(dev, &addr);
4441da177e4SLinus Torvalds 		else
4451da177e4SLinus Torvalds 			ipv6_dev_mc_dec(dev, &addr);
4461da177e4SLinus Torvalds 	}
4471da177e4SLinus Torvalds 	for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
4481da177e4SLinus Torvalds 		if (idev->cnf.forwarding)
4491da177e4SLinus Torvalds 			addrconf_join_anycast(ifa);
4501da177e4SLinus Torvalds 		else
4511da177e4SLinus Torvalds 			addrconf_leave_anycast(ifa);
4521da177e4SLinus Torvalds 	}
4531da177e4SLinus Torvalds }
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds static void addrconf_forward_change(void)
4571da177e4SLinus Torvalds {
4581da177e4SLinus Torvalds 	struct net_device *dev;
4591da177e4SLinus Torvalds 	struct inet6_dev *idev;
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
4621da177e4SLinus Torvalds 	for (dev=dev_base; dev; dev=dev->next) {
4631da177e4SLinus Torvalds 		read_lock(&addrconf_lock);
4641da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
4651da177e4SLinus Torvalds 		if (idev) {
4661da177e4SLinus Torvalds 			int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding);
4671da177e4SLinus Torvalds 			idev->cnf.forwarding = ipv6_devconf.forwarding;
4681da177e4SLinus Torvalds 			if (changed)
4691da177e4SLinus Torvalds 				dev_forward_change(idev);
4701da177e4SLinus Torvalds 		}
4711da177e4SLinus Torvalds 		read_unlock(&addrconf_lock);
4721da177e4SLinus Torvalds 	}
4731da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
4741da177e4SLinus Torvalds }
4751da177e4SLinus Torvalds #endif
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds /* Nobody refers to this ifaddr, destroy it */
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
4801da177e4SLinus Torvalds {
4811da177e4SLinus Torvalds 	BUG_TRAP(ifp->if_next==NULL);
4821da177e4SLinus Torvalds 	BUG_TRAP(ifp->lst_next==NULL);
4831da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
4841da177e4SLinus Torvalds 	printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
4851da177e4SLinus Torvalds #endif
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 	in6_dev_put(ifp->idev);
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	if (del_timer(&ifp->timer))
4901da177e4SLinus Torvalds 		printk("Timer is still running, when freeing ifa=%p\n", ifp);
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	if (!ifp->dead) {
4931da177e4SLinus Torvalds 		printk("Freeing alive inet6 address %p\n", ifp);
4941da177e4SLinus Torvalds 		return;
4951da177e4SLinus Torvalds 	}
4961da177e4SLinus Torvalds 	dst_release(&ifp->rt->u.dst);
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 	kfree(ifp);
4991da177e4SLinus Torvalds }
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds static struct inet6_ifaddr *
5041da177e4SLinus Torvalds ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
505e431b8c0SJamal Hadi Salim 	      int scope, u32 flags)
5061da177e4SLinus Torvalds {
5071da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
5081da177e4SLinus Torvalds 	struct rt6_info *rt;
5091da177e4SLinus Torvalds 	int hash;
5101da177e4SLinus Torvalds 	int err = 0;
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 	read_lock_bh(&addrconf_lock);
5131da177e4SLinus Torvalds 	if (idev->dead) {
5141da177e4SLinus Torvalds 		err = -ENODEV;			/*XXX*/
5151da177e4SLinus Torvalds 		goto out2;
5161da177e4SLinus Torvalds 	}
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	write_lock(&addrconf_hash_lock);
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	/* Ignore adding duplicate addresses on an interface */
5211da177e4SLinus Torvalds 	if (ipv6_chk_same_addr(addr, idev->dev)) {
5221da177e4SLinus Torvalds 		ADBG(("ipv6_add_addr: already assigned\n"));
5231da177e4SLinus Torvalds 		err = -EEXIST;
5241da177e4SLinus Torvalds 		goto out;
5251da177e4SLinus Torvalds 	}
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 	ifa = kmalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 	if (ifa == NULL) {
5301da177e4SLinus Torvalds 		ADBG(("ipv6_add_addr: malloc failed\n"));
5311da177e4SLinus Torvalds 		err = -ENOBUFS;
5321da177e4SLinus Torvalds 		goto out;
5331da177e4SLinus Torvalds 	}
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds 	rt = addrconf_dst_alloc(idev, addr, 0);
5361da177e4SLinus Torvalds 	if (IS_ERR(rt)) {
5371da177e4SLinus Torvalds 		err = PTR_ERR(rt);
5381da177e4SLinus Torvalds 		goto out;
5391da177e4SLinus Torvalds 	}
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 	memset(ifa, 0, sizeof(struct inet6_ifaddr));
5421da177e4SLinus Torvalds 	ipv6_addr_copy(&ifa->addr, addr);
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 	spin_lock_init(&ifa->lock);
5451da177e4SLinus Torvalds 	init_timer(&ifa->timer);
5461da177e4SLinus Torvalds 	ifa->timer.data = (unsigned long) ifa;
5471da177e4SLinus Torvalds 	ifa->scope = scope;
5481da177e4SLinus Torvalds 	ifa->prefix_len = pfxlen;
5491da177e4SLinus Torvalds 	ifa->flags = flags | IFA_F_TENTATIVE;
5501da177e4SLinus Torvalds 	ifa->cstamp = ifa->tstamp = jiffies;
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds 	ifa->idev = idev;
5531da177e4SLinus Torvalds 	in6_dev_hold(idev);
5541da177e4SLinus Torvalds 	/* For caller */
5551da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	/* Add to big hash table */
5581da177e4SLinus Torvalds 	hash = ipv6_addr_hash(addr);
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 	ifa->lst_next = inet6_addr_lst[hash];
5611da177e4SLinus Torvalds 	inet6_addr_lst[hash] = ifa;
5621da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
5631da177e4SLinus Torvalds 	write_unlock(&addrconf_hash_lock);
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	write_lock(&idev->lock);
5661da177e4SLinus Torvalds 	/* Add to inet6_dev unicast addr list. */
5671da177e4SLinus Torvalds 	ifa->if_next = idev->addr_list;
5681da177e4SLinus Torvalds 	idev->addr_list = ifa;
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
5711da177e4SLinus Torvalds 	if (ifa->flags&IFA_F_TEMPORARY) {
5721da177e4SLinus Torvalds 		ifa->tmp_next = idev->tempaddr_list;
5731da177e4SLinus Torvalds 		idev->tempaddr_list = ifa;
5741da177e4SLinus Torvalds 		in6_ifa_hold(ifa);
5751da177e4SLinus Torvalds 	}
5761da177e4SLinus Torvalds #endif
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	ifa->rt = rt;
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
5811da177e4SLinus Torvalds 	write_unlock(&idev->lock);
5821da177e4SLinus Torvalds out2:
5831da177e4SLinus Torvalds 	read_unlock_bh(&addrconf_lock);
5841da177e4SLinus Torvalds 
585fd92833aSYOSHIFUJI Hideaki 	if (likely(err == 0))
5861da177e4SLinus Torvalds 		notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
5871da177e4SLinus Torvalds 	else {
5881da177e4SLinus Torvalds 		kfree(ifa);
5891da177e4SLinus Torvalds 		ifa = ERR_PTR(err);
5901da177e4SLinus Torvalds 	}
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 	return ifa;
5931da177e4SLinus Torvalds out:
5941da177e4SLinus Torvalds 	write_unlock(&addrconf_hash_lock);
5951da177e4SLinus Torvalds 	goto out2;
5961da177e4SLinus Torvalds }
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp)
6011da177e4SLinus Torvalds {
6021da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa, **ifap;
6031da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
6041da177e4SLinus Torvalds 	int hash;
6051da177e4SLinus Torvalds 	int deleted = 0, onlink = 0;
6061da177e4SLinus Torvalds 	unsigned long expires = jiffies;
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 	hash = ipv6_addr_hash(&ifp->addr);
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	ifp->dead = 1;
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 	write_lock_bh(&addrconf_hash_lock);
6131da177e4SLinus Torvalds 	for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL;
6141da177e4SLinus Torvalds 	     ifap = &ifa->lst_next) {
6151da177e4SLinus Torvalds 		if (ifa == ifp) {
6161da177e4SLinus Torvalds 			*ifap = ifa->lst_next;
6171da177e4SLinus Torvalds 			__in6_ifa_put(ifp);
6181da177e4SLinus Torvalds 			ifa->lst_next = NULL;
6191da177e4SLinus Torvalds 			break;
6201da177e4SLinus Torvalds 		}
6211da177e4SLinus Torvalds 	}
6221da177e4SLinus Torvalds 	write_unlock_bh(&addrconf_hash_lock);
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
6251da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
6261da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_TEMPORARY) {
6271da177e4SLinus Torvalds 		for (ifap = &idev->tempaddr_list; (ifa=*ifap) != NULL;
6281da177e4SLinus Torvalds 		     ifap = &ifa->tmp_next) {
6291da177e4SLinus Torvalds 			if (ifa == ifp) {
6301da177e4SLinus Torvalds 				*ifap = ifa->tmp_next;
6311da177e4SLinus Torvalds 				if (ifp->ifpub) {
6321da177e4SLinus Torvalds 					in6_ifa_put(ifp->ifpub);
6331da177e4SLinus Torvalds 					ifp->ifpub = NULL;
6341da177e4SLinus Torvalds 				}
6351da177e4SLinus Torvalds 				__in6_ifa_put(ifp);
6361da177e4SLinus Torvalds 				ifa->tmp_next = NULL;
6371da177e4SLinus Torvalds 				break;
6381da177e4SLinus Torvalds 			}
6391da177e4SLinus Torvalds 		}
6401da177e4SLinus Torvalds 	}
6411da177e4SLinus Torvalds #endif
6421da177e4SLinus Torvalds 
6431d142804SKristian Slavov 	for (ifap = &idev->addr_list; (ifa=*ifap) != NULL;) {
6441da177e4SLinus Torvalds 		if (ifa == ifp) {
6451da177e4SLinus Torvalds 			*ifap = ifa->if_next;
6461da177e4SLinus Torvalds 			__in6_ifa_put(ifp);
6471da177e4SLinus Torvalds 			ifa->if_next = NULL;
6481da177e4SLinus Torvalds 			if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
6491da177e4SLinus Torvalds 				break;
6501da177e4SLinus Torvalds 			deleted = 1;
6511d142804SKristian Slavov 			continue;
6521da177e4SLinus Torvalds 		} else if (ifp->flags & IFA_F_PERMANENT) {
6531da177e4SLinus Torvalds 			if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
6541da177e4SLinus Torvalds 					      ifp->prefix_len)) {
6551da177e4SLinus Torvalds 				if (ifa->flags & IFA_F_PERMANENT) {
6561da177e4SLinus Torvalds 					onlink = 1;
6571da177e4SLinus Torvalds 					if (deleted)
6581da177e4SLinus Torvalds 						break;
6591da177e4SLinus Torvalds 				} else {
6601da177e4SLinus Torvalds 					unsigned long lifetime;
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 					if (!onlink)
6631da177e4SLinus Torvalds 						onlink = -1;
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds 					spin_lock(&ifa->lock);
6661da177e4SLinus Torvalds 					lifetime = min_t(unsigned long,
6671da177e4SLinus Torvalds 							 ifa->valid_lft, 0x7fffffffUL/HZ);
6681da177e4SLinus Torvalds 					if (time_before(expires,
6691da177e4SLinus Torvalds 							ifa->tstamp + lifetime * HZ))
6701da177e4SLinus Torvalds 						expires = ifa->tstamp + lifetime * HZ;
6711da177e4SLinus Torvalds 					spin_unlock(&ifa->lock);
6721da177e4SLinus Torvalds 				}
6731da177e4SLinus Torvalds 			}
6741da177e4SLinus Torvalds 		}
6751d142804SKristian Slavov 		ifap = &ifa->if_next;
6761da177e4SLinus Torvalds 	}
6771da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_DELADDR, ifp);
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 	notifier_call_chain(&inet6addr_chain,NETDEV_DOWN,ifp);
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 	addrconf_del_timer(ifp);
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 	/*
6861da177e4SLinus Torvalds 	 * Purge or update corresponding prefix
6871da177e4SLinus Torvalds 	 *
6881da177e4SLinus Torvalds 	 * 1) we don't purge prefix here if address was not permanent.
6891da177e4SLinus Torvalds 	 *    prefix is managed by its own lifetime.
6901da177e4SLinus Torvalds 	 * 2) if there're no addresses, delete prefix.
6911da177e4SLinus Torvalds 	 * 3) if there're still other permanent address(es),
6921da177e4SLinus Torvalds 	 *    corresponding prefix is still permanent.
6931da177e4SLinus Torvalds 	 * 4) otherwise, update prefix lifetime to the
6941da177e4SLinus Torvalds 	 *    longest valid lifetime among the corresponding
6951da177e4SLinus Torvalds 	 *    addresses on the device.
6961da177e4SLinus Torvalds 	 *    Note: subsequent RA will update lifetime.
6971da177e4SLinus Torvalds 	 *
6981da177e4SLinus Torvalds 	 * --yoshfuji
6991da177e4SLinus Torvalds 	 */
7001da177e4SLinus Torvalds 	if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
7011da177e4SLinus Torvalds 		struct in6_addr prefix;
7021da177e4SLinus Torvalds 		struct rt6_info *rt;
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 		ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
7051da177e4SLinus Torvalds 		rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 		if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
7081da177e4SLinus Torvalds 			if (onlink == 0) {
7090d51aa80SJamal Hadi Salim 				ip6_del_rt(rt, NULL, NULL, NULL);
7101da177e4SLinus Torvalds 				rt = NULL;
7111da177e4SLinus Torvalds 			} else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
7121da177e4SLinus Torvalds 				rt->rt6i_expires = expires;
7131da177e4SLinus Torvalds 				rt->rt6i_flags |= RTF_EXPIRES;
7141da177e4SLinus Torvalds 			}
7151da177e4SLinus Torvalds 		}
7161da177e4SLinus Torvalds 		dst_release(&rt->u.dst);
7171da177e4SLinus Torvalds 	}
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds 	in6_ifa_put(ifp);
7201da177e4SLinus Torvalds }
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
7231da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
7241da177e4SLinus Torvalds {
7251da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
7261da177e4SLinus Torvalds 	struct in6_addr addr, *tmpaddr;
7271da177e4SLinus Torvalds 	unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
7281da177e4SLinus Torvalds 	int tmp_plen;
7291da177e4SLinus Torvalds 	int ret = 0;
7301da177e4SLinus Torvalds 	int max_addresses;
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 	write_lock(&idev->lock);
7331da177e4SLinus Torvalds 	if (ift) {
7341da177e4SLinus Torvalds 		spin_lock_bh(&ift->lock);
7351da177e4SLinus Torvalds 		memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
7361da177e4SLinus Torvalds 		spin_unlock_bh(&ift->lock);
7371da177e4SLinus Torvalds 		tmpaddr = &addr;
7381da177e4SLinus Torvalds 	} else {
7391da177e4SLinus Torvalds 		tmpaddr = NULL;
7401da177e4SLinus Torvalds 	}
7411da177e4SLinus Torvalds retry:
7421da177e4SLinus Torvalds 	in6_dev_hold(idev);
7431da177e4SLinus Torvalds 	if (idev->cnf.use_tempaddr <= 0) {
7441da177e4SLinus Torvalds 		write_unlock(&idev->lock);
7451da177e4SLinus Torvalds 		printk(KERN_INFO
7461da177e4SLinus Torvalds 			"ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
7471da177e4SLinus Torvalds 		in6_dev_put(idev);
7481da177e4SLinus Torvalds 		ret = -1;
7491da177e4SLinus Torvalds 		goto out;
7501da177e4SLinus Torvalds 	}
7511da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
7521da177e4SLinus Torvalds 	if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
7531da177e4SLinus Torvalds 		idev->cnf.use_tempaddr = -1;	/*XXX*/
7541da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
7551da177e4SLinus Torvalds 		write_unlock(&idev->lock);
7561da177e4SLinus Torvalds 		printk(KERN_WARNING
7571da177e4SLinus Torvalds 			"ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
7581da177e4SLinus Torvalds 		in6_dev_put(idev);
7591da177e4SLinus Torvalds 		ret = -1;
7601da177e4SLinus Torvalds 		goto out;
7611da177e4SLinus Torvalds 	}
7621da177e4SLinus Torvalds 	in6_ifa_hold(ifp);
7631da177e4SLinus Torvalds 	memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
7641da177e4SLinus Torvalds 	if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
7651da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
7661da177e4SLinus Torvalds 		write_unlock(&idev->lock);
7671da177e4SLinus Torvalds 		printk(KERN_WARNING
7681da177e4SLinus Torvalds 			"ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
7691da177e4SLinus Torvalds 		in6_ifa_put(ifp);
7701da177e4SLinus Torvalds 		in6_dev_put(idev);
7711da177e4SLinus Torvalds 		ret = -1;
7721da177e4SLinus Torvalds 		goto out;
7731da177e4SLinus Torvalds 	}
7741da177e4SLinus Torvalds 	memcpy(&addr.s6_addr[8], idev->rndid, 8);
7751da177e4SLinus Torvalds 	tmp_valid_lft = min_t(__u32,
7761da177e4SLinus Torvalds 			      ifp->valid_lft,
7771da177e4SLinus Torvalds 			      idev->cnf.temp_valid_lft);
7781da177e4SLinus Torvalds 	tmp_prefered_lft = min_t(__u32,
7791da177e4SLinus Torvalds 				 ifp->prefered_lft,
7801da177e4SLinus Torvalds 				 idev->cnf.temp_prefered_lft - desync_factor / HZ);
7811da177e4SLinus Torvalds 	tmp_plen = ifp->prefix_len;
7821da177e4SLinus Torvalds 	max_addresses = idev->cnf.max_addresses;
7831da177e4SLinus Torvalds 	tmp_cstamp = ifp->cstamp;
7841da177e4SLinus Torvalds 	tmp_tstamp = ifp->tstamp;
7851da177e4SLinus Torvalds 	spin_unlock_bh(&ifp->lock);
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 	write_unlock(&idev->lock);
7881da177e4SLinus Torvalds 	ift = !max_addresses ||
7891da177e4SLinus Torvalds 	      ipv6_count_addresses(idev) < max_addresses ?
7901da177e4SLinus Torvalds 		ipv6_add_addr(idev, &addr, tmp_plen,
7911da177e4SLinus Torvalds 			      ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, IFA_F_TEMPORARY) : NULL;
7921da177e4SLinus Torvalds 	if (!ift || IS_ERR(ift)) {
7931da177e4SLinus Torvalds 		in6_ifa_put(ifp);
7941da177e4SLinus Torvalds 		in6_dev_put(idev);
7951da177e4SLinus Torvalds 		printk(KERN_INFO
7961da177e4SLinus Torvalds 			"ipv6_create_tempaddr(): retry temporary address regeneration.\n");
7971da177e4SLinus Torvalds 		tmpaddr = &addr;
7981da177e4SLinus Torvalds 		write_lock(&idev->lock);
7991da177e4SLinus Torvalds 		goto retry;
8001da177e4SLinus Torvalds 	}
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	spin_lock_bh(&ift->lock);
8031da177e4SLinus Torvalds 	ift->ifpub = ifp;
8041da177e4SLinus Torvalds 	ift->valid_lft = tmp_valid_lft;
8051da177e4SLinus Torvalds 	ift->prefered_lft = tmp_prefered_lft;
8061da177e4SLinus Torvalds 	ift->cstamp = tmp_cstamp;
8071da177e4SLinus Torvalds 	ift->tstamp = tmp_tstamp;
8081da177e4SLinus Torvalds 	spin_unlock_bh(&ift->lock);
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 	addrconf_dad_start(ift, 0);
8111da177e4SLinus Torvalds 	in6_ifa_put(ift);
8121da177e4SLinus Torvalds 	in6_dev_put(idev);
8131da177e4SLinus Torvalds out:
8141da177e4SLinus Torvalds 	return ret;
8151da177e4SLinus Torvalds }
8161da177e4SLinus Torvalds #endif
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds /*
819072047e4SYOSHIFUJI Hideaki  *	Choose an appropriate source address (RFC3484)
8201da177e4SLinus Torvalds  */
821072047e4SYOSHIFUJI Hideaki struct ipv6_saddr_score {
822072047e4SYOSHIFUJI Hideaki 	int		addr_type;
823072047e4SYOSHIFUJI Hideaki 	unsigned int	attrs;
824072047e4SYOSHIFUJI Hideaki 	int		matchlen;
825072047e4SYOSHIFUJI Hideaki 	unsigned int	scope;
826072047e4SYOSHIFUJI Hideaki 	unsigned int	rule;
827072047e4SYOSHIFUJI Hideaki };
828072047e4SYOSHIFUJI Hideaki 
829072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_LOCAL		0x0001
830072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_PREFERRED	0x0004
831072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_HOA		0x0008
832072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_OIF		0x0010
833072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_LABEL		0x0020
834072047e4SYOSHIFUJI Hideaki #define IPV6_SADDR_SCORE_PRIVACY	0x0040
835072047e4SYOSHIFUJI Hideaki 
836072047e4SYOSHIFUJI Hideaki static int inline ipv6_saddr_preferred(int type)
8371da177e4SLinus Torvalds {
838072047e4SYOSHIFUJI Hideaki 	if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|
839072047e4SYOSHIFUJI Hideaki 		    IPV6_ADDR_LOOPBACK|IPV6_ADDR_RESERVED))
840072047e4SYOSHIFUJI Hideaki 		return 1;
841072047e4SYOSHIFUJI Hideaki 	return 0;
8421da177e4SLinus Torvalds }
8431da177e4SLinus Torvalds 
844072047e4SYOSHIFUJI Hideaki /* static matching label */
845072047e4SYOSHIFUJI Hideaki static int inline ipv6_saddr_label(const struct in6_addr *addr, int type)
846072047e4SYOSHIFUJI Hideaki {
847072047e4SYOSHIFUJI Hideaki  /*
848072047e4SYOSHIFUJI Hideaki   * 	prefix (longest match)	label
849072047e4SYOSHIFUJI Hideaki   * 	-----------------------------
850072047e4SYOSHIFUJI Hideaki   * 	::1/128			0
851072047e4SYOSHIFUJI Hideaki   * 	::/0			1
852072047e4SYOSHIFUJI Hideaki   * 	2002::/16		2
853072047e4SYOSHIFUJI Hideaki   * 	::/96			3
854072047e4SYOSHIFUJI Hideaki   * 	::ffff:0:0/96		4
855072047e4SYOSHIFUJI Hideaki   */
856072047e4SYOSHIFUJI Hideaki 	if (type & IPV6_ADDR_LOOPBACK)
857072047e4SYOSHIFUJI Hideaki 		return 0;
858072047e4SYOSHIFUJI Hideaki 	else if (type & IPV6_ADDR_COMPATv4)
859072047e4SYOSHIFUJI Hideaki 		return 3;
860072047e4SYOSHIFUJI Hideaki 	else if (type & IPV6_ADDR_MAPPED)
861072047e4SYOSHIFUJI Hideaki 		return 4;
862072047e4SYOSHIFUJI Hideaki 	else if (addr->s6_addr16[0] == htons(0x2002))
863072047e4SYOSHIFUJI Hideaki 		return 2;
864072047e4SYOSHIFUJI Hideaki 	return 1;
865072047e4SYOSHIFUJI Hideaki }
8661da177e4SLinus Torvalds 
867072047e4SYOSHIFUJI Hideaki int ipv6_dev_get_saddr(struct net_device *daddr_dev,
8681da177e4SLinus Torvalds 		       struct in6_addr *daddr, struct in6_addr *saddr)
8691da177e4SLinus Torvalds {
870072047e4SYOSHIFUJI Hideaki 	struct ipv6_saddr_score hiscore;
871072047e4SYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifa_result = NULL;
872072047e4SYOSHIFUJI Hideaki 	int daddr_type = __ipv6_addr_type(daddr);
873072047e4SYOSHIFUJI Hideaki 	int daddr_scope = __ipv6_addr_src_scope(daddr_type);
874072047e4SYOSHIFUJI Hideaki 	u32 daddr_label = ipv6_saddr_label(daddr, daddr_type);
875072047e4SYOSHIFUJI Hideaki 	struct net_device *dev;
8761da177e4SLinus Torvalds 
877072047e4SYOSHIFUJI Hideaki 	memset(&hiscore, 0, sizeof(hiscore));
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
8801da177e4SLinus Torvalds 	read_lock(&addrconf_lock);
881072047e4SYOSHIFUJI Hideaki 
8821da177e4SLinus Torvalds 	for (dev = dev_base; dev; dev=dev->next) {
883072047e4SYOSHIFUJI Hideaki 		struct inet6_dev *idev;
884072047e4SYOSHIFUJI Hideaki 		struct inet6_ifaddr *ifa;
885072047e4SYOSHIFUJI Hideaki 
886072047e4SYOSHIFUJI Hideaki 		/* Rule 0: Candidate Source Address (section 4)
887072047e4SYOSHIFUJI Hideaki 		 *  - multicast and link-local destination address,
888072047e4SYOSHIFUJI Hideaki 		 *    the set of candidate source address MUST only
889072047e4SYOSHIFUJI Hideaki 		 *    include addresses assigned to interfaces
890072047e4SYOSHIFUJI Hideaki 		 *    belonging to the same link as the outgoing
891072047e4SYOSHIFUJI Hideaki 		 *    interface.
892072047e4SYOSHIFUJI Hideaki 		 * (- For site-local destination addresses, the
893072047e4SYOSHIFUJI Hideaki 		 *    set of candidate source addresses MUST only
894072047e4SYOSHIFUJI Hideaki 		 *    include addresses assigned to interfaces
895072047e4SYOSHIFUJI Hideaki 		 *    belonging to the same site as the outgoing
896072047e4SYOSHIFUJI Hideaki 		 *    interface.)
897072047e4SYOSHIFUJI Hideaki 		 */
898072047e4SYOSHIFUJI Hideaki 		if ((daddr_type & IPV6_ADDR_MULTICAST ||
899072047e4SYOSHIFUJI Hideaki 		     daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
900072047e4SYOSHIFUJI Hideaki 		    daddr_dev && dev != daddr_dev)
901072047e4SYOSHIFUJI Hideaki 			continue;
902072047e4SYOSHIFUJI Hideaki 
9031da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
904072047e4SYOSHIFUJI Hideaki 		if (!idev)
905072047e4SYOSHIFUJI Hideaki 			continue;
906072047e4SYOSHIFUJI Hideaki 
9071da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
908072047e4SYOSHIFUJI Hideaki 		for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
909072047e4SYOSHIFUJI Hideaki 			struct ipv6_saddr_score score;
910072047e4SYOSHIFUJI Hideaki 
911072047e4SYOSHIFUJI Hideaki 			score.addr_type = __ipv6_addr_type(&ifa->addr);
912072047e4SYOSHIFUJI Hideaki 
9136b3ae80aSYOSHIFUJI Hideaki 			/* Rule 0:
9146b3ae80aSYOSHIFUJI Hideaki 			 * - Tentative Address (RFC2462 section 5.4)
9156b3ae80aSYOSHIFUJI Hideaki 			 *  - A tentative address is not considered
9166b3ae80aSYOSHIFUJI Hideaki 			 *    "assigned to an interface" in the traditional
9176b3ae80aSYOSHIFUJI Hideaki 			 *    sense.
9186b3ae80aSYOSHIFUJI Hideaki 			 * - Candidate Source Address (section 4)
919072047e4SYOSHIFUJI Hideaki 			 *  - In any case, anycast addresses, multicast
920072047e4SYOSHIFUJI Hideaki 			 *    addresses, and the unspecified address MUST
921072047e4SYOSHIFUJI Hideaki 			 *    NOT be included in a candidate set.
922072047e4SYOSHIFUJI Hideaki 			 */
9236b3ae80aSYOSHIFUJI Hideaki 			if (ifa->flags & IFA_F_TENTATIVE)
9246b3ae80aSYOSHIFUJI Hideaki 				continue;
925072047e4SYOSHIFUJI Hideaki 			if (unlikely(score.addr_type == IPV6_ADDR_ANY ||
926072047e4SYOSHIFUJI Hideaki 				     score.addr_type & IPV6_ADDR_MULTICAST)) {
927072047e4SYOSHIFUJI Hideaki 				LIMIT_NETDEBUG(KERN_DEBUG
928072047e4SYOSHIFUJI Hideaki 					       "ADDRCONF: unspecified / multicast address"
929072047e4SYOSHIFUJI Hideaki 					       "assigned as unicast address on %s",
930072047e4SYOSHIFUJI Hideaki 					       dev->name);
9311da177e4SLinus Torvalds 				continue;
932072047e4SYOSHIFUJI Hideaki 			}
933072047e4SYOSHIFUJI Hideaki 
934072047e4SYOSHIFUJI Hideaki 			score.attrs = 0;
935072047e4SYOSHIFUJI Hideaki 			score.matchlen = 0;
936072047e4SYOSHIFUJI Hideaki 			score.scope = 0;
937072047e4SYOSHIFUJI Hideaki 			score.rule = 0;
938072047e4SYOSHIFUJI Hideaki 
939072047e4SYOSHIFUJI Hideaki 			if (ifa_result == NULL) {
940072047e4SYOSHIFUJI Hideaki 				/* record it if the first available entry */
941072047e4SYOSHIFUJI Hideaki 				goto record_it;
942072047e4SYOSHIFUJI Hideaki 			}
943072047e4SYOSHIFUJI Hideaki 
944072047e4SYOSHIFUJI Hideaki 			/* Rule 1: Prefer same address */
945072047e4SYOSHIFUJI Hideaki 			if (hiscore.rule < 1) {
946072047e4SYOSHIFUJI Hideaki 				if (ipv6_addr_equal(&ifa_result->addr, daddr))
947072047e4SYOSHIFUJI Hideaki 					hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL;
948072047e4SYOSHIFUJI Hideaki 				hiscore.rule++;
949072047e4SYOSHIFUJI Hideaki 			}
950072047e4SYOSHIFUJI Hideaki 			if (ipv6_addr_equal(&ifa->addr, daddr)) {
951072047e4SYOSHIFUJI Hideaki 				score.attrs |= IPV6_SADDR_SCORE_LOCAL;
952072047e4SYOSHIFUJI Hideaki 				if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) {
953072047e4SYOSHIFUJI Hideaki 					score.rule = 1;
954072047e4SYOSHIFUJI Hideaki 					goto record_it;
955072047e4SYOSHIFUJI Hideaki 				}
956072047e4SYOSHIFUJI Hideaki 			} else {
957072047e4SYOSHIFUJI Hideaki 				if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)
958072047e4SYOSHIFUJI Hideaki 					continue;
959072047e4SYOSHIFUJI Hideaki 			}
960072047e4SYOSHIFUJI Hideaki 
961072047e4SYOSHIFUJI Hideaki 			/* Rule 2: Prefer appropriate scope */
962072047e4SYOSHIFUJI Hideaki 			if (hiscore.rule < 2) {
963072047e4SYOSHIFUJI Hideaki 				hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type);
964072047e4SYOSHIFUJI Hideaki 				hiscore.rule++;
965072047e4SYOSHIFUJI Hideaki 			}
966072047e4SYOSHIFUJI Hideaki 			score.scope = __ipv6_addr_src_scope(score.addr_type);
967072047e4SYOSHIFUJI Hideaki 			if (hiscore.scope < score.scope) {
968072047e4SYOSHIFUJI Hideaki 				if (hiscore.scope < daddr_scope) {
969072047e4SYOSHIFUJI Hideaki 					score.rule = 2;
970072047e4SYOSHIFUJI Hideaki 					goto record_it;
971072047e4SYOSHIFUJI Hideaki 				} else
972072047e4SYOSHIFUJI Hideaki 					continue;
973072047e4SYOSHIFUJI Hideaki 			} else if (score.scope < hiscore.scope) {
974072047e4SYOSHIFUJI Hideaki 				if (score.scope < daddr_scope)
975072047e4SYOSHIFUJI Hideaki 					continue;
976072047e4SYOSHIFUJI Hideaki 				else {
977072047e4SYOSHIFUJI Hideaki 					score.rule = 2;
978072047e4SYOSHIFUJI Hideaki 					goto record_it;
979072047e4SYOSHIFUJI Hideaki 				}
980072047e4SYOSHIFUJI Hideaki 			}
981072047e4SYOSHIFUJI Hideaki 
982072047e4SYOSHIFUJI Hideaki 			/* Rule 3: Avoid deprecated address */
983072047e4SYOSHIFUJI Hideaki 			if (hiscore.rule < 3) {
984072047e4SYOSHIFUJI Hideaki 				if (ipv6_saddr_preferred(hiscore.addr_type) ||
985072047e4SYOSHIFUJI Hideaki 				    !(ifa_result->flags & IFA_F_DEPRECATED))
986072047e4SYOSHIFUJI Hideaki 					hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED;
987072047e4SYOSHIFUJI Hideaki 				hiscore.rule++;
988072047e4SYOSHIFUJI Hideaki 			}
989072047e4SYOSHIFUJI Hideaki 			if (ipv6_saddr_preferred(score.addr_type) ||
990072047e4SYOSHIFUJI Hideaki 			    !(ifa->flags & IFA_F_DEPRECATED)) {
991072047e4SYOSHIFUJI Hideaki 				score.attrs |= IPV6_SADDR_SCORE_PREFERRED;
992072047e4SYOSHIFUJI Hideaki 				if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) {
993072047e4SYOSHIFUJI Hideaki 					score.rule = 3;
994072047e4SYOSHIFUJI Hideaki 					goto record_it;
995072047e4SYOSHIFUJI Hideaki 				}
996072047e4SYOSHIFUJI Hideaki 			} else {
997072047e4SYOSHIFUJI Hideaki 				if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)
998072047e4SYOSHIFUJI Hideaki 					continue;
999072047e4SYOSHIFUJI Hideaki 			}
1000072047e4SYOSHIFUJI Hideaki 
1001072047e4SYOSHIFUJI Hideaki 			/* Rule 4: Prefer home address -- not implemented yet */
1002220bbd74SYOSHIFUJI Hideaki 			if (hiscore.rule < 4)
1003220bbd74SYOSHIFUJI Hideaki 				hiscore.rule++;
1004072047e4SYOSHIFUJI Hideaki 
1005072047e4SYOSHIFUJI Hideaki 			/* Rule 5: Prefer outgoing interface */
1006072047e4SYOSHIFUJI Hideaki 			if (hiscore.rule < 5) {
1007072047e4SYOSHIFUJI Hideaki 				if (daddr_dev == NULL ||
1008072047e4SYOSHIFUJI Hideaki 				    daddr_dev == ifa_result->idev->dev)
1009072047e4SYOSHIFUJI Hideaki 					hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
1010072047e4SYOSHIFUJI Hideaki 				hiscore.rule++;
1011072047e4SYOSHIFUJI Hideaki 			}
1012072047e4SYOSHIFUJI Hideaki 			if (daddr_dev == NULL ||
1013072047e4SYOSHIFUJI Hideaki 			    daddr_dev == ifa->idev->dev) {
1014072047e4SYOSHIFUJI Hideaki 				score.attrs |= IPV6_SADDR_SCORE_OIF;
1015072047e4SYOSHIFUJI Hideaki 				if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
1016072047e4SYOSHIFUJI Hideaki 					score.rule = 5;
1017072047e4SYOSHIFUJI Hideaki 					goto record_it;
1018072047e4SYOSHIFUJI Hideaki 				}
1019072047e4SYOSHIFUJI Hideaki 			} else {
1020072047e4SYOSHIFUJI Hideaki 				if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
1021072047e4SYOSHIFUJI Hideaki 					continue;
1022072047e4SYOSHIFUJI Hideaki 			}
1023072047e4SYOSHIFUJI Hideaki 
1024072047e4SYOSHIFUJI Hideaki 			/* Rule 6: Prefer matching label */
1025072047e4SYOSHIFUJI Hideaki 			if (hiscore.rule < 6) {
1026072047e4SYOSHIFUJI Hideaki 				if (ipv6_saddr_label(&ifa_result->addr, hiscore.addr_type) == daddr_label)
1027072047e4SYOSHIFUJI Hideaki 					hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
1028072047e4SYOSHIFUJI Hideaki 				hiscore.rule++;
1029072047e4SYOSHIFUJI Hideaki 			}
1030072047e4SYOSHIFUJI Hideaki 			if (ipv6_saddr_label(&ifa->addr, score.addr_type) == daddr_label) {
1031072047e4SYOSHIFUJI Hideaki 				score.attrs |= IPV6_SADDR_SCORE_LABEL;
1032072047e4SYOSHIFUJI Hideaki 				if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
1033072047e4SYOSHIFUJI Hideaki 					score.rule = 6;
1034072047e4SYOSHIFUJI Hideaki 					goto record_it;
1035072047e4SYOSHIFUJI Hideaki 				}
1036072047e4SYOSHIFUJI Hideaki 			} else {
1037072047e4SYOSHIFUJI Hideaki 				if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
1038072047e4SYOSHIFUJI Hideaki 					continue;
1039072047e4SYOSHIFUJI Hideaki 			}
1040072047e4SYOSHIFUJI Hideaki 
104144fd0261SPeter Chubb #ifdef CONFIG_IPV6_PRIVACY
1042072047e4SYOSHIFUJI Hideaki 			/* Rule 7: Prefer public address
1043072047e4SYOSHIFUJI Hideaki 			 * Note: prefer temprary address if use_tempaddr >= 2
1044072047e4SYOSHIFUJI Hideaki 			 */
1045072047e4SYOSHIFUJI Hideaki 			if (hiscore.rule < 7) {
1046072047e4SYOSHIFUJI Hideaki 				if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
1047072047e4SYOSHIFUJI Hideaki 				    (ifa_result->idev->cnf.use_tempaddr >= 2))
1048072047e4SYOSHIFUJI Hideaki 					hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1049072047e4SYOSHIFUJI Hideaki 				hiscore.rule++;
1050072047e4SYOSHIFUJI Hideaki 			}
1051072047e4SYOSHIFUJI Hideaki 			if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
1052072047e4SYOSHIFUJI Hideaki 			    (ifa->idev->cnf.use_tempaddr >= 2)) {
1053072047e4SYOSHIFUJI Hideaki 				score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
1054072047e4SYOSHIFUJI Hideaki 				if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
1055072047e4SYOSHIFUJI Hideaki 					score.rule = 7;
1056072047e4SYOSHIFUJI Hideaki 					goto record_it;
1057072047e4SYOSHIFUJI Hideaki 				}
1058072047e4SYOSHIFUJI Hideaki 			} else {
1059072047e4SYOSHIFUJI Hideaki 				if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
1060072047e4SYOSHIFUJI Hideaki 					continue;
1061072047e4SYOSHIFUJI Hideaki 			}
106244fd0261SPeter Chubb #endif
1063072047e4SYOSHIFUJI Hideaki 			/* Rule 8: Use longest matching prefix */
106412da2a43SYan Zheng 			if (hiscore.rule < 8) {
1065072047e4SYOSHIFUJI Hideaki 				hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
106612da2a43SYan Zheng 				hiscore.rule++;
106712da2a43SYan Zheng 			}
1068072047e4SYOSHIFUJI Hideaki 			score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
1069072047e4SYOSHIFUJI Hideaki 			if (score.matchlen > hiscore.matchlen) {
1070072047e4SYOSHIFUJI Hideaki 				score.rule = 8;
1071072047e4SYOSHIFUJI Hideaki 				goto record_it;
1072072047e4SYOSHIFUJI Hideaki 			}
1073072047e4SYOSHIFUJI Hideaki #if 0
1074072047e4SYOSHIFUJI Hideaki 			else if (score.matchlen < hiscore.matchlen)
1075072047e4SYOSHIFUJI Hideaki 				continue;
10761da177e4SLinus Torvalds #endif
1077072047e4SYOSHIFUJI Hideaki 
1078072047e4SYOSHIFUJI Hideaki 			/* Final Rule: choose first available one */
10791da177e4SLinus Torvalds 			continue;
1080072047e4SYOSHIFUJI Hideaki record_it:
1081072047e4SYOSHIFUJI Hideaki 			if (ifa_result)
1082072047e4SYOSHIFUJI Hideaki 				in6_ifa_put(ifa_result);
1083072047e4SYOSHIFUJI Hideaki 			in6_ifa_hold(ifa);
1084072047e4SYOSHIFUJI Hideaki 			ifa_result = ifa;
10851da177e4SLinus Torvalds 			hiscore = score;
10861da177e4SLinus Torvalds 		}
10871da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
10881da177e4SLinus Torvalds 	}
10891da177e4SLinus Torvalds 	read_unlock(&addrconf_lock);
10901da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
10911da177e4SLinus Torvalds 
1092072047e4SYOSHIFUJI Hideaki 	if (!ifa_result)
1093072047e4SYOSHIFUJI Hideaki 		return -EADDRNOTAVAIL;
10941da177e4SLinus Torvalds 
1095072047e4SYOSHIFUJI Hideaki 	ipv6_addr_copy(saddr, &ifa_result->addr);
1096072047e4SYOSHIFUJI Hideaki 	in6_ifa_put(ifa_result);
1097072047e4SYOSHIFUJI Hideaki 	return 0;
10981da177e4SLinus Torvalds }
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds int ipv6_get_saddr(struct dst_entry *dst,
11021da177e4SLinus Torvalds 		   struct in6_addr *daddr, struct in6_addr *saddr)
11031da177e4SLinus Torvalds {
11041da177e4SLinus Torvalds 	return ipv6_dev_get_saddr(dst ? ((struct rt6_info *)dst)->rt6i_idev->dev : NULL, daddr, saddr);
11051da177e4SLinus Torvalds }
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds 
11081da177e4SLinus Torvalds int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr)
11091da177e4SLinus Torvalds {
11101da177e4SLinus Torvalds 	struct inet6_dev *idev;
11111da177e4SLinus Torvalds 	int err = -EADDRNOTAVAIL;
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds 	read_lock(&addrconf_lock);
11141da177e4SLinus Torvalds 	if ((idev = __in6_dev_get(dev)) != NULL) {
11151da177e4SLinus Torvalds 		struct inet6_ifaddr *ifp;
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
11181da177e4SLinus Torvalds 		for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
11191da177e4SLinus Torvalds 			if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
11201da177e4SLinus Torvalds 				ipv6_addr_copy(addr, &ifp->addr);
11211da177e4SLinus Torvalds 				err = 0;
11221da177e4SLinus Torvalds 				break;
11231da177e4SLinus Torvalds 			}
11241da177e4SLinus Torvalds 		}
11251da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
11261da177e4SLinus Torvalds 	}
11271da177e4SLinus Torvalds 	read_unlock(&addrconf_lock);
11281da177e4SLinus Torvalds 	return err;
11291da177e4SLinus Torvalds }
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev)
11321da177e4SLinus Torvalds {
11331da177e4SLinus Torvalds 	int cnt = 0;
11341da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
11371da177e4SLinus Torvalds 	for (ifp=idev->addr_list; ifp; ifp=ifp->if_next)
11381da177e4SLinus Torvalds 		cnt++;
11391da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
11401da177e4SLinus Torvalds 	return cnt;
11411da177e4SLinus Torvalds }
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
11441da177e4SLinus Torvalds {
11451da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
11461da177e4SLinus Torvalds 	u8 hash = ipv6_addr_hash(addr);
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds 	read_lock_bh(&addrconf_hash_lock);
11491da177e4SLinus Torvalds 	for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
11501da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr) &&
11511da177e4SLinus Torvalds 		    !(ifp->flags&IFA_F_TENTATIVE)) {
11521da177e4SLinus Torvalds 			if (dev == NULL || ifp->idev->dev == dev ||
11531da177e4SLinus Torvalds 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
11541da177e4SLinus Torvalds 				break;
11551da177e4SLinus Torvalds 		}
11561da177e4SLinus Torvalds 	}
11571da177e4SLinus Torvalds 	read_unlock_bh(&addrconf_hash_lock);
11581da177e4SLinus Torvalds 	return ifp != NULL;
11591da177e4SLinus Torvalds }
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds static
11621da177e4SLinus Torvalds int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
11631da177e4SLinus Torvalds {
11641da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
11651da177e4SLinus Torvalds 	u8 hash = ipv6_addr_hash(addr);
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 	for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
11681da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
11691da177e4SLinus Torvalds 			if (dev == NULL || ifp->idev->dev == dev)
11701da177e4SLinus Torvalds 				break;
11711da177e4SLinus Torvalds 		}
11721da177e4SLinus Torvalds 	}
11731da177e4SLinus Torvalds 	return ifp != NULL;
11741da177e4SLinus Torvalds }
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
11771da177e4SLinus Torvalds {
11781da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
11791da177e4SLinus Torvalds 	u8 hash = ipv6_addr_hash(addr);
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds 	read_lock_bh(&addrconf_hash_lock);
11821da177e4SLinus Torvalds 	for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
11831da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
11841da177e4SLinus Torvalds 			if (dev == NULL || ifp->idev->dev == dev ||
11851da177e4SLinus Torvalds 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
11861da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
11871da177e4SLinus Torvalds 				break;
11881da177e4SLinus Torvalds 			}
11891da177e4SLinus Torvalds 		}
11901da177e4SLinus Torvalds 	}
11911da177e4SLinus Torvalds 	read_unlock_bh(&addrconf_hash_lock);
11921da177e4SLinus Torvalds 
11931da177e4SLinus Torvalds 	return ifp;
11941da177e4SLinus Torvalds }
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
11971da177e4SLinus Torvalds {
11981da177e4SLinus Torvalds 	const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
11990fa1a53eSArnaldo Carvalho de Melo 	const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
12001da177e4SLinus Torvalds 	u32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
12018feaf0c0SArnaldo Carvalho de Melo 	u32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
12021da177e4SLinus Torvalds 	int sk_ipv6only = ipv6_only_sock(sk);
1203463c84b9SArnaldo Carvalho de Melo 	int sk2_ipv6only = inet_v6_ipv6only(sk2);
12041da177e4SLinus Torvalds 	int addr_type = ipv6_addr_type(sk_rcv_saddr6);
12051da177e4SLinus Torvalds 	int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds 	if (!sk2_rcv_saddr && !sk_ipv6only)
12081da177e4SLinus Torvalds 		return 1;
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 	if (addr_type2 == IPV6_ADDR_ANY &&
12111da177e4SLinus Torvalds 	    !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
12121da177e4SLinus Torvalds 		return 1;
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds 	if (addr_type == IPV6_ADDR_ANY &&
12151da177e4SLinus Torvalds 	    !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
12161da177e4SLinus Torvalds 		return 1;
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds 	if (sk2_rcv_saddr6 &&
12191da177e4SLinus Torvalds 	    ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
12201da177e4SLinus Torvalds 		return 1;
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds 	if (addr_type == IPV6_ADDR_MAPPED &&
12231da177e4SLinus Torvalds 	    !sk2_ipv6only &&
12241da177e4SLinus Torvalds 	    (!sk2_rcv_saddr || !sk_rcv_saddr || sk_rcv_saddr == sk2_rcv_saddr))
12251da177e4SLinus Torvalds 		return 1;
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds 	return 0;
12281da177e4SLinus Torvalds }
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */
12311da177e4SLinus Torvalds 
12329f5336e2SAdrian Bunk static void addrconf_dad_stop(struct inet6_ifaddr *ifp)
12331da177e4SLinus Torvalds {
12341da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_PERMANENT) {
12351da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
12361da177e4SLinus Torvalds 		addrconf_del_timer(ifp);
12371da177e4SLinus Torvalds 		ifp->flags |= IFA_F_TENTATIVE;
12381da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
12391da177e4SLinus Torvalds 		in6_ifa_put(ifp);
12401da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
12411da177e4SLinus Torvalds 	} else if (ifp->flags&IFA_F_TEMPORARY) {
12421da177e4SLinus Torvalds 		struct inet6_ifaddr *ifpub;
12431da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
12441da177e4SLinus Torvalds 		ifpub = ifp->ifpub;
12451da177e4SLinus Torvalds 		if (ifpub) {
12461da177e4SLinus Torvalds 			in6_ifa_hold(ifpub);
12471da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
12481da177e4SLinus Torvalds 			ipv6_create_tempaddr(ifpub, ifp);
12491da177e4SLinus Torvalds 			in6_ifa_put(ifpub);
12501da177e4SLinus Torvalds 		} else {
12511da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
12521da177e4SLinus Torvalds 		}
12531da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
12541da177e4SLinus Torvalds #endif
12551da177e4SLinus Torvalds 	} else
12561da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
12571da177e4SLinus Torvalds }
12581da177e4SLinus Torvalds 
12593c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp)
12603c21edbdSYOSHIFUJI Hideaki {
12613c21edbdSYOSHIFUJI Hideaki 	if (net_ratelimit())
12623c21edbdSYOSHIFUJI Hideaki 		printk(KERN_INFO "%s: duplicate address detected!\n", ifp->idev->dev->name);
12633c21edbdSYOSHIFUJI Hideaki 	addrconf_dad_stop(ifp);
12643c21edbdSYOSHIFUJI Hideaki }
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds /* Join to solicited addr multicast group. */
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds void addrconf_join_solict(struct net_device *dev, struct in6_addr *addr)
12691da177e4SLinus Torvalds {
12701da177e4SLinus Torvalds 	struct in6_addr maddr;
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds 	if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
12731da177e4SLinus Torvalds 		return;
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
12761da177e4SLinus Torvalds 	ipv6_dev_mc_inc(dev, &maddr);
12771da177e4SLinus Torvalds }
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds void addrconf_leave_solict(struct inet6_dev *idev, struct in6_addr *addr)
12801da177e4SLinus Torvalds {
12811da177e4SLinus Torvalds 	struct in6_addr maddr;
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 	if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
12841da177e4SLinus Torvalds 		return;
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
12871da177e4SLinus Torvalds 	__ipv6_dev_mc_dec(idev, &maddr);
12881da177e4SLinus Torvalds }
12891da177e4SLinus Torvalds 
129020380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
12911da177e4SLinus Torvalds {
12921da177e4SLinus Torvalds 	struct in6_addr addr;
12931da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
12941da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
12951da177e4SLinus Torvalds 		return;
12961da177e4SLinus Torvalds 	ipv6_dev_ac_inc(ifp->idev->dev, &addr);
12971da177e4SLinus Torvalds }
12981da177e4SLinus Torvalds 
129920380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
13001da177e4SLinus Torvalds {
13011da177e4SLinus Torvalds 	struct in6_addr addr;
13021da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
13031da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
13041da177e4SLinus Torvalds 		return;
13051da177e4SLinus Torvalds 	__ipv6_dev_ac_dec(ifp->idev, &addr);
13061da177e4SLinus Torvalds }
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
13091da177e4SLinus Torvalds {
13101da177e4SLinus Torvalds 	switch (dev->type) {
13111da177e4SLinus Torvalds 	case ARPHRD_ETHER:
13121da177e4SLinus Torvalds 	case ARPHRD_FDDI:
13131da177e4SLinus Torvalds 	case ARPHRD_IEEE802_TR:
13141da177e4SLinus Torvalds 		if (dev->addr_len != ETH_ALEN)
13151da177e4SLinus Torvalds 			return -1;
13161da177e4SLinus Torvalds 		memcpy(eui, dev->dev_addr, 3);
13171da177e4SLinus Torvalds 		memcpy(eui + 5, dev->dev_addr + 3, 3);
13181da177e4SLinus Torvalds 
13191da177e4SLinus Torvalds 		/*
13201da177e4SLinus Torvalds 		 * The zSeries OSA network cards can be shared among various
13211da177e4SLinus Torvalds 		 * OS instances, but the OSA cards have only one MAC address.
13221da177e4SLinus Torvalds 		 * This leads to duplicate address conflicts in conjunction
13231da177e4SLinus Torvalds 		 * with IPv6 if more than one instance uses the same card.
13241da177e4SLinus Torvalds 		 *
13251da177e4SLinus Torvalds 		 * The driver for these cards can deliver a unique 16-bit
13261da177e4SLinus Torvalds 		 * identifier for each instance sharing the same card.  It is
13271da177e4SLinus Torvalds 		 * placed instead of 0xFFFE in the interface identifier.  The
13281da177e4SLinus Torvalds 		 * "u" bit of the interface identifier is not inverted in this
13291da177e4SLinus Torvalds 		 * case.  Hence the resulting interface identifier has local
13301da177e4SLinus Torvalds 		 * scope according to RFC2373.
13311da177e4SLinus Torvalds 		 */
13321da177e4SLinus Torvalds 		if (dev->dev_id) {
13331da177e4SLinus Torvalds 			eui[3] = (dev->dev_id >> 8) & 0xFF;
13341da177e4SLinus Torvalds 			eui[4] = dev->dev_id & 0xFF;
13351da177e4SLinus Torvalds 		} else {
13361da177e4SLinus Torvalds 			eui[3] = 0xFF;
13371da177e4SLinus Torvalds 			eui[4] = 0xFE;
13381da177e4SLinus Torvalds 			eui[0] ^= 2;
13391da177e4SLinus Torvalds 		}
13401da177e4SLinus Torvalds 		return 0;
13411da177e4SLinus Torvalds 	case ARPHRD_ARCNET:
13421da177e4SLinus Torvalds 		/* XXX: inherit EUI-64 from other interface -- yoshfuji */
13431da177e4SLinus Torvalds 		if (dev->addr_len != ARCNET_ALEN)
13441da177e4SLinus Torvalds 			return -1;
13451da177e4SLinus Torvalds 		memset(eui, 0, 7);
13461da177e4SLinus Torvalds 		eui[7] = *(u8*)dev->dev_addr;
13471da177e4SLinus Torvalds 		return 0;
13481da177e4SLinus Torvalds 	case ARPHRD_INFINIBAND:
13491da177e4SLinus Torvalds 		if (dev->addr_len != INFINIBAND_ALEN)
13501da177e4SLinus Torvalds 			return -1;
13511da177e4SLinus Torvalds 		memcpy(eui, dev->dev_addr + 12, 8);
13521da177e4SLinus Torvalds 		eui[0] |= 2;
13531da177e4SLinus Torvalds 		return 0;
13541da177e4SLinus Torvalds 	}
13551da177e4SLinus Torvalds 	return -1;
13561da177e4SLinus Torvalds }
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
13591da177e4SLinus Torvalds {
13601da177e4SLinus Torvalds 	int err = -1;
13611da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
13641da177e4SLinus Torvalds 	for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) {
13651da177e4SLinus Torvalds 		if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
13661da177e4SLinus Torvalds 			memcpy(eui, ifp->addr.s6_addr+8, 8);
13671da177e4SLinus Torvalds 			err = 0;
13681da177e4SLinus Torvalds 			break;
13691da177e4SLinus Torvalds 		}
13701da177e4SLinus Torvalds 	}
13711da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
13721da177e4SLinus Torvalds 	return err;
13731da177e4SLinus Torvalds }
13741da177e4SLinus Torvalds 
13751da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
13761da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
13771da177e4SLinus Torvalds static int __ipv6_regen_rndid(struct inet6_dev *idev)
13781da177e4SLinus Torvalds {
13791da177e4SLinus Torvalds 	struct net_device *dev;
13801da177e4SLinus Torvalds 	struct scatterlist sg[2];
13811da177e4SLinus Torvalds 
1382378f058cSDavid Hardeman 	sg_set_buf(&sg[0], idev->entropy, 8);
1383378f058cSDavid Hardeman 	sg_set_buf(&sg[1], idev->work_eui64, 8);
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds 	dev = idev->dev;
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds 	if (ipv6_generate_eui64(idev->work_eui64, dev)) {
13881da177e4SLinus Torvalds 		printk(KERN_INFO
13891da177e4SLinus Torvalds 			"__ipv6_regen_rndid(idev=%p): cannot get EUI64 identifier; use random bytes.\n",
13901da177e4SLinus Torvalds 			idev);
13911da177e4SLinus Torvalds 		get_random_bytes(idev->work_eui64, sizeof(idev->work_eui64));
13921da177e4SLinus Torvalds 	}
13931da177e4SLinus Torvalds regen:
13941da177e4SLinus Torvalds 	spin_lock(&md5_tfm_lock);
13951da177e4SLinus Torvalds 	if (unlikely(md5_tfm == NULL)) {
13961da177e4SLinus Torvalds 		spin_unlock(&md5_tfm_lock);
13971da177e4SLinus Torvalds 		return -1;
13981da177e4SLinus Torvalds 	}
13991da177e4SLinus Torvalds 	crypto_digest_init(md5_tfm);
14001da177e4SLinus Torvalds 	crypto_digest_update(md5_tfm, sg, 2);
14011da177e4SLinus Torvalds 	crypto_digest_final(md5_tfm, idev->work_digest);
14021da177e4SLinus Torvalds 	spin_unlock(&md5_tfm_lock);
14031da177e4SLinus Torvalds 
14041da177e4SLinus Torvalds 	memcpy(idev->rndid, &idev->work_digest[0], 8);
14051da177e4SLinus Torvalds 	idev->rndid[0] &= ~0x02;
14061da177e4SLinus Torvalds 	memcpy(idev->entropy, &idev->work_digest[8], 8);
14071da177e4SLinus Torvalds 
14081da177e4SLinus Torvalds 	/*
14091da177e4SLinus Torvalds 	 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
14101da177e4SLinus Torvalds 	 * check if generated address is not inappropriate
14111da177e4SLinus Torvalds 	 *
14121da177e4SLinus Torvalds 	 *  - Reserved subnet anycast (RFC 2526)
14131da177e4SLinus Torvalds 	 *	11111101 11....11 1xxxxxxx
14141da177e4SLinus Torvalds 	 *  - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
14151da177e4SLinus Torvalds 	 *	00-00-5E-FE-xx-xx-xx-xx
14161da177e4SLinus Torvalds 	 *  - value 0
14171da177e4SLinus Torvalds 	 *  - XXX: already assigned to an address on the device
14181da177e4SLinus Torvalds 	 */
14191da177e4SLinus Torvalds 	if (idev->rndid[0] == 0xfd &&
14201da177e4SLinus Torvalds 	    (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
14211da177e4SLinus Torvalds 	    (idev->rndid[7]&0x80))
14221da177e4SLinus Torvalds 		goto regen;
14231da177e4SLinus Torvalds 	if ((idev->rndid[0]|idev->rndid[1]) == 0) {
14241da177e4SLinus Torvalds 		if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
14251da177e4SLinus Torvalds 			goto regen;
14261da177e4SLinus Torvalds 		if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
14271da177e4SLinus Torvalds 			goto regen;
14281da177e4SLinus Torvalds 	}
14291da177e4SLinus Torvalds 
14301da177e4SLinus Torvalds 	return 0;
14311da177e4SLinus Torvalds }
14321da177e4SLinus Torvalds 
14331da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data)
14341da177e4SLinus Torvalds {
14351da177e4SLinus Torvalds 	struct inet6_dev *idev = (struct inet6_dev *) data;
14361da177e4SLinus Torvalds 	unsigned long expires;
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds 	read_lock_bh(&addrconf_lock);
14391da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
14401da177e4SLinus Torvalds 
14411da177e4SLinus Torvalds 	if (idev->dead)
14421da177e4SLinus Torvalds 		goto out;
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds 	if (__ipv6_regen_rndid(idev) < 0)
14451da177e4SLinus Torvalds 		goto out;
14461da177e4SLinus Torvalds 
14471da177e4SLinus Torvalds 	expires = jiffies +
14481da177e4SLinus Torvalds 		idev->cnf.temp_prefered_lft * HZ -
14491da177e4SLinus Torvalds 		idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time - desync_factor;
14501da177e4SLinus Torvalds 	if (time_before(expires, jiffies)) {
14511da177e4SLinus Torvalds 		printk(KERN_WARNING
14521da177e4SLinus Torvalds 			"ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
14531da177e4SLinus Torvalds 			idev->dev->name);
14541da177e4SLinus Torvalds 		goto out;
14551da177e4SLinus Torvalds 	}
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds 	if (!mod_timer(&idev->regen_timer, expires))
14581da177e4SLinus Torvalds 		in6_dev_hold(idev);
14591da177e4SLinus Torvalds 
14601da177e4SLinus Torvalds out:
14611da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
14621da177e4SLinus Torvalds 	read_unlock_bh(&addrconf_lock);
14631da177e4SLinus Torvalds 	in6_dev_put(idev);
14641da177e4SLinus Torvalds }
14651da177e4SLinus Torvalds 
14661da177e4SLinus Torvalds static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
14671da177e4SLinus Torvalds 	int ret = 0;
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds 	if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
14701da177e4SLinus Torvalds 		ret = __ipv6_regen_rndid(idev);
14711da177e4SLinus Torvalds 	return ret;
14721da177e4SLinus Torvalds }
14731da177e4SLinus Torvalds #endif
14741da177e4SLinus Torvalds 
14751da177e4SLinus Torvalds /*
14761da177e4SLinus Torvalds  *	Add prefix route.
14771da177e4SLinus Torvalds  */
14781da177e4SLinus Torvalds 
14791da177e4SLinus Torvalds static void
14801da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1481e431b8c0SJamal Hadi Salim 		      unsigned long expires, u32 flags)
14821da177e4SLinus Torvalds {
14831da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
14841da177e4SLinus Torvalds 
14851da177e4SLinus Torvalds 	memset(&rtmsg, 0, sizeof(rtmsg));
14861da177e4SLinus Torvalds 	ipv6_addr_copy(&rtmsg.rtmsg_dst, pfx);
14871da177e4SLinus Torvalds 	rtmsg.rtmsg_dst_len = plen;
14881da177e4SLinus Torvalds 	rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
14891da177e4SLinus Torvalds 	rtmsg.rtmsg_ifindex = dev->ifindex;
14901da177e4SLinus Torvalds 	rtmsg.rtmsg_info = expires;
14911da177e4SLinus Torvalds 	rtmsg.rtmsg_flags = RTF_UP|flags;
14921da177e4SLinus Torvalds 	rtmsg.rtmsg_type = RTMSG_NEWROUTE;
14931da177e4SLinus Torvalds 
14941da177e4SLinus Torvalds 	/* Prevent useless cloning on PtP SIT.
14951da177e4SLinus Torvalds 	   This thing is done here expecting that the whole
14961da177e4SLinus Torvalds 	   class of non-broadcast devices need not cloning.
14971da177e4SLinus Torvalds 	 */
14981da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT && (dev->flags&IFF_POINTOPOINT))
14991da177e4SLinus Torvalds 		rtmsg.rtmsg_flags |= RTF_NONEXTHOP;
15001da177e4SLinus Torvalds 
15010d51aa80SJamal Hadi Salim 	ip6_route_add(&rtmsg, NULL, NULL, NULL);
15021da177e4SLinus Torvalds }
15031da177e4SLinus Torvalds 
15041da177e4SLinus Torvalds /* Create "default" multicast route to the interface */
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev)
15071da177e4SLinus Torvalds {
15081da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
15091da177e4SLinus Torvalds 
15101da177e4SLinus Torvalds 	memset(&rtmsg, 0, sizeof(rtmsg));
15111da177e4SLinus Torvalds 	ipv6_addr_set(&rtmsg.rtmsg_dst,
15121da177e4SLinus Torvalds 		      htonl(0xFF000000), 0, 0, 0);
15131da177e4SLinus Torvalds 	rtmsg.rtmsg_dst_len = 8;
15141da177e4SLinus Torvalds 	rtmsg.rtmsg_metric = IP6_RT_PRIO_ADDRCONF;
15151da177e4SLinus Torvalds 	rtmsg.rtmsg_ifindex = dev->ifindex;
15161da177e4SLinus Torvalds 	rtmsg.rtmsg_flags = RTF_UP;
15171da177e4SLinus Torvalds 	rtmsg.rtmsg_type = RTMSG_NEWROUTE;
15180d51aa80SJamal Hadi Salim 	ip6_route_add(&rtmsg, NULL, NULL, NULL);
15191da177e4SLinus Torvalds }
15201da177e4SLinus Torvalds 
15211da177e4SLinus Torvalds static void sit_route_add(struct net_device *dev)
15221da177e4SLinus Torvalds {
15231da177e4SLinus Torvalds 	struct in6_rtmsg rtmsg;
15241da177e4SLinus Torvalds 
15251da177e4SLinus Torvalds 	memset(&rtmsg, 0, sizeof(rtmsg));
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds 	rtmsg.rtmsg_type	= RTMSG_NEWROUTE;
15281da177e4SLinus Torvalds 	rtmsg.rtmsg_metric	= IP6_RT_PRIO_ADDRCONF;
15291da177e4SLinus Torvalds 
15301da177e4SLinus Torvalds 	/* prefix length - 96 bits "::d.d.d.d" */
15311da177e4SLinus Torvalds 	rtmsg.rtmsg_dst_len	= 96;
15321da177e4SLinus Torvalds 	rtmsg.rtmsg_flags	= RTF_UP|RTF_NONEXTHOP;
15331da177e4SLinus Torvalds 	rtmsg.rtmsg_ifindex	= dev->ifindex;
15341da177e4SLinus Torvalds 
15350d51aa80SJamal Hadi Salim 	ip6_route_add(&rtmsg, NULL, NULL, NULL);
15361da177e4SLinus Torvalds }
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds static void addrconf_add_lroute(struct net_device *dev)
15391da177e4SLinus Torvalds {
15401da177e4SLinus Torvalds 	struct in6_addr addr;
15411da177e4SLinus Torvalds 
15421da177e4SLinus Torvalds 	ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
15431da177e4SLinus Torvalds 	addrconf_prefix_route(&addr, 64, dev, 0, 0);
15441da177e4SLinus Torvalds }
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
15471da177e4SLinus Torvalds {
15481da177e4SLinus Torvalds 	struct inet6_dev *idev;
15491da177e4SLinus Torvalds 
15501da177e4SLinus Torvalds 	ASSERT_RTNL();
15511da177e4SLinus Torvalds 
15521da177e4SLinus Torvalds 	if ((idev = ipv6_find_idev(dev)) == NULL)
15531da177e4SLinus Torvalds 		return NULL;
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds 	/* Add default multicast route */
15561da177e4SLinus Torvalds 	addrconf_add_mroute(dev);
15571da177e4SLinus Torvalds 
15581da177e4SLinus Torvalds 	/* Add link local route */
15591da177e4SLinus Torvalds 	addrconf_add_lroute(dev);
15601da177e4SLinus Torvalds 	return idev;
15611da177e4SLinus Torvalds }
15621da177e4SLinus Torvalds 
15631da177e4SLinus Torvalds void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
15641da177e4SLinus Torvalds {
15651da177e4SLinus Torvalds 	struct prefix_info *pinfo;
15661da177e4SLinus Torvalds 	__u32 valid_lft;
15671da177e4SLinus Torvalds 	__u32 prefered_lft;
15681da177e4SLinus Torvalds 	int addr_type;
15691da177e4SLinus Torvalds 	unsigned long rt_expires;
15701da177e4SLinus Torvalds 	struct inet6_dev *in6_dev;
15711da177e4SLinus Torvalds 
15721da177e4SLinus Torvalds 	pinfo = (struct prefix_info *) opt;
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds 	if (len < sizeof(struct prefix_info)) {
15751da177e4SLinus Torvalds 		ADBG(("addrconf: prefix option too short\n"));
15761da177e4SLinus Torvalds 		return;
15771da177e4SLinus Torvalds 	}
15781da177e4SLinus Torvalds 
15791da177e4SLinus Torvalds 	/*
15801da177e4SLinus Torvalds 	 *	Validation checks ([ADDRCONF], page 19)
15811da177e4SLinus Torvalds 	 */
15821da177e4SLinus Torvalds 
15831da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&pinfo->prefix);
15841da177e4SLinus Torvalds 
15851da177e4SLinus Torvalds 	if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
15861da177e4SLinus Torvalds 		return;
15871da177e4SLinus Torvalds 
15881da177e4SLinus Torvalds 	valid_lft = ntohl(pinfo->valid);
15891da177e4SLinus Torvalds 	prefered_lft = ntohl(pinfo->prefered);
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds 	if (prefered_lft > valid_lft) {
15921da177e4SLinus Torvalds 		if (net_ratelimit())
15931da177e4SLinus Torvalds 			printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
15941da177e4SLinus Torvalds 		return;
15951da177e4SLinus Torvalds 	}
15961da177e4SLinus Torvalds 
15971da177e4SLinus Torvalds 	in6_dev = in6_dev_get(dev);
15981da177e4SLinus Torvalds 
15991da177e4SLinus Torvalds 	if (in6_dev == NULL) {
16001da177e4SLinus Torvalds 		if (net_ratelimit())
16011da177e4SLinus Torvalds 			printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
16021da177e4SLinus Torvalds 		return;
16031da177e4SLinus Torvalds 	}
16041da177e4SLinus Torvalds 
16051da177e4SLinus Torvalds 	/*
16061da177e4SLinus Torvalds 	 *	Two things going on here:
16071da177e4SLinus Torvalds 	 *	1) Add routes for on-link prefixes
16081da177e4SLinus Torvalds 	 *	2) Configure prefixes with the auto flag set
16091da177e4SLinus Torvalds 	 */
16101da177e4SLinus Torvalds 
16111da177e4SLinus Torvalds 	/* Avoid arithmetic overflow. Really, we could
16121da177e4SLinus Torvalds 	   save rt_expires in seconds, likely valid_lft,
16131da177e4SLinus Torvalds 	   but it would require division in fib gc, that it
16141da177e4SLinus Torvalds 	   not good.
16151da177e4SLinus Torvalds 	 */
16161da177e4SLinus Torvalds 	if (valid_lft >= 0x7FFFFFFF/HZ)
16173dd4bc68SYOSHIFUJI Hideaki 		rt_expires = 0x7FFFFFFF - (0x7FFFFFFF % HZ);
16181da177e4SLinus Torvalds 	else
16193dd4bc68SYOSHIFUJI Hideaki 		rt_expires = valid_lft * HZ;
16203dd4bc68SYOSHIFUJI Hideaki 
16213dd4bc68SYOSHIFUJI Hideaki 	/*
16223dd4bc68SYOSHIFUJI Hideaki 	 * We convert this (in jiffies) to clock_t later.
16233dd4bc68SYOSHIFUJI Hideaki 	 * Avoid arithmetic overflow there as well.
16243dd4bc68SYOSHIFUJI Hideaki 	 * Overflow can happen only if HZ < USER_HZ.
16253dd4bc68SYOSHIFUJI Hideaki 	 */
16263dd4bc68SYOSHIFUJI Hideaki 	if (HZ < USER_HZ && rt_expires > 0x7FFFFFFF / USER_HZ)
16273dd4bc68SYOSHIFUJI Hideaki 		rt_expires = 0x7FFFFFFF / USER_HZ;
16281da177e4SLinus Torvalds 
16291da177e4SLinus Torvalds 	if (pinfo->onlink) {
16301da177e4SLinus Torvalds 		struct rt6_info *rt;
16311da177e4SLinus Torvalds 		rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
16321da177e4SLinus Torvalds 
16331da177e4SLinus Torvalds 		if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
16341da177e4SLinus Torvalds 			if (rt->rt6i_flags&RTF_EXPIRES) {
16351da177e4SLinus Torvalds 				if (valid_lft == 0) {
16360d51aa80SJamal Hadi Salim 					ip6_del_rt(rt, NULL, NULL, NULL);
16371da177e4SLinus Torvalds 					rt = NULL;
16381da177e4SLinus Torvalds 				} else {
16393dd4bc68SYOSHIFUJI Hideaki 					rt->rt6i_expires = jiffies + rt_expires;
16401da177e4SLinus Torvalds 				}
16411da177e4SLinus Torvalds 			}
16421da177e4SLinus Torvalds 		} else if (valid_lft) {
16431da177e4SLinus Torvalds 			addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
16443dd4bc68SYOSHIFUJI Hideaki 					      dev, jiffies_to_clock_t(rt_expires), RTF_ADDRCONF|RTF_EXPIRES|RTF_PREFIX_RT);
16451da177e4SLinus Torvalds 		}
16461da177e4SLinus Torvalds 		if (rt)
16471da177e4SLinus Torvalds 			dst_release(&rt->u.dst);
16481da177e4SLinus Torvalds 	}
16491da177e4SLinus Torvalds 
16501da177e4SLinus Torvalds 	/* Try to figure out our local address for this prefix */
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds 	if (pinfo->autoconf && in6_dev->cnf.autoconf) {
16531da177e4SLinus Torvalds 		struct inet6_ifaddr * ifp;
16541da177e4SLinus Torvalds 		struct in6_addr addr;
16551da177e4SLinus Torvalds 		int create = 0, update_lft = 0;
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds 		if (pinfo->prefix_len == 64) {
16581da177e4SLinus Torvalds 			memcpy(&addr, &pinfo->prefix, 8);
16591da177e4SLinus Torvalds 			if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
16601da177e4SLinus Torvalds 			    ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
16611da177e4SLinus Torvalds 				in6_dev_put(in6_dev);
16621da177e4SLinus Torvalds 				return;
16631da177e4SLinus Torvalds 			}
16641da177e4SLinus Torvalds 			goto ok;
16651da177e4SLinus Torvalds 		}
16661da177e4SLinus Torvalds 		if (net_ratelimit())
16671da177e4SLinus Torvalds 			printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
16681da177e4SLinus Torvalds 			       pinfo->prefix_len);
16691da177e4SLinus Torvalds 		in6_dev_put(in6_dev);
16701da177e4SLinus Torvalds 		return;
16711da177e4SLinus Torvalds 
16721da177e4SLinus Torvalds ok:
16731da177e4SLinus Torvalds 
16741da177e4SLinus Torvalds 		ifp = ipv6_get_ifaddr(&addr, dev, 1);
16751da177e4SLinus Torvalds 
16761da177e4SLinus Torvalds 		if (ifp == NULL && valid_lft) {
16771da177e4SLinus Torvalds 			int max_addresses = in6_dev->cnf.max_addresses;
16781da177e4SLinus Torvalds 
16791da177e4SLinus Torvalds 			/* Do not allow to create too much of autoconfigured
16801da177e4SLinus Torvalds 			 * addresses; this would be too easy way to crash kernel.
16811da177e4SLinus Torvalds 			 */
16821da177e4SLinus Torvalds 			if (!max_addresses ||
16831da177e4SLinus Torvalds 			    ipv6_count_addresses(in6_dev) < max_addresses)
16841da177e4SLinus Torvalds 				ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
16851da177e4SLinus Torvalds 						    addr_type&IPV6_ADDR_SCOPE_MASK, 0);
16861da177e4SLinus Torvalds 
16871da177e4SLinus Torvalds 			if (!ifp || IS_ERR(ifp)) {
16881da177e4SLinus Torvalds 				in6_dev_put(in6_dev);
16891da177e4SLinus Torvalds 				return;
16901da177e4SLinus Torvalds 			}
16911da177e4SLinus Torvalds 
16921da177e4SLinus Torvalds 			update_lft = create = 1;
16931da177e4SLinus Torvalds 			ifp->cstamp = jiffies;
16941da177e4SLinus Torvalds 			addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
16951da177e4SLinus Torvalds 		}
16961da177e4SLinus Torvalds 
16971da177e4SLinus Torvalds 		if (ifp) {
16981da177e4SLinus Torvalds 			int flags;
16991da177e4SLinus Torvalds 			unsigned long now;
17001da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
17011da177e4SLinus Torvalds 			struct inet6_ifaddr *ift;
17021da177e4SLinus Torvalds #endif
17031da177e4SLinus Torvalds 			u32 stored_lft;
17041da177e4SLinus Torvalds 
17051da177e4SLinus Torvalds 			/* update lifetime (RFC2462 5.5.3 e) */
17061da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
17071da177e4SLinus Torvalds 			now = jiffies;
17081da177e4SLinus Torvalds 			if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
17091da177e4SLinus Torvalds 				stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
17101da177e4SLinus Torvalds 			else
17111da177e4SLinus Torvalds 				stored_lft = 0;
17121da177e4SLinus Torvalds 			if (!update_lft && stored_lft) {
17131da177e4SLinus Torvalds 				if (valid_lft > MIN_VALID_LIFETIME ||
17141da177e4SLinus Torvalds 				    valid_lft > stored_lft)
17151da177e4SLinus Torvalds 					update_lft = 1;
17161da177e4SLinus Torvalds 				else if (stored_lft <= MIN_VALID_LIFETIME) {
17171da177e4SLinus Torvalds 					/* valid_lft <= stored_lft is always true */
17181da177e4SLinus Torvalds 					/* XXX: IPsec */
17191da177e4SLinus Torvalds 					update_lft = 0;
17201da177e4SLinus Torvalds 				} else {
17211da177e4SLinus Torvalds 					valid_lft = MIN_VALID_LIFETIME;
17221da177e4SLinus Torvalds 					if (valid_lft < prefered_lft)
17231da177e4SLinus Torvalds 						prefered_lft = valid_lft;
17241da177e4SLinus Torvalds 					update_lft = 1;
17251da177e4SLinus Torvalds 				}
17261da177e4SLinus Torvalds 			}
17271da177e4SLinus Torvalds 
17281da177e4SLinus Torvalds 			if (update_lft) {
17291da177e4SLinus Torvalds 				ifp->valid_lft = valid_lft;
17301da177e4SLinus Torvalds 				ifp->prefered_lft = prefered_lft;
17311da177e4SLinus Torvalds 				ifp->tstamp = now;
17321da177e4SLinus Torvalds 				flags = ifp->flags;
17331da177e4SLinus Torvalds 				ifp->flags &= ~IFA_F_DEPRECATED;
17341da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
17351da177e4SLinus Torvalds 
17361da177e4SLinus Torvalds 				if (!(flags&IFA_F_TENTATIVE))
17371da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
17381da177e4SLinus Torvalds 			} else
17391da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
17401da177e4SLinus Torvalds 
17411da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
17421da177e4SLinus Torvalds 			read_lock_bh(&in6_dev->lock);
17431da177e4SLinus Torvalds 			/* update all temporary addresses in the list */
17441da177e4SLinus Torvalds 			for (ift=in6_dev->tempaddr_list; ift; ift=ift->tmp_next) {
17451da177e4SLinus Torvalds 				/*
17461da177e4SLinus Torvalds 				 * When adjusting the lifetimes of an existing
17471da177e4SLinus Torvalds 				 * temporary address, only lower the lifetimes.
17481da177e4SLinus Torvalds 				 * Implementations must not increase the
17491da177e4SLinus Torvalds 				 * lifetimes of an existing temporary address
17501da177e4SLinus Torvalds 				 * when processing a Prefix Information Option.
17511da177e4SLinus Torvalds 				 */
17521da177e4SLinus Torvalds 				spin_lock(&ift->lock);
17531da177e4SLinus Torvalds 				flags = ift->flags;
17541da177e4SLinus Torvalds 				if (ift->valid_lft > valid_lft &&
17551da177e4SLinus Torvalds 				    ift->valid_lft - valid_lft > (jiffies - ift->tstamp) / HZ)
17561da177e4SLinus Torvalds 					ift->valid_lft = valid_lft + (jiffies - ift->tstamp) / HZ;
17571da177e4SLinus Torvalds 				if (ift->prefered_lft > prefered_lft &&
17581da177e4SLinus Torvalds 				    ift->prefered_lft - prefered_lft > (jiffies - ift->tstamp) / HZ)
17591da177e4SLinus Torvalds 					ift->prefered_lft = prefered_lft + (jiffies - ift->tstamp) / HZ;
17601da177e4SLinus Torvalds 				spin_unlock(&ift->lock);
17611da177e4SLinus Torvalds 				if (!(flags&IFA_F_TENTATIVE))
17621da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ift);
17631da177e4SLinus Torvalds 			}
17641da177e4SLinus Torvalds 
17651da177e4SLinus Torvalds 			if (create && in6_dev->cnf.use_tempaddr > 0) {
17661da177e4SLinus Torvalds 				/*
17671da177e4SLinus Torvalds 				 * When a new public address is created as described in [ADDRCONF],
17681da177e4SLinus Torvalds 				 * also create a new temporary address.
17691da177e4SLinus Torvalds 				 */
17701da177e4SLinus Torvalds 				read_unlock_bh(&in6_dev->lock);
17711da177e4SLinus Torvalds 				ipv6_create_tempaddr(ifp, NULL);
17721da177e4SLinus Torvalds 			} else {
17731da177e4SLinus Torvalds 				read_unlock_bh(&in6_dev->lock);
17741da177e4SLinus Torvalds 			}
17751da177e4SLinus Torvalds #endif
17761da177e4SLinus Torvalds 			in6_ifa_put(ifp);
17771da177e4SLinus Torvalds 			addrconf_verify(0);
17781da177e4SLinus Torvalds 		}
17791da177e4SLinus Torvalds 	}
17801da177e4SLinus Torvalds 	inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
17811da177e4SLinus Torvalds 	in6_dev_put(in6_dev);
17821da177e4SLinus Torvalds }
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds /*
17851da177e4SLinus Torvalds  *	Set destination address.
17861da177e4SLinus Torvalds  *	Special case for SIT interfaces where we create a new "virtual"
17871da177e4SLinus Torvalds  *	device.
17881da177e4SLinus Torvalds  */
17891da177e4SLinus Torvalds int addrconf_set_dstaddr(void __user *arg)
17901da177e4SLinus Torvalds {
17911da177e4SLinus Torvalds 	struct in6_ifreq ireq;
17921da177e4SLinus Torvalds 	struct net_device *dev;
17931da177e4SLinus Torvalds 	int err = -EINVAL;
17941da177e4SLinus Torvalds 
17951da177e4SLinus Torvalds 	rtnl_lock();
17961da177e4SLinus Torvalds 
17971da177e4SLinus Torvalds 	err = -EFAULT;
17981da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
17991da177e4SLinus Torvalds 		goto err_exit;
18001da177e4SLinus Torvalds 
18011da177e4SLinus Torvalds 	dev = __dev_get_by_index(ireq.ifr6_ifindex);
18021da177e4SLinus Torvalds 
18031da177e4SLinus Torvalds 	err = -ENODEV;
18041da177e4SLinus Torvalds 	if (dev == NULL)
18051da177e4SLinus Torvalds 		goto err_exit;
18061da177e4SLinus Torvalds 
18071da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT) {
18081da177e4SLinus Torvalds 		struct ifreq ifr;
18091da177e4SLinus Torvalds 		mm_segment_t	oldfs;
18101da177e4SLinus Torvalds 		struct ip_tunnel_parm p;
18111da177e4SLinus Torvalds 
18121da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
18131da177e4SLinus Torvalds 		if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
18141da177e4SLinus Torvalds 			goto err_exit;
18151da177e4SLinus Torvalds 
18161da177e4SLinus Torvalds 		memset(&p, 0, sizeof(p));
18171da177e4SLinus Torvalds 		p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
18181da177e4SLinus Torvalds 		p.iph.saddr = 0;
18191da177e4SLinus Torvalds 		p.iph.version = 4;
18201da177e4SLinus Torvalds 		p.iph.ihl = 5;
18211da177e4SLinus Torvalds 		p.iph.protocol = IPPROTO_IPV6;
18221da177e4SLinus Torvalds 		p.iph.ttl = 64;
18231da177e4SLinus Torvalds 		ifr.ifr_ifru.ifru_data = (void __user *)&p;
18241da177e4SLinus Torvalds 
18251da177e4SLinus Torvalds 		oldfs = get_fs(); set_fs(KERNEL_DS);
18261da177e4SLinus Torvalds 		err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
18271da177e4SLinus Torvalds 		set_fs(oldfs);
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds 		if (err == 0) {
18301da177e4SLinus Torvalds 			err = -ENOBUFS;
18311da177e4SLinus Torvalds 			if ((dev = __dev_get_by_name(p.name)) == NULL)
18321da177e4SLinus Torvalds 				goto err_exit;
18331da177e4SLinus Torvalds 			err = dev_open(dev);
18341da177e4SLinus Torvalds 		}
18351da177e4SLinus Torvalds 	}
18361da177e4SLinus Torvalds 
18371da177e4SLinus Torvalds err_exit:
18381da177e4SLinus Torvalds 	rtnl_unlock();
18391da177e4SLinus Torvalds 	return err;
18401da177e4SLinus Torvalds }
18411da177e4SLinus Torvalds 
18421da177e4SLinus Torvalds /*
18431da177e4SLinus Torvalds  *	Manual configuration of address on an interface
18441da177e4SLinus Torvalds  */
18451da177e4SLinus Torvalds static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen)
18461da177e4SLinus Torvalds {
18471da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
18481da177e4SLinus Torvalds 	struct inet6_dev *idev;
18491da177e4SLinus Torvalds 	struct net_device *dev;
18501da177e4SLinus Torvalds 	int scope;
18511da177e4SLinus Torvalds 
18521da177e4SLinus Torvalds 	ASSERT_RTNL();
18531da177e4SLinus Torvalds 
18541da177e4SLinus Torvalds 	if ((dev = __dev_get_by_index(ifindex)) == NULL)
18551da177e4SLinus Torvalds 		return -ENODEV;
18561da177e4SLinus Torvalds 
18571da177e4SLinus Torvalds 	if (!(dev->flags&IFF_UP))
18581da177e4SLinus Torvalds 		return -ENETDOWN;
18591da177e4SLinus Torvalds 
18601da177e4SLinus Torvalds 	if ((idev = addrconf_add_dev(dev)) == NULL)
18611da177e4SLinus Torvalds 		return -ENOBUFS;
18621da177e4SLinus Torvalds 
18631da177e4SLinus Torvalds 	scope = ipv6_addr_scope(pfx);
18641da177e4SLinus Torvalds 
18651da177e4SLinus Torvalds 	ifp = ipv6_add_addr(idev, pfx, plen, scope, IFA_F_PERMANENT);
18661da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
18671da177e4SLinus Torvalds 		addrconf_dad_start(ifp, 0);
18681da177e4SLinus Torvalds 		in6_ifa_put(ifp);
18691da177e4SLinus Torvalds 		return 0;
18701da177e4SLinus Torvalds 	}
18711da177e4SLinus Torvalds 
18721da177e4SLinus Torvalds 	return PTR_ERR(ifp);
18731da177e4SLinus Torvalds }
18741da177e4SLinus Torvalds 
18751da177e4SLinus Torvalds static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
18761da177e4SLinus Torvalds {
18771da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
18781da177e4SLinus Torvalds 	struct inet6_dev *idev;
18791da177e4SLinus Torvalds 	struct net_device *dev;
18801da177e4SLinus Torvalds 
18811da177e4SLinus Torvalds 	if ((dev = __dev_get_by_index(ifindex)) == NULL)
18821da177e4SLinus Torvalds 		return -ENODEV;
18831da177e4SLinus Torvalds 
18841da177e4SLinus Torvalds 	if ((idev = __in6_dev_get(dev)) == NULL)
18851da177e4SLinus Torvalds 		return -ENXIO;
18861da177e4SLinus Torvalds 
18871da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
18881da177e4SLinus Torvalds 	for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
18891da177e4SLinus Torvalds 		if (ifp->prefix_len == plen &&
18901da177e4SLinus Torvalds 		    ipv6_addr_equal(pfx, &ifp->addr)) {
18911da177e4SLinus Torvalds 			in6_ifa_hold(ifp);
18921da177e4SLinus Torvalds 			read_unlock_bh(&idev->lock);
18931da177e4SLinus Torvalds 
18941da177e4SLinus Torvalds 			ipv6_del_addr(ifp);
18951da177e4SLinus Torvalds 
18961da177e4SLinus Torvalds 			/* If the last address is deleted administratively,
18971da177e4SLinus Torvalds 			   disable IPv6 on this interface.
18981da177e4SLinus Torvalds 			 */
18991da177e4SLinus Torvalds 			if (idev->addr_list == NULL)
19001da177e4SLinus Torvalds 				addrconf_ifdown(idev->dev, 1);
19011da177e4SLinus Torvalds 			return 0;
19021da177e4SLinus Torvalds 		}
19031da177e4SLinus Torvalds 	}
19041da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
19051da177e4SLinus Torvalds 	return -EADDRNOTAVAIL;
19061da177e4SLinus Torvalds }
19071da177e4SLinus Torvalds 
19081da177e4SLinus Torvalds 
19091da177e4SLinus Torvalds int addrconf_add_ifaddr(void __user *arg)
19101da177e4SLinus Torvalds {
19111da177e4SLinus Torvalds 	struct in6_ifreq ireq;
19121da177e4SLinus Torvalds 	int err;
19131da177e4SLinus Torvalds 
19141da177e4SLinus Torvalds 	if (!capable(CAP_NET_ADMIN))
19151da177e4SLinus Torvalds 		return -EPERM;
19161da177e4SLinus Torvalds 
19171da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
19181da177e4SLinus Torvalds 		return -EFAULT;
19191da177e4SLinus Torvalds 
19201da177e4SLinus Torvalds 	rtnl_lock();
19211da177e4SLinus Torvalds 	err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
19221da177e4SLinus Torvalds 	rtnl_unlock();
19231da177e4SLinus Torvalds 	return err;
19241da177e4SLinus Torvalds }
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds int addrconf_del_ifaddr(void __user *arg)
19271da177e4SLinus Torvalds {
19281da177e4SLinus Torvalds 	struct in6_ifreq ireq;
19291da177e4SLinus Torvalds 	int err;
19301da177e4SLinus Torvalds 
19311da177e4SLinus Torvalds 	if (!capable(CAP_NET_ADMIN))
19321da177e4SLinus Torvalds 		return -EPERM;
19331da177e4SLinus Torvalds 
19341da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
19351da177e4SLinus Torvalds 		return -EFAULT;
19361da177e4SLinus Torvalds 
19371da177e4SLinus Torvalds 	rtnl_lock();
19381da177e4SLinus Torvalds 	err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
19391da177e4SLinus Torvalds 	rtnl_unlock();
19401da177e4SLinus Torvalds 	return err;
19411da177e4SLinus Torvalds }
19421da177e4SLinus Torvalds 
19431da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev)
19441da177e4SLinus Torvalds {
19451da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
19461da177e4SLinus Torvalds 	struct in6_addr addr;
19471da177e4SLinus Torvalds 	struct net_device *dev;
19481da177e4SLinus Torvalds 	int scope;
19491da177e4SLinus Torvalds 
19501da177e4SLinus Torvalds 	ASSERT_RTNL();
19511da177e4SLinus Torvalds 
19521da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
19531da177e4SLinus Torvalds 	memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
19541da177e4SLinus Torvalds 
19551da177e4SLinus Torvalds 	if (idev->dev->flags&IFF_POINTOPOINT) {
19561da177e4SLinus Torvalds 		addr.s6_addr32[0] = htonl(0xfe800000);
19571da177e4SLinus Torvalds 		scope = IFA_LINK;
19581da177e4SLinus Torvalds 	} else {
19591da177e4SLinus Torvalds 		scope = IPV6_ADDR_COMPATv4;
19601da177e4SLinus Torvalds 	}
19611da177e4SLinus Torvalds 
19621da177e4SLinus Torvalds 	if (addr.s6_addr32[3]) {
19631da177e4SLinus Torvalds 		ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
19641da177e4SLinus Torvalds 		if (!IS_ERR(ifp)) {
19651da177e4SLinus Torvalds 			spin_lock_bh(&ifp->lock);
19661da177e4SLinus Torvalds 			ifp->flags &= ~IFA_F_TENTATIVE;
19671da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
19681da177e4SLinus Torvalds 			ipv6_ifa_notify(RTM_NEWADDR, ifp);
19691da177e4SLinus Torvalds 			in6_ifa_put(ifp);
19701da177e4SLinus Torvalds 		}
19711da177e4SLinus Torvalds 		return;
19721da177e4SLinus Torvalds 	}
19731da177e4SLinus Torvalds 
19741da177e4SLinus Torvalds         for (dev = dev_base; dev != NULL; dev = dev->next) {
1975e5ed6399SHerbert Xu 		struct in_device * in_dev = __in_dev_get_rtnl(dev);
19761da177e4SLinus Torvalds 		if (in_dev && (dev->flags & IFF_UP)) {
19771da177e4SLinus Torvalds 			struct in_ifaddr * ifa;
19781da177e4SLinus Torvalds 
19791da177e4SLinus Torvalds 			int flag = scope;
19801da177e4SLinus Torvalds 
19811da177e4SLinus Torvalds 			for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
19821da177e4SLinus Torvalds 				int plen;
19831da177e4SLinus Torvalds 
19841da177e4SLinus Torvalds 				addr.s6_addr32[3] = ifa->ifa_local;
19851da177e4SLinus Torvalds 
19861da177e4SLinus Torvalds 				if (ifa->ifa_scope == RT_SCOPE_LINK)
19871da177e4SLinus Torvalds 					continue;
19881da177e4SLinus Torvalds 				if (ifa->ifa_scope >= RT_SCOPE_HOST) {
19891da177e4SLinus Torvalds 					if (idev->dev->flags&IFF_POINTOPOINT)
19901da177e4SLinus Torvalds 						continue;
19911da177e4SLinus Torvalds 					flag |= IFA_HOST;
19921da177e4SLinus Torvalds 				}
19931da177e4SLinus Torvalds 				if (idev->dev->flags&IFF_POINTOPOINT)
19941da177e4SLinus Torvalds 					plen = 64;
19951da177e4SLinus Torvalds 				else
19961da177e4SLinus Torvalds 					plen = 96;
19971da177e4SLinus Torvalds 
19981da177e4SLinus Torvalds 				ifp = ipv6_add_addr(idev, &addr, plen, flag,
19991da177e4SLinus Torvalds 						    IFA_F_PERMANENT);
20001da177e4SLinus Torvalds 				if (!IS_ERR(ifp)) {
20011da177e4SLinus Torvalds 					spin_lock_bh(&ifp->lock);
20021da177e4SLinus Torvalds 					ifp->flags &= ~IFA_F_TENTATIVE;
20031da177e4SLinus Torvalds 					spin_unlock_bh(&ifp->lock);
20041da177e4SLinus Torvalds 					ipv6_ifa_notify(RTM_NEWADDR, ifp);
20051da177e4SLinus Torvalds 					in6_ifa_put(ifp);
20061da177e4SLinus Torvalds 				}
20071da177e4SLinus Torvalds 			}
20081da177e4SLinus Torvalds 		}
20091da177e4SLinus Torvalds         }
20101da177e4SLinus Torvalds }
20111da177e4SLinus Torvalds 
20121da177e4SLinus Torvalds static void init_loopback(struct net_device *dev)
20131da177e4SLinus Torvalds {
20141da177e4SLinus Torvalds 	struct inet6_dev  *idev;
20151da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
20161da177e4SLinus Torvalds 
20171da177e4SLinus Torvalds 	/* ::1 */
20181da177e4SLinus Torvalds 
20191da177e4SLinus Torvalds 	ASSERT_RTNL();
20201da177e4SLinus Torvalds 
20211da177e4SLinus Torvalds 	if ((idev = ipv6_find_idev(dev)) == NULL) {
20221da177e4SLinus Torvalds 		printk(KERN_DEBUG "init loopback: add_dev failed\n");
20231da177e4SLinus Torvalds 		return;
20241da177e4SLinus Torvalds 	}
20251da177e4SLinus Torvalds 
20261da177e4SLinus Torvalds 	ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
20271da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
20281da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
20291da177e4SLinus Torvalds 		ifp->flags &= ~IFA_F_TENTATIVE;
20301da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
20311da177e4SLinus Torvalds 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
20321da177e4SLinus Torvalds 		in6_ifa_put(ifp);
20331da177e4SLinus Torvalds 	}
20341da177e4SLinus Torvalds }
20351da177e4SLinus Torvalds 
20361da177e4SLinus Torvalds static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr)
20371da177e4SLinus Torvalds {
20381da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
20391da177e4SLinus Torvalds 
20401da177e4SLinus Torvalds 	ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
20411da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
20421da177e4SLinus Torvalds 		addrconf_dad_start(ifp, 0);
20431da177e4SLinus Torvalds 		in6_ifa_put(ifp);
20441da177e4SLinus Torvalds 	}
20451da177e4SLinus Torvalds }
20461da177e4SLinus Torvalds 
20471da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev)
20481da177e4SLinus Torvalds {
20491da177e4SLinus Torvalds 	struct in6_addr addr;
20501da177e4SLinus Torvalds 	struct inet6_dev    * idev;
20511da177e4SLinus Torvalds 
20521da177e4SLinus Torvalds 	ASSERT_RTNL();
20531da177e4SLinus Torvalds 
20541da177e4SLinus Torvalds 	if ((dev->type != ARPHRD_ETHER) &&
20551da177e4SLinus Torvalds 	    (dev->type != ARPHRD_FDDI) &&
20561da177e4SLinus Torvalds 	    (dev->type != ARPHRD_IEEE802_TR) &&
20571da177e4SLinus Torvalds 	    (dev->type != ARPHRD_ARCNET) &&
20581da177e4SLinus Torvalds 	    (dev->type != ARPHRD_INFINIBAND)) {
20591da177e4SLinus Torvalds 		/* Alas, we support only Ethernet autoconfiguration. */
20601da177e4SLinus Torvalds 		return;
20611da177e4SLinus Torvalds 	}
20621da177e4SLinus Torvalds 
20631da177e4SLinus Torvalds 	idev = addrconf_add_dev(dev);
20641da177e4SLinus Torvalds 	if (idev == NULL)
20651da177e4SLinus Torvalds 		return;
20661da177e4SLinus Torvalds 
20671da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
20681da177e4SLinus Torvalds 	addr.s6_addr32[0] = htonl(0xFE800000);
20691da177e4SLinus Torvalds 
20701da177e4SLinus Torvalds 	if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
20711da177e4SLinus Torvalds 		addrconf_add_linklocal(idev, &addr);
20721da177e4SLinus Torvalds }
20731da177e4SLinus Torvalds 
20741da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev)
20751da177e4SLinus Torvalds {
20761da177e4SLinus Torvalds 	struct inet6_dev *idev;
20771da177e4SLinus Torvalds 
20781da177e4SLinus Torvalds 	ASSERT_RTNL();
20791da177e4SLinus Torvalds 
20801da177e4SLinus Torvalds 	/*
20811da177e4SLinus Torvalds 	 * Configure the tunnel with one of our IPv4
20821da177e4SLinus Torvalds 	 * addresses... we should configure all of
20831da177e4SLinus Torvalds 	 * our v4 addrs in the tunnel
20841da177e4SLinus Torvalds 	 */
20851da177e4SLinus Torvalds 
20861da177e4SLinus Torvalds 	if ((idev = ipv6_find_idev(dev)) == NULL) {
20871da177e4SLinus Torvalds 		printk(KERN_DEBUG "init sit: add_dev failed\n");
20881da177e4SLinus Torvalds 		return;
20891da177e4SLinus Torvalds 	}
20901da177e4SLinus Torvalds 
20911da177e4SLinus Torvalds 	sit_add_v4_addrs(idev);
20921da177e4SLinus Torvalds 
20931da177e4SLinus Torvalds 	if (dev->flags&IFF_POINTOPOINT) {
20941da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
20951da177e4SLinus Torvalds 		addrconf_add_lroute(dev);
20961da177e4SLinus Torvalds 	} else
20971da177e4SLinus Torvalds 		sit_route_add(dev);
20981da177e4SLinus Torvalds }
20991da177e4SLinus Torvalds 
21001da177e4SLinus Torvalds static inline int
21011da177e4SLinus Torvalds ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
21021da177e4SLinus Torvalds {
21031da177e4SLinus Torvalds 	struct in6_addr lladdr;
21041da177e4SLinus Torvalds 
21051da177e4SLinus Torvalds 	if (!ipv6_get_lladdr(link_dev, &lladdr)) {
21061da177e4SLinus Torvalds 		addrconf_add_linklocal(idev, &lladdr);
21071da177e4SLinus Torvalds 		return 0;
21081da177e4SLinus Torvalds 	}
21091da177e4SLinus Torvalds 	return -1;
21101da177e4SLinus Torvalds }
21111da177e4SLinus Torvalds 
21121da177e4SLinus Torvalds static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
21131da177e4SLinus Torvalds {
21141da177e4SLinus Torvalds 	struct net_device *link_dev;
21151da177e4SLinus Torvalds 
21161da177e4SLinus Torvalds 	/* first try to inherit the link-local address from the link device */
21171da177e4SLinus Torvalds 	if (idev->dev->iflink &&
21181da177e4SLinus Torvalds 	    (link_dev = __dev_get_by_index(idev->dev->iflink))) {
21191da177e4SLinus Torvalds 		if (!ipv6_inherit_linklocal(idev, link_dev))
21201da177e4SLinus Torvalds 			return;
21211da177e4SLinus Torvalds 	}
21221da177e4SLinus Torvalds 	/* then try to inherit it from any device */
21231da177e4SLinus Torvalds 	for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
21241da177e4SLinus Torvalds 		if (!ipv6_inherit_linklocal(idev, link_dev))
21251da177e4SLinus Torvalds 			return;
21261da177e4SLinus Torvalds 	}
21271da177e4SLinus Torvalds 	printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
21281da177e4SLinus Torvalds }
21291da177e4SLinus Torvalds 
21301da177e4SLinus Torvalds /*
21311da177e4SLinus Torvalds  * Autoconfigure tunnel with a link-local address so routing protocols,
21321da177e4SLinus Torvalds  * DHCPv6, MLD etc. can be run over the virtual link
21331da177e4SLinus Torvalds  */
21341da177e4SLinus Torvalds 
21351da177e4SLinus Torvalds static void addrconf_ip6_tnl_config(struct net_device *dev)
21361da177e4SLinus Torvalds {
21371da177e4SLinus Torvalds 	struct inet6_dev *idev;
21381da177e4SLinus Torvalds 
21391da177e4SLinus Torvalds 	ASSERT_RTNL();
21401da177e4SLinus Torvalds 
21411da177e4SLinus Torvalds 	if ((idev = addrconf_add_dev(dev)) == NULL) {
21421da177e4SLinus Torvalds 		printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
21431da177e4SLinus Torvalds 		return;
21441da177e4SLinus Torvalds 	}
21451da177e4SLinus Torvalds 	ip6_tnl_add_linklocal(idev);
21461da177e4SLinus Torvalds 	addrconf_add_mroute(dev);
21471da177e4SLinus Torvalds }
21481da177e4SLinus Torvalds 
21491da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event,
21501da177e4SLinus Torvalds 			   void * data)
21511da177e4SLinus Torvalds {
21521da177e4SLinus Torvalds 	struct net_device *dev = (struct net_device *) data;
21531da177e4SLinus Torvalds 	struct inet6_dev *idev = __in6_dev_get(dev);
2154c5e33bddSYOSHIFUJI Hideaki 	int run_pending = 0;
21551da177e4SLinus Torvalds 
21561da177e4SLinus Torvalds 	switch(event) {
21571da177e4SLinus Torvalds 	case NETDEV_UP:
21583c21edbdSYOSHIFUJI Hideaki 	case NETDEV_CHANGE:
21593c21edbdSYOSHIFUJI Hideaki 		if (event == NETDEV_UP) {
21603c21edbdSYOSHIFUJI Hideaki 			if (!netif_carrier_ok(dev)) {
21613c21edbdSYOSHIFUJI Hideaki 				/* device is not ready yet. */
21623c21edbdSYOSHIFUJI Hideaki 				printk(KERN_INFO
21633c21edbdSYOSHIFUJI Hideaki 					"ADDRCONF(NETDEV_UP): %s: "
21643c21edbdSYOSHIFUJI Hideaki 					"link is not ready\n",
21653c21edbdSYOSHIFUJI Hideaki 					dev->name);
21663c21edbdSYOSHIFUJI Hideaki 				break;
21673c21edbdSYOSHIFUJI Hideaki 			}
21683c21edbdSYOSHIFUJI Hideaki 		} else {
21693c21edbdSYOSHIFUJI Hideaki 			if (!netif_carrier_ok(dev)) {
21703c21edbdSYOSHIFUJI Hideaki 				/* device is still not ready. */
21713c21edbdSYOSHIFUJI Hideaki 				break;
21723c21edbdSYOSHIFUJI Hideaki 			}
21733c21edbdSYOSHIFUJI Hideaki 
21743c21edbdSYOSHIFUJI Hideaki 			if (idev) {
21753c21edbdSYOSHIFUJI Hideaki 				if (idev->if_flags & IF_READY) {
21763c21edbdSYOSHIFUJI Hideaki 					/* device is already configured. */
21773c21edbdSYOSHIFUJI Hideaki 					break;
21783c21edbdSYOSHIFUJI Hideaki 				}
21793c21edbdSYOSHIFUJI Hideaki 				idev->if_flags |= IF_READY;
21803c21edbdSYOSHIFUJI Hideaki 			}
21813c21edbdSYOSHIFUJI Hideaki 
21823c21edbdSYOSHIFUJI Hideaki 			printk(KERN_INFO
21833c21edbdSYOSHIFUJI Hideaki 					"ADDRCONF(NETDEV_CHANGE): %s: "
21843c21edbdSYOSHIFUJI Hideaki 					"link becomes ready\n",
21853c21edbdSYOSHIFUJI Hideaki 					dev->name);
21863c21edbdSYOSHIFUJI Hideaki 
2187c5e33bddSYOSHIFUJI Hideaki 			run_pending = 1;
21883c21edbdSYOSHIFUJI Hideaki 		}
21893c21edbdSYOSHIFUJI Hideaki 
21901da177e4SLinus Torvalds 		switch(dev->type) {
21911da177e4SLinus Torvalds 		case ARPHRD_SIT:
21921da177e4SLinus Torvalds 			addrconf_sit_config(dev);
21931da177e4SLinus Torvalds 			break;
21941da177e4SLinus Torvalds 		case ARPHRD_TUNNEL6:
21951da177e4SLinus Torvalds 			addrconf_ip6_tnl_config(dev);
21961da177e4SLinus Torvalds 			break;
21971da177e4SLinus Torvalds 		case ARPHRD_LOOPBACK:
21981da177e4SLinus Torvalds 			init_loopback(dev);
21991da177e4SLinus Torvalds 			break;
22001da177e4SLinus Torvalds 
22011da177e4SLinus Torvalds 		default:
22021da177e4SLinus Torvalds 			addrconf_dev_config(dev);
22031da177e4SLinus Torvalds 			break;
22041da177e4SLinus Torvalds 		};
22051da177e4SLinus Torvalds 		if (idev) {
2206c5e33bddSYOSHIFUJI Hideaki 			if (run_pending)
2207c5e33bddSYOSHIFUJI Hideaki 				addrconf_dad_run(idev);
2208c5e33bddSYOSHIFUJI Hideaki 
22091da177e4SLinus Torvalds 			/* If the MTU changed during the interface down, when the
22101da177e4SLinus Torvalds 			   interface up, the changed MTU must be reflected in the
22111da177e4SLinus Torvalds 			   idev as well as routers.
22121da177e4SLinus Torvalds 			 */
22131da177e4SLinus Torvalds 			if (idev->cnf.mtu6 != dev->mtu && dev->mtu >= IPV6_MIN_MTU) {
22141da177e4SLinus Torvalds 				rt6_mtu_change(dev, dev->mtu);
22151da177e4SLinus Torvalds 				idev->cnf.mtu6 = dev->mtu;
22161da177e4SLinus Torvalds 			}
22171da177e4SLinus Torvalds 			idev->tstamp = jiffies;
22181da177e4SLinus Torvalds 			inet6_ifinfo_notify(RTM_NEWLINK, idev);
22191da177e4SLinus Torvalds 			/* If the changed mtu during down is lower than IPV6_MIN_MTU
22201da177e4SLinus Torvalds 			   stop IPv6 on this interface.
22211da177e4SLinus Torvalds 			 */
22221da177e4SLinus Torvalds 			if (dev->mtu < IPV6_MIN_MTU)
22231da177e4SLinus Torvalds 				addrconf_ifdown(dev, event != NETDEV_DOWN);
22241da177e4SLinus Torvalds 		}
22251da177e4SLinus Torvalds 		break;
22261da177e4SLinus Torvalds 
22271da177e4SLinus Torvalds 	case NETDEV_CHANGEMTU:
22281da177e4SLinus Torvalds 		if ( idev && dev->mtu >= IPV6_MIN_MTU) {
22291da177e4SLinus Torvalds 			rt6_mtu_change(dev, dev->mtu);
22301da177e4SLinus Torvalds 			idev->cnf.mtu6 = dev->mtu;
22311da177e4SLinus Torvalds 			break;
22321da177e4SLinus Torvalds 		}
22331da177e4SLinus Torvalds 
22341da177e4SLinus Torvalds 		/* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
22351da177e4SLinus Torvalds 
22361da177e4SLinus Torvalds 	case NETDEV_DOWN:
22371da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
22381da177e4SLinus Torvalds 		/*
22391da177e4SLinus Torvalds 		 *	Remove all addresses from this interface.
22401da177e4SLinus Torvalds 		 */
22411da177e4SLinus Torvalds 		addrconf_ifdown(dev, event != NETDEV_DOWN);
22421da177e4SLinus Torvalds 		break;
22433c21edbdSYOSHIFUJI Hideaki 
22441da177e4SLinus Torvalds 	case NETDEV_CHANGENAME:
22451da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
22461da177e4SLinus Torvalds 		if (idev) {
22471da177e4SLinus Torvalds 			addrconf_sysctl_unregister(&idev->cnf);
22481da177e4SLinus Torvalds 			neigh_sysctl_unregister(idev->nd_parms);
22491da177e4SLinus Torvalds 			neigh_sysctl_register(dev, idev->nd_parms,
22501da177e4SLinus Torvalds 					      NET_IPV6, NET_IPV6_NEIGH, "ipv6",
22511da177e4SLinus Torvalds 					      &ndisc_ifinfo_sysctl_change,
22521da177e4SLinus Torvalds 					      NULL);
22531da177e4SLinus Torvalds 			addrconf_sysctl_register(idev, &idev->cnf);
22541da177e4SLinus Torvalds 		}
22551da177e4SLinus Torvalds #endif
22561da177e4SLinus Torvalds 		break;
22571da177e4SLinus Torvalds 	};
22581da177e4SLinus Torvalds 
22591da177e4SLinus Torvalds 	return NOTIFY_OK;
22601da177e4SLinus Torvalds }
22611da177e4SLinus Torvalds 
22621da177e4SLinus Torvalds /*
22631da177e4SLinus Torvalds  *	addrconf module should be notified of a device going up
22641da177e4SLinus Torvalds  */
22651da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = {
22661da177e4SLinus Torvalds 	.notifier_call = addrconf_notify,
22671da177e4SLinus Torvalds 	.priority = 0
22681da177e4SLinus Torvalds };
22691da177e4SLinus Torvalds 
22701da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how)
22711da177e4SLinus Torvalds {
22721da177e4SLinus Torvalds 	struct inet6_dev *idev;
22731da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa, **bifa;
22741da177e4SLinus Torvalds 	int i;
22751da177e4SLinus Torvalds 
22761da177e4SLinus Torvalds 	ASSERT_RTNL();
22771da177e4SLinus Torvalds 
22781da177e4SLinus Torvalds 	if (dev == &loopback_dev && how == 1)
22791da177e4SLinus Torvalds 		how = 0;
22801da177e4SLinus Torvalds 
22811da177e4SLinus Torvalds 	rt6_ifdown(dev);
22821da177e4SLinus Torvalds 	neigh_ifdown(&nd_tbl, dev);
22831da177e4SLinus Torvalds 
22841da177e4SLinus Torvalds 	idev = __in6_dev_get(dev);
22851da177e4SLinus Torvalds 	if (idev == NULL)
22861da177e4SLinus Torvalds 		return -ENODEV;
22871da177e4SLinus Torvalds 
22881da177e4SLinus Torvalds 	/* Step 1: remove reference to ipv6 device from parent device.
22891da177e4SLinus Torvalds 	           Do not dev_put!
22901da177e4SLinus Torvalds 	 */
22911da177e4SLinus Torvalds 	if (how == 1) {
22921da177e4SLinus Torvalds 		write_lock_bh(&addrconf_lock);
22931da177e4SLinus Torvalds 		dev->ip6_ptr = NULL;
22941da177e4SLinus Torvalds 		idev->dead = 1;
22951da177e4SLinus Torvalds 		write_unlock_bh(&addrconf_lock);
22961da177e4SLinus Torvalds 
22971da177e4SLinus Torvalds 		/* Step 1.5: remove snmp6 entry */
22981da177e4SLinus Torvalds 		snmp6_unregister_dev(idev);
22991da177e4SLinus Torvalds 
23001da177e4SLinus Torvalds 	}
23011da177e4SLinus Torvalds 
23021da177e4SLinus Torvalds 	/* Step 2: clear hash table */
23031da177e4SLinus Torvalds 	for (i=0; i<IN6_ADDR_HSIZE; i++) {
23041da177e4SLinus Torvalds 		bifa = &inet6_addr_lst[i];
23051da177e4SLinus Torvalds 
23061da177e4SLinus Torvalds 		write_lock_bh(&addrconf_hash_lock);
23071da177e4SLinus Torvalds 		while ((ifa = *bifa) != NULL) {
23081da177e4SLinus Torvalds 			if (ifa->idev == idev) {
23091da177e4SLinus Torvalds 				*bifa = ifa->lst_next;
23101da177e4SLinus Torvalds 				ifa->lst_next = NULL;
23111da177e4SLinus Torvalds 				addrconf_del_timer(ifa);
23121da177e4SLinus Torvalds 				in6_ifa_put(ifa);
23131da177e4SLinus Torvalds 				continue;
23141da177e4SLinus Torvalds 			}
23151da177e4SLinus Torvalds 			bifa = &ifa->lst_next;
23161da177e4SLinus Torvalds 		}
23171da177e4SLinus Torvalds 		write_unlock_bh(&addrconf_hash_lock);
23181da177e4SLinus Torvalds 	}
23191da177e4SLinus Torvalds 
23201da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
23211da177e4SLinus Torvalds 
23221da177e4SLinus Torvalds 	/* Step 3: clear flags for stateless addrconf */
23231da177e4SLinus Torvalds 	if (how != 1)
23243c21edbdSYOSHIFUJI Hideaki 		idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
23251da177e4SLinus Torvalds 
23261da177e4SLinus Torvalds 	/* Step 4: clear address list */
23271da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
23281da177e4SLinus Torvalds 	if (how == 1 && del_timer(&idev->regen_timer))
23291da177e4SLinus Torvalds 		in6_dev_put(idev);
23301da177e4SLinus Torvalds 
23311da177e4SLinus Torvalds 	/* clear tempaddr list */
23321da177e4SLinus Torvalds 	while ((ifa = idev->tempaddr_list) != NULL) {
23331da177e4SLinus Torvalds 		idev->tempaddr_list = ifa->tmp_next;
23341da177e4SLinus Torvalds 		ifa->tmp_next = NULL;
23351da177e4SLinus Torvalds 		ifa->dead = 1;
23361da177e4SLinus Torvalds 		write_unlock_bh(&idev->lock);
23371da177e4SLinus Torvalds 		spin_lock_bh(&ifa->lock);
23381da177e4SLinus Torvalds 
23391da177e4SLinus Torvalds 		if (ifa->ifpub) {
23401da177e4SLinus Torvalds 			in6_ifa_put(ifa->ifpub);
23411da177e4SLinus Torvalds 			ifa->ifpub = NULL;
23421da177e4SLinus Torvalds 		}
23431da177e4SLinus Torvalds 		spin_unlock_bh(&ifa->lock);
23441da177e4SLinus Torvalds 		in6_ifa_put(ifa);
23451da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
23461da177e4SLinus Torvalds 	}
23471da177e4SLinus Torvalds #endif
23481da177e4SLinus Torvalds 	while ((ifa = idev->addr_list) != NULL) {
23491da177e4SLinus Torvalds 		idev->addr_list = ifa->if_next;
23501da177e4SLinus Torvalds 		ifa->if_next = NULL;
23511da177e4SLinus Torvalds 		ifa->dead = 1;
23521da177e4SLinus Torvalds 		addrconf_del_timer(ifa);
23531da177e4SLinus Torvalds 		write_unlock_bh(&idev->lock);
23541da177e4SLinus Torvalds 
23551da177e4SLinus Torvalds 		__ipv6_ifa_notify(RTM_DELADDR, ifa);
23561da177e4SLinus Torvalds 		in6_ifa_put(ifa);
23571da177e4SLinus Torvalds 
23581da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
23591da177e4SLinus Torvalds 	}
23601da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
23611da177e4SLinus Torvalds 
23621da177e4SLinus Torvalds 	/* Step 5: Discard multicast list */
23631da177e4SLinus Torvalds 
23641da177e4SLinus Torvalds 	if (how == 1)
23651da177e4SLinus Torvalds 		ipv6_mc_destroy_dev(idev);
23661da177e4SLinus Torvalds 	else
23671da177e4SLinus Torvalds 		ipv6_mc_down(idev);
23681da177e4SLinus Torvalds 
23691da177e4SLinus Torvalds 	/* Step 5: netlink notification of this interface */
23701da177e4SLinus Torvalds 	idev->tstamp = jiffies;
2371979ad663SYan Zheng 	inet6_ifinfo_notify(RTM_DELLINK, idev);
23721da177e4SLinus Torvalds 
23731da177e4SLinus Torvalds 	/* Shot the device (if unregistered) */
23741da177e4SLinus Torvalds 
23751da177e4SLinus Torvalds 	if (how == 1) {
23761da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
23771da177e4SLinus Torvalds 		addrconf_sysctl_unregister(&idev->cnf);
23781da177e4SLinus Torvalds 		neigh_sysctl_unregister(idev->nd_parms);
23791da177e4SLinus Torvalds #endif
23801da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, idev->nd_parms);
23811da177e4SLinus Torvalds 		neigh_ifdown(&nd_tbl, dev);
23821da177e4SLinus Torvalds 		in6_dev_put(idev);
23831da177e4SLinus Torvalds 	}
23841da177e4SLinus Torvalds 	return 0;
23851da177e4SLinus Torvalds }
23861da177e4SLinus Torvalds 
23871da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data)
23881da177e4SLinus Torvalds {
23891da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
23901da177e4SLinus Torvalds 
23911da177e4SLinus Torvalds 	if (ifp->idev->cnf.forwarding)
23921da177e4SLinus Torvalds 		goto out;
23931da177e4SLinus Torvalds 
23941da177e4SLinus Torvalds 	if (ifp->idev->if_flags & IF_RA_RCVD) {
23951da177e4SLinus Torvalds 		/*
23961da177e4SLinus Torvalds 		 *	Announcement received after solicitation
23971da177e4SLinus Torvalds 		 *	was sent
23981da177e4SLinus Torvalds 		 */
23991da177e4SLinus Torvalds 		goto out;
24001da177e4SLinus Torvalds 	}
24011da177e4SLinus Torvalds 
24021da177e4SLinus Torvalds 	spin_lock(&ifp->lock);
24031da177e4SLinus Torvalds 	if (ifp->probes++ < ifp->idev->cnf.rtr_solicits) {
24041da177e4SLinus Torvalds 		struct in6_addr all_routers;
24051da177e4SLinus Torvalds 
24061da177e4SLinus Torvalds 		/* The wait after the last probe can be shorter */
24071da177e4SLinus Torvalds 		addrconf_mod_timer(ifp, AC_RS,
24081da177e4SLinus Torvalds 				   (ifp->probes == ifp->idev->cnf.rtr_solicits) ?
24091da177e4SLinus Torvalds 				   ifp->idev->cnf.rtr_solicit_delay :
24101da177e4SLinus Torvalds 				   ifp->idev->cnf.rtr_solicit_interval);
24111da177e4SLinus Torvalds 		spin_unlock(&ifp->lock);
24121da177e4SLinus Torvalds 
24131da177e4SLinus Torvalds 		ipv6_addr_all_routers(&all_routers);
24141da177e4SLinus Torvalds 
24151da177e4SLinus Torvalds 		ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
24161da177e4SLinus Torvalds 	} else {
24171da177e4SLinus Torvalds 		spin_unlock(&ifp->lock);
24181da177e4SLinus Torvalds 		/*
24191da177e4SLinus Torvalds 		 * Note: we do not support deprecated "all on-link"
24201da177e4SLinus Torvalds 		 * assumption any longer.
24211da177e4SLinus Torvalds 		 */
24221da177e4SLinus Torvalds 		printk(KERN_DEBUG "%s: no IPv6 routers present\n",
24231da177e4SLinus Torvalds 		       ifp->idev->dev->name);
24241da177e4SLinus Torvalds 	}
24251da177e4SLinus Torvalds 
24261da177e4SLinus Torvalds out:
24271da177e4SLinus Torvalds 	in6_ifa_put(ifp);
24281da177e4SLinus Torvalds }
24291da177e4SLinus Torvalds 
24301da177e4SLinus Torvalds /*
24311da177e4SLinus Torvalds  *	Duplicate Address Detection
24321da177e4SLinus Torvalds  */
24333c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
24343c21edbdSYOSHIFUJI Hideaki {
24353c21edbdSYOSHIFUJI Hideaki 	unsigned long rand_num;
24363c21edbdSYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
24373c21edbdSYOSHIFUJI Hideaki 
24383c21edbdSYOSHIFUJI Hideaki 	rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
24393c21edbdSYOSHIFUJI Hideaki 	ifp->probes = idev->cnf.dad_transmits;
24403c21edbdSYOSHIFUJI Hideaki 	addrconf_mod_timer(ifp, AC_DAD, rand_num);
24413c21edbdSYOSHIFUJI Hideaki }
24423c21edbdSYOSHIFUJI Hideaki 
2443e431b8c0SJamal Hadi Salim static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
24441da177e4SLinus Torvalds {
24451da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
24461da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
24471da177e4SLinus Torvalds 
24481da177e4SLinus Torvalds 	addrconf_join_solict(dev, &ifp->addr);
24491da177e4SLinus Torvalds 
24501da177e4SLinus Torvalds 	if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
24511da177e4SLinus Torvalds 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev, 0,
24521da177e4SLinus Torvalds 					flags);
24531da177e4SLinus Torvalds 
24541da177e4SLinus Torvalds 	net_srandom(ifp->addr.s6_addr32[3]);
24551da177e4SLinus Torvalds 
24561da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
24571da177e4SLinus Torvalds 	if (ifp->dead)
24581da177e4SLinus Torvalds 		goto out;
24591da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
24601da177e4SLinus Torvalds 
24611da177e4SLinus Torvalds 	if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
24621da177e4SLinus Torvalds 	    !(ifp->flags&IFA_F_TENTATIVE)) {
24631da177e4SLinus Torvalds 		ifp->flags &= ~IFA_F_TENTATIVE;
24641da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
24651da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
24661da177e4SLinus Torvalds 
24671da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
24681da177e4SLinus Torvalds 		return;
24691da177e4SLinus Torvalds 	}
24701da177e4SLinus Torvalds 
24716732badeSYOSHIFUJI Hideaki 	if (!(idev->if_flags & IF_READY)) {
24723dd3bf83SYOSHIFUJI Hideaki 		spin_unlock_bh(&ifp->lock);
24736732badeSYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
24743c21edbdSYOSHIFUJI Hideaki 		/*
24753c21edbdSYOSHIFUJI Hideaki 		 * If the defice is not ready:
24763c21edbdSYOSHIFUJI Hideaki 		 * - keep it tentative if it is a permanent address.
24773c21edbdSYOSHIFUJI Hideaki 		 * - otherwise, kill it.
24783c21edbdSYOSHIFUJI Hideaki 		 */
24793c21edbdSYOSHIFUJI Hideaki 		in6_ifa_hold(ifp);
24803c21edbdSYOSHIFUJI Hideaki 		addrconf_dad_stop(ifp);
24816732badeSYOSHIFUJI Hideaki 		return;
24823c21edbdSYOSHIFUJI Hideaki 	}
24836732badeSYOSHIFUJI Hideaki 	addrconf_dad_kick(ifp);
24846732badeSYOSHIFUJI Hideaki 	spin_unlock_bh(&ifp->lock);
24851da177e4SLinus Torvalds out:
24861da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
24871da177e4SLinus Torvalds }
24881da177e4SLinus Torvalds 
24891da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data)
24901da177e4SLinus Torvalds {
24911da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
24921da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
24931da177e4SLinus Torvalds 	struct in6_addr unspec;
24941da177e4SLinus Torvalds 	struct in6_addr mcaddr;
24951da177e4SLinus Torvalds 
24961da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
24971da177e4SLinus Torvalds 	if (idev->dead) {
24981da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
24991da177e4SLinus Torvalds 		goto out;
25001da177e4SLinus Torvalds 	}
25011da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
25021da177e4SLinus Torvalds 	if (ifp->probes == 0) {
25031da177e4SLinus Torvalds 		/*
25041da177e4SLinus Torvalds 		 * DAD was successful
25051da177e4SLinus Torvalds 		 */
25061da177e4SLinus Torvalds 
25071da177e4SLinus Torvalds 		ifp->flags &= ~IFA_F_TENTATIVE;
25081da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
25091da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
25101da177e4SLinus Torvalds 
25111da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
25121da177e4SLinus Torvalds 
25131da177e4SLinus Torvalds 		goto out;
25141da177e4SLinus Torvalds 	}
25151da177e4SLinus Torvalds 
25161da177e4SLinus Torvalds 	ifp->probes--;
25171da177e4SLinus Torvalds 	addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
25181da177e4SLinus Torvalds 	spin_unlock_bh(&ifp->lock);
25191da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
25201da177e4SLinus Torvalds 
25211da177e4SLinus Torvalds 	/* send a neighbour solicitation for our addr */
25221da177e4SLinus Torvalds 	memset(&unspec, 0, sizeof(unspec));
25231da177e4SLinus Torvalds 	addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
25241da177e4SLinus Torvalds 	ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
25251da177e4SLinus Torvalds out:
25261da177e4SLinus Torvalds 	in6_ifa_put(ifp);
25271da177e4SLinus Torvalds }
25281da177e4SLinus Torvalds 
25291da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
25301da177e4SLinus Torvalds {
25311da177e4SLinus Torvalds 	struct net_device *	dev = ifp->idev->dev;
25321da177e4SLinus Torvalds 
25331da177e4SLinus Torvalds 	/*
25341da177e4SLinus Torvalds 	 *	Configure the address for reception. Now it is valid.
25351da177e4SLinus Torvalds 	 */
25361da177e4SLinus Torvalds 
25371da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_NEWADDR, ifp);
25381da177e4SLinus Torvalds 
25391da177e4SLinus Torvalds 	/* If added prefix is link local and forwarding is off,
25401da177e4SLinus Torvalds 	   start sending router solicitations.
25411da177e4SLinus Torvalds 	 */
25421da177e4SLinus Torvalds 
25431da177e4SLinus Torvalds 	if (ifp->idev->cnf.forwarding == 0 &&
25441da177e4SLinus Torvalds 	    ifp->idev->cnf.rtr_solicits > 0 &&
25451da177e4SLinus Torvalds 	    (dev->flags&IFF_LOOPBACK) == 0 &&
25461da177e4SLinus Torvalds 	    (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
25471da177e4SLinus Torvalds 		struct in6_addr all_routers;
25481da177e4SLinus Torvalds 
25491da177e4SLinus Torvalds 		ipv6_addr_all_routers(&all_routers);
25501da177e4SLinus Torvalds 
25511da177e4SLinus Torvalds 		/*
25521da177e4SLinus Torvalds 		 *	If a host as already performed a random delay
25531da177e4SLinus Torvalds 		 *	[...] as part of DAD [...] there is no need
25541da177e4SLinus Torvalds 		 *	to delay again before sending the first RS
25551da177e4SLinus Torvalds 		 */
25561da177e4SLinus Torvalds 		ndisc_send_rs(ifp->idev->dev, &ifp->addr, &all_routers);
25571da177e4SLinus Torvalds 
25581da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
25591da177e4SLinus Torvalds 		ifp->probes = 1;
25601da177e4SLinus Torvalds 		ifp->idev->if_flags |= IF_RS_SENT;
25611da177e4SLinus Torvalds 		addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
25621da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
25631da177e4SLinus Torvalds 	}
25641da177e4SLinus Torvalds }
25651da177e4SLinus Torvalds 
2566c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev) {
2567c5e33bddSYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifp;
2568c5e33bddSYOSHIFUJI Hideaki 
2569c5e33bddSYOSHIFUJI Hideaki 	read_lock_bh(&idev->lock);
2570c5e33bddSYOSHIFUJI Hideaki 	for (ifp = idev->addr_list; ifp; ifp = ifp->if_next) {
2571c5e33bddSYOSHIFUJI Hideaki 		spin_lock_bh(&ifp->lock);
2572c5e33bddSYOSHIFUJI Hideaki 		if (!(ifp->flags & IFA_F_TENTATIVE)) {
2573c5e33bddSYOSHIFUJI Hideaki 			spin_unlock_bh(&ifp->lock);
2574c5e33bddSYOSHIFUJI Hideaki 			continue;
2575c5e33bddSYOSHIFUJI Hideaki 		}
2576c5e33bddSYOSHIFUJI Hideaki 		spin_unlock_bh(&ifp->lock);
2577c5e33bddSYOSHIFUJI Hideaki 		addrconf_dad_kick(ifp);
2578c5e33bddSYOSHIFUJI Hideaki 	}
2579c5e33bddSYOSHIFUJI Hideaki 	read_unlock_bh(&idev->lock);
2580c5e33bddSYOSHIFUJI Hideaki }
2581c5e33bddSYOSHIFUJI Hideaki 
25821da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
25831da177e4SLinus Torvalds struct if6_iter_state {
25841da177e4SLinus Torvalds 	int bucket;
25851da177e4SLinus Torvalds };
25861da177e4SLinus Torvalds 
25871da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
25881da177e4SLinus Torvalds {
25891da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
25901da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
25911da177e4SLinus Torvalds 
25921da177e4SLinus Torvalds 	for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
25931da177e4SLinus Torvalds 		ifa = inet6_addr_lst[state->bucket];
25941da177e4SLinus Torvalds 		if (ifa)
25951da177e4SLinus Torvalds 			break;
25961da177e4SLinus Torvalds 	}
25971da177e4SLinus Torvalds 	return ifa;
25981da177e4SLinus Torvalds }
25991da177e4SLinus Torvalds 
26001da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
26011da177e4SLinus Torvalds {
26021da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
26031da177e4SLinus Torvalds 
26041da177e4SLinus Torvalds 	ifa = ifa->lst_next;
26051da177e4SLinus Torvalds try_again:
26061da177e4SLinus Torvalds 	if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
26071da177e4SLinus Torvalds 		ifa = inet6_addr_lst[state->bucket];
26081da177e4SLinus Torvalds 		goto try_again;
26091da177e4SLinus Torvalds 	}
26101da177e4SLinus Torvalds 	return ifa;
26111da177e4SLinus Torvalds }
26121da177e4SLinus Torvalds 
26131da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
26141da177e4SLinus Torvalds {
26151da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = if6_get_first(seq);
26161da177e4SLinus Torvalds 
26171da177e4SLinus Torvalds 	if (ifa)
26181da177e4SLinus Torvalds 		while(pos && (ifa = if6_get_next(seq, ifa)) != NULL)
26191da177e4SLinus Torvalds 			--pos;
26201da177e4SLinus Torvalds 	return pos ? NULL : ifa;
26211da177e4SLinus Torvalds }
26221da177e4SLinus Torvalds 
26231da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
26241da177e4SLinus Torvalds {
26251da177e4SLinus Torvalds 	read_lock_bh(&addrconf_hash_lock);
26261da177e4SLinus Torvalds 	return if6_get_idx(seq, *pos);
26271da177e4SLinus Torvalds }
26281da177e4SLinus Torvalds 
26291da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
26301da177e4SLinus Torvalds {
26311da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
26321da177e4SLinus Torvalds 
26331da177e4SLinus Torvalds 	ifa = if6_get_next(seq, v);
26341da177e4SLinus Torvalds 	++*pos;
26351da177e4SLinus Torvalds 	return ifa;
26361da177e4SLinus Torvalds }
26371da177e4SLinus Torvalds 
26381da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v)
26391da177e4SLinus Torvalds {
26401da177e4SLinus Torvalds 	read_unlock_bh(&addrconf_hash_lock);
26411da177e4SLinus Torvalds }
26421da177e4SLinus Torvalds 
26431da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v)
26441da177e4SLinus Torvalds {
26451da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
26461da177e4SLinus Torvalds 	seq_printf(seq,
2647*9343e79aSYOSHIFUJI Hideaki 		   NIP6_SEQFMT " %02x %02x %02x %02x %8s\n",
26481da177e4SLinus Torvalds 		   NIP6(ifp->addr),
26491da177e4SLinus Torvalds 		   ifp->idev->dev->ifindex,
26501da177e4SLinus Torvalds 		   ifp->prefix_len,
26511da177e4SLinus Torvalds 		   ifp->scope,
26521da177e4SLinus Torvalds 		   ifp->flags,
26531da177e4SLinus Torvalds 		   ifp->idev->dev->name);
26541da177e4SLinus Torvalds 	return 0;
26551da177e4SLinus Torvalds }
26561da177e4SLinus Torvalds 
26571da177e4SLinus Torvalds static struct seq_operations if6_seq_ops = {
26581da177e4SLinus Torvalds 	.start	= if6_seq_start,
26591da177e4SLinus Torvalds 	.next	= if6_seq_next,
26601da177e4SLinus Torvalds 	.show	= if6_seq_show,
26611da177e4SLinus Torvalds 	.stop	= if6_seq_stop,
26621da177e4SLinus Torvalds };
26631da177e4SLinus Torvalds 
26641da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file)
26651da177e4SLinus Torvalds {
26661da177e4SLinus Torvalds 	struct seq_file *seq;
26671da177e4SLinus Torvalds 	int rc = -ENOMEM;
26681da177e4SLinus Torvalds 	struct if6_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
26691da177e4SLinus Torvalds 
26701da177e4SLinus Torvalds 	if (!s)
26711da177e4SLinus Torvalds 		goto out;
26721da177e4SLinus Torvalds 	memset(s, 0, sizeof(*s));
26731da177e4SLinus Torvalds 
26741da177e4SLinus Torvalds 	rc = seq_open(file, &if6_seq_ops);
26751da177e4SLinus Torvalds 	if (rc)
26761da177e4SLinus Torvalds 		goto out_kfree;
26771da177e4SLinus Torvalds 
26781da177e4SLinus Torvalds 	seq = file->private_data;
26791da177e4SLinus Torvalds 	seq->private = s;
26801da177e4SLinus Torvalds out:
26811da177e4SLinus Torvalds 	return rc;
26821da177e4SLinus Torvalds out_kfree:
26831da177e4SLinus Torvalds 	kfree(s);
26841da177e4SLinus Torvalds 	goto out;
26851da177e4SLinus Torvalds }
26861da177e4SLinus Torvalds 
26871da177e4SLinus Torvalds static struct file_operations if6_fops = {
26881da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
26891da177e4SLinus Torvalds 	.open		= if6_seq_open,
26901da177e4SLinus Torvalds 	.read		= seq_read,
26911da177e4SLinus Torvalds 	.llseek		= seq_lseek,
26921da177e4SLinus Torvalds 	.release	= seq_release_private,
26931da177e4SLinus Torvalds };
26941da177e4SLinus Torvalds 
26951da177e4SLinus Torvalds int __init if6_proc_init(void)
26961da177e4SLinus Torvalds {
26971da177e4SLinus Torvalds 	if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
26981da177e4SLinus Torvalds 		return -ENOMEM;
26991da177e4SLinus Torvalds 	return 0;
27001da177e4SLinus Torvalds }
27011da177e4SLinus Torvalds 
27021da177e4SLinus Torvalds void if6_proc_exit(void)
27031da177e4SLinus Torvalds {
27041da177e4SLinus Torvalds 	proc_net_remove("if_inet6");
27051da177e4SLinus Torvalds }
27061da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
27071da177e4SLinus Torvalds 
27081da177e4SLinus Torvalds /*
27091da177e4SLinus Torvalds  *	Periodic address status verification
27101da177e4SLinus Torvalds  */
27111da177e4SLinus Torvalds 
27121da177e4SLinus Torvalds static void addrconf_verify(unsigned long foo)
27131da177e4SLinus Torvalds {
27141da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
27151da177e4SLinus Torvalds 	unsigned long now, next;
27161da177e4SLinus Torvalds 	int i;
27171da177e4SLinus Torvalds 
27181da177e4SLinus Torvalds 	spin_lock_bh(&addrconf_verify_lock);
27191da177e4SLinus Torvalds 	now = jiffies;
27201da177e4SLinus Torvalds 	next = now + ADDR_CHECK_FREQUENCY;
27211da177e4SLinus Torvalds 
27221da177e4SLinus Torvalds 	del_timer(&addr_chk_timer);
27231da177e4SLinus Torvalds 
27241da177e4SLinus Torvalds 	for (i=0; i < IN6_ADDR_HSIZE; i++) {
27251da177e4SLinus Torvalds 
27261da177e4SLinus Torvalds restart:
27275d5780dfSYan Zheng 		read_lock(&addrconf_hash_lock);
27281da177e4SLinus Torvalds 		for (ifp=inet6_addr_lst[i]; ifp; ifp=ifp->lst_next) {
27291da177e4SLinus Torvalds 			unsigned long age;
27301da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
27311da177e4SLinus Torvalds 			unsigned long regen_advance;
27321da177e4SLinus Torvalds #endif
27331da177e4SLinus Torvalds 
27341da177e4SLinus Torvalds 			if (ifp->flags & IFA_F_PERMANENT)
27351da177e4SLinus Torvalds 				continue;
27361da177e4SLinus Torvalds 
27371da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
27381da177e4SLinus Torvalds 			age = (now - ifp->tstamp) / HZ;
27391da177e4SLinus Torvalds 
27401da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
27411da177e4SLinus Torvalds 			regen_advance = ifp->idev->cnf.regen_max_retry *
27421da177e4SLinus Torvalds 					ifp->idev->cnf.dad_transmits *
27431da177e4SLinus Torvalds 					ifp->idev->nd_parms->retrans_time / HZ;
27441da177e4SLinus Torvalds #endif
27451da177e4SLinus Torvalds 
27461da177e4SLinus Torvalds 			if (age >= ifp->valid_lft) {
27471da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
27481da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
27495d5780dfSYan Zheng 				read_unlock(&addrconf_hash_lock);
27501da177e4SLinus Torvalds 				ipv6_del_addr(ifp);
27511da177e4SLinus Torvalds 				goto restart;
27521da177e4SLinus Torvalds 			} else if (age >= ifp->prefered_lft) {
27531da177e4SLinus Torvalds 				/* jiffies - ifp->tsamp > age >= ifp->prefered_lft */
27541da177e4SLinus Torvalds 				int deprecate = 0;
27551da177e4SLinus Torvalds 
27561da177e4SLinus Torvalds 				if (!(ifp->flags&IFA_F_DEPRECATED)) {
27571da177e4SLinus Torvalds 					deprecate = 1;
27581da177e4SLinus Torvalds 					ifp->flags |= IFA_F_DEPRECATED;
27591da177e4SLinus Torvalds 				}
27601da177e4SLinus Torvalds 
27611da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
27621da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->valid_lft * HZ;
27631da177e4SLinus Torvalds 
27641da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
27651da177e4SLinus Torvalds 
27661da177e4SLinus Torvalds 				if (deprecate) {
27671da177e4SLinus Torvalds 					in6_ifa_hold(ifp);
27685d5780dfSYan Zheng 					read_unlock(&addrconf_hash_lock);
27691da177e4SLinus Torvalds 
27701da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
27711da177e4SLinus Torvalds 					in6_ifa_put(ifp);
27721da177e4SLinus Torvalds 					goto restart;
27731da177e4SLinus Torvalds 				}
27741da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
27751da177e4SLinus Torvalds 			} else if ((ifp->flags&IFA_F_TEMPORARY) &&
27761da177e4SLinus Torvalds 				   !(ifp->flags&IFA_F_TENTATIVE)) {
27771da177e4SLinus Torvalds 				if (age >= ifp->prefered_lft - regen_advance) {
27781da177e4SLinus Torvalds 					struct inet6_ifaddr *ifpub = ifp->ifpub;
27791da177e4SLinus Torvalds 					if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
27801da177e4SLinus Torvalds 						next = ifp->tstamp + ifp->prefered_lft * HZ;
27811da177e4SLinus Torvalds 					if (!ifp->regen_count && ifpub) {
27821da177e4SLinus Torvalds 						ifp->regen_count++;
27831da177e4SLinus Torvalds 						in6_ifa_hold(ifp);
27841da177e4SLinus Torvalds 						in6_ifa_hold(ifpub);
27851da177e4SLinus Torvalds 						spin_unlock(&ifp->lock);
27865d5780dfSYan Zheng 						read_unlock(&addrconf_hash_lock);
2787291d809bSHiroyuki YAMAMORI 						spin_lock(&ifpub->lock);
2788291d809bSHiroyuki YAMAMORI 						ifpub->regen_count = 0;
2789291d809bSHiroyuki YAMAMORI 						spin_unlock(&ifpub->lock);
27901da177e4SLinus Torvalds 						ipv6_create_tempaddr(ifpub, ifp);
27911da177e4SLinus Torvalds 						in6_ifa_put(ifpub);
27921da177e4SLinus Torvalds 						in6_ifa_put(ifp);
27931da177e4SLinus Torvalds 						goto restart;
27941da177e4SLinus Torvalds 					}
27951da177e4SLinus Torvalds 				} else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
27961da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
27971da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
27981da177e4SLinus Torvalds #endif
27991da177e4SLinus Torvalds 			} else {
28001da177e4SLinus Torvalds 				/* ifp->prefered_lft <= ifp->valid_lft */
28011da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
28021da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ;
28031da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
28041da177e4SLinus Torvalds 			}
28051da177e4SLinus Torvalds 		}
28065d5780dfSYan Zheng 		read_unlock(&addrconf_hash_lock);
28071da177e4SLinus Torvalds 	}
28081da177e4SLinus Torvalds 
28091da177e4SLinus Torvalds 	addr_chk_timer.expires = time_before(next, jiffies + HZ) ? jiffies + HZ : next;
28101da177e4SLinus Torvalds 	add_timer(&addr_chk_timer);
28111da177e4SLinus Torvalds 	spin_unlock_bh(&addrconf_verify_lock);
28121da177e4SLinus Torvalds }
28131da177e4SLinus Torvalds 
28141da177e4SLinus Torvalds static int
28151da177e4SLinus Torvalds inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
28161da177e4SLinus Torvalds {
28171da177e4SLinus Torvalds 	struct rtattr **rta = arg;
28181da177e4SLinus Torvalds 	struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
28191da177e4SLinus Torvalds 	struct in6_addr *pfx;
28201da177e4SLinus Torvalds 
28211da177e4SLinus Torvalds 	pfx = NULL;
28221da177e4SLinus Torvalds 	if (rta[IFA_ADDRESS-1]) {
28231da177e4SLinus Torvalds 		if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
28241da177e4SLinus Torvalds 			return -EINVAL;
28251da177e4SLinus Torvalds 		pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
28261da177e4SLinus Torvalds 	}
28271da177e4SLinus Torvalds 	if (rta[IFA_LOCAL-1]) {
28281da177e4SLinus Torvalds 		if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
28291da177e4SLinus Torvalds 			return -EINVAL;
28301da177e4SLinus Torvalds 		pfx = RTA_DATA(rta[IFA_LOCAL-1]);
28311da177e4SLinus Torvalds 	}
28321da177e4SLinus Torvalds 	if (pfx == NULL)
28331da177e4SLinus Torvalds 		return -EINVAL;
28341da177e4SLinus Torvalds 
28351da177e4SLinus Torvalds 	return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
28361da177e4SLinus Torvalds }
28371da177e4SLinus Torvalds 
28381da177e4SLinus Torvalds static int
28391da177e4SLinus Torvalds inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
28401da177e4SLinus Torvalds {
28411da177e4SLinus Torvalds 	struct rtattr  **rta = arg;
28421da177e4SLinus Torvalds 	struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
28431da177e4SLinus Torvalds 	struct in6_addr *pfx;
28441da177e4SLinus Torvalds 
28451da177e4SLinus Torvalds 	pfx = NULL;
28461da177e4SLinus Torvalds 	if (rta[IFA_ADDRESS-1]) {
28471da177e4SLinus Torvalds 		if (RTA_PAYLOAD(rta[IFA_ADDRESS-1]) < sizeof(*pfx))
28481da177e4SLinus Torvalds 			return -EINVAL;
28491da177e4SLinus Torvalds 		pfx = RTA_DATA(rta[IFA_ADDRESS-1]);
28501da177e4SLinus Torvalds 	}
28511da177e4SLinus Torvalds 	if (rta[IFA_LOCAL-1]) {
28521da177e4SLinus Torvalds 		if (pfx && memcmp(pfx, RTA_DATA(rta[IFA_LOCAL-1]), sizeof(*pfx)))
28531da177e4SLinus Torvalds 			return -EINVAL;
28541da177e4SLinus Torvalds 		pfx = RTA_DATA(rta[IFA_LOCAL-1]);
28551da177e4SLinus Torvalds 	}
28561da177e4SLinus Torvalds 	if (pfx == NULL)
28571da177e4SLinus Torvalds 		return -EINVAL;
28581da177e4SLinus Torvalds 
28591da177e4SLinus Torvalds 	return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
28601da177e4SLinus Torvalds }
28611da177e4SLinus Torvalds 
28621da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
2863b6544c0bSJamal Hadi Salim 			     u32 pid, u32 seq, int event, unsigned int flags)
28641da177e4SLinus Torvalds {
28651da177e4SLinus Torvalds 	struct ifaddrmsg *ifm;
28661da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
28671da177e4SLinus Torvalds 	struct ifa_cacheinfo ci;
28681da177e4SLinus Torvalds 	unsigned char	 *b = skb->tail;
28691da177e4SLinus Torvalds 
2870b6544c0bSJamal Hadi Salim 	nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
28711da177e4SLinus Torvalds 	ifm = NLMSG_DATA(nlh);
28721da177e4SLinus Torvalds 	ifm->ifa_family = AF_INET6;
28731da177e4SLinus Torvalds 	ifm->ifa_prefixlen = ifa->prefix_len;
28741da177e4SLinus Torvalds 	ifm->ifa_flags = ifa->flags;
28751da177e4SLinus Torvalds 	ifm->ifa_scope = RT_SCOPE_UNIVERSE;
28761da177e4SLinus Torvalds 	if (ifa->scope&IFA_HOST)
28771da177e4SLinus Torvalds 		ifm->ifa_scope = RT_SCOPE_HOST;
28781da177e4SLinus Torvalds 	else if (ifa->scope&IFA_LINK)
28791da177e4SLinus Torvalds 		ifm->ifa_scope = RT_SCOPE_LINK;
28801da177e4SLinus Torvalds 	else if (ifa->scope&IFA_SITE)
28811da177e4SLinus Torvalds 		ifm->ifa_scope = RT_SCOPE_SITE;
28821da177e4SLinus Torvalds 	ifm->ifa_index = ifa->idev->dev->ifindex;
28831da177e4SLinus Torvalds 	RTA_PUT(skb, IFA_ADDRESS, 16, &ifa->addr);
28841da177e4SLinus Torvalds 	if (!(ifa->flags&IFA_F_PERMANENT)) {
28851da177e4SLinus Torvalds 		ci.ifa_prefered = ifa->prefered_lft;
28861da177e4SLinus Torvalds 		ci.ifa_valid = ifa->valid_lft;
28871da177e4SLinus Torvalds 		if (ci.ifa_prefered != INFINITY_LIFE_TIME) {
28881da177e4SLinus Torvalds 			long tval = (jiffies - ifa->tstamp)/HZ;
28891da177e4SLinus Torvalds 			ci.ifa_prefered -= tval;
28901da177e4SLinus Torvalds 			if (ci.ifa_valid != INFINITY_LIFE_TIME)
28911da177e4SLinus Torvalds 				ci.ifa_valid -= tval;
28921da177e4SLinus Torvalds 		}
28931da177e4SLinus Torvalds 	} else {
28941da177e4SLinus Torvalds 		ci.ifa_prefered = INFINITY_LIFE_TIME;
28951da177e4SLinus Torvalds 		ci.ifa_valid = INFINITY_LIFE_TIME;
28961da177e4SLinus Torvalds 	}
28971da177e4SLinus Torvalds 	ci.cstamp = (__u32)(TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) / HZ * 100
28981da177e4SLinus Torvalds 		    + TIME_DELTA(ifa->cstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
28991da177e4SLinus Torvalds 	ci.tstamp = (__u32)(TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) / HZ * 100
29001da177e4SLinus Torvalds 		    + TIME_DELTA(ifa->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
29011da177e4SLinus Torvalds 	RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
29021da177e4SLinus Torvalds 	nlh->nlmsg_len = skb->tail - b;
29031da177e4SLinus Torvalds 	return skb->len;
29041da177e4SLinus Torvalds 
29051da177e4SLinus Torvalds nlmsg_failure:
29061da177e4SLinus Torvalds rtattr_failure:
29071da177e4SLinus Torvalds 	skb_trim(skb, b - skb->data);
29081da177e4SLinus Torvalds 	return -1;
29091da177e4SLinus Torvalds }
29101da177e4SLinus Torvalds 
29111da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
2912e431b8c0SJamal Hadi Salim 				u32 pid, u32 seq, int event, u16 flags)
29131da177e4SLinus Torvalds {
29141da177e4SLinus Torvalds 	struct ifaddrmsg *ifm;
29151da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
29161da177e4SLinus Torvalds 	struct ifa_cacheinfo ci;
29171da177e4SLinus Torvalds 	unsigned char	 *b = skb->tail;
29181da177e4SLinus Torvalds 
2919b6544c0bSJamal Hadi Salim 	nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
29201da177e4SLinus Torvalds 	ifm = NLMSG_DATA(nlh);
29211da177e4SLinus Torvalds 	ifm->ifa_family = AF_INET6;
29221da177e4SLinus Torvalds 	ifm->ifa_prefixlen = 128;
29231da177e4SLinus Torvalds 	ifm->ifa_flags = IFA_F_PERMANENT;
29241da177e4SLinus Torvalds 	ifm->ifa_scope = RT_SCOPE_UNIVERSE;
29251da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifmca->mca_addr)&IFA_SITE)
29261da177e4SLinus Torvalds 		ifm->ifa_scope = RT_SCOPE_SITE;
29271da177e4SLinus Torvalds 	ifm->ifa_index = ifmca->idev->dev->ifindex;
29281da177e4SLinus Torvalds 	RTA_PUT(skb, IFA_MULTICAST, 16, &ifmca->mca_addr);
29291da177e4SLinus Torvalds 	ci.cstamp = (__u32)(TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) / HZ
29301da177e4SLinus Torvalds 		    * 100 + TIME_DELTA(ifmca->mca_cstamp, INITIAL_JIFFIES) % HZ
29311da177e4SLinus Torvalds 		    * 100 / HZ);
29321da177e4SLinus Torvalds 	ci.tstamp = (__u32)(TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) / HZ
29331da177e4SLinus Torvalds 		    * 100 + TIME_DELTA(ifmca->mca_tstamp, INITIAL_JIFFIES) % HZ
29341da177e4SLinus Torvalds 		    * 100 / HZ);
29351da177e4SLinus Torvalds 	ci.ifa_prefered = INFINITY_LIFE_TIME;
29361da177e4SLinus Torvalds 	ci.ifa_valid = INFINITY_LIFE_TIME;
29371da177e4SLinus Torvalds 	RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
29381da177e4SLinus Torvalds 	nlh->nlmsg_len = skb->tail - b;
29391da177e4SLinus Torvalds 	return skb->len;
29401da177e4SLinus Torvalds 
29411da177e4SLinus Torvalds nlmsg_failure:
29421da177e4SLinus Torvalds rtattr_failure:
29431da177e4SLinus Torvalds 	skb_trim(skb, b - skb->data);
29441da177e4SLinus Torvalds 	return -1;
29451da177e4SLinus Torvalds }
29461da177e4SLinus Torvalds 
29471da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
2948b6544c0bSJamal Hadi Salim 				u32 pid, u32 seq, int event, unsigned int flags)
29491da177e4SLinus Torvalds {
29501da177e4SLinus Torvalds 	struct ifaddrmsg *ifm;
29511da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
29521da177e4SLinus Torvalds 	struct ifa_cacheinfo ci;
29531da177e4SLinus Torvalds 	unsigned char	 *b = skb->tail;
29541da177e4SLinus Torvalds 
2955b6544c0bSJamal Hadi Salim 	nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
29561da177e4SLinus Torvalds 	ifm = NLMSG_DATA(nlh);
29571da177e4SLinus Torvalds 	ifm->ifa_family = AF_INET6;
29581da177e4SLinus Torvalds 	ifm->ifa_prefixlen = 128;
29591da177e4SLinus Torvalds 	ifm->ifa_flags = IFA_F_PERMANENT;
29601da177e4SLinus Torvalds 	ifm->ifa_scope = RT_SCOPE_UNIVERSE;
29611da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifaca->aca_addr)&IFA_SITE)
29621da177e4SLinus Torvalds 		ifm->ifa_scope = RT_SCOPE_SITE;
29631da177e4SLinus Torvalds 	ifm->ifa_index = ifaca->aca_idev->dev->ifindex;
29641da177e4SLinus Torvalds 	RTA_PUT(skb, IFA_ANYCAST, 16, &ifaca->aca_addr);
29651da177e4SLinus Torvalds 	ci.cstamp = (__u32)(TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) / HZ
29661da177e4SLinus Torvalds 		    * 100 + TIME_DELTA(ifaca->aca_cstamp, INITIAL_JIFFIES) % HZ
29671da177e4SLinus Torvalds 		    * 100 / HZ);
29681da177e4SLinus Torvalds 	ci.tstamp = (__u32)(TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) / HZ
29691da177e4SLinus Torvalds 		    * 100 + TIME_DELTA(ifaca->aca_tstamp, INITIAL_JIFFIES) % HZ
29701da177e4SLinus Torvalds 		    * 100 / HZ);
29711da177e4SLinus Torvalds 	ci.ifa_prefered = INFINITY_LIFE_TIME;
29721da177e4SLinus Torvalds 	ci.ifa_valid = INFINITY_LIFE_TIME;
29731da177e4SLinus Torvalds 	RTA_PUT(skb, IFA_CACHEINFO, sizeof(ci), &ci);
29741da177e4SLinus Torvalds 	nlh->nlmsg_len = skb->tail - b;
29751da177e4SLinus Torvalds 	return skb->len;
29761da177e4SLinus Torvalds 
29771da177e4SLinus Torvalds nlmsg_failure:
29781da177e4SLinus Torvalds rtattr_failure:
29791da177e4SLinus Torvalds 	skb_trim(skb, b - skb->data);
29801da177e4SLinus Torvalds 	return -1;
29811da177e4SLinus Torvalds }
29821da177e4SLinus Torvalds 
29831da177e4SLinus Torvalds enum addr_type_t
29841da177e4SLinus Torvalds {
29851da177e4SLinus Torvalds 	UNICAST_ADDR,
29861da177e4SLinus Torvalds 	MULTICAST_ADDR,
29871da177e4SLinus Torvalds 	ANYCAST_ADDR,
29881da177e4SLinus Torvalds };
29891da177e4SLinus Torvalds 
29901da177e4SLinus Torvalds static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
29911da177e4SLinus Torvalds 			   enum addr_type_t type)
29921da177e4SLinus Torvalds {
29931da177e4SLinus Torvalds 	int idx, ip_idx;
29941da177e4SLinus Torvalds 	int s_idx, s_ip_idx;
29951da177e4SLinus Torvalds 	int err = 1;
29961da177e4SLinus Torvalds 	struct net_device *dev;
29971da177e4SLinus Torvalds 	struct inet6_dev *idev = NULL;
29981da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
29991da177e4SLinus Torvalds 	struct ifmcaddr6 *ifmca;
30001da177e4SLinus Torvalds 	struct ifacaddr6 *ifaca;
30011da177e4SLinus Torvalds 
30021da177e4SLinus Torvalds 	s_idx = cb->args[0];
30031da177e4SLinus Torvalds 	s_ip_idx = ip_idx = cb->args[1];
30041da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
30051da177e4SLinus Torvalds 
30061da177e4SLinus Torvalds 	for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
30071da177e4SLinus Torvalds 		if (idx < s_idx)
30081da177e4SLinus Torvalds 			continue;
30091da177e4SLinus Torvalds 		if (idx > s_idx)
30101da177e4SLinus Torvalds 			s_ip_idx = 0;
30111da177e4SLinus Torvalds 		ip_idx = 0;
30121da177e4SLinus Torvalds 		if ((idev = in6_dev_get(dev)) == NULL)
30131da177e4SLinus Torvalds 			continue;
30141da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
30151da177e4SLinus Torvalds 		switch (type) {
30161da177e4SLinus Torvalds 		case UNICAST_ADDR:
3017ae9cda5dSYOSHIFUJI Hideaki 			/* unicast address incl. temp addr */
30181da177e4SLinus Torvalds 			for (ifa = idev->addr_list; ifa;
30191da177e4SLinus Torvalds 			     ifa = ifa->if_next, ip_idx++) {
30201da177e4SLinus Torvalds 				if (ip_idx < s_ip_idx)
30211da177e4SLinus Torvalds 					continue;
30221da177e4SLinus Torvalds 				if ((err = inet6_fill_ifaddr(skb, ifa,
30231da177e4SLinus Torvalds 				    NETLINK_CB(cb->skb).pid,
3024b6544c0bSJamal Hadi Salim 				    cb->nlh->nlmsg_seq, RTM_NEWADDR,
3025b6544c0bSJamal Hadi Salim 				    NLM_F_MULTI)) <= 0)
30261da177e4SLinus Torvalds 					goto done;
30271da177e4SLinus Torvalds 			}
30281da177e4SLinus Torvalds 			break;
30291da177e4SLinus Torvalds 		case MULTICAST_ADDR:
30301da177e4SLinus Torvalds 			/* multicast address */
30311da177e4SLinus Torvalds 			for (ifmca = idev->mc_list; ifmca;
30321da177e4SLinus Torvalds 			     ifmca = ifmca->next, ip_idx++) {
30331da177e4SLinus Torvalds 				if (ip_idx < s_ip_idx)
30341da177e4SLinus Torvalds 					continue;
30351da177e4SLinus Torvalds 				if ((err = inet6_fill_ifmcaddr(skb, ifmca,
30361da177e4SLinus Torvalds 				    NETLINK_CB(cb->skb).pid,
3037b6544c0bSJamal Hadi Salim 				    cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
3038b6544c0bSJamal Hadi Salim 				    NLM_F_MULTI)) <= 0)
30391da177e4SLinus Torvalds 					goto done;
30401da177e4SLinus Torvalds 			}
30411da177e4SLinus Torvalds 			break;
30421da177e4SLinus Torvalds 		case ANYCAST_ADDR:
30431da177e4SLinus Torvalds 			/* anycast address */
30441da177e4SLinus Torvalds 			for (ifaca = idev->ac_list; ifaca;
30451da177e4SLinus Torvalds 			     ifaca = ifaca->aca_next, ip_idx++) {
30461da177e4SLinus Torvalds 				if (ip_idx < s_ip_idx)
30471da177e4SLinus Torvalds 					continue;
30481da177e4SLinus Torvalds 				if ((err = inet6_fill_ifacaddr(skb, ifaca,
30491da177e4SLinus Torvalds 				    NETLINK_CB(cb->skb).pid,
3050b6544c0bSJamal Hadi Salim 				    cb->nlh->nlmsg_seq, RTM_GETANYCAST,
3051b6544c0bSJamal Hadi Salim 				    NLM_F_MULTI)) <= 0)
30521da177e4SLinus Torvalds 					goto done;
30531da177e4SLinus Torvalds 			}
30541da177e4SLinus Torvalds 			break;
30551da177e4SLinus Torvalds 		default:
30561da177e4SLinus Torvalds 			break;
30571da177e4SLinus Torvalds 		}
30581da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
30591da177e4SLinus Torvalds 		in6_dev_put(idev);
30601da177e4SLinus Torvalds 	}
30611da177e4SLinus Torvalds done:
30621da177e4SLinus Torvalds 	if (err <= 0) {
30631da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
30641da177e4SLinus Torvalds 		in6_dev_put(idev);
30651da177e4SLinus Torvalds 	}
30661da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
30671da177e4SLinus Torvalds 	cb->args[0] = idx;
30681da177e4SLinus Torvalds 	cb->args[1] = ip_idx;
30691da177e4SLinus Torvalds 	return skb->len;
30701da177e4SLinus Torvalds }
30711da177e4SLinus Torvalds 
30721da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
30731da177e4SLinus Torvalds {
30741da177e4SLinus Torvalds 	enum addr_type_t type = UNICAST_ADDR;
30751da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
30761da177e4SLinus Torvalds }
30771da177e4SLinus Torvalds 
30781da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
30791da177e4SLinus Torvalds {
30801da177e4SLinus Torvalds 	enum addr_type_t type = MULTICAST_ADDR;
30811da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
30821da177e4SLinus Torvalds }
30831da177e4SLinus Torvalds 
30841da177e4SLinus Torvalds 
30851da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
30861da177e4SLinus Torvalds {
30871da177e4SLinus Torvalds 	enum addr_type_t type = ANYCAST_ADDR;
30881da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
30891da177e4SLinus Torvalds }
30901da177e4SLinus Torvalds 
30911da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
30921da177e4SLinus Torvalds {
30931da177e4SLinus Torvalds 	struct sk_buff *skb;
30941da177e4SLinus Torvalds 	int size = NLMSG_SPACE(sizeof(struct ifaddrmsg)+128);
30951da177e4SLinus Torvalds 
30961da177e4SLinus Torvalds 	skb = alloc_skb(size, GFP_ATOMIC);
30971da177e4SLinus Torvalds 	if (!skb) {
3098ac6d439dSPatrick McHardy 		netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFADDR, ENOBUFS);
30991da177e4SLinus Torvalds 		return;
31001da177e4SLinus Torvalds 	}
31019ed19f33SJamal Hadi Salim 	if (inet6_fill_ifaddr(skb, ifa, current->pid, 0, event, 0) < 0) {
31021da177e4SLinus Torvalds 		kfree_skb(skb);
3103ac6d439dSPatrick McHardy 		netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFADDR, EINVAL);
31041da177e4SLinus Torvalds 		return;
31051da177e4SLinus Torvalds 	}
3106ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_IFADDR;
3107ac6d439dSPatrick McHardy 	netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_IFADDR, GFP_ATOMIC);
31081da177e4SLinus Torvalds }
31091da177e4SLinus Torvalds 
31101da177e4SLinus Torvalds static void inline ipv6_store_devconf(struct ipv6_devconf *cnf,
31111da177e4SLinus Torvalds 				__s32 *array, int bytes)
31121da177e4SLinus Torvalds {
31131da177e4SLinus Torvalds 	memset(array, 0, bytes);
31141da177e4SLinus Torvalds 	array[DEVCONF_FORWARDING] = cnf->forwarding;
31151da177e4SLinus Torvalds 	array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
31161da177e4SLinus Torvalds 	array[DEVCONF_MTU6] = cnf->mtu6;
31171da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
31181da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
31191da177e4SLinus Torvalds 	array[DEVCONF_AUTOCONF] = cnf->autoconf;
31201da177e4SLinus Torvalds 	array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
31211da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
31221da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICIT_INTERVAL] = cnf->rtr_solicit_interval;
31231da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICIT_DELAY] = cnf->rtr_solicit_delay;
31241da177e4SLinus Torvalds 	array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
31251da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
31261da177e4SLinus Torvalds 	array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
31271da177e4SLinus Torvalds 	array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
31281da177e4SLinus Torvalds 	array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
31291da177e4SLinus Torvalds 	array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
31301da177e4SLinus Torvalds 	array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
31311da177e4SLinus Torvalds #endif
31321da177e4SLinus Torvalds 	array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
31331da177e4SLinus Torvalds }
31341da177e4SLinus Torvalds 
31351da177e4SLinus Torvalds static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
3136b6544c0bSJamal Hadi Salim 			     u32 pid, u32 seq, int event, unsigned int flags)
31371da177e4SLinus Torvalds {
31381da177e4SLinus Torvalds 	struct net_device	*dev = idev->dev;
31391da177e4SLinus Torvalds 	__s32			*array = NULL;
31401da177e4SLinus Torvalds 	struct ifinfomsg	*r;
31411da177e4SLinus Torvalds 	struct nlmsghdr 	*nlh;
31421da177e4SLinus Torvalds 	unsigned char		*b = skb->tail;
31431da177e4SLinus Torvalds 	struct rtattr		*subattr;
31441da177e4SLinus Torvalds 	__u32			mtu = dev->mtu;
31451da177e4SLinus Torvalds 	struct ifla_cacheinfo	ci;
31461da177e4SLinus Torvalds 
3147b6544c0bSJamal Hadi Salim 	nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*r), flags);
31481da177e4SLinus Torvalds 	r = NLMSG_DATA(nlh);
31491da177e4SLinus Torvalds 	r->ifi_family = AF_INET6;
31509ef1d4c7SPatrick McHardy 	r->__ifi_pad = 0;
31511da177e4SLinus Torvalds 	r->ifi_type = dev->type;
31521da177e4SLinus Torvalds 	r->ifi_index = dev->ifindex;
31531da177e4SLinus Torvalds 	r->ifi_flags = dev_get_flags(dev);
31541da177e4SLinus Torvalds 	r->ifi_change = 0;
31551da177e4SLinus Torvalds 
31561da177e4SLinus Torvalds 	RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
31571da177e4SLinus Torvalds 
31581da177e4SLinus Torvalds 	if (dev->addr_len)
31591da177e4SLinus Torvalds 		RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
31601da177e4SLinus Torvalds 
31611da177e4SLinus Torvalds 	RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
31621da177e4SLinus Torvalds 	if (dev->ifindex != dev->iflink)
31631da177e4SLinus Torvalds 		RTA_PUT(skb, IFLA_LINK, sizeof(int), &dev->iflink);
31641da177e4SLinus Torvalds 
31651da177e4SLinus Torvalds 	subattr = (struct rtattr*)skb->tail;
31661da177e4SLinus Torvalds 
31671da177e4SLinus Torvalds 	RTA_PUT(skb, IFLA_PROTINFO, 0, NULL);
31681da177e4SLinus Torvalds 
31691da177e4SLinus Torvalds 	/* return the device flags */
31701da177e4SLinus Torvalds 	RTA_PUT(skb, IFLA_INET6_FLAGS, sizeof(__u32), &idev->if_flags);
31711da177e4SLinus Torvalds 
31721da177e4SLinus Torvalds 	/* return interface cacheinfo */
31731da177e4SLinus Torvalds 	ci.max_reasm_len = IPV6_MAXPLEN;
31741da177e4SLinus Torvalds 	ci.tstamp = (__u32)(TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) / HZ * 100
31751da177e4SLinus Torvalds 		    + TIME_DELTA(idev->tstamp, INITIAL_JIFFIES) % HZ * 100 / HZ);
31761da177e4SLinus Torvalds 	ci.reachable_time = idev->nd_parms->reachable_time;
31771da177e4SLinus Torvalds 	ci.retrans_time = idev->nd_parms->retrans_time;
31781da177e4SLinus Torvalds 	RTA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
31791da177e4SLinus Torvalds 
31801da177e4SLinus Torvalds 	/* return the device sysctl params */
31811da177e4SLinus Torvalds 	if ((array = kmalloc(DEVCONF_MAX * sizeof(*array), GFP_ATOMIC)) == NULL)
31821da177e4SLinus Torvalds 		goto rtattr_failure;
31831da177e4SLinus Torvalds 	ipv6_store_devconf(&idev->cnf, array, DEVCONF_MAX * sizeof(*array));
31841da177e4SLinus Torvalds 	RTA_PUT(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(*array), array);
31851da177e4SLinus Torvalds 
31861da177e4SLinus Torvalds 	/* XXX - Statistics/MC not implemented */
31871da177e4SLinus Torvalds 	subattr->rta_len = skb->tail - (u8*)subattr;
31881da177e4SLinus Torvalds 
31891da177e4SLinus Torvalds 	nlh->nlmsg_len = skb->tail - b;
31901da177e4SLinus Torvalds 	kfree(array);
31911da177e4SLinus Torvalds 	return skb->len;
31921da177e4SLinus Torvalds 
31931da177e4SLinus Torvalds nlmsg_failure:
31941da177e4SLinus Torvalds rtattr_failure:
31951da177e4SLinus Torvalds 	kfree(array);
31961da177e4SLinus Torvalds 	skb_trim(skb, b - skb->data);
31971da177e4SLinus Torvalds 	return -1;
31981da177e4SLinus Torvalds }
31991da177e4SLinus Torvalds 
32001da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
32011da177e4SLinus Torvalds {
32021da177e4SLinus Torvalds 	int idx, err;
32031da177e4SLinus Torvalds 	int s_idx = cb->args[0];
32041da177e4SLinus Torvalds 	struct net_device *dev;
32051da177e4SLinus Torvalds 	struct inet6_dev *idev;
32061da177e4SLinus Torvalds 
32071da177e4SLinus Torvalds 	read_lock(&dev_base_lock);
32081da177e4SLinus Torvalds 	for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
32091da177e4SLinus Torvalds 		if (idx < s_idx)
32101da177e4SLinus Torvalds 			continue;
32111da177e4SLinus Torvalds 		if ((idev = in6_dev_get(dev)) == NULL)
32121da177e4SLinus Torvalds 			continue;
32131da177e4SLinus Torvalds 		err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
3214b6544c0bSJamal Hadi Salim 				cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
32151da177e4SLinus Torvalds 		in6_dev_put(idev);
32161da177e4SLinus Torvalds 		if (err <= 0)
32171da177e4SLinus Torvalds 			break;
32181da177e4SLinus Torvalds 	}
32191da177e4SLinus Torvalds 	read_unlock(&dev_base_lock);
32201da177e4SLinus Torvalds 	cb->args[0] = idx;
32211da177e4SLinus Torvalds 
32221da177e4SLinus Torvalds 	return skb->len;
32231da177e4SLinus Torvalds }
32241da177e4SLinus Torvalds 
32251da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
32261da177e4SLinus Torvalds {
32271da177e4SLinus Torvalds 	struct sk_buff *skb;
32281da177e4SLinus Torvalds 	/* 128 bytes ?? */
32291da177e4SLinus Torvalds 	int size = NLMSG_SPACE(sizeof(struct ifinfomsg)+128);
32301da177e4SLinus Torvalds 
32311da177e4SLinus Torvalds 	skb = alloc_skb(size, GFP_ATOMIC);
32321da177e4SLinus Torvalds 	if (!skb) {
3233ac6d439dSPatrick McHardy 		netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFINFO, ENOBUFS);
32341da177e4SLinus Torvalds 		return;
32351da177e4SLinus Torvalds 	}
32369ed19f33SJamal Hadi Salim 	if (inet6_fill_ifinfo(skb, idev, current->pid, 0, event, 0) < 0) {
32371da177e4SLinus Torvalds 		kfree_skb(skb);
3238ac6d439dSPatrick McHardy 		netlink_set_err(rtnl, 0, RTNLGRP_IPV6_IFINFO, EINVAL);
32391da177e4SLinus Torvalds 		return;
32401da177e4SLinus Torvalds 	}
3241ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_IFINFO;
3242ac6d439dSPatrick McHardy 	netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_IFINFO, GFP_ATOMIC);
32431da177e4SLinus Torvalds }
32441da177e4SLinus Torvalds 
32451da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
3246b6544c0bSJamal Hadi Salim 			struct prefix_info *pinfo, u32 pid, u32 seq,
3247b6544c0bSJamal Hadi Salim 			int event, unsigned int flags)
32481da177e4SLinus Torvalds {
32491da177e4SLinus Torvalds 	struct prefixmsg	*pmsg;
32501da177e4SLinus Torvalds 	struct nlmsghdr 	*nlh;
32511da177e4SLinus Torvalds 	unsigned char		*b = skb->tail;
32521da177e4SLinus Torvalds 	struct prefix_cacheinfo	ci;
32531da177e4SLinus Torvalds 
3254b6544c0bSJamal Hadi Salim 	nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*pmsg), flags);
32551da177e4SLinus Torvalds 	pmsg = NLMSG_DATA(nlh);
32561da177e4SLinus Torvalds 	pmsg->prefix_family = AF_INET6;
32578a47077aSPatrick McHardy 	pmsg->prefix_pad1 = 0;
32588a47077aSPatrick McHardy 	pmsg->prefix_pad2 = 0;
32591da177e4SLinus Torvalds 	pmsg->prefix_ifindex = idev->dev->ifindex;
32601da177e4SLinus Torvalds 	pmsg->prefix_len = pinfo->prefix_len;
32611da177e4SLinus Torvalds 	pmsg->prefix_type = pinfo->type;
32628a47077aSPatrick McHardy 	pmsg->prefix_pad3 = 0;
32631da177e4SLinus Torvalds 
32641da177e4SLinus Torvalds 	pmsg->prefix_flags = 0;
32651da177e4SLinus Torvalds 	if (pinfo->onlink)
32661da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_ONLINK;
32671da177e4SLinus Torvalds 	if (pinfo->autoconf)
32681da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
32691da177e4SLinus Torvalds 
32701da177e4SLinus Torvalds 	RTA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
32711da177e4SLinus Torvalds 
32721da177e4SLinus Torvalds 	ci.preferred_time = ntohl(pinfo->prefered);
32731da177e4SLinus Torvalds 	ci.valid_time = ntohl(pinfo->valid);
32741da177e4SLinus Torvalds 	RTA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
32751da177e4SLinus Torvalds 
32761da177e4SLinus Torvalds 	nlh->nlmsg_len = skb->tail - b;
32771da177e4SLinus Torvalds 	return skb->len;
32781da177e4SLinus Torvalds 
32791da177e4SLinus Torvalds nlmsg_failure:
32801da177e4SLinus Torvalds rtattr_failure:
32811da177e4SLinus Torvalds 	skb_trim(skb, b - skb->data);
32821da177e4SLinus Torvalds 	return -1;
32831da177e4SLinus Torvalds }
32841da177e4SLinus Torvalds 
32851da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
32861da177e4SLinus Torvalds 			 struct prefix_info *pinfo)
32871da177e4SLinus Torvalds {
32881da177e4SLinus Torvalds 	struct sk_buff *skb;
32891da177e4SLinus Torvalds 	int size = NLMSG_SPACE(sizeof(struct prefixmsg)+128);
32901da177e4SLinus Torvalds 
32911da177e4SLinus Torvalds 	skb = alloc_skb(size, GFP_ATOMIC);
32921da177e4SLinus Torvalds 	if (!skb) {
3293ac6d439dSPatrick McHardy 		netlink_set_err(rtnl, 0, RTNLGRP_IPV6_PREFIX, ENOBUFS);
32941da177e4SLinus Torvalds 		return;
32951da177e4SLinus Torvalds 	}
32969ed19f33SJamal Hadi Salim 	if (inet6_fill_prefix(skb, idev, pinfo, current->pid, 0, event, 0) < 0) {
32971da177e4SLinus Torvalds 		kfree_skb(skb);
3298ac6d439dSPatrick McHardy 		netlink_set_err(rtnl, 0, RTNLGRP_IPV6_PREFIX, EINVAL);
32991da177e4SLinus Torvalds 		return;
33001da177e4SLinus Torvalds 	}
3301ac6d439dSPatrick McHardy 	NETLINK_CB(skb).dst_group = RTNLGRP_IPV6_PREFIX;
3302ac6d439dSPatrick McHardy 	netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV6_PREFIX, GFP_ATOMIC);
33031da177e4SLinus Torvalds }
33041da177e4SLinus Torvalds 
3305db46edc6SThomas Graf static struct rtnetlink_link inet6_rtnetlink_table[RTM_NR_MSGTYPES] = {
33061da177e4SLinus Torvalds 	[RTM_GETLINK - RTM_BASE] = { .dumpit	= inet6_dump_ifinfo, },
33071da177e4SLinus Torvalds 	[RTM_NEWADDR - RTM_BASE] = { .doit	= inet6_rtm_newaddr, },
33081da177e4SLinus Torvalds 	[RTM_DELADDR - RTM_BASE] = { .doit	= inet6_rtm_deladdr, },
33091da177e4SLinus Torvalds 	[RTM_GETADDR - RTM_BASE] = { .dumpit	= inet6_dump_ifaddr, },
33101da177e4SLinus Torvalds 	[RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
33111da177e4SLinus Torvalds 	[RTM_GETANYCAST - RTM_BASE] = { .dumpit	= inet6_dump_ifacaddr, },
33121da177e4SLinus Torvalds 	[RTM_NEWROUTE - RTM_BASE] = { .doit	= inet6_rtm_newroute, },
33131da177e4SLinus Torvalds 	[RTM_DELROUTE - RTM_BASE] = { .doit	= inet6_rtm_delroute, },
33141da177e4SLinus Torvalds 	[RTM_GETROUTE - RTM_BASE] = { .doit	= inet6_rtm_getroute,
33151da177e4SLinus Torvalds 				      .dumpit	= inet6_dump_fib, },
33161da177e4SLinus Torvalds };
33171da177e4SLinus Torvalds 
33181da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
33191da177e4SLinus Torvalds {
33201da177e4SLinus Torvalds 	inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
33211da177e4SLinus Torvalds 
33221da177e4SLinus Torvalds 	switch (event) {
33231da177e4SLinus Torvalds 	case RTM_NEWADDR:
33241da177e4SLinus Torvalds 		dst_hold(&ifp->rt->u.dst);
33250d51aa80SJamal Hadi Salim 		if (ip6_ins_rt(ifp->rt, NULL, NULL, NULL))
33261da177e4SLinus Torvalds 			dst_release(&ifp->rt->u.dst);
33271da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
33281da177e4SLinus Torvalds 			addrconf_join_anycast(ifp);
33291da177e4SLinus Torvalds 		break;
33301da177e4SLinus Torvalds 	case RTM_DELADDR:
33311da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
33321da177e4SLinus Torvalds 			addrconf_leave_anycast(ifp);
33331da177e4SLinus Torvalds 		addrconf_leave_solict(ifp->idev, &ifp->addr);
33341da177e4SLinus Torvalds 		dst_hold(&ifp->rt->u.dst);
33350d51aa80SJamal Hadi Salim 		if (ip6_del_rt(ifp->rt, NULL, NULL, NULL))
33361da177e4SLinus Torvalds 			dst_free(&ifp->rt->u.dst);
33371da177e4SLinus Torvalds 		else
33381da177e4SLinus Torvalds 			dst_release(&ifp->rt->u.dst);
33391da177e4SLinus Torvalds 		break;
33401da177e4SLinus Torvalds 	}
33411da177e4SLinus Torvalds }
33421da177e4SLinus Torvalds 
33431da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
33441da177e4SLinus Torvalds {
33451da177e4SLinus Torvalds 	read_lock_bh(&addrconf_lock);
33461da177e4SLinus Torvalds 	if (likely(ifp->idev->dead == 0))
33471da177e4SLinus Torvalds 		__ipv6_ifa_notify(event, ifp);
33481da177e4SLinus Torvalds 	read_unlock_bh(&addrconf_lock);
33491da177e4SLinus Torvalds }
33501da177e4SLinus Torvalds 
33511da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
33521da177e4SLinus Torvalds 
33531da177e4SLinus Torvalds static
33541da177e4SLinus Torvalds int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
33551da177e4SLinus Torvalds 			   void __user *buffer, size_t *lenp, loff_t *ppos)
33561da177e4SLinus Torvalds {
33571da177e4SLinus Torvalds 	int *valp = ctl->data;
33581da177e4SLinus Torvalds 	int val = *valp;
33591da177e4SLinus Torvalds 	int ret;
33601da177e4SLinus Torvalds 
33611da177e4SLinus Torvalds 	ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
33621da177e4SLinus Torvalds 
33631da177e4SLinus Torvalds 	if (write && valp != &ipv6_devconf_dflt.forwarding) {
33641da177e4SLinus Torvalds 		if (valp != &ipv6_devconf.forwarding) {
33651da177e4SLinus Torvalds 			if ((!*valp) ^ (!val)) {
33661da177e4SLinus Torvalds 				struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
33671da177e4SLinus Torvalds 				if (idev == NULL)
33681da177e4SLinus Torvalds 					return ret;
33691da177e4SLinus Torvalds 				dev_forward_change(idev);
33701da177e4SLinus Torvalds 			}
33711da177e4SLinus Torvalds 		} else {
33721da177e4SLinus Torvalds 			ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
33731da177e4SLinus Torvalds 			addrconf_forward_change();
33741da177e4SLinus Torvalds 		}
33751da177e4SLinus Torvalds 		if (*valp)
33761da177e4SLinus Torvalds 			rt6_purge_dflt_routers();
33771da177e4SLinus Torvalds 	}
33781da177e4SLinus Torvalds 
33791da177e4SLinus Torvalds         return ret;
33801da177e4SLinus Torvalds }
33811da177e4SLinus Torvalds 
33821da177e4SLinus Torvalds static int addrconf_sysctl_forward_strategy(ctl_table *table,
33831da177e4SLinus Torvalds 					    int __user *name, int nlen,
33841da177e4SLinus Torvalds 					    void __user *oldval,
33851da177e4SLinus Torvalds 					    size_t __user *oldlenp,
33861da177e4SLinus Torvalds 					    void __user *newval, size_t newlen,
33871da177e4SLinus Torvalds 					    void **context)
33881da177e4SLinus Torvalds {
33891da177e4SLinus Torvalds 	int *valp = table->data;
33901da177e4SLinus Torvalds 	int new;
33911da177e4SLinus Torvalds 
33921da177e4SLinus Torvalds 	if (!newval || !newlen)
33931da177e4SLinus Torvalds 		return 0;
33941da177e4SLinus Torvalds 	if (newlen != sizeof(int))
33951da177e4SLinus Torvalds 		return -EINVAL;
33961da177e4SLinus Torvalds 	if (get_user(new, (int __user *)newval))
33971da177e4SLinus Torvalds 		return -EFAULT;
33981da177e4SLinus Torvalds 	if (new == *valp)
33991da177e4SLinus Torvalds 		return 0;
34001da177e4SLinus Torvalds 	if (oldval && oldlenp) {
34011da177e4SLinus Torvalds 		size_t len;
34021da177e4SLinus Torvalds 		if (get_user(len, oldlenp))
34031da177e4SLinus Torvalds 			return -EFAULT;
34041da177e4SLinus Torvalds 		if (len) {
34051da177e4SLinus Torvalds 			if (len > table->maxlen)
34061da177e4SLinus Torvalds 				len = table->maxlen;
34071da177e4SLinus Torvalds 			if (copy_to_user(oldval, valp, len))
34081da177e4SLinus Torvalds 				return -EFAULT;
34091da177e4SLinus Torvalds 			if (put_user(len, oldlenp))
34101da177e4SLinus Torvalds 				return -EFAULT;
34111da177e4SLinus Torvalds 		}
34121da177e4SLinus Torvalds 	}
34131da177e4SLinus Torvalds 
34141da177e4SLinus Torvalds 	if (valp != &ipv6_devconf_dflt.forwarding) {
34151da177e4SLinus Torvalds 		if (valp != &ipv6_devconf.forwarding) {
34161da177e4SLinus Torvalds 			struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
34171da177e4SLinus Torvalds 			int changed;
34181da177e4SLinus Torvalds 			if (unlikely(idev == NULL))
34191da177e4SLinus Torvalds 				return -ENODEV;
34201da177e4SLinus Torvalds 			changed = (!*valp) ^ (!new);
34211da177e4SLinus Torvalds 			*valp = new;
34221da177e4SLinus Torvalds 			if (changed)
34231da177e4SLinus Torvalds 				dev_forward_change(idev);
34241da177e4SLinus Torvalds 		} else {
34251da177e4SLinus Torvalds 			*valp = new;
34261da177e4SLinus Torvalds 			addrconf_forward_change();
34271da177e4SLinus Torvalds 		}
34281da177e4SLinus Torvalds 
34291da177e4SLinus Torvalds 		if (*valp)
34301da177e4SLinus Torvalds 			rt6_purge_dflt_routers();
34311da177e4SLinus Torvalds 	} else
34321da177e4SLinus Torvalds 		*valp = new;
34331da177e4SLinus Torvalds 
34341da177e4SLinus Torvalds 	return 1;
34351da177e4SLinus Torvalds }
34361da177e4SLinus Torvalds 
34371da177e4SLinus Torvalds static struct addrconf_sysctl_table
34381da177e4SLinus Torvalds {
34391da177e4SLinus Torvalds 	struct ctl_table_header *sysctl_header;
34401da177e4SLinus Torvalds 	ctl_table addrconf_vars[__NET_IPV6_MAX];
34411da177e4SLinus Torvalds 	ctl_table addrconf_dev[2];
34421da177e4SLinus Torvalds 	ctl_table addrconf_conf_dir[2];
34431da177e4SLinus Torvalds 	ctl_table addrconf_proto_dir[2];
34441da177e4SLinus Torvalds 	ctl_table addrconf_root_dir[2];
34451da177e4SLinus Torvalds } addrconf_sysctl = {
34461da177e4SLinus Torvalds 	.sysctl_header = NULL,
34471da177e4SLinus Torvalds 	.addrconf_vars = {
34481da177e4SLinus Torvalds         	{
34491da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_FORWARDING,
34501da177e4SLinus Torvalds 			.procname	=	"forwarding",
34511da177e4SLinus Torvalds          		.data		=	&ipv6_devconf.forwarding,
34521da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
34531da177e4SLinus Torvalds 			.mode		=	0644,
34541da177e4SLinus Torvalds          		.proc_handler	=	&addrconf_sysctl_forward,
34551da177e4SLinus Torvalds 			.strategy	=	&addrconf_sysctl_forward_strategy,
34561da177e4SLinus Torvalds 		},
34571da177e4SLinus Torvalds 		{
34581da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_HOP_LIMIT,
34591da177e4SLinus Torvalds 			.procname	=	"hop_limit",
34601da177e4SLinus Torvalds          		.data		=	&ipv6_devconf.hop_limit,
34611da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
34621da177e4SLinus Torvalds 			.mode		=	0644,
34631da177e4SLinus Torvalds 			.proc_handler	=	proc_dointvec,
34641da177e4SLinus Torvalds 		},
34651da177e4SLinus Torvalds 		{
34661da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_MTU,
34671da177e4SLinus Torvalds 			.procname	=	"mtu",
34681da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.mtu6,
34691da177e4SLinus Torvalds          		.maxlen		=	sizeof(int),
34701da177e4SLinus Torvalds 			.mode		=	0644,
34711da177e4SLinus Torvalds          		.proc_handler	=	&proc_dointvec,
34721da177e4SLinus Torvalds 		},
34731da177e4SLinus Torvalds 		{
34741da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_ACCEPT_RA,
34751da177e4SLinus Torvalds 			.procname	=	"accept_ra",
34761da177e4SLinus Torvalds          		.data		=	&ipv6_devconf.accept_ra,
34771da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
34781da177e4SLinus Torvalds 			.mode		=	0644,
34791da177e4SLinus Torvalds          		.proc_handler	=	&proc_dointvec,
34801da177e4SLinus Torvalds 		},
34811da177e4SLinus Torvalds 		{
34821da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_ACCEPT_REDIRECTS,
34831da177e4SLinus Torvalds 			.procname	=	"accept_redirects",
34841da177e4SLinus Torvalds          		.data		=	&ipv6_devconf.accept_redirects,
34851da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
34861da177e4SLinus Torvalds 			.mode		=	0644,
34871da177e4SLinus Torvalds          		.proc_handler	=	&proc_dointvec,
34881da177e4SLinus Torvalds 		},
34891da177e4SLinus Torvalds 		{
34901da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_AUTOCONF,
34911da177e4SLinus Torvalds 			.procname	=	"autoconf",
34921da177e4SLinus Torvalds          		.data		=	&ipv6_devconf.autoconf,
34931da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
34941da177e4SLinus Torvalds 			.mode		=	0644,
34951da177e4SLinus Torvalds          		.proc_handler	=	&proc_dointvec,
34961da177e4SLinus Torvalds 		},
34971da177e4SLinus Torvalds 		{
34981da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_DAD_TRANSMITS,
34991da177e4SLinus Torvalds 			.procname	=	"dad_transmits",
35001da177e4SLinus Torvalds          		.data		=	&ipv6_devconf.dad_transmits,
35011da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
35021da177e4SLinus Torvalds 			.mode		=	0644,
35031da177e4SLinus Torvalds          		.proc_handler	=	&proc_dointvec,
35041da177e4SLinus Torvalds 		},
35051da177e4SLinus Torvalds 		{
35061da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_RTR_SOLICITS,
35071da177e4SLinus Torvalds 			.procname	=	"router_solicitations",
35081da177e4SLinus Torvalds          		.data		=	&ipv6_devconf.rtr_solicits,
35091da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
35101da177e4SLinus Torvalds 			.mode		=	0644,
35111da177e4SLinus Torvalds          		.proc_handler	=	&proc_dointvec,
35121da177e4SLinus Torvalds 		},
35131da177e4SLinus Torvalds 		{
35141da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_RTR_SOLICIT_INTERVAL,
35151da177e4SLinus Torvalds 			.procname	=	"router_solicitation_interval",
35161da177e4SLinus Torvalds          		.data		=	&ipv6_devconf.rtr_solicit_interval,
35171da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
35181da177e4SLinus Torvalds 			.mode		=	0644,
35191da177e4SLinus Torvalds          		.proc_handler	=	&proc_dointvec_jiffies,
35201da177e4SLinus Torvalds 			.strategy	=	&sysctl_jiffies,
35211da177e4SLinus Torvalds 		},
35221da177e4SLinus Torvalds 		{
35231da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_RTR_SOLICIT_DELAY,
35241da177e4SLinus Torvalds 			.procname	=	"router_solicitation_delay",
35251da177e4SLinus Torvalds          		.data		=	&ipv6_devconf.rtr_solicit_delay,
35261da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
35271da177e4SLinus Torvalds 			.mode		=	0644,
35281da177e4SLinus Torvalds          		.proc_handler	=	&proc_dointvec_jiffies,
35291da177e4SLinus Torvalds 			.strategy	=	&sysctl_jiffies,
35301da177e4SLinus Torvalds 		},
35311da177e4SLinus Torvalds 		{
35321da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_FORCE_MLD_VERSION,
35331da177e4SLinus Torvalds 			.procname	=	"force_mld_version",
35341da177e4SLinus Torvalds          		.data		=	&ipv6_devconf.force_mld_version,
35351da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
35361da177e4SLinus Torvalds 			.mode		=	0644,
35371da177e4SLinus Torvalds          		.proc_handler	=	&proc_dointvec,
35381da177e4SLinus Torvalds 		},
35391da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
35401da177e4SLinus Torvalds 		{
35411da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_USE_TEMPADDR,
35421da177e4SLinus Torvalds 			.procname	=	"use_tempaddr",
35431da177e4SLinus Torvalds 	 		.data		=	&ipv6_devconf.use_tempaddr,
35441da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
35451da177e4SLinus Torvalds 			.mode		=	0644,
35461da177e4SLinus Torvalds 	 		.proc_handler	=	&proc_dointvec,
35471da177e4SLinus Torvalds 		},
35481da177e4SLinus Torvalds 		{
35491da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_TEMP_VALID_LFT,
35501da177e4SLinus Torvalds 			.procname	=	"temp_valid_lft",
35511da177e4SLinus Torvalds 	 		.data		=	&ipv6_devconf.temp_valid_lft,
35521da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
35531da177e4SLinus Torvalds 			.mode		=	0644,
35541da177e4SLinus Torvalds 	 		.proc_handler	=	&proc_dointvec,
35551da177e4SLinus Torvalds 		},
35561da177e4SLinus Torvalds 		{
35571da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_TEMP_PREFERED_LFT,
35581da177e4SLinus Torvalds 			.procname	=	"temp_prefered_lft",
35591da177e4SLinus Torvalds 	 		.data		=	&ipv6_devconf.temp_prefered_lft,
35601da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
35611da177e4SLinus Torvalds 			.mode		=	0644,
35621da177e4SLinus Torvalds 	 		.proc_handler	=	&proc_dointvec,
35631da177e4SLinus Torvalds 		},
35641da177e4SLinus Torvalds 		{
35651da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_REGEN_MAX_RETRY,
35661da177e4SLinus Torvalds 			.procname	=	"regen_max_retry",
35671da177e4SLinus Torvalds 	 		.data		=	&ipv6_devconf.regen_max_retry,
35681da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
35691da177e4SLinus Torvalds 			.mode		=	0644,
35701da177e4SLinus Torvalds 	 		.proc_handler	=	&proc_dointvec,
35711da177e4SLinus Torvalds 		},
35721da177e4SLinus Torvalds 		{
35731da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_MAX_DESYNC_FACTOR,
35741da177e4SLinus Torvalds 			.procname	=	"max_desync_factor",
35751da177e4SLinus Torvalds 	 		.data		=	&ipv6_devconf.max_desync_factor,
35761da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
35771da177e4SLinus Torvalds 			.mode		=	0644,
35781da177e4SLinus Torvalds 	 		.proc_handler	=	&proc_dointvec,
35791da177e4SLinus Torvalds 		},
35801da177e4SLinus Torvalds #endif
35811da177e4SLinus Torvalds 		{
35821da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_MAX_ADDRESSES,
35831da177e4SLinus Torvalds 			.procname	=	"max_addresses",
35841da177e4SLinus Torvalds 			.data		=	&ipv6_devconf.max_addresses,
35851da177e4SLinus Torvalds 			.maxlen		=	sizeof(int),
35861da177e4SLinus Torvalds 			.mode		=	0644,
35871da177e4SLinus Torvalds 			.proc_handler	=	&proc_dointvec,
35881da177e4SLinus Torvalds 		},
35891da177e4SLinus Torvalds 		{
35901da177e4SLinus Torvalds 			.ctl_name	=	0,	/* sentinel */
35911da177e4SLinus Torvalds 		}
35921da177e4SLinus Torvalds 	},
35931da177e4SLinus Torvalds 	.addrconf_dev = {
35941da177e4SLinus Torvalds 		{
35951da177e4SLinus Torvalds 			.ctl_name	=	NET_PROTO_CONF_ALL,
35961da177e4SLinus Torvalds 			.procname	=	"all",
35971da177e4SLinus Torvalds 			.mode		=	0555,
35981da177e4SLinus Torvalds 			.child		=	addrconf_sysctl.addrconf_vars,
35991da177e4SLinus Torvalds 		},
36001da177e4SLinus Torvalds 		{
36011da177e4SLinus Torvalds 			.ctl_name	=	0,	/* sentinel */
36021da177e4SLinus Torvalds 		}
36031da177e4SLinus Torvalds 	},
36041da177e4SLinus Torvalds 	.addrconf_conf_dir = {
36051da177e4SLinus Torvalds 		{
36061da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6_CONF,
36071da177e4SLinus Torvalds 			.procname	=	"conf",
36081da177e4SLinus Torvalds 			.mode		=	0555,
36091da177e4SLinus Torvalds 			.child		=	addrconf_sysctl.addrconf_dev,
36101da177e4SLinus Torvalds 		},
36111da177e4SLinus Torvalds 		{
36121da177e4SLinus Torvalds 			.ctl_name	=	0,	/* sentinel */
36131da177e4SLinus Torvalds 		}
36141da177e4SLinus Torvalds 	},
36151da177e4SLinus Torvalds 	.addrconf_proto_dir = {
36161da177e4SLinus Torvalds 		{
36171da177e4SLinus Torvalds 			.ctl_name	=	NET_IPV6,
36181da177e4SLinus Torvalds 			.procname	=	"ipv6",
36191da177e4SLinus Torvalds 			.mode		=	0555,
36201da177e4SLinus Torvalds 			.child		=	addrconf_sysctl.addrconf_conf_dir,
36211da177e4SLinus Torvalds 		},
36221da177e4SLinus Torvalds 		{
36231da177e4SLinus Torvalds 			.ctl_name	=	0,	/* sentinel */
36241da177e4SLinus Torvalds 		}
36251da177e4SLinus Torvalds 	},
36261da177e4SLinus Torvalds 	.addrconf_root_dir = {
36271da177e4SLinus Torvalds 		{
36281da177e4SLinus Torvalds 			.ctl_name	=	CTL_NET,
36291da177e4SLinus Torvalds 			.procname	=	"net",
36301da177e4SLinus Torvalds 			.mode		=	0555,
36311da177e4SLinus Torvalds 			.child		=	addrconf_sysctl.addrconf_proto_dir,
36321da177e4SLinus Torvalds 		},
36331da177e4SLinus Torvalds 		{
36341da177e4SLinus Torvalds 			.ctl_name	=	0,	/* sentinel */
36351da177e4SLinus Torvalds 		}
36361da177e4SLinus Torvalds 	},
36371da177e4SLinus Torvalds };
36381da177e4SLinus Torvalds 
36391da177e4SLinus Torvalds static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
36401da177e4SLinus Torvalds {
36411da177e4SLinus Torvalds 	int i;
36421da177e4SLinus Torvalds 	struct net_device *dev = idev ? idev->dev : NULL;
36431da177e4SLinus Torvalds 	struct addrconf_sysctl_table *t;
36441da177e4SLinus Torvalds 	char *dev_name = NULL;
36451da177e4SLinus Torvalds 
36461da177e4SLinus Torvalds 	t = kmalloc(sizeof(*t), GFP_KERNEL);
36471da177e4SLinus Torvalds 	if (t == NULL)
36481da177e4SLinus Torvalds 		return;
36491da177e4SLinus Torvalds 	memcpy(t, &addrconf_sysctl, sizeof(*t));
36501da177e4SLinus Torvalds 	for (i=0; t->addrconf_vars[i].data; i++) {
36511da177e4SLinus Torvalds 		t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
36521da177e4SLinus Torvalds 		t->addrconf_vars[i].de = NULL;
36531da177e4SLinus Torvalds 		t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
36541da177e4SLinus Torvalds 	}
36551da177e4SLinus Torvalds 	if (dev) {
36561da177e4SLinus Torvalds 		dev_name = dev->name;
36571da177e4SLinus Torvalds 		t->addrconf_dev[0].ctl_name = dev->ifindex;
36581da177e4SLinus Torvalds 	} else {
36591da177e4SLinus Torvalds 		dev_name = "default";
36601da177e4SLinus Torvalds 		t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
36611da177e4SLinus Torvalds 	}
36621da177e4SLinus Torvalds 
36631da177e4SLinus Torvalds 	/*
36641da177e4SLinus Torvalds 	 * Make a copy of dev_name, because '.procname' is regarded as const
36651da177e4SLinus Torvalds 	 * by sysctl and we wouldn't want anyone to change it under our feet
36661da177e4SLinus Torvalds 	 * (see SIOCSIFNAME).
36671da177e4SLinus Torvalds 	 */
3668543537bdSPaulo Marques 	dev_name = kstrdup(dev_name, GFP_KERNEL);
36691da177e4SLinus Torvalds 	if (!dev_name)
36701da177e4SLinus Torvalds 	    goto free;
36711da177e4SLinus Torvalds 
36721da177e4SLinus Torvalds 	t->addrconf_dev[0].procname = dev_name;
36731da177e4SLinus Torvalds 
36741da177e4SLinus Torvalds 	t->addrconf_dev[0].child = t->addrconf_vars;
36751da177e4SLinus Torvalds 	t->addrconf_dev[0].de = NULL;
36761da177e4SLinus Torvalds 	t->addrconf_conf_dir[0].child = t->addrconf_dev;
36771da177e4SLinus Torvalds 	t->addrconf_conf_dir[0].de = NULL;
36781da177e4SLinus Torvalds 	t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
36791da177e4SLinus Torvalds 	t->addrconf_proto_dir[0].de = NULL;
36801da177e4SLinus Torvalds 	t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
36811da177e4SLinus Torvalds 	t->addrconf_root_dir[0].de = NULL;
36821da177e4SLinus Torvalds 
36831da177e4SLinus Torvalds 	t->sysctl_header = register_sysctl_table(t->addrconf_root_dir, 0);
36841da177e4SLinus Torvalds 	if (t->sysctl_header == NULL)
36851da177e4SLinus Torvalds 		goto free_procname;
36861da177e4SLinus Torvalds 	else
36871da177e4SLinus Torvalds 		p->sysctl = t;
36881da177e4SLinus Torvalds 	return;
36891da177e4SLinus Torvalds 
36901da177e4SLinus Torvalds 	/* error path */
36911da177e4SLinus Torvalds  free_procname:
36921da177e4SLinus Torvalds 	kfree(dev_name);
36931da177e4SLinus Torvalds  free:
36941da177e4SLinus Torvalds 	kfree(t);
36951da177e4SLinus Torvalds 
36961da177e4SLinus Torvalds 	return;
36971da177e4SLinus Torvalds }
36981da177e4SLinus Torvalds 
36991da177e4SLinus Torvalds static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
37001da177e4SLinus Torvalds {
37011da177e4SLinus Torvalds 	if (p->sysctl) {
37021da177e4SLinus Torvalds 		struct addrconf_sysctl_table *t = p->sysctl;
37031da177e4SLinus Torvalds 		p->sysctl = NULL;
37041da177e4SLinus Torvalds 		unregister_sysctl_table(t->sysctl_header);
37051da177e4SLinus Torvalds 		kfree(t->addrconf_dev[0].procname);
37061da177e4SLinus Torvalds 		kfree(t);
37071da177e4SLinus Torvalds 	}
37081da177e4SLinus Torvalds }
37091da177e4SLinus Torvalds 
37101da177e4SLinus Torvalds 
37111da177e4SLinus Torvalds #endif
37121da177e4SLinus Torvalds 
37131da177e4SLinus Torvalds /*
37141da177e4SLinus Torvalds  *      Device notifier
37151da177e4SLinus Torvalds  */
37161da177e4SLinus Torvalds 
37171da177e4SLinus Torvalds int register_inet6addr_notifier(struct notifier_block *nb)
37181da177e4SLinus Torvalds {
37191da177e4SLinus Torvalds         return notifier_chain_register(&inet6addr_chain, nb);
37201da177e4SLinus Torvalds }
37211da177e4SLinus Torvalds 
37221da177e4SLinus Torvalds int unregister_inet6addr_notifier(struct notifier_block *nb)
37231da177e4SLinus Torvalds {
37241da177e4SLinus Torvalds         return notifier_chain_unregister(&inet6addr_chain,nb);
37251da177e4SLinus Torvalds }
37261da177e4SLinus Torvalds 
37271da177e4SLinus Torvalds /*
37281da177e4SLinus Torvalds  *	Init / cleanup code
37291da177e4SLinus Torvalds  */
37301da177e4SLinus Torvalds 
37311da177e4SLinus Torvalds int __init addrconf_init(void)
37321da177e4SLinus Torvalds {
37331da177e4SLinus Torvalds 	int err = 0;
37341da177e4SLinus Torvalds 
37351da177e4SLinus Torvalds 	/* The addrconf netdev notifier requires that loopback_dev
37361da177e4SLinus Torvalds 	 * has it's ipv6 private information allocated and setup
37371da177e4SLinus Torvalds 	 * before it can bring up and give link-local addresses
37381da177e4SLinus Torvalds 	 * to other devices which are up.
37391da177e4SLinus Torvalds 	 *
37401da177e4SLinus Torvalds 	 * Unfortunately, loopback_dev is not necessarily the first
37411da177e4SLinus Torvalds 	 * entry in the global dev_base list of net devices.  In fact,
37421da177e4SLinus Torvalds 	 * it is likely to be the very last entry on that list.
37431da177e4SLinus Torvalds 	 * So this causes the notifier registry below to try and
37441da177e4SLinus Torvalds 	 * give link-local addresses to all devices besides loopback_dev
37451da177e4SLinus Torvalds 	 * first, then loopback_dev, which cases all the non-loopback_dev
37461da177e4SLinus Torvalds 	 * devices to fail to get a link-local address.
37471da177e4SLinus Torvalds 	 *
37481da177e4SLinus Torvalds 	 * So, as a temporary fix, allocate the ipv6 structure for
37491da177e4SLinus Torvalds 	 * loopback_dev first by hand.
37501da177e4SLinus Torvalds 	 * Longer term, all of the dependencies ipv6 has upon the loopback
37511da177e4SLinus Torvalds 	 * device and it being up should be removed.
37521da177e4SLinus Torvalds 	 */
37531da177e4SLinus Torvalds 	rtnl_lock();
37541da177e4SLinus Torvalds 	if (!ipv6_add_dev(&loopback_dev))
37551da177e4SLinus Torvalds 		err = -ENOMEM;
37561da177e4SLinus Torvalds 	rtnl_unlock();
37571da177e4SLinus Torvalds 	if (err)
37581da177e4SLinus Torvalds 		return err;
37591da177e4SLinus Torvalds 
3760c62dba90SHerbert Xu 	ip6_null_entry.rt6i_idev = in6_dev_get(&loopback_dev);
3761c62dba90SHerbert Xu 
37621da177e4SLinus Torvalds 	register_netdevice_notifier(&ipv6_dev_notf);
37631da177e4SLinus Torvalds 
37641da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
37651da177e4SLinus Torvalds 	md5_tfm = crypto_alloc_tfm("md5", 0);
37661da177e4SLinus Torvalds 	if (unlikely(md5_tfm == NULL))
37671da177e4SLinus Torvalds 		printk(KERN_WARNING
37681da177e4SLinus Torvalds 			"failed to load transform for md5\n");
37691da177e4SLinus Torvalds #endif
37701da177e4SLinus Torvalds 
37711da177e4SLinus Torvalds 	addrconf_verify(0);
37721da177e4SLinus Torvalds 	rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
37731da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
37741da177e4SLinus Torvalds 	addrconf_sysctl.sysctl_header =
37751da177e4SLinus Torvalds 		register_sysctl_table(addrconf_sysctl.addrconf_root_dir, 0);
37761da177e4SLinus Torvalds 	addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
37771da177e4SLinus Torvalds #endif
37781da177e4SLinus Torvalds 
37791da177e4SLinus Torvalds 	return 0;
37801da177e4SLinus Torvalds }
37811da177e4SLinus Torvalds 
37821da177e4SLinus Torvalds void __exit addrconf_cleanup(void)
37831da177e4SLinus Torvalds {
37841da177e4SLinus Torvalds  	struct net_device *dev;
37851da177e4SLinus Torvalds  	struct inet6_dev *idev;
37861da177e4SLinus Torvalds  	struct inet6_ifaddr *ifa;
37871da177e4SLinus Torvalds 	int i;
37881da177e4SLinus Torvalds 
37891da177e4SLinus Torvalds 	unregister_netdevice_notifier(&ipv6_dev_notf);
37901da177e4SLinus Torvalds 
37911da177e4SLinus Torvalds 	rtnetlink_links[PF_INET6] = NULL;
37921da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
37931da177e4SLinus Torvalds 	addrconf_sysctl_unregister(&ipv6_devconf_dflt);
37941da177e4SLinus Torvalds 	addrconf_sysctl_unregister(&ipv6_devconf);
37951da177e4SLinus Torvalds #endif
37961da177e4SLinus Torvalds 
37971da177e4SLinus Torvalds 	rtnl_lock();
37981da177e4SLinus Torvalds 
37991da177e4SLinus Torvalds 	/*
38001da177e4SLinus Torvalds 	 *	clean dev list.
38011da177e4SLinus Torvalds 	 */
38021da177e4SLinus Torvalds 
38031da177e4SLinus Torvalds 	for (dev=dev_base; dev; dev=dev->next) {
38041da177e4SLinus Torvalds 		if ((idev = __in6_dev_get(dev)) == NULL)
38051da177e4SLinus Torvalds 			continue;
38061da177e4SLinus Torvalds 		addrconf_ifdown(dev, 1);
38071da177e4SLinus Torvalds 	}
38081da177e4SLinus Torvalds 	addrconf_ifdown(&loopback_dev, 2);
38091da177e4SLinus Torvalds 
38101da177e4SLinus Torvalds 	/*
38111da177e4SLinus Torvalds 	 *	Check hash table.
38121da177e4SLinus Torvalds 	 */
38131da177e4SLinus Torvalds 
38141da177e4SLinus Torvalds 	write_lock_bh(&addrconf_hash_lock);
38151da177e4SLinus Torvalds 	for (i=0; i < IN6_ADDR_HSIZE; i++) {
38161da177e4SLinus Torvalds 		for (ifa=inet6_addr_lst[i]; ifa; ) {
38171da177e4SLinus Torvalds 			struct inet6_ifaddr *bifa;
38181da177e4SLinus Torvalds 
38191da177e4SLinus Torvalds 			bifa = ifa;
38201da177e4SLinus Torvalds 			ifa = ifa->lst_next;
38211da177e4SLinus Torvalds 			printk(KERN_DEBUG "bug: IPv6 address leakage detected: ifa=%p\n", bifa);
38221da177e4SLinus Torvalds 			/* Do not free it; something is wrong.
38231da177e4SLinus Torvalds 			   Now we can investigate it with debugger.
38241da177e4SLinus Torvalds 			 */
38251da177e4SLinus Torvalds 		}
38261da177e4SLinus Torvalds 	}
38271da177e4SLinus Torvalds 	write_unlock_bh(&addrconf_hash_lock);
38281da177e4SLinus Torvalds 
38291da177e4SLinus Torvalds 	del_timer(&addr_chk_timer);
38301da177e4SLinus Torvalds 
38311da177e4SLinus Torvalds 	rtnl_unlock();
38321da177e4SLinus Torvalds 
38331da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
38341da177e4SLinus Torvalds 	crypto_free_tfm(md5_tfm);
38351da177e4SLinus Torvalds 	md5_tfm = NULL;
38361da177e4SLinus Torvalds #endif
38371da177e4SLinus Torvalds 
38381da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
38391da177e4SLinus Torvalds 	proc_net_remove("if_inet6");
38401da177e4SLinus Torvalds #endif
38411da177e4SLinus Torvalds }
3842