xref: /openbmc/linux/net/ipv6/addrconf.c (revision fe2c6338fd2c6f383c4d4164262f35c8f3708e1f)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	IPv6 Address [auto]configuration
31da177e4SLinus Torvalds  *	Linux INET6 implementation
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *	Authors:
61da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
71da177e4SLinus Torvalds  *	Alexey Kuznetsov	<kuznet@ms2.inr.ac.ru>
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
101da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
111da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
121da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds /*
161da177e4SLinus Torvalds  *	Changes:
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  *	Janos Farkas			:	delete timer on ifdown
191da177e4SLinus Torvalds  *	<chexum@bankinf.banki.hu>
201da177e4SLinus Torvalds  *	Andi Kleen			:	kill double kfree on module
211da177e4SLinus Torvalds  *						unload.
221da177e4SLinus Torvalds  *	Maciej W. Rozycki		:	FDDI support
231da177e4SLinus Torvalds  *	sekiya@USAGI			:	Don't send too many RS
241da177e4SLinus Torvalds  *						packets.
251da177e4SLinus Torvalds  *	yoshfuji@USAGI			:       Fixed interval between DAD
261da177e4SLinus Torvalds  *						packets.
271da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	improved accuracy of
281da177e4SLinus Torvalds  *						address validation timer.
291da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	Privacy Extensions (RFC3041)
301da177e4SLinus Torvalds  *						support.
311da177e4SLinus Torvalds  *	Yuji SEKIYA @USAGI		:	Don't assign a same IPv6
321da177e4SLinus Torvalds  *						address on a same interface.
331da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	ARCnet support
341da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI	:	convert /proc/net/if_inet6 to
351da177e4SLinus Torvalds  *						seq_file.
36b1cacb68SYOSHIFUJI Hideaki  *	YOSHIFUJI Hideaki @USAGI	:	improved source address
37b1cacb68SYOSHIFUJI Hideaki  *						selection; consider scope,
38b1cacb68SYOSHIFUJI Hideaki  *						status etc.
391da177e4SLinus Torvalds  */
401da177e4SLinus Torvalds 
41f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt
42f3213831SJoe Perches 
431da177e4SLinus Torvalds #include <linux/errno.h>
441da177e4SLinus Torvalds #include <linux/types.h>
45a0bffffcSIlpo Järvinen #include <linux/kernel.h>
461da177e4SLinus Torvalds #include <linux/socket.h>
471da177e4SLinus Torvalds #include <linux/sockios.h>
481da177e4SLinus Torvalds #include <linux/net.h>
491da177e4SLinus Torvalds #include <linux/in6.h>
501da177e4SLinus Torvalds #include <linux/netdevice.h>
511823730fSThomas Graf #include <linux/if_addr.h>
521da177e4SLinus Torvalds #include <linux/if_arp.h>
531da177e4SLinus Torvalds #include <linux/if_arcnet.h>
541da177e4SLinus Torvalds #include <linux/if_infiniband.h>
551da177e4SLinus Torvalds #include <linux/route.h>
561da177e4SLinus Torvalds #include <linux/inetdevice.h>
571da177e4SLinus Torvalds #include <linux/init.h>
585a0e3ad6STejun Heo #include <linux/slab.h>
591da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
601da177e4SLinus Torvalds #include <linux/sysctl.h>
611da177e4SLinus Torvalds #endif
624fc268d2SRandy Dunlap #include <linux/capability.h>
631da177e4SLinus Torvalds #include <linux/delay.h>
641da177e4SLinus Torvalds #include <linux/notifier.h>
65543537bdSPaulo Marques #include <linux/string.h>
66ddbe5032SEric Dumazet #include <linux/hash.h>
671da177e4SLinus Torvalds 
68457c4cbcSEric W. Biederman #include <net/net_namespace.h>
691da177e4SLinus Torvalds #include <net/sock.h>
701da177e4SLinus Torvalds #include <net/snmp.h>
711da177e4SLinus Torvalds 
7206a4c1c5Salex.bluesman.smirnov@gmail.com #include <net/af_ieee802154.h>
73cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 #include <net/firewire.h>
741da177e4SLinus Torvalds #include <net/ipv6.h>
751da177e4SLinus Torvalds #include <net/protocol.h>
761da177e4SLinus Torvalds #include <net/ndisc.h>
771da177e4SLinus Torvalds #include <net/ip6_route.h>
781da177e4SLinus Torvalds #include <net/addrconf.h>
791da177e4SLinus Torvalds #include <net/tcp.h>
801da177e4SLinus Torvalds #include <net/ip.h>
815d620266SThomas Graf #include <net/netlink.h>
82f24e3d65SMitsuru Chinen #include <net/pkt_sched.h>
831da177e4SLinus Torvalds #include <linux/if_tunnel.h>
841da177e4SLinus Torvalds #include <linux/rtnetlink.h>
85f3a1bfb1SNicolas Dichtel #include <linux/netconf.h>
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
881da177e4SLinus Torvalds #include <linux/random.h>
891da177e4SLinus Torvalds #endif
901da177e4SLinus Torvalds 
91e21e8467SStephen Hemminger #include <linux/uaccess.h>
927f7d9a6bSHerbert Xu #include <asm/unaligned.h>
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds #include <linux/proc_fs.h>
951da177e4SLinus Torvalds #include <linux/seq_file.h>
96bc3b2d7fSPaul Gortmaker #include <linux/export.h>
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds /* Set to 3 to get tracing... */
991da177e4SLinus Torvalds #define ACONF_DEBUG 2
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds #if ACONF_DEBUG >= 3
1021da177e4SLinus Torvalds #define ADBG(x) printk x
1031da177e4SLinus Torvalds #else
1041da177e4SLinus Torvalds #define ADBG(x)
1051da177e4SLinus Torvalds #endif
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds #define	INFINITY_LIFE_TIME	0xFFFFFFFF
10818a31e1eSThomas Graf 
10918a31e1eSThomas Graf static inline u32 cstamp_delta(unsigned long cstamp)
11018a31e1eSThomas Graf {
11118a31e1eSThomas Graf 	return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
11218a31e1eSThomas Graf }
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
115f52295a9SPavel Emelyanov static void addrconf_sysctl_register(struct inet6_dev *idev);
116408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev);
117408c4768SPavel Emelyanov #else
118408c4768SPavel Emelyanov static inline void addrconf_sysctl_register(struct inet6_dev *idev)
119408c4768SPavel Emelyanov {
120408c4768SPavel Emelyanov }
121408c4768SPavel Emelyanov 
122408c4768SPavel Emelyanov static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
123408c4768SPavel Emelyanov {
124408c4768SPavel Emelyanov }
1251da177e4SLinus Torvalds #endif
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
128eb7e0575SSorin Dumitru static void __ipv6_regen_rndid(struct inet6_dev *idev);
129eb7e0575SSorin Dumitru static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
1301da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data);
1311da177e4SLinus Torvalds #endif
1321da177e4SLinus Torvalds 
1331b34be74SYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
1341da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev);
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds /*
1371da177e4SLinus Torvalds  *	Configured unicast address hash table
1381da177e4SLinus Torvalds  */
139c2e21293Sstephen hemminger static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
1405c578aedSstephen hemminger static DEFINE_SPINLOCK(addrconf_hash_lock);
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds static void addrconf_verify(unsigned long);
1431da177e4SLinus Torvalds 
1448d06afabSIngo Molnar static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
1451da177e4SLinus Torvalds static DEFINE_SPINLOCK(addrconf_verify_lock);
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
1481da177e4SLinus Torvalds static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
1491da177e4SLinus Torvalds 
15093d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev,
15175c78500SMoni Shoua 				 unsigned long event);
1521da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how);
1531da177e4SLinus Torvalds 
15421caa662SRomain Kuntz static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
15521caa662SRomain Kuntz 						  int plen,
15621caa662SRomain Kuntz 						  const struct net_device *dev,
15721caa662SRomain Kuntz 						  u32 flags, u32 noflags);
15821caa662SRomain Kuntz 
159cf22f9a2SDavid S. Miller static void addrconf_dad_start(struct inet6_ifaddr *ifp);
1601da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data);
1611da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
162c5e33bddSYOSHIFUJI Hideaki static void addrconf_dad_run(struct inet6_dev *idev);
1631da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data);
1641da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1651da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
1681da177e4SLinus Torvalds 				struct prefix_info *pinfo);
1693e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
17006bfe655SDaniel Lezcano 			       struct net_device *dev);
1711da177e4SLinus Torvalds 
172888c848eSAdrian Bunk static struct ipv6_devconf ipv6_devconf __read_mostly = {
1731da177e4SLinus Torvalds 	.forwarding		= 0,
1741da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
1751da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
1761da177e4SLinus Torvalds 	.accept_ra		= 1,
1771da177e4SLinus Torvalds 	.accept_redirects	= 1,
1781da177e4SLinus Torvalds 	.autoconf		= 1,
1791da177e4SLinus Torvalds 	.force_mld_version	= 0,
1801da177e4SLinus Torvalds 	.dad_transmits		= 1,
1811da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
1821da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
1831da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
1841da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
1851da177e4SLinus Torvalds 	.use_tempaddr 		= 0,
1861da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
1871da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
1881da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
1891da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
1901da177e4SLinus Torvalds #endif
1911da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
19265f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
193c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
194930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
195930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
19652e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
19709c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
19809c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
19909c884d4SYOSHIFUJI Hideaki #endif
200930d6ff2SYOSHIFUJI Hideaki #endif
201fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2020bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
203778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
2041b34be74SYOSHIFUJI Hideaki 	.accept_dad		= 1,
2051da177e4SLinus Torvalds };
2061da177e4SLinus Torvalds 
207ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
2081da177e4SLinus Torvalds 	.forwarding		= 0,
2091da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
2101da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
2111da177e4SLinus Torvalds 	.accept_ra		= 1,
2121da177e4SLinus Torvalds 	.accept_redirects	= 1,
2131da177e4SLinus Torvalds 	.autoconf		= 1,
2141da177e4SLinus Torvalds 	.dad_transmits		= 1,
2151da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
2161da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
2171da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
2181da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
2191da177e4SLinus Torvalds 	.use_tempaddr		= 0,
2201da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
2211da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
2221da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
2231da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
2241da177e4SLinus Torvalds #endif
2251da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
22665f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
227c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
228930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
229930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
23052e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
23109c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
23209c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
23309c884d4SYOSHIFUJI Hideaki #endif
234930d6ff2SYOSHIFUJI Hideaki #endif
235fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2360bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
237778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
2381b34be74SYOSHIFUJI Hideaki 	.accept_dad		= 1,
2391da177e4SLinus Torvalds };
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
2421da177e4SLinus Torvalds const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
2431da177e4SLinus Torvalds const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
244f3ee4010SYOSHIFUJI Hideaki const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
245f3ee4010SYOSHIFUJI Hideaki const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
2462c5e8933SHannes Frederic Sowa const struct in6_addr in6addr_interfacelocal_allnodes = IN6ADDR_INTERFACELOCAL_ALLNODES_INIT;
2472c5e8933SHannes Frederic Sowa const struct in6_addr in6addr_interfacelocal_allrouters = IN6ADDR_INTERFACELOCAL_ALLROUTERS_INIT;
2482c5e8933SHannes Frederic Sowa const struct in6_addr in6addr_sitelocal_allrouters = IN6ADDR_SITELOCAL_ALLROUTERS_INIT;
2491da177e4SLinus Torvalds 
250f24e3d65SMitsuru Chinen /* Check if a valid qdisc is available */
25105297949SDavid S. Miller static inline bool addrconf_qdisc_ok(const struct net_device *dev)
252f24e3d65SMitsuru Chinen {
25305297949SDavid S. Miller 	return !qdisc_tx_is_noop(dev);
254f24e3d65SMitsuru Chinen }
255f24e3d65SMitsuru Chinen 
2561da177e4SLinus Torvalds static void addrconf_del_timer(struct inet6_ifaddr *ifp)
2571da177e4SLinus Torvalds {
2581da177e4SLinus Torvalds 	if (del_timer(&ifp->timer))
2591da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds 
262e21e8467SStephen Hemminger enum addrconf_timer_t {
2631da177e4SLinus Torvalds 	AC_NONE,
2641da177e4SLinus Torvalds 	AC_DAD,
2651da177e4SLinus Torvalds 	AC_RS,
2661da177e4SLinus Torvalds };
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
2691da177e4SLinus Torvalds 			       enum addrconf_timer_t what,
2701da177e4SLinus Torvalds 			       unsigned long when)
2711da177e4SLinus Torvalds {
2721da177e4SLinus Torvalds 	if (!del_timer(&ifp->timer))
2731da177e4SLinus Torvalds 		in6_ifa_hold(ifp);
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	switch (what) {
2761da177e4SLinus Torvalds 	case AC_DAD:
2771da177e4SLinus Torvalds 		ifp->timer.function = addrconf_dad_timer;
2781da177e4SLinus Torvalds 		break;
2791da177e4SLinus Torvalds 	case AC_RS:
2801da177e4SLinus Torvalds 		ifp->timer.function = addrconf_rs_timer;
2811da177e4SLinus Torvalds 		break;
282e21e8467SStephen Hemminger 	default:
283e21e8467SStephen Hemminger 		break;
2841da177e4SLinus Torvalds 	}
2851da177e4SLinus Torvalds 	ifp->timer.expires = jiffies + when;
2861da177e4SLinus Torvalds 	add_timer(&ifp->timer);
2871da177e4SLinus Torvalds }
2881da177e4SLinus Torvalds 
2897f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev)
2907f7d9a6bSHerbert Xu {
2917d720c3eSTejun Heo 	if (snmp_mib_init((void __percpu **)idev->stats.ipv6,
2921823e4c8SEric Dumazet 			  sizeof(struct ipstats_mib),
2931823e4c8SEric Dumazet 			  __alignof__(struct ipstats_mib)) < 0)
2947f7d9a6bSHerbert Xu 		goto err_ip;
295be281e55SEric Dumazet 	idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
296be281e55SEric Dumazet 					GFP_KERNEL);
297be281e55SEric Dumazet 	if (!idev->stats.icmpv6dev)
2987f7d9a6bSHerbert Xu 		goto err_icmp;
299be281e55SEric Dumazet 	idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
300be281e55SEric Dumazet 					   GFP_KERNEL);
301be281e55SEric Dumazet 	if (!idev->stats.icmpv6msgdev)
30214878f75SDavid L Stevens 		goto err_icmpmsg;
3037f7d9a6bSHerbert Xu 
3047f7d9a6bSHerbert Xu 	return 0;
3057f7d9a6bSHerbert Xu 
30614878f75SDavid L Stevens err_icmpmsg:
307be281e55SEric Dumazet 	kfree(idev->stats.icmpv6dev);
3087f7d9a6bSHerbert Xu err_icmp:
3097d720c3eSTejun Heo 	snmp_mib_free((void __percpu **)idev->stats.ipv6);
3107f7d9a6bSHerbert Xu err_ip:
311aaf70ec7SPavel Emelyanov 	return -ENOMEM;
3127f7d9a6bSHerbert Xu }
3137f7d9a6bSHerbert Xu 
31416910b98SPavel Emelyanov static void snmp6_free_dev(struct inet6_dev *idev)
3157f7d9a6bSHerbert Xu {
316be281e55SEric Dumazet 	kfree(idev->stats.icmpv6msgdev);
317be281e55SEric Dumazet 	kfree(idev->stats.icmpv6dev);
3187d720c3eSTejun Heo 	snmp_mib_free((void __percpu **)idev->stats.ipv6);
3197f7d9a6bSHerbert Xu }
3207f7d9a6bSHerbert Xu 
3211da177e4SLinus Torvalds /* Nobody refers to this device, we may destroy it. */
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds void in6_dev_finish_destroy(struct inet6_dev *idev)
3241da177e4SLinus Torvalds {
3251da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
326547b792cSIlpo Järvinen 
327502a2ffdSstephen hemminger 	WARN_ON(!list_empty(&idev->addr_list));
328547b792cSIlpo Järvinen 	WARN_ON(idev->mc_list != NULL);
329547b792cSIlpo Järvinen 
3301da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
33191df42beSJoe Perches 	pr_debug("%s: %s\n", __func__, dev ? dev->name : "NIL");
3321da177e4SLinus Torvalds #endif
3331da177e4SLinus Torvalds 	dev_put(dev);
3341da177e4SLinus Torvalds 	if (!idev->dead) {
335f3213831SJoe Perches 		pr_warn("Freeing alive inet6 device %p\n", idev);
3361da177e4SLinus Torvalds 		return;
3371da177e4SLinus Torvalds 	}
3381da177e4SLinus Torvalds 	snmp6_free_dev(idev);
33938f57d1aSLai Jiangshan 	kfree_rcu(idev, rcu);
3401da177e4SLinus Torvalds }
3417159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(in6_dev_finish_destroy);
3427159039aSYOSHIFUJI Hideaki 
3431da177e4SLinus Torvalds static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
3441da177e4SLinus Torvalds {
3451da177e4SLinus Torvalds 	struct inet6_dev *ndev;
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 	ASSERT_RTNL();
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	if (dev->mtu < IPV6_MIN_MTU)
3501da177e4SLinus Torvalds 		return NULL;
3511da177e4SLinus Torvalds 
352322f74a4SIngo Oeser 	ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
3531da177e4SLinus Torvalds 
354322f74a4SIngo Oeser 	if (ndev == NULL)
355322f74a4SIngo Oeser 		return NULL;
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	rwlock_init(&ndev->lock);
3581da177e4SLinus Torvalds 	ndev->dev = dev;
359502a2ffdSstephen hemminger 	INIT_LIST_HEAD(&ndev->addr_list);
360502a2ffdSstephen hemminger 
361c346dca1SYOSHIFUJI Hideaki 	memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
3621da177e4SLinus Torvalds 	ndev->cnf.mtu6 = dev->mtu;
3631da177e4SLinus Torvalds 	ndev->cnf.sysctl = NULL;
3641da177e4SLinus Torvalds 	ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
3651da177e4SLinus Torvalds 	if (ndev->nd_parms == NULL) {
3661da177e4SLinus Torvalds 		kfree(ndev);
3671da177e4SLinus Torvalds 		return NULL;
3681da177e4SLinus Torvalds 	}
3690187bdfbSBen Hutchings 	if (ndev->cnf.forwarding)
3700187bdfbSBen Hutchings 		dev_disable_lro(dev);
3711da177e4SLinus Torvalds 	/* We refer to the device */
3721da177e4SLinus Torvalds 	dev_hold(dev);
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	if (snmp6_alloc_dev(ndev) < 0) {
3751da177e4SLinus Torvalds 		ADBG((KERN_WARNING
376f3213831SJoe Perches 			"%s: cannot allocate memory for statistics; dev=%s.\n",
3770dc47877SHarvey Harrison 			__func__, dev->name));
3781da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, ndev->nd_parms);
3798603e33dSRoy Li 		dev_put(dev);
3808603e33dSRoy Li 		kfree(ndev);
3811da177e4SLinus Torvalds 		return NULL;
3821da177e4SLinus Torvalds 	}
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	if (snmp6_register_dev(ndev) < 0) {
3851da177e4SLinus Torvalds 		ADBG((KERN_WARNING
386f3213831SJoe Perches 			"%s: cannot create /proc/net/dev_snmp6/%s\n",
3870dc47877SHarvey Harrison 			__func__, dev->name));
3881da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, ndev->nd_parms);
3891da177e4SLinus Torvalds 		ndev->dead = 1;
3901da177e4SLinus Torvalds 		in6_dev_finish_destroy(ndev);
3911da177e4SLinus Torvalds 		return NULL;
3921da177e4SLinus Torvalds 	}
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	/* One reference from device.  We must do this before
3951da177e4SLinus Torvalds 	 * we invoke __ipv6_regen_rndid().
3961da177e4SLinus Torvalds 	 */
3971da177e4SLinus Torvalds 	in6_dev_hold(ndev);
3981da177e4SLinus Torvalds 
3991b34be74SYOSHIFUJI Hideaki 	if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
4001b34be74SYOSHIFUJI Hideaki 		ndev->cnf.accept_dad = -1;
4011b34be74SYOSHIFUJI Hideaki 
40207a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
403c7dc89c0SFred L. Templin 	if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
404f3213831SJoe Perches 		pr_info("%s: Disabled Multicast RS\n", dev->name);
405c7dc89c0SFred L. Templin 		ndev->cnf.rtr_solicits = 0;
406c7dc89c0SFred L. Templin 	}
407b077d7abSYOSHIFUJI Hideaki #endif
408b077d7abSYOSHIFUJI Hideaki 
4091da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
410372e6c8fSstephen hemminger 	INIT_LIST_HEAD(&ndev->tempaddr_list);
4111da177e4SLinus Torvalds 	setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
4121da177e4SLinus Torvalds 	if ((dev->flags&IFF_LOOPBACK) ||
4131da177e4SLinus Torvalds 	    dev->type == ARPHRD_TUNNEL ||
4149625ed72SYOSHIFUJI Hideaki 	    dev->type == ARPHRD_TUNNEL6 ||
4151da177e4SLinus Torvalds 	    dev->type == ARPHRD_SIT ||
4161da177e4SLinus Torvalds 	    dev->type == ARPHRD_NONE) {
4170be669bbSJoerg Roedel 		ndev->cnf.use_tempaddr = -1;
4181da177e4SLinus Torvalds 	} else {
4191da177e4SLinus Torvalds 		in6_dev_hold(ndev);
4201da177e4SLinus Torvalds 		ipv6_regen_rndid((unsigned long) ndev);
4211da177e4SLinus Torvalds 	}
4221da177e4SLinus Torvalds #endif
423914faa14SDaniel Borkmann 	ndev->token = in6addr_any;
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 */
432cf778b00SEric Dumazet 	rcu_assign_pointer(dev->ip6_ptr, ndev);
433d88ae4ccSYOSHIFUJI Hideaki 
4342c5e8933SHannes Frederic Sowa 	/* Join interface-local all-node multicast group */
4352c5e8933SHannes Frederic Sowa 	ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
4362c5e8933SHannes Frederic Sowa 
437d88ae4ccSYOSHIFUJI Hideaki 	/* Join all-node multicast group */
438f3ee4010SYOSHIFUJI Hideaki 	ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
439d88ae4ccSYOSHIFUJI Hideaki 
440d6ddef9eSLi Wei 	/* Join all-router multicast group if forwarding is set */
4418b2aaedeSLi Wei 	if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
442d6ddef9eSLi Wei 		ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
443d6ddef9eSLi Wei 
4441da177e4SLinus Torvalds 	return ndev;
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds 
447cee89473SYOSHIFUJI Hideaki static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
4481da177e4SLinus Torvalds {
4491da177e4SLinus Torvalds 	struct inet6_dev *idev;
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	ASSERT_RTNL();
4521da177e4SLinus Torvalds 
453e21e8467SStephen Hemminger 	idev = __in6_dev_get(dev);
454e21e8467SStephen Hemminger 	if (!idev) {
455e21e8467SStephen Hemminger 		idev = ipv6_add_dev(dev);
456e21e8467SStephen Hemminger 		if (!idev)
4571da177e4SLinus Torvalds 			return NULL;
4581da177e4SLinus Torvalds 	}
459c5e33bddSYOSHIFUJI Hideaki 
4601da177e4SLinus Torvalds 	if (dev->flags&IFF_UP)
4611da177e4SLinus Torvalds 		ipv6_mc_up(idev);
4621da177e4SLinus Torvalds 	return idev;
4631da177e4SLinus Torvalds }
4641da177e4SLinus Torvalds 
465f3a1bfb1SNicolas Dichtel static int inet6_netconf_msgsize_devconf(int type)
466f3a1bfb1SNicolas Dichtel {
467f3a1bfb1SNicolas Dichtel 	int size =  NLMSG_ALIGN(sizeof(struct netconfmsg))
468f3a1bfb1SNicolas Dichtel 		    + nla_total_size(4);	/* NETCONFA_IFINDEX */
469f3a1bfb1SNicolas Dichtel 
47076f8f6cbSNicolas Dichtel 	/* type -1 is used for ALL */
47176f8f6cbSNicolas Dichtel 	if (type == -1 || type == NETCONFA_FORWARDING)
472f3a1bfb1SNicolas Dichtel 		size += nla_total_size(4);
473b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
474d67b8c61SNicolas Dichtel 	if (type == -1 || type == NETCONFA_MC_FORWARDING)
475d67b8c61SNicolas Dichtel 		size += nla_total_size(4);
476b1afce95SDavid S. Miller #endif
477f3a1bfb1SNicolas Dichtel 
478f3a1bfb1SNicolas Dichtel 	return size;
479f3a1bfb1SNicolas Dichtel }
480f3a1bfb1SNicolas Dichtel 
481f3a1bfb1SNicolas Dichtel static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
482f3a1bfb1SNicolas Dichtel 				      struct ipv6_devconf *devconf, u32 portid,
483f3a1bfb1SNicolas Dichtel 				      u32 seq, int event, unsigned int flags,
484f3a1bfb1SNicolas Dichtel 				      int type)
485f3a1bfb1SNicolas Dichtel {
486f3a1bfb1SNicolas Dichtel 	struct nlmsghdr  *nlh;
487f3a1bfb1SNicolas Dichtel 	struct netconfmsg *ncm;
488f3a1bfb1SNicolas Dichtel 
489f3a1bfb1SNicolas Dichtel 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
490f3a1bfb1SNicolas Dichtel 			flags);
491f3a1bfb1SNicolas Dichtel 	if (nlh == NULL)
492f3a1bfb1SNicolas Dichtel 		return -EMSGSIZE;
493f3a1bfb1SNicolas Dichtel 
494f3a1bfb1SNicolas Dichtel 	ncm = nlmsg_data(nlh);
495f3a1bfb1SNicolas Dichtel 	ncm->ncm_family = AF_INET6;
496f3a1bfb1SNicolas Dichtel 
497f3a1bfb1SNicolas Dichtel 	if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
498f3a1bfb1SNicolas Dichtel 		goto nla_put_failure;
499f3a1bfb1SNicolas Dichtel 
50076f8f6cbSNicolas Dichtel 	/* type -1 is used for ALL */
50176f8f6cbSNicolas Dichtel 	if ((type == -1 || type == NETCONFA_FORWARDING) &&
502f3a1bfb1SNicolas Dichtel 	    nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
503f3a1bfb1SNicolas Dichtel 		goto nla_put_failure;
504b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
505d67b8c61SNicolas Dichtel 	if ((type == -1 || type == NETCONFA_MC_FORWARDING) &&
506d67b8c61SNicolas Dichtel 	    nla_put_s32(skb, NETCONFA_MC_FORWARDING,
507d67b8c61SNicolas Dichtel 			devconf->mc_forwarding) < 0)
508d67b8c61SNicolas Dichtel 		goto nla_put_failure;
509b1afce95SDavid S. Miller #endif
510f3a1bfb1SNicolas Dichtel 	return nlmsg_end(skb, nlh);
511f3a1bfb1SNicolas Dichtel 
512f3a1bfb1SNicolas Dichtel nla_put_failure:
513f3a1bfb1SNicolas Dichtel 	nlmsg_cancel(skb, nlh);
514f3a1bfb1SNicolas Dichtel 	return -EMSGSIZE;
515f3a1bfb1SNicolas Dichtel }
516f3a1bfb1SNicolas Dichtel 
517d67b8c61SNicolas Dichtel void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
518f3a1bfb1SNicolas Dichtel 				  struct ipv6_devconf *devconf)
519f3a1bfb1SNicolas Dichtel {
520f3a1bfb1SNicolas Dichtel 	struct sk_buff *skb;
521f3a1bfb1SNicolas Dichtel 	int err = -ENOBUFS;
522f3a1bfb1SNicolas Dichtel 
523f3a1bfb1SNicolas Dichtel 	skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC);
524f3a1bfb1SNicolas Dichtel 	if (skb == NULL)
525f3a1bfb1SNicolas Dichtel 		goto errout;
526f3a1bfb1SNicolas Dichtel 
527f3a1bfb1SNicolas Dichtel 	err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
528f3a1bfb1SNicolas Dichtel 					 RTM_NEWNETCONF, 0, type);
529f3a1bfb1SNicolas Dichtel 	if (err < 0) {
530f3a1bfb1SNicolas Dichtel 		/* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
531f3a1bfb1SNicolas Dichtel 		WARN_ON(err == -EMSGSIZE);
532f3a1bfb1SNicolas Dichtel 		kfree_skb(skb);
533f3a1bfb1SNicolas Dichtel 		goto errout;
534f3a1bfb1SNicolas Dichtel 	}
535f3a1bfb1SNicolas Dichtel 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_ATOMIC);
536f3a1bfb1SNicolas Dichtel 	return;
537f3a1bfb1SNicolas Dichtel errout:
538f3a1bfb1SNicolas Dichtel 	rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
539f3a1bfb1SNicolas Dichtel }
540f3a1bfb1SNicolas Dichtel 
54176f8f6cbSNicolas Dichtel static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
54276f8f6cbSNicolas Dichtel 	[NETCONFA_IFINDEX]	= { .len = sizeof(int) },
54376f8f6cbSNicolas Dichtel 	[NETCONFA_FORWARDING]	= { .len = sizeof(int) },
54476f8f6cbSNicolas Dichtel };
54576f8f6cbSNicolas Dichtel 
54676f8f6cbSNicolas Dichtel static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
547661d2967SThomas Graf 				     struct nlmsghdr *nlh)
54876f8f6cbSNicolas Dichtel {
54976f8f6cbSNicolas Dichtel 	struct net *net = sock_net(in_skb->sk);
55076f8f6cbSNicolas Dichtel 	struct nlattr *tb[NETCONFA_MAX+1];
55176f8f6cbSNicolas Dichtel 	struct netconfmsg *ncm;
55276f8f6cbSNicolas Dichtel 	struct sk_buff *skb;
55376f8f6cbSNicolas Dichtel 	struct ipv6_devconf *devconf;
55476f8f6cbSNicolas Dichtel 	struct inet6_dev *in6_dev;
55576f8f6cbSNicolas Dichtel 	struct net_device *dev;
55676f8f6cbSNicolas Dichtel 	int ifindex;
55776f8f6cbSNicolas Dichtel 	int err;
55876f8f6cbSNicolas Dichtel 
55976f8f6cbSNicolas Dichtel 	err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
56076f8f6cbSNicolas Dichtel 			  devconf_ipv6_policy);
56176f8f6cbSNicolas Dichtel 	if (err < 0)
56276f8f6cbSNicolas Dichtel 		goto errout;
56376f8f6cbSNicolas Dichtel 
56476f8f6cbSNicolas Dichtel 	err = EINVAL;
56576f8f6cbSNicolas Dichtel 	if (!tb[NETCONFA_IFINDEX])
56676f8f6cbSNicolas Dichtel 		goto errout;
56776f8f6cbSNicolas Dichtel 
56876f8f6cbSNicolas Dichtel 	ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
56976f8f6cbSNicolas Dichtel 	switch (ifindex) {
57076f8f6cbSNicolas Dichtel 	case NETCONFA_IFINDEX_ALL:
57176f8f6cbSNicolas Dichtel 		devconf = net->ipv6.devconf_all;
57276f8f6cbSNicolas Dichtel 		break;
57376f8f6cbSNicolas Dichtel 	case NETCONFA_IFINDEX_DEFAULT:
57476f8f6cbSNicolas Dichtel 		devconf = net->ipv6.devconf_dflt;
57576f8f6cbSNicolas Dichtel 		break;
57676f8f6cbSNicolas Dichtel 	default:
57776f8f6cbSNicolas Dichtel 		dev = __dev_get_by_index(net, ifindex);
57876f8f6cbSNicolas Dichtel 		if (dev == NULL)
57976f8f6cbSNicolas Dichtel 			goto errout;
58076f8f6cbSNicolas Dichtel 		in6_dev = __in6_dev_get(dev);
58176f8f6cbSNicolas Dichtel 		if (in6_dev == NULL)
58276f8f6cbSNicolas Dichtel 			goto errout;
58376f8f6cbSNicolas Dichtel 		devconf = &in6_dev->cnf;
58476f8f6cbSNicolas Dichtel 		break;
58576f8f6cbSNicolas Dichtel 	}
58676f8f6cbSNicolas Dichtel 
58776f8f6cbSNicolas Dichtel 	err = -ENOBUFS;
58876f8f6cbSNicolas Dichtel 	skb = nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC);
58976f8f6cbSNicolas Dichtel 	if (skb == NULL)
59076f8f6cbSNicolas Dichtel 		goto errout;
59176f8f6cbSNicolas Dichtel 
59276f8f6cbSNicolas Dichtel 	err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
59376f8f6cbSNicolas Dichtel 					 NETLINK_CB(in_skb).portid,
59476f8f6cbSNicolas Dichtel 					 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
59576f8f6cbSNicolas Dichtel 					 -1);
59676f8f6cbSNicolas Dichtel 	if (err < 0) {
59776f8f6cbSNicolas Dichtel 		/* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
59876f8f6cbSNicolas Dichtel 		WARN_ON(err == -EMSGSIZE);
59976f8f6cbSNicolas Dichtel 		kfree_skb(skb);
60076f8f6cbSNicolas Dichtel 		goto errout;
60176f8f6cbSNicolas Dichtel 	}
60276f8f6cbSNicolas Dichtel 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
60376f8f6cbSNicolas Dichtel errout:
60476f8f6cbSNicolas Dichtel 	return err;
60576f8f6cbSNicolas Dichtel }
60676f8f6cbSNicolas Dichtel 
6077a674200SNicolas Dichtel static int inet6_netconf_dump_devconf(struct sk_buff *skb,
6087a674200SNicolas Dichtel 				      struct netlink_callback *cb)
6097a674200SNicolas Dichtel {
6107a674200SNicolas Dichtel 	struct net *net = sock_net(skb->sk);
6117a674200SNicolas Dichtel 	int h, s_h;
6127a674200SNicolas Dichtel 	int idx, s_idx;
6137a674200SNicolas Dichtel 	struct net_device *dev;
6147a674200SNicolas Dichtel 	struct inet6_dev *idev;
6157a674200SNicolas Dichtel 	struct hlist_head *head;
6167a674200SNicolas Dichtel 
6177a674200SNicolas Dichtel 	s_h = cb->args[0];
6187a674200SNicolas Dichtel 	s_idx = idx = cb->args[1];
6197a674200SNicolas Dichtel 
6207a674200SNicolas Dichtel 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
6217a674200SNicolas Dichtel 		idx = 0;
6227a674200SNicolas Dichtel 		head = &net->dev_index_head[h];
6237a674200SNicolas Dichtel 		rcu_read_lock();
62463998ac2SNicolas Dichtel 		cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
62563998ac2SNicolas Dichtel 			  net->dev_base_seq;
6267a674200SNicolas Dichtel 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
6277a674200SNicolas Dichtel 			if (idx < s_idx)
6287a674200SNicolas Dichtel 				goto cont;
6297a674200SNicolas Dichtel 			idev = __in6_dev_get(dev);
6307a674200SNicolas Dichtel 			if (!idev)
6317a674200SNicolas Dichtel 				goto cont;
6327a674200SNicolas Dichtel 
6337a674200SNicolas Dichtel 			if (inet6_netconf_fill_devconf(skb, dev->ifindex,
6347a674200SNicolas Dichtel 						       &idev->cnf,
6357a674200SNicolas Dichtel 						       NETLINK_CB(cb->skb).portid,
6367a674200SNicolas Dichtel 						       cb->nlh->nlmsg_seq,
6377a674200SNicolas Dichtel 						       RTM_NEWNETCONF,
6387a674200SNicolas Dichtel 						       NLM_F_MULTI,
6397a674200SNicolas Dichtel 						       -1) <= 0) {
6407a674200SNicolas Dichtel 				rcu_read_unlock();
6417a674200SNicolas Dichtel 				goto done;
6427a674200SNicolas Dichtel 			}
64363998ac2SNicolas Dichtel 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
6447a674200SNicolas Dichtel cont:
6457a674200SNicolas Dichtel 			idx++;
6467a674200SNicolas Dichtel 		}
6477a674200SNicolas Dichtel 		rcu_read_unlock();
6487a674200SNicolas Dichtel 	}
6497a674200SNicolas Dichtel 	if (h == NETDEV_HASHENTRIES) {
6507a674200SNicolas Dichtel 		if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
6517a674200SNicolas Dichtel 					       net->ipv6.devconf_all,
6527a674200SNicolas Dichtel 					       NETLINK_CB(cb->skb).portid,
6537a674200SNicolas Dichtel 					       cb->nlh->nlmsg_seq,
6547a674200SNicolas Dichtel 					       RTM_NEWNETCONF, NLM_F_MULTI,
6557a674200SNicolas Dichtel 					       -1) <= 0)
6567a674200SNicolas Dichtel 			goto done;
6577a674200SNicolas Dichtel 		else
6587a674200SNicolas Dichtel 			h++;
6597a674200SNicolas Dichtel 	}
6607a674200SNicolas Dichtel 	if (h == NETDEV_HASHENTRIES + 1) {
6617a674200SNicolas Dichtel 		if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
6627a674200SNicolas Dichtel 					       net->ipv6.devconf_dflt,
6637a674200SNicolas Dichtel 					       NETLINK_CB(cb->skb).portid,
6647a674200SNicolas Dichtel 					       cb->nlh->nlmsg_seq,
6657a674200SNicolas Dichtel 					       RTM_NEWNETCONF, NLM_F_MULTI,
6667a674200SNicolas Dichtel 					       -1) <= 0)
6677a674200SNicolas Dichtel 			goto done;
6687a674200SNicolas Dichtel 		else
6697a674200SNicolas Dichtel 			h++;
6707a674200SNicolas Dichtel 	}
6717a674200SNicolas Dichtel done:
6727a674200SNicolas Dichtel 	cb->args[0] = h;
6737a674200SNicolas Dichtel 	cb->args[1] = idx;
6747a674200SNicolas Dichtel 
6757a674200SNicolas Dichtel 	return skb->len;
6767a674200SNicolas Dichtel }
6777a674200SNicolas Dichtel 
6781da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
6791da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev)
6801da177e4SLinus Torvalds {
6811da177e4SLinus Torvalds 	struct net_device *dev;
6821da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds 	if (!idev)
6851da177e4SLinus Torvalds 		return;
6861da177e4SLinus Torvalds 	dev = idev->dev;
6870187bdfbSBen Hutchings 	if (idev->cnf.forwarding)
6880187bdfbSBen Hutchings 		dev_disable_lro(dev);
6891a940835SAmerigo Wang 	if (dev->flags & IFF_MULTICAST) {
6902c5e8933SHannes Frederic Sowa 		if (idev->cnf.forwarding) {
691f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
6922c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
6932c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
6942c5e8933SHannes Frederic Sowa 		} else {
695f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
6962c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
6972c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
6982c5e8933SHannes Frederic Sowa 		}
6991da177e4SLinus Torvalds 	}
700502a2ffdSstephen hemminger 
701502a2ffdSstephen hemminger 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
7022c12a74cSMichal Wrobel 		if (ifa->flags&IFA_F_TENTATIVE)
7032c12a74cSMichal Wrobel 			continue;
7041da177e4SLinus Torvalds 		if (idev->cnf.forwarding)
7051da177e4SLinus Torvalds 			addrconf_join_anycast(ifa);
7061da177e4SLinus Torvalds 		else
7071da177e4SLinus Torvalds 			addrconf_leave_anycast(ifa);
7081da177e4SLinus Torvalds 	}
709f3a1bfb1SNicolas Dichtel 	inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
710f3a1bfb1SNicolas Dichtel 				     dev->ifindex, &idev->cnf);
7111da177e4SLinus Torvalds }
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 
714e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf)
7151da177e4SLinus Torvalds {
7161da177e4SLinus Torvalds 	struct net_device *dev;
7171da177e4SLinus Torvalds 	struct inet6_dev *idev;
7181da177e4SLinus Torvalds 
7194acd4945SBen Hutchings 	for_each_netdev(net, dev) {
7201da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
7211da177e4SLinus Torvalds 		if (idev) {
722e186932bSPavel Emelyanov 			int changed = (!idev->cnf.forwarding) ^ (!newf);
723e186932bSPavel Emelyanov 			idev->cnf.forwarding = newf;
7241da177e4SLinus Torvalds 			if (changed)
7251da177e4SLinus Torvalds 				dev_forward_change(idev);
7261da177e4SLinus Torvalds 		}
7271da177e4SLinus Torvalds 	}
7281da177e4SLinus Torvalds }
729c8fecf22SPavel Emelyanov 
730013d97e9SFrancesco Ruggeri static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
731c8fecf22SPavel Emelyanov {
732bff16c2fSPavel Emelyanov 	struct net *net;
733013d97e9SFrancesco Ruggeri 	int old;
734013d97e9SFrancesco Ruggeri 
735013d97e9SFrancesco Ruggeri 	if (!rtnl_trylock())
736013d97e9SFrancesco Ruggeri 		return restart_syscall();
737bff16c2fSPavel Emelyanov 
738bff16c2fSPavel Emelyanov 	net = (struct net *)table->extra2;
739013d97e9SFrancesco Ruggeri 	old = *p;
740013d97e9SFrancesco Ruggeri 	*p = newf;
741c8fecf22SPavel Emelyanov 
742013d97e9SFrancesco Ruggeri 	if (p == &net->ipv6.devconf_dflt->forwarding) {
743f3a1bfb1SNicolas Dichtel 		if ((!newf) ^ (!old))
744f3a1bfb1SNicolas Dichtel 			inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
745f3a1bfb1SNicolas Dichtel 						     NETCONFA_IFINDEX_DEFAULT,
746f3a1bfb1SNicolas Dichtel 						     net->ipv6.devconf_dflt);
747013d97e9SFrancesco Ruggeri 		rtnl_unlock();
748013d97e9SFrancesco Ruggeri 		return 0;
74988af182eSEric W. Biederman 	}
750b325fddbSStephen Hemminger 
751e186932bSPavel Emelyanov 	if (p == &net->ipv6.devconf_all->forwarding) {
752e186932bSPavel Emelyanov 		net->ipv6.devconf_dflt->forwarding = newf;
753e186932bSPavel Emelyanov 		addrconf_forward_change(net, newf);
754f3a1bfb1SNicolas Dichtel 		if ((!newf) ^ (!old))
755f3a1bfb1SNicolas Dichtel 			inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
756f3a1bfb1SNicolas Dichtel 						     NETCONFA_IFINDEX_ALL,
757f3a1bfb1SNicolas Dichtel 						     net->ipv6.devconf_all);
758013d97e9SFrancesco Ruggeri 	} else if ((!newf) ^ (!old))
759c8fecf22SPavel Emelyanov 		dev_forward_change((struct inet6_dev *)table->extra1);
7600187bdfbSBen Hutchings 	rtnl_unlock();
761c8fecf22SPavel Emelyanov 
762013d97e9SFrancesco Ruggeri 	if (newf)
7637b4da532SDaniel Lezcano 		rt6_purge_dflt_routers(net);
764b325fddbSStephen Hemminger 	return 1;
765c8fecf22SPavel Emelyanov }
7661da177e4SLinus Torvalds #endif
7671da177e4SLinus Torvalds 
7685c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */
7691da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
7701da177e4SLinus Torvalds {
771c2e21293Sstephen hemminger 	WARN_ON(!hlist_unhashed(&ifp->addr_lst));
772547b792cSIlpo Järvinen 
7731da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
77491df42beSJoe Perches 	pr_debug("%s\n", __func__);
7751da177e4SLinus Torvalds #endif
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds 	in6_dev_put(ifp->idev);
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds 	if (del_timer(&ifp->timer))
780e21e8467SStephen Hemminger 		pr_notice("Timer is still running, when freeing ifa=%p\n", ifp);
7811da177e4SLinus Torvalds 
782e9d3e084SHerbert Xu 	if (ifp->state != INET6_IFADDR_STATE_DEAD) {
783f3213831SJoe Perches 		pr_warn("Freeing alive inet6 address %p\n", ifp);
7841da177e4SLinus Torvalds 		return;
7851da177e4SLinus Torvalds 	}
78694e187c0SAmerigo Wang 	ip6_rt_put(ifp->rt);
7871da177e4SLinus Torvalds 
788e5785985SLai Jiangshan 	kfree_rcu(ifp, rcu);
7891da177e4SLinus Torvalds }
7901da177e4SLinus Torvalds 
791e55ffac6SBrian Haley static void
792e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
793e55ffac6SBrian Haley {
794502a2ffdSstephen hemminger 	struct list_head *p;
7958a6ce0c0SYOSHIFUJI Hideaki 	int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
796e55ffac6SBrian Haley 
797e55ffac6SBrian Haley 	/*
798e55ffac6SBrian Haley 	 * Each device address list is sorted in order of scope -
799e55ffac6SBrian Haley 	 * global before linklocal.
800e55ffac6SBrian Haley 	 */
801502a2ffdSstephen hemminger 	list_for_each(p, &idev->addr_list) {
802502a2ffdSstephen hemminger 		struct inet6_ifaddr *ifa
803502a2ffdSstephen hemminger 			= list_entry(p, struct inet6_ifaddr, if_list);
8048a6ce0c0SYOSHIFUJI Hideaki 		if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
805e55ffac6SBrian Haley 			break;
806e55ffac6SBrian Haley 	}
807e55ffac6SBrian Haley 
808b54c9b98SDavid S. Miller 	list_add_tail(&ifp->if_list, p);
809e55ffac6SBrian Haley }
810e55ffac6SBrian Haley 
811ddbe5032SEric Dumazet static u32 inet6_addr_hash(const struct in6_addr *addr)
8123eb84f49SYOSHIFUJI Hideaki {
813ddbe5032SEric Dumazet 	return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
8143eb84f49SYOSHIFUJI Hideaki }
8153eb84f49SYOSHIFUJI Hideaki 
8161da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds static struct inet6_ifaddr *
8191da177e4SLinus Torvalds ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
820e431b8c0SJamal Hadi Salim 	      int scope, u32 flags)
8211da177e4SLinus Torvalds {
8221da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
8231da177e4SLinus Torvalds 	struct rt6_info *rt;
8243a88a81dSstephen hemminger 	unsigned int hash;
8251da177e4SLinus Torvalds 	int err = 0;
826d68b8270SYOSHIFUJI Hideaki 	int addr_type = ipv6_addr_type(addr);
827d68b8270SYOSHIFUJI Hideaki 
828d68b8270SYOSHIFUJI Hideaki 	if (addr_type == IPV6_ADDR_ANY ||
829d68b8270SYOSHIFUJI Hideaki 	    addr_type & IPV6_ADDR_MULTICAST ||
830d68b8270SYOSHIFUJI Hideaki 	    (!(idev->dev->flags & IFF_LOOPBACK) &&
831d68b8270SYOSHIFUJI Hideaki 	     addr_type & IPV6_ADDR_LOOPBACK))
832d68b8270SYOSHIFUJI Hideaki 		return ERR_PTR(-EADDRNOTAVAIL);
8331da177e4SLinus Torvalds 
8348814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
8351da177e4SLinus Torvalds 	if (idev->dead) {
8361da177e4SLinus Torvalds 		err = -ENODEV;			/*XXX*/
8371da177e4SLinus Torvalds 		goto out2;
8381da177e4SLinus Torvalds 	}
8391da177e4SLinus Torvalds 
84056d417b1SBrian Haley 	if (idev->cnf.disable_ipv6) {
8419bdd8d40SBrian Haley 		err = -EACCES;
8429bdd8d40SBrian Haley 		goto out2;
8439bdd8d40SBrian Haley 	}
8449bdd8d40SBrian Haley 
8455c578aedSstephen hemminger 	spin_lock(&addrconf_hash_lock);
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	/* Ignore adding duplicate addresses on an interface */
848c346dca1SYOSHIFUJI Hideaki 	if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
8491da177e4SLinus Torvalds 		ADBG(("ipv6_add_addr: already assigned\n"));
8501da177e4SLinus Torvalds 		err = -EEXIST;
8511da177e4SLinus Torvalds 		goto out;
8521da177e4SLinus Torvalds 	}
8531da177e4SLinus Torvalds 
854322f74a4SIngo Oeser 	ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 	if (ifa == NULL) {
8571da177e4SLinus Torvalds 		ADBG(("ipv6_add_addr: malloc failed\n"));
8581da177e4SLinus Torvalds 		err = -ENOBUFS;
8591da177e4SLinus Torvalds 		goto out;
8601da177e4SLinus Torvalds 	}
8611da177e4SLinus Torvalds 
8628f031519SDavid S. Miller 	rt = addrconf_dst_alloc(idev, addr, false);
8631da177e4SLinus Torvalds 	if (IS_ERR(rt)) {
8641da177e4SLinus Torvalds 		err = PTR_ERR(rt);
8651da177e4SLinus Torvalds 		goto out;
8661da177e4SLinus Torvalds 	}
8671da177e4SLinus Torvalds 
8684e3fd7a0SAlexey Dobriyan 	ifa->addr = *addr;
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 	spin_lock_init(&ifa->lock);
871e9d3e084SHerbert Xu 	spin_lock_init(&ifa->state_lock);
8721da177e4SLinus Torvalds 	init_timer(&ifa->timer);
873c2e21293Sstephen hemminger 	INIT_HLIST_NODE(&ifa->addr_lst);
8741da177e4SLinus Torvalds 	ifa->timer.data = (unsigned long) ifa;
8751da177e4SLinus Torvalds 	ifa->scope = scope;
8761da177e4SLinus Torvalds 	ifa->prefix_len = pfxlen;
8771da177e4SLinus Torvalds 	ifa->flags = flags | IFA_F_TENTATIVE;
8781da177e4SLinus Torvalds 	ifa->cstamp = ifa->tstamp = jiffies;
879617fe29dSDaniel Borkmann 	ifa->tokenized = false;
8801da177e4SLinus Torvalds 
88157f5f544SKeir Fraser 	ifa->rt = rt;
88257f5f544SKeir Fraser 
8831da177e4SLinus Torvalds 	ifa->idev = idev;
8841da177e4SLinus Torvalds 	in6_dev_hold(idev);
8851da177e4SLinus Torvalds 	/* For caller */
8861da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 	/* Add to big hash table */
889ddbe5032SEric Dumazet 	hash = inet6_addr_hash(addr);
8901da177e4SLinus Torvalds 
8915c578aedSstephen hemminger 	hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
8925c578aedSstephen hemminger 	spin_unlock(&addrconf_hash_lock);
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	write_lock(&idev->lock);
8951da177e4SLinus Torvalds 	/* Add to inet6_dev unicast addr list. */
896e55ffac6SBrian Haley 	ipv6_link_dev_addr(idev, ifa);
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
8991da177e4SLinus Torvalds 	if (ifa->flags&IFA_F_TEMPORARY) {
900372e6c8fSstephen hemminger 		list_add(&ifa->tmp_list, &idev->tempaddr_list);
9011da177e4SLinus Torvalds 		in6_ifa_hold(ifa);
9021da177e4SLinus Torvalds 	}
9031da177e4SLinus Torvalds #endif
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
9061da177e4SLinus Torvalds 	write_unlock(&idev->lock);
9071da177e4SLinus Torvalds out2:
9088814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
9091da177e4SLinus Torvalds 
910fd92833aSYOSHIFUJI Hideaki 	if (likely(err == 0))
911f88c91ddSCong Wang 		inet6addr_notifier_call_chain(NETDEV_UP, ifa);
9121da177e4SLinus Torvalds 	else {
9131da177e4SLinus Torvalds 		kfree(ifa);
9141da177e4SLinus Torvalds 		ifa = ERR_PTR(err);
9151da177e4SLinus Torvalds 	}
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 	return ifa;
9181da177e4SLinus Torvalds out:
9195c578aedSstephen hemminger 	spin_unlock(&addrconf_hash_lock);
9201da177e4SLinus Torvalds 	goto out2;
9211da177e4SLinus Torvalds }
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp)
9261da177e4SLinus Torvalds {
927502a2ffdSstephen hemminger 	struct inet6_ifaddr *ifa, *ifn;
9281da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
9294c5ff6a6SHerbert Xu 	int state;
9301da177e4SLinus Torvalds 	int deleted = 0, onlink = 0;
9311da177e4SLinus Torvalds 	unsigned long expires = jiffies;
9321da177e4SLinus Torvalds 
9334c5ff6a6SHerbert Xu 	spin_lock_bh(&ifp->state_lock);
9344c5ff6a6SHerbert Xu 	state = ifp->state;
935e9d3e084SHerbert Xu 	ifp->state = INET6_IFADDR_STATE_DEAD;
9364c5ff6a6SHerbert Xu 	spin_unlock_bh(&ifp->state_lock);
9374c5ff6a6SHerbert Xu 
9384c5ff6a6SHerbert Xu 	if (state == INET6_IFADDR_STATE_DEAD)
9394c5ff6a6SHerbert Xu 		goto out;
9401da177e4SLinus Torvalds 
9415c578aedSstephen hemminger 	spin_lock_bh(&addrconf_hash_lock);
9425c578aedSstephen hemminger 	hlist_del_init_rcu(&ifp->addr_lst);
9435c578aedSstephen hemminger 	spin_unlock_bh(&addrconf_hash_lock);
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
9461da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
9471da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_TEMPORARY) {
948372e6c8fSstephen hemminger 		list_del(&ifp->tmp_list);
9491da177e4SLinus Torvalds 		if (ifp->ifpub) {
9501da177e4SLinus Torvalds 			in6_ifa_put(ifp->ifpub);
9511da177e4SLinus Torvalds 			ifp->ifpub = NULL;
9521da177e4SLinus Torvalds 		}
9531da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
9541da177e4SLinus Torvalds 	}
9551da177e4SLinus Torvalds #endif
9561da177e4SLinus Torvalds 
957502a2ffdSstephen hemminger 	list_for_each_entry_safe(ifa, ifn, &idev->addr_list, if_list) {
9581da177e4SLinus Torvalds 		if (ifa == ifp) {
959502a2ffdSstephen hemminger 			list_del_init(&ifp->if_list);
9601da177e4SLinus Torvalds 			__in6_ifa_put(ifp);
961502a2ffdSstephen hemminger 
9621da177e4SLinus Torvalds 			if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
9631da177e4SLinus Torvalds 				break;
9641da177e4SLinus Torvalds 			deleted = 1;
9651d142804SKristian Slavov 			continue;
9661da177e4SLinus Torvalds 		} else if (ifp->flags & IFA_F_PERMANENT) {
9671da177e4SLinus Torvalds 			if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
9681da177e4SLinus Torvalds 					      ifp->prefix_len)) {
9691da177e4SLinus Torvalds 				if (ifa->flags & IFA_F_PERMANENT) {
9701da177e4SLinus Torvalds 					onlink = 1;
9711da177e4SLinus Torvalds 					if (deleted)
9721da177e4SLinus Torvalds 						break;
9731da177e4SLinus Torvalds 				} else {
9741da177e4SLinus Torvalds 					unsigned long lifetime;
9751da177e4SLinus Torvalds 
9761da177e4SLinus Torvalds 					if (!onlink)
9771da177e4SLinus Torvalds 						onlink = -1;
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 					spin_lock(&ifa->lock);
9804bed72e4SYOSHIFUJI Hideaki 
9814bed72e4SYOSHIFUJI Hideaki 					lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
9824bed72e4SYOSHIFUJI Hideaki 					/*
9834bed72e4SYOSHIFUJI Hideaki 					 * Note: Because this address is
9844bed72e4SYOSHIFUJI Hideaki 					 * not permanent, lifetime <
9854bed72e4SYOSHIFUJI Hideaki 					 * LONG_MAX / HZ here.
9864bed72e4SYOSHIFUJI Hideaki 					 */
9871da177e4SLinus Torvalds 					if (time_before(expires,
9881da177e4SLinus Torvalds 							ifa->tstamp + lifetime * HZ))
9891da177e4SLinus Torvalds 						expires = ifa->tstamp + lifetime * HZ;
9901da177e4SLinus Torvalds 					spin_unlock(&ifa->lock);
9911da177e4SLinus Torvalds 				}
9921da177e4SLinus Torvalds 			}
9931da177e4SLinus Torvalds 		}
9941da177e4SLinus Torvalds 	}
9951da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
9961da177e4SLinus Torvalds 
997b2238566SAndrey Vagin 	addrconf_del_timer(ifp);
998b2238566SAndrey Vagin 
9991da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_DELADDR, ifp);
10001da177e4SLinus Torvalds 
1001f88c91ddSCong Wang 	inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds 	/*
10041da177e4SLinus Torvalds 	 * Purge or update corresponding prefix
10051da177e4SLinus Torvalds 	 *
10061da177e4SLinus Torvalds 	 * 1) we don't purge prefix here if address was not permanent.
10071da177e4SLinus Torvalds 	 *    prefix is managed by its own lifetime.
10081da177e4SLinus Torvalds 	 * 2) if there're no addresses, delete prefix.
10091da177e4SLinus Torvalds 	 * 3) if there're still other permanent address(es),
10101da177e4SLinus Torvalds 	 *    corresponding prefix is still permanent.
10111da177e4SLinus Torvalds 	 * 4) otherwise, update prefix lifetime to the
10121da177e4SLinus Torvalds 	 *    longest valid lifetime among the corresponding
10131da177e4SLinus Torvalds 	 *    addresses on the device.
10141da177e4SLinus Torvalds 	 *    Note: subsequent RA will update lifetime.
10151da177e4SLinus Torvalds 	 *
10161da177e4SLinus Torvalds 	 * --yoshfuji
10171da177e4SLinus Torvalds 	 */
10181da177e4SLinus Torvalds 	if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
10191da177e4SLinus Torvalds 		struct in6_addr prefix;
10201da177e4SLinus Torvalds 		struct rt6_info *rt;
10211da177e4SLinus Torvalds 
102264c6d08eSNicolas Dichtel 		ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
102364c6d08eSNicolas Dichtel 
102421caa662SRomain Kuntz 		rt = addrconf_get_prefix_route(&prefix,
102521caa662SRomain Kuntz 					       ifp->prefix_len,
102621caa662SRomain Kuntz 					       ifp->idev->dev,
102721caa662SRomain Kuntz 					       0, RTF_GATEWAY | RTF_DEFAULT);
102821caa662SRomain Kuntz 
102921caa662SRomain Kuntz 		if (rt) {
10301da177e4SLinus Torvalds 			if (onlink == 0) {
1031e0a1ad73SThomas Graf 				ip6_del_rt(rt);
10321da177e4SLinus Torvalds 				rt = NULL;
10331da177e4SLinus Torvalds 			} else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
10341716a961SGao feng 				rt6_set_expires(rt, expires);
10351da177e4SLinus Torvalds 			}
10361da177e4SLinus Torvalds 		}
103794e187c0SAmerigo Wang 		ip6_rt_put(rt);
10381da177e4SLinus Torvalds 	}
10391da177e4SLinus Torvalds 
1040c3968a85SDaniel Walter 	/* clean up prefsrc entries */
1041c3968a85SDaniel Walter 	rt6_remove_prefsrc(ifp);
10424c5ff6a6SHerbert Xu out:
10431da177e4SLinus Torvalds 	in6_ifa_put(ifp);
10441da177e4SLinus Torvalds }
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
10471da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
10481da177e4SLinus Torvalds {
10491da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
10501da177e4SLinus Torvalds 	struct in6_addr addr, *tmpaddr;
105176f793e3SLorenzo Colitti 	unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
1052eac55bf9SBenoit Boissinot 	unsigned long regen_advance;
10531da177e4SLinus Torvalds 	int tmp_plen;
10541da177e4SLinus Torvalds 	int ret = 0;
10551da177e4SLinus Torvalds 	int max_addresses;
105695c385b4SNeil Horman 	u32 addr_flags;
105776f793e3SLorenzo Colitti 	unsigned long now = jiffies;
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds 	write_lock(&idev->lock);
10601da177e4SLinus Torvalds 	if (ift) {
10611da177e4SLinus Torvalds 		spin_lock_bh(&ift->lock);
10621da177e4SLinus Torvalds 		memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
10631da177e4SLinus Torvalds 		spin_unlock_bh(&ift->lock);
10641da177e4SLinus Torvalds 		tmpaddr = &addr;
10651da177e4SLinus Torvalds 	} else {
10661da177e4SLinus Torvalds 		tmpaddr = NULL;
10671da177e4SLinus Torvalds 	}
10681da177e4SLinus Torvalds retry:
10691da177e4SLinus Torvalds 	in6_dev_hold(idev);
10701da177e4SLinus Torvalds 	if (idev->cnf.use_tempaddr <= 0) {
10711da177e4SLinus Torvalds 		write_unlock(&idev->lock);
1072f3213831SJoe Perches 		pr_info("%s: use_tempaddr is disabled\n", __func__);
10731da177e4SLinus Torvalds 		in6_dev_put(idev);
10741da177e4SLinus Torvalds 		ret = -1;
10751da177e4SLinus Torvalds 		goto out;
10761da177e4SLinus Torvalds 	}
10771da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
10781da177e4SLinus Torvalds 	if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
10791da177e4SLinus Torvalds 		idev->cnf.use_tempaddr = -1;	/*XXX*/
10801da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
10811da177e4SLinus Torvalds 		write_unlock(&idev->lock);
1082f3213831SJoe Perches 		pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1083f3213831SJoe Perches 			__func__);
10841da177e4SLinus Torvalds 		in6_dev_put(idev);
10851da177e4SLinus Torvalds 		ret = -1;
10861da177e4SLinus Torvalds 		goto out;
10871da177e4SLinus Torvalds 	}
10881da177e4SLinus Torvalds 	in6_ifa_hold(ifp);
10891da177e4SLinus Torvalds 	memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1090eb7e0575SSorin Dumitru 	__ipv6_try_regen_rndid(idev, tmpaddr);
10911da177e4SLinus Torvalds 	memcpy(&addr.s6_addr[8], idev->rndid, 8);
109276f793e3SLorenzo Colitti 	age = (now - ifp->tstamp) / HZ;
10931da177e4SLinus Torvalds 	tmp_valid_lft = min_t(__u32,
10941da177e4SLinus Torvalds 			      ifp->valid_lft,
10957a876b0eSGlenn Wurster 			      idev->cnf.temp_valid_lft + age);
10961da177e4SLinus Torvalds 	tmp_prefered_lft = min_t(__u32,
10971da177e4SLinus Torvalds 				 ifp->prefered_lft,
10987a876b0eSGlenn Wurster 				 idev->cnf.temp_prefered_lft + age -
1099784e2710SBen Hutchings 				 idev->cnf.max_desync_factor);
11001da177e4SLinus Torvalds 	tmp_plen = ifp->prefix_len;
11011da177e4SLinus Torvalds 	max_addresses = idev->cnf.max_addresses;
11021da177e4SLinus Torvalds 	tmp_tstamp = ifp->tstamp;
11031da177e4SLinus Torvalds 	spin_unlock_bh(&ifp->lock);
11041da177e4SLinus Torvalds 
1105eac55bf9SBenoit Boissinot 	regen_advance = idev->cnf.regen_max_retry *
1106eac55bf9SBenoit Boissinot 	                idev->cnf.dad_transmits *
1107eac55bf9SBenoit Boissinot 	                idev->nd_parms->retrans_time / HZ;
11081da177e4SLinus Torvalds 	write_unlock(&idev->lock);
110995c385b4SNeil Horman 
1110eac55bf9SBenoit Boissinot 	/* A temporary address is created only if this calculated Preferred
1111eac55bf9SBenoit Boissinot 	 * Lifetime is greater than REGEN_ADVANCE time units.  In particular,
1112eac55bf9SBenoit Boissinot 	 * an implementation must not create a temporary address with a zero
1113eac55bf9SBenoit Boissinot 	 * Preferred Lifetime.
1114eac55bf9SBenoit Boissinot 	 */
1115eac55bf9SBenoit Boissinot 	if (tmp_prefered_lft <= regen_advance) {
1116eac55bf9SBenoit Boissinot 		in6_ifa_put(ifp);
1117eac55bf9SBenoit Boissinot 		in6_dev_put(idev);
1118eac55bf9SBenoit Boissinot 		ret = -1;
1119eac55bf9SBenoit Boissinot 		goto out;
1120eac55bf9SBenoit Boissinot 	}
1121eac55bf9SBenoit Boissinot 
112295c385b4SNeil Horman 	addr_flags = IFA_F_TEMPORARY;
112395c385b4SNeil Horman 	/* set in addrconf_prefix_rcv() */
112495c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
112595c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
112695c385b4SNeil Horman 
11271da177e4SLinus Torvalds 	ift = !max_addresses ||
11281da177e4SLinus Torvalds 	      ipv6_count_addresses(idev) < max_addresses ?
112988924753SCong Wang 		ipv6_add_addr(idev, &addr, tmp_plen, ipv6_addr_scope(&addr),
113095c385b4SNeil Horman 			      addr_flags) : NULL;
11313f0d2ba0SYOSHIFUJI Hideaki / 吉藤英明 	if (IS_ERR_OR_NULL(ift)) {
11321da177e4SLinus Torvalds 		in6_ifa_put(ifp);
11331da177e4SLinus Torvalds 		in6_dev_put(idev);
1134f3213831SJoe Perches 		pr_info("%s: retry temporary address regeneration\n", __func__);
11351da177e4SLinus Torvalds 		tmpaddr = &addr;
11361da177e4SLinus Torvalds 		write_lock(&idev->lock);
11371da177e4SLinus Torvalds 		goto retry;
11381da177e4SLinus Torvalds 	}
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 	spin_lock_bh(&ift->lock);
11411da177e4SLinus Torvalds 	ift->ifpub = ifp;
11421da177e4SLinus Torvalds 	ift->valid_lft = tmp_valid_lft;
11431da177e4SLinus Torvalds 	ift->prefered_lft = tmp_prefered_lft;
114476f793e3SLorenzo Colitti 	ift->cstamp = now;
11451da177e4SLinus Torvalds 	ift->tstamp = tmp_tstamp;
11461da177e4SLinus Torvalds 	spin_unlock_bh(&ift->lock);
11471da177e4SLinus Torvalds 
1148cf22f9a2SDavid S. Miller 	addrconf_dad_start(ift);
11491da177e4SLinus Torvalds 	in6_ifa_put(ift);
11501da177e4SLinus Torvalds 	in6_dev_put(idev);
11511da177e4SLinus Torvalds out:
11521da177e4SLinus Torvalds 	return ret;
11531da177e4SLinus Torvalds }
11541da177e4SLinus Torvalds #endif
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds /*
1157072047e4SYOSHIFUJI Hideaki  *	Choose an appropriate source address (RFC3484)
11581da177e4SLinus Torvalds  */
1159a9b05723SYOSHIFUJI Hideaki enum {
1160a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_INIT = 0,
1161a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LOCAL,
1162a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_SCOPE,
1163a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFERRED,
1164a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1165a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_HOA,
1166a9b05723SYOSHIFUJI Hideaki #endif
1167a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_OIF,
1168a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LABEL,
1169a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_PRIVACY
1170a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PRIVACY,
1171a9b05723SYOSHIFUJI Hideaki #endif
1172a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_ORCHID,
1173a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFIX,
1174a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_MAX
1175072047e4SYOSHIFUJI Hideaki };
1176072047e4SYOSHIFUJI Hideaki 
1177a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score {
1178a9b05723SYOSHIFUJI Hideaki 	int			rule;
1179a9b05723SYOSHIFUJI Hideaki 	int			addr_type;
1180a9b05723SYOSHIFUJI Hideaki 	struct inet6_ifaddr	*ifa;
1181a9b05723SYOSHIFUJI Hideaki 	DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1182a9b05723SYOSHIFUJI Hideaki 	int			scopedist;
1183a9b05723SYOSHIFUJI Hideaki 	int			matchlen;
1184a9b05723SYOSHIFUJI Hideaki };
1185a9b05723SYOSHIFUJI Hideaki 
1186a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst {
11879acd9f3aSYOSHIFUJI Hideaki 	const struct in6_addr *addr;
1188a9b05723SYOSHIFUJI Hideaki 	int ifindex;
1189a9b05723SYOSHIFUJI Hideaki 	int scope;
1190a9b05723SYOSHIFUJI Hideaki 	int label;
11917cbca67cSYOSHIFUJI Hideaki 	unsigned int prefs;
1192a9b05723SYOSHIFUJI Hideaki };
1193072047e4SYOSHIFUJI Hideaki 
1194b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type)
11951da177e4SLinus Torvalds {
119645bb0060SUlrich Weber 	if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1197072047e4SYOSHIFUJI Hideaki 		return 1;
1198072047e4SYOSHIFUJI Hideaki 	return 0;
11991da177e4SLinus Torvalds }
12001da177e4SLinus Torvalds 
12013de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net,
12023de23255SBenjamin Thery 			       struct ipv6_saddr_score *score,
1203a9b05723SYOSHIFUJI Hideaki 			       struct ipv6_saddr_dst *dst,
1204a9b05723SYOSHIFUJI Hideaki 			       int i)
1205a9b05723SYOSHIFUJI Hideaki {
1206a9b05723SYOSHIFUJI Hideaki 	int ret;
1207a9b05723SYOSHIFUJI Hideaki 
1208a9b05723SYOSHIFUJI Hideaki 	if (i <= score->rule) {
1209a9b05723SYOSHIFUJI Hideaki 		switch (i) {
1210a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_SCOPE:
1211a9b05723SYOSHIFUJI Hideaki 			ret = score->scopedist;
1212a9b05723SYOSHIFUJI Hideaki 			break;
1213a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_PREFIX:
1214a9b05723SYOSHIFUJI Hideaki 			ret = score->matchlen;
1215a9b05723SYOSHIFUJI Hideaki 			break;
1216a9b05723SYOSHIFUJI Hideaki 		default:
1217a9b05723SYOSHIFUJI Hideaki 			ret = !!test_bit(i, score->scorebits);
1218a9b05723SYOSHIFUJI Hideaki 		}
1219a9b05723SYOSHIFUJI Hideaki 		goto out;
1220a9b05723SYOSHIFUJI Hideaki 	}
1221a9b05723SYOSHIFUJI Hideaki 
1222a9b05723SYOSHIFUJI Hideaki 	switch (i) {
1223a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_INIT:
1224a9b05723SYOSHIFUJI Hideaki 		/* Rule 0: remember if hiscore is not ready yet */
1225a9b05723SYOSHIFUJI Hideaki 		ret = !!score->ifa;
1226a9b05723SYOSHIFUJI Hideaki 		break;
1227a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LOCAL:
1228a9b05723SYOSHIFUJI Hideaki 		/* Rule 1: Prefer same address */
1229a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1230a9b05723SYOSHIFUJI Hideaki 		break;
1231a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_SCOPE:
1232a9b05723SYOSHIFUJI Hideaki 		/* Rule 2: Prefer appropriate scope
1233a9b05723SYOSHIFUJI Hideaki 		 *
1234a9b05723SYOSHIFUJI Hideaki 		 *      ret
1235a9b05723SYOSHIFUJI Hideaki 		 *       ^
1236a9b05723SYOSHIFUJI Hideaki 		 *    -1 |  d 15
1237a9b05723SYOSHIFUJI Hideaki 		 *    ---+--+-+---> scope
1238a9b05723SYOSHIFUJI Hideaki 		 *       |
1239a9b05723SYOSHIFUJI Hideaki 		 *       |             d is scope of the destination.
1240a9b05723SYOSHIFUJI Hideaki 		 *  B-d  |  \
1241a9b05723SYOSHIFUJI Hideaki 		 *       |   \      <- smaller scope is better if
1242a9b05723SYOSHIFUJI Hideaki 		 *  B-15 |    \        if scope is enough for destinaion.
1243a9b05723SYOSHIFUJI Hideaki 		 *       |             ret = B - scope (-1 <= scope >= d <= 15).
1244a9b05723SYOSHIFUJI Hideaki 		 * d-C-1 | /
1245a9b05723SYOSHIFUJI Hideaki 		 *       |/         <- greater is better
1246a9b05723SYOSHIFUJI Hideaki 		 *   -C  /             if scope is not enough for destination.
1247a9b05723SYOSHIFUJI Hideaki 		 *      /|             ret = scope - C (-1 <= d < scope <= 15).
1248a9b05723SYOSHIFUJI Hideaki 		 *
1249a9b05723SYOSHIFUJI Hideaki 		 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1250a9b05723SYOSHIFUJI Hideaki 		 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1251a9b05723SYOSHIFUJI Hideaki 		 * Assume B = 0 and we get C > 29.
1252a9b05723SYOSHIFUJI Hideaki 		 */
1253a9b05723SYOSHIFUJI Hideaki 		ret = __ipv6_addr_src_scope(score->addr_type);
1254a9b05723SYOSHIFUJI Hideaki 		if (ret >= dst->scope)
1255a9b05723SYOSHIFUJI Hideaki 			ret = -ret;
1256a9b05723SYOSHIFUJI Hideaki 		else
1257a9b05723SYOSHIFUJI Hideaki 			ret -= 128;	/* 30 is enough */
1258a9b05723SYOSHIFUJI Hideaki 		score->scopedist = ret;
1259a9b05723SYOSHIFUJI Hideaki 		break;
1260a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFERRED:
1261a9b05723SYOSHIFUJI Hideaki 		/* Rule 3: Avoid deprecated and optimistic addresses */
1262a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_saddr_preferred(score->addr_type) ||
1263a9b05723SYOSHIFUJI Hideaki 		      !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC));
1264a9b05723SYOSHIFUJI Hideaki 		break;
1265a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1266a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_HOA:
12677cbca67cSYOSHIFUJI Hideaki 	    {
1268a9b05723SYOSHIFUJI Hideaki 		/* Rule 4: Prefer home address */
12697cbca67cSYOSHIFUJI Hideaki 		int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
12707cbca67cSYOSHIFUJI Hideaki 		ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1271a9b05723SYOSHIFUJI Hideaki 		break;
12727cbca67cSYOSHIFUJI Hideaki 	    }
1273a9b05723SYOSHIFUJI Hideaki #endif
1274a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_OIF:
1275a9b05723SYOSHIFUJI Hideaki 		/* Rule 5: Prefer outgoing interface */
1276a9b05723SYOSHIFUJI Hideaki 		ret = (!dst->ifindex ||
1277a9b05723SYOSHIFUJI Hideaki 		       dst->ifindex == score->ifa->idev->dev->ifindex);
1278a9b05723SYOSHIFUJI Hideaki 		break;
1279a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LABEL:
1280a9b05723SYOSHIFUJI Hideaki 		/* Rule 6: Prefer matching label */
12813de23255SBenjamin Thery 		ret = ipv6_addr_label(net,
12823de23255SBenjamin Thery 				      &score->ifa->addr, score->addr_type,
1283a9b05723SYOSHIFUJI Hideaki 				      score->ifa->idev->dev->ifindex) == dst->label;
1284a9b05723SYOSHIFUJI Hideaki 		break;
1285a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_PRIVACY
1286a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PRIVACY:
12877cbca67cSYOSHIFUJI Hideaki 	    {
1288a9b05723SYOSHIFUJI Hideaki 		/* Rule 7: Prefer public address
128925985edcSLucas De Marchi 		 * Note: prefer temporary address if use_tempaddr >= 2
1290a9b05723SYOSHIFUJI Hideaki 		 */
12917cbca67cSYOSHIFUJI Hideaki 		int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
12927cbca67cSYOSHIFUJI Hideaki 				!!(dst->prefs & IPV6_PREFER_SRC_TMP) :
12937cbca67cSYOSHIFUJI Hideaki 				score->ifa->idev->cnf.use_tempaddr >= 2;
12947cbca67cSYOSHIFUJI Hideaki 		ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1295a9b05723SYOSHIFUJI Hideaki 		break;
12967cbca67cSYOSHIFUJI Hideaki 	    }
1297a9b05723SYOSHIFUJI Hideaki #endif
1298a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_ORCHID:
1299a9b05723SYOSHIFUJI Hideaki 		/* Rule 8-: Prefer ORCHID vs ORCHID or
1300a9b05723SYOSHIFUJI Hideaki 		 *	    non-ORCHID vs non-ORCHID
1301a9b05723SYOSHIFUJI Hideaki 		 */
1302a9b05723SYOSHIFUJI Hideaki 		ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1303a9b05723SYOSHIFUJI Hideaki 			ipv6_addr_orchid(dst->addr));
1304a9b05723SYOSHIFUJI Hideaki 		break;
1305a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFIX:
1306a9b05723SYOSHIFUJI Hideaki 		/* Rule 8: Use longest matching prefix */
130791b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
130891b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		if (ret > score->ifa->prefix_len)
130991b4b04fSYOSHIFUJI Hideaki / 吉藤英明 			ret = score->ifa->prefix_len;
131091b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		score->matchlen = ret;
1311a9b05723SYOSHIFUJI Hideaki 		break;
1312a9b05723SYOSHIFUJI Hideaki 	default:
1313a9b05723SYOSHIFUJI Hideaki 		ret = 0;
1314a9b05723SYOSHIFUJI Hideaki 	}
1315a9b05723SYOSHIFUJI Hideaki 
1316a9b05723SYOSHIFUJI Hideaki 	if (ret)
1317a9b05723SYOSHIFUJI Hideaki 		__set_bit(i, score->scorebits);
1318a9b05723SYOSHIFUJI Hideaki 	score->rule = i;
1319a9b05723SYOSHIFUJI Hideaki out:
1320a9b05723SYOSHIFUJI Hideaki 	return ret;
1321a9b05723SYOSHIFUJI Hideaki }
1322a9b05723SYOSHIFUJI Hideaki 
1323b3f644fcSPatrick McHardy int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
13249acd9f3aSYOSHIFUJI Hideaki 		       const struct in6_addr *daddr, unsigned int prefs,
13257cbca67cSYOSHIFUJI Hideaki 		       struct in6_addr *saddr)
13261da177e4SLinus Torvalds {
1327a9b05723SYOSHIFUJI Hideaki 	struct ipv6_saddr_score scores[2],
1328a9b05723SYOSHIFUJI Hideaki 				*score = &scores[0], *hiscore = &scores[1];
1329a9b05723SYOSHIFUJI Hideaki 	struct ipv6_saddr_dst dst;
1330072047e4SYOSHIFUJI Hideaki 	struct net_device *dev;
1331a9b05723SYOSHIFUJI Hideaki 	int dst_type;
13321da177e4SLinus Torvalds 
1333a9b05723SYOSHIFUJI Hideaki 	dst_type = __ipv6_addr_type(daddr);
1334a9b05723SYOSHIFUJI Hideaki 	dst.addr = daddr;
1335a9b05723SYOSHIFUJI Hideaki 	dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1336a9b05723SYOSHIFUJI Hideaki 	dst.scope = __ipv6_addr_src_scope(dst_type);
13373de23255SBenjamin Thery 	dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
13387cbca67cSYOSHIFUJI Hideaki 	dst.prefs = prefs;
1339a9b05723SYOSHIFUJI Hideaki 
1340a9b05723SYOSHIFUJI Hideaki 	hiscore->rule = -1;
1341a9b05723SYOSHIFUJI Hideaki 	hiscore->ifa = NULL;
13421da177e4SLinus Torvalds 
13438814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
1344072047e4SYOSHIFUJI Hideaki 
1345c6d14c84SEric Dumazet 	for_each_netdev_rcu(net, dev) {
1346072047e4SYOSHIFUJI Hideaki 		struct inet6_dev *idev;
1347072047e4SYOSHIFUJI Hideaki 
1348a9b05723SYOSHIFUJI Hideaki 		/* Candidate Source Address (section 4)
1349072047e4SYOSHIFUJI Hideaki 		 *  - multicast and link-local destination address,
1350072047e4SYOSHIFUJI Hideaki 		 *    the set of candidate source address MUST only
1351072047e4SYOSHIFUJI Hideaki 		 *    include addresses assigned to interfaces
1352072047e4SYOSHIFUJI Hideaki 		 *    belonging to the same link as the outgoing
1353072047e4SYOSHIFUJI Hideaki 		 *    interface.
1354072047e4SYOSHIFUJI Hideaki 		 * (- For site-local destination addresses, the
1355072047e4SYOSHIFUJI Hideaki 		 *    set of candidate source addresses MUST only
1356072047e4SYOSHIFUJI Hideaki 		 *    include addresses assigned to interfaces
1357072047e4SYOSHIFUJI Hideaki 		 *    belonging to the same site as the outgoing
1358072047e4SYOSHIFUJI Hideaki 		 *    interface.)
1359072047e4SYOSHIFUJI Hideaki 		 */
1360a9b05723SYOSHIFUJI Hideaki 		if (((dst_type & IPV6_ADDR_MULTICAST) ||
1361a9b05723SYOSHIFUJI Hideaki 		     dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
1362a9b05723SYOSHIFUJI Hideaki 		    dst.ifindex && dev->ifindex != dst.ifindex)
1363072047e4SYOSHIFUJI Hideaki 			continue;
1364072047e4SYOSHIFUJI Hideaki 
13651da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
1366072047e4SYOSHIFUJI Hideaki 		if (!idev)
1367072047e4SYOSHIFUJI Hideaki 			continue;
1368072047e4SYOSHIFUJI Hideaki 
13691da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
1370502a2ffdSstephen hemminger 		list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1371a9b05723SYOSHIFUJI Hideaki 			int i;
1372072047e4SYOSHIFUJI Hideaki 
1373a9b05723SYOSHIFUJI Hideaki 			/*
13746b3ae80aSYOSHIFUJI Hideaki 			 * - Tentative Address (RFC2462 section 5.4)
13756b3ae80aSYOSHIFUJI Hideaki 			 *  - A tentative address is not considered
13766b3ae80aSYOSHIFUJI Hideaki 			 *    "assigned to an interface" in the traditional
137795c385b4SNeil Horman 			 *    sense, unless it is also flagged as optimistic.
13786b3ae80aSYOSHIFUJI Hideaki 			 * - Candidate Source Address (section 4)
1379072047e4SYOSHIFUJI Hideaki 			 *  - In any case, anycast addresses, multicast
1380072047e4SYOSHIFUJI Hideaki 			 *    addresses, and the unspecified address MUST
1381072047e4SYOSHIFUJI Hideaki 			 *    NOT be included in a candidate set.
1382072047e4SYOSHIFUJI Hideaki 			 */
1383a9b05723SYOSHIFUJI Hideaki 			if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1384a9b05723SYOSHIFUJI Hideaki 			    (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
13856b3ae80aSYOSHIFUJI Hideaki 				continue;
1386a9b05723SYOSHIFUJI Hideaki 
1387a9b05723SYOSHIFUJI Hideaki 			score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1388a9b05723SYOSHIFUJI Hideaki 
1389a9b05723SYOSHIFUJI Hideaki 			if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1390a9b05723SYOSHIFUJI Hideaki 				     score->addr_type & IPV6_ADDR_MULTICAST)) {
1391072047e4SYOSHIFUJI Hideaki 				LIMIT_NETDEBUG(KERN_DEBUG
1392072047e4SYOSHIFUJI Hideaki 					       "ADDRCONF: unspecified / multicast address "
1393072047e4SYOSHIFUJI Hideaki 					       "assigned as unicast address on %s",
1394072047e4SYOSHIFUJI Hideaki 					       dev->name);
13951da177e4SLinus Torvalds 				continue;
1396072047e4SYOSHIFUJI Hideaki 			}
1397072047e4SYOSHIFUJI Hideaki 
1398a9b05723SYOSHIFUJI Hideaki 			score->rule = -1;
1399a9b05723SYOSHIFUJI Hideaki 			bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1400072047e4SYOSHIFUJI Hideaki 
1401a9b05723SYOSHIFUJI Hideaki 			for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1402a9b05723SYOSHIFUJI Hideaki 				int minihiscore, miniscore;
1403072047e4SYOSHIFUJI Hideaki 
14043de23255SBenjamin Thery 				minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i);
14053de23255SBenjamin Thery 				miniscore = ipv6_get_saddr_eval(net, score, &dst, i);
1406072047e4SYOSHIFUJI Hideaki 
1407a9b05723SYOSHIFUJI Hideaki 				if (minihiscore > miniscore) {
1408a9b05723SYOSHIFUJI Hideaki 					if (i == IPV6_SADDR_RULE_SCOPE &&
1409a9b05723SYOSHIFUJI Hideaki 					    score->scopedist > 0) {
1410a9b05723SYOSHIFUJI Hideaki 						/*
1411a9b05723SYOSHIFUJI Hideaki 						 * special case:
1412a9b05723SYOSHIFUJI Hideaki 						 * each remaining entry
1413a9b05723SYOSHIFUJI Hideaki 						 * has too small (not enough)
1414a9b05723SYOSHIFUJI Hideaki 						 * scope, because ifa entries
1415a9b05723SYOSHIFUJI Hideaki 						 * are sorted by their scope
1416a9b05723SYOSHIFUJI Hideaki 						 * values.
1417072047e4SYOSHIFUJI Hideaki 						 */
1418a9b05723SYOSHIFUJI Hideaki 						goto try_nextdev;
1419072047e4SYOSHIFUJI Hideaki 					}
1420a9b05723SYOSHIFUJI Hideaki 					break;
1421a9b05723SYOSHIFUJI Hideaki 				} else if (minihiscore < miniscore) {
1422a9b05723SYOSHIFUJI Hideaki 					if (hiscore->ifa)
1423a9b05723SYOSHIFUJI Hideaki 						in6_ifa_put(hiscore->ifa);
142499cd07a5SJuha-Matti Tapio 
1425a9b05723SYOSHIFUJI Hideaki 					in6_ifa_hold(score->ifa);
1426072047e4SYOSHIFUJI Hideaki 
1427a0bffffcSIlpo Järvinen 					swap(hiscore, score);
1428a9b05723SYOSHIFUJI Hideaki 
1429a9b05723SYOSHIFUJI Hideaki 					/* restore our iterator */
1430a9b05723SYOSHIFUJI Hideaki 					score->ifa = hiscore->ifa;
1431a9b05723SYOSHIFUJI Hideaki 
1432a9b05723SYOSHIFUJI Hideaki 					break;
14331da177e4SLinus Torvalds 				}
1434a9b05723SYOSHIFUJI Hideaki 			}
1435a9b05723SYOSHIFUJI Hideaki 		}
1436a9b05723SYOSHIFUJI Hideaki try_nextdev:
14371da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
14381da177e4SLinus Torvalds 	}
14398814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
14401da177e4SLinus Torvalds 
1441a9b05723SYOSHIFUJI Hideaki 	if (!hiscore->ifa)
1442072047e4SYOSHIFUJI Hideaki 		return -EADDRNOTAVAIL;
14431da177e4SLinus Torvalds 
14444e3fd7a0SAlexey Dobriyan 	*saddr = hiscore->ifa->addr;
1445a9b05723SYOSHIFUJI Hideaki 	in6_ifa_put(hiscore->ifa);
1446072047e4SYOSHIFUJI Hideaki 	return 0;
14471da177e4SLinus Torvalds }
14485e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr);
14491da177e4SLinus Torvalds 
145095c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
145195c385b4SNeil Horman 		    unsigned char banned_flags)
14521da177e4SLinus Torvalds {
14531da177e4SLinus Torvalds 	struct inet6_dev *idev;
14541da177e4SLinus Torvalds 	int err = -EADDRNOTAVAIL;
14551da177e4SLinus Torvalds 
14568814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
1457e21e8467SStephen Hemminger 	idev = __in6_dev_get(dev);
1458e21e8467SStephen Hemminger 	if (idev) {
14591da177e4SLinus Torvalds 		struct inet6_ifaddr *ifp;
14601da177e4SLinus Torvalds 
14611da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
1462502a2ffdSstephen hemminger 		list_for_each_entry(ifp, &idev->addr_list, if_list) {
1463502a2ffdSstephen hemminger 			if (ifp->scope == IFA_LINK &&
1464502a2ffdSstephen hemminger 			    !(ifp->flags & banned_flags)) {
14654e3fd7a0SAlexey Dobriyan 				*addr = ifp->addr;
14661da177e4SLinus Torvalds 				err = 0;
14671da177e4SLinus Torvalds 				break;
14681da177e4SLinus Torvalds 			}
14691da177e4SLinus Torvalds 		}
14701da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
14711da177e4SLinus Torvalds 	}
14728814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
14731da177e4SLinus Torvalds 	return err;
14741da177e4SLinus Torvalds }
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev)
14771da177e4SLinus Torvalds {
14781da177e4SLinus Torvalds 	int cnt = 0;
14791da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
1482502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list)
14831da177e4SLinus Torvalds 		cnt++;
14841da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
14851da177e4SLinus Torvalds 	return cnt;
14861da177e4SLinus Torvalds }
14871da177e4SLinus Torvalds 
1488b71d1d42SEric Dumazet int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
14892a7851bfSFlorian Westphal 		  const struct net_device *dev, int strict)
14901da177e4SLinus Torvalds {
1491eedf042aSStephen Hemminger 	struct inet6_ifaddr *ifp;
1492ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
14931da177e4SLinus Torvalds 
14945c578aedSstephen hemminger 	rcu_read_lock_bh();
1495b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1496878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
1497bfeade08SDaniel Lezcano 			continue;
14981da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr) &&
1499eedf042aSStephen Hemminger 		    !(ifp->flags&IFA_F_TENTATIVE) &&
1500eedf042aSStephen Hemminger 		    (dev == NULL || ifp->idev->dev == dev ||
1501eedf042aSStephen Hemminger 		     !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
15025c578aedSstephen hemminger 			rcu_read_unlock_bh();
1503eedf042aSStephen Hemminger 			return 1;
1504eedf042aSStephen Hemminger 		}
1505eedf042aSStephen Hemminger 	}
15065c578aedSstephen hemminger 
1507eedf042aSStephen Hemminger 	rcu_read_unlock_bh();
1508eedf042aSStephen Hemminger 	return 0;
15091da177e4SLinus Torvalds }
15107159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_addr);
15117159039aSYOSHIFUJI Hideaki 
15123e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
151306bfe655SDaniel Lezcano 			       struct net_device *dev)
15141da177e4SLinus Torvalds {
1515ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
15161da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
15171da177e4SLinus Torvalds 
1518b67bfe0dSSasha Levin 	hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
1519878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
152006bfe655SDaniel Lezcano 			continue;
15211da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
15221da177e4SLinus Torvalds 			if (dev == NULL || ifp->idev->dev == dev)
15233e81c6daSDavid S. Miller 				return true;
15241da177e4SLinus Torvalds 		}
15251da177e4SLinus Torvalds 	}
15263e81c6daSDavid S. Miller 	return false;
15271da177e4SLinus Torvalds }
15281da177e4SLinus Torvalds 
1529b71d1d42SEric Dumazet int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
153052eeeb84SYOSHIFUJI Hideaki {
153152eeeb84SYOSHIFUJI Hideaki 	struct inet6_dev *idev;
153252eeeb84SYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifa;
153352eeeb84SYOSHIFUJI Hideaki 	int	onlink;
153452eeeb84SYOSHIFUJI Hideaki 
153552eeeb84SYOSHIFUJI Hideaki 	onlink = 0;
153652eeeb84SYOSHIFUJI Hideaki 	rcu_read_lock();
153752eeeb84SYOSHIFUJI Hideaki 	idev = __in6_dev_get(dev);
153852eeeb84SYOSHIFUJI Hideaki 	if (idev) {
153952eeeb84SYOSHIFUJI Hideaki 		read_lock_bh(&idev->lock);
1540502a2ffdSstephen hemminger 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
154152eeeb84SYOSHIFUJI Hideaki 			onlink = ipv6_prefix_equal(addr, &ifa->addr,
154252eeeb84SYOSHIFUJI Hideaki 						   ifa->prefix_len);
154352eeeb84SYOSHIFUJI Hideaki 			if (onlink)
154452eeeb84SYOSHIFUJI Hideaki 				break;
154552eeeb84SYOSHIFUJI Hideaki 		}
154652eeeb84SYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
154752eeeb84SYOSHIFUJI Hideaki 	}
154852eeeb84SYOSHIFUJI Hideaki 	rcu_read_unlock();
154952eeeb84SYOSHIFUJI Hideaki 	return onlink;
155052eeeb84SYOSHIFUJI Hideaki }
155152eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix);
155252eeeb84SYOSHIFUJI Hideaki 
15539acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
15541cab3da6SDaniel Lezcano 				     struct net_device *dev, int strict)
15551da177e4SLinus Torvalds {
1556b79d1d54SDavid S. Miller 	struct inet6_ifaddr *ifp, *result = NULL;
1557ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
15581da177e4SLinus Torvalds 
15595c578aedSstephen hemminger 	rcu_read_lock_bh();
1560b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
1561878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
15621cab3da6SDaniel Lezcano 			continue;
15631da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
15641da177e4SLinus Torvalds 			if (dev == NULL || ifp->idev->dev == dev ||
15651da177e4SLinus Torvalds 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1566b79d1d54SDavid S. Miller 				result = ifp;
15671da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
15681da177e4SLinus Torvalds 				break;
15691da177e4SLinus Torvalds 			}
15701da177e4SLinus Torvalds 		}
15711da177e4SLinus Torvalds 	}
15725c578aedSstephen hemminger 	rcu_read_unlock_bh();
15731da177e4SLinus Torvalds 
1574b79d1d54SDavid S. Miller 	return result;
15751da177e4SLinus Torvalds }
15761da177e4SLinus Torvalds 
15771da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */
15781da177e4SLinus Torvalds 
1579cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
15801da177e4SLinus Torvalds {
15811da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_PERMANENT) {
15821da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
15831da177e4SLinus Torvalds 		addrconf_del_timer(ifp);
15841da177e4SLinus Torvalds 		ifp->flags |= IFA_F_TENTATIVE;
1585cc411d0bSBrian Haley 		if (dad_failed)
1586cc411d0bSBrian Haley 			ifp->flags |= IFA_F_DADFAILED;
15871da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
1588e2577a06SHerbert Xu 		if (dad_failed)
1589e2577a06SHerbert Xu 			ipv6_ifa_notify(0, ifp);
15901da177e4SLinus Torvalds 		in6_ifa_put(ifp);
15911da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
15921da177e4SLinus Torvalds 	} else if (ifp->flags&IFA_F_TEMPORARY) {
15931da177e4SLinus Torvalds 		struct inet6_ifaddr *ifpub;
15941da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
15951da177e4SLinus Torvalds 		ifpub = ifp->ifpub;
15961da177e4SLinus Torvalds 		if (ifpub) {
15971da177e4SLinus Torvalds 			in6_ifa_hold(ifpub);
15981da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
15991da177e4SLinus Torvalds 			ipv6_create_tempaddr(ifpub, ifp);
16001da177e4SLinus Torvalds 			in6_ifa_put(ifpub);
16011da177e4SLinus Torvalds 		} else {
16021da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
16031da177e4SLinus Torvalds 		}
16041da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
16051da177e4SLinus Torvalds #endif
16061da177e4SLinus Torvalds 	} else
16071da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
16081da177e4SLinus Torvalds }
16091da177e4SLinus Torvalds 
1610f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1611f2344a13SHerbert Xu {
1612f2344a13SHerbert Xu 	int err = -ENOENT;
1613f2344a13SHerbert Xu 
1614f2344a13SHerbert Xu 	spin_lock(&ifp->state_lock);
1615f2344a13SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DAD) {
1616f2344a13SHerbert Xu 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
1617f2344a13SHerbert Xu 		err = 0;
1618f2344a13SHerbert Xu 	}
1619f2344a13SHerbert Xu 	spin_unlock(&ifp->state_lock);
1620f2344a13SHerbert Xu 
1621f2344a13SHerbert Xu 	return err;
1622f2344a13SHerbert Xu }
1623f2344a13SHerbert Xu 
16243c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp)
16253c21edbdSYOSHIFUJI Hideaki {
16261b34be74SYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
16279bdd8d40SBrian Haley 
1628853dc2e0SUrsula Braun 	if (addrconf_dad_end(ifp)) {
1629853dc2e0SUrsula Braun 		in6_ifa_put(ifp);
1630f2344a13SHerbert Xu 		return;
1631853dc2e0SUrsula Braun 	}
1632f2344a13SHerbert Xu 
1633e87cc472SJoe Perches 	net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
16340522fea6SJens Rosenboom 			     ifp->idev->dev->name, &ifp->addr);
16359bdd8d40SBrian Haley 
16361b34be74SYOSHIFUJI Hideaki 	if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
16371b34be74SYOSHIFUJI Hideaki 		struct in6_addr addr;
16381b34be74SYOSHIFUJI Hideaki 
16391b34be74SYOSHIFUJI Hideaki 		addr.s6_addr32[0] = htonl(0xfe800000);
16401b34be74SYOSHIFUJI Hideaki 		addr.s6_addr32[1] = 0;
16411b34be74SYOSHIFUJI Hideaki 
16421b34be74SYOSHIFUJI Hideaki 		if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
16431b34be74SYOSHIFUJI Hideaki 		    ipv6_addr_equal(&ifp->addr, &addr)) {
16441b34be74SYOSHIFUJI Hideaki 			/* DAD failed for link-local based on MAC address */
16451b34be74SYOSHIFUJI Hideaki 			idev->cnf.disable_ipv6 = 1;
16469bdd8d40SBrian Haley 
1647f3213831SJoe Perches 			pr_info("%s: IPv6 being disabled!\n",
16489bdd8d40SBrian Haley 				ifp->idev->dev->name);
16491b34be74SYOSHIFUJI Hideaki 		}
16501b34be74SYOSHIFUJI Hideaki 	}
16511b34be74SYOSHIFUJI Hideaki 
1652cc411d0bSBrian Haley 	addrconf_dad_stop(ifp, 1);
16533c21edbdSYOSHIFUJI Hideaki }
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds /* Join to solicited addr multicast group. */
16561da177e4SLinus Torvalds 
1657b71d1d42SEric Dumazet void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
16581da177e4SLinus Torvalds {
16591da177e4SLinus Torvalds 	struct in6_addr maddr;
16601da177e4SLinus Torvalds 
16611da177e4SLinus Torvalds 	if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
16621da177e4SLinus Torvalds 		return;
16631da177e4SLinus Torvalds 
16641da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
16651da177e4SLinus Torvalds 	ipv6_dev_mc_inc(dev, &maddr);
16661da177e4SLinus Torvalds }
16671da177e4SLinus Torvalds 
1668b71d1d42SEric Dumazet void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
16691da177e4SLinus Torvalds {
16701da177e4SLinus Torvalds 	struct in6_addr maddr;
16711da177e4SLinus Torvalds 
16721da177e4SLinus Torvalds 	if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
16731da177e4SLinus Torvalds 		return;
16741da177e4SLinus Torvalds 
16751da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
16761da177e4SLinus Torvalds 	__ipv6_dev_mc_dec(idev, &maddr);
16771da177e4SLinus Torvalds }
16781da177e4SLinus Torvalds 
167920380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
16801da177e4SLinus Torvalds {
16811da177e4SLinus Torvalds 	struct in6_addr addr;
16822bda8a0cSBjørn Mork 	if (ifp->prefix_len == 127) /* RFC 6164 */
16832bda8a0cSBjørn Mork 		return;
16841da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
16851da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
16861da177e4SLinus Torvalds 		return;
16871da177e4SLinus Torvalds 	ipv6_dev_ac_inc(ifp->idev->dev, &addr);
16881da177e4SLinus Torvalds }
16891da177e4SLinus Torvalds 
169020380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
16911da177e4SLinus Torvalds {
16921da177e4SLinus Torvalds 	struct in6_addr addr;
169332019e65SYOSHIFUJI Hideaki 	if (ifp->prefix_len == 127) /* RFC 6164 */
169432019e65SYOSHIFUJI Hideaki 		return;
16951da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
16961da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
16971da177e4SLinus Torvalds 		return;
16981da177e4SLinus Torvalds 	__ipv6_dev_ac_dec(ifp->idev, &addr);
16991da177e4SLinus Torvalds }
17001da177e4SLinus Torvalds 
1701073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
17021da177e4SLinus Torvalds {
17031da177e4SLinus Torvalds 	if (dev->addr_len != ETH_ALEN)
17041da177e4SLinus Torvalds 		return -1;
17051da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr, 3);
17061da177e4SLinus Torvalds 	memcpy(eui + 5, dev->dev_addr + 3, 3);
17071da177e4SLinus Torvalds 
17081da177e4SLinus Torvalds 	/*
17091da177e4SLinus Torvalds 	 * The zSeries OSA network cards can be shared among various
17101da177e4SLinus Torvalds 	 * OS instances, but the OSA cards have only one MAC address.
17111da177e4SLinus Torvalds 	 * This leads to duplicate address conflicts in conjunction
17121da177e4SLinus Torvalds 	 * with IPv6 if more than one instance uses the same card.
17131da177e4SLinus Torvalds 	 *
17141da177e4SLinus Torvalds 	 * The driver for these cards can deliver a unique 16-bit
17151da177e4SLinus Torvalds 	 * identifier for each instance sharing the same card.  It is
17161da177e4SLinus Torvalds 	 * placed instead of 0xFFFE in the interface identifier.  The
17171da177e4SLinus Torvalds 	 * "u" bit of the interface identifier is not inverted in this
17181da177e4SLinus Torvalds 	 * case.  Hence the resulting interface identifier has local
17191da177e4SLinus Torvalds 	 * scope according to RFC2373.
17201da177e4SLinus Torvalds 	 */
17211da177e4SLinus Torvalds 	if (dev->dev_id) {
17221da177e4SLinus Torvalds 		eui[3] = (dev->dev_id >> 8) & 0xFF;
17231da177e4SLinus Torvalds 		eui[4] = dev->dev_id & 0xFF;
17241da177e4SLinus Torvalds 	} else {
17251da177e4SLinus Torvalds 		eui[3] = 0xFF;
17261da177e4SLinus Torvalds 		eui[4] = 0xFE;
17271da177e4SLinus Torvalds 		eui[0] ^= 2;
17281da177e4SLinus Torvalds 	}
17291da177e4SLinus Torvalds 	return 0;
1730073a8e0eSYOSHIFUJI Hideaki }
1731073a8e0eSYOSHIFUJI Hideaki 
173206a4c1c5Salex.bluesman.smirnov@gmail.com static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
173306a4c1c5Salex.bluesman.smirnov@gmail.com {
173406a4c1c5Salex.bluesman.smirnov@gmail.com 	if (dev->addr_len != IEEE802154_ADDR_LEN)
173506a4c1c5Salex.bluesman.smirnov@gmail.com 		return -1;
173606a4c1c5Salex.bluesman.smirnov@gmail.com 	memcpy(eui, dev->dev_addr, 8);
17375e98a36eSYOSHIFUJI Hideaki / 吉藤英明 	eui[0] ^= 2;
173806a4c1c5Salex.bluesman.smirnov@gmail.com 	return 0;
173906a4c1c5Salex.bluesman.smirnov@gmail.com }
174006a4c1c5Salex.bluesman.smirnov@gmail.com 
1741cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
1742cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 {
1743cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	union fwnet_hwaddr *ha;
1744cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
1745cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	if (dev->addr_len != FWNET_ALEN)
1746cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 		return -1;
1747cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
1748cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	ha = (union fwnet_hwaddr *)dev->dev_addr;
1749cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
1750cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
1751cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	eui[0] ^= 2;
1752cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	return 0;
1753cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 }
1754cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
1755073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1756073a8e0eSYOSHIFUJI Hideaki {
17571da177e4SLinus Torvalds 	/* XXX: inherit EUI-64 from other interface -- yoshfuji */
17581da177e4SLinus Torvalds 	if (dev->addr_len != ARCNET_ALEN)
17591da177e4SLinus Torvalds 		return -1;
17601da177e4SLinus Torvalds 	memset(eui, 0, 7);
17611da177e4SLinus Torvalds 	eui[7] = *(u8 *)dev->dev_addr;
17621da177e4SLinus Torvalds 	return 0;
1763073a8e0eSYOSHIFUJI Hideaki }
1764073a8e0eSYOSHIFUJI Hideaki 
1765073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1766073a8e0eSYOSHIFUJI Hideaki {
17671da177e4SLinus Torvalds 	if (dev->addr_len != INFINIBAND_ALEN)
17681da177e4SLinus Torvalds 		return -1;
17691da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr + 12, 8);
17701da177e4SLinus Torvalds 	eui[0] |= 2;
17711da177e4SLinus Torvalds 	return 0;
17721da177e4SLinus Torvalds }
1773073a8e0eSYOSHIFUJI Hideaki 
1774c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
1775dfd982baSYOSHIFUJI Hideaki {
17769af28511SSascha Hlusiak 	if (addr == 0)
17779af28511SSascha Hlusiak 		return -1;
1778dfd982baSYOSHIFUJI Hideaki 	eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1779dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1780dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1781dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1782dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1783dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1784dfd982baSYOSHIFUJI Hideaki 	eui[1] = 0;
1785dfd982baSYOSHIFUJI Hideaki 	eui[2] = 0x5E;
1786dfd982baSYOSHIFUJI Hideaki 	eui[3] = 0xFE;
1787dfd982baSYOSHIFUJI Hideaki 	memcpy(eui + 4, &addr, 4);
1788dfd982baSYOSHIFUJI Hideaki 	return 0;
1789dfd982baSYOSHIFUJI Hideaki }
1790dfd982baSYOSHIFUJI Hideaki 
1791dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1792dfd982baSYOSHIFUJI Hideaki {
1793dfd982baSYOSHIFUJI Hideaki 	if (dev->priv_flags & IFF_ISATAP)
1794dfd982baSYOSHIFUJI Hideaki 		return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1795dfd982baSYOSHIFUJI Hideaki 	return -1;
1796dfd982baSYOSHIFUJI Hideaki }
1797dfd982baSYOSHIFUJI Hideaki 
1798aee80b54Sstephen hemminger static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
1799aee80b54Sstephen hemminger {
1800aee80b54Sstephen hemminger 	return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1801aee80b54Sstephen hemminger }
1802aee80b54Sstephen hemminger 
1803073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1804073a8e0eSYOSHIFUJI Hideaki {
1805073a8e0eSYOSHIFUJI Hideaki 	switch (dev->type) {
1806073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ETHER:
1807073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_FDDI:
1808073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_eui48(eui, dev);
1809073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ARCNET:
1810073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_arcnet(eui, dev);
1811073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_INFINIBAND:
1812073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_infiniband(eui, dev);
1813c7dc89c0SFred L. Templin 	case ARPHRD_SIT:
1814dfd982baSYOSHIFUJI Hideaki 		return addrconf_ifid_sit(eui, dev);
1815aee80b54Sstephen hemminger 	case ARPHRD_IPGRE:
1816aee80b54Sstephen hemminger 		return addrconf_ifid_gre(eui, dev);
181706a4c1c5Salex.bluesman.smirnov@gmail.com 	case ARPHRD_IEEE802154:
181806a4c1c5Salex.bluesman.smirnov@gmail.com 		return addrconf_ifid_eui64(eui, dev);
1819cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	case ARPHRD_IEEE1394:
1820cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 		return addrconf_ifid_ieee1394(eui, dev);
1821073a8e0eSYOSHIFUJI Hideaki 	}
18221da177e4SLinus Torvalds 	return -1;
18231da177e4SLinus Torvalds }
18241da177e4SLinus Torvalds 
18251da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
18261da177e4SLinus Torvalds {
18271da177e4SLinus Torvalds 	int err = -1;
18281da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
18291da177e4SLinus Torvalds 
18301da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
1831502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
18321da177e4SLinus Torvalds 		if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
18331da177e4SLinus Torvalds 			memcpy(eui, ifp->addr.s6_addr+8, 8);
18341da177e4SLinus Torvalds 			err = 0;
18351da177e4SLinus Torvalds 			break;
18361da177e4SLinus Torvalds 		}
18371da177e4SLinus Torvalds 	}
18381da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
18391da177e4SLinus Torvalds 	return err;
18401da177e4SLinus Torvalds }
18411da177e4SLinus Torvalds 
18421da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
18431da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1844eb7e0575SSorin Dumitru static void __ipv6_regen_rndid(struct inet6_dev *idev)
18451da177e4SLinus Torvalds {
18461da177e4SLinus Torvalds regen:
1847955189efSYOSHIFUJI Hideaki 	get_random_bytes(idev->rndid, sizeof(idev->rndid));
18481da177e4SLinus Torvalds 	idev->rndid[0] &= ~0x02;
18491da177e4SLinus Torvalds 
18501da177e4SLinus Torvalds 	/*
18511da177e4SLinus Torvalds 	 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
18521da177e4SLinus Torvalds 	 * check if generated address is not inappropriate
18531da177e4SLinus Torvalds 	 *
18541da177e4SLinus Torvalds 	 *  - Reserved subnet anycast (RFC 2526)
18551da177e4SLinus Torvalds 	 *	11111101 11....11 1xxxxxxx
1856c7dc89c0SFred L. Templin 	 *  - ISATAP (RFC4214) 6.1
18571da177e4SLinus Torvalds 	 *	00-00-5E-FE-xx-xx-xx-xx
18581da177e4SLinus Torvalds 	 *  - value 0
18591da177e4SLinus Torvalds 	 *  - XXX: already assigned to an address on the device
18601da177e4SLinus Torvalds 	 */
18611da177e4SLinus Torvalds 	if (idev->rndid[0] == 0xfd &&
18621da177e4SLinus Torvalds 	    (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
18631da177e4SLinus Torvalds 	    (idev->rndid[7]&0x80))
18641da177e4SLinus Torvalds 		goto regen;
18651da177e4SLinus Torvalds 	if ((idev->rndid[0]|idev->rndid[1]) == 0) {
18661da177e4SLinus Torvalds 		if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
18671da177e4SLinus Torvalds 			goto regen;
18681da177e4SLinus Torvalds 		if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
18691da177e4SLinus Torvalds 			goto regen;
18701da177e4SLinus Torvalds 	}
18711da177e4SLinus Torvalds }
18721da177e4SLinus Torvalds 
18731da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data)
18741da177e4SLinus Torvalds {
18751da177e4SLinus Torvalds 	struct inet6_dev *idev = (struct inet6_dev *) data;
18761da177e4SLinus Torvalds 	unsigned long expires;
18771da177e4SLinus Torvalds 
18788814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
18791da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
18801da177e4SLinus Torvalds 
18811da177e4SLinus Torvalds 	if (idev->dead)
18821da177e4SLinus Torvalds 		goto out;
18831da177e4SLinus Torvalds 
1884eb7e0575SSorin Dumitru 	__ipv6_regen_rndid(idev);
18851da177e4SLinus Torvalds 
18861da177e4SLinus Torvalds 	expires = jiffies +
18871da177e4SLinus Torvalds 		idev->cnf.temp_prefered_lft * HZ -
1888784e2710SBen Hutchings 		idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time -
1889784e2710SBen Hutchings 		idev->cnf.max_desync_factor * HZ;
18901da177e4SLinus Torvalds 	if (time_before(expires, jiffies)) {
1891f3213831SJoe Perches 		pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
1892f3213831SJoe Perches 			__func__, idev->dev->name);
18931da177e4SLinus Torvalds 		goto out;
18941da177e4SLinus Torvalds 	}
18951da177e4SLinus Torvalds 
18961da177e4SLinus Torvalds 	if (!mod_timer(&idev->regen_timer, expires))
18971da177e4SLinus Torvalds 		in6_dev_hold(idev);
18981da177e4SLinus Torvalds 
18991da177e4SLinus Torvalds out:
19001da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
19018814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
19021da177e4SLinus Torvalds 	in6_dev_put(idev);
19031da177e4SLinus Torvalds }
19041da177e4SLinus Torvalds 
1905eb7e0575SSorin Dumitru static void  __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
19068e5e8f30SEldad Zack {
19071da177e4SLinus Torvalds 	if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1908eb7e0575SSorin Dumitru 		__ipv6_regen_rndid(idev);
19091da177e4SLinus Torvalds }
19101da177e4SLinus Torvalds #endif
19111da177e4SLinus Torvalds 
19121da177e4SLinus Torvalds /*
19131da177e4SLinus Torvalds  *	Add prefix route.
19141da177e4SLinus Torvalds  */
19151da177e4SLinus Torvalds 
19161da177e4SLinus Torvalds static void
19171da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1918e431b8c0SJamal Hadi Salim 		      unsigned long expires, u32 flags)
19191da177e4SLinus Torvalds {
192086872cb5SThomas Graf 	struct fib6_config cfg = {
192186872cb5SThomas Graf 		.fc_table = RT6_TABLE_PREFIX,
192286872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
192386872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
192486872cb5SThomas Graf 		.fc_expires = expires,
192586872cb5SThomas Graf 		.fc_dst_len = plen,
192686872cb5SThomas Graf 		.fc_flags = RTF_UP | flags,
1927c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
1928f410a1fbSStephen Hemminger 		.fc_protocol = RTPROT_KERNEL,
192986872cb5SThomas Graf 	};
19301da177e4SLinus Torvalds 
19314e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *pfx;
19321da177e4SLinus Torvalds 
19331da177e4SLinus Torvalds 	/* Prevent useless cloning on PtP SIT.
19341da177e4SLinus Torvalds 	   This thing is done here expecting that the whole
19351da177e4SLinus Torvalds 	   class of non-broadcast devices need not cloning.
19361da177e4SLinus Torvalds 	 */
193707a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
19381da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
193986872cb5SThomas Graf 		cfg.fc_flags |= RTF_NONEXTHOP;
19400be669bbSJoerg Roedel #endif
19411da177e4SLinus Torvalds 
194286872cb5SThomas Graf 	ip6_route_add(&cfg);
19431da177e4SLinus Torvalds }
19441da177e4SLinus Torvalds 
194514ef37b6SAndreas Hofmeister 
194614ef37b6SAndreas Hofmeister static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
194714ef37b6SAndreas Hofmeister 						  int plen,
194814ef37b6SAndreas Hofmeister 						  const struct net_device *dev,
194914ef37b6SAndreas Hofmeister 						  u32 flags, u32 noflags)
195014ef37b6SAndreas Hofmeister {
195114ef37b6SAndreas Hofmeister 	struct fib6_node *fn;
195214ef37b6SAndreas Hofmeister 	struct rt6_info *rt = NULL;
195314ef37b6SAndreas Hofmeister 	struct fib6_table *table;
195414ef37b6SAndreas Hofmeister 
195514ef37b6SAndreas Hofmeister 	table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX);
195614ef37b6SAndreas Hofmeister 	if (table == NULL)
195714ef37b6SAndreas Hofmeister 		return NULL;
195814ef37b6SAndreas Hofmeister 
19595744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
196014ef37b6SAndreas Hofmeister 	fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
196114ef37b6SAndreas Hofmeister 	if (!fn)
196214ef37b6SAndreas Hofmeister 		goto out;
196314ef37b6SAndreas Hofmeister 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1964d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != dev->ifindex)
196514ef37b6SAndreas Hofmeister 			continue;
196614ef37b6SAndreas Hofmeister 		if ((rt->rt6i_flags & flags) != flags)
196714ef37b6SAndreas Hofmeister 			continue;
196885da53bfSRomain Kuntz 		if ((rt->rt6i_flags & noflags) != 0)
196914ef37b6SAndreas Hofmeister 			continue;
197014ef37b6SAndreas Hofmeister 		dst_hold(&rt->dst);
197114ef37b6SAndreas Hofmeister 		break;
197214ef37b6SAndreas Hofmeister 	}
197314ef37b6SAndreas Hofmeister out:
19745744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
197514ef37b6SAndreas Hofmeister 	return rt;
197614ef37b6SAndreas Hofmeister }
197714ef37b6SAndreas Hofmeister 
197814ef37b6SAndreas Hofmeister 
19791da177e4SLinus Torvalds /* Create "default" multicast route to the interface */
19801da177e4SLinus Torvalds 
19811da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev)
19821da177e4SLinus Torvalds {
198386872cb5SThomas Graf 	struct fib6_config cfg = {
198486872cb5SThomas Graf 		.fc_table = RT6_TABLE_LOCAL,
198586872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
198686872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
198786872cb5SThomas Graf 		.fc_dst_len = 8,
198886872cb5SThomas Graf 		.fc_flags = RTF_UP,
1989c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
199086872cb5SThomas Graf 	};
19911da177e4SLinus Torvalds 
199286872cb5SThomas Graf 	ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
199386872cb5SThomas Graf 
199486872cb5SThomas Graf 	ip6_route_add(&cfg);
19951da177e4SLinus Torvalds }
19961da177e4SLinus Torvalds 
199707a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
19981da177e4SLinus Torvalds static void sit_route_add(struct net_device *dev)
19991da177e4SLinus Torvalds {
200086872cb5SThomas Graf 	struct fib6_config cfg = {
200186872cb5SThomas Graf 		.fc_table = RT6_TABLE_MAIN,
200286872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
200386872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
200486872cb5SThomas Graf 		.fc_dst_len = 96,
200586872cb5SThomas Graf 		.fc_flags = RTF_UP | RTF_NONEXTHOP,
2006c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
200786872cb5SThomas Graf 	};
20081da177e4SLinus Torvalds 
20091da177e4SLinus Torvalds 	/* prefix length - 96 bits "::d.d.d.d" */
201086872cb5SThomas Graf 	ip6_route_add(&cfg);
20111da177e4SLinus Torvalds }
20120be669bbSJoerg Roedel #endif
20131da177e4SLinus Torvalds 
20141da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
20151da177e4SLinus Torvalds {
20161da177e4SLinus Torvalds 	struct inet6_dev *idev;
20171da177e4SLinus Torvalds 
20181da177e4SLinus Torvalds 	ASSERT_RTNL();
20191da177e4SLinus Torvalds 
2020e21e8467SStephen Hemminger 	idev = ipv6_find_idev(dev);
2021e21e8467SStephen Hemminger 	if (!idev)
202264e724f6SBrian Haley 		return ERR_PTR(-ENOBUFS);
202364e724f6SBrian Haley 
202464e724f6SBrian Haley 	if (idev->cnf.disable_ipv6)
202564e724f6SBrian Haley 		return ERR_PTR(-EACCES);
20261da177e4SLinus Torvalds 
20271da177e4SLinus Torvalds 	/* Add default multicast route */
20284af04abaSLi Wei 	if (!(dev->flags & IFF_LOOPBACK))
20291da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
20301da177e4SLinus Torvalds 
20311da177e4SLinus Torvalds 	return idev;
20321da177e4SLinus Torvalds }
20331da177e4SLinus Torvalds 
2034e6bff995SNeil Horman void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
20351da177e4SLinus Torvalds {
20361da177e4SLinus Torvalds 	struct prefix_info *pinfo;
20371da177e4SLinus Torvalds 	__u32 valid_lft;
20381da177e4SLinus Torvalds 	__u32 prefered_lft;
20391da177e4SLinus Torvalds 	int addr_type;
20401da177e4SLinus Torvalds 	struct inet6_dev *in6_dev;
204156d417b1SBrian Haley 	struct net *net = dev_net(dev);
20421da177e4SLinus Torvalds 
20431da177e4SLinus Torvalds 	pinfo = (struct prefix_info *) opt;
20441da177e4SLinus Torvalds 
20451da177e4SLinus Torvalds 	if (len < sizeof(struct prefix_info)) {
20461da177e4SLinus Torvalds 		ADBG(("addrconf: prefix option too short\n"));
20471da177e4SLinus Torvalds 		return;
20481da177e4SLinus Torvalds 	}
20491da177e4SLinus Torvalds 
20501da177e4SLinus Torvalds 	/*
20511da177e4SLinus Torvalds 	 *	Validation checks ([ADDRCONF], page 19)
20521da177e4SLinus Torvalds 	 */
20531da177e4SLinus Torvalds 
20541da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&pinfo->prefix);
20551da177e4SLinus Torvalds 
20561da177e4SLinus Torvalds 	if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
20571da177e4SLinus Torvalds 		return;
20581da177e4SLinus Torvalds 
20591da177e4SLinus Torvalds 	valid_lft = ntohl(pinfo->valid);
20601da177e4SLinus Torvalds 	prefered_lft = ntohl(pinfo->prefered);
20611da177e4SLinus Torvalds 
20621da177e4SLinus Torvalds 	if (prefered_lft > valid_lft) {
2063e87cc472SJoe Perches 		net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
20641da177e4SLinus Torvalds 		return;
20651da177e4SLinus Torvalds 	}
20661da177e4SLinus Torvalds 
20671da177e4SLinus Torvalds 	in6_dev = in6_dev_get(dev);
20681da177e4SLinus Torvalds 
20691da177e4SLinus Torvalds 	if (in6_dev == NULL) {
2070e87cc472SJoe Perches 		net_dbg_ratelimited("addrconf: device %s not configured\n",
2071e87cc472SJoe Perches 				    dev->name);
20721da177e4SLinus Torvalds 		return;
20731da177e4SLinus Torvalds 	}
20741da177e4SLinus Torvalds 
20751da177e4SLinus Torvalds 	/*
20761da177e4SLinus Torvalds 	 *	Two things going on here:
20771da177e4SLinus Torvalds 	 *	1) Add routes for on-link prefixes
20781da177e4SLinus Torvalds 	 *	2) Configure prefixes with the auto flag set
20791da177e4SLinus Torvalds 	 */
20801da177e4SLinus Torvalds 
20814bed72e4SYOSHIFUJI Hideaki 	if (pinfo->onlink) {
20824bed72e4SYOSHIFUJI Hideaki 		struct rt6_info *rt;
20834bed72e4SYOSHIFUJI Hideaki 		unsigned long rt_expires;
20844bed72e4SYOSHIFUJI Hideaki 
20851da177e4SLinus Torvalds 		/* Avoid arithmetic overflow. Really, we could
20866f704992SYOSHIFUJI Hideaki 		 * save rt_expires in seconds, likely valid_lft,
20876f704992SYOSHIFUJI Hideaki 		 * but it would require division in fib gc, that it
20886f704992SYOSHIFUJI Hideaki 		 * not good.
20891da177e4SLinus Torvalds 		 */
20904bed72e4SYOSHIFUJI Hideaki 		if (HZ > USER_HZ)
20914bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
20924bed72e4SYOSHIFUJI Hideaki 		else
20934bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
20943dd4bc68SYOSHIFUJI Hideaki 
20954bed72e4SYOSHIFUJI Hideaki 		if (addrconf_finite_timeout(rt_expires))
20964bed72e4SYOSHIFUJI Hideaki 			rt_expires *= HZ;
20971da177e4SLinus Torvalds 
209814ef37b6SAndreas Hofmeister 		rt = addrconf_get_prefix_route(&pinfo->prefix,
209914ef37b6SAndreas Hofmeister 					       pinfo->prefix_len,
210014ef37b6SAndreas Hofmeister 					       dev,
210114ef37b6SAndreas Hofmeister 					       RTF_ADDRCONF | RTF_PREFIX_RT,
210214ef37b6SAndreas Hofmeister 					       RTF_GATEWAY | RTF_DEFAULT);
21031da177e4SLinus Torvalds 
210414ef37b6SAndreas Hofmeister 		if (rt) {
21056f704992SYOSHIFUJI Hideaki 			/* Autoconf prefix route */
21061da177e4SLinus Torvalds 			if (valid_lft == 0) {
2107e0a1ad73SThomas Graf 				ip6_del_rt(rt);
21081da177e4SLinus Torvalds 				rt = NULL;
21094bed72e4SYOSHIFUJI Hideaki 			} else if (addrconf_finite_timeout(rt_expires)) {
21106f704992SYOSHIFUJI Hideaki 				/* not infinity */
21111716a961SGao feng 				rt6_set_expires(rt, jiffies + rt_expires);
21126f704992SYOSHIFUJI Hideaki 			} else {
21131716a961SGao feng 				rt6_clean_expires(rt);
21141da177e4SLinus Torvalds 			}
21151da177e4SLinus Torvalds 		} else if (valid_lft) {
21166f704992SYOSHIFUJI Hideaki 			clock_t expires = 0;
21174bed72e4SYOSHIFUJI Hideaki 			int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
21184bed72e4SYOSHIFUJI Hideaki 			if (addrconf_finite_timeout(rt_expires)) {
21196f704992SYOSHIFUJI Hideaki 				/* not infinity */
21206f704992SYOSHIFUJI Hideaki 				flags |= RTF_EXPIRES;
21216f704992SYOSHIFUJI Hideaki 				expires = jiffies_to_clock_t(rt_expires);
21226f704992SYOSHIFUJI Hideaki 			}
21231da177e4SLinus Torvalds 			addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
21246f704992SYOSHIFUJI Hideaki 					      dev, expires, flags);
21251da177e4SLinus Torvalds 		}
212694e187c0SAmerigo Wang 		ip6_rt_put(rt);
21271da177e4SLinus Torvalds 	}
21281da177e4SLinus Torvalds 
21291da177e4SLinus Torvalds 	/* Try to figure out our local address for this prefix */
21301da177e4SLinus Torvalds 
21311da177e4SLinus Torvalds 	if (pinfo->autoconf && in6_dev->cnf.autoconf) {
21321da177e4SLinus Torvalds 		struct inet6_ifaddr *ifp;
21331da177e4SLinus Torvalds 		struct in6_addr addr;
21341da177e4SLinus Torvalds 		int create = 0, update_lft = 0;
2135617fe29dSDaniel Borkmann 		bool tokenized = false;
21361da177e4SLinus Torvalds 
21371da177e4SLinus Torvalds 		if (pinfo->prefix_len == 64) {
21381da177e4SLinus Torvalds 			memcpy(&addr, &pinfo->prefix, 8);
2139f53adae4SDaniel Borkmann 
2140f53adae4SDaniel Borkmann 			if (!ipv6_addr_any(&in6_dev->token)) {
2141f53adae4SDaniel Borkmann 				read_lock_bh(&in6_dev->lock);
2142f53adae4SDaniel Borkmann 				memcpy(addr.s6_addr + 8,
2143f53adae4SDaniel Borkmann 				       in6_dev->token.s6_addr + 8, 8);
2144f53adae4SDaniel Borkmann 				read_unlock_bh(&in6_dev->lock);
2145617fe29dSDaniel Borkmann 				tokenized = true;
2146f53adae4SDaniel Borkmann 			} else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
21471da177e4SLinus Torvalds 				   ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
21481da177e4SLinus Torvalds 				in6_dev_put(in6_dev);
21491da177e4SLinus Torvalds 				return;
21501da177e4SLinus Torvalds 			}
21511da177e4SLinus Torvalds 			goto ok;
21521da177e4SLinus Torvalds 		}
2153e87cc472SJoe Perches 		net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
21541da177e4SLinus Torvalds 				    pinfo->prefix_len);
21551da177e4SLinus Torvalds 		in6_dev_put(in6_dev);
21561da177e4SLinus Torvalds 		return;
21571da177e4SLinus Torvalds 
21581da177e4SLinus Torvalds ok:
21591da177e4SLinus Torvalds 
216053b7997fSYOSHIFUJI Hideaki 		ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
21611da177e4SLinus Torvalds 
21621da177e4SLinus Torvalds 		if (ifp == NULL && valid_lft) {
21631da177e4SLinus Torvalds 			int max_addresses = in6_dev->cnf.max_addresses;
216495c385b4SNeil Horman 			u32 addr_flags = 0;
216595c385b4SNeil Horman 
216695c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
216795c385b4SNeil Horman 			if (in6_dev->cnf.optimistic_dad &&
2168e6bff995SNeil Horman 			    !net->ipv6.devconf_all->forwarding && sllao)
216995c385b4SNeil Horman 				addr_flags = IFA_F_OPTIMISTIC;
217095c385b4SNeil Horman #endif
21711da177e4SLinus Torvalds 
21721da177e4SLinus Torvalds 			/* Do not allow to create too much of autoconfigured
21731da177e4SLinus Torvalds 			 * addresses; this would be too easy way to crash kernel.
21741da177e4SLinus Torvalds 			 */
21751da177e4SLinus Torvalds 			if (!max_addresses ||
21761da177e4SLinus Torvalds 			    ipv6_count_addresses(in6_dev) < max_addresses)
21771da177e4SLinus Torvalds 				ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
217895c385b4SNeil Horman 						    addr_type&IPV6_ADDR_SCOPE_MASK,
217995c385b4SNeil Horman 						    addr_flags);
21801da177e4SLinus Torvalds 
21813f0d2ba0SYOSHIFUJI Hideaki / 吉藤英明 			if (IS_ERR_OR_NULL(ifp)) {
21821da177e4SLinus Torvalds 				in6_dev_put(in6_dev);
21831da177e4SLinus Torvalds 				return;
21841da177e4SLinus Torvalds 			}
21851da177e4SLinus Torvalds 
21861da177e4SLinus Torvalds 			update_lft = create = 1;
21871da177e4SLinus Torvalds 			ifp->cstamp = jiffies;
2188617fe29dSDaniel Borkmann 			ifp->tokenized = tokenized;
2189cf22f9a2SDavid S. Miller 			addrconf_dad_start(ifp);
21901da177e4SLinus Torvalds 		}
21911da177e4SLinus Torvalds 
21921da177e4SLinus Torvalds 		if (ifp) {
21931da177e4SLinus Torvalds 			int flags;
21941da177e4SLinus Torvalds 			unsigned long now;
21951da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
21961da177e4SLinus Torvalds 			struct inet6_ifaddr *ift;
21971da177e4SLinus Torvalds #endif
21981da177e4SLinus Torvalds 			u32 stored_lft;
21991da177e4SLinus Torvalds 
22001da177e4SLinus Torvalds 			/* update lifetime (RFC2462 5.5.3 e) */
22011da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
22021da177e4SLinus Torvalds 			now = jiffies;
22031da177e4SLinus Torvalds 			if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
22041da177e4SLinus Torvalds 				stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
22051da177e4SLinus Torvalds 			else
22061da177e4SLinus Torvalds 				stored_lft = 0;
22071da177e4SLinus Torvalds 			if (!update_lft && stored_lft) {
22081da177e4SLinus Torvalds 				if (valid_lft > MIN_VALID_LIFETIME ||
22091da177e4SLinus Torvalds 				    valid_lft > stored_lft)
22101da177e4SLinus Torvalds 					update_lft = 1;
22111da177e4SLinus Torvalds 				else if (stored_lft <= MIN_VALID_LIFETIME) {
22121da177e4SLinus Torvalds 					/* valid_lft <= stored_lft is always true */
2213a1ed0526SBrian Haley 					/*
2214a1ed0526SBrian Haley 					 * RFC 4862 Section 5.5.3e:
2215a1ed0526SBrian Haley 					 * "Note that the preferred lifetime of
2216a1ed0526SBrian Haley 					 *  the corresponding address is always
2217a1ed0526SBrian Haley 					 *  reset to the Preferred Lifetime in
2218a1ed0526SBrian Haley 					 *  the received Prefix Information
2219a1ed0526SBrian Haley 					 *  option, regardless of whether the
2220a1ed0526SBrian Haley 					 *  valid lifetime is also reset or
2221a1ed0526SBrian Haley 					 *  ignored."
2222a1ed0526SBrian Haley 					 *
2223a1ed0526SBrian Haley 					 *  So if the preferred lifetime in
2224a1ed0526SBrian Haley 					 *  this advertisement is different
2225a1ed0526SBrian Haley 					 *  than what we have stored, but the
2226a1ed0526SBrian Haley 					 *  valid lifetime is invalid, just
2227a1ed0526SBrian Haley 					 *  reset prefered_lft.
2228a1ed0526SBrian Haley 					 *
2229a1ed0526SBrian Haley 					 *  We must set the valid lifetime
2230a1ed0526SBrian Haley 					 *  to the stored lifetime since we'll
2231a1ed0526SBrian Haley 					 *  be updating the timestamp below,
2232a1ed0526SBrian Haley 					 *  else we'll set it back to the
223325985edcSLucas De Marchi 					 *  minimum.
2234a1ed0526SBrian Haley 					 */
2235a1ed0526SBrian Haley 					if (prefered_lft != ifp->prefered_lft) {
2236a1ed0526SBrian Haley 						valid_lft = stored_lft;
2237a1ed0526SBrian Haley 						update_lft = 1;
2238a1ed0526SBrian Haley 					}
22391da177e4SLinus Torvalds 				} else {
22401da177e4SLinus Torvalds 					valid_lft = MIN_VALID_LIFETIME;
22411da177e4SLinus Torvalds 					if (valid_lft < prefered_lft)
22421da177e4SLinus Torvalds 						prefered_lft = valid_lft;
22431da177e4SLinus Torvalds 					update_lft = 1;
22441da177e4SLinus Torvalds 				}
22451da177e4SLinus Torvalds 			}
22461da177e4SLinus Torvalds 
22471da177e4SLinus Torvalds 			if (update_lft) {
22481da177e4SLinus Torvalds 				ifp->valid_lft = valid_lft;
22491da177e4SLinus Torvalds 				ifp->prefered_lft = prefered_lft;
22501da177e4SLinus Torvalds 				ifp->tstamp = now;
22511da177e4SLinus Torvalds 				flags = ifp->flags;
22521da177e4SLinus Torvalds 				ifp->flags &= ~IFA_F_DEPRECATED;
22531da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
22541da177e4SLinus Torvalds 
22551da177e4SLinus Torvalds 				if (!(flags&IFA_F_TENTATIVE))
22561da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
22571da177e4SLinus Torvalds 			} else
22581da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
22591da177e4SLinus Torvalds 
22601da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
22611da177e4SLinus Torvalds 			read_lock_bh(&in6_dev->lock);
22621da177e4SLinus Torvalds 			/* update all temporary addresses in the list */
226376f793e3SLorenzo Colitti 			list_for_each_entry(ift, &in6_dev->tempaddr_list,
226476f793e3SLorenzo Colitti 					    tmp_list) {
226576f793e3SLorenzo Colitti 				int age, max_valid, max_prefered;
226676f793e3SLorenzo Colitti 
2267c6fbfac2SBenoit Boissinot 				if (ifp != ift->ifpub)
2268c6fbfac2SBenoit Boissinot 					continue;
2269c6fbfac2SBenoit Boissinot 
227076f793e3SLorenzo Colitti 				/*
227176f793e3SLorenzo Colitti 				 * RFC 4941 section 3.3:
227276f793e3SLorenzo Colitti 				 * If a received option will extend the lifetime
227376f793e3SLorenzo Colitti 				 * of a public address, the lifetimes of
227476f793e3SLorenzo Colitti 				 * temporary addresses should be extended,
227576f793e3SLorenzo Colitti 				 * subject to the overall constraint that no
227676f793e3SLorenzo Colitti 				 * temporary addresses should ever remain
227776f793e3SLorenzo Colitti 				 * "valid" or "preferred" for a time longer than
227876f793e3SLorenzo Colitti 				 * (TEMP_VALID_LIFETIME) or
227976f793e3SLorenzo Colitti 				 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR),
228076f793e3SLorenzo Colitti 				 * respectively.
228176f793e3SLorenzo Colitti 				 */
228276f793e3SLorenzo Colitti 				age = (now - ift->cstamp) / HZ;
228376f793e3SLorenzo Colitti 				max_valid = in6_dev->cnf.temp_valid_lft - age;
228476f793e3SLorenzo Colitti 				if (max_valid < 0)
228576f793e3SLorenzo Colitti 					max_valid = 0;
228676f793e3SLorenzo Colitti 
228776f793e3SLorenzo Colitti 				max_prefered = in6_dev->cnf.temp_prefered_lft -
228876f793e3SLorenzo Colitti 					       in6_dev->cnf.max_desync_factor -
228976f793e3SLorenzo Colitti 					       age;
229076f793e3SLorenzo Colitti 				if (max_prefered < 0)
229176f793e3SLorenzo Colitti 					max_prefered = 0;
229276f793e3SLorenzo Colitti 
229376f793e3SLorenzo Colitti 				if (valid_lft > max_valid)
229476f793e3SLorenzo Colitti 					valid_lft = max_valid;
229576f793e3SLorenzo Colitti 
229676f793e3SLorenzo Colitti 				if (prefered_lft > max_prefered)
229776f793e3SLorenzo Colitti 					prefered_lft = max_prefered;
229876f793e3SLorenzo Colitti 
22991da177e4SLinus Torvalds 				spin_lock(&ift->lock);
23001da177e4SLinus Torvalds 				flags = ift->flags;
230176f793e3SLorenzo Colitti 				ift->valid_lft = valid_lft;
230276f793e3SLorenzo Colitti 				ift->prefered_lft = prefered_lft;
230376f793e3SLorenzo Colitti 				ift->tstamp = now;
230476f793e3SLorenzo Colitti 				if (prefered_lft > 0)
230576f793e3SLorenzo Colitti 					ift->flags &= ~IFA_F_DEPRECATED;
230676f793e3SLorenzo Colitti 
23071da177e4SLinus Torvalds 				spin_unlock(&ift->lock);
23081da177e4SLinus Torvalds 				if (!(flags&IFA_F_TENTATIVE))
23091da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ift);
23101da177e4SLinus Torvalds 			}
23111da177e4SLinus Torvalds 
2312aed65501SGlenn Wurster 			if ((create || list_empty(&in6_dev->tempaddr_list)) && in6_dev->cnf.use_tempaddr > 0) {
23131da177e4SLinus Torvalds 				/*
231476f793e3SLorenzo Colitti 				 * When a new public address is created as
231576f793e3SLorenzo Colitti 				 * described in [ADDRCONF], also create a new
231676f793e3SLorenzo Colitti 				 * temporary address. Also create a temporary
231776f793e3SLorenzo Colitti 				 * address if it's enabled but no temporary
231876f793e3SLorenzo Colitti 				 * address currently exists.
23191da177e4SLinus Torvalds 				 */
23201da177e4SLinus Torvalds 				read_unlock_bh(&in6_dev->lock);
23211da177e4SLinus Torvalds 				ipv6_create_tempaddr(ifp, NULL);
23221da177e4SLinus Torvalds 			} else {
23231da177e4SLinus Torvalds 				read_unlock_bh(&in6_dev->lock);
23241da177e4SLinus Torvalds 			}
23251da177e4SLinus Torvalds #endif
23261da177e4SLinus Torvalds 			in6_ifa_put(ifp);
23271da177e4SLinus Torvalds 			addrconf_verify(0);
23281da177e4SLinus Torvalds 		}
23291da177e4SLinus Torvalds 	}
23301da177e4SLinus Torvalds 	inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
23311da177e4SLinus Torvalds 	in6_dev_put(in6_dev);
23321da177e4SLinus Torvalds }
23331da177e4SLinus Torvalds 
23341da177e4SLinus Torvalds /*
23351da177e4SLinus Torvalds  *	Set destination address.
23361da177e4SLinus Torvalds  *	Special case for SIT interfaces where we create a new "virtual"
23371da177e4SLinus Torvalds  *	device.
23381da177e4SLinus Torvalds  */
2339af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg)
23401da177e4SLinus Torvalds {
23411da177e4SLinus Torvalds 	struct in6_ifreq ireq;
23421da177e4SLinus Torvalds 	struct net_device *dev;
23431da177e4SLinus Torvalds 	int err = -EINVAL;
23441da177e4SLinus Torvalds 
23451da177e4SLinus Torvalds 	rtnl_lock();
23461da177e4SLinus Torvalds 
23471da177e4SLinus Torvalds 	err = -EFAULT;
23481da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
23491da177e4SLinus Torvalds 		goto err_exit;
23501da177e4SLinus Torvalds 
2351af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
23521da177e4SLinus Torvalds 
23531da177e4SLinus Torvalds 	err = -ENODEV;
23541da177e4SLinus Torvalds 	if (dev == NULL)
23551da177e4SLinus Torvalds 		goto err_exit;
23561da177e4SLinus Torvalds 
235707a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
23581da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT) {
23595bc3eb7eSStephen Hemminger 		const struct net_device_ops *ops = dev->netdev_ops;
23601da177e4SLinus Torvalds 		struct ifreq ifr;
23611da177e4SLinus Torvalds 		struct ip_tunnel_parm p;
23621da177e4SLinus Torvalds 
23631da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
23641da177e4SLinus Torvalds 		if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
23651da177e4SLinus Torvalds 			goto err_exit;
23661da177e4SLinus Torvalds 
23671da177e4SLinus Torvalds 		memset(&p, 0, sizeof(p));
23681da177e4SLinus Torvalds 		p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
23691da177e4SLinus Torvalds 		p.iph.saddr = 0;
23701da177e4SLinus Torvalds 		p.iph.version = 4;
23711da177e4SLinus Torvalds 		p.iph.ihl = 5;
23721da177e4SLinus Torvalds 		p.iph.protocol = IPPROTO_IPV6;
23731da177e4SLinus Torvalds 		p.iph.ttl = 64;
2374d20b3109SStephen Hemminger 		ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
23751da177e4SLinus Torvalds 
23765bc3eb7eSStephen Hemminger 		if (ops->ndo_do_ioctl) {
23775bc3eb7eSStephen Hemminger 			mm_segment_t oldfs = get_fs();
23785bc3eb7eSStephen Hemminger 
23795bc3eb7eSStephen Hemminger 			set_fs(KERNEL_DS);
23805bc3eb7eSStephen Hemminger 			err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
23811da177e4SLinus Torvalds 			set_fs(oldfs);
23825bc3eb7eSStephen Hemminger 		} else
23835bc3eb7eSStephen Hemminger 			err = -EOPNOTSUPP;
23841da177e4SLinus Torvalds 
23851da177e4SLinus Torvalds 		if (err == 0) {
23861da177e4SLinus Torvalds 			err = -ENOBUFS;
2387af284937SDaniel Lezcano 			dev = __dev_get_by_name(net, p.name);
2388af284937SDaniel Lezcano 			if (!dev)
23891da177e4SLinus Torvalds 				goto err_exit;
23901da177e4SLinus Torvalds 			err = dev_open(dev);
23911da177e4SLinus Torvalds 		}
23921da177e4SLinus Torvalds 	}
23930be669bbSJoerg Roedel #endif
23941da177e4SLinus Torvalds 
23951da177e4SLinus Torvalds err_exit:
23961da177e4SLinus Torvalds 	rtnl_unlock();
23971da177e4SLinus Torvalds 	return err;
23981da177e4SLinus Torvalds }
23991da177e4SLinus Torvalds 
24001da177e4SLinus Torvalds /*
24011da177e4SLinus Torvalds  *	Manual configuration of address on an interface
24021da177e4SLinus Torvalds  */
2403b71d1d42SEric Dumazet static int inet6_addr_add(struct net *net, int ifindex, const struct in6_addr *pfx,
2404caeaba79SNicolas Dichtel 			  const struct in6_addr *peer_pfx,
240524ef0da7SThomas Graf 			  unsigned int plen, __u8 ifa_flags, __u32 prefered_lft,
2406af284937SDaniel Lezcano 			  __u32 valid_lft)
24071da177e4SLinus Torvalds {
24081da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
24091da177e4SLinus Torvalds 	struct inet6_dev *idev;
24101da177e4SLinus Torvalds 	struct net_device *dev;
24111da177e4SLinus Torvalds 	int scope;
24126f704992SYOSHIFUJI Hideaki 	u32 flags;
24136f704992SYOSHIFUJI Hideaki 	clock_t expires;
24144bed72e4SYOSHIFUJI Hideaki 	unsigned long timeout;
24151da177e4SLinus Torvalds 
24161da177e4SLinus Torvalds 	ASSERT_RTNL();
24171da177e4SLinus Torvalds 
241824ef0da7SThomas Graf 	if (plen > 128)
241924ef0da7SThomas Graf 		return -EINVAL;
242024ef0da7SThomas Graf 
24210778769dSNoriaki TAKAMIYA 	/* check the lifetime */
24220778769dSNoriaki TAKAMIYA 	if (!valid_lft || prefered_lft > valid_lft)
24230778769dSNoriaki TAKAMIYA 		return -EINVAL;
24240778769dSNoriaki TAKAMIYA 
2425af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2426af284937SDaniel Lezcano 	if (!dev)
24271da177e4SLinus Torvalds 		return -ENODEV;
24281da177e4SLinus Torvalds 
242964e724f6SBrian Haley 	idev = addrconf_add_dev(dev);
243064e724f6SBrian Haley 	if (IS_ERR(idev))
243164e724f6SBrian Haley 		return PTR_ERR(idev);
24321da177e4SLinus Torvalds 
24331da177e4SLinus Torvalds 	scope = ipv6_addr_scope(pfx);
24341da177e4SLinus Torvalds 
24354bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
24364bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
24374bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
24384bed72e4SYOSHIFUJI Hideaki 		valid_lft = timeout;
24396f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
24404bed72e4SYOSHIFUJI Hideaki 	} else {
24414bed72e4SYOSHIFUJI Hideaki 		expires = 0;
24424bed72e4SYOSHIFUJI Hideaki 		flags = 0;
24434bed72e4SYOSHIFUJI Hideaki 		ifa_flags |= IFA_F_PERMANENT;
24446f704992SYOSHIFUJI Hideaki 	}
24450778769dSNoriaki TAKAMIYA 
24464bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
24474bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
24484bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
24490778769dSNoriaki TAKAMIYA 			ifa_flags |= IFA_F_DEPRECATED;
24504bed72e4SYOSHIFUJI Hideaki 		prefered_lft = timeout;
24514bed72e4SYOSHIFUJI Hideaki 	}
24520778769dSNoriaki TAKAMIYA 
24530778769dSNoriaki TAKAMIYA 	ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
24540778769dSNoriaki TAKAMIYA 
24551da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
245606aebfb7SHerbert Xu 		spin_lock_bh(&ifp->lock);
24570778769dSNoriaki TAKAMIYA 		ifp->valid_lft = valid_lft;
24580778769dSNoriaki TAKAMIYA 		ifp->prefered_lft = prefered_lft;
24590778769dSNoriaki TAKAMIYA 		ifp->tstamp = jiffies;
2460caeaba79SNicolas Dichtel 		if (peer_pfx)
2461caeaba79SNicolas Dichtel 			ifp->peer_addr = *peer_pfx;
246206aebfb7SHerbert Xu 		spin_unlock_bh(&ifp->lock);
24630778769dSNoriaki TAKAMIYA 
246446d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
24656f704992SYOSHIFUJI Hideaki 				      expires, flags);
246695c385b4SNeil Horman 		/*
246795c385b4SNeil Horman 		 * Note that section 3.1 of RFC 4429 indicates
246895c385b4SNeil Horman 		 * that the Optimistic flag should not be set for
246995c385b4SNeil Horman 		 * manually configured addresses
247095c385b4SNeil Horman 		 */
2471cf22f9a2SDavid S. Miller 		addrconf_dad_start(ifp);
24721da177e4SLinus Torvalds 		in6_ifa_put(ifp);
24730778769dSNoriaki TAKAMIYA 		addrconf_verify(0);
24741da177e4SLinus Torvalds 		return 0;
24751da177e4SLinus Torvalds 	}
24761da177e4SLinus Torvalds 
24771da177e4SLinus Torvalds 	return PTR_ERR(ifp);
24781da177e4SLinus Torvalds }
24791da177e4SLinus Torvalds 
2480b71d1d42SEric Dumazet static int inet6_addr_del(struct net *net, int ifindex, const struct in6_addr *pfx,
248124ef0da7SThomas Graf 			  unsigned int plen)
24821da177e4SLinus Torvalds {
24831da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
24841da177e4SLinus Torvalds 	struct inet6_dev *idev;
24851da177e4SLinus Torvalds 	struct net_device *dev;
24861da177e4SLinus Torvalds 
248724ef0da7SThomas Graf 	if (plen > 128)
248824ef0da7SThomas Graf 		return -EINVAL;
248924ef0da7SThomas Graf 
2490af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2491af284937SDaniel Lezcano 	if (!dev)
24921da177e4SLinus Torvalds 		return -ENODEV;
24931da177e4SLinus Torvalds 
24941da177e4SLinus Torvalds 	if ((idev = __in6_dev_get(dev)) == NULL)
24951da177e4SLinus Torvalds 		return -ENXIO;
24961da177e4SLinus Torvalds 
24971da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
2498502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
24991da177e4SLinus Torvalds 		if (ifp->prefix_len == plen &&
25001da177e4SLinus Torvalds 		    ipv6_addr_equal(pfx, &ifp->addr)) {
25011da177e4SLinus Torvalds 			in6_ifa_hold(ifp);
25021da177e4SLinus Torvalds 			read_unlock_bh(&idev->lock);
25031da177e4SLinus Torvalds 
25041da177e4SLinus Torvalds 			ipv6_del_addr(ifp);
25051da177e4SLinus Torvalds 
25061da177e4SLinus Torvalds 			/* If the last address is deleted administratively,
25071da177e4SLinus Torvalds 			   disable IPv6 on this interface.
25081da177e4SLinus Torvalds 			 */
2509502a2ffdSstephen hemminger 			if (list_empty(&idev->addr_list))
25101da177e4SLinus Torvalds 				addrconf_ifdown(idev->dev, 1);
25111da177e4SLinus Torvalds 			return 0;
25121da177e4SLinus Torvalds 		}
25131da177e4SLinus Torvalds 	}
25141da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
25151da177e4SLinus Torvalds 	return -EADDRNOTAVAIL;
25161da177e4SLinus Torvalds }
25171da177e4SLinus Torvalds 
25181da177e4SLinus Torvalds 
2519af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg)
25201da177e4SLinus Torvalds {
25211da177e4SLinus Torvalds 	struct in6_ifreq ireq;
25221da177e4SLinus Torvalds 	int err;
25231da177e4SLinus Torvalds 
2524af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
25251da177e4SLinus Torvalds 		return -EPERM;
25261da177e4SLinus Torvalds 
25271da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
25281da177e4SLinus Torvalds 		return -EFAULT;
25291da177e4SLinus Torvalds 
25301da177e4SLinus Torvalds 	rtnl_lock();
2531caeaba79SNicolas Dichtel 	err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
2532af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2533af284937SDaniel Lezcano 			     INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
25341da177e4SLinus Torvalds 	rtnl_unlock();
25351da177e4SLinus Torvalds 	return err;
25361da177e4SLinus Torvalds }
25371da177e4SLinus Torvalds 
2538af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg)
25391da177e4SLinus Torvalds {
25401da177e4SLinus Torvalds 	struct in6_ifreq ireq;
25411da177e4SLinus Torvalds 	int err;
25421da177e4SLinus Torvalds 
2543af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
25441da177e4SLinus Torvalds 		return -EPERM;
25451da177e4SLinus Torvalds 
25461da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
25471da177e4SLinus Torvalds 		return -EFAULT;
25481da177e4SLinus Torvalds 
25491da177e4SLinus Torvalds 	rtnl_lock();
2550af284937SDaniel Lezcano 	err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2551af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen);
25521da177e4SLinus Torvalds 	rtnl_unlock();
25531da177e4SLinus Torvalds 	return err;
25541da177e4SLinus Torvalds }
25551da177e4SLinus Torvalds 
2556b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2557b5f348e5SIlpo Järvinen 		     int plen, int scope)
2558b5f348e5SIlpo Järvinen {
2559b5f348e5SIlpo Järvinen 	struct inet6_ifaddr *ifp;
2560b5f348e5SIlpo Järvinen 
2561b5f348e5SIlpo Järvinen 	ifp = ipv6_add_addr(idev, addr, plen, scope, IFA_F_PERMANENT);
2562b5f348e5SIlpo Järvinen 	if (!IS_ERR(ifp)) {
2563b5f348e5SIlpo Järvinen 		spin_lock_bh(&ifp->lock);
2564b5f348e5SIlpo Järvinen 		ifp->flags &= ~IFA_F_TENTATIVE;
2565b5f348e5SIlpo Järvinen 		spin_unlock_bh(&ifp->lock);
2566b5f348e5SIlpo Järvinen 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
2567b5f348e5SIlpo Järvinen 		in6_ifa_put(ifp);
2568b5f348e5SIlpo Järvinen 	}
2569b5f348e5SIlpo Järvinen }
2570b5f348e5SIlpo Järvinen 
257107a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
25721da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev)
25731da177e4SLinus Torvalds {
25741da177e4SLinus Torvalds 	struct in6_addr addr;
25751da177e4SLinus Torvalds 	struct net_device *dev;
2576c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
25771da177e4SLinus Torvalds 	int scope;
25781da177e4SLinus Torvalds 
25791da177e4SLinus Torvalds 	ASSERT_RTNL();
25801da177e4SLinus Torvalds 
25811da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
25821da177e4SLinus Torvalds 	memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
25831da177e4SLinus Torvalds 
25841da177e4SLinus Torvalds 	if (idev->dev->flags&IFF_POINTOPOINT) {
25851da177e4SLinus Torvalds 		addr.s6_addr32[0] = htonl(0xfe800000);
25861da177e4SLinus Torvalds 		scope = IFA_LINK;
25871da177e4SLinus Torvalds 	} else {
25881da177e4SLinus Torvalds 		scope = IPV6_ADDR_COMPATv4;
25891da177e4SLinus Torvalds 	}
25901da177e4SLinus Torvalds 
25911da177e4SLinus Torvalds 	if (addr.s6_addr32[3]) {
2592b5f348e5SIlpo Järvinen 		add_addr(idev, &addr, 128, scope);
25931da177e4SLinus Torvalds 		return;
25941da177e4SLinus Torvalds 	}
25951da177e4SLinus Torvalds 
25966fda7350SBenjamin Thery 	for_each_netdev(net, dev) {
2597e5ed6399SHerbert Xu 		struct in_device *in_dev = __in_dev_get_rtnl(dev);
25981da177e4SLinus Torvalds 		if (in_dev && (dev->flags & IFF_UP)) {
25991da177e4SLinus Torvalds 			struct in_ifaddr *ifa;
26001da177e4SLinus Torvalds 
26011da177e4SLinus Torvalds 			int flag = scope;
26021da177e4SLinus Torvalds 
26031da177e4SLinus Torvalds 			for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
26041da177e4SLinus Torvalds 				int plen;
26051da177e4SLinus Torvalds 
26061da177e4SLinus Torvalds 				addr.s6_addr32[3] = ifa->ifa_local;
26071da177e4SLinus Torvalds 
26081da177e4SLinus Torvalds 				if (ifa->ifa_scope == RT_SCOPE_LINK)
26091da177e4SLinus Torvalds 					continue;
26101da177e4SLinus Torvalds 				if (ifa->ifa_scope >= RT_SCOPE_HOST) {
26111da177e4SLinus Torvalds 					if (idev->dev->flags&IFF_POINTOPOINT)
26121da177e4SLinus Torvalds 						continue;
26131da177e4SLinus Torvalds 					flag |= IFA_HOST;
26141da177e4SLinus Torvalds 				}
26151da177e4SLinus Torvalds 				if (idev->dev->flags&IFF_POINTOPOINT)
26161da177e4SLinus Torvalds 					plen = 64;
26171da177e4SLinus Torvalds 				else
26181da177e4SLinus Torvalds 					plen = 96;
26191da177e4SLinus Torvalds 
2620b5f348e5SIlpo Järvinen 				add_addr(idev, &addr, plen, flag);
26211da177e4SLinus Torvalds 			}
26221da177e4SLinus Torvalds 		}
26231da177e4SLinus Torvalds 	}
26241da177e4SLinus Torvalds }
26250be669bbSJoerg Roedel #endif
26261da177e4SLinus Torvalds 
26271da177e4SLinus Torvalds static void init_loopback(struct net_device *dev)
26281da177e4SLinus Torvalds {
26291da177e4SLinus Torvalds 	struct inet6_dev  *idev;
263025fb6ca4SBalakumaran Kannan 	struct net_device *sp_dev;
263125fb6ca4SBalakumaran Kannan 	struct inet6_ifaddr *sp_ifa;
263225fb6ca4SBalakumaran Kannan 	struct rt6_info *sp_rt;
26331da177e4SLinus Torvalds 
26341da177e4SLinus Torvalds 	/* ::1 */
26351da177e4SLinus Torvalds 
26361da177e4SLinus Torvalds 	ASSERT_RTNL();
26371da177e4SLinus Torvalds 
26381da177e4SLinus Torvalds 	if ((idev = ipv6_find_idev(dev)) == NULL) {
263991df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
26401da177e4SLinus Torvalds 		return;
26411da177e4SLinus Torvalds 	}
26421da177e4SLinus Torvalds 
2643b5f348e5SIlpo Järvinen 	add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
264425fb6ca4SBalakumaran Kannan 
264525fb6ca4SBalakumaran Kannan 	/* Add routes to other interface's IPv6 addresses */
264625fb6ca4SBalakumaran Kannan 	for_each_netdev(dev_net(dev), sp_dev) {
264725fb6ca4SBalakumaran Kannan 		if (!strcmp(sp_dev->name, dev->name))
264825fb6ca4SBalakumaran Kannan 			continue;
264925fb6ca4SBalakumaran Kannan 
265025fb6ca4SBalakumaran Kannan 		idev = __in6_dev_get(sp_dev);
265125fb6ca4SBalakumaran Kannan 		if (!idev)
265225fb6ca4SBalakumaran Kannan 			continue;
265325fb6ca4SBalakumaran Kannan 
265425fb6ca4SBalakumaran Kannan 		read_lock_bh(&idev->lock);
265525fb6ca4SBalakumaran Kannan 		list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
265625fb6ca4SBalakumaran Kannan 
265725fb6ca4SBalakumaran Kannan 			if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
265825fb6ca4SBalakumaran Kannan 				continue;
265925fb6ca4SBalakumaran Kannan 
266025fb6ca4SBalakumaran Kannan 			sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, 0);
266125fb6ca4SBalakumaran Kannan 
266225fb6ca4SBalakumaran Kannan 			/* Failure cases are ignored */
2663534c8779SGao feng 			if (!IS_ERR(sp_rt)) {
2664534c8779SGao feng 				sp_ifa->rt = sp_rt;
266525fb6ca4SBalakumaran Kannan 				ip6_ins_rt(sp_rt);
266625fb6ca4SBalakumaran Kannan 			}
2667534c8779SGao feng 		}
266825fb6ca4SBalakumaran Kannan 		read_unlock_bh(&idev->lock);
266925fb6ca4SBalakumaran Kannan 	}
26701da177e4SLinus Torvalds }
26711da177e4SLinus Torvalds 
2672b71d1d42SEric Dumazet static void addrconf_add_linklocal(struct inet6_dev *idev, const struct in6_addr *addr)
26731da177e4SLinus Torvalds {
26741da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
267595c385b4SNeil Horman 	u32 addr_flags = IFA_F_PERMANENT;
26761da177e4SLinus Torvalds 
267795c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
267895c385b4SNeil Horman 	if (idev->cnf.optimistic_dad &&
2679702beb87SDavid Miller 	    !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
268095c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
268195c385b4SNeil Horman #endif
268295c385b4SNeil Horman 
268395c385b4SNeil Horman 
268495c385b4SNeil Horman 	ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags);
26851da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
268646d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
2687cf22f9a2SDavid S. Miller 		addrconf_dad_start(ifp);
26881da177e4SLinus Torvalds 		in6_ifa_put(ifp);
26891da177e4SLinus Torvalds 	}
26901da177e4SLinus Torvalds }
26911da177e4SLinus Torvalds 
26921da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev)
26931da177e4SLinus Torvalds {
26941da177e4SLinus Torvalds 	struct in6_addr addr;
26951da177e4SLinus Torvalds 	struct inet6_dev *idev;
26961da177e4SLinus Torvalds 
26971da177e4SLinus Torvalds 	ASSERT_RTNL();
26981da177e4SLinus Torvalds 
269974235a25SHerbert Xu 	if ((dev->type != ARPHRD_ETHER) &&
270074235a25SHerbert Xu 	    (dev->type != ARPHRD_FDDI) &&
270174235a25SHerbert Xu 	    (dev->type != ARPHRD_ARCNET) &&
270206a4c1c5Salex.bluesman.smirnov@gmail.com 	    (dev->type != ARPHRD_INFINIBAND) &&
2703cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	    (dev->type != ARPHRD_IEEE802154) &&
2704cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	    (dev->type != ARPHRD_IEEE1394)) {
270574235a25SHerbert Xu 		/* Alas, we support only Ethernet autoconfiguration. */
270674235a25SHerbert Xu 		return;
270774235a25SHerbert Xu 	}
270874235a25SHerbert Xu 
27091da177e4SLinus Torvalds 	idev = addrconf_add_dev(dev);
271064e724f6SBrian Haley 	if (IS_ERR(idev))
27111da177e4SLinus Torvalds 		return;
27121da177e4SLinus Torvalds 
27131da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
27141da177e4SLinus Torvalds 	addr.s6_addr32[0] = htonl(0xFE800000);
27151da177e4SLinus Torvalds 
27161da177e4SLinus Torvalds 	if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
27171da177e4SLinus Torvalds 		addrconf_add_linklocal(idev, &addr);
27181da177e4SLinus Torvalds }
27191da177e4SLinus Torvalds 
272007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
27211da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev)
27221da177e4SLinus Torvalds {
27231da177e4SLinus Torvalds 	struct inet6_dev *idev;
27241da177e4SLinus Torvalds 
27251da177e4SLinus Torvalds 	ASSERT_RTNL();
27261da177e4SLinus Torvalds 
27271da177e4SLinus Torvalds 	/*
27281da177e4SLinus Torvalds 	 * Configure the tunnel with one of our IPv4
27291da177e4SLinus Torvalds 	 * addresses... we should configure all of
27301da177e4SLinus Torvalds 	 * our v4 addrs in the tunnel
27311da177e4SLinus Torvalds 	 */
27321da177e4SLinus Torvalds 
27331da177e4SLinus Torvalds 	if ((idev = ipv6_find_idev(dev)) == NULL) {
273491df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
27351da177e4SLinus Torvalds 		return;
27361da177e4SLinus Torvalds 	}
27371da177e4SLinus Torvalds 
2738c7dc89c0SFred L. Templin 	if (dev->priv_flags & IFF_ISATAP) {
2739c7dc89c0SFred L. Templin 		struct in6_addr addr;
2740c7dc89c0SFred L. Templin 
2741c7dc89c0SFred L. Templin 		ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
2742c7dc89c0SFred L. Templin 		addrconf_prefix_route(&addr, 64, dev, 0, 0);
2743c7dc89c0SFred L. Templin 		if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2744c7dc89c0SFred L. Templin 			addrconf_add_linklocal(idev, &addr);
2745c7dc89c0SFred L. Templin 		return;
2746c7dc89c0SFred L. Templin 	}
2747c7dc89c0SFred L. Templin 
27481da177e4SLinus Torvalds 	sit_add_v4_addrs(idev);
27491da177e4SLinus Torvalds 
275062b54dd9SNicolas Dichtel 	if (dev->flags&IFF_POINTOPOINT)
27511da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
275262b54dd9SNicolas Dichtel 	else
27531da177e4SLinus Torvalds 		sit_route_add(dev);
27541da177e4SLinus Torvalds }
27550be669bbSJoerg Roedel #endif
27561da177e4SLinus Torvalds 
275707a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE)
2758aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev)
2759aee80b54Sstephen hemminger {
2760aee80b54Sstephen hemminger 	struct inet6_dev *idev;
2761aee80b54Sstephen hemminger 	struct in6_addr addr;
2762aee80b54Sstephen hemminger 
2763f3213831SJoe Perches 	pr_info("%s(%s)\n", __func__, dev->name);
2764aee80b54Sstephen hemminger 
2765aee80b54Sstephen hemminger 	ASSERT_RTNL();
2766aee80b54Sstephen hemminger 
2767aee80b54Sstephen hemminger 	if ((idev = ipv6_find_idev(dev)) == NULL) {
276891df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
2769aee80b54Sstephen hemminger 		return;
2770aee80b54Sstephen hemminger 	}
2771aee80b54Sstephen hemminger 
2772aee80b54Sstephen hemminger 	ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
2773aee80b54Sstephen hemminger 	addrconf_prefix_route(&addr, 64, dev, 0, 0);
2774aee80b54Sstephen hemminger 
2775aee80b54Sstephen hemminger 	if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2776aee80b54Sstephen hemminger 		addrconf_add_linklocal(idev, &addr);
2777aee80b54Sstephen hemminger }
2778aee80b54Sstephen hemminger #endif
2779aee80b54Sstephen hemminger 
27801da177e4SLinus Torvalds static inline int
27811da177e4SLinus Torvalds ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
27821da177e4SLinus Torvalds {
27831da177e4SLinus Torvalds 	struct in6_addr lladdr;
27841da177e4SLinus Torvalds 
278595c385b4SNeil Horman 	if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
27861da177e4SLinus Torvalds 		addrconf_add_linklocal(idev, &lladdr);
27871da177e4SLinus Torvalds 		return 0;
27881da177e4SLinus Torvalds 	}
27891da177e4SLinus Torvalds 	return -1;
27901da177e4SLinus Torvalds }
27911da177e4SLinus Torvalds 
27921da177e4SLinus Torvalds static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
27931da177e4SLinus Torvalds {
27941da177e4SLinus Torvalds 	struct net_device *link_dev;
2795c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
27961da177e4SLinus Torvalds 
27971da177e4SLinus Torvalds 	/* first try to inherit the link-local address from the link device */
27981da177e4SLinus Torvalds 	if (idev->dev->iflink &&
27996fda7350SBenjamin Thery 	    (link_dev = __dev_get_by_index(net, idev->dev->iflink))) {
28001da177e4SLinus Torvalds 		if (!ipv6_inherit_linklocal(idev, link_dev))
28011da177e4SLinus Torvalds 			return;
28021da177e4SLinus Torvalds 	}
28031da177e4SLinus Torvalds 	/* then try to inherit it from any device */
28046fda7350SBenjamin Thery 	for_each_netdev(net, link_dev) {
28051da177e4SLinus Torvalds 		if (!ipv6_inherit_linklocal(idev, link_dev))
28061da177e4SLinus Torvalds 			return;
28071da177e4SLinus Torvalds 	}
280891df42beSJoe Perches 	pr_debug("init ip6-ip6: add_linklocal failed\n");
28091da177e4SLinus Torvalds }
28101da177e4SLinus Torvalds 
28111da177e4SLinus Torvalds /*
28121da177e4SLinus Torvalds  * Autoconfigure tunnel with a link-local address so routing protocols,
28131da177e4SLinus Torvalds  * DHCPv6, MLD etc. can be run over the virtual link
28141da177e4SLinus Torvalds  */
28151da177e4SLinus Torvalds 
28161da177e4SLinus Torvalds static void addrconf_ip6_tnl_config(struct net_device *dev)
28171da177e4SLinus Torvalds {
28181da177e4SLinus Torvalds 	struct inet6_dev *idev;
28191da177e4SLinus Torvalds 
28201da177e4SLinus Torvalds 	ASSERT_RTNL();
28211da177e4SLinus Torvalds 
2822e21e8467SStephen Hemminger 	idev = addrconf_add_dev(dev);
282364e724f6SBrian Haley 	if (IS_ERR(idev)) {
282491df42beSJoe Perches 		pr_debug("init ip6-ip6: add_dev failed\n");
28251da177e4SLinus Torvalds 		return;
28261da177e4SLinus Torvalds 	}
28271da177e4SLinus Torvalds 	ip6_tnl_add_linklocal(idev);
28281da177e4SLinus Torvalds }
28291da177e4SLinus Torvalds 
28301da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event,
2831351638e7SJiri Pirko 			   void *ptr)
28321da177e4SLinus Torvalds {
2833351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2834748e2d93SEric Dumazet 	struct inet6_dev *idev = __in6_dev_get(dev);
2835c5e33bddSYOSHIFUJI Hideaki 	int run_pending = 0;
2836b217d616SHerbert Xu 	int err;
28371da177e4SLinus Torvalds 
28381da177e4SLinus Torvalds 	switch (event) {
283945ba9dd2SYOSHIFUJI Hideaki 	case NETDEV_REGISTER:
284074235a25SHerbert Xu 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
284145ba9dd2SYOSHIFUJI Hideaki 			idev = ipv6_add_dev(dev);
284245ba9dd2SYOSHIFUJI Hideaki 			if (!idev)
2843b217d616SHerbert Xu 				return notifier_from_errno(-ENOMEM);
284445ba9dd2SYOSHIFUJI Hideaki 		}
284545ba9dd2SYOSHIFUJI Hideaki 		break;
2846bcdd553fSStephen Hemminger 
28471da177e4SLinus Torvalds 	case NETDEV_UP:
28483c21edbdSYOSHIFUJI Hideaki 	case NETDEV_CHANGE:
2849c2edacf8SJay Vosburgh 		if (dev->flags & IFF_SLAVE)
2850c2edacf8SJay Vosburgh 			break;
2851c2edacf8SJay Vosburgh 
28523c21edbdSYOSHIFUJI Hideaki 		if (event == NETDEV_UP) {
2853f24e3d65SMitsuru Chinen 			if (!addrconf_qdisc_ok(dev)) {
28543c21edbdSYOSHIFUJI Hideaki 				/* device is not ready yet. */
2855f3213831SJoe Perches 				pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
28563c21edbdSYOSHIFUJI Hideaki 					dev->name);
28573c21edbdSYOSHIFUJI Hideaki 				break;
28583c21edbdSYOSHIFUJI Hideaki 			}
285999081049SKristian Slavov 
2860d31c7b8fSEvgeniy Polyakov 			if (!idev && dev->mtu >= IPV6_MIN_MTU)
2861d31c7b8fSEvgeniy Polyakov 				idev = ipv6_add_dev(dev);
2862d31c7b8fSEvgeniy Polyakov 
2863e3ec6cfcSBenjamin Thery 			if (idev) {
286499081049SKristian Slavov 				idev->if_flags |= IF_READY;
2865e3ec6cfcSBenjamin Thery 				run_pending = 1;
2866e3ec6cfcSBenjamin Thery 			}
28673c21edbdSYOSHIFUJI Hideaki 		} else {
2868f24e3d65SMitsuru Chinen 			if (!addrconf_qdisc_ok(dev)) {
28693c21edbdSYOSHIFUJI Hideaki 				/* device is still not ready. */
28703c21edbdSYOSHIFUJI Hideaki 				break;
28713c21edbdSYOSHIFUJI Hideaki 			}
28723c21edbdSYOSHIFUJI Hideaki 
28733c21edbdSYOSHIFUJI Hideaki 			if (idev) {
2874bcdd553fSStephen Hemminger 				if (idev->if_flags & IF_READY)
28753c21edbdSYOSHIFUJI Hideaki 					/* device is already configured. */
28763c21edbdSYOSHIFUJI Hideaki 					break;
28773c21edbdSYOSHIFUJI Hideaki 				idev->if_flags |= IF_READY;
28783c21edbdSYOSHIFUJI Hideaki 			}
28793c21edbdSYOSHIFUJI Hideaki 
2880f3213831SJoe Perches 			pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
28813c21edbdSYOSHIFUJI Hideaki 				dev->name);
28823c21edbdSYOSHIFUJI Hideaki 
2883c5e33bddSYOSHIFUJI Hideaki 			run_pending = 1;
28843c21edbdSYOSHIFUJI Hideaki 		}
28853c21edbdSYOSHIFUJI Hideaki 
28861da177e4SLinus Torvalds 		switch (dev->type) {
288707a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
28881da177e4SLinus Torvalds 		case ARPHRD_SIT:
28891da177e4SLinus Torvalds 			addrconf_sit_config(dev);
28901da177e4SLinus Torvalds 			break;
28910be669bbSJoerg Roedel #endif
289207a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE)
2893aee80b54Sstephen hemminger 		case ARPHRD_IPGRE:
2894aee80b54Sstephen hemminger 			addrconf_gre_config(dev);
2895aee80b54Sstephen hemminger 			break;
2896aee80b54Sstephen hemminger #endif
28971da177e4SLinus Torvalds 		case ARPHRD_TUNNEL6:
28981da177e4SLinus Torvalds 			addrconf_ip6_tnl_config(dev);
28991da177e4SLinus Torvalds 			break;
29001da177e4SLinus Torvalds 		case ARPHRD_LOOPBACK:
29011da177e4SLinus Torvalds 			init_loopback(dev);
29021da177e4SLinus Torvalds 			break;
29031da177e4SLinus Torvalds 
29041da177e4SLinus Torvalds 		default:
29051da177e4SLinus Torvalds 			addrconf_dev_config(dev);
29061da177e4SLinus Torvalds 			break;
29073ff50b79SStephen Hemminger 		}
2908bcdd553fSStephen Hemminger 
29091da177e4SLinus Torvalds 		if (idev) {
2910c5e33bddSYOSHIFUJI Hideaki 			if (run_pending)
2911c5e33bddSYOSHIFUJI Hideaki 				addrconf_dad_run(idev);
2912c5e33bddSYOSHIFUJI Hideaki 
2913bcdd553fSStephen Hemminger 			/*
2914bcdd553fSStephen Hemminger 			 * If the MTU changed during the interface down,
2915bcdd553fSStephen Hemminger 			 * when the interface up, the changed MTU must be
2916bcdd553fSStephen Hemminger 			 * reflected in the idev as well as routers.
29171da177e4SLinus Torvalds 			 */
2918bcdd553fSStephen Hemminger 			if (idev->cnf.mtu6 != dev->mtu &&
2919bcdd553fSStephen Hemminger 			    dev->mtu >= IPV6_MIN_MTU) {
29201da177e4SLinus Torvalds 				rt6_mtu_change(dev, dev->mtu);
29211da177e4SLinus Torvalds 				idev->cnf.mtu6 = dev->mtu;
29221da177e4SLinus Torvalds 			}
29231da177e4SLinus Torvalds 			idev->tstamp = jiffies;
29241da177e4SLinus Torvalds 			inet6_ifinfo_notify(RTM_NEWLINK, idev);
2925bcdd553fSStephen Hemminger 
2926bcdd553fSStephen Hemminger 			/*
2927bcdd553fSStephen Hemminger 			 * If the changed mtu during down is lower than
2928bcdd553fSStephen Hemminger 			 * IPV6_MIN_MTU stop IPv6 on this interface.
29291da177e4SLinus Torvalds 			 */
29301da177e4SLinus Torvalds 			if (dev->mtu < IPV6_MIN_MTU)
2931bcdd553fSStephen Hemminger 				addrconf_ifdown(dev, 1);
29321da177e4SLinus Torvalds 		}
29331da177e4SLinus Torvalds 		break;
29341da177e4SLinus Torvalds 
29351da177e4SLinus Torvalds 	case NETDEV_CHANGEMTU:
29361da177e4SLinus Torvalds 		if (idev && dev->mtu >= IPV6_MIN_MTU) {
29371da177e4SLinus Torvalds 			rt6_mtu_change(dev, dev->mtu);
29381da177e4SLinus Torvalds 			idev->cnf.mtu6 = dev->mtu;
29391da177e4SLinus Torvalds 			break;
29401da177e4SLinus Torvalds 		}
29411da177e4SLinus Torvalds 
2942d31c7b8fSEvgeniy Polyakov 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2943d31c7b8fSEvgeniy Polyakov 			idev = ipv6_add_dev(dev);
2944d31c7b8fSEvgeniy Polyakov 			if (idev)
2945d31c7b8fSEvgeniy Polyakov 				break;
2946d31c7b8fSEvgeniy Polyakov 		}
2947d31c7b8fSEvgeniy Polyakov 
2948bcdd553fSStephen Hemminger 		/*
2949bcdd553fSStephen Hemminger 		 * MTU falled under IPV6_MIN_MTU.
2950bcdd553fSStephen Hemminger 		 * Stop IPv6 on this interface.
2951bcdd553fSStephen Hemminger 		 */
29521da177e4SLinus Torvalds 
29531da177e4SLinus Torvalds 	case NETDEV_DOWN:
29541da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
29551da177e4SLinus Torvalds 		/*
29561da177e4SLinus Torvalds 		 *	Remove all addresses from this interface.
29571da177e4SLinus Torvalds 		 */
29581da177e4SLinus Torvalds 		addrconf_ifdown(dev, event != NETDEV_DOWN);
29591da177e4SLinus Torvalds 		break;
29603c21edbdSYOSHIFUJI Hideaki 
29611da177e4SLinus Torvalds 	case NETDEV_CHANGENAME:
29621da177e4SLinus Torvalds 		if (idev) {
29635632c515SStephen Hemminger 			snmp6_unregister_dev(idev);
2964408c4768SPavel Emelyanov 			addrconf_sysctl_unregister(idev);
2965f52295a9SPavel Emelyanov 			addrconf_sysctl_register(idev);
2966b217d616SHerbert Xu 			err = snmp6_register_dev(idev);
2967b217d616SHerbert Xu 			if (err)
2968b217d616SHerbert Xu 				return notifier_from_errno(err);
29695632c515SStephen Hemminger 		}
29701da177e4SLinus Torvalds 		break;
2971bcdd553fSStephen Hemminger 
297293d9b7d7SJiri Pirko 	case NETDEV_PRE_TYPE_CHANGE:
297393d9b7d7SJiri Pirko 	case NETDEV_POST_TYPE_CHANGE:
297493d9b7d7SJiri Pirko 		addrconf_type_change(dev, event);
297575c78500SMoni Shoua 		break;
29763ff50b79SStephen Hemminger 	}
29771da177e4SLinus Torvalds 
29781da177e4SLinus Torvalds 	return NOTIFY_OK;
29791da177e4SLinus Torvalds }
29801da177e4SLinus Torvalds 
29811da177e4SLinus Torvalds /*
29821da177e4SLinus Torvalds  *	addrconf module should be notified of a device going up
29831da177e4SLinus Torvalds  */
29841da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = {
29851da177e4SLinus Torvalds 	.notifier_call = addrconf_notify,
29861da177e4SLinus Torvalds };
29871da177e4SLinus Torvalds 
298893d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event)
298975c78500SMoni Shoua {
299075c78500SMoni Shoua 	struct inet6_dev *idev;
299175c78500SMoni Shoua 	ASSERT_RTNL();
299275c78500SMoni Shoua 
299375c78500SMoni Shoua 	idev = __in6_dev_get(dev);
299475c78500SMoni Shoua 
299593d9b7d7SJiri Pirko 	if (event == NETDEV_POST_TYPE_CHANGE)
299675c78500SMoni Shoua 		ipv6_mc_remap(idev);
299793d9b7d7SJiri Pirko 	else if (event == NETDEV_PRE_TYPE_CHANGE)
299875c78500SMoni Shoua 		ipv6_mc_unmap(idev);
299975c78500SMoni Shoua }
300075c78500SMoni Shoua 
30011da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how)
30021da177e4SLinus Torvalds {
3003c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
3004502a2ffdSstephen hemminger 	struct inet6_dev *idev;
3005502a2ffdSstephen hemminger 	struct inet6_ifaddr *ifa;
300673a8bd74SDavid S. Miller 	int state, i;
30071da177e4SLinus Torvalds 
30081da177e4SLinus Torvalds 	ASSERT_RTNL();
30091da177e4SLinus Torvalds 
3010f3db4851SDaniel Lezcano 	rt6_ifdown(net, dev);
301173a8bd74SDavid S. Miller 	neigh_ifdown(&nd_tbl, dev);
30121da177e4SLinus Torvalds 
30131da177e4SLinus Torvalds 	idev = __in6_dev_get(dev);
30141da177e4SLinus Torvalds 	if (idev == NULL)
30151da177e4SLinus Torvalds 		return -ENODEV;
30161da177e4SLinus Torvalds 
3017bcdd553fSStephen Hemminger 	/*
3018bcdd553fSStephen Hemminger 	 * Step 1: remove reference to ipv6 device from parent device.
3019bcdd553fSStephen Hemminger 	 *	   Do not dev_put!
30201da177e4SLinus Torvalds 	 */
3021439e2385SDenis V. Lunev 	if (how) {
30221da177e4SLinus Torvalds 		idev->dead = 1;
30238814c4b5SYOSHIFUJI Hideaki 
30248814c4b5SYOSHIFUJI Hideaki 		/* protected by rtnl_lock */
3025a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(dev->ip6_ptr, NULL);
30261da177e4SLinus Torvalds 
30271da177e4SLinus Torvalds 		/* Step 1.5: remove snmp6 entry */
30281da177e4SLinus Torvalds 		snmp6_unregister_dev(idev);
30291da177e4SLinus Torvalds 
30301da177e4SLinus Torvalds 	}
30311da177e4SLinus Torvalds 
303273a8bd74SDavid S. Miller 	/* Step 2: clear hash table */
303373a8bd74SDavid S. Miller 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
303473a8bd74SDavid S. Miller 		struct hlist_head *h = &inet6_addr_lst[i];
303573a8bd74SDavid S. Miller 
303673a8bd74SDavid S. Miller 		spin_lock_bh(&addrconf_hash_lock);
303773a8bd74SDavid S. Miller 	restart:
3038b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(ifa, h, addr_lst) {
303973a8bd74SDavid S. Miller 			if (ifa->idev == idev) {
304073a8bd74SDavid S. Miller 				hlist_del_init_rcu(&ifa->addr_lst);
304173a8bd74SDavid S. Miller 				addrconf_del_timer(ifa);
304273a8bd74SDavid S. Miller 				goto restart;
304373a8bd74SDavid S. Miller 			}
304473a8bd74SDavid S. Miller 		}
304573a8bd74SDavid S. Miller 		spin_unlock_bh(&addrconf_hash_lock);
304673a8bd74SDavid S. Miller 	}
304773a8bd74SDavid S. Miller 
30481da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
30491da177e4SLinus Torvalds 
3050bcdd553fSStephen Hemminger 	/* Step 2: clear flags for stateless addrconf */
3051439e2385SDenis V. Lunev 	if (!how)
30523c21edbdSYOSHIFUJI Hideaki 		idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
30531da177e4SLinus Torvalds 
30541da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
3055439e2385SDenis V. Lunev 	if (how && del_timer(&idev->regen_timer))
30561da177e4SLinus Torvalds 		in6_dev_put(idev);
30571da177e4SLinus Torvalds 
3058bcdd553fSStephen Hemminger 	/* Step 3: clear tempaddr list */
3059372e6c8fSstephen hemminger 	while (!list_empty(&idev->tempaddr_list)) {
3060372e6c8fSstephen hemminger 		ifa = list_first_entry(&idev->tempaddr_list,
3061372e6c8fSstephen hemminger 				       struct inet6_ifaddr, tmp_list);
3062372e6c8fSstephen hemminger 		list_del(&ifa->tmp_list);
30631da177e4SLinus Torvalds 		write_unlock_bh(&idev->lock);
30641da177e4SLinus Torvalds 		spin_lock_bh(&ifa->lock);
30651da177e4SLinus Torvalds 
30661da177e4SLinus Torvalds 		if (ifa->ifpub) {
30671da177e4SLinus Torvalds 			in6_ifa_put(ifa->ifpub);
30681da177e4SLinus Torvalds 			ifa->ifpub = NULL;
30691da177e4SLinus Torvalds 		}
30701da177e4SLinus Torvalds 		spin_unlock_bh(&ifa->lock);
30711da177e4SLinus Torvalds 		in6_ifa_put(ifa);
30721da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
30731da177e4SLinus Torvalds 	}
30741da177e4SLinus Torvalds #endif
30758f37ada5Sstephen hemminger 
3076502a2ffdSstephen hemminger 	while (!list_empty(&idev->addr_list)) {
3077502a2ffdSstephen hemminger 		ifa = list_first_entry(&idev->addr_list,
3078502a2ffdSstephen hemminger 				       struct inet6_ifaddr, if_list);
307984e8b803Sstephen hemminger 		addrconf_del_timer(ifa);
308084e8b803Sstephen hemminger 
3081502a2ffdSstephen hemminger 		list_del(&ifa->if_list);
30821da177e4SLinus Torvalds 
30834c5ff6a6SHerbert Xu 		write_unlock_bh(&idev->lock);
308473a8bd74SDavid S. Miller 
30854c5ff6a6SHerbert Xu 		spin_lock_bh(&ifa->state_lock);
30864c5ff6a6SHerbert Xu 		state = ifa->state;
30874c5ff6a6SHerbert Xu 		ifa->state = INET6_IFADDR_STATE_DEAD;
30884c5ff6a6SHerbert Xu 		spin_unlock_bh(&ifa->state_lock);
30894c5ff6a6SHerbert Xu 
30909d82ca98SJohn Fastabend 		if (state != INET6_IFADDR_STATE_DEAD) {
30911da177e4SLinus Torvalds 			__ipv6_ifa_notify(RTM_DELADDR, ifa);
3092f88c91ddSCong Wang 			inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
30932de79570SLorenzo Colitti 		}
30949d82ca98SJohn Fastabend 		in6_ifa_put(ifa);
309573a8bd74SDavid S. Miller 
30961da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
30971da177e4SLinus Torvalds 	}
30988f37ada5Sstephen hemminger 
30991da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
31001da177e4SLinus Torvalds 
31011da177e4SLinus Torvalds 	/* Step 5: Discard multicast list */
3102439e2385SDenis V. Lunev 	if (how)
31031da177e4SLinus Torvalds 		ipv6_mc_destroy_dev(idev);
31041da177e4SLinus Torvalds 	else
31051da177e4SLinus Torvalds 		ipv6_mc_down(idev);
31061da177e4SLinus Torvalds 
31071da177e4SLinus Torvalds 	idev->tstamp = jiffies;
31081da177e4SLinus Torvalds 
3109bcdd553fSStephen Hemminger 	/* Last: Shot the device (if unregistered) */
3110439e2385SDenis V. Lunev 	if (how) {
3111408c4768SPavel Emelyanov 		addrconf_sysctl_unregister(idev);
31121da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, idev->nd_parms);
31131da177e4SLinus Torvalds 		neigh_ifdown(&nd_tbl, dev);
31141da177e4SLinus Torvalds 		in6_dev_put(idev);
31151da177e4SLinus Torvalds 	}
31161da177e4SLinus Torvalds 	return 0;
31171da177e4SLinus Torvalds }
31181da177e4SLinus Torvalds 
31191da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data)
31201da177e4SLinus Torvalds {
31211da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
31225b2a1953Sstephen hemminger 	struct inet6_dev *idev = ifp->idev;
31231da177e4SLinus Torvalds 
31245b2a1953Sstephen hemminger 	read_lock(&idev->lock);
31255b2a1953Sstephen hemminger 	if (idev->dead || !(idev->if_flags & IF_READY))
31261da177e4SLinus Torvalds 		goto out;
31271da177e4SLinus Torvalds 
31289ba2add3SShmulik Ladkani 	if (!ipv6_accept_ra(idev))
31291da177e4SLinus Torvalds 		goto out;
31305b2a1953Sstephen hemminger 
31315b2a1953Sstephen hemminger 	/* Announcement received after solicitation was sent */
31325b2a1953Sstephen hemminger 	if (idev->if_flags & IF_RA_RCVD)
31335b2a1953Sstephen hemminger 		goto out;
31341da177e4SLinus Torvalds 
31351da177e4SLinus Torvalds 	spin_lock(&ifp->lock);
31365b2a1953Sstephen hemminger 	if (ifp->probes++ < idev->cnf.rtr_solicits) {
31371da177e4SLinus Torvalds 		/* The wait after the last probe can be shorter */
31381da177e4SLinus Torvalds 		addrconf_mod_timer(ifp, AC_RS,
31395b2a1953Sstephen hemminger 				   (ifp->probes == idev->cnf.rtr_solicits) ?
31405b2a1953Sstephen hemminger 				   idev->cnf.rtr_solicit_delay :
31415b2a1953Sstephen hemminger 				   idev->cnf.rtr_solicit_interval);
31421da177e4SLinus Torvalds 		spin_unlock(&ifp->lock);
31431da177e4SLinus Torvalds 
31445b2a1953Sstephen hemminger 		ndisc_send_rs(idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
31451da177e4SLinus Torvalds 	} else {
31461da177e4SLinus Torvalds 		spin_unlock(&ifp->lock);
31471da177e4SLinus Torvalds 		/*
31481da177e4SLinus Torvalds 		 * Note: we do not support deprecated "all on-link"
31491da177e4SLinus Torvalds 		 * assumption any longer.
31501da177e4SLinus Torvalds 		 */
315191df42beSJoe Perches 		pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
31521da177e4SLinus Torvalds 	}
31531da177e4SLinus Torvalds 
31541da177e4SLinus Torvalds out:
31555b2a1953Sstephen hemminger 	read_unlock(&idev->lock);
31561da177e4SLinus Torvalds 	in6_ifa_put(ifp);
31571da177e4SLinus Torvalds }
31581da177e4SLinus Torvalds 
31591da177e4SLinus Torvalds /*
31601da177e4SLinus Torvalds  *	Duplicate Address Detection
31611da177e4SLinus Torvalds  */
31623c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
31633c21edbdSYOSHIFUJI Hideaki {
31643c21edbdSYOSHIFUJI Hideaki 	unsigned long rand_num;
31653c21edbdSYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
31663c21edbdSYOSHIFUJI Hideaki 
316795c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
316895c385b4SNeil Horman 		rand_num = 0;
316995c385b4SNeil Horman 	else
31703c21edbdSYOSHIFUJI Hideaki 		rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
317195c385b4SNeil Horman 
31723c21edbdSYOSHIFUJI Hideaki 	ifp->probes = idev->cnf.dad_transmits;
31733c21edbdSYOSHIFUJI Hideaki 	addrconf_mod_timer(ifp, AC_DAD, rand_num);
31743c21edbdSYOSHIFUJI Hideaki }
31753c21edbdSYOSHIFUJI Hideaki 
3176cf22f9a2SDavid S. Miller static void addrconf_dad_start(struct inet6_ifaddr *ifp)
31771da177e4SLinus Torvalds {
31781da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
31791da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
31801da177e4SLinus Torvalds 
31811da177e4SLinus Torvalds 	addrconf_join_solict(dev, &ifp->addr);
31821da177e4SLinus Torvalds 
31831da177e4SLinus Torvalds 	net_srandom(ifp->addr.s6_addr32[3]);
31841da177e4SLinus Torvalds 
31851da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
3186622ccdf1SHerbert Xu 	spin_lock(&ifp->lock);
3187e9d3e084SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DEAD)
31881da177e4SLinus Torvalds 		goto out;
31891da177e4SLinus Torvalds 
31901da177e4SLinus Torvalds 	if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
31911b34be74SYOSHIFUJI Hideaki 	    idev->cnf.accept_dad < 1 ||
319255ebaef1SNoriaki TAKAMIYA 	    !(ifp->flags&IFA_F_TENTATIVE) ||
319355ebaef1SNoriaki TAKAMIYA 	    ifp->flags & IFA_F_NODAD) {
3194cc411d0bSBrian Haley 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
319521809fafSstephen hemminger 		spin_unlock(&ifp->lock);
31961da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
31971da177e4SLinus Torvalds 
31981da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
31991da177e4SLinus Torvalds 		return;
32001da177e4SLinus Torvalds 	}
32011da177e4SLinus Torvalds 
32026732badeSYOSHIFUJI Hideaki 	if (!(idev->if_flags & IF_READY)) {
320321809fafSstephen hemminger 		spin_unlock(&ifp->lock);
32046732badeSYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
32053c21edbdSYOSHIFUJI Hideaki 		/*
3206590a9887SMasatake YAMATO 		 * If the device is not ready:
32073c21edbdSYOSHIFUJI Hideaki 		 * - keep it tentative if it is a permanent address.
32083c21edbdSYOSHIFUJI Hideaki 		 * - otherwise, kill it.
32093c21edbdSYOSHIFUJI Hideaki 		 */
32103c21edbdSYOSHIFUJI Hideaki 		in6_ifa_hold(ifp);
3211cc411d0bSBrian Haley 		addrconf_dad_stop(ifp, 0);
32126732badeSYOSHIFUJI Hideaki 		return;
32133c21edbdSYOSHIFUJI Hideaki 	}
321495c385b4SNeil Horman 
321595c385b4SNeil Horman 	/*
321695c385b4SNeil Horman 	 * Optimistic nodes can start receiving
321795c385b4SNeil Horman 	 * Frames right away
321895c385b4SNeil Horman 	 */
321995c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
322095c385b4SNeil Horman 		ip6_ins_rt(ifp->rt);
322195c385b4SNeil Horman 
32226732badeSYOSHIFUJI Hideaki 	addrconf_dad_kick(ifp);
32231da177e4SLinus Torvalds out:
3224622ccdf1SHerbert Xu 	spin_unlock(&ifp->lock);
32251da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
32261da177e4SLinus Torvalds }
32271da177e4SLinus Torvalds 
32281da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data)
32291da177e4SLinus Torvalds {
32301da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
32311da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
32321da177e4SLinus Torvalds 	struct in6_addr mcaddr;
32331da177e4SLinus Torvalds 
3234f2344a13SHerbert Xu 	if (!ifp->probes && addrconf_dad_end(ifp))
3235f2344a13SHerbert Xu 		goto out;
3236f2344a13SHerbert Xu 
3237122e4519Sstephen hemminger 	read_lock(&idev->lock);
3238122e4519Sstephen hemminger 	if (idev->dead || !(idev->if_flags & IF_READY)) {
3239122e4519Sstephen hemminger 		read_unlock(&idev->lock);
32401da177e4SLinus Torvalds 		goto out;
32411da177e4SLinus Torvalds 	}
324221809fafSstephen hemminger 
324321809fafSstephen hemminger 	spin_lock(&ifp->lock);
3244622ccdf1SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3245622ccdf1SHerbert Xu 		spin_unlock(&ifp->lock);
3246622ccdf1SHerbert Xu 		read_unlock(&idev->lock);
3247622ccdf1SHerbert Xu 		goto out;
3248622ccdf1SHerbert Xu 	}
3249622ccdf1SHerbert Xu 
32501da177e4SLinus Torvalds 	if (ifp->probes == 0) {
32511da177e4SLinus Torvalds 		/*
32521da177e4SLinus Torvalds 		 * DAD was successful
32531da177e4SLinus Torvalds 		 */
32541da177e4SLinus Torvalds 
3255cc411d0bSBrian Haley 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
325621809fafSstephen hemminger 		spin_unlock(&ifp->lock);
3257122e4519Sstephen hemminger 		read_unlock(&idev->lock);
32581da177e4SLinus Torvalds 
32591da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
32601da177e4SLinus Torvalds 
32611da177e4SLinus Torvalds 		goto out;
32621da177e4SLinus Torvalds 	}
32631da177e4SLinus Torvalds 
32641da177e4SLinus Torvalds 	ifp->probes--;
32651da177e4SLinus Torvalds 	addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
326621809fafSstephen hemminger 	spin_unlock(&ifp->lock);
3267122e4519Sstephen hemminger 	read_unlock(&idev->lock);
32681da177e4SLinus Torvalds 
32691da177e4SLinus Torvalds 	/* send a neighbour solicitation for our addr */
32701da177e4SLinus Torvalds 	addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
3271d7aabf22SYOSHIFUJI Hideaki 	ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
32721da177e4SLinus Torvalds out:
32731da177e4SLinus Torvalds 	in6_ifa_put(ifp);
32741da177e4SLinus Torvalds }
32751da177e4SLinus Torvalds 
32761da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
32771da177e4SLinus Torvalds {
32781da177e4SLinus Torvalds 	struct net_device *dev = ifp->idev->dev;
32791da177e4SLinus Torvalds 
32801da177e4SLinus Torvalds 	/*
32811da177e4SLinus Torvalds 	 *	Configure the address for reception. Now it is valid.
32821da177e4SLinus Torvalds 	 */
32831da177e4SLinus Torvalds 
32841da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_NEWADDR, ifp);
32851da177e4SLinus Torvalds 
3286026359bcSTore Anderson 	/* If added prefix is link local and we are prepared to process
3287026359bcSTore Anderson 	   router advertisements, start sending router solicitations.
32881da177e4SLinus Torvalds 	 */
32891da177e4SLinus Torvalds 
3290aeaf6e9dSShmulik Ladkani 	if (ipv6_accept_ra(ifp->idev) &&
32911da177e4SLinus Torvalds 	    ifp->idev->cnf.rtr_solicits > 0 &&
32921da177e4SLinus Torvalds 	    (dev->flags&IFF_LOOPBACK) == 0 &&
32931da177e4SLinus Torvalds 	    (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
32941da177e4SLinus Torvalds 		/*
32951da177e4SLinus Torvalds 		 *	If a host as already performed a random delay
32961da177e4SLinus Torvalds 		 *	[...] as part of DAD [...] there is no need
32971da177e4SLinus Torvalds 		 *	to delay again before sending the first RS
32981da177e4SLinus Torvalds 		 */
3299f3ee4010SYOSHIFUJI Hideaki 		ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
33001da177e4SLinus Torvalds 
33011da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
33021da177e4SLinus Torvalds 		ifp->probes = 1;
33031da177e4SLinus Torvalds 		ifp->idev->if_flags |= IF_RS_SENT;
33041da177e4SLinus Torvalds 		addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
33051da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
33061da177e4SLinus Torvalds 	}
33071da177e4SLinus Torvalds }
33081da177e4SLinus Torvalds 
3309e21e8467SStephen Hemminger static void addrconf_dad_run(struct inet6_dev *idev)
3310e21e8467SStephen Hemminger {
3311c5e33bddSYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifp;
3312c5e33bddSYOSHIFUJI Hideaki 
3313c5e33bddSYOSHIFUJI Hideaki 	read_lock_bh(&idev->lock);
3314502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
331521809fafSstephen hemminger 		spin_lock(&ifp->lock);
3316f2344a13SHerbert Xu 		if (ifp->flags & IFA_F_TENTATIVE &&
3317f2344a13SHerbert Xu 		    ifp->state == INET6_IFADDR_STATE_DAD)
3318c5e33bddSYOSHIFUJI Hideaki 			addrconf_dad_kick(ifp);
3319f2344a13SHerbert Xu 		spin_unlock(&ifp->lock);
3320c5e33bddSYOSHIFUJI Hideaki 	}
3321c5e33bddSYOSHIFUJI Hideaki 	read_unlock_bh(&idev->lock);
3322c5e33bddSYOSHIFUJI Hideaki }
3323c5e33bddSYOSHIFUJI Hideaki 
33241da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
33251da177e4SLinus Torvalds struct if6_iter_state {
33263c40090aSDaniel Lezcano 	struct seq_net_private p;
33271da177e4SLinus Torvalds 	int bucket;
33281d578303SMihai Maruseac 	int offset;
33291da177e4SLinus Torvalds };
33301da177e4SLinus Torvalds 
33311d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
33321da177e4SLinus Torvalds {
33331da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
33341da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
33351218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
33361d578303SMihai Maruseac 	int p = 0;
33371da177e4SLinus Torvalds 
33381d578303SMihai Maruseac 	/* initial bucket if pos is 0 */
33391d578303SMihai Maruseac 	if (pos == 0) {
33401d578303SMihai Maruseac 		state->bucket = 0;
33411d578303SMihai Maruseac 		state->offset = 0;
33421d578303SMihai Maruseac 	}
33431d578303SMihai Maruseac 
33441d578303SMihai Maruseac 	for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
3345b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
33461d578303SMihai Maruseac 					 addr_lst) {
33479f0d3c27SEric Dumazet 			if (!net_eq(dev_net(ifa->idev->dev), net))
33489f0d3c27SEric Dumazet 				continue;
33491d578303SMihai Maruseac 			/* sync with offset */
33501d578303SMihai Maruseac 			if (p < state->offset) {
33511d578303SMihai Maruseac 				p++;
33521d578303SMihai Maruseac 				continue;
33531d578303SMihai Maruseac 			}
33541d578303SMihai Maruseac 			state->offset++;
33551da177e4SLinus Torvalds 			return ifa;
33561da177e4SLinus Torvalds 		}
33571d578303SMihai Maruseac 
33581d578303SMihai Maruseac 		/* prepare for next bucket */
33591d578303SMihai Maruseac 		state->offset = 0;
33601d578303SMihai Maruseac 		p = 0;
33611d578303SMihai Maruseac 	}
3362c2e21293Sstephen hemminger 	return NULL;
3363c2e21293Sstephen hemminger }
33641da177e4SLinus Torvalds 
3365c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
3366c2e21293Sstephen hemminger 					 struct inet6_ifaddr *ifa)
33671da177e4SLinus Torvalds {
33681da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
33691218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
33701da177e4SLinus Torvalds 
3371b67bfe0dSSasha Levin 	hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
33729f0d3c27SEric Dumazet 		if (!net_eq(dev_net(ifa->idev->dev), net))
33739f0d3c27SEric Dumazet 			continue;
33741d578303SMihai Maruseac 		state->offset++;
33751da177e4SLinus Torvalds 		return ifa;
33761d578303SMihai Maruseac 	}
33771da177e4SLinus Torvalds 
3378c2e21293Sstephen hemminger 	while (++state->bucket < IN6_ADDR_HSIZE) {
33791d578303SMihai Maruseac 		state->offset = 0;
3380b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu_bh(ifa,
3381c2e21293Sstephen hemminger 				     &inet6_addr_lst[state->bucket], addr_lst) {
33829f0d3c27SEric Dumazet 			if (!net_eq(dev_net(ifa->idev->dev), net))
33839f0d3c27SEric Dumazet 				continue;
33841d578303SMihai Maruseac 			state->offset++;
3385c2e21293Sstephen hemminger 			return ifa;
3386c2e21293Sstephen hemminger 		}
3387c2e21293Sstephen hemminger 	}
3388c2e21293Sstephen hemminger 
3389c2e21293Sstephen hemminger 	return NULL;
3390c2e21293Sstephen hemminger }
3391c2e21293Sstephen hemminger 
33921da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
33934f70eccaSEric Dumazet 	__acquires(rcu_bh)
33941da177e4SLinus Torvalds {
33955c578aedSstephen hemminger 	rcu_read_lock_bh();
33961d578303SMihai Maruseac 	return if6_get_first(seq, *pos);
33971da177e4SLinus Torvalds }
33981da177e4SLinus Torvalds 
33991da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
34001da177e4SLinus Torvalds {
34011da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
34021da177e4SLinus Torvalds 
34031da177e4SLinus Torvalds 	ifa = if6_get_next(seq, v);
34041da177e4SLinus Torvalds 	++*pos;
34051da177e4SLinus Torvalds 	return ifa;
34061da177e4SLinus Torvalds }
34071da177e4SLinus Torvalds 
34081da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v)
34094f70eccaSEric Dumazet 	__releases(rcu_bh)
34101da177e4SLinus Torvalds {
34115c578aedSstephen hemminger 	rcu_read_unlock_bh();
34121da177e4SLinus Torvalds }
34131da177e4SLinus Torvalds 
34141da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v)
34151da177e4SLinus Torvalds {
34161da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
34174b7a4274SHarvey Harrison 	seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
3418b071195dSHarvey Harrison 		   &ifp->addr,
34191da177e4SLinus Torvalds 		   ifp->idev->dev->ifindex,
34201da177e4SLinus Torvalds 		   ifp->prefix_len,
34211da177e4SLinus Torvalds 		   ifp->scope,
34221da177e4SLinus Torvalds 		   ifp->flags,
34231da177e4SLinus Torvalds 		   ifp->idev->dev->name);
34241da177e4SLinus Torvalds 	return 0;
34251da177e4SLinus Torvalds }
34261da177e4SLinus Torvalds 
342756b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = {
34281da177e4SLinus Torvalds 	.start	= if6_seq_start,
34291da177e4SLinus Torvalds 	.next	= if6_seq_next,
34301da177e4SLinus Torvalds 	.show	= if6_seq_show,
34311da177e4SLinus Torvalds 	.stop	= if6_seq_stop,
34321da177e4SLinus Torvalds };
34331da177e4SLinus Torvalds 
34341da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file)
34351da177e4SLinus Torvalds {
34363c40090aSDaniel Lezcano 	return seq_open_net(inode, file, &if6_seq_ops,
3437cf7732e4SPavel Emelyanov 			    sizeof(struct if6_iter_state));
34381da177e4SLinus Torvalds }
34391da177e4SLinus Torvalds 
34409a32144eSArjan van de Ven static const struct file_operations if6_fops = {
34411da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
34421da177e4SLinus Torvalds 	.open		= if6_seq_open,
34431da177e4SLinus Torvalds 	.read		= seq_read,
34441da177e4SLinus Torvalds 	.llseek		= seq_lseek,
34453c40090aSDaniel Lezcano 	.release	= seq_release_net,
34461da177e4SLinus Torvalds };
34471da177e4SLinus Torvalds 
34482c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net)
34491da177e4SLinus Torvalds {
3450d4beaa66SGao feng 	if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
34511da177e4SLinus Torvalds 		return -ENOMEM;
34521da177e4SLinus Torvalds 	return 0;
34531da177e4SLinus Torvalds }
34541da177e4SLinus Torvalds 
34552c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net)
34563c40090aSDaniel Lezcano {
3457ece31ffdSGao feng 	remove_proc_entry("if_inet6", net->proc_net);
34583c40090aSDaniel Lezcano }
34593c40090aSDaniel Lezcano 
34603c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = {
34613c40090aSDaniel Lezcano        .init = if6_proc_net_init,
34623c40090aSDaniel Lezcano        .exit = if6_proc_net_exit,
34633c40090aSDaniel Lezcano };
34643c40090aSDaniel Lezcano 
34653c40090aSDaniel Lezcano int __init if6_proc_init(void)
34663c40090aSDaniel Lezcano {
34673c40090aSDaniel Lezcano 	return register_pernet_subsys(&if6_proc_net_ops);
34683c40090aSDaniel Lezcano }
34693c40090aSDaniel Lezcano 
34701da177e4SLinus Torvalds void if6_proc_exit(void)
34711da177e4SLinus Torvalds {
34723c40090aSDaniel Lezcano 	unregister_pernet_subsys(&if6_proc_net_ops);
34731da177e4SLinus Torvalds }
34741da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
34751da177e4SLinus Torvalds 
347607a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
34773b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */
3478b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
34793b9f9a1cSNoriaki TAKAMIYA {
34803b9f9a1cSNoriaki TAKAMIYA 	int ret = 0;
3481c2e21293Sstephen hemminger 	struct inet6_ifaddr *ifp = NULL;
3482ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
3483c2e21293Sstephen hemminger 
34845c578aedSstephen hemminger 	rcu_read_lock_bh();
3485b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
3486878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
3487389f6612SDaniel Lezcano 			continue;
3488caad295fSYOSHIFUJI Hideaki 		if (ipv6_addr_equal(&ifp->addr, addr) &&
34893b9f9a1cSNoriaki TAKAMIYA 		    (ifp->flags & IFA_F_HOMEADDRESS)) {
34903b9f9a1cSNoriaki TAKAMIYA 			ret = 1;
34913b9f9a1cSNoriaki TAKAMIYA 			break;
34923b9f9a1cSNoriaki TAKAMIYA 		}
34933b9f9a1cSNoriaki TAKAMIYA 	}
34945c578aedSstephen hemminger 	rcu_read_unlock_bh();
34953b9f9a1cSNoriaki TAKAMIYA 	return ret;
34963b9f9a1cSNoriaki TAKAMIYA }
34973b9f9a1cSNoriaki TAKAMIYA #endif
34983b9f9a1cSNoriaki TAKAMIYA 
34991da177e4SLinus Torvalds /*
35001da177e4SLinus Torvalds  *	Periodic address status verification
35011da177e4SLinus Torvalds  */
35021da177e4SLinus Torvalds 
35031da177e4SLinus Torvalds static void addrconf_verify(unsigned long foo)
35041da177e4SLinus Torvalds {
3505b2db7564SYOSHIFUJI Hideaki 	unsigned long now, next, next_sec, next_sched;
35061da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
35071da177e4SLinus Torvalds 	int i;
35081da177e4SLinus Torvalds 
35095c578aedSstephen hemminger 	rcu_read_lock_bh();
35105c578aedSstephen hemminger 	spin_lock(&addrconf_verify_lock);
35111da177e4SLinus Torvalds 	now = jiffies;
3512b2db7564SYOSHIFUJI Hideaki 	next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
35131da177e4SLinus Torvalds 
35141da177e4SLinus Torvalds 	del_timer(&addr_chk_timer);
35151da177e4SLinus Torvalds 
35161da177e4SLinus Torvalds 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
35171da177e4SLinus Torvalds restart:
3518b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu_bh(ifp,
35195c578aedSstephen hemminger 					 &inet6_addr_lst[i], addr_lst) {
35201da177e4SLinus Torvalds 			unsigned long age;
35211da177e4SLinus Torvalds 
35221da177e4SLinus Torvalds 			if (ifp->flags & IFA_F_PERMANENT)
35231da177e4SLinus Torvalds 				continue;
35241da177e4SLinus Torvalds 
35251da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
3526b2db7564SYOSHIFUJI Hideaki 			/* We try to batch several events at once. */
3527b2db7564SYOSHIFUJI Hideaki 			age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
35281da177e4SLinus Torvalds 
35298f27ebb9SYOSHIFUJI Hideaki 			if (ifp->valid_lft != INFINITY_LIFE_TIME &&
35308f27ebb9SYOSHIFUJI Hideaki 			    age >= ifp->valid_lft) {
35311da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
35321da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
35331da177e4SLinus Torvalds 				ipv6_del_addr(ifp);
35341da177e4SLinus Torvalds 				goto restart;
35358f27ebb9SYOSHIFUJI Hideaki 			} else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
35368f27ebb9SYOSHIFUJI Hideaki 				spin_unlock(&ifp->lock);
35378f27ebb9SYOSHIFUJI Hideaki 				continue;
35381da177e4SLinus Torvalds 			} else if (age >= ifp->prefered_lft) {
3539a1ed0526SBrian Haley 				/* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
35401da177e4SLinus Torvalds 				int deprecate = 0;
35411da177e4SLinus Torvalds 
35421da177e4SLinus Torvalds 				if (!(ifp->flags&IFA_F_DEPRECATED)) {
35431da177e4SLinus Torvalds 					deprecate = 1;
35441da177e4SLinus Torvalds 					ifp->flags |= IFA_F_DEPRECATED;
35451da177e4SLinus Torvalds 				}
35461da177e4SLinus Torvalds 
35471da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
35481da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->valid_lft * HZ;
35491da177e4SLinus Torvalds 
35501da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
35511da177e4SLinus Torvalds 
35521da177e4SLinus Torvalds 				if (deprecate) {
35531da177e4SLinus Torvalds 					in6_ifa_hold(ifp);
35541da177e4SLinus Torvalds 
35551da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
35561da177e4SLinus Torvalds 					in6_ifa_put(ifp);
35571da177e4SLinus Torvalds 					goto restart;
35581da177e4SLinus Torvalds 				}
35591da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
35601da177e4SLinus Torvalds 			} else if ((ifp->flags&IFA_F_TEMPORARY) &&
35611da177e4SLinus Torvalds 				   !(ifp->flags&IFA_F_TENTATIVE)) {
356288949cf4Sstephen hemminger 				unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
356388949cf4Sstephen hemminger 					ifp->idev->cnf.dad_transmits *
356488949cf4Sstephen hemminger 					ifp->idev->nd_parms->retrans_time / HZ;
356588949cf4Sstephen hemminger 
35661da177e4SLinus Torvalds 				if (age >= ifp->prefered_lft - regen_advance) {
35671da177e4SLinus Torvalds 					struct inet6_ifaddr *ifpub = ifp->ifpub;
35681da177e4SLinus Torvalds 					if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
35691da177e4SLinus Torvalds 						next = ifp->tstamp + ifp->prefered_lft * HZ;
35701da177e4SLinus Torvalds 					if (!ifp->regen_count && ifpub) {
35711da177e4SLinus Torvalds 						ifp->regen_count++;
35721da177e4SLinus Torvalds 						in6_ifa_hold(ifp);
35731da177e4SLinus Torvalds 						in6_ifa_hold(ifpub);
35741da177e4SLinus Torvalds 						spin_unlock(&ifp->lock);
35755c578aedSstephen hemminger 
3576291d809bSHiroyuki YAMAMORI 						spin_lock(&ifpub->lock);
3577291d809bSHiroyuki YAMAMORI 						ifpub->regen_count = 0;
3578291d809bSHiroyuki YAMAMORI 						spin_unlock(&ifpub->lock);
35791da177e4SLinus Torvalds 						ipv6_create_tempaddr(ifpub, ifp);
35801da177e4SLinus Torvalds 						in6_ifa_put(ifpub);
35811da177e4SLinus Torvalds 						in6_ifa_put(ifp);
35821da177e4SLinus Torvalds 						goto restart;
35831da177e4SLinus Torvalds 					}
35841da177e4SLinus Torvalds 				} else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
35851da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
35861da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
35871da177e4SLinus Torvalds #endif
35881da177e4SLinus Torvalds 			} else {
35891da177e4SLinus Torvalds 				/* ifp->prefered_lft <= ifp->valid_lft */
35901da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
35911da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ;
35921da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
35931da177e4SLinus Torvalds 			}
35941da177e4SLinus Torvalds 		}
35951da177e4SLinus Torvalds 	}
35961da177e4SLinus Torvalds 
3597b2db7564SYOSHIFUJI Hideaki 	next_sec = round_jiffies_up(next);
3598b2db7564SYOSHIFUJI Hideaki 	next_sched = next;
3599b2db7564SYOSHIFUJI Hideaki 
3600b2db7564SYOSHIFUJI Hideaki 	/* If rounded timeout is accurate enough, accept it. */
3601b2db7564SYOSHIFUJI Hideaki 	if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
3602b2db7564SYOSHIFUJI Hideaki 		next_sched = next_sec;
3603b2db7564SYOSHIFUJI Hideaki 
3604b2db7564SYOSHIFUJI Hideaki 	/* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
3605b2db7564SYOSHIFUJI Hideaki 	if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
3606b2db7564SYOSHIFUJI Hideaki 		next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
3607b2db7564SYOSHIFUJI Hideaki 
3608b2db7564SYOSHIFUJI Hideaki 	ADBG((KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
3609b2db7564SYOSHIFUJI Hideaki 	      now, next, next_sec, next_sched));
3610b2db7564SYOSHIFUJI Hideaki 
3611b2db7564SYOSHIFUJI Hideaki 	addr_chk_timer.expires = next_sched;
36121da177e4SLinus Torvalds 	add_timer(&addr_chk_timer);
36135c578aedSstephen hemminger 	spin_unlock(&addrconf_verify_lock);
36145c578aedSstephen hemminger 	rcu_read_unlock_bh();
36151da177e4SLinus Torvalds }
36161da177e4SLinus Torvalds 
3617caeaba79SNicolas Dichtel static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
3618caeaba79SNicolas Dichtel 				     struct in6_addr **peer_pfx)
3619461d8837SThomas Graf {
3620461d8837SThomas Graf 	struct in6_addr *pfx = NULL;
3621461d8837SThomas Graf 
3622caeaba79SNicolas Dichtel 	*peer_pfx = NULL;
3623caeaba79SNicolas Dichtel 
3624461d8837SThomas Graf 	if (addr)
3625461d8837SThomas Graf 		pfx = nla_data(addr);
3626461d8837SThomas Graf 
3627461d8837SThomas Graf 	if (local) {
3628461d8837SThomas Graf 		if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3629caeaba79SNicolas Dichtel 			*peer_pfx = pfx;
3630461d8837SThomas Graf 		pfx = nla_data(local);
3631461d8837SThomas Graf 	}
3632461d8837SThomas Graf 
3633461d8837SThomas Graf 	return pfx;
3634461d8837SThomas Graf }
3635461d8837SThomas Graf 
3636ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
3637461d8837SThomas Graf 	[IFA_ADDRESS]		= { .len = sizeof(struct in6_addr) },
3638461d8837SThomas Graf 	[IFA_LOCAL]		= { .len = sizeof(struct in6_addr) },
3639461d8837SThomas Graf 	[IFA_CACHEINFO]		= { .len = sizeof(struct ifa_cacheinfo) },
3640461d8837SThomas Graf };
3641461d8837SThomas Graf 
36421da177e4SLinus Torvalds static int
3643661d2967SThomas Graf inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
36441da177e4SLinus Torvalds {
36453b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
3646b933f716SThomas Graf 	struct ifaddrmsg *ifm;
3647b933f716SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
3648caeaba79SNicolas Dichtel 	struct in6_addr *pfx, *peer_pfx;
3649b933f716SThomas Graf 	int err;
36501da177e4SLinus Torvalds 
3651b933f716SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3652b933f716SThomas Graf 	if (err < 0)
3653b933f716SThomas Graf 		return err;
3654b933f716SThomas Graf 
3655b933f716SThomas Graf 	ifm = nlmsg_data(nlh);
3656caeaba79SNicolas Dichtel 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
36571da177e4SLinus Torvalds 	if (pfx == NULL)
36581da177e4SLinus Torvalds 		return -EINVAL;
36591da177e4SLinus Torvalds 
3660af284937SDaniel Lezcano 	return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen);
36611da177e4SLinus Torvalds }
36621da177e4SLinus Torvalds 
366355ebaef1SNoriaki TAKAMIYA static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
366455ebaef1SNoriaki TAKAMIYA 			     u32 prefered_lft, u32 valid_lft)
3665081bba5bSNoriaki TAKAMIYA {
36666f704992SYOSHIFUJI Hideaki 	u32 flags;
36676f704992SYOSHIFUJI Hideaki 	clock_t expires;
36684bed72e4SYOSHIFUJI Hideaki 	unsigned long timeout;
366946d48046SYOSHIFUJI Hideaki 
3670081bba5bSNoriaki TAKAMIYA 	if (!valid_lft || (prefered_lft > valid_lft))
3671081bba5bSNoriaki TAKAMIYA 		return -EINVAL;
3672081bba5bSNoriaki TAKAMIYA 
36734bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
36744bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
36754bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
36764bed72e4SYOSHIFUJI Hideaki 		valid_lft = timeout;
36776f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
36784bed72e4SYOSHIFUJI Hideaki 	} else {
36794bed72e4SYOSHIFUJI Hideaki 		expires = 0;
36804bed72e4SYOSHIFUJI Hideaki 		flags = 0;
36814bed72e4SYOSHIFUJI Hideaki 		ifa_flags |= IFA_F_PERMANENT;
36826f704992SYOSHIFUJI Hideaki 	}
3683081bba5bSNoriaki TAKAMIYA 
36844bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
36854bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
36864bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
368755ebaef1SNoriaki TAKAMIYA 			ifa_flags |= IFA_F_DEPRECATED;
36884bed72e4SYOSHIFUJI Hideaki 		prefered_lft = timeout;
36894bed72e4SYOSHIFUJI Hideaki 	}
3690081bba5bSNoriaki TAKAMIYA 
3691081bba5bSNoriaki TAKAMIYA 	spin_lock_bh(&ifp->lock);
36923b9f9a1cSNoriaki TAKAMIYA 	ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags;
3693081bba5bSNoriaki TAKAMIYA 	ifp->tstamp = jiffies;
3694081bba5bSNoriaki TAKAMIYA 	ifp->valid_lft = valid_lft;
3695081bba5bSNoriaki TAKAMIYA 	ifp->prefered_lft = prefered_lft;
3696081bba5bSNoriaki TAKAMIYA 
3697081bba5bSNoriaki TAKAMIYA 	spin_unlock_bh(&ifp->lock);
3698081bba5bSNoriaki TAKAMIYA 	if (!(ifp->flags&IFA_F_TENTATIVE))
3699081bba5bSNoriaki TAKAMIYA 		ipv6_ifa_notify(0, ifp);
3700081bba5bSNoriaki TAKAMIYA 
370146d48046SYOSHIFUJI Hideaki 	addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
37026f704992SYOSHIFUJI Hideaki 			      expires, flags);
3703081bba5bSNoriaki TAKAMIYA 	addrconf_verify(0);
3704081bba5bSNoriaki TAKAMIYA 
3705081bba5bSNoriaki TAKAMIYA 	return 0;
3706081bba5bSNoriaki TAKAMIYA }
3707081bba5bSNoriaki TAKAMIYA 
3708081bba5bSNoriaki TAKAMIYA static int
3709661d2967SThomas Graf inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
37101da177e4SLinus Torvalds {
37113b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
3712461d8837SThomas Graf 	struct ifaddrmsg *ifm;
3713461d8837SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
3714caeaba79SNicolas Dichtel 	struct in6_addr *pfx, *peer_pfx;
37157198f8ceSThomas Graf 	struct inet6_ifaddr *ifa;
37167198f8ceSThomas Graf 	struct net_device *dev;
371755ebaef1SNoriaki TAKAMIYA 	u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
371855ebaef1SNoriaki TAKAMIYA 	u8 ifa_flags;
3719461d8837SThomas Graf 	int err;
37201da177e4SLinus Torvalds 
3721461d8837SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3722461d8837SThomas Graf 	if (err < 0)
3723461d8837SThomas Graf 		return err;
3724461d8837SThomas Graf 
3725461d8837SThomas Graf 	ifm = nlmsg_data(nlh);
3726caeaba79SNicolas Dichtel 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
37271da177e4SLinus Torvalds 	if (pfx == NULL)
37281da177e4SLinus Torvalds 		return -EINVAL;
37291da177e4SLinus Torvalds 
3730461d8837SThomas Graf 	if (tb[IFA_CACHEINFO]) {
37310778769dSNoriaki TAKAMIYA 		struct ifa_cacheinfo *ci;
3732461d8837SThomas Graf 
3733461d8837SThomas Graf 		ci = nla_data(tb[IFA_CACHEINFO]);
37340778769dSNoriaki TAKAMIYA 		valid_lft = ci->ifa_valid;
3735461d8837SThomas Graf 		preferred_lft = ci->ifa_prefered;
3736461d8837SThomas Graf 	} else {
3737461d8837SThomas Graf 		preferred_lft = INFINITY_LIFE_TIME;
3738461d8837SThomas Graf 		valid_lft = INFINITY_LIFE_TIME;
37390778769dSNoriaki TAKAMIYA 	}
37400778769dSNoriaki TAKAMIYA 
3741af284937SDaniel Lezcano 	dev =  __dev_get_by_index(net, ifm->ifa_index);
37427198f8ceSThomas Graf 	if (dev == NULL)
37437198f8ceSThomas Graf 		return -ENODEV;
3744081bba5bSNoriaki TAKAMIYA 
374555ebaef1SNoriaki TAKAMIYA 	/* We ignore other flags so far. */
37463b9f9a1cSNoriaki TAKAMIYA 	ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
374755ebaef1SNoriaki TAKAMIYA 
37481cab3da6SDaniel Lezcano 	ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
37497198f8ceSThomas Graf 	if (ifa == NULL) {
37507198f8ceSThomas Graf 		/*
37517198f8ceSThomas Graf 		 * It would be best to check for !NLM_F_CREATE here but
37527198f8ceSThomas Graf 		 * userspace alreay relies on not having to provide this.
37537198f8ceSThomas Graf 		 */
3754caeaba79SNicolas Dichtel 		return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
3755af284937SDaniel Lezcano 				      ifm->ifa_prefixlen, ifa_flags,
3756af284937SDaniel Lezcano 				      preferred_lft, valid_lft);
37571da177e4SLinus Torvalds 	}
37581da177e4SLinus Torvalds 
37597198f8ceSThomas Graf 	if (nlh->nlmsg_flags & NLM_F_EXCL ||
37607198f8ceSThomas Graf 	    !(nlh->nlmsg_flags & NLM_F_REPLACE))
37617198f8ceSThomas Graf 		err = -EEXIST;
37627198f8ceSThomas Graf 	else
376355ebaef1SNoriaki TAKAMIYA 		err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
37647198f8ceSThomas Graf 
37657198f8ceSThomas Graf 	in6_ifa_put(ifa);
37667198f8ceSThomas Graf 
37677198f8ceSThomas Graf 	return err;
37687198f8ceSThomas Graf }
37697198f8ceSThomas Graf 
3770101bb229SThomas Graf static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3771101bb229SThomas Graf 			  u8 scope, int ifindex)
3772101bb229SThomas Graf {
3773101bb229SThomas Graf 	struct ifaddrmsg *ifm;
3774101bb229SThomas Graf 
3775101bb229SThomas Graf 	ifm = nlmsg_data(nlh);
3776101bb229SThomas Graf 	ifm->ifa_family = AF_INET6;
3777101bb229SThomas Graf 	ifm->ifa_prefixlen = prefixlen;
3778101bb229SThomas Graf 	ifm->ifa_flags = flags;
3779101bb229SThomas Graf 	ifm->ifa_scope = scope;
3780101bb229SThomas Graf 	ifm->ifa_index = ifindex;
3781101bb229SThomas Graf }
3782101bb229SThomas Graf 
378385486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
378485486af0SThomas Graf 			 unsigned long tstamp, u32 preferred, u32 valid)
378585486af0SThomas Graf {
378685486af0SThomas Graf 	struct ifa_cacheinfo ci;
378785486af0SThomas Graf 
378818a31e1eSThomas Graf 	ci.cstamp = cstamp_delta(cstamp);
378918a31e1eSThomas Graf 	ci.tstamp = cstamp_delta(tstamp);
379085486af0SThomas Graf 	ci.ifa_prefered = preferred;
379185486af0SThomas Graf 	ci.ifa_valid = valid;
379285486af0SThomas Graf 
379385486af0SThomas Graf 	return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
379485486af0SThomas Graf }
379585486af0SThomas Graf 
3796101bb229SThomas Graf static inline int rt_scope(int ifa_scope)
3797101bb229SThomas Graf {
3798101bb229SThomas Graf 	if (ifa_scope & IFA_HOST)
3799101bb229SThomas Graf 		return RT_SCOPE_HOST;
3800101bb229SThomas Graf 	else if (ifa_scope & IFA_LINK)
3801101bb229SThomas Graf 		return RT_SCOPE_LINK;
3802101bb229SThomas Graf 	else if (ifa_scope & IFA_SITE)
3803101bb229SThomas Graf 		return RT_SCOPE_SITE;
3804101bb229SThomas Graf 	else
3805101bb229SThomas Graf 		return RT_SCOPE_UNIVERSE;
3806101bb229SThomas Graf }
3807101bb229SThomas Graf 
38080ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void)
38090ab6803bSThomas Graf {
3810339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3811caeaba79SNicolas Dichtel 	       + nla_total_size(16) /* IFA_LOCAL */
3812339bf98fSThomas Graf 	       + nla_total_size(16) /* IFA_ADDRESS */
3813339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct ifa_cacheinfo));
38140ab6803bSThomas Graf }
3815c5396a31SYOSHIFUJI Hideaki 
38161da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
381715e47304SEric W. Biederman 			     u32 portid, u32 seq, int event, unsigned int flags)
38181da177e4SLinus Torvalds {
38191da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
382085486af0SThomas Graf 	u32 preferred, valid;
38211da177e4SLinus Torvalds 
382215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
38230ab6803bSThomas Graf 	if (nlh == NULL)
382426932566SPatrick McHardy 		return -EMSGSIZE;
38250ab6803bSThomas Graf 
3826101bb229SThomas Graf 	put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3827101bb229SThomas Graf 		      ifa->idev->dev->ifindex);
3828101bb229SThomas Graf 
38291da177e4SLinus Torvalds 	if (!(ifa->flags&IFA_F_PERMANENT)) {
383085486af0SThomas Graf 		preferred = ifa->prefered_lft;
383185486af0SThomas Graf 		valid = ifa->valid_lft;
383285486af0SThomas Graf 		if (preferred != INFINITY_LIFE_TIME) {
38331da177e4SLinus Torvalds 			long tval = (jiffies - ifa->tstamp)/HZ;
3834a1faa698SJens Rosenboom 			if (preferred > tval)
383585486af0SThomas Graf 				preferred -= tval;
3836a1faa698SJens Rosenboom 			else
3837a1faa698SJens Rosenboom 				preferred = 0;
3838f56619fcSBen Hutchings 			if (valid != INFINITY_LIFE_TIME) {
3839f56619fcSBen Hutchings 				if (valid > tval)
384085486af0SThomas Graf 					valid -= tval;
3841f56619fcSBen Hutchings 				else
3842f56619fcSBen Hutchings 					valid = 0;
3843f56619fcSBen Hutchings 			}
38441da177e4SLinus Torvalds 		}
38451da177e4SLinus Torvalds 	} else {
384685486af0SThomas Graf 		preferred = INFINITY_LIFE_TIME;
384785486af0SThomas Graf 		valid = INFINITY_LIFE_TIME;
38481da177e4SLinus Torvalds 	}
384985486af0SThomas Graf 
38507996c799SCong Wang 	if (!ipv6_addr_any(&ifa->peer_addr)) {
3851caeaba79SNicolas Dichtel 		if (nla_put(skb, IFA_LOCAL, 16, &ifa->addr) < 0 ||
3852caeaba79SNicolas Dichtel 		    nla_put(skb, IFA_ADDRESS, 16, &ifa->peer_addr) < 0)
3853caeaba79SNicolas Dichtel 			goto error;
3854caeaba79SNicolas Dichtel 	} else
3855caeaba79SNicolas Dichtel 		if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0)
3856caeaba79SNicolas Dichtel 			goto error;
3857caeaba79SNicolas Dichtel 
3858caeaba79SNicolas Dichtel 	if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
3859caeaba79SNicolas Dichtel 		goto error;
386085486af0SThomas Graf 
38610ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
3862caeaba79SNicolas Dichtel 
3863caeaba79SNicolas Dichtel error:
3864caeaba79SNicolas Dichtel 	nlmsg_cancel(skb, nlh);
3865caeaba79SNicolas Dichtel 	return -EMSGSIZE;
38661da177e4SLinus Torvalds }
38671da177e4SLinus Torvalds 
38681da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
386915e47304SEric W. Biederman 				u32 portid, u32 seq, int event, u16 flags)
38701da177e4SLinus Torvalds {
38711da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
3872101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
3873101bb229SThomas Graf 	int ifindex = ifmca->idev->dev->ifindex;
38741da177e4SLinus Torvalds 
38751da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3876101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
3877101bb229SThomas Graf 
387815e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
38790ab6803bSThomas Graf 	if (nlh == NULL)
388026932566SPatrick McHardy 		return -EMSGSIZE;
38810ab6803bSThomas Graf 
3882101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
38830ab6803bSThomas Graf 	if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
38840ab6803bSThomas Graf 	    put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
388526932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
388626932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
388726932566SPatrick McHardy 		return -EMSGSIZE;
388826932566SPatrick McHardy 	}
388985486af0SThomas Graf 
38900ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
38911da177e4SLinus Torvalds }
38921da177e4SLinus Torvalds 
38931da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
389415e47304SEric W. Biederman 				u32 portid, u32 seq, int event, unsigned int flags)
38951da177e4SLinus Torvalds {
38961da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
3897101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
3898101bb229SThomas Graf 	int ifindex = ifaca->aca_idev->dev->ifindex;
38991da177e4SLinus Torvalds 
39001da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3901101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
3902101bb229SThomas Graf 
390315e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
39040ab6803bSThomas Graf 	if (nlh == NULL)
390526932566SPatrick McHardy 		return -EMSGSIZE;
39060ab6803bSThomas Graf 
3907101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
39080ab6803bSThomas Graf 	if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
39090ab6803bSThomas Graf 	    put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
391026932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
391126932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
391226932566SPatrick McHardy 		return -EMSGSIZE;
391326932566SPatrick McHardy 	}
391485486af0SThomas Graf 
39150ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
39161da177e4SLinus Torvalds }
39171da177e4SLinus Torvalds 
3918e21e8467SStephen Hemminger enum addr_type_t {
39191da177e4SLinus Torvalds 	UNICAST_ADDR,
39201da177e4SLinus Torvalds 	MULTICAST_ADDR,
39211da177e4SLinus Torvalds 	ANYCAST_ADDR,
39221da177e4SLinus Torvalds };
39231da177e4SLinus Torvalds 
3924234b27c3SEric Dumazet /* called with rcu_read_lock() */
3925234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
3926234b27c3SEric Dumazet 			  struct netlink_callback *cb, enum addr_type_t type,
3927234b27c3SEric Dumazet 			  int s_ip_idx, int *p_ip_idx)
39281da177e4SLinus Torvalds {
39291da177e4SLinus Torvalds 	struct ifmcaddr6 *ifmca;
39301da177e4SLinus Torvalds 	struct ifacaddr6 *ifaca;
3931234b27c3SEric Dumazet 	int err = 1;
3932234b27c3SEric Dumazet 	int ip_idx = *p_ip_idx;
39331da177e4SLinus Torvalds 
39341da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
39351da177e4SLinus Torvalds 	switch (type) {
3936502a2ffdSstephen hemminger 	case UNICAST_ADDR: {
3937502a2ffdSstephen hemminger 		struct inet6_ifaddr *ifa;
3938502a2ffdSstephen hemminger 
3939ae9cda5dSYOSHIFUJI Hideaki 		/* unicast address incl. temp addr */
3940502a2ffdSstephen hemminger 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
3941502a2ffdSstephen hemminger 			if (++ip_idx < s_ip_idx)
39421da177e4SLinus Torvalds 				continue;
39435d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifaddr(skb, ifa,
394415e47304SEric W. Biederman 						NETLINK_CB(cb->skb).portid,
39455d5619b4SYOSHIFUJI Hideaki 						cb->nlh->nlmsg_seq,
39465d5619b4SYOSHIFUJI Hideaki 						RTM_NEWADDR,
39475d5619b4SYOSHIFUJI Hideaki 						NLM_F_MULTI);
3948bcd32326SEric Dumazet 			if (err <= 0)
3949bcd32326SEric Dumazet 				break;
395063998ac2SNicolas Dichtel 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
39511da177e4SLinus Torvalds 		}
39521da177e4SLinus Torvalds 		break;
3953502a2ffdSstephen hemminger 	}
39541da177e4SLinus Torvalds 	case MULTICAST_ADDR:
39551da177e4SLinus Torvalds 		/* multicast address */
39561da177e4SLinus Torvalds 		for (ifmca = idev->mc_list; ifmca;
39571da177e4SLinus Torvalds 		     ifmca = ifmca->next, ip_idx++) {
39581da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
39591da177e4SLinus Torvalds 				continue;
39605d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifmcaddr(skb, ifmca,
396115e47304SEric W. Biederman 						  NETLINK_CB(cb->skb).portid,
39625d5619b4SYOSHIFUJI Hideaki 						  cb->nlh->nlmsg_seq,
39635d5619b4SYOSHIFUJI Hideaki 						  RTM_GETMULTICAST,
39645d5619b4SYOSHIFUJI Hideaki 						  NLM_F_MULTI);
3965bcd32326SEric Dumazet 			if (err <= 0)
3966bcd32326SEric Dumazet 				break;
39671da177e4SLinus Torvalds 		}
39681da177e4SLinus Torvalds 		break;
39691da177e4SLinus Torvalds 	case ANYCAST_ADDR:
39701da177e4SLinus Torvalds 		/* anycast address */
39711da177e4SLinus Torvalds 		for (ifaca = idev->ac_list; ifaca;
39721da177e4SLinus Torvalds 		     ifaca = ifaca->aca_next, ip_idx++) {
39731da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
39741da177e4SLinus Torvalds 				continue;
39755d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifacaddr(skb, ifaca,
397615e47304SEric W. Biederman 						  NETLINK_CB(cb->skb).portid,
39775d5619b4SYOSHIFUJI Hideaki 						  cb->nlh->nlmsg_seq,
39785d5619b4SYOSHIFUJI Hideaki 						  RTM_GETANYCAST,
39795d5619b4SYOSHIFUJI Hideaki 						  NLM_F_MULTI);
3980bcd32326SEric Dumazet 			if (err <= 0)
3981bcd32326SEric Dumazet 				break;
39821da177e4SLinus Torvalds 		}
39831da177e4SLinus Torvalds 		break;
39841da177e4SLinus Torvalds 	default:
39851da177e4SLinus Torvalds 		break;
39861da177e4SLinus Torvalds 	}
39871da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
3988234b27c3SEric Dumazet 	*p_ip_idx = ip_idx;
3989234b27c3SEric Dumazet 	return err;
3990234b27c3SEric Dumazet }
39915d5619b4SYOSHIFUJI Hideaki 
3992234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3993234b27c3SEric Dumazet 			   enum addr_type_t type)
3994234b27c3SEric Dumazet {
3995234b27c3SEric Dumazet 	struct net *net = sock_net(skb->sk);
3996234b27c3SEric Dumazet 	int h, s_h;
3997234b27c3SEric Dumazet 	int idx, ip_idx;
3998234b27c3SEric Dumazet 	int s_idx, s_ip_idx;
3999234b27c3SEric Dumazet 	struct net_device *dev;
4000234b27c3SEric Dumazet 	struct inet6_dev *idev;
4001234b27c3SEric Dumazet 	struct hlist_head *head;
4002234b27c3SEric Dumazet 
4003234b27c3SEric Dumazet 	s_h = cb->args[0];
4004234b27c3SEric Dumazet 	s_idx = idx = cb->args[1];
4005234b27c3SEric Dumazet 	s_ip_idx = ip_idx = cb->args[2];
4006234b27c3SEric Dumazet 
4007234b27c3SEric Dumazet 	rcu_read_lock();
400863998ac2SNicolas Dichtel 	cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
4009234b27c3SEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4010234b27c3SEric Dumazet 		idx = 0;
4011234b27c3SEric Dumazet 		head = &net->dev_index_head[h];
4012b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
4013234b27c3SEric Dumazet 			if (idx < s_idx)
4014234b27c3SEric Dumazet 				goto cont;
40154b97efdfSPatrick McHardy 			if (h > s_h || idx > s_idx)
4016234b27c3SEric Dumazet 				s_ip_idx = 0;
4017234b27c3SEric Dumazet 			ip_idx = 0;
4018e21e8467SStephen Hemminger 			idev = __in6_dev_get(dev);
4019e21e8467SStephen Hemminger 			if (!idev)
4020234b27c3SEric Dumazet 				goto cont;
4021234b27c3SEric Dumazet 
4022234b27c3SEric Dumazet 			if (in6_dump_addrs(idev, skb, cb, type,
4023234b27c3SEric Dumazet 					   s_ip_idx, &ip_idx) <= 0)
4024234b27c3SEric Dumazet 				goto done;
40257562f876SPavel Emelianov cont:
40267562f876SPavel Emelianov 			idx++;
40271da177e4SLinus Torvalds 		}
4028234b27c3SEric Dumazet 	}
4029234b27c3SEric Dumazet done:
4030234b27c3SEric Dumazet 	rcu_read_unlock();
4031234b27c3SEric Dumazet 	cb->args[0] = h;
4032234b27c3SEric Dumazet 	cb->args[1] = idx;
4033234b27c3SEric Dumazet 	cb->args[2] = ip_idx;
4034234b27c3SEric Dumazet 
40351da177e4SLinus Torvalds 	return skb->len;
40361da177e4SLinus Torvalds }
40371da177e4SLinus Torvalds 
40381da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
40391da177e4SLinus Torvalds {
40401da177e4SLinus Torvalds 	enum addr_type_t type = UNICAST_ADDR;
4041b854272bSDenis V. Lunev 
40421da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
40431da177e4SLinus Torvalds }
40441da177e4SLinus Torvalds 
40451da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
40461da177e4SLinus Torvalds {
40471da177e4SLinus Torvalds 	enum addr_type_t type = MULTICAST_ADDR;
4048b854272bSDenis V. Lunev 
40491da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
40501da177e4SLinus Torvalds }
40511da177e4SLinus Torvalds 
40521da177e4SLinus Torvalds 
40531da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
40541da177e4SLinus Torvalds {
40551da177e4SLinus Torvalds 	enum addr_type_t type = ANYCAST_ADDR;
4056b854272bSDenis V. Lunev 
40571da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
40581da177e4SLinus Torvalds }
40591da177e4SLinus Torvalds 
4060661d2967SThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
40616c223828SNoriaki TAKAMIYA {
40623b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
40631b29fc2cSThomas Graf 	struct ifaddrmsg *ifm;
40641b29fc2cSThomas Graf 	struct nlattr *tb[IFA_MAX+1];
4065caeaba79SNicolas Dichtel 	struct in6_addr *addr = NULL, *peer;
40666c223828SNoriaki TAKAMIYA 	struct net_device *dev = NULL;
40676c223828SNoriaki TAKAMIYA 	struct inet6_ifaddr *ifa;
40686c223828SNoriaki TAKAMIYA 	struct sk_buff *skb;
40696c223828SNoriaki TAKAMIYA 	int err;
40706c223828SNoriaki TAKAMIYA 
40711b29fc2cSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
40721b29fc2cSThomas Graf 	if (err < 0)
40731b29fc2cSThomas Graf 		goto errout;
40746c223828SNoriaki TAKAMIYA 
4075caeaba79SNicolas Dichtel 	addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
40761b29fc2cSThomas Graf 	if (addr == NULL) {
40771b29fc2cSThomas Graf 		err = -EINVAL;
40781b29fc2cSThomas Graf 		goto errout;
40791b29fc2cSThomas Graf 	}
40801b29fc2cSThomas Graf 
40811b29fc2cSThomas Graf 	ifm = nlmsg_data(nlh);
40826c223828SNoriaki TAKAMIYA 	if (ifm->ifa_index)
40836fda7350SBenjamin Thery 		dev = __dev_get_by_index(net, ifm->ifa_index);
40846c223828SNoriaki TAKAMIYA 
4085e21e8467SStephen Hemminger 	ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4086e21e8467SStephen Hemminger 	if (!ifa) {
40871b29fc2cSThomas Graf 		err = -EADDRNOTAVAIL;
40881b29fc2cSThomas Graf 		goto errout;
40896c223828SNoriaki TAKAMIYA 	}
40906c223828SNoriaki TAKAMIYA 
4091e21e8467SStephen Hemminger 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4092e21e8467SStephen Hemminger 	if (!skb) {
40931b29fc2cSThomas Graf 		err = -ENOBUFS;
40941b29fc2cSThomas Graf 		goto errout_ifa;
40951b29fc2cSThomas Graf 	}
40961b29fc2cSThomas Graf 
409715e47304SEric W. Biederman 	err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
40986c223828SNoriaki TAKAMIYA 				nlh->nlmsg_seq, RTM_NEWADDR, 0);
409926932566SPatrick McHardy 	if (err < 0) {
410026932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
410126932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
410226932566SPatrick McHardy 		kfree_skb(skb);
410326932566SPatrick McHardy 		goto errout_ifa;
410426932566SPatrick McHardy 	}
410515e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
41061b29fc2cSThomas Graf errout_ifa:
41076c223828SNoriaki TAKAMIYA 	in6_ifa_put(ifa);
41081b29fc2cSThomas Graf errout:
41096c223828SNoriaki TAKAMIYA 	return err;
41106c223828SNoriaki TAKAMIYA }
41116c223828SNoriaki TAKAMIYA 
41121da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
41131da177e4SLinus Torvalds {
41141da177e4SLinus Torvalds 	struct sk_buff *skb;
4115c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(ifa->idev->dev);
41165d620266SThomas Graf 	int err = -ENOBUFS;
41171da177e4SLinus Torvalds 
41180ab6803bSThomas Graf 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
41195d620266SThomas Graf 	if (skb == NULL)
41205d620266SThomas Graf 		goto errout;
41215d620266SThomas Graf 
41225d620266SThomas Graf 	err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
412326932566SPatrick McHardy 	if (err < 0) {
412426932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
412526932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
412626932566SPatrick McHardy 		kfree_skb(skb);
412726932566SPatrick McHardy 		goto errout;
412826932566SPatrick McHardy 	}
41291ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
41301ce85fe4SPablo Neira Ayuso 	return;
41315d620266SThomas Graf errout:
41325d620266SThomas Graf 	if (err < 0)
41336fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
41341da177e4SLinus Torvalds }
41351da177e4SLinus Torvalds 
4136b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
41371da177e4SLinus Torvalds 				__s32 *array, int bytes)
41381da177e4SLinus Torvalds {
413904561c1fSThomas Graf 	BUG_ON(bytes < (DEVCONF_MAX * 4));
414004561c1fSThomas Graf 
41411da177e4SLinus Torvalds 	memset(array, 0, bytes);
41421da177e4SLinus Torvalds 	array[DEVCONF_FORWARDING] = cnf->forwarding;
41431da177e4SLinus Torvalds 	array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
41441da177e4SLinus Torvalds 	array[DEVCONF_MTU6] = cnf->mtu6;
41451da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
41461da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
41471da177e4SLinus Torvalds 	array[DEVCONF_AUTOCONF] = cnf->autoconf;
41481da177e4SLinus Torvalds 	array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
41491da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
415093908d19SThomas Graf 	array[DEVCONF_RTR_SOLICIT_INTERVAL] =
415193908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_solicit_interval);
415293908d19SThomas Graf 	array[DEVCONF_RTR_SOLICIT_DELAY] =
415393908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_solicit_delay);
41541da177e4SLinus Torvalds 	array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
41551da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
41561da177e4SLinus Torvalds 	array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
41571da177e4SLinus Torvalds 	array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
41581da177e4SLinus Torvalds 	array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
41591da177e4SLinus Torvalds 	array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
41601da177e4SLinus Torvalds 	array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
41611da177e4SLinus Torvalds #endif
41621da177e4SLinus Torvalds 	array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
416365f5c7c1SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
4164c4fd30ebSYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
4165930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
4166930d6ff2SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
416793908d19SThomas Graf 	array[DEVCONF_RTR_PROBE_INTERVAL] =
416893908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_probe_interval);
4169fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
417009c884d4SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
417109c884d4SYOSHIFUJI Hideaki #endif
4172930d6ff2SYOSHIFUJI Hideaki #endif
4173fbea49e1SYOSHIFUJI Hideaki 	array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
41740bcbc926SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
417595c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
417695c385b4SNeil Horman 	array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
417795c385b4SNeil Horman #endif
41787bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
41797bc570c8SYOSHIFUJI Hideaki 	array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
41807bc570c8SYOSHIFUJI Hideaki #endif
4181778d80beSYOSHIFUJI Hideaki 	array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
41821b34be74SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
4183c3faca05SCosmin Ratiu 	array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
41845cb04436SHannes Frederic Sowa 	array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
41851da177e4SLinus Torvalds }
41861da177e4SLinus Torvalds 
4187b382b191SThomas Graf static inline size_t inet6_ifla6_size(void)
4188b382b191SThomas Graf {
4189b382b191SThomas Graf 	return nla_total_size(4) /* IFLA_INET6_FLAGS */
4190b382b191SThomas Graf 	     + nla_total_size(sizeof(struct ifla_cacheinfo))
4191b382b191SThomas Graf 	     + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
4192b382b191SThomas Graf 	     + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
4193f53adae4SDaniel Borkmann 	     + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
4194f53adae4SDaniel Borkmann 	     + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
4195b382b191SThomas Graf }
4196b382b191SThomas Graf 
4197339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void)
4198339bf98fSThomas Graf {
4199339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4200339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4201339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4202339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
4203339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
4204b382b191SThomas Graf 	       + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
4205339bf98fSThomas Graf }
4206c5396a31SYOSHIFUJI Hideaki 
4207be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
42087d720c3eSTejun Heo 				      int items, int bytes)
42097f7d9a6bSHerbert Xu {
42107f7d9a6bSHerbert Xu 	int i;
42117f7d9a6bSHerbert Xu 	int pad = bytes - sizeof(u64) * items;
42127f7d9a6bSHerbert Xu 	BUG_ON(pad < 0);
42137f7d9a6bSHerbert Xu 
42147f7d9a6bSHerbert Xu 	/* Use put_unaligned() because stats may not be aligned for u64. */
42157f7d9a6bSHerbert Xu 	put_unaligned(items, &stats[0]);
42167f7d9a6bSHerbert Xu 	for (i = 1; i < items; i++)
4217be281e55SEric Dumazet 		put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
42187f7d9a6bSHerbert Xu 
42197f7d9a6bSHerbert Xu 	memset(&stats[items], 0, pad);
42207f7d9a6bSHerbert Xu }
42217f7d9a6bSHerbert Xu 
42224ce3c183SEric Dumazet static inline void __snmp6_fill_stats64(u64 *stats, void __percpu **mib,
42234ce3c183SEric Dumazet 				      int items, int bytes, size_t syncpoff)
42244ce3c183SEric Dumazet {
42254ce3c183SEric Dumazet 	int i;
42264ce3c183SEric Dumazet 	int pad = bytes - sizeof(u64) * items;
42274ce3c183SEric Dumazet 	BUG_ON(pad < 0);
42284ce3c183SEric Dumazet 
42294ce3c183SEric Dumazet 	/* Use put_unaligned() because stats may not be aligned for u64. */
42304ce3c183SEric Dumazet 	put_unaligned(items, &stats[0]);
42314ce3c183SEric Dumazet 	for (i = 1; i < items; i++)
42324ce3c183SEric Dumazet 		put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]);
42334ce3c183SEric Dumazet 
42344ce3c183SEric Dumazet 	memset(&stats[items], 0, pad);
42354ce3c183SEric Dumazet }
42364ce3c183SEric Dumazet 
42377f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
42387f7d9a6bSHerbert Xu 			     int bytes)
42397f7d9a6bSHerbert Xu {
42407f7d9a6bSHerbert Xu 	switch (attrtype) {
42417f7d9a6bSHerbert Xu 	case IFLA_INET6_STATS:
42424ce3c183SEric Dumazet 		__snmp6_fill_stats64(stats, (void __percpu **)idev->stats.ipv6,
42434ce3c183SEric Dumazet 				     IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp));
42447f7d9a6bSHerbert Xu 		break;
42457f7d9a6bSHerbert Xu 	case IFLA_INET6_ICMP6STATS:
4246be281e55SEric Dumazet 		__snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes);
42477f7d9a6bSHerbert Xu 		break;
42487f7d9a6bSHerbert Xu 	}
42497f7d9a6bSHerbert Xu }
42507f7d9a6bSHerbert Xu 
4251b382b191SThomas Graf static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev)
42521da177e4SLinus Torvalds {
4253bf99f1bdSYOSHIFUJI Hideaki 	struct nlattr *nla;
42541da177e4SLinus Torvalds 	struct ifla_cacheinfo ci;
42551da177e4SLinus Torvalds 
4256c78679e8SDavid S. Miller 	if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
4257c78679e8SDavid S. Miller 		goto nla_put_failure;
42581da177e4SLinus Torvalds 	ci.max_reasm_len = IPV6_MAXPLEN;
425924912420SDavid S. Miller 	ci.tstamp = cstamp_delta(idev->tstamp);
426024912420SDavid S. Miller 	ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
426124912420SDavid S. Miller 	ci.retrans_time = jiffies_to_msecs(idev->nd_parms->retrans_time);
4262c78679e8SDavid S. Miller 	if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
4263c78679e8SDavid S. Miller 		goto nla_put_failure;
4264bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
4265bf99f1bdSYOSHIFUJI Hideaki 	if (nla == NULL)
426604561c1fSThomas Graf 		goto nla_put_failure;
4267bf99f1bdSYOSHIFUJI Hideaki 	ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
42681da177e4SLinus Torvalds 
4269bf99f1bdSYOSHIFUJI Hideaki 	/* XXX - MC not implemented */
4270bf99f1bdSYOSHIFUJI Hideaki 
4271bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
4272bf99f1bdSYOSHIFUJI Hideaki 	if (nla == NULL)
4273bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
4274bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
4275bf99f1bdSYOSHIFUJI Hideaki 
4276bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
4277bf99f1bdSYOSHIFUJI Hideaki 	if (nla == NULL)
4278bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
4279bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
42801da177e4SLinus Torvalds 
4281f53adae4SDaniel Borkmann 	nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
4282f53adae4SDaniel Borkmann 	if (nla == NULL)
4283f53adae4SDaniel Borkmann 		goto nla_put_failure;
4284f53adae4SDaniel Borkmann 	read_lock_bh(&idev->lock);
4285f53adae4SDaniel Borkmann 	memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
4286f53adae4SDaniel Borkmann 	read_unlock_bh(&idev->lock);
4287f53adae4SDaniel Borkmann 
4288b382b191SThomas Graf 	return 0;
4289b382b191SThomas Graf 
4290b382b191SThomas Graf nla_put_failure:
4291b382b191SThomas Graf 	return -EMSGSIZE;
4292b382b191SThomas Graf }
4293b382b191SThomas Graf 
4294b382b191SThomas Graf static size_t inet6_get_link_af_size(const struct net_device *dev)
4295b382b191SThomas Graf {
4296b382b191SThomas Graf 	if (!__in6_dev_get(dev))
4297b382b191SThomas Graf 		return 0;
4298b382b191SThomas Graf 
4299b382b191SThomas Graf 	return inet6_ifla6_size();
4300b382b191SThomas Graf }
4301b382b191SThomas Graf 
4302b382b191SThomas Graf static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
4303b382b191SThomas Graf {
4304b382b191SThomas Graf 	struct inet6_dev *idev = __in6_dev_get(dev);
4305b382b191SThomas Graf 
4306b382b191SThomas Graf 	if (!idev)
4307b382b191SThomas Graf 		return -ENODATA;
4308b382b191SThomas Graf 
4309b382b191SThomas Graf 	if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4310b382b191SThomas Graf 		return -EMSGSIZE;
4311b382b191SThomas Graf 
4312b382b191SThomas Graf 	return 0;
4313b382b191SThomas Graf }
4314b382b191SThomas Graf 
4315f53adae4SDaniel Borkmann static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
4316f53adae4SDaniel Borkmann {
4317f53adae4SDaniel Borkmann 	struct inet6_ifaddr *ifp;
4318f53adae4SDaniel Borkmann 	struct net_device *dev = idev->dev;
4319fc403832SDaniel Borkmann 	bool update_rs = false;
4320f53adae4SDaniel Borkmann 
4321f53adae4SDaniel Borkmann 	if (token == NULL)
4322f53adae4SDaniel Borkmann 		return -EINVAL;
4323f53adae4SDaniel Borkmann 	if (ipv6_addr_any(token))
4324f53adae4SDaniel Borkmann 		return -EINVAL;
4325f53adae4SDaniel Borkmann 	if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
4326f53adae4SDaniel Borkmann 		return -EINVAL;
4327f53adae4SDaniel Borkmann 	if (!ipv6_accept_ra(idev))
4328f53adae4SDaniel Borkmann 		return -EINVAL;
4329f53adae4SDaniel Borkmann 	if (idev->cnf.rtr_solicits <= 0)
4330f53adae4SDaniel Borkmann 		return -EINVAL;
4331f53adae4SDaniel Borkmann 
4332f53adae4SDaniel Borkmann 	write_lock_bh(&idev->lock);
4333f53adae4SDaniel Borkmann 
4334f53adae4SDaniel Borkmann 	BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
4335f53adae4SDaniel Borkmann 	memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
4336f53adae4SDaniel Borkmann 
4337f53adae4SDaniel Borkmann 	write_unlock_bh(&idev->lock);
4338f53adae4SDaniel Borkmann 
4339fc403832SDaniel Borkmann 	if (!idev->dead && (idev->if_flags & IF_READY)) {
4340fc403832SDaniel Borkmann 		struct in6_addr ll_addr;
4341fc403832SDaniel Borkmann 
4342fc403832SDaniel Borkmann 		ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
4343fc403832SDaniel Borkmann 				IFA_F_OPTIMISTIC);
4344fc403832SDaniel Borkmann 
4345fc403832SDaniel Borkmann 		/* If we're not ready, then normal ifup will take care
4346fc403832SDaniel Borkmann 		 * of this. Otherwise, we need to request our rs here.
4347fc403832SDaniel Borkmann 		 */
4348f53adae4SDaniel Borkmann 		ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
4349fc403832SDaniel Borkmann 		update_rs = true;
4350fc403832SDaniel Borkmann 	}
4351f53adae4SDaniel Borkmann 
4352f53adae4SDaniel Borkmann 	write_lock_bh(&idev->lock);
4353fc403832SDaniel Borkmann 
4354fc403832SDaniel Borkmann 	if (update_rs)
4355f53adae4SDaniel Borkmann 		idev->if_flags |= IF_RS_SENT;
4356f53adae4SDaniel Borkmann 
4357f53adae4SDaniel Borkmann 	/* Well, that's kinda nasty ... */
4358f53adae4SDaniel Borkmann 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
4359f53adae4SDaniel Borkmann 		spin_lock(&ifp->lock);
4360617fe29dSDaniel Borkmann 		if (ifp->tokenized) {
4361f53adae4SDaniel Borkmann 			ifp->valid_lft = 0;
4362f53adae4SDaniel Borkmann 			ifp->prefered_lft = 0;
4363f53adae4SDaniel Borkmann 		}
4364f53adae4SDaniel Borkmann 		spin_unlock(&ifp->lock);
4365f53adae4SDaniel Borkmann 	}
4366f53adae4SDaniel Borkmann 
4367f53adae4SDaniel Borkmann 	write_unlock_bh(&idev->lock);
4368f53adae4SDaniel Borkmann 	return 0;
4369f53adae4SDaniel Borkmann }
4370f53adae4SDaniel Borkmann 
4371f53adae4SDaniel Borkmann static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
4372f53adae4SDaniel Borkmann {
4373f53adae4SDaniel Borkmann 	int err = -EINVAL;
4374f53adae4SDaniel Borkmann 	struct inet6_dev *idev = __in6_dev_get(dev);
4375f53adae4SDaniel Borkmann 	struct nlattr *tb[IFLA_INET6_MAX + 1];
4376f53adae4SDaniel Borkmann 
4377f53adae4SDaniel Borkmann 	if (!idev)
4378f53adae4SDaniel Borkmann 		return -EAFNOSUPPORT;
4379f53adae4SDaniel Borkmann 
4380f53adae4SDaniel Borkmann 	if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
4381f53adae4SDaniel Borkmann 		BUG();
4382f53adae4SDaniel Borkmann 
4383f53adae4SDaniel Borkmann 	if (tb[IFLA_INET6_TOKEN])
4384f53adae4SDaniel Borkmann 		err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
4385f53adae4SDaniel Borkmann 
4386f53adae4SDaniel Borkmann 	return err;
4387f53adae4SDaniel Borkmann }
4388f53adae4SDaniel Borkmann 
4389b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
439015e47304SEric W. Biederman 			     u32 portid, u32 seq, int event, unsigned int flags)
4391b382b191SThomas Graf {
4392b382b191SThomas Graf 	struct net_device *dev = idev->dev;
4393b382b191SThomas Graf 	struct ifinfomsg *hdr;
4394b382b191SThomas Graf 	struct nlmsghdr *nlh;
4395b382b191SThomas Graf 	void *protoinfo;
4396b382b191SThomas Graf 
439715e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
4398b382b191SThomas Graf 	if (nlh == NULL)
4399b382b191SThomas Graf 		return -EMSGSIZE;
4400b382b191SThomas Graf 
4401b382b191SThomas Graf 	hdr = nlmsg_data(nlh);
4402b382b191SThomas Graf 	hdr->ifi_family = AF_INET6;
4403b382b191SThomas Graf 	hdr->__ifi_pad = 0;
4404b382b191SThomas Graf 	hdr->ifi_type = dev->type;
4405b382b191SThomas Graf 	hdr->ifi_index = dev->ifindex;
4406b382b191SThomas Graf 	hdr->ifi_flags = dev_get_flags(dev);
4407b382b191SThomas Graf 	hdr->ifi_change = 0;
4408b382b191SThomas Graf 
4409c78679e8SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4410c78679e8SDavid S. Miller 	    (dev->addr_len &&
4411c78679e8SDavid S. Miller 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4412c78679e8SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4413c78679e8SDavid S. Miller 	    (dev->ifindex != dev->iflink &&
4414c78679e8SDavid S. Miller 	     nla_put_u32(skb, IFLA_LINK, dev->iflink)))
4415c78679e8SDavid S. Miller 		goto nla_put_failure;
4416b382b191SThomas Graf 	protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
4417b382b191SThomas Graf 	if (protoinfo == NULL)
4418b382b191SThomas Graf 		goto nla_put_failure;
4419b382b191SThomas Graf 
4420b382b191SThomas Graf 	if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4421b382b191SThomas Graf 		goto nla_put_failure;
4422b382b191SThomas Graf 
442304561c1fSThomas Graf 	nla_nest_end(skb, protoinfo);
442404561c1fSThomas Graf 	return nlmsg_end(skb, nlh);
44251da177e4SLinus Torvalds 
442604561c1fSThomas Graf nla_put_failure:
442726932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
442826932566SPatrick McHardy 	return -EMSGSIZE;
44291da177e4SLinus Torvalds }
44301da177e4SLinus Torvalds 
44311da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
44321da177e4SLinus Torvalds {
44333b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
443484d2697dSEric Dumazet 	int h, s_h;
4435434a8a58SDavid S. Miller 	int idx = 0, s_idx;
44361da177e4SLinus Torvalds 	struct net_device *dev;
44371da177e4SLinus Torvalds 	struct inet6_dev *idev;
443884d2697dSEric Dumazet 	struct hlist_head *head;
44391da177e4SLinus Torvalds 
444084d2697dSEric Dumazet 	s_h = cb->args[0];
444184d2697dSEric Dumazet 	s_idx = cb->args[1];
444284d2697dSEric Dumazet 
444384d2697dSEric Dumazet 	rcu_read_lock();
444484d2697dSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
44457562f876SPavel Emelianov 		idx = 0;
444684d2697dSEric Dumazet 		head = &net->dev_index_head[h];
4447b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
44481da177e4SLinus Torvalds 			if (idx < s_idx)
44497562f876SPavel Emelianov 				goto cont;
445084d2697dSEric Dumazet 			idev = __in6_dev_get(dev);
445184d2697dSEric Dumazet 			if (!idev)
44527562f876SPavel Emelianov 				goto cont;
445384d2697dSEric Dumazet 			if (inet6_fill_ifinfo(skb, idev,
445415e47304SEric W. Biederman 					      NETLINK_CB(cb->skb).portid,
445584d2697dSEric Dumazet 					      cb->nlh->nlmsg_seq,
445684d2697dSEric Dumazet 					      RTM_NEWLINK, NLM_F_MULTI) <= 0)
445784d2697dSEric Dumazet 				goto out;
44587562f876SPavel Emelianov cont:
44597562f876SPavel Emelianov 			idx++;
44601da177e4SLinus Torvalds 		}
446184d2697dSEric Dumazet 	}
446284d2697dSEric Dumazet out:
446384d2697dSEric Dumazet 	rcu_read_unlock();
446484d2697dSEric Dumazet 	cb->args[1] = idx;
446584d2697dSEric Dumazet 	cb->args[0] = h;
44661da177e4SLinus Torvalds 
44671da177e4SLinus Torvalds 	return skb->len;
44681da177e4SLinus Torvalds }
44691da177e4SLinus Torvalds 
44701da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
44711da177e4SLinus Torvalds {
44721da177e4SLinus Torvalds 	struct sk_buff *skb;
4473c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
44748d7a76c9SThomas Graf 	int err = -ENOBUFS;
44751da177e4SLinus Torvalds 
4476339bf98fSThomas Graf 	skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
44778d7a76c9SThomas Graf 	if (skb == NULL)
44788d7a76c9SThomas Graf 		goto errout;
44798d7a76c9SThomas Graf 
44808d7a76c9SThomas Graf 	err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
448126932566SPatrick McHardy 	if (err < 0) {
448226932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
448326932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
448426932566SPatrick McHardy 		kfree_skb(skb);
448526932566SPatrick McHardy 		goto errout;
448626932566SPatrick McHardy 	}
44875f75a104SNicolas Dichtel 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
44881ce85fe4SPablo Neira Ayuso 	return;
44898d7a76c9SThomas Graf errout:
44908d7a76c9SThomas Graf 	if (err < 0)
44915f75a104SNicolas Dichtel 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
44921da177e4SLinus Torvalds }
44931da177e4SLinus Torvalds 
4494339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void)
4495339bf98fSThomas Graf {
4496339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct prefixmsg))
4497339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct in6_addr))
4498339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct prefix_cacheinfo));
4499339bf98fSThomas Graf }
4500c5396a31SYOSHIFUJI Hideaki 
45011da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
450215e47304SEric W. Biederman 			     struct prefix_info *pinfo, u32 portid, u32 seq,
4503b6544c0bSJamal Hadi Salim 			     int event, unsigned int flags)
45041da177e4SLinus Torvalds {
45051da177e4SLinus Torvalds 	struct prefixmsg *pmsg;
45061da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
45071da177e4SLinus Torvalds 	struct prefix_cacheinfo	ci;
45081da177e4SLinus Torvalds 
450915e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
45106051e2f4SThomas Graf 	if (nlh == NULL)
451126932566SPatrick McHardy 		return -EMSGSIZE;
45126051e2f4SThomas Graf 
45136051e2f4SThomas Graf 	pmsg = nlmsg_data(nlh);
45141da177e4SLinus Torvalds 	pmsg->prefix_family = AF_INET6;
45158a47077aSPatrick McHardy 	pmsg->prefix_pad1 = 0;
45168a47077aSPatrick McHardy 	pmsg->prefix_pad2 = 0;
45171da177e4SLinus Torvalds 	pmsg->prefix_ifindex = idev->dev->ifindex;
45181da177e4SLinus Torvalds 	pmsg->prefix_len = pinfo->prefix_len;
45191da177e4SLinus Torvalds 	pmsg->prefix_type = pinfo->type;
45208a47077aSPatrick McHardy 	pmsg->prefix_pad3 = 0;
45211da177e4SLinus Torvalds 	pmsg->prefix_flags = 0;
45221da177e4SLinus Torvalds 	if (pinfo->onlink)
45231da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_ONLINK;
45241da177e4SLinus Torvalds 	if (pinfo->autoconf)
45251da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
45261da177e4SLinus Torvalds 
4527c78679e8SDavid S. Miller 	if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
4528c78679e8SDavid S. Miller 		goto nla_put_failure;
45291da177e4SLinus Torvalds 	ci.preferred_time = ntohl(pinfo->prefered);
45301da177e4SLinus Torvalds 	ci.valid_time = ntohl(pinfo->valid);
4531c78679e8SDavid S. Miller 	if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
4532c78679e8SDavid S. Miller 		goto nla_put_failure;
45336051e2f4SThomas Graf 	return nlmsg_end(skb, nlh);
45341da177e4SLinus Torvalds 
45356051e2f4SThomas Graf nla_put_failure:
453626932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
453726932566SPatrick McHardy 	return -EMSGSIZE;
45381da177e4SLinus Torvalds }
45391da177e4SLinus Torvalds 
45401da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
45411da177e4SLinus Torvalds 			 struct prefix_info *pinfo)
45421da177e4SLinus Torvalds {
45431da177e4SLinus Torvalds 	struct sk_buff *skb;
4544c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
45458c384bfaSThomas Graf 	int err = -ENOBUFS;
45461da177e4SLinus Torvalds 
4547339bf98fSThomas Graf 	skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
45488c384bfaSThomas Graf 	if (skb == NULL)
45498c384bfaSThomas Graf 		goto errout;
45508c384bfaSThomas Graf 
45518c384bfaSThomas Graf 	err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
455226932566SPatrick McHardy 	if (err < 0) {
455326932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
455426932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
455526932566SPatrick McHardy 		kfree_skb(skb);
455626932566SPatrick McHardy 		goto errout;
455726932566SPatrick McHardy 	}
45581ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
45591ce85fe4SPablo Neira Ayuso 	return;
45608c384bfaSThomas Graf errout:
45618c384bfaSThomas Graf 	if (err < 0)
45626fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
45631da177e4SLinus Torvalds }
45641da177e4SLinus Torvalds 
45651da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
45661da177e4SLinus Torvalds {
456763998ac2SNicolas Dichtel 	struct net *net = dev_net(ifp->idev->dev);
456863998ac2SNicolas Dichtel 
45691da177e4SLinus Torvalds 	inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
45701da177e4SLinus Torvalds 
45711da177e4SLinus Torvalds 	switch (event) {
45721da177e4SLinus Torvalds 	case RTM_NEWADDR:
457395c385b4SNeil Horman 		/*
457495c385b4SNeil Horman 		 * If the address was optimistic
457595c385b4SNeil Horman 		 * we inserted the route at the start of
457695c385b4SNeil Horman 		 * our DAD process, so we don't need
457795c385b4SNeil Horman 		 * to do it again
457895c385b4SNeil Horman 		 */
457995c385b4SNeil Horman 		if (!(ifp->rt->rt6i_node))
458040e22e8fSThomas Graf 			ip6_ins_rt(ifp->rt);
45811da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
45821da177e4SLinus Torvalds 			addrconf_join_anycast(ifp);
45837996c799SCong Wang 		if (!ipv6_addr_any(&ifp->peer_addr))
4584caeaba79SNicolas Dichtel 			addrconf_prefix_route(&ifp->peer_addr, 128,
4585caeaba79SNicolas Dichtel 					      ifp->idev->dev, 0, 0);
45861da177e4SLinus Torvalds 		break;
45871da177e4SLinus Torvalds 	case RTM_DELADDR:
45881da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
45891da177e4SLinus Torvalds 			addrconf_leave_anycast(ifp);
45901da177e4SLinus Torvalds 		addrconf_leave_solict(ifp->idev, &ifp->addr);
45917996c799SCong Wang 		if (!ipv6_addr_any(&ifp->peer_addr)) {
4592caeaba79SNicolas Dichtel 			struct rt6_info *rt;
4593caeaba79SNicolas Dichtel 			struct net_device *dev = ifp->idev->dev;
4594caeaba79SNicolas Dichtel 
4595caeaba79SNicolas Dichtel 			rt = rt6_lookup(dev_net(dev), &ifp->peer_addr, NULL,
4596caeaba79SNicolas Dichtel 					dev->ifindex, 1);
4597caeaba79SNicolas Dichtel 			if (rt) {
4598caeaba79SNicolas Dichtel 				dst_hold(&rt->dst);
4599caeaba79SNicolas Dichtel 				if (ip6_del_rt(rt))
4600caeaba79SNicolas Dichtel 					dst_free(&rt->dst);
4601caeaba79SNicolas Dichtel 			}
4602caeaba79SNicolas Dichtel 		}
4603d8d1f30bSChangli Gao 		dst_hold(&ifp->rt->dst);
460493fa159aSstephen hemminger 
460573a8bd74SDavid S. Miller 		if (ip6_del_rt(ifp->rt))
4606d8d1f30bSChangli Gao 			dst_free(&ifp->rt->dst);
46071da177e4SLinus Torvalds 		break;
46081da177e4SLinus Torvalds 	}
460963998ac2SNicolas Dichtel 	atomic_inc(&net->ipv6.dev_addr_genid);
46101da177e4SLinus Torvalds }
46111da177e4SLinus Torvalds 
46121da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
46131da177e4SLinus Torvalds {
46148814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
46151da177e4SLinus Torvalds 	if (likely(ifp->idev->dead == 0))
46161da177e4SLinus Torvalds 		__ipv6_ifa_notify(event, ifp);
46178814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
46181da177e4SLinus Torvalds }
46191da177e4SLinus Torvalds 
46201da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
46211da177e4SLinus Torvalds 
46221da177e4SLinus Torvalds static
4623*fe2c6338SJoe Perches int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
46241da177e4SLinus Torvalds 			   void __user *buffer, size_t *lenp, loff_t *ppos)
46251da177e4SLinus Torvalds {
46261da177e4SLinus Torvalds 	int *valp = ctl->data;
46271da177e4SLinus Torvalds 	int val = *valp;
462888af182eSEric W. Biederman 	loff_t pos = *ppos;
4629*fe2c6338SJoe Perches 	struct ctl_table lctl;
46301da177e4SLinus Torvalds 	int ret;
46311da177e4SLinus Torvalds 
4632013d97e9SFrancesco Ruggeri 	/*
4633013d97e9SFrancesco Ruggeri 	 * ctl->data points to idev->cnf.forwarding, we should
4634013d97e9SFrancesco Ruggeri 	 * not modify it until we get the rtnl lock.
4635013d97e9SFrancesco Ruggeri 	 */
4636013d97e9SFrancesco Ruggeri 	lctl = *ctl;
4637013d97e9SFrancesco Ruggeri 	lctl.data = &val;
4638013d97e9SFrancesco Ruggeri 
4639013d97e9SFrancesco Ruggeri 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
46401da177e4SLinus Torvalds 
4641c8fecf22SPavel Emelyanov 	if (write)
4642b325fddbSStephen Hemminger 		ret = addrconf_fixup_forwarding(ctl, valp, val);
464388af182eSEric W. Biederman 	if (ret)
464488af182eSEric W. Biederman 		*ppos = pos;
46451da177e4SLinus Torvalds 	return ret;
46461da177e4SLinus Torvalds }
46471da177e4SLinus Torvalds 
464856d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev)
464956d417b1SBrian Haley {
465075538c2bSCong Wang 	struct netdev_notifier_info info;
465175538c2bSCong Wang 
465256d417b1SBrian Haley 	if (!idev || !idev->dev)
465356d417b1SBrian Haley 		return;
465456d417b1SBrian Haley 
465575538c2bSCong Wang 	netdev_notifier_info_init(&info, idev->dev);
465656d417b1SBrian Haley 	if (idev->cnf.disable_ipv6)
465775538c2bSCong Wang 		addrconf_notify(NULL, NETDEV_DOWN, &info);
465856d417b1SBrian Haley 	else
465975538c2bSCong Wang 		addrconf_notify(NULL, NETDEV_UP, &info);
466056d417b1SBrian Haley }
466156d417b1SBrian Haley 
466256d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf)
466356d417b1SBrian Haley {
466456d417b1SBrian Haley 	struct net_device *dev;
466556d417b1SBrian Haley 	struct inet6_dev *idev;
466656d417b1SBrian Haley 
466756d417b1SBrian Haley 	rcu_read_lock();
4668c6d14c84SEric Dumazet 	for_each_netdev_rcu(net, dev) {
466956d417b1SBrian Haley 		idev = __in6_dev_get(dev);
467056d417b1SBrian Haley 		if (idev) {
467156d417b1SBrian Haley 			int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
467256d417b1SBrian Haley 			idev->cnf.disable_ipv6 = newf;
467356d417b1SBrian Haley 			if (changed)
467456d417b1SBrian Haley 				dev_disable_change(idev);
467556d417b1SBrian Haley 		}
467656d417b1SBrian Haley 	}
4677c6d14c84SEric Dumazet 	rcu_read_unlock();
467856d417b1SBrian Haley }
467956d417b1SBrian Haley 
4680013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
468156d417b1SBrian Haley {
468256d417b1SBrian Haley 	struct net *net;
4683013d97e9SFrancesco Ruggeri 	int old;
4684013d97e9SFrancesco Ruggeri 
4685013d97e9SFrancesco Ruggeri 	if (!rtnl_trylock())
4686013d97e9SFrancesco Ruggeri 		return restart_syscall();
468756d417b1SBrian Haley 
468856d417b1SBrian Haley 	net = (struct net *)table->extra2;
4689013d97e9SFrancesco Ruggeri 	old = *p;
4690013d97e9SFrancesco Ruggeri 	*p = newf;
469156d417b1SBrian Haley 
4692013d97e9SFrancesco Ruggeri 	if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
4693013d97e9SFrancesco Ruggeri 		rtnl_unlock();
469456d417b1SBrian Haley 		return 0;
469588af182eSEric W. Biederman 	}
469656d417b1SBrian Haley 
469756d417b1SBrian Haley 	if (p == &net->ipv6.devconf_all->disable_ipv6) {
469856d417b1SBrian Haley 		net->ipv6.devconf_dflt->disable_ipv6 = newf;
469956d417b1SBrian Haley 		addrconf_disable_change(net, newf);
4700013d97e9SFrancesco Ruggeri 	} else if ((!newf) ^ (!old))
470156d417b1SBrian Haley 		dev_disable_change((struct inet6_dev *)table->extra1);
470256d417b1SBrian Haley 
470356d417b1SBrian Haley 	rtnl_unlock();
470456d417b1SBrian Haley 	return 0;
470556d417b1SBrian Haley }
470656d417b1SBrian Haley 
470756d417b1SBrian Haley static
4708*fe2c6338SJoe Perches int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
470956d417b1SBrian Haley 			    void __user *buffer, size_t *lenp, loff_t *ppos)
471056d417b1SBrian Haley {
471156d417b1SBrian Haley 	int *valp = ctl->data;
471256d417b1SBrian Haley 	int val = *valp;
471388af182eSEric W. Biederman 	loff_t pos = *ppos;
4714*fe2c6338SJoe Perches 	struct ctl_table lctl;
471556d417b1SBrian Haley 	int ret;
471656d417b1SBrian Haley 
4717013d97e9SFrancesco Ruggeri 	/*
4718013d97e9SFrancesco Ruggeri 	 * ctl->data points to idev->cnf.disable_ipv6, we should
4719013d97e9SFrancesco Ruggeri 	 * not modify it until we get the rtnl lock.
4720013d97e9SFrancesco Ruggeri 	 */
4721013d97e9SFrancesco Ruggeri 	lctl = *ctl;
4722013d97e9SFrancesco Ruggeri 	lctl.data = &val;
4723013d97e9SFrancesco Ruggeri 
4724013d97e9SFrancesco Ruggeri 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
472556d417b1SBrian Haley 
472656d417b1SBrian Haley 	if (write)
472756d417b1SBrian Haley 		ret = addrconf_disable_ipv6(ctl, valp, val);
472888af182eSEric W. Biederman 	if (ret)
472988af182eSEric W. Biederman 		*ppos = pos;
473056d417b1SBrian Haley 	return ret;
473156d417b1SBrian Haley }
473256d417b1SBrian Haley 
47331da177e4SLinus Torvalds static struct addrconf_sysctl_table
47341da177e4SLinus Torvalds {
47351da177e4SLinus Torvalds 	struct ctl_table_header *sysctl_header;
4736*fe2c6338SJoe Perches 	struct ctl_table addrconf_vars[DEVCONF_MAX+1];
4737ab32ea5dSBrian Haley } addrconf_sysctl __read_mostly = {
47381da177e4SLinus Torvalds 	.sysctl_header = NULL,
47391da177e4SLinus Torvalds 	.addrconf_vars = {
47401da177e4SLinus Torvalds 		{
47411da177e4SLinus Torvalds 			.procname	= "forwarding",
47421da177e4SLinus Torvalds 			.data		= &ipv6_devconf.forwarding,
47431da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
47441da177e4SLinus Torvalds 			.mode		= 0644,
47456d9f239aSAlexey Dobriyan 			.proc_handler	= addrconf_sysctl_forward,
47461da177e4SLinus Torvalds 		},
47471da177e4SLinus Torvalds 		{
47481da177e4SLinus Torvalds 			.procname	= "hop_limit",
47491da177e4SLinus Torvalds 			.data		= &ipv6_devconf.hop_limit,
47501da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
47511da177e4SLinus Torvalds 			.mode		= 0644,
47521da177e4SLinus Torvalds 			.proc_handler	= proc_dointvec,
47531da177e4SLinus Torvalds 		},
47541da177e4SLinus Torvalds 		{
47551da177e4SLinus Torvalds 			.procname	= "mtu",
47561da177e4SLinus Torvalds 			.data		= &ipv6_devconf.mtu6,
47571da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
47581da177e4SLinus Torvalds 			.mode		= 0644,
47596d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
47601da177e4SLinus Torvalds 		},
47611da177e4SLinus Torvalds 		{
47621da177e4SLinus Torvalds 			.procname	= "accept_ra",
47631da177e4SLinus Torvalds 			.data		= &ipv6_devconf.accept_ra,
47641da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
47651da177e4SLinus Torvalds 			.mode		= 0644,
47666d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
47671da177e4SLinus Torvalds 		},
47681da177e4SLinus Torvalds 		{
47691da177e4SLinus Torvalds 			.procname	= "accept_redirects",
47701da177e4SLinus Torvalds 			.data		= &ipv6_devconf.accept_redirects,
47711da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
47721da177e4SLinus Torvalds 			.mode		= 0644,
47736d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
47741da177e4SLinus Torvalds 		},
47751da177e4SLinus Torvalds 		{
47761da177e4SLinus Torvalds 			.procname	= "autoconf",
47771da177e4SLinus Torvalds 			.data		= &ipv6_devconf.autoconf,
47781da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
47791da177e4SLinus Torvalds 			.mode		= 0644,
47806d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
47811da177e4SLinus Torvalds 		},
47821da177e4SLinus Torvalds 		{
47831da177e4SLinus Torvalds 			.procname	= "dad_transmits",
47841da177e4SLinus Torvalds 			.data		= &ipv6_devconf.dad_transmits,
47851da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
47861da177e4SLinus Torvalds 			.mode		= 0644,
47876d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
47881da177e4SLinus Torvalds 		},
47891da177e4SLinus Torvalds 		{
47901da177e4SLinus Torvalds 			.procname	= "router_solicitations",
47911da177e4SLinus Torvalds 			.data		= &ipv6_devconf.rtr_solicits,
47921da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
47931da177e4SLinus Torvalds 			.mode		= 0644,
47946d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
47951da177e4SLinus Torvalds 		},
47961da177e4SLinus Torvalds 		{
47971da177e4SLinus Torvalds 			.procname	= "router_solicitation_interval",
47981da177e4SLinus Torvalds 			.data		= &ipv6_devconf.rtr_solicit_interval,
47991da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
48001da177e4SLinus Torvalds 			.mode		= 0644,
48016d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
48021da177e4SLinus Torvalds 		},
48031da177e4SLinus Torvalds 		{
48041da177e4SLinus Torvalds 			.procname	= "router_solicitation_delay",
48051da177e4SLinus Torvalds 			.data		= &ipv6_devconf.rtr_solicit_delay,
48061da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
48071da177e4SLinus Torvalds 			.mode		= 0644,
48086d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
48091da177e4SLinus Torvalds 		},
48101da177e4SLinus Torvalds 		{
48111da177e4SLinus Torvalds 			.procname	= "force_mld_version",
48121da177e4SLinus Torvalds 			.data		= &ipv6_devconf.force_mld_version,
48131da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
48141da177e4SLinus Torvalds 			.mode		= 0644,
48156d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
48161da177e4SLinus Torvalds 		},
48171da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
48181da177e4SLinus Torvalds 		{
48191da177e4SLinus Torvalds 			.procname	= "use_tempaddr",
48201da177e4SLinus Torvalds 			.data		= &ipv6_devconf.use_tempaddr,
48211da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
48221da177e4SLinus Torvalds 			.mode		= 0644,
48236d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
48241da177e4SLinus Torvalds 		},
48251da177e4SLinus Torvalds 		{
48261da177e4SLinus Torvalds 			.procname	= "temp_valid_lft",
48271da177e4SLinus Torvalds 			.data		= &ipv6_devconf.temp_valid_lft,
48281da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
48291da177e4SLinus Torvalds 			.mode		= 0644,
48306d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
48311da177e4SLinus Torvalds 		},
48321da177e4SLinus Torvalds 		{
48331da177e4SLinus Torvalds 			.procname	= "temp_prefered_lft",
48341da177e4SLinus Torvalds 			.data		= &ipv6_devconf.temp_prefered_lft,
48351da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
48361da177e4SLinus Torvalds 			.mode		= 0644,
48376d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
48381da177e4SLinus Torvalds 		},
48391da177e4SLinus Torvalds 		{
48401da177e4SLinus Torvalds 			.procname	= "regen_max_retry",
48411da177e4SLinus Torvalds 			.data		= &ipv6_devconf.regen_max_retry,
48421da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
48431da177e4SLinus Torvalds 			.mode		= 0644,
48446d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
48451da177e4SLinus Torvalds 		},
48461da177e4SLinus Torvalds 		{
48471da177e4SLinus Torvalds 			.procname	= "max_desync_factor",
48481da177e4SLinus Torvalds 			.data		= &ipv6_devconf.max_desync_factor,
48491da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
48501da177e4SLinus Torvalds 			.mode		= 0644,
48516d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
48521da177e4SLinus Torvalds 		},
48531da177e4SLinus Torvalds #endif
48541da177e4SLinus Torvalds 		{
48551da177e4SLinus Torvalds 			.procname	= "max_addresses",
48561da177e4SLinus Torvalds 			.data		= &ipv6_devconf.max_addresses,
48571da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
48581da177e4SLinus Torvalds 			.mode		= 0644,
48596d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
48601da177e4SLinus Torvalds 		},
48611da177e4SLinus Torvalds 		{
486265f5c7c1SYOSHIFUJI Hideaki 			.procname	= "accept_ra_defrtr",
486365f5c7c1SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_ra_defrtr,
486465f5c7c1SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
486565f5c7c1SYOSHIFUJI Hideaki 			.mode		= 0644,
48666d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
486765f5c7c1SYOSHIFUJI Hideaki 		},
486865f5c7c1SYOSHIFUJI Hideaki 		{
4869c4fd30ebSYOSHIFUJI Hideaki 			.procname	= "accept_ra_pinfo",
4870c4fd30ebSYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_ra_pinfo,
4871c4fd30ebSYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
4872c4fd30ebSYOSHIFUJI Hideaki 			.mode		= 0644,
48736d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
4874c4fd30ebSYOSHIFUJI Hideaki 		},
4875930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
4876930d6ff2SYOSHIFUJI Hideaki 		{
4877930d6ff2SYOSHIFUJI Hideaki 			.procname	= "accept_ra_rtr_pref",
4878930d6ff2SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_ra_rtr_pref,
4879930d6ff2SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
4880930d6ff2SYOSHIFUJI Hideaki 			.mode		= 0644,
48816d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
4882930d6ff2SYOSHIFUJI Hideaki 		},
488352e16356SYOSHIFUJI Hideaki 		{
488452e16356SYOSHIFUJI Hideaki 			.procname	= "router_probe_interval",
488552e16356SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.rtr_probe_interval,
488652e16356SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
488752e16356SYOSHIFUJI Hideaki 			.mode		= 0644,
48886d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
488952e16356SYOSHIFUJI Hideaki 		},
4890fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
489109c884d4SYOSHIFUJI Hideaki 		{
489209c884d4SYOSHIFUJI Hideaki 			.procname	= "accept_ra_rt_info_max_plen",
489309c884d4SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_ra_rt_info_max_plen,
489409c884d4SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
489509c884d4SYOSHIFUJI Hideaki 			.mode		= 0644,
48966d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
489709c884d4SYOSHIFUJI Hideaki 		},
489809c884d4SYOSHIFUJI Hideaki #endif
4899930d6ff2SYOSHIFUJI Hideaki #endif
4900c4fd30ebSYOSHIFUJI Hideaki 		{
4901fbea49e1SYOSHIFUJI Hideaki 			.procname	= "proxy_ndp",
4902fbea49e1SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.proxy_ndp,
4903fbea49e1SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
4904fbea49e1SYOSHIFUJI Hideaki 			.mode		= 0644,
49056d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
4906fbea49e1SYOSHIFUJI Hideaki 		},
4907fbea49e1SYOSHIFUJI Hideaki 		{
49080bcbc926SYOSHIFUJI Hideaki 			.procname	= "accept_source_route",
49090bcbc926SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_source_route,
49100bcbc926SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
49110bcbc926SYOSHIFUJI Hideaki 			.mode		= 0644,
49126d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
49130bcbc926SYOSHIFUJI Hideaki 		},
491495c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
491595c385b4SNeil Horman 		{
491695c385b4SNeil Horman 			.procname       = "optimistic_dad",
491795c385b4SNeil Horman 			.data           = &ipv6_devconf.optimistic_dad,
491895c385b4SNeil Horman 			.maxlen         = sizeof(int),
491995c385b4SNeil Horman 			.mode           = 0644,
49206d9f239aSAlexey Dobriyan 			.proc_handler   = proc_dointvec,
492195c385b4SNeil Horman 
492295c385b4SNeil Horman 		},
492395c385b4SNeil Horman #endif
49247bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
49257bc570c8SYOSHIFUJI Hideaki 		{
49267bc570c8SYOSHIFUJI Hideaki 			.procname	= "mc_forwarding",
49277bc570c8SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.mc_forwarding,
49287bc570c8SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
4929a4e6db07SDavid S. Miller 			.mode		= 0444,
49306d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
49317bc570c8SYOSHIFUJI Hideaki 		},
49327bc570c8SYOSHIFUJI Hideaki #endif
49330bcbc926SYOSHIFUJI Hideaki 		{
4934778d80beSYOSHIFUJI Hideaki 			.procname	= "disable_ipv6",
4935778d80beSYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.disable_ipv6,
4936778d80beSYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
4937778d80beSYOSHIFUJI Hideaki 			.mode		= 0644,
493856d417b1SBrian Haley 			.proc_handler	= addrconf_sysctl_disable,
4939778d80beSYOSHIFUJI Hideaki 		},
4940778d80beSYOSHIFUJI Hideaki 		{
49411b34be74SYOSHIFUJI Hideaki 			.procname	= "accept_dad",
49421b34be74SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_dad,
49431b34be74SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
49441b34be74SYOSHIFUJI Hideaki 			.mode		= 0644,
49456d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
49461b34be74SYOSHIFUJI Hideaki 		},
49471b34be74SYOSHIFUJI Hideaki 		{
4948f7734fdfSOctavian Purdila 			.procname       = "force_tllao",
4949f7734fdfSOctavian Purdila 			.data           = &ipv6_devconf.force_tllao,
4950f7734fdfSOctavian Purdila 			.maxlen         = sizeof(int),
4951f7734fdfSOctavian Purdila 			.mode           = 0644,
4952f7734fdfSOctavian Purdila 			.proc_handler   = proc_dointvec
4953f7734fdfSOctavian Purdila 		},
4954f7734fdfSOctavian Purdila 		{
49555cb04436SHannes Frederic Sowa 			.procname       = "ndisc_notify",
49565cb04436SHannes Frederic Sowa 			.data           = &ipv6_devconf.ndisc_notify,
49575cb04436SHannes Frederic Sowa 			.maxlen         = sizeof(int),
49585cb04436SHannes Frederic Sowa 			.mode           = 0644,
49595cb04436SHannes Frederic Sowa 			.proc_handler   = proc_dointvec
49605cb04436SHannes Frederic Sowa 		},
49615cb04436SHannes Frederic Sowa 		{
4962f8572d8fSEric W. Biederman 			/* sentinel */
49631da177e4SLinus Torvalds 		}
49641da177e4SLinus Torvalds 	},
49651da177e4SLinus Torvalds };
49661da177e4SLinus Torvalds 
4967bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name,
4968f8572d8fSEric W. Biederman 		struct inet6_dev *idev, struct ipv6_devconf *p)
49691da177e4SLinus Torvalds {
49701da177e4SLinus Torvalds 	int i;
49711da177e4SLinus Torvalds 	struct addrconf_sysctl_table *t;
49726105e293SEric W. Biederman 	char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
49731dab6222SPavel Emelyanov 
4974af879cc7SArnaldo Carvalho de Melo 	t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
49751da177e4SLinus Torvalds 	if (t == NULL)
4976f68635e6SPavel Emelyanov 		goto out;
4977f68635e6SPavel Emelyanov 
49781da177e4SLinus Torvalds 	for (i = 0; t->addrconf_vars[i].data; i++) {
49791da177e4SLinus Torvalds 		t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf;
49801da177e4SLinus Torvalds 		t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
4981bff16c2fSPavel Emelyanov 		t->addrconf_vars[i].extra2 = net;
49821da177e4SLinus Torvalds 	}
49831da177e4SLinus Torvalds 
49846105e293SEric W. Biederman 	snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
49851da177e4SLinus Torvalds 
49866105e293SEric W. Biederman 	t->sysctl_header = register_net_sysctl(net, path, t->addrconf_vars);
49871da177e4SLinus Torvalds 	if (t->sysctl_header == NULL)
49886105e293SEric W. Biederman 		goto free;
4989f68635e6SPavel Emelyanov 
49901da177e4SLinus Torvalds 	p->sysctl = t;
499195897312SPavel Emelyanov 	return 0;
49921da177e4SLinus Torvalds 
49931da177e4SLinus Torvalds free:
49941da177e4SLinus Torvalds 	kfree(t);
4995f68635e6SPavel Emelyanov out:
499695897312SPavel Emelyanov 	return -ENOBUFS;
49971da177e4SLinus Torvalds }
49981da177e4SLinus Torvalds 
4999408c4768SPavel Emelyanov static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
5000f52295a9SPavel Emelyanov {
5001408c4768SPavel Emelyanov 	struct addrconf_sysctl_table *t;
5002f52295a9SPavel Emelyanov 
5003408c4768SPavel Emelyanov 	if (p->sysctl == NULL)
5004408c4768SPavel Emelyanov 		return;
5005408c4768SPavel Emelyanov 
5006408c4768SPavel Emelyanov 	t = p->sysctl;
50071da177e4SLinus Torvalds 	p->sysctl = NULL;
5008ff538818SLucian Adrian Grijincu 	unregister_net_sysctl_table(t->sysctl_header);
50091da177e4SLinus Torvalds 	kfree(t);
50101da177e4SLinus Torvalds }
5011408c4768SPavel Emelyanov 
5012408c4768SPavel Emelyanov static void addrconf_sysctl_register(struct inet6_dev *idev)
5013408c4768SPavel Emelyanov {
501454716e3bSEric W. Biederman 	neigh_sysctl_register(idev->dev, idev->nd_parms, "ipv6",
5015f8572d8fSEric W. Biederman 			      &ndisc_ifinfo_sysctl_change);
5016c346dca1SYOSHIFUJI Hideaki 	__addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
5017f8572d8fSEric W. Biederman 					idev, &idev->cnf);
5018408c4768SPavel Emelyanov }
5019408c4768SPavel Emelyanov 
5020408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev)
5021408c4768SPavel Emelyanov {
5022408c4768SPavel Emelyanov 	__addrconf_sysctl_unregister(&idev->cnf);
5023408c4768SPavel Emelyanov 	neigh_sysctl_unregister(idev->nd_parms);
50241da177e4SLinus Torvalds }
50251da177e4SLinus Torvalds 
50261da177e4SLinus Torvalds 
50271da177e4SLinus Torvalds #endif
50281da177e4SLinus Torvalds 
50292c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net)
5030e0da5a48SPavel Emelyanov {
5031a79ca223SHong Zhiguo 	int err = -ENOMEM;
5032e0da5a48SPavel Emelyanov 	struct ipv6_devconf *all, *dflt;
5033e0da5a48SPavel Emelyanov 
5034a79ca223SHong Zhiguo 	all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
5035e0da5a48SPavel Emelyanov 	if (all == NULL)
5036e0da5a48SPavel Emelyanov 		goto err_alloc_all;
5037e0da5a48SPavel Emelyanov 
5038a79ca223SHong Zhiguo 	dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
5039e0da5a48SPavel Emelyanov 	if (dflt == NULL)
5040e0da5a48SPavel Emelyanov 		goto err_alloc_dflt;
5041a79ca223SHong Zhiguo 
504256d417b1SBrian Haley 	/* these will be inherited by all namespaces */
504356d417b1SBrian Haley 	dflt->autoconf = ipv6_defaults.autoconf;
504456d417b1SBrian Haley 	dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
5045e0da5a48SPavel Emelyanov 
5046e0da5a48SPavel Emelyanov 	net->ipv6.devconf_all = all;
5047e0da5a48SPavel Emelyanov 	net->ipv6.devconf_dflt = dflt;
5048e0da5a48SPavel Emelyanov 
5049e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
5050f8572d8fSEric W. Biederman 	err = __addrconf_sysctl_register(net, "all", NULL, all);
5051e0da5a48SPavel Emelyanov 	if (err < 0)
5052e0da5a48SPavel Emelyanov 		goto err_reg_all;
5053e0da5a48SPavel Emelyanov 
5054f8572d8fSEric W. Biederman 	err = __addrconf_sysctl_register(net, "default", NULL, dflt);
5055e0da5a48SPavel Emelyanov 	if (err < 0)
5056e0da5a48SPavel Emelyanov 		goto err_reg_dflt;
5057e0da5a48SPavel Emelyanov #endif
5058e0da5a48SPavel Emelyanov 	return 0;
5059e0da5a48SPavel Emelyanov 
5060e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
5061e0da5a48SPavel Emelyanov err_reg_dflt:
5062e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(all);
5063e0da5a48SPavel Emelyanov err_reg_all:
5064e0da5a48SPavel Emelyanov 	kfree(dflt);
5065e0da5a48SPavel Emelyanov #endif
5066e0da5a48SPavel Emelyanov err_alloc_dflt:
5067e0da5a48SPavel Emelyanov 	kfree(all);
5068e0da5a48SPavel Emelyanov err_alloc_all:
5069e0da5a48SPavel Emelyanov 	return err;
5070e0da5a48SPavel Emelyanov }
5071e0da5a48SPavel Emelyanov 
50722c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net)
5073e0da5a48SPavel Emelyanov {
5074e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
5075e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
5076e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(net->ipv6.devconf_all);
5077e0da5a48SPavel Emelyanov #endif
507809ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net)) {
5079e0da5a48SPavel Emelyanov 		kfree(net->ipv6.devconf_dflt);
5080e0da5a48SPavel Emelyanov 		kfree(net->ipv6.devconf_all);
5081e0da5a48SPavel Emelyanov 	}
5082e0da5a48SPavel Emelyanov }
5083e0da5a48SPavel Emelyanov 
5084e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = {
5085e0da5a48SPavel Emelyanov 	.init = addrconf_init_net,
5086e0da5a48SPavel Emelyanov 	.exit = addrconf_exit_net,
5087e0da5a48SPavel Emelyanov };
5088e0da5a48SPavel Emelyanov 
5089b382b191SThomas Graf static struct rtnl_af_ops inet6_ops = {
5090b382b191SThomas Graf 	.family		  = AF_INET6,
5091b382b191SThomas Graf 	.fill_link_af	  = inet6_fill_link_af,
5092b382b191SThomas Graf 	.get_link_af_size = inet6_get_link_af_size,
5093f53adae4SDaniel Borkmann 	.set_link_af	  = inet6_set_link_af,
5094b382b191SThomas Graf };
5095b382b191SThomas Graf 
50961da177e4SLinus Torvalds /*
50971da177e4SLinus Torvalds  *	Init / cleanup code
50981da177e4SLinus Torvalds  */
50991da177e4SLinus Torvalds 
51001da177e4SLinus Torvalds int __init addrconf_init(void)
51011da177e4SLinus Torvalds {
5102c2e21293Sstephen hemminger 	int i, err;
51032a8cc6c8SYOSHIFUJI Hideaki 
5104e21e8467SStephen Hemminger 	err = ipv6_addr_label_init();
5105e21e8467SStephen Hemminger 	if (err < 0) {
5106f3213831SJoe Perches 		pr_crit("%s: cannot initialize default policy table: %d\n",
5107f3213831SJoe Perches 			__func__, err);
51082cc6d2bfSNeil Horman 		goto out;
51092a8cc6c8SYOSHIFUJI Hideaki 	}
51101da177e4SLinus Torvalds 
51112cc6d2bfSNeil Horman 	err = register_pernet_subsys(&addrconf_ops);
51122cc6d2bfSNeil Horman 	if (err < 0)
51132cc6d2bfSNeil Horman 		goto out_addrlabel;
5114e0da5a48SPavel Emelyanov 
51151da177e4SLinus Torvalds 	/* The addrconf netdev notifier requires that loopback_dev
51161da177e4SLinus Torvalds 	 * has it's ipv6 private information allocated and setup
51171da177e4SLinus Torvalds 	 * before it can bring up and give link-local addresses
51181da177e4SLinus Torvalds 	 * to other devices which are up.
51191da177e4SLinus Torvalds 	 *
51201da177e4SLinus Torvalds 	 * Unfortunately, loopback_dev is not necessarily the first
51211da177e4SLinus Torvalds 	 * entry in the global dev_base list of net devices.  In fact,
51221da177e4SLinus Torvalds 	 * it is likely to be the very last entry on that list.
51231da177e4SLinus Torvalds 	 * So this causes the notifier registry below to try and
51241da177e4SLinus Torvalds 	 * give link-local addresses to all devices besides loopback_dev
51251da177e4SLinus Torvalds 	 * first, then loopback_dev, which cases all the non-loopback_dev
51261da177e4SLinus Torvalds 	 * devices to fail to get a link-local address.
51271da177e4SLinus Torvalds 	 *
51281da177e4SLinus Torvalds 	 * So, as a temporary fix, allocate the ipv6 structure for
51291da177e4SLinus Torvalds 	 * loopback_dev first by hand.
51301da177e4SLinus Torvalds 	 * Longer term, all of the dependencies ipv6 has upon the loopback
51311da177e4SLinus Torvalds 	 * device and it being up should be removed.
51321da177e4SLinus Torvalds 	 */
51331da177e4SLinus Torvalds 	rtnl_lock();
51342774c7abSEric W. Biederman 	if (!ipv6_add_dev(init_net.loopback_dev))
51351da177e4SLinus Torvalds 		err = -ENOMEM;
51361da177e4SLinus Torvalds 	rtnl_unlock();
51371da177e4SLinus Torvalds 	if (err)
5138e0da5a48SPavel Emelyanov 		goto errlo;
51391da177e4SLinus Torvalds 
5140c2e21293Sstephen hemminger 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
5141c2e21293Sstephen hemminger 		INIT_HLIST_HEAD(&inet6_addr_lst[i]);
5142c2e21293Sstephen hemminger 
51431da177e4SLinus Torvalds 	register_netdevice_notifier(&ipv6_dev_notf);
51441da177e4SLinus Torvalds 
51451da177e4SLinus Torvalds 	addrconf_verify(0);
5146c127ea2cSThomas Graf 
5147b382b191SThomas Graf 	err = rtnl_af_register(&inet6_ops);
5148b382b191SThomas Graf 	if (err < 0)
5149b382b191SThomas Graf 		goto errout_af;
5150b382b191SThomas Graf 
5151c7ac8679SGreg Rose 	err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
5152c7ac8679SGreg Rose 			      NULL);
5153c127ea2cSThomas Graf 	if (err < 0)
5154c127ea2cSThomas Graf 		goto errout;
5155c127ea2cSThomas Graf 
5156c127ea2cSThomas Graf 	/* Only the first call to __rtnl_register can fail */
5157c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
5158c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
5159c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
5160c7ac8679SGreg Rose 			inet6_dump_ifaddr, NULL);
5161c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
5162c7ac8679SGreg Rose 			inet6_dump_ifmcaddr, NULL);
5163c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
5164c7ac8679SGreg Rose 			inet6_dump_ifacaddr, NULL);
516576f8f6cbSNicolas Dichtel 	__rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
51667a674200SNicolas Dichtel 			inet6_netconf_dump_devconf, NULL);
5167c127ea2cSThomas Graf 
51682a8cc6c8SYOSHIFUJI Hideaki 	ipv6_addr_label_rtnl_register();
51692a8cc6c8SYOSHIFUJI Hideaki 
51701da177e4SLinus Torvalds 	return 0;
5171c127ea2cSThomas Graf errout:
5172b382b191SThomas Graf 	rtnl_af_unregister(&inet6_ops);
5173b382b191SThomas Graf errout_af:
5174c127ea2cSThomas Graf 	unregister_netdevice_notifier(&ipv6_dev_notf);
5175e0da5a48SPavel Emelyanov errlo:
5176e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
51772cc6d2bfSNeil Horman out_addrlabel:
51782cc6d2bfSNeil Horman 	ipv6_addr_label_cleanup();
51792cc6d2bfSNeil Horman out:
5180c127ea2cSThomas Graf 	return err;
51811da177e4SLinus Torvalds }
51821da177e4SLinus Torvalds 
518309f7709fSDaniel Lezcano void addrconf_cleanup(void)
51841da177e4SLinus Torvalds {
5185176c39afSDaniel Lezcano 	struct net_device *dev;
51861da177e4SLinus Torvalds 	int i;
51871da177e4SLinus Torvalds 
51881da177e4SLinus Torvalds 	unregister_netdevice_notifier(&ipv6_dev_notf);
5189e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
51902cc6d2bfSNeil Horman 	ipv6_addr_label_cleanup();
51911da177e4SLinus Torvalds 
51921da177e4SLinus Torvalds 	rtnl_lock();
51931da177e4SLinus Torvalds 
5194b382b191SThomas Graf 	__rtnl_af_unregister(&inet6_ops);
5195b382b191SThomas Graf 
5196176c39afSDaniel Lezcano 	/* clean dev list */
5197176c39afSDaniel Lezcano 	for_each_netdev(&init_net, dev) {
5198176c39afSDaniel Lezcano 		if (__in6_dev_get(dev) == NULL)
5199176c39afSDaniel Lezcano 			continue;
5200176c39afSDaniel Lezcano 		addrconf_ifdown(dev, 1);
5201176c39afSDaniel Lezcano 	}
5202176c39afSDaniel Lezcano 	addrconf_ifdown(init_net.loopback_dev, 2);
5203176c39afSDaniel Lezcano 
52041da177e4SLinus Torvalds 	/*
52051da177e4SLinus Torvalds 	 *	Check hash table.
52061da177e4SLinus Torvalds 	 */
52075c578aedSstephen hemminger 	spin_lock_bh(&addrconf_hash_lock);
5208c2e21293Sstephen hemminger 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
5209c2e21293Sstephen hemminger 		WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
52105c578aedSstephen hemminger 	spin_unlock_bh(&addrconf_hash_lock);
52111da177e4SLinus Torvalds 
52121da177e4SLinus Torvalds 	del_timer(&addr_chk_timer);
52131da177e4SLinus Torvalds 	rtnl_unlock();
52141da177e4SLinus Torvalds }
5215