xref: /openbmc/linux/net/ipv6/addrconf.c (revision fc403832f7bd94014fe3d965e0652a857160f1c9)
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 
172e041c683SAlan Stern static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
1731da177e4SLinus Torvalds 
174888c848eSAdrian Bunk static struct ipv6_devconf ipv6_devconf __read_mostly = {
1751da177e4SLinus Torvalds 	.forwarding		= 0,
1761da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
1771da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
1781da177e4SLinus Torvalds 	.accept_ra		= 1,
1791da177e4SLinus Torvalds 	.accept_redirects	= 1,
1801da177e4SLinus Torvalds 	.autoconf		= 1,
1811da177e4SLinus Torvalds 	.force_mld_version	= 0,
1821da177e4SLinus Torvalds 	.dad_transmits		= 1,
1831da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
1841da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
1851da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
1861da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
1871da177e4SLinus Torvalds 	.use_tempaddr 		= 0,
1881da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
1891da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
1901da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
1911da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
1921da177e4SLinus Torvalds #endif
1931da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
19465f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
195c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
196930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
197930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
19852e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
19909c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
20009c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
20109c884d4SYOSHIFUJI Hideaki #endif
202930d6ff2SYOSHIFUJI Hideaki #endif
203fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2040bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
205778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
2061b34be74SYOSHIFUJI Hideaki 	.accept_dad		= 1,
2071da177e4SLinus Torvalds };
2081da177e4SLinus Torvalds 
209ab32ea5dSBrian Haley static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
2101da177e4SLinus Torvalds 	.forwarding		= 0,
2111da177e4SLinus Torvalds 	.hop_limit		= IPV6_DEFAULT_HOPLIMIT,
2121da177e4SLinus Torvalds 	.mtu6			= IPV6_MIN_MTU,
2131da177e4SLinus Torvalds 	.accept_ra		= 1,
2141da177e4SLinus Torvalds 	.accept_redirects	= 1,
2151da177e4SLinus Torvalds 	.autoconf		= 1,
2161da177e4SLinus Torvalds 	.dad_transmits		= 1,
2171da177e4SLinus Torvalds 	.rtr_solicits		= MAX_RTR_SOLICITATIONS,
2181da177e4SLinus Torvalds 	.rtr_solicit_interval	= RTR_SOLICITATION_INTERVAL,
2191da177e4SLinus Torvalds 	.rtr_solicit_delay	= MAX_RTR_SOLICITATION_DELAY,
2201da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
2211da177e4SLinus Torvalds 	.use_tempaddr		= 0,
2221da177e4SLinus Torvalds 	.temp_valid_lft		= TEMP_VALID_LIFETIME,
2231da177e4SLinus Torvalds 	.temp_prefered_lft	= TEMP_PREFERRED_LIFETIME,
2241da177e4SLinus Torvalds 	.regen_max_retry	= REGEN_MAX_RETRY,
2251da177e4SLinus Torvalds 	.max_desync_factor	= MAX_DESYNC_FACTOR,
2261da177e4SLinus Torvalds #endif
2271da177e4SLinus Torvalds 	.max_addresses		= IPV6_MAX_ADDRESSES,
22865f5c7c1SYOSHIFUJI Hideaki 	.accept_ra_defrtr	= 1,
229c4fd30ebSYOSHIFUJI Hideaki 	.accept_ra_pinfo	= 1,
230930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
231930d6ff2SYOSHIFUJI Hideaki 	.accept_ra_rtr_pref	= 1,
23252e16356SYOSHIFUJI Hideaki 	.rtr_probe_interval	= 60 * HZ,
23309c884d4SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTE_INFO
23409c884d4SYOSHIFUJI Hideaki 	.accept_ra_rt_info_max_plen = 0,
23509c884d4SYOSHIFUJI Hideaki #endif
236930d6ff2SYOSHIFUJI Hideaki #endif
237fbea49e1SYOSHIFUJI Hideaki 	.proxy_ndp		= 0,
2380bcbc926SYOSHIFUJI Hideaki 	.accept_source_route	= 0,	/* we do not accept RH0 by default. */
239778d80beSYOSHIFUJI Hideaki 	.disable_ipv6		= 0,
2401b34be74SYOSHIFUJI Hideaki 	.accept_dad		= 1,
2411da177e4SLinus Torvalds };
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
2441da177e4SLinus Torvalds const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
2451da177e4SLinus Torvalds const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
246f3ee4010SYOSHIFUJI Hideaki const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
247f3ee4010SYOSHIFUJI Hideaki const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
2482c5e8933SHannes Frederic Sowa const struct in6_addr in6addr_interfacelocal_allnodes = IN6ADDR_INTERFACELOCAL_ALLNODES_INIT;
2492c5e8933SHannes Frederic Sowa const struct in6_addr in6addr_interfacelocal_allrouters = IN6ADDR_INTERFACELOCAL_ALLROUTERS_INIT;
2502c5e8933SHannes Frederic Sowa const struct in6_addr in6addr_sitelocal_allrouters = IN6ADDR_SITELOCAL_ALLROUTERS_INIT;
2511da177e4SLinus Torvalds 
252f24e3d65SMitsuru Chinen /* Check if a valid qdisc is available */
25305297949SDavid S. Miller static inline bool addrconf_qdisc_ok(const struct net_device *dev)
254f24e3d65SMitsuru Chinen {
25505297949SDavid S. Miller 	return !qdisc_tx_is_noop(dev);
256f24e3d65SMitsuru Chinen }
257f24e3d65SMitsuru Chinen 
2581da177e4SLinus Torvalds static void addrconf_del_timer(struct inet6_ifaddr *ifp)
2591da177e4SLinus Torvalds {
2601da177e4SLinus Torvalds 	if (del_timer(&ifp->timer))
2611da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
2621da177e4SLinus Torvalds }
2631da177e4SLinus Torvalds 
264e21e8467SStephen Hemminger enum addrconf_timer_t {
2651da177e4SLinus Torvalds 	AC_NONE,
2661da177e4SLinus Torvalds 	AC_DAD,
2671da177e4SLinus Torvalds 	AC_RS,
2681da177e4SLinus Torvalds };
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
2711da177e4SLinus Torvalds 			       enum addrconf_timer_t what,
2721da177e4SLinus Torvalds 			       unsigned long when)
2731da177e4SLinus Torvalds {
2741da177e4SLinus Torvalds 	if (!del_timer(&ifp->timer))
2751da177e4SLinus Torvalds 		in6_ifa_hold(ifp);
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	switch (what) {
2781da177e4SLinus Torvalds 	case AC_DAD:
2791da177e4SLinus Torvalds 		ifp->timer.function = addrconf_dad_timer;
2801da177e4SLinus Torvalds 		break;
2811da177e4SLinus Torvalds 	case AC_RS:
2821da177e4SLinus Torvalds 		ifp->timer.function = addrconf_rs_timer;
2831da177e4SLinus Torvalds 		break;
284e21e8467SStephen Hemminger 	default:
285e21e8467SStephen Hemminger 		break;
2861da177e4SLinus Torvalds 	}
2871da177e4SLinus Torvalds 	ifp->timer.expires = jiffies + when;
2881da177e4SLinus Torvalds 	add_timer(&ifp->timer);
2891da177e4SLinus Torvalds }
2901da177e4SLinus Torvalds 
2917f7d9a6bSHerbert Xu static int snmp6_alloc_dev(struct inet6_dev *idev)
2927f7d9a6bSHerbert Xu {
2937d720c3eSTejun Heo 	if (snmp_mib_init((void __percpu **)idev->stats.ipv6,
2941823e4c8SEric Dumazet 			  sizeof(struct ipstats_mib),
2951823e4c8SEric Dumazet 			  __alignof__(struct ipstats_mib)) < 0)
2967f7d9a6bSHerbert Xu 		goto err_ip;
297be281e55SEric Dumazet 	idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
298be281e55SEric Dumazet 					GFP_KERNEL);
299be281e55SEric Dumazet 	if (!idev->stats.icmpv6dev)
3007f7d9a6bSHerbert Xu 		goto err_icmp;
301be281e55SEric Dumazet 	idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
302be281e55SEric Dumazet 					   GFP_KERNEL);
303be281e55SEric Dumazet 	if (!idev->stats.icmpv6msgdev)
30414878f75SDavid L Stevens 		goto err_icmpmsg;
3057f7d9a6bSHerbert Xu 
3067f7d9a6bSHerbert Xu 	return 0;
3077f7d9a6bSHerbert Xu 
30814878f75SDavid L Stevens err_icmpmsg:
309be281e55SEric Dumazet 	kfree(idev->stats.icmpv6dev);
3107f7d9a6bSHerbert Xu err_icmp:
3117d720c3eSTejun Heo 	snmp_mib_free((void __percpu **)idev->stats.ipv6);
3127f7d9a6bSHerbert Xu err_ip:
313aaf70ec7SPavel Emelyanov 	return -ENOMEM;
3147f7d9a6bSHerbert Xu }
3157f7d9a6bSHerbert Xu 
31616910b98SPavel Emelyanov static void snmp6_free_dev(struct inet6_dev *idev)
3177f7d9a6bSHerbert Xu {
318be281e55SEric Dumazet 	kfree(idev->stats.icmpv6msgdev);
319be281e55SEric Dumazet 	kfree(idev->stats.icmpv6dev);
3207d720c3eSTejun Heo 	snmp_mib_free((void __percpu **)idev->stats.ipv6);
3217f7d9a6bSHerbert Xu }
3227f7d9a6bSHerbert Xu 
3231da177e4SLinus Torvalds /* Nobody refers to this device, we may destroy it. */
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds void in6_dev_finish_destroy(struct inet6_dev *idev)
3261da177e4SLinus Torvalds {
3271da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
328547b792cSIlpo Järvinen 
329502a2ffdSstephen hemminger 	WARN_ON(!list_empty(&idev->addr_list));
330547b792cSIlpo Järvinen 	WARN_ON(idev->mc_list != NULL);
331547b792cSIlpo Järvinen 
3321da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
33391df42beSJoe Perches 	pr_debug("%s: %s\n", __func__, dev ? dev->name : "NIL");
3341da177e4SLinus Torvalds #endif
3351da177e4SLinus Torvalds 	dev_put(dev);
3361da177e4SLinus Torvalds 	if (!idev->dead) {
337f3213831SJoe Perches 		pr_warn("Freeing alive inet6 device %p\n", idev);
3381da177e4SLinus Torvalds 		return;
3391da177e4SLinus Torvalds 	}
3401da177e4SLinus Torvalds 	snmp6_free_dev(idev);
34138f57d1aSLai Jiangshan 	kfree_rcu(idev, rcu);
3421da177e4SLinus Torvalds }
3437159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(in6_dev_finish_destroy);
3447159039aSYOSHIFUJI Hideaki 
3451da177e4SLinus Torvalds static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
3461da177e4SLinus Torvalds {
3471da177e4SLinus Torvalds 	struct inet6_dev *ndev;
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	ASSERT_RTNL();
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 	if (dev->mtu < IPV6_MIN_MTU)
3521da177e4SLinus Torvalds 		return NULL;
3531da177e4SLinus Torvalds 
354322f74a4SIngo Oeser 	ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
3551da177e4SLinus Torvalds 
356322f74a4SIngo Oeser 	if (ndev == NULL)
357322f74a4SIngo Oeser 		return NULL;
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	rwlock_init(&ndev->lock);
3601da177e4SLinus Torvalds 	ndev->dev = dev;
361502a2ffdSstephen hemminger 	INIT_LIST_HEAD(&ndev->addr_list);
362502a2ffdSstephen hemminger 
363c346dca1SYOSHIFUJI Hideaki 	memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
3641da177e4SLinus Torvalds 	ndev->cnf.mtu6 = dev->mtu;
3651da177e4SLinus Torvalds 	ndev->cnf.sysctl = NULL;
3661da177e4SLinus Torvalds 	ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
3671da177e4SLinus Torvalds 	if (ndev->nd_parms == NULL) {
3681da177e4SLinus Torvalds 		kfree(ndev);
3691da177e4SLinus Torvalds 		return NULL;
3701da177e4SLinus Torvalds 	}
3710187bdfbSBen Hutchings 	if (ndev->cnf.forwarding)
3720187bdfbSBen Hutchings 		dev_disable_lro(dev);
3731da177e4SLinus Torvalds 	/* We refer to the device */
3741da177e4SLinus Torvalds 	dev_hold(dev);
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	if (snmp6_alloc_dev(ndev) < 0) {
3771da177e4SLinus Torvalds 		ADBG((KERN_WARNING
378f3213831SJoe Perches 			"%s: cannot allocate memory for statistics; dev=%s.\n",
3790dc47877SHarvey Harrison 			__func__, dev->name));
3801da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, ndev->nd_parms);
3818603e33dSRoy Li 		dev_put(dev);
3828603e33dSRoy Li 		kfree(ndev);
3831da177e4SLinus Torvalds 		return NULL;
3841da177e4SLinus Torvalds 	}
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 	if (snmp6_register_dev(ndev) < 0) {
3871da177e4SLinus Torvalds 		ADBG((KERN_WARNING
388f3213831SJoe Perches 			"%s: cannot create /proc/net/dev_snmp6/%s\n",
3890dc47877SHarvey Harrison 			__func__, dev->name));
3901da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, ndev->nd_parms);
3911da177e4SLinus Torvalds 		ndev->dead = 1;
3921da177e4SLinus Torvalds 		in6_dev_finish_destroy(ndev);
3931da177e4SLinus Torvalds 		return NULL;
3941da177e4SLinus Torvalds 	}
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	/* One reference from device.  We must do this before
3971da177e4SLinus Torvalds 	 * we invoke __ipv6_regen_rndid().
3981da177e4SLinus Torvalds 	 */
3991da177e4SLinus Torvalds 	in6_dev_hold(ndev);
4001da177e4SLinus Torvalds 
4011b34be74SYOSHIFUJI Hideaki 	if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
4021b34be74SYOSHIFUJI Hideaki 		ndev->cnf.accept_dad = -1;
4031b34be74SYOSHIFUJI Hideaki 
40407a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
405c7dc89c0SFred L. Templin 	if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
406f3213831SJoe Perches 		pr_info("%s: Disabled Multicast RS\n", dev->name);
407c7dc89c0SFred L. Templin 		ndev->cnf.rtr_solicits = 0;
408c7dc89c0SFred L. Templin 	}
409b077d7abSYOSHIFUJI Hideaki #endif
410b077d7abSYOSHIFUJI Hideaki 
4111da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
412372e6c8fSstephen hemminger 	INIT_LIST_HEAD(&ndev->tempaddr_list);
4131da177e4SLinus Torvalds 	setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
4141da177e4SLinus Torvalds 	if ((dev->flags&IFF_LOOPBACK) ||
4151da177e4SLinus Torvalds 	    dev->type == ARPHRD_TUNNEL ||
4169625ed72SYOSHIFUJI Hideaki 	    dev->type == ARPHRD_TUNNEL6 ||
4171da177e4SLinus Torvalds 	    dev->type == ARPHRD_SIT ||
4181da177e4SLinus Torvalds 	    dev->type == ARPHRD_NONE) {
4190be669bbSJoerg Roedel 		ndev->cnf.use_tempaddr = -1;
4201da177e4SLinus Torvalds 	} else {
4211da177e4SLinus Torvalds 		in6_dev_hold(ndev);
4221da177e4SLinus Torvalds 		ipv6_regen_rndid((unsigned long) ndev);
4231da177e4SLinus Torvalds 	}
4241da177e4SLinus Torvalds #endif
425914faa14SDaniel Borkmann 	ndev->token = in6addr_any;
4261da177e4SLinus Torvalds 
427f24e3d65SMitsuru Chinen 	if (netif_running(dev) && addrconf_qdisc_ok(dev))
42853aadcc9SHerbert Xu 		ndev->if_flags |= IF_READY;
42953aadcc9SHerbert Xu 
4301da177e4SLinus Torvalds 	ipv6_mc_init_dev(ndev);
4311da177e4SLinus Torvalds 	ndev->tstamp = jiffies;
432f52295a9SPavel Emelyanov 	addrconf_sysctl_register(ndev);
43330c4cf57SDavid L Stevens 	/* protected by rtnl_lock */
434cf778b00SEric Dumazet 	rcu_assign_pointer(dev->ip6_ptr, ndev);
435d88ae4ccSYOSHIFUJI Hideaki 
4362c5e8933SHannes Frederic Sowa 	/* Join interface-local all-node multicast group */
4372c5e8933SHannes Frederic Sowa 	ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
4382c5e8933SHannes Frederic Sowa 
439d88ae4ccSYOSHIFUJI Hideaki 	/* Join all-node multicast group */
440f3ee4010SYOSHIFUJI Hideaki 	ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
441d88ae4ccSYOSHIFUJI Hideaki 
442d6ddef9eSLi Wei 	/* Join all-router multicast group if forwarding is set */
4438b2aaedeSLi Wei 	if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
444d6ddef9eSLi Wei 		ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
445d6ddef9eSLi Wei 
4461da177e4SLinus Torvalds 	return ndev;
4471da177e4SLinus Torvalds }
4481da177e4SLinus Torvalds 
449cee89473SYOSHIFUJI Hideaki static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
4501da177e4SLinus Torvalds {
4511da177e4SLinus Torvalds 	struct inet6_dev *idev;
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	ASSERT_RTNL();
4541da177e4SLinus Torvalds 
455e21e8467SStephen Hemminger 	idev = __in6_dev_get(dev);
456e21e8467SStephen Hemminger 	if (!idev) {
457e21e8467SStephen Hemminger 		idev = ipv6_add_dev(dev);
458e21e8467SStephen Hemminger 		if (!idev)
4591da177e4SLinus Torvalds 			return NULL;
4601da177e4SLinus Torvalds 	}
461c5e33bddSYOSHIFUJI Hideaki 
4621da177e4SLinus Torvalds 	if (dev->flags&IFF_UP)
4631da177e4SLinus Torvalds 		ipv6_mc_up(idev);
4641da177e4SLinus Torvalds 	return idev;
4651da177e4SLinus Torvalds }
4661da177e4SLinus Torvalds 
467f3a1bfb1SNicolas Dichtel static int inet6_netconf_msgsize_devconf(int type)
468f3a1bfb1SNicolas Dichtel {
469f3a1bfb1SNicolas Dichtel 	int size =  NLMSG_ALIGN(sizeof(struct netconfmsg))
470f3a1bfb1SNicolas Dichtel 		    + nla_total_size(4);	/* NETCONFA_IFINDEX */
471f3a1bfb1SNicolas Dichtel 
47276f8f6cbSNicolas Dichtel 	/* type -1 is used for ALL */
47376f8f6cbSNicolas Dichtel 	if (type == -1 || type == NETCONFA_FORWARDING)
474f3a1bfb1SNicolas Dichtel 		size += nla_total_size(4);
475b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
476d67b8c61SNicolas Dichtel 	if (type == -1 || type == NETCONFA_MC_FORWARDING)
477d67b8c61SNicolas Dichtel 		size += nla_total_size(4);
478b1afce95SDavid S. Miller #endif
479f3a1bfb1SNicolas Dichtel 
480f3a1bfb1SNicolas Dichtel 	return size;
481f3a1bfb1SNicolas Dichtel }
482f3a1bfb1SNicolas Dichtel 
483f3a1bfb1SNicolas Dichtel static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
484f3a1bfb1SNicolas Dichtel 				      struct ipv6_devconf *devconf, u32 portid,
485f3a1bfb1SNicolas Dichtel 				      u32 seq, int event, unsigned int flags,
486f3a1bfb1SNicolas Dichtel 				      int type)
487f3a1bfb1SNicolas Dichtel {
488f3a1bfb1SNicolas Dichtel 	struct nlmsghdr  *nlh;
489f3a1bfb1SNicolas Dichtel 	struct netconfmsg *ncm;
490f3a1bfb1SNicolas Dichtel 
491f3a1bfb1SNicolas Dichtel 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
492f3a1bfb1SNicolas Dichtel 			flags);
493f3a1bfb1SNicolas Dichtel 	if (nlh == NULL)
494f3a1bfb1SNicolas Dichtel 		return -EMSGSIZE;
495f3a1bfb1SNicolas Dichtel 
496f3a1bfb1SNicolas Dichtel 	ncm = nlmsg_data(nlh);
497f3a1bfb1SNicolas Dichtel 	ncm->ncm_family = AF_INET6;
498f3a1bfb1SNicolas Dichtel 
499f3a1bfb1SNicolas Dichtel 	if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
500f3a1bfb1SNicolas Dichtel 		goto nla_put_failure;
501f3a1bfb1SNicolas Dichtel 
50276f8f6cbSNicolas Dichtel 	/* type -1 is used for ALL */
50376f8f6cbSNicolas Dichtel 	if ((type == -1 || type == NETCONFA_FORWARDING) &&
504f3a1bfb1SNicolas Dichtel 	    nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
505f3a1bfb1SNicolas Dichtel 		goto nla_put_failure;
506b1afce95SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
507d67b8c61SNicolas Dichtel 	if ((type == -1 || type == NETCONFA_MC_FORWARDING) &&
508d67b8c61SNicolas Dichtel 	    nla_put_s32(skb, NETCONFA_MC_FORWARDING,
509d67b8c61SNicolas Dichtel 			devconf->mc_forwarding) < 0)
510d67b8c61SNicolas Dichtel 		goto nla_put_failure;
511b1afce95SDavid S. Miller #endif
512f3a1bfb1SNicolas Dichtel 	return nlmsg_end(skb, nlh);
513f3a1bfb1SNicolas Dichtel 
514f3a1bfb1SNicolas Dichtel nla_put_failure:
515f3a1bfb1SNicolas Dichtel 	nlmsg_cancel(skb, nlh);
516f3a1bfb1SNicolas Dichtel 	return -EMSGSIZE;
517f3a1bfb1SNicolas Dichtel }
518f3a1bfb1SNicolas Dichtel 
519d67b8c61SNicolas Dichtel void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
520f3a1bfb1SNicolas Dichtel 				  struct ipv6_devconf *devconf)
521f3a1bfb1SNicolas Dichtel {
522f3a1bfb1SNicolas Dichtel 	struct sk_buff *skb;
523f3a1bfb1SNicolas Dichtel 	int err = -ENOBUFS;
524f3a1bfb1SNicolas Dichtel 
525f3a1bfb1SNicolas Dichtel 	skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC);
526f3a1bfb1SNicolas Dichtel 	if (skb == NULL)
527f3a1bfb1SNicolas Dichtel 		goto errout;
528f3a1bfb1SNicolas Dichtel 
529f3a1bfb1SNicolas Dichtel 	err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
530f3a1bfb1SNicolas Dichtel 					 RTM_NEWNETCONF, 0, type);
531f3a1bfb1SNicolas Dichtel 	if (err < 0) {
532f3a1bfb1SNicolas Dichtel 		/* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
533f3a1bfb1SNicolas Dichtel 		WARN_ON(err == -EMSGSIZE);
534f3a1bfb1SNicolas Dichtel 		kfree_skb(skb);
535f3a1bfb1SNicolas Dichtel 		goto errout;
536f3a1bfb1SNicolas Dichtel 	}
537f3a1bfb1SNicolas Dichtel 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_ATOMIC);
538f3a1bfb1SNicolas Dichtel 	return;
539f3a1bfb1SNicolas Dichtel errout:
540f3a1bfb1SNicolas Dichtel 	rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
541f3a1bfb1SNicolas Dichtel }
542f3a1bfb1SNicolas Dichtel 
54376f8f6cbSNicolas Dichtel static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
54476f8f6cbSNicolas Dichtel 	[NETCONFA_IFINDEX]	= { .len = sizeof(int) },
54576f8f6cbSNicolas Dichtel 	[NETCONFA_FORWARDING]	= { .len = sizeof(int) },
54676f8f6cbSNicolas Dichtel };
54776f8f6cbSNicolas Dichtel 
54876f8f6cbSNicolas Dichtel static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
549661d2967SThomas Graf 				     struct nlmsghdr *nlh)
55076f8f6cbSNicolas Dichtel {
55176f8f6cbSNicolas Dichtel 	struct net *net = sock_net(in_skb->sk);
55276f8f6cbSNicolas Dichtel 	struct nlattr *tb[NETCONFA_MAX+1];
55376f8f6cbSNicolas Dichtel 	struct netconfmsg *ncm;
55476f8f6cbSNicolas Dichtel 	struct sk_buff *skb;
55576f8f6cbSNicolas Dichtel 	struct ipv6_devconf *devconf;
55676f8f6cbSNicolas Dichtel 	struct inet6_dev *in6_dev;
55776f8f6cbSNicolas Dichtel 	struct net_device *dev;
55876f8f6cbSNicolas Dichtel 	int ifindex;
55976f8f6cbSNicolas Dichtel 	int err;
56076f8f6cbSNicolas Dichtel 
56176f8f6cbSNicolas Dichtel 	err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
56276f8f6cbSNicolas Dichtel 			  devconf_ipv6_policy);
56376f8f6cbSNicolas Dichtel 	if (err < 0)
56476f8f6cbSNicolas Dichtel 		goto errout;
56576f8f6cbSNicolas Dichtel 
56676f8f6cbSNicolas Dichtel 	err = EINVAL;
56776f8f6cbSNicolas Dichtel 	if (!tb[NETCONFA_IFINDEX])
56876f8f6cbSNicolas Dichtel 		goto errout;
56976f8f6cbSNicolas Dichtel 
57076f8f6cbSNicolas Dichtel 	ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
57176f8f6cbSNicolas Dichtel 	switch (ifindex) {
57276f8f6cbSNicolas Dichtel 	case NETCONFA_IFINDEX_ALL:
57376f8f6cbSNicolas Dichtel 		devconf = net->ipv6.devconf_all;
57476f8f6cbSNicolas Dichtel 		break;
57576f8f6cbSNicolas Dichtel 	case NETCONFA_IFINDEX_DEFAULT:
57676f8f6cbSNicolas Dichtel 		devconf = net->ipv6.devconf_dflt;
57776f8f6cbSNicolas Dichtel 		break;
57876f8f6cbSNicolas Dichtel 	default:
57976f8f6cbSNicolas Dichtel 		dev = __dev_get_by_index(net, ifindex);
58076f8f6cbSNicolas Dichtel 		if (dev == NULL)
58176f8f6cbSNicolas Dichtel 			goto errout;
58276f8f6cbSNicolas Dichtel 		in6_dev = __in6_dev_get(dev);
58376f8f6cbSNicolas Dichtel 		if (in6_dev == NULL)
58476f8f6cbSNicolas Dichtel 			goto errout;
58576f8f6cbSNicolas Dichtel 		devconf = &in6_dev->cnf;
58676f8f6cbSNicolas Dichtel 		break;
58776f8f6cbSNicolas Dichtel 	}
58876f8f6cbSNicolas Dichtel 
58976f8f6cbSNicolas Dichtel 	err = -ENOBUFS;
59076f8f6cbSNicolas Dichtel 	skb = nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC);
59176f8f6cbSNicolas Dichtel 	if (skb == NULL)
59276f8f6cbSNicolas Dichtel 		goto errout;
59376f8f6cbSNicolas Dichtel 
59476f8f6cbSNicolas Dichtel 	err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
59576f8f6cbSNicolas Dichtel 					 NETLINK_CB(in_skb).portid,
59676f8f6cbSNicolas Dichtel 					 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
59776f8f6cbSNicolas Dichtel 					 -1);
59876f8f6cbSNicolas Dichtel 	if (err < 0) {
59976f8f6cbSNicolas Dichtel 		/* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
60076f8f6cbSNicolas Dichtel 		WARN_ON(err == -EMSGSIZE);
60176f8f6cbSNicolas Dichtel 		kfree_skb(skb);
60276f8f6cbSNicolas Dichtel 		goto errout;
60376f8f6cbSNicolas Dichtel 	}
60476f8f6cbSNicolas Dichtel 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
60576f8f6cbSNicolas Dichtel errout:
60676f8f6cbSNicolas Dichtel 	return err;
60776f8f6cbSNicolas Dichtel }
60876f8f6cbSNicolas Dichtel 
6097a674200SNicolas Dichtel static int inet6_netconf_dump_devconf(struct sk_buff *skb,
6107a674200SNicolas Dichtel 				      struct netlink_callback *cb)
6117a674200SNicolas Dichtel {
6127a674200SNicolas Dichtel 	struct net *net = sock_net(skb->sk);
6137a674200SNicolas Dichtel 	int h, s_h;
6147a674200SNicolas Dichtel 	int idx, s_idx;
6157a674200SNicolas Dichtel 	struct net_device *dev;
6167a674200SNicolas Dichtel 	struct inet6_dev *idev;
6177a674200SNicolas Dichtel 	struct hlist_head *head;
6187a674200SNicolas Dichtel 
6197a674200SNicolas Dichtel 	s_h = cb->args[0];
6207a674200SNicolas Dichtel 	s_idx = idx = cb->args[1];
6217a674200SNicolas Dichtel 
6227a674200SNicolas Dichtel 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
6237a674200SNicolas Dichtel 		idx = 0;
6247a674200SNicolas Dichtel 		head = &net->dev_index_head[h];
6257a674200SNicolas Dichtel 		rcu_read_lock();
62663998ac2SNicolas Dichtel 		cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
62763998ac2SNicolas Dichtel 			  net->dev_base_seq;
6287a674200SNicolas Dichtel 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
6297a674200SNicolas Dichtel 			if (idx < s_idx)
6307a674200SNicolas Dichtel 				goto cont;
6317a674200SNicolas Dichtel 			idev = __in6_dev_get(dev);
6327a674200SNicolas Dichtel 			if (!idev)
6337a674200SNicolas Dichtel 				goto cont;
6347a674200SNicolas Dichtel 
6357a674200SNicolas Dichtel 			if (inet6_netconf_fill_devconf(skb, dev->ifindex,
6367a674200SNicolas Dichtel 						       &idev->cnf,
6377a674200SNicolas Dichtel 						       NETLINK_CB(cb->skb).portid,
6387a674200SNicolas Dichtel 						       cb->nlh->nlmsg_seq,
6397a674200SNicolas Dichtel 						       RTM_NEWNETCONF,
6407a674200SNicolas Dichtel 						       NLM_F_MULTI,
6417a674200SNicolas Dichtel 						       -1) <= 0) {
6427a674200SNicolas Dichtel 				rcu_read_unlock();
6437a674200SNicolas Dichtel 				goto done;
6447a674200SNicolas Dichtel 			}
64563998ac2SNicolas Dichtel 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
6467a674200SNicolas Dichtel cont:
6477a674200SNicolas Dichtel 			idx++;
6487a674200SNicolas Dichtel 		}
6497a674200SNicolas Dichtel 		rcu_read_unlock();
6507a674200SNicolas Dichtel 	}
6517a674200SNicolas Dichtel 	if (h == NETDEV_HASHENTRIES) {
6527a674200SNicolas Dichtel 		if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
6537a674200SNicolas Dichtel 					       net->ipv6.devconf_all,
6547a674200SNicolas Dichtel 					       NETLINK_CB(cb->skb).portid,
6557a674200SNicolas Dichtel 					       cb->nlh->nlmsg_seq,
6567a674200SNicolas Dichtel 					       RTM_NEWNETCONF, NLM_F_MULTI,
6577a674200SNicolas Dichtel 					       -1) <= 0)
6587a674200SNicolas Dichtel 			goto done;
6597a674200SNicolas Dichtel 		else
6607a674200SNicolas Dichtel 			h++;
6617a674200SNicolas Dichtel 	}
6627a674200SNicolas Dichtel 	if (h == NETDEV_HASHENTRIES + 1) {
6637a674200SNicolas Dichtel 		if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
6647a674200SNicolas Dichtel 					       net->ipv6.devconf_dflt,
6657a674200SNicolas Dichtel 					       NETLINK_CB(cb->skb).portid,
6667a674200SNicolas Dichtel 					       cb->nlh->nlmsg_seq,
6677a674200SNicolas Dichtel 					       RTM_NEWNETCONF, NLM_F_MULTI,
6687a674200SNicolas Dichtel 					       -1) <= 0)
6697a674200SNicolas Dichtel 			goto done;
6707a674200SNicolas Dichtel 		else
6717a674200SNicolas Dichtel 			h++;
6727a674200SNicolas Dichtel 	}
6737a674200SNicolas Dichtel done:
6747a674200SNicolas Dichtel 	cb->args[0] = h;
6757a674200SNicolas Dichtel 	cb->args[1] = idx;
6767a674200SNicolas Dichtel 
6777a674200SNicolas Dichtel 	return skb->len;
6787a674200SNicolas Dichtel }
6797a674200SNicolas Dichtel 
6801da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
6811da177e4SLinus Torvalds static void dev_forward_change(struct inet6_dev *idev)
6821da177e4SLinus Torvalds {
6831da177e4SLinus Torvalds 	struct net_device *dev;
6841da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds 	if (!idev)
6871da177e4SLinus Torvalds 		return;
6881da177e4SLinus Torvalds 	dev = idev->dev;
6890187bdfbSBen Hutchings 	if (idev->cnf.forwarding)
6900187bdfbSBen Hutchings 		dev_disable_lro(dev);
6911a940835SAmerigo Wang 	if (dev->flags & IFF_MULTICAST) {
6922c5e8933SHannes Frederic Sowa 		if (idev->cnf.forwarding) {
693f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
6942c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
6952c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
6962c5e8933SHannes Frederic Sowa 		} else {
697f3ee4010SYOSHIFUJI Hideaki 			ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
6982c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
6992c5e8933SHannes Frederic Sowa 			ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
7002c5e8933SHannes Frederic Sowa 		}
7011da177e4SLinus Torvalds 	}
702502a2ffdSstephen hemminger 
703502a2ffdSstephen hemminger 	list_for_each_entry(ifa, &idev->addr_list, if_list) {
7042c12a74cSMichal Wrobel 		if (ifa->flags&IFA_F_TENTATIVE)
7052c12a74cSMichal Wrobel 			continue;
7061da177e4SLinus Torvalds 		if (idev->cnf.forwarding)
7071da177e4SLinus Torvalds 			addrconf_join_anycast(ifa);
7081da177e4SLinus Torvalds 		else
7091da177e4SLinus Torvalds 			addrconf_leave_anycast(ifa);
7101da177e4SLinus Torvalds 	}
711f3a1bfb1SNicolas Dichtel 	inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
712f3a1bfb1SNicolas Dichtel 				     dev->ifindex, &idev->cnf);
7131da177e4SLinus Torvalds }
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds 
716e186932bSPavel Emelyanov static void addrconf_forward_change(struct net *net, __s32 newf)
7171da177e4SLinus Torvalds {
7181da177e4SLinus Torvalds 	struct net_device *dev;
7191da177e4SLinus Torvalds 	struct inet6_dev *idev;
7201da177e4SLinus Torvalds 
7214acd4945SBen Hutchings 	for_each_netdev(net, dev) {
7221da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
7231da177e4SLinus Torvalds 		if (idev) {
724e186932bSPavel Emelyanov 			int changed = (!idev->cnf.forwarding) ^ (!newf);
725e186932bSPavel Emelyanov 			idev->cnf.forwarding = newf;
7261da177e4SLinus Torvalds 			if (changed)
7271da177e4SLinus Torvalds 				dev_forward_change(idev);
7281da177e4SLinus Torvalds 		}
7291da177e4SLinus Torvalds 	}
7301da177e4SLinus Torvalds }
731c8fecf22SPavel Emelyanov 
732013d97e9SFrancesco Ruggeri static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
733c8fecf22SPavel Emelyanov {
734bff16c2fSPavel Emelyanov 	struct net *net;
735013d97e9SFrancesco Ruggeri 	int old;
736013d97e9SFrancesco Ruggeri 
737013d97e9SFrancesco Ruggeri 	if (!rtnl_trylock())
738013d97e9SFrancesco Ruggeri 		return restart_syscall();
739bff16c2fSPavel Emelyanov 
740bff16c2fSPavel Emelyanov 	net = (struct net *)table->extra2;
741013d97e9SFrancesco Ruggeri 	old = *p;
742013d97e9SFrancesco Ruggeri 	*p = newf;
743c8fecf22SPavel Emelyanov 
744013d97e9SFrancesco Ruggeri 	if (p == &net->ipv6.devconf_dflt->forwarding) {
745f3a1bfb1SNicolas Dichtel 		if ((!newf) ^ (!old))
746f3a1bfb1SNicolas Dichtel 			inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
747f3a1bfb1SNicolas Dichtel 						     NETCONFA_IFINDEX_DEFAULT,
748f3a1bfb1SNicolas Dichtel 						     net->ipv6.devconf_dflt);
749013d97e9SFrancesco Ruggeri 		rtnl_unlock();
750013d97e9SFrancesco Ruggeri 		return 0;
75188af182eSEric W. Biederman 	}
752b325fddbSStephen Hemminger 
753e186932bSPavel Emelyanov 	if (p == &net->ipv6.devconf_all->forwarding) {
754e186932bSPavel Emelyanov 		net->ipv6.devconf_dflt->forwarding = newf;
755e186932bSPavel Emelyanov 		addrconf_forward_change(net, newf);
756f3a1bfb1SNicolas Dichtel 		if ((!newf) ^ (!old))
757f3a1bfb1SNicolas Dichtel 			inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
758f3a1bfb1SNicolas Dichtel 						     NETCONFA_IFINDEX_ALL,
759f3a1bfb1SNicolas Dichtel 						     net->ipv6.devconf_all);
760013d97e9SFrancesco Ruggeri 	} else if ((!newf) ^ (!old))
761c8fecf22SPavel Emelyanov 		dev_forward_change((struct inet6_dev *)table->extra1);
7620187bdfbSBen Hutchings 	rtnl_unlock();
763c8fecf22SPavel Emelyanov 
764013d97e9SFrancesco Ruggeri 	if (newf)
7657b4da532SDaniel Lezcano 		rt6_purge_dflt_routers(net);
766b325fddbSStephen Hemminger 	return 1;
767c8fecf22SPavel Emelyanov }
7681da177e4SLinus Torvalds #endif
7691da177e4SLinus Torvalds 
7705c578aedSstephen hemminger /* Nobody refers to this ifaddr, destroy it */
7711da177e4SLinus Torvalds void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
7721da177e4SLinus Torvalds {
773c2e21293Sstephen hemminger 	WARN_ON(!hlist_unhashed(&ifp->addr_lst));
774547b792cSIlpo Järvinen 
7751da177e4SLinus Torvalds #ifdef NET_REFCNT_DEBUG
77691df42beSJoe Perches 	pr_debug("%s\n", __func__);
7771da177e4SLinus Torvalds #endif
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds 	in6_dev_put(ifp->idev);
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	if (del_timer(&ifp->timer))
782e21e8467SStephen Hemminger 		pr_notice("Timer is still running, when freeing ifa=%p\n", ifp);
7831da177e4SLinus Torvalds 
784e9d3e084SHerbert Xu 	if (ifp->state != INET6_IFADDR_STATE_DEAD) {
785f3213831SJoe Perches 		pr_warn("Freeing alive inet6 address %p\n", ifp);
7861da177e4SLinus Torvalds 		return;
7871da177e4SLinus Torvalds 	}
78894e187c0SAmerigo Wang 	ip6_rt_put(ifp->rt);
7891da177e4SLinus Torvalds 
790e5785985SLai Jiangshan 	kfree_rcu(ifp, rcu);
7911da177e4SLinus Torvalds }
7921da177e4SLinus Torvalds 
793e55ffac6SBrian Haley static void
794e55ffac6SBrian Haley ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
795e55ffac6SBrian Haley {
796502a2ffdSstephen hemminger 	struct list_head *p;
7978a6ce0c0SYOSHIFUJI Hideaki 	int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
798e55ffac6SBrian Haley 
799e55ffac6SBrian Haley 	/*
800e55ffac6SBrian Haley 	 * Each device address list is sorted in order of scope -
801e55ffac6SBrian Haley 	 * global before linklocal.
802e55ffac6SBrian Haley 	 */
803502a2ffdSstephen hemminger 	list_for_each(p, &idev->addr_list) {
804502a2ffdSstephen hemminger 		struct inet6_ifaddr *ifa
805502a2ffdSstephen hemminger 			= list_entry(p, struct inet6_ifaddr, if_list);
8068a6ce0c0SYOSHIFUJI Hideaki 		if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
807e55ffac6SBrian Haley 			break;
808e55ffac6SBrian Haley 	}
809e55ffac6SBrian Haley 
810b54c9b98SDavid S. Miller 	list_add_tail(&ifp->if_list, p);
811e55ffac6SBrian Haley }
812e55ffac6SBrian Haley 
813ddbe5032SEric Dumazet static u32 inet6_addr_hash(const struct in6_addr *addr)
8143eb84f49SYOSHIFUJI Hideaki {
815ddbe5032SEric Dumazet 	return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
8163eb84f49SYOSHIFUJI Hideaki }
8173eb84f49SYOSHIFUJI Hideaki 
8181da177e4SLinus Torvalds /* On success it returns ifp with increased reference count */
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds static struct inet6_ifaddr *
8211da177e4SLinus Torvalds ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
822e431b8c0SJamal Hadi Salim 	      int scope, u32 flags)
8231da177e4SLinus Torvalds {
8241da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
8251da177e4SLinus Torvalds 	struct rt6_info *rt;
8263a88a81dSstephen hemminger 	unsigned int hash;
8271da177e4SLinus Torvalds 	int err = 0;
828d68b8270SYOSHIFUJI Hideaki 	int addr_type = ipv6_addr_type(addr);
829d68b8270SYOSHIFUJI Hideaki 
830d68b8270SYOSHIFUJI Hideaki 	if (addr_type == IPV6_ADDR_ANY ||
831d68b8270SYOSHIFUJI Hideaki 	    addr_type & IPV6_ADDR_MULTICAST ||
832d68b8270SYOSHIFUJI Hideaki 	    (!(idev->dev->flags & IFF_LOOPBACK) &&
833d68b8270SYOSHIFUJI Hideaki 	     addr_type & IPV6_ADDR_LOOPBACK))
834d68b8270SYOSHIFUJI Hideaki 		return ERR_PTR(-EADDRNOTAVAIL);
8351da177e4SLinus Torvalds 
8368814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
8371da177e4SLinus Torvalds 	if (idev->dead) {
8381da177e4SLinus Torvalds 		err = -ENODEV;			/*XXX*/
8391da177e4SLinus Torvalds 		goto out2;
8401da177e4SLinus Torvalds 	}
8411da177e4SLinus Torvalds 
84256d417b1SBrian Haley 	if (idev->cnf.disable_ipv6) {
8439bdd8d40SBrian Haley 		err = -EACCES;
8449bdd8d40SBrian Haley 		goto out2;
8459bdd8d40SBrian Haley 	}
8469bdd8d40SBrian Haley 
8475c578aedSstephen hemminger 	spin_lock(&addrconf_hash_lock);
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds 	/* Ignore adding duplicate addresses on an interface */
850c346dca1SYOSHIFUJI Hideaki 	if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
8511da177e4SLinus Torvalds 		ADBG(("ipv6_add_addr: already assigned\n"));
8521da177e4SLinus Torvalds 		err = -EEXIST;
8531da177e4SLinus Torvalds 		goto out;
8541da177e4SLinus Torvalds 	}
8551da177e4SLinus Torvalds 
856322f74a4SIngo Oeser 	ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 	if (ifa == NULL) {
8591da177e4SLinus Torvalds 		ADBG(("ipv6_add_addr: malloc failed\n"));
8601da177e4SLinus Torvalds 		err = -ENOBUFS;
8611da177e4SLinus Torvalds 		goto out;
8621da177e4SLinus Torvalds 	}
8631da177e4SLinus Torvalds 
8648f031519SDavid S. Miller 	rt = addrconf_dst_alloc(idev, addr, false);
8651da177e4SLinus Torvalds 	if (IS_ERR(rt)) {
8661da177e4SLinus Torvalds 		err = PTR_ERR(rt);
8671da177e4SLinus Torvalds 		goto out;
8681da177e4SLinus Torvalds 	}
8691da177e4SLinus Torvalds 
8704e3fd7a0SAlexey Dobriyan 	ifa->addr = *addr;
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 	spin_lock_init(&ifa->lock);
873e9d3e084SHerbert Xu 	spin_lock_init(&ifa->state_lock);
8741da177e4SLinus Torvalds 	init_timer(&ifa->timer);
875c2e21293Sstephen hemminger 	INIT_HLIST_NODE(&ifa->addr_lst);
8761da177e4SLinus Torvalds 	ifa->timer.data = (unsigned long) ifa;
8771da177e4SLinus Torvalds 	ifa->scope = scope;
8781da177e4SLinus Torvalds 	ifa->prefix_len = pfxlen;
8791da177e4SLinus Torvalds 	ifa->flags = flags | IFA_F_TENTATIVE;
8801da177e4SLinus Torvalds 	ifa->cstamp = ifa->tstamp = jiffies;
8811da177e4SLinus Torvalds 
88257f5f544SKeir Fraser 	ifa->rt = rt;
88357f5f544SKeir Fraser 
8841da177e4SLinus Torvalds 	ifa->idev = idev;
8851da177e4SLinus Torvalds 	in6_dev_hold(idev);
8861da177e4SLinus Torvalds 	/* For caller */
8871da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 	/* Add to big hash table */
890ddbe5032SEric Dumazet 	hash = inet6_addr_hash(addr);
8911da177e4SLinus Torvalds 
8925c578aedSstephen hemminger 	hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
8935c578aedSstephen hemminger 	spin_unlock(&addrconf_hash_lock);
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds 	write_lock(&idev->lock);
8961da177e4SLinus Torvalds 	/* Add to inet6_dev unicast addr list. */
897e55ffac6SBrian Haley 	ipv6_link_dev_addr(idev, ifa);
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
9001da177e4SLinus Torvalds 	if (ifa->flags&IFA_F_TEMPORARY) {
901372e6c8fSstephen hemminger 		list_add(&ifa->tmp_list, &idev->tempaddr_list);
9021da177e4SLinus Torvalds 		in6_ifa_hold(ifa);
9031da177e4SLinus Torvalds 	}
9041da177e4SLinus Torvalds #endif
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 	in6_ifa_hold(ifa);
9071da177e4SLinus Torvalds 	write_unlock(&idev->lock);
9081da177e4SLinus Torvalds out2:
9098814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
9101da177e4SLinus Torvalds 
911fd92833aSYOSHIFUJI Hideaki 	if (likely(err == 0))
912e041c683SAlan Stern 		atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
9131da177e4SLinus Torvalds 	else {
9141da177e4SLinus Torvalds 		kfree(ifa);
9151da177e4SLinus Torvalds 		ifa = ERR_PTR(err);
9161da177e4SLinus Torvalds 	}
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	return ifa;
9191da177e4SLinus Torvalds out:
9205c578aedSstephen hemminger 	spin_unlock(&addrconf_hash_lock);
9211da177e4SLinus Torvalds 	goto out2;
9221da177e4SLinus Torvalds }
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds /* This function wants to get referenced ifp and releases it before return */
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds static void ipv6_del_addr(struct inet6_ifaddr *ifp)
9271da177e4SLinus Torvalds {
928502a2ffdSstephen hemminger 	struct inet6_ifaddr *ifa, *ifn;
9291da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
9304c5ff6a6SHerbert Xu 	int state;
9311da177e4SLinus Torvalds 	int deleted = 0, onlink = 0;
9321da177e4SLinus Torvalds 	unsigned long expires = jiffies;
9331da177e4SLinus Torvalds 
9344c5ff6a6SHerbert Xu 	spin_lock_bh(&ifp->state_lock);
9354c5ff6a6SHerbert Xu 	state = ifp->state;
936e9d3e084SHerbert Xu 	ifp->state = INET6_IFADDR_STATE_DEAD;
9374c5ff6a6SHerbert Xu 	spin_unlock_bh(&ifp->state_lock);
9384c5ff6a6SHerbert Xu 
9394c5ff6a6SHerbert Xu 	if (state == INET6_IFADDR_STATE_DEAD)
9404c5ff6a6SHerbert Xu 		goto out;
9411da177e4SLinus Torvalds 
9425c578aedSstephen hemminger 	spin_lock_bh(&addrconf_hash_lock);
9435c578aedSstephen hemminger 	hlist_del_init_rcu(&ifp->addr_lst);
9445c578aedSstephen hemminger 	spin_unlock_bh(&addrconf_hash_lock);
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
9471da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
9481da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_TEMPORARY) {
949372e6c8fSstephen hemminger 		list_del(&ifp->tmp_list);
9501da177e4SLinus Torvalds 		if (ifp->ifpub) {
9511da177e4SLinus Torvalds 			in6_ifa_put(ifp->ifpub);
9521da177e4SLinus Torvalds 			ifp->ifpub = NULL;
9531da177e4SLinus Torvalds 		}
9541da177e4SLinus Torvalds 		__in6_ifa_put(ifp);
9551da177e4SLinus Torvalds 	}
9561da177e4SLinus Torvalds #endif
9571da177e4SLinus Torvalds 
958502a2ffdSstephen hemminger 	list_for_each_entry_safe(ifa, ifn, &idev->addr_list, if_list) {
9591da177e4SLinus Torvalds 		if (ifa == ifp) {
960502a2ffdSstephen hemminger 			list_del_init(&ifp->if_list);
9611da177e4SLinus Torvalds 			__in6_ifa_put(ifp);
962502a2ffdSstephen hemminger 
9631da177e4SLinus Torvalds 			if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
9641da177e4SLinus Torvalds 				break;
9651da177e4SLinus Torvalds 			deleted = 1;
9661d142804SKristian Slavov 			continue;
9671da177e4SLinus Torvalds 		} else if (ifp->flags & IFA_F_PERMANENT) {
9681da177e4SLinus Torvalds 			if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
9691da177e4SLinus Torvalds 					      ifp->prefix_len)) {
9701da177e4SLinus Torvalds 				if (ifa->flags & IFA_F_PERMANENT) {
9711da177e4SLinus Torvalds 					onlink = 1;
9721da177e4SLinus Torvalds 					if (deleted)
9731da177e4SLinus Torvalds 						break;
9741da177e4SLinus Torvalds 				} else {
9751da177e4SLinus Torvalds 					unsigned long lifetime;
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds 					if (!onlink)
9781da177e4SLinus Torvalds 						onlink = -1;
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 					spin_lock(&ifa->lock);
9814bed72e4SYOSHIFUJI Hideaki 
9824bed72e4SYOSHIFUJI Hideaki 					lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
9834bed72e4SYOSHIFUJI Hideaki 					/*
9844bed72e4SYOSHIFUJI Hideaki 					 * Note: Because this address is
9854bed72e4SYOSHIFUJI Hideaki 					 * not permanent, lifetime <
9864bed72e4SYOSHIFUJI Hideaki 					 * LONG_MAX / HZ here.
9874bed72e4SYOSHIFUJI Hideaki 					 */
9881da177e4SLinus Torvalds 					if (time_before(expires,
9891da177e4SLinus Torvalds 							ifa->tstamp + lifetime * HZ))
9901da177e4SLinus Torvalds 						expires = ifa->tstamp + lifetime * HZ;
9911da177e4SLinus Torvalds 					spin_unlock(&ifa->lock);
9921da177e4SLinus Torvalds 				}
9931da177e4SLinus Torvalds 			}
9941da177e4SLinus Torvalds 		}
9951da177e4SLinus Torvalds 	}
9961da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
9971da177e4SLinus Torvalds 
998b2238566SAndrey Vagin 	addrconf_del_timer(ifp);
999b2238566SAndrey Vagin 
10001da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_DELADDR, ifp);
10011da177e4SLinus Torvalds 
1002e041c683SAlan Stern 	atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	/*
10051da177e4SLinus Torvalds 	 * Purge or update corresponding prefix
10061da177e4SLinus Torvalds 	 *
10071da177e4SLinus Torvalds 	 * 1) we don't purge prefix here if address was not permanent.
10081da177e4SLinus Torvalds 	 *    prefix is managed by its own lifetime.
10091da177e4SLinus Torvalds 	 * 2) if there're no addresses, delete prefix.
10101da177e4SLinus Torvalds 	 * 3) if there're still other permanent address(es),
10111da177e4SLinus Torvalds 	 *    corresponding prefix is still permanent.
10121da177e4SLinus Torvalds 	 * 4) otherwise, update prefix lifetime to the
10131da177e4SLinus Torvalds 	 *    longest valid lifetime among the corresponding
10141da177e4SLinus Torvalds 	 *    addresses on the device.
10151da177e4SLinus Torvalds 	 *    Note: subsequent RA will update lifetime.
10161da177e4SLinus Torvalds 	 *
10171da177e4SLinus Torvalds 	 * --yoshfuji
10181da177e4SLinus Torvalds 	 */
10191da177e4SLinus Torvalds 	if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
10201da177e4SLinus Torvalds 		struct in6_addr prefix;
10211da177e4SLinus Torvalds 		struct rt6_info *rt;
10221da177e4SLinus Torvalds 
102364c6d08eSNicolas Dichtel 		ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
102464c6d08eSNicolas Dichtel 
102521caa662SRomain Kuntz 		rt = addrconf_get_prefix_route(&prefix,
102621caa662SRomain Kuntz 					       ifp->prefix_len,
102721caa662SRomain Kuntz 					       ifp->idev->dev,
102821caa662SRomain Kuntz 					       0, RTF_GATEWAY | RTF_DEFAULT);
102921caa662SRomain Kuntz 
103021caa662SRomain Kuntz 		if (rt) {
10311da177e4SLinus Torvalds 			if (onlink == 0) {
1032e0a1ad73SThomas Graf 				ip6_del_rt(rt);
10331da177e4SLinus Torvalds 				rt = NULL;
10341da177e4SLinus Torvalds 			} else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
10351716a961SGao feng 				rt6_set_expires(rt, expires);
10361da177e4SLinus Torvalds 			}
10371da177e4SLinus Torvalds 		}
103894e187c0SAmerigo Wang 		ip6_rt_put(rt);
10391da177e4SLinus Torvalds 	}
10401da177e4SLinus Torvalds 
1041c3968a85SDaniel Walter 	/* clean up prefsrc entries */
1042c3968a85SDaniel Walter 	rt6_remove_prefsrc(ifp);
10434c5ff6a6SHerbert Xu out:
10441da177e4SLinus Torvalds 	in6_ifa_put(ifp);
10451da177e4SLinus Torvalds }
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
10481da177e4SLinus Torvalds static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
10491da177e4SLinus Torvalds {
10501da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
10511da177e4SLinus Torvalds 	struct in6_addr addr, *tmpaddr;
105276f793e3SLorenzo Colitti 	unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
1053eac55bf9SBenoit Boissinot 	unsigned long regen_advance;
10541da177e4SLinus Torvalds 	int tmp_plen;
10551da177e4SLinus Torvalds 	int ret = 0;
10561da177e4SLinus Torvalds 	int max_addresses;
105795c385b4SNeil Horman 	u32 addr_flags;
105876f793e3SLorenzo Colitti 	unsigned long now = jiffies;
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds 	write_lock(&idev->lock);
10611da177e4SLinus Torvalds 	if (ift) {
10621da177e4SLinus Torvalds 		spin_lock_bh(&ift->lock);
10631da177e4SLinus Torvalds 		memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
10641da177e4SLinus Torvalds 		spin_unlock_bh(&ift->lock);
10651da177e4SLinus Torvalds 		tmpaddr = &addr;
10661da177e4SLinus Torvalds 	} else {
10671da177e4SLinus Torvalds 		tmpaddr = NULL;
10681da177e4SLinus Torvalds 	}
10691da177e4SLinus Torvalds retry:
10701da177e4SLinus Torvalds 	in6_dev_hold(idev);
10711da177e4SLinus Torvalds 	if (idev->cnf.use_tempaddr <= 0) {
10721da177e4SLinus Torvalds 		write_unlock(&idev->lock);
1073f3213831SJoe Perches 		pr_info("%s: use_tempaddr is disabled\n", __func__);
10741da177e4SLinus Torvalds 		in6_dev_put(idev);
10751da177e4SLinus Torvalds 		ret = -1;
10761da177e4SLinus Torvalds 		goto out;
10771da177e4SLinus Torvalds 	}
10781da177e4SLinus Torvalds 	spin_lock_bh(&ifp->lock);
10791da177e4SLinus Torvalds 	if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
10801da177e4SLinus Torvalds 		idev->cnf.use_tempaddr = -1;	/*XXX*/
10811da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
10821da177e4SLinus Torvalds 		write_unlock(&idev->lock);
1083f3213831SJoe Perches 		pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1084f3213831SJoe Perches 			__func__);
10851da177e4SLinus Torvalds 		in6_dev_put(idev);
10861da177e4SLinus Torvalds 		ret = -1;
10871da177e4SLinus Torvalds 		goto out;
10881da177e4SLinus Torvalds 	}
10891da177e4SLinus Torvalds 	in6_ifa_hold(ifp);
10901da177e4SLinus Torvalds 	memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
1091eb7e0575SSorin Dumitru 	__ipv6_try_regen_rndid(idev, tmpaddr);
10921da177e4SLinus Torvalds 	memcpy(&addr.s6_addr[8], idev->rndid, 8);
109376f793e3SLorenzo Colitti 	age = (now - ifp->tstamp) / HZ;
10941da177e4SLinus Torvalds 	tmp_valid_lft = min_t(__u32,
10951da177e4SLinus Torvalds 			      ifp->valid_lft,
10967a876b0eSGlenn Wurster 			      idev->cnf.temp_valid_lft + age);
10971da177e4SLinus Torvalds 	tmp_prefered_lft = min_t(__u32,
10981da177e4SLinus Torvalds 				 ifp->prefered_lft,
10997a876b0eSGlenn Wurster 				 idev->cnf.temp_prefered_lft + age -
1100784e2710SBen Hutchings 				 idev->cnf.max_desync_factor);
11011da177e4SLinus Torvalds 	tmp_plen = ifp->prefix_len;
11021da177e4SLinus Torvalds 	max_addresses = idev->cnf.max_addresses;
11031da177e4SLinus Torvalds 	tmp_tstamp = ifp->tstamp;
11041da177e4SLinus Torvalds 	spin_unlock_bh(&ifp->lock);
11051da177e4SLinus Torvalds 
1106eac55bf9SBenoit Boissinot 	regen_advance = idev->cnf.regen_max_retry *
1107eac55bf9SBenoit Boissinot 	                idev->cnf.dad_transmits *
1108eac55bf9SBenoit Boissinot 	                idev->nd_parms->retrans_time / HZ;
11091da177e4SLinus Torvalds 	write_unlock(&idev->lock);
111095c385b4SNeil Horman 
1111eac55bf9SBenoit Boissinot 	/* A temporary address is created only if this calculated Preferred
1112eac55bf9SBenoit Boissinot 	 * Lifetime is greater than REGEN_ADVANCE time units.  In particular,
1113eac55bf9SBenoit Boissinot 	 * an implementation must not create a temporary address with a zero
1114eac55bf9SBenoit Boissinot 	 * Preferred Lifetime.
1115eac55bf9SBenoit Boissinot 	 */
1116eac55bf9SBenoit Boissinot 	if (tmp_prefered_lft <= regen_advance) {
1117eac55bf9SBenoit Boissinot 		in6_ifa_put(ifp);
1118eac55bf9SBenoit Boissinot 		in6_dev_put(idev);
1119eac55bf9SBenoit Boissinot 		ret = -1;
1120eac55bf9SBenoit Boissinot 		goto out;
1121eac55bf9SBenoit Boissinot 	}
1122eac55bf9SBenoit Boissinot 
112395c385b4SNeil Horman 	addr_flags = IFA_F_TEMPORARY;
112495c385b4SNeil Horman 	/* set in addrconf_prefix_rcv() */
112595c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
112695c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
112795c385b4SNeil Horman 
11281da177e4SLinus Torvalds 	ift = !max_addresses ||
11291da177e4SLinus Torvalds 	      ipv6_count_addresses(idev) < max_addresses ?
11301da177e4SLinus Torvalds 		ipv6_add_addr(idev, &addr, tmp_plen,
113195c385b4SNeil Horman 			      ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK,
113295c385b4SNeil Horman 			      addr_flags) : NULL;
11333f0d2ba0SYOSHIFUJI Hideaki / 吉藤英明 	if (IS_ERR_OR_NULL(ift)) {
11341da177e4SLinus Torvalds 		in6_ifa_put(ifp);
11351da177e4SLinus Torvalds 		in6_dev_put(idev);
1136f3213831SJoe Perches 		pr_info("%s: retry temporary address regeneration\n", __func__);
11371da177e4SLinus Torvalds 		tmpaddr = &addr;
11381da177e4SLinus Torvalds 		write_lock(&idev->lock);
11391da177e4SLinus Torvalds 		goto retry;
11401da177e4SLinus Torvalds 	}
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 	spin_lock_bh(&ift->lock);
11431da177e4SLinus Torvalds 	ift->ifpub = ifp;
11441da177e4SLinus Torvalds 	ift->valid_lft = tmp_valid_lft;
11451da177e4SLinus Torvalds 	ift->prefered_lft = tmp_prefered_lft;
114676f793e3SLorenzo Colitti 	ift->cstamp = now;
11471da177e4SLinus Torvalds 	ift->tstamp = tmp_tstamp;
11481da177e4SLinus Torvalds 	spin_unlock_bh(&ift->lock);
11491da177e4SLinus Torvalds 
1150cf22f9a2SDavid S. Miller 	addrconf_dad_start(ift);
11511da177e4SLinus Torvalds 	in6_ifa_put(ift);
11521da177e4SLinus Torvalds 	in6_dev_put(idev);
11531da177e4SLinus Torvalds out:
11541da177e4SLinus Torvalds 	return ret;
11551da177e4SLinus Torvalds }
11561da177e4SLinus Torvalds #endif
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds /*
1159072047e4SYOSHIFUJI Hideaki  *	Choose an appropriate source address (RFC3484)
11601da177e4SLinus Torvalds  */
1161a9b05723SYOSHIFUJI Hideaki enum {
1162a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_INIT = 0,
1163a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LOCAL,
1164a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_SCOPE,
1165a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFERRED,
1166a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1167a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_HOA,
1168a9b05723SYOSHIFUJI Hideaki #endif
1169a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_OIF,
1170a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_LABEL,
1171a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_PRIVACY
1172a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PRIVACY,
1173a9b05723SYOSHIFUJI Hideaki #endif
1174a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_ORCHID,
1175a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_PREFIX,
1176a9b05723SYOSHIFUJI Hideaki 	IPV6_SADDR_RULE_MAX
1177072047e4SYOSHIFUJI Hideaki };
1178072047e4SYOSHIFUJI Hideaki 
1179a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_score {
1180a9b05723SYOSHIFUJI Hideaki 	int			rule;
1181a9b05723SYOSHIFUJI Hideaki 	int			addr_type;
1182a9b05723SYOSHIFUJI Hideaki 	struct inet6_ifaddr	*ifa;
1183a9b05723SYOSHIFUJI Hideaki 	DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1184a9b05723SYOSHIFUJI Hideaki 	int			scopedist;
1185a9b05723SYOSHIFUJI Hideaki 	int			matchlen;
1186a9b05723SYOSHIFUJI Hideaki };
1187a9b05723SYOSHIFUJI Hideaki 
1188a9b05723SYOSHIFUJI Hideaki struct ipv6_saddr_dst {
11899acd9f3aSYOSHIFUJI Hideaki 	const struct in6_addr *addr;
1190a9b05723SYOSHIFUJI Hideaki 	int ifindex;
1191a9b05723SYOSHIFUJI Hideaki 	int scope;
1192a9b05723SYOSHIFUJI Hideaki 	int label;
11937cbca67cSYOSHIFUJI Hideaki 	unsigned int prefs;
1194a9b05723SYOSHIFUJI Hideaki };
1195072047e4SYOSHIFUJI Hideaki 
1196b6f99a21SDave Jones static inline int ipv6_saddr_preferred(int type)
11971da177e4SLinus Torvalds {
119845bb0060SUlrich Weber 	if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
1199072047e4SYOSHIFUJI Hideaki 		return 1;
1200072047e4SYOSHIFUJI Hideaki 	return 0;
12011da177e4SLinus Torvalds }
12021da177e4SLinus Torvalds 
12033de23255SBenjamin Thery static int ipv6_get_saddr_eval(struct net *net,
12043de23255SBenjamin Thery 			       struct ipv6_saddr_score *score,
1205a9b05723SYOSHIFUJI Hideaki 			       struct ipv6_saddr_dst *dst,
1206a9b05723SYOSHIFUJI Hideaki 			       int i)
1207a9b05723SYOSHIFUJI Hideaki {
1208a9b05723SYOSHIFUJI Hideaki 	int ret;
1209a9b05723SYOSHIFUJI Hideaki 
1210a9b05723SYOSHIFUJI Hideaki 	if (i <= score->rule) {
1211a9b05723SYOSHIFUJI Hideaki 		switch (i) {
1212a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_SCOPE:
1213a9b05723SYOSHIFUJI Hideaki 			ret = score->scopedist;
1214a9b05723SYOSHIFUJI Hideaki 			break;
1215a9b05723SYOSHIFUJI Hideaki 		case IPV6_SADDR_RULE_PREFIX:
1216a9b05723SYOSHIFUJI Hideaki 			ret = score->matchlen;
1217a9b05723SYOSHIFUJI Hideaki 			break;
1218a9b05723SYOSHIFUJI Hideaki 		default:
1219a9b05723SYOSHIFUJI Hideaki 			ret = !!test_bit(i, score->scorebits);
1220a9b05723SYOSHIFUJI Hideaki 		}
1221a9b05723SYOSHIFUJI Hideaki 		goto out;
1222a9b05723SYOSHIFUJI Hideaki 	}
1223a9b05723SYOSHIFUJI Hideaki 
1224a9b05723SYOSHIFUJI Hideaki 	switch (i) {
1225a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_INIT:
1226a9b05723SYOSHIFUJI Hideaki 		/* Rule 0: remember if hiscore is not ready yet */
1227a9b05723SYOSHIFUJI Hideaki 		ret = !!score->ifa;
1228a9b05723SYOSHIFUJI Hideaki 		break;
1229a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LOCAL:
1230a9b05723SYOSHIFUJI Hideaki 		/* Rule 1: Prefer same address */
1231a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1232a9b05723SYOSHIFUJI Hideaki 		break;
1233a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_SCOPE:
1234a9b05723SYOSHIFUJI Hideaki 		/* Rule 2: Prefer appropriate scope
1235a9b05723SYOSHIFUJI Hideaki 		 *
1236a9b05723SYOSHIFUJI Hideaki 		 *      ret
1237a9b05723SYOSHIFUJI Hideaki 		 *       ^
1238a9b05723SYOSHIFUJI Hideaki 		 *    -1 |  d 15
1239a9b05723SYOSHIFUJI Hideaki 		 *    ---+--+-+---> scope
1240a9b05723SYOSHIFUJI Hideaki 		 *       |
1241a9b05723SYOSHIFUJI Hideaki 		 *       |             d is scope of the destination.
1242a9b05723SYOSHIFUJI Hideaki 		 *  B-d  |  \
1243a9b05723SYOSHIFUJI Hideaki 		 *       |   \      <- smaller scope is better if
1244a9b05723SYOSHIFUJI Hideaki 		 *  B-15 |    \        if scope is enough for destinaion.
1245a9b05723SYOSHIFUJI Hideaki 		 *       |             ret = B - scope (-1 <= scope >= d <= 15).
1246a9b05723SYOSHIFUJI Hideaki 		 * d-C-1 | /
1247a9b05723SYOSHIFUJI Hideaki 		 *       |/         <- greater is better
1248a9b05723SYOSHIFUJI Hideaki 		 *   -C  /             if scope is not enough for destination.
1249a9b05723SYOSHIFUJI Hideaki 		 *      /|             ret = scope - C (-1 <= d < scope <= 15).
1250a9b05723SYOSHIFUJI Hideaki 		 *
1251a9b05723SYOSHIFUJI Hideaki 		 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1252a9b05723SYOSHIFUJI Hideaki 		 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1253a9b05723SYOSHIFUJI Hideaki 		 * Assume B = 0 and we get C > 29.
1254a9b05723SYOSHIFUJI Hideaki 		 */
1255a9b05723SYOSHIFUJI Hideaki 		ret = __ipv6_addr_src_scope(score->addr_type);
1256a9b05723SYOSHIFUJI Hideaki 		if (ret >= dst->scope)
1257a9b05723SYOSHIFUJI Hideaki 			ret = -ret;
1258a9b05723SYOSHIFUJI Hideaki 		else
1259a9b05723SYOSHIFUJI Hideaki 			ret -= 128;	/* 30 is enough */
1260a9b05723SYOSHIFUJI Hideaki 		score->scopedist = ret;
1261a9b05723SYOSHIFUJI Hideaki 		break;
1262a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFERRED:
1263a9b05723SYOSHIFUJI Hideaki 		/* Rule 3: Avoid deprecated and optimistic addresses */
1264a9b05723SYOSHIFUJI Hideaki 		ret = ipv6_saddr_preferred(score->addr_type) ||
1265a9b05723SYOSHIFUJI Hideaki 		      !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC));
1266a9b05723SYOSHIFUJI Hideaki 		break;
1267a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MIP6
1268a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_HOA:
12697cbca67cSYOSHIFUJI Hideaki 	    {
1270a9b05723SYOSHIFUJI Hideaki 		/* Rule 4: Prefer home address */
12717cbca67cSYOSHIFUJI Hideaki 		int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
12727cbca67cSYOSHIFUJI Hideaki 		ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
1273a9b05723SYOSHIFUJI Hideaki 		break;
12747cbca67cSYOSHIFUJI Hideaki 	    }
1275a9b05723SYOSHIFUJI Hideaki #endif
1276a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_OIF:
1277a9b05723SYOSHIFUJI Hideaki 		/* Rule 5: Prefer outgoing interface */
1278a9b05723SYOSHIFUJI Hideaki 		ret = (!dst->ifindex ||
1279a9b05723SYOSHIFUJI Hideaki 		       dst->ifindex == score->ifa->idev->dev->ifindex);
1280a9b05723SYOSHIFUJI Hideaki 		break;
1281a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_LABEL:
1282a9b05723SYOSHIFUJI Hideaki 		/* Rule 6: Prefer matching label */
12833de23255SBenjamin Thery 		ret = ipv6_addr_label(net,
12843de23255SBenjamin Thery 				      &score->ifa->addr, score->addr_type,
1285a9b05723SYOSHIFUJI Hideaki 				      score->ifa->idev->dev->ifindex) == dst->label;
1286a9b05723SYOSHIFUJI Hideaki 		break;
1287a9b05723SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_PRIVACY
1288a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PRIVACY:
12897cbca67cSYOSHIFUJI Hideaki 	    {
1290a9b05723SYOSHIFUJI Hideaki 		/* Rule 7: Prefer public address
129125985edcSLucas De Marchi 		 * Note: prefer temporary address if use_tempaddr >= 2
1292a9b05723SYOSHIFUJI Hideaki 		 */
12937cbca67cSYOSHIFUJI Hideaki 		int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
12947cbca67cSYOSHIFUJI Hideaki 				!!(dst->prefs & IPV6_PREFER_SRC_TMP) :
12957cbca67cSYOSHIFUJI Hideaki 				score->ifa->idev->cnf.use_tempaddr >= 2;
12967cbca67cSYOSHIFUJI Hideaki 		ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1297a9b05723SYOSHIFUJI Hideaki 		break;
12987cbca67cSYOSHIFUJI Hideaki 	    }
1299a9b05723SYOSHIFUJI Hideaki #endif
1300a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_ORCHID:
1301a9b05723SYOSHIFUJI Hideaki 		/* Rule 8-: Prefer ORCHID vs ORCHID or
1302a9b05723SYOSHIFUJI Hideaki 		 *	    non-ORCHID vs non-ORCHID
1303a9b05723SYOSHIFUJI Hideaki 		 */
1304a9b05723SYOSHIFUJI Hideaki 		ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1305a9b05723SYOSHIFUJI Hideaki 			ipv6_addr_orchid(dst->addr));
1306a9b05723SYOSHIFUJI Hideaki 		break;
1307a9b05723SYOSHIFUJI Hideaki 	case IPV6_SADDR_RULE_PREFIX:
1308a9b05723SYOSHIFUJI Hideaki 		/* Rule 8: Use longest matching prefix */
130991b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
131091b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		if (ret > score->ifa->prefix_len)
131191b4b04fSYOSHIFUJI Hideaki / 吉藤英明 			ret = score->ifa->prefix_len;
131291b4b04fSYOSHIFUJI Hideaki / 吉藤英明 		score->matchlen = ret;
1313a9b05723SYOSHIFUJI Hideaki 		break;
1314a9b05723SYOSHIFUJI Hideaki 	default:
1315a9b05723SYOSHIFUJI Hideaki 		ret = 0;
1316a9b05723SYOSHIFUJI Hideaki 	}
1317a9b05723SYOSHIFUJI Hideaki 
1318a9b05723SYOSHIFUJI Hideaki 	if (ret)
1319a9b05723SYOSHIFUJI Hideaki 		__set_bit(i, score->scorebits);
1320a9b05723SYOSHIFUJI Hideaki 	score->rule = i;
1321a9b05723SYOSHIFUJI Hideaki out:
1322a9b05723SYOSHIFUJI Hideaki 	return ret;
1323a9b05723SYOSHIFUJI Hideaki }
1324a9b05723SYOSHIFUJI Hideaki 
1325b3f644fcSPatrick McHardy int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
13269acd9f3aSYOSHIFUJI Hideaki 		       const struct in6_addr *daddr, unsigned int prefs,
13277cbca67cSYOSHIFUJI Hideaki 		       struct in6_addr *saddr)
13281da177e4SLinus Torvalds {
1329a9b05723SYOSHIFUJI Hideaki 	struct ipv6_saddr_score scores[2],
1330a9b05723SYOSHIFUJI Hideaki 				*score = &scores[0], *hiscore = &scores[1];
1331a9b05723SYOSHIFUJI Hideaki 	struct ipv6_saddr_dst dst;
1332072047e4SYOSHIFUJI Hideaki 	struct net_device *dev;
1333a9b05723SYOSHIFUJI Hideaki 	int dst_type;
13341da177e4SLinus Torvalds 
1335a9b05723SYOSHIFUJI Hideaki 	dst_type = __ipv6_addr_type(daddr);
1336a9b05723SYOSHIFUJI Hideaki 	dst.addr = daddr;
1337a9b05723SYOSHIFUJI Hideaki 	dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1338a9b05723SYOSHIFUJI Hideaki 	dst.scope = __ipv6_addr_src_scope(dst_type);
13393de23255SBenjamin Thery 	dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
13407cbca67cSYOSHIFUJI Hideaki 	dst.prefs = prefs;
1341a9b05723SYOSHIFUJI Hideaki 
1342a9b05723SYOSHIFUJI Hideaki 	hiscore->rule = -1;
1343a9b05723SYOSHIFUJI Hideaki 	hiscore->ifa = NULL;
13441da177e4SLinus Torvalds 
13458814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
1346072047e4SYOSHIFUJI Hideaki 
1347c6d14c84SEric Dumazet 	for_each_netdev_rcu(net, dev) {
1348072047e4SYOSHIFUJI Hideaki 		struct inet6_dev *idev;
1349072047e4SYOSHIFUJI Hideaki 
1350a9b05723SYOSHIFUJI Hideaki 		/* Candidate Source Address (section 4)
1351072047e4SYOSHIFUJI Hideaki 		 *  - multicast and link-local destination address,
1352072047e4SYOSHIFUJI Hideaki 		 *    the set of candidate source address MUST only
1353072047e4SYOSHIFUJI Hideaki 		 *    include addresses assigned to interfaces
1354072047e4SYOSHIFUJI Hideaki 		 *    belonging to the same link as the outgoing
1355072047e4SYOSHIFUJI Hideaki 		 *    interface.
1356072047e4SYOSHIFUJI Hideaki 		 * (- For site-local destination addresses, the
1357072047e4SYOSHIFUJI Hideaki 		 *    set of candidate source addresses MUST only
1358072047e4SYOSHIFUJI Hideaki 		 *    include addresses assigned to interfaces
1359072047e4SYOSHIFUJI Hideaki 		 *    belonging to the same site as the outgoing
1360072047e4SYOSHIFUJI Hideaki 		 *    interface.)
1361072047e4SYOSHIFUJI Hideaki 		 */
1362a9b05723SYOSHIFUJI Hideaki 		if (((dst_type & IPV6_ADDR_MULTICAST) ||
1363a9b05723SYOSHIFUJI Hideaki 		     dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
1364a9b05723SYOSHIFUJI Hideaki 		    dst.ifindex && dev->ifindex != dst.ifindex)
1365072047e4SYOSHIFUJI Hideaki 			continue;
1366072047e4SYOSHIFUJI Hideaki 
13671da177e4SLinus Torvalds 		idev = __in6_dev_get(dev);
1368072047e4SYOSHIFUJI Hideaki 		if (!idev)
1369072047e4SYOSHIFUJI Hideaki 			continue;
1370072047e4SYOSHIFUJI Hideaki 
13711da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
1372502a2ffdSstephen hemminger 		list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1373a9b05723SYOSHIFUJI Hideaki 			int i;
1374072047e4SYOSHIFUJI Hideaki 
1375a9b05723SYOSHIFUJI Hideaki 			/*
13766b3ae80aSYOSHIFUJI Hideaki 			 * - Tentative Address (RFC2462 section 5.4)
13776b3ae80aSYOSHIFUJI Hideaki 			 *  - A tentative address is not considered
13786b3ae80aSYOSHIFUJI Hideaki 			 *    "assigned to an interface" in the traditional
137995c385b4SNeil Horman 			 *    sense, unless it is also flagged as optimistic.
13806b3ae80aSYOSHIFUJI Hideaki 			 * - Candidate Source Address (section 4)
1381072047e4SYOSHIFUJI Hideaki 			 *  - In any case, anycast addresses, multicast
1382072047e4SYOSHIFUJI Hideaki 			 *    addresses, and the unspecified address MUST
1383072047e4SYOSHIFUJI Hideaki 			 *    NOT be included in a candidate set.
1384072047e4SYOSHIFUJI Hideaki 			 */
1385a9b05723SYOSHIFUJI Hideaki 			if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1386a9b05723SYOSHIFUJI Hideaki 			    (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
13876b3ae80aSYOSHIFUJI Hideaki 				continue;
1388a9b05723SYOSHIFUJI Hideaki 
1389a9b05723SYOSHIFUJI Hideaki 			score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1390a9b05723SYOSHIFUJI Hideaki 
1391a9b05723SYOSHIFUJI Hideaki 			if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1392a9b05723SYOSHIFUJI Hideaki 				     score->addr_type & IPV6_ADDR_MULTICAST)) {
1393072047e4SYOSHIFUJI Hideaki 				LIMIT_NETDEBUG(KERN_DEBUG
1394072047e4SYOSHIFUJI Hideaki 					       "ADDRCONF: unspecified / multicast address "
1395072047e4SYOSHIFUJI Hideaki 					       "assigned as unicast address on %s",
1396072047e4SYOSHIFUJI Hideaki 					       dev->name);
13971da177e4SLinus Torvalds 				continue;
1398072047e4SYOSHIFUJI Hideaki 			}
1399072047e4SYOSHIFUJI Hideaki 
1400a9b05723SYOSHIFUJI Hideaki 			score->rule = -1;
1401a9b05723SYOSHIFUJI Hideaki 			bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1402072047e4SYOSHIFUJI Hideaki 
1403a9b05723SYOSHIFUJI Hideaki 			for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1404a9b05723SYOSHIFUJI Hideaki 				int minihiscore, miniscore;
1405072047e4SYOSHIFUJI Hideaki 
14063de23255SBenjamin Thery 				minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i);
14073de23255SBenjamin Thery 				miniscore = ipv6_get_saddr_eval(net, score, &dst, i);
1408072047e4SYOSHIFUJI Hideaki 
1409a9b05723SYOSHIFUJI Hideaki 				if (minihiscore > miniscore) {
1410a9b05723SYOSHIFUJI Hideaki 					if (i == IPV6_SADDR_RULE_SCOPE &&
1411a9b05723SYOSHIFUJI Hideaki 					    score->scopedist > 0) {
1412a9b05723SYOSHIFUJI Hideaki 						/*
1413a9b05723SYOSHIFUJI Hideaki 						 * special case:
1414a9b05723SYOSHIFUJI Hideaki 						 * each remaining entry
1415a9b05723SYOSHIFUJI Hideaki 						 * has too small (not enough)
1416a9b05723SYOSHIFUJI Hideaki 						 * scope, because ifa entries
1417a9b05723SYOSHIFUJI Hideaki 						 * are sorted by their scope
1418a9b05723SYOSHIFUJI Hideaki 						 * values.
1419072047e4SYOSHIFUJI Hideaki 						 */
1420a9b05723SYOSHIFUJI Hideaki 						goto try_nextdev;
1421072047e4SYOSHIFUJI Hideaki 					}
1422a9b05723SYOSHIFUJI Hideaki 					break;
1423a9b05723SYOSHIFUJI Hideaki 				} else if (minihiscore < miniscore) {
1424a9b05723SYOSHIFUJI Hideaki 					if (hiscore->ifa)
1425a9b05723SYOSHIFUJI Hideaki 						in6_ifa_put(hiscore->ifa);
142699cd07a5SJuha-Matti Tapio 
1427a9b05723SYOSHIFUJI Hideaki 					in6_ifa_hold(score->ifa);
1428072047e4SYOSHIFUJI Hideaki 
1429a0bffffcSIlpo Järvinen 					swap(hiscore, score);
1430a9b05723SYOSHIFUJI Hideaki 
1431a9b05723SYOSHIFUJI Hideaki 					/* restore our iterator */
1432a9b05723SYOSHIFUJI Hideaki 					score->ifa = hiscore->ifa;
1433a9b05723SYOSHIFUJI Hideaki 
1434a9b05723SYOSHIFUJI Hideaki 					break;
14351da177e4SLinus Torvalds 				}
1436a9b05723SYOSHIFUJI Hideaki 			}
1437a9b05723SYOSHIFUJI Hideaki 		}
1438a9b05723SYOSHIFUJI Hideaki try_nextdev:
14391da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
14401da177e4SLinus Torvalds 	}
14418814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
14421da177e4SLinus Torvalds 
1443a9b05723SYOSHIFUJI Hideaki 	if (!hiscore->ifa)
1444072047e4SYOSHIFUJI Hideaki 		return -EADDRNOTAVAIL;
14451da177e4SLinus Torvalds 
14464e3fd7a0SAlexey Dobriyan 	*saddr = hiscore->ifa->addr;
1447a9b05723SYOSHIFUJI Hideaki 	in6_ifa_put(hiscore->ifa);
1448072047e4SYOSHIFUJI Hideaki 	return 0;
14491da177e4SLinus Torvalds }
14505e5f3f0fSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_dev_get_saddr);
14511da177e4SLinus Torvalds 
145295c385b4SNeil Horman int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
145395c385b4SNeil Horman 		    unsigned char banned_flags)
14541da177e4SLinus Torvalds {
14551da177e4SLinus Torvalds 	struct inet6_dev *idev;
14561da177e4SLinus Torvalds 	int err = -EADDRNOTAVAIL;
14571da177e4SLinus Torvalds 
14588814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
1459e21e8467SStephen Hemminger 	idev = __in6_dev_get(dev);
1460e21e8467SStephen Hemminger 	if (idev) {
14611da177e4SLinus Torvalds 		struct inet6_ifaddr *ifp;
14621da177e4SLinus Torvalds 
14631da177e4SLinus Torvalds 		read_lock_bh(&idev->lock);
1464502a2ffdSstephen hemminger 		list_for_each_entry(ifp, &idev->addr_list, if_list) {
1465502a2ffdSstephen hemminger 			if (ifp->scope == IFA_LINK &&
1466502a2ffdSstephen hemminger 			    !(ifp->flags & banned_flags)) {
14674e3fd7a0SAlexey Dobriyan 				*addr = ifp->addr;
14681da177e4SLinus Torvalds 				err = 0;
14691da177e4SLinus Torvalds 				break;
14701da177e4SLinus Torvalds 			}
14711da177e4SLinus Torvalds 		}
14721da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
14731da177e4SLinus Torvalds 	}
14748814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
14751da177e4SLinus Torvalds 	return err;
14761da177e4SLinus Torvalds }
14771da177e4SLinus Torvalds 
14781da177e4SLinus Torvalds static int ipv6_count_addresses(struct inet6_dev *idev)
14791da177e4SLinus Torvalds {
14801da177e4SLinus Torvalds 	int cnt = 0;
14811da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
14821da177e4SLinus Torvalds 
14831da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
1484502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list)
14851da177e4SLinus Torvalds 		cnt++;
14861da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
14871da177e4SLinus Torvalds 	return cnt;
14881da177e4SLinus Torvalds }
14891da177e4SLinus Torvalds 
1490b71d1d42SEric Dumazet int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
1491bfeade08SDaniel Lezcano 		  struct net_device *dev, int strict)
14921da177e4SLinus Torvalds {
1493eedf042aSStephen Hemminger 	struct inet6_ifaddr *ifp;
1494ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
14951da177e4SLinus Torvalds 
14965c578aedSstephen hemminger 	rcu_read_lock_bh();
1497b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
1498878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
1499bfeade08SDaniel Lezcano 			continue;
15001da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr) &&
1501eedf042aSStephen Hemminger 		    !(ifp->flags&IFA_F_TENTATIVE) &&
1502eedf042aSStephen Hemminger 		    (dev == NULL || ifp->idev->dev == dev ||
1503eedf042aSStephen Hemminger 		     !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
15045c578aedSstephen hemminger 			rcu_read_unlock_bh();
1505eedf042aSStephen Hemminger 			return 1;
1506eedf042aSStephen Hemminger 		}
1507eedf042aSStephen Hemminger 	}
15085c578aedSstephen hemminger 
1509eedf042aSStephen Hemminger 	rcu_read_unlock_bh();
1510eedf042aSStephen Hemminger 	return 0;
15111da177e4SLinus Torvalds }
15127159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_addr);
15137159039aSYOSHIFUJI Hideaki 
15143e81c6daSDavid S. Miller static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
151506bfe655SDaniel Lezcano 			       struct net_device *dev)
15161da177e4SLinus Torvalds {
1517ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
15181da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
15191da177e4SLinus Torvalds 
1520b67bfe0dSSasha Levin 	hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
1521878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
152206bfe655SDaniel Lezcano 			continue;
15231da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
15241da177e4SLinus Torvalds 			if (dev == NULL || ifp->idev->dev == dev)
15253e81c6daSDavid S. Miller 				return true;
15261da177e4SLinus Torvalds 		}
15271da177e4SLinus Torvalds 	}
15283e81c6daSDavid S. Miller 	return false;
15291da177e4SLinus Torvalds }
15301da177e4SLinus Torvalds 
1531b71d1d42SEric Dumazet int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
153252eeeb84SYOSHIFUJI Hideaki {
153352eeeb84SYOSHIFUJI Hideaki 	struct inet6_dev *idev;
153452eeeb84SYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifa;
153552eeeb84SYOSHIFUJI Hideaki 	int	onlink;
153652eeeb84SYOSHIFUJI Hideaki 
153752eeeb84SYOSHIFUJI Hideaki 	onlink = 0;
153852eeeb84SYOSHIFUJI Hideaki 	rcu_read_lock();
153952eeeb84SYOSHIFUJI Hideaki 	idev = __in6_dev_get(dev);
154052eeeb84SYOSHIFUJI Hideaki 	if (idev) {
154152eeeb84SYOSHIFUJI Hideaki 		read_lock_bh(&idev->lock);
1542502a2ffdSstephen hemminger 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
154352eeeb84SYOSHIFUJI Hideaki 			onlink = ipv6_prefix_equal(addr, &ifa->addr,
154452eeeb84SYOSHIFUJI Hideaki 						   ifa->prefix_len);
154552eeeb84SYOSHIFUJI Hideaki 			if (onlink)
154652eeeb84SYOSHIFUJI Hideaki 				break;
154752eeeb84SYOSHIFUJI Hideaki 		}
154852eeeb84SYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
154952eeeb84SYOSHIFUJI Hideaki 	}
155052eeeb84SYOSHIFUJI Hideaki 	rcu_read_unlock();
155152eeeb84SYOSHIFUJI Hideaki 	return onlink;
155252eeeb84SYOSHIFUJI Hideaki }
155352eeeb84SYOSHIFUJI Hideaki EXPORT_SYMBOL(ipv6_chk_prefix);
155452eeeb84SYOSHIFUJI Hideaki 
15559acd9f3aSYOSHIFUJI Hideaki struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
15561cab3da6SDaniel Lezcano 				     struct net_device *dev, int strict)
15571da177e4SLinus Torvalds {
1558b79d1d54SDavid S. Miller 	struct inet6_ifaddr *ifp, *result = NULL;
1559ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
15601da177e4SLinus Torvalds 
15615c578aedSstephen hemminger 	rcu_read_lock_bh();
1562b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
1563878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
15641cab3da6SDaniel Lezcano 			continue;
15651da177e4SLinus Torvalds 		if (ipv6_addr_equal(&ifp->addr, addr)) {
15661da177e4SLinus Torvalds 			if (dev == NULL || ifp->idev->dev == dev ||
15671da177e4SLinus Torvalds 			    !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1568b79d1d54SDavid S. Miller 				result = ifp;
15691da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
15701da177e4SLinus Torvalds 				break;
15711da177e4SLinus Torvalds 			}
15721da177e4SLinus Torvalds 		}
15731da177e4SLinus Torvalds 	}
15745c578aedSstephen hemminger 	rcu_read_unlock_bh();
15751da177e4SLinus Torvalds 
1576b79d1d54SDavid S. Miller 	return result;
15771da177e4SLinus Torvalds }
15781da177e4SLinus Torvalds 
15791da177e4SLinus Torvalds /* Gets referenced address, destroys ifaddr */
15801da177e4SLinus Torvalds 
1581cc411d0bSBrian Haley static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
15821da177e4SLinus Torvalds {
15831da177e4SLinus Torvalds 	if (ifp->flags&IFA_F_PERMANENT) {
15841da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
15851da177e4SLinus Torvalds 		addrconf_del_timer(ifp);
15861da177e4SLinus Torvalds 		ifp->flags |= IFA_F_TENTATIVE;
1587cc411d0bSBrian Haley 		if (dad_failed)
1588cc411d0bSBrian Haley 			ifp->flags |= IFA_F_DADFAILED;
15891da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
1590e2577a06SHerbert Xu 		if (dad_failed)
1591e2577a06SHerbert Xu 			ipv6_ifa_notify(0, ifp);
15921da177e4SLinus Torvalds 		in6_ifa_put(ifp);
15931da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
15941da177e4SLinus Torvalds 	} else if (ifp->flags&IFA_F_TEMPORARY) {
15951da177e4SLinus Torvalds 		struct inet6_ifaddr *ifpub;
15961da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
15971da177e4SLinus Torvalds 		ifpub = ifp->ifpub;
15981da177e4SLinus Torvalds 		if (ifpub) {
15991da177e4SLinus Torvalds 			in6_ifa_hold(ifpub);
16001da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
16011da177e4SLinus Torvalds 			ipv6_create_tempaddr(ifpub, ifp);
16021da177e4SLinus Torvalds 			in6_ifa_put(ifpub);
16031da177e4SLinus Torvalds 		} else {
16041da177e4SLinus Torvalds 			spin_unlock_bh(&ifp->lock);
16051da177e4SLinus Torvalds 		}
16061da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
16071da177e4SLinus Torvalds #endif
16081da177e4SLinus Torvalds 	} else
16091da177e4SLinus Torvalds 		ipv6_del_addr(ifp);
16101da177e4SLinus Torvalds }
16111da177e4SLinus Torvalds 
1612f2344a13SHerbert Xu static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1613f2344a13SHerbert Xu {
1614f2344a13SHerbert Xu 	int err = -ENOENT;
1615f2344a13SHerbert Xu 
1616f2344a13SHerbert Xu 	spin_lock(&ifp->state_lock);
1617f2344a13SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DAD) {
1618f2344a13SHerbert Xu 		ifp->state = INET6_IFADDR_STATE_POSTDAD;
1619f2344a13SHerbert Xu 		err = 0;
1620f2344a13SHerbert Xu 	}
1621f2344a13SHerbert Xu 	spin_unlock(&ifp->state_lock);
1622f2344a13SHerbert Xu 
1623f2344a13SHerbert Xu 	return err;
1624f2344a13SHerbert Xu }
1625f2344a13SHerbert Xu 
16263c21edbdSYOSHIFUJI Hideaki void addrconf_dad_failure(struct inet6_ifaddr *ifp)
16273c21edbdSYOSHIFUJI Hideaki {
16281b34be74SYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
16299bdd8d40SBrian Haley 
1630853dc2e0SUrsula Braun 	if (addrconf_dad_end(ifp)) {
1631853dc2e0SUrsula Braun 		in6_ifa_put(ifp);
1632f2344a13SHerbert Xu 		return;
1633853dc2e0SUrsula Braun 	}
1634f2344a13SHerbert Xu 
1635e87cc472SJoe Perches 	net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
16360522fea6SJens Rosenboom 			     ifp->idev->dev->name, &ifp->addr);
16379bdd8d40SBrian Haley 
16381b34be74SYOSHIFUJI Hideaki 	if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
16391b34be74SYOSHIFUJI Hideaki 		struct in6_addr addr;
16401b34be74SYOSHIFUJI Hideaki 
16411b34be74SYOSHIFUJI Hideaki 		addr.s6_addr32[0] = htonl(0xfe800000);
16421b34be74SYOSHIFUJI Hideaki 		addr.s6_addr32[1] = 0;
16431b34be74SYOSHIFUJI Hideaki 
16441b34be74SYOSHIFUJI Hideaki 		if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
16451b34be74SYOSHIFUJI Hideaki 		    ipv6_addr_equal(&ifp->addr, &addr)) {
16461b34be74SYOSHIFUJI Hideaki 			/* DAD failed for link-local based on MAC address */
16471b34be74SYOSHIFUJI Hideaki 			idev->cnf.disable_ipv6 = 1;
16489bdd8d40SBrian Haley 
1649f3213831SJoe Perches 			pr_info("%s: IPv6 being disabled!\n",
16509bdd8d40SBrian Haley 				ifp->idev->dev->name);
16511b34be74SYOSHIFUJI Hideaki 		}
16521b34be74SYOSHIFUJI Hideaki 	}
16531b34be74SYOSHIFUJI Hideaki 
1654cc411d0bSBrian Haley 	addrconf_dad_stop(ifp, 1);
16553c21edbdSYOSHIFUJI Hideaki }
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds /* Join to solicited addr multicast group. */
16581da177e4SLinus Torvalds 
1659b71d1d42SEric Dumazet void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
16601da177e4SLinus Torvalds {
16611da177e4SLinus Torvalds 	struct in6_addr maddr;
16621da177e4SLinus Torvalds 
16631da177e4SLinus Torvalds 	if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
16641da177e4SLinus Torvalds 		return;
16651da177e4SLinus Torvalds 
16661da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
16671da177e4SLinus Torvalds 	ipv6_dev_mc_inc(dev, &maddr);
16681da177e4SLinus Torvalds }
16691da177e4SLinus Torvalds 
1670b71d1d42SEric Dumazet void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
16711da177e4SLinus Torvalds {
16721da177e4SLinus Torvalds 	struct in6_addr maddr;
16731da177e4SLinus Torvalds 
16741da177e4SLinus Torvalds 	if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
16751da177e4SLinus Torvalds 		return;
16761da177e4SLinus Torvalds 
16771da177e4SLinus Torvalds 	addrconf_addr_solict_mult(addr, &maddr);
16781da177e4SLinus Torvalds 	__ipv6_dev_mc_dec(idev, &maddr);
16791da177e4SLinus Torvalds }
16801da177e4SLinus Torvalds 
168120380731SArnaldo Carvalho de Melo static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
16821da177e4SLinus Torvalds {
16831da177e4SLinus Torvalds 	struct in6_addr addr;
16842bda8a0cSBjørn Mork 	if (ifp->prefix_len == 127) /* RFC 6164 */
16852bda8a0cSBjørn Mork 		return;
16861da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
16871da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
16881da177e4SLinus Torvalds 		return;
16891da177e4SLinus Torvalds 	ipv6_dev_ac_inc(ifp->idev->dev, &addr);
16901da177e4SLinus Torvalds }
16911da177e4SLinus Torvalds 
169220380731SArnaldo Carvalho de Melo static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
16931da177e4SLinus Torvalds {
16941da177e4SLinus Torvalds 	struct in6_addr addr;
169532019e65SYOSHIFUJI Hideaki 	if (ifp->prefix_len == 127) /* RFC 6164 */
169632019e65SYOSHIFUJI Hideaki 		return;
16971da177e4SLinus Torvalds 	ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
16981da177e4SLinus Torvalds 	if (ipv6_addr_any(&addr))
16991da177e4SLinus Torvalds 		return;
17001da177e4SLinus Torvalds 	__ipv6_dev_ac_dec(ifp->idev, &addr);
17011da177e4SLinus Torvalds }
17021da177e4SLinus Torvalds 
1703073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
17041da177e4SLinus Torvalds {
17051da177e4SLinus Torvalds 	if (dev->addr_len != ETH_ALEN)
17061da177e4SLinus Torvalds 		return -1;
17071da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr, 3);
17081da177e4SLinus Torvalds 	memcpy(eui + 5, dev->dev_addr + 3, 3);
17091da177e4SLinus Torvalds 
17101da177e4SLinus Torvalds 	/*
17111da177e4SLinus Torvalds 	 * The zSeries OSA network cards can be shared among various
17121da177e4SLinus Torvalds 	 * OS instances, but the OSA cards have only one MAC address.
17131da177e4SLinus Torvalds 	 * This leads to duplicate address conflicts in conjunction
17141da177e4SLinus Torvalds 	 * with IPv6 if more than one instance uses the same card.
17151da177e4SLinus Torvalds 	 *
17161da177e4SLinus Torvalds 	 * The driver for these cards can deliver a unique 16-bit
17171da177e4SLinus Torvalds 	 * identifier for each instance sharing the same card.  It is
17181da177e4SLinus Torvalds 	 * placed instead of 0xFFFE in the interface identifier.  The
17191da177e4SLinus Torvalds 	 * "u" bit of the interface identifier is not inverted in this
17201da177e4SLinus Torvalds 	 * case.  Hence the resulting interface identifier has local
17211da177e4SLinus Torvalds 	 * scope according to RFC2373.
17221da177e4SLinus Torvalds 	 */
17231da177e4SLinus Torvalds 	if (dev->dev_id) {
17241da177e4SLinus Torvalds 		eui[3] = (dev->dev_id >> 8) & 0xFF;
17251da177e4SLinus Torvalds 		eui[4] = dev->dev_id & 0xFF;
17261da177e4SLinus Torvalds 	} else {
17271da177e4SLinus Torvalds 		eui[3] = 0xFF;
17281da177e4SLinus Torvalds 		eui[4] = 0xFE;
17291da177e4SLinus Torvalds 		eui[0] ^= 2;
17301da177e4SLinus Torvalds 	}
17311da177e4SLinus Torvalds 	return 0;
1732073a8e0eSYOSHIFUJI Hideaki }
1733073a8e0eSYOSHIFUJI Hideaki 
173406a4c1c5Salex.bluesman.smirnov@gmail.com static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
173506a4c1c5Salex.bluesman.smirnov@gmail.com {
173606a4c1c5Salex.bluesman.smirnov@gmail.com 	if (dev->addr_len != IEEE802154_ADDR_LEN)
173706a4c1c5Salex.bluesman.smirnov@gmail.com 		return -1;
173806a4c1c5Salex.bluesman.smirnov@gmail.com 	memcpy(eui, dev->dev_addr, 8);
17395e98a36eSYOSHIFUJI Hideaki / 吉藤英明 	eui[0] ^= 2;
174006a4c1c5Salex.bluesman.smirnov@gmail.com 	return 0;
174106a4c1c5Salex.bluesman.smirnov@gmail.com }
174206a4c1c5Salex.bluesman.smirnov@gmail.com 
1743cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
1744cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 {
1745cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	union fwnet_hwaddr *ha;
1746cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
1747cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	if (dev->addr_len != FWNET_ALEN)
1748cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 		return -1;
1749cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
1750cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	ha = (union fwnet_hwaddr *)dev->dev_addr;
1751cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
1752cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
1753cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	eui[0] ^= 2;
1754cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	return 0;
1755cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 }
1756cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 
1757073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1758073a8e0eSYOSHIFUJI Hideaki {
17591da177e4SLinus Torvalds 	/* XXX: inherit EUI-64 from other interface -- yoshfuji */
17601da177e4SLinus Torvalds 	if (dev->addr_len != ARCNET_ALEN)
17611da177e4SLinus Torvalds 		return -1;
17621da177e4SLinus Torvalds 	memset(eui, 0, 7);
17631da177e4SLinus Torvalds 	eui[7] = *(u8 *)dev->dev_addr;
17641da177e4SLinus Torvalds 	return 0;
1765073a8e0eSYOSHIFUJI Hideaki }
1766073a8e0eSYOSHIFUJI Hideaki 
1767073a8e0eSYOSHIFUJI Hideaki static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1768073a8e0eSYOSHIFUJI Hideaki {
17691da177e4SLinus Torvalds 	if (dev->addr_len != INFINIBAND_ALEN)
17701da177e4SLinus Torvalds 		return -1;
17711da177e4SLinus Torvalds 	memcpy(eui, dev->dev_addr + 12, 8);
17721da177e4SLinus Torvalds 	eui[0] |= 2;
17731da177e4SLinus Torvalds 	return 0;
17741da177e4SLinus Torvalds }
1775073a8e0eSYOSHIFUJI Hideaki 
1776c61393eaSstephen hemminger static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
1777dfd982baSYOSHIFUJI Hideaki {
17789af28511SSascha Hlusiak 	if (addr == 0)
17799af28511SSascha Hlusiak 		return -1;
1780dfd982baSYOSHIFUJI Hideaki 	eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1781dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1782dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1783dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1784dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1785dfd982baSYOSHIFUJI Hideaki 		  ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1786dfd982baSYOSHIFUJI Hideaki 	eui[1] = 0;
1787dfd982baSYOSHIFUJI Hideaki 	eui[2] = 0x5E;
1788dfd982baSYOSHIFUJI Hideaki 	eui[3] = 0xFE;
1789dfd982baSYOSHIFUJI Hideaki 	memcpy(eui + 4, &addr, 4);
1790dfd982baSYOSHIFUJI Hideaki 	return 0;
1791dfd982baSYOSHIFUJI Hideaki }
1792dfd982baSYOSHIFUJI Hideaki 
1793dfd982baSYOSHIFUJI Hideaki static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1794dfd982baSYOSHIFUJI Hideaki {
1795dfd982baSYOSHIFUJI Hideaki 	if (dev->priv_flags & IFF_ISATAP)
1796dfd982baSYOSHIFUJI Hideaki 		return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1797dfd982baSYOSHIFUJI Hideaki 	return -1;
1798dfd982baSYOSHIFUJI Hideaki }
1799dfd982baSYOSHIFUJI Hideaki 
1800aee80b54Sstephen hemminger static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
1801aee80b54Sstephen hemminger {
1802aee80b54Sstephen hemminger 	return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1803aee80b54Sstephen hemminger }
1804aee80b54Sstephen hemminger 
1805073a8e0eSYOSHIFUJI Hideaki static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1806073a8e0eSYOSHIFUJI Hideaki {
1807073a8e0eSYOSHIFUJI Hideaki 	switch (dev->type) {
1808073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ETHER:
1809073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_FDDI:
1810073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_eui48(eui, dev);
1811073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_ARCNET:
1812073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_arcnet(eui, dev);
1813073a8e0eSYOSHIFUJI Hideaki 	case ARPHRD_INFINIBAND:
1814073a8e0eSYOSHIFUJI Hideaki 		return addrconf_ifid_infiniband(eui, dev);
1815c7dc89c0SFred L. Templin 	case ARPHRD_SIT:
1816dfd982baSYOSHIFUJI Hideaki 		return addrconf_ifid_sit(eui, dev);
1817aee80b54Sstephen hemminger 	case ARPHRD_IPGRE:
1818aee80b54Sstephen hemminger 		return addrconf_ifid_gre(eui, dev);
181906a4c1c5Salex.bluesman.smirnov@gmail.com 	case ARPHRD_IEEE802154:
182006a4c1c5Salex.bluesman.smirnov@gmail.com 		return addrconf_ifid_eui64(eui, dev);
1821cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	case ARPHRD_IEEE1394:
1822cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 		return addrconf_ifid_ieee1394(eui, dev);
1823073a8e0eSYOSHIFUJI Hideaki 	}
18241da177e4SLinus Torvalds 	return -1;
18251da177e4SLinus Torvalds }
18261da177e4SLinus Torvalds 
18271da177e4SLinus Torvalds static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
18281da177e4SLinus Torvalds {
18291da177e4SLinus Torvalds 	int err = -1;
18301da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
18311da177e4SLinus Torvalds 
18321da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
1833502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
18341da177e4SLinus Torvalds 		if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
18351da177e4SLinus Torvalds 			memcpy(eui, ifp->addr.s6_addr+8, 8);
18361da177e4SLinus Torvalds 			err = 0;
18371da177e4SLinus Torvalds 			break;
18381da177e4SLinus Torvalds 		}
18391da177e4SLinus Torvalds 	}
18401da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
18411da177e4SLinus Torvalds 	return err;
18421da177e4SLinus Torvalds }
18431da177e4SLinus Torvalds 
18441da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
18451da177e4SLinus Torvalds /* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1846eb7e0575SSorin Dumitru static void __ipv6_regen_rndid(struct inet6_dev *idev)
18471da177e4SLinus Torvalds {
18481da177e4SLinus Torvalds regen:
1849955189efSYOSHIFUJI Hideaki 	get_random_bytes(idev->rndid, sizeof(idev->rndid));
18501da177e4SLinus Torvalds 	idev->rndid[0] &= ~0x02;
18511da177e4SLinus Torvalds 
18521da177e4SLinus Torvalds 	/*
18531da177e4SLinus Torvalds 	 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
18541da177e4SLinus Torvalds 	 * check if generated address is not inappropriate
18551da177e4SLinus Torvalds 	 *
18561da177e4SLinus Torvalds 	 *  - Reserved subnet anycast (RFC 2526)
18571da177e4SLinus Torvalds 	 *	11111101 11....11 1xxxxxxx
1858c7dc89c0SFred L. Templin 	 *  - ISATAP (RFC4214) 6.1
18591da177e4SLinus Torvalds 	 *	00-00-5E-FE-xx-xx-xx-xx
18601da177e4SLinus Torvalds 	 *  - value 0
18611da177e4SLinus Torvalds 	 *  - XXX: already assigned to an address on the device
18621da177e4SLinus Torvalds 	 */
18631da177e4SLinus Torvalds 	if (idev->rndid[0] == 0xfd &&
18641da177e4SLinus Torvalds 	    (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
18651da177e4SLinus Torvalds 	    (idev->rndid[7]&0x80))
18661da177e4SLinus Torvalds 		goto regen;
18671da177e4SLinus Torvalds 	if ((idev->rndid[0]|idev->rndid[1]) == 0) {
18681da177e4SLinus Torvalds 		if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
18691da177e4SLinus Torvalds 			goto regen;
18701da177e4SLinus Torvalds 		if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
18711da177e4SLinus Torvalds 			goto regen;
18721da177e4SLinus Torvalds 	}
18731da177e4SLinus Torvalds }
18741da177e4SLinus Torvalds 
18751da177e4SLinus Torvalds static void ipv6_regen_rndid(unsigned long data)
18761da177e4SLinus Torvalds {
18771da177e4SLinus Torvalds 	struct inet6_dev *idev = (struct inet6_dev *) data;
18781da177e4SLinus Torvalds 	unsigned long expires;
18791da177e4SLinus Torvalds 
18808814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
18811da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
18821da177e4SLinus Torvalds 
18831da177e4SLinus Torvalds 	if (idev->dead)
18841da177e4SLinus Torvalds 		goto out;
18851da177e4SLinus Torvalds 
1886eb7e0575SSorin Dumitru 	__ipv6_regen_rndid(idev);
18871da177e4SLinus Torvalds 
18881da177e4SLinus Torvalds 	expires = jiffies +
18891da177e4SLinus Torvalds 		idev->cnf.temp_prefered_lft * HZ -
1890784e2710SBen Hutchings 		idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time -
1891784e2710SBen Hutchings 		idev->cnf.max_desync_factor * HZ;
18921da177e4SLinus Torvalds 	if (time_before(expires, jiffies)) {
1893f3213831SJoe Perches 		pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
1894f3213831SJoe Perches 			__func__, idev->dev->name);
18951da177e4SLinus Torvalds 		goto out;
18961da177e4SLinus Torvalds 	}
18971da177e4SLinus Torvalds 
18981da177e4SLinus Torvalds 	if (!mod_timer(&idev->regen_timer, expires))
18991da177e4SLinus Torvalds 		in6_dev_hold(idev);
19001da177e4SLinus Torvalds 
19011da177e4SLinus Torvalds out:
19021da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
19038814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
19041da177e4SLinus Torvalds 	in6_dev_put(idev);
19051da177e4SLinus Torvalds }
19061da177e4SLinus Torvalds 
1907eb7e0575SSorin Dumitru static void  __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
19088e5e8f30SEldad Zack {
19091da177e4SLinus Torvalds 	if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1910eb7e0575SSorin Dumitru 		__ipv6_regen_rndid(idev);
19111da177e4SLinus Torvalds }
19121da177e4SLinus Torvalds #endif
19131da177e4SLinus Torvalds 
19141da177e4SLinus Torvalds /*
19151da177e4SLinus Torvalds  *	Add prefix route.
19161da177e4SLinus Torvalds  */
19171da177e4SLinus Torvalds 
19181da177e4SLinus Torvalds static void
19191da177e4SLinus Torvalds addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
1920e431b8c0SJamal Hadi Salim 		      unsigned long expires, u32 flags)
19211da177e4SLinus Torvalds {
192286872cb5SThomas Graf 	struct fib6_config cfg = {
192386872cb5SThomas Graf 		.fc_table = RT6_TABLE_PREFIX,
192486872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
192586872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
192686872cb5SThomas Graf 		.fc_expires = expires,
192786872cb5SThomas Graf 		.fc_dst_len = plen,
192886872cb5SThomas Graf 		.fc_flags = RTF_UP | flags,
1929c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
1930f410a1fbSStephen Hemminger 		.fc_protocol = RTPROT_KERNEL,
193186872cb5SThomas Graf 	};
19321da177e4SLinus Torvalds 
19334e3fd7a0SAlexey Dobriyan 	cfg.fc_dst = *pfx;
19341da177e4SLinus Torvalds 
19351da177e4SLinus Torvalds 	/* Prevent useless cloning on PtP SIT.
19361da177e4SLinus Torvalds 	   This thing is done here expecting that the whole
19371da177e4SLinus Torvalds 	   class of non-broadcast devices need not cloning.
19381da177e4SLinus Torvalds 	 */
193907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
19401da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
194186872cb5SThomas Graf 		cfg.fc_flags |= RTF_NONEXTHOP;
19420be669bbSJoerg Roedel #endif
19431da177e4SLinus Torvalds 
194486872cb5SThomas Graf 	ip6_route_add(&cfg);
19451da177e4SLinus Torvalds }
19461da177e4SLinus Torvalds 
194714ef37b6SAndreas Hofmeister 
194814ef37b6SAndreas Hofmeister static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
194914ef37b6SAndreas Hofmeister 						  int plen,
195014ef37b6SAndreas Hofmeister 						  const struct net_device *dev,
195114ef37b6SAndreas Hofmeister 						  u32 flags, u32 noflags)
195214ef37b6SAndreas Hofmeister {
195314ef37b6SAndreas Hofmeister 	struct fib6_node *fn;
195414ef37b6SAndreas Hofmeister 	struct rt6_info *rt = NULL;
195514ef37b6SAndreas Hofmeister 	struct fib6_table *table;
195614ef37b6SAndreas Hofmeister 
195714ef37b6SAndreas Hofmeister 	table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX);
195814ef37b6SAndreas Hofmeister 	if (table == NULL)
195914ef37b6SAndreas Hofmeister 		return NULL;
196014ef37b6SAndreas Hofmeister 
19615744dd9bSLi RongQing 	read_lock_bh(&table->tb6_lock);
196214ef37b6SAndreas Hofmeister 	fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
196314ef37b6SAndreas Hofmeister 	if (!fn)
196414ef37b6SAndreas Hofmeister 		goto out;
196514ef37b6SAndreas Hofmeister 	for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1966d1918542SDavid S. Miller 		if (rt->dst.dev->ifindex != dev->ifindex)
196714ef37b6SAndreas Hofmeister 			continue;
196814ef37b6SAndreas Hofmeister 		if ((rt->rt6i_flags & flags) != flags)
196914ef37b6SAndreas Hofmeister 			continue;
197085da53bfSRomain Kuntz 		if ((rt->rt6i_flags & noflags) != 0)
197114ef37b6SAndreas Hofmeister 			continue;
197214ef37b6SAndreas Hofmeister 		dst_hold(&rt->dst);
197314ef37b6SAndreas Hofmeister 		break;
197414ef37b6SAndreas Hofmeister 	}
197514ef37b6SAndreas Hofmeister out:
19765744dd9bSLi RongQing 	read_unlock_bh(&table->tb6_lock);
197714ef37b6SAndreas Hofmeister 	return rt;
197814ef37b6SAndreas Hofmeister }
197914ef37b6SAndreas Hofmeister 
198014ef37b6SAndreas Hofmeister 
19811da177e4SLinus Torvalds /* Create "default" multicast route to the interface */
19821da177e4SLinus Torvalds 
19831da177e4SLinus Torvalds static void addrconf_add_mroute(struct net_device *dev)
19841da177e4SLinus Torvalds {
198586872cb5SThomas Graf 	struct fib6_config cfg = {
198686872cb5SThomas Graf 		.fc_table = RT6_TABLE_LOCAL,
198786872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
198886872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
198986872cb5SThomas Graf 		.fc_dst_len = 8,
199086872cb5SThomas Graf 		.fc_flags = RTF_UP,
1991c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
199286872cb5SThomas Graf 	};
19931da177e4SLinus Torvalds 
199486872cb5SThomas Graf 	ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
199586872cb5SThomas Graf 
199686872cb5SThomas Graf 	ip6_route_add(&cfg);
19971da177e4SLinus Torvalds }
19981da177e4SLinus Torvalds 
199907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
20001da177e4SLinus Torvalds static void sit_route_add(struct net_device *dev)
20011da177e4SLinus Torvalds {
200286872cb5SThomas Graf 	struct fib6_config cfg = {
200386872cb5SThomas Graf 		.fc_table = RT6_TABLE_MAIN,
200486872cb5SThomas Graf 		.fc_metric = IP6_RT_PRIO_ADDRCONF,
200586872cb5SThomas Graf 		.fc_ifindex = dev->ifindex,
200686872cb5SThomas Graf 		.fc_dst_len = 96,
200786872cb5SThomas Graf 		.fc_flags = RTF_UP | RTF_NONEXTHOP,
2008c346dca1SYOSHIFUJI Hideaki 		.fc_nlinfo.nl_net = dev_net(dev),
200986872cb5SThomas Graf 	};
20101da177e4SLinus Torvalds 
20111da177e4SLinus Torvalds 	/* prefix length - 96 bits "::d.d.d.d" */
201286872cb5SThomas Graf 	ip6_route_add(&cfg);
20131da177e4SLinus Torvalds }
20140be669bbSJoerg Roedel #endif
20151da177e4SLinus Torvalds 
20161da177e4SLinus Torvalds static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
20171da177e4SLinus Torvalds {
20181da177e4SLinus Torvalds 	struct inet6_dev *idev;
20191da177e4SLinus Torvalds 
20201da177e4SLinus Torvalds 	ASSERT_RTNL();
20211da177e4SLinus Torvalds 
2022e21e8467SStephen Hemminger 	idev = ipv6_find_idev(dev);
2023e21e8467SStephen Hemminger 	if (!idev)
202464e724f6SBrian Haley 		return ERR_PTR(-ENOBUFS);
202564e724f6SBrian Haley 
202664e724f6SBrian Haley 	if (idev->cnf.disable_ipv6)
202764e724f6SBrian Haley 		return ERR_PTR(-EACCES);
20281da177e4SLinus Torvalds 
20291da177e4SLinus Torvalds 	/* Add default multicast route */
20304af04abaSLi Wei 	if (!(dev->flags & IFF_LOOPBACK))
20311da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
20321da177e4SLinus Torvalds 
20331da177e4SLinus Torvalds 	return idev;
20341da177e4SLinus Torvalds }
20351da177e4SLinus Torvalds 
2036e6bff995SNeil Horman void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
20371da177e4SLinus Torvalds {
20381da177e4SLinus Torvalds 	struct prefix_info *pinfo;
20391da177e4SLinus Torvalds 	__u32 valid_lft;
20401da177e4SLinus Torvalds 	__u32 prefered_lft;
20411da177e4SLinus Torvalds 	int addr_type;
20421da177e4SLinus Torvalds 	struct inet6_dev *in6_dev;
204356d417b1SBrian Haley 	struct net *net = dev_net(dev);
20441da177e4SLinus Torvalds 
20451da177e4SLinus Torvalds 	pinfo = (struct prefix_info *) opt;
20461da177e4SLinus Torvalds 
20471da177e4SLinus Torvalds 	if (len < sizeof(struct prefix_info)) {
20481da177e4SLinus Torvalds 		ADBG(("addrconf: prefix option too short\n"));
20491da177e4SLinus Torvalds 		return;
20501da177e4SLinus Torvalds 	}
20511da177e4SLinus Torvalds 
20521da177e4SLinus Torvalds 	/*
20531da177e4SLinus Torvalds 	 *	Validation checks ([ADDRCONF], page 19)
20541da177e4SLinus Torvalds 	 */
20551da177e4SLinus Torvalds 
20561da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&pinfo->prefix);
20571da177e4SLinus Torvalds 
20581da177e4SLinus Torvalds 	if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
20591da177e4SLinus Torvalds 		return;
20601da177e4SLinus Torvalds 
20611da177e4SLinus Torvalds 	valid_lft = ntohl(pinfo->valid);
20621da177e4SLinus Torvalds 	prefered_lft = ntohl(pinfo->prefered);
20631da177e4SLinus Torvalds 
20641da177e4SLinus Torvalds 	if (prefered_lft > valid_lft) {
2065e87cc472SJoe Perches 		net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
20661da177e4SLinus Torvalds 		return;
20671da177e4SLinus Torvalds 	}
20681da177e4SLinus Torvalds 
20691da177e4SLinus Torvalds 	in6_dev = in6_dev_get(dev);
20701da177e4SLinus Torvalds 
20711da177e4SLinus Torvalds 	if (in6_dev == NULL) {
2072e87cc472SJoe Perches 		net_dbg_ratelimited("addrconf: device %s not configured\n",
2073e87cc472SJoe Perches 				    dev->name);
20741da177e4SLinus Torvalds 		return;
20751da177e4SLinus Torvalds 	}
20761da177e4SLinus Torvalds 
20771da177e4SLinus Torvalds 	/*
20781da177e4SLinus Torvalds 	 *	Two things going on here:
20791da177e4SLinus Torvalds 	 *	1) Add routes for on-link prefixes
20801da177e4SLinus Torvalds 	 *	2) Configure prefixes with the auto flag set
20811da177e4SLinus Torvalds 	 */
20821da177e4SLinus Torvalds 
20834bed72e4SYOSHIFUJI Hideaki 	if (pinfo->onlink) {
20844bed72e4SYOSHIFUJI Hideaki 		struct rt6_info *rt;
20854bed72e4SYOSHIFUJI Hideaki 		unsigned long rt_expires;
20864bed72e4SYOSHIFUJI Hideaki 
20871da177e4SLinus Torvalds 		/* Avoid arithmetic overflow. Really, we could
20886f704992SYOSHIFUJI Hideaki 		 * save rt_expires in seconds, likely valid_lft,
20896f704992SYOSHIFUJI Hideaki 		 * but it would require division in fib gc, that it
20906f704992SYOSHIFUJI Hideaki 		 * not good.
20911da177e4SLinus Torvalds 		 */
20924bed72e4SYOSHIFUJI Hideaki 		if (HZ > USER_HZ)
20934bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
20944bed72e4SYOSHIFUJI Hideaki 		else
20954bed72e4SYOSHIFUJI Hideaki 			rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
20963dd4bc68SYOSHIFUJI Hideaki 
20974bed72e4SYOSHIFUJI Hideaki 		if (addrconf_finite_timeout(rt_expires))
20984bed72e4SYOSHIFUJI Hideaki 			rt_expires *= HZ;
20991da177e4SLinus Torvalds 
210014ef37b6SAndreas Hofmeister 		rt = addrconf_get_prefix_route(&pinfo->prefix,
210114ef37b6SAndreas Hofmeister 					       pinfo->prefix_len,
210214ef37b6SAndreas Hofmeister 					       dev,
210314ef37b6SAndreas Hofmeister 					       RTF_ADDRCONF | RTF_PREFIX_RT,
210414ef37b6SAndreas Hofmeister 					       RTF_GATEWAY | RTF_DEFAULT);
21051da177e4SLinus Torvalds 
210614ef37b6SAndreas Hofmeister 		if (rt) {
21076f704992SYOSHIFUJI Hideaki 			/* Autoconf prefix route */
21081da177e4SLinus Torvalds 			if (valid_lft == 0) {
2109e0a1ad73SThomas Graf 				ip6_del_rt(rt);
21101da177e4SLinus Torvalds 				rt = NULL;
21114bed72e4SYOSHIFUJI Hideaki 			} else if (addrconf_finite_timeout(rt_expires)) {
21126f704992SYOSHIFUJI Hideaki 				/* not infinity */
21131716a961SGao feng 				rt6_set_expires(rt, jiffies + rt_expires);
21146f704992SYOSHIFUJI Hideaki 			} else {
21151716a961SGao feng 				rt6_clean_expires(rt);
21161da177e4SLinus Torvalds 			}
21171da177e4SLinus Torvalds 		} else if (valid_lft) {
21186f704992SYOSHIFUJI Hideaki 			clock_t expires = 0;
21194bed72e4SYOSHIFUJI Hideaki 			int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
21204bed72e4SYOSHIFUJI Hideaki 			if (addrconf_finite_timeout(rt_expires)) {
21216f704992SYOSHIFUJI Hideaki 				/* not infinity */
21226f704992SYOSHIFUJI Hideaki 				flags |= RTF_EXPIRES;
21236f704992SYOSHIFUJI Hideaki 				expires = jiffies_to_clock_t(rt_expires);
21246f704992SYOSHIFUJI Hideaki 			}
21251da177e4SLinus Torvalds 			addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
21266f704992SYOSHIFUJI Hideaki 					      dev, expires, flags);
21271da177e4SLinus Torvalds 		}
212894e187c0SAmerigo Wang 		ip6_rt_put(rt);
21291da177e4SLinus Torvalds 	}
21301da177e4SLinus Torvalds 
21311da177e4SLinus Torvalds 	/* Try to figure out our local address for this prefix */
21321da177e4SLinus Torvalds 
21331da177e4SLinus Torvalds 	if (pinfo->autoconf && in6_dev->cnf.autoconf) {
21341da177e4SLinus Torvalds 		struct inet6_ifaddr *ifp;
21351da177e4SLinus Torvalds 		struct in6_addr addr;
21361da177e4SLinus Torvalds 		int create = 0, update_lft = 0;
21371da177e4SLinus Torvalds 
21381da177e4SLinus Torvalds 		if (pinfo->prefix_len == 64) {
21391da177e4SLinus Torvalds 			memcpy(&addr, &pinfo->prefix, 8);
2140f53adae4SDaniel Borkmann 
2141f53adae4SDaniel Borkmann 			if (!ipv6_addr_any(&in6_dev->token)) {
2142f53adae4SDaniel Borkmann 				read_lock_bh(&in6_dev->lock);
2143f53adae4SDaniel Borkmann 				memcpy(addr.s6_addr + 8,
2144f53adae4SDaniel Borkmann 				       in6_dev->token.s6_addr + 8, 8);
2145f53adae4SDaniel Borkmann 				read_unlock_bh(&in6_dev->lock);
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;
2188cf22f9a2SDavid S. Miller 			addrconf_dad_start(ifp);
21891da177e4SLinus Torvalds 		}
21901da177e4SLinus Torvalds 
21911da177e4SLinus Torvalds 		if (ifp) {
21921da177e4SLinus Torvalds 			int flags;
21931da177e4SLinus Torvalds 			unsigned long now;
21941da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
21951da177e4SLinus Torvalds 			struct inet6_ifaddr *ift;
21961da177e4SLinus Torvalds #endif
21971da177e4SLinus Torvalds 			u32 stored_lft;
21981da177e4SLinus Torvalds 
21991da177e4SLinus Torvalds 			/* update lifetime (RFC2462 5.5.3 e) */
22001da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
22011da177e4SLinus Torvalds 			now = jiffies;
22021da177e4SLinus Torvalds 			if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
22031da177e4SLinus Torvalds 				stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
22041da177e4SLinus Torvalds 			else
22051da177e4SLinus Torvalds 				stored_lft = 0;
22061da177e4SLinus Torvalds 			if (!update_lft && stored_lft) {
22071da177e4SLinus Torvalds 				if (valid_lft > MIN_VALID_LIFETIME ||
22081da177e4SLinus Torvalds 				    valid_lft > stored_lft)
22091da177e4SLinus Torvalds 					update_lft = 1;
22101da177e4SLinus Torvalds 				else if (stored_lft <= MIN_VALID_LIFETIME) {
22111da177e4SLinus Torvalds 					/* valid_lft <= stored_lft is always true */
2212a1ed0526SBrian Haley 					/*
2213a1ed0526SBrian Haley 					 * RFC 4862 Section 5.5.3e:
2214a1ed0526SBrian Haley 					 * "Note that the preferred lifetime of
2215a1ed0526SBrian Haley 					 *  the corresponding address is always
2216a1ed0526SBrian Haley 					 *  reset to the Preferred Lifetime in
2217a1ed0526SBrian Haley 					 *  the received Prefix Information
2218a1ed0526SBrian Haley 					 *  option, regardless of whether the
2219a1ed0526SBrian Haley 					 *  valid lifetime is also reset or
2220a1ed0526SBrian Haley 					 *  ignored."
2221a1ed0526SBrian Haley 					 *
2222a1ed0526SBrian Haley 					 *  So if the preferred lifetime in
2223a1ed0526SBrian Haley 					 *  this advertisement is different
2224a1ed0526SBrian Haley 					 *  than what we have stored, but the
2225a1ed0526SBrian Haley 					 *  valid lifetime is invalid, just
2226a1ed0526SBrian Haley 					 *  reset prefered_lft.
2227a1ed0526SBrian Haley 					 *
2228a1ed0526SBrian Haley 					 *  We must set the valid lifetime
2229a1ed0526SBrian Haley 					 *  to the stored lifetime since we'll
2230a1ed0526SBrian Haley 					 *  be updating the timestamp below,
2231a1ed0526SBrian Haley 					 *  else we'll set it back to the
223225985edcSLucas De Marchi 					 *  minimum.
2233a1ed0526SBrian Haley 					 */
2234a1ed0526SBrian Haley 					if (prefered_lft != ifp->prefered_lft) {
2235a1ed0526SBrian Haley 						valid_lft = stored_lft;
2236a1ed0526SBrian Haley 						update_lft = 1;
2237a1ed0526SBrian Haley 					}
22381da177e4SLinus Torvalds 				} else {
22391da177e4SLinus Torvalds 					valid_lft = MIN_VALID_LIFETIME;
22401da177e4SLinus Torvalds 					if (valid_lft < prefered_lft)
22411da177e4SLinus Torvalds 						prefered_lft = valid_lft;
22421da177e4SLinus Torvalds 					update_lft = 1;
22431da177e4SLinus Torvalds 				}
22441da177e4SLinus Torvalds 			}
22451da177e4SLinus Torvalds 
22461da177e4SLinus Torvalds 			if (update_lft) {
22471da177e4SLinus Torvalds 				ifp->valid_lft = valid_lft;
22481da177e4SLinus Torvalds 				ifp->prefered_lft = prefered_lft;
22491da177e4SLinus Torvalds 				ifp->tstamp = now;
22501da177e4SLinus Torvalds 				flags = ifp->flags;
22511da177e4SLinus Torvalds 				ifp->flags &= ~IFA_F_DEPRECATED;
22521da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
22531da177e4SLinus Torvalds 
22541da177e4SLinus Torvalds 				if (!(flags&IFA_F_TENTATIVE))
22551da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
22561da177e4SLinus Torvalds 			} else
22571da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
22581da177e4SLinus Torvalds 
22591da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
22601da177e4SLinus Torvalds 			read_lock_bh(&in6_dev->lock);
22611da177e4SLinus Torvalds 			/* update all temporary addresses in the list */
226276f793e3SLorenzo Colitti 			list_for_each_entry(ift, &in6_dev->tempaddr_list,
226376f793e3SLorenzo Colitti 					    tmp_list) {
226476f793e3SLorenzo Colitti 				int age, max_valid, max_prefered;
226576f793e3SLorenzo Colitti 
2266c6fbfac2SBenoit Boissinot 				if (ifp != ift->ifpub)
2267c6fbfac2SBenoit Boissinot 					continue;
2268c6fbfac2SBenoit Boissinot 
226976f793e3SLorenzo Colitti 				/*
227076f793e3SLorenzo Colitti 				 * RFC 4941 section 3.3:
227176f793e3SLorenzo Colitti 				 * If a received option will extend the lifetime
227276f793e3SLorenzo Colitti 				 * of a public address, the lifetimes of
227376f793e3SLorenzo Colitti 				 * temporary addresses should be extended,
227476f793e3SLorenzo Colitti 				 * subject to the overall constraint that no
227576f793e3SLorenzo Colitti 				 * temporary addresses should ever remain
227676f793e3SLorenzo Colitti 				 * "valid" or "preferred" for a time longer than
227776f793e3SLorenzo Colitti 				 * (TEMP_VALID_LIFETIME) or
227876f793e3SLorenzo Colitti 				 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR),
227976f793e3SLorenzo Colitti 				 * respectively.
228076f793e3SLorenzo Colitti 				 */
228176f793e3SLorenzo Colitti 				age = (now - ift->cstamp) / HZ;
228276f793e3SLorenzo Colitti 				max_valid = in6_dev->cnf.temp_valid_lft - age;
228376f793e3SLorenzo Colitti 				if (max_valid < 0)
228476f793e3SLorenzo Colitti 					max_valid = 0;
228576f793e3SLorenzo Colitti 
228676f793e3SLorenzo Colitti 				max_prefered = in6_dev->cnf.temp_prefered_lft -
228776f793e3SLorenzo Colitti 					       in6_dev->cnf.max_desync_factor -
228876f793e3SLorenzo Colitti 					       age;
228976f793e3SLorenzo Colitti 				if (max_prefered < 0)
229076f793e3SLorenzo Colitti 					max_prefered = 0;
229176f793e3SLorenzo Colitti 
229276f793e3SLorenzo Colitti 				if (valid_lft > max_valid)
229376f793e3SLorenzo Colitti 					valid_lft = max_valid;
229476f793e3SLorenzo Colitti 
229576f793e3SLorenzo Colitti 				if (prefered_lft > max_prefered)
229676f793e3SLorenzo Colitti 					prefered_lft = max_prefered;
229776f793e3SLorenzo Colitti 
22981da177e4SLinus Torvalds 				spin_lock(&ift->lock);
22991da177e4SLinus Torvalds 				flags = ift->flags;
230076f793e3SLorenzo Colitti 				ift->valid_lft = valid_lft;
230176f793e3SLorenzo Colitti 				ift->prefered_lft = prefered_lft;
230276f793e3SLorenzo Colitti 				ift->tstamp = now;
230376f793e3SLorenzo Colitti 				if (prefered_lft > 0)
230476f793e3SLorenzo Colitti 					ift->flags &= ~IFA_F_DEPRECATED;
230576f793e3SLorenzo Colitti 
23061da177e4SLinus Torvalds 				spin_unlock(&ift->lock);
23071da177e4SLinus Torvalds 				if (!(flags&IFA_F_TENTATIVE))
23081da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ift);
23091da177e4SLinus Torvalds 			}
23101da177e4SLinus Torvalds 
2311aed65501SGlenn Wurster 			if ((create || list_empty(&in6_dev->tempaddr_list)) && in6_dev->cnf.use_tempaddr > 0) {
23121da177e4SLinus Torvalds 				/*
231376f793e3SLorenzo Colitti 				 * When a new public address is created as
231476f793e3SLorenzo Colitti 				 * described in [ADDRCONF], also create a new
231576f793e3SLorenzo Colitti 				 * temporary address. Also create a temporary
231676f793e3SLorenzo Colitti 				 * address if it's enabled but no temporary
231776f793e3SLorenzo Colitti 				 * address currently exists.
23181da177e4SLinus Torvalds 				 */
23191da177e4SLinus Torvalds 				read_unlock_bh(&in6_dev->lock);
23201da177e4SLinus Torvalds 				ipv6_create_tempaddr(ifp, NULL);
23211da177e4SLinus Torvalds 			} else {
23221da177e4SLinus Torvalds 				read_unlock_bh(&in6_dev->lock);
23231da177e4SLinus Torvalds 			}
23241da177e4SLinus Torvalds #endif
23251da177e4SLinus Torvalds 			in6_ifa_put(ifp);
23261da177e4SLinus Torvalds 			addrconf_verify(0);
23271da177e4SLinus Torvalds 		}
23281da177e4SLinus Torvalds 	}
23291da177e4SLinus Torvalds 	inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
23301da177e4SLinus Torvalds 	in6_dev_put(in6_dev);
23311da177e4SLinus Torvalds }
23321da177e4SLinus Torvalds 
23331da177e4SLinus Torvalds /*
23341da177e4SLinus Torvalds  *	Set destination address.
23351da177e4SLinus Torvalds  *	Special case for SIT interfaces where we create a new "virtual"
23361da177e4SLinus Torvalds  *	device.
23371da177e4SLinus Torvalds  */
2338af284937SDaniel Lezcano int addrconf_set_dstaddr(struct net *net, void __user *arg)
23391da177e4SLinus Torvalds {
23401da177e4SLinus Torvalds 	struct in6_ifreq ireq;
23411da177e4SLinus Torvalds 	struct net_device *dev;
23421da177e4SLinus Torvalds 	int err = -EINVAL;
23431da177e4SLinus Torvalds 
23441da177e4SLinus Torvalds 	rtnl_lock();
23451da177e4SLinus Torvalds 
23461da177e4SLinus Torvalds 	err = -EFAULT;
23471da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
23481da177e4SLinus Torvalds 		goto err_exit;
23491da177e4SLinus Torvalds 
2350af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
23511da177e4SLinus Torvalds 
23521da177e4SLinus Torvalds 	err = -ENODEV;
23531da177e4SLinus Torvalds 	if (dev == NULL)
23541da177e4SLinus Torvalds 		goto err_exit;
23551da177e4SLinus Torvalds 
235607a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
23571da177e4SLinus Torvalds 	if (dev->type == ARPHRD_SIT) {
23585bc3eb7eSStephen Hemminger 		const struct net_device_ops *ops = dev->netdev_ops;
23591da177e4SLinus Torvalds 		struct ifreq ifr;
23601da177e4SLinus Torvalds 		struct ip_tunnel_parm p;
23611da177e4SLinus Torvalds 
23621da177e4SLinus Torvalds 		err = -EADDRNOTAVAIL;
23631da177e4SLinus Torvalds 		if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
23641da177e4SLinus Torvalds 			goto err_exit;
23651da177e4SLinus Torvalds 
23661da177e4SLinus Torvalds 		memset(&p, 0, sizeof(p));
23671da177e4SLinus Torvalds 		p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
23681da177e4SLinus Torvalds 		p.iph.saddr = 0;
23691da177e4SLinus Torvalds 		p.iph.version = 4;
23701da177e4SLinus Torvalds 		p.iph.ihl = 5;
23711da177e4SLinus Torvalds 		p.iph.protocol = IPPROTO_IPV6;
23721da177e4SLinus Torvalds 		p.iph.ttl = 64;
2373d20b3109SStephen Hemminger 		ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
23741da177e4SLinus Torvalds 
23755bc3eb7eSStephen Hemminger 		if (ops->ndo_do_ioctl) {
23765bc3eb7eSStephen Hemminger 			mm_segment_t oldfs = get_fs();
23775bc3eb7eSStephen Hemminger 
23785bc3eb7eSStephen Hemminger 			set_fs(KERNEL_DS);
23795bc3eb7eSStephen Hemminger 			err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
23801da177e4SLinus Torvalds 			set_fs(oldfs);
23815bc3eb7eSStephen Hemminger 		} else
23825bc3eb7eSStephen Hemminger 			err = -EOPNOTSUPP;
23831da177e4SLinus Torvalds 
23841da177e4SLinus Torvalds 		if (err == 0) {
23851da177e4SLinus Torvalds 			err = -ENOBUFS;
2386af284937SDaniel Lezcano 			dev = __dev_get_by_name(net, p.name);
2387af284937SDaniel Lezcano 			if (!dev)
23881da177e4SLinus Torvalds 				goto err_exit;
23891da177e4SLinus Torvalds 			err = dev_open(dev);
23901da177e4SLinus Torvalds 		}
23911da177e4SLinus Torvalds 	}
23920be669bbSJoerg Roedel #endif
23931da177e4SLinus Torvalds 
23941da177e4SLinus Torvalds err_exit:
23951da177e4SLinus Torvalds 	rtnl_unlock();
23961da177e4SLinus Torvalds 	return err;
23971da177e4SLinus Torvalds }
23981da177e4SLinus Torvalds 
23991da177e4SLinus Torvalds /*
24001da177e4SLinus Torvalds  *	Manual configuration of address on an interface
24011da177e4SLinus Torvalds  */
2402b71d1d42SEric Dumazet static int inet6_addr_add(struct net *net, int ifindex, const struct in6_addr *pfx,
240324ef0da7SThomas Graf 			  unsigned int plen, __u8 ifa_flags, __u32 prefered_lft,
2404af284937SDaniel Lezcano 			  __u32 valid_lft)
24051da177e4SLinus Torvalds {
24061da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
24071da177e4SLinus Torvalds 	struct inet6_dev *idev;
24081da177e4SLinus Torvalds 	struct net_device *dev;
24091da177e4SLinus Torvalds 	int scope;
24106f704992SYOSHIFUJI Hideaki 	u32 flags;
24116f704992SYOSHIFUJI Hideaki 	clock_t expires;
24124bed72e4SYOSHIFUJI Hideaki 	unsigned long timeout;
24131da177e4SLinus Torvalds 
24141da177e4SLinus Torvalds 	ASSERT_RTNL();
24151da177e4SLinus Torvalds 
241624ef0da7SThomas Graf 	if (plen > 128)
241724ef0da7SThomas Graf 		return -EINVAL;
241824ef0da7SThomas Graf 
24190778769dSNoriaki TAKAMIYA 	/* check the lifetime */
24200778769dSNoriaki TAKAMIYA 	if (!valid_lft || prefered_lft > valid_lft)
24210778769dSNoriaki TAKAMIYA 		return -EINVAL;
24220778769dSNoriaki TAKAMIYA 
2423af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2424af284937SDaniel Lezcano 	if (!dev)
24251da177e4SLinus Torvalds 		return -ENODEV;
24261da177e4SLinus Torvalds 
242764e724f6SBrian Haley 	idev = addrconf_add_dev(dev);
242864e724f6SBrian Haley 	if (IS_ERR(idev))
242964e724f6SBrian Haley 		return PTR_ERR(idev);
24301da177e4SLinus Torvalds 
24311da177e4SLinus Torvalds 	scope = ipv6_addr_scope(pfx);
24321da177e4SLinus Torvalds 
24334bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
24344bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
24354bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
24364bed72e4SYOSHIFUJI Hideaki 		valid_lft = timeout;
24376f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
24384bed72e4SYOSHIFUJI Hideaki 	} else {
24394bed72e4SYOSHIFUJI Hideaki 		expires = 0;
24404bed72e4SYOSHIFUJI Hideaki 		flags = 0;
24414bed72e4SYOSHIFUJI Hideaki 		ifa_flags |= IFA_F_PERMANENT;
24426f704992SYOSHIFUJI Hideaki 	}
24430778769dSNoriaki TAKAMIYA 
24444bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
24454bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
24464bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
24470778769dSNoriaki TAKAMIYA 			ifa_flags |= IFA_F_DEPRECATED;
24484bed72e4SYOSHIFUJI Hideaki 		prefered_lft = timeout;
24494bed72e4SYOSHIFUJI Hideaki 	}
24500778769dSNoriaki TAKAMIYA 
24510778769dSNoriaki TAKAMIYA 	ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
24520778769dSNoriaki TAKAMIYA 
24531da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
245406aebfb7SHerbert Xu 		spin_lock_bh(&ifp->lock);
24550778769dSNoriaki TAKAMIYA 		ifp->valid_lft = valid_lft;
24560778769dSNoriaki TAKAMIYA 		ifp->prefered_lft = prefered_lft;
24570778769dSNoriaki TAKAMIYA 		ifp->tstamp = jiffies;
245806aebfb7SHerbert Xu 		spin_unlock_bh(&ifp->lock);
24590778769dSNoriaki TAKAMIYA 
246046d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
24616f704992SYOSHIFUJI Hideaki 				      expires, flags);
246295c385b4SNeil Horman 		/*
246395c385b4SNeil Horman 		 * Note that section 3.1 of RFC 4429 indicates
246495c385b4SNeil Horman 		 * that the Optimistic flag should not be set for
246595c385b4SNeil Horman 		 * manually configured addresses
246695c385b4SNeil Horman 		 */
2467cf22f9a2SDavid S. Miller 		addrconf_dad_start(ifp);
24681da177e4SLinus Torvalds 		in6_ifa_put(ifp);
24690778769dSNoriaki TAKAMIYA 		addrconf_verify(0);
24701da177e4SLinus Torvalds 		return 0;
24711da177e4SLinus Torvalds 	}
24721da177e4SLinus Torvalds 
24731da177e4SLinus Torvalds 	return PTR_ERR(ifp);
24741da177e4SLinus Torvalds }
24751da177e4SLinus Torvalds 
2476b71d1d42SEric Dumazet static int inet6_addr_del(struct net *net, int ifindex, const struct in6_addr *pfx,
247724ef0da7SThomas Graf 			  unsigned int plen)
24781da177e4SLinus Torvalds {
24791da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
24801da177e4SLinus Torvalds 	struct inet6_dev *idev;
24811da177e4SLinus Torvalds 	struct net_device *dev;
24821da177e4SLinus Torvalds 
248324ef0da7SThomas Graf 	if (plen > 128)
248424ef0da7SThomas Graf 		return -EINVAL;
248524ef0da7SThomas Graf 
2486af284937SDaniel Lezcano 	dev = __dev_get_by_index(net, ifindex);
2487af284937SDaniel Lezcano 	if (!dev)
24881da177e4SLinus Torvalds 		return -ENODEV;
24891da177e4SLinus Torvalds 
24901da177e4SLinus Torvalds 	if ((idev = __in6_dev_get(dev)) == NULL)
24911da177e4SLinus Torvalds 		return -ENXIO;
24921da177e4SLinus Torvalds 
24931da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
2494502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
24951da177e4SLinus Torvalds 		if (ifp->prefix_len == plen &&
24961da177e4SLinus Torvalds 		    ipv6_addr_equal(pfx, &ifp->addr)) {
24971da177e4SLinus Torvalds 			in6_ifa_hold(ifp);
24981da177e4SLinus Torvalds 			read_unlock_bh(&idev->lock);
24991da177e4SLinus Torvalds 
25001da177e4SLinus Torvalds 			ipv6_del_addr(ifp);
25011da177e4SLinus Torvalds 
25021da177e4SLinus Torvalds 			/* If the last address is deleted administratively,
25031da177e4SLinus Torvalds 			   disable IPv6 on this interface.
25041da177e4SLinus Torvalds 			 */
2505502a2ffdSstephen hemminger 			if (list_empty(&idev->addr_list))
25061da177e4SLinus Torvalds 				addrconf_ifdown(idev->dev, 1);
25071da177e4SLinus Torvalds 			return 0;
25081da177e4SLinus Torvalds 		}
25091da177e4SLinus Torvalds 	}
25101da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
25111da177e4SLinus Torvalds 	return -EADDRNOTAVAIL;
25121da177e4SLinus Torvalds }
25131da177e4SLinus Torvalds 
25141da177e4SLinus Torvalds 
2515af284937SDaniel Lezcano int addrconf_add_ifaddr(struct net *net, void __user *arg)
25161da177e4SLinus Torvalds {
25171da177e4SLinus Torvalds 	struct in6_ifreq ireq;
25181da177e4SLinus Torvalds 	int err;
25191da177e4SLinus Torvalds 
2520af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
25211da177e4SLinus Torvalds 		return -EPERM;
25221da177e4SLinus Torvalds 
25231da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
25241da177e4SLinus Torvalds 		return -EFAULT;
25251da177e4SLinus Torvalds 
25261da177e4SLinus Torvalds 	rtnl_lock();
2527af284937SDaniel Lezcano 	err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2528af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2529af284937SDaniel Lezcano 			     INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
25301da177e4SLinus Torvalds 	rtnl_unlock();
25311da177e4SLinus Torvalds 	return err;
25321da177e4SLinus Torvalds }
25331da177e4SLinus Torvalds 
2534af284937SDaniel Lezcano int addrconf_del_ifaddr(struct net *net, void __user *arg)
25351da177e4SLinus Torvalds {
25361da177e4SLinus Torvalds 	struct in6_ifreq ireq;
25371da177e4SLinus Torvalds 	int err;
25381da177e4SLinus Torvalds 
2539af31f412SEric W. Biederman 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
25401da177e4SLinus Torvalds 		return -EPERM;
25411da177e4SLinus Torvalds 
25421da177e4SLinus Torvalds 	if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
25431da177e4SLinus Torvalds 		return -EFAULT;
25441da177e4SLinus Torvalds 
25451da177e4SLinus Torvalds 	rtnl_lock();
2546af284937SDaniel Lezcano 	err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2547af284937SDaniel Lezcano 			     ireq.ifr6_prefixlen);
25481da177e4SLinus Torvalds 	rtnl_unlock();
25491da177e4SLinus Torvalds 	return err;
25501da177e4SLinus Torvalds }
25511da177e4SLinus Torvalds 
2552b5f348e5SIlpo Järvinen static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2553b5f348e5SIlpo Järvinen 		     int plen, int scope)
2554b5f348e5SIlpo Järvinen {
2555b5f348e5SIlpo Järvinen 	struct inet6_ifaddr *ifp;
2556b5f348e5SIlpo Järvinen 
2557b5f348e5SIlpo Järvinen 	ifp = ipv6_add_addr(idev, addr, plen, scope, IFA_F_PERMANENT);
2558b5f348e5SIlpo Järvinen 	if (!IS_ERR(ifp)) {
2559b5f348e5SIlpo Järvinen 		spin_lock_bh(&ifp->lock);
2560b5f348e5SIlpo Järvinen 		ifp->flags &= ~IFA_F_TENTATIVE;
2561b5f348e5SIlpo Järvinen 		spin_unlock_bh(&ifp->lock);
2562b5f348e5SIlpo Järvinen 		ipv6_ifa_notify(RTM_NEWADDR, ifp);
2563b5f348e5SIlpo Järvinen 		in6_ifa_put(ifp);
2564b5f348e5SIlpo Järvinen 	}
2565b5f348e5SIlpo Järvinen }
2566b5f348e5SIlpo Järvinen 
256707a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
25681da177e4SLinus Torvalds static void sit_add_v4_addrs(struct inet6_dev *idev)
25691da177e4SLinus Torvalds {
25701da177e4SLinus Torvalds 	struct in6_addr addr;
25711da177e4SLinus Torvalds 	struct net_device *dev;
2572c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
25731da177e4SLinus Torvalds 	int scope;
25741da177e4SLinus Torvalds 
25751da177e4SLinus Torvalds 	ASSERT_RTNL();
25761da177e4SLinus Torvalds 
25771da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
25781da177e4SLinus Torvalds 	memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
25791da177e4SLinus Torvalds 
25801da177e4SLinus Torvalds 	if (idev->dev->flags&IFF_POINTOPOINT) {
25811da177e4SLinus Torvalds 		addr.s6_addr32[0] = htonl(0xfe800000);
25821da177e4SLinus Torvalds 		scope = IFA_LINK;
25831da177e4SLinus Torvalds 	} else {
25841da177e4SLinus Torvalds 		scope = IPV6_ADDR_COMPATv4;
25851da177e4SLinus Torvalds 	}
25861da177e4SLinus Torvalds 
25871da177e4SLinus Torvalds 	if (addr.s6_addr32[3]) {
2588b5f348e5SIlpo Järvinen 		add_addr(idev, &addr, 128, scope);
25891da177e4SLinus Torvalds 		return;
25901da177e4SLinus Torvalds 	}
25911da177e4SLinus Torvalds 
25926fda7350SBenjamin Thery 	for_each_netdev(net, dev) {
2593e5ed6399SHerbert Xu 		struct in_device *in_dev = __in_dev_get_rtnl(dev);
25941da177e4SLinus Torvalds 		if (in_dev && (dev->flags & IFF_UP)) {
25951da177e4SLinus Torvalds 			struct in_ifaddr *ifa;
25961da177e4SLinus Torvalds 
25971da177e4SLinus Torvalds 			int flag = scope;
25981da177e4SLinus Torvalds 
25991da177e4SLinus Torvalds 			for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
26001da177e4SLinus Torvalds 				int plen;
26011da177e4SLinus Torvalds 
26021da177e4SLinus Torvalds 				addr.s6_addr32[3] = ifa->ifa_local;
26031da177e4SLinus Torvalds 
26041da177e4SLinus Torvalds 				if (ifa->ifa_scope == RT_SCOPE_LINK)
26051da177e4SLinus Torvalds 					continue;
26061da177e4SLinus Torvalds 				if (ifa->ifa_scope >= RT_SCOPE_HOST) {
26071da177e4SLinus Torvalds 					if (idev->dev->flags&IFF_POINTOPOINT)
26081da177e4SLinus Torvalds 						continue;
26091da177e4SLinus Torvalds 					flag |= IFA_HOST;
26101da177e4SLinus Torvalds 				}
26111da177e4SLinus Torvalds 				if (idev->dev->flags&IFF_POINTOPOINT)
26121da177e4SLinus Torvalds 					plen = 64;
26131da177e4SLinus Torvalds 				else
26141da177e4SLinus Torvalds 					plen = 96;
26151da177e4SLinus Torvalds 
2616b5f348e5SIlpo Järvinen 				add_addr(idev, &addr, plen, flag);
26171da177e4SLinus Torvalds 			}
26181da177e4SLinus Torvalds 		}
26191da177e4SLinus Torvalds 	}
26201da177e4SLinus Torvalds }
26210be669bbSJoerg Roedel #endif
26221da177e4SLinus Torvalds 
26231da177e4SLinus Torvalds static void init_loopback(struct net_device *dev)
26241da177e4SLinus Torvalds {
26251da177e4SLinus Torvalds 	struct inet6_dev  *idev;
262625fb6ca4SBalakumaran Kannan 	struct net_device *sp_dev;
262725fb6ca4SBalakumaran Kannan 	struct inet6_ifaddr *sp_ifa;
262825fb6ca4SBalakumaran Kannan 	struct rt6_info *sp_rt;
26291da177e4SLinus Torvalds 
26301da177e4SLinus Torvalds 	/* ::1 */
26311da177e4SLinus Torvalds 
26321da177e4SLinus Torvalds 	ASSERT_RTNL();
26331da177e4SLinus Torvalds 
26341da177e4SLinus Torvalds 	if ((idev = ipv6_find_idev(dev)) == NULL) {
263591df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
26361da177e4SLinus Torvalds 		return;
26371da177e4SLinus Torvalds 	}
26381da177e4SLinus Torvalds 
2639b5f348e5SIlpo Järvinen 	add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
264025fb6ca4SBalakumaran Kannan 
264125fb6ca4SBalakumaran Kannan 	/* Add routes to other interface's IPv6 addresses */
264225fb6ca4SBalakumaran Kannan 	for_each_netdev(dev_net(dev), sp_dev) {
264325fb6ca4SBalakumaran Kannan 		if (!strcmp(sp_dev->name, dev->name))
264425fb6ca4SBalakumaran Kannan 			continue;
264525fb6ca4SBalakumaran Kannan 
264625fb6ca4SBalakumaran Kannan 		idev = __in6_dev_get(sp_dev);
264725fb6ca4SBalakumaran Kannan 		if (!idev)
264825fb6ca4SBalakumaran Kannan 			continue;
264925fb6ca4SBalakumaran Kannan 
265025fb6ca4SBalakumaran Kannan 		read_lock_bh(&idev->lock);
265125fb6ca4SBalakumaran Kannan 		list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
265225fb6ca4SBalakumaran Kannan 
265325fb6ca4SBalakumaran Kannan 			if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
265425fb6ca4SBalakumaran Kannan 				continue;
265525fb6ca4SBalakumaran Kannan 
265625fb6ca4SBalakumaran Kannan 			sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, 0);
265725fb6ca4SBalakumaran Kannan 
265825fb6ca4SBalakumaran Kannan 			/* Failure cases are ignored */
265925fb6ca4SBalakumaran Kannan 			if (!IS_ERR(sp_rt))
266025fb6ca4SBalakumaran Kannan 				ip6_ins_rt(sp_rt);
266125fb6ca4SBalakumaran Kannan 		}
266225fb6ca4SBalakumaran Kannan 		read_unlock_bh(&idev->lock);
266325fb6ca4SBalakumaran Kannan 	}
26641da177e4SLinus Torvalds }
26651da177e4SLinus Torvalds 
2666b71d1d42SEric Dumazet static void addrconf_add_linklocal(struct inet6_dev *idev, const struct in6_addr *addr)
26671da177e4SLinus Torvalds {
26681da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
266995c385b4SNeil Horman 	u32 addr_flags = IFA_F_PERMANENT;
26701da177e4SLinus Torvalds 
267195c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
267295c385b4SNeil Horman 	if (idev->cnf.optimistic_dad &&
2673702beb87SDavid Miller 	    !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
267495c385b4SNeil Horman 		addr_flags |= IFA_F_OPTIMISTIC;
267595c385b4SNeil Horman #endif
267695c385b4SNeil Horman 
267795c385b4SNeil Horman 
267895c385b4SNeil Horman 	ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags);
26791da177e4SLinus Torvalds 	if (!IS_ERR(ifp)) {
268046d48046SYOSHIFUJI Hideaki 		addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
2681cf22f9a2SDavid S. Miller 		addrconf_dad_start(ifp);
26821da177e4SLinus Torvalds 		in6_ifa_put(ifp);
26831da177e4SLinus Torvalds 	}
26841da177e4SLinus Torvalds }
26851da177e4SLinus Torvalds 
26861da177e4SLinus Torvalds static void addrconf_dev_config(struct net_device *dev)
26871da177e4SLinus Torvalds {
26881da177e4SLinus Torvalds 	struct in6_addr addr;
26891da177e4SLinus Torvalds 	struct inet6_dev *idev;
26901da177e4SLinus Torvalds 
26911da177e4SLinus Torvalds 	ASSERT_RTNL();
26921da177e4SLinus Torvalds 
269374235a25SHerbert Xu 	if ((dev->type != ARPHRD_ETHER) &&
269474235a25SHerbert Xu 	    (dev->type != ARPHRD_FDDI) &&
269574235a25SHerbert Xu 	    (dev->type != ARPHRD_ARCNET) &&
269606a4c1c5Salex.bluesman.smirnov@gmail.com 	    (dev->type != ARPHRD_INFINIBAND) &&
2697cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	    (dev->type != ARPHRD_IEEE802154) &&
2698cb6bf355SYOSHIFUJI Hideaki / 吉藤英明 	    (dev->type != ARPHRD_IEEE1394)) {
269974235a25SHerbert Xu 		/* Alas, we support only Ethernet autoconfiguration. */
270074235a25SHerbert Xu 		return;
270174235a25SHerbert Xu 	}
270274235a25SHerbert Xu 
27031da177e4SLinus Torvalds 	idev = addrconf_add_dev(dev);
270464e724f6SBrian Haley 	if (IS_ERR(idev))
27051da177e4SLinus Torvalds 		return;
27061da177e4SLinus Torvalds 
27071da177e4SLinus Torvalds 	memset(&addr, 0, sizeof(struct in6_addr));
27081da177e4SLinus Torvalds 	addr.s6_addr32[0] = htonl(0xFE800000);
27091da177e4SLinus Torvalds 
27101da177e4SLinus Torvalds 	if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
27111da177e4SLinus Torvalds 		addrconf_add_linklocal(idev, &addr);
27121da177e4SLinus Torvalds }
27131da177e4SLinus Torvalds 
271407a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
27151da177e4SLinus Torvalds static void addrconf_sit_config(struct net_device *dev)
27161da177e4SLinus Torvalds {
27171da177e4SLinus Torvalds 	struct inet6_dev *idev;
27181da177e4SLinus Torvalds 
27191da177e4SLinus Torvalds 	ASSERT_RTNL();
27201da177e4SLinus Torvalds 
27211da177e4SLinus Torvalds 	/*
27221da177e4SLinus Torvalds 	 * Configure the tunnel with one of our IPv4
27231da177e4SLinus Torvalds 	 * addresses... we should configure all of
27241da177e4SLinus Torvalds 	 * our v4 addrs in the tunnel
27251da177e4SLinus Torvalds 	 */
27261da177e4SLinus Torvalds 
27271da177e4SLinus Torvalds 	if ((idev = ipv6_find_idev(dev)) == NULL) {
272891df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
27291da177e4SLinus Torvalds 		return;
27301da177e4SLinus Torvalds 	}
27311da177e4SLinus Torvalds 
2732c7dc89c0SFred L. Templin 	if (dev->priv_flags & IFF_ISATAP) {
2733c7dc89c0SFred L. Templin 		struct in6_addr addr;
2734c7dc89c0SFred L. Templin 
2735c7dc89c0SFred L. Templin 		ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
2736c7dc89c0SFred L. Templin 		addrconf_prefix_route(&addr, 64, dev, 0, 0);
2737c7dc89c0SFred L. Templin 		if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2738c7dc89c0SFred L. Templin 			addrconf_add_linklocal(idev, &addr);
2739c7dc89c0SFred L. Templin 		return;
2740c7dc89c0SFred L. Templin 	}
2741c7dc89c0SFred L. Templin 
27421da177e4SLinus Torvalds 	sit_add_v4_addrs(idev);
27431da177e4SLinus Torvalds 
274462b54dd9SNicolas Dichtel 	if (dev->flags&IFF_POINTOPOINT)
27451da177e4SLinus Torvalds 		addrconf_add_mroute(dev);
274662b54dd9SNicolas Dichtel 	else
27471da177e4SLinus Torvalds 		sit_route_add(dev);
27481da177e4SLinus Torvalds }
27490be669bbSJoerg Roedel #endif
27501da177e4SLinus Torvalds 
275107a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE)
2752aee80b54Sstephen hemminger static void addrconf_gre_config(struct net_device *dev)
2753aee80b54Sstephen hemminger {
2754aee80b54Sstephen hemminger 	struct inet6_dev *idev;
2755aee80b54Sstephen hemminger 	struct in6_addr addr;
2756aee80b54Sstephen hemminger 
2757f3213831SJoe Perches 	pr_info("%s(%s)\n", __func__, dev->name);
2758aee80b54Sstephen hemminger 
2759aee80b54Sstephen hemminger 	ASSERT_RTNL();
2760aee80b54Sstephen hemminger 
2761aee80b54Sstephen hemminger 	if ((idev = ipv6_find_idev(dev)) == NULL) {
276291df42beSJoe Perches 		pr_debug("%s: add_dev failed\n", __func__);
2763aee80b54Sstephen hemminger 		return;
2764aee80b54Sstephen hemminger 	}
2765aee80b54Sstephen hemminger 
2766aee80b54Sstephen hemminger 	ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
2767aee80b54Sstephen hemminger 	addrconf_prefix_route(&addr, 64, dev, 0, 0);
2768aee80b54Sstephen hemminger 
2769aee80b54Sstephen hemminger 	if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2770aee80b54Sstephen hemminger 		addrconf_add_linklocal(idev, &addr);
2771aee80b54Sstephen hemminger }
2772aee80b54Sstephen hemminger #endif
2773aee80b54Sstephen hemminger 
27741da177e4SLinus Torvalds static inline int
27751da177e4SLinus Torvalds ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
27761da177e4SLinus Torvalds {
27771da177e4SLinus Torvalds 	struct in6_addr lladdr;
27781da177e4SLinus Torvalds 
277995c385b4SNeil Horman 	if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
27801da177e4SLinus Torvalds 		addrconf_add_linklocal(idev, &lladdr);
27811da177e4SLinus Torvalds 		return 0;
27821da177e4SLinus Torvalds 	}
27831da177e4SLinus Torvalds 	return -1;
27841da177e4SLinus Torvalds }
27851da177e4SLinus Torvalds 
27861da177e4SLinus Torvalds static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
27871da177e4SLinus Torvalds {
27881da177e4SLinus Torvalds 	struct net_device *link_dev;
2789c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
27901da177e4SLinus Torvalds 
27911da177e4SLinus Torvalds 	/* first try to inherit the link-local address from the link device */
27921da177e4SLinus Torvalds 	if (idev->dev->iflink &&
27936fda7350SBenjamin Thery 	    (link_dev = __dev_get_by_index(net, idev->dev->iflink))) {
27941da177e4SLinus Torvalds 		if (!ipv6_inherit_linklocal(idev, link_dev))
27951da177e4SLinus Torvalds 			return;
27961da177e4SLinus Torvalds 	}
27971da177e4SLinus Torvalds 	/* then try to inherit it from any device */
27986fda7350SBenjamin Thery 	for_each_netdev(net, link_dev) {
27991da177e4SLinus Torvalds 		if (!ipv6_inherit_linklocal(idev, link_dev))
28001da177e4SLinus Torvalds 			return;
28011da177e4SLinus Torvalds 	}
280291df42beSJoe Perches 	pr_debug("init ip6-ip6: add_linklocal failed\n");
28031da177e4SLinus Torvalds }
28041da177e4SLinus Torvalds 
28051da177e4SLinus Torvalds /*
28061da177e4SLinus Torvalds  * Autoconfigure tunnel with a link-local address so routing protocols,
28071da177e4SLinus Torvalds  * DHCPv6, MLD etc. can be run over the virtual link
28081da177e4SLinus Torvalds  */
28091da177e4SLinus Torvalds 
28101da177e4SLinus Torvalds static void addrconf_ip6_tnl_config(struct net_device *dev)
28111da177e4SLinus Torvalds {
28121da177e4SLinus Torvalds 	struct inet6_dev *idev;
28131da177e4SLinus Torvalds 
28141da177e4SLinus Torvalds 	ASSERT_RTNL();
28151da177e4SLinus Torvalds 
2816e21e8467SStephen Hemminger 	idev = addrconf_add_dev(dev);
281764e724f6SBrian Haley 	if (IS_ERR(idev)) {
281891df42beSJoe Perches 		pr_debug("init ip6-ip6: add_dev failed\n");
28191da177e4SLinus Torvalds 		return;
28201da177e4SLinus Torvalds 	}
28211da177e4SLinus Torvalds 	ip6_tnl_add_linklocal(idev);
28221da177e4SLinus Torvalds }
28231da177e4SLinus Torvalds 
28241da177e4SLinus Torvalds static int addrconf_notify(struct notifier_block *this, unsigned long event,
28251da177e4SLinus Torvalds 			   void *data)
28261da177e4SLinus Torvalds {
28271da177e4SLinus Torvalds 	struct net_device *dev = (struct net_device *) data;
2828748e2d93SEric Dumazet 	struct inet6_dev *idev = __in6_dev_get(dev);
2829c5e33bddSYOSHIFUJI Hideaki 	int run_pending = 0;
2830b217d616SHerbert Xu 	int err;
28311da177e4SLinus Torvalds 
28321da177e4SLinus Torvalds 	switch (event) {
283345ba9dd2SYOSHIFUJI Hideaki 	case NETDEV_REGISTER:
283474235a25SHerbert Xu 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
283545ba9dd2SYOSHIFUJI Hideaki 			idev = ipv6_add_dev(dev);
283645ba9dd2SYOSHIFUJI Hideaki 			if (!idev)
2837b217d616SHerbert Xu 				return notifier_from_errno(-ENOMEM);
283845ba9dd2SYOSHIFUJI Hideaki 		}
283945ba9dd2SYOSHIFUJI Hideaki 		break;
2840bcdd553fSStephen Hemminger 
28411da177e4SLinus Torvalds 	case NETDEV_UP:
28423c21edbdSYOSHIFUJI Hideaki 	case NETDEV_CHANGE:
2843c2edacf8SJay Vosburgh 		if (dev->flags & IFF_SLAVE)
2844c2edacf8SJay Vosburgh 			break;
2845c2edacf8SJay Vosburgh 
28463c21edbdSYOSHIFUJI Hideaki 		if (event == NETDEV_UP) {
2847f24e3d65SMitsuru Chinen 			if (!addrconf_qdisc_ok(dev)) {
28483c21edbdSYOSHIFUJI Hideaki 				/* device is not ready yet. */
2849f3213831SJoe Perches 				pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
28503c21edbdSYOSHIFUJI Hideaki 					dev->name);
28513c21edbdSYOSHIFUJI Hideaki 				break;
28523c21edbdSYOSHIFUJI Hideaki 			}
285399081049SKristian Slavov 
2854d31c7b8fSEvgeniy Polyakov 			if (!idev && dev->mtu >= IPV6_MIN_MTU)
2855d31c7b8fSEvgeniy Polyakov 				idev = ipv6_add_dev(dev);
2856d31c7b8fSEvgeniy Polyakov 
2857e3ec6cfcSBenjamin Thery 			if (idev) {
285899081049SKristian Slavov 				idev->if_flags |= IF_READY;
2859e3ec6cfcSBenjamin Thery 				run_pending = 1;
2860e3ec6cfcSBenjamin Thery 			}
28613c21edbdSYOSHIFUJI Hideaki 		} else {
2862f24e3d65SMitsuru Chinen 			if (!addrconf_qdisc_ok(dev)) {
28633c21edbdSYOSHIFUJI Hideaki 				/* device is still not ready. */
28643c21edbdSYOSHIFUJI Hideaki 				break;
28653c21edbdSYOSHIFUJI Hideaki 			}
28663c21edbdSYOSHIFUJI Hideaki 
28673c21edbdSYOSHIFUJI Hideaki 			if (idev) {
2868bcdd553fSStephen Hemminger 				if (idev->if_flags & IF_READY)
28693c21edbdSYOSHIFUJI Hideaki 					/* device is already configured. */
28703c21edbdSYOSHIFUJI Hideaki 					break;
28713c21edbdSYOSHIFUJI Hideaki 				idev->if_flags |= IF_READY;
28723c21edbdSYOSHIFUJI Hideaki 			}
28733c21edbdSYOSHIFUJI Hideaki 
2874f3213831SJoe Perches 			pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
28753c21edbdSYOSHIFUJI Hideaki 				dev->name);
28763c21edbdSYOSHIFUJI Hideaki 
2877c5e33bddSYOSHIFUJI Hideaki 			run_pending = 1;
28783c21edbdSYOSHIFUJI Hideaki 		}
28793c21edbdSYOSHIFUJI Hideaki 
28801da177e4SLinus Torvalds 		switch (dev->type) {
288107a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_SIT)
28821da177e4SLinus Torvalds 		case ARPHRD_SIT:
28831da177e4SLinus Torvalds 			addrconf_sit_config(dev);
28841da177e4SLinus Torvalds 			break;
28850be669bbSJoerg Roedel #endif
288607a93626SAmerigo Wang #if IS_ENABLED(CONFIG_NET_IPGRE)
2887aee80b54Sstephen hemminger 		case ARPHRD_IPGRE:
2888aee80b54Sstephen hemminger 			addrconf_gre_config(dev);
2889aee80b54Sstephen hemminger 			break;
2890aee80b54Sstephen hemminger #endif
28911da177e4SLinus Torvalds 		case ARPHRD_TUNNEL6:
28921da177e4SLinus Torvalds 			addrconf_ip6_tnl_config(dev);
28931da177e4SLinus Torvalds 			break;
28941da177e4SLinus Torvalds 		case ARPHRD_LOOPBACK:
28951da177e4SLinus Torvalds 			init_loopback(dev);
28961da177e4SLinus Torvalds 			break;
28971da177e4SLinus Torvalds 
28981da177e4SLinus Torvalds 		default:
28991da177e4SLinus Torvalds 			addrconf_dev_config(dev);
29001da177e4SLinus Torvalds 			break;
29013ff50b79SStephen Hemminger 		}
2902bcdd553fSStephen Hemminger 
29031da177e4SLinus Torvalds 		if (idev) {
2904c5e33bddSYOSHIFUJI Hideaki 			if (run_pending)
2905c5e33bddSYOSHIFUJI Hideaki 				addrconf_dad_run(idev);
2906c5e33bddSYOSHIFUJI Hideaki 
2907bcdd553fSStephen Hemminger 			/*
2908bcdd553fSStephen Hemminger 			 * If the MTU changed during the interface down,
2909bcdd553fSStephen Hemminger 			 * when the interface up, the changed MTU must be
2910bcdd553fSStephen Hemminger 			 * reflected in the idev as well as routers.
29111da177e4SLinus Torvalds 			 */
2912bcdd553fSStephen Hemminger 			if (idev->cnf.mtu6 != dev->mtu &&
2913bcdd553fSStephen Hemminger 			    dev->mtu >= IPV6_MIN_MTU) {
29141da177e4SLinus Torvalds 				rt6_mtu_change(dev, dev->mtu);
29151da177e4SLinus Torvalds 				idev->cnf.mtu6 = dev->mtu;
29161da177e4SLinus Torvalds 			}
29171da177e4SLinus Torvalds 			idev->tstamp = jiffies;
29181da177e4SLinus Torvalds 			inet6_ifinfo_notify(RTM_NEWLINK, idev);
2919bcdd553fSStephen Hemminger 
2920bcdd553fSStephen Hemminger 			/*
2921bcdd553fSStephen Hemminger 			 * If the changed mtu during down is lower than
2922bcdd553fSStephen Hemminger 			 * IPV6_MIN_MTU stop IPv6 on this interface.
29231da177e4SLinus Torvalds 			 */
29241da177e4SLinus Torvalds 			if (dev->mtu < IPV6_MIN_MTU)
2925bcdd553fSStephen Hemminger 				addrconf_ifdown(dev, 1);
29261da177e4SLinus Torvalds 		}
29271da177e4SLinus Torvalds 		break;
29281da177e4SLinus Torvalds 
29291da177e4SLinus Torvalds 	case NETDEV_CHANGEMTU:
29301da177e4SLinus Torvalds 		if (idev && dev->mtu >= IPV6_MIN_MTU) {
29311da177e4SLinus Torvalds 			rt6_mtu_change(dev, dev->mtu);
29321da177e4SLinus Torvalds 			idev->cnf.mtu6 = dev->mtu;
29331da177e4SLinus Torvalds 			break;
29341da177e4SLinus Torvalds 		}
29351da177e4SLinus Torvalds 
2936d31c7b8fSEvgeniy Polyakov 		if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2937d31c7b8fSEvgeniy Polyakov 			idev = ipv6_add_dev(dev);
2938d31c7b8fSEvgeniy Polyakov 			if (idev)
2939d31c7b8fSEvgeniy Polyakov 				break;
2940d31c7b8fSEvgeniy Polyakov 		}
2941d31c7b8fSEvgeniy Polyakov 
2942bcdd553fSStephen Hemminger 		/*
2943bcdd553fSStephen Hemminger 		 * MTU falled under IPV6_MIN_MTU.
2944bcdd553fSStephen Hemminger 		 * Stop IPv6 on this interface.
2945bcdd553fSStephen Hemminger 		 */
29461da177e4SLinus Torvalds 
29471da177e4SLinus Torvalds 	case NETDEV_DOWN:
29481da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
29491da177e4SLinus Torvalds 		/*
29501da177e4SLinus Torvalds 		 *	Remove all addresses from this interface.
29511da177e4SLinus Torvalds 		 */
29521da177e4SLinus Torvalds 		addrconf_ifdown(dev, event != NETDEV_DOWN);
29531da177e4SLinus Torvalds 		break;
29543c21edbdSYOSHIFUJI Hideaki 
29551da177e4SLinus Torvalds 	case NETDEV_CHANGENAME:
29561da177e4SLinus Torvalds 		if (idev) {
29575632c515SStephen Hemminger 			snmp6_unregister_dev(idev);
2958408c4768SPavel Emelyanov 			addrconf_sysctl_unregister(idev);
2959f52295a9SPavel Emelyanov 			addrconf_sysctl_register(idev);
2960b217d616SHerbert Xu 			err = snmp6_register_dev(idev);
2961b217d616SHerbert Xu 			if (err)
2962b217d616SHerbert Xu 				return notifier_from_errno(err);
29635632c515SStephen Hemminger 		}
29641da177e4SLinus Torvalds 		break;
2965bcdd553fSStephen Hemminger 
296693d9b7d7SJiri Pirko 	case NETDEV_PRE_TYPE_CHANGE:
296793d9b7d7SJiri Pirko 	case NETDEV_POST_TYPE_CHANGE:
296893d9b7d7SJiri Pirko 		addrconf_type_change(dev, event);
296975c78500SMoni Shoua 		break;
29703ff50b79SStephen Hemminger 	}
29711da177e4SLinus Torvalds 
29721da177e4SLinus Torvalds 	return NOTIFY_OK;
29731da177e4SLinus Torvalds }
29741da177e4SLinus Torvalds 
29751da177e4SLinus Torvalds /*
29761da177e4SLinus Torvalds  *	addrconf module should be notified of a device going up
29771da177e4SLinus Torvalds  */
29781da177e4SLinus Torvalds static struct notifier_block ipv6_dev_notf = {
29791da177e4SLinus Torvalds 	.notifier_call = addrconf_notify,
29801da177e4SLinus Torvalds };
29811da177e4SLinus Torvalds 
298293d9b7d7SJiri Pirko static void addrconf_type_change(struct net_device *dev, unsigned long event)
298375c78500SMoni Shoua {
298475c78500SMoni Shoua 	struct inet6_dev *idev;
298575c78500SMoni Shoua 	ASSERT_RTNL();
298675c78500SMoni Shoua 
298775c78500SMoni Shoua 	idev = __in6_dev_get(dev);
298875c78500SMoni Shoua 
298993d9b7d7SJiri Pirko 	if (event == NETDEV_POST_TYPE_CHANGE)
299075c78500SMoni Shoua 		ipv6_mc_remap(idev);
299193d9b7d7SJiri Pirko 	else if (event == NETDEV_PRE_TYPE_CHANGE)
299275c78500SMoni Shoua 		ipv6_mc_unmap(idev);
299375c78500SMoni Shoua }
299475c78500SMoni Shoua 
29951da177e4SLinus Torvalds static int addrconf_ifdown(struct net_device *dev, int how)
29961da177e4SLinus Torvalds {
2997c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(dev);
2998502a2ffdSstephen hemminger 	struct inet6_dev *idev;
2999502a2ffdSstephen hemminger 	struct inet6_ifaddr *ifa;
300073a8bd74SDavid S. Miller 	int state, i;
30011da177e4SLinus Torvalds 
30021da177e4SLinus Torvalds 	ASSERT_RTNL();
30031da177e4SLinus Torvalds 
3004f3db4851SDaniel Lezcano 	rt6_ifdown(net, dev);
300573a8bd74SDavid S. Miller 	neigh_ifdown(&nd_tbl, dev);
30061da177e4SLinus Torvalds 
30071da177e4SLinus Torvalds 	idev = __in6_dev_get(dev);
30081da177e4SLinus Torvalds 	if (idev == NULL)
30091da177e4SLinus Torvalds 		return -ENODEV;
30101da177e4SLinus Torvalds 
3011bcdd553fSStephen Hemminger 	/*
3012bcdd553fSStephen Hemminger 	 * Step 1: remove reference to ipv6 device from parent device.
3013bcdd553fSStephen Hemminger 	 *	   Do not dev_put!
30141da177e4SLinus Torvalds 	 */
3015439e2385SDenis V. Lunev 	if (how) {
30161da177e4SLinus Torvalds 		idev->dead = 1;
30178814c4b5SYOSHIFUJI Hideaki 
30188814c4b5SYOSHIFUJI Hideaki 		/* protected by rtnl_lock */
3019a9b3cd7fSStephen Hemminger 		RCU_INIT_POINTER(dev->ip6_ptr, NULL);
30201da177e4SLinus Torvalds 
30211da177e4SLinus Torvalds 		/* Step 1.5: remove snmp6 entry */
30221da177e4SLinus Torvalds 		snmp6_unregister_dev(idev);
30231da177e4SLinus Torvalds 
30241da177e4SLinus Torvalds 	}
30251da177e4SLinus Torvalds 
302673a8bd74SDavid S. Miller 	/* Step 2: clear hash table */
302773a8bd74SDavid S. Miller 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
302873a8bd74SDavid S. Miller 		struct hlist_head *h = &inet6_addr_lst[i];
302973a8bd74SDavid S. Miller 
303073a8bd74SDavid S. Miller 		spin_lock_bh(&addrconf_hash_lock);
303173a8bd74SDavid S. Miller 	restart:
3032b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(ifa, h, addr_lst) {
303373a8bd74SDavid S. Miller 			if (ifa->idev == idev) {
303473a8bd74SDavid S. Miller 				hlist_del_init_rcu(&ifa->addr_lst);
303573a8bd74SDavid S. Miller 				addrconf_del_timer(ifa);
303673a8bd74SDavid S. Miller 				goto restart;
303773a8bd74SDavid S. Miller 			}
303873a8bd74SDavid S. Miller 		}
303973a8bd74SDavid S. Miller 		spin_unlock_bh(&addrconf_hash_lock);
304073a8bd74SDavid S. Miller 	}
304173a8bd74SDavid S. Miller 
30421da177e4SLinus Torvalds 	write_lock_bh(&idev->lock);
30431da177e4SLinus Torvalds 
3044bcdd553fSStephen Hemminger 	/* Step 2: clear flags for stateless addrconf */
3045439e2385SDenis V. Lunev 	if (!how)
30463c21edbdSYOSHIFUJI Hideaki 		idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
30471da177e4SLinus Torvalds 
30481da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
3049439e2385SDenis V. Lunev 	if (how && del_timer(&idev->regen_timer))
30501da177e4SLinus Torvalds 		in6_dev_put(idev);
30511da177e4SLinus Torvalds 
3052bcdd553fSStephen Hemminger 	/* Step 3: clear tempaddr list */
3053372e6c8fSstephen hemminger 	while (!list_empty(&idev->tempaddr_list)) {
3054372e6c8fSstephen hemminger 		ifa = list_first_entry(&idev->tempaddr_list,
3055372e6c8fSstephen hemminger 				       struct inet6_ifaddr, tmp_list);
3056372e6c8fSstephen hemminger 		list_del(&ifa->tmp_list);
30571da177e4SLinus Torvalds 		write_unlock_bh(&idev->lock);
30581da177e4SLinus Torvalds 		spin_lock_bh(&ifa->lock);
30591da177e4SLinus Torvalds 
30601da177e4SLinus Torvalds 		if (ifa->ifpub) {
30611da177e4SLinus Torvalds 			in6_ifa_put(ifa->ifpub);
30621da177e4SLinus Torvalds 			ifa->ifpub = NULL;
30631da177e4SLinus Torvalds 		}
30641da177e4SLinus Torvalds 		spin_unlock_bh(&ifa->lock);
30651da177e4SLinus Torvalds 		in6_ifa_put(ifa);
30661da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
30671da177e4SLinus Torvalds 	}
30681da177e4SLinus Torvalds #endif
30698f37ada5Sstephen hemminger 
3070502a2ffdSstephen hemminger 	while (!list_empty(&idev->addr_list)) {
3071502a2ffdSstephen hemminger 		ifa = list_first_entry(&idev->addr_list,
3072502a2ffdSstephen hemminger 				       struct inet6_ifaddr, if_list);
307384e8b803Sstephen hemminger 		addrconf_del_timer(ifa);
307484e8b803Sstephen hemminger 
3075502a2ffdSstephen hemminger 		list_del(&ifa->if_list);
30761da177e4SLinus Torvalds 
30774c5ff6a6SHerbert Xu 		write_unlock_bh(&idev->lock);
307873a8bd74SDavid S. Miller 
30794c5ff6a6SHerbert Xu 		spin_lock_bh(&ifa->state_lock);
30804c5ff6a6SHerbert Xu 		state = ifa->state;
30814c5ff6a6SHerbert Xu 		ifa->state = INET6_IFADDR_STATE_DEAD;
30824c5ff6a6SHerbert Xu 		spin_unlock_bh(&ifa->state_lock);
30834c5ff6a6SHerbert Xu 
30849d82ca98SJohn Fastabend 		if (state != INET6_IFADDR_STATE_DEAD) {
30851da177e4SLinus Torvalds 			__ipv6_ifa_notify(RTM_DELADDR, ifa);
308673a8bd74SDavid S. Miller 			atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa);
30872de79570SLorenzo Colitti 		}
30889d82ca98SJohn Fastabend 		in6_ifa_put(ifa);
308973a8bd74SDavid S. Miller 
30901da177e4SLinus Torvalds 		write_lock_bh(&idev->lock);
30911da177e4SLinus Torvalds 	}
30928f37ada5Sstephen hemminger 
30931da177e4SLinus Torvalds 	write_unlock_bh(&idev->lock);
30941da177e4SLinus Torvalds 
30951da177e4SLinus Torvalds 	/* Step 5: Discard multicast list */
3096439e2385SDenis V. Lunev 	if (how)
30971da177e4SLinus Torvalds 		ipv6_mc_destroy_dev(idev);
30981da177e4SLinus Torvalds 	else
30991da177e4SLinus Torvalds 		ipv6_mc_down(idev);
31001da177e4SLinus Torvalds 
31011da177e4SLinus Torvalds 	idev->tstamp = jiffies;
31021da177e4SLinus Torvalds 
3103bcdd553fSStephen Hemminger 	/* Last: Shot the device (if unregistered) */
3104439e2385SDenis V. Lunev 	if (how) {
3105408c4768SPavel Emelyanov 		addrconf_sysctl_unregister(idev);
31061da177e4SLinus Torvalds 		neigh_parms_release(&nd_tbl, idev->nd_parms);
31071da177e4SLinus Torvalds 		neigh_ifdown(&nd_tbl, dev);
31081da177e4SLinus Torvalds 		in6_dev_put(idev);
31091da177e4SLinus Torvalds 	}
31101da177e4SLinus Torvalds 	return 0;
31111da177e4SLinus Torvalds }
31121da177e4SLinus Torvalds 
31131da177e4SLinus Torvalds static void addrconf_rs_timer(unsigned long data)
31141da177e4SLinus Torvalds {
31151da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
31165b2a1953Sstephen hemminger 	struct inet6_dev *idev = ifp->idev;
31171da177e4SLinus Torvalds 
31185b2a1953Sstephen hemminger 	read_lock(&idev->lock);
31195b2a1953Sstephen hemminger 	if (idev->dead || !(idev->if_flags & IF_READY))
31201da177e4SLinus Torvalds 		goto out;
31211da177e4SLinus Torvalds 
31229ba2add3SShmulik Ladkani 	if (!ipv6_accept_ra(idev))
31231da177e4SLinus Torvalds 		goto out;
31245b2a1953Sstephen hemminger 
31255b2a1953Sstephen hemminger 	/* Announcement received after solicitation was sent */
31265b2a1953Sstephen hemminger 	if (idev->if_flags & IF_RA_RCVD)
31275b2a1953Sstephen hemminger 		goto out;
31281da177e4SLinus Torvalds 
31291da177e4SLinus Torvalds 	spin_lock(&ifp->lock);
31305b2a1953Sstephen hemminger 	if (ifp->probes++ < idev->cnf.rtr_solicits) {
31311da177e4SLinus Torvalds 		/* The wait after the last probe can be shorter */
31321da177e4SLinus Torvalds 		addrconf_mod_timer(ifp, AC_RS,
31335b2a1953Sstephen hemminger 				   (ifp->probes == idev->cnf.rtr_solicits) ?
31345b2a1953Sstephen hemminger 				   idev->cnf.rtr_solicit_delay :
31355b2a1953Sstephen hemminger 				   idev->cnf.rtr_solicit_interval);
31361da177e4SLinus Torvalds 		spin_unlock(&ifp->lock);
31371da177e4SLinus Torvalds 
31385b2a1953Sstephen hemminger 		ndisc_send_rs(idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
31391da177e4SLinus Torvalds 	} else {
31401da177e4SLinus Torvalds 		spin_unlock(&ifp->lock);
31411da177e4SLinus Torvalds 		/*
31421da177e4SLinus Torvalds 		 * Note: we do not support deprecated "all on-link"
31431da177e4SLinus Torvalds 		 * assumption any longer.
31441da177e4SLinus Torvalds 		 */
314591df42beSJoe Perches 		pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
31461da177e4SLinus Torvalds 	}
31471da177e4SLinus Torvalds 
31481da177e4SLinus Torvalds out:
31495b2a1953Sstephen hemminger 	read_unlock(&idev->lock);
31501da177e4SLinus Torvalds 	in6_ifa_put(ifp);
31511da177e4SLinus Torvalds }
31521da177e4SLinus Torvalds 
31531da177e4SLinus Torvalds /*
31541da177e4SLinus Torvalds  *	Duplicate Address Detection
31551da177e4SLinus Torvalds  */
31563c21edbdSYOSHIFUJI Hideaki static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
31573c21edbdSYOSHIFUJI Hideaki {
31583c21edbdSYOSHIFUJI Hideaki 	unsigned long rand_num;
31593c21edbdSYOSHIFUJI Hideaki 	struct inet6_dev *idev = ifp->idev;
31603c21edbdSYOSHIFUJI Hideaki 
316195c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
316295c385b4SNeil Horman 		rand_num = 0;
316395c385b4SNeil Horman 	else
31643c21edbdSYOSHIFUJI Hideaki 		rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
316595c385b4SNeil Horman 
31663c21edbdSYOSHIFUJI Hideaki 	ifp->probes = idev->cnf.dad_transmits;
31673c21edbdSYOSHIFUJI Hideaki 	addrconf_mod_timer(ifp, AC_DAD, rand_num);
31683c21edbdSYOSHIFUJI Hideaki }
31693c21edbdSYOSHIFUJI Hideaki 
3170cf22f9a2SDavid S. Miller static void addrconf_dad_start(struct inet6_ifaddr *ifp)
31711da177e4SLinus Torvalds {
31721da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
31731da177e4SLinus Torvalds 	struct net_device *dev = idev->dev;
31741da177e4SLinus Torvalds 
31751da177e4SLinus Torvalds 	addrconf_join_solict(dev, &ifp->addr);
31761da177e4SLinus Torvalds 
31771da177e4SLinus Torvalds 	net_srandom(ifp->addr.s6_addr32[3]);
31781da177e4SLinus Torvalds 
31791da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
3180622ccdf1SHerbert Xu 	spin_lock(&ifp->lock);
3181e9d3e084SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DEAD)
31821da177e4SLinus Torvalds 		goto out;
31831da177e4SLinus Torvalds 
31841da177e4SLinus Torvalds 	if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
31851b34be74SYOSHIFUJI Hideaki 	    idev->cnf.accept_dad < 1 ||
318655ebaef1SNoriaki TAKAMIYA 	    !(ifp->flags&IFA_F_TENTATIVE) ||
318755ebaef1SNoriaki TAKAMIYA 	    ifp->flags & IFA_F_NODAD) {
3188cc411d0bSBrian Haley 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
318921809fafSstephen hemminger 		spin_unlock(&ifp->lock);
31901da177e4SLinus Torvalds 		read_unlock_bh(&idev->lock);
31911da177e4SLinus Torvalds 
31921da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
31931da177e4SLinus Torvalds 		return;
31941da177e4SLinus Torvalds 	}
31951da177e4SLinus Torvalds 
31966732badeSYOSHIFUJI Hideaki 	if (!(idev->if_flags & IF_READY)) {
319721809fafSstephen hemminger 		spin_unlock(&ifp->lock);
31986732badeSYOSHIFUJI Hideaki 		read_unlock_bh(&idev->lock);
31993c21edbdSYOSHIFUJI Hideaki 		/*
3200590a9887SMasatake YAMATO 		 * If the device is not ready:
32013c21edbdSYOSHIFUJI Hideaki 		 * - keep it tentative if it is a permanent address.
32023c21edbdSYOSHIFUJI Hideaki 		 * - otherwise, kill it.
32033c21edbdSYOSHIFUJI Hideaki 		 */
32043c21edbdSYOSHIFUJI Hideaki 		in6_ifa_hold(ifp);
3205cc411d0bSBrian Haley 		addrconf_dad_stop(ifp, 0);
32066732badeSYOSHIFUJI Hideaki 		return;
32073c21edbdSYOSHIFUJI Hideaki 	}
320895c385b4SNeil Horman 
320995c385b4SNeil Horman 	/*
321095c385b4SNeil Horman 	 * Optimistic nodes can start receiving
321195c385b4SNeil Horman 	 * Frames right away
321295c385b4SNeil Horman 	 */
321395c385b4SNeil Horman 	if (ifp->flags & IFA_F_OPTIMISTIC)
321495c385b4SNeil Horman 		ip6_ins_rt(ifp->rt);
321595c385b4SNeil Horman 
32166732badeSYOSHIFUJI Hideaki 	addrconf_dad_kick(ifp);
32171da177e4SLinus Torvalds out:
3218622ccdf1SHerbert Xu 	spin_unlock(&ifp->lock);
32191da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
32201da177e4SLinus Torvalds }
32211da177e4SLinus Torvalds 
32221da177e4SLinus Torvalds static void addrconf_dad_timer(unsigned long data)
32231da177e4SLinus Torvalds {
32241da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
32251da177e4SLinus Torvalds 	struct inet6_dev *idev = ifp->idev;
32261da177e4SLinus Torvalds 	struct in6_addr mcaddr;
32271da177e4SLinus Torvalds 
3228f2344a13SHerbert Xu 	if (!ifp->probes && addrconf_dad_end(ifp))
3229f2344a13SHerbert Xu 		goto out;
3230f2344a13SHerbert Xu 
3231122e4519Sstephen hemminger 	read_lock(&idev->lock);
3232122e4519Sstephen hemminger 	if (idev->dead || !(idev->if_flags & IF_READY)) {
3233122e4519Sstephen hemminger 		read_unlock(&idev->lock);
32341da177e4SLinus Torvalds 		goto out;
32351da177e4SLinus Torvalds 	}
323621809fafSstephen hemminger 
323721809fafSstephen hemminger 	spin_lock(&ifp->lock);
3238622ccdf1SHerbert Xu 	if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3239622ccdf1SHerbert Xu 		spin_unlock(&ifp->lock);
3240622ccdf1SHerbert Xu 		read_unlock(&idev->lock);
3241622ccdf1SHerbert Xu 		goto out;
3242622ccdf1SHerbert Xu 	}
3243622ccdf1SHerbert Xu 
32441da177e4SLinus Torvalds 	if (ifp->probes == 0) {
32451da177e4SLinus Torvalds 		/*
32461da177e4SLinus Torvalds 		 * DAD was successful
32471da177e4SLinus Torvalds 		 */
32481da177e4SLinus Torvalds 
3249cc411d0bSBrian Haley 		ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
325021809fafSstephen hemminger 		spin_unlock(&ifp->lock);
3251122e4519Sstephen hemminger 		read_unlock(&idev->lock);
32521da177e4SLinus Torvalds 
32531da177e4SLinus Torvalds 		addrconf_dad_completed(ifp);
32541da177e4SLinus Torvalds 
32551da177e4SLinus Torvalds 		goto out;
32561da177e4SLinus Torvalds 	}
32571da177e4SLinus Torvalds 
32581da177e4SLinus Torvalds 	ifp->probes--;
32591da177e4SLinus Torvalds 	addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
326021809fafSstephen hemminger 	spin_unlock(&ifp->lock);
3261122e4519Sstephen hemminger 	read_unlock(&idev->lock);
32621da177e4SLinus Torvalds 
32631da177e4SLinus Torvalds 	/* send a neighbour solicitation for our addr */
32641da177e4SLinus Torvalds 	addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
3265d7aabf22SYOSHIFUJI Hideaki 	ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
32661da177e4SLinus Torvalds out:
32671da177e4SLinus Torvalds 	in6_ifa_put(ifp);
32681da177e4SLinus Torvalds }
32691da177e4SLinus Torvalds 
32701da177e4SLinus Torvalds static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
32711da177e4SLinus Torvalds {
32721da177e4SLinus Torvalds 	struct net_device *dev = ifp->idev->dev;
32731da177e4SLinus Torvalds 
32741da177e4SLinus Torvalds 	/*
32751da177e4SLinus Torvalds 	 *	Configure the address for reception. Now it is valid.
32761da177e4SLinus Torvalds 	 */
32771da177e4SLinus Torvalds 
32781da177e4SLinus Torvalds 	ipv6_ifa_notify(RTM_NEWADDR, ifp);
32791da177e4SLinus Torvalds 
3280026359bcSTore Anderson 	/* If added prefix is link local and we are prepared to process
3281026359bcSTore Anderson 	   router advertisements, start sending router solicitations.
32821da177e4SLinus Torvalds 	 */
32831da177e4SLinus Torvalds 
3284aeaf6e9dSShmulik Ladkani 	if (ipv6_accept_ra(ifp->idev) &&
32851da177e4SLinus Torvalds 	    ifp->idev->cnf.rtr_solicits > 0 &&
32861da177e4SLinus Torvalds 	    (dev->flags&IFF_LOOPBACK) == 0 &&
32871da177e4SLinus Torvalds 	    (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
32881da177e4SLinus Torvalds 		/*
32891da177e4SLinus Torvalds 		 *	If a host as already performed a random delay
32901da177e4SLinus Torvalds 		 *	[...] as part of DAD [...] there is no need
32911da177e4SLinus Torvalds 		 *	to delay again before sending the first RS
32921da177e4SLinus Torvalds 		 */
3293f3ee4010SYOSHIFUJI Hideaki 		ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
32941da177e4SLinus Torvalds 
32951da177e4SLinus Torvalds 		spin_lock_bh(&ifp->lock);
32961da177e4SLinus Torvalds 		ifp->probes = 1;
32971da177e4SLinus Torvalds 		ifp->idev->if_flags |= IF_RS_SENT;
32981da177e4SLinus Torvalds 		addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
32991da177e4SLinus Torvalds 		spin_unlock_bh(&ifp->lock);
33001da177e4SLinus Torvalds 	}
33011da177e4SLinus Torvalds }
33021da177e4SLinus Torvalds 
3303e21e8467SStephen Hemminger static void addrconf_dad_run(struct inet6_dev *idev)
3304e21e8467SStephen Hemminger {
3305c5e33bddSYOSHIFUJI Hideaki 	struct inet6_ifaddr *ifp;
3306c5e33bddSYOSHIFUJI Hideaki 
3307c5e33bddSYOSHIFUJI Hideaki 	read_lock_bh(&idev->lock);
3308502a2ffdSstephen hemminger 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
330921809fafSstephen hemminger 		spin_lock(&ifp->lock);
3310f2344a13SHerbert Xu 		if (ifp->flags & IFA_F_TENTATIVE &&
3311f2344a13SHerbert Xu 		    ifp->state == INET6_IFADDR_STATE_DAD)
3312c5e33bddSYOSHIFUJI Hideaki 			addrconf_dad_kick(ifp);
3313f2344a13SHerbert Xu 		spin_unlock(&ifp->lock);
3314c5e33bddSYOSHIFUJI Hideaki 	}
3315c5e33bddSYOSHIFUJI Hideaki 	read_unlock_bh(&idev->lock);
3316c5e33bddSYOSHIFUJI Hideaki }
3317c5e33bddSYOSHIFUJI Hideaki 
33181da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
33191da177e4SLinus Torvalds struct if6_iter_state {
33203c40090aSDaniel Lezcano 	struct seq_net_private p;
33211da177e4SLinus Torvalds 	int bucket;
33221d578303SMihai Maruseac 	int offset;
33231da177e4SLinus Torvalds };
33241da177e4SLinus Torvalds 
33251d578303SMihai Maruseac static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
33261da177e4SLinus Torvalds {
33271da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa = NULL;
33281da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
33291218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
33301d578303SMihai Maruseac 	int p = 0;
33311da177e4SLinus Torvalds 
33321d578303SMihai Maruseac 	/* initial bucket if pos is 0 */
33331d578303SMihai Maruseac 	if (pos == 0) {
33341d578303SMihai Maruseac 		state->bucket = 0;
33351d578303SMihai Maruseac 		state->offset = 0;
33361d578303SMihai Maruseac 	}
33371d578303SMihai Maruseac 
33381d578303SMihai Maruseac 	for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
3339b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
33401d578303SMihai Maruseac 					 addr_lst) {
33419f0d3c27SEric Dumazet 			if (!net_eq(dev_net(ifa->idev->dev), net))
33429f0d3c27SEric Dumazet 				continue;
33431d578303SMihai Maruseac 			/* sync with offset */
33441d578303SMihai Maruseac 			if (p < state->offset) {
33451d578303SMihai Maruseac 				p++;
33461d578303SMihai Maruseac 				continue;
33471d578303SMihai Maruseac 			}
33481d578303SMihai Maruseac 			state->offset++;
33491da177e4SLinus Torvalds 			return ifa;
33501da177e4SLinus Torvalds 		}
33511d578303SMihai Maruseac 
33521d578303SMihai Maruseac 		/* prepare for next bucket */
33531d578303SMihai Maruseac 		state->offset = 0;
33541d578303SMihai Maruseac 		p = 0;
33551d578303SMihai Maruseac 	}
3356c2e21293Sstephen hemminger 	return NULL;
3357c2e21293Sstephen hemminger }
33581da177e4SLinus Torvalds 
3359c2e21293Sstephen hemminger static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
3360c2e21293Sstephen hemminger 					 struct inet6_ifaddr *ifa)
33611da177e4SLinus Torvalds {
33621da177e4SLinus Torvalds 	struct if6_iter_state *state = seq->private;
33631218854aSYOSHIFUJI Hideaki 	struct net *net = seq_file_net(seq);
33641da177e4SLinus Torvalds 
3365b67bfe0dSSasha Levin 	hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
33669f0d3c27SEric Dumazet 		if (!net_eq(dev_net(ifa->idev->dev), net))
33679f0d3c27SEric Dumazet 			continue;
33681d578303SMihai Maruseac 		state->offset++;
33691da177e4SLinus Torvalds 		return ifa;
33701d578303SMihai Maruseac 	}
33711da177e4SLinus Torvalds 
3372c2e21293Sstephen hemminger 	while (++state->bucket < IN6_ADDR_HSIZE) {
33731d578303SMihai Maruseac 		state->offset = 0;
3374b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu_bh(ifa,
3375c2e21293Sstephen hemminger 				     &inet6_addr_lst[state->bucket], addr_lst) {
33769f0d3c27SEric Dumazet 			if (!net_eq(dev_net(ifa->idev->dev), net))
33779f0d3c27SEric Dumazet 				continue;
33781d578303SMihai Maruseac 			state->offset++;
3379c2e21293Sstephen hemminger 			return ifa;
3380c2e21293Sstephen hemminger 		}
3381c2e21293Sstephen hemminger 	}
3382c2e21293Sstephen hemminger 
3383c2e21293Sstephen hemminger 	return NULL;
3384c2e21293Sstephen hemminger }
3385c2e21293Sstephen hemminger 
33861da177e4SLinus Torvalds static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
33874f70eccaSEric Dumazet 	__acquires(rcu_bh)
33881da177e4SLinus Torvalds {
33895c578aedSstephen hemminger 	rcu_read_lock_bh();
33901d578303SMihai Maruseac 	return if6_get_first(seq, *pos);
33911da177e4SLinus Torvalds }
33921da177e4SLinus Torvalds 
33931da177e4SLinus Torvalds static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
33941da177e4SLinus Torvalds {
33951da177e4SLinus Torvalds 	struct inet6_ifaddr *ifa;
33961da177e4SLinus Torvalds 
33971da177e4SLinus Torvalds 	ifa = if6_get_next(seq, v);
33981da177e4SLinus Torvalds 	++*pos;
33991da177e4SLinus Torvalds 	return ifa;
34001da177e4SLinus Torvalds }
34011da177e4SLinus Torvalds 
34021da177e4SLinus Torvalds static void if6_seq_stop(struct seq_file *seq, void *v)
34034f70eccaSEric Dumazet 	__releases(rcu_bh)
34041da177e4SLinus Torvalds {
34055c578aedSstephen hemminger 	rcu_read_unlock_bh();
34061da177e4SLinus Torvalds }
34071da177e4SLinus Torvalds 
34081da177e4SLinus Torvalds static int if6_seq_show(struct seq_file *seq, void *v)
34091da177e4SLinus Torvalds {
34101da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
34114b7a4274SHarvey Harrison 	seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
3412b071195dSHarvey Harrison 		   &ifp->addr,
34131da177e4SLinus Torvalds 		   ifp->idev->dev->ifindex,
34141da177e4SLinus Torvalds 		   ifp->prefix_len,
34151da177e4SLinus Torvalds 		   ifp->scope,
34161da177e4SLinus Torvalds 		   ifp->flags,
34171da177e4SLinus Torvalds 		   ifp->idev->dev->name);
34181da177e4SLinus Torvalds 	return 0;
34191da177e4SLinus Torvalds }
34201da177e4SLinus Torvalds 
342156b3d975SPhilippe De Muyter static const struct seq_operations if6_seq_ops = {
34221da177e4SLinus Torvalds 	.start	= if6_seq_start,
34231da177e4SLinus Torvalds 	.next	= if6_seq_next,
34241da177e4SLinus Torvalds 	.show	= if6_seq_show,
34251da177e4SLinus Torvalds 	.stop	= if6_seq_stop,
34261da177e4SLinus Torvalds };
34271da177e4SLinus Torvalds 
34281da177e4SLinus Torvalds static int if6_seq_open(struct inode *inode, struct file *file)
34291da177e4SLinus Torvalds {
34303c40090aSDaniel Lezcano 	return seq_open_net(inode, file, &if6_seq_ops,
3431cf7732e4SPavel Emelyanov 			    sizeof(struct if6_iter_state));
34321da177e4SLinus Torvalds }
34331da177e4SLinus Torvalds 
34349a32144eSArjan van de Ven static const struct file_operations if6_fops = {
34351da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
34361da177e4SLinus Torvalds 	.open		= if6_seq_open,
34371da177e4SLinus Torvalds 	.read		= seq_read,
34381da177e4SLinus Torvalds 	.llseek		= seq_lseek,
34393c40090aSDaniel Lezcano 	.release	= seq_release_net,
34401da177e4SLinus Torvalds };
34411da177e4SLinus Torvalds 
34422c8c1e72SAlexey Dobriyan static int __net_init if6_proc_net_init(struct net *net)
34431da177e4SLinus Torvalds {
3444d4beaa66SGao feng 	if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
34451da177e4SLinus Torvalds 		return -ENOMEM;
34461da177e4SLinus Torvalds 	return 0;
34471da177e4SLinus Torvalds }
34481da177e4SLinus Torvalds 
34492c8c1e72SAlexey Dobriyan static void __net_exit if6_proc_net_exit(struct net *net)
34503c40090aSDaniel Lezcano {
3451ece31ffdSGao feng 	remove_proc_entry("if_inet6", net->proc_net);
34523c40090aSDaniel Lezcano }
34533c40090aSDaniel Lezcano 
34543c40090aSDaniel Lezcano static struct pernet_operations if6_proc_net_ops = {
34553c40090aSDaniel Lezcano        .init = if6_proc_net_init,
34563c40090aSDaniel Lezcano        .exit = if6_proc_net_exit,
34573c40090aSDaniel Lezcano };
34583c40090aSDaniel Lezcano 
34593c40090aSDaniel Lezcano int __init if6_proc_init(void)
34603c40090aSDaniel Lezcano {
34613c40090aSDaniel Lezcano 	return register_pernet_subsys(&if6_proc_net_ops);
34623c40090aSDaniel Lezcano }
34633c40090aSDaniel Lezcano 
34641da177e4SLinus Torvalds void if6_proc_exit(void)
34651da177e4SLinus Torvalds {
34663c40090aSDaniel Lezcano 	unregister_pernet_subsys(&if6_proc_net_ops);
34671da177e4SLinus Torvalds }
34681da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
34691da177e4SLinus Torvalds 
347007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
34713b9f9a1cSNoriaki TAKAMIYA /* Check if address is a home address configured on any interface. */
3472b71d1d42SEric Dumazet int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
34733b9f9a1cSNoriaki TAKAMIYA {
34743b9f9a1cSNoriaki TAKAMIYA 	int ret = 0;
3475c2e21293Sstephen hemminger 	struct inet6_ifaddr *ifp = NULL;
3476ddbe5032SEric Dumazet 	unsigned int hash = inet6_addr_hash(addr);
3477c2e21293Sstephen hemminger 
34785c578aedSstephen hemminger 	rcu_read_lock_bh();
3479b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
3480878628fbSYOSHIFUJI Hideaki 		if (!net_eq(dev_net(ifp->idev->dev), net))
3481389f6612SDaniel Lezcano 			continue;
3482caad295fSYOSHIFUJI Hideaki 		if (ipv6_addr_equal(&ifp->addr, addr) &&
34833b9f9a1cSNoriaki TAKAMIYA 		    (ifp->flags & IFA_F_HOMEADDRESS)) {
34843b9f9a1cSNoriaki TAKAMIYA 			ret = 1;
34853b9f9a1cSNoriaki TAKAMIYA 			break;
34863b9f9a1cSNoriaki TAKAMIYA 		}
34873b9f9a1cSNoriaki TAKAMIYA 	}
34885c578aedSstephen hemminger 	rcu_read_unlock_bh();
34893b9f9a1cSNoriaki TAKAMIYA 	return ret;
34903b9f9a1cSNoriaki TAKAMIYA }
34913b9f9a1cSNoriaki TAKAMIYA #endif
34923b9f9a1cSNoriaki TAKAMIYA 
34931da177e4SLinus Torvalds /*
34941da177e4SLinus Torvalds  *	Periodic address status verification
34951da177e4SLinus Torvalds  */
34961da177e4SLinus Torvalds 
34971da177e4SLinus Torvalds static void addrconf_verify(unsigned long foo)
34981da177e4SLinus Torvalds {
3499b2db7564SYOSHIFUJI Hideaki 	unsigned long now, next, next_sec, next_sched;
35001da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
35011da177e4SLinus Torvalds 	int i;
35021da177e4SLinus Torvalds 
35035c578aedSstephen hemminger 	rcu_read_lock_bh();
35045c578aedSstephen hemminger 	spin_lock(&addrconf_verify_lock);
35051da177e4SLinus Torvalds 	now = jiffies;
3506b2db7564SYOSHIFUJI Hideaki 	next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
35071da177e4SLinus Torvalds 
35081da177e4SLinus Torvalds 	del_timer(&addr_chk_timer);
35091da177e4SLinus Torvalds 
35101da177e4SLinus Torvalds 	for (i = 0; i < IN6_ADDR_HSIZE; i++) {
35111da177e4SLinus Torvalds restart:
3512b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu_bh(ifp,
35135c578aedSstephen hemminger 					 &inet6_addr_lst[i], addr_lst) {
35141da177e4SLinus Torvalds 			unsigned long age;
35151da177e4SLinus Torvalds 
35161da177e4SLinus Torvalds 			if (ifp->flags & IFA_F_PERMANENT)
35171da177e4SLinus Torvalds 				continue;
35181da177e4SLinus Torvalds 
35191da177e4SLinus Torvalds 			spin_lock(&ifp->lock);
3520b2db7564SYOSHIFUJI Hideaki 			/* We try to batch several events at once. */
3521b2db7564SYOSHIFUJI Hideaki 			age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
35221da177e4SLinus Torvalds 
35238f27ebb9SYOSHIFUJI Hideaki 			if (ifp->valid_lft != INFINITY_LIFE_TIME &&
35248f27ebb9SYOSHIFUJI Hideaki 			    age >= ifp->valid_lft) {
35251da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
35261da177e4SLinus Torvalds 				in6_ifa_hold(ifp);
35271da177e4SLinus Torvalds 				ipv6_del_addr(ifp);
35281da177e4SLinus Torvalds 				goto restart;
35298f27ebb9SYOSHIFUJI Hideaki 			} else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
35308f27ebb9SYOSHIFUJI Hideaki 				spin_unlock(&ifp->lock);
35318f27ebb9SYOSHIFUJI Hideaki 				continue;
35321da177e4SLinus Torvalds 			} else if (age >= ifp->prefered_lft) {
3533a1ed0526SBrian Haley 				/* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
35341da177e4SLinus Torvalds 				int deprecate = 0;
35351da177e4SLinus Torvalds 
35361da177e4SLinus Torvalds 				if (!(ifp->flags&IFA_F_DEPRECATED)) {
35371da177e4SLinus Torvalds 					deprecate = 1;
35381da177e4SLinus Torvalds 					ifp->flags |= IFA_F_DEPRECATED;
35391da177e4SLinus Torvalds 				}
35401da177e4SLinus Torvalds 
35411da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
35421da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->valid_lft * HZ;
35431da177e4SLinus Torvalds 
35441da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
35451da177e4SLinus Torvalds 
35461da177e4SLinus Torvalds 				if (deprecate) {
35471da177e4SLinus Torvalds 					in6_ifa_hold(ifp);
35481da177e4SLinus Torvalds 
35491da177e4SLinus Torvalds 					ipv6_ifa_notify(0, ifp);
35501da177e4SLinus Torvalds 					in6_ifa_put(ifp);
35511da177e4SLinus Torvalds 					goto restart;
35521da177e4SLinus Torvalds 				}
35531da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
35541da177e4SLinus Torvalds 			} else if ((ifp->flags&IFA_F_TEMPORARY) &&
35551da177e4SLinus Torvalds 				   !(ifp->flags&IFA_F_TENTATIVE)) {
355688949cf4Sstephen hemminger 				unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
355788949cf4Sstephen hemminger 					ifp->idev->cnf.dad_transmits *
355888949cf4Sstephen hemminger 					ifp->idev->nd_parms->retrans_time / HZ;
355988949cf4Sstephen hemminger 
35601da177e4SLinus Torvalds 				if (age >= ifp->prefered_lft - regen_advance) {
35611da177e4SLinus Torvalds 					struct inet6_ifaddr *ifpub = ifp->ifpub;
35621da177e4SLinus Torvalds 					if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
35631da177e4SLinus Torvalds 						next = ifp->tstamp + ifp->prefered_lft * HZ;
35641da177e4SLinus Torvalds 					if (!ifp->regen_count && ifpub) {
35651da177e4SLinus Torvalds 						ifp->regen_count++;
35661da177e4SLinus Torvalds 						in6_ifa_hold(ifp);
35671da177e4SLinus Torvalds 						in6_ifa_hold(ifpub);
35681da177e4SLinus Torvalds 						spin_unlock(&ifp->lock);
35695c578aedSstephen hemminger 
3570291d809bSHiroyuki YAMAMORI 						spin_lock(&ifpub->lock);
3571291d809bSHiroyuki YAMAMORI 						ifpub->regen_count = 0;
3572291d809bSHiroyuki YAMAMORI 						spin_unlock(&ifpub->lock);
35731da177e4SLinus Torvalds 						ipv6_create_tempaddr(ifpub, ifp);
35741da177e4SLinus Torvalds 						in6_ifa_put(ifpub);
35751da177e4SLinus Torvalds 						in6_ifa_put(ifp);
35761da177e4SLinus Torvalds 						goto restart;
35771da177e4SLinus Torvalds 					}
35781da177e4SLinus Torvalds 				} else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
35791da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
35801da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
35811da177e4SLinus Torvalds #endif
35821da177e4SLinus Torvalds 			} else {
35831da177e4SLinus Torvalds 				/* ifp->prefered_lft <= ifp->valid_lft */
35841da177e4SLinus Torvalds 				if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
35851da177e4SLinus Torvalds 					next = ifp->tstamp + ifp->prefered_lft * HZ;
35861da177e4SLinus Torvalds 				spin_unlock(&ifp->lock);
35871da177e4SLinus Torvalds 			}
35881da177e4SLinus Torvalds 		}
35891da177e4SLinus Torvalds 	}
35901da177e4SLinus Torvalds 
3591b2db7564SYOSHIFUJI Hideaki 	next_sec = round_jiffies_up(next);
3592b2db7564SYOSHIFUJI Hideaki 	next_sched = next;
3593b2db7564SYOSHIFUJI Hideaki 
3594b2db7564SYOSHIFUJI Hideaki 	/* If rounded timeout is accurate enough, accept it. */
3595b2db7564SYOSHIFUJI Hideaki 	if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
3596b2db7564SYOSHIFUJI Hideaki 		next_sched = next_sec;
3597b2db7564SYOSHIFUJI Hideaki 
3598b2db7564SYOSHIFUJI Hideaki 	/* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
3599b2db7564SYOSHIFUJI Hideaki 	if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
3600b2db7564SYOSHIFUJI Hideaki 		next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
3601b2db7564SYOSHIFUJI Hideaki 
3602b2db7564SYOSHIFUJI Hideaki 	ADBG((KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
3603b2db7564SYOSHIFUJI Hideaki 	      now, next, next_sec, next_sched));
3604b2db7564SYOSHIFUJI Hideaki 
3605b2db7564SYOSHIFUJI Hideaki 	addr_chk_timer.expires = next_sched;
36061da177e4SLinus Torvalds 	add_timer(&addr_chk_timer);
36075c578aedSstephen hemminger 	spin_unlock(&addrconf_verify_lock);
36085c578aedSstephen hemminger 	rcu_read_unlock_bh();
36091da177e4SLinus Torvalds }
36101da177e4SLinus Torvalds 
3611461d8837SThomas Graf static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
3612461d8837SThomas Graf {
3613461d8837SThomas Graf 	struct in6_addr *pfx = NULL;
3614461d8837SThomas Graf 
3615461d8837SThomas Graf 	if (addr)
3616461d8837SThomas Graf 		pfx = nla_data(addr);
3617461d8837SThomas Graf 
3618461d8837SThomas Graf 	if (local) {
3619461d8837SThomas Graf 		if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3620461d8837SThomas Graf 			pfx = NULL;
3621461d8837SThomas Graf 		else
3622461d8837SThomas Graf 			pfx = nla_data(local);
3623461d8837SThomas Graf 	}
3624461d8837SThomas Graf 
3625461d8837SThomas Graf 	return pfx;
3626461d8837SThomas Graf }
3627461d8837SThomas Graf 
3628ef7c79edSPatrick McHardy static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
3629461d8837SThomas Graf 	[IFA_ADDRESS]		= { .len = sizeof(struct in6_addr) },
3630461d8837SThomas Graf 	[IFA_LOCAL]		= { .len = sizeof(struct in6_addr) },
3631461d8837SThomas Graf 	[IFA_CACHEINFO]		= { .len = sizeof(struct ifa_cacheinfo) },
3632461d8837SThomas Graf };
3633461d8837SThomas Graf 
36341da177e4SLinus Torvalds static int
3635661d2967SThomas Graf inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
36361da177e4SLinus Torvalds {
36373b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
3638b933f716SThomas Graf 	struct ifaddrmsg *ifm;
3639b933f716SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
36401da177e4SLinus Torvalds 	struct in6_addr *pfx;
3641b933f716SThomas Graf 	int err;
36421da177e4SLinus Torvalds 
3643b933f716SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3644b933f716SThomas Graf 	if (err < 0)
3645b933f716SThomas Graf 		return err;
3646b933f716SThomas Graf 
3647b933f716SThomas Graf 	ifm = nlmsg_data(nlh);
3648b933f716SThomas Graf 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
36491da177e4SLinus Torvalds 	if (pfx == NULL)
36501da177e4SLinus Torvalds 		return -EINVAL;
36511da177e4SLinus Torvalds 
3652af284937SDaniel Lezcano 	return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen);
36531da177e4SLinus Torvalds }
36541da177e4SLinus Torvalds 
365555ebaef1SNoriaki TAKAMIYA static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
365655ebaef1SNoriaki TAKAMIYA 			     u32 prefered_lft, u32 valid_lft)
3657081bba5bSNoriaki TAKAMIYA {
36586f704992SYOSHIFUJI Hideaki 	u32 flags;
36596f704992SYOSHIFUJI Hideaki 	clock_t expires;
36604bed72e4SYOSHIFUJI Hideaki 	unsigned long timeout;
366146d48046SYOSHIFUJI Hideaki 
3662081bba5bSNoriaki TAKAMIYA 	if (!valid_lft || (prefered_lft > valid_lft))
3663081bba5bSNoriaki TAKAMIYA 		return -EINVAL;
3664081bba5bSNoriaki TAKAMIYA 
36654bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(valid_lft, HZ);
36664bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
36674bed72e4SYOSHIFUJI Hideaki 		expires = jiffies_to_clock_t(timeout * HZ);
36684bed72e4SYOSHIFUJI Hideaki 		valid_lft = timeout;
36696f704992SYOSHIFUJI Hideaki 		flags = RTF_EXPIRES;
36704bed72e4SYOSHIFUJI Hideaki 	} else {
36714bed72e4SYOSHIFUJI Hideaki 		expires = 0;
36724bed72e4SYOSHIFUJI Hideaki 		flags = 0;
36734bed72e4SYOSHIFUJI Hideaki 		ifa_flags |= IFA_F_PERMANENT;
36746f704992SYOSHIFUJI Hideaki 	}
3675081bba5bSNoriaki TAKAMIYA 
36764bed72e4SYOSHIFUJI Hideaki 	timeout = addrconf_timeout_fixup(prefered_lft, HZ);
36774bed72e4SYOSHIFUJI Hideaki 	if (addrconf_finite_timeout(timeout)) {
36784bed72e4SYOSHIFUJI Hideaki 		if (timeout == 0)
367955ebaef1SNoriaki TAKAMIYA 			ifa_flags |= IFA_F_DEPRECATED;
36804bed72e4SYOSHIFUJI Hideaki 		prefered_lft = timeout;
36814bed72e4SYOSHIFUJI Hideaki 	}
3682081bba5bSNoriaki TAKAMIYA 
3683081bba5bSNoriaki TAKAMIYA 	spin_lock_bh(&ifp->lock);
36843b9f9a1cSNoriaki TAKAMIYA 	ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags;
3685081bba5bSNoriaki TAKAMIYA 	ifp->tstamp = jiffies;
3686081bba5bSNoriaki TAKAMIYA 	ifp->valid_lft = valid_lft;
3687081bba5bSNoriaki TAKAMIYA 	ifp->prefered_lft = prefered_lft;
3688081bba5bSNoriaki TAKAMIYA 
3689081bba5bSNoriaki TAKAMIYA 	spin_unlock_bh(&ifp->lock);
3690081bba5bSNoriaki TAKAMIYA 	if (!(ifp->flags&IFA_F_TENTATIVE))
3691081bba5bSNoriaki TAKAMIYA 		ipv6_ifa_notify(0, ifp);
3692081bba5bSNoriaki TAKAMIYA 
369346d48046SYOSHIFUJI Hideaki 	addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
36946f704992SYOSHIFUJI Hideaki 			      expires, flags);
3695081bba5bSNoriaki TAKAMIYA 	addrconf_verify(0);
3696081bba5bSNoriaki TAKAMIYA 
3697081bba5bSNoriaki TAKAMIYA 	return 0;
3698081bba5bSNoriaki TAKAMIYA }
3699081bba5bSNoriaki TAKAMIYA 
3700081bba5bSNoriaki TAKAMIYA static int
3701661d2967SThomas Graf inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
37021da177e4SLinus Torvalds {
37033b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
3704461d8837SThomas Graf 	struct ifaddrmsg *ifm;
3705461d8837SThomas Graf 	struct nlattr *tb[IFA_MAX+1];
37061da177e4SLinus Torvalds 	struct in6_addr *pfx;
37077198f8ceSThomas Graf 	struct inet6_ifaddr *ifa;
37087198f8ceSThomas Graf 	struct net_device *dev;
370955ebaef1SNoriaki TAKAMIYA 	u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
371055ebaef1SNoriaki TAKAMIYA 	u8 ifa_flags;
3711461d8837SThomas Graf 	int err;
37121da177e4SLinus Torvalds 
3713461d8837SThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3714461d8837SThomas Graf 	if (err < 0)
3715461d8837SThomas Graf 		return err;
3716461d8837SThomas Graf 
3717461d8837SThomas Graf 	ifm = nlmsg_data(nlh);
3718461d8837SThomas Graf 	pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
37191da177e4SLinus Torvalds 	if (pfx == NULL)
37201da177e4SLinus Torvalds 		return -EINVAL;
37211da177e4SLinus Torvalds 
3722461d8837SThomas Graf 	if (tb[IFA_CACHEINFO]) {
37230778769dSNoriaki TAKAMIYA 		struct ifa_cacheinfo *ci;
3724461d8837SThomas Graf 
3725461d8837SThomas Graf 		ci = nla_data(tb[IFA_CACHEINFO]);
37260778769dSNoriaki TAKAMIYA 		valid_lft = ci->ifa_valid;
3727461d8837SThomas Graf 		preferred_lft = ci->ifa_prefered;
3728461d8837SThomas Graf 	} else {
3729461d8837SThomas Graf 		preferred_lft = INFINITY_LIFE_TIME;
3730461d8837SThomas Graf 		valid_lft = INFINITY_LIFE_TIME;
37310778769dSNoriaki TAKAMIYA 	}
37320778769dSNoriaki TAKAMIYA 
3733af284937SDaniel Lezcano 	dev =  __dev_get_by_index(net, ifm->ifa_index);
37347198f8ceSThomas Graf 	if (dev == NULL)
37357198f8ceSThomas Graf 		return -ENODEV;
3736081bba5bSNoriaki TAKAMIYA 
373755ebaef1SNoriaki TAKAMIYA 	/* We ignore other flags so far. */
37383b9f9a1cSNoriaki TAKAMIYA 	ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
373955ebaef1SNoriaki TAKAMIYA 
37401cab3da6SDaniel Lezcano 	ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
37417198f8ceSThomas Graf 	if (ifa == NULL) {
37427198f8ceSThomas Graf 		/*
37437198f8ceSThomas Graf 		 * It would be best to check for !NLM_F_CREATE here but
37447198f8ceSThomas Graf 		 * userspace alreay relies on not having to provide this.
37457198f8ceSThomas Graf 		 */
3746af284937SDaniel Lezcano 		return inet6_addr_add(net, ifm->ifa_index, pfx,
3747af284937SDaniel Lezcano 				      ifm->ifa_prefixlen, ifa_flags,
3748af284937SDaniel Lezcano 				      preferred_lft, valid_lft);
37491da177e4SLinus Torvalds 	}
37501da177e4SLinus Torvalds 
37517198f8ceSThomas Graf 	if (nlh->nlmsg_flags & NLM_F_EXCL ||
37527198f8ceSThomas Graf 	    !(nlh->nlmsg_flags & NLM_F_REPLACE))
37537198f8ceSThomas Graf 		err = -EEXIST;
37547198f8ceSThomas Graf 	else
375555ebaef1SNoriaki TAKAMIYA 		err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
37567198f8ceSThomas Graf 
37577198f8ceSThomas Graf 	in6_ifa_put(ifa);
37587198f8ceSThomas Graf 
37597198f8ceSThomas Graf 	return err;
37607198f8ceSThomas Graf }
37617198f8ceSThomas Graf 
3762101bb229SThomas Graf static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3763101bb229SThomas Graf 			  u8 scope, int ifindex)
3764101bb229SThomas Graf {
3765101bb229SThomas Graf 	struct ifaddrmsg *ifm;
3766101bb229SThomas Graf 
3767101bb229SThomas Graf 	ifm = nlmsg_data(nlh);
3768101bb229SThomas Graf 	ifm->ifa_family = AF_INET6;
3769101bb229SThomas Graf 	ifm->ifa_prefixlen = prefixlen;
3770101bb229SThomas Graf 	ifm->ifa_flags = flags;
3771101bb229SThomas Graf 	ifm->ifa_scope = scope;
3772101bb229SThomas Graf 	ifm->ifa_index = ifindex;
3773101bb229SThomas Graf }
3774101bb229SThomas Graf 
377585486af0SThomas Graf static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
377685486af0SThomas Graf 			 unsigned long tstamp, u32 preferred, u32 valid)
377785486af0SThomas Graf {
377885486af0SThomas Graf 	struct ifa_cacheinfo ci;
377985486af0SThomas Graf 
378018a31e1eSThomas Graf 	ci.cstamp = cstamp_delta(cstamp);
378118a31e1eSThomas Graf 	ci.tstamp = cstamp_delta(tstamp);
378285486af0SThomas Graf 	ci.ifa_prefered = preferred;
378385486af0SThomas Graf 	ci.ifa_valid = valid;
378485486af0SThomas Graf 
378585486af0SThomas Graf 	return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
378685486af0SThomas Graf }
378785486af0SThomas Graf 
3788101bb229SThomas Graf static inline int rt_scope(int ifa_scope)
3789101bb229SThomas Graf {
3790101bb229SThomas Graf 	if (ifa_scope & IFA_HOST)
3791101bb229SThomas Graf 		return RT_SCOPE_HOST;
3792101bb229SThomas Graf 	else if (ifa_scope & IFA_LINK)
3793101bb229SThomas Graf 		return RT_SCOPE_LINK;
3794101bb229SThomas Graf 	else if (ifa_scope & IFA_SITE)
3795101bb229SThomas Graf 		return RT_SCOPE_SITE;
3796101bb229SThomas Graf 	else
3797101bb229SThomas Graf 		return RT_SCOPE_UNIVERSE;
3798101bb229SThomas Graf }
3799101bb229SThomas Graf 
38000ab6803bSThomas Graf static inline int inet6_ifaddr_msgsize(void)
38010ab6803bSThomas Graf {
3802339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3803339bf98fSThomas Graf 	       + nla_total_size(16) /* IFA_ADDRESS */
3804339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct ifa_cacheinfo));
38050ab6803bSThomas Graf }
3806c5396a31SYOSHIFUJI Hideaki 
38071da177e4SLinus Torvalds static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
380815e47304SEric W. Biederman 			     u32 portid, u32 seq, int event, unsigned int flags)
38091da177e4SLinus Torvalds {
38101da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
381185486af0SThomas Graf 	u32 preferred, valid;
38121da177e4SLinus Torvalds 
381315e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
38140ab6803bSThomas Graf 	if (nlh == NULL)
381526932566SPatrick McHardy 		return -EMSGSIZE;
38160ab6803bSThomas Graf 
3817101bb229SThomas Graf 	put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3818101bb229SThomas Graf 		      ifa->idev->dev->ifindex);
3819101bb229SThomas Graf 
38201da177e4SLinus Torvalds 	if (!(ifa->flags&IFA_F_PERMANENT)) {
382185486af0SThomas Graf 		preferred = ifa->prefered_lft;
382285486af0SThomas Graf 		valid = ifa->valid_lft;
382385486af0SThomas Graf 		if (preferred != INFINITY_LIFE_TIME) {
38241da177e4SLinus Torvalds 			long tval = (jiffies - ifa->tstamp)/HZ;
3825a1faa698SJens Rosenboom 			if (preferred > tval)
382685486af0SThomas Graf 				preferred -= tval;
3827a1faa698SJens Rosenboom 			else
3828a1faa698SJens Rosenboom 				preferred = 0;
3829f56619fcSBen Hutchings 			if (valid != INFINITY_LIFE_TIME) {
3830f56619fcSBen Hutchings 				if (valid > tval)
383185486af0SThomas Graf 					valid -= tval;
3832f56619fcSBen Hutchings 				else
3833f56619fcSBen Hutchings 					valid = 0;
3834f56619fcSBen Hutchings 			}
38351da177e4SLinus Torvalds 		}
38361da177e4SLinus Torvalds 	} else {
383785486af0SThomas Graf 		preferred = INFINITY_LIFE_TIME;
383885486af0SThomas Graf 		valid = INFINITY_LIFE_TIME;
38391da177e4SLinus Torvalds 	}
384085486af0SThomas Graf 
38410ab6803bSThomas Graf 	if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
384226932566SPatrick McHardy 	    put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
384326932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
384426932566SPatrick McHardy 		return -EMSGSIZE;
384526932566SPatrick McHardy 	}
384685486af0SThomas Graf 
38470ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
38481da177e4SLinus Torvalds }
38491da177e4SLinus Torvalds 
38501da177e4SLinus Torvalds static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
385115e47304SEric W. Biederman 				u32 portid, u32 seq, int event, u16 flags)
38521da177e4SLinus Torvalds {
38531da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
3854101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
3855101bb229SThomas Graf 	int ifindex = ifmca->idev->dev->ifindex;
38561da177e4SLinus Torvalds 
38571da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3858101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
3859101bb229SThomas Graf 
386015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
38610ab6803bSThomas Graf 	if (nlh == NULL)
386226932566SPatrick McHardy 		return -EMSGSIZE;
38630ab6803bSThomas Graf 
3864101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
38650ab6803bSThomas Graf 	if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
38660ab6803bSThomas Graf 	    put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
386726932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
386826932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
386926932566SPatrick McHardy 		return -EMSGSIZE;
387026932566SPatrick McHardy 	}
387185486af0SThomas Graf 
38720ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
38731da177e4SLinus Torvalds }
38741da177e4SLinus Torvalds 
38751da177e4SLinus Torvalds static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
387615e47304SEric W. Biederman 				u32 portid, u32 seq, int event, unsigned int flags)
38771da177e4SLinus Torvalds {
38781da177e4SLinus Torvalds 	struct nlmsghdr  *nlh;
3879101bb229SThomas Graf 	u8 scope = RT_SCOPE_UNIVERSE;
3880101bb229SThomas Graf 	int ifindex = ifaca->aca_idev->dev->ifindex;
38811da177e4SLinus Torvalds 
38821da177e4SLinus Torvalds 	if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3883101bb229SThomas Graf 		scope = RT_SCOPE_SITE;
3884101bb229SThomas Graf 
388515e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
38860ab6803bSThomas Graf 	if (nlh == NULL)
388726932566SPatrick McHardy 		return -EMSGSIZE;
38880ab6803bSThomas Graf 
3889101bb229SThomas Graf 	put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
38900ab6803bSThomas Graf 	if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
38910ab6803bSThomas Graf 	    put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
389226932566SPatrick McHardy 			  INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
389326932566SPatrick McHardy 		nlmsg_cancel(skb, nlh);
389426932566SPatrick McHardy 		return -EMSGSIZE;
389526932566SPatrick McHardy 	}
389685486af0SThomas Graf 
38970ab6803bSThomas Graf 	return nlmsg_end(skb, nlh);
38981da177e4SLinus Torvalds }
38991da177e4SLinus Torvalds 
3900e21e8467SStephen Hemminger enum addr_type_t {
39011da177e4SLinus Torvalds 	UNICAST_ADDR,
39021da177e4SLinus Torvalds 	MULTICAST_ADDR,
39031da177e4SLinus Torvalds 	ANYCAST_ADDR,
39041da177e4SLinus Torvalds };
39051da177e4SLinus Torvalds 
3906234b27c3SEric Dumazet /* called with rcu_read_lock() */
3907234b27c3SEric Dumazet static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
3908234b27c3SEric Dumazet 			  struct netlink_callback *cb, enum addr_type_t type,
3909234b27c3SEric Dumazet 			  int s_ip_idx, int *p_ip_idx)
39101da177e4SLinus Torvalds {
39111da177e4SLinus Torvalds 	struct ifmcaddr6 *ifmca;
39121da177e4SLinus Torvalds 	struct ifacaddr6 *ifaca;
3913234b27c3SEric Dumazet 	int err = 1;
3914234b27c3SEric Dumazet 	int ip_idx = *p_ip_idx;
39151da177e4SLinus Torvalds 
39161da177e4SLinus Torvalds 	read_lock_bh(&idev->lock);
39171da177e4SLinus Torvalds 	switch (type) {
3918502a2ffdSstephen hemminger 	case UNICAST_ADDR: {
3919502a2ffdSstephen hemminger 		struct inet6_ifaddr *ifa;
3920502a2ffdSstephen hemminger 
3921ae9cda5dSYOSHIFUJI Hideaki 		/* unicast address incl. temp addr */
3922502a2ffdSstephen hemminger 		list_for_each_entry(ifa, &idev->addr_list, if_list) {
3923502a2ffdSstephen hemminger 			if (++ip_idx < s_ip_idx)
39241da177e4SLinus Torvalds 				continue;
39255d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifaddr(skb, ifa,
392615e47304SEric W. Biederman 						NETLINK_CB(cb->skb).portid,
39275d5619b4SYOSHIFUJI Hideaki 						cb->nlh->nlmsg_seq,
39285d5619b4SYOSHIFUJI Hideaki 						RTM_NEWADDR,
39295d5619b4SYOSHIFUJI Hideaki 						NLM_F_MULTI);
3930bcd32326SEric Dumazet 			if (err <= 0)
3931bcd32326SEric Dumazet 				break;
393263998ac2SNicolas Dichtel 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
39331da177e4SLinus Torvalds 		}
39341da177e4SLinus Torvalds 		break;
3935502a2ffdSstephen hemminger 	}
39361da177e4SLinus Torvalds 	case MULTICAST_ADDR:
39371da177e4SLinus Torvalds 		/* multicast address */
39381da177e4SLinus Torvalds 		for (ifmca = idev->mc_list; ifmca;
39391da177e4SLinus Torvalds 		     ifmca = ifmca->next, ip_idx++) {
39401da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
39411da177e4SLinus Torvalds 				continue;
39425d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifmcaddr(skb, ifmca,
394315e47304SEric W. Biederman 						  NETLINK_CB(cb->skb).portid,
39445d5619b4SYOSHIFUJI Hideaki 						  cb->nlh->nlmsg_seq,
39455d5619b4SYOSHIFUJI Hideaki 						  RTM_GETMULTICAST,
39465d5619b4SYOSHIFUJI Hideaki 						  NLM_F_MULTI);
3947bcd32326SEric Dumazet 			if (err <= 0)
3948bcd32326SEric Dumazet 				break;
39491da177e4SLinus Torvalds 		}
39501da177e4SLinus Torvalds 		break;
39511da177e4SLinus Torvalds 	case ANYCAST_ADDR:
39521da177e4SLinus Torvalds 		/* anycast address */
39531da177e4SLinus Torvalds 		for (ifaca = idev->ac_list; ifaca;
39541da177e4SLinus Torvalds 		     ifaca = ifaca->aca_next, ip_idx++) {
39551da177e4SLinus Torvalds 			if (ip_idx < s_ip_idx)
39561da177e4SLinus Torvalds 				continue;
39575d5619b4SYOSHIFUJI Hideaki 			err = inet6_fill_ifacaddr(skb, ifaca,
395815e47304SEric W. Biederman 						  NETLINK_CB(cb->skb).portid,
39595d5619b4SYOSHIFUJI Hideaki 						  cb->nlh->nlmsg_seq,
39605d5619b4SYOSHIFUJI Hideaki 						  RTM_GETANYCAST,
39615d5619b4SYOSHIFUJI Hideaki 						  NLM_F_MULTI);
3962bcd32326SEric Dumazet 			if (err <= 0)
3963bcd32326SEric Dumazet 				break;
39641da177e4SLinus Torvalds 		}
39651da177e4SLinus Torvalds 		break;
39661da177e4SLinus Torvalds 	default:
39671da177e4SLinus Torvalds 		break;
39681da177e4SLinus Torvalds 	}
39691da177e4SLinus Torvalds 	read_unlock_bh(&idev->lock);
3970234b27c3SEric Dumazet 	*p_ip_idx = ip_idx;
3971234b27c3SEric Dumazet 	return err;
3972234b27c3SEric Dumazet }
39735d5619b4SYOSHIFUJI Hideaki 
3974234b27c3SEric Dumazet static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3975234b27c3SEric Dumazet 			   enum addr_type_t type)
3976234b27c3SEric Dumazet {
3977234b27c3SEric Dumazet 	struct net *net = sock_net(skb->sk);
3978234b27c3SEric Dumazet 	int h, s_h;
3979234b27c3SEric Dumazet 	int idx, ip_idx;
3980234b27c3SEric Dumazet 	int s_idx, s_ip_idx;
3981234b27c3SEric Dumazet 	struct net_device *dev;
3982234b27c3SEric Dumazet 	struct inet6_dev *idev;
3983234b27c3SEric Dumazet 	struct hlist_head *head;
3984234b27c3SEric Dumazet 
3985234b27c3SEric Dumazet 	s_h = cb->args[0];
3986234b27c3SEric Dumazet 	s_idx = idx = cb->args[1];
3987234b27c3SEric Dumazet 	s_ip_idx = ip_idx = cb->args[2];
3988234b27c3SEric Dumazet 
3989234b27c3SEric Dumazet 	rcu_read_lock();
399063998ac2SNicolas Dichtel 	cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
3991234b27c3SEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3992234b27c3SEric Dumazet 		idx = 0;
3993234b27c3SEric Dumazet 		head = &net->dev_index_head[h];
3994b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
3995234b27c3SEric Dumazet 			if (idx < s_idx)
3996234b27c3SEric Dumazet 				goto cont;
39974b97efdfSPatrick McHardy 			if (h > s_h || idx > s_idx)
3998234b27c3SEric Dumazet 				s_ip_idx = 0;
3999234b27c3SEric Dumazet 			ip_idx = 0;
4000e21e8467SStephen Hemminger 			idev = __in6_dev_get(dev);
4001e21e8467SStephen Hemminger 			if (!idev)
4002234b27c3SEric Dumazet 				goto cont;
4003234b27c3SEric Dumazet 
4004234b27c3SEric Dumazet 			if (in6_dump_addrs(idev, skb, cb, type,
4005234b27c3SEric Dumazet 					   s_ip_idx, &ip_idx) <= 0)
4006234b27c3SEric Dumazet 				goto done;
40077562f876SPavel Emelianov cont:
40087562f876SPavel Emelianov 			idx++;
40091da177e4SLinus Torvalds 		}
4010234b27c3SEric Dumazet 	}
4011234b27c3SEric Dumazet done:
4012234b27c3SEric Dumazet 	rcu_read_unlock();
4013234b27c3SEric Dumazet 	cb->args[0] = h;
4014234b27c3SEric Dumazet 	cb->args[1] = idx;
4015234b27c3SEric Dumazet 	cb->args[2] = ip_idx;
4016234b27c3SEric Dumazet 
40171da177e4SLinus Torvalds 	return skb->len;
40181da177e4SLinus Torvalds }
40191da177e4SLinus Torvalds 
40201da177e4SLinus Torvalds static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
40211da177e4SLinus Torvalds {
40221da177e4SLinus Torvalds 	enum addr_type_t type = UNICAST_ADDR;
4023b854272bSDenis V. Lunev 
40241da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
40251da177e4SLinus Torvalds }
40261da177e4SLinus Torvalds 
40271da177e4SLinus Torvalds static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
40281da177e4SLinus Torvalds {
40291da177e4SLinus Torvalds 	enum addr_type_t type = MULTICAST_ADDR;
4030b854272bSDenis V. Lunev 
40311da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
40321da177e4SLinus Torvalds }
40331da177e4SLinus Torvalds 
40341da177e4SLinus Torvalds 
40351da177e4SLinus Torvalds static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
40361da177e4SLinus Torvalds {
40371da177e4SLinus Torvalds 	enum addr_type_t type = ANYCAST_ADDR;
4038b854272bSDenis V. Lunev 
40391da177e4SLinus Torvalds 	return inet6_dump_addr(skb, cb, type);
40401da177e4SLinus Torvalds }
40411da177e4SLinus Torvalds 
4042661d2967SThomas Graf static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
40436c223828SNoriaki TAKAMIYA {
40443b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(in_skb->sk);
40451b29fc2cSThomas Graf 	struct ifaddrmsg *ifm;
40461b29fc2cSThomas Graf 	struct nlattr *tb[IFA_MAX+1];
40476c223828SNoriaki TAKAMIYA 	struct in6_addr *addr = NULL;
40486c223828SNoriaki TAKAMIYA 	struct net_device *dev = NULL;
40496c223828SNoriaki TAKAMIYA 	struct inet6_ifaddr *ifa;
40506c223828SNoriaki TAKAMIYA 	struct sk_buff *skb;
40516c223828SNoriaki TAKAMIYA 	int err;
40526c223828SNoriaki TAKAMIYA 
40531b29fc2cSThomas Graf 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
40541b29fc2cSThomas Graf 	if (err < 0)
40551b29fc2cSThomas Graf 		goto errout;
40566c223828SNoriaki TAKAMIYA 
40571b29fc2cSThomas Graf 	addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
40581b29fc2cSThomas Graf 	if (addr == NULL) {
40591b29fc2cSThomas Graf 		err = -EINVAL;
40601b29fc2cSThomas Graf 		goto errout;
40611b29fc2cSThomas Graf 	}
40621b29fc2cSThomas Graf 
40631b29fc2cSThomas Graf 	ifm = nlmsg_data(nlh);
40646c223828SNoriaki TAKAMIYA 	if (ifm->ifa_index)
40656fda7350SBenjamin Thery 		dev = __dev_get_by_index(net, ifm->ifa_index);
40666c223828SNoriaki TAKAMIYA 
4067e21e8467SStephen Hemminger 	ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4068e21e8467SStephen Hemminger 	if (!ifa) {
40691b29fc2cSThomas Graf 		err = -EADDRNOTAVAIL;
40701b29fc2cSThomas Graf 		goto errout;
40716c223828SNoriaki TAKAMIYA 	}
40726c223828SNoriaki TAKAMIYA 
4073e21e8467SStephen Hemminger 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4074e21e8467SStephen Hemminger 	if (!skb) {
40751b29fc2cSThomas Graf 		err = -ENOBUFS;
40761b29fc2cSThomas Graf 		goto errout_ifa;
40771b29fc2cSThomas Graf 	}
40781b29fc2cSThomas Graf 
407915e47304SEric W. Biederman 	err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
40806c223828SNoriaki TAKAMIYA 				nlh->nlmsg_seq, RTM_NEWADDR, 0);
408126932566SPatrick McHardy 	if (err < 0) {
408226932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
408326932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
408426932566SPatrick McHardy 		kfree_skb(skb);
408526932566SPatrick McHardy 		goto errout_ifa;
408626932566SPatrick McHardy 	}
408715e47304SEric W. Biederman 	err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
40881b29fc2cSThomas Graf errout_ifa:
40896c223828SNoriaki TAKAMIYA 	in6_ifa_put(ifa);
40901b29fc2cSThomas Graf errout:
40916c223828SNoriaki TAKAMIYA 	return err;
40926c223828SNoriaki TAKAMIYA }
40936c223828SNoriaki TAKAMIYA 
40941da177e4SLinus Torvalds static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
40951da177e4SLinus Torvalds {
40961da177e4SLinus Torvalds 	struct sk_buff *skb;
4097c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(ifa->idev->dev);
40985d620266SThomas Graf 	int err = -ENOBUFS;
40991da177e4SLinus Torvalds 
41000ab6803bSThomas Graf 	skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
41015d620266SThomas Graf 	if (skb == NULL)
41025d620266SThomas Graf 		goto errout;
41035d620266SThomas Graf 
41045d620266SThomas Graf 	err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
410526932566SPatrick McHardy 	if (err < 0) {
410626932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
410726932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
410826932566SPatrick McHardy 		kfree_skb(skb);
410926932566SPatrick McHardy 		goto errout;
411026932566SPatrick McHardy 	}
41111ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
41121ce85fe4SPablo Neira Ayuso 	return;
41135d620266SThomas Graf errout:
41145d620266SThomas Graf 	if (err < 0)
41156fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
41161da177e4SLinus Torvalds }
41171da177e4SLinus Torvalds 
4118b6f99a21SDave Jones static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
41191da177e4SLinus Torvalds 				__s32 *array, int bytes)
41201da177e4SLinus Torvalds {
412104561c1fSThomas Graf 	BUG_ON(bytes < (DEVCONF_MAX * 4));
412204561c1fSThomas Graf 
41231da177e4SLinus Torvalds 	memset(array, 0, bytes);
41241da177e4SLinus Torvalds 	array[DEVCONF_FORWARDING] = cnf->forwarding;
41251da177e4SLinus Torvalds 	array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
41261da177e4SLinus Torvalds 	array[DEVCONF_MTU6] = cnf->mtu6;
41271da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
41281da177e4SLinus Torvalds 	array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
41291da177e4SLinus Torvalds 	array[DEVCONF_AUTOCONF] = cnf->autoconf;
41301da177e4SLinus Torvalds 	array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
41311da177e4SLinus Torvalds 	array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
413293908d19SThomas Graf 	array[DEVCONF_RTR_SOLICIT_INTERVAL] =
413393908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_solicit_interval);
413493908d19SThomas Graf 	array[DEVCONF_RTR_SOLICIT_DELAY] =
413593908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_solicit_delay);
41361da177e4SLinus Torvalds 	array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
41371da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
41381da177e4SLinus Torvalds 	array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
41391da177e4SLinus Torvalds 	array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
41401da177e4SLinus Torvalds 	array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
41411da177e4SLinus Torvalds 	array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
41421da177e4SLinus Torvalds 	array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
41431da177e4SLinus Torvalds #endif
41441da177e4SLinus Torvalds 	array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
414565f5c7c1SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
4146c4fd30ebSYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
4147930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
4148930d6ff2SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
414993908d19SThomas Graf 	array[DEVCONF_RTR_PROBE_INTERVAL] =
415093908d19SThomas Graf 		jiffies_to_msecs(cnf->rtr_probe_interval);
4151fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
415209c884d4SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
415309c884d4SYOSHIFUJI Hideaki #endif
4154930d6ff2SYOSHIFUJI Hideaki #endif
4155fbea49e1SYOSHIFUJI Hideaki 	array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
41560bcbc926SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
415795c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
415895c385b4SNeil Horman 	array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
415995c385b4SNeil Horman #endif
41607bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
41617bc570c8SYOSHIFUJI Hideaki 	array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
41627bc570c8SYOSHIFUJI Hideaki #endif
4163778d80beSYOSHIFUJI Hideaki 	array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
41641b34be74SYOSHIFUJI Hideaki 	array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
4165c3faca05SCosmin Ratiu 	array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
41665cb04436SHannes Frederic Sowa 	array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
41671da177e4SLinus Torvalds }
41681da177e4SLinus Torvalds 
4169b382b191SThomas Graf static inline size_t inet6_ifla6_size(void)
4170b382b191SThomas Graf {
4171b382b191SThomas Graf 	return nla_total_size(4) /* IFLA_INET6_FLAGS */
4172b382b191SThomas Graf 	     + nla_total_size(sizeof(struct ifla_cacheinfo))
4173b382b191SThomas Graf 	     + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
4174b382b191SThomas Graf 	     + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
4175f53adae4SDaniel Borkmann 	     + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
4176f53adae4SDaniel Borkmann 	     + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
4177b382b191SThomas Graf }
4178b382b191SThomas Graf 
4179339bf98fSThomas Graf static inline size_t inet6_if_nlmsg_size(void)
4180339bf98fSThomas Graf {
4181339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4182339bf98fSThomas Graf 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4183339bf98fSThomas Graf 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4184339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_MTU */
4185339bf98fSThomas Graf 	       + nla_total_size(4) /* IFLA_LINK */
4186b382b191SThomas Graf 	       + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
4187339bf98fSThomas Graf }
4188c5396a31SYOSHIFUJI Hideaki 
4189be281e55SEric Dumazet static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
41907d720c3eSTejun Heo 				      int items, int bytes)
41917f7d9a6bSHerbert Xu {
41927f7d9a6bSHerbert Xu 	int i;
41937f7d9a6bSHerbert Xu 	int pad = bytes - sizeof(u64) * items;
41947f7d9a6bSHerbert Xu 	BUG_ON(pad < 0);
41957f7d9a6bSHerbert Xu 
41967f7d9a6bSHerbert Xu 	/* Use put_unaligned() because stats may not be aligned for u64. */
41977f7d9a6bSHerbert Xu 	put_unaligned(items, &stats[0]);
41987f7d9a6bSHerbert Xu 	for (i = 1; i < items; i++)
4199be281e55SEric Dumazet 		put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
42007f7d9a6bSHerbert Xu 
42017f7d9a6bSHerbert Xu 	memset(&stats[items], 0, pad);
42027f7d9a6bSHerbert Xu }
42037f7d9a6bSHerbert Xu 
42044ce3c183SEric Dumazet static inline void __snmp6_fill_stats64(u64 *stats, void __percpu **mib,
42054ce3c183SEric Dumazet 				      int items, int bytes, size_t syncpoff)
42064ce3c183SEric Dumazet {
42074ce3c183SEric Dumazet 	int i;
42084ce3c183SEric Dumazet 	int pad = bytes - sizeof(u64) * items;
42094ce3c183SEric Dumazet 	BUG_ON(pad < 0);
42104ce3c183SEric Dumazet 
42114ce3c183SEric Dumazet 	/* Use put_unaligned() because stats may not be aligned for u64. */
42124ce3c183SEric Dumazet 	put_unaligned(items, &stats[0]);
42134ce3c183SEric Dumazet 	for (i = 1; i < items; i++)
42144ce3c183SEric Dumazet 		put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]);
42154ce3c183SEric Dumazet 
42164ce3c183SEric Dumazet 	memset(&stats[items], 0, pad);
42174ce3c183SEric Dumazet }
42184ce3c183SEric Dumazet 
42197f7d9a6bSHerbert Xu static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
42207f7d9a6bSHerbert Xu 			     int bytes)
42217f7d9a6bSHerbert Xu {
42227f7d9a6bSHerbert Xu 	switch (attrtype) {
42237f7d9a6bSHerbert Xu 	case IFLA_INET6_STATS:
42244ce3c183SEric Dumazet 		__snmp6_fill_stats64(stats, (void __percpu **)idev->stats.ipv6,
42254ce3c183SEric Dumazet 				     IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp));
42267f7d9a6bSHerbert Xu 		break;
42277f7d9a6bSHerbert Xu 	case IFLA_INET6_ICMP6STATS:
4228be281e55SEric Dumazet 		__snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes);
42297f7d9a6bSHerbert Xu 		break;
42307f7d9a6bSHerbert Xu 	}
42317f7d9a6bSHerbert Xu }
42327f7d9a6bSHerbert Xu 
4233b382b191SThomas Graf static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev)
42341da177e4SLinus Torvalds {
4235bf99f1bdSYOSHIFUJI Hideaki 	struct nlattr *nla;
42361da177e4SLinus Torvalds 	struct ifla_cacheinfo ci;
42371da177e4SLinus Torvalds 
4238c78679e8SDavid S. Miller 	if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
4239c78679e8SDavid S. Miller 		goto nla_put_failure;
42401da177e4SLinus Torvalds 	ci.max_reasm_len = IPV6_MAXPLEN;
424124912420SDavid S. Miller 	ci.tstamp = cstamp_delta(idev->tstamp);
424224912420SDavid S. Miller 	ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
424324912420SDavid S. Miller 	ci.retrans_time = jiffies_to_msecs(idev->nd_parms->retrans_time);
4244c78679e8SDavid S. Miller 	if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
4245c78679e8SDavid S. Miller 		goto nla_put_failure;
4246bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
4247bf99f1bdSYOSHIFUJI Hideaki 	if (nla == NULL)
424804561c1fSThomas Graf 		goto nla_put_failure;
4249bf99f1bdSYOSHIFUJI Hideaki 	ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
42501da177e4SLinus Torvalds 
4251bf99f1bdSYOSHIFUJI Hideaki 	/* XXX - MC not implemented */
4252bf99f1bdSYOSHIFUJI Hideaki 
4253bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
4254bf99f1bdSYOSHIFUJI Hideaki 	if (nla == NULL)
4255bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
4256bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
4257bf99f1bdSYOSHIFUJI Hideaki 
4258bf99f1bdSYOSHIFUJI Hideaki 	nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
4259bf99f1bdSYOSHIFUJI Hideaki 	if (nla == NULL)
4260bf99f1bdSYOSHIFUJI Hideaki 		goto nla_put_failure;
4261bf99f1bdSYOSHIFUJI Hideaki 	snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
42621da177e4SLinus Torvalds 
4263f53adae4SDaniel Borkmann 	nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
4264f53adae4SDaniel Borkmann 	if (nla == NULL)
4265f53adae4SDaniel Borkmann 		goto nla_put_failure;
4266f53adae4SDaniel Borkmann 	read_lock_bh(&idev->lock);
4267f53adae4SDaniel Borkmann 	memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
4268f53adae4SDaniel Borkmann 	read_unlock_bh(&idev->lock);
4269f53adae4SDaniel Borkmann 
4270b382b191SThomas Graf 	return 0;
4271b382b191SThomas Graf 
4272b382b191SThomas Graf nla_put_failure:
4273b382b191SThomas Graf 	return -EMSGSIZE;
4274b382b191SThomas Graf }
4275b382b191SThomas Graf 
4276b382b191SThomas Graf static size_t inet6_get_link_af_size(const struct net_device *dev)
4277b382b191SThomas Graf {
4278b382b191SThomas Graf 	if (!__in6_dev_get(dev))
4279b382b191SThomas Graf 		return 0;
4280b382b191SThomas Graf 
4281b382b191SThomas Graf 	return inet6_ifla6_size();
4282b382b191SThomas Graf }
4283b382b191SThomas Graf 
4284b382b191SThomas Graf static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
4285b382b191SThomas Graf {
4286b382b191SThomas Graf 	struct inet6_dev *idev = __in6_dev_get(dev);
4287b382b191SThomas Graf 
4288b382b191SThomas Graf 	if (!idev)
4289b382b191SThomas Graf 		return -ENODATA;
4290b382b191SThomas Graf 
4291b382b191SThomas Graf 	if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4292b382b191SThomas Graf 		return -EMSGSIZE;
4293b382b191SThomas Graf 
4294b382b191SThomas Graf 	return 0;
4295b382b191SThomas Graf }
4296b382b191SThomas Graf 
4297f53adae4SDaniel Borkmann static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
4298f53adae4SDaniel Borkmann {
4299f53adae4SDaniel Borkmann 	struct inet6_ifaddr *ifp;
4300f53adae4SDaniel Borkmann 	struct net_device *dev = idev->dev;
4301*fc403832SDaniel Borkmann 	bool update_rs = false;
4302f53adae4SDaniel Borkmann 
4303f53adae4SDaniel Borkmann 	if (token == NULL)
4304f53adae4SDaniel Borkmann 		return -EINVAL;
4305f53adae4SDaniel Borkmann 	if (ipv6_addr_any(token))
4306f53adae4SDaniel Borkmann 		return -EINVAL;
4307f53adae4SDaniel Borkmann 	if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
4308f53adae4SDaniel Borkmann 		return -EINVAL;
4309f53adae4SDaniel Borkmann 	if (!ipv6_accept_ra(idev))
4310f53adae4SDaniel Borkmann 		return -EINVAL;
4311f53adae4SDaniel Borkmann 	if (idev->cnf.rtr_solicits <= 0)
4312f53adae4SDaniel Borkmann 		return -EINVAL;
4313f53adae4SDaniel Borkmann 
4314f53adae4SDaniel Borkmann 	write_lock_bh(&idev->lock);
4315f53adae4SDaniel Borkmann 
4316f53adae4SDaniel Borkmann 	BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
4317f53adae4SDaniel Borkmann 	memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
4318f53adae4SDaniel Borkmann 
4319f53adae4SDaniel Borkmann 	write_unlock_bh(&idev->lock);
4320f53adae4SDaniel Borkmann 
4321*fc403832SDaniel Borkmann 	if (!idev->dead && (idev->if_flags & IF_READY)) {
4322*fc403832SDaniel Borkmann 		struct in6_addr ll_addr;
4323*fc403832SDaniel Borkmann 
4324*fc403832SDaniel Borkmann 		ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
4325*fc403832SDaniel Borkmann 				IFA_F_OPTIMISTIC);
4326*fc403832SDaniel Borkmann 
4327*fc403832SDaniel Borkmann 		/* If we're not ready, then normal ifup will take care
4328*fc403832SDaniel Borkmann 		 * of this. Otherwise, we need to request our rs here.
4329*fc403832SDaniel Borkmann 		 */
4330f53adae4SDaniel Borkmann 		ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
4331*fc403832SDaniel Borkmann 		update_rs = true;
4332*fc403832SDaniel Borkmann 	}
4333f53adae4SDaniel Borkmann 
4334f53adae4SDaniel Borkmann 	write_lock_bh(&idev->lock);
4335*fc403832SDaniel Borkmann 
4336*fc403832SDaniel Borkmann 	if (update_rs)
4337f53adae4SDaniel Borkmann 		idev->if_flags |= IF_RS_SENT;
4338f53adae4SDaniel Borkmann 
4339f53adae4SDaniel Borkmann 	/* Well, that's kinda nasty ... */
4340f53adae4SDaniel Borkmann 	list_for_each_entry(ifp, &idev->addr_list, if_list) {
4341f53adae4SDaniel Borkmann 		spin_lock(&ifp->lock);
4342f53adae4SDaniel Borkmann 		if (ipv6_addr_src_scope(&ifp->addr) ==
4343f53adae4SDaniel Borkmann 		    IPV6_ADDR_SCOPE_GLOBAL) {
4344f53adae4SDaniel Borkmann 			ifp->valid_lft = 0;
4345f53adae4SDaniel Borkmann 			ifp->prefered_lft = 0;
4346f53adae4SDaniel Borkmann 		}
4347f53adae4SDaniel Borkmann 		spin_unlock(&ifp->lock);
4348f53adae4SDaniel Borkmann 	}
4349f53adae4SDaniel Borkmann 
4350f53adae4SDaniel Borkmann 	write_unlock_bh(&idev->lock);
4351f53adae4SDaniel Borkmann 	return 0;
4352f53adae4SDaniel Borkmann }
4353f53adae4SDaniel Borkmann 
4354f53adae4SDaniel Borkmann static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
4355f53adae4SDaniel Borkmann {
4356f53adae4SDaniel Borkmann 	int err = -EINVAL;
4357f53adae4SDaniel Borkmann 	struct inet6_dev *idev = __in6_dev_get(dev);
4358f53adae4SDaniel Borkmann 	struct nlattr *tb[IFLA_INET6_MAX + 1];
4359f53adae4SDaniel Borkmann 
4360f53adae4SDaniel Borkmann 	if (!idev)
4361f53adae4SDaniel Borkmann 		return -EAFNOSUPPORT;
4362f53adae4SDaniel Borkmann 
4363f53adae4SDaniel Borkmann 	if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
4364f53adae4SDaniel Borkmann 		BUG();
4365f53adae4SDaniel Borkmann 
4366f53adae4SDaniel Borkmann 	if (tb[IFLA_INET6_TOKEN])
4367f53adae4SDaniel Borkmann 		err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
4368f53adae4SDaniel Borkmann 
4369f53adae4SDaniel Borkmann 	return err;
4370f53adae4SDaniel Borkmann }
4371f53adae4SDaniel Borkmann 
4372b382b191SThomas Graf static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
437315e47304SEric W. Biederman 			     u32 portid, u32 seq, int event, unsigned int flags)
4374b382b191SThomas Graf {
4375b382b191SThomas Graf 	struct net_device *dev = idev->dev;
4376b382b191SThomas Graf 	struct ifinfomsg *hdr;
4377b382b191SThomas Graf 	struct nlmsghdr *nlh;
4378b382b191SThomas Graf 	void *protoinfo;
4379b382b191SThomas Graf 
438015e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
4381b382b191SThomas Graf 	if (nlh == NULL)
4382b382b191SThomas Graf 		return -EMSGSIZE;
4383b382b191SThomas Graf 
4384b382b191SThomas Graf 	hdr = nlmsg_data(nlh);
4385b382b191SThomas Graf 	hdr->ifi_family = AF_INET6;
4386b382b191SThomas Graf 	hdr->__ifi_pad = 0;
4387b382b191SThomas Graf 	hdr->ifi_type = dev->type;
4388b382b191SThomas Graf 	hdr->ifi_index = dev->ifindex;
4389b382b191SThomas Graf 	hdr->ifi_flags = dev_get_flags(dev);
4390b382b191SThomas Graf 	hdr->ifi_change = 0;
4391b382b191SThomas Graf 
4392c78679e8SDavid S. Miller 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4393c78679e8SDavid S. Miller 	    (dev->addr_len &&
4394c78679e8SDavid S. Miller 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4395c78679e8SDavid S. Miller 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4396c78679e8SDavid S. Miller 	    (dev->ifindex != dev->iflink &&
4397c78679e8SDavid S. Miller 	     nla_put_u32(skb, IFLA_LINK, dev->iflink)))
4398c78679e8SDavid S. Miller 		goto nla_put_failure;
4399b382b191SThomas Graf 	protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
4400b382b191SThomas Graf 	if (protoinfo == NULL)
4401b382b191SThomas Graf 		goto nla_put_failure;
4402b382b191SThomas Graf 
4403b382b191SThomas Graf 	if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4404b382b191SThomas Graf 		goto nla_put_failure;
4405b382b191SThomas Graf 
440604561c1fSThomas Graf 	nla_nest_end(skb, protoinfo);
440704561c1fSThomas Graf 	return nlmsg_end(skb, nlh);
44081da177e4SLinus Torvalds 
440904561c1fSThomas Graf nla_put_failure:
441026932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
441126932566SPatrick McHardy 	return -EMSGSIZE;
44121da177e4SLinus Torvalds }
44131da177e4SLinus Torvalds 
44141da177e4SLinus Torvalds static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
44151da177e4SLinus Torvalds {
44163b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(skb->sk);
441784d2697dSEric Dumazet 	int h, s_h;
4418434a8a58SDavid S. Miller 	int idx = 0, s_idx;
44191da177e4SLinus Torvalds 	struct net_device *dev;
44201da177e4SLinus Torvalds 	struct inet6_dev *idev;
442184d2697dSEric Dumazet 	struct hlist_head *head;
44221da177e4SLinus Torvalds 
442384d2697dSEric Dumazet 	s_h = cb->args[0];
442484d2697dSEric Dumazet 	s_idx = cb->args[1];
442584d2697dSEric Dumazet 
442684d2697dSEric Dumazet 	rcu_read_lock();
442784d2697dSEric Dumazet 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
44287562f876SPavel Emelianov 		idx = 0;
442984d2697dSEric Dumazet 		head = &net->dev_index_head[h];
4430b67bfe0dSSasha Levin 		hlist_for_each_entry_rcu(dev, head, index_hlist) {
44311da177e4SLinus Torvalds 			if (idx < s_idx)
44327562f876SPavel Emelianov 				goto cont;
443384d2697dSEric Dumazet 			idev = __in6_dev_get(dev);
443484d2697dSEric Dumazet 			if (!idev)
44357562f876SPavel Emelianov 				goto cont;
443684d2697dSEric Dumazet 			if (inet6_fill_ifinfo(skb, idev,
443715e47304SEric W. Biederman 					      NETLINK_CB(cb->skb).portid,
443884d2697dSEric Dumazet 					      cb->nlh->nlmsg_seq,
443984d2697dSEric Dumazet 					      RTM_NEWLINK, NLM_F_MULTI) <= 0)
444084d2697dSEric Dumazet 				goto out;
44417562f876SPavel Emelianov cont:
44427562f876SPavel Emelianov 			idx++;
44431da177e4SLinus Torvalds 		}
444484d2697dSEric Dumazet 	}
444584d2697dSEric Dumazet out:
444684d2697dSEric Dumazet 	rcu_read_unlock();
444784d2697dSEric Dumazet 	cb->args[1] = idx;
444884d2697dSEric Dumazet 	cb->args[0] = h;
44491da177e4SLinus Torvalds 
44501da177e4SLinus Torvalds 	return skb->len;
44511da177e4SLinus Torvalds }
44521da177e4SLinus Torvalds 
44531da177e4SLinus Torvalds void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
44541da177e4SLinus Torvalds {
44551da177e4SLinus Torvalds 	struct sk_buff *skb;
4456c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
44578d7a76c9SThomas Graf 	int err = -ENOBUFS;
44581da177e4SLinus Torvalds 
4459339bf98fSThomas Graf 	skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
44608d7a76c9SThomas Graf 	if (skb == NULL)
44618d7a76c9SThomas Graf 		goto errout;
44628d7a76c9SThomas Graf 
44638d7a76c9SThomas Graf 	err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
446426932566SPatrick McHardy 	if (err < 0) {
446526932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
446626932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
446726932566SPatrick McHardy 		kfree_skb(skb);
446826932566SPatrick McHardy 		goto errout;
446926932566SPatrick McHardy 	}
44705f75a104SNicolas Dichtel 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
44711ce85fe4SPablo Neira Ayuso 	return;
44728d7a76c9SThomas Graf errout:
44738d7a76c9SThomas Graf 	if (err < 0)
44745f75a104SNicolas Dichtel 		rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
44751da177e4SLinus Torvalds }
44761da177e4SLinus Torvalds 
4477339bf98fSThomas Graf static inline size_t inet6_prefix_nlmsg_size(void)
4478339bf98fSThomas Graf {
4479339bf98fSThomas Graf 	return NLMSG_ALIGN(sizeof(struct prefixmsg))
4480339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct in6_addr))
4481339bf98fSThomas Graf 	       + nla_total_size(sizeof(struct prefix_cacheinfo));
4482339bf98fSThomas Graf }
4483c5396a31SYOSHIFUJI Hideaki 
44841da177e4SLinus Torvalds static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
448515e47304SEric W. Biederman 			     struct prefix_info *pinfo, u32 portid, u32 seq,
4486b6544c0bSJamal Hadi Salim 			     int event, unsigned int flags)
44871da177e4SLinus Torvalds {
44881da177e4SLinus Torvalds 	struct prefixmsg *pmsg;
44891da177e4SLinus Torvalds 	struct nlmsghdr *nlh;
44901da177e4SLinus Torvalds 	struct prefix_cacheinfo	ci;
44911da177e4SLinus Torvalds 
449215e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
44936051e2f4SThomas Graf 	if (nlh == NULL)
449426932566SPatrick McHardy 		return -EMSGSIZE;
44956051e2f4SThomas Graf 
44966051e2f4SThomas Graf 	pmsg = nlmsg_data(nlh);
44971da177e4SLinus Torvalds 	pmsg->prefix_family = AF_INET6;
44988a47077aSPatrick McHardy 	pmsg->prefix_pad1 = 0;
44998a47077aSPatrick McHardy 	pmsg->prefix_pad2 = 0;
45001da177e4SLinus Torvalds 	pmsg->prefix_ifindex = idev->dev->ifindex;
45011da177e4SLinus Torvalds 	pmsg->prefix_len = pinfo->prefix_len;
45021da177e4SLinus Torvalds 	pmsg->prefix_type = pinfo->type;
45038a47077aSPatrick McHardy 	pmsg->prefix_pad3 = 0;
45041da177e4SLinus Torvalds 	pmsg->prefix_flags = 0;
45051da177e4SLinus Torvalds 	if (pinfo->onlink)
45061da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_ONLINK;
45071da177e4SLinus Torvalds 	if (pinfo->autoconf)
45081da177e4SLinus Torvalds 		pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
45091da177e4SLinus Torvalds 
4510c78679e8SDavid S. Miller 	if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
4511c78679e8SDavid S. Miller 		goto nla_put_failure;
45121da177e4SLinus Torvalds 	ci.preferred_time = ntohl(pinfo->prefered);
45131da177e4SLinus Torvalds 	ci.valid_time = ntohl(pinfo->valid);
4514c78679e8SDavid S. Miller 	if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
4515c78679e8SDavid S. Miller 		goto nla_put_failure;
45166051e2f4SThomas Graf 	return nlmsg_end(skb, nlh);
45171da177e4SLinus Torvalds 
45186051e2f4SThomas Graf nla_put_failure:
451926932566SPatrick McHardy 	nlmsg_cancel(skb, nlh);
452026932566SPatrick McHardy 	return -EMSGSIZE;
45211da177e4SLinus Torvalds }
45221da177e4SLinus Torvalds 
45231da177e4SLinus Torvalds static void inet6_prefix_notify(int event, struct inet6_dev *idev,
45241da177e4SLinus Torvalds 			 struct prefix_info *pinfo)
45251da177e4SLinus Torvalds {
45261da177e4SLinus Torvalds 	struct sk_buff *skb;
4527c346dca1SYOSHIFUJI Hideaki 	struct net *net = dev_net(idev->dev);
45288c384bfaSThomas Graf 	int err = -ENOBUFS;
45291da177e4SLinus Torvalds 
4530339bf98fSThomas Graf 	skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
45318c384bfaSThomas Graf 	if (skb == NULL)
45328c384bfaSThomas Graf 		goto errout;
45338c384bfaSThomas Graf 
45348c384bfaSThomas Graf 	err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
453526932566SPatrick McHardy 	if (err < 0) {
453626932566SPatrick McHardy 		/* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
453726932566SPatrick McHardy 		WARN_ON(err == -EMSGSIZE);
453826932566SPatrick McHardy 		kfree_skb(skb);
453926932566SPatrick McHardy 		goto errout;
454026932566SPatrick McHardy 	}
45411ce85fe4SPablo Neira Ayuso 	rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
45421ce85fe4SPablo Neira Ayuso 	return;
45438c384bfaSThomas Graf errout:
45448c384bfaSThomas Graf 	if (err < 0)
45456fda7350SBenjamin Thery 		rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
45461da177e4SLinus Torvalds }
45471da177e4SLinus Torvalds 
45481da177e4SLinus Torvalds static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
45491da177e4SLinus Torvalds {
455063998ac2SNicolas Dichtel 	struct net *net = dev_net(ifp->idev->dev);
455163998ac2SNicolas Dichtel 
45521da177e4SLinus Torvalds 	inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
45531da177e4SLinus Torvalds 
45541da177e4SLinus Torvalds 	switch (event) {
45551da177e4SLinus Torvalds 	case RTM_NEWADDR:
455695c385b4SNeil Horman 		/*
455795c385b4SNeil Horman 		 * If the address was optimistic
455895c385b4SNeil Horman 		 * we inserted the route at the start of
455995c385b4SNeil Horman 		 * our DAD process, so we don't need
456095c385b4SNeil Horman 		 * to do it again
456195c385b4SNeil Horman 		 */
456295c385b4SNeil Horman 		if (!(ifp->rt->rt6i_node))
456340e22e8fSThomas Graf 			ip6_ins_rt(ifp->rt);
45641da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
45651da177e4SLinus Torvalds 			addrconf_join_anycast(ifp);
45661da177e4SLinus Torvalds 		break;
45671da177e4SLinus Torvalds 	case RTM_DELADDR:
45681da177e4SLinus Torvalds 		if (ifp->idev->cnf.forwarding)
45691da177e4SLinus Torvalds 			addrconf_leave_anycast(ifp);
45701da177e4SLinus Torvalds 		addrconf_leave_solict(ifp->idev, &ifp->addr);
4571d8d1f30bSChangli Gao 		dst_hold(&ifp->rt->dst);
457293fa159aSstephen hemminger 
457373a8bd74SDavid S. Miller 		if (ip6_del_rt(ifp->rt))
4574d8d1f30bSChangli Gao 			dst_free(&ifp->rt->dst);
45751da177e4SLinus Torvalds 		break;
45761da177e4SLinus Torvalds 	}
457763998ac2SNicolas Dichtel 	atomic_inc(&net->ipv6.dev_addr_genid);
45781da177e4SLinus Torvalds }
45791da177e4SLinus Torvalds 
45801da177e4SLinus Torvalds static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
45811da177e4SLinus Torvalds {
45828814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock_bh();
45831da177e4SLinus Torvalds 	if (likely(ifp->idev->dead == 0))
45841da177e4SLinus Torvalds 		__ipv6_ifa_notify(event, ifp);
45858814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock_bh();
45861da177e4SLinus Torvalds }
45871da177e4SLinus Torvalds 
45881da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
45891da177e4SLinus Torvalds 
45901da177e4SLinus Torvalds static
45918d65af78SAlexey Dobriyan int addrconf_sysctl_forward(ctl_table *ctl, int write,
45921da177e4SLinus Torvalds 			   void __user *buffer, size_t *lenp, loff_t *ppos)
45931da177e4SLinus Torvalds {
45941da177e4SLinus Torvalds 	int *valp = ctl->data;
45951da177e4SLinus Torvalds 	int val = *valp;
459688af182eSEric W. Biederman 	loff_t pos = *ppos;
4597013d97e9SFrancesco Ruggeri 	ctl_table lctl;
45981da177e4SLinus Torvalds 	int ret;
45991da177e4SLinus Torvalds 
4600013d97e9SFrancesco Ruggeri 	/*
4601013d97e9SFrancesco Ruggeri 	 * ctl->data points to idev->cnf.forwarding, we should
4602013d97e9SFrancesco Ruggeri 	 * not modify it until we get the rtnl lock.
4603013d97e9SFrancesco Ruggeri 	 */
4604013d97e9SFrancesco Ruggeri 	lctl = *ctl;
4605013d97e9SFrancesco Ruggeri 	lctl.data = &val;
4606013d97e9SFrancesco Ruggeri 
4607013d97e9SFrancesco Ruggeri 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
46081da177e4SLinus Torvalds 
4609c8fecf22SPavel Emelyanov 	if (write)
4610b325fddbSStephen Hemminger 		ret = addrconf_fixup_forwarding(ctl, valp, val);
461188af182eSEric W. Biederman 	if (ret)
461288af182eSEric W. Biederman 		*ppos = pos;
46131da177e4SLinus Torvalds 	return ret;
46141da177e4SLinus Torvalds }
46151da177e4SLinus Torvalds 
461656d417b1SBrian Haley static void dev_disable_change(struct inet6_dev *idev)
461756d417b1SBrian Haley {
461856d417b1SBrian Haley 	if (!idev || !idev->dev)
461956d417b1SBrian Haley 		return;
462056d417b1SBrian Haley 
462156d417b1SBrian Haley 	if (idev->cnf.disable_ipv6)
462256d417b1SBrian Haley 		addrconf_notify(NULL, NETDEV_DOWN, idev->dev);
462356d417b1SBrian Haley 	else
462456d417b1SBrian Haley 		addrconf_notify(NULL, NETDEV_UP, idev->dev);
462556d417b1SBrian Haley }
462656d417b1SBrian Haley 
462756d417b1SBrian Haley static void addrconf_disable_change(struct net *net, __s32 newf)
462856d417b1SBrian Haley {
462956d417b1SBrian Haley 	struct net_device *dev;
463056d417b1SBrian Haley 	struct inet6_dev *idev;
463156d417b1SBrian Haley 
463256d417b1SBrian Haley 	rcu_read_lock();
4633c6d14c84SEric Dumazet 	for_each_netdev_rcu(net, dev) {
463456d417b1SBrian Haley 		idev = __in6_dev_get(dev);
463556d417b1SBrian Haley 		if (idev) {
463656d417b1SBrian Haley 			int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
463756d417b1SBrian Haley 			idev->cnf.disable_ipv6 = newf;
463856d417b1SBrian Haley 			if (changed)
463956d417b1SBrian Haley 				dev_disable_change(idev);
464056d417b1SBrian Haley 		}
464156d417b1SBrian Haley 	}
4642c6d14c84SEric Dumazet 	rcu_read_unlock();
464356d417b1SBrian Haley }
464456d417b1SBrian Haley 
4645013d97e9SFrancesco Ruggeri static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
464656d417b1SBrian Haley {
464756d417b1SBrian Haley 	struct net *net;
4648013d97e9SFrancesco Ruggeri 	int old;
4649013d97e9SFrancesco Ruggeri 
4650013d97e9SFrancesco Ruggeri 	if (!rtnl_trylock())
4651013d97e9SFrancesco Ruggeri 		return restart_syscall();
465256d417b1SBrian Haley 
465356d417b1SBrian Haley 	net = (struct net *)table->extra2;
4654013d97e9SFrancesco Ruggeri 	old = *p;
4655013d97e9SFrancesco Ruggeri 	*p = newf;
465656d417b1SBrian Haley 
4657013d97e9SFrancesco Ruggeri 	if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
4658013d97e9SFrancesco Ruggeri 		rtnl_unlock();
465956d417b1SBrian Haley 		return 0;
466088af182eSEric W. Biederman 	}
466156d417b1SBrian Haley 
466256d417b1SBrian Haley 	if (p == &net->ipv6.devconf_all->disable_ipv6) {
466356d417b1SBrian Haley 		net->ipv6.devconf_dflt->disable_ipv6 = newf;
466456d417b1SBrian Haley 		addrconf_disable_change(net, newf);
4665013d97e9SFrancesco Ruggeri 	} else if ((!newf) ^ (!old))
466656d417b1SBrian Haley 		dev_disable_change((struct inet6_dev *)table->extra1);
466756d417b1SBrian Haley 
466856d417b1SBrian Haley 	rtnl_unlock();
466956d417b1SBrian Haley 	return 0;
467056d417b1SBrian Haley }
467156d417b1SBrian Haley 
467256d417b1SBrian Haley static
46738d65af78SAlexey Dobriyan int addrconf_sysctl_disable(ctl_table *ctl, int write,
467456d417b1SBrian Haley 			    void __user *buffer, size_t *lenp, loff_t *ppos)
467556d417b1SBrian Haley {
467656d417b1SBrian Haley 	int *valp = ctl->data;
467756d417b1SBrian Haley 	int val = *valp;
467888af182eSEric W. Biederman 	loff_t pos = *ppos;
4679013d97e9SFrancesco Ruggeri 	ctl_table lctl;
468056d417b1SBrian Haley 	int ret;
468156d417b1SBrian Haley 
4682013d97e9SFrancesco Ruggeri 	/*
4683013d97e9SFrancesco Ruggeri 	 * ctl->data points to idev->cnf.disable_ipv6, we should
4684013d97e9SFrancesco Ruggeri 	 * not modify it until we get the rtnl lock.
4685013d97e9SFrancesco Ruggeri 	 */
4686013d97e9SFrancesco Ruggeri 	lctl = *ctl;
4687013d97e9SFrancesco Ruggeri 	lctl.data = &val;
4688013d97e9SFrancesco Ruggeri 
4689013d97e9SFrancesco Ruggeri 	ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
469056d417b1SBrian Haley 
469156d417b1SBrian Haley 	if (write)
469256d417b1SBrian Haley 		ret = addrconf_disable_ipv6(ctl, valp, val);
469388af182eSEric W. Biederman 	if (ret)
469488af182eSEric W. Biederman 		*ppos = pos;
469556d417b1SBrian Haley 	return ret;
469656d417b1SBrian Haley }
469756d417b1SBrian Haley 
46981da177e4SLinus Torvalds static struct addrconf_sysctl_table
46991da177e4SLinus Torvalds {
47001da177e4SLinus Torvalds 	struct ctl_table_header *sysctl_header;
4701f6a07b29SYOSHIFUJI Hideaki 	ctl_table addrconf_vars[DEVCONF_MAX+1];
4702ab32ea5dSBrian Haley } addrconf_sysctl __read_mostly = {
47031da177e4SLinus Torvalds 	.sysctl_header = NULL,
47041da177e4SLinus Torvalds 	.addrconf_vars = {
47051da177e4SLinus Torvalds 		{
47061da177e4SLinus Torvalds 			.procname	= "forwarding",
47071da177e4SLinus Torvalds 			.data		= &ipv6_devconf.forwarding,
47081da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
47091da177e4SLinus Torvalds 			.mode		= 0644,
47106d9f239aSAlexey Dobriyan 			.proc_handler	= addrconf_sysctl_forward,
47111da177e4SLinus Torvalds 		},
47121da177e4SLinus Torvalds 		{
47131da177e4SLinus Torvalds 			.procname	= "hop_limit",
47141da177e4SLinus Torvalds 			.data		= &ipv6_devconf.hop_limit,
47151da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
47161da177e4SLinus Torvalds 			.mode		= 0644,
47171da177e4SLinus Torvalds 			.proc_handler	= proc_dointvec,
47181da177e4SLinus Torvalds 		},
47191da177e4SLinus Torvalds 		{
47201da177e4SLinus Torvalds 			.procname	= "mtu",
47211da177e4SLinus Torvalds 			.data		= &ipv6_devconf.mtu6,
47221da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
47231da177e4SLinus Torvalds 			.mode		= 0644,
47246d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
47251da177e4SLinus Torvalds 		},
47261da177e4SLinus Torvalds 		{
47271da177e4SLinus Torvalds 			.procname	= "accept_ra",
47281da177e4SLinus Torvalds 			.data		= &ipv6_devconf.accept_ra,
47291da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
47301da177e4SLinus Torvalds 			.mode		= 0644,
47316d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
47321da177e4SLinus Torvalds 		},
47331da177e4SLinus Torvalds 		{
47341da177e4SLinus Torvalds 			.procname	= "accept_redirects",
47351da177e4SLinus Torvalds 			.data		= &ipv6_devconf.accept_redirects,
47361da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
47371da177e4SLinus Torvalds 			.mode		= 0644,
47386d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
47391da177e4SLinus Torvalds 		},
47401da177e4SLinus Torvalds 		{
47411da177e4SLinus Torvalds 			.procname	= "autoconf",
47421da177e4SLinus Torvalds 			.data		= &ipv6_devconf.autoconf,
47431da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
47441da177e4SLinus Torvalds 			.mode		= 0644,
47456d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
47461da177e4SLinus Torvalds 		},
47471da177e4SLinus Torvalds 		{
47481da177e4SLinus Torvalds 			.procname	= "dad_transmits",
47491da177e4SLinus Torvalds 			.data		= &ipv6_devconf.dad_transmits,
47501da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
47511da177e4SLinus Torvalds 			.mode		= 0644,
47526d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
47531da177e4SLinus Torvalds 		},
47541da177e4SLinus Torvalds 		{
47551da177e4SLinus Torvalds 			.procname	= "router_solicitations",
47561da177e4SLinus Torvalds 			.data		= &ipv6_devconf.rtr_solicits,
47571da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
47581da177e4SLinus Torvalds 			.mode		= 0644,
47596d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
47601da177e4SLinus Torvalds 		},
47611da177e4SLinus Torvalds 		{
47621da177e4SLinus Torvalds 			.procname	= "router_solicitation_interval",
47631da177e4SLinus Torvalds 			.data		= &ipv6_devconf.rtr_solicit_interval,
47641da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
47651da177e4SLinus Torvalds 			.mode		= 0644,
47666d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
47671da177e4SLinus Torvalds 		},
47681da177e4SLinus Torvalds 		{
47691da177e4SLinus Torvalds 			.procname	= "router_solicitation_delay",
47701da177e4SLinus Torvalds 			.data		= &ipv6_devconf.rtr_solicit_delay,
47711da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
47721da177e4SLinus Torvalds 			.mode		= 0644,
47736d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
47741da177e4SLinus Torvalds 		},
47751da177e4SLinus Torvalds 		{
47761da177e4SLinus Torvalds 			.procname	= "force_mld_version",
47771da177e4SLinus Torvalds 			.data		= &ipv6_devconf.force_mld_version,
47781da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
47791da177e4SLinus Torvalds 			.mode		= 0644,
47806d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
47811da177e4SLinus Torvalds 		},
47821da177e4SLinus Torvalds #ifdef CONFIG_IPV6_PRIVACY
47831da177e4SLinus Torvalds 		{
47841da177e4SLinus Torvalds 			.procname	= "use_tempaddr",
47851da177e4SLinus Torvalds 			.data		= &ipv6_devconf.use_tempaddr,
47861da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
47871da177e4SLinus Torvalds 			.mode		= 0644,
47886d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
47891da177e4SLinus Torvalds 		},
47901da177e4SLinus Torvalds 		{
47911da177e4SLinus Torvalds 			.procname	= "temp_valid_lft",
47921da177e4SLinus Torvalds 			.data		= &ipv6_devconf.temp_valid_lft,
47931da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
47941da177e4SLinus Torvalds 			.mode		= 0644,
47956d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
47961da177e4SLinus Torvalds 		},
47971da177e4SLinus Torvalds 		{
47981da177e4SLinus Torvalds 			.procname	= "temp_prefered_lft",
47991da177e4SLinus Torvalds 			.data		= &ipv6_devconf.temp_prefered_lft,
48001da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
48011da177e4SLinus Torvalds 			.mode		= 0644,
48026d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
48031da177e4SLinus Torvalds 		},
48041da177e4SLinus Torvalds 		{
48051da177e4SLinus Torvalds 			.procname	= "regen_max_retry",
48061da177e4SLinus Torvalds 			.data		= &ipv6_devconf.regen_max_retry,
48071da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
48081da177e4SLinus Torvalds 			.mode		= 0644,
48096d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
48101da177e4SLinus Torvalds 		},
48111da177e4SLinus Torvalds 		{
48121da177e4SLinus Torvalds 			.procname	= "max_desync_factor",
48131da177e4SLinus Torvalds 			.data		= &ipv6_devconf.max_desync_factor,
48141da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
48151da177e4SLinus Torvalds 			.mode		= 0644,
48166d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
48171da177e4SLinus Torvalds 		},
48181da177e4SLinus Torvalds #endif
48191da177e4SLinus Torvalds 		{
48201da177e4SLinus Torvalds 			.procname	= "max_addresses",
48211da177e4SLinus Torvalds 			.data		= &ipv6_devconf.max_addresses,
48221da177e4SLinus Torvalds 			.maxlen		= sizeof(int),
48231da177e4SLinus Torvalds 			.mode		= 0644,
48246d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
48251da177e4SLinus Torvalds 		},
48261da177e4SLinus Torvalds 		{
482765f5c7c1SYOSHIFUJI Hideaki 			.procname	= "accept_ra_defrtr",
482865f5c7c1SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_ra_defrtr,
482965f5c7c1SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
483065f5c7c1SYOSHIFUJI Hideaki 			.mode		= 0644,
48316d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
483265f5c7c1SYOSHIFUJI Hideaki 		},
483365f5c7c1SYOSHIFUJI Hideaki 		{
4834c4fd30ebSYOSHIFUJI Hideaki 			.procname	= "accept_ra_pinfo",
4835c4fd30ebSYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_ra_pinfo,
4836c4fd30ebSYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
4837c4fd30ebSYOSHIFUJI Hideaki 			.mode		= 0644,
48386d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
4839c4fd30ebSYOSHIFUJI Hideaki 		},
4840930d6ff2SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_ROUTER_PREF
4841930d6ff2SYOSHIFUJI Hideaki 		{
4842930d6ff2SYOSHIFUJI Hideaki 			.procname	= "accept_ra_rtr_pref",
4843930d6ff2SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_ra_rtr_pref,
4844930d6ff2SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
4845930d6ff2SYOSHIFUJI Hideaki 			.mode		= 0644,
48466d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
4847930d6ff2SYOSHIFUJI Hideaki 		},
484852e16356SYOSHIFUJI Hideaki 		{
484952e16356SYOSHIFUJI Hideaki 			.procname	= "router_probe_interval",
485052e16356SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.rtr_probe_interval,
485152e16356SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
485252e16356SYOSHIFUJI Hideaki 			.mode		= 0644,
48536d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec_jiffies,
485452e16356SYOSHIFUJI Hideaki 		},
4855fa03ef38SNeil Horman #ifdef CONFIG_IPV6_ROUTE_INFO
485609c884d4SYOSHIFUJI Hideaki 		{
485709c884d4SYOSHIFUJI Hideaki 			.procname	= "accept_ra_rt_info_max_plen",
485809c884d4SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_ra_rt_info_max_plen,
485909c884d4SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
486009c884d4SYOSHIFUJI Hideaki 			.mode		= 0644,
48616d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
486209c884d4SYOSHIFUJI Hideaki 		},
486309c884d4SYOSHIFUJI Hideaki #endif
4864930d6ff2SYOSHIFUJI Hideaki #endif
4865c4fd30ebSYOSHIFUJI Hideaki 		{
4866fbea49e1SYOSHIFUJI Hideaki 			.procname	= "proxy_ndp",
4867fbea49e1SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.proxy_ndp,
4868fbea49e1SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
4869fbea49e1SYOSHIFUJI Hideaki 			.mode		= 0644,
48706d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
4871fbea49e1SYOSHIFUJI Hideaki 		},
4872fbea49e1SYOSHIFUJI Hideaki 		{
48730bcbc926SYOSHIFUJI Hideaki 			.procname	= "accept_source_route",
48740bcbc926SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_source_route,
48750bcbc926SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
48760bcbc926SYOSHIFUJI Hideaki 			.mode		= 0644,
48776d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
48780bcbc926SYOSHIFUJI Hideaki 		},
487995c385b4SNeil Horman #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
488095c385b4SNeil Horman 		{
488195c385b4SNeil Horman 			.procname       = "optimistic_dad",
488295c385b4SNeil Horman 			.data           = &ipv6_devconf.optimistic_dad,
488395c385b4SNeil Horman 			.maxlen         = sizeof(int),
488495c385b4SNeil Horman 			.mode           = 0644,
48856d9f239aSAlexey Dobriyan 			.proc_handler   = proc_dointvec,
488695c385b4SNeil Horman 
488795c385b4SNeil Horman 		},
488895c385b4SNeil Horman #endif
48897bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
48907bc570c8SYOSHIFUJI Hideaki 		{
48917bc570c8SYOSHIFUJI Hideaki 			.procname	= "mc_forwarding",
48927bc570c8SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.mc_forwarding,
48937bc570c8SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
4894a4e6db07SDavid S. Miller 			.mode		= 0444,
48956d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
48967bc570c8SYOSHIFUJI Hideaki 		},
48977bc570c8SYOSHIFUJI Hideaki #endif
48980bcbc926SYOSHIFUJI Hideaki 		{
4899778d80beSYOSHIFUJI Hideaki 			.procname	= "disable_ipv6",
4900778d80beSYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.disable_ipv6,
4901778d80beSYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
4902778d80beSYOSHIFUJI Hideaki 			.mode		= 0644,
490356d417b1SBrian Haley 			.proc_handler	= addrconf_sysctl_disable,
4904778d80beSYOSHIFUJI Hideaki 		},
4905778d80beSYOSHIFUJI Hideaki 		{
49061b34be74SYOSHIFUJI Hideaki 			.procname	= "accept_dad",
49071b34be74SYOSHIFUJI Hideaki 			.data		= &ipv6_devconf.accept_dad,
49081b34be74SYOSHIFUJI Hideaki 			.maxlen		= sizeof(int),
49091b34be74SYOSHIFUJI Hideaki 			.mode		= 0644,
49106d9f239aSAlexey Dobriyan 			.proc_handler	= proc_dointvec,
49111b34be74SYOSHIFUJI Hideaki 		},
49121b34be74SYOSHIFUJI Hideaki 		{
4913f7734fdfSOctavian Purdila 			.procname       = "force_tllao",
4914f7734fdfSOctavian Purdila 			.data           = &ipv6_devconf.force_tllao,
4915f7734fdfSOctavian Purdila 			.maxlen         = sizeof(int),
4916f7734fdfSOctavian Purdila 			.mode           = 0644,
4917f7734fdfSOctavian Purdila 			.proc_handler   = proc_dointvec
4918f7734fdfSOctavian Purdila 		},
4919f7734fdfSOctavian Purdila 		{
49205cb04436SHannes Frederic Sowa 			.procname       = "ndisc_notify",
49215cb04436SHannes Frederic Sowa 			.data           = &ipv6_devconf.ndisc_notify,
49225cb04436SHannes Frederic Sowa 			.maxlen         = sizeof(int),
49235cb04436SHannes Frederic Sowa 			.mode           = 0644,
49245cb04436SHannes Frederic Sowa 			.proc_handler   = proc_dointvec
49255cb04436SHannes Frederic Sowa 		},
49265cb04436SHannes Frederic Sowa 		{
4927f8572d8fSEric W. Biederman 			/* sentinel */
49281da177e4SLinus Torvalds 		}
49291da177e4SLinus Torvalds 	},
49301da177e4SLinus Torvalds };
49311da177e4SLinus Torvalds 
4932bff16c2fSPavel Emelyanov static int __addrconf_sysctl_register(struct net *net, char *dev_name,
4933f8572d8fSEric W. Biederman 		struct inet6_dev *idev, struct ipv6_devconf *p)
49341da177e4SLinus Torvalds {
49351da177e4SLinus Torvalds 	int i;
49361da177e4SLinus Torvalds 	struct addrconf_sysctl_table *t;
49376105e293SEric W. Biederman 	char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
49381dab6222SPavel Emelyanov 
4939af879cc7SArnaldo Carvalho de Melo 	t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
49401da177e4SLinus Torvalds 	if (t == NULL)
4941f68635e6SPavel Emelyanov 		goto out;
4942f68635e6SPavel Emelyanov 
49431da177e4SLinus Torvalds 	for (i = 0; t->addrconf_vars[i].data; i++) {
49441da177e4SLinus Torvalds 		t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf;
49451da177e4SLinus Torvalds 		t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
4946bff16c2fSPavel Emelyanov 		t->addrconf_vars[i].extra2 = net;
49471da177e4SLinus Torvalds 	}
49481da177e4SLinus Torvalds 
49496105e293SEric W. Biederman 	snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
49501da177e4SLinus Torvalds 
49516105e293SEric W. Biederman 	t->sysctl_header = register_net_sysctl(net, path, t->addrconf_vars);
49521da177e4SLinus Torvalds 	if (t->sysctl_header == NULL)
49536105e293SEric W. Biederman 		goto free;
4954f68635e6SPavel Emelyanov 
49551da177e4SLinus Torvalds 	p->sysctl = t;
495695897312SPavel Emelyanov 	return 0;
49571da177e4SLinus Torvalds 
49581da177e4SLinus Torvalds free:
49591da177e4SLinus Torvalds 	kfree(t);
4960f68635e6SPavel Emelyanov out:
496195897312SPavel Emelyanov 	return -ENOBUFS;
49621da177e4SLinus Torvalds }
49631da177e4SLinus Torvalds 
4964408c4768SPavel Emelyanov static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
4965f52295a9SPavel Emelyanov {
4966408c4768SPavel Emelyanov 	struct addrconf_sysctl_table *t;
4967f52295a9SPavel Emelyanov 
4968408c4768SPavel Emelyanov 	if (p->sysctl == NULL)
4969408c4768SPavel Emelyanov 		return;
4970408c4768SPavel Emelyanov 
4971408c4768SPavel Emelyanov 	t = p->sysctl;
49721da177e4SLinus Torvalds 	p->sysctl = NULL;
4973ff538818SLucian Adrian Grijincu 	unregister_net_sysctl_table(t->sysctl_header);
49741da177e4SLinus Torvalds 	kfree(t);
49751da177e4SLinus Torvalds }
4976408c4768SPavel Emelyanov 
4977408c4768SPavel Emelyanov static void addrconf_sysctl_register(struct inet6_dev *idev)
4978408c4768SPavel Emelyanov {
497954716e3bSEric W. Biederman 	neigh_sysctl_register(idev->dev, idev->nd_parms, "ipv6",
4980f8572d8fSEric W. Biederman 			      &ndisc_ifinfo_sysctl_change);
4981c346dca1SYOSHIFUJI Hideaki 	__addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
4982f8572d8fSEric W. Biederman 					idev, &idev->cnf);
4983408c4768SPavel Emelyanov }
4984408c4768SPavel Emelyanov 
4985408c4768SPavel Emelyanov static void addrconf_sysctl_unregister(struct inet6_dev *idev)
4986408c4768SPavel Emelyanov {
4987408c4768SPavel Emelyanov 	__addrconf_sysctl_unregister(&idev->cnf);
4988408c4768SPavel Emelyanov 	neigh_sysctl_unregister(idev->nd_parms);
49891da177e4SLinus Torvalds }
49901da177e4SLinus Torvalds 
49911da177e4SLinus Torvalds 
49921da177e4SLinus Torvalds #endif
49931da177e4SLinus Torvalds 
49942c8c1e72SAlexey Dobriyan static int __net_init addrconf_init_net(struct net *net)
4995e0da5a48SPavel Emelyanov {
4996a79ca223SHong Zhiguo 	int err = -ENOMEM;
4997e0da5a48SPavel Emelyanov 	struct ipv6_devconf *all, *dflt;
4998e0da5a48SPavel Emelyanov 
4999a79ca223SHong Zhiguo 	all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
5000e0da5a48SPavel Emelyanov 	if (all == NULL)
5001e0da5a48SPavel Emelyanov 		goto err_alloc_all;
5002e0da5a48SPavel Emelyanov 
5003a79ca223SHong Zhiguo 	dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
5004e0da5a48SPavel Emelyanov 	if (dflt == NULL)
5005e0da5a48SPavel Emelyanov 		goto err_alloc_dflt;
5006a79ca223SHong Zhiguo 
500756d417b1SBrian Haley 	/* these will be inherited by all namespaces */
500856d417b1SBrian Haley 	dflt->autoconf = ipv6_defaults.autoconf;
500956d417b1SBrian Haley 	dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
5010e0da5a48SPavel Emelyanov 
5011e0da5a48SPavel Emelyanov 	net->ipv6.devconf_all = all;
5012e0da5a48SPavel Emelyanov 	net->ipv6.devconf_dflt = dflt;
5013e0da5a48SPavel Emelyanov 
5014e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
5015f8572d8fSEric W. Biederman 	err = __addrconf_sysctl_register(net, "all", NULL, all);
5016e0da5a48SPavel Emelyanov 	if (err < 0)
5017e0da5a48SPavel Emelyanov 		goto err_reg_all;
5018e0da5a48SPavel Emelyanov 
5019f8572d8fSEric W. Biederman 	err = __addrconf_sysctl_register(net, "default", NULL, dflt);
5020e0da5a48SPavel Emelyanov 	if (err < 0)
5021e0da5a48SPavel Emelyanov 		goto err_reg_dflt;
5022e0da5a48SPavel Emelyanov #endif
5023e0da5a48SPavel Emelyanov 	return 0;
5024e0da5a48SPavel Emelyanov 
5025e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
5026e0da5a48SPavel Emelyanov err_reg_dflt:
5027e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(all);
5028e0da5a48SPavel Emelyanov err_reg_all:
5029e0da5a48SPavel Emelyanov 	kfree(dflt);
5030e0da5a48SPavel Emelyanov #endif
5031e0da5a48SPavel Emelyanov err_alloc_dflt:
5032e0da5a48SPavel Emelyanov 	kfree(all);
5033e0da5a48SPavel Emelyanov err_alloc_all:
5034e0da5a48SPavel Emelyanov 	return err;
5035e0da5a48SPavel Emelyanov }
5036e0da5a48SPavel Emelyanov 
50372c8c1e72SAlexey Dobriyan static void __net_exit addrconf_exit_net(struct net *net)
5038e0da5a48SPavel Emelyanov {
5039e0da5a48SPavel Emelyanov #ifdef CONFIG_SYSCTL
5040e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
5041e0da5a48SPavel Emelyanov 	__addrconf_sysctl_unregister(net->ipv6.devconf_all);
5042e0da5a48SPavel Emelyanov #endif
504309ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net)) {
5044e0da5a48SPavel Emelyanov 		kfree(net->ipv6.devconf_dflt);
5045e0da5a48SPavel Emelyanov 		kfree(net->ipv6.devconf_all);
5046e0da5a48SPavel Emelyanov 	}
5047e0da5a48SPavel Emelyanov }
5048e0da5a48SPavel Emelyanov 
5049e0da5a48SPavel Emelyanov static struct pernet_operations addrconf_ops = {
5050e0da5a48SPavel Emelyanov 	.init = addrconf_init_net,
5051e0da5a48SPavel Emelyanov 	.exit = addrconf_exit_net,
5052e0da5a48SPavel Emelyanov };
5053e0da5a48SPavel Emelyanov 
50541da177e4SLinus Torvalds /*
50551da177e4SLinus Torvalds  *      Device notifier
50561da177e4SLinus Torvalds  */
50571da177e4SLinus Torvalds 
50581da177e4SLinus Torvalds int register_inet6addr_notifier(struct notifier_block *nb)
50591da177e4SLinus Torvalds {
5060e041c683SAlan Stern 	return atomic_notifier_chain_register(&inet6addr_chain, nb);
50611da177e4SLinus Torvalds }
50627159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(register_inet6addr_notifier);
50637159039aSYOSHIFUJI Hideaki 
50641da177e4SLinus Torvalds int unregister_inet6addr_notifier(struct notifier_block *nb)
50651da177e4SLinus Torvalds {
5066e041c683SAlan Stern 	return atomic_notifier_chain_unregister(&inet6addr_chain, nb);
50671da177e4SLinus Torvalds }
50687159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(unregister_inet6addr_notifier);
50697159039aSYOSHIFUJI Hideaki 
5070b382b191SThomas Graf static struct rtnl_af_ops inet6_ops = {
5071b382b191SThomas Graf 	.family		  = AF_INET6,
5072b382b191SThomas Graf 	.fill_link_af	  = inet6_fill_link_af,
5073b382b191SThomas Graf 	.get_link_af_size = inet6_get_link_af_size,
5074f53adae4SDaniel Borkmann 	.set_link_af	  = inet6_set_link_af,
5075b382b191SThomas Graf };
5076b382b191SThomas Graf 
50771da177e4SLinus Torvalds /*
50781da177e4SLinus Torvalds  *	Init / cleanup code
50791da177e4SLinus Torvalds  */
50801da177e4SLinus Torvalds 
50811da177e4SLinus Torvalds int __init addrconf_init(void)
50821da177e4SLinus Torvalds {
5083c2e21293Sstephen hemminger 	int i, err;
50842a8cc6c8SYOSHIFUJI Hideaki 
5085e21e8467SStephen Hemminger 	err = ipv6_addr_label_init();
5086e21e8467SStephen Hemminger 	if (err < 0) {
5087f3213831SJoe Perches 		pr_crit("%s: cannot initialize default policy table: %d\n",
5088f3213831SJoe Perches 			__func__, err);
50892cc6d2bfSNeil Horman 		goto out;
50902a8cc6c8SYOSHIFUJI Hideaki 	}
50911da177e4SLinus Torvalds 
50922cc6d2bfSNeil Horman 	err = register_pernet_subsys(&addrconf_ops);
50932cc6d2bfSNeil Horman 	if (err < 0)
50942cc6d2bfSNeil Horman 		goto out_addrlabel;
5095e0da5a48SPavel Emelyanov 
50961da177e4SLinus Torvalds 	/* The addrconf netdev notifier requires that loopback_dev
50971da177e4SLinus Torvalds 	 * has it's ipv6 private information allocated and setup
50981da177e4SLinus Torvalds 	 * before it can bring up and give link-local addresses
50991da177e4SLinus Torvalds 	 * to other devices which are up.
51001da177e4SLinus Torvalds 	 *
51011da177e4SLinus Torvalds 	 * Unfortunately, loopback_dev is not necessarily the first
51021da177e4SLinus Torvalds 	 * entry in the global dev_base list of net devices.  In fact,
51031da177e4SLinus Torvalds 	 * it is likely to be the very last entry on that list.
51041da177e4SLinus Torvalds 	 * So this causes the notifier registry below to try and
51051da177e4SLinus Torvalds 	 * give link-local addresses to all devices besides loopback_dev
51061da177e4SLinus Torvalds 	 * first, then loopback_dev, which cases all the non-loopback_dev
51071da177e4SLinus Torvalds 	 * devices to fail to get a link-local address.
51081da177e4SLinus Torvalds 	 *
51091da177e4SLinus Torvalds 	 * So, as a temporary fix, allocate the ipv6 structure for
51101da177e4SLinus Torvalds 	 * loopback_dev first by hand.
51111da177e4SLinus Torvalds 	 * Longer term, all of the dependencies ipv6 has upon the loopback
51121da177e4SLinus Torvalds 	 * device and it being up should be removed.
51131da177e4SLinus Torvalds 	 */
51141da177e4SLinus Torvalds 	rtnl_lock();
51152774c7abSEric W. Biederman 	if (!ipv6_add_dev(init_net.loopback_dev))
51161da177e4SLinus Torvalds 		err = -ENOMEM;
51171da177e4SLinus Torvalds 	rtnl_unlock();
51181da177e4SLinus Torvalds 	if (err)
5119e0da5a48SPavel Emelyanov 		goto errlo;
51201da177e4SLinus Torvalds 
5121c2e21293Sstephen hemminger 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
5122c2e21293Sstephen hemminger 		INIT_HLIST_HEAD(&inet6_addr_lst[i]);
5123c2e21293Sstephen hemminger 
51241da177e4SLinus Torvalds 	register_netdevice_notifier(&ipv6_dev_notf);
51251da177e4SLinus Torvalds 
51261da177e4SLinus Torvalds 	addrconf_verify(0);
5127c127ea2cSThomas Graf 
5128b382b191SThomas Graf 	err = rtnl_af_register(&inet6_ops);
5129b382b191SThomas Graf 	if (err < 0)
5130b382b191SThomas Graf 		goto errout_af;
5131b382b191SThomas Graf 
5132c7ac8679SGreg Rose 	err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
5133c7ac8679SGreg Rose 			      NULL);
5134c127ea2cSThomas Graf 	if (err < 0)
5135c127ea2cSThomas Graf 		goto errout;
5136c127ea2cSThomas Graf 
5137c127ea2cSThomas Graf 	/* Only the first call to __rtnl_register can fail */
5138c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
5139c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
5140c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
5141c7ac8679SGreg Rose 			inet6_dump_ifaddr, NULL);
5142c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
5143c7ac8679SGreg Rose 			inet6_dump_ifmcaddr, NULL);
5144c7ac8679SGreg Rose 	__rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
5145c7ac8679SGreg Rose 			inet6_dump_ifacaddr, NULL);
514676f8f6cbSNicolas Dichtel 	__rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
51477a674200SNicolas Dichtel 			inet6_netconf_dump_devconf, NULL);
5148c127ea2cSThomas Graf 
51492a8cc6c8SYOSHIFUJI Hideaki 	ipv6_addr_label_rtnl_register();
51502a8cc6c8SYOSHIFUJI Hideaki 
51511da177e4SLinus Torvalds 	return 0;
5152c127ea2cSThomas Graf errout:
5153b382b191SThomas Graf 	rtnl_af_unregister(&inet6_ops);
5154b382b191SThomas Graf errout_af:
5155c127ea2cSThomas Graf 	unregister_netdevice_notifier(&ipv6_dev_notf);
5156e0da5a48SPavel Emelyanov errlo:
5157e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
51582cc6d2bfSNeil Horman out_addrlabel:
51592cc6d2bfSNeil Horman 	ipv6_addr_label_cleanup();
51602cc6d2bfSNeil Horman out:
5161c127ea2cSThomas Graf 	return err;
51621da177e4SLinus Torvalds }
51631da177e4SLinus Torvalds 
516409f7709fSDaniel Lezcano void addrconf_cleanup(void)
51651da177e4SLinus Torvalds {
5166176c39afSDaniel Lezcano 	struct net_device *dev;
51671da177e4SLinus Torvalds 	int i;
51681da177e4SLinus Torvalds 
51691da177e4SLinus Torvalds 	unregister_netdevice_notifier(&ipv6_dev_notf);
5170e0da5a48SPavel Emelyanov 	unregister_pernet_subsys(&addrconf_ops);
51712cc6d2bfSNeil Horman 	ipv6_addr_label_cleanup();
51721da177e4SLinus Torvalds 
51731da177e4SLinus Torvalds 	rtnl_lock();
51741da177e4SLinus Torvalds 
5175b382b191SThomas Graf 	__rtnl_af_unregister(&inet6_ops);
5176b382b191SThomas Graf 
5177176c39afSDaniel Lezcano 	/* clean dev list */
5178176c39afSDaniel Lezcano 	for_each_netdev(&init_net, dev) {
5179176c39afSDaniel Lezcano 		if (__in6_dev_get(dev) == NULL)
5180176c39afSDaniel Lezcano 			continue;
5181176c39afSDaniel Lezcano 		addrconf_ifdown(dev, 1);
5182176c39afSDaniel Lezcano 	}
5183176c39afSDaniel Lezcano 	addrconf_ifdown(init_net.loopback_dev, 2);
5184176c39afSDaniel Lezcano 
51851da177e4SLinus Torvalds 	/*
51861da177e4SLinus Torvalds 	 *	Check hash table.
51871da177e4SLinus Torvalds 	 */
51885c578aedSstephen hemminger 	spin_lock_bh(&addrconf_hash_lock);
5189c2e21293Sstephen hemminger 	for (i = 0; i < IN6_ADDR_HSIZE; i++)
5190c2e21293Sstephen hemminger 		WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
51915c578aedSstephen hemminger 	spin_unlock_bh(&addrconf_hash_lock);
51921da177e4SLinus Torvalds 
51931da177e4SLinus Torvalds 	del_timer(&addr_chk_timer);
51941da177e4SLinus Torvalds 	rtnl_unlock();
51951da177e4SLinus Torvalds }
5196