xref: /openbmc/linux/net/ipv6/addrconf.c (revision 8f0315190dec88bf035d50e4fd1db89859b414f6)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	IPv6 Address [auto]configuration
31da177e4SLinus Torvalds  *	Linux INET6 implementation
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *	Authors:
61da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
71da177e4SLinus Torvalds  *	Alexey Kuznetsov	<kuznet@ms2.inr.ac.ru>
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
101da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
111da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
121da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds /*
161da177e4SLinus Torvalds  *	Changes:
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  *	Janos Farkas			:	delete timer on ifdown
191da177e4SLinus Torvalds  *	<chexum@bankinf.banki.hu>
201da177e4SLinus Torvalds  *	Andi Kleen			:	kill double kfree on module
211da177e4SLinus Torvalds  *						unload.
221da177e4SLinus Torvalds  *	Maciej W. Rozycki		:	FDDI support
231da177e4SLinus Torvalds  *	sekiya@USAGI			:	Don't send too many RS
241da177e4SLinus Torvalds  *						packets.
251da177e4SLinus Torvalds  *	yoshfuji@USAGI			:       Fixed interval between DAD
261da177e4SLinus Torvalds  *						packets.
271da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	improved accuracy of
281da177e4SLinus Torvalds  *						address validation timer.
291da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	Privacy Extensions (RFC3041)
301da177e4SLinus Torvalds  *						support.
311da177e4SLinus Torvalds  *	Yuji SEKIYA @USAGI		:	Don't assign a same IPv6
321da177e4SLinus Torvalds  *						address on a same interface.
331da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	ARCnet support
341da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	convert /proc/net/if_inet6 to
351da177e4SLinus Torvalds  *						seq_file.
36b1cacb68SYOSHIFUJI Hideaki  *	YOSHIFUJI Hideaki @USAGI	:	improved source address
37b1cacb68SYOSHIFUJI Hideaki  *						selection; consider scope,
38b1cacb68SYOSHIFUJI Hideaki  *						status etc.
391da177e4SLinus Torvalds  */
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #include <linux/errno.h>
421da177e4SLinus Torvalds #include <linux/types.h>
43a0bffffcSIlpo Järvinen #include <linux/kernel.h>
441da177e4SLinus Torvalds #include <linux/socket.h>
451da177e4SLinus Torvalds #include <linux/sockios.h>
461da177e4SLinus Torvalds #include <linux/net.h>
471da177e4SLinus Torvalds #include <linux/in6.h>
481da177e4SLinus Torvalds #include <linux/netdevice.h>
491823730fSThomas Graf #include <linux/if_addr.h>
501da177e4SLinus Torvalds #include <linux/if_arp.h>
511da177e4SLinus Torvalds #include <linux/if_arcnet.h>
521da177e4SLinus Torvalds #include <linux/if_infiniband.h>
531da177e4SLinus Torvalds #include <linux/route.h>
541da177e4SLinus Torvalds #include <linux/inetdevice.h>
551da177e4SLinus Torvalds #include <linux/init.h>
565a0e3ad6STejun Heo #include <linux/slab.h>
571da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
581da177e4SLinus Torvalds #include <linux/sysctl.h>
591da177e4SLinus Torvalds #endif
604fc268d2SRandy Dunlap #include <linux/capability.h>
611da177e4SLinus Torvalds #include <linux/delay.h>
621da177e4SLinus Torvalds #include <linux/notifier.h>
63543537bdSPaulo Marques #include <linux/string.h>
641da177e4SLinus Torvalds 
65457c4cbcSEric W. Biederman #include <net/net_namespace.h>
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>
765d620266SThomas Graf #include <net/netlink.h>
77f24e3d65SMitsuru Chinen #include <net/pkt_sched.h>
781da177e4SLinus Torvalds #include <linux/if_tunnel.h>
791da177e4SLinus Torvalds #include <linux/rtnetlink.h>
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
821da177e4SLinus Torvalds #include <linux/random.h>
831da177e4SLinus Torvalds #endif
841da177e4SLinus Torvalds 
85e21e8467SStephen Hemminger #include <linux/uaccess.h>
867f7d9a6bSHerbert Xu #include <asm/unaligned.h>
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds #include <linux/proc_fs.h>
891da177e4SLinus Torvalds #include <linux/seq_file.h>
90bc3b2d7fSPaul Gortmaker #include <linux/export.h>
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds /* Set to 3 to get tracing... */
931da177e4SLinus Torvalds #define ACONF_DEBUG 2
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds #if ACONF_DEBUG >= 3
961da177e4SLinus Torvalds #define ADBG(x) printk x
971da177e4SLinus Torvalds #else
981da177e4SLinus Torvalds #define ADBG(x)
991da177e4SLinus Torvalds #endif
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds #define	INFINITY_LIFE_TIME	0xFFFFFFFF
10218a31e1eSThomas Graf 
10318a31e1eSThomas Graf static inline u32 cstamp_delta(unsigned long cstamp)
10418a31e1eSThomas Graf {
10518a31e1eSThomas Graf 	return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
10618a31e1eSThomas Graf }
1071da177e4SLinus Torvalds 
108b2db7564SYOSHIFUJI Hideaki #define ADDRCONF_TIMER_FUZZ_MINUS	(HZ > 50 ? HZ/50 : 1)
109b2db7564SYOSHIFUJI Hideaki #define ADDRCONF_TIMER_FUZZ		(HZ / 4)
110b2db7564SYOSHIFUJI Hideaki #define ADDRCONF_TIMER_FUZZ_MAX		(HZ)
111b2db7564SYOSHIFUJI Hideaki 
1121da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
113f52295a9SPavel Emelyanov static void addrconf_sysctl_register(struct inet6_dev *idev);
114408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev);
115408c4768SPavel Emelyanov #else
116408c4768SPavel Emelyanov static inline void addrconf_sysctl_register(struct inet6_dev *idev)
117408c4768SPavel Emelyanov {
118408c4768SPavel Emelyanov }
119408c4768SPavel Emelyanov 
120408c4768SPavel Emelyanov static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
121408c4768SPavel Emelyanov {
122408c4768SPavel Emelyanov }
1231da177e4SLinus Torvalds #endif
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
1261da177e4SLinus Torvalds static int __ipv6_regen_rndid(struct inet6_dev *idev);
1271da177e4SLinus Torvalds static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
1281da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data);
1291da177e4SLinus Torvalds #endif
1301da177e4SLinus Torvalds 
1311b34be74SYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
1321da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev);
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds /*
1351da177e4SLinus Torvalds  *	Configured unicast address hash table
1361da177e4SLinus Torvalds  */
137c2e21293Sstephen hemminger static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
1385c578aedSstephen hemminger static DEFINE_SPINLOCK(addrconf_hash_lock);
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds static void addrconf_verify(unsigned long);
1411da177e4SLinus Torvalds 
1428d06afabSIngo Molnar static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
1431da177e4SLinus Torvalds static DEFINE_SPINLOCK(addrconf_verify_lock);
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
1461da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
1471da177e4SLinus Torvalds 
14893d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev,
14975c78500SMoni Shoua 				 unsigned long event);
1501da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how);
1511da177e4SLinus Torvalds 
152e431b8c0SJamal Hadi Salim static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
1531da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data);
1541da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
155c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev);
1561da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data);
1571da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1581da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
1611da177e4SLinus Torvalds 				struct prefix_info *pinfo);
1623e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
16306bfe655SDaniel Lezcano 			       struct net_device *dev);
1641da177e4SLinus Torvalds 
165e041c683SAlan Stern static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
1661da177e4SLinus Torvalds 
167888c848eSAdrian Bunk static struct ipv6_devconf ipv6_devconf __read_mostly = {
1681da177e4SLinus Torvalds 	.forwarding		= 0,
1691da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
1701da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
1711da177e4SLinus Torvalds 	.accept_ra		= 1,
1721da177e4SLinus Torvalds 	.accept_redirects	= 1,
1731da177e4SLinus Torvalds 	.autoconf		= 1,
1741da177e4SLinus Torvalds 	.force_mld_version	= 0,
1751da177e4SLinus Torvalds 	.dad_transmits		= 1,
1761da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
1771da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
1781da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
1791da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
1801da177e4SLinus Torvalds 	.use_tempaddr 		= 0,
1811da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
1821da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
1831da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
1841da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
1851da177e4SLinus Torvalds #endif
1861da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
18765f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
188c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
189930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
190930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
19152e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
19209c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
19309c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
19409c884d4SYOSHIFUJI Hideaki #endif
195930d6ff2SYOSHIFUJI Hideaki #endif
196fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
1970bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
198778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
1991b34be74SYOSHIFUJI Hideaki 	.accept_dad		= 1,
2001da177e4SLinus Torvalds };
2011da177e4SLinus Torvalds 
202ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
2031da177e4SLinus Torvalds 	.forwarding		= 0,
2041da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
2051da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
2061da177e4SLinus Torvalds 	.accept_ra		= 1,
2071da177e4SLinus Torvalds 	.accept_redirects	= 1,
2081da177e4SLinus Torvalds 	.autoconf		= 1,
2091da177e4SLinus Torvalds 	.dad_transmits		= 1,
2101da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
2111da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
2121da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
2131da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
2141da177e4SLinus Torvalds 	.use_tempaddr		= 0,
2151da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
2161da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
2171da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
2181da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
2191da177e4SLinus Torvalds #endif
2201da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
22165f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
222c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
223930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
224930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
22552e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
22609c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
22709c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
22809c884d4SYOSHIFUJI Hideaki #endif
229930d6ff2SYOSHIFUJI Hideaki #endif
230fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2310bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
232778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
2331b34be74SYOSHIFUJI Hideaki 	.accept_dad		= 1,
2341da177e4SLinus Torvalds };
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
2371da177e4SLinus Torvalds const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
2381da177e4SLinus Torvalds const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
239f3ee4010SYOSHIFUJI Hideaki const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
240f3ee4010SYOSHIFUJI Hideaki const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
2411da177e4SLinus Torvalds 
242f24e3d65SMitsuru Chinen /* Check if a valid qdisc is available */
24305297949SDavid S. Miller static inline bool addrconf_qdisc_ok(const struct net_device *dev)
244f24e3d65SMitsuru Chinen {
24505297949SDavid S. Miller 	return !qdisc_tx_is_noop(dev);
246f24e3d65SMitsuru Chinen }
247f24e3d65SMitsuru Chinen 
2482b5ead46SYOSHIFUJI Hideaki /* Check if a route is valid prefix route */
2492b5ead46SYOSHIFUJI Hideaki static inline int addrconf_is_prefix_route(const struct rt6_info *rt)
2502b5ead46SYOSHIFUJI Hideaki {
251a02cec21SEric Dumazet 	return (rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0;
2522b5ead46SYOSHIFUJI Hideaki }
2532b5ead46SYOSHIFUJI Hideaki 
2541da177e4SLinus Torvalds static void addrconf_del_timer(struct inet6_ifaddr *ifp)
2551da177e4SLinus Torvalds {
2561da177e4SLinus Torvalds 	if (del_timer(&ifp->timer))
2571da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
2581da177e4SLinus Torvalds }
2591da177e4SLinus Torvalds 
260e21e8467SStephen Hemminger enum addrconf_timer_t {
2611da177e4SLinus Torvalds 	AC_NONE,
2621da177e4SLinus Torvalds 	AC_DAD,
2631da177e4SLinus Torvalds 	AC_RS,
2641da177e4SLinus Torvalds };
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
2671da177e4SLinus Torvalds 			       enum addrconf_timer_t what,
2681da177e4SLinus Torvalds 			       unsigned long when)
2691da177e4SLinus Torvalds {
2701da177e4SLinus Torvalds 	if (!del_timer(&ifp->timer))
2711da177e4SLinus Torvalds 		in6_ifa_hold(ifp);
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	switch (what) {
2741da177e4SLinus Torvalds 	case AC_DAD:
2751da177e4SLinus Torvalds 		ifp->timer.function = addrconf_dad_timer;
2761da177e4SLinus Torvalds 		break;
2771da177e4SLinus Torvalds 	case AC_RS:
2781da177e4SLinus Torvalds 		ifp->timer.function = addrconf_rs_timer;
2791da177e4SLinus Torvalds 		break;
280e21e8467SStephen Hemminger 	default:
281e21e8467SStephen Hemminger 		break;
2821da177e4SLinus Torvalds 	}
2831da177e4SLinus Torvalds 	ifp->timer.expires = jiffies + when;
2841da177e4SLinus Torvalds 	add_timer(&ifp->timer);
2851da177e4SLinus Torvalds }
2861da177e4SLinus Torvalds 
2877f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev)
2887f7d9a6bSHerbert Xu {
2897d720c3eSTejun Heo 	if (snmp_mib_init((void __percpu **)idev->stats.ipv6,
2901823e4c8SEric Dumazet 			  sizeof(struct ipstats_mib),
2911823e4c8SEric Dumazet 			  __alignof__(struct ipstats_mib)) < 0)
2927f7d9a6bSHerbert Xu 		goto err_ip;
293be281e55SEric Dumazet 	idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
294be281e55SEric Dumazet 					GFP_KERNEL);
295be281e55SEric Dumazet 	if (!idev->stats.icmpv6dev)
2967f7d9a6bSHerbert Xu 		goto err_icmp;
297be281e55SEric Dumazet 	idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
298be281e55SEric Dumazet 					   GFP_KERNEL);
299be281e55SEric Dumazet 	if (!idev->stats.icmpv6msgdev)
30014878f75SDavid L Stevens 		goto err_icmpmsg;
3017f7d9a6bSHerbert Xu 
3027f7d9a6bSHerbert Xu 	return 0;
3037f7d9a6bSHerbert Xu 
30414878f75SDavid L Stevens err_icmpmsg:
305be281e55SEric Dumazet 	kfree(idev->stats.icmpv6dev);
3067f7d9a6bSHerbert Xu err_icmp:
3077d720c3eSTejun Heo 	snmp_mib_free((void __percpu **)idev->stats.ipv6);
3087f7d9a6bSHerbert Xu err_ip:
309aaf70ec7SPavel Emelyanov 	return -ENOMEM;
3107f7d9a6bSHerbert Xu }
3117f7d9a6bSHerbert Xu 
31216910b98SPavel Emelyanov static void snmp6_free_dev(struct inet6_dev *idev)
3137f7d9a6bSHerbert Xu {
314be281e55SEric Dumazet 	kfree(idev->stats.icmpv6msgdev);
315be281e55SEric Dumazet 	kfree(idev->stats.icmpv6dev);
3167d720c3eSTejun Heo 	snmp_mib_free((void __percpu **)idev->stats.ipv6);
3177f7d9a6bSHerbert Xu }
3187f7d9a6bSHerbert Xu 
3191da177e4SLinus Torvalds /* Nobody refers to this device, we may destroy it. */
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds void in6_dev_finish_destroy(struct inet6_dev *idev)
3221da177e4SLinus Torvalds {
3231da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
324547b792cSIlpo Järvinen 
325502a2ffdSstephen hemminger 	WARN_ON(!list_empty(&idev->addr_list));
326547b792cSIlpo Järvinen 	WARN_ON(idev->mc_list != NULL);
327547b792cSIlpo Järvinen 
3281da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
3291da177e4SLinus Torvalds 	printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
3301da177e4SLinus Torvalds #endif
3311da177e4SLinus Torvalds 	dev_put(dev);
3321da177e4SLinus Torvalds 	if (!idev->dead) {
333e21e8467SStephen Hemminger 		pr_warning("Freeing alive inet6 device %p\n", idev);
3341da177e4SLinus Torvalds 		return;
3351da177e4SLinus Torvalds 	}
3361da177e4SLinus Torvalds 	snmp6_free_dev(idev);
33738f57d1aSLai Jiangshan 	kfree_rcu(idev, rcu);
3381da177e4SLinus Torvalds }
3391da177e4SLinus Torvalds 
3407159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(in6_dev_finish_destroy);
3417159039aSYOSHIFUJI Hideaki 
3421da177e4SLinus Torvalds static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
3431da177e4SLinus Torvalds {
3441da177e4SLinus Torvalds 	struct inet6_dev *ndev;
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	ASSERT_RTNL();
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 	if (dev->mtu < IPV6_MIN_MTU)
3491da177e4SLinus Torvalds 		return NULL;
3501da177e4SLinus Torvalds 
351322f74a4SIngo Oeser 	ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
3521da177e4SLinus Torvalds 
353322f74a4SIngo Oeser 	if (ndev == NULL)
354322f74a4SIngo Oeser 		return NULL;
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 	rwlock_init(&ndev->lock);
3571da177e4SLinus Torvalds 	ndev->dev = dev;
358502a2ffdSstephen hemminger 	INIT_LIST_HEAD(&ndev->addr_list);
359502a2ffdSstephen hemminger 
360c346dca1SYOSHIFUJI Hideaki 	memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
3611da177e4SLinus Torvalds 	ndev->cnf.mtu6 = dev->mtu;
3621da177e4SLinus Torvalds 	ndev->cnf.sysctl = NULL;
3631da177e4SLinus Torvalds 	ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
3641da177e4SLinus Torvalds 	if (ndev->nd_parms == NULL) {
3651da177e4SLinus Torvalds 		kfree(ndev);
3661da177e4SLinus Torvalds 		return NULL;
3671da177e4SLinus Torvalds 	}
3680187bdfbSBen Hutchings 	if (ndev->cnf.forwarding)
3690187bdfbSBen Hutchings 		dev_disable_lro(dev);
3701da177e4SLinus Torvalds 	/* We refer to the device */
3711da177e4SLinus Torvalds 	dev_hold(dev);
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	if (snmp6_alloc_dev(ndev) < 0) {
3741da177e4SLinus Torvalds 		ADBG((KERN_WARNING
3751da177e4SLinus Torvalds 			"%s(): cannot allocate memory for statistics; dev=%s.\n",
3760dc47877SHarvey Harrison 			__func__, dev->name));
3771da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, ndev->nd_parms);
3788603e33dSRoy Li 		dev_put(dev);
3798603e33dSRoy Li 		kfree(ndev);
3801da177e4SLinus Torvalds 		return NULL;
3811da177e4SLinus Torvalds 	}
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 	if (snmp6_register_dev(ndev) < 0) {
3841da177e4SLinus Torvalds 		ADBG((KERN_WARNING
3851da177e4SLinus Torvalds 			"%s(): cannot create /proc/net/dev_snmp6/%s\n",
3860dc47877SHarvey Harrison 			__func__, dev->name));
3871da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, ndev->nd_parms);
3881da177e4SLinus Torvalds 		ndev->dead = 1;
3891da177e4SLinus Torvalds 		in6_dev_finish_destroy(ndev);
3901da177e4SLinus Torvalds 		return NULL;
3911da177e4SLinus Torvalds 	}
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 	/* One reference from device.  We must do this before
3941da177e4SLinus Torvalds 	 * we invoke __ipv6_regen_rndid().
3951da177e4SLinus Torvalds 	 */
3961da177e4SLinus Torvalds 	in6_dev_hold(ndev);
3971da177e4SLinus Torvalds 
3981b34be74SYOSHIFUJI Hideaki 	if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
3991b34be74SYOSHIFUJI Hideaki 		ndev->cnf.accept_dad = -1;
4001b34be74SYOSHIFUJI Hideaki 
4010be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
402c7dc89c0SFred L. Templin 	if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
403c7dc89c0SFred L. Templin 		printk(KERN_INFO
404c7dc89c0SFred L. Templin 		       "%s: Disabled Multicast RS\n",
405c7dc89c0SFred L. Templin 		       dev->name);
406c7dc89c0SFred L. Templin 		ndev->cnf.rtr_solicits = 0;
407c7dc89c0SFred L. Templin 	}
408b077d7abSYOSHIFUJI Hideaki #endif
409b077d7abSYOSHIFUJI Hideaki 
4101da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
411372e6c8fSstephen hemminger 	INIT_LIST_HEAD(&ndev->tempaddr_list);
4121da177e4SLinus Torvalds 	setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
4131da177e4SLinus Torvalds 	if ((dev->flags&IFF_LOOPBACK) ||
4141da177e4SLinus Torvalds 	    dev->type == ARPHRD_TUNNEL ||
4159625ed72SYOSHIFUJI Hideaki 	    dev->type == ARPHRD_TUNNEL6 ||
4161da177e4SLinus Torvalds 	    dev->type == ARPHRD_SIT ||
4171da177e4SLinus Torvalds 	    dev->type == ARPHRD_NONE) {
4180be669bbSJoerg Roedel 		ndev->cnf.use_tempaddr = -1;
4191da177e4SLinus Torvalds 	} else {
4201da177e4SLinus Torvalds 		in6_dev_hold(ndev);
4211da177e4SLinus Torvalds 		ipv6_regen_rndid((unsigned long) ndev);
4221da177e4SLinus Torvalds 	}
4231da177e4SLinus Torvalds #endif
4241da177e4SLinus Torvalds 
425f24e3d65SMitsuru Chinen 	if (netif_running(dev) && addrconf_qdisc_ok(dev))
42653aadcc9SHerbert Xu 		ndev->if_flags |= IF_READY;
42753aadcc9SHerbert Xu 
4281da177e4SLinus Torvalds 	ipv6_mc_init_dev(ndev);
4291da177e4SLinus Torvalds 	ndev->tstamp = jiffies;
430f52295a9SPavel Emelyanov 	addrconf_sysctl_register(ndev);
43130c4cf57SDavid L Stevens 	/* protected by rtnl_lock */
432a9b3cd7fSStephen Hemminger 	RCU_INIT_POINTER(dev->ip6_ptr, ndev);
433d88ae4ccSYOSHIFUJI Hideaki 
434d88ae4ccSYOSHIFUJI Hideaki 	/* Join all-node multicast group */
435f3ee4010SYOSHIFUJI Hideaki 	ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
436d88ae4ccSYOSHIFUJI Hideaki 
4371da177e4SLinus Torvalds 	return ndev;
4381da177e4SLinus Torvalds }
4391da177e4SLinus Torvalds 
440cee89473SYOSHIFUJI Hideaki static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
4411da177e4SLinus Torvalds {
4421da177e4SLinus Torvalds 	struct inet6_dev *idev;
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 	ASSERT_RTNL();
4451da177e4SLinus Torvalds 
446e21e8467SStephen Hemminger 	idev = __in6_dev_get(dev);
447e21e8467SStephen Hemminger 	if (!idev) {
448e21e8467SStephen Hemminger 		idev = ipv6_add_dev(dev);
449e21e8467SStephen Hemminger 		if (!idev)
4501da177e4SLinus Torvalds 			return NULL;
4511da177e4SLinus Torvalds 	}
452c5e33bddSYOSHIFUJI Hideaki 
4531da177e4SLinus Torvalds 	if (dev->flags&IFF_UP)
4541da177e4SLinus Torvalds 		ipv6_mc_up(idev);
4551da177e4SLinus Torvalds 	return idev;
4561da177e4SLinus Torvalds }
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
4591da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev)
4601da177e4SLinus Torvalds {
4611da177e4SLinus Torvalds 	struct net_device *dev;
4621da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	if (!idev)
4651da177e4SLinus Torvalds 		return;
4661da177e4SLinus Torvalds 	dev = idev->dev;
4670187bdfbSBen Hutchings 	if (idev->cnf.forwarding)
4680187bdfbSBen Hutchings 		dev_disable_lro(dev);
4691da177e4SLinus Torvalds 	if (dev && (dev->flags & IFF_MULTICAST)) {
4701da177e4SLinus Torvalds 		if (idev->cnf.forwarding)
471f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
4721da177e4SLinus Torvalds 		else
473f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
4741da177e4SLinus Torvalds 	}
475502a2ffdSstephen hemminger 
476502a2ffdSstephen hemminger 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
4772c12a74cSMichal Wrobel 		if (ifa->flags&IFA_F_TENTATIVE)
4782c12a74cSMichal Wrobel 			continue;
4791da177e4SLinus Torvalds 		if (idev->cnf.forwarding)
4801da177e4SLinus Torvalds 			addrconf_join_anycast(ifa);
4811da177e4SLinus Torvalds 		else
4821da177e4SLinus Torvalds 			addrconf_leave_anycast(ifa);
4831da177e4SLinus Torvalds 	}
4841da177e4SLinus Torvalds }
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 
487e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf)
4881da177e4SLinus Torvalds {
4891da177e4SLinus Torvalds 	struct net_device *dev;
4901da177e4SLinus Torvalds 	struct inet6_dev *idev;
4911da177e4SLinus Torvalds 
4928814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
493c6d14c84SEric Dumazet 	for_each_netdev_rcu(net, dev) {
4941da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
4951da177e4SLinus Torvalds 		if (idev) {
496e186932bSPavel Emelyanov 			int changed = (!idev->cnf.forwarding) ^ (!newf);
497e186932bSPavel Emelyanov 			idev->cnf.forwarding = newf;
4981da177e4SLinus Torvalds 			if (changed)
4991da177e4SLinus Torvalds 				dev_forward_change(idev);
5001da177e4SLinus Torvalds 		}
5011da177e4SLinus Torvalds 	}
502c6d14c84SEric Dumazet 	rcu_read_unlock();
5031da177e4SLinus Torvalds }
504c8fecf22SPavel Emelyanov 
505b325fddbSStephen Hemminger static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int old)
506c8fecf22SPavel Emelyanov {
507bff16c2fSPavel Emelyanov 	struct net *net;
508bff16c2fSPavel Emelyanov 
509bff16c2fSPavel Emelyanov 	net = (struct net *)table->extra2;
510441fc2a2SPavel Emelyanov 	if (p == &net->ipv6.devconf_dflt->forwarding)
511b325fddbSStephen Hemminger 		return 0;
512c8fecf22SPavel Emelyanov 
51388af182eSEric W. Biederman 	if (!rtnl_trylock()) {
51488af182eSEric W. Biederman 		/* Restore the original values before restarting */
51588af182eSEric W. Biederman 		*p = old;
5165007392dSEric W. Biederman 		return restart_syscall();
51788af182eSEric W. Biederman 	}
518b325fddbSStephen Hemminger 
519e186932bSPavel Emelyanov 	if (p == &net->ipv6.devconf_all->forwarding) {
520e186932bSPavel Emelyanov 		__s32 newf = net->ipv6.devconf_all->forwarding;
521e186932bSPavel Emelyanov 		net->ipv6.devconf_dflt->forwarding = newf;
522e186932bSPavel Emelyanov 		addrconf_forward_change(net, newf);
523c8fecf22SPavel Emelyanov 	} else if ((!*p) ^ (!old))
524c8fecf22SPavel Emelyanov 		dev_forward_change((struct inet6_dev *)table->extra1);
5250187bdfbSBen Hutchings 	rtnl_unlock();
526c8fecf22SPavel Emelyanov 
527c8fecf22SPavel Emelyanov 	if (*p)
5287b4da532SDaniel Lezcano 		rt6_purge_dflt_routers(net);
529b325fddbSStephen Hemminger 	return 1;
530c8fecf22SPavel Emelyanov }
5311da177e4SLinus Torvalds #endif
5321da177e4SLinus Torvalds 
5335c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */
5341da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
5351da177e4SLinus Torvalds {
536c2e21293Sstephen hemminger 	WARN_ON(!hlist_unhashed(&ifp->addr_lst));
537547b792cSIlpo Järvinen 
5381da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
5391da177e4SLinus Torvalds 	printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
5401da177e4SLinus Torvalds #endif
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	in6_dev_put(ifp->idev);
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 	if (del_timer(&ifp->timer))
545e21e8467SStephen Hemminger 		pr_notice("Timer is still running, when freeing ifa=%p\n", ifp);
5461da177e4SLinus Torvalds 
547e9d3e084SHerbert Xu 	if (ifp->state != INET6_IFADDR_STATE_DEAD) {
548e21e8467SStephen Hemminger 		pr_warning("Freeing alive inet6 address %p\n", ifp);
5491da177e4SLinus Torvalds 		return;
5501da177e4SLinus Torvalds 	}
551d8d1f30bSChangli Gao 	dst_release(&ifp->rt->dst);
5521da177e4SLinus Torvalds 
553e5785985SLai Jiangshan 	kfree_rcu(ifp, rcu);
5541da177e4SLinus Torvalds }
5551da177e4SLinus Torvalds 
556e55ffac6SBrian Haley static void
557e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
558e55ffac6SBrian Haley {
559502a2ffdSstephen hemminger 	struct list_head *p;
5608a6ce0c0SYOSHIFUJI Hideaki 	int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
561e55ffac6SBrian Haley 
562e55ffac6SBrian Haley 	/*
563e55ffac6SBrian Haley 	 * Each device address list is sorted in order of scope -
564e55ffac6SBrian Haley 	 * global before linklocal.
565e55ffac6SBrian Haley 	 */
566502a2ffdSstephen hemminger 	list_for_each(p, &idev->addr_list) {
567502a2ffdSstephen hemminger 		struct inet6_ifaddr *ifa
568502a2ffdSstephen hemminger 			= list_entry(p, struct inet6_ifaddr, if_list);
5698a6ce0c0SYOSHIFUJI Hideaki 		if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
570e55ffac6SBrian Haley 			break;
571e55ffac6SBrian Haley 	}
572e55ffac6SBrian Haley 
573b54c9b98SDavid S. Miller 	list_add_tail(&ifp->if_list, p);
574e55ffac6SBrian Haley }
575e55ffac6SBrian Haley 
5763a88a81dSstephen hemminger static u32 ipv6_addr_hash(const struct in6_addr *addr)
5773eb84f49SYOSHIFUJI Hideaki {
5783eb84f49SYOSHIFUJI Hideaki 	/*
5793eb84f49SYOSHIFUJI Hideaki 	 * We perform the hash function over the last 64 bits of the address
5803eb84f49SYOSHIFUJI Hideaki 	 * This will include the IEEE address token on links that support it.
5813eb84f49SYOSHIFUJI Hideaki 	 */
5820eae88f3SEric Dumazet 	return jhash_2words((__force u32)addr->s6_addr32[2],
5830eae88f3SEric Dumazet 			    (__force u32)addr->s6_addr32[3], 0)
5843a88a81dSstephen hemminger 		& (IN6_ADDR_HSIZE - 1);
5853eb84f49SYOSHIFUJI Hideaki }
5863eb84f49SYOSHIFUJI Hideaki 
5871da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds static struct inet6_ifaddr *
5901da177e4SLinus Torvalds ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
591e431b8c0SJamal Hadi Salim 	      int scope, u32 flags)
5921da177e4SLinus Torvalds {
5931da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
5941da177e4SLinus Torvalds 	struct rt6_info *rt;
5953a88a81dSstephen hemminger 	unsigned int hash;
5961da177e4SLinus Torvalds 	int err = 0;
597d68b8270SYOSHIFUJI Hideaki 	int addr_type = ipv6_addr_type(addr);
598d68b8270SYOSHIFUJI Hideaki 
599d68b8270SYOSHIFUJI Hideaki 	if (addr_type == IPV6_ADDR_ANY ||
600d68b8270SYOSHIFUJI Hideaki 	    addr_type & IPV6_ADDR_MULTICAST ||
601d68b8270SYOSHIFUJI Hideaki 	    (!(idev->dev->flags & IFF_LOOPBACK) &&
602d68b8270SYOSHIFUJI Hideaki 	     addr_type & IPV6_ADDR_LOOPBACK))
603d68b8270SYOSHIFUJI Hideaki 		return ERR_PTR(-EADDRNOTAVAIL);
6041da177e4SLinus Torvalds 
6058814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
6061da177e4SLinus Torvalds 	if (idev->dead) {
6071da177e4SLinus Torvalds 		err = -ENODEV;			/*XXX*/
6081da177e4SLinus Torvalds 		goto out2;
6091da177e4SLinus Torvalds 	}
6101da177e4SLinus Torvalds 
61156d417b1SBrian Haley 	if (idev->cnf.disable_ipv6) {
6129bdd8d40SBrian Haley 		err = -EACCES;
6139bdd8d40SBrian Haley 		goto out2;
6149bdd8d40SBrian Haley 	}
6159bdd8d40SBrian Haley 
6165c578aedSstephen hemminger 	spin_lock(&addrconf_hash_lock);
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds 	/* Ignore adding duplicate addresses on an interface */
619c346dca1SYOSHIFUJI Hideaki 	if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
6201da177e4SLinus Torvalds 		ADBG(("ipv6_add_addr: already assigned\n"));
6211da177e4SLinus Torvalds 		err = -EEXIST;
6221da177e4SLinus Torvalds 		goto out;
6231da177e4SLinus Torvalds 	}
6241da177e4SLinus Torvalds 
625322f74a4SIngo Oeser 	ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds 	if (ifa == NULL) {
6281da177e4SLinus Torvalds 		ADBG(("ipv6_add_addr: malloc failed\n"));
6291da177e4SLinus Torvalds 		err = -ENOBUFS;
6301da177e4SLinus Torvalds 		goto out;
6311da177e4SLinus Torvalds 	}
6321da177e4SLinus Torvalds 
633*8f031519SDavid S. Miller 	rt = addrconf_dst_alloc(idev, addr, false);
6341da177e4SLinus Torvalds 	if (IS_ERR(rt)) {
6351da177e4SLinus Torvalds 		err = PTR_ERR(rt);
6361da177e4SLinus Torvalds 		goto out;
6371da177e4SLinus Torvalds 	}
6381da177e4SLinus Torvalds 
6394e3fd7a0SAlexey Dobriyan 	ifa->addr = *addr;
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 	spin_lock_init(&ifa->lock);
642e9d3e084SHerbert Xu 	spin_lock_init(&ifa->state_lock);
6431da177e4SLinus Torvalds 	init_timer(&ifa->timer);
644c2e21293Sstephen hemminger 	INIT_HLIST_NODE(&ifa->addr_lst);
6451da177e4SLinus Torvalds 	ifa->timer.data = (unsigned long) ifa;
6461da177e4SLinus Torvalds 	ifa->scope = scope;
6471da177e4SLinus Torvalds 	ifa->prefix_len = pfxlen;
6481da177e4SLinus Torvalds 	ifa->flags = flags | IFA_F_TENTATIVE;
6491da177e4SLinus Torvalds 	ifa->cstamp = ifa->tstamp = jiffies;
6501da177e4SLinus Torvalds 
65157f5f544SKeir Fraser 	ifa->rt = rt;
65257f5f544SKeir Fraser 
65395c385b4SNeil Horman 	/*
65495c385b4SNeil Horman 	 * part one of RFC 4429, section 3.3
65595c385b4SNeil Horman 	 * We should not configure an address as
65695c385b4SNeil Horman 	 * optimistic if we do not yet know the link
65795c385b4SNeil Horman 	 * layer address of our nexhop router
65895c385b4SNeil Horman 	 */
65995c385b4SNeil Horman 
66027217455SDavid Miller 	if (dst_get_neighbour_noref_raw(&rt->dst) == NULL)
66195c385b4SNeil Horman 		ifa->flags &= ~IFA_F_OPTIMISTIC;
66295c385b4SNeil Horman 
6631da177e4SLinus Torvalds 	ifa->idev = idev;
6641da177e4SLinus Torvalds 	in6_dev_hold(idev);
6651da177e4SLinus Torvalds 	/* For caller */
6661da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 	/* Add to big hash table */
6691da177e4SLinus Torvalds 	hash = ipv6_addr_hash(addr);
6701da177e4SLinus Torvalds 
6715c578aedSstephen hemminger 	hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
6725c578aedSstephen hemminger 	spin_unlock(&addrconf_hash_lock);
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	write_lock(&idev->lock);
6751da177e4SLinus Torvalds 	/* Add to inet6_dev unicast addr list. */
676e55ffac6SBrian Haley 	ipv6_link_dev_addr(idev, ifa);
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
6791da177e4SLinus Torvalds 	if (ifa->flags&IFA_F_TEMPORARY) {
680372e6c8fSstephen hemminger 		list_add(&ifa->tmp_list, &idev->tempaddr_list);
6811da177e4SLinus Torvalds 		in6_ifa_hold(ifa);
6821da177e4SLinus Torvalds 	}
6831da177e4SLinus Torvalds #endif
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
6861da177e4SLinus Torvalds 	write_unlock(&idev->lock);
6871da177e4SLinus Torvalds out2:
6888814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
6891da177e4SLinus Torvalds 
690fd92833aSYOSHIFUJI Hideaki 	if (likely(err == 0))
691e041c683SAlan Stern 		atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
6921da177e4SLinus Torvalds 	else {
6931da177e4SLinus Torvalds 		kfree(ifa);
6941da177e4SLinus Torvalds 		ifa = ERR_PTR(err);
6951da177e4SLinus Torvalds 	}
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 	return ifa;
6981da177e4SLinus Torvalds out:
6995c578aedSstephen hemminger 	spin_unlock(&addrconf_hash_lock);
7001da177e4SLinus Torvalds 	goto out2;
7011da177e4SLinus Torvalds }
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp)
7061da177e4SLinus Torvalds {
707502a2ffdSstephen hemminger 	struct inet6_ifaddr *ifa, *ifn;
7081da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
7094c5ff6a6SHerbert Xu 	int state;
7101da177e4SLinus Torvalds 	int deleted = 0, onlink = 0;
7111da177e4SLinus Torvalds 	unsigned long expires = jiffies;
7121da177e4SLinus Torvalds 
7134c5ff6a6SHerbert Xu 	spin_lock_bh(&ifp->state_lock);
7144c5ff6a6SHerbert Xu 	state = ifp->state;
715e9d3e084SHerbert Xu 	ifp->state = INET6_IFADDR_STATE_DEAD;
7164c5ff6a6SHerbert Xu 	spin_unlock_bh(&ifp->state_lock);
7174c5ff6a6SHerbert Xu 
7184c5ff6a6SHerbert Xu 	if (state == INET6_IFADDR_STATE_DEAD)
7194c5ff6a6SHerbert Xu 		goto out;
7201da177e4SLinus Torvalds 
7215c578aedSstephen hemminger 	spin_lock_bh(&addrconf_hash_lock);
7225c578aedSstephen hemminger 	hlist_del_init_rcu(&ifp->addr_lst);
7235c578aedSstephen hemminger 	spin_unlock_bh(&addrconf_hash_lock);
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
7261da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
7271da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_TEMPORARY) {
728372e6c8fSstephen hemminger 		list_del(&ifp->tmp_list);
7291da177e4SLinus Torvalds 		if (ifp->ifpub) {
7301da177e4SLinus Torvalds 			in6_ifa_put(ifp->ifpub);
7311da177e4SLinus Torvalds 			ifp->ifpub = NULL;
7321da177e4SLinus Torvalds 		}
7331da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
7341da177e4SLinus Torvalds 	}
7351da177e4SLinus Torvalds #endif
7361da177e4SLinus Torvalds 
737502a2ffdSstephen hemminger 	list_for_each_entry_safe(ifa, ifn, &idev->addr_list, if_list) {
7381da177e4SLinus Torvalds 		if (ifa == ifp) {
739502a2ffdSstephen hemminger 			list_del_init(&ifp->if_list);
7401da177e4SLinus Torvalds 			__in6_ifa_put(ifp);
741502a2ffdSstephen hemminger 
7421da177e4SLinus Torvalds 			if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
7431da177e4SLinus Torvalds 				break;
7441da177e4SLinus Torvalds 			deleted = 1;
7451d142804SKristian Slavov 			continue;
7461da177e4SLinus Torvalds 		} else if (ifp->flags & IFA_F_PERMANENT) {
7471da177e4SLinus Torvalds 			if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
7481da177e4SLinus Torvalds 					      ifp->prefix_len)) {
7491da177e4SLinus Torvalds 				if (ifa->flags & IFA_F_PERMANENT) {
7501da177e4SLinus Torvalds 					onlink = 1;
7511da177e4SLinus Torvalds 					if (deleted)
7521da177e4SLinus Torvalds 						break;
7531da177e4SLinus Torvalds 				} else {
7541da177e4SLinus Torvalds 					unsigned long lifetime;
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds 					if (!onlink)
7571da177e4SLinus Torvalds 						onlink = -1;
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 					spin_lock(&ifa->lock);
7604bed72e4SYOSHIFUJI Hideaki 
7614bed72e4SYOSHIFUJI Hideaki 					lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
7624bed72e4SYOSHIFUJI Hideaki 					/*
7634bed72e4SYOSHIFUJI Hideaki 					 * Note: Because this address is
7644bed72e4SYOSHIFUJI Hideaki 					 * not permanent, lifetime <
7654bed72e4SYOSHIFUJI Hideaki 					 * LONG_MAX / HZ here.
7664bed72e4SYOSHIFUJI Hideaki 					 */
7671da177e4SLinus Torvalds 					if (time_before(expires,
7681da177e4SLinus Torvalds 							ifa->tstamp + lifetime * HZ))
7691da177e4SLinus Torvalds 						expires = ifa->tstamp + lifetime * HZ;
7701da177e4SLinus Torvalds 					spin_unlock(&ifa->lock);
7711da177e4SLinus Torvalds 				}
7721da177e4SLinus Torvalds 			}
7731da177e4SLinus Torvalds 		}
7741da177e4SLinus Torvalds 	}
7751da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
7761da177e4SLinus Torvalds 
777b2238566SAndrey Vagin 	addrconf_del_timer(ifp);
778b2238566SAndrey Vagin 
7791da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_DELADDR, ifp);
7801da177e4SLinus Torvalds 
781e041c683SAlan Stern 	atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	/*
7841da177e4SLinus Torvalds 	 * Purge or update corresponding prefix
7851da177e4SLinus Torvalds 	 *
7861da177e4SLinus Torvalds 	 * 1) we don't purge prefix here if address was not permanent.
7871da177e4SLinus Torvalds 	 *    prefix is managed by its own lifetime.
7881da177e4SLinus Torvalds 	 * 2) if there're no addresses, delete prefix.
7891da177e4SLinus Torvalds 	 * 3) if there're still other permanent address(es),
7901da177e4SLinus Torvalds 	 *    corresponding prefix is still permanent.
7911da177e4SLinus Torvalds 	 * 4) otherwise, update prefix lifetime to the
7921da177e4SLinus Torvalds 	 *    longest valid lifetime among the corresponding
7931da177e4SLinus Torvalds 	 *    addresses on the device.
7941da177e4SLinus Torvalds 	 *    Note: subsequent RA will update lifetime.
7951da177e4SLinus Torvalds 	 *
7961da177e4SLinus Torvalds 	 * --yoshfuji
7971da177e4SLinus Torvalds 	 */
7981da177e4SLinus Torvalds 	if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
7991da177e4SLinus Torvalds 		struct in6_addr prefix;
8001da177e4SLinus Torvalds 		struct rt6_info *rt;
801c346dca1SYOSHIFUJI Hideaki 		struct net *net = dev_net(ifp->idev->dev);
8021da177e4SLinus Torvalds 		ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
8036fda7350SBenjamin Thery 		rt = rt6_lookup(net, &prefix, NULL, ifp->idev->dev->ifindex, 1);
8041da177e4SLinus Torvalds 
8052b5ead46SYOSHIFUJI Hideaki 		if (rt && addrconf_is_prefix_route(rt)) {
8061da177e4SLinus Torvalds 			if (onlink == 0) {
807e0a1ad73SThomas Graf 				ip6_del_rt(rt);
8081da177e4SLinus Torvalds 				rt = NULL;
8091da177e4SLinus Torvalds 			} else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
8101da177e4SLinus Torvalds 				rt->rt6i_expires = expires;
8111da177e4SLinus Torvalds 				rt->rt6i_flags |= RTF_EXPIRES;
8121da177e4SLinus Torvalds 			}
8131da177e4SLinus Torvalds 		}
814d8d1f30bSChangli Gao 		dst_release(&rt->dst);
8151da177e4SLinus Torvalds 	}
8161da177e4SLinus Torvalds 
817c3968a85SDaniel Walter 	/* clean up prefsrc entries */
818c3968a85SDaniel Walter 	rt6_remove_prefsrc(ifp);
8194c5ff6a6SHerbert Xu out:
8201da177e4SLinus Torvalds 	in6_ifa_put(ifp);
8211da177e4SLinus Torvalds }
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
8241da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
8251da177e4SLinus Torvalds {
8261da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
8271da177e4SLinus Torvalds 	struct in6_addr addr, *tmpaddr;
82876f793e3SLorenzo Colitti 	unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
829eac55bf9SBenoit Boissinot 	unsigned long regen_advance;
8301da177e4SLinus Torvalds 	int tmp_plen;
8311da177e4SLinus Torvalds 	int ret = 0;
8321da177e4SLinus Torvalds 	int max_addresses;
83395c385b4SNeil Horman 	u32 addr_flags;
83476f793e3SLorenzo Colitti 	unsigned long now = jiffies;
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 	write_lock(&idev->lock);
8371da177e4SLinus Torvalds 	if (ift) {
8381da177e4SLinus Torvalds 		spin_lock_bh(&ift->lock);
8391da177e4SLinus Torvalds 		memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
8401da177e4SLinus Torvalds 		spin_unlock_bh(&ift->lock);
8411da177e4SLinus Torvalds 		tmpaddr = &addr;
8421da177e4SLinus Torvalds 	} else {
8431da177e4SLinus Torvalds 		tmpaddr = NULL;
8441da177e4SLinus Torvalds 	}
8451da177e4SLinus Torvalds retry:
8461da177e4SLinus Torvalds 	in6_dev_hold(idev);
8471da177e4SLinus Torvalds 	if (idev->cnf.use_tempaddr <= 0) {
8481da177e4SLinus Torvalds 		write_unlock(&idev->lock);
8491da177e4SLinus Torvalds 		printk(KERN_INFO
8501da177e4SLinus Torvalds 			"ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
8511da177e4SLinus Torvalds 		in6_dev_put(idev);
8521da177e4SLinus Torvalds 		ret = -1;
8531da177e4SLinus Torvalds 		goto out;
8541da177e4SLinus Torvalds 	}
8551da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
8561da177e4SLinus Torvalds 	if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
8571da177e4SLinus Torvalds 		idev->cnf.use_tempaddr = -1;	/*XXX*/
8581da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
8591da177e4SLinus Torvalds 		write_unlock(&idev->lock);
8601da177e4SLinus Torvalds 		printk(KERN_WARNING
8611da177e4SLinus Torvalds 			"ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
8621da177e4SLinus Torvalds 		in6_dev_put(idev);
8631da177e4SLinus Torvalds 		ret = -1;
8641da177e4SLinus Torvalds 		goto out;
8651da177e4SLinus Torvalds 	}
8661da177e4SLinus Torvalds 	in6_ifa_hold(ifp);
8671da177e4SLinus Torvalds 	memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
8681da177e4SLinus Torvalds 	if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
8691da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
8701da177e4SLinus Torvalds 		write_unlock(&idev->lock);
8711da177e4SLinus Torvalds 		printk(KERN_WARNING
8721da177e4SLinus Torvalds 			"ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
8731da177e4SLinus Torvalds 		in6_ifa_put(ifp);
8741da177e4SLinus Torvalds 		in6_dev_put(idev);
8751da177e4SLinus Torvalds 		ret = -1;
8761da177e4SLinus Torvalds 		goto out;
8771da177e4SLinus Torvalds 	}
8781da177e4SLinus Torvalds 	memcpy(&addr.s6_addr[8], idev->rndid, 8);
87976f793e3SLorenzo Colitti 	age = (now - ifp->tstamp) / HZ;
8801da177e4SLinus Torvalds 	tmp_valid_lft = min_t(__u32,
8811da177e4SLinus Torvalds 			      ifp->valid_lft,
8827a876b0eSGlenn Wurster 			      idev->cnf.temp_valid_lft + age);
8831da177e4SLinus Torvalds 	tmp_prefered_lft = min_t(__u32,
8841da177e4SLinus Torvalds 				 ifp->prefered_lft,
8857a876b0eSGlenn Wurster 				 idev->cnf.temp_prefered_lft + age -
886784e2710SBen Hutchings 				 idev->cnf.max_desync_factor);
8871da177e4SLinus Torvalds 	tmp_plen = ifp->prefix_len;
8881da177e4SLinus Torvalds 	max_addresses = idev->cnf.max_addresses;
8891da177e4SLinus Torvalds 	tmp_tstamp = ifp->tstamp;
8901da177e4SLinus Torvalds 	spin_unlock_bh(&ifp->lock);
8911da177e4SLinus Torvalds 
892eac55bf9SBenoit Boissinot 	regen_advance = idev->cnf.regen_max_retry *
893eac55bf9SBenoit Boissinot 	                idev->cnf.dad_transmits *
894eac55bf9SBenoit Boissinot 	                idev->nd_parms->retrans_time / HZ;
8951da177e4SLinus Torvalds 	write_unlock(&idev->lock);
89695c385b4SNeil Horman 
897eac55bf9SBenoit Boissinot 	/* A temporary address is created only if this calculated Preferred
898eac55bf9SBenoit Boissinot 	 * Lifetime is greater than REGEN_ADVANCE time units.  In particular,
899eac55bf9SBenoit Boissinot 	 * an implementation must not create a temporary address with a zero
900eac55bf9SBenoit Boissinot 	 * Preferred Lifetime.
901eac55bf9SBenoit Boissinot 	 */
902eac55bf9SBenoit Boissinot 	if (tmp_prefered_lft <= regen_advance) {
903eac55bf9SBenoit Boissinot 		in6_ifa_put(ifp);
904eac55bf9SBenoit Boissinot 		in6_dev_put(idev);
905eac55bf9SBenoit Boissinot 		ret = -1;
906eac55bf9SBenoit Boissinot 		goto out;
907eac55bf9SBenoit Boissinot 	}
908eac55bf9SBenoit Boissinot 
90995c385b4SNeil Horman 	addr_flags = IFA_F_TEMPORARY;
91095c385b4SNeil Horman 	/* set in addrconf_prefix_rcv() */
91195c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
91295c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
91395c385b4SNeil Horman 
9141da177e4SLinus Torvalds 	ift = !max_addresses ||
9151da177e4SLinus Torvalds 	      ipv6_count_addresses(idev) < max_addresses ?
9161da177e4SLinus Torvalds 		ipv6_add_addr(idev, &addr, tmp_plen,
91795c385b4SNeil Horman 			      ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK,
91895c385b4SNeil Horman 			      addr_flags) : NULL;
9191da177e4SLinus Torvalds 	if (!ift || IS_ERR(ift)) {
9201da177e4SLinus Torvalds 		in6_ifa_put(ifp);
9211da177e4SLinus Torvalds 		in6_dev_put(idev);
9221da177e4SLinus Torvalds 		printk(KERN_INFO
9231da177e4SLinus Torvalds 			"ipv6_create_tempaddr(): retry temporary address regeneration.\n");
9241da177e4SLinus Torvalds 		tmpaddr = &addr;
9251da177e4SLinus Torvalds 		write_lock(&idev->lock);
9261da177e4SLinus Torvalds 		goto retry;
9271da177e4SLinus Torvalds 	}
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds 	spin_lock_bh(&ift->lock);
9301da177e4SLinus Torvalds 	ift->ifpub = ifp;
9311da177e4SLinus Torvalds 	ift->valid_lft = tmp_valid_lft;
9321da177e4SLinus Torvalds 	ift->prefered_lft = tmp_prefered_lft;
93376f793e3SLorenzo Colitti 	ift->cstamp = now;
9341da177e4SLinus Torvalds 	ift->tstamp = tmp_tstamp;
9351da177e4SLinus Torvalds 	spin_unlock_bh(&ift->lock);
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 	addrconf_dad_start(ift, 0);
9381da177e4SLinus Torvalds 	in6_ifa_put(ift);
9391da177e4SLinus Torvalds 	in6_dev_put(idev);
9401da177e4SLinus Torvalds out:
9411da177e4SLinus Torvalds 	return ret;
9421da177e4SLinus Torvalds }
9431da177e4SLinus Torvalds #endif
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds /*
946072047e4SYOSHIFUJI Hideaki  *	Choose an appropriate source address (RFC3484)
9471da177e4SLinus Torvalds  */
948a9b05723SYOSHIFUJI Hideaki enum {
949a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_INIT = 0,
950a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LOCAL,
951a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_SCOPE,
952a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFERRED,
953a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
954a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_HOA,
955a9b05723SYOSHIFUJI Hideaki #endif
956a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_OIF,
957a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LABEL,
958a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_PRIVACY
959a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PRIVACY,
960a9b05723SYOSHIFUJI Hideaki #endif
961a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_ORCHID,
962a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFIX,
963a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_MAX
964072047e4SYOSHIFUJI Hideaki };
965072047e4SYOSHIFUJI Hideaki 
966a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score {
967a9b05723SYOSHIFUJI Hideaki 	int			rule;
968a9b05723SYOSHIFUJI Hideaki 	int			addr_type;
969a9b05723SYOSHIFUJI Hideaki 	struct inet6_ifaddr	*ifa;
970a9b05723SYOSHIFUJI Hideaki 	DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
971a9b05723SYOSHIFUJI Hideaki 	int			scopedist;
972a9b05723SYOSHIFUJI Hideaki 	int			matchlen;
973a9b05723SYOSHIFUJI Hideaki };
974a9b05723SYOSHIFUJI Hideaki 
975a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst {
9769acd9f3aSYOSHIFUJI Hideaki 	const struct in6_addr *addr;
977a9b05723SYOSHIFUJI Hideaki 	int ifindex;
978a9b05723SYOSHIFUJI Hideaki 	int scope;
979a9b05723SYOSHIFUJI Hideaki 	int label;
9807cbca67cSYOSHIFUJI Hideaki 	unsigned int prefs;
981a9b05723SYOSHIFUJI Hideaki };
982072047e4SYOSHIFUJI Hideaki 
983b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type)
9841da177e4SLinus Torvalds {
98545bb0060SUlrich Weber 	if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
986072047e4SYOSHIFUJI Hideaki 		return 1;
987072047e4SYOSHIFUJI Hideaki 	return 0;
9881da177e4SLinus Torvalds }
9891da177e4SLinus Torvalds 
9903de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net,
9913de23255SBenjamin Thery 			       struct ipv6_saddr_score *score,
992a9b05723SYOSHIFUJI Hideaki 			       struct ipv6_saddr_dst *dst,
993a9b05723SYOSHIFUJI Hideaki 			       int i)
994a9b05723SYOSHIFUJI Hideaki {
995a9b05723SYOSHIFUJI Hideaki 	int ret;
996a9b05723SYOSHIFUJI Hideaki 
997a9b05723SYOSHIFUJI Hideaki 	if (i <= score->rule) {
998a9b05723SYOSHIFUJI Hideaki 		switch (i) {
999a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_SCOPE:
1000a9b05723SYOSHIFUJI Hideaki 			ret = score->scopedist;
1001a9b05723SYOSHIFUJI Hideaki 			break;
1002a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_PREFIX:
1003a9b05723SYOSHIFUJI Hideaki 			ret = score->matchlen;
1004a9b05723SYOSHIFUJI Hideaki 			break;
1005a9b05723SYOSHIFUJI Hideaki 		default:
1006a9b05723SYOSHIFUJI Hideaki 			ret = !!test_bit(i, score->scorebits);
1007a9b05723SYOSHIFUJI Hideaki 		}
1008a9b05723SYOSHIFUJI Hideaki 		goto out;
1009a9b05723SYOSHIFUJI Hideaki 	}
1010a9b05723SYOSHIFUJI Hideaki 
1011a9b05723SYOSHIFUJI Hideaki 	switch (i) {
1012a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_INIT:
1013a9b05723SYOSHIFUJI Hideaki 		/* Rule 0: remember if hiscore is not ready yet */
1014a9b05723SYOSHIFUJI Hideaki 		ret = !!score->ifa;
1015a9b05723SYOSHIFUJI Hideaki 		break;
1016a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LOCAL:
1017a9b05723SYOSHIFUJI Hideaki 		/* Rule 1: Prefer same address */
1018a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1019a9b05723SYOSHIFUJI Hideaki 		break;
1020a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_SCOPE:
1021a9b05723SYOSHIFUJI Hideaki 		/* Rule 2: Prefer appropriate scope
1022a9b05723SYOSHIFUJI Hideaki 		 *
1023a9b05723SYOSHIFUJI Hideaki 		 *      ret
1024a9b05723SYOSHIFUJI Hideaki 		 *       ^
1025a9b05723SYOSHIFUJI Hideaki 		 *    -1 |  d 15
1026a9b05723SYOSHIFUJI Hideaki 		 *    ---+--+-+---> scope
1027a9b05723SYOSHIFUJI Hideaki 		 *       |
1028a9b05723SYOSHIFUJI Hideaki 		 *       |             d is scope of the destination.
1029a9b05723SYOSHIFUJI Hideaki 		 *  B-d  |  \
1030a9b05723SYOSHIFUJI Hideaki 		 *       |   \      <- smaller scope is better if
1031a9b05723SYOSHIFUJI Hideaki 		 *  B-15 |    \        if scope is enough for destinaion.
1032a9b05723SYOSHIFUJI Hideaki 		 *       |             ret = B - scope (-1 <= scope >= d <= 15).
1033a9b05723SYOSHIFUJI Hideaki 		 * d-C-1 | /
1034a9b05723SYOSHIFUJI Hideaki 		 *       |/         <- greater is better
1035a9b05723SYOSHIFUJI Hideaki 		 *   -C  /             if scope is not enough for destination.
1036a9b05723SYOSHIFUJI Hideaki 		 *      /|             ret = scope - C (-1 <= d < scope <= 15).
1037a9b05723SYOSHIFUJI Hideaki 		 *
1038a9b05723SYOSHIFUJI Hideaki 		 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1039a9b05723SYOSHIFUJI Hideaki 		 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1040a9b05723SYOSHIFUJI Hideaki 		 * Assume B = 0 and we get C > 29.
1041a9b05723SYOSHIFUJI Hideaki 		 */
1042a9b05723SYOSHIFUJI Hideaki 		ret = __ipv6_addr_src_scope(score->addr_type);
1043a9b05723SYOSHIFUJI Hideaki 		if (ret >= dst->scope)
1044a9b05723SYOSHIFUJI Hideaki 			ret = -ret;
1045a9b05723SYOSHIFUJI Hideaki 		else
1046a9b05723SYOSHIFUJI Hideaki 			ret -= 128;	/* 30 is enough */
1047a9b05723SYOSHIFUJI Hideaki 		score->scopedist = ret;
1048a9b05723SYOSHIFUJI Hideaki 		break;
1049a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFERRED:
1050a9b05723SYOSHIFUJI Hideaki 		/* Rule 3: Avoid deprecated and optimistic addresses */
1051a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_saddr_preferred(score->addr_type) ||
1052a9b05723SYOSHIFUJI Hideaki 		      !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC));
1053a9b05723SYOSHIFUJI Hideaki 		break;
1054a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1055a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_HOA:
10567cbca67cSYOSHIFUJI Hideaki 	    {
1057a9b05723SYOSHIFUJI Hideaki 		/* Rule 4: Prefer home address */
10587cbca67cSYOSHIFUJI Hideaki 		int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
10597cbca67cSYOSHIFUJI Hideaki 		ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1060a9b05723SYOSHIFUJI Hideaki 		break;
10617cbca67cSYOSHIFUJI Hideaki 	    }
1062a9b05723SYOSHIFUJI Hideaki #endif
1063a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_OIF:
1064a9b05723SYOSHIFUJI Hideaki 		/* Rule 5: Prefer outgoing interface */
1065a9b05723SYOSHIFUJI Hideaki 		ret = (!dst->ifindex ||
1066a9b05723SYOSHIFUJI Hideaki 		       dst->ifindex == score->ifa->idev->dev->ifindex);
1067a9b05723SYOSHIFUJI Hideaki 		break;
1068a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LABEL:
1069a9b05723SYOSHIFUJI Hideaki 		/* Rule 6: Prefer matching label */
10703de23255SBenjamin Thery 		ret = ipv6_addr_label(net,
10713de23255SBenjamin Thery 				      &score->ifa->addr, score->addr_type,
1072a9b05723SYOSHIFUJI Hideaki 				      score->ifa->idev->dev->ifindex) == dst->label;
1073a9b05723SYOSHIFUJI Hideaki 		break;
1074a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_PRIVACY
1075a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PRIVACY:
10767cbca67cSYOSHIFUJI Hideaki 	    {
1077a9b05723SYOSHIFUJI Hideaki 		/* Rule 7: Prefer public address
107825985edcSLucas De Marchi 		 * Note: prefer temporary address if use_tempaddr >= 2
1079a9b05723SYOSHIFUJI Hideaki 		 */
10807cbca67cSYOSHIFUJI Hideaki 		int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
10817cbca67cSYOSHIFUJI Hideaki 				!!(dst->prefs & IPV6_PREFER_SRC_TMP) :
10827cbca67cSYOSHIFUJI Hideaki 				score->ifa->idev->cnf.use_tempaddr >= 2;
10837cbca67cSYOSHIFUJI Hideaki 		ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1084a9b05723SYOSHIFUJI Hideaki 		break;
10857cbca67cSYOSHIFUJI Hideaki 	    }
1086a9b05723SYOSHIFUJI Hideaki #endif
1087a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_ORCHID:
1088a9b05723SYOSHIFUJI Hideaki 		/* Rule 8-: Prefer ORCHID vs ORCHID or
1089a9b05723SYOSHIFUJI Hideaki 		 *	    non-ORCHID vs non-ORCHID
1090a9b05723SYOSHIFUJI Hideaki 		 */
1091a9b05723SYOSHIFUJI Hideaki 		ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1092a9b05723SYOSHIFUJI Hideaki 			ipv6_addr_orchid(dst->addr));
1093a9b05723SYOSHIFUJI Hideaki 		break;
1094a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFIX:
1095a9b05723SYOSHIFUJI Hideaki 		/* Rule 8: Use longest matching prefix */
1096a9b05723SYOSHIFUJI Hideaki 		score->matchlen = ret = ipv6_addr_diff(&score->ifa->addr,
1097a9b05723SYOSHIFUJI Hideaki 						       dst->addr);
1098a9b05723SYOSHIFUJI Hideaki 		break;
1099a9b05723SYOSHIFUJI Hideaki 	default:
1100a9b05723SYOSHIFUJI Hideaki 		ret = 0;
1101a9b05723SYOSHIFUJI Hideaki 	}
1102a9b05723SYOSHIFUJI Hideaki 
1103a9b05723SYOSHIFUJI Hideaki 	if (ret)
1104a9b05723SYOSHIFUJI Hideaki 		__set_bit(i, score->scorebits);
1105a9b05723SYOSHIFUJI Hideaki 	score->rule = i;
1106a9b05723SYOSHIFUJI Hideaki out:
1107a9b05723SYOSHIFUJI Hideaki 	return ret;
1108a9b05723SYOSHIFUJI Hideaki }
1109a9b05723SYOSHIFUJI Hideaki 
1110191cd582SBrian Haley int ipv6_dev_get_saddr(struct net *net, struct net_device *dst_dev,
11119acd9f3aSYOSHIFUJI Hideaki 		       const struct in6_addr *daddr, unsigned int prefs,
11127cbca67cSYOSHIFUJI Hideaki 		       struct in6_addr *saddr)
11131da177e4SLinus Torvalds {
1114a9b05723SYOSHIFUJI Hideaki 	struct ipv6_saddr_score scores[2],
1115a9b05723SYOSHIFUJI Hideaki 				*score = &scores[0], *hiscore = &scores[1];
1116a9b05723SYOSHIFUJI Hideaki 	struct ipv6_saddr_dst dst;
1117072047e4SYOSHIFUJI Hideaki 	struct net_device *dev;
1118a9b05723SYOSHIFUJI Hideaki 	int dst_type;
11191da177e4SLinus Torvalds 
1120a9b05723SYOSHIFUJI Hideaki 	dst_type = __ipv6_addr_type(daddr);
1121a9b05723SYOSHIFUJI Hideaki 	dst.addr = daddr;
1122a9b05723SYOSHIFUJI Hideaki 	dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1123a9b05723SYOSHIFUJI Hideaki 	dst.scope = __ipv6_addr_src_scope(dst_type);
11243de23255SBenjamin Thery 	dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
11257cbca67cSYOSHIFUJI Hideaki 	dst.prefs = prefs;
1126a9b05723SYOSHIFUJI Hideaki 
1127a9b05723SYOSHIFUJI Hideaki 	hiscore->rule = -1;
1128a9b05723SYOSHIFUJI Hideaki 	hiscore->ifa = NULL;
11291da177e4SLinus Torvalds 
11308814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
1131072047e4SYOSHIFUJI Hideaki 
1132c6d14c84SEric Dumazet 	for_each_netdev_rcu(net, dev) {
1133072047e4SYOSHIFUJI Hideaki 		struct inet6_dev *idev;
1134072047e4SYOSHIFUJI Hideaki 
1135a9b05723SYOSHIFUJI Hideaki 		/* Candidate Source Address (section 4)
1136072047e4SYOSHIFUJI Hideaki 		 *  - multicast and link-local destination address,
1137072047e4SYOSHIFUJI Hideaki 		 *    the set of candidate source address MUST only
1138072047e4SYOSHIFUJI Hideaki 		 *    include addresses assigned to interfaces
1139072047e4SYOSHIFUJI Hideaki 		 *    belonging to the same link as the outgoing
1140072047e4SYOSHIFUJI Hideaki 		 *    interface.
1141072047e4SYOSHIFUJI Hideaki 		 * (- For site-local destination addresses, the
1142072047e4SYOSHIFUJI Hideaki 		 *    set of candidate source addresses MUST only
1143072047e4SYOSHIFUJI Hideaki 		 *    include addresses assigned to interfaces
1144072047e4SYOSHIFUJI Hideaki 		 *    belonging to the same site as the outgoing
1145072047e4SYOSHIFUJI Hideaki 		 *    interface.)
1146072047e4SYOSHIFUJI Hideaki 		 */
1147a9b05723SYOSHIFUJI Hideaki 		if (((dst_type & IPV6_ADDR_MULTICAST) ||
1148a9b05723SYOSHIFUJI Hideaki 		     dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
1149a9b05723SYOSHIFUJI Hideaki 		    dst.ifindex && dev->ifindex != dst.ifindex)
1150072047e4SYOSHIFUJI Hideaki 			continue;
1151072047e4SYOSHIFUJI Hideaki 
11521da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
1153072047e4SYOSHIFUJI Hideaki 		if (!idev)
1154072047e4SYOSHIFUJI Hideaki 			continue;
1155072047e4SYOSHIFUJI Hideaki 
11561da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
1157502a2ffdSstephen hemminger 		list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1158a9b05723SYOSHIFUJI Hideaki 			int i;
1159072047e4SYOSHIFUJI Hideaki 
1160a9b05723SYOSHIFUJI Hideaki 			/*
11616b3ae80aSYOSHIFUJI Hideaki 			 * - Tentative Address (RFC2462 section 5.4)
11626b3ae80aSYOSHIFUJI Hideaki 			 *  - A tentative address is not considered
11636b3ae80aSYOSHIFUJI Hideaki 			 *    "assigned to an interface" in the traditional
116495c385b4SNeil Horman 			 *    sense, unless it is also flagged as optimistic.
11656b3ae80aSYOSHIFUJI Hideaki 			 * - Candidate Source Address (section 4)
1166072047e4SYOSHIFUJI Hideaki 			 *  - In any case, anycast addresses, multicast
1167072047e4SYOSHIFUJI Hideaki 			 *    addresses, and the unspecified address MUST
1168072047e4SYOSHIFUJI Hideaki 			 *    NOT be included in a candidate set.
1169072047e4SYOSHIFUJI Hideaki 			 */
1170a9b05723SYOSHIFUJI Hideaki 			if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1171a9b05723SYOSHIFUJI Hideaki 			    (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
11726b3ae80aSYOSHIFUJI Hideaki 				continue;
1173a9b05723SYOSHIFUJI Hideaki 
1174a9b05723SYOSHIFUJI Hideaki 			score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1175a9b05723SYOSHIFUJI Hideaki 
1176a9b05723SYOSHIFUJI Hideaki 			if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1177a9b05723SYOSHIFUJI Hideaki 				     score->addr_type & IPV6_ADDR_MULTICAST)) {
1178072047e4SYOSHIFUJI Hideaki 				LIMIT_NETDEBUG(KERN_DEBUG
1179072047e4SYOSHIFUJI Hideaki 					       "ADDRCONF: unspecified / multicast address "
1180072047e4SYOSHIFUJI Hideaki 					       "assigned as unicast address on %s",
1181072047e4SYOSHIFUJI Hideaki 					       dev->name);
11821da177e4SLinus Torvalds 				continue;
1183072047e4SYOSHIFUJI Hideaki 			}
1184072047e4SYOSHIFUJI Hideaki 
1185a9b05723SYOSHIFUJI Hideaki 			score->rule = -1;
1186a9b05723SYOSHIFUJI Hideaki 			bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1187072047e4SYOSHIFUJI Hideaki 
1188a9b05723SYOSHIFUJI Hideaki 			for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1189a9b05723SYOSHIFUJI Hideaki 				int minihiscore, miniscore;
1190072047e4SYOSHIFUJI Hideaki 
11913de23255SBenjamin Thery 				minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i);
11923de23255SBenjamin Thery 				miniscore = ipv6_get_saddr_eval(net, score, &dst, i);
1193072047e4SYOSHIFUJI Hideaki 
1194a9b05723SYOSHIFUJI Hideaki 				if (minihiscore > miniscore) {
1195a9b05723SYOSHIFUJI Hideaki 					if (i == IPV6_SADDR_RULE_SCOPE &&
1196a9b05723SYOSHIFUJI Hideaki 					    score->scopedist > 0) {
1197a9b05723SYOSHIFUJI Hideaki 						/*
1198a9b05723SYOSHIFUJI Hideaki 						 * special case:
1199a9b05723SYOSHIFUJI Hideaki 						 * each remaining entry
1200a9b05723SYOSHIFUJI Hideaki 						 * has too small (not enough)
1201a9b05723SYOSHIFUJI Hideaki 						 * scope, because ifa entries
1202a9b05723SYOSHIFUJI Hideaki 						 * are sorted by their scope
1203a9b05723SYOSHIFUJI Hideaki 						 * values.
1204072047e4SYOSHIFUJI Hideaki 						 */
1205a9b05723SYOSHIFUJI Hideaki 						goto try_nextdev;
1206072047e4SYOSHIFUJI Hideaki 					}
1207a9b05723SYOSHIFUJI Hideaki 					break;
1208a9b05723SYOSHIFUJI Hideaki 				} else if (minihiscore < miniscore) {
1209a9b05723SYOSHIFUJI Hideaki 					if (hiscore->ifa)
1210a9b05723SYOSHIFUJI Hideaki 						in6_ifa_put(hiscore->ifa);
121199cd07a5SJuha-Matti Tapio 
1212a9b05723SYOSHIFUJI Hideaki 					in6_ifa_hold(score->ifa);
1213072047e4SYOSHIFUJI Hideaki 
1214a0bffffcSIlpo Järvinen 					swap(hiscore, score);
1215a9b05723SYOSHIFUJI Hideaki 
1216a9b05723SYOSHIFUJI Hideaki 					/* restore our iterator */
1217a9b05723SYOSHIFUJI Hideaki 					score->ifa = hiscore->ifa;
1218a9b05723SYOSHIFUJI Hideaki 
1219a9b05723SYOSHIFUJI Hideaki 					break;
12201da177e4SLinus Torvalds 				}
1221a9b05723SYOSHIFUJI Hideaki 			}
1222a9b05723SYOSHIFUJI Hideaki 		}
1223a9b05723SYOSHIFUJI Hideaki try_nextdev:
12241da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
12251da177e4SLinus Torvalds 	}
12268814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
12271da177e4SLinus Torvalds 
1228a9b05723SYOSHIFUJI Hideaki 	if (!hiscore->ifa)
1229072047e4SYOSHIFUJI Hideaki 		return -EADDRNOTAVAIL;
12301da177e4SLinus Torvalds 
12314e3fd7a0SAlexey Dobriyan 	*saddr = hiscore->ifa->addr;
1232a9b05723SYOSHIFUJI Hideaki 	in6_ifa_put(hiscore->ifa);
1233072047e4SYOSHIFUJI Hideaki 	return 0;
12341da177e4SLinus Torvalds }
12355e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr);
12361da177e4SLinus Torvalds 
123795c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
123895c385b4SNeil Horman 		    unsigned char banned_flags)
12391da177e4SLinus Torvalds {
12401da177e4SLinus Torvalds 	struct inet6_dev *idev;
12411da177e4SLinus Torvalds 	int err = -EADDRNOTAVAIL;
12421da177e4SLinus Torvalds 
12438814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
1244e21e8467SStephen Hemminger 	idev = __in6_dev_get(dev);
1245e21e8467SStephen Hemminger 	if (idev) {
12461da177e4SLinus Torvalds 		struct inet6_ifaddr *ifp;
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
1249502a2ffdSstephen hemminger 		list_for_each_entry(ifp, &idev->addr_list, if_list) {
1250502a2ffdSstephen hemminger 			if (ifp->scope == IFA_LINK &&
1251502a2ffdSstephen hemminger 			    !(ifp->flags & banned_flags)) {
12524e3fd7a0SAlexey Dobriyan 				*addr = ifp->addr;
12531da177e4SLinus Torvalds 				err = 0;
12541da177e4SLinus Torvalds 				break;
12551da177e4SLinus Torvalds 			}
12561da177e4SLinus Torvalds 		}
12571da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
12581da177e4SLinus Torvalds 	}
12598814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
12601da177e4SLinus Torvalds 	return err;
12611da177e4SLinus Torvalds }
12621da177e4SLinus Torvalds 
12631da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev)
12641da177e4SLinus Torvalds {
12651da177e4SLinus Torvalds 	int cnt = 0;
12661da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
1269502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list)
12701da177e4SLinus Torvalds 		cnt++;
12711da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
12721da177e4SLinus Torvalds 	return cnt;
12731da177e4SLinus Torvalds }
12741da177e4SLinus Torvalds 
1275b71d1d42SEric Dumazet int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
1276bfeade08SDaniel Lezcano 		  struct net_device *dev, int strict)
12771da177e4SLinus Torvalds {
1278eedf042aSStephen Hemminger 	struct inet6_ifaddr *ifp;
1279c2e21293Sstephen hemminger 	struct hlist_node *node;
12803a88a81dSstephen hemminger 	unsigned int hash = ipv6_addr_hash(addr);
12811da177e4SLinus Torvalds 
12825c578aedSstephen hemminger 	rcu_read_lock_bh();
12835c578aedSstephen hemminger 	hlist_for_each_entry_rcu(ifp, node, &inet6_addr_lst[hash], addr_lst) {
1284878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
1285bfeade08SDaniel Lezcano 			continue;
12861da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr) &&
1287eedf042aSStephen Hemminger 		    !(ifp->flags&IFA_F_TENTATIVE) &&
1288eedf042aSStephen Hemminger 		    (dev == NULL || ifp->idev->dev == dev ||
1289eedf042aSStephen Hemminger 		     !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
12905c578aedSstephen hemminger 			rcu_read_unlock_bh();
1291eedf042aSStephen Hemminger 			return 1;
1292eedf042aSStephen Hemminger 		}
1293eedf042aSStephen Hemminger 	}
12945c578aedSstephen hemminger 
1295eedf042aSStephen Hemminger 	rcu_read_unlock_bh();
1296eedf042aSStephen Hemminger 	return 0;
12971da177e4SLinus Torvalds }
12987159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_addr);
12997159039aSYOSHIFUJI Hideaki 
13003e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
130106bfe655SDaniel Lezcano 			       struct net_device *dev)
13021da177e4SLinus Torvalds {
13033e81c6daSDavid S. Miller 	unsigned int hash = ipv6_addr_hash(addr);
13041da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
1305c2e21293Sstephen hemminger 	struct hlist_node *node;
13061da177e4SLinus Torvalds 
1307c2e21293Sstephen hemminger 	hlist_for_each_entry(ifp, node, &inet6_addr_lst[hash], addr_lst) {
1308878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
130906bfe655SDaniel Lezcano 			continue;
13101da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
13111da177e4SLinus Torvalds 			if (dev == NULL || ifp->idev->dev == dev)
13123e81c6daSDavid S. Miller 				return true;
13131da177e4SLinus Torvalds 		}
13141da177e4SLinus Torvalds 	}
13153e81c6daSDavid S. Miller 	return false;
13161da177e4SLinus Torvalds }
13171da177e4SLinus Torvalds 
1318b71d1d42SEric Dumazet int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
131952eeeb84SYOSHIFUJI Hideaki {
132052eeeb84SYOSHIFUJI Hideaki 	struct inet6_dev *idev;
132152eeeb84SYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifa;
132252eeeb84SYOSHIFUJI Hideaki 	int	onlink;
132352eeeb84SYOSHIFUJI Hideaki 
132452eeeb84SYOSHIFUJI Hideaki 	onlink = 0;
132552eeeb84SYOSHIFUJI Hideaki 	rcu_read_lock();
132652eeeb84SYOSHIFUJI Hideaki 	idev = __in6_dev_get(dev);
132752eeeb84SYOSHIFUJI Hideaki 	if (idev) {
132852eeeb84SYOSHIFUJI Hideaki 		read_lock_bh(&idev->lock);
1329502a2ffdSstephen hemminger 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
133052eeeb84SYOSHIFUJI Hideaki 			onlink = ipv6_prefix_equal(addr, &ifa->addr,
133152eeeb84SYOSHIFUJI Hideaki 						   ifa->prefix_len);
133252eeeb84SYOSHIFUJI Hideaki 			if (onlink)
133352eeeb84SYOSHIFUJI Hideaki 				break;
133452eeeb84SYOSHIFUJI Hideaki 		}
133552eeeb84SYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
133652eeeb84SYOSHIFUJI Hideaki 	}
133752eeeb84SYOSHIFUJI Hideaki 	rcu_read_unlock();
133852eeeb84SYOSHIFUJI Hideaki 	return onlink;
133952eeeb84SYOSHIFUJI Hideaki }
134052eeeb84SYOSHIFUJI Hideaki 
134152eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix);
134252eeeb84SYOSHIFUJI Hideaki 
13439acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
13441cab3da6SDaniel Lezcano 				     struct net_device *dev, int strict)
13451da177e4SLinus Torvalds {
1346b79d1d54SDavid S. Miller 	struct inet6_ifaddr *ifp, *result = NULL;
13473a88a81dSstephen hemminger 	unsigned int hash = ipv6_addr_hash(addr);
1348b79d1d54SDavid S. Miller 	struct hlist_node *node;
13491da177e4SLinus Torvalds 
13505c578aedSstephen hemminger 	rcu_read_lock_bh();
13514f70eccaSEric Dumazet 	hlist_for_each_entry_rcu_bh(ifp, node, &inet6_addr_lst[hash], addr_lst) {
1352878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
13531cab3da6SDaniel Lezcano 			continue;
13541da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
13551da177e4SLinus Torvalds 			if (dev == NULL || ifp->idev->dev == dev ||
13561da177e4SLinus Torvalds 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1357b79d1d54SDavid S. Miller 				result = ifp;
13581da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
13591da177e4SLinus Torvalds 				break;
13601da177e4SLinus Torvalds 			}
13611da177e4SLinus Torvalds 		}
13621da177e4SLinus Torvalds 	}
13635c578aedSstephen hemminger 	rcu_read_unlock_bh();
13641da177e4SLinus Torvalds 
1365b79d1d54SDavid S. Miller 	return result;
13661da177e4SLinus Torvalds }
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */
13691da177e4SLinus Torvalds 
1370cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
13711da177e4SLinus Torvalds {
13721da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_PERMANENT) {
13731da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
13741da177e4SLinus Torvalds 		addrconf_del_timer(ifp);
13751da177e4SLinus Torvalds 		ifp->flags |= IFA_F_TENTATIVE;
1376cc411d0bSBrian Haley 		if (dad_failed)
1377cc411d0bSBrian Haley 			ifp->flags |= IFA_F_DADFAILED;
13781da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
1379e2577a06SHerbert Xu 		if (dad_failed)
1380e2577a06SHerbert Xu 			ipv6_ifa_notify(0, ifp);
13811da177e4SLinus Torvalds 		in6_ifa_put(ifp);
13821da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
13831da177e4SLinus Torvalds 	} else if (ifp->flags&IFA_F_TEMPORARY) {
13841da177e4SLinus Torvalds 		struct inet6_ifaddr *ifpub;
13851da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
13861da177e4SLinus Torvalds 		ifpub = ifp->ifpub;
13871da177e4SLinus Torvalds 		if (ifpub) {
13881da177e4SLinus Torvalds 			in6_ifa_hold(ifpub);
13891da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
13901da177e4SLinus Torvalds 			ipv6_create_tempaddr(ifpub, ifp);
13911da177e4SLinus Torvalds 			in6_ifa_put(ifpub);
13921da177e4SLinus Torvalds 		} else {
13931da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
13941da177e4SLinus Torvalds 		}
13951da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
13961da177e4SLinus Torvalds #endif
13971da177e4SLinus Torvalds 	} else
13981da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
13991da177e4SLinus Torvalds }
14001da177e4SLinus Torvalds 
1401f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1402f2344a13SHerbert Xu {
1403f2344a13SHerbert Xu 	int err = -ENOENT;
1404f2344a13SHerbert Xu 
1405f2344a13SHerbert Xu 	spin_lock(&ifp->state_lock);
1406f2344a13SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DAD) {
1407f2344a13SHerbert Xu 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
1408f2344a13SHerbert Xu 		err = 0;
1409f2344a13SHerbert Xu 	}
1410f2344a13SHerbert Xu 	spin_unlock(&ifp->state_lock);
1411f2344a13SHerbert Xu 
1412f2344a13SHerbert Xu 	return err;
1413f2344a13SHerbert Xu }
1414f2344a13SHerbert Xu 
14153c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp)
14163c21edbdSYOSHIFUJI Hideaki {
14171b34be74SYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
14189bdd8d40SBrian Haley 
1419853dc2e0SUrsula Braun 	if (addrconf_dad_end(ifp)) {
1420853dc2e0SUrsula Braun 		in6_ifa_put(ifp);
1421f2344a13SHerbert Xu 		return;
1422853dc2e0SUrsula Braun 	}
1423f2344a13SHerbert Xu 
14249bdd8d40SBrian Haley 	if (net_ratelimit())
14250522fea6SJens Rosenboom 		printk(KERN_INFO "%s: IPv6 duplicate address %pI6c detected!\n",
14260522fea6SJens Rosenboom 			ifp->idev->dev->name, &ifp->addr);
14279bdd8d40SBrian Haley 
14281b34be74SYOSHIFUJI Hideaki 	if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
14291b34be74SYOSHIFUJI Hideaki 		struct in6_addr addr;
14301b34be74SYOSHIFUJI Hideaki 
14311b34be74SYOSHIFUJI Hideaki 		addr.s6_addr32[0] = htonl(0xfe800000);
14321b34be74SYOSHIFUJI Hideaki 		addr.s6_addr32[1] = 0;
14331b34be74SYOSHIFUJI Hideaki 
14341b34be74SYOSHIFUJI Hideaki 		if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
14351b34be74SYOSHIFUJI Hideaki 		    ipv6_addr_equal(&ifp->addr, &addr)) {
14361b34be74SYOSHIFUJI Hideaki 			/* DAD failed for link-local based on MAC address */
14371b34be74SYOSHIFUJI Hideaki 			idev->cnf.disable_ipv6 = 1;
14389bdd8d40SBrian Haley 
14399bdd8d40SBrian Haley 			printk(KERN_INFO "%s: IPv6 being disabled!\n",
14409bdd8d40SBrian Haley 				ifp->idev->dev->name);
14411b34be74SYOSHIFUJI Hideaki 		}
14421b34be74SYOSHIFUJI Hideaki 	}
14431b34be74SYOSHIFUJI Hideaki 
1444cc411d0bSBrian Haley 	addrconf_dad_stop(ifp, 1);
14453c21edbdSYOSHIFUJI Hideaki }
14461da177e4SLinus Torvalds 
14471da177e4SLinus Torvalds /* Join to solicited addr multicast group. */
14481da177e4SLinus Torvalds 
1449b71d1d42SEric Dumazet void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
14501da177e4SLinus Torvalds {
14511da177e4SLinus Torvalds 	struct in6_addr maddr;
14521da177e4SLinus Torvalds 
14531da177e4SLinus Torvalds 	if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
14541da177e4SLinus Torvalds 		return;
14551da177e4SLinus Torvalds 
14561da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
14571da177e4SLinus Torvalds 	ipv6_dev_mc_inc(dev, &maddr);
14581da177e4SLinus Torvalds }
14591da177e4SLinus Torvalds 
1460b71d1d42SEric Dumazet void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
14611da177e4SLinus Torvalds {
14621da177e4SLinus Torvalds 	struct in6_addr maddr;
14631da177e4SLinus Torvalds 
14641da177e4SLinus Torvalds 	if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
14651da177e4SLinus Torvalds 		return;
14661da177e4SLinus Torvalds 
14671da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
14681da177e4SLinus Torvalds 	__ipv6_dev_mc_dec(idev, &maddr);
14691da177e4SLinus Torvalds }
14701da177e4SLinus Torvalds 
147120380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
14721da177e4SLinus Torvalds {
14731da177e4SLinus Torvalds 	struct in6_addr addr;
14742bda8a0cSBjørn Mork 	if (ifp->prefix_len == 127) /* RFC 6164 */
14752bda8a0cSBjørn Mork 		return;
14761da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
14771da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
14781da177e4SLinus Torvalds 		return;
14791da177e4SLinus Torvalds 	ipv6_dev_ac_inc(ifp->idev->dev, &addr);
14801da177e4SLinus Torvalds }
14811da177e4SLinus Torvalds 
148220380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
14831da177e4SLinus Torvalds {
14841da177e4SLinus Torvalds 	struct in6_addr addr;
148532019e65SYOSHIFUJI Hideaki 	if (ifp->prefix_len == 127) /* RFC 6164 */
148632019e65SYOSHIFUJI Hideaki 		return;
14871da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
14881da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
14891da177e4SLinus Torvalds 		return;
14901da177e4SLinus Torvalds 	__ipv6_dev_ac_dec(ifp->idev, &addr);
14911da177e4SLinus Torvalds }
14921da177e4SLinus Torvalds 
1493073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
14941da177e4SLinus Torvalds {
14951da177e4SLinus Torvalds 	if (dev->addr_len != ETH_ALEN)
14961da177e4SLinus Torvalds 		return -1;
14971da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr, 3);
14981da177e4SLinus Torvalds 	memcpy(eui + 5, dev->dev_addr + 3, 3);
14991da177e4SLinus Torvalds 
15001da177e4SLinus Torvalds 	/*
15011da177e4SLinus Torvalds 	 * The zSeries OSA network cards can be shared among various
15021da177e4SLinus Torvalds 	 * OS instances, but the OSA cards have only one MAC address.
15031da177e4SLinus Torvalds 	 * This leads to duplicate address conflicts in conjunction
15041da177e4SLinus Torvalds 	 * with IPv6 if more than one instance uses the same card.
15051da177e4SLinus Torvalds 	 *
15061da177e4SLinus Torvalds 	 * The driver for these cards can deliver a unique 16-bit
15071da177e4SLinus Torvalds 	 * identifier for each instance sharing the same card.  It is
15081da177e4SLinus Torvalds 	 * placed instead of 0xFFFE in the interface identifier.  The
15091da177e4SLinus Torvalds 	 * "u" bit of the interface identifier is not inverted in this
15101da177e4SLinus Torvalds 	 * case.  Hence the resulting interface identifier has local
15111da177e4SLinus Torvalds 	 * scope according to RFC2373.
15121da177e4SLinus Torvalds 	 */
15131da177e4SLinus Torvalds 	if (dev->dev_id) {
15141da177e4SLinus Torvalds 		eui[3] = (dev->dev_id >> 8) & 0xFF;
15151da177e4SLinus Torvalds 		eui[4] = dev->dev_id & 0xFF;
15161da177e4SLinus Torvalds 	} else {
15171da177e4SLinus Torvalds 		eui[3] = 0xFF;
15181da177e4SLinus Torvalds 		eui[4] = 0xFE;
15191da177e4SLinus Torvalds 		eui[0] ^= 2;
15201da177e4SLinus Torvalds 	}
15211da177e4SLinus Torvalds 	return 0;
1522073a8e0eSYOSHIFUJI Hideaki }
1523073a8e0eSYOSHIFUJI Hideaki 
1524073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1525073a8e0eSYOSHIFUJI Hideaki {
15261da177e4SLinus Torvalds 	/* XXX: inherit EUI-64 from other interface -- yoshfuji */
15271da177e4SLinus Torvalds 	if (dev->addr_len != ARCNET_ALEN)
15281da177e4SLinus Torvalds 		return -1;
15291da177e4SLinus Torvalds 	memset(eui, 0, 7);
15301da177e4SLinus Torvalds 	eui[7] = *(u8*)dev->dev_addr;
15311da177e4SLinus Torvalds 	return 0;
1532073a8e0eSYOSHIFUJI Hideaki }
1533073a8e0eSYOSHIFUJI Hideaki 
1534073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1535073a8e0eSYOSHIFUJI Hideaki {
15361da177e4SLinus Torvalds 	if (dev->addr_len != INFINIBAND_ALEN)
15371da177e4SLinus Torvalds 		return -1;
15381da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr + 12, 8);
15391da177e4SLinus Torvalds 	eui[0] |= 2;
15401da177e4SLinus Torvalds 	return 0;
15411da177e4SLinus Torvalds }
1542073a8e0eSYOSHIFUJI Hideaki 
1543c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
1544dfd982baSYOSHIFUJI Hideaki {
15459af28511SSascha Hlusiak 	if (addr == 0)
15469af28511SSascha Hlusiak 		return -1;
1547dfd982baSYOSHIFUJI Hideaki 	eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1548dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1549dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1550dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1551dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1552dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1553dfd982baSYOSHIFUJI Hideaki 	eui[1] = 0;
1554dfd982baSYOSHIFUJI Hideaki 	eui[2] = 0x5E;
1555dfd982baSYOSHIFUJI Hideaki 	eui[3] = 0xFE;
1556dfd982baSYOSHIFUJI Hideaki 	memcpy(eui + 4, &addr, 4);
1557dfd982baSYOSHIFUJI Hideaki 	return 0;
1558dfd982baSYOSHIFUJI Hideaki }
1559dfd982baSYOSHIFUJI Hideaki 
1560dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1561dfd982baSYOSHIFUJI Hideaki {
1562dfd982baSYOSHIFUJI Hideaki 	if (dev->priv_flags & IFF_ISATAP)
1563dfd982baSYOSHIFUJI Hideaki 		return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1564dfd982baSYOSHIFUJI Hideaki 	return -1;
1565dfd982baSYOSHIFUJI Hideaki }
1566dfd982baSYOSHIFUJI Hideaki 
1567aee80b54Sstephen hemminger static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
1568aee80b54Sstephen hemminger {
1569aee80b54Sstephen hemminger 	return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1570aee80b54Sstephen hemminger }
1571aee80b54Sstephen hemminger 
1572073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1573073a8e0eSYOSHIFUJI Hideaki {
1574073a8e0eSYOSHIFUJI Hideaki 	switch (dev->type) {
1575073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ETHER:
1576073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_FDDI:
1577073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_IEEE802_TR:
1578073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_eui48(eui, dev);
1579073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ARCNET:
1580073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_arcnet(eui, dev);
1581073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_INFINIBAND:
1582073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_infiniband(eui, dev);
1583c7dc89c0SFred L. Templin 	case ARPHRD_SIT:
1584dfd982baSYOSHIFUJI Hideaki 		return addrconf_ifid_sit(eui, dev);
1585aee80b54Sstephen hemminger 	case ARPHRD_IPGRE:
1586aee80b54Sstephen hemminger 		return addrconf_ifid_gre(eui, dev);
1587073a8e0eSYOSHIFUJI Hideaki 	}
15881da177e4SLinus Torvalds 	return -1;
15891da177e4SLinus Torvalds }
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
15921da177e4SLinus Torvalds {
15931da177e4SLinus Torvalds 	int err = -1;
15941da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
15951da177e4SLinus Torvalds 
15961da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
1597502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
15981da177e4SLinus Torvalds 		if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
15991da177e4SLinus Torvalds 			memcpy(eui, ifp->addr.s6_addr+8, 8);
16001da177e4SLinus Torvalds 			err = 0;
16011da177e4SLinus Torvalds 			break;
16021da177e4SLinus Torvalds 		}
16031da177e4SLinus Torvalds 	}
16041da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
16051da177e4SLinus Torvalds 	return err;
16061da177e4SLinus Torvalds }
16071da177e4SLinus Torvalds 
16081da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
16091da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
16101da177e4SLinus Torvalds static int __ipv6_regen_rndid(struct inet6_dev *idev)
16111da177e4SLinus Torvalds {
16121da177e4SLinus Torvalds regen:
1613955189efSYOSHIFUJI Hideaki 	get_random_bytes(idev->rndid, sizeof(idev->rndid));
16141da177e4SLinus Torvalds 	idev->rndid[0] &= ~0x02;
16151da177e4SLinus Torvalds 
16161da177e4SLinus Torvalds 	/*
16171da177e4SLinus Torvalds 	 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
16181da177e4SLinus Torvalds 	 * check if generated address is not inappropriate
16191da177e4SLinus Torvalds 	 *
16201da177e4SLinus Torvalds 	 *  - Reserved subnet anycast (RFC 2526)
16211da177e4SLinus Torvalds 	 *	11111101 11....11 1xxxxxxx
1622c7dc89c0SFred L. Templin 	 *  - ISATAP (RFC4214) 6.1
16231da177e4SLinus Torvalds 	 *	00-00-5E-FE-xx-xx-xx-xx
16241da177e4SLinus Torvalds 	 *  - value 0
16251da177e4SLinus Torvalds 	 *  - XXX: already assigned to an address on the device
16261da177e4SLinus Torvalds 	 */
16271da177e4SLinus Torvalds 	if (idev->rndid[0] == 0xfd &&
16281da177e4SLinus Torvalds 	    (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
16291da177e4SLinus Torvalds 	    (idev->rndid[7]&0x80))
16301da177e4SLinus Torvalds 		goto regen;
16311da177e4SLinus Torvalds 	if ((idev->rndid[0]|idev->rndid[1]) == 0) {
16321da177e4SLinus Torvalds 		if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
16331da177e4SLinus Torvalds 			goto regen;
16341da177e4SLinus Torvalds 		if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
16351da177e4SLinus Torvalds 			goto regen;
16361da177e4SLinus Torvalds 	}
16371da177e4SLinus Torvalds 
16381da177e4SLinus Torvalds 	return 0;
16391da177e4SLinus Torvalds }
16401da177e4SLinus Torvalds 
16411da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data)
16421da177e4SLinus Torvalds {
16431da177e4SLinus Torvalds 	struct inet6_dev *idev = (struct inet6_dev *) data;
16441da177e4SLinus Torvalds 	unsigned long expires;
16451da177e4SLinus Torvalds 
16468814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
16471da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
16481da177e4SLinus Torvalds 
16491da177e4SLinus Torvalds 	if (idev->dead)
16501da177e4SLinus Torvalds 		goto out;
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds 	if (__ipv6_regen_rndid(idev) < 0)
16531da177e4SLinus Torvalds 		goto out;
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds 	expires = jiffies +
16561da177e4SLinus Torvalds 		idev->cnf.temp_prefered_lft * HZ -
1657784e2710SBen Hutchings 		idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time -
1658784e2710SBen Hutchings 		idev->cnf.max_desync_factor * HZ;
16591da177e4SLinus Torvalds 	if (time_before(expires, jiffies)) {
16601da177e4SLinus Torvalds 		printk(KERN_WARNING
16611da177e4SLinus Torvalds 			"ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
16621da177e4SLinus Torvalds 			idev->dev->name);
16631da177e4SLinus Torvalds 		goto out;
16641da177e4SLinus Torvalds 	}
16651da177e4SLinus Torvalds 
16661da177e4SLinus Torvalds 	if (!mod_timer(&idev->regen_timer, expires))
16671da177e4SLinus Torvalds 		in6_dev_hold(idev);
16681da177e4SLinus Torvalds 
16691da177e4SLinus Torvalds out:
16701da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
16718814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
16721da177e4SLinus Torvalds 	in6_dev_put(idev);
16731da177e4SLinus Torvalds }
16741da177e4SLinus Torvalds 
16751da177e4SLinus Torvalds static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
16761da177e4SLinus Torvalds 	int ret = 0;
16771da177e4SLinus Torvalds 
16781da177e4SLinus Torvalds 	if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
16791da177e4SLinus Torvalds 		ret = __ipv6_regen_rndid(idev);
16801da177e4SLinus Torvalds 	return ret;
16811da177e4SLinus Torvalds }
16821da177e4SLinus Torvalds #endif
16831da177e4SLinus Torvalds 
16841da177e4SLinus Torvalds /*
16851da177e4SLinus Torvalds  *	Add prefix route.
16861da177e4SLinus Torvalds  */
16871da177e4SLinus Torvalds 
16881da177e4SLinus Torvalds static void
16891da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1690e431b8c0SJamal Hadi Salim 		      unsigned long expires, u32 flags)
16911da177e4SLinus Torvalds {
169286872cb5SThomas Graf 	struct fib6_config cfg = {
169386872cb5SThomas Graf 		.fc_table = RT6_TABLE_PREFIX,
169486872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
169586872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
169686872cb5SThomas Graf 		.fc_expires = expires,
169786872cb5SThomas Graf 		.fc_dst_len = plen,
169886872cb5SThomas Graf 		.fc_flags = RTF_UP | flags,
1699c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
1700f410a1fbSStephen Hemminger 		.fc_protocol = RTPROT_KERNEL,
170186872cb5SThomas Graf 	};
17021da177e4SLinus Torvalds 
17034e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *pfx;
17041da177e4SLinus Torvalds 
17051da177e4SLinus Torvalds 	/* Prevent useless cloning on PtP SIT.
17061da177e4SLinus Torvalds 	   This thing is done here expecting that the whole
17071da177e4SLinus Torvalds 	   class of non-broadcast devices need not cloning.
17081da177e4SLinus Torvalds 	 */
17090be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
17101da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
171186872cb5SThomas Graf 		cfg.fc_flags |= RTF_NONEXTHOP;
17120be669bbSJoerg Roedel #endif
17131da177e4SLinus Torvalds 
171486872cb5SThomas Graf 	ip6_route_add(&cfg);
17151da177e4SLinus Torvalds }
17161da177e4SLinus Torvalds 
171714ef37b6SAndreas Hofmeister 
171814ef37b6SAndreas Hofmeister static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
171914ef37b6SAndreas Hofmeister 						  int plen,
172014ef37b6SAndreas Hofmeister 						  const struct net_device *dev,
172114ef37b6SAndreas Hofmeister 						  u32 flags, u32 noflags)
172214ef37b6SAndreas Hofmeister {
172314ef37b6SAndreas Hofmeister 	struct fib6_node *fn;
172414ef37b6SAndreas Hofmeister 	struct rt6_info *rt = NULL;
172514ef37b6SAndreas Hofmeister 	struct fib6_table *table;
172614ef37b6SAndreas Hofmeister 
172714ef37b6SAndreas Hofmeister 	table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX);
172814ef37b6SAndreas Hofmeister 	if (table == NULL)
172914ef37b6SAndreas Hofmeister 		return NULL;
173014ef37b6SAndreas Hofmeister 
173114ef37b6SAndreas Hofmeister 	write_lock_bh(&table->tb6_lock);
173214ef37b6SAndreas Hofmeister 	fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
173314ef37b6SAndreas Hofmeister 	if (!fn)
173414ef37b6SAndreas Hofmeister 		goto out;
173514ef37b6SAndreas Hofmeister 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
173614ef37b6SAndreas Hofmeister 		if (rt->rt6i_dev->ifindex != dev->ifindex)
173714ef37b6SAndreas Hofmeister 			continue;
173814ef37b6SAndreas Hofmeister 		if ((rt->rt6i_flags & flags) != flags)
173914ef37b6SAndreas Hofmeister 			continue;
174014ef37b6SAndreas Hofmeister 		if ((noflags != 0) && ((rt->rt6i_flags & flags) != 0))
174114ef37b6SAndreas Hofmeister 			continue;
174214ef37b6SAndreas Hofmeister 		dst_hold(&rt->dst);
174314ef37b6SAndreas Hofmeister 		break;
174414ef37b6SAndreas Hofmeister 	}
174514ef37b6SAndreas Hofmeister out:
174614ef37b6SAndreas Hofmeister 	write_unlock_bh(&table->tb6_lock);
174714ef37b6SAndreas Hofmeister 	return rt;
174814ef37b6SAndreas Hofmeister }
174914ef37b6SAndreas Hofmeister 
175014ef37b6SAndreas Hofmeister 
17511da177e4SLinus Torvalds /* Create "default" multicast route to the interface */
17521da177e4SLinus Torvalds 
17531da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev)
17541da177e4SLinus Torvalds {
175586872cb5SThomas Graf 	struct fib6_config cfg = {
175686872cb5SThomas Graf 		.fc_table = RT6_TABLE_LOCAL,
175786872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
175886872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
175986872cb5SThomas Graf 		.fc_dst_len = 8,
176086872cb5SThomas Graf 		.fc_flags = RTF_UP,
1761c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
176286872cb5SThomas Graf 	};
17631da177e4SLinus Torvalds 
176486872cb5SThomas Graf 	ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
176586872cb5SThomas Graf 
176686872cb5SThomas Graf 	ip6_route_add(&cfg);
17671da177e4SLinus Torvalds }
17681da177e4SLinus Torvalds 
17690be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
17701da177e4SLinus Torvalds static void sit_route_add(struct net_device *dev)
17711da177e4SLinus Torvalds {
177286872cb5SThomas Graf 	struct fib6_config cfg = {
177386872cb5SThomas Graf 		.fc_table = RT6_TABLE_MAIN,
177486872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
177586872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
177686872cb5SThomas Graf 		.fc_dst_len = 96,
177786872cb5SThomas Graf 		.fc_flags = RTF_UP | RTF_NONEXTHOP,
1778c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
177986872cb5SThomas Graf 	};
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds 	/* prefix length - 96 bits "::d.d.d.d" */
178286872cb5SThomas Graf 	ip6_route_add(&cfg);
17831da177e4SLinus Torvalds }
17840be669bbSJoerg Roedel #endif
17851da177e4SLinus Torvalds 
17861da177e4SLinus Torvalds static void addrconf_add_lroute(struct net_device *dev)
17871da177e4SLinus Torvalds {
17881da177e4SLinus Torvalds 	struct in6_addr addr;
17891da177e4SLinus Torvalds 
17901da177e4SLinus Torvalds 	ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
17911da177e4SLinus Torvalds 	addrconf_prefix_route(&addr, 64, dev, 0, 0);
17921da177e4SLinus Torvalds }
17931da177e4SLinus Torvalds 
17941da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
17951da177e4SLinus Torvalds {
17961da177e4SLinus Torvalds 	struct inet6_dev *idev;
17971da177e4SLinus Torvalds 
17981da177e4SLinus Torvalds 	ASSERT_RTNL();
17991da177e4SLinus Torvalds 
1800e21e8467SStephen Hemminger 	idev = ipv6_find_idev(dev);
1801e21e8467SStephen Hemminger 	if (!idev)
180264e724f6SBrian Haley 		return ERR_PTR(-ENOBUFS);
180364e724f6SBrian Haley 
180464e724f6SBrian Haley 	if (idev->cnf.disable_ipv6)
180564e724f6SBrian Haley 		return ERR_PTR(-EACCES);
18061da177e4SLinus Torvalds 
18071da177e4SLinus Torvalds 	/* Add default multicast route */
18081da177e4SLinus Torvalds 	addrconf_add_mroute(dev);
18091da177e4SLinus Torvalds 
18101da177e4SLinus Torvalds 	/* Add link local route */
18111da177e4SLinus Torvalds 	addrconf_add_lroute(dev);
18121da177e4SLinus Torvalds 	return idev;
18131da177e4SLinus Torvalds }
18141da177e4SLinus Torvalds 
18151da177e4SLinus Torvalds void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
18161da177e4SLinus Torvalds {
18171da177e4SLinus Torvalds 	struct prefix_info *pinfo;
18181da177e4SLinus Torvalds 	__u32 valid_lft;
18191da177e4SLinus Torvalds 	__u32 prefered_lft;
18201da177e4SLinus Torvalds 	int addr_type;
18211da177e4SLinus Torvalds 	struct inet6_dev *in6_dev;
182256d417b1SBrian Haley 	struct net *net = dev_net(dev);
18231da177e4SLinus Torvalds 
18241da177e4SLinus Torvalds 	pinfo = (struct prefix_info *) opt;
18251da177e4SLinus Torvalds 
18261da177e4SLinus Torvalds 	if (len < sizeof(struct prefix_info)) {
18271da177e4SLinus Torvalds 		ADBG(("addrconf: prefix option too short\n"));
18281da177e4SLinus Torvalds 		return;
18291da177e4SLinus Torvalds 	}
18301da177e4SLinus Torvalds 
18311da177e4SLinus Torvalds 	/*
18321da177e4SLinus Torvalds 	 *	Validation checks ([ADDRCONF], page 19)
18331da177e4SLinus Torvalds 	 */
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&pinfo->prefix);
18361da177e4SLinus Torvalds 
18371da177e4SLinus Torvalds 	if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
18381da177e4SLinus Torvalds 		return;
18391da177e4SLinus Torvalds 
18401da177e4SLinus Torvalds 	valid_lft = ntohl(pinfo->valid);
18411da177e4SLinus Torvalds 	prefered_lft = ntohl(pinfo->prefered);
18421da177e4SLinus Torvalds 
18431da177e4SLinus Torvalds 	if (prefered_lft > valid_lft) {
18441da177e4SLinus Torvalds 		if (net_ratelimit())
18451da177e4SLinus Torvalds 			printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
18461da177e4SLinus Torvalds 		return;
18471da177e4SLinus Torvalds 	}
18481da177e4SLinus Torvalds 
18491da177e4SLinus Torvalds 	in6_dev = in6_dev_get(dev);
18501da177e4SLinus Torvalds 
18511da177e4SLinus Torvalds 	if (in6_dev == NULL) {
18521da177e4SLinus Torvalds 		if (net_ratelimit())
18531da177e4SLinus Torvalds 			printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
18541da177e4SLinus Torvalds 		return;
18551da177e4SLinus Torvalds 	}
18561da177e4SLinus Torvalds 
18571da177e4SLinus Torvalds 	/*
18581da177e4SLinus Torvalds 	 *	Two things going on here:
18591da177e4SLinus Torvalds 	 *	1) Add routes for on-link prefixes
18601da177e4SLinus Torvalds 	 *	2) Configure prefixes with the auto flag set
18611da177e4SLinus Torvalds 	 */
18621da177e4SLinus Torvalds 
18634bed72e4SYOSHIFUJI Hideaki 	if (pinfo->onlink) {
18644bed72e4SYOSHIFUJI Hideaki 		struct rt6_info *rt;
18654bed72e4SYOSHIFUJI Hideaki 		unsigned long rt_expires;
18664bed72e4SYOSHIFUJI Hideaki 
18671da177e4SLinus Torvalds 		/* Avoid arithmetic overflow. Really, we could
18686f704992SYOSHIFUJI Hideaki 		 * save rt_expires in seconds, likely valid_lft,
18696f704992SYOSHIFUJI Hideaki 		 * but it would require division in fib gc, that it
18706f704992SYOSHIFUJI Hideaki 		 * not good.
18711da177e4SLinus Torvalds 		 */
18724bed72e4SYOSHIFUJI Hideaki 		if (HZ > USER_HZ)
18734bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
18744bed72e4SYOSHIFUJI Hideaki 		else
18754bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
18763dd4bc68SYOSHIFUJI Hideaki 
18774bed72e4SYOSHIFUJI Hideaki 		if (addrconf_finite_timeout(rt_expires))
18784bed72e4SYOSHIFUJI Hideaki 			rt_expires *= HZ;
18791da177e4SLinus Torvalds 
188014ef37b6SAndreas Hofmeister 		rt = addrconf_get_prefix_route(&pinfo->prefix,
188114ef37b6SAndreas Hofmeister 					       pinfo->prefix_len,
188214ef37b6SAndreas Hofmeister 					       dev,
188314ef37b6SAndreas Hofmeister 					       RTF_ADDRCONF | RTF_PREFIX_RT,
188414ef37b6SAndreas Hofmeister 					       RTF_GATEWAY | RTF_DEFAULT);
18851da177e4SLinus Torvalds 
188614ef37b6SAndreas Hofmeister 		if (rt) {
18876f704992SYOSHIFUJI Hideaki 			/* Autoconf prefix route */
18881da177e4SLinus Torvalds 			if (valid_lft == 0) {
1889e0a1ad73SThomas Graf 				ip6_del_rt(rt);
18901da177e4SLinus Torvalds 				rt = NULL;
18914bed72e4SYOSHIFUJI Hideaki 			} else if (addrconf_finite_timeout(rt_expires)) {
18926f704992SYOSHIFUJI Hideaki 				/* not infinity */
18933dd4bc68SYOSHIFUJI Hideaki 				rt->rt6i_expires = jiffies + rt_expires;
18946f704992SYOSHIFUJI Hideaki 				rt->rt6i_flags |= RTF_EXPIRES;
18956f704992SYOSHIFUJI Hideaki 			} else {
18966f704992SYOSHIFUJI Hideaki 				rt->rt6i_flags &= ~RTF_EXPIRES;
18976f704992SYOSHIFUJI Hideaki 				rt->rt6i_expires = 0;
18981da177e4SLinus Torvalds 			}
18991da177e4SLinus Torvalds 		} else if (valid_lft) {
19006f704992SYOSHIFUJI Hideaki 			clock_t expires = 0;
19014bed72e4SYOSHIFUJI Hideaki 			int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
19024bed72e4SYOSHIFUJI Hideaki 			if (addrconf_finite_timeout(rt_expires)) {
19036f704992SYOSHIFUJI Hideaki 				/* not infinity */
19046f704992SYOSHIFUJI Hideaki 				flags |= RTF_EXPIRES;
19056f704992SYOSHIFUJI Hideaki 				expires = jiffies_to_clock_t(rt_expires);
19066f704992SYOSHIFUJI Hideaki 			}
19071da177e4SLinus Torvalds 			addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
19086f704992SYOSHIFUJI Hideaki 					      dev, expires, flags);
19091da177e4SLinus Torvalds 		}
19101da177e4SLinus Torvalds 		if (rt)
1911d8d1f30bSChangli Gao 			dst_release(&rt->dst);
19121da177e4SLinus Torvalds 	}
19131da177e4SLinus Torvalds 
19141da177e4SLinus Torvalds 	/* Try to figure out our local address for this prefix */
19151da177e4SLinus Torvalds 
19161da177e4SLinus Torvalds 	if (pinfo->autoconf && in6_dev->cnf.autoconf) {
19171da177e4SLinus Torvalds 		struct inet6_ifaddr * ifp;
19181da177e4SLinus Torvalds 		struct in6_addr addr;
19191da177e4SLinus Torvalds 		int create = 0, update_lft = 0;
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds 		if (pinfo->prefix_len == 64) {
19221da177e4SLinus Torvalds 			memcpy(&addr, &pinfo->prefix, 8);
19231da177e4SLinus Torvalds 			if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
19241da177e4SLinus Torvalds 			    ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
19251da177e4SLinus Torvalds 				in6_dev_put(in6_dev);
19261da177e4SLinus Torvalds 				return;
19271da177e4SLinus Torvalds 			}
19281da177e4SLinus Torvalds 			goto ok;
19291da177e4SLinus Torvalds 		}
19301da177e4SLinus Torvalds 		if (net_ratelimit())
19311da177e4SLinus Torvalds 			printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
19321da177e4SLinus Torvalds 			       pinfo->prefix_len);
19331da177e4SLinus Torvalds 		in6_dev_put(in6_dev);
19341da177e4SLinus Torvalds 		return;
19351da177e4SLinus Torvalds 
19361da177e4SLinus Torvalds ok:
19371da177e4SLinus Torvalds 
193853b7997fSYOSHIFUJI Hideaki 		ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
19391da177e4SLinus Torvalds 
19401da177e4SLinus Torvalds 		if (ifp == NULL && valid_lft) {
19411da177e4SLinus Torvalds 			int max_addresses = in6_dev->cnf.max_addresses;
194295c385b4SNeil Horman 			u32 addr_flags = 0;
194395c385b4SNeil Horman 
194495c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
194595c385b4SNeil Horman 			if (in6_dev->cnf.optimistic_dad &&
194653b7997fSYOSHIFUJI Hideaki 			    !net->ipv6.devconf_all->forwarding)
194795c385b4SNeil Horman 				addr_flags = IFA_F_OPTIMISTIC;
194895c385b4SNeil Horman #endif
19491da177e4SLinus Torvalds 
19501da177e4SLinus Torvalds 			/* Do not allow to create too much of autoconfigured
19511da177e4SLinus Torvalds 			 * addresses; this would be too easy way to crash kernel.
19521da177e4SLinus Torvalds 			 */
19531da177e4SLinus Torvalds 			if (!max_addresses ||
19541da177e4SLinus Torvalds 			    ipv6_count_addresses(in6_dev) < max_addresses)
19551da177e4SLinus Torvalds 				ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
195695c385b4SNeil Horman 						    addr_type&IPV6_ADDR_SCOPE_MASK,
195795c385b4SNeil Horman 						    addr_flags);
19581da177e4SLinus Torvalds 
19591da177e4SLinus Torvalds 			if (!ifp || IS_ERR(ifp)) {
19601da177e4SLinus Torvalds 				in6_dev_put(in6_dev);
19611da177e4SLinus Torvalds 				return;
19621da177e4SLinus Torvalds 			}
19631da177e4SLinus Torvalds 
19641da177e4SLinus Torvalds 			update_lft = create = 1;
19651da177e4SLinus Torvalds 			ifp->cstamp = jiffies;
19661da177e4SLinus Torvalds 			addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
19671da177e4SLinus Torvalds 		}
19681da177e4SLinus Torvalds 
19691da177e4SLinus Torvalds 		if (ifp) {
19701da177e4SLinus Torvalds 			int flags;
19711da177e4SLinus Torvalds 			unsigned long now;
19721da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
19731da177e4SLinus Torvalds 			struct inet6_ifaddr *ift;
19741da177e4SLinus Torvalds #endif
19751da177e4SLinus Torvalds 			u32 stored_lft;
19761da177e4SLinus Torvalds 
19771da177e4SLinus Torvalds 			/* update lifetime (RFC2462 5.5.3 e) */
19781da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
19791da177e4SLinus Torvalds 			now = jiffies;
19801da177e4SLinus Torvalds 			if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
19811da177e4SLinus Torvalds 				stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
19821da177e4SLinus Torvalds 			else
19831da177e4SLinus Torvalds 				stored_lft = 0;
19841da177e4SLinus Torvalds 			if (!update_lft && stored_lft) {
19851da177e4SLinus Torvalds 				if (valid_lft > MIN_VALID_LIFETIME ||
19861da177e4SLinus Torvalds 				    valid_lft > stored_lft)
19871da177e4SLinus Torvalds 					update_lft = 1;
19881da177e4SLinus Torvalds 				else if (stored_lft <= MIN_VALID_LIFETIME) {
19891da177e4SLinus Torvalds 					/* valid_lft <= stored_lft is always true */
1990a1ed0526SBrian Haley 					/*
1991a1ed0526SBrian Haley 					 * RFC 4862 Section 5.5.3e:
1992a1ed0526SBrian Haley 					 * "Note that the preferred lifetime of
1993a1ed0526SBrian Haley 					 *  the corresponding address is always
1994a1ed0526SBrian Haley 					 *  reset to the Preferred Lifetime in
1995a1ed0526SBrian Haley 					 *  the received Prefix Information
1996a1ed0526SBrian Haley 					 *  option, regardless of whether the
1997a1ed0526SBrian Haley 					 *  valid lifetime is also reset or
1998a1ed0526SBrian Haley 					 *  ignored."
1999a1ed0526SBrian Haley 					 *
2000a1ed0526SBrian Haley 					 *  So if the preferred lifetime in
2001a1ed0526SBrian Haley 					 *  this advertisement is different
2002a1ed0526SBrian Haley 					 *  than what we have stored, but the
2003a1ed0526SBrian Haley 					 *  valid lifetime is invalid, just
2004a1ed0526SBrian Haley 					 *  reset prefered_lft.
2005a1ed0526SBrian Haley 					 *
2006a1ed0526SBrian Haley 					 *  We must set the valid lifetime
2007a1ed0526SBrian Haley 					 *  to the stored lifetime since we'll
2008a1ed0526SBrian Haley 					 *  be updating the timestamp below,
2009a1ed0526SBrian Haley 					 *  else we'll set it back to the
201025985edcSLucas De Marchi 					 *  minimum.
2011a1ed0526SBrian Haley 					 */
2012a1ed0526SBrian Haley 					if (prefered_lft != ifp->prefered_lft) {
2013a1ed0526SBrian Haley 						valid_lft = stored_lft;
2014a1ed0526SBrian Haley 						update_lft = 1;
2015a1ed0526SBrian Haley 					}
20161da177e4SLinus Torvalds 				} else {
20171da177e4SLinus Torvalds 					valid_lft = MIN_VALID_LIFETIME;
20181da177e4SLinus Torvalds 					if (valid_lft < prefered_lft)
20191da177e4SLinus Torvalds 						prefered_lft = valid_lft;
20201da177e4SLinus Torvalds 					update_lft = 1;
20211da177e4SLinus Torvalds 				}
20221da177e4SLinus Torvalds 			}
20231da177e4SLinus Torvalds 
20241da177e4SLinus Torvalds 			if (update_lft) {
20251da177e4SLinus Torvalds 				ifp->valid_lft = valid_lft;
20261da177e4SLinus Torvalds 				ifp->prefered_lft = prefered_lft;
20271da177e4SLinus Torvalds 				ifp->tstamp = now;
20281da177e4SLinus Torvalds 				flags = ifp->flags;
20291da177e4SLinus Torvalds 				ifp->flags &= ~IFA_F_DEPRECATED;
20301da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
20311da177e4SLinus Torvalds 
20321da177e4SLinus Torvalds 				if (!(flags&IFA_F_TENTATIVE))
20331da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
20341da177e4SLinus Torvalds 			} else
20351da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
20361da177e4SLinus Torvalds 
20371da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
20381da177e4SLinus Torvalds 			read_lock_bh(&in6_dev->lock);
20391da177e4SLinus Torvalds 			/* update all temporary addresses in the list */
204076f793e3SLorenzo Colitti 			list_for_each_entry(ift, &in6_dev->tempaddr_list,
204176f793e3SLorenzo Colitti 					    tmp_list) {
204276f793e3SLorenzo Colitti 				int age, max_valid, max_prefered;
204376f793e3SLorenzo Colitti 
2044c6fbfac2SBenoit Boissinot 				if (ifp != ift->ifpub)
2045c6fbfac2SBenoit Boissinot 					continue;
2046c6fbfac2SBenoit Boissinot 
204776f793e3SLorenzo Colitti 				/*
204876f793e3SLorenzo Colitti 				 * RFC 4941 section 3.3:
204976f793e3SLorenzo Colitti 				 * If a received option will extend the lifetime
205076f793e3SLorenzo Colitti 				 * of a public address, the lifetimes of
205176f793e3SLorenzo Colitti 				 * temporary addresses should be extended,
205276f793e3SLorenzo Colitti 				 * subject to the overall constraint that no
205376f793e3SLorenzo Colitti 				 * temporary addresses should ever remain
205476f793e3SLorenzo Colitti 				 * "valid" or "preferred" for a time longer than
205576f793e3SLorenzo Colitti 				 * (TEMP_VALID_LIFETIME) or
205676f793e3SLorenzo Colitti 				 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR),
205776f793e3SLorenzo Colitti 				 * respectively.
205876f793e3SLorenzo Colitti 				 */
205976f793e3SLorenzo Colitti 				age = (now - ift->cstamp) / HZ;
206076f793e3SLorenzo Colitti 				max_valid = in6_dev->cnf.temp_valid_lft - age;
206176f793e3SLorenzo Colitti 				if (max_valid < 0)
206276f793e3SLorenzo Colitti 					max_valid = 0;
206376f793e3SLorenzo Colitti 
206476f793e3SLorenzo Colitti 				max_prefered = in6_dev->cnf.temp_prefered_lft -
206576f793e3SLorenzo Colitti 					       in6_dev->cnf.max_desync_factor -
206676f793e3SLorenzo Colitti 					       age;
206776f793e3SLorenzo Colitti 				if (max_prefered < 0)
206876f793e3SLorenzo Colitti 					max_prefered = 0;
206976f793e3SLorenzo Colitti 
207076f793e3SLorenzo Colitti 				if (valid_lft > max_valid)
207176f793e3SLorenzo Colitti 					valid_lft = max_valid;
207276f793e3SLorenzo Colitti 
207376f793e3SLorenzo Colitti 				if (prefered_lft > max_prefered)
207476f793e3SLorenzo Colitti 					prefered_lft = max_prefered;
207576f793e3SLorenzo Colitti 
20761da177e4SLinus Torvalds 				spin_lock(&ift->lock);
20771da177e4SLinus Torvalds 				flags = ift->flags;
207876f793e3SLorenzo Colitti 				ift->valid_lft = valid_lft;
207976f793e3SLorenzo Colitti 				ift->prefered_lft = prefered_lft;
208076f793e3SLorenzo Colitti 				ift->tstamp = now;
208176f793e3SLorenzo Colitti 				if (prefered_lft > 0)
208276f793e3SLorenzo Colitti 					ift->flags &= ~IFA_F_DEPRECATED;
208376f793e3SLorenzo Colitti 
20841da177e4SLinus Torvalds 				spin_unlock(&ift->lock);
20851da177e4SLinus Torvalds 				if (!(flags&IFA_F_TENTATIVE))
20861da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ift);
20871da177e4SLinus Torvalds 			}
20881da177e4SLinus Torvalds 
2089aed65501SGlenn Wurster 			if ((create || list_empty(&in6_dev->tempaddr_list)) && in6_dev->cnf.use_tempaddr > 0) {
20901da177e4SLinus Torvalds 				/*
209176f793e3SLorenzo Colitti 				 * When a new public address is created as
209276f793e3SLorenzo Colitti 				 * described in [ADDRCONF], also create a new
209376f793e3SLorenzo Colitti 				 * temporary address. Also create a temporary
209476f793e3SLorenzo Colitti 				 * address if it's enabled but no temporary
209576f793e3SLorenzo Colitti 				 * address currently exists.
20961da177e4SLinus Torvalds 				 */
20971da177e4SLinus Torvalds 				read_unlock_bh(&in6_dev->lock);
20981da177e4SLinus Torvalds 				ipv6_create_tempaddr(ifp, NULL);
20991da177e4SLinus Torvalds 			} else {
21001da177e4SLinus Torvalds 				read_unlock_bh(&in6_dev->lock);
21011da177e4SLinus Torvalds 			}
21021da177e4SLinus Torvalds #endif
21031da177e4SLinus Torvalds 			in6_ifa_put(ifp);
21041da177e4SLinus Torvalds 			addrconf_verify(0);
21051da177e4SLinus Torvalds 		}
21061da177e4SLinus Torvalds 	}
21071da177e4SLinus Torvalds 	inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
21081da177e4SLinus Torvalds 	in6_dev_put(in6_dev);
21091da177e4SLinus Torvalds }
21101da177e4SLinus Torvalds 
21111da177e4SLinus Torvalds /*
21121da177e4SLinus Torvalds  *	Set destination address.
21131da177e4SLinus Torvalds  *	Special case for SIT interfaces where we create a new "virtual"
21141da177e4SLinus Torvalds  *	device.
21151da177e4SLinus Torvalds  */
2116af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg)
21171da177e4SLinus Torvalds {
21181da177e4SLinus Torvalds 	struct in6_ifreq ireq;
21191da177e4SLinus Torvalds 	struct net_device *dev;
21201da177e4SLinus Torvalds 	int err = -EINVAL;
21211da177e4SLinus Torvalds 
21221da177e4SLinus Torvalds 	rtnl_lock();
21231da177e4SLinus Torvalds 
21241da177e4SLinus Torvalds 	err = -EFAULT;
21251da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
21261da177e4SLinus Torvalds 		goto err_exit;
21271da177e4SLinus Torvalds 
2128af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
21291da177e4SLinus Torvalds 
21301da177e4SLinus Torvalds 	err = -ENODEV;
21311da177e4SLinus Torvalds 	if (dev == NULL)
21321da177e4SLinus Torvalds 		goto err_exit;
21331da177e4SLinus Torvalds 
21340be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
21351da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT) {
21365bc3eb7eSStephen Hemminger 		const struct net_device_ops *ops = dev->netdev_ops;
21371da177e4SLinus Torvalds 		struct ifreq ifr;
21381da177e4SLinus Torvalds 		struct ip_tunnel_parm p;
21391da177e4SLinus Torvalds 
21401da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
21411da177e4SLinus Torvalds 		if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
21421da177e4SLinus Torvalds 			goto err_exit;
21431da177e4SLinus Torvalds 
21441da177e4SLinus Torvalds 		memset(&p, 0, sizeof(p));
21451da177e4SLinus Torvalds 		p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
21461da177e4SLinus Torvalds 		p.iph.saddr = 0;
21471da177e4SLinus Torvalds 		p.iph.version = 4;
21481da177e4SLinus Torvalds 		p.iph.ihl = 5;
21491da177e4SLinus Torvalds 		p.iph.protocol = IPPROTO_IPV6;
21501da177e4SLinus Torvalds 		p.iph.ttl = 64;
2151d20b3109SStephen Hemminger 		ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
21521da177e4SLinus Torvalds 
21535bc3eb7eSStephen Hemminger 		if (ops->ndo_do_ioctl) {
21545bc3eb7eSStephen Hemminger 			mm_segment_t oldfs = get_fs();
21555bc3eb7eSStephen Hemminger 
21565bc3eb7eSStephen Hemminger 			set_fs(KERNEL_DS);
21575bc3eb7eSStephen Hemminger 			err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
21581da177e4SLinus Torvalds 			set_fs(oldfs);
21595bc3eb7eSStephen Hemminger 		} else
21605bc3eb7eSStephen Hemminger 			err = -EOPNOTSUPP;
21611da177e4SLinus Torvalds 
21621da177e4SLinus Torvalds 		if (err == 0) {
21631da177e4SLinus Torvalds 			err = -ENOBUFS;
2164af284937SDaniel Lezcano 			dev = __dev_get_by_name(net, p.name);
2165af284937SDaniel Lezcano 			if (!dev)
21661da177e4SLinus Torvalds 				goto err_exit;
21671da177e4SLinus Torvalds 			err = dev_open(dev);
21681da177e4SLinus Torvalds 		}
21691da177e4SLinus Torvalds 	}
21700be669bbSJoerg Roedel #endif
21711da177e4SLinus Torvalds 
21721da177e4SLinus Torvalds err_exit:
21731da177e4SLinus Torvalds 	rtnl_unlock();
21741da177e4SLinus Torvalds 	return err;
21751da177e4SLinus Torvalds }
21761da177e4SLinus Torvalds 
21771da177e4SLinus Torvalds /*
21781da177e4SLinus Torvalds  *	Manual configuration of address on an interface
21791da177e4SLinus Torvalds  */
2180b71d1d42SEric Dumazet static int inet6_addr_add(struct net *net, int ifindex, const struct in6_addr *pfx,
218124ef0da7SThomas Graf 			  unsigned int plen, __u8 ifa_flags, __u32 prefered_lft,
2182af284937SDaniel Lezcano 			  __u32 valid_lft)
21831da177e4SLinus Torvalds {
21841da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
21851da177e4SLinus Torvalds 	struct inet6_dev *idev;
21861da177e4SLinus Torvalds 	struct net_device *dev;
21871da177e4SLinus Torvalds 	int scope;
21886f704992SYOSHIFUJI Hideaki 	u32 flags;
21896f704992SYOSHIFUJI Hideaki 	clock_t expires;
21904bed72e4SYOSHIFUJI Hideaki 	unsigned long timeout;
21911da177e4SLinus Torvalds 
21921da177e4SLinus Torvalds 	ASSERT_RTNL();
21931da177e4SLinus Torvalds 
219424ef0da7SThomas Graf 	if (plen > 128)
219524ef0da7SThomas Graf 		return -EINVAL;
219624ef0da7SThomas Graf 
21970778769dSNoriaki TAKAMIYA 	/* check the lifetime */
21980778769dSNoriaki TAKAMIYA 	if (!valid_lft || prefered_lft > valid_lft)
21990778769dSNoriaki TAKAMIYA 		return -EINVAL;
22000778769dSNoriaki TAKAMIYA 
2201af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2202af284937SDaniel Lezcano 	if (!dev)
22031da177e4SLinus Torvalds 		return -ENODEV;
22041da177e4SLinus Torvalds 
220564e724f6SBrian Haley 	idev = addrconf_add_dev(dev);
220664e724f6SBrian Haley 	if (IS_ERR(idev))
220764e724f6SBrian Haley 		return PTR_ERR(idev);
22081da177e4SLinus Torvalds 
22091da177e4SLinus Torvalds 	scope = ipv6_addr_scope(pfx);
22101da177e4SLinus Torvalds 
22114bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
22124bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
22134bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
22144bed72e4SYOSHIFUJI Hideaki 		valid_lft = timeout;
22156f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
22164bed72e4SYOSHIFUJI Hideaki 	} else {
22174bed72e4SYOSHIFUJI Hideaki 		expires = 0;
22184bed72e4SYOSHIFUJI Hideaki 		flags = 0;
22194bed72e4SYOSHIFUJI Hideaki 		ifa_flags |= IFA_F_PERMANENT;
22206f704992SYOSHIFUJI Hideaki 	}
22210778769dSNoriaki TAKAMIYA 
22224bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
22234bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
22244bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
22250778769dSNoriaki TAKAMIYA 			ifa_flags |= IFA_F_DEPRECATED;
22264bed72e4SYOSHIFUJI Hideaki 		prefered_lft = timeout;
22274bed72e4SYOSHIFUJI Hideaki 	}
22280778769dSNoriaki TAKAMIYA 
22290778769dSNoriaki TAKAMIYA 	ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
22300778769dSNoriaki TAKAMIYA 
22311da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
223206aebfb7SHerbert Xu 		spin_lock_bh(&ifp->lock);
22330778769dSNoriaki TAKAMIYA 		ifp->valid_lft = valid_lft;
22340778769dSNoriaki TAKAMIYA 		ifp->prefered_lft = prefered_lft;
22350778769dSNoriaki TAKAMIYA 		ifp->tstamp = jiffies;
223606aebfb7SHerbert Xu 		spin_unlock_bh(&ifp->lock);
22370778769dSNoriaki TAKAMIYA 
223846d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
22396f704992SYOSHIFUJI Hideaki 				      expires, flags);
224095c385b4SNeil Horman 		/*
224195c385b4SNeil Horman 		 * Note that section 3.1 of RFC 4429 indicates
224295c385b4SNeil Horman 		 * that the Optimistic flag should not be set for
224395c385b4SNeil Horman 		 * manually configured addresses
224495c385b4SNeil Horman 		 */
22451da177e4SLinus Torvalds 		addrconf_dad_start(ifp, 0);
22461da177e4SLinus Torvalds 		in6_ifa_put(ifp);
22470778769dSNoriaki TAKAMIYA 		addrconf_verify(0);
22481da177e4SLinus Torvalds 		return 0;
22491da177e4SLinus Torvalds 	}
22501da177e4SLinus Torvalds 
22511da177e4SLinus Torvalds 	return PTR_ERR(ifp);
22521da177e4SLinus Torvalds }
22531da177e4SLinus Torvalds 
2254b71d1d42SEric Dumazet static int inet6_addr_del(struct net *net, int ifindex, const struct in6_addr *pfx,
225524ef0da7SThomas Graf 			  unsigned int plen)
22561da177e4SLinus Torvalds {
22571da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
22581da177e4SLinus Torvalds 	struct inet6_dev *idev;
22591da177e4SLinus Torvalds 	struct net_device *dev;
22601da177e4SLinus Torvalds 
226124ef0da7SThomas Graf 	if (plen > 128)
226224ef0da7SThomas Graf 		return -EINVAL;
226324ef0da7SThomas Graf 
2264af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2265af284937SDaniel Lezcano 	if (!dev)
22661da177e4SLinus Torvalds 		return -ENODEV;
22671da177e4SLinus Torvalds 
22681da177e4SLinus Torvalds 	if ((idev = __in6_dev_get(dev)) == NULL)
22691da177e4SLinus Torvalds 		return -ENXIO;
22701da177e4SLinus Torvalds 
22711da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
2272502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
22731da177e4SLinus Torvalds 		if (ifp->prefix_len == plen &&
22741da177e4SLinus Torvalds 		    ipv6_addr_equal(pfx, &ifp->addr)) {
22751da177e4SLinus Torvalds 			in6_ifa_hold(ifp);
22761da177e4SLinus Torvalds 			read_unlock_bh(&idev->lock);
22771da177e4SLinus Torvalds 
22781da177e4SLinus Torvalds 			ipv6_del_addr(ifp);
22791da177e4SLinus Torvalds 
22801da177e4SLinus Torvalds 			/* If the last address is deleted administratively,
22811da177e4SLinus Torvalds 			   disable IPv6 on this interface.
22821da177e4SLinus Torvalds 			 */
2283502a2ffdSstephen hemminger 			if (list_empty(&idev->addr_list))
22841da177e4SLinus Torvalds 				addrconf_ifdown(idev->dev, 1);
22851da177e4SLinus Torvalds 			return 0;
22861da177e4SLinus Torvalds 		}
22871da177e4SLinus Torvalds 	}
22881da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
22891da177e4SLinus Torvalds 	return -EADDRNOTAVAIL;
22901da177e4SLinus Torvalds }
22911da177e4SLinus Torvalds 
22921da177e4SLinus Torvalds 
2293af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg)
22941da177e4SLinus Torvalds {
22951da177e4SLinus Torvalds 	struct in6_ifreq ireq;
22961da177e4SLinus Torvalds 	int err;
22971da177e4SLinus Torvalds 
22981da177e4SLinus Torvalds 	if (!capable(CAP_NET_ADMIN))
22991da177e4SLinus Torvalds 		return -EPERM;
23001da177e4SLinus Torvalds 
23011da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
23021da177e4SLinus Torvalds 		return -EFAULT;
23031da177e4SLinus Torvalds 
23041da177e4SLinus Torvalds 	rtnl_lock();
2305af284937SDaniel Lezcano 	err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2306af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2307af284937SDaniel Lezcano 			     INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
23081da177e4SLinus Torvalds 	rtnl_unlock();
23091da177e4SLinus Torvalds 	return err;
23101da177e4SLinus Torvalds }
23111da177e4SLinus Torvalds 
2312af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg)
23131da177e4SLinus Torvalds {
23141da177e4SLinus Torvalds 	struct in6_ifreq ireq;
23151da177e4SLinus Torvalds 	int err;
23161da177e4SLinus Torvalds 
23171da177e4SLinus Torvalds 	if (!capable(CAP_NET_ADMIN))
23181da177e4SLinus Torvalds 		return -EPERM;
23191da177e4SLinus Torvalds 
23201da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
23211da177e4SLinus Torvalds 		return -EFAULT;
23221da177e4SLinus Torvalds 
23231da177e4SLinus Torvalds 	rtnl_lock();
2324af284937SDaniel Lezcano 	err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2325af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen);
23261da177e4SLinus Torvalds 	rtnl_unlock();
23271da177e4SLinus Torvalds 	return err;
23281da177e4SLinus Torvalds }
23291da177e4SLinus Torvalds 
2330b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2331b5f348e5SIlpo Järvinen 		     int plen, int scope)
2332b5f348e5SIlpo Järvinen {
2333b5f348e5SIlpo Järvinen 	struct inet6_ifaddr *ifp;
2334b5f348e5SIlpo Järvinen 
2335b5f348e5SIlpo Järvinen 	ifp = ipv6_add_addr(idev, addr, plen, scope, IFA_F_PERMANENT);
2336b5f348e5SIlpo Järvinen 	if (!IS_ERR(ifp)) {
2337b5f348e5SIlpo Järvinen 		spin_lock_bh(&ifp->lock);
2338b5f348e5SIlpo Järvinen 		ifp->flags &= ~IFA_F_TENTATIVE;
2339b5f348e5SIlpo Järvinen 		spin_unlock_bh(&ifp->lock);
2340b5f348e5SIlpo Järvinen 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
2341b5f348e5SIlpo Järvinen 		in6_ifa_put(ifp);
2342b5f348e5SIlpo Järvinen 	}
2343b5f348e5SIlpo Järvinen }
2344b5f348e5SIlpo Järvinen 
23450be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
23461da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev)
23471da177e4SLinus Torvalds {
23481da177e4SLinus Torvalds 	struct in6_addr addr;
23491da177e4SLinus Torvalds 	struct net_device *dev;
2350c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
23511da177e4SLinus Torvalds 	int scope;
23521da177e4SLinus Torvalds 
23531da177e4SLinus Torvalds 	ASSERT_RTNL();
23541da177e4SLinus Torvalds 
23551da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
23561da177e4SLinus Torvalds 	memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
23571da177e4SLinus Torvalds 
23581da177e4SLinus Torvalds 	if (idev->dev->flags&IFF_POINTOPOINT) {
23591da177e4SLinus Torvalds 		addr.s6_addr32[0] = htonl(0xfe800000);
23601da177e4SLinus Torvalds 		scope = IFA_LINK;
23611da177e4SLinus Torvalds 	} else {
23621da177e4SLinus Torvalds 		scope = IPV6_ADDR_COMPATv4;
23631da177e4SLinus Torvalds 	}
23641da177e4SLinus Torvalds 
23651da177e4SLinus Torvalds 	if (addr.s6_addr32[3]) {
2366b5f348e5SIlpo Järvinen 		add_addr(idev, &addr, 128, scope);
23671da177e4SLinus Torvalds 		return;
23681da177e4SLinus Torvalds 	}
23691da177e4SLinus Torvalds 
23706fda7350SBenjamin Thery 	for_each_netdev(net, dev) {
2371e5ed6399SHerbert Xu 		struct in_device * in_dev = __in_dev_get_rtnl(dev);
23721da177e4SLinus Torvalds 		if (in_dev && (dev->flags & IFF_UP)) {
23731da177e4SLinus Torvalds 			struct in_ifaddr * ifa;
23741da177e4SLinus Torvalds 
23751da177e4SLinus Torvalds 			int flag = scope;
23761da177e4SLinus Torvalds 
23771da177e4SLinus Torvalds 			for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
23781da177e4SLinus Torvalds 				int plen;
23791da177e4SLinus Torvalds 
23801da177e4SLinus Torvalds 				addr.s6_addr32[3] = ifa->ifa_local;
23811da177e4SLinus Torvalds 
23821da177e4SLinus Torvalds 				if (ifa->ifa_scope == RT_SCOPE_LINK)
23831da177e4SLinus Torvalds 					continue;
23841da177e4SLinus Torvalds 				if (ifa->ifa_scope >= RT_SCOPE_HOST) {
23851da177e4SLinus Torvalds 					if (idev->dev->flags&IFF_POINTOPOINT)
23861da177e4SLinus Torvalds 						continue;
23871da177e4SLinus Torvalds 					flag |= IFA_HOST;
23881da177e4SLinus Torvalds 				}
23891da177e4SLinus Torvalds 				if (idev->dev->flags&IFF_POINTOPOINT)
23901da177e4SLinus Torvalds 					plen = 64;
23911da177e4SLinus Torvalds 				else
23921da177e4SLinus Torvalds 					plen = 96;
23931da177e4SLinus Torvalds 
2394b5f348e5SIlpo Järvinen 				add_addr(idev, &addr, plen, flag);
23951da177e4SLinus Torvalds 			}
23961da177e4SLinus Torvalds 		}
23971da177e4SLinus Torvalds 	}
23981da177e4SLinus Torvalds }
23990be669bbSJoerg Roedel #endif
24001da177e4SLinus Torvalds 
24011da177e4SLinus Torvalds static void init_loopback(struct net_device *dev)
24021da177e4SLinus Torvalds {
24031da177e4SLinus Torvalds 	struct inet6_dev  *idev;
24041da177e4SLinus Torvalds 
24051da177e4SLinus Torvalds 	/* ::1 */
24061da177e4SLinus Torvalds 
24071da177e4SLinus Torvalds 	ASSERT_RTNL();
24081da177e4SLinus Torvalds 
24091da177e4SLinus Torvalds 	if ((idev = ipv6_find_idev(dev)) == NULL) {
24101da177e4SLinus Torvalds 		printk(KERN_DEBUG "init loopback: add_dev failed\n");
24111da177e4SLinus Torvalds 		return;
24121da177e4SLinus Torvalds 	}
24131da177e4SLinus Torvalds 
2414b5f348e5SIlpo Järvinen 	add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
24151da177e4SLinus Torvalds }
24161da177e4SLinus Torvalds 
2417b71d1d42SEric Dumazet static void addrconf_add_linklocal(struct inet6_dev *idev, const struct in6_addr *addr)
24181da177e4SLinus Torvalds {
24191da177e4SLinus Torvalds 	struct inet6_ifaddr * ifp;
242095c385b4SNeil Horman 	u32 addr_flags = IFA_F_PERMANENT;
24211da177e4SLinus Torvalds 
242295c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
242395c385b4SNeil Horman 	if (idev->cnf.optimistic_dad &&
2424702beb87SDavid Miller 	    !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
242595c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
242695c385b4SNeil Horman #endif
242795c385b4SNeil Horman 
242895c385b4SNeil Horman 
242995c385b4SNeil Horman 	ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags);
24301da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
243146d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
24321da177e4SLinus Torvalds 		addrconf_dad_start(ifp, 0);
24331da177e4SLinus Torvalds 		in6_ifa_put(ifp);
24341da177e4SLinus Torvalds 	}
24351da177e4SLinus Torvalds }
24361da177e4SLinus Torvalds 
24371da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev)
24381da177e4SLinus Torvalds {
24391da177e4SLinus Torvalds 	struct in6_addr addr;
24401da177e4SLinus Torvalds 	struct inet6_dev    * idev;
24411da177e4SLinus Torvalds 
24421da177e4SLinus Torvalds 	ASSERT_RTNL();
24431da177e4SLinus Torvalds 
244474235a25SHerbert Xu 	if ((dev->type != ARPHRD_ETHER) &&
244574235a25SHerbert Xu 	    (dev->type != ARPHRD_FDDI) &&
244674235a25SHerbert Xu 	    (dev->type != ARPHRD_IEEE802_TR) &&
244774235a25SHerbert Xu 	    (dev->type != ARPHRD_ARCNET) &&
244874235a25SHerbert Xu 	    (dev->type != ARPHRD_INFINIBAND)) {
244974235a25SHerbert Xu 		/* Alas, we support only Ethernet autoconfiguration. */
245074235a25SHerbert Xu 		return;
245174235a25SHerbert Xu 	}
245274235a25SHerbert Xu 
24531da177e4SLinus Torvalds 	idev = addrconf_add_dev(dev);
245464e724f6SBrian Haley 	if (IS_ERR(idev))
24551da177e4SLinus Torvalds 		return;
24561da177e4SLinus Torvalds 
24571da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
24581da177e4SLinus Torvalds 	addr.s6_addr32[0] = htonl(0xFE800000);
24591da177e4SLinus Torvalds 
24601da177e4SLinus Torvalds 	if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
24611da177e4SLinus Torvalds 		addrconf_add_linklocal(idev, &addr);
24621da177e4SLinus Torvalds }
24631da177e4SLinus Torvalds 
24640be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
24651da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev)
24661da177e4SLinus Torvalds {
24671da177e4SLinus Torvalds 	struct inet6_dev *idev;
24681da177e4SLinus Torvalds 
24691da177e4SLinus Torvalds 	ASSERT_RTNL();
24701da177e4SLinus Torvalds 
24711da177e4SLinus Torvalds 	/*
24721da177e4SLinus Torvalds 	 * Configure the tunnel with one of our IPv4
24731da177e4SLinus Torvalds 	 * addresses... we should configure all of
24741da177e4SLinus Torvalds 	 * our v4 addrs in the tunnel
24751da177e4SLinus Torvalds 	 */
24761da177e4SLinus Torvalds 
24771da177e4SLinus Torvalds 	if ((idev = ipv6_find_idev(dev)) == NULL) {
24781da177e4SLinus Torvalds 		printk(KERN_DEBUG "init sit: add_dev failed\n");
24791da177e4SLinus Torvalds 		return;
24801da177e4SLinus Torvalds 	}
24811da177e4SLinus Torvalds 
2482c7dc89c0SFred L. Templin 	if (dev->priv_flags & IFF_ISATAP) {
2483c7dc89c0SFred L. Templin 		struct in6_addr addr;
2484c7dc89c0SFred L. Templin 
2485c7dc89c0SFred L. Templin 		ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
2486c7dc89c0SFred L. Templin 		addrconf_prefix_route(&addr, 64, dev, 0, 0);
2487c7dc89c0SFred L. Templin 		if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2488c7dc89c0SFred L. Templin 			addrconf_add_linklocal(idev, &addr);
2489c7dc89c0SFred L. Templin 		return;
2490c7dc89c0SFred L. Templin 	}
2491c7dc89c0SFred L. Templin 
24921da177e4SLinus Torvalds 	sit_add_v4_addrs(idev);
24931da177e4SLinus Torvalds 
24941da177e4SLinus Torvalds 	if (dev->flags&IFF_POINTOPOINT) {
24951da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
24961da177e4SLinus Torvalds 		addrconf_add_lroute(dev);
24971da177e4SLinus Torvalds 	} else
24981da177e4SLinus Torvalds 		sit_route_add(dev);
24991da177e4SLinus Torvalds }
25000be669bbSJoerg Roedel #endif
25011da177e4SLinus Torvalds 
2502aee80b54Sstephen hemminger #if defined(CONFIG_NET_IPGRE) || defined(CONFIG_NET_IPGRE_MODULE)
2503aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev)
2504aee80b54Sstephen hemminger {
2505aee80b54Sstephen hemminger 	struct inet6_dev *idev;
2506aee80b54Sstephen hemminger 	struct in6_addr addr;
2507aee80b54Sstephen hemminger 
2508aee80b54Sstephen hemminger 	pr_info("ipv6: addrconf_gre_config(%s)\n", dev->name);
2509aee80b54Sstephen hemminger 
2510aee80b54Sstephen hemminger 	ASSERT_RTNL();
2511aee80b54Sstephen hemminger 
2512aee80b54Sstephen hemminger 	if ((idev = ipv6_find_idev(dev)) == NULL) {
2513aee80b54Sstephen hemminger 		printk(KERN_DEBUG "init gre: add_dev failed\n");
2514aee80b54Sstephen hemminger 		return;
2515aee80b54Sstephen hemminger 	}
2516aee80b54Sstephen hemminger 
2517aee80b54Sstephen hemminger 	ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
2518aee80b54Sstephen hemminger 	addrconf_prefix_route(&addr, 64, dev, 0, 0);
2519aee80b54Sstephen hemminger 
2520aee80b54Sstephen hemminger 	if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2521aee80b54Sstephen hemminger 		addrconf_add_linklocal(idev, &addr);
2522aee80b54Sstephen hemminger }
2523aee80b54Sstephen hemminger #endif
2524aee80b54Sstephen hemminger 
25251da177e4SLinus Torvalds static inline int
25261da177e4SLinus Torvalds ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
25271da177e4SLinus Torvalds {
25281da177e4SLinus Torvalds 	struct in6_addr lladdr;
25291da177e4SLinus Torvalds 
253095c385b4SNeil Horman 	if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
25311da177e4SLinus Torvalds 		addrconf_add_linklocal(idev, &lladdr);
25321da177e4SLinus Torvalds 		return 0;
25331da177e4SLinus Torvalds 	}
25341da177e4SLinus Torvalds 	return -1;
25351da177e4SLinus Torvalds }
25361da177e4SLinus Torvalds 
25371da177e4SLinus Torvalds static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
25381da177e4SLinus Torvalds {
25391da177e4SLinus Torvalds 	struct net_device *link_dev;
2540c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
25411da177e4SLinus Torvalds 
25421da177e4SLinus Torvalds 	/* first try to inherit the link-local address from the link device */
25431da177e4SLinus Torvalds 	if (idev->dev->iflink &&
25446fda7350SBenjamin Thery 	    (link_dev = __dev_get_by_index(net, idev->dev->iflink))) {
25451da177e4SLinus Torvalds 		if (!ipv6_inherit_linklocal(idev, link_dev))
25461da177e4SLinus Torvalds 			return;
25471da177e4SLinus Torvalds 	}
25481da177e4SLinus Torvalds 	/* then try to inherit it from any device */
25496fda7350SBenjamin Thery 	for_each_netdev(net, link_dev) {
25501da177e4SLinus Torvalds 		if (!ipv6_inherit_linklocal(idev, link_dev))
25511da177e4SLinus Torvalds 			return;
25521da177e4SLinus Torvalds 	}
25531da177e4SLinus Torvalds 	printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
25541da177e4SLinus Torvalds }
25551da177e4SLinus Torvalds 
25561da177e4SLinus Torvalds /*
25571da177e4SLinus Torvalds  * Autoconfigure tunnel with a link-local address so routing protocols,
25581da177e4SLinus Torvalds  * DHCPv6, MLD etc. can be run over the virtual link
25591da177e4SLinus Torvalds  */
25601da177e4SLinus Torvalds 
25611da177e4SLinus Torvalds static void addrconf_ip6_tnl_config(struct net_device *dev)
25621da177e4SLinus Torvalds {
25631da177e4SLinus Torvalds 	struct inet6_dev *idev;
25641da177e4SLinus Torvalds 
25651da177e4SLinus Torvalds 	ASSERT_RTNL();
25661da177e4SLinus Torvalds 
2567e21e8467SStephen Hemminger 	idev = addrconf_add_dev(dev);
256864e724f6SBrian Haley 	if (IS_ERR(idev)) {
25691da177e4SLinus Torvalds 		printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
25701da177e4SLinus Torvalds 		return;
25711da177e4SLinus Torvalds 	}
25721da177e4SLinus Torvalds 	ip6_tnl_add_linklocal(idev);
25731da177e4SLinus Torvalds }
25741da177e4SLinus Torvalds 
25751da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event,
25761da177e4SLinus Torvalds 			   void * data)
25771da177e4SLinus Torvalds {
25781da177e4SLinus Torvalds 	struct net_device *dev = (struct net_device *) data;
257974235a25SHerbert Xu 	struct inet6_dev *idev = __in6_dev_get(dev);
2580c5e33bddSYOSHIFUJI Hideaki 	int run_pending = 0;
2581b217d616SHerbert Xu 	int err;
25821da177e4SLinus Torvalds 
25831da177e4SLinus Torvalds 	switch (event) {
258445ba9dd2SYOSHIFUJI Hideaki 	case NETDEV_REGISTER:
258574235a25SHerbert Xu 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
258645ba9dd2SYOSHIFUJI Hideaki 			idev = ipv6_add_dev(dev);
258745ba9dd2SYOSHIFUJI Hideaki 			if (!idev)
2588b217d616SHerbert Xu 				return notifier_from_errno(-ENOMEM);
258945ba9dd2SYOSHIFUJI Hideaki 		}
259045ba9dd2SYOSHIFUJI Hideaki 		break;
2591bcdd553fSStephen Hemminger 
25921da177e4SLinus Torvalds 	case NETDEV_UP:
25933c21edbdSYOSHIFUJI Hideaki 	case NETDEV_CHANGE:
2594c2edacf8SJay Vosburgh 		if (dev->flags & IFF_SLAVE)
2595c2edacf8SJay Vosburgh 			break;
2596c2edacf8SJay Vosburgh 
25973c21edbdSYOSHIFUJI Hideaki 		if (event == NETDEV_UP) {
2598f24e3d65SMitsuru Chinen 			if (!addrconf_qdisc_ok(dev)) {
25993c21edbdSYOSHIFUJI Hideaki 				/* device is not ready yet. */
26003c21edbdSYOSHIFUJI Hideaki 				printk(KERN_INFO
26013c21edbdSYOSHIFUJI Hideaki 					"ADDRCONF(NETDEV_UP): %s: "
26023c21edbdSYOSHIFUJI Hideaki 					"link is not ready\n",
26033c21edbdSYOSHIFUJI Hideaki 					dev->name);
26043c21edbdSYOSHIFUJI Hideaki 				break;
26053c21edbdSYOSHIFUJI Hideaki 			}
260699081049SKristian Slavov 
2607d31c7b8fSEvgeniy Polyakov 			if (!idev && dev->mtu >= IPV6_MIN_MTU)
2608d31c7b8fSEvgeniy Polyakov 				idev = ipv6_add_dev(dev);
2609d31c7b8fSEvgeniy Polyakov 
2610e3ec6cfcSBenjamin Thery 			if (idev) {
261199081049SKristian Slavov 				idev->if_flags |= IF_READY;
2612e3ec6cfcSBenjamin Thery 				run_pending = 1;
2613e3ec6cfcSBenjamin Thery 			}
26143c21edbdSYOSHIFUJI Hideaki 		} else {
2615f24e3d65SMitsuru Chinen 			if (!addrconf_qdisc_ok(dev)) {
26163c21edbdSYOSHIFUJI Hideaki 				/* device is still not ready. */
26173c21edbdSYOSHIFUJI Hideaki 				break;
26183c21edbdSYOSHIFUJI Hideaki 			}
26193c21edbdSYOSHIFUJI Hideaki 
26203c21edbdSYOSHIFUJI Hideaki 			if (idev) {
2621bcdd553fSStephen Hemminger 				if (idev->if_flags & IF_READY)
26223c21edbdSYOSHIFUJI Hideaki 					/* device is already configured. */
26233c21edbdSYOSHIFUJI Hideaki 					break;
26243c21edbdSYOSHIFUJI Hideaki 				idev->if_flags |= IF_READY;
26253c21edbdSYOSHIFUJI Hideaki 			}
26263c21edbdSYOSHIFUJI Hideaki 
26273c21edbdSYOSHIFUJI Hideaki 			printk(KERN_INFO
26283c21edbdSYOSHIFUJI Hideaki 					"ADDRCONF(NETDEV_CHANGE): %s: "
26293c21edbdSYOSHIFUJI Hideaki 					"link becomes ready\n",
26303c21edbdSYOSHIFUJI Hideaki 					dev->name);
26313c21edbdSYOSHIFUJI Hideaki 
2632c5e33bddSYOSHIFUJI Hideaki 			run_pending = 1;
26333c21edbdSYOSHIFUJI Hideaki 		}
26343c21edbdSYOSHIFUJI Hideaki 
26351da177e4SLinus Torvalds 		switch (dev->type) {
26360be669bbSJoerg Roedel #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
26371da177e4SLinus Torvalds 		case ARPHRD_SIT:
26381da177e4SLinus Torvalds 			addrconf_sit_config(dev);
26391da177e4SLinus Torvalds 			break;
26400be669bbSJoerg Roedel #endif
2641aee80b54Sstephen hemminger #if defined(CONFIG_NET_IPGRE) || defined(CONFIG_NET_IPGRE_MODULE)
2642aee80b54Sstephen hemminger 		case ARPHRD_IPGRE:
2643aee80b54Sstephen hemminger 			addrconf_gre_config(dev);
2644aee80b54Sstephen hemminger 			break;
2645aee80b54Sstephen hemminger #endif
26461da177e4SLinus Torvalds 		case ARPHRD_TUNNEL6:
26471da177e4SLinus Torvalds 			addrconf_ip6_tnl_config(dev);
26481da177e4SLinus Torvalds 			break;
26491da177e4SLinus Torvalds 		case ARPHRD_LOOPBACK:
26501da177e4SLinus Torvalds 			init_loopback(dev);
26511da177e4SLinus Torvalds 			break;
26521da177e4SLinus Torvalds 
26531da177e4SLinus Torvalds 		default:
26541da177e4SLinus Torvalds 			addrconf_dev_config(dev);
26551da177e4SLinus Torvalds 			break;
26563ff50b79SStephen Hemminger 		}
2657bcdd553fSStephen Hemminger 
26581da177e4SLinus Torvalds 		if (idev) {
2659c5e33bddSYOSHIFUJI Hideaki 			if (run_pending)
2660c5e33bddSYOSHIFUJI Hideaki 				addrconf_dad_run(idev);
2661c5e33bddSYOSHIFUJI Hideaki 
2662bcdd553fSStephen Hemminger 			/*
2663bcdd553fSStephen Hemminger 			 * If the MTU changed during the interface down,
2664bcdd553fSStephen Hemminger 			 * when the interface up, the changed MTU must be
2665bcdd553fSStephen Hemminger 			 * reflected in the idev as well as routers.
26661da177e4SLinus Torvalds 			 */
2667bcdd553fSStephen Hemminger 			if (idev->cnf.mtu6 != dev->mtu &&
2668bcdd553fSStephen Hemminger 			    dev->mtu >= IPV6_MIN_MTU) {
26691da177e4SLinus Torvalds 				rt6_mtu_change(dev, dev->mtu);
26701da177e4SLinus Torvalds 				idev->cnf.mtu6 = dev->mtu;
26711da177e4SLinus Torvalds 			}
26721da177e4SLinus Torvalds 			idev->tstamp = jiffies;
26731da177e4SLinus Torvalds 			inet6_ifinfo_notify(RTM_NEWLINK, idev);
2674bcdd553fSStephen Hemminger 
2675bcdd553fSStephen Hemminger 			/*
2676bcdd553fSStephen Hemminger 			 * If the changed mtu during down is lower than
2677bcdd553fSStephen Hemminger 			 * IPV6_MIN_MTU stop IPv6 on this interface.
26781da177e4SLinus Torvalds 			 */
26791da177e4SLinus Torvalds 			if (dev->mtu < IPV6_MIN_MTU)
2680bcdd553fSStephen Hemminger 				addrconf_ifdown(dev, 1);
26811da177e4SLinus Torvalds 		}
26821da177e4SLinus Torvalds 		break;
26831da177e4SLinus Torvalds 
26841da177e4SLinus Torvalds 	case NETDEV_CHANGEMTU:
26851da177e4SLinus Torvalds 		if (idev && dev->mtu >= IPV6_MIN_MTU) {
26861da177e4SLinus Torvalds 			rt6_mtu_change(dev, dev->mtu);
26871da177e4SLinus Torvalds 			idev->cnf.mtu6 = dev->mtu;
26881da177e4SLinus Torvalds 			break;
26891da177e4SLinus Torvalds 		}
26901da177e4SLinus Torvalds 
2691d31c7b8fSEvgeniy Polyakov 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2692d31c7b8fSEvgeniy Polyakov 			idev = ipv6_add_dev(dev);
2693d31c7b8fSEvgeniy Polyakov 			if (idev)
2694d31c7b8fSEvgeniy Polyakov 				break;
2695d31c7b8fSEvgeniy Polyakov 		}
2696d31c7b8fSEvgeniy Polyakov 
2697bcdd553fSStephen Hemminger 		/*
2698bcdd553fSStephen Hemminger 		 * MTU falled under IPV6_MIN_MTU.
2699bcdd553fSStephen Hemminger 		 * Stop IPv6 on this interface.
2700bcdd553fSStephen Hemminger 		 */
27011da177e4SLinus Torvalds 
27021da177e4SLinus Torvalds 	case NETDEV_DOWN:
27031da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
27041da177e4SLinus Torvalds 		/*
27051da177e4SLinus Torvalds 		 *	Remove all addresses from this interface.
27061da177e4SLinus Torvalds 		 */
27071da177e4SLinus Torvalds 		addrconf_ifdown(dev, event != NETDEV_DOWN);
27081da177e4SLinus Torvalds 		break;
27093c21edbdSYOSHIFUJI Hideaki 
27101da177e4SLinus Torvalds 	case NETDEV_CHANGENAME:
27111da177e4SLinus Torvalds 		if (idev) {
27125632c515SStephen Hemminger 			snmp6_unregister_dev(idev);
2713408c4768SPavel Emelyanov 			addrconf_sysctl_unregister(idev);
2714f52295a9SPavel Emelyanov 			addrconf_sysctl_register(idev);
2715b217d616SHerbert Xu 			err = snmp6_register_dev(idev);
2716b217d616SHerbert Xu 			if (err)
2717b217d616SHerbert Xu 				return notifier_from_errno(err);
27185632c515SStephen Hemminger 		}
27191da177e4SLinus Torvalds 		break;
2720bcdd553fSStephen Hemminger 
272193d9b7d7SJiri Pirko 	case NETDEV_PRE_TYPE_CHANGE:
272293d9b7d7SJiri Pirko 	case NETDEV_POST_TYPE_CHANGE:
272393d9b7d7SJiri Pirko 		addrconf_type_change(dev, event);
272475c78500SMoni Shoua 		break;
27253ff50b79SStephen Hemminger 	}
27261da177e4SLinus Torvalds 
27271da177e4SLinus Torvalds 	return NOTIFY_OK;
27281da177e4SLinus Torvalds }
27291da177e4SLinus Torvalds 
27301da177e4SLinus Torvalds /*
27311da177e4SLinus Torvalds  *	addrconf module should be notified of a device going up
27321da177e4SLinus Torvalds  */
27331da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = {
27341da177e4SLinus Torvalds 	.notifier_call = addrconf_notify,
27351da177e4SLinus Torvalds };
27361da177e4SLinus Torvalds 
273793d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event)
273875c78500SMoni Shoua {
273975c78500SMoni Shoua 	struct inet6_dev *idev;
274075c78500SMoni Shoua 	ASSERT_RTNL();
274175c78500SMoni Shoua 
274275c78500SMoni Shoua 	idev = __in6_dev_get(dev);
274375c78500SMoni Shoua 
274493d9b7d7SJiri Pirko 	if (event == NETDEV_POST_TYPE_CHANGE)
274575c78500SMoni Shoua 		ipv6_mc_remap(idev);
274693d9b7d7SJiri Pirko 	else if (event == NETDEV_PRE_TYPE_CHANGE)
274775c78500SMoni Shoua 		ipv6_mc_unmap(idev);
274875c78500SMoni Shoua }
274975c78500SMoni Shoua 
27501da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how)
27511da177e4SLinus Torvalds {
2752c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
2753502a2ffdSstephen hemminger 	struct inet6_dev *idev;
2754502a2ffdSstephen hemminger 	struct inet6_ifaddr *ifa;
275573a8bd74SDavid S. Miller 	int state, i;
27561da177e4SLinus Torvalds 
27571da177e4SLinus Torvalds 	ASSERT_RTNL();
27581da177e4SLinus Torvalds 
2759f3db4851SDaniel Lezcano 	rt6_ifdown(net, dev);
276073a8bd74SDavid S. Miller 	neigh_ifdown(&nd_tbl, dev);
27611da177e4SLinus Torvalds 
27621da177e4SLinus Torvalds 	idev = __in6_dev_get(dev);
27631da177e4SLinus Torvalds 	if (idev == NULL)
27641da177e4SLinus Torvalds 		return -ENODEV;
27651da177e4SLinus Torvalds 
2766bcdd553fSStephen Hemminger 	/*
2767bcdd553fSStephen Hemminger 	 * Step 1: remove reference to ipv6 device from parent device.
2768bcdd553fSStephen Hemminger 	 *	   Do not dev_put!
27691da177e4SLinus Torvalds 	 */
2770439e2385SDenis V. Lunev 	if (how) {
27711da177e4SLinus Torvalds 		idev->dead = 1;
27728814c4b5SYOSHIFUJI Hideaki 
27738814c4b5SYOSHIFUJI Hideaki 		/* protected by rtnl_lock */
2774a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(dev->ip6_ptr, NULL);
27751da177e4SLinus Torvalds 
27761da177e4SLinus Torvalds 		/* Step 1.5: remove snmp6 entry */
27771da177e4SLinus Torvalds 		snmp6_unregister_dev(idev);
27781da177e4SLinus Torvalds 
27791da177e4SLinus Torvalds 	}
27801da177e4SLinus Torvalds 
278173a8bd74SDavid S. Miller 	/* Step 2: clear hash table */
278273a8bd74SDavid S. Miller 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
278373a8bd74SDavid S. Miller 		struct hlist_head *h = &inet6_addr_lst[i];
278473a8bd74SDavid S. Miller 		struct hlist_node *n;
278573a8bd74SDavid S. Miller 
278673a8bd74SDavid S. Miller 		spin_lock_bh(&addrconf_hash_lock);
278773a8bd74SDavid S. Miller 	restart:
278873a8bd74SDavid S. Miller 		hlist_for_each_entry_rcu(ifa, n, h, addr_lst) {
278973a8bd74SDavid S. Miller 			if (ifa->idev == idev) {
279073a8bd74SDavid S. Miller 				hlist_del_init_rcu(&ifa->addr_lst);
279173a8bd74SDavid S. Miller 				addrconf_del_timer(ifa);
279273a8bd74SDavid S. Miller 				goto restart;
279373a8bd74SDavid S. Miller 			}
279473a8bd74SDavid S. Miller 		}
279573a8bd74SDavid S. Miller 		spin_unlock_bh(&addrconf_hash_lock);
279673a8bd74SDavid S. Miller 	}
279773a8bd74SDavid S. Miller 
27981da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
27991da177e4SLinus Torvalds 
2800bcdd553fSStephen Hemminger 	/* Step 2: clear flags for stateless addrconf */
2801439e2385SDenis V. Lunev 	if (!how)
28023c21edbdSYOSHIFUJI Hideaki 		idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
28031da177e4SLinus Torvalds 
28041da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
2805439e2385SDenis V. Lunev 	if (how && del_timer(&idev->regen_timer))
28061da177e4SLinus Torvalds 		in6_dev_put(idev);
28071da177e4SLinus Torvalds 
2808bcdd553fSStephen Hemminger 	/* Step 3: clear tempaddr list */
2809372e6c8fSstephen hemminger 	while (!list_empty(&idev->tempaddr_list)) {
2810372e6c8fSstephen hemminger 		ifa = list_first_entry(&idev->tempaddr_list,
2811372e6c8fSstephen hemminger 				       struct inet6_ifaddr, tmp_list);
2812372e6c8fSstephen hemminger 		list_del(&ifa->tmp_list);
28131da177e4SLinus Torvalds 		write_unlock_bh(&idev->lock);
28141da177e4SLinus Torvalds 		spin_lock_bh(&ifa->lock);
28151da177e4SLinus Torvalds 
28161da177e4SLinus Torvalds 		if (ifa->ifpub) {
28171da177e4SLinus Torvalds 			in6_ifa_put(ifa->ifpub);
28181da177e4SLinus Torvalds 			ifa->ifpub = NULL;
28191da177e4SLinus Torvalds 		}
28201da177e4SLinus Torvalds 		spin_unlock_bh(&ifa->lock);
28211da177e4SLinus Torvalds 		in6_ifa_put(ifa);
28221da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
28231da177e4SLinus Torvalds 	}
28241da177e4SLinus Torvalds #endif
28258f37ada5Sstephen hemminger 
2826502a2ffdSstephen hemminger 	while (!list_empty(&idev->addr_list)) {
2827502a2ffdSstephen hemminger 		ifa = list_first_entry(&idev->addr_list,
2828502a2ffdSstephen hemminger 				       struct inet6_ifaddr, if_list);
282984e8b803Sstephen hemminger 		addrconf_del_timer(ifa);
283084e8b803Sstephen hemminger 
2831502a2ffdSstephen hemminger 		list_del(&ifa->if_list);
28321da177e4SLinus Torvalds 
28334c5ff6a6SHerbert Xu 		write_unlock_bh(&idev->lock);
283473a8bd74SDavid S. Miller 
28354c5ff6a6SHerbert Xu 		spin_lock_bh(&ifa->state_lock);
28364c5ff6a6SHerbert Xu 		state = ifa->state;
28374c5ff6a6SHerbert Xu 		ifa->state = INET6_IFADDR_STATE_DEAD;
28384c5ff6a6SHerbert Xu 		spin_unlock_bh(&ifa->state_lock);
28394c5ff6a6SHerbert Xu 
28409d82ca98SJohn Fastabend 		if (state != INET6_IFADDR_STATE_DEAD) {
28411da177e4SLinus Torvalds 			__ipv6_ifa_notify(RTM_DELADDR, ifa);
284273a8bd74SDavid S. Miller 			atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa);
28432de79570SLorenzo Colitti 		}
28449d82ca98SJohn Fastabend 		in6_ifa_put(ifa);
284573a8bd74SDavid S. Miller 
28461da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
28471da177e4SLinus Torvalds 	}
28488f37ada5Sstephen hemminger 
28491da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
28501da177e4SLinus Torvalds 
28511da177e4SLinus Torvalds 	/* Step 5: Discard multicast list */
2852439e2385SDenis V. Lunev 	if (how)
28531da177e4SLinus Torvalds 		ipv6_mc_destroy_dev(idev);
28541da177e4SLinus Torvalds 	else
28551da177e4SLinus Torvalds 		ipv6_mc_down(idev);
28561da177e4SLinus Torvalds 
28571da177e4SLinus Torvalds 	idev->tstamp = jiffies;
28581da177e4SLinus Torvalds 
2859bcdd553fSStephen Hemminger 	/* Last: Shot the device (if unregistered) */
2860439e2385SDenis V. Lunev 	if (how) {
2861408c4768SPavel Emelyanov 		addrconf_sysctl_unregister(idev);
28621da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, idev->nd_parms);
28631da177e4SLinus Torvalds 		neigh_ifdown(&nd_tbl, dev);
28641da177e4SLinus Torvalds 		in6_dev_put(idev);
28651da177e4SLinus Torvalds 	}
28661da177e4SLinus Torvalds 	return 0;
28671da177e4SLinus Torvalds }
28681da177e4SLinus Torvalds 
28691da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data)
28701da177e4SLinus Torvalds {
28711da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
28725b2a1953Sstephen hemminger 	struct inet6_dev *idev = ifp->idev;
28731da177e4SLinus Torvalds 
28745b2a1953Sstephen hemminger 	read_lock(&idev->lock);
28755b2a1953Sstephen hemminger 	if (idev->dead || !(idev->if_flags & IF_READY))
28761da177e4SLinus Torvalds 		goto out;
28771da177e4SLinus Torvalds 
28785b2a1953Sstephen hemminger 	if (idev->cnf.forwarding)
28791da177e4SLinus Torvalds 		goto out;
28805b2a1953Sstephen hemminger 
28815b2a1953Sstephen hemminger 	/* Announcement received after solicitation was sent */
28825b2a1953Sstephen hemminger 	if (idev->if_flags & IF_RA_RCVD)
28835b2a1953Sstephen hemminger 		goto out;
28841da177e4SLinus Torvalds 
28851da177e4SLinus Torvalds 	spin_lock(&ifp->lock);
28865b2a1953Sstephen hemminger 	if (ifp->probes++ < idev->cnf.rtr_solicits) {
28871da177e4SLinus Torvalds 		/* The wait after the last probe can be shorter */
28881da177e4SLinus Torvalds 		addrconf_mod_timer(ifp, AC_RS,
28895b2a1953Sstephen hemminger 				   (ifp->probes == idev->cnf.rtr_solicits) ?
28905b2a1953Sstephen hemminger 				   idev->cnf.rtr_solicit_delay :
28915b2a1953Sstephen hemminger 				   idev->cnf.rtr_solicit_interval);
28921da177e4SLinus Torvalds 		spin_unlock(&ifp->lock);
28931da177e4SLinus Torvalds 
28945b2a1953Sstephen hemminger 		ndisc_send_rs(idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
28951da177e4SLinus Torvalds 	} else {
28961da177e4SLinus Torvalds 		spin_unlock(&ifp->lock);
28971da177e4SLinus Torvalds 		/*
28981da177e4SLinus Torvalds 		 * Note: we do not support deprecated "all on-link"
28991da177e4SLinus Torvalds 		 * assumption any longer.
29001da177e4SLinus Torvalds 		 */
29011da177e4SLinus Torvalds 		printk(KERN_DEBUG "%s: no IPv6 routers present\n",
29025b2a1953Sstephen hemminger 		       idev->dev->name);
29031da177e4SLinus Torvalds 	}
29041da177e4SLinus Torvalds 
29051da177e4SLinus Torvalds out:
29065b2a1953Sstephen hemminger 	read_unlock(&idev->lock);
29071da177e4SLinus Torvalds 	in6_ifa_put(ifp);
29081da177e4SLinus Torvalds }
29091da177e4SLinus Torvalds 
29101da177e4SLinus Torvalds /*
29111da177e4SLinus Torvalds  *	Duplicate Address Detection
29121da177e4SLinus Torvalds  */
29133c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
29143c21edbdSYOSHIFUJI Hideaki {
29153c21edbdSYOSHIFUJI Hideaki 	unsigned long rand_num;
29163c21edbdSYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
29173c21edbdSYOSHIFUJI Hideaki 
291895c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
291995c385b4SNeil Horman 		rand_num = 0;
292095c385b4SNeil Horman 	else
29213c21edbdSYOSHIFUJI Hideaki 		rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
292295c385b4SNeil Horman 
29233c21edbdSYOSHIFUJI Hideaki 	ifp->probes = idev->cnf.dad_transmits;
29243c21edbdSYOSHIFUJI Hideaki 	addrconf_mod_timer(ifp, AC_DAD, rand_num);
29253c21edbdSYOSHIFUJI Hideaki }
29263c21edbdSYOSHIFUJI Hideaki 
2927e431b8c0SJamal Hadi Salim static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
29281da177e4SLinus Torvalds {
29291da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
29301da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
29311da177e4SLinus Torvalds 
29321da177e4SLinus Torvalds 	addrconf_join_solict(dev, &ifp->addr);
29331da177e4SLinus Torvalds 
29341da177e4SLinus Torvalds 	net_srandom(ifp->addr.s6_addr32[3]);
29351da177e4SLinus Torvalds 
29361da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
2937622ccdf1SHerbert Xu 	spin_lock(&ifp->lock);
2938e9d3e084SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DEAD)
29391da177e4SLinus Torvalds 		goto out;
29401da177e4SLinus Torvalds 
29411da177e4SLinus Torvalds 	if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
29421b34be74SYOSHIFUJI Hideaki 	    idev->cnf.accept_dad < 1 ||
294355ebaef1SNoriaki TAKAMIYA 	    !(ifp->flags&IFA_F_TENTATIVE) ||
294455ebaef1SNoriaki TAKAMIYA 	    ifp->flags & IFA_F_NODAD) {
2945cc411d0bSBrian Haley 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
294621809fafSstephen hemminger 		spin_unlock(&ifp->lock);
29471da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
29481da177e4SLinus Torvalds 
29491da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
29501da177e4SLinus Torvalds 		return;
29511da177e4SLinus Torvalds 	}
29521da177e4SLinus Torvalds 
29536732badeSYOSHIFUJI Hideaki 	if (!(idev->if_flags & IF_READY)) {
295421809fafSstephen hemminger 		spin_unlock(&ifp->lock);
29556732badeSYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
29563c21edbdSYOSHIFUJI Hideaki 		/*
2957590a9887SMasatake YAMATO 		 * If the device is not ready:
29583c21edbdSYOSHIFUJI Hideaki 		 * - keep it tentative if it is a permanent address.
29593c21edbdSYOSHIFUJI Hideaki 		 * - otherwise, kill it.
29603c21edbdSYOSHIFUJI Hideaki 		 */
29613c21edbdSYOSHIFUJI Hideaki 		in6_ifa_hold(ifp);
2962cc411d0bSBrian Haley 		addrconf_dad_stop(ifp, 0);
29636732badeSYOSHIFUJI Hideaki 		return;
29643c21edbdSYOSHIFUJI Hideaki 	}
296595c385b4SNeil Horman 
296695c385b4SNeil Horman 	/*
296795c385b4SNeil Horman 	 * Optimistic nodes can start receiving
296895c385b4SNeil Horman 	 * Frames right away
296995c385b4SNeil Horman 	 */
297095c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
297195c385b4SNeil Horman 		ip6_ins_rt(ifp->rt);
297295c385b4SNeil Horman 
29736732badeSYOSHIFUJI Hideaki 	addrconf_dad_kick(ifp);
29741da177e4SLinus Torvalds out:
2975622ccdf1SHerbert Xu 	spin_unlock(&ifp->lock);
29761da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
29771da177e4SLinus Torvalds }
29781da177e4SLinus Torvalds 
29791da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data)
29801da177e4SLinus Torvalds {
29811da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
29821da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
29831da177e4SLinus Torvalds 	struct in6_addr mcaddr;
29841da177e4SLinus Torvalds 
2985f2344a13SHerbert Xu 	if (!ifp->probes && addrconf_dad_end(ifp))
2986f2344a13SHerbert Xu 		goto out;
2987f2344a13SHerbert Xu 
2988122e4519Sstephen hemminger 	read_lock(&idev->lock);
2989122e4519Sstephen hemminger 	if (idev->dead || !(idev->if_flags & IF_READY)) {
2990122e4519Sstephen hemminger 		read_unlock(&idev->lock);
29911da177e4SLinus Torvalds 		goto out;
29921da177e4SLinus Torvalds 	}
299321809fafSstephen hemminger 
299421809fafSstephen hemminger 	spin_lock(&ifp->lock);
2995622ccdf1SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DEAD) {
2996622ccdf1SHerbert Xu 		spin_unlock(&ifp->lock);
2997622ccdf1SHerbert Xu 		read_unlock(&idev->lock);
2998622ccdf1SHerbert Xu 		goto out;
2999622ccdf1SHerbert Xu 	}
3000622ccdf1SHerbert Xu 
30011da177e4SLinus Torvalds 	if (ifp->probes == 0) {
30021da177e4SLinus Torvalds 		/*
30031da177e4SLinus Torvalds 		 * DAD was successful
30041da177e4SLinus Torvalds 		 */
30051da177e4SLinus Torvalds 
3006cc411d0bSBrian Haley 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
300721809fafSstephen hemminger 		spin_unlock(&ifp->lock);
3008122e4519Sstephen hemminger 		read_unlock(&idev->lock);
30091da177e4SLinus Torvalds 
30101da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
30111da177e4SLinus Torvalds 
30121da177e4SLinus Torvalds 		goto out;
30131da177e4SLinus Torvalds 	}
30141da177e4SLinus Torvalds 
30151da177e4SLinus Torvalds 	ifp->probes--;
30161da177e4SLinus Torvalds 	addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
301721809fafSstephen hemminger 	spin_unlock(&ifp->lock);
3018122e4519Sstephen hemminger 	read_unlock(&idev->lock);
30191da177e4SLinus Torvalds 
30201da177e4SLinus Torvalds 	/* send a neighbour solicitation for our addr */
30211da177e4SLinus Torvalds 	addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
3022d7aabf22SYOSHIFUJI Hideaki 	ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
30231da177e4SLinus Torvalds out:
30241da177e4SLinus Torvalds 	in6_ifa_put(ifp);
30251da177e4SLinus Torvalds }
30261da177e4SLinus Torvalds 
30271da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
30281da177e4SLinus Torvalds {
30291da177e4SLinus Torvalds 	struct net_device *dev = ifp->idev->dev;
30301da177e4SLinus Torvalds 
30311da177e4SLinus Torvalds 	/*
30321da177e4SLinus Torvalds 	 *	Configure the address for reception. Now it is valid.
30331da177e4SLinus Torvalds 	 */
30341da177e4SLinus Torvalds 
30351da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_NEWADDR, ifp);
30361da177e4SLinus Torvalds 
3037026359bcSTore Anderson 	/* If added prefix is link local and we are prepared to process
3038026359bcSTore Anderson 	   router advertisements, start sending router solicitations.
30391da177e4SLinus Torvalds 	 */
30401da177e4SLinus Torvalds 
3041026359bcSTore Anderson 	if (((ifp->idev->cnf.accept_ra == 1 && !ifp->idev->cnf.forwarding) ||
3042026359bcSTore Anderson 	     ifp->idev->cnf.accept_ra == 2) &&
30431da177e4SLinus Torvalds 	    ifp->idev->cnf.rtr_solicits > 0 &&
30441da177e4SLinus Torvalds 	    (dev->flags&IFF_LOOPBACK) == 0 &&
30451da177e4SLinus Torvalds 	    (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
30461da177e4SLinus Torvalds 		/*
30471da177e4SLinus Torvalds 		 *	If a host as already performed a random delay
30481da177e4SLinus Torvalds 		 *	[...] as part of DAD [...] there is no need
30491da177e4SLinus Torvalds 		 *	to delay again before sending the first RS
30501da177e4SLinus Torvalds 		 */
3051f3ee4010SYOSHIFUJI Hideaki 		ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
30521da177e4SLinus Torvalds 
30531da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
30541da177e4SLinus Torvalds 		ifp->probes = 1;
30551da177e4SLinus Torvalds 		ifp->idev->if_flags |= IF_RS_SENT;
30561da177e4SLinus Torvalds 		addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
30571da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
30581da177e4SLinus Torvalds 	}
30591da177e4SLinus Torvalds }
30601da177e4SLinus Torvalds 
3061e21e8467SStephen Hemminger static void addrconf_dad_run(struct inet6_dev *idev)
3062e21e8467SStephen Hemminger {
3063c5e33bddSYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifp;
3064c5e33bddSYOSHIFUJI Hideaki 
3065c5e33bddSYOSHIFUJI Hideaki 	read_lock_bh(&idev->lock);
3066502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
306721809fafSstephen hemminger 		spin_lock(&ifp->lock);
3068f2344a13SHerbert Xu 		if (ifp->flags & IFA_F_TENTATIVE &&
3069f2344a13SHerbert Xu 		    ifp->state == INET6_IFADDR_STATE_DAD)
3070c5e33bddSYOSHIFUJI Hideaki 			addrconf_dad_kick(ifp);
3071f2344a13SHerbert Xu 		spin_unlock(&ifp->lock);
3072c5e33bddSYOSHIFUJI Hideaki 	}
3073c5e33bddSYOSHIFUJI Hideaki 	read_unlock_bh(&idev->lock);
3074c5e33bddSYOSHIFUJI Hideaki }
3075c5e33bddSYOSHIFUJI Hideaki 
30761da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
30771da177e4SLinus Torvalds struct if6_iter_state {
30783c40090aSDaniel Lezcano 	struct seq_net_private p;
30791da177e4SLinus Torvalds 	int bucket;
30801da177e4SLinus Torvalds };
30811da177e4SLinus Torvalds 
30821da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
30831da177e4SLinus Torvalds {
30841da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
30851da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
30861218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
30871da177e4SLinus Torvalds 
30881da177e4SLinus Torvalds 	for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
3089c2e21293Sstephen hemminger 		struct hlist_node *n;
30904f70eccaSEric Dumazet 		hlist_for_each_entry_rcu_bh(ifa, n, &inet6_addr_lst[state->bucket],
30915c578aedSstephen hemminger 					 addr_lst)
3092c2e21293Sstephen hemminger 			if (net_eq(dev_net(ifa->idev->dev), net))
30931da177e4SLinus Torvalds 				return ifa;
30941da177e4SLinus Torvalds 	}
3095c2e21293Sstephen hemminger 	return NULL;
3096c2e21293Sstephen hemminger }
30971da177e4SLinus Torvalds 
3098c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
3099c2e21293Sstephen hemminger 					 struct inet6_ifaddr *ifa)
31001da177e4SLinus Torvalds {
31011da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
31021218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
3103c2e21293Sstephen hemminger 	struct hlist_node *n = &ifa->addr_lst;
31041da177e4SLinus Torvalds 
31054f70eccaSEric Dumazet 	hlist_for_each_entry_continue_rcu_bh(ifa, n, addr_lst)
3106c2e21293Sstephen hemminger 		if (net_eq(dev_net(ifa->idev->dev), net))
31071da177e4SLinus Torvalds 			return ifa;
31081da177e4SLinus Torvalds 
3109c2e21293Sstephen hemminger 	while (++state->bucket < IN6_ADDR_HSIZE) {
31104f70eccaSEric Dumazet 		hlist_for_each_entry_rcu_bh(ifa, n,
3111c2e21293Sstephen hemminger 				     &inet6_addr_lst[state->bucket], addr_lst) {
3112c2e21293Sstephen hemminger 			if (net_eq(dev_net(ifa->idev->dev), net))
3113c2e21293Sstephen hemminger 				return ifa;
3114c2e21293Sstephen hemminger 		}
3115c2e21293Sstephen hemminger 	}
3116c2e21293Sstephen hemminger 
3117c2e21293Sstephen hemminger 	return NULL;
3118c2e21293Sstephen hemminger }
3119c2e21293Sstephen hemminger 
31201da177e4SLinus Torvalds static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
31211da177e4SLinus Torvalds {
31221da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = if6_get_first(seq);
31231da177e4SLinus Torvalds 
31241da177e4SLinus Torvalds 	if (ifa)
31251da177e4SLinus Torvalds 		while (pos && (ifa = if6_get_next(seq, ifa)) != NULL)
31261da177e4SLinus Torvalds 			--pos;
31271da177e4SLinus Torvalds 	return pos ? NULL : ifa;
31281da177e4SLinus Torvalds }
31291da177e4SLinus Torvalds 
31301da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
31314f70eccaSEric Dumazet 	__acquires(rcu_bh)
31321da177e4SLinus Torvalds {
31335c578aedSstephen hemminger 	rcu_read_lock_bh();
31341da177e4SLinus Torvalds 	return if6_get_idx(seq, *pos);
31351da177e4SLinus Torvalds }
31361da177e4SLinus Torvalds 
31371da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
31381da177e4SLinus Torvalds {
31391da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
31401da177e4SLinus Torvalds 
31411da177e4SLinus Torvalds 	ifa = if6_get_next(seq, v);
31421da177e4SLinus Torvalds 	++*pos;
31431da177e4SLinus Torvalds 	return ifa;
31441da177e4SLinus Torvalds }
31451da177e4SLinus Torvalds 
31461da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v)
31474f70eccaSEric Dumazet 	__releases(rcu_bh)
31481da177e4SLinus Torvalds {
31495c578aedSstephen hemminger 	rcu_read_unlock_bh();
31501da177e4SLinus Torvalds }
31511da177e4SLinus Torvalds 
31521da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v)
31531da177e4SLinus Torvalds {
31541da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
31554b7a4274SHarvey Harrison 	seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
3156b071195dSHarvey Harrison 		   &ifp->addr,
31571da177e4SLinus Torvalds 		   ifp->idev->dev->ifindex,
31581da177e4SLinus Torvalds 		   ifp->prefix_len,
31591da177e4SLinus Torvalds 		   ifp->scope,
31601da177e4SLinus Torvalds 		   ifp->flags,
31611da177e4SLinus Torvalds 		   ifp->idev->dev->name);
31621da177e4SLinus Torvalds 	return 0;
31631da177e4SLinus Torvalds }
31641da177e4SLinus Torvalds 
316556b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = {
31661da177e4SLinus Torvalds 	.start	= if6_seq_start,
31671da177e4SLinus Torvalds 	.next	= if6_seq_next,
31681da177e4SLinus Torvalds 	.show	= if6_seq_show,
31691da177e4SLinus Torvalds 	.stop	= if6_seq_stop,
31701da177e4SLinus Torvalds };
31711da177e4SLinus Torvalds 
31721da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file)
31731da177e4SLinus Torvalds {
31743c40090aSDaniel Lezcano 	return seq_open_net(inode, file, &if6_seq_ops,
3175cf7732e4SPavel Emelyanov 			    sizeof(struct if6_iter_state));
31761da177e4SLinus Torvalds }
31771da177e4SLinus Torvalds 
31789a32144eSArjan van de Ven static const struct file_operations if6_fops = {
31791da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
31801da177e4SLinus Torvalds 	.open		= if6_seq_open,
31811da177e4SLinus Torvalds 	.read		= seq_read,
31821da177e4SLinus Torvalds 	.llseek		= seq_lseek,
31833c40090aSDaniel Lezcano 	.release	= seq_release_net,
31841da177e4SLinus Torvalds };
31851da177e4SLinus Torvalds 
31862c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net)
31871da177e4SLinus Torvalds {
31883c40090aSDaniel Lezcano 	if (!proc_net_fops_create(net, "if_inet6", S_IRUGO, &if6_fops))
31891da177e4SLinus Torvalds 		return -ENOMEM;
31901da177e4SLinus Torvalds 	return 0;
31911da177e4SLinus Torvalds }
31921da177e4SLinus Torvalds 
31932c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net)
31943c40090aSDaniel Lezcano {
31953c40090aSDaniel Lezcano        proc_net_remove(net, "if_inet6");
31963c40090aSDaniel Lezcano }
31973c40090aSDaniel Lezcano 
31983c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = {
31993c40090aSDaniel Lezcano        .init = if6_proc_net_init,
32003c40090aSDaniel Lezcano        .exit = if6_proc_net_exit,
32013c40090aSDaniel Lezcano };
32023c40090aSDaniel Lezcano 
32033c40090aSDaniel Lezcano int __init if6_proc_init(void)
32043c40090aSDaniel Lezcano {
32053c40090aSDaniel Lezcano 	return register_pernet_subsys(&if6_proc_net_ops);
32063c40090aSDaniel Lezcano }
32073c40090aSDaniel Lezcano 
32081da177e4SLinus Torvalds void if6_proc_exit(void)
32091da177e4SLinus Torvalds {
32103c40090aSDaniel Lezcano 	unregister_pernet_subsys(&if6_proc_net_ops);
32111da177e4SLinus Torvalds }
32121da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
32131da177e4SLinus Torvalds 
321459fbb3a6SMasahide NAKAMURA #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
32153b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */
3216b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
32173b9f9a1cSNoriaki TAKAMIYA {
32183b9f9a1cSNoriaki TAKAMIYA 	int ret = 0;
3219c2e21293Sstephen hemminger 	struct inet6_ifaddr *ifp = NULL;
3220c2e21293Sstephen hemminger 	struct hlist_node *n;
32213a88a81dSstephen hemminger 	unsigned int hash = ipv6_addr_hash(addr);
3222c2e21293Sstephen hemminger 
32235c578aedSstephen hemminger 	rcu_read_lock_bh();
32244f70eccaSEric Dumazet 	hlist_for_each_entry_rcu_bh(ifp, n, &inet6_addr_lst[hash], addr_lst) {
3225878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
3226389f6612SDaniel Lezcano 			continue;
3227caad295fSYOSHIFUJI Hideaki 		if (ipv6_addr_equal(&ifp->addr, addr) &&
32283b9f9a1cSNoriaki TAKAMIYA 		    (ifp->flags & IFA_F_HOMEADDRESS)) {
32293b9f9a1cSNoriaki TAKAMIYA 			ret = 1;
32303b9f9a1cSNoriaki TAKAMIYA 			break;
32313b9f9a1cSNoriaki TAKAMIYA 		}
32323b9f9a1cSNoriaki TAKAMIYA 	}
32335c578aedSstephen hemminger 	rcu_read_unlock_bh();
32343b9f9a1cSNoriaki TAKAMIYA 	return ret;
32353b9f9a1cSNoriaki TAKAMIYA }
32363b9f9a1cSNoriaki TAKAMIYA #endif
32373b9f9a1cSNoriaki TAKAMIYA 
32381da177e4SLinus Torvalds /*
32391da177e4SLinus Torvalds  *	Periodic address status verification
32401da177e4SLinus Torvalds  */
32411da177e4SLinus Torvalds 
32421da177e4SLinus Torvalds static void addrconf_verify(unsigned long foo)
32431da177e4SLinus Torvalds {
3244b2db7564SYOSHIFUJI Hideaki 	unsigned long now, next, next_sec, next_sched;
32451da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
3246c2e21293Sstephen hemminger 	struct hlist_node *node;
32471da177e4SLinus Torvalds 	int i;
32481da177e4SLinus Torvalds 
32495c578aedSstephen hemminger 	rcu_read_lock_bh();
32505c578aedSstephen hemminger 	spin_lock(&addrconf_verify_lock);
32511da177e4SLinus Torvalds 	now = jiffies;
3252b2db7564SYOSHIFUJI Hideaki 	next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
32531da177e4SLinus Torvalds 
32541da177e4SLinus Torvalds 	del_timer(&addr_chk_timer);
32551da177e4SLinus Torvalds 
32561da177e4SLinus Torvalds 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
32571da177e4SLinus Torvalds restart:
32584f70eccaSEric Dumazet 		hlist_for_each_entry_rcu_bh(ifp, node,
32595c578aedSstephen hemminger 					 &inet6_addr_lst[i], addr_lst) {
32601da177e4SLinus Torvalds 			unsigned long age;
32611da177e4SLinus Torvalds 
32621da177e4SLinus Torvalds 			if (ifp->flags & IFA_F_PERMANENT)
32631da177e4SLinus Torvalds 				continue;
32641da177e4SLinus Torvalds 
32651da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
3266b2db7564SYOSHIFUJI Hideaki 			/* We try to batch several events at once. */
3267b2db7564SYOSHIFUJI Hideaki 			age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
32681da177e4SLinus Torvalds 
32698f27ebb9SYOSHIFUJI Hideaki 			if (ifp->valid_lft != INFINITY_LIFE_TIME &&
32708f27ebb9SYOSHIFUJI Hideaki 			    age >= ifp->valid_lft) {
32711da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
32721da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
32731da177e4SLinus Torvalds 				ipv6_del_addr(ifp);
32741da177e4SLinus Torvalds 				goto restart;
32758f27ebb9SYOSHIFUJI Hideaki 			} else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
32768f27ebb9SYOSHIFUJI Hideaki 				spin_unlock(&ifp->lock);
32778f27ebb9SYOSHIFUJI Hideaki 				continue;
32781da177e4SLinus Torvalds 			} else if (age >= ifp->prefered_lft) {
3279a1ed0526SBrian Haley 				/* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
32801da177e4SLinus Torvalds 				int deprecate = 0;
32811da177e4SLinus Torvalds 
32821da177e4SLinus Torvalds 				if (!(ifp->flags&IFA_F_DEPRECATED)) {
32831da177e4SLinus Torvalds 					deprecate = 1;
32841da177e4SLinus Torvalds 					ifp->flags |= IFA_F_DEPRECATED;
32851da177e4SLinus Torvalds 				}
32861da177e4SLinus Torvalds 
32871da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
32881da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->valid_lft * HZ;
32891da177e4SLinus Torvalds 
32901da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
32911da177e4SLinus Torvalds 
32921da177e4SLinus Torvalds 				if (deprecate) {
32931da177e4SLinus Torvalds 					in6_ifa_hold(ifp);
32941da177e4SLinus Torvalds 
32951da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
32961da177e4SLinus Torvalds 					in6_ifa_put(ifp);
32971da177e4SLinus Torvalds 					goto restart;
32981da177e4SLinus Torvalds 				}
32991da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
33001da177e4SLinus Torvalds 			} else if ((ifp->flags&IFA_F_TEMPORARY) &&
33011da177e4SLinus Torvalds 				   !(ifp->flags&IFA_F_TENTATIVE)) {
330288949cf4Sstephen hemminger 				unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
330388949cf4Sstephen hemminger 					ifp->idev->cnf.dad_transmits *
330488949cf4Sstephen hemminger 					ifp->idev->nd_parms->retrans_time / HZ;
330588949cf4Sstephen hemminger 
33061da177e4SLinus Torvalds 				if (age >= ifp->prefered_lft - regen_advance) {
33071da177e4SLinus Torvalds 					struct inet6_ifaddr *ifpub = ifp->ifpub;
33081da177e4SLinus Torvalds 					if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
33091da177e4SLinus Torvalds 						next = ifp->tstamp + ifp->prefered_lft * HZ;
33101da177e4SLinus Torvalds 					if (!ifp->regen_count && ifpub) {
33111da177e4SLinus Torvalds 						ifp->regen_count++;
33121da177e4SLinus Torvalds 						in6_ifa_hold(ifp);
33131da177e4SLinus Torvalds 						in6_ifa_hold(ifpub);
33141da177e4SLinus Torvalds 						spin_unlock(&ifp->lock);
33155c578aedSstephen hemminger 
3316291d809bSHiroyuki YAMAMORI 						spin_lock(&ifpub->lock);
3317291d809bSHiroyuki YAMAMORI 						ifpub->regen_count = 0;
3318291d809bSHiroyuki YAMAMORI 						spin_unlock(&ifpub->lock);
33191da177e4SLinus Torvalds 						ipv6_create_tempaddr(ifpub, ifp);
33201da177e4SLinus Torvalds 						in6_ifa_put(ifpub);
33211da177e4SLinus Torvalds 						in6_ifa_put(ifp);
33221da177e4SLinus Torvalds 						goto restart;
33231da177e4SLinus Torvalds 					}
33241da177e4SLinus Torvalds 				} else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
33251da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
33261da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
33271da177e4SLinus Torvalds #endif
33281da177e4SLinus Torvalds 			} else {
33291da177e4SLinus Torvalds 				/* ifp->prefered_lft <= ifp->valid_lft */
33301da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
33311da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ;
33321da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
33331da177e4SLinus Torvalds 			}
33341da177e4SLinus Torvalds 		}
33351da177e4SLinus Torvalds 	}
33361da177e4SLinus Torvalds 
3337b2db7564SYOSHIFUJI Hideaki 	next_sec = round_jiffies_up(next);
3338b2db7564SYOSHIFUJI Hideaki 	next_sched = next;
3339b2db7564SYOSHIFUJI Hideaki 
3340b2db7564SYOSHIFUJI Hideaki 	/* If rounded timeout is accurate enough, accept it. */
3341b2db7564SYOSHIFUJI Hideaki 	if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
3342b2db7564SYOSHIFUJI Hideaki 		next_sched = next_sec;
3343b2db7564SYOSHIFUJI Hideaki 
3344b2db7564SYOSHIFUJI Hideaki 	/* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
3345b2db7564SYOSHIFUJI Hideaki 	if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
3346b2db7564SYOSHIFUJI Hideaki 		next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
3347b2db7564SYOSHIFUJI Hideaki 
3348b2db7564SYOSHIFUJI Hideaki 	ADBG((KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
3349b2db7564SYOSHIFUJI Hideaki 	      now, next, next_sec, next_sched));
3350b2db7564SYOSHIFUJI Hideaki 
3351b2db7564SYOSHIFUJI Hideaki 	addr_chk_timer.expires = next_sched;
33521da177e4SLinus Torvalds 	add_timer(&addr_chk_timer);
33535c578aedSstephen hemminger 	spin_unlock(&addrconf_verify_lock);
33545c578aedSstephen hemminger 	rcu_read_unlock_bh();
33551da177e4SLinus Torvalds }
33561da177e4SLinus Torvalds 
3357461d8837SThomas Graf static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
3358461d8837SThomas Graf {
3359461d8837SThomas Graf 	struct in6_addr *pfx = NULL;
3360461d8837SThomas Graf 
3361461d8837SThomas Graf 	if (addr)
3362461d8837SThomas Graf 		pfx = nla_data(addr);
3363461d8837SThomas Graf 
3364461d8837SThomas Graf 	if (local) {
3365461d8837SThomas Graf 		if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3366461d8837SThomas Graf 			pfx = NULL;
3367461d8837SThomas Graf 		else
3368461d8837SThomas Graf 			pfx = nla_data(local);
3369461d8837SThomas Graf 	}
3370461d8837SThomas Graf 
3371461d8837SThomas Graf 	return pfx;
3372461d8837SThomas Graf }
3373461d8837SThomas Graf 
3374ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
3375461d8837SThomas Graf 	[IFA_ADDRESS]		= { .len = sizeof(struct in6_addr) },
3376461d8837SThomas Graf 	[IFA_LOCAL]		= { .len = sizeof(struct in6_addr) },
3377461d8837SThomas Graf 	[IFA_CACHEINFO]		= { .len = sizeof(struct ifa_cacheinfo) },
3378461d8837SThomas Graf };
3379461d8837SThomas Graf 
33801da177e4SLinus Torvalds static int
33811da177e4SLinus Torvalds inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
33821da177e4SLinus Torvalds {
33833b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
3384b933f716SThomas Graf 	struct ifaddrmsg *ifm;
3385b933f716SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
33861da177e4SLinus Torvalds 	struct in6_addr *pfx;
3387b933f716SThomas Graf 	int err;
33881da177e4SLinus Torvalds 
3389b933f716SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3390b933f716SThomas Graf 	if (err < 0)
3391b933f716SThomas Graf 		return err;
3392b933f716SThomas Graf 
3393b933f716SThomas Graf 	ifm = nlmsg_data(nlh);
3394b933f716SThomas Graf 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
33951da177e4SLinus Torvalds 	if (pfx == NULL)
33961da177e4SLinus Torvalds 		return -EINVAL;
33971da177e4SLinus Torvalds 
3398af284937SDaniel Lezcano 	return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen);
33991da177e4SLinus Torvalds }
34001da177e4SLinus Torvalds 
340155ebaef1SNoriaki TAKAMIYA static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
340255ebaef1SNoriaki TAKAMIYA 			     u32 prefered_lft, u32 valid_lft)
3403081bba5bSNoriaki TAKAMIYA {
34046f704992SYOSHIFUJI Hideaki 	u32 flags;
34056f704992SYOSHIFUJI Hideaki 	clock_t expires;
34064bed72e4SYOSHIFUJI Hideaki 	unsigned long timeout;
340746d48046SYOSHIFUJI Hideaki 
3408081bba5bSNoriaki TAKAMIYA 	if (!valid_lft || (prefered_lft > valid_lft))
3409081bba5bSNoriaki TAKAMIYA 		return -EINVAL;
3410081bba5bSNoriaki TAKAMIYA 
34114bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
34124bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
34134bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
34144bed72e4SYOSHIFUJI Hideaki 		valid_lft = timeout;
34156f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
34164bed72e4SYOSHIFUJI Hideaki 	} else {
34174bed72e4SYOSHIFUJI Hideaki 		expires = 0;
34184bed72e4SYOSHIFUJI Hideaki 		flags = 0;
34194bed72e4SYOSHIFUJI Hideaki 		ifa_flags |= IFA_F_PERMANENT;
34206f704992SYOSHIFUJI Hideaki 	}
3421081bba5bSNoriaki TAKAMIYA 
34224bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
34234bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
34244bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
342555ebaef1SNoriaki TAKAMIYA 			ifa_flags |= IFA_F_DEPRECATED;
34264bed72e4SYOSHIFUJI Hideaki 		prefered_lft = timeout;
34274bed72e4SYOSHIFUJI Hideaki 	}
3428081bba5bSNoriaki TAKAMIYA 
3429081bba5bSNoriaki TAKAMIYA 	spin_lock_bh(&ifp->lock);
34303b9f9a1cSNoriaki TAKAMIYA 	ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags;
3431081bba5bSNoriaki TAKAMIYA 	ifp->tstamp = jiffies;
3432081bba5bSNoriaki TAKAMIYA 	ifp->valid_lft = valid_lft;
3433081bba5bSNoriaki TAKAMIYA 	ifp->prefered_lft = prefered_lft;
3434081bba5bSNoriaki TAKAMIYA 
3435081bba5bSNoriaki TAKAMIYA 	spin_unlock_bh(&ifp->lock);
3436081bba5bSNoriaki TAKAMIYA 	if (!(ifp->flags&IFA_F_TENTATIVE))
3437081bba5bSNoriaki TAKAMIYA 		ipv6_ifa_notify(0, ifp);
3438081bba5bSNoriaki TAKAMIYA 
343946d48046SYOSHIFUJI Hideaki 	addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
34406f704992SYOSHIFUJI Hideaki 			      expires, flags);
3441081bba5bSNoriaki TAKAMIYA 	addrconf_verify(0);
3442081bba5bSNoriaki TAKAMIYA 
3443081bba5bSNoriaki TAKAMIYA 	return 0;
3444081bba5bSNoriaki TAKAMIYA }
3445081bba5bSNoriaki TAKAMIYA 
3446081bba5bSNoriaki TAKAMIYA static int
34471da177e4SLinus Torvalds inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
34481da177e4SLinus Torvalds {
34493b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
3450461d8837SThomas Graf 	struct ifaddrmsg *ifm;
3451461d8837SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
34521da177e4SLinus Torvalds 	struct in6_addr *pfx;
34537198f8ceSThomas Graf 	struct inet6_ifaddr *ifa;
34547198f8ceSThomas Graf 	struct net_device *dev;
345555ebaef1SNoriaki TAKAMIYA 	u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
345655ebaef1SNoriaki TAKAMIYA 	u8 ifa_flags;
3457461d8837SThomas Graf 	int err;
34581da177e4SLinus Torvalds 
3459461d8837SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3460461d8837SThomas Graf 	if (err < 0)
3461461d8837SThomas Graf 		return err;
3462461d8837SThomas Graf 
3463461d8837SThomas Graf 	ifm = nlmsg_data(nlh);
3464461d8837SThomas Graf 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
34651da177e4SLinus Torvalds 	if (pfx == NULL)
34661da177e4SLinus Torvalds 		return -EINVAL;
34671da177e4SLinus Torvalds 
3468461d8837SThomas Graf 	if (tb[IFA_CACHEINFO]) {
34690778769dSNoriaki TAKAMIYA 		struct ifa_cacheinfo *ci;
3470461d8837SThomas Graf 
3471461d8837SThomas Graf 		ci = nla_data(tb[IFA_CACHEINFO]);
34720778769dSNoriaki TAKAMIYA 		valid_lft = ci->ifa_valid;
3473461d8837SThomas Graf 		preferred_lft = ci->ifa_prefered;
3474461d8837SThomas Graf 	} else {
3475461d8837SThomas Graf 		preferred_lft = INFINITY_LIFE_TIME;
3476461d8837SThomas Graf 		valid_lft = INFINITY_LIFE_TIME;
34770778769dSNoriaki TAKAMIYA 	}
34780778769dSNoriaki TAKAMIYA 
3479af284937SDaniel Lezcano 	dev =  __dev_get_by_index(net, ifm->ifa_index);
34807198f8ceSThomas Graf 	if (dev == NULL)
34817198f8ceSThomas Graf 		return -ENODEV;
3482081bba5bSNoriaki TAKAMIYA 
348355ebaef1SNoriaki TAKAMIYA 	/* We ignore other flags so far. */
34843b9f9a1cSNoriaki TAKAMIYA 	ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
348555ebaef1SNoriaki TAKAMIYA 
34861cab3da6SDaniel Lezcano 	ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
34877198f8ceSThomas Graf 	if (ifa == NULL) {
34887198f8ceSThomas Graf 		/*
34897198f8ceSThomas Graf 		 * It would be best to check for !NLM_F_CREATE here but
34907198f8ceSThomas Graf 		 * userspace alreay relies on not having to provide this.
34917198f8ceSThomas Graf 		 */
3492af284937SDaniel Lezcano 		return inet6_addr_add(net, ifm->ifa_index, pfx,
3493af284937SDaniel Lezcano 				      ifm->ifa_prefixlen, ifa_flags,
3494af284937SDaniel Lezcano 				      preferred_lft, valid_lft);
34951da177e4SLinus Torvalds 	}
34961da177e4SLinus Torvalds 
34977198f8ceSThomas Graf 	if (nlh->nlmsg_flags & NLM_F_EXCL ||
34987198f8ceSThomas Graf 	    !(nlh->nlmsg_flags & NLM_F_REPLACE))
34997198f8ceSThomas Graf 		err = -EEXIST;
35007198f8ceSThomas Graf 	else
350155ebaef1SNoriaki TAKAMIYA 		err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
35027198f8ceSThomas Graf 
35037198f8ceSThomas Graf 	in6_ifa_put(ifa);
35047198f8ceSThomas Graf 
35057198f8ceSThomas Graf 	return err;
35067198f8ceSThomas Graf }
35077198f8ceSThomas Graf 
3508101bb229SThomas Graf static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3509101bb229SThomas Graf 			  u8 scope, int ifindex)
3510101bb229SThomas Graf {
3511101bb229SThomas Graf 	struct ifaddrmsg *ifm;
3512101bb229SThomas Graf 
3513101bb229SThomas Graf 	ifm = nlmsg_data(nlh);
3514101bb229SThomas Graf 	ifm->ifa_family = AF_INET6;
3515101bb229SThomas Graf 	ifm->ifa_prefixlen = prefixlen;
3516101bb229SThomas Graf 	ifm->ifa_flags = flags;
3517101bb229SThomas Graf 	ifm->ifa_scope = scope;
3518101bb229SThomas Graf 	ifm->ifa_index = ifindex;
3519101bb229SThomas Graf }
3520101bb229SThomas Graf 
352185486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
352285486af0SThomas Graf 			 unsigned long tstamp, u32 preferred, u32 valid)
352385486af0SThomas Graf {
352485486af0SThomas Graf 	struct ifa_cacheinfo ci;
352585486af0SThomas Graf 
352618a31e1eSThomas Graf 	ci.cstamp = cstamp_delta(cstamp);
352718a31e1eSThomas Graf 	ci.tstamp = cstamp_delta(tstamp);
352885486af0SThomas Graf 	ci.ifa_prefered = preferred;
352985486af0SThomas Graf 	ci.ifa_valid = valid;
353085486af0SThomas Graf 
353185486af0SThomas Graf 	return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
353285486af0SThomas Graf }
353385486af0SThomas Graf 
3534101bb229SThomas Graf static inline int rt_scope(int ifa_scope)
3535101bb229SThomas Graf {
3536101bb229SThomas Graf 	if (ifa_scope & IFA_HOST)
3537101bb229SThomas Graf 		return RT_SCOPE_HOST;
3538101bb229SThomas Graf 	else if (ifa_scope & IFA_LINK)
3539101bb229SThomas Graf 		return RT_SCOPE_LINK;
3540101bb229SThomas Graf 	else if (ifa_scope & IFA_SITE)
3541101bb229SThomas Graf 		return RT_SCOPE_SITE;
3542101bb229SThomas Graf 	else
3543101bb229SThomas Graf 		return RT_SCOPE_UNIVERSE;
3544101bb229SThomas Graf }
3545101bb229SThomas Graf 
35460ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void)
35470ab6803bSThomas Graf {
3548339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3549339bf98fSThomas Graf 	       + nla_total_size(16) /* IFA_ADDRESS */
3550339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct ifa_cacheinfo));
35510ab6803bSThomas Graf }
3552c5396a31SYOSHIFUJI Hideaki 
35531da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
3554b6544c0bSJamal Hadi Salim 			     u32 pid, u32 seq, int event, unsigned int flags)
35551da177e4SLinus Torvalds {
35561da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
355785486af0SThomas Graf 	u32 preferred, valid;
35581da177e4SLinus Torvalds 
35590ab6803bSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
35600ab6803bSThomas Graf 	if (nlh == NULL)
356126932566SPatrick McHardy 		return -EMSGSIZE;
35620ab6803bSThomas Graf 
3563101bb229SThomas Graf 	put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3564101bb229SThomas Graf 		      ifa->idev->dev->ifindex);
3565101bb229SThomas Graf 
35661da177e4SLinus Torvalds 	if (!(ifa->flags&IFA_F_PERMANENT)) {
356785486af0SThomas Graf 		preferred = ifa->prefered_lft;
356885486af0SThomas Graf 		valid = ifa->valid_lft;
356985486af0SThomas Graf 		if (preferred != INFINITY_LIFE_TIME) {
35701da177e4SLinus Torvalds 			long tval = (jiffies - ifa->tstamp)/HZ;
3571a1faa698SJens Rosenboom 			if (preferred > tval)
357285486af0SThomas Graf 				preferred -= tval;
3573a1faa698SJens Rosenboom 			else
3574a1faa698SJens Rosenboom 				preferred = 0;
3575f56619fcSBen Hutchings 			if (valid != INFINITY_LIFE_TIME) {
3576f56619fcSBen Hutchings 				if (valid > tval)
357785486af0SThomas Graf 					valid -= tval;
3578f56619fcSBen Hutchings 				else
3579f56619fcSBen Hutchings 					valid = 0;
3580f56619fcSBen Hutchings 			}
35811da177e4SLinus Torvalds 		}
35821da177e4SLinus Torvalds 	} else {
358385486af0SThomas Graf 		preferred = INFINITY_LIFE_TIME;
358485486af0SThomas Graf 		valid = INFINITY_LIFE_TIME;
35851da177e4SLinus Torvalds 	}
358685486af0SThomas Graf 
35870ab6803bSThomas Graf 	if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
358826932566SPatrick McHardy 	    put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
358926932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
359026932566SPatrick McHardy 		return -EMSGSIZE;
359126932566SPatrick McHardy 	}
359285486af0SThomas Graf 
35930ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
35941da177e4SLinus Torvalds }
35951da177e4SLinus Torvalds 
35961da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
3597e431b8c0SJamal Hadi Salim 				u32 pid, u32 seq, int event, u16 flags)
35981da177e4SLinus Torvalds {
35991da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
3600101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
3601101bb229SThomas Graf 	int ifindex = ifmca->idev->dev->ifindex;
36021da177e4SLinus Torvalds 
36031da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3604101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
3605101bb229SThomas Graf 
36060ab6803bSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
36070ab6803bSThomas Graf 	if (nlh == NULL)
360826932566SPatrick McHardy 		return -EMSGSIZE;
36090ab6803bSThomas Graf 
3610101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
36110ab6803bSThomas Graf 	if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
36120ab6803bSThomas Graf 	    put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
361326932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
361426932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
361526932566SPatrick McHardy 		return -EMSGSIZE;
361626932566SPatrick McHardy 	}
361785486af0SThomas Graf 
36180ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
36191da177e4SLinus Torvalds }
36201da177e4SLinus Torvalds 
36211da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
3622b6544c0bSJamal Hadi Salim 				u32 pid, u32 seq, int event, unsigned int flags)
36231da177e4SLinus Torvalds {
36241da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
3625101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
3626101bb229SThomas Graf 	int ifindex = ifaca->aca_idev->dev->ifindex;
36271da177e4SLinus Torvalds 
36281da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3629101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
3630101bb229SThomas Graf 
36310ab6803bSThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
36320ab6803bSThomas Graf 	if (nlh == NULL)
363326932566SPatrick McHardy 		return -EMSGSIZE;
36340ab6803bSThomas Graf 
3635101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
36360ab6803bSThomas Graf 	if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
36370ab6803bSThomas Graf 	    put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
363826932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
363926932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
364026932566SPatrick McHardy 		return -EMSGSIZE;
364126932566SPatrick McHardy 	}
364285486af0SThomas Graf 
36430ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
36441da177e4SLinus Torvalds }
36451da177e4SLinus Torvalds 
3646e21e8467SStephen Hemminger enum addr_type_t {
36471da177e4SLinus Torvalds 	UNICAST_ADDR,
36481da177e4SLinus Torvalds 	MULTICAST_ADDR,
36491da177e4SLinus Torvalds 	ANYCAST_ADDR,
36501da177e4SLinus Torvalds };
36511da177e4SLinus Torvalds 
3652234b27c3SEric Dumazet /* called with rcu_read_lock() */
3653234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
3654234b27c3SEric Dumazet 			  struct netlink_callback *cb, enum addr_type_t type,
3655234b27c3SEric Dumazet 			  int s_ip_idx, int *p_ip_idx)
36561da177e4SLinus Torvalds {
36571da177e4SLinus Torvalds 	struct ifmcaddr6 *ifmca;
36581da177e4SLinus Torvalds 	struct ifacaddr6 *ifaca;
3659234b27c3SEric Dumazet 	int err = 1;
3660234b27c3SEric Dumazet 	int ip_idx = *p_ip_idx;
36611da177e4SLinus Torvalds 
36621da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
36631da177e4SLinus Torvalds 	switch (type) {
3664502a2ffdSstephen hemminger 	case UNICAST_ADDR: {
3665502a2ffdSstephen hemminger 		struct inet6_ifaddr *ifa;
3666502a2ffdSstephen hemminger 
3667ae9cda5dSYOSHIFUJI Hideaki 		/* unicast address incl. temp addr */
3668502a2ffdSstephen hemminger 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
3669502a2ffdSstephen hemminger 			if (++ip_idx < s_ip_idx)
36701da177e4SLinus Torvalds 				continue;
36715d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifaddr(skb, ifa,
36721da177e4SLinus Torvalds 						NETLINK_CB(cb->skb).pid,
36735d5619b4SYOSHIFUJI Hideaki 						cb->nlh->nlmsg_seq,
36745d5619b4SYOSHIFUJI Hideaki 						RTM_NEWADDR,
36755d5619b4SYOSHIFUJI Hideaki 						NLM_F_MULTI);
3676bcd32326SEric Dumazet 			if (err <= 0)
3677bcd32326SEric Dumazet 				break;
36781da177e4SLinus Torvalds 		}
36791da177e4SLinus Torvalds 		break;
3680502a2ffdSstephen hemminger 	}
36811da177e4SLinus Torvalds 	case MULTICAST_ADDR:
36821da177e4SLinus Torvalds 		/* multicast address */
36831da177e4SLinus Torvalds 		for (ifmca = idev->mc_list; ifmca;
36841da177e4SLinus Torvalds 		     ifmca = ifmca->next, ip_idx++) {
36851da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
36861da177e4SLinus Torvalds 				continue;
36875d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifmcaddr(skb, ifmca,
36881da177e4SLinus Torvalds 						  NETLINK_CB(cb->skb).pid,
36895d5619b4SYOSHIFUJI Hideaki 						  cb->nlh->nlmsg_seq,
36905d5619b4SYOSHIFUJI Hideaki 						  RTM_GETMULTICAST,
36915d5619b4SYOSHIFUJI Hideaki 						  NLM_F_MULTI);
3692bcd32326SEric Dumazet 			if (err <= 0)
3693bcd32326SEric Dumazet 				break;
36941da177e4SLinus Torvalds 		}
36951da177e4SLinus Torvalds 		break;
36961da177e4SLinus Torvalds 	case ANYCAST_ADDR:
36971da177e4SLinus Torvalds 		/* anycast address */
36981da177e4SLinus Torvalds 		for (ifaca = idev->ac_list; ifaca;
36991da177e4SLinus Torvalds 		     ifaca = ifaca->aca_next, ip_idx++) {
37001da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
37011da177e4SLinus Torvalds 				continue;
37025d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifacaddr(skb, ifaca,
37031da177e4SLinus Torvalds 						  NETLINK_CB(cb->skb).pid,
37045d5619b4SYOSHIFUJI Hideaki 						  cb->nlh->nlmsg_seq,
37055d5619b4SYOSHIFUJI Hideaki 						  RTM_GETANYCAST,
37065d5619b4SYOSHIFUJI Hideaki 						  NLM_F_MULTI);
3707bcd32326SEric Dumazet 			if (err <= 0)
3708bcd32326SEric Dumazet 				break;
37091da177e4SLinus Torvalds 		}
37101da177e4SLinus Torvalds 		break;
37111da177e4SLinus Torvalds 	default:
37121da177e4SLinus Torvalds 		break;
37131da177e4SLinus Torvalds 	}
37141da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
3715234b27c3SEric Dumazet 	*p_ip_idx = ip_idx;
3716234b27c3SEric Dumazet 	return err;
3717234b27c3SEric Dumazet }
37185d5619b4SYOSHIFUJI Hideaki 
3719234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3720234b27c3SEric Dumazet 			   enum addr_type_t type)
3721234b27c3SEric Dumazet {
3722234b27c3SEric Dumazet 	struct net *net = sock_net(skb->sk);
3723234b27c3SEric Dumazet 	int h, s_h;
3724234b27c3SEric Dumazet 	int idx, ip_idx;
3725234b27c3SEric Dumazet 	int s_idx, s_ip_idx;
3726234b27c3SEric Dumazet 	struct net_device *dev;
3727234b27c3SEric Dumazet 	struct inet6_dev *idev;
3728234b27c3SEric Dumazet 	struct hlist_head *head;
3729234b27c3SEric Dumazet 	struct hlist_node *node;
3730234b27c3SEric Dumazet 
3731234b27c3SEric Dumazet 	s_h = cb->args[0];
3732234b27c3SEric Dumazet 	s_idx = idx = cb->args[1];
3733234b27c3SEric Dumazet 	s_ip_idx = ip_idx = cb->args[2];
3734234b27c3SEric Dumazet 
3735234b27c3SEric Dumazet 	rcu_read_lock();
3736234b27c3SEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3737234b27c3SEric Dumazet 		idx = 0;
3738234b27c3SEric Dumazet 		head = &net->dev_index_head[h];
3739234b27c3SEric Dumazet 		hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
3740234b27c3SEric Dumazet 			if (idx < s_idx)
3741234b27c3SEric Dumazet 				goto cont;
37424b97efdfSPatrick McHardy 			if (h > s_h || idx > s_idx)
3743234b27c3SEric Dumazet 				s_ip_idx = 0;
3744234b27c3SEric Dumazet 			ip_idx = 0;
3745e21e8467SStephen Hemminger 			idev = __in6_dev_get(dev);
3746e21e8467SStephen Hemminger 			if (!idev)
3747234b27c3SEric Dumazet 				goto cont;
3748234b27c3SEric Dumazet 
3749234b27c3SEric Dumazet 			if (in6_dump_addrs(idev, skb, cb, type,
3750234b27c3SEric Dumazet 					   s_ip_idx, &ip_idx) <= 0)
3751234b27c3SEric Dumazet 				goto done;
37527562f876SPavel Emelianov cont:
37537562f876SPavel Emelianov 			idx++;
37541da177e4SLinus Torvalds 		}
3755234b27c3SEric Dumazet 	}
3756234b27c3SEric Dumazet done:
3757234b27c3SEric Dumazet 	rcu_read_unlock();
3758234b27c3SEric Dumazet 	cb->args[0] = h;
3759234b27c3SEric Dumazet 	cb->args[1] = idx;
3760234b27c3SEric Dumazet 	cb->args[2] = ip_idx;
3761234b27c3SEric Dumazet 
37621da177e4SLinus Torvalds 	return skb->len;
37631da177e4SLinus Torvalds }
37641da177e4SLinus Torvalds 
37651da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
37661da177e4SLinus Torvalds {
37671da177e4SLinus Torvalds 	enum addr_type_t type = UNICAST_ADDR;
3768b854272bSDenis V. Lunev 
37691da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
37701da177e4SLinus Torvalds }
37711da177e4SLinus Torvalds 
37721da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
37731da177e4SLinus Torvalds {
37741da177e4SLinus Torvalds 	enum addr_type_t type = MULTICAST_ADDR;
3775b854272bSDenis V. Lunev 
37761da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
37771da177e4SLinus Torvalds }
37781da177e4SLinus Torvalds 
37791da177e4SLinus Torvalds 
37801da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
37811da177e4SLinus Torvalds {
37821da177e4SLinus Torvalds 	enum addr_type_t type = ANYCAST_ADDR;
3783b854272bSDenis V. Lunev 
37841da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
37851da177e4SLinus Torvalds }
37861da177e4SLinus Torvalds 
37871b29fc2cSThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
37881b29fc2cSThomas Graf 			     void *arg)
37896c223828SNoriaki TAKAMIYA {
37903b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
37911b29fc2cSThomas Graf 	struct ifaddrmsg *ifm;
37921b29fc2cSThomas Graf 	struct nlattr *tb[IFA_MAX+1];
37936c223828SNoriaki TAKAMIYA 	struct in6_addr *addr = NULL;
37946c223828SNoriaki TAKAMIYA 	struct net_device *dev = NULL;
37956c223828SNoriaki TAKAMIYA 	struct inet6_ifaddr *ifa;
37966c223828SNoriaki TAKAMIYA 	struct sk_buff *skb;
37976c223828SNoriaki TAKAMIYA 	int err;
37986c223828SNoriaki TAKAMIYA 
37991b29fc2cSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
38001b29fc2cSThomas Graf 	if (err < 0)
38011b29fc2cSThomas Graf 		goto errout;
38026c223828SNoriaki TAKAMIYA 
38031b29fc2cSThomas Graf 	addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
38041b29fc2cSThomas Graf 	if (addr == NULL) {
38051b29fc2cSThomas Graf 		err = -EINVAL;
38061b29fc2cSThomas Graf 		goto errout;
38071b29fc2cSThomas Graf 	}
38081b29fc2cSThomas Graf 
38091b29fc2cSThomas Graf 	ifm = nlmsg_data(nlh);
38106c223828SNoriaki TAKAMIYA 	if (ifm->ifa_index)
38116fda7350SBenjamin Thery 		dev = __dev_get_by_index(net, ifm->ifa_index);
38126c223828SNoriaki TAKAMIYA 
3813e21e8467SStephen Hemminger 	ifa = ipv6_get_ifaddr(net, addr, dev, 1);
3814e21e8467SStephen Hemminger 	if (!ifa) {
38151b29fc2cSThomas Graf 		err = -EADDRNOTAVAIL;
38161b29fc2cSThomas Graf 		goto errout;
38176c223828SNoriaki TAKAMIYA 	}
38186c223828SNoriaki TAKAMIYA 
3819e21e8467SStephen Hemminger 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
3820e21e8467SStephen Hemminger 	if (!skb) {
38211b29fc2cSThomas Graf 		err = -ENOBUFS;
38221b29fc2cSThomas Graf 		goto errout_ifa;
38231b29fc2cSThomas Graf 	}
38241b29fc2cSThomas Graf 
38256c223828SNoriaki TAKAMIYA 	err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
38266c223828SNoriaki TAKAMIYA 				nlh->nlmsg_seq, RTM_NEWADDR, 0);
382726932566SPatrick McHardy 	if (err < 0) {
382826932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
382926932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
383026932566SPatrick McHardy 		kfree_skb(skb);
383126932566SPatrick McHardy 		goto errout_ifa;
383226932566SPatrick McHardy 	}
38336fda7350SBenjamin Thery 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
38341b29fc2cSThomas Graf errout_ifa:
38356c223828SNoriaki TAKAMIYA 	in6_ifa_put(ifa);
38361b29fc2cSThomas Graf errout:
38376c223828SNoriaki TAKAMIYA 	return err;
38386c223828SNoriaki TAKAMIYA }
38396c223828SNoriaki TAKAMIYA 
38401da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
38411da177e4SLinus Torvalds {
38421da177e4SLinus Torvalds 	struct sk_buff *skb;
3843c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(ifa->idev->dev);
38445d620266SThomas Graf 	int err = -ENOBUFS;
38451da177e4SLinus Torvalds 
38460ab6803bSThomas Graf 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
38475d620266SThomas Graf 	if (skb == NULL)
38485d620266SThomas Graf 		goto errout;
38495d620266SThomas Graf 
38505d620266SThomas Graf 	err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
385126932566SPatrick McHardy 	if (err < 0) {
385226932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
385326932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
385426932566SPatrick McHardy 		kfree_skb(skb);
385526932566SPatrick McHardy 		goto errout;
385626932566SPatrick McHardy 	}
38571ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
38581ce85fe4SPablo Neira Ayuso 	return;
38595d620266SThomas Graf errout:
38605d620266SThomas Graf 	if (err < 0)
38616fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
38621da177e4SLinus Torvalds }
38631da177e4SLinus Torvalds 
3864b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
38651da177e4SLinus Torvalds 				__s32 *array, int bytes)
38661da177e4SLinus Torvalds {
386704561c1fSThomas Graf 	BUG_ON(bytes < (DEVCONF_MAX * 4));
386804561c1fSThomas Graf 
38691da177e4SLinus Torvalds 	memset(array, 0, bytes);
38701da177e4SLinus Torvalds 	array[DEVCONF_FORWARDING] = cnf->forwarding;
38711da177e4SLinus Torvalds 	array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
38721da177e4SLinus Torvalds 	array[DEVCONF_MTU6] = cnf->mtu6;
38731da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
38741da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
38751da177e4SLinus Torvalds 	array[DEVCONF_AUTOCONF] = cnf->autoconf;
38761da177e4SLinus Torvalds 	array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
38771da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
387893908d19SThomas Graf 	array[DEVCONF_RTR_SOLICIT_INTERVAL] =
387993908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_solicit_interval);
388093908d19SThomas Graf 	array[DEVCONF_RTR_SOLICIT_DELAY] =
388193908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_solicit_delay);
38821da177e4SLinus Torvalds 	array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
38831da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
38841da177e4SLinus Torvalds 	array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
38851da177e4SLinus Torvalds 	array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
38861da177e4SLinus Torvalds 	array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
38871da177e4SLinus Torvalds 	array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
38881da177e4SLinus Torvalds 	array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
38891da177e4SLinus Torvalds #endif
38901da177e4SLinus Torvalds 	array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
389165f5c7c1SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
3892c4fd30ebSYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
3893930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
3894930d6ff2SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
389593908d19SThomas Graf 	array[DEVCONF_RTR_PROBE_INTERVAL] =
389693908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_probe_interval);
3897fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
389809c884d4SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
389909c884d4SYOSHIFUJI Hideaki #endif
3900930d6ff2SYOSHIFUJI Hideaki #endif
3901fbea49e1SYOSHIFUJI Hideaki 	array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
39020bcbc926SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
390395c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
390495c385b4SNeil Horman 	array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
390595c385b4SNeil Horman #endif
39067bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
39077bc570c8SYOSHIFUJI Hideaki 	array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
39087bc570c8SYOSHIFUJI Hideaki #endif
3909778d80beSYOSHIFUJI Hideaki 	array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
39101b34be74SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
3911c3faca05SCosmin Ratiu 	array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
39121da177e4SLinus Torvalds }
39131da177e4SLinus Torvalds 
3914b382b191SThomas Graf static inline size_t inet6_ifla6_size(void)
3915b382b191SThomas Graf {
3916b382b191SThomas Graf 	return nla_total_size(4) /* IFLA_INET6_FLAGS */
3917b382b191SThomas Graf 	     + nla_total_size(sizeof(struct ifla_cacheinfo))
3918b382b191SThomas Graf 	     + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
3919b382b191SThomas Graf 	     + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
3920b382b191SThomas Graf 	     + nla_total_size(ICMP6_MIB_MAX * 8); /* IFLA_INET6_ICMP6STATS */
3921b382b191SThomas Graf }
3922b382b191SThomas Graf 
3923339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void)
3924339bf98fSThomas Graf {
3925339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3926339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3927339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3928339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
3929339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
3930b382b191SThomas Graf 	       + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
3931339bf98fSThomas Graf }
3932c5396a31SYOSHIFUJI Hideaki 
3933be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
39347d720c3eSTejun Heo 				      int items, int bytes)
39357f7d9a6bSHerbert Xu {
39367f7d9a6bSHerbert Xu 	int i;
39377f7d9a6bSHerbert Xu 	int pad = bytes - sizeof(u64) * items;
39387f7d9a6bSHerbert Xu 	BUG_ON(pad < 0);
39397f7d9a6bSHerbert Xu 
39407f7d9a6bSHerbert Xu 	/* Use put_unaligned() because stats may not be aligned for u64. */
39417f7d9a6bSHerbert Xu 	put_unaligned(items, &stats[0]);
39427f7d9a6bSHerbert Xu 	for (i = 1; i < items; i++)
3943be281e55SEric Dumazet 		put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
39447f7d9a6bSHerbert Xu 
39457f7d9a6bSHerbert Xu 	memset(&stats[items], 0, pad);
39467f7d9a6bSHerbert Xu }
39477f7d9a6bSHerbert Xu 
39484ce3c183SEric Dumazet static inline void __snmp6_fill_stats64(u64 *stats, void __percpu **mib,
39494ce3c183SEric Dumazet 				      int items, int bytes, size_t syncpoff)
39504ce3c183SEric Dumazet {
39514ce3c183SEric Dumazet 	int i;
39524ce3c183SEric Dumazet 	int pad = bytes - sizeof(u64) * items;
39534ce3c183SEric Dumazet 	BUG_ON(pad < 0);
39544ce3c183SEric Dumazet 
39554ce3c183SEric Dumazet 	/* Use put_unaligned() because stats may not be aligned for u64. */
39564ce3c183SEric Dumazet 	put_unaligned(items, &stats[0]);
39574ce3c183SEric Dumazet 	for (i = 1; i < items; i++)
39584ce3c183SEric Dumazet 		put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]);
39594ce3c183SEric Dumazet 
39604ce3c183SEric Dumazet 	memset(&stats[items], 0, pad);
39614ce3c183SEric Dumazet }
39624ce3c183SEric Dumazet 
39637f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
39647f7d9a6bSHerbert Xu 			     int bytes)
39657f7d9a6bSHerbert Xu {
39667f7d9a6bSHerbert Xu 	switch (attrtype) {
39677f7d9a6bSHerbert Xu 	case IFLA_INET6_STATS:
39684ce3c183SEric Dumazet 		__snmp6_fill_stats64(stats, (void __percpu **)idev->stats.ipv6,
39694ce3c183SEric Dumazet 				     IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp));
39707f7d9a6bSHerbert Xu 		break;
39717f7d9a6bSHerbert Xu 	case IFLA_INET6_ICMP6STATS:
3972be281e55SEric Dumazet 		__snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes);
39737f7d9a6bSHerbert Xu 		break;
39747f7d9a6bSHerbert Xu 	}
39757f7d9a6bSHerbert Xu }
39767f7d9a6bSHerbert Xu 
3977b382b191SThomas Graf static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev)
39781da177e4SLinus Torvalds {
3979bf99f1bdSYOSHIFUJI Hideaki 	struct nlattr *nla;
39801da177e4SLinus Torvalds 	struct ifla_cacheinfo ci;
39811da177e4SLinus Torvalds 
398204561c1fSThomas Graf 	NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags);
39831da177e4SLinus Torvalds 
39841da177e4SLinus Torvalds 	ci.max_reasm_len = IPV6_MAXPLEN;
398524912420SDavid S. Miller 	ci.tstamp = cstamp_delta(idev->tstamp);
398624912420SDavid S. Miller 	ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
398724912420SDavid S. Miller 	ci.retrans_time = jiffies_to_msecs(idev->nd_parms->retrans_time);
398804561c1fSThomas Graf 	NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
39891da177e4SLinus Torvalds 
3990bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
3991bf99f1bdSYOSHIFUJI Hideaki 	if (nla == NULL)
399204561c1fSThomas Graf 		goto nla_put_failure;
3993bf99f1bdSYOSHIFUJI Hideaki 	ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
39941da177e4SLinus Torvalds 
3995bf99f1bdSYOSHIFUJI Hideaki 	/* XXX - MC not implemented */
3996bf99f1bdSYOSHIFUJI Hideaki 
3997bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
3998bf99f1bdSYOSHIFUJI Hideaki 	if (nla == NULL)
3999bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
4000bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
4001bf99f1bdSYOSHIFUJI Hideaki 
4002bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
4003bf99f1bdSYOSHIFUJI Hideaki 	if (nla == NULL)
4004bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
4005bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
40061da177e4SLinus Torvalds 
4007b382b191SThomas Graf 	return 0;
4008b382b191SThomas Graf 
4009b382b191SThomas Graf nla_put_failure:
4010b382b191SThomas Graf 	return -EMSGSIZE;
4011b382b191SThomas Graf }
4012b382b191SThomas Graf 
4013b382b191SThomas Graf static size_t inet6_get_link_af_size(const struct net_device *dev)
4014b382b191SThomas Graf {
4015b382b191SThomas Graf 	if (!__in6_dev_get(dev))
4016b382b191SThomas Graf 		return 0;
4017b382b191SThomas Graf 
4018b382b191SThomas Graf 	return inet6_ifla6_size();
4019b382b191SThomas Graf }
4020b382b191SThomas Graf 
4021b382b191SThomas Graf static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
4022b382b191SThomas Graf {
4023b382b191SThomas Graf 	struct inet6_dev *idev = __in6_dev_get(dev);
4024b382b191SThomas Graf 
4025b382b191SThomas Graf 	if (!idev)
4026b382b191SThomas Graf 		return -ENODATA;
4027b382b191SThomas Graf 
4028b382b191SThomas Graf 	if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4029b382b191SThomas Graf 		return -EMSGSIZE;
4030b382b191SThomas Graf 
4031b382b191SThomas Graf 	return 0;
4032b382b191SThomas Graf }
4033b382b191SThomas Graf 
4034b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
4035b382b191SThomas Graf 			     u32 pid, u32 seq, int event, unsigned int flags)
4036b382b191SThomas Graf {
4037b382b191SThomas Graf 	struct net_device *dev = idev->dev;
4038b382b191SThomas Graf 	struct ifinfomsg *hdr;
4039b382b191SThomas Graf 	struct nlmsghdr *nlh;
4040b382b191SThomas Graf 	void *protoinfo;
4041b382b191SThomas Graf 
4042b382b191SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
4043b382b191SThomas Graf 	if (nlh == NULL)
4044b382b191SThomas Graf 		return -EMSGSIZE;
4045b382b191SThomas Graf 
4046b382b191SThomas Graf 	hdr = nlmsg_data(nlh);
4047b382b191SThomas Graf 	hdr->ifi_family = AF_INET6;
4048b382b191SThomas Graf 	hdr->__ifi_pad = 0;
4049b382b191SThomas Graf 	hdr->ifi_type = dev->type;
4050b382b191SThomas Graf 	hdr->ifi_index = dev->ifindex;
4051b382b191SThomas Graf 	hdr->ifi_flags = dev_get_flags(dev);
4052b382b191SThomas Graf 	hdr->ifi_change = 0;
4053b382b191SThomas Graf 
4054b382b191SThomas Graf 	NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
4055b382b191SThomas Graf 
4056b382b191SThomas Graf 	if (dev->addr_len)
4057b382b191SThomas Graf 		NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
4058b382b191SThomas Graf 
4059b382b191SThomas Graf 	NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
4060b382b191SThomas Graf 	if (dev->ifindex != dev->iflink)
4061b382b191SThomas Graf 		NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
4062b382b191SThomas Graf 
4063b382b191SThomas Graf 	protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
4064b382b191SThomas Graf 	if (protoinfo == NULL)
4065b382b191SThomas Graf 		goto nla_put_failure;
4066b382b191SThomas Graf 
4067b382b191SThomas Graf 	if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4068b382b191SThomas Graf 		goto nla_put_failure;
4069b382b191SThomas Graf 
407004561c1fSThomas Graf 	nla_nest_end(skb, protoinfo);
407104561c1fSThomas Graf 	return nlmsg_end(skb, nlh);
40721da177e4SLinus Torvalds 
407304561c1fSThomas Graf nla_put_failure:
407426932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
407526932566SPatrick McHardy 	return -EMSGSIZE;
40761da177e4SLinus Torvalds }
40771da177e4SLinus Torvalds 
40781da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
40791da177e4SLinus Torvalds {
40803b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
408184d2697dSEric Dumazet 	int h, s_h;
4082434a8a58SDavid S. Miller 	int idx = 0, s_idx;
40831da177e4SLinus Torvalds 	struct net_device *dev;
40841da177e4SLinus Torvalds 	struct inet6_dev *idev;
408584d2697dSEric Dumazet 	struct hlist_head *head;
408684d2697dSEric Dumazet 	struct hlist_node *node;
40871da177e4SLinus Torvalds 
408884d2697dSEric Dumazet 	s_h = cb->args[0];
408984d2697dSEric Dumazet 	s_idx = cb->args[1];
409084d2697dSEric Dumazet 
409184d2697dSEric Dumazet 	rcu_read_lock();
409284d2697dSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
40937562f876SPavel Emelianov 		idx = 0;
409484d2697dSEric Dumazet 		head = &net->dev_index_head[h];
409584d2697dSEric Dumazet 		hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
40961da177e4SLinus Torvalds 			if (idx < s_idx)
40977562f876SPavel Emelianov 				goto cont;
409884d2697dSEric Dumazet 			idev = __in6_dev_get(dev);
409984d2697dSEric Dumazet 			if (!idev)
41007562f876SPavel Emelianov 				goto cont;
410184d2697dSEric Dumazet 			if (inet6_fill_ifinfo(skb, idev,
410284d2697dSEric Dumazet 					      NETLINK_CB(cb->skb).pid,
410384d2697dSEric Dumazet 					      cb->nlh->nlmsg_seq,
410484d2697dSEric Dumazet 					      RTM_NEWLINK, NLM_F_MULTI) <= 0)
410584d2697dSEric Dumazet 				goto out;
41067562f876SPavel Emelianov cont:
41077562f876SPavel Emelianov 			idx++;
41081da177e4SLinus Torvalds 		}
410984d2697dSEric Dumazet 	}
411084d2697dSEric Dumazet out:
411184d2697dSEric Dumazet 	rcu_read_unlock();
411284d2697dSEric Dumazet 	cb->args[1] = idx;
411384d2697dSEric Dumazet 	cb->args[0] = h;
41141da177e4SLinus Torvalds 
41151da177e4SLinus Torvalds 	return skb->len;
41161da177e4SLinus Torvalds }
41171da177e4SLinus Torvalds 
41181da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
41191da177e4SLinus Torvalds {
41201da177e4SLinus Torvalds 	struct sk_buff *skb;
4121c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
41228d7a76c9SThomas Graf 	int err = -ENOBUFS;
41231da177e4SLinus Torvalds 
4124339bf98fSThomas Graf 	skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
41258d7a76c9SThomas Graf 	if (skb == NULL)
41268d7a76c9SThomas Graf 		goto errout;
41278d7a76c9SThomas Graf 
41288d7a76c9SThomas Graf 	err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
412926932566SPatrick McHardy 	if (err < 0) {
413026932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
413126932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
413226932566SPatrick McHardy 		kfree_skb(skb);
413326932566SPatrick McHardy 		goto errout;
413426932566SPatrick McHardy 	}
41355f75a104SNicolas Dichtel 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
41361ce85fe4SPablo Neira Ayuso 	return;
41378d7a76c9SThomas Graf errout:
41388d7a76c9SThomas Graf 	if (err < 0)
41395f75a104SNicolas Dichtel 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
41401da177e4SLinus Torvalds }
41411da177e4SLinus Torvalds 
4142339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void)
4143339bf98fSThomas Graf {
4144339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct prefixmsg))
4145339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct in6_addr))
4146339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct prefix_cacheinfo));
4147339bf98fSThomas Graf }
4148c5396a31SYOSHIFUJI Hideaki 
41491da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
4150b6544c0bSJamal Hadi Salim 			     struct prefix_info *pinfo, u32 pid, u32 seq,
4151b6544c0bSJamal Hadi Salim 			     int event, unsigned int flags)
41521da177e4SLinus Torvalds {
41531da177e4SLinus Torvalds 	struct prefixmsg *pmsg;
41541da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
41551da177e4SLinus Torvalds 	struct prefix_cacheinfo	ci;
41561da177e4SLinus Torvalds 
41576051e2f4SThomas Graf 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags);
41586051e2f4SThomas Graf 	if (nlh == NULL)
415926932566SPatrick McHardy 		return -EMSGSIZE;
41606051e2f4SThomas Graf 
41616051e2f4SThomas Graf 	pmsg = nlmsg_data(nlh);
41621da177e4SLinus Torvalds 	pmsg->prefix_family = AF_INET6;
41638a47077aSPatrick McHardy 	pmsg->prefix_pad1 = 0;
41648a47077aSPatrick McHardy 	pmsg->prefix_pad2 = 0;
41651da177e4SLinus Torvalds 	pmsg->prefix_ifindex = idev->dev->ifindex;
41661da177e4SLinus Torvalds 	pmsg->prefix_len = pinfo->prefix_len;
41671da177e4SLinus Torvalds 	pmsg->prefix_type = pinfo->type;
41688a47077aSPatrick McHardy 	pmsg->prefix_pad3 = 0;
41691da177e4SLinus Torvalds 	pmsg->prefix_flags = 0;
41701da177e4SLinus Torvalds 	if (pinfo->onlink)
41711da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_ONLINK;
41721da177e4SLinus Torvalds 	if (pinfo->autoconf)
41731da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
41741da177e4SLinus Torvalds 
41756051e2f4SThomas Graf 	NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
41761da177e4SLinus Torvalds 
41771da177e4SLinus Torvalds 	ci.preferred_time = ntohl(pinfo->prefered);
41781da177e4SLinus Torvalds 	ci.valid_time = ntohl(pinfo->valid);
41796051e2f4SThomas Graf 	NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
41801da177e4SLinus Torvalds 
41816051e2f4SThomas Graf 	return nlmsg_end(skb, nlh);
41821da177e4SLinus Torvalds 
41836051e2f4SThomas Graf nla_put_failure:
418426932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
418526932566SPatrick McHardy 	return -EMSGSIZE;
41861da177e4SLinus Torvalds }
41871da177e4SLinus Torvalds 
41881da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
41891da177e4SLinus Torvalds 			 struct prefix_info *pinfo)
41901da177e4SLinus Torvalds {
41911da177e4SLinus Torvalds 	struct sk_buff *skb;
4192c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
41938c384bfaSThomas Graf 	int err = -ENOBUFS;
41941da177e4SLinus Torvalds 
4195339bf98fSThomas Graf 	skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
41968c384bfaSThomas Graf 	if (skb == NULL)
41978c384bfaSThomas Graf 		goto errout;
41988c384bfaSThomas Graf 
41998c384bfaSThomas Graf 	err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
420026932566SPatrick McHardy 	if (err < 0) {
420126932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
420226932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
420326932566SPatrick McHardy 		kfree_skb(skb);
420426932566SPatrick McHardy 		goto errout;
420526932566SPatrick McHardy 	}
42061ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
42071ce85fe4SPablo Neira Ayuso 	return;
42088c384bfaSThomas Graf errout:
42098c384bfaSThomas Graf 	if (err < 0)
42106fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
42111da177e4SLinus Torvalds }
42121da177e4SLinus Torvalds 
42131da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
42141da177e4SLinus Torvalds {
42151da177e4SLinus Torvalds 	inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
42161da177e4SLinus Torvalds 
42171da177e4SLinus Torvalds 	switch (event) {
42181da177e4SLinus Torvalds 	case RTM_NEWADDR:
421995c385b4SNeil Horman 		/*
422095c385b4SNeil Horman 		 * If the address was optimistic
422195c385b4SNeil Horman 		 * we inserted the route at the start of
422295c385b4SNeil Horman 		 * our DAD process, so we don't need
422395c385b4SNeil Horman 		 * to do it again
422495c385b4SNeil Horman 		 */
422595c385b4SNeil Horman 		if (!(ifp->rt->rt6i_node))
422640e22e8fSThomas Graf 			ip6_ins_rt(ifp->rt);
42271da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
42281da177e4SLinus Torvalds 			addrconf_join_anycast(ifp);
42291da177e4SLinus Torvalds 		break;
42301da177e4SLinus Torvalds 	case RTM_DELADDR:
42311da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
42321da177e4SLinus Torvalds 			addrconf_leave_anycast(ifp);
42331da177e4SLinus Torvalds 		addrconf_leave_solict(ifp->idev, &ifp->addr);
4234d8d1f30bSChangli Gao 		dst_hold(&ifp->rt->dst);
423593fa159aSstephen hemminger 
423673a8bd74SDavid S. Miller 		if (ip6_del_rt(ifp->rt))
4237d8d1f30bSChangli Gao 			dst_free(&ifp->rt->dst);
42381da177e4SLinus Torvalds 		break;
42391da177e4SLinus Torvalds 	}
42401da177e4SLinus Torvalds }
42411da177e4SLinus Torvalds 
42421da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
42431da177e4SLinus Torvalds {
42448814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
42451da177e4SLinus Torvalds 	if (likely(ifp->idev->dead == 0))
42461da177e4SLinus Torvalds 		__ipv6_ifa_notify(event, ifp);
42478814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
42481da177e4SLinus Torvalds }
42491da177e4SLinus Torvalds 
42501da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
42511da177e4SLinus Torvalds 
42521da177e4SLinus Torvalds static
42538d65af78SAlexey Dobriyan int addrconf_sysctl_forward(ctl_table *ctl, int write,
42541da177e4SLinus Torvalds 			   void __user *buffer, size_t *lenp, loff_t *ppos)
42551da177e4SLinus Torvalds {
42561da177e4SLinus Torvalds 	int *valp = ctl->data;
42571da177e4SLinus Torvalds 	int val = *valp;
425888af182eSEric W. Biederman 	loff_t pos = *ppos;
42591da177e4SLinus Torvalds 	int ret;
42601da177e4SLinus Torvalds 
42618d65af78SAlexey Dobriyan 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
42621da177e4SLinus Torvalds 
4263c8fecf22SPavel Emelyanov 	if (write)
4264b325fddbSStephen Hemminger 		ret = addrconf_fixup_forwarding(ctl, valp, val);
426588af182eSEric W. Biederman 	if (ret)
426688af182eSEric W. Biederman 		*ppos = pos;
42671da177e4SLinus Torvalds 	return ret;
42681da177e4SLinus Torvalds }
42691da177e4SLinus Torvalds 
427056d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev)
427156d417b1SBrian Haley {
427256d417b1SBrian Haley 	if (!idev || !idev->dev)
427356d417b1SBrian Haley 		return;
427456d417b1SBrian Haley 
427556d417b1SBrian Haley 	if (idev->cnf.disable_ipv6)
427656d417b1SBrian Haley 		addrconf_notify(NULL, NETDEV_DOWN, idev->dev);
427756d417b1SBrian Haley 	else
427856d417b1SBrian Haley 		addrconf_notify(NULL, NETDEV_UP, idev->dev);
427956d417b1SBrian Haley }
428056d417b1SBrian Haley 
428156d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf)
428256d417b1SBrian Haley {
428356d417b1SBrian Haley 	struct net_device *dev;
428456d417b1SBrian Haley 	struct inet6_dev *idev;
428556d417b1SBrian Haley 
428656d417b1SBrian Haley 	rcu_read_lock();
4287c6d14c84SEric Dumazet 	for_each_netdev_rcu(net, dev) {
428856d417b1SBrian Haley 		idev = __in6_dev_get(dev);
428956d417b1SBrian Haley 		if (idev) {
429056d417b1SBrian Haley 			int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
429156d417b1SBrian Haley 			idev->cnf.disable_ipv6 = newf;
429256d417b1SBrian Haley 			if (changed)
429356d417b1SBrian Haley 				dev_disable_change(idev);
429456d417b1SBrian Haley 		}
429556d417b1SBrian Haley 	}
4296c6d14c84SEric Dumazet 	rcu_read_unlock();
429756d417b1SBrian Haley }
429856d417b1SBrian Haley 
429956d417b1SBrian Haley static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int old)
430056d417b1SBrian Haley {
430156d417b1SBrian Haley 	struct net *net;
430256d417b1SBrian Haley 
430356d417b1SBrian Haley 	net = (struct net *)table->extra2;
430456d417b1SBrian Haley 
430556d417b1SBrian Haley 	if (p == &net->ipv6.devconf_dflt->disable_ipv6)
430656d417b1SBrian Haley 		return 0;
430756d417b1SBrian Haley 
430888af182eSEric W. Biederman 	if (!rtnl_trylock()) {
430988af182eSEric W. Biederman 		/* Restore the original values before restarting */
431088af182eSEric W. Biederman 		*p = old;
431156d417b1SBrian Haley 		return restart_syscall();
431288af182eSEric W. Biederman 	}
431356d417b1SBrian Haley 
431456d417b1SBrian Haley 	if (p == &net->ipv6.devconf_all->disable_ipv6) {
431556d417b1SBrian Haley 		__s32 newf = net->ipv6.devconf_all->disable_ipv6;
431656d417b1SBrian Haley 		net->ipv6.devconf_dflt->disable_ipv6 = newf;
431756d417b1SBrian Haley 		addrconf_disable_change(net, newf);
431856d417b1SBrian Haley 	} else if ((!*p) ^ (!old))
431956d417b1SBrian Haley 		dev_disable_change((struct inet6_dev *)table->extra1);
432056d417b1SBrian Haley 
432156d417b1SBrian Haley 	rtnl_unlock();
432256d417b1SBrian Haley 	return 0;
432356d417b1SBrian Haley }
432456d417b1SBrian Haley 
432556d417b1SBrian Haley static
43268d65af78SAlexey Dobriyan int addrconf_sysctl_disable(ctl_table *ctl, int write,
432756d417b1SBrian Haley 			    void __user *buffer, size_t *lenp, loff_t *ppos)
432856d417b1SBrian Haley {
432956d417b1SBrian Haley 	int *valp = ctl->data;
433056d417b1SBrian Haley 	int val = *valp;
433188af182eSEric W. Biederman 	loff_t pos = *ppos;
433256d417b1SBrian Haley 	int ret;
433356d417b1SBrian Haley 
43348d65af78SAlexey Dobriyan 	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
433556d417b1SBrian Haley 
433656d417b1SBrian Haley 	if (write)
433756d417b1SBrian Haley 		ret = addrconf_disable_ipv6(ctl, valp, val);
433888af182eSEric W. Biederman 	if (ret)
433988af182eSEric W. Biederman 		*ppos = pos;
434056d417b1SBrian Haley 	return ret;
434156d417b1SBrian Haley }
434256d417b1SBrian Haley 
43431da177e4SLinus Torvalds static struct addrconf_sysctl_table
43441da177e4SLinus Torvalds {
43451da177e4SLinus Torvalds 	struct ctl_table_header *sysctl_header;
4346f6a07b29SYOSHIFUJI Hideaki 	ctl_table addrconf_vars[DEVCONF_MAX+1];
43471dab6222SPavel Emelyanov 	char *dev_name;
4348ab32ea5dSBrian Haley } addrconf_sysctl __read_mostly = {
43491da177e4SLinus Torvalds 	.sysctl_header = NULL,
43501da177e4SLinus Torvalds 	.addrconf_vars = {
43511da177e4SLinus Torvalds 		{
43521da177e4SLinus Torvalds 			.procname	= "forwarding",
43531da177e4SLinus Torvalds 			.data		= &ipv6_devconf.forwarding,
43541da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
43551da177e4SLinus Torvalds 			.mode		= 0644,
43566d9f239aSAlexey Dobriyan 			.proc_handler	= addrconf_sysctl_forward,
43571da177e4SLinus Torvalds 		},
43581da177e4SLinus Torvalds 		{
43591da177e4SLinus Torvalds 			.procname	= "hop_limit",
43601da177e4SLinus Torvalds 			.data		= &ipv6_devconf.hop_limit,
43611da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
43621da177e4SLinus Torvalds 			.mode		= 0644,
43631da177e4SLinus Torvalds 			.proc_handler	= proc_dointvec,
43641da177e4SLinus Torvalds 		},
43651da177e4SLinus Torvalds 		{
43661da177e4SLinus Torvalds 			.procname	= "mtu",
43671da177e4SLinus Torvalds 			.data		= &ipv6_devconf.mtu6,
43681da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
43691da177e4SLinus Torvalds 			.mode		= 0644,
43706d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
43711da177e4SLinus Torvalds 		},
43721da177e4SLinus Torvalds 		{
43731da177e4SLinus Torvalds 			.procname	= "accept_ra",
43741da177e4SLinus Torvalds 			.data		= &ipv6_devconf.accept_ra,
43751da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
43761da177e4SLinus Torvalds 			.mode		= 0644,
43776d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
43781da177e4SLinus Torvalds 		},
43791da177e4SLinus Torvalds 		{
43801da177e4SLinus Torvalds 			.procname	= "accept_redirects",
43811da177e4SLinus Torvalds 			.data		= &ipv6_devconf.accept_redirects,
43821da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
43831da177e4SLinus Torvalds 			.mode		= 0644,
43846d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
43851da177e4SLinus Torvalds 		},
43861da177e4SLinus Torvalds 		{
43871da177e4SLinus Torvalds 			.procname	= "autoconf",
43881da177e4SLinus Torvalds 			.data		= &ipv6_devconf.autoconf,
43891da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
43901da177e4SLinus Torvalds 			.mode		= 0644,
43916d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
43921da177e4SLinus Torvalds 		},
43931da177e4SLinus Torvalds 		{
43941da177e4SLinus Torvalds 			.procname	= "dad_transmits",
43951da177e4SLinus Torvalds 			.data		= &ipv6_devconf.dad_transmits,
43961da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
43971da177e4SLinus Torvalds 			.mode		= 0644,
43986d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
43991da177e4SLinus Torvalds 		},
44001da177e4SLinus Torvalds 		{
44011da177e4SLinus Torvalds 			.procname	= "router_solicitations",
44021da177e4SLinus Torvalds 			.data		= &ipv6_devconf.rtr_solicits,
44031da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
44041da177e4SLinus Torvalds 			.mode		= 0644,
44056d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
44061da177e4SLinus Torvalds 		},
44071da177e4SLinus Torvalds 		{
44081da177e4SLinus Torvalds 			.procname	= "router_solicitation_interval",
44091da177e4SLinus Torvalds 			.data		= &ipv6_devconf.rtr_solicit_interval,
44101da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
44111da177e4SLinus Torvalds 			.mode		= 0644,
44126d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
44131da177e4SLinus Torvalds 		},
44141da177e4SLinus Torvalds 		{
44151da177e4SLinus Torvalds 			.procname	= "router_solicitation_delay",
44161da177e4SLinus Torvalds 			.data		= &ipv6_devconf.rtr_solicit_delay,
44171da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
44181da177e4SLinus Torvalds 			.mode		= 0644,
44196d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
44201da177e4SLinus Torvalds 		},
44211da177e4SLinus Torvalds 		{
44221da177e4SLinus Torvalds 			.procname	= "force_mld_version",
44231da177e4SLinus Torvalds 			.data		= &ipv6_devconf.force_mld_version,
44241da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
44251da177e4SLinus Torvalds 			.mode		= 0644,
44266d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
44271da177e4SLinus Torvalds 		},
44281da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
44291da177e4SLinus Torvalds 		{
44301da177e4SLinus Torvalds 			.procname	= "use_tempaddr",
44311da177e4SLinus Torvalds 			.data		= &ipv6_devconf.use_tempaddr,
44321da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
44331da177e4SLinus Torvalds 			.mode		= 0644,
44346d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
44351da177e4SLinus Torvalds 		},
44361da177e4SLinus Torvalds 		{
44371da177e4SLinus Torvalds 			.procname	= "temp_valid_lft",
44381da177e4SLinus Torvalds 			.data		= &ipv6_devconf.temp_valid_lft,
44391da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
44401da177e4SLinus Torvalds 			.mode		= 0644,
44416d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
44421da177e4SLinus Torvalds 		},
44431da177e4SLinus Torvalds 		{
44441da177e4SLinus Torvalds 			.procname	= "temp_prefered_lft",
44451da177e4SLinus Torvalds 			.data		= &ipv6_devconf.temp_prefered_lft,
44461da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
44471da177e4SLinus Torvalds 			.mode		= 0644,
44486d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
44491da177e4SLinus Torvalds 		},
44501da177e4SLinus Torvalds 		{
44511da177e4SLinus Torvalds 			.procname	= "regen_max_retry",
44521da177e4SLinus Torvalds 			.data		= &ipv6_devconf.regen_max_retry,
44531da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
44541da177e4SLinus Torvalds 			.mode		= 0644,
44556d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
44561da177e4SLinus Torvalds 		},
44571da177e4SLinus Torvalds 		{
44581da177e4SLinus Torvalds 			.procname	= "max_desync_factor",
44591da177e4SLinus Torvalds 			.data		= &ipv6_devconf.max_desync_factor,
44601da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
44611da177e4SLinus Torvalds 			.mode		= 0644,
44626d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
44631da177e4SLinus Torvalds 		},
44641da177e4SLinus Torvalds #endif
44651da177e4SLinus Torvalds 		{
44661da177e4SLinus Torvalds 			.procname	= "max_addresses",
44671da177e4SLinus Torvalds 			.data		= &ipv6_devconf.max_addresses,
44681da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
44691da177e4SLinus Torvalds 			.mode		= 0644,
44706d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
44711da177e4SLinus Torvalds 		},
44721da177e4SLinus Torvalds 		{
447365f5c7c1SYOSHIFUJI Hideaki 			.procname	= "accept_ra_defrtr",
447465f5c7c1SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_ra_defrtr,
447565f5c7c1SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
447665f5c7c1SYOSHIFUJI Hideaki 			.mode		= 0644,
44776d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
447865f5c7c1SYOSHIFUJI Hideaki 		},
447965f5c7c1SYOSHIFUJI Hideaki 		{
4480c4fd30ebSYOSHIFUJI Hideaki 			.procname	= "accept_ra_pinfo",
4481c4fd30ebSYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_ra_pinfo,
4482c4fd30ebSYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
4483c4fd30ebSYOSHIFUJI Hideaki 			.mode		= 0644,
44846d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
4485c4fd30ebSYOSHIFUJI Hideaki 		},
4486930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
4487930d6ff2SYOSHIFUJI Hideaki 		{
4488930d6ff2SYOSHIFUJI Hideaki 			.procname	= "accept_ra_rtr_pref",
4489930d6ff2SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_ra_rtr_pref,
4490930d6ff2SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
4491930d6ff2SYOSHIFUJI Hideaki 			.mode		= 0644,
44926d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
4493930d6ff2SYOSHIFUJI Hideaki 		},
449452e16356SYOSHIFUJI Hideaki 		{
449552e16356SYOSHIFUJI Hideaki 			.procname	= "router_probe_interval",
449652e16356SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.rtr_probe_interval,
449752e16356SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
449852e16356SYOSHIFUJI Hideaki 			.mode		= 0644,
44996d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
450052e16356SYOSHIFUJI Hideaki 		},
4501fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
450209c884d4SYOSHIFUJI Hideaki 		{
450309c884d4SYOSHIFUJI Hideaki 			.procname	= "accept_ra_rt_info_max_plen",
450409c884d4SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_ra_rt_info_max_plen,
450509c884d4SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
450609c884d4SYOSHIFUJI Hideaki 			.mode		= 0644,
45076d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
450809c884d4SYOSHIFUJI Hideaki 		},
450909c884d4SYOSHIFUJI Hideaki #endif
4510930d6ff2SYOSHIFUJI Hideaki #endif
4511c4fd30ebSYOSHIFUJI Hideaki 		{
4512fbea49e1SYOSHIFUJI Hideaki 			.procname	= "proxy_ndp",
4513fbea49e1SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.proxy_ndp,
4514fbea49e1SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
4515fbea49e1SYOSHIFUJI Hideaki 			.mode		= 0644,
45166d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
4517fbea49e1SYOSHIFUJI Hideaki 		},
4518fbea49e1SYOSHIFUJI Hideaki 		{
45190bcbc926SYOSHIFUJI Hideaki 			.procname	= "accept_source_route",
45200bcbc926SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_source_route,
45210bcbc926SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
45220bcbc926SYOSHIFUJI Hideaki 			.mode		= 0644,
45236d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
45240bcbc926SYOSHIFUJI Hideaki 		},
452595c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
452695c385b4SNeil Horman 		{
452795c385b4SNeil Horman 			.procname       = "optimistic_dad",
452895c385b4SNeil Horman 			.data           = &ipv6_devconf.optimistic_dad,
452995c385b4SNeil Horman 			.maxlen         = sizeof(int),
453095c385b4SNeil Horman 			.mode           = 0644,
45316d9f239aSAlexey Dobriyan 			.proc_handler   = proc_dointvec,
453295c385b4SNeil Horman 
453395c385b4SNeil Horman 		},
453495c385b4SNeil Horman #endif
45357bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
45367bc570c8SYOSHIFUJI Hideaki 		{
45377bc570c8SYOSHIFUJI Hideaki 			.procname	= "mc_forwarding",
45387bc570c8SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.mc_forwarding,
45397bc570c8SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
4540a4e6db07SDavid S. Miller 			.mode		= 0444,
45416d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
45427bc570c8SYOSHIFUJI Hideaki 		},
45437bc570c8SYOSHIFUJI Hideaki #endif
45440bcbc926SYOSHIFUJI Hideaki 		{
4545778d80beSYOSHIFUJI Hideaki 			.procname	= "disable_ipv6",
4546778d80beSYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.disable_ipv6,
4547778d80beSYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
4548778d80beSYOSHIFUJI Hideaki 			.mode		= 0644,
454956d417b1SBrian Haley 			.proc_handler	= addrconf_sysctl_disable,
4550778d80beSYOSHIFUJI Hideaki 		},
4551778d80beSYOSHIFUJI Hideaki 		{
45521b34be74SYOSHIFUJI Hideaki 			.procname	= "accept_dad",
45531b34be74SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_dad,
45541b34be74SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
45551b34be74SYOSHIFUJI Hideaki 			.mode		= 0644,
45566d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
45571b34be74SYOSHIFUJI Hideaki 		},
45581b34be74SYOSHIFUJI Hideaki 		{
4559f7734fdfSOctavian Purdila 			.procname       = "force_tllao",
4560f7734fdfSOctavian Purdila 			.data           = &ipv6_devconf.force_tllao,
4561f7734fdfSOctavian Purdila 			.maxlen         = sizeof(int),
4562f7734fdfSOctavian Purdila 			.mode           = 0644,
4563f7734fdfSOctavian Purdila 			.proc_handler   = proc_dointvec
4564f7734fdfSOctavian Purdila 		},
4565f7734fdfSOctavian Purdila 		{
4566f8572d8fSEric W. Biederman 			/* sentinel */
45671da177e4SLinus Torvalds 		}
45681da177e4SLinus Torvalds 	},
45691da177e4SLinus Torvalds };
45701da177e4SLinus Torvalds 
4571bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name,
4572f8572d8fSEric W. Biederman 		struct inet6_dev *idev, struct ipv6_devconf *p)
45731da177e4SLinus Torvalds {
45741da177e4SLinus Torvalds 	int i;
45751da177e4SLinus Torvalds 	struct addrconf_sysctl_table *t;
45761da177e4SLinus Torvalds 
45771dab6222SPavel Emelyanov #define ADDRCONF_CTL_PATH_DEV	3
45781dab6222SPavel Emelyanov 
45791dab6222SPavel Emelyanov 	struct ctl_path addrconf_ctl_path[] = {
4580f8572d8fSEric W. Biederman 		{ .procname = "net", },
4581f8572d8fSEric W. Biederman 		{ .procname = "ipv6", },
4582f8572d8fSEric W. Biederman 		{ .procname = "conf", },
45831dab6222SPavel Emelyanov 		{ /* to be set */ },
45841dab6222SPavel Emelyanov 		{ },
45851dab6222SPavel Emelyanov 	};
45861dab6222SPavel Emelyanov 
45871dab6222SPavel Emelyanov 
4588af879cc7SArnaldo Carvalho de Melo 	t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
45891da177e4SLinus Torvalds 	if (t == NULL)
4590f68635e6SPavel Emelyanov 		goto out;
4591f68635e6SPavel Emelyanov 
45921da177e4SLinus Torvalds 	for (i = 0; t->addrconf_vars[i].data; i++) {
45931da177e4SLinus Torvalds 		t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf;
45941da177e4SLinus Torvalds 		t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
4595bff16c2fSPavel Emelyanov 		t->addrconf_vars[i].extra2 = net;
45961da177e4SLinus Torvalds 	}
45971da177e4SLinus Torvalds 
45981da177e4SLinus Torvalds 	/*
45991da177e4SLinus Torvalds 	 * Make a copy of dev_name, because '.procname' is regarded as const
46001da177e4SLinus Torvalds 	 * by sysctl and we wouldn't want anyone to change it under our feet
46011da177e4SLinus Torvalds 	 * (see SIOCSIFNAME).
46021da177e4SLinus Torvalds 	 */
46031dab6222SPavel Emelyanov 	t->dev_name = kstrdup(dev_name, GFP_KERNEL);
46041dab6222SPavel Emelyanov 	if (!t->dev_name)
46051da177e4SLinus Torvalds 		goto free;
46061da177e4SLinus Torvalds 
46071dab6222SPavel Emelyanov 	addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].procname = t->dev_name;
46081da177e4SLinus Torvalds 
4609bff16c2fSPavel Emelyanov 	t->sysctl_header = register_net_sysctl_table(net, addrconf_ctl_path,
46101dab6222SPavel Emelyanov 			t->addrconf_vars);
46111da177e4SLinus Torvalds 	if (t->sysctl_header == NULL)
46121da177e4SLinus Torvalds 		goto free_procname;
4613f68635e6SPavel Emelyanov 
46141da177e4SLinus Torvalds 	p->sysctl = t;
461595897312SPavel Emelyanov 	return 0;
46161da177e4SLinus Torvalds 
46171da177e4SLinus Torvalds free_procname:
46181dab6222SPavel Emelyanov 	kfree(t->dev_name);
46191da177e4SLinus Torvalds free:
46201da177e4SLinus Torvalds 	kfree(t);
4621f68635e6SPavel Emelyanov out:
462295897312SPavel Emelyanov 	return -ENOBUFS;
46231da177e4SLinus Torvalds }
46241da177e4SLinus Torvalds 
4625408c4768SPavel Emelyanov static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
4626f52295a9SPavel Emelyanov {
4627408c4768SPavel Emelyanov 	struct addrconf_sysctl_table *t;
4628f52295a9SPavel Emelyanov 
4629408c4768SPavel Emelyanov 	if (p->sysctl == NULL)
4630408c4768SPavel Emelyanov 		return;
4631408c4768SPavel Emelyanov 
4632408c4768SPavel Emelyanov 	t = p->sysctl;
46331da177e4SLinus Torvalds 	p->sysctl = NULL;
4634ff538818SLucian Adrian Grijincu 	unregister_net_sysctl_table(t->sysctl_header);
46351dab6222SPavel Emelyanov 	kfree(t->dev_name);
46361da177e4SLinus Torvalds 	kfree(t);
46371da177e4SLinus Torvalds }
4638408c4768SPavel Emelyanov 
4639408c4768SPavel Emelyanov static void addrconf_sysctl_register(struct inet6_dev *idev)
4640408c4768SPavel Emelyanov {
464154716e3bSEric W. Biederman 	neigh_sysctl_register(idev->dev, idev->nd_parms, "ipv6",
4642f8572d8fSEric W. Biederman 			      &ndisc_ifinfo_sysctl_change);
4643c346dca1SYOSHIFUJI Hideaki 	__addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
4644f8572d8fSEric W. Biederman 					idev, &idev->cnf);
4645408c4768SPavel Emelyanov }
4646408c4768SPavel Emelyanov 
4647408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev)
4648408c4768SPavel Emelyanov {
4649408c4768SPavel Emelyanov 	__addrconf_sysctl_unregister(&idev->cnf);
4650408c4768SPavel Emelyanov 	neigh_sysctl_unregister(idev->nd_parms);
46511da177e4SLinus Torvalds }
46521da177e4SLinus Torvalds 
46531da177e4SLinus Torvalds 
46541da177e4SLinus Torvalds #endif
46551da177e4SLinus Torvalds 
46562c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net)
4657e0da5a48SPavel Emelyanov {
4658e0da5a48SPavel Emelyanov 	int err;
4659e0da5a48SPavel Emelyanov 	struct ipv6_devconf *all, *dflt;
4660e0da5a48SPavel Emelyanov 
4661e0da5a48SPavel Emelyanov 	err = -ENOMEM;
4662e0da5a48SPavel Emelyanov 	all = &ipv6_devconf;
4663e0da5a48SPavel Emelyanov 	dflt = &ipv6_devconf_dflt;
4664e0da5a48SPavel Emelyanov 
466509ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net)) {
4666e0da5a48SPavel Emelyanov 		all = kmemdup(all, sizeof(ipv6_devconf), GFP_KERNEL);
4667e0da5a48SPavel Emelyanov 		if (all == NULL)
4668e0da5a48SPavel Emelyanov 			goto err_alloc_all;
4669e0da5a48SPavel Emelyanov 
4670e0da5a48SPavel Emelyanov 		dflt = kmemdup(dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
4671e0da5a48SPavel Emelyanov 		if (dflt == NULL)
4672e0da5a48SPavel Emelyanov 			goto err_alloc_dflt;
467356d417b1SBrian Haley 	} else {
467456d417b1SBrian Haley 		/* these will be inherited by all namespaces */
467556d417b1SBrian Haley 		dflt->autoconf = ipv6_defaults.autoconf;
467656d417b1SBrian Haley 		dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
4677e0da5a48SPavel Emelyanov 	}
4678e0da5a48SPavel Emelyanov 
4679e0da5a48SPavel Emelyanov 	net->ipv6.devconf_all = all;
4680e0da5a48SPavel Emelyanov 	net->ipv6.devconf_dflt = dflt;
4681e0da5a48SPavel Emelyanov 
4682e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
4683f8572d8fSEric W. Biederman 	err = __addrconf_sysctl_register(net, "all", NULL, all);
4684e0da5a48SPavel Emelyanov 	if (err < 0)
4685e0da5a48SPavel Emelyanov 		goto err_reg_all;
4686e0da5a48SPavel Emelyanov 
4687f8572d8fSEric W. Biederman 	err = __addrconf_sysctl_register(net, "default", NULL, dflt);
4688e0da5a48SPavel Emelyanov 	if (err < 0)
4689e0da5a48SPavel Emelyanov 		goto err_reg_dflt;
4690e0da5a48SPavel Emelyanov #endif
4691e0da5a48SPavel Emelyanov 	return 0;
4692e0da5a48SPavel Emelyanov 
4693e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
4694e0da5a48SPavel Emelyanov err_reg_dflt:
4695e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(all);
4696e0da5a48SPavel Emelyanov err_reg_all:
4697e0da5a48SPavel Emelyanov 	kfree(dflt);
4698e0da5a48SPavel Emelyanov #endif
4699e0da5a48SPavel Emelyanov err_alloc_dflt:
4700e0da5a48SPavel Emelyanov 	kfree(all);
4701e0da5a48SPavel Emelyanov err_alloc_all:
4702e0da5a48SPavel Emelyanov 	return err;
4703e0da5a48SPavel Emelyanov }
4704e0da5a48SPavel Emelyanov 
47052c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net)
4706e0da5a48SPavel Emelyanov {
4707e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
4708e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
4709e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(net->ipv6.devconf_all);
4710e0da5a48SPavel Emelyanov #endif
471109ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net)) {
4712e0da5a48SPavel Emelyanov 		kfree(net->ipv6.devconf_dflt);
4713e0da5a48SPavel Emelyanov 		kfree(net->ipv6.devconf_all);
4714e0da5a48SPavel Emelyanov 	}
4715e0da5a48SPavel Emelyanov }
4716e0da5a48SPavel Emelyanov 
4717e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = {
4718e0da5a48SPavel Emelyanov 	.init = addrconf_init_net,
4719e0da5a48SPavel Emelyanov 	.exit = addrconf_exit_net,
4720e0da5a48SPavel Emelyanov };
4721e0da5a48SPavel Emelyanov 
47221da177e4SLinus Torvalds /*
47231da177e4SLinus Torvalds  *      Device notifier
47241da177e4SLinus Torvalds  */
47251da177e4SLinus Torvalds 
47261da177e4SLinus Torvalds int register_inet6addr_notifier(struct notifier_block *nb)
47271da177e4SLinus Torvalds {
4728e041c683SAlan Stern 	return atomic_notifier_chain_register(&inet6addr_chain, nb);
47291da177e4SLinus Torvalds }
47307159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(register_inet6addr_notifier);
47317159039aSYOSHIFUJI Hideaki 
47321da177e4SLinus Torvalds int unregister_inet6addr_notifier(struct notifier_block *nb)
47331da177e4SLinus Torvalds {
4734e041c683SAlan Stern 	return atomic_notifier_chain_unregister(&inet6addr_chain, nb);
47351da177e4SLinus Torvalds }
47367159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(unregister_inet6addr_notifier);
47377159039aSYOSHIFUJI Hideaki 
4738b382b191SThomas Graf static struct rtnl_af_ops inet6_ops = {
4739b382b191SThomas Graf 	.family		  = AF_INET6,
4740b382b191SThomas Graf 	.fill_link_af	  = inet6_fill_link_af,
4741b382b191SThomas Graf 	.get_link_af_size = inet6_get_link_af_size,
4742b382b191SThomas Graf };
4743b382b191SThomas Graf 
47441da177e4SLinus Torvalds /*
47451da177e4SLinus Torvalds  *	Init / cleanup code
47461da177e4SLinus Torvalds  */
47471da177e4SLinus Torvalds 
47481da177e4SLinus Torvalds int __init addrconf_init(void)
47491da177e4SLinus Torvalds {
4750c2e21293Sstephen hemminger 	int i, err;
47512a8cc6c8SYOSHIFUJI Hideaki 
4752e21e8467SStephen Hemminger 	err = ipv6_addr_label_init();
4753e21e8467SStephen Hemminger 	if (err < 0) {
4754e21e8467SStephen Hemminger 		printk(KERN_CRIT "IPv6 Addrconf:"
4755e21e8467SStephen Hemminger 		       " cannot initialize default policy table: %d.\n", err);
47562cc6d2bfSNeil Horman 		goto out;
47572a8cc6c8SYOSHIFUJI Hideaki 	}
47581da177e4SLinus Torvalds 
47592cc6d2bfSNeil Horman 	err = register_pernet_subsys(&addrconf_ops);
47602cc6d2bfSNeil Horman 	if (err < 0)
47612cc6d2bfSNeil Horman 		goto out_addrlabel;
4762e0da5a48SPavel Emelyanov 
47631da177e4SLinus Torvalds 	/* The addrconf netdev notifier requires that loopback_dev
47641da177e4SLinus Torvalds 	 * has it's ipv6 private information allocated and setup
47651da177e4SLinus Torvalds 	 * before it can bring up and give link-local addresses
47661da177e4SLinus Torvalds 	 * to other devices which are up.
47671da177e4SLinus Torvalds 	 *
47681da177e4SLinus Torvalds 	 * Unfortunately, loopback_dev is not necessarily the first
47691da177e4SLinus Torvalds 	 * entry in the global dev_base list of net devices.  In fact,
47701da177e4SLinus Torvalds 	 * it is likely to be the very last entry on that list.
47711da177e4SLinus Torvalds 	 * So this causes the notifier registry below to try and
47721da177e4SLinus Torvalds 	 * give link-local addresses to all devices besides loopback_dev
47731da177e4SLinus Torvalds 	 * first, then loopback_dev, which cases all the non-loopback_dev
47741da177e4SLinus Torvalds 	 * devices to fail to get a link-local address.
47751da177e4SLinus Torvalds 	 *
47761da177e4SLinus Torvalds 	 * So, as a temporary fix, allocate the ipv6 structure for
47771da177e4SLinus Torvalds 	 * loopback_dev first by hand.
47781da177e4SLinus Torvalds 	 * Longer term, all of the dependencies ipv6 has upon the loopback
47791da177e4SLinus Torvalds 	 * device and it being up should be removed.
47801da177e4SLinus Torvalds 	 */
47811da177e4SLinus Torvalds 	rtnl_lock();
47822774c7abSEric W. Biederman 	if (!ipv6_add_dev(init_net.loopback_dev))
47831da177e4SLinus Torvalds 		err = -ENOMEM;
47841da177e4SLinus Torvalds 	rtnl_unlock();
47851da177e4SLinus Torvalds 	if (err)
4786e0da5a48SPavel Emelyanov 		goto errlo;
47871da177e4SLinus Torvalds 
4788c2e21293Sstephen hemminger 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
4789c2e21293Sstephen hemminger 		INIT_HLIST_HEAD(&inet6_addr_lst[i]);
4790c2e21293Sstephen hemminger 
47911da177e4SLinus Torvalds 	register_netdevice_notifier(&ipv6_dev_notf);
47921da177e4SLinus Torvalds 
47931da177e4SLinus Torvalds 	addrconf_verify(0);
4794c127ea2cSThomas Graf 
4795b382b191SThomas Graf 	err = rtnl_af_register(&inet6_ops);
4796b382b191SThomas Graf 	if (err < 0)
4797b382b191SThomas Graf 		goto errout_af;
4798b382b191SThomas Graf 
4799c7ac8679SGreg Rose 	err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
4800c7ac8679SGreg Rose 			      NULL);
4801c127ea2cSThomas Graf 	if (err < 0)
4802c127ea2cSThomas Graf 		goto errout;
4803c127ea2cSThomas Graf 
4804c127ea2cSThomas Graf 	/* Only the first call to __rtnl_register can fail */
4805c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
4806c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
4807c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
4808c7ac8679SGreg Rose 			inet6_dump_ifaddr, NULL);
4809c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
4810c7ac8679SGreg Rose 			inet6_dump_ifmcaddr, NULL);
4811c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
4812c7ac8679SGreg Rose 			inet6_dump_ifacaddr, NULL);
4813c127ea2cSThomas Graf 
48142a8cc6c8SYOSHIFUJI Hideaki 	ipv6_addr_label_rtnl_register();
48152a8cc6c8SYOSHIFUJI Hideaki 
48161da177e4SLinus Torvalds 	return 0;
4817c127ea2cSThomas Graf errout:
4818b382b191SThomas Graf 	rtnl_af_unregister(&inet6_ops);
4819b382b191SThomas Graf errout_af:
4820c127ea2cSThomas Graf 	unregister_netdevice_notifier(&ipv6_dev_notf);
4821e0da5a48SPavel Emelyanov errlo:
4822e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
48232cc6d2bfSNeil Horman out_addrlabel:
48242cc6d2bfSNeil Horman 	ipv6_addr_label_cleanup();
48252cc6d2bfSNeil Horman out:
4826c127ea2cSThomas Graf 	return err;
48271da177e4SLinus Torvalds }
48281da177e4SLinus Torvalds 
482909f7709fSDaniel Lezcano void addrconf_cleanup(void)
48301da177e4SLinus Torvalds {
4831176c39afSDaniel Lezcano 	struct net_device *dev;
48321da177e4SLinus Torvalds 	int i;
48331da177e4SLinus Torvalds 
48341da177e4SLinus Torvalds 	unregister_netdevice_notifier(&ipv6_dev_notf);
4835e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
48362cc6d2bfSNeil Horman 	ipv6_addr_label_cleanup();
48371da177e4SLinus Torvalds 
48381da177e4SLinus Torvalds 	rtnl_lock();
48391da177e4SLinus Torvalds 
4840b382b191SThomas Graf 	__rtnl_af_unregister(&inet6_ops);
4841b382b191SThomas Graf 
4842176c39afSDaniel Lezcano 	/* clean dev list */
4843176c39afSDaniel Lezcano 	for_each_netdev(&init_net, dev) {
4844176c39afSDaniel Lezcano 		if (__in6_dev_get(dev) == NULL)
4845176c39afSDaniel Lezcano 			continue;
4846176c39afSDaniel Lezcano 		addrconf_ifdown(dev, 1);
4847176c39afSDaniel Lezcano 	}
4848176c39afSDaniel Lezcano 	addrconf_ifdown(init_net.loopback_dev, 2);
4849176c39afSDaniel Lezcano 
48501da177e4SLinus Torvalds 	/*
48511da177e4SLinus Torvalds 	 *	Check hash table.
48521da177e4SLinus Torvalds 	 */
48535c578aedSstephen hemminger 	spin_lock_bh(&addrconf_hash_lock);
4854c2e21293Sstephen hemminger 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
4855c2e21293Sstephen hemminger 		WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
48565c578aedSstephen hemminger 	spin_unlock_bh(&addrconf_hash_lock);
48571da177e4SLinus Torvalds 
48581da177e4SLinus Torvalds 	del_timer(&addr_chk_timer);
48591da177e4SLinus Torvalds 	rtnl_unlock();
48601da177e4SLinus Torvalds }
4861